Programming The Gigatron
Programming The Gigatron
CPU
The CPU implemented by the TTL IC's of the Gigatron has a Harvard architecture, which means that it does not have a shared bus for the ROM and the RAM.
Actually, the ROM is only used to store instructions and there are no instruction to access the ROM directly. (Nevertheless, there is a clever trick by which it is
possible to 'read' data from the ROM.) The CPU is an 8-bit processor with a 14-bit program counter and a 15-bit RAM range. It has three 8-bit registers and an 8-bit
input and output.
CPU instructions
I rewrote the gtemu.c program such that it would print out what the different instruction actually do in some pseudo-C. After a lot of revisions, this resulted in the
following table. The numbers on the rows and columns need to be added together to get the instruction number. I changed the order of the rows such that similar (or
the same) instrucions are grouped together. The 'function' hi returns the high byte of the value. It should be noted that the value used to access the RAM is 'clipped'
to 0x7fff as there is only 32 kilobyte of RAM. The program counter is incremented after all instructions, except the instructions that modify it. In case an instruction
performs two operations, a semi-collon is used for separation.
# 00 # 20 # 40 # 60 # 80 # a0
---+-----------------------+---------------------------+---------------------------+---------------------------+---------------------------+----------
00 # A = oper # A &= oper # A |= oper # A ^= oper # A += oper # A -= oper
04 # A = oper # A &= oper # A |= oper # A ^= oper # A += oper # A -= oper
08 # A = oper # A &= oper # A |= oper # A ^= oper # A += oper # A -= oper
0c # A = oper # A &= oper # A |= oper # A ^= oper # A += oper # A -= oper
10 # X = oper # X = A & oper # X = A | oper # X = A ^ oper # X = A + oper # X = A - o
14 # Y = oper # Y = A & oper # Y = A | oper # Y = A ^ oper # Y = A + oper # Y = A - o
18 # OUT = oper # OUT = A & oper # OUT = A | oper # OUT = A ^ oper # OUT = A + oper # OUT = A -
1c # OUT = oper; X++ # OUT = A & oper; X++ # OUT = A | oper; X++ # OUT = A ^ oper; X++ # OUT = A + oper; X++ # OUT = A -
01 # A = RAM[oper] # A &= RAM[oper] # A |= RAM[oper] # A ^= RAM[oper] # A += RAM[oper] # A -= RAM
05 # A = RAM[X] # A &= RAM[X] # A |= RAM[X] # A ^= RAM[X] # A += RAM[X] # A -= RAM
09 # A = RAM[(Y>>8)|oper] # A &= RAM[(Y>>8)|oper] # A |= RAM[(Y>>8)|oper] # A ^= RAM[(Y>>8)|oper] # A += RAM[(Y>>8)|oper] # A -= RAM
0d # A = RAM[(Y>>8)|X] # A &= RAM[(Y>>8)|X] # A |= RAM[(Y>>8)|X] # A ^= RAM[(Y>>8)|X] # A += RAM[(Y>>8)|X] # A -= RAM
11 # X = RAM[oper] # X = A & RAM[oper] # X = A | RAM[oper] # X = A ^ RAM[oper] # X = A + RAM[oper] # X = A - R
15 # Y = RAM[oper] # Y = A & RAM[oper] # Y = A | RAM[oper] # Y = A ^ RAM[oper] # Y = A + RAM[oper] # Y = A - R
19 # OUT = RAM[oper] # OUT = A & RAM[oper] # OUT = A | RAM[oper] # OUT = A ^ RAM[oper] # OUT = A + RAM[oper] # OUT = A -
1d # OUT = RAM[(Y>>8)|X++] # OUT = A & RAM[(Y>>8)|X++] # OUT = A | RAM[(Y>>8)|X++] # OUT = A ^ RAM[(Y>>8)|X++] # OUT = A + RAM[(Y>>8)|X++] # OUT = A -
02 # /*nop*/ # /*nop*/ # /*nop*/ # A = 0 # A *= 2 # A = 0
06 # /*nop*/ # /*nop*/ # /*nop*/ # A = 0 # A *= 2 # A = 0
0a # /*nop*/ # /*nop*/ # /*nop*/ # A = 0 # A *= 2 # A = 0
0e # /*nop*/ # /*nop*/ # /*nop*/ # A = 0 # A *= 2 # A = 0
12 # X = A # X = A # X = A # X = 0 # X = 2*A # X = 0
16 # Y = A # Y = A # Y = A # Y = 0 # Y = 2*A # Y = 0
1a # OUT = A # OUT = A # OUT = A # OUT = 0 # OUT = 2*A # OUT = 0
1e # OUT = A; X++ # OUT = A; X++ # OUT = A; X++ # OUT = 0; X++ # OUT = 2*A; X++ # OUT = 0;
03 # A = IN # A &= IN # A |= IN # A ^= IN # A += IN # A -= IN
07 # A = IN # A &= IN # A |= IN # A ^= IN # A += IN # A -= IN
0b # A = IN # A &= IN # A |= IN # A ^= IN # A += IN # A -= IN
0f # A = IN # A &= IN # A |= IN # A ^= IN # A += IN # A -= IN
13 # X = IN # X = A & IN # X = A | IN # X = A ^ IN # X = A + IN # X = A - I
17 # Y = IN # Y = A & IN # Y = A | IN # Y = A ^ IN # Y = A + IN # Y = A - I
1b # OUT = IN # OUT = A & IN # OUT = A | IN # OUT = A ^ IN # OUT = A + IN # OUT = A -
1f # OUT = IN; X++ # OUT = A & IN; X++ # OUT = A | IN; X++ # OUT = A ^ IN; X++ # OUT = A + IN; X++ # OUT = A -
Next, I wrote a progam to parse the theloop.asm file to discover which instructions are actually used. This resulted in the following output given below. In the first
column the hexadecimal instruction number, in the second column the number of times the instruction is mentioned in the file, in the third column the pseudo-C
code, and in the last column the mnemonic being used in the file. $00 stands for the operand.
www.iwriteiam.nl/PGigatron.html 1/5
27/12/2019 Programming the Gigatron
81 28: A += RAM[oper] // adda [$00]
82 36: A *= 2 // adda ac
85 2: A += RAM[X] // adda [x]
89 6: A += RAM[(Y<<8)|oper] // adda [y,$00]
8d 4: A += RAM[(Y<<8)|X] // adda [y,x]
90 11: X = A + oper // adda $00,x
91 1: X = A + RAM[oper] // adda [$00],x
92 2: X = 2*A // adda ac,x
95 1: Y = A + RAM[oper] // adda [$00],y
vCPU
The virtual CPU has a Von Neumann architecture.
vCPU instructions
The ROM implements a virtual 16-bit CPU, which runs when the CPU is not busy with generating the VGA signal. At first I did not understand how these
instructions are encoded. It took me some time to understand that they are simply offset in one of the memory segments. This means that there are actually 256
instructions implemented by the vCPU, many of which do nothing usefull (or might have make the gigatron crash). Later, I discovered that the offset mechanism is
actual mentioned in one of the blogs on the website.
The vCPU has an accumulator (below denoted by vA, stored at memory location 0x18 and 0x19), a program counter (below denoted by vPC, stored in memory
locations 0x16 and 0x17), a stack pointer (below denoted by vSP, stored in memory location 0x1c), and a return address (below denoted by vLR, stored in memory
locations 0x1a and 0x1b). The vCPU instructions are described in GCL-language.txt. Using pseudo-C, the instructions, followed by their opcodes in brackets, can be
described as follows:
ST(0x5e) oper RAM[oper] = lo(vA);
STW(0x2b) oper RAM[oper] = lo(vA); RAM[oper+1] = hi(vA);
STLW(0xec) oper RAM[vSP+oper] = lo(vA); RAM[vSP+oper+1] = hi(vA);
LD(0x1a) oper vA = RAM[oper];
LDI(0x59) oper vA = oper;
LDWI(0x11) op1 op2 vA = op1 | (op2<<8);
LDW(0x21) oper vA = RAM[oper] | (RAM[oper+1]<<8)
LDLW(0xee) oper vA = RAM[vSP+oper] | (RAM[vSP+oper+1]<<8)
ADDI(0xe3) oper vA += oper
ADDW(0x99) oper vA += RAM[oper] | (RAM[oper+1]<<8)
SUBI(0xe6) oper vA -= oper
SUBW(0xb8) oper vA -= RAM[oper] | (RAM[oper+1]<<8)
ANDI(0x83) oper vA &= oper
ANDW(0xf8) oper vA &= RAM[oper] | (RAM[oper+1]<<8)
ORI(0x88) oper vA |= oper
ORW(0xfa) oper vA |= RAM[oper] | (RAM[oper+1]<<8);
XORI(0x8c) oper vA ^= oper;
XORW(0xfc) oper vA ^= RAM[oper] | (RAM[oper+1]<<8);
INC(0x93) oper RAM[oper]++;
PEEK(0xad) vA = RAM[vA];
DEEK(0xf6) vA = RAM[vA] | (RAM[vA+1]<<8);
POKE(0xf3) oper RAM[RAM[oper]|(RAM[oper+1]<<8)] = lo(vA);
DOKE(0xf3) oper addr = RAM[oper]|(RAM[oper+1]<<8); RAM[addr] = lo(vA); RAM[addr+1] = hi(vA);
LSLW(0xe9) vA = vA << 1
BCC(0x35) EQ(0x3f) addr if (vA == 0) vPC = hi(vPC)|addr;
BCC(0x35) GT(0x4d) addr if (vA > 0) vPC = hi(vPC)|addr;
BCC(0x35) LT(0x50) addr if (vA < 0) vPC = hi(vPC)|addr;
BCC(0x35) GE(0x53) addr if (vA >= 0) vPC = hi(vPC)|addr;
BCC(0x35) LE(0x56) addr if (vA <= 0) vPC = hi(vPC)|addr;
BCC(0x35) NE(0x73) addr if (vA != 0) vPC = hi(vPC)|addr;
BRA(0x90) vPC = hi(vPC)|addr;
LUP((0x7f) oper vA = ROM[vA | (oper<<8)];
www.iwriteiam.nl/PGigatron.html 2/5
27/12/2019 Programming the Gigatron
The SYS instruction executes the system call whoes address is stored in sysFn (memory locations 0x22 and 0x23). The arguments for the system call are stored in
sysArgs (the eight memory locations 0x24 to 0x2b). The system calls are:
primary_expr
: ident
| int
| char
| "(" expr ")"
.
www.iwriteiam.nl/PGigatron.html 3/5
27/12/2019 Programming the Gigatron
postfix_expr
: postfix_expr "[" expr "]" [array]
| postfix_expr "(" assignment_expr LIST OPT ")" [call]
| postfix_expr "++" [post_inc]
| postfix_expr "--" [post_dec]
| primary_expr
.
unary_expr
: "++" unary_expr [pre_inc]
| "--" unary_expr [pre_dec]
| "-" cast_expr [min]
| "~" cast_expr [invert]
| "!" cast_expr [not]
| postfix_expr
.
cast_expr
: "(" simple_type ")" cast_expr [cast]
| unary_expr
.
conditional_expr
: l_expr9 "?" l_expr9 ":" conditional_expr [if_expr]
| l_expr9
.
assignment_expr
: unary_expr "=" assignment_expr [ass]
| unary_expr "+=" assignment_expr [add_ass]
| unary_expr "-=" assignment_expr [sub_ass]
| unary_expr "<<=" assignment_expr [sl_ass]
| unary_expr ">>=" assignment_expr [sr_ass]
| unary_expr "&=" assignment_expr [and_ass]
| unary_expr "|=" assignment_expr [or_ass]
| unary_expr "^=" assignment_expr [exor_ass]
| conditional_expr
.
simple_type
: "word" [word]
| "byte" [byte]
.
func_decl
: ("void"[void]|simple_type) ident "(" (simple_type ident)LIST OPT ")"
"{" statement SEQ OPT "}" [funcdef]
.
statement
: "{" statement SEQ OPT "}"
| simple_type ident ("=" conditional_expr) OPT ";" [vardecl]
| expr OPT ";" [expr]
| "if" "(" expr ")" statement ("else" statement) OPT [if]
| "while" "(" expr ")" statement [while]
| "do" statement "while" "(" expr ")" ";" [do]
| "for" "(" (simple_type OPT ident "=" expr)OPT ";"
expr OPT ";"
expr OPT ")" statement [for]
| "continue" ";" [cont]
| "break" ";" [break]
| "return" expr OPT ";" [ret]
.
while (true)
{
for (word h = 1; h < 199; h++)
for (word v = 1; v < 199; v++)
{
word c = 0;
word vk = v - 1;
if (getScreen(h-1,vk) < 0) c++;
vk++;
if (getScreen(h-1,vk) < 0) c++;
vk++;
if (getScreen(h-1,vk) < 0) c++;
vk -= 2;
if (getScreen(h,vk) < 0) c++;
vk++;
word m = getScreen(h, vk) & 0x3f;
vk++;
if (getScreen(h,vk) & 0x3f != 0) c++;
vk -= 2;
if (getScreen(h+1,vk) & 0x3f != 0) c++;
vk++;
if (getScreen(h+1,vk) & 0x3f != 0) c++;
vk++;
if (getScreen(h+1,vk) & 0x3f != 0) c++;
if (m != 0)
{
if (c == 2 || c == 3)
m = 0xff
else
m = 0xd0;
}
else
{
if (c == 3)
m = 0x3f;
else
m = 0;
}
setScreen(h, v, m);
}
}
www.iwriteiam.nl/PGigatron.html 5/5