I appreciate the advice. I was so excited to share what I had, I didn't notice the errors in my micro program generation. I've caught 2 address assignment errors in that code so far. I wrote a hand-assembled hello world thinking after writing just the flimsiest excuse for a test program and I still haven't worked out all the bugs in the microcode. I do agree now that the emulator is quite inelegant. I'm going to apply your refactoring suggestions when I have more free time. I'm working retail.
Here's the program load logic I've implemented:
Here's the program load logic I've implemented:
def main (): test = CPU() # I've decided to implement program loading from text files containing # string representations of dictionaries with nested lists. The keys will # be the starting addresses for loading particular code segment. The # lists will be the sequence of bytes to be loaded into memory starting at # that address. programPath = input("Please type the path to the program you want to run: ") prg = {} with open(programPath, 'r') as f: prg = eval(f.read()) for i in prg.keys(): for index, byte in enumerate(prg[i]): # Trap any overlaping code segment errors, signal an error, and exit. if test.RAM[i + index].get() != 0: input(f"The code segment starting at address {i} overlaps another code segment!") exit() test.RAM[i + index].set(byte) test.clockLoop()This is the hand assembled hello world I wrote. Hopefully I can get this to run.
### ### testProgram.txt ### { 0x0000: [ # sel Ara ; Ara will hold the characters from the string 0b00_000_000, # ldi ro, 0x0100 ; ro will be the pointer to the address of the string. 0b11_011_011, 0x01, 0x00, # ldi rb, 0x0000 ; rb will be an offset to the pointer. 0b11_001_001, 0x00, 0x00, # ldi rc, 0x0002 ; rc will hold an increment constant for rb. 0b11_010_010, 0x00, 0x02, # ld Ara, [ro + rb] ::loop1:: ; loop1 = 0x0009 0b11_000_101, # out Ara 0b00_110_000, # sel Arb 0b00_001_001, # add Arb, rc 0b01_000_010, # push rc ; stack = [rc] 0b11_111_010, # ldi rc, 0x00FF 0b11_011_010, 0x00, 0xFF, # sel Arc ; Use rc for a comparison of the lower byte of ra to 0x09 (a tab space) 0b00_010_010, # and Arc, ra 0b01_010_000, # sub Arc, 0x0009 ; Set the flags according to the subtraction. 0b01_111_101, 0x00, 0x09, # jfl Z end 0b10_000_011, 0x00, 0x1F, # pop Arc ; stack = [] 0b11_010_111, # sel Ara 0b00_000_000, # jmp loop1 0b00_111_111, 0x00, 0x09, # pop rc ::end:: ; end = 0x001F 0b11_010_111, # hlt 0b10_110_000 ], # The hello world string in hexadecimal 0x0100: [ 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x09, 0x00, 0x00 ] }I've also had to edit the microcode and the emulator. I hit the size limit again so I'll put them in separate posts.