Emulating Simple CPUs Using Arduino

In my exploration of minimal instruction set (MISC) cpus, it is often a worthwhile exercise to define the instruction set and then simulate the behaviour of the cpu in software.

This can be done relatively straightforwardly using even a humble Arduino to mimic the operation of the cpu. A couple of evenings programming and you have a simple simulator – that can readily be changed to suit new architectures or new instruction sets. Whist the original Arduino is resource limited – the same code will run on the MEGA or DUE bards – offering a whole lot of extra RAM – and in the case of the DUE – speed.

The Arduino Uno or Duemillenove has 2Kbytes of RAM – and this is just about enough to write short programs – particularly if the instructions are multiple bytes long.

To create a CPU simulator, we first have to create an array in memory that will hold the program in the form of the machine instructions for the proposed processor. If the cpu needs a 16-bit instruction width,  then for practical purpose this array should only be about 512 words long, which will immediately consume 50%  of the available RAM.

We also have to define the principal registers – including Program Counter (PC), Instruction Register (I),  Accumulator,  and possibly the address of any memory location that is updated by the instruction.

Now we come to the instruction set and architecture (ISA) – and the instruction decoder.  First we need to choose how many instructions, and what operands they will act upon.

For a MISC machine we should really be looking at about 32 or fewer instructions – mostly based on what the chosen ALU can do.  We will also require program branching, loops and other conditional program flow control instructions.

In the case of th J1 Forth CPU – there are just 5 classes of instructions



Conditional Branch

Unconditional Branch

ALU Operation

A true load-store architecture will be able to operate on any location in the available program RAM.  A register based architecture will have a group of registers that are operated on preferentially – either implemented in block RAM on an FPGA or possibly in off chip RAM – occupying the low addresses of the zero page – which minimises the addressing overhead.  Finally  – with a stack based architecture, there will be the top of stack, the next of stack, and some elements in the return stack that have preferential access.

In the case of EDSAC, which is a traditional load-store architecture – all of it’s 1024 words of RAM storage are treated identically by the ALU. However, there is no reason why for improving the programming model – that the firs few words in memory could not be given register names – and used preferentially for moving data about.

Instruction Decoder

The Instruction Decoder can just be based on a switch-case statement. For a machine with just 32 instructions – this can be as little as a few lines of C-code, that calculate the new state of the Accumulator, Program Counter and Memory – based on whatever instruction has been executed.


About monsonite

mostly human
This entry was posted in Uncategorized. Bookmark the permalink.

2 Responses to Emulating Simple CPUs Using Arduino

  1. Richard says:

    It’s perfectly possible that I completely miss the point here.

    To simulate a CPU in software, why do it in a resource constrained environment like the Arduino? Surely it would be easier to create the simulator in some (reasonably) portable high level language, probably C++, and run it in the far larger environment of a standard PC. It would be far easier to instrument in meaningful ways and you’d have a far shorter tool chain involved in the process.

    I feel I am missing something major here.

  2. monsonite says:


    It’s a fair comment – why develop on a resource limited processor when I could use a laptop with almost infinite more resources and better toolchain?

    Well, quite simply, I wouldn’t know where to start coding in C on a laptop. All of my coding experience has been on embedded systems, with limited resources.

    The end game of this project is to develop a tiny development toolkit that will run on virtually any small microcontroller – and so the ATmega328 on an Arduino Uno is representative of the final target system.

    I’m working towards a compact, human readable interpreted language that can be ported within 2K bytes to any micro – and for that you have to get involved with stripped down code and even native assembly language. It sound like wearing a hair shirt – but it’s a means to an end.

    The Arduino IDE and it’s derivatives have given my code access to AVR, MSP430, ARM Cortex M4 and a variety of other processors, something that writing C on a laptop would not have achieved.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s