-
Notifications
You must be signed in to change notification settings - Fork 8
/
emulator.go
385 lines (326 loc) · 8.81 KB
/
emulator.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package main
import (
"fmt"
"io/ioutil"
"ndsemu/arm"
"ndsemu/e2d"
"ndsemu/emu"
"ndsemu/emu/debugger"
"ndsemu/emu/gfx"
log "ndsemu/emu/logger"
"ndsemu/raster3d"
"os"
"path/filepath"
"strconv"
"github.com/BurntSushi/toml"
)
type EmuMode int
const (
ModeNds EmuMode = 0
ModeGba EmuMode = 1
)
type NDSMemory struct {
Ram [4 * 1024 * 1024]byte // main RAM
Vram [656 * 1024]byte // video RAM
Wram [64 * 1024]byte // work RAM (nds7)
PaletteRam [2048]byte // pallette RAM
OamRam [2048]byte // object attribute RAM
}
type NDSRom struct {
Bios9 []byte
Bios7 []byte
BiosGba []byte
}
type NDSHardware struct {
E2d [2]*e2d.HwEngine2d
E3d *raster3d.HwEngine3d
Lcd9 *HwLcd
Lcd7 *HwLcd
Mc *HwMemoryController
Ipc *HwIpc
Div *HwDivisor
Rtc *HwRtc
Wifi *HwWifi
Spi *HwSpiBus
Gc *Gamecard
Ff *HwFirmwareFlash
Tsc *HwTouchScreen
Pow *HwPowerMan
Key *HwKey
Snd *HwSound
Geom *HwGeometry
Bkp *HwBackupRam
Sl2 *HwSlot2
}
type NDSEmulator struct {
Mem *NDSMemory
Rom *NDSRom
Hw *NDSHardware
Sync *emu.Sync
Mode EmuMode
dbg *debugger.Debugger
screen gfx.Buffer
audio []int16
framecount int
powcnt uint32
switchingToGba bool
}
var Emu *NDSEmulator
func NewNDSHardware(mem *NDSMemory, firmware string, dojit bool) *NDSHardware {
hw := new(NDSHardware)
bindir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
nds9 = NewNDS9(dojit)
nds7 = NewNDS7(dojit)
hw.Mc = NewMemoryController(nds9, nds7, mem.Vram[:])
hw.E3d = raster3d.NewHwEngine3d()
hw.E2d[0] = e2d.NewHwEngine2d(0, hw.Mc, gfx.LayerFunc{Func: hw.E3d.Draw3D})
hw.E2d[1] = e2d.NewHwEngine2d(1, hw.Mc, nil)
hw.Lcd9 = NewHwLcd(nds9.Irq, &NdsLcdConfig)
hw.Lcd7 = NewHwLcd(nds7.Irq, &NdsLcdConfig)
hw.Ipc = NewHwIpc(nds9.Irq, nds7.Irq)
hw.Div = NewHwDivisor()
hw.Rtc = NewHwRtc()
hw.Wifi = NewHwWifi()
hw.Bkp = NewHwBackupRam()
hw.Gc = NewGamecard(filepath.Join(bindir, "bios/biosnds7.rom"), hw.Bkp)
hw.Tsc = NewHwTouchScreen()
hw.Key = NewHwKey()
hw.Snd = NewHwSound(nds7.Bus)
hw.Geom = NewHwGeometry(nds9.Irq, hw.E3d)
hw.Sl2 = NewHwSlot2()
hw.Spi = NewHwSpiBus()
hw.Ff = NewHwFirmwareFlash()
hw.Pow = NewHwPowerMan()
hw.Spi.AddDevice(0, hw.Pow)
hw.Spi.AddDevice(1, hw.Ff)
hw.Spi.AddDevice(2, hw.Tsc)
// Pass bg scrolling regs to 3D engine for final 2D compositing pass
hw.E3d.SetBgRegs(&hw.E2d[0].DispCnt.Value,
&hw.E2d[0].Bg0Cnt.Value, &hw.E2d[0].Bg0XOfs.Value)
// FIXME: remove this hack once jit.Jit handles multicore
// with shared memory
if jit9 := nds9.Cpu.Jit(); jit9 != nil {
jit9.HACK_OtherJit = nds7.Cpu.Jit()
}
if jit7 := nds7.Cpu.Jit(); jit7 != nil {
jit7.HACK_OtherJit = nds9.Cpu.Jit()
}
return hw
}
func NewNDSRom() *NDSRom {
rom := new(NDSRom)
bindir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
bios9, err := ioutil.ReadFile(filepath.Join(bindir, "bios/biosnds9.rom"))
if err != nil {
log.ModEmu.FatalZ("error loading rom").Error("err", err).End()
}
rom.Bios9 = bios9
bios7, err := ioutil.ReadFile(filepath.Join(bindir, "bios/biosnds7.rom"))
if err != nil {
log.ModEmu.FatalZ("error loading rom").Error("err", err).End()
}
rom.Bios7 = bios7
biosgba, err := ioutil.ReadFile(filepath.Join(bindir, "bios/biosgba.rom"))
if err != nil {
log.ModEmu.WarnZ("error loading rom").Error("err", err).End()
}
rom.BiosGba = biosgba
return rom
}
func NewNDSEmulator(firmware string, dojit bool) *NDSEmulator {
mem := new(NDSMemory)
rom := NewNDSRom()
hw := NewNDSHardware(mem, firmware, dojit)
// Initialize syncing system
sync, err := emu.NewSync(NdsSyncConfig)
if err != nil {
panic(err)
}
sync.AddCpu(nds9, "arm9")
sync.AddCpu(nds7, "arm7")
sync.AddSubsystem(nds9.Timers, "timers9")
sync.AddSubsystem(nds7.Timers, "timers7")
sync.AddSubsystem(hw.Geom, "gx")
e := &NDSEmulator{
Mem: mem,
Hw: hw,
Rom: rom,
Sync: sync,
Mode: ModeNds,
}
// Set the hsync callback to this instance's function
NdsSyncConfig.HSync = e.hsync
// Register the syncer's logger as global logging function,
// so that everything will also log the current subsystem
// status (eg: CPU program counter)
log.AddContext(e.Sync)
emu.BreakFunc = e.DebugBreak
// Initialize the memory map and reset the CPUs
nds9.InitBus(e)
nds7.InitBus(e)
nds9.Reset()
nds7.Reset()
return e
}
func (emu *NDSEmulator) SwitchToGba() {
emu.switchingToGba = true
}
func (emu *NDSEmulator) switchToGba() {
// Create new sync with GBA timings and without ARM9
emu.Mode = ModeGba
nds7.InitBusGba(emu)
emu.Hw.Lcd7.Cfg = &GbaLcdConfig
// Reconfigure sync
emu.Sync.SetConfig(GbaSyncConfig)
GbaSyncConfig.HSync = emu.hsync
emu.Sync.Reset()
// Reconfigure graphic engine
mc := &GbaMemCnt{Bus: nds7.Bus}
emu.Hw.E2d[0].SetHwType(e2d.HwGba, mc)
emu.Hw.E2d[1].SetHwType(e2d.HwGba, mc)
// Release the halt line, to make the CPU restore execution
nds7.Cpu.SetLine(arm.LineHalt, false)
log.ModEmu.WarnZ("switched to GBA").End()
}
func (emu *NDSEmulator) StartDebugger() {
emu.dbg = debugger.New([]debugger.Cpu{nds7.Cpu, nds9.Cpu}, emu.Sync)
type DebugConfig struct {
Breakpoints []string
Watchpoints []string
}
cfg := &DebugConfig{}
if _, err := toml.DecodeFile("debug.ini", cfg); err != nil {
log.ModEmu.WithField("error", err).Warnf("error loading debug.ini")
} else {
for _, bkp := range cfg.Breakpoints {
if b, err := strconv.ParseUint(bkp, 0, 32); err != nil {
log.ModEmu.WithField("error", err).Fatalf("invalid breakpoint %q", bkp)
} else {
emu.dbg.AddBreakpoint(uint32(b))
log.ModEmu.WithField("break", fmt.Sprintf("0x%08x", uint32(b))).Warnf("add breakpoint")
}
}
for _, bkp := range cfg.Watchpoints {
if b, err := strconv.ParseUint(bkp, 0, 32); err != nil {
log.ModEmu.WithField("error", err).Fatalf("invalid watchpoint %q", bkp)
} else {
emu.dbg.AddWatchpoint(uint32(b))
log.ModEmu.WithField("watch", fmt.Sprintf("0x%08x", uint32(b))).Warnf("add watchpoint")
}
}
}
go emu.dbg.Run()
}
func (emu *NDSEmulator) DebugBreak(msg string) {
if emu.dbg != nil {
emu.dbg.Break(msg)
} else {
log.ModEmu.ErrorZ(msg).End()
log.ModEmu.PanicZ("debugging breakpoint, aborting").End()
}
}
func (emu *NDSEmulator) eaOn() bool { return emu.powcnt&(1<<1) != 0 }
func (emu *NDSEmulator) ebOn() bool { return emu.powcnt&(1<<9) != 0 }
func (emu *NDSEmulator) lcdSwapped() bool { return emu.powcnt&(1<<15) != 0 }
func (emu *NDSEmulator) hsync(x, y int) {
emu.Hw.Lcd9.SyncEvent(x, y)
emu.Hw.Lcd7.SyncEvent(x, y)
cfg := emu.Hw.Lcd7.Cfg
if y == 0 && x == 0 {
if emu.eaOn() {
emu.Hw.E2d[0].BeginFrame()
}
if emu.ebOn() {
emu.Hw.E2d[1].BeginFrame()
}
}
if y < cfg.VBlankFirstLine {
if x == 0 {
emu.beginLine(y)
} else if x == cfg.HBlankFirstDot {
emu.endLine(y)
// Trigger the DMA hblank event (only in visible part of screen)
if emu.Mode == ModeNds {
for _, dmach := range nds9.Dma {
dmach.TriggerEvent(DmaEventHBlank)
}
} else {
for _, dmach := range nds7.Dma {
dmach.TriggerEvent(DmaEventHBlank)
}
}
}
}
// Vblank
if y == cfg.VBlankFirstLine && x == 0 {
if emu.eaOn() {
emu.Hw.E2d[0].EndFrame()
}
if emu.ebOn() {
emu.Hw.E2d[1].EndFrame()
}
emu.Hw.E3d.EndFrame()
}
// 3D starts at scanline 214, before VBlank end. This is useful for us too, as we
// need some time to prepare the first line (computations, texture decompression, etc.)
if emu.Mode == ModeNds && y == 214 && x == 0 {
emu.Hw.E3d.SetVram(emu.Hw.Mc.VramTextureBank(), emu.Hw.Mc.VramTexturePaletteBank())
emu.Hw.E3d.BeginFrame()
}
// Per-line audio emulation
if x == 0 && emu.Hw.Pow.AudioEnabled() && emu.Mode == ModeNds {
nsamples := len(emu.audio) / 2
n0 := (nsamples * y)
n1 := (nsamples * (y + 1))
if emu.Mode == ModeNds {
n0 /= 263
n1 /= 263
} else {
n0 /= 228
n1 /= 228
}
emu.Hw.Snd.RunOneFrame(emu.audio[n0*2 : n1*2])
}
}
func (emu *NDSEmulator) RunOneFrame(screen gfx.Buffer, audio []int16) bool {
// Save powcnt for this frame; letting it change within a frame isn't
// really necessary and it's hard to handle with our parallel system
emu.powcnt = nds9.misc.PowCnt.Value
up, down := "B", "A"
if emu.lcdSwapped() {
up, down = down, up
}
log.ModGfx.InfoZ("begin frame").String("up", up).String("down", down).End()
emu.screen = screen
emu.audio = audio
emu.Sync.RunOneFrame()
emu.audio = nil
emu.framecount++
if emu.switchingToGba {
// Switching to Gba now (after frame end)
emu.switchToGba()
emu.switchingToGba = false
}
return emu.Hw.Pow.PowerOff()
}
func (emu *NDSEmulator) beginLine(y int) {
ya := y + 192 + 90
yb := y
if emu.lcdSwapped() {
ya, yb = yb, ya
}
if emu.eaOn() {
emu.Hw.E2d[0].BeginLine(y, emu.screen.Line(ya))
}
if emu.ebOn() {
emu.Hw.E2d[1].BeginLine(y, emu.screen.Line(yb))
}
}
func (emu *NDSEmulator) endLine(y int) {
if emu.eaOn() {
emu.Hw.E2d[0].EndLine(y)
}
if emu.ebOn() {
emu.Hw.E2d[1].EndLine(y)
}
}