What is a computer? A computer processes a list of instructions to compute some data. Though this computer is limited, it follows many of the same principles of computers today.
This computer uses 8-Bit values which simply means that all data (variables, memory values, pixels, etc.) is stored as numbers which are sized between 0 and 255. It is customary to view these numbers in hexadecimal with hex ‘digits’ (0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F). In this fashion, 0 becomes 00 and 255 becomes FF
Instructions
The Computer operates off of instructions which each perform small tasks (increment a number, halt,
etc.). These instructions are represented by numbers. As an example, “increment” could be
5E and “halt” 2F.
The computer finds these instructions in the Memory — a 16×16 grid of values. Starting at the top left, the computer will look at each value as an instruction, and sequentially perform the specific task for that instruction. For example, if the memory looked _something_ like:
5E 2F . . .
The computer would first see 5E as the “increment” instruction and increment a
variable. then it would see 2F as the “halt” instruction and stop. This is the
general principle behind the computer: read next instruction, do task, repeat. Many instructions are
available for use. Some are simple arithmetic like “add” or “divide”, and some are
more interesting like “goto” and “set pixel”.
Variables
Of course, the question arises: “what are we incrementing? what numbers are we adding?” The answer lies in the parameters and variables. Some instructions require values as parameters. For example, the earlier example would be correct like:
5E 00 2F . . .
Along with the “increment” instruction (5E), we must tell the computer which
variable should be incremented (00) with a parameter. In this updated example, the
computer would look in memory and first see 5E as the “increment” instruction. The
computer understands that the next value will be the variable to increment. After incrementing variable
number 00 the computer will then see 2F as the “halt” instruction and
stop.
In total the computer has 8 variables (00, 01, 02, 03,
04, 05, 06, 07). Each variable simply contains an 8-bit
value like each value in the memory. At the very top above the memory each variable is listed. Variables
start off as zero.
The TV
The TV is a screen of pixels which can be drawn on automatically by the computer. The TV has 16 rows of pixels and 16 pixels for a total resolution of 16x16. Each pixel is set as an 8-bit value by the computer. Each 8-bit value represents a different color TODO (pals)
A Simple Program
All computation happens on variables. When the computer adds two numbers together, it adds two variables together then puts the result in a variable. Additionally, there is another form of addition which adds a constant parameter value to a variable, and puts the result in the variable. Constant parameters work just like variable parameters except their value is simply the value of the parameter in memory.
Let's write a simple program which:
- Generates a random number in variable
00 - Multiplies the value in variable
00by three - Generates a random number in variable
01 -
On the TV, Paints pixel at position in variable
01with color in variable00 - Ends
Click the “Edit” button to go into edit mode.
When writing programs it's helpful to reference the ISA (TODO what does ISA mean?) (in tab above)
which has details for each instruction. First we need a random number. The ISA has “random
number” as F0:
| Code | Parameters | Action | Description |
| F0 | V | Random Number | Sets variable (P1) to a random value |
Per the ISA, this instruction has the value F0 and takes one variable parameter. We begin our
program by setting the variable 00 to a random number:
F0 00
Then, we want to multiply it by three. The ISA has the multiply instruction:
| Code | Parameters | Action | Description |
| 55 | V C | Multiply | Multiplies the value in variable (P1) by constant value (P2) |
“multiply” takes one variable parameter and one constant parameter. We want to multiply the
variable
00 by 03, so:
F0 00 55 00 03
--------
new
Next we generate the second random variable (01) just like the last.
F0 00 55 00 03 F0 01
-----
new
Now we set the pixel on the TV, the ISA entry for “set pixel” looks like:
| Code | Parameters | Action | Description |
| FE | Pair(VA, V) | Set Pixel | Sets the color value in variable (P2) for pixel at position in variable (P1) |
“set pixel” uses a new type of parameter called a pair variable parameter. Pair
variables parameters are used when an instruction requires two variables. In this case, we have our position
in 01 and our color in 00, so our pair would be 10. The updated
program looks like:
F0 00 55 00 03 F0 01 FE 10
-----
new
Finally, we add a halt instruction:
F0 00 55 00 03 F0 01 FE 10 2F
And the program is complete!
Program LinkClick Step to step through each instruction and parameter to see how the computer views it at it.
Repetition
A single pixel is a bit boring though isn't it? We could modify our program to repeat and fill the TV with a ton of random pixels.
The “goto” instruction tells the computer where in the memory it should look next. If have a
“goto” instruction which tells the computer to look at 00 the program will loop!
(Note, there are bunch of different types of “goto”. refer to the ISA)
| Code | Parameters | Action | Description |
| 21 | CA | Goto | Look for next instruction at memory position (P1) |
We remove the “halt” instruction and replace it with a “goto”:
F0 00 55 00 03 F0 01 FE 10 21 00
-----
new
Program Link
Now, when the computer sees the “goto” instruction 21 and the memory position
parameter 00, it will then look for the next instruction at memory position 00
(turn the speed up from normal to turbo for a little fun)
Conditionals
In the repeating example, we used "goto" to unconditionally go back to the start of the program, in effect creating an infinite loop. If instead we wanted to, for example, paint 20 random pixels and then halt, we would need the conditional "goto if not equal to zero" instruction.
| Code | Parameters | Action | Description |
| 23 | V CA | Goto if Not equal to zero | Look for next instruction at memory address in constant (P2) if the value in variable (P1) ≠ 0 |
We use this "goto ≠ 0" to repeat the program in the same way as the last example, except we additionally keep a counter counting down from 20. When this counter variable reaches zero, the "goto ≠ 0" will stop repeating and 'fall though' to the next instruction, which will be a "halt".
First we need to setup the counter variable (07) to be 14 (20 in decimal) The "Set
Variable" instruction is used.
| Code | Parameters | Action | Description |
| 19 | V C | Set Variable | Assigns constant value (P2) to variable (P1) |
19 07 14
Now, lets reuse of the code from the previous example, with the goto removed
19 07 14 F0 00 55 00 03 F0 01 FE 10
---------------------------
reused
At the end of each repetition, a few things need to happen:
- The counter variable (
07) must be lowered by one -
If the counter (
07) is0, then go back to right after the counter was set to 20 - Else, Halt
The first step uses the "decrement" instruction.
| Code | Parameters | Action | Description |
| 5F | V | Decrement | Decrements the value within variable (P1) by 1 |
19 07 14 F0 00 55 00 03 F0 01 FE 10 5F 07
-----
new
Then, the "goto ≠ 0" instruction to goto the beginning of the program. Note that the memory address the goto
points to is 03 instead of 00. If it was 00, the counter would reset
every repitition.
19 07 14 F0 00 55 00 03 F0 01 FE 10 5F 07 23 07
03
Finally, a "halt" (2F) instruction is added
19 07 14 F0 00 55 00 03 F0 01 FE 10 5F 07 23 07
03 2F
Program Link
Wrapping
One question you might have is: "what happens when a number becomes too big to fit into an 8-bit value?" For example, with the multiplication 16×17=272. In cases like these, the computer wraps the number around using modular wrapping. The simplest way to think about it is to add/subtract 256 until the number is within the valid range.
For example. 600 is way out of range, yet can be wrapped easily like so:
600 - 256 = 344
344 - 256 = 88
Another example, say a subtraction that results in a negative number:
8 - 100 = -92
-92 + 256 = 164