A TDD approach for building a simple microprocessor model in Python

September 20, 2017



Modern microprocessors are a very complex piece of machinery with a lot of different parts. I've learned assembler and microprocessors in my undergraduate course some years ago but, as I've been working on higher level software languages for quite some time now, I forgot many things. Lately I decided to revisit some of the topics on microprocessors and to build a very simple model of a microprocessor in Python for education purposes (I work as CS professor). You can find the final source code and some samples at https://github.com/joaoventura/simproc.

The "simple microprocessor" is a test-driven development approach to the implementation of a model of a very basic microprocessor in Python 3. It's purpose is to illustrate the basic components of a hardware microprocessor, such as simple instructions, memory, registers and the instruction pointer. In the end it allows to build very simple programs such as the following one to add two numbers (5 and 3):

MOV 0, 5 
MOV 1, 3
INC 0
DEC 1
MOV r0, [1]
JNZ 2
MOV r0, [0]

Basically, it keeps incrementing by one the value in memory position [0] and decrementing by 1 the value in memory position [1] until value in [1] reaches zero. At that point [0] has the sum of both values, which is moved to register r0. For those that know NASM, I use a similar syntax.

I've made a video documenting the development of this model. I think it is quite illustrative but as I'm not an english native speaker and I'm a little bit rust, the video is a tad long, sorry for that! :)

Source code and some samples at https://github.com/joaoventura/simproc.