-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·98 lines (77 loc) · 1.66 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"flag"
"fmt"
"log"
"os"
"time"
"github.com/danmrichards/chip8/internal/chip8"
"github.com/danmrichards/chip8/internal/event"
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
)
var (
vm *chip8.VM
rom string
debug bool
)
const cycleRate = 300
func main() {
log.SetFlags(log.LstdFlags)
flag.StringVar(&rom, "rom", "", "Path to the ROM file to load")
flag.BoolVar(&debug, "debug", false, "Run the emulator in debug mode")
flag.Parse()
// Validate the ROM flag.
if rom == "" {
fmt.Println("ROM flag is required")
os.Exit(1)
}
if _, err := os.Stat(rom); err != nil {
if os.IsNotExist(err) {
fmt.Printf("ROM %q does not exist", rom)
os.Exit(1)
} else {
log.Fatal(err)
}
}
pixelgl.Run(run)
}
func run() {
tick := time.NewTicker(time.Second / cycleRate)
defer tick.Stop()
cfg := pixelgl.WindowConfig{
Title: "chip8",
Bounds: pixel.R(0, 0, 1024, 768),
VSync: true,
}
window, err := pixelgl.NewWindow(cfg)
if err != nil {
log.Fatal("Could not create event:", err)
}
vm = chip8.New()
vm.Debug = debug
eh := event.NewHandler(window, vm)
rom, err := os.Open(rom)
if err != nil {
log.Fatalln("Could not open ROM:", err)
}
if err := vm.Load(rom); err != nil {
log.Fatal("Could not load ROM:", err)
}
// Handle input, screen and sound events.
go eh.Handle()
// Emulation loop.
for !window.Closed() {
window.UpdateInput()
if window.Pressed(pixelgl.KeyEscape) {
break
}
// Emulate a cycle.
if err = vm.Cycle(); err != nil {
log.Fatal(err)
}
// A bit dirty, but block the next cycle until a tick. This prevents
// the emulator from running too quickly.
<-tick.C
}
}