MATLAB is an odd language

Things are, as usual, completely hectic. The most interesting thing I’ve done in the past few weeks has been writing a Bedrock emulator inside MATLAB. Time is ticking, so let’s dig in.

University

All of my work in semester one paid off, I was one of the thirty lucky students picked to transfer into the Bachelor of Engineering programme at the University of Auckland. I wasn’t able to enter engineering outright in semester one because of a few missing prerequisites from high school, but I’m here now. This was the whole reason I applied to uni in the first place, I want to dive deep into learning about electronics and processor design so that I can take my computing projects further and then eventually get a job doing stuff close to the metal.

So on one hand I’m elated that I’ve made it into the programme, but on the other hand the semester two courses have hit me like a train. The four courses are dictated by the engineering programme, so I’m taking courses on materials science, engineering design, electrical engineering, and programming. Materials science is fascinating but heavy, I never took chemistry in high school so I’m having to patch up the missing knowledge around different kinds of bonds and oxygenation states and stuff. Engineering design is very homework heavy. Electrical engineering is really enjoyable but also really challenging. My one reprieve is programming, where we’re learning MATLAB and then C later in the semester.

Because I entered engineering this semester, I haven’t taken the engineering maths and physics courses that the rest of the cohort completed last semester, which means that I’m also missing a chunk of calculus knowledge that keeps poking through into the practice questions this semester. So while I’m trying to keep on top of the workload this semester, I also really need to be digging into the course material for those other two courses to figure out how that all works.

Bedrock

And then we have Bedrock, which I want to sink time into but there just isn’t that much to go around these days. As I catch up on the course load, I’m finding it easier to chip away for a couple of hours here and there, but it’s slow progress.

Specification (revision 3)

The third revision of the specification is still my main focus with Bedrock, it’s dragged out so long with everything being pushed aside because of uni. I’ve been making steady progress on it in the background, I sunk some good time into it last week and finished a lot of the front matter tying the different sections together, but there’s still a long way to go. If you want to see where I’m up to with it, check out the specification (revision 3) page, but be warned that it’s a work in progress. It’s literally just the text file as I write it on my computer.

bedrock-matlab

I mentioned the course at uni where we’re learning how to write programs in MATLAB, and as part of that we’re slowly going through and learning the language from scratch. I’m a busy guy, so to learn the language quickly I sat down and used it to write a Bedrock emulator. I find that emulators really get you digging to the edges of a language.

MATLAB is a programming language, yes, but it’s quite different in many ways to almost any other language used today. For starters, it’s not free to use, which isn’t as much of a surprise as it could be given that it came out sometime in the 80’s when programming tools cost good money. The privilege of using MATLAB comes at the eye-watering cost of a couple thousand dollars a year (the uni foots the bill, so I get to use it for free). The language is also irrevocably bound to a huge, slow IDE — this is because MATLAB isn’t exactly a language for writing programs, per se, it’s more of a very complex programmable calculator. Programs are just scripts that run line-by-line in a REPL environment, variables declared in the global namespace persist across runs (the ‘workspace’ is a kind of persistent scratch pad), and a lot of standard programming constructs are provided only reluctantly (for and while loops are to be used as a last resort, they can run ~100 times slower than vectorised operations). Really, if you want to get the most out of MATLAB then you really want to be doing everything with matrices, it’s all in the name (MATrix LABoratory).

Another reason for writing the emulator was to test how hard it is to implement Bedrock using languages with weird semantics. A few quirks of MATLAB that made this particularly interesting are that arrays are indexed from 1 (Bedrock memory starts from 0), that values saturate at bounds (no wrapping arithmetic), and that functions are always pass-by-value (annoyingly verbose to mutate variables with functions). The couple of patches of undefined behaviour in the Bedrock spec came through well here: instead of having to implement bounds checking or wrapping arithmetic on the stack pointers, I can just use a plain increment or decrement and trust that they won’t blow out, keeping the code more performant than it otherwise would be.

Here’s a screenshot of the almost-final emulator running inside MATLAB. The top region shows a section of the processor code, the bottom region shows the processor tests running with a few failures. That’s all cleaned up now, all of the tests pass in the final version.

Benchmarking

After I finished the emulator, I tested how fast it is by running the no-math numbers benchmark program. This is a program I’ve had around for a while now: it iterates over every number from 65535 to 0, converting each to a decimal string using the double-dabble algorithm and then printing those strings to the terminal. It deliberately doesn’t use the math device for this, partially in order to be a slower benchmark and partially so that you only need to implement port 0x86 to be able to use it.

Under the regular bedrock-pc emulator (written in Rust), the benchmark takes 0.3 seconds to run on a circa 2011 desktop computer. Under bedrock-matlab, it takes roughly an hour (extrapolated, I didn’t wait for the program to complete). Multiplying it out, we can see that bedrock-matlab is around 10,000x slower than bedrock-pc. Amazing.

If for some reason you have access to a MATLAB subscription (a niche audience already) and you want to use it to run a very slow implementation of Bedrock (I couldn’t imagine why you would), you can find the files at bedrock-matlab. Try not to have too much fun with it.

Thanks

Alright, I need to get back to studying now. Thanks for the support, it’s always easier to find the time to work on this stuff when I’ve got people watching me. Till next time!