I started building a little computer this week. The idea/constraint is: what if we built a late-70s-early-80s-style 8-bit computer in 2020
This pic is extremely early, mostly just getting a toolchain and workflow into place
While I wait for some parts to arrive, I’ve been working on the BASIC interpreter, and have gained an appreciation for the people writing firmware back in the 70s, often without actual hardware. I’ve got it easy!
(check out @adventofcomp for these stories, its a great listen)
The 2s pause tells you that this is successfully running its first BASIC program:
10 ON 20 SLEEP 30 OFF 40 SLEEP 50 GOTO 10
usb serial doober was delivered to the office, so I went and picked it up last night. this evening, wired up, wrote some string handling code, and hello there!
a decent evening of work and I have a working input look and a nice hex dumper. type things, hit enter, get the contents of the input buffer back (you can see the ASCII codes right there)
ASCII number string -> actual value parser is working nicely. Will be used for BASIC line numbers and I guess some math stuff. 16 bits should be enough for anyone.
nothing of note to show, but
- couch dev rig is awkward but workable
- I bought some new LEDs and holy shit they’re bright
wrote half the statement parser last night, then got cranky at the assembler, found a different one, papered over a couple of bugs, and then finished the parser. here’s statement->opcode mapping. by modern standards of string lookups its bloody awful 😎️
new toy! existing PL2303 serial bridge (top) is apparently a sketchy knock-off chip, and quite unstable (starts dropping frames after a while and needs reset). New is a CH340 breakout, which have a much better track record. will add a pin header and make go this afternoon!
wrote an error handler. anywhere, anytime, stick the error code in r25 and return, and the mainloop will print something nice. there’s a reason unix has a global errno and you just check it and bail out if you see it set: it’s the cheapest and easiest way to do it
ahaha immediate mode is working for simple (no args) statements this is rad
yes friends, that is a STORED PROGRAM COMPUTER (and yes I am just a little bit excited)
adding small niceties like handling whitespace and runtime errors while avoiding writing the expression parser
here’s program storage and memory management in action. the program is an array of variable-length data sequences: one byte of length, two bytes of line number, 1+ bytes of opcode and args. they remain sorted by line number; insertions move the whole program up in memory
removing a line closes the gap, replacing a line adjusts the existing gap. the relative expense of moving the entire program space around sucks, but a true linked list is harder to code and uses more memory. the common case of append is fast though, no moves required!
expression parser is in! converts infix→postfix for the four basic operations and parens. operands get prefixed with a “type”, which will allow variable lookups and functions to just work in the evaluator (which doesn’t exist yet heh 🤷♂️)
the entire parser is 89 instructions 😎
alright, there’s the first cut expression evaluator, and I’ve got myself a little calculator. alas, I forgot that numbers less than zero exist
a day later, and I think we’ve got full 16-bit signed math going. uncovered a bunch of bugs along the way, not least of which was total confusion ‘-’ as minus and ‘-’ as negation. just a couple more edge cases to smooth out and then we can move on to the next thing!
solid evening! visit a friend, have a few glasses of wine, get home and still a enough time to drop in some sneaky string support and write the classic BASIC starter program
variables! we’re almost at the point where you could write a useful program with this thing!
omg I typed in a program from a programming book and it work ahahahaha I’m living the dream
huge day: added subroutines (GOSUB/RETURN), conditionals (IF/THEN) and user input (uh, INPUT). put it all together, and here’s our first actual useful(-ish) program: factorial calculator, via recursion
lets print and input multiple things! BASIC’s separators are kinda bonkers
lots of loose ends tidied. can now replace and delete existing lines, NEW and CLEAR work. can’t put it off any longer: this most trivial of programs is gonna need string variable support, so that’s next. oof.
several days later, and a usable amount of string variables is in. it got kinda bonkers, mainly around memory management for variable storage (strings don’t fit in registers!) and tracking separate types. still some kinks to sort out but I think the shapes are mostly right
space monsters, Xtra good
originally posted on twitter at: https://twitter.com/robn/status/1255079227990261769