Simulator for the hypothetical CPU mentioned in the materials for Computer Systems at Loughborough University.
When you have a program, click Run to see register states, data memory and output.
Click line numbers in program memory to set breakpoints. When running, if a breakpoint is hit you can continue execution or run instructions one step at a time.
Examples & Instruction Set
Lecture Instruction Set
LOAD1 <address>- Load
LOAD2 <address>- Load
data[DP1] + data[DP2]in
data[DP1] - data[DP2]in
data[DP1] * data[DP2]in
JUMP <address>- Set
data[DP1] != 0
INPUT "prompt"- Set
data[DP1]to prompted number
STOP- Halt program exection
LSHIFT <steps>- Put
data[DP1] << <steps>in
RSHIFT <steps>- Put
data[DP1] >> <steps>in
round(data[DP1] / data[DP2])in
data[DP1] ^ data[DP2]in
data[DP1] | data[DP2]in
data[DP1] & data[DP2]in
data[DP1] % data[DP2]in
OUTPUTbut converts numeric value to ASCII
Credit mostly goes to Dr. Daniel Reidenbach who wrote the module materials, without which I wouldn't have written this.
If you have any issues, get in touch
First Program Walkthrough
Ok, so you have no clue what this is or how to use it? Fear no longer for help is at hand.
Basically this is just a little simulator (a script in wolfs clothing) for CPU-S, which is the very basic and idealised CPU defined in the lecture notes for module COA124/COA126 (Computer Systems and Computer Systems for Engineers) at Loughborough University (or at least it was in ~2007).
So, if you check out This Diagram of what this script is pretending to be you might get a better understanding of how things work together.
I'll skip over a full explanation as I assume we all went to lectures...right? So you& already know what registers are what and what they do. Thus we can now skip straight to the joys of programming!
Lesson 1: Basic Assembly
Assembly is just one rung higher on the ladder of computer languages above machine code, which is as low level as you get. I chose to keep this simulator at the assembly level as that is all we dealt with in the lectures and I wasn't up for spending more time writing an assembler and coming up with opcodes for each instruction.
Assembly generally consists of instructions and parameters. Instructions, usually mapping directly to opcodes, can have parameters following on the same line, e.g.
ADD 1,2 However, CPU-S is VERY simple, so only one parameter at a time, for instructions that take parameters. That's about all, check the Instruction Set above for more details on what instructions are available, what they do and what parameter is needed, if any.
Lesson 2: Memory
In a modern computer memory is generally treated as one block, be it opcodes for the program, memory that has been cached or even external devices sometimes. However CPU-S has two separate memory modules; one for the program and one for the data that the program manipulates. Hence the two boxes above titled "Program memory" and "Data memory".
Lesson 3: Programming a basic counter
Adding numbers is too basic and Hello World would be pointless as the lecture notes keep CPS-S as a numerical system with no ASCII character output. Instead let's write a simple program that counts from one number up to another.
First we need to work out some basic pseudocode:
from := 5 to := 10 count := from increment := 1 DO: OUTPUT count count := count + increment WHILE count != to
This should do, it enters a loop, outputs the current count, increments the count and continues the loop while the count has not yet reached the upper limit. From that simple bit of code we can list some variables, or data to work on. Data? Data memory! I see a connection...
So in the data memory for our CPU-S program we need four things: Where we start counting from, what we count up to, a variable to count up with and a variable to increment it.
So our data memory could look like this (comments start with a #):
5 # from 10 # to 0 # count 1 # What we increment with
Now on to the program. First that count variable, in our pseudocode it was created with the value of from but we can't do that with assembly. To make it equal to the starting value we need to copy it. How do we do that though, there isn't any copy command? Why not load a 0, the value, add them and store where the 0 was (or in a different place on other occasions)? Sounds good:
LOAD1 2 LOAD2 0 ADD STORE
Now we copied data cell 0 into data cell 2. Next we need to start the do loop. It's fairly simple; we need to output the count and increment the counter:
LOAD1 2 OUTPUT LOAD1 2 LOAD2 3 ADD STORE
Then we need to do the conditional jump of the while part (Read how
JUMP works if you forgot already). You'll also note I store the result of the comparison in a new data cell, so as not to interfere with current data:
LOAD1 1 LOAD2 2 SUB LOAD1 4 STORE JUMP [[ To the start of the previous segment ]]
And then we end it:
So the full program would be (Program data):
LOAD1 2 LOAD2 0 ADD STORE LOAD1 2 OUTPUT LOAD1 2 LOAD2 3 ADD STORE LOAD1 1 LOAD2 2 SUB LOAD1 4 STORE JUMP 4 STOP
There you have it. Not the best example and certainly not the best final code but it's written to give some guidelines on various opcodes and how they can be used together to make something more advanced, like a do-while loop.
Click Here to load this code into the editors (Saves copy/paste).
If you don't really get what's going on you should try loading the example with the link above and running it. Then change some of the data variables and see what happens. Perhaps modify the code to count down?
You may also note that I added some extra opcodes myself which aren't used in lectures. With these you can make the code much simpler, but that would be cheating! But then I am a cheater so with a few minor modifications, and some use of the extra instructions I added, like
OUTPUTCHAR, you can modify this code to make a relatively simple Hello World program, Click here to load Hello World. This uses the counter to indirectly load memory at the counter locations and then output the ASCII code as a character with