-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstruction.py
251 lines (216 loc) · 8.24 KB
/
instruction.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
from enum import IntEnum
import struct
from warnings import warn
class AddrMode(IntEnum):
imp = 1 # implicit
imm = 2 # immediate
zp = 3 # zero page
zpx = 4 # zero page, X
zpy = 5 # zero page, Y
izx = 6 # indirect, X
izy = 7 # indirect, Y
abs = 8 # absolute
abx = 9 # absolute, X
aby = 10 # absolute, Y
ind = 11 # indexed
rel = 12 # relative
n_addrs = 13
AM = AddrMode
# Length of specifiers for address mode. Instruction length is this + 1.
ADDR_MODE_LENGTHS = {
AM.imp : 0,
AM.imm : 1,
AM.zp : 1,
AM.zpx : 1,
AM.zpy : 1,
AM.izx : 1,
AM.izy : 1,
AM.abs : 2,
AM.abx : 2,
AM.aby : 2,
AM.ind : 2,
AM.rel : 1,
}
# Number of extra cycles taken to use a cycle.
ADDR_MODE_CYCLES = {
AM.imp: 0,
AM.imm: 0,
AM.zp : 1,
AM.zpx : 2,
AM.zpy : 2,
AM.izx : 4,
AM.izy : 3, # TODO extra cycle for page crossing
AM.abs : 2,
AM.abx : 2, # TODO extra cycle for page crossing
AM.aby : 2, # TODO extra cycle for page crossing
AM.ind : 4,
AM.rel : 0, # TODO +1 if branch succeeds, +2 if to new page (so is that +2 or +3 total?)
}
class Opcode(object):
def __init__(self, name, f, code, addrMode, baseCycles):
self.name = name
self.f = f
self.code = code
self.addrMode = addrMode
self.baseCycles = baseCycles
@property
def addrSize(self):
return ADDR_MODE_LENGTHS[self.addrMode]
@property
def size(self):
return 1 + self.addrSize
class Instruction(object):
def __init__(self, addr, opcode, addrData, rawBytes):
# Don't call this on its own, use the makeInstr factory method
self.addr = addr
self.opcode = opcode
self.addrData = addrData
self.rawBytes = rawBytes
self.cycles = self.opcode.baseCycles + ADDR_MODE_CYCLES[self.opcode.addrMode]
@property
def size(self):
return self.opcode.size
@property
def nextaddr(self):
"""The address of the next instruction (by listing; doesn't take
jumps into account)."""
return self.addr + self.size
def memAddr(self, cpu):
"""Currently just an alias for computeMemAddr."""
return self.computeMemAddr(cpu)
def computeMemAddr(self, cpu):
"""Returns the memory address to be written to or read from. This will
depend on the addressing mode."""
# see http://wiki.nesdev.com/w/index.php/CPU_addressing_modes
# we convert endianness here
raise RuntimeError("computeMemAddr on abstract Instruction class")
def readMem(self, cpu):
#print "reading %x" % ord(cpu.mem.read(self.memAddr(cpu))) # DEBUG
return cpu.mem.read(self.memAddr(cpu))
def writeMem(self, val, cpu):
#print "WRITING to %x" % self.memAddr(cpu) # DEBUG
cpu.mem.write(self.memAddr(cpu), val)
def call(self, cpu):
self.opcode.f(self, cpu)
def addrDataStr(self):
# This probably could have been shorter. Oh well.
am = self.opcode.addrMode
if am == AM.imp:
return ""
elif am == AM.imm:
return "#$%02x" % ord(self.addrData)
elif am == AM.zp:
return "$%02x" % ord(self.addrData)
elif am == AM.zpx:
return "$%02x, X" % ord(self.addrData)
elif am == AM.zpy:
return "$%02x, Y" % ord(self.addrData)
elif am == AM.izx:
return "($%02x, X)" % ord(self.addrData)
elif am == AM.izy:
return "($%02x), Y" % ord(self.addrData)
# remember little-endian from here on
elif am == AM.abs:
return "$%02x%02x" % (ord(self.addrData[1]),
ord(self.addrData[0]))
elif am == AM.abx:
return "$%02x%02x, X" % (ord(self.addrData[1]),
ord(self.addrData[0]))
elif am == AM.aby:
return "$%02x%02x, Y" % (ord(self.addrData[1]),
ord(self.addrData[0]))
elif am == AM.ind:
return "($%02x%02x)" % (ord(self.addrData[1]),
ord(self.addrData[0]))
elif am == AM.rel:
# here addrData is a signed integer that represents an
# offest from the address we'll reach after the
# instruction, at least as far as I can tell
offset = struct.unpack('b', self.addrData)[0]
target = self.addr + offset + 2 # no endianness to worry about
return "$%04x" % target
else:
raise RuntimeError("Unrecognized addressing mode")
def disassemble(self): # TODO print hex data here too
return "%04x: %s %s %s" % (self.addr,
self.opcode.name,
self.addrDataStr(),
str([hex(ord(b)) for b in self.rawBytes]))
@staticmethod
def makeInstr(addr, opcode, addrData, rawBytes):
cls = AM_CLASSES[opcode.addrMode]
return cls(addr, opcode, addrData, rawBytes)
class ImpliedAddrInstr(Instruction):
def computeMemAddr(self, cpu):
warn("Trying to access memory for implicit-addressing instruction")
return None
class ImmediateAddrInstr(Instruction):
def computeMemAddr(self, cpu):
return self.addr + 1
class ZeroPageAddrInstr(Instruction):
def computeMemAddr(self, cpu):
# address high byte is zero (hence "zero page")
return ord(self.addrData)
class ZeroPageXAddrInstr(Instruction):
def computeMemAddr(self, cpu):
return (ord(self.addrData) + cpu.reg_X) % 256
class ZeroPageYAddrInstr(Instruction):
def computeMemAddr(self, cpu):
return (ord(self.addrData) + cpu.reg_Y) % 256
class IndirectZeroXAddrInstr(Instruction):
def computeMemAddr(self, cpu):
# can't use the dereference utility function for this because
# we have to stay in the zero page
pointer = (ord(self.addrData) + cpu.reg_X) % 256
addrLow = ord(cpu.mem.read(pointer))
addrHigh = ord(cpu.mem.read((pointer + 1) % 256))
return addrLow + addrHigh * 256
class IndirectZeroYAddrInstr(Instruction):
def computeMemAddr(self, cpu):
pointer = ord(self.addrData)
addrLow = ord(cpu.mem.read(pointer))
addrHigh = ord(cpu.mem.read((pointer + 1) % 256))
return (addrLow + addrHigh * 256 + cpu.reg_Y) & 0xffff
# remember little-endian from here on
class AbsoluteAddrInstr(Instruction):
def computeMemAddr(self, cpu):
return struct.unpack('H', self.addrData)[0]
class AbsoluteXAddrInstr(Instruction):
def computeMemAddr(self, cpu):
offset = struct.unpack('H', self.addrData)[0]
return (offset + cpu.reg_X) & 0xffff
class AbsoluteYAddrInstr(Instruction):
def computeMemAddr(self, cpu):
offset = struct.unpack('H', self.addrData)[0]
return (offset + cpu.reg_Y) & 0xffff
class IndexedAddrInstr(Instruction):
def computeMemAddr(self, cpu):
pointer = struct.unpack('H', self.addrData)[0]
addrLow = ord(cpu.mem.read(pointer))
# 6502 bug? see http://forums.nesdev.com/viewtopic.php?t=5388
addrHighLoc = pointer + 1
if (addrHighLoc & 0xff00) != (pointer & 0xff00):
addrHighLoc -= 0x100
addrHigh = ord(cpu.mem.read(addrHighLoc))
return addrLow + addrHigh * 256
class RelativeAddrInstr(Instruction):
def computeMemAddr(self, cpu):
# Here addrData is a signed integer that represents an offest
# from the address we'll reach after the instruction, at least
# as far as I can tell. No endianness to worry about.
offset = struct.unpack('b', self.addrData)[0]
target = self.addr + offset + 2 # lol computers
return target
AM_CLASSES = [None] * AM.n_addrs
AM_CLASSES[AM.imp] = ImpliedAddrInstr
AM_CLASSES[AM.imm] = ImmediateAddrInstr
AM_CLASSES[AM.zp] = ZeroPageAddrInstr
AM_CLASSES[AM.zpx] = ZeroPageXAddrInstr
AM_CLASSES[AM.zpy] = ZeroPageYAddrInstr
AM_CLASSES[AM.izx] = IndirectZeroXAddrInstr
AM_CLASSES[AM.izy] = IndirectZeroYAddrInstr
AM_CLASSES[AM.abs] = AbsoluteAddrInstr
AM_CLASSES[AM.abx] = AbsoluteXAddrInstr
AM_CLASSES[AM.aby] = AbsoluteYAddrInstr
AM_CLASSES[AM.ind] = IndexedAddrInstr
AM_CLASSES[AM.rel] = RelativeAddrInstr