-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnes.go
332 lines (266 loc) · 8.05 KB
/
nes.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
package nes
import (
"bytes"
"fmt"
"log"
"math"
"github.com/cd1/nes-emulator/cpu"
"github.com/cd1/nes-emulator/parser"
)
const (
MemorySize = 65536
PRGROMStart = 0x8000
InitialStackAddress = 0x0100
ResetVectorAddress = 0xFFFC
)
type NES struct {
CPU CPU
Memory Memory
Verbose bool
}
func (nes *NES) Reset() uint8 {
nes.CPU.ProgramCounter = 0xC000 // nes.ReadWord(ResetVectorAddress)
nes.CPU.StackPointer = math.MaxUint8 - 2 // math.MaxUint8
nes.CPU.SetStatus(StatusInterrupt|StatusUnused, true)
nes.Memory = NewMemory(MemorySize)
return 7
}
func (nes *NES) Run(game Game) error {
resetCycles := nes.Reset()
totalCycles := uint64(resetCycles)
nes.loadGameInMemory(game)
disassembleConfig := parser.DisassembleConfig{
DisplayBytes: true,
DisplayMemoryAddress: true,
}
for {
op, err := parser.ConvertBinaryToOperation(bytes.NewReader(nes.Memory[nes.CPU.ProgramCounter:]))
if err != nil {
return err
}
if nes.Verbose {
var str bytes.Buffer
if err = parser.ConvertOperationToText(op, &str, disassembleConfig, nes.CPU.ProgramCounter, nes); err != nil {
return err
}
fmt.Printf("%-47v A:%02X X:%02X Y:%02X P:%02X SP:%02X PPU:%3v,%3v CYC:%v\n",
str.String(), nes.CPU.Accumulator, nes.CPU.IndexX, nes.CPU.IndexY, nes.CPU.Status, nes.CPU.StackPointer, -1, -1, totalCycles)
}
cycles, err := op.ExecuteIn(nes)
if err != nil {
return err
}
totalCycles += uint64(cycles)
}
return nil
}
func (nes *NES) loadGameInMemory(game Game) {
switch game.Header.PRGBankCount() {
case 1:
// load ROM twice, in 0x8000-0xBFFF and also in 0xC000-0xFFFF
copy(nes.Memory[PRGROMStart:PRGROMStart+PRGBankSize], game.PRG)
copy(nes.Memory[PRGROMStart+PRGBankSize:PRGROMStart+2*PRGBankSize], game.PRG)
case 2:
// load ROM in 0x8000-0xFFFF
copy(nes.Memory[PRGROMStart:PRGROMStart+2*PRGBankSize], game.PRG)
default:
log.Printf("unexpected PRG bank count (%v); ROM was not loaded into memory", game.Header.PRGBankCount())
}
}
func (nes *NES) GetAccumulator() uint8 {
return nes.CPU.Accumulator
}
func (nes *NES) SetAccumulator(value uint8) {
nes.CPU.Accumulator = value
}
func (nes *NES) GetIndexX() uint8 {
return nes.CPU.IndexX
}
func (nes *NES) SetIndexX(value uint8) {
nes.CPU.IndexX = value
}
func (nes *NES) GetIndexY() uint8 {
return nes.CPU.IndexY
}
func (nes *NES) SetIndexY(value uint8) {
nes.CPU.IndexY = value
}
func (nes *NES) GetProgramCounter() uint16 {
return nes.CPU.ProgramCounter
}
func (nes *NES) SetProgramCounter(value uint16) {
nes.CPU.ProgramCounter = value
}
func (nes *NES) GetStackPointer() uint8 {
return nes.CPU.StackPointer
}
func (nes *NES) SetStackPointer(value uint8) {
nes.CPU.StackPointer = value
}
func (nes *NES) GetStatus() uint8 {
return nes.CPU.Status
}
func (nes *NES) SetStatus(value uint8) {
// ignore StatusBreak from the received value
effectiveValue := value & ^StatusBreak | StatusUnused
nes.CPU.Status = effectiveValue
}
func (nes *NES) IsStatusBreak() bool {
return nes.CPU.GetStatus(StatusBreak)
}
func (nes *NES) SetStatusBreak(isSet bool) {
nes.CPU.SetStatus(StatusBreak, isSet)
}
func (nes *NES) IsStatusCarry() bool {
return nes.CPU.GetStatus(StatusCarry)
}
func (nes *NES) SetStatusCarry(isSet bool) {
nes.CPU.SetStatus(StatusCarry, isSet)
}
func (nes *NES) IsStatusDecimal() bool {
return nes.CPU.GetStatus(StatusDecimal)
}
func (nes *NES) SetStatusDecimal(isSet bool) {
nes.CPU.SetStatus(StatusDecimal, isSet)
}
func (nes *NES) IsStatusInterrupt() bool {
return nes.CPU.GetStatus(StatusInterrupt)
}
func (nes *NES) SetStatusInterrupt(isSet bool) {
nes.CPU.SetStatus(StatusInterrupt, isSet)
}
func (nes *NES) IsStatusNegative() bool {
return nes.CPU.GetStatus(StatusNegative)
}
func (nes *NES) SetStatusNegative(isSet bool) {
nes.CPU.SetStatus(StatusNegative, isSet)
}
func (nes *NES) IsStatusOverflow() bool {
return nes.CPU.GetStatus(StatusOverflow)
}
func (nes *NES) SetStatusOverflow(isSet bool) {
nes.CPU.SetStatus(StatusOverflow, isSet)
}
func (nes *NES) IsStatusUnused() bool {
return nes.CPU.GetStatus(StatusUnused)
}
func (nes *NES) SetStatusUnused(isSet bool) {
nes.CPU.SetStatus(StatusUnused, isSet)
}
func (nes *NES) IsStatusZero() bool {
return nes.CPU.GetStatus(StatusZero)
}
func (nes *NES) SetStatusZero(isSet bool) {
nes.CPU.SetStatus(StatusZero, isSet)
}
func mapMemoryAddress(address uint16) uint16 {
var newAddress uint16
if address < 0x2000 {
// (0x0800, 0x0FFF): mirror of (0x0000, 0x07FF)
// ... x1
// (0x1800, 0x1FFF): mirror of (0x0000, 0x07FF)
newAddress = address % 0x800
} else if address >= 0x2008 && address < 0x4000 {
// (0x2008, 0x200F): mirror of (0x2000, 0x2007)
// ... x22
// (0x3FF8, 0x3FFF): mirror of (0x2000, 0x2007)
newAddress = 0x2000 + (address-0x2000)%0x08
} else {
newAddress = address
}
if address != newAddress {
log.Printf("memory mapped from %04X -> %04X", address, newAddress)
}
return newAddress
}
func (nes *NES) ReadByte(address uint16) uint8 {
return nes.Memory.ReadByte(mapMemoryAddress(address))
}
func (nes *NES) WriteByte(address uint16, value uint8) {
nes.Memory.WriteByte(address, value)
}
func (nes *NES) ReadWord(address uint16) uint16 {
return nes.Memory.ReadWord(mapMemoryAddress(address))
}
func (nes *NES) ReadWordSamePage(address uint16) uint16 {
return nes.Memory.ReadWordSamePage(address)
}
func (nes *NES) WriteWord(address uint16, value uint16) {
nes.Memory.WriteWord(address, value)
}
func (nes *NES) PushByteToStack(value uint8) {
nes.WriteByte(InitialStackAddress+uint16(nes.CPU.StackPointer), value)
nes.CPU.StackPointer--
}
func (nes *NES) PushWordToStack(value uint16) {
nes.WriteWord(InitialStackAddress+uint16(nes.CPU.StackPointer-1), value)
nes.CPU.StackPointer -= 2
}
func (nes *NES) PullByteFromStack() uint8 {
nes.CPU.StackPointer++
return nes.ReadByte(InitialStackAddress + uint16(nes.CPU.StackPointer))
}
func (nes *NES) PullWordFromStack() uint16 {
value := nes.ReadWord(InitialStackAddress + uint16(nes.CPU.StackPointer+1))
nes.CPU.StackPointer += 2
return value
}
func (nes *NES) IncrementProgramCounter(value uint8) {
nes.CPU.ProgramCounter += uint16(int8(value))
}
func inSamePage(addr0 uint16, addr1 uint16) bool {
return addr0&0xFF00 == addr1&0xFF00
}
func (nes *NES) FetchOperand(op cpu.Operation) (uint16, uint8, bool) {
var address uint16
var operand uint8
var pageCrossed bool
switch op.AddressMode() {
case cpu.AddrModeAccumulator:
// operand not in memory
operand = nes.CPU.Accumulator
case cpu.AddrModeAbsolute:
address = op.WordArg()
operand = nes.ReadByte(address)
case cpu.AddrModeAbsoluteX:
address = op.WordArg() + uint16(nes.CPU.IndexX)
operand = nes.ReadByte(address)
pageCrossed = !inSamePage(op.WordArg(), address)
case cpu.AddrModeAbsoluteY:
address = op.WordArg() + uint16(nes.CPU.IndexY)
operand = nes.ReadByte(address)
pageCrossed = !inSamePage(op.WordArg(), address)
case cpu.AddrModeImmediate:
// operand not in memory
operand = op.ByteArg()
case cpu.AddrModeImplied:
// no operand
case cpu.AddrModeIndirect:
address = nes.ReadWordSamePage(op.WordArg())
operand = nes.ReadByte(address)
case cpu.AddrModeIndirectX:
address = nes.ReadWordSamePage(uint16(op.ByteArg() + nes.CPU.IndexX))
operand = nes.ReadByte(address)
case cpu.AddrModeIndirectY:
innerAddress := nes.ReadWordSamePage(uint16(op.ByteArg()))
address = innerAddress + uint16(nes.CPU.IndexY)
operand = nes.ReadByte(address)
pageCrossed = !inSamePage(innerAddress, address)
case cpu.AddrModeRelative:
// operand not in memory
operand = op.ByteArg()
pageCrossed = !inSamePage(nes.CPU.ProgramCounter+uint16(int8(op.Size()+operand)), nes.CPU.ProgramCounter+uint16(op.Size()))
case cpu.AddrModeZero:
address = uint16(op.ByteArg())
operand = nes.ReadByte(address)
case cpu.AddrModeZeroX:
address = uint16(op.ByteArg() + nes.CPU.IndexX)
operand = nes.ReadByte(address)
case cpu.AddrModeZeroY:
address = uint16(op.ByteArg() + nes.CPU.IndexY)
operand = nes.ReadByte(address)
default:
log.Printf("failed to fetch operand: invalid address mode (%v)", op.AddressMode())
}
return address, operand, pageCrossed
}