From 6bea93308c2de450540452648d412395b0c543a8 Mon Sep 17 00:00:00 2001 From: n loewen Date: Tue, 25 Mar 2025 17:04:25 +0000 Subject: [PATCH] Get 8x8 led matrix working --- src/pi-pico/cpu-pi-pico.py | 79 +++++++++++++++++++++++--------------- 1 file changed, 49 insertions(+), 30 deletions(-) diff --git a/src/pi-pico/cpu-pi-pico.py b/src/pi-pico/cpu-pi-pico.py index 87d911d..13dc4cf 100644 --- a/src/pi-pico/cpu-pi-pico.py +++ b/src/pi-pico/cpu-pi-pico.py @@ -13,7 +13,9 @@ import board import keypad import digitalio from tm1637_display import TM1637Display - +import board +import busio # for led matrix +from adafruit_ht16k33 import matrix class TwoDigitHexInput: def __init__(self): @@ -59,13 +61,15 @@ class CPU: self.running = True def step(self): - if self.IP >= 256: + if self.IP >= 255: # TODO CHECK self.IP = 0 print("IP:", self.IP) self.instruction['opcode'] = self.memory[self.IP] self.IP = self.IP+1 self.instruction['operand'] = self.memory[self.IP] self.IP = self.IP+1 + print(self.memory[self.IP -1], self.memory[self.IP-2]) + print(self.instruction) self.nums2mnems[self.instruction['opcode']](self, self.instruction['operand']) print("instr:", self.instruction['opcode'], self.instruction['operand']) @@ -87,13 +91,13 @@ class CPU: self.acc = operand def lda_mem(self, operand): - self.acc = memory[operand] + self.acc = self.memory[operand] def sta_lit(self, operand): - memory[operand] = self.acc + self.memory[operand] = self.acc def sta_mem(self, operand): - memory[memory[operand]] = self.acc + self.memory[self.memory[operand]] = self.acc def add_lit(self, operand): self.acc = self.acc + operand @@ -168,26 +172,26 @@ class CPU: self.flags['C'] = False nums2mnems = { - 0: hlt, - 1: nop, - 2: lda_lit, - 3: sta_lit, - 4: add_lit, - 5: sub_lit, - 6: jmp_lit, - 7: ske, - 8: skz, - 9: skn, - 10: skc, - 11: cst, - 12: ccl, - 16: hlt, - 17: nop, - 18: lda_mem, - 19: sta_mem, - 20: add_mem, - 21: sub_mem, - 22: jmp_mem, + 0: hlt, # x0 + 1: nop, # x1 + 2: lda_lit, # 02 + 3: sta_lit, # 03 + 4: add_lit, # 04 + 5: sub_lit, # 05 + 6: jmp_lit, # 06 + 7: ske, # x7 + 8: skz, # x8 + 9: skn, # x9 + 10: skc, # A + 11: cst, # B + 12: ccl, # C + 16: hlt, # + 17: nop, # + 18: lda_mem, # 12 + 19: sta_mem, # 13 + 20: add_mem, # 14 + 21: sub_mem, # 15 + 22: jmp_mem, # 16 23: ske, 24: skz, 25: skn, @@ -204,6 +208,11 @@ class CPU: display_1 = TM1637Display(board.GP0, board.GP1, length=4) display_2 = TM1637Display(board.GP2, board.GP3, length=4) +i2c = busio.I2C(board.GP17, board.GP16) # scl, sda +matrix = matrix.Matrix8x8(i2c) +matrix.brightness = 1 +matrix.blink_rate = 0 + keymatrix = keypad.KeyMatrix( row_pins = (board.GP5, board.GP6, board.GP7, board.GP8), column_pins = (board.GP9, board.GP10, board.GP11, board.GP12, board.GP13) ) @@ -293,20 +302,27 @@ class Monitor: print("Acc", self.cpu.acc, "IP", self.cpu.IP, "Data", self.cpu.memory[self.cpu.IP], "\n") + + def displayScreen(self): + for x in range(8): + for y in range(8): + matrix[x, y] = self.cpu.memory[x + (8*y)] + def run(self): #self.cpu.start() t = time.time() - while (time.time() - t) < 120: + while (time.time() - t) < 120: # TODO: add a time delta or sth maybe so this doesn't just burn cycles self.handleKeys() 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)) + self.displayScreen() if self.cpu.running: self.cpu.step() - time.sleep(0.5) + # time.sleep(0.5) # TODO ? print("timeout") print(self.cpu.memory) @@ -314,11 +330,14 @@ class Monitor: cpu = CPU() monitor = Monitor(cpu) -#prog = '04 FF 04 01 14 01 00 00 01 01 01 01 01 01' -prog = '00' +preamble = '00' * 64 +prog = preamble + '02 01 13 f0 12 f0 04 02 03 f0 12 f0 05 41 08 00 06 40 00 00' # STRIPES +#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') +#program_with_jump = program_bytes + bytearray(254 - len(program_bytes)) + bytearray.fromhex('0600') # jump to addr 00 +program_with_jump = program_bytes + bytearray(254 - len(program_bytes)) + bytearray.fromhex('0640') # jump to addr 0x40 (dec 64) cpu.load_memory(program_with_jump) monitor.run() \ No newline at end of file