Assembler for Ben Eater's 8-bit computer

I've been thinking about 8-bit computers recently and came across the most excellent breadboard 8-bit computer series by Ben Eater.  Absolutely terrific stuff, fantastic break down of the subject and a clear and talented educator.  Like many, it got me pondering building my own 8-bit computer, modifying the original design to my specifications and writing custom opcodes and microcode.  Thinking about how to convert assembly language easily to machine code for such a custom computer, I wondered if there were any assemblers out there for assembling custom ASM.  It turns out there are, and the one I've been playing with is called... customasm.


The web interface is powerful and straightforward to use.  First I defined the instruction set:

#cpudef
{
    #bits 8
    
nop => 0x00

lda [{address}] => 0b0001 @ address[3:0]
add [{address}] => 0b0010 @ address[3:0]
sub [{address}] => 0b0011 @ address[3:0]
sta [{address}] => 0b0100 @ address[3:0]
out => 0b0101 @ 0b0000
jmp {address} => 0b0110 @ address[3:0]
ldi {value} => 0b0111 @ value[3:0]
jc {address} => 0b1000 @ address[3:0]
jnc {address} => 0b1001 @ address[3:0]

halt => 0b1111 @ 0b0000

}

This tells customasm how it should interpret the mnemonics and operands into machine.  Below this, we can then define our program.  In this case it outputs the Fibonacci sequence:

#addr 0x00
lda [13]  ; A = contents of memory address 13
loop:
out
lda [14]    ; A = y
add [15] ; A(x) = A(y) + z

sta [13] ; temp = A (x)
lda [14] ; A = y
sta [15] ; z = A = y

lda [13] ; A = temp = A(x)
sta [14] ; y = A = temp = x

jnc loop ; If carry not set, jump to loop
halt
#addr 13
#d8 1,1,0 ; temp, y, z 

Note that usefully we can specific the starting address of the program, and where to put data bytes.    When we're happy, we can hit Ctrl+Enter, or the "Assemble" but to generate the machine code output.  There are a variety of useful options for the output format from a drop down menu.  

In my case I wanted a comma-separated Hex string, so that I could load into into a 8-bit CPU simulator I've written (more on that another time!).  It should be possible to write programs for Ben Eater's computer, or a CPU of your own design and generate the custom machine code as required.

Output (comma-separated Hex string) of Fibonacci sequence program:
0x1d, 0x50, 0x1e, 0x2f, 0x4d, 0x1e, 0x4f, 0x1d, 0x4e, 0x91, 0xf0, 0x00, 0x00, 0x01, 0x01, 0x00

Easy-peasy!  The Ben Eater computer is limited to 4 bits of addressing, meaning only 16 bytes are available in total for instructions and data!  Of course that is an interesting challenge in itself to fit a program within those requirements, but I think it will be fun to expand this computer to have (say) 256 bytes of RAM, and 16-bit program memory addresses.  I am currently simulating different architecture's of custom 8-bit computers I am interested in building, and using customasm to generate the machine code to run on them.  It's a fantastic tool and I'm really glad it exists.  

Happy hacking!

Comments

  1. Hello, I tried putting your code into customeasm, but when I assemble it, I get an error with the : after the loop. Any idea why this may be happening?

    ReplyDelete
    Replies
    1. Hi Unknown,

      Thanks for pointing this out. It seems that the problem was having '->' rather than '=>' in the instruction definitions. Not sure whether this changed since I first published this post or what was going on. Fixed now.

      cheers,
      Duncan

      Delete

Post a Comment

Popular posts from this blog

Arduino and Raspberry Pi serial communciation

Getting started with the Pro Micro Arduino Board