-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegmago.go
292 lines (244 loc) · 5.34 KB
/
segmago.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
package segmago
import (
"bytes"
"fmt"
"os"
)
type emuState struct {
CPU z80
Mem mem
Input Input
VDP vdp
SN76489 sn76489
ResetPressed bool
THAOutput bool
THBOutput bool
TRAOutput bool
TRBOutput bool
THAInOutputMode bool
THBInOutputMode bool
TRAInOutputMode bool
TRBInOutputMode bool
IsDomesticConsole bool
IoDisabled bool
IsGameGear bool
GameGearExtDataReg byte
GameGearExtDirReg byte
GameGearSerialSendReg byte
GameGearSerialCtrlReg byte
Cycles uint32
devMode bool
}
func (emu *emuState) InDevMode() bool { return emu.devMode }
func (emu *emuState) SetDevMode(b bool) { emu.devMode = b }
func (emu *emuState) devPrintln(args ...interface{}) {
if emu.devMode {
fmt.Println(args...)
}
}
func (emu *emuState) setMemControlReg(val byte) {
if emu.IsGameGear {
// TODO: support enabling the tiny bios in the first k?
} else {
if val&0x40 == 0 {
emu.devPrintln("set to cart storage")
emu.Mem.selectedMem = &emu.Mem.CartStorage
} else if val&0x08 == 0 {
emu.devPrintln("set to bios storage")
emu.Mem.selectedMem = &emu.Mem.BIOSStorage
} else {
emu.devPrintln("set to null storage")
emu.Mem.selectedMem = &emu.Mem.NullStorage
}
if val&0x04 == 0 {
emu.IoDisabled = false
emu.devPrintln("IO Enabled")
} else {
emu.IoDisabled = true
emu.devPrintln("IO Disabled")
}
}
}
func (emu *emuState) setIOControlReg(val byte) {
var THBInInputMode, TRBInInputMode bool
var THAInInputMode, TRAInInputMode bool
var THBOutputTry, TRBOutputTry bool
var THAOutputTry, TRAOutputTry bool
boolsFromByte(val,
&THBOutputTry,
&TRBOutputTry,
&THAOutputTry,
&TRAOutputTry,
&THBInInputMode,
&TRBInInputMode,
&THAInInputMode,
&TRAInInputMode,
)
if emu.THBInOutputMode {
emu.THBOutput = THBOutputTry
if emu.THBOutput {
emu.VDP.updateHCounter()
}
}
if emu.TRBInOutputMode {
emu.TRBOutput = TRBOutputTry
}
if emu.THAInOutputMode {
emu.THAOutput = THAOutputTry
if emu.THAOutput {
emu.VDP.updateHCounter()
}
}
if emu.TRAInOutputMode {
emu.TRAOutput = TRAOutputTry
}
emu.THBInOutputMode = !THBInInputMode
emu.TRBInOutputMode = !TRBInInputMode
emu.THAInOutputMode = !THAInInputMode
emu.TRAInOutputMode = !TRAInInputMode
}
func newState(cart, bios []byte, devMode bool) *emuState {
// strip a header that is only sometimes seen...
if len(cart)&0x3fff == 512 {
if devMode {
fmt.Println("found added header, stripping...")
}
cart = cart[512:]
}
state := emuState{}
state.Mem.init(cart, bios)
state.CPU.Read = state.read
state.CPU.Write = state.write
state.CPU.In = state.in
state.CPU.Out = state.out
state.CPU.RunCycles = state.runCycles
checkCart(cart)
state.Mem.RAM[0] = 0xab
for i := 1; i < len(state.Mem.RAM); i++ {
state.Mem.RAM[i] = 0xff
}
state.CPU.SP = 0xdfec
state.SN76489.init()
state.VDP.init(tvNTSC)
state.GameGearExtDataReg = 0x7f
state.GameGearExtDirReg = 0xff
state.GameGearSerialSendReg = 0x00
state.devMode = devMode
return &state
}
func checkCart(cart []byte) {
hdrLocs := []int{0x1ff0, 0x3ff0, 0x7ff0}
hdrStart := 0
for _, addr := range hdrLocs {
if len(cart) < addr+8 {
continue
}
magic := cart[addr : addr+8]
if bytes.Equal(magic, []byte("TMR SEGA")) {
hdrStart = addr
break
}
}
if hdrStart == 0 {
fmt.Println("info: no cart hdr found")
}
}
func (emu *emuState) runCycles(numCycles uint32) {
for i := uint32(0); i < numCycles; i++ {
emu.Cycles++
emu.VDP.runCycle()
emu.SN76489.runCycle()
}
emu.CPU.IRQ = (emu.VDP.LineInterruptEnable && emu.VDP.LineInterruptPending) ||
(emu.VDP.FrameInterruptEnable && emu.VDP.FrameInterruptPending)
}
// Input covers all outside info sent to the Emulator
type Input struct {
// Keys is a bool array of keydown state
Keys [256]bool
Joypad1 Joypad
Joypad2 Joypad
}
// Joypad contains gamepad state
type Joypad struct {
Up bool
Down bool
Left bool
Right bool
A bool
B bool
Fire bool // for lightgun
Start bool // for Game Gear
}
func (emu *emuState) readJoyReg0() byte {
return byteFromBools(
!emu.Input.Joypad2.Down,
!emu.Input.Joypad2.Up,
!emu.Input.Joypad1.B,
!emu.Input.Joypad1.A,
!emu.Input.Joypad1.Right,
!emu.Input.Joypad1.Left,
!emu.Input.Joypad1.Down,
!emu.Input.Joypad1.Up,
)
}
func (emu *emuState) readJoyReg1() byte {
thB := !emu.Input.Joypad2.Fire
thA := !emu.Input.Joypad1.Fire
// TODO: both export and domestic console differences
// (this is export mode, domestic returns 0x00)
if emu.THBInOutputMode {
if emu.IsDomesticConsole {
thB = false
} else { // export
thB = emu.THBOutput
}
}
if emu.THAInOutputMode {
if emu.IsDomesticConsole {
thB = false
} else { // export
thA = emu.THAOutput
}
}
return byteFromBools(
thB,
thA,
true,
!emu.ResetPressed,
!emu.Input.Joypad2.B,
!emu.Input.Joypad2.A,
!emu.Input.Joypad2.Right,
!emu.Input.Joypad2.Left,
)
}
var showDebugStatusLine = false
func (emu *emuState) step() {
if emu.Input.Keys['`'] {
showDebugStatusLine = !showDebugStatusLine
}
if showDebugStatusLine {
fmt.Println(emu.CPU.debugStatusLine())
}
emu.CPU.Step()
}
func errOut(v ...interface{}) {
fmt.Println(v...)
os.Exit(1)
}
func fatalErr(v ...interface{}) {
fmt.Println(v...)
panic("fatalErr()")
}
func assert(test bool, msg string) {
if !test {
fmt.Println(msg)
os.Exit(1)
}
}
func dieIf(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}