-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
118 lines (97 loc) · 2.15 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"bufio"
"flag"
"fmt"
"os"
"time"
"github.com/veandco/go-sdl2/sdl"
)
var DebugMode bool
var ProgramPath string
var Tiled bool
func main() {
flag.BoolVar(&DebugMode, "debug", false, "Program enters into interactive debugger mode, wherein user can step through opcodes one at a time")
flag.StringVar(&ProgramPath, "prog", "", "Path to the chip8 machine code to emulate")
flag.BoolVar(&Tiled, "tiled", false, "Determines whether the chip8 display will include a border around pixels")
flag.Parse()
window, err := initSDLWindow()
if err != nil {
panic(err)
}
defer window.Destroy()
defer sdl.Quit()
graphics := NewGraphics(window)
chip := NewCHIP8(graphics)
chip.LoadProgram(ProgramPath) // should return err
if DebugMode {
debugLoop(window, chip)
} else {
mainLoop(window, chip)
}
}
func mainLoop(window *sdl.Window, chip *chip8) {
surface, err := window.GetSurface()
if err != nil {
panic(err)
}
surface.FillRect(nil, 0)
// controls emulation ticks
go func() {
ticker := time.NewTicker(time.Second / hz)
for {
<-ticker.C
chip.tick <- true
}
}()
// this handles main emulation logic
go chip.emulationLoop(window)
os.Exit(chip.SDLLoop())
}
func debugLoop(window *sdl.Window, chip *chip8) {
surface, err := window.GetSurface()
if err != nil {
panic(err)
}
surface.FillRect(nil, 0)
scanner := bufio.NewScanner(os.Stdin)
go func() {
for {
fmt.Print("[CH-I-P8]> ")
scanner.Scan()
input := scanner.Text()
chip.HandleDebugInput(input)
chip.graphics.Render()
}
}()
// this handles main emulation logic
go chip.emulationLoop(window)
os.Exit(chip.SDLLoop())
}
func (chip *chip8) emulationLoop(window *sdl.Window) {
for {
select {
case <-chip.sound:
break
case input := <-chip.input:
chip.HandleInput(input)
case <-chip.tick:
chip.EmulateNext()
}
}
}
func (chip *chip8) SDLLoop() int {
// SDL poll loop
running := true
for running {
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch event.(type) {
case *sdl.QuitEvent:
running = false
case *sdl.KeyboardEvent:
chip.input <- *event.(*sdl.KeyboardEvent)
}
}
}
return 0
}