-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu.py
319 lines (255 loc) · 11.7 KB
/
cpu.py
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
import hardware
import romloader
import instructions
import ppu
import time
class cpu:
def __init__(self, nes):
self.nesSystem = nes
self.ppu = ppu.ppu(nes)
self.operand = 0
self.DEBUG = 0
#dict containing the 6502's opcodes. Lowercase, because hex in python is lower
self.opCodes = {
"0xa" : instructions.ASL,
"0x2c" : instructions.BIT,
"0x78" : instructions.SEI,
"0xd8" : instructions.CLD,
"0xa9" : instructions.LDA_Immediate,
"0xa5" : instructions.LDA_ZeroPage,
"0x5" : instructions.ORA_ZeroPage,
"0x9" : instructions.ORA_Immediate,
"0x10" : instructions.BPL,
"0x18" : instructions.CLC,
"0x8d" : instructions.STA_Absolute,
"0x20" : instructions.JSR,
"0x2a" : instructions.ROL_Accumulator,
"0x38" : instructions.SEC,
"0x3d" : instructions.AND_Absolute,
"0x40" : instructions.RTI,
"0x45" : instructions.EOR_ZeroPage,
"0x48" : instructions.PHA,
"0x4a" : instructions.LSR_Accumulator,
"0x4c" : instructions.JMP,
"0x29" : instructions.AND_Immediate,
"0xa8" : instructions.TAY,
"0xaa" : instructions.TAX,
"0xac" : instructions.LDY_Absolute,
"0xa2" : instructions.LDX_Immediate,
"0xae" : instructions.LDX_Absolute,
"0x8a" : instructions.TXA,
"0x9a" : instructions.TXS,
"0x99" : instructions.STA_AbsoluteY,
"0xad" : instructions.LDA_Absolute,
"0xa0" : instructions.LDY_Immediate,
"0xb1" : instructions.LDA_IndirectY,
"0xbd" : instructions.LDA_AbsoluteX,
"0xbe" : instructions.LDX_AbsoluteY,
"0xb9" : instructions.LDA_AbsoluteY,
"0xc9" : instructions.CMP,
"0xc6" : instructions.DEC_ZeroPage,
"0xb0" : instructions.BCS,
"0xca" : instructions.DEX,
"0xce" : instructions.DEC_Absolute,
"0xd0" : instructions.BNE,
"0x6c" : instructions.JMP_Indirect,
"0x6d" : instructions.ADC_Absolute,
"0x69" : instructions.ADC_Immediate,
"0x7e" : instructions.ROR_AbsoluteX,
"0x85" : instructions.STA_ZeroPage,
"0x86" : instructions.STX_ZeroPage,
"0x8c" : instructions.STY_Absolute,
"0xe0" : instructions.CPX,
"0xe8" : instructions.INX,
"0x90" : instructions.BCC,
"0x91" : instructions.STA_IndirectY,
"0x98" : instructions.TYA,
"0x9d" : instructions.STA_AbsoluteX,
"0x88" : instructions.DEY,
"0xc0" : instructions.CPY,
"0xc8" : instructions.INY,
"0xee" : instructions.INC,
"0xe6" : instructions.INC_ZeroPage,
"0x60" : instructions.RTS,
"0x65" : instructions.ADC_ZeroPage,
"0x68" : instructions.PLA,
"0xf0" : instructions.BEQ,
"0xf8" : instructions.SED,
"0xf9" : instructions.SBC_AbsoluteY,
}
def trace(self, currentOpCode, tempCounter, instrName):
print "PC:", hex(tempCounter), " Accumulator:", (self.nesSystem.cpu.accumulator), "Status:", self.nesSystem.cpu.status, "\n"
def getNextByte(self):
return self.readMemory(self.nesSystem.cpu.programCounter + 1)
def getNextWord(self):
byte1 = self.nesSystem.cpu.cpuMemory[self.nesSystem.cpu.programCounter+1]
byte2 = self.nesSystem.cpu.cpuMemory[self.nesSystem.cpu.programCounter+2]
word = byte2
word = word << 8
word += byte1
return word
def readPRG(self, address):
data = 0xFF
data = self.nesSystem.cpu.cpuMemory[address]
return data
def writePRG(self, address, data):
pass
def readMemory(self, address):
data = 0x00
if address < 0x2000:
if address < 0x800:
data = self.nesSystem.cpu.scratch1[address]
elif address < 0x1000:
pass
elif address < 0x1800:
pass
elif address < 0x6000:
registers = {
0x2002 : self.ppu.statusRegisterRead,
0x4016 : self.ppu.donothing,
0x4017 : self.ppu.donothing,
}
data = registers[address](self.nesSystem)
elif address < 0x8000:
pass
else:
data = self.readPRG(address)
return data
def readMemory16(self, address):
data1 = 0x00
data2 = 0x00
if address < 0x2000:
if address < 0x800:
data1 = self.nesSystem.cpu.scratch1[address]
data2 = self.nesSystem.cpu.scratch1[address + 1]
elif address < 0x1000:
data1 = self.nesSystem.cpu.scratch1[address - 0x800]
data2 = self.nesSystem.cpu.scratch1[address - 0x800 + 1]
elif address < 0x1800:
data1 = self.nesSystem.cpu.scratch1[address - 0x1000]
data2 = self.nesSystem.cpu.scratch1[address - 0x1000 + 1]
else:
data1 = self.nesSystem.cpu.scratch1[address - 0x1800]
data2 = self.nesSystem.cpu.scratch1[address - 0x1800 + 1]
elif address < 0x8000:
pass
else:
data1 = self.readPRG(address)
data2 = self.readPRG(address + 1)
data = (data2 << 8) + data1
return data
def writeMemory(self, address, data):
if address < 0x800:
self.nesSystem.cpu.scratch1[address] = data;
elif address < 0x1000:
pass
elif address < 0x1800:
pass
elif address < 0x2000:
pass
elif address < 0x4000:
registers = {
0x2000 : self.ppu.controlRegister1Write,
0x2001 : self.ppu.controlRegister2Write,
0x2003 : self.ppu.spriteAddressRegisterWrite,
0x2005 : self.ppu.vRamRegister1Write,
0x2006 : self.ppu.vRamRegister2Write,
0x2007 : self.ppu.ppuDataRegisterWrite
}
registers[address](self.nesSystem, data)
elif address < 0x6000:
pass
elif address < 0x8000:
pass
else:
self.writePRG(address, data)
def memoryInit(self):
for index, x in enumerate(self.nesSystem.rom.prgData):
self.nesSystem.cpu.cpuMemory[index + 0x8000] = self.nesSystem.rom.prgData[index]
for index, x in enumerate(self.nesSystem.rom.chrData):
self.nesSystem.ppu.ppuMemory[index] = self.nesSystem.rom.chrData[index]
def cpuInit(self):
#PC = byte at 0xFFFD * 256 + byte at 0xFFFC
self.nesSystem.cpu.programCounter = (self.nesSystem.cpu.cpuMemory[0xFFFD] * 256) + self.nesSystem.cpu.cpuMemory[0xFFFC]
self.nesSystem.cpu.stackP = 0
self.nesSystem.cpu.accumulator = 0
self.nesSystem.cpu.registerX = 0
self.nesSystem.cpu.registerY = 0
self.nesSystem.cpu.status = [0, 0, 0, 0, 0, 0, 0, 0]
def pushByte8(self, byte):
self.writeMemory((0x100 + self.nesSystem.cpu.stackP), (byte & 255))
self.nesSystem.cpu.stackP -= 1
def pushByte16(self, byte):
self.pushByte8(byte >> 8)
self.pushByte8(byte & 255)
def pullByte8(self):
self.nesSystem.cpu.stackP += 1
return self.readMemory(0x100 + self.nesSystem.cpu.stackP)
def pullByte16(self):
byte1 = self.pullByte8()
byte2 = self.pullByte8()
word = byte2
word = word << 8
word += byte1
return word
def pullStatus(self):
statusData = self.pullByte8()
if (statusData & 128) == 128:
self.nesSystem.cpu.status[instructions.N_F] = 1
else:
self.nesSystem.cpu.status[instructions.N_F] = 0
if (statusData & 64) == 64:
self.nesSystem.cpu.status[instructions.V_F] = 1
else:
self.nesSystem.cpu.status[instructions.V_F] = 0
if (statusData & 16) == 16:
self.nesSystem.cpu.status[instructions.B_F] = 1
else:
self.nesSystem.cpu.status[instructions.B_F] = 0
if (statusData & 8) == 8:
self.nesSystem.cpu.status[instructions.D_F] = 1
else:
self.nesSystem.cpu.status[instructions.D_F] = 0
if (statusData & 4) == 4:
self.nesSystem.cpu.status[instructions.I_F] = 1
else:
self.nesSystem.cpu.status[instructions.I_F] = 0
if (statusData & 2) == 2:
self.nesSystem.cpu.status[instructions.Z_F] = 1
else:
self.nesSystem.cpu.status[instructions.Z_F] = 0
if (statusData & 1) == 1:
self.nesSystem.cpu.status[instructions.C_F] = 1
else:
self.nesSystem.cpu.status[instructions.C_F] = 0
def pushStatus(self):
status = 0
if self.nesSystem.cpu.status[instructions.N_F] == 1:
status += 128
if self.nesSystem.cpu.status[instructions.V_F] == 1:
status += 64
if self.nesSystem.cpu.status[instructions.B_F] == 1:
status += 16
if self.nesSystem.cpu.status[instructions.D_F] == 1:
status += 8
if self.nesSystem.cpu.status[instructions.I_F] == 1:
status += 4
if self.nesSystem.cpu.status[instructions.Z_F] == 1:
status += 2
self.pushByte8(status)
def executeNMI(self):
self.pushPC()
self.pushStatus()
self.nesSystem.cpu.programCounter = self.nesSystem.cpu.cpuMemory[0xFFFA]
def executeOpCode(self):
#print hex(self.nesSystem.cpu.programCounter)#, "RegX", hex(self.nesSystem.cpu.registerX), "RegY", hex(self.nesSystem.cpu.registerY), "StackP", hex(self.nesSystem.cpu.stackP)
#print self.nesSystem.cpu.accumulator
currentOpCode = hex(self.readMemory(self.nesSystem.cpu.programCounter))
tempCounter = self.nesSystem.cpu.programCounter
#execute the instruction corresponding with the opcode signature
cyclesTaken = self.opCodes[currentOpCode](self.nesSystem, self)
# if self.nesSystem.cpu.programCounter == 36395:
# x = raw_input()
if self.DEBUG:
self.trace(currentOpCode, tempCounter, self.opCodes[currentOpCode].__name__)
return cyclesTaken