-
Notifications
You must be signed in to change notification settings - Fork 0
/
nes_cpu_instructions.py
395 lines (329 loc) · 12.1 KB
/
nes_cpu_instructions.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
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
386
387
388
389
390
391
392
393
394
395
import nes_memory
import struct
from nes_byte_utils import *
import logging
class Instructions:
cpu = None
memory = None
def __init__(self, cpu):
self.cpu = cpu
self.memory = self.cpu.memory
loggingi = 0
def logOpcode(self, message):
pc = self.cpu.get_program_counter()
if (pc <= 0xc007): return
if (self.loggingi > 19000 and self.loggingi < 25000 ):
logging.info ('pc:'+self.cpu.get_program_counter_as_str()+' x:'+str(self.cpu.x_register)+' y:'+str(self.cpu.y_register)+' acc:'+str(self.cpu.accumulator)+' zero:'+str(self.cpu.zero_flag)+' carry:'+str(self.cpu.carry_flag)+' negative:'+str(self.cpu.negative_flag)+' m:'+message) #+' cycles:'+str(self.cpu.total_cycles)
self.loggingi += 1
#####################################
# Register opcodes
#####################################
def execute_cld(self, addr):
self.cpu.decimal_flag = 0
def execute_clc(self, addr):
self.cpu.carry_flag = 0
# compare the accumulator to the memory at addr
def execute_cmp(self, addr):
memory_bytes = byte_to_signed_int(self.cpu.memory.read_byte_from_memory(addr))
difference_between_acc_and_memory = self.cpu.accumulator - memory_bytes
self.setZeroFlagForValue(difference_between_acc_and_memory)
self.setNegativeFlagForValue(difference_between_acc_and_memory)
self.setCarryFlagForValue( negative_byte_wrap(difference_between_acc_and_memory) )
# compare the x register to the memory at addr
def execute_cpx(self, addr):
memory_bytes = byte_to_signed_int(self.cpu.memory.read_byte_from_memory(addr))
difference_between_x_and_memory = self.cpu.x_register - memory_bytes
self.setZeroFlagForValue(difference_between_x_and_memory)
self.setNegativeFlagForValue(difference_between_x_and_memory)
self.setCarryFlagForValue( negative_byte_wrap(difference_between_x_and_memory) )
def execute_sei(self, addr):
self.cpu.interrupt_flag = 1
#####################################
# Storage opcodes
#####################################
def execute_lda(self, addr):
self.logOpcode('Before LDA:'+str(addr))
value = self.cpu.memory.read_byte_from_memory(addr)
self.cpu.accumulator = byte_to_signed_int(value)
self.setZeroFlagForValue(value)
self.setNegativeFlagForValue(value)
self.logOpcode(str(value)+'After LDA\n')
def execute_ldx(self, addr):
self.logOpcode('Before LDX')
value = self.cpu.x_register = byte_to_signed_int(self.cpu.memory.read_byte_from_memory(addr))
self.setZeroFlagForValue(value)
self.setNegativeFlagForValue(value)
self.logOpcode('After LDX\n')
def execute_ldy(self, addr):
self.logOpcode('Before LDY')
value = self.cpu.y_register = byte_to_signed_int(self.cpu.memory.read_byte_from_memory(addr))
self.setZeroFlagForValue(value)
self.setNegativeFlagForValue(value)
self.logOpcode('After LDY\n')
def execute_sta(self, addr):
self.logOpcode('Before STA:'+str(addr))
self.cpu.memory.write_int_to_memory(addr, self.cpu.accumulator)
self.logOpcode('After STA\n')
def execute_stx(self, addr):
self.logOpcode('Before STX:'+str(addr))
self.cpu.memory.write_int_to_memory(addr, self.cpu.x_register)
self.logOpcode('After STX\n')
def execute_sty(self, addr):
self.logOpcode('Before STY:'+str(addr))
self.cpu.memory.write_int_to_memory(addr, self.cpu.y_register)
self.logOpcode('After STY\n')
def execute_txs(self, addr):
value = self.cpu.stack_pointer = self.cpu.x_register#+0x0100
self.setZeroFlagForValue(value)
self.setNegativeFlagForValue(value)
print ('Transfer '+str(self.cpu.x_register)+' to stack pointer')
# Transfer X to accumulator
def execute_txa(self, addr):
value = self.cpu.accumulator = self.cpu.x_register
self.setZeroFlagForValue(value)
self.setNegativeFlagForValue(value)
# Transfer Accumulator to Y
def execute_tay(self, addr):
self.logOpcode('Before TAY')
value = self.cpu.y_register = self.cpu.accumulator
self.setZeroFlagForValue(value)
self.setNegativeFlagForValue(value)
self.logOpcode('After TAY\n')
# Transfer Accumulator to X
def execute_tax(self, addr):
self.logOpcode('Before TAX')
value = self.cpu.x_register = self.cpu.accumulator
self.setZeroFlagForValue(value)
self.setNegativeFlagForValue(value)
self.logOpcode('After TAX\n')
#####################################
# Math opcodes
#####################################
def execute_dex(self, addr):
self.logOpcode('Before DEX')
self.cpu.x_register -= 1
value = self.cpu.x_register
self.cpu.x_register = negative_byte_wrap(self.cpu.x_register)
self.setZeroFlagForValue(value)
self.setNegativeFlagForValue(value)
self.logOpcode('After DEX\n')
def execute_dey(self, addr):
self.logOpcode('Before DEY')
#value = self.cpu.y_register = (self.cpu.y_register-1)&0xFF
#this.F_SIGN = (this.REG_Y>>7)&1;
self.cpu.y_register -= 1
value = self.cpu.y_register &0xFF
self.cpu.y_register = value
#self.cpu.y_register = abs(value)#negative_byte_wrap(self.cpu.y_register)
self.setZeroFlagForValue(value)
self.setNegativeFlagForValue(value)
self.logOpcode('After DEY\n')
def execute_dec(self, addr):
value = byte_to_unsigned_int(self.cpu.memory.read_byte_from_memory(addr))
value -= 1
self.cpu.memory.write_int_to_memory(addr, negative_byte_wrap(value))
self.setZeroFlagForValue(value)
self.setNegativeFlagForValue(value)
def execute_inx(self, addr):
self.logOpcode('Before INX')
self.cpu.x_register += 1
value = self.cpu.x_register
self.cpu.x_register = negative_byte_wrap(self.cpu.x_register)
self.setZeroFlagForValue(value)
self.setNegativeFlagForValue(value)
self.logOpcode('After INX\n')
def execute_iny(self, addr):
self.logOpcode('Before INY')
self.cpu.y_register += 1
value = self.cpu.y_register
self.cpu.y_register = negative_byte_wrap(self.cpu.y_register)
self.setZeroFlagForValue(value)
self.setNegativeFlagForValue(value)
self.logOpcode('After INY\n')
def execute_adc(self, addr):
memory_value = byte_to_signed_int(self.cpu.memory.read_byte_from_memory(addr))
value = self.cpu.accumulator + memory_value + self.cpu.carry_flag
self.setZeroFlagForValue(value)
self.setNegativeFlagForValue(value)
self.setCarryFlagForValue(value)
self.setOverflowFlagForValue(self.cpu.accumulator, memory_value, value)
self.cpu.accumulator = value # need to set accumulator after set overflow
#####################################
# Branch opcodes
#####################################
# BPL: Branch on PLus (negative clear)
def execute_bpl(self, addr):
self.logOpcode('Before BPL')
if (self.cpu.negative_flag == 0):
self.cpu.program_counter = addr
self.logOpcode('After BPL\n')
# Branch on not zero
def execute_bne(self, addr):
self.logOpcode('Before BNE')
if (self.cpu.zero_flag == 0):
self.cpu.program_counter = addr
self.logOpcode('After BNE\n')
# Branch on equal to zero (zero flag set)
def execute_beq(self, addr):
self.logOpcode('Before BEQ')
if (self.cpu.zero_flag == 1):
self.cpu.program_counter = addr
self.logOpcode('After BEQ\n')
# Branch on carry set
def execute_bcs(self, addr):
self.logOpcode('Before BCS')
if (self.cpu.carry_flag == 1):
self.cpu.program_counter = addr
self.logOpcode('After BCS\n')
#####################################
# Jump opcodes
#####################################
def execute_jmp(self, addr):
self.logOpcode('Before JMP')
self.cpu.program_counter = addr
self.logOpcode('After JMP\n')
def execute_jsr(self, addr):
self.logOpcode('Before JSR')
self.push_short_to_stack(self.cpu.program_counter)
self.cpu.program_counter = addr
self.logOpcode('After JSR\n')
def execute_rts(self, addr):
self.logOpcode('Before RTS')
self.cpu.program_counter = self.pop_short_from_stack()
self.logOpcode('After RTS\n')
#####################################
# Bitwise opcodes
#####################################
def execute_and(self, addr):
value = self.cpu.accumulator & byte_to_signed_int(self.cpu.memory.read_byte_from_memory(addr))
self.cpu.accumulator = value
self.setZeroFlagForValue(value)
self.setNegativeFlagForValue(value)
def execute_asl(self, addr):
if (addr == -1):
value = self.cpu.accumulator
else:
value = byte_to_signed_int(self.cpu.memory.read_byte_from_memory(addr))
value = negative_byte_wrap(value << 1) #shift 1 bit left
self.setZeroFlagForValue(value)
self.setNegativeFlagForValue(value)
self.setCarryFlagForValue(value)
#write value back to either accumulator or memory
if (addr == -1):
self.cpu.accumulator = value
else:
self.cpu.memory.write_int_to_memory(addr, value)
#####################################
# Stack opcodes
#####################################
# Pull Accumulator from Stack
def execute_pla(self, addr):
value = self.pop_byte_from_stack()
self.setZeroFlagForValue(value)
self.setNegativeFlagForValue(value)
self.cpu.accumulator = value
def execute_instruction(self, opcode_name, addr):
#print ('execute: '+opcode_name)
#try:
if opcode_name in self.function_table:
self.function_table[opcode_name](self, addr)
else:
print ('Error at pc='+get_program_counter_as_str(self.cpu.program_counter))
print ('Missing instruction:'+opcode_name + ' '+str(addr))
raise KeyError
#except Exception as e:
# print (e)
# print ('execute: '+opcode_name)
return 2
####################################
# Stack handeling helper methods
#####################################
def push_short_to_stack(self, val):
self.cpu.stack_pointer -= 2 #stack starts at 0xFF and shrinks
addr = 0x0100 + self.cpu.stack_pointer
self.cpu.memory.write_short_to_memory(addr, val)
print ('pushed:'+str(val)+' to stack ( at location:'+str(addr)+' in mem)')
return
def pop_short_from_stack(self):
#get the value pointed at by the stack pointer then increment stack pointer
addr = 0x0100 + self.cpu.stack_pointer
value = self.cpu.memory.read_short_from_memory(addr)
self.cpu.stack_pointer += 2
print ('poped: '+str(value)+' from stack')
return value
def pop_byte_from_stack(self):
#get the value pointed at by the stack pointer then increment stack pointer back up
addr = 0x0100 + self.cpu.stack_pointer
value = self.cpu.memory.read_byte_from_memory(addr)
self.cpu.stack_pointer += 1
print ('poped: '+str(value)+' from stack')
return value
####################################
# Flag setting helper methods
#####################################
# Carry indicates unsigned overflow, so check if the value is > 255
def setCarryFlagForValue(self, value):
if (value > 255):
self.cpu.carry_flag = 1
else:
self.cpu.carry_flag = 0
# Overflow indicates signed overflow
def setOverflowFlagForValue(self, acc, mem, total):
if acc >0 and mem >0 and total < 0:
# positive and positive made negative so signed overflow
self.cpu.overflow_flag = 1
elif acc < 0 and mem < 0 and total > 0:
# negative and negative made a positive so signed overflow
self.cpu.overflow_flag = 1
else:
self.cpu.overflow_flag = 0
def setNegativeFlagForValue(self, value):
self.cpu.negative_flag = self.valueIsNegative(value)
def setZeroFlagForValue(self, value):
value = byte_to_signed_int(value)
if (value == 0):
self.cpu.zero_flag = 1
else:
self.cpu.zero_flag = 0
#
# valueIsNegative returns 1 or 0 depending on if the value is 2's compliment negative
# it moves the bits 7 positions to the right leaving just the last bit
# the last bit is returned as it controls whether the value is + or -
def valueIsNegative(self, value):
value = byte_to_signed_int(value)
if value < 0: return 1 #unsigned value is negative when below 0
elif value > 127: return 1 #signed value is actually negative when over 127
else: return 0
function_table = {
'adc' : execute_adc,
'and' : execute_and,
'asl' : execute_asl,
'clc' : execute_clc,
'cld' : execute_cld,
'cmp' : execute_cmp,
'cpx' : execute_cpx,
'sei' : execute_sei,
'lda' : execute_lda,
'ldx' : execute_ldx,
'ldy' : execute_ldy,
'sta' : execute_sta,
'stx' : execute_stx,
'sty' : execute_sty,
'dex' : execute_dex,
'dey' : execute_dey,
'dec' : execute_dec,
'inx' : execute_inx,
'iny' : execute_iny,
'txs' : execute_txs,
'txa' : execute_txa,
'tay' : execute_tay,
'tax' : execute_tax,
'jmp' : execute_jmp,
'jsr' : execute_jsr,
'rts' : execute_rts,
'pla' : execute_pla,
'bne' : execute_bne,
'beq' : execute_beq,
'bcs' : execute_bcs,
'bpl' : execute_bpl
}