From cf68271440011c9e92b6d0910cc050a7282ef132 Mon Sep 17 00:00:00 2001 From: n loewen Date: Tue, 25 Mar 2025 15:22:25 +0000 Subject: [PATCH] Get monitor keypad working --- src/pi-pico/cpu-pi-pico.py | 52 ++++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/src/pi-pico/cpu-pi-pico.py b/src/pi-pico/cpu-pi-pico.py index 0fef346..87d911d 100644 --- a/src/pi-pico/cpu-pi-pico.py +++ b/src/pi-pico/cpu-pi-pico.py @@ -24,13 +24,20 @@ class TwoDigitHexInput: def input(self, d): self.digits[self.currentDigit] = d self.value = (self.digits[0] * 16) + self.digits[1] - print("INPUT", self.digits) + print("INPUT", self.digits, "current digit: " + str(self.currentDigit), "value: " + str(self.value)) self.currentDigit = 0 if self.currentDigit else 1 def clear(self): self.__init__() print(self.digits) + def set(self, n): + self.value = n + self.digits[0] = n >> 4 + self.digits[1] = n & 0xF + + + class CPU: def __init__(self): @@ -204,8 +211,8 @@ keymatrix = keypad.KeyMatrix( keymap = { 15:"0", 16:"1", 17:"2", 18:"3", 19:"runhalt", 10:"4", 11:"5", 12:"6", 13:"7", 14:"step", - 5:"8", 6:"9", 7:"A", 8:"B", 9:"addrdata", - 0:"C", 1:"D", 2:"E", 3:"F", 4:"NA" } + 5:"8", 6:"9", 7:"A", 8:"B", 9:"addr", + 0:"C", 1:"D", 2:"E", 3:"F", 4:"data" } numericKeys = [ "0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F" ] @@ -219,12 +226,18 @@ class Monitor: self.monitorAddressInput = TwoDigitHexInput() self.monitorDataInput = TwoDigitHexInput() + # In data entry mode, when a full byte is keyed in, + # the next keypress advances to the next address and continues entering data there. + # This variable tracks whether it's time to do that or not. + self.advanceDataEntryNextPress = False + def handleKeys(self): keypad_event = keymatrix.events.get() keyPressed = True if (keypad_event and keypad_event.released ) else False key = keymap[keypad_event.key_number] if keyPressed else False numericKeyPressed = True if (keyPressed and (key in numericKeys)) else False + if self.cpu.running: if key == "runhalt": print("HALT PRESSED") @@ -241,12 +254,17 @@ class Monitor: print("\nSTARTING") time.sleep(0.5) # lazy debounce - if key == "addrdata": - self.monitorMode = 'addressEntry' if self.monitorMode != 'addressEntry' else 'dataEntry' + if key == "addr": + self.monitorMode = 'addressEntry' print("\nENTERING", self.monitorMode, "MODE") - self.monitorDataInput.currentDigit = 0 self.monitorAddressInput.currentDigit = 0 time.sleep(0.5) # lazy debounce + if key == "data": + self.monitorMode = 'dataEntry' + print("\nENTERING", self.monitorMode, "MODE") + self.monitorDataInput.clear() + self.advanceDataEntryNextPress = False + time.sleep(0.5) # lazy debounce if key == "step": print("\nSINGLE STEP FROM MONITOR ADDR") @@ -260,24 +278,32 @@ class Monitor: self.cpu.IP = self.monitorAddressInput.value print("MA", self.cpu.IP) - else: + if self.monitorMode == 'dataEntry': + if self.advanceDataEntryNextPress: + print("ADVANCING") + self.cpu.IP = (self.cpu.IP + 1) % 256 + # self.monitorDataInput.clear() # reset .currentDigit + self.monitorDataInput.set(self.cpu.memory[self.cpu.IP]) + self.advanceDataEntryNextPress = False self.monitorDataInput.input(int(key, 16)) self.cpu.memory[self.cpu.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.cpu.IP = (self.cpu.IP + 1) % 256 - print("ADVANCING") + if self.monitorDataInput.currentDigit == 0: # that was the second keypress, so next keypress is for the next address + self.advanceDataEntryNextPress = True + print("Acc", self.cpu.acc, "IP", self.cpu.IP, "Data", self.cpu.memory[self.cpu.IP], "\n") def run(self): - self.cpu.start() + #self.cpu.start() t = time.time() while (time.time() - t) < 120: self.handleKeys() - display_1.print(toHex(self.monitorAddressInput.value) + toHex(self.monitorDataInput.value)) - display_2.print(toHex(self.cpu.IP) + toHex(self.cpu.acc)) + display_1.print(toHex(self.cpu.IP) + toHex(self.cpu.memory[self.cpu.IP])) + # display_1.print(toHex(self.monitorAddressInput.value) + toHex(self.cpu.memory[self.cpu.IP])) + # display_2.print(toHex(self.cpu.IP) + toHex(self.cpu.acc)) + display_2.print(toHex(self.cpu.acc)) if self.cpu.running: self.cpu.step() time.sleep(0.5)