Compare commits
5 Commits
f381423fa3
...
1bfbd93d7c
| Author | SHA1 | Date | |
|---|---|---|---|
| 1bfbd93d7c | |||
| 52b905eb14 | |||
| b9684ceaac | |||
| 9d41d45d80 | |||
| 3c4e8f258a |
@ -15,7 +15,6 @@ def main():
|
||||
default="out.mem"
|
||||
)
|
||||
args = parser.parse_args()
|
||||
print(args)
|
||||
with open(args.mem_file, 'rb') as f:
|
||||
mem = bytearray(f.read())
|
||||
vm = VM(mem)
|
||||
|
||||
@ -43,13 +43,13 @@ OpD = OpcodeDescription
|
||||
|
||||
OPCODES = {
|
||||
# block 1
|
||||
0x00: OpD(OpF(0), OpL.MATH, OpA.ADD),
|
||||
0x00: OpD(OpF.UNEXPANDED, OpL.MATH, OpA.ADD),
|
||||
0x10: OpD(OpF.QUICK, OpL.MATH, OpA.ADD),
|
||||
0x01: OpD(OpF(0), OpL.MATH, OpA.SUB),
|
||||
0x01: OpD(OpF.UNEXPANDED, OpL.MATH, OpA.SUB),
|
||||
0x11: OpD(OpF.QUICK, OpL.MATH, OpA.SUB),
|
||||
0x02: OpD(OpF(0), OpL.MATH, OpA.MUL),
|
||||
0x02: OpD(OpF.UNEXPANDED, OpL.MATH, OpA.MUL),
|
||||
0x12: OpD(OpF.QUICK, OpL.MATH, OpA.MUL),
|
||||
0x03: OpD(OpF(0), OpL.MATH, OpA.DIV),
|
||||
0x03: OpD(OpF.UNEXPANDED, OpL.MATH, OpA.DIV),
|
||||
0x13: OpD(OpF.QUICK, OpL.MATH, OpA.DIV),
|
||||
0x04: OpD(OpF.UNEXPANDED, OpL.MATH, OpA.AND),
|
||||
0x05: OpD(OpF.UNEXPANDED, OpL.MATH, OpA.OR),
|
||||
|
||||
14
src/vm.py
14
src/vm.py
@ -119,6 +119,11 @@ class VM:
|
||||
"""
|
||||
Make one step (only step into)
|
||||
"""
|
||||
if self.pc.value * 4 > len(self.mem) - 4:
|
||||
raise VMException(
|
||||
VMExceptionType.END_OF_MEM,
|
||||
self.pc.value
|
||||
)
|
||||
opcode = self.mem[self.pc.value * 4]
|
||||
opdesc = self._fetch_opcode_desc(opcode)
|
||||
args = self._parse_arguments(opdesc)
|
||||
@ -134,7 +139,7 @@ class VM:
|
||||
"""
|
||||
Continue from current breakpoint
|
||||
"""
|
||||
while self.pc.value < len(self.mem):
|
||||
while self.pc.value * 4 < len(self.mem):
|
||||
if self.pc.value in self.breakpoints:
|
||||
raise Breakpoint(self.pc.value)
|
||||
self.step()
|
||||
@ -148,6 +153,11 @@ class VM:
|
||||
self.continue_()
|
||||
|
||||
def _fetch_opcode_desc(self, opcode: int):
|
||||
if not opcode in OPCODES:
|
||||
raise VMException(
|
||||
VMExceptionType.INVALID_OPCODE,
|
||||
self.pc.value
|
||||
)
|
||||
return OPCODES[opcode]
|
||||
|
||||
def _parse_arguments(self, opdesc: OpcodeDescription) -> tuple[int, ...]:
|
||||
@ -267,14 +277,12 @@ class VM:
|
||||
c = Condition(cond)
|
||||
vm_c = Condition(self.cc)
|
||||
if (c.v & vm_c.v) & (c.n & vm_c.n) & (c.z & vm_c.z) == c.i:
|
||||
self._vm_flags |= VMFlags.AFTER_BRANCH
|
||||
self.pc = c_uint32(self.pc.value + disp)
|
||||
|
||||
def _branch_indexed_callback(self, cond: int, r1: int, disp: int) -> None:
|
||||
c = Condition(cond)
|
||||
vm_c = Condition(self.cc.value)
|
||||
if (c.v & vm_c.v) & (c.n & vm_c.n) & (c.z & vm_c.z) == c.i:
|
||||
self._vm_flags |= VMFlags.AFTER_BRANCH
|
||||
addr = self.registers[r1].value + disp
|
||||
self.pc = c_uint32(addr)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user