-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
66 lines (47 loc) · 1.16 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"fmt"
"os"
"time"
"github.com/rbaron/awesomenes/awesomenes"
)
const (
FRAME_RATE = 60
FRAME_CYCLES = awesomenes.CPU_FREQ / FRAME_RATE
// All timings in nanoseconds
FRAME_TIME = 1e9 / float64(FRAME_RATE)
)
func main() {
tv := awesomenes.MakeTV()
if len(os.Args) != 2 {
fmt.Println("Usage: awesomenes ROM_PATH")
os.Exit(2)
}
rom := awesomenes.ReadROM(os.Args[1])
mapper := awesomenes.MakeMapper(rom)
ppu := awesomenes.MakePPU(nil, rom, mapper)
ppu.Reset()
controller := awesomenes.MakeController()
cpuAddrSpace := awesomenes.MakeCPUAddrSpace(rom, ppu, controller, mapper)
cpu := awesomenes.MakeCPU(cpuAddrSpace)
ppu.CPU = cpu
ppu.TV = tv
var cpuCycles, newCycles, ppuCycles int
t0 := time.Now().UnixNano()
for {
newCycles = cpu.Run()
cpuCycles += newCycles
for ppuCycles = 0; ppuCycles < 3*newCycles; ppuCycles++ {
ppu.TickScanline()
}
if cpuCycles > FRAME_CYCLES {
cpuCycles -= FRAME_CYCLES
tv.ShowPixels()
tv.UpdateInputState(controller)
if delta := FRAME_TIME - float64(time.Now().UnixNano()-t0); delta > 0 {
time.Sleep(time.Duration(delta))
}
t0 = time.Now().UnixNano()
}
}
}