-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
135 lines (123 loc) · 2.86 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"time"
"github.com/mpingram/gameboy-emu/cpu"
frontend "github.com/mpingram/gameboy-emu/frontend/opengl"
"github.com/mpingram/gameboy-emu/mmu"
"github.com/mpingram/gameboy-emu/ppu"
)
func main() {
bootRomFileLocation := "./roms/boot/DMG_ROM.gb"
bootRom, err := os.Open(bootRomFileLocation)
if err != nil {
panic(err)
}
gameRomFileLocation := os.Args[1]
gameRom, err := os.Open(gameRomFileLocation)
if err != nil {
panic(err)
}
var breakpointEnabled bool
var breakpoint int64
if len(os.Args) > 2 {
breakpointInput := os.Args[2]
breakpoint, err = strconv.ParseInt(breakpointInput, 0, 0)
if err != nil {
if breakpointInput != "" {
fmt.Printf("ERR: Failed to parse breakpoint: %v\n", breakpointInput)
return
}
breakpointEnabled = false
} else {
breakpointEnabled = true
}
}
m := mmu.New(mmu.MMUOptions{BootRom: bootRom, GameRom: gameRom})
if err != nil {
panic(err)
}
p := ppu.New(m.PPUInterface)
c := cpu.New(m.CPUInterface)
cpuClock := time.NewTicker(time.Nanosecond)
defer cpuClock.Stop()
paused := false
// cpu goroutine
go func() {
var instr cpu.Instruction
for {
<-cpuClock.C
if paused {
fmt.Print("> ")
command := waitForInput()
if command == "p\n" || command == "print\n" {
fmt.Println(printCPUState(c))
} else if command == "m\n" || command == "memdump\n" {
memdump, err := os.Create("dumps/memdump.bin")
defer memdump.Close()
if err != nil {
panic(err)
}
// dump memory to file
m.Dump(memdump)
} else if command == "c\n" || command == "continue\n" {
paused = false
} else if command == "q\n" || command == "quit\n" {
break
} else {
pc := c.PC
instr, _ = c.Step()
fmt.Printf("($%04x)\t%s\n", pc, instr.String())
fmt.Printf("c.PC is now: %04x\n", c.PC)
}
} else {
pc := c.PC
instr, cycles := c.Step()
p.RunFor(cycles)
if breakpointEnabled && int64(pc) == breakpoint {
paused = true
fmt.Println("HALTED after executing")
fmt.Printf("($%04x)\t%s\n", pc, instr.String())
}
}
}
}()
frontend.ConnectVideo(p.VideoOut)
}
func waitForInput() string {
// block until the user types anything
reader := bufio.NewReader(os.Stdin)
text, err := reader.ReadString('\n')
if err != nil {
panic(err)
}
return text
}
func printCPUState(c *cpu.CPU) string {
return fmt.Sprintf(`
===== CPU =====
PC: %0x \t SP: %0x
A: %0x \t F: %0x
B: %0x \t C: %0x
D: %0x \t E: %0x
H: %0x \t L: %0x
F: %08b
`, c.PC, c.SP, c.A, c.F, c.B, c.C, c.D, c.E, c.H, c.L, c.F)
}
func placeholderScreen() []byte {
screen := make([]byte, 0)
var row, col byte
for row = 0; row < 144; row++ {
for col = 0; col < 160; col++ {
var r, g, b byte
r = row
g = col
b = byte(time.Now().Second() % 255)
screen = append(screen, r, g, b)
}
}
return screen
}