-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
270 lines (221 loc) · 7.25 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package main
import (
"errors"
"flag"
"fmt"
"log"
"os"
"github.com/maxfierke/gogo-gb/cart"
"github.com/maxfierke/gogo-gb/cpu/isa"
"github.com/maxfierke/gogo-gb/debug"
"github.com/maxfierke/gogo-gb/devices"
"github.com/maxfierke/gogo-gb/hardware"
"github.com/maxfierke/gogo-gb/host"
)
type CLIOptions struct {
bootRomPath string
cartPath string
debugger string
debugPrint string
logPath string
logger *log.Logger
serialPort string
skipBootRom bool
ui bool
}
const LOG_PREFIX = ""
var DEFAULT_BOOT_ROM_PATHS = []string{
"gb_bios.bin",
"dmg_bios.bin",
"mgb_bios.bin",
"dmg0_bios.bin",
}
func main() {
options := CLIOptions{}
parseOptions(&options)
if options.logPath == "" || options.logPath == "stdout" {
options.logger = log.New(os.Stdout, LOG_PREFIX, log.LstdFlags)
} else if options.logPath == "stderr" {
options.logger = log.New(os.Stderr, LOG_PREFIX, log.LstdFlags)
} else {
logFile, err := os.Create(options.logPath)
if err != nil {
log.Fatalf("ERROR: Unable to open log file '%s' for writing: %v\n", options.logPath, err)
}
defer logFile.Close()
options.logger = log.New(logFile, LOG_PREFIX, log.LstdFlags)
}
if options.debugPrint != "" {
debugPrint(&options)
} else {
options.logger.Println("welcome to gogo-gb, the go-getting GB emulator")
if err := runCart(&options); err != nil {
options.logger.Fatalf("ERROR: %v\n", err)
}
}
}
func parseOptions(options *CLIOptions) {
flag.StringVar(&options.bootRomPath, "bootrom", "", "Path to boot ROM file (dmg_bios.bin, mgb_bios.bin, etc.). Defaults to a lookup on common boot ROM filenames in current directory")
flag.StringVar(&options.cartPath, "cart", "", "Path to cartridge file (.gb, .gbc)")
flag.StringVar(&options.debugger, "debugger", "none", "Specify debugger to use (\"none\", \"gameboy-doctor\", \"interactive\")")
flag.StringVar(&options.debugPrint, "debug-print", "", "Print out something for debugging purposes (\"cart-header\", \"opcodes\")")
flag.StringVar(&options.logPath, "log", "", "Path to log file. Default/empty implies stdout")
flag.StringVar(&options.serialPort, "serial-port", "", "Path to serial port IO (could be a file, UNIX socket, etc.)")
flag.BoolVar(&options.skipBootRom, "skip-bootrom", false, "Skip loading a boot ROM")
flag.BoolVar(&options.ui, "ui", false, "Launch with UI")
flag.Parse()
}
func debugPrint(options *CLIOptions) {
switch options.debugPrint {
case "cart-header":
debugPrintCartHeader(options)
case "opcodes":
debugPrintOpcodes(options)
default:
options.logger.Fatalf("ERROR: unrecognized \"debug-print\" option: %v\n", options.debugPrint)
}
}
func debugPrintCartHeader(options *CLIOptions) {
logger := options.logger
cartFile, err := os.Open(options.cartPath)
if options.cartPath == "" || err != nil {
logger.Fatalf("ERROR: Unable to load cartridge. Please ensure it's inserted correctly (exists): %v\n", err)
}
defer cartFile.Close()
cartReader, err := cart.NewReader(cartFile)
if errors.Is(err, cart.ErrHeader) {
logger.Printf("WARN: Cartridge header does not match expected checksum. Continuing, but subsequent operations may fail")
} else if err != nil {
logger.Fatalf("ERROR: Unable to load cartridge. Please ensure it's inserted correctly or trying blowing on it: %v\n", err)
}
cartReader.Header.DebugPrint(logger)
}
func debugPrintOpcodes(options *CLIOptions) {
logger := options.logger
opcodes, err := isa.LoadOpcodes()
if err != nil {
logger.Fatalf("ERROR: Unable to load opcodes: %v\n", err)
}
opcodes.DebugPrint(logger)
}
func initHost(options *CLIOptions) (host.Host, error) {
var hostDevice host.Host
if options.ui {
hostDevice = host.NewUIHost()
} else {
hostDevice = host.NewCLIHost()
}
hostDevice.SetLogger(options.logger)
if options.serialPort != "" {
serialCable := devices.NewHostSerialCable()
if options.serialPort == "stdout" || options.serialPort == "/dev/stdout" {
serialCable.SetWriter(os.Stdout)
} else if options.serialPort == "stderr" || options.serialPort == "/dev/stderr" {
serialCable.SetWriter(os.Stderr)
} else {
serialPort, err := os.Create(options.serialPort)
if err != nil {
return nil, fmt.Errorf("unable to open file '%s' as serial port: %w", options.serialPort, err)
}
serialCable.SetReader(serialPort)
serialCable.SetWriter(serialPort)
}
hostDevice.AttachSerialCable(serialCable)
}
return hostDevice, nil
}
func initDMG(options *CLIOptions) (*hardware.DMG, error) {
debugger, err := debug.NewDebugger(options.debugger)
if err != nil {
return nil, fmt.Errorf("unable to initialize Debugger: %w", err)
}
opts := []hardware.DMGOption{
hardware.WithDebugger(debugger),
}
if options.skipBootRom {
opts = append(opts, hardware.WithFakeBootROM())
} else {
bootRomFile, err := loadBootROM(options)
if err != nil {
return nil, fmt.Errorf("unable to load boot ROM: %w", err)
}
if bootRomFile == nil {
opts = append(opts, hardware.WithFakeBootROM())
} else {
defer bootRomFile.Close()
opts = append(opts, hardware.WithBootROM(bootRomFile))
}
}
dmg, err := hardware.NewDMG(opts...)
if err != nil {
return nil, fmt.Errorf("unable to initialize DMG: %w", err)
}
return dmg, nil
}
func loadBootROM(options *CLIOptions) (*os.File, error) {
logger := options.logger
bootRomPath := options.bootRomPath
var bootRomFile *os.File
var err error
if bootRomPath == "" {
for _, romPath := range DEFAULT_BOOT_ROM_PATHS {
if bootRomFile, err = os.Open(romPath); err != nil && !errors.Is(err, os.ErrNotExist) {
return nil, err
} else if bootRomFile != nil {
// yay! we found one!
break
}
}
} else if bootRomFile, err = os.Open(bootRomPath); err != nil {
return nil, err
}
if bootRomFile == nil {
// Bail out if no boot ROM loaded
logger.Printf("WARN: No boot ROM provided. Some emulation functionality may be incorrect.")
return nil, nil
}
logger.Printf("Loaded boot ROM: %s\n", bootRomFile.Name())
return bootRomFile, nil
}
func loadCart(dmg *hardware.DMG, options *CLIOptions) error {
if options.cartPath == "" {
return nil
}
logger := options.logger
cartFile, err := os.Open(options.cartPath)
if options.cartPath == "" || err != nil {
return fmt.Errorf("unable to load cartridge. Please ensure it's inserted correctly (e.g. file exists): %w", err)
}
defer cartFile.Close()
cartReader, err := cart.NewReader(cartFile)
if errors.Is(err, cart.ErrHeader) {
logger.Printf("WARN: Cartridge header does not match expected checksum. Continuing, but subsequent operations may fail")
} else if err != nil {
return fmt.Errorf("unable to load cartridge. Please ensure it's inserted correctly (e.g. file exists): %w", err)
}
err = dmg.LoadCartridge(cartReader)
if errors.Is(err, cart.ErrHeader) {
logger.Printf("WARN: Cartridge header does not match expected checksum. Continuing, but subsequent operations may fail")
} else if err != nil {
return fmt.Errorf("unable to load cartridge: %w", err)
}
return nil
}
func runCart(options *CLIOptions) error {
hostDevice, err := initHost(options)
if err != nil {
return fmt.Errorf("unable to initialize host device: %w", err)
}
dmg, err := initDMG(options)
if err != nil {
return err
}
if err := loadCart(dmg, options); err != nil {
return err
}
err = hostDevice.Run(dmg)
if err != nil {
return err
}
return nil
}