diff --git a/src/cpu.py b/src/cpu.py index d0d294f..6152e3a 100644 --- a/src/cpu.py +++ b/src/cpu.py @@ -27,6 +27,22 @@ km = keypad.KeyMatrix( column_pins = (board.P4, board.P6, board.P8, board.P9) ) +class TwoDigitHexInput: + def __init__(self): + self.digits = [0x0, 0x0] + self.currentDigit = 0 + self.value = 0 + + def input(self, d): + self.digits[self.currentDigit] = d + self.value = (self.digits[0] * 16) + self.digits[1] + print("INPUT", self.digits) + self.currentDigit = 0 if self.currentDigit else 1 + + def clear(self): + self.__init__() + print(self.digits) + class CPU: def __init__(self): @@ -37,10 +53,8 @@ class CPU: self.instruction = { 'opcode': False, 'operand': False } self.memory = False self.monitorMode = 'addressEntry' # dataEntry - self.monitorAddressInput = "00" # These are strings because it makes shifting digits in easier - self.monitorDataInput = "00" - self.monitorAddressDisplay = "00" - self.monitorDataDisplay = "00" + self.monitorAddressInput = TwoDigitHexInput() + self.monitorDataInput = TwoDigitHexInput() def load_memory(self, bytes): @@ -68,7 +82,7 @@ class CPU: displayGroup = displayio.Group() board.DISPLAY.root_group = displayGroup - text = self.monitorAddressDisplay + " " + self.monitorDataDisplay + text = "IP " + str(self.IP) + "DATA " + str(self.memory[self.IP]) + "ACC " + str(self.acc) text_area = label.Label(terminalio.FONT, text=text) text_area.x = 10 text_area.y = 100 @@ -99,29 +113,28 @@ class CPU: if btnb.value == False: self.monitorMode = 'addressEntry' if self.monitorMode != 'addressEntry' else 'dataEntry' print("\nENTERING", self.monitorMode, "MODE") + self.monitorDataInput.currentDigit = 0 + self.monitorAddressInput.currentDigit = 0 time.sleep(0.5) # lazy debounce if btnr.value == False: print("\nSINGLE STEP FROM MONITOR ADDR") - self.IP = int(self.monitorAddressInput, 16) + # self.IP = self.monitorAddressInput.value self.step() time.sleep(0.5) # lazy debounce if keypad_event and keypad_event.released: - new_digit_0x = hex(keypad_event.key_number) - new_digit = new_digit_0x[2:3] if self.monitorMode == 'addressEntry': - cat = self.monitorAddressInput + new_digit - self.monitorAddressInput = cat[1:3] - print("MA", self.monitorAddressInput) + self.monitorAddressInput.input(keypad_event.key_number) + self.IP = self.monitorAddressInput.value + print("MA", self.IP) else: - cat = self.monitorDataInput + new_digit - self.monitorDataInput = cat[1:3] # TODO this variable might not be necessary, huh? - self.memory[int(self.monitorAddressInput, 16)] = int(self.monitorDataInput, 16) - print("MD", self.monitorDataInput) - print("Acc", self.acc, "Addr", self.monitorAddressInput, "Data", self.memory[int(self.monitorAddressInput, 16)]) + self.monitorDataInput.input(keypad_event.key_number) + self.memory[self.IP] = self.monitorDataInput.value + print("MD", self.monitorDataInput.value) + if self.monitorDataInput.currentDigit == 0: # that was the second keypress, so go to the next addresss + self.IP = (self.IP + 1) % 256 + print("ADVANCING") + print("Acc", self.acc, "IP", self.IP, "Data", self.memory[self.IP], "\n") - # TODO: update data with keypad - # then, automatically go to next address (?) - def step(self): if self.IP >= 256: @@ -148,11 +161,12 @@ class CPU: t = time.time() while (time.time() - t) < 30: self.read_keys() - #self.print_monitor() - #if self.running: - # self.step() - # time.time.sleep(0.5) +# self.print_monitor() +# if self.running: +# self.step() +# time.sleep(0.5) print("timeout") + print(self.memory) def hlt(self, operand): self.running = False @@ -275,7 +289,8 @@ class CPU: cpu = CPU() -prog = '04 FF 04 01 14 01 00 00' +#prog = '04 FF 04 01 14 01 00 00' +prog = '00' program_bytes = bytearray.fromhex(prog.replace(" ", "")) # Add jmp at addr 254: program_with_jump = program_bytes + bytearray(254 - len(program_bytes)) + bytearray.fromhex('0600')