PaperCPU is a simple CPU architecture where programs and execution steps can be written on paper or a whiteboard, designed for teaching and learning purposes. The idea comes from computer educational aids used in the 1950s–70s, with which students would write programs and manually simulate register operations to help learn how computers work, which was useful when actual machines were too expensive or inaccessible. Of course nowadays computers are widely accessible, so this site provides a simulator for the CPU, allowing students to experiment either on paper or directly in the browser.
To use the simulator, simply write your program on the left and press 'Run Program'. You can pause the execution by pressing 'Pause' and step through instructions one at at time using 'Step'. You can adjust the speed using the speed slider, and when the program wants input the CPU will pause and highlight the input box green, when done writing a number you press 'Set' and the CPU will read the input and continue.
To use the PaperCPU on paper, print this linked PDF on a sheet of 8.5x11 paper, or draw a similar design on paper or whiteboard. Write the instructions for your program to the left and step through each instruction, writing the result of the operations in the registers. You can use a calculator to do any math as needed.
To the left is the program editor. The CPU has an internal 'program counter' (PC) that keeps track of the currently executing instruction, which is highlighted in green when running. When working on paper, you can use a paper cut-out arrow or any pointing object to mark the current instruction. The line numbers on the left correspond to instruction numbers and help when using jump instructions. (Note: the simulator currently doesn't number blank lines correctly, so keep that in mind when writing code.)
To the right, the CPU provides 8 general purpose registers and an input and output register. Each register holds a floating-point value. On paper you can be flexible and write values as integers or decimals, or write them in different number bases, but the javascript simulator implements them using JavaScript’s 'Number' type and displays them as base 10 decimals.
These instruction do arithmetic and logical operations and operate on the 8 registers. They support register and immediate addressing modes:
Instruction | Description | Example |
---|---|---|
mov | Move a value into a register | mov r0, #10 |
not | Bitwise NOT | not r0, r1 |
and | Bitwise AND | and r0, r1, #255 |
or | Bitwise OR | or r0, r1, r2 |
add | Add two values | add r0, r1, #1 |
sub | Subtract second value from first | sub r0, r0, #1 |
mul | Multiply two values | mul r1, r1, r0 |
div | Divide first value by second | div r0, r0, #2 |
rem | Remainder (modulo) of division | rem r0, r1, #3 |
Note on bitwise operations: JavaScript performs bitwise operations (and, or, not) by converting values to 32-bit signed integers internally, does the operation, then converts them back to a float. On paper, round the values down to the nearest integer (floor operation) then apply the bitwise operation.
Control flow instructions change the order of execution by setting the PC to a specific line in the program. Jump instructions can be unconditional or conditional, and conditional jumps have register or immediate addressing modes.
Instruction | Description | Example |
---|---|---|
jmp | Jump to line number | jmp 10 |
jeq | Jump if equal | jeq r0, #0, 5 |
jneq | Jump if not equal | jneq r1, r2, 3 |
jgt | Jump if greater than | jgt r0, #10, 8 |
jgte | Jump if greater than or equal | jgte r1, r2, 12 |
jlt | Jump if less than | jlt r0, #1, 4 |
jlte | Jump if less than or equal | jlte r0, r1, 6 |
These instructions send or recieve values from the input and output registers and allow for the program to interact with the user.
Instruction | Description | Example |
---|---|---|
in | Read value into register from input register | in r0 |
out | Write register value to output register | out r1 |
This instruction halts execution. If the CPU reaches the end of the program and there's no 'hlt' instruction, the CPU will show an error message.
Instruction | Description | Example |
---|---|---|
hlt | Stop execution | hlt |
becomes:
Asks for the first input, then the second input, then the operation, where 0 = add, 1 = sub, 2 = mul, 3 = div.
becomes:
The user thinks of a secret number 1-100, and the CPU will try to guess it. If its guess is too low, enter 0, if too high, enter 1, if it's correct, enter 2.
becomes:
Code is under MPL, media is under CC-BY-SA. Code is at https://codeberg.org/bobbbob/papercpu