summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorp4bl0 <r _at_ uzy .dot. me>2018-06-11 11:32:09 (CEST)
committerp4bl0 <r _at_ uzy .dot. me>2018-06-11 11:32:09 (CEST)
commitf14634fda1137c0be69d92aceb5c15298f85b704 (patch)
tree1bf725c3e368fa1b2fed854285f59d57645da84c
parent7d86787ac7ea55c68a547f445c58f18fd76b5352 (diff)
downloadseselab-f14634fda1137c0be69d92aceb5c15298f85b704.zip
seselab-f14634fda1137c0be69d92aceb5c15298f85b704.tar.gz
refactor run/cycle for better integration with future GUI
-rw-r--r--cpu.py50
1 files changed, 30 insertions, 20 deletions
diff --git a/cpu.py b/cpu.py
index fac0919..c1d896c 100644
--- a/cpu.py
+++ b/cpu.py
@@ -7,10 +7,18 @@ class CPU:
def __init__ (self, ram_size, reg_count, code, consolog):
self._ram = Memory(ram_size)
+
self._reg = Memory(reg_count)
self.__ra = reg_count - 1
self.__sp = reg_count - 2
+
self._code = code
+
+ self._ip = 0 # init instruction pointer
+ self._max_ip = len(self._code)
+ self._reg[self.__sp] = self._ram._size # init stack pointer
+ self._reg[self.__ra] = self._max_ip # init return address
+
self._probe = Probe(consolog)
def value (self, val):
@@ -39,10 +47,9 @@ class CPU:
a += self.value(addr[2])
self._ram[a] = val
- def cycle (self, ip):
+ def execute_instruction (self, ip):
instr = self._code[ip][0]
opcode = instr[0]
-
# opcodes with no arguments
if opcode == 'nop':
pass
@@ -123,29 +130,32 @@ class CPU:
return None
- def run (self):
+ def cycle (self):
try:
- max_ip = len(self._code)
- self._reg[self.__sp] = self._ram._size # init stack pointer
- self._reg[self.__ra] = max_ip # init return address
- self._ip = 0 # init instruction pointer
- while self._ip >= 0 and self._ip < max_ip:
- ip = self.cycle(self._ip)
- if ip is not None:
- self._ip = ip
- else:
- self._ip += 1
- self._probe.read(self._ram.get_activity())
- self._probe.read(self._reg.get_activity())
- self._probe.output_activity()
- sys.stdout.flush()
+ ip = self.execute_instruction(self._ip)
+ if ip is not None:
+ self._ip = ip
+ else:
+ self._ip += 1
+ self._probe.read(self._ram.get_activity())
+ self._probe.read(self._reg.get_activity())
+ self._probe.output_activity()
+ sys.stdout.flush()
+
+ if self._ip < 0 or self._ip >= self._max_ip:
+ return False
+ return True
except AddrError as e:
- print('Invalid address ' + str(e.addr) + self.dbg(self._ip))
+ print('Invalid address ' + str(e.addr) + self.dbg(self._ip), file=sys.stderr)
except ValError as e:
- print('Invalid value ' + str(e.val) + self.dbg(self._ip))
+ print('Invalid value ' + str(e.val) + self.dbg(self._ip), file=sys.stderr)
except WriteError:
- print('Invalid write ' + self.dbg(self._ip))
+ print('Invalid write ' + self.dbg(self._ip), file=sys.stderr)
+
+ def run (self):
+ while self.cycle():
+ pass
def dbg (self, ip):
return (' on line ' + str(self._code[ip][1][1]) +
Pablo Rauzy — generated by cgit