Python Forum
I wrote an emulator!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I wrote an emulator!
#6
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:
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.
Reply


Messages In This Thread
I wrote an emulator! - by keames - Nov-10-2019, 09:07 PM
RE: I wrote an emulator! - by Gribouillis - Nov-10-2019, 10:02 PM
RE: I wrote an emulator! - by keames - Nov-10-2019, 11:10 PM
RE: I wrote an emulator! - by buran - Nov-11-2019, 12:08 PM
RE: I wrote an emulator! - by buran - Nov-11-2019, 12:45 PM
RE: I wrote an emulator! - by keames - Nov-12-2019, 04:41 AM
RE: I wrote an emulator! - by keames - Nov-13-2019, 04:12 AM
RE: I wrote an emulator! - by buran - Nov-13-2019, 04:39 AM
RE: I wrote an emulator! - by keames - Nov-13-2019, 10:13 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Intel 8051 microcontroller emulator estarq 0 2,217 Apr-22-2022, 10:59 AM
Last Post: estarq
  I wrote a cryptographic algorithm! keames 3 3,434 May-11-2021, 06:10 AM
Last Post: Gribouillis
  four commands i wrote the other day Skaperen 4 2,984 Jun-20-2019, 05:47 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020