Pangram verdict · v3.3
We believe that this document is fully human-written
AI likelihood · overall
HumanArticle text · 1,683 words · 7 segments analyzed
I bought a cute little 4 bit cpu kit from Aliexpress called the TD4. It has 2 registers, some LEDs, and 16 bytes of program ROM. Quite limited but still very cool and teaches a lot of principles of computer architecture.
The documentation, schematics, and pictures for this cpu are here https://github.com/wuxx/TD4-4BIT-CPU. It’s a little sparse though. I can imagine a student getting overwhelmed. So I thought it would be helpful to write some longer form notes.
Building
We took two sessions just soldering it up.
This image
plus the schematic were enough instructions such that it wasn’t too hard.
The directionality of the surface mount diodes gave us pause, but we used a diode tester functionality in our multimeter to figure it out. The tiny green line on the front of the diode is towards the bottom of the board. I believe the line was on the back of the diode was towards the top, but it is soldered down now so I can’t check.
The thing that gave us the most trouble was soldering on the USB connector. I’d advise doing this before putting on the IC sockets, because it got in the way of the iron. The middle pins of the USB are unconnected, so you can just blast them if need be. The USB is only for power.
Which chip goes where can be seen by inspecting the schematic for part number and IC number. The notch aligns with the notch printed on the PCB board. It basically worked the first time, minus an intermittent power connection on the usb.
Soldering all those diodes sucked.
How it Works
At a a high level here’s how it works
The program ROM is a bank of 16 dip switches. This is the entire size of programs available to you, which makes it tricky to do much. Pins 5-8 of each dip switch are an opcode, selecting between ADD, MOV, IN, OUT, JNC, JMP instructions. These bits go through some combinatorial circuit to control
What signal enters the addition unit (input operand of instruction) Which register latches in the output of the addition unit. (The output operand of the instruction)
Bits 1-4 of the dip switch are the immediate value for the instruction, which is always piped into the adder.
The data selector can also select a hard wired zero signal (ground). This is used for example by the MOV A,Im instruction. This MOV instruction is implemented by adding Im to 0 and clocking the result into A.
The order of the immediate and command bits is a bit confusing. Sometimes it feels like they are reversed. The least significant bit is the low labelled one on the dip switch. For example, the value 3 is entered in as 1100 on the first 4 bits of the dip switch.
A Little Deeper
Address Decoder
The address decoder chip IC11 is a demultiplexer that takes in 4bit signal and drives the one of it’s 16 output signals low that corresponds to this number. When driven low, current can flow through that dip bank’s diodes if the dip switch is connected. Otherwise, pullup resistors R21-R26 keep the signal high.
IC12 is essentially a buffering inverter circuit for the result of the ROM.
Command Decoder
The command bits are translated by discrete combinatorial logic chips in IC8 and IC10 into not_LOAD0,1,2,3 and SEL_A/SEL_B signals. Really, not_LOAD0,1,2,3 could be called not_LOADA,B,Out,PC since that is what they are hooked up to.
instruction bit7-bit4 bit3-bit0 C SEL_B SEL_A #LOAD0/A #LOAD1/B #LOAD2/IN #LOAD3/PC ADD A, Im 0000 Im X L L L H H H MOV A, B 0001 0000 X L H L H H H IN A 0010
0000 X H L L H H H MOV A, Im 0011 Im X H H L H H H MOV B, A 0100 0000 X L L H L H H ADD B, Im 0101 Im X L H H L H H IN B 0110 0000 X H L H L H H MOV B, Im 0111 Im X H H H L H H OUT B 1001 0000 X L H H H L H OUT Im 1011 Im X H H H H L H JNC Im 1110 Im L H H H H H L JNC Im 1110 Im H X X H H H H JMP Im 1111 Im X H H H H H L
Src
Data Selector
SEL_A and SEL_B are wired into the data selector chips IC6 and IC7.
They select from 4 possible options,
A B In Operand 0 0 A 0 1 B 1 0 In 1 1 Zero
The Zero signal is hardwired to ground.
Adder
The 4 bits selected output to the Adder chip IC8, which also receives the immediate bits1-4 from the ROM bank selected. A side note, The immediate bits are read by more instructions than might be apparent from their pneumonic. For example, OUT B actually still adds in the immediate. This functionality could be useful but also confusing. By default, zero out the immediate bits if you don’t want them.
Registers
The registers A,B,Out,PC are all counter chips. Only PC has the counting functionality enabled. On the edge of the clock signal, if the not_LOADx signal is low, data will latch in from pins A,B,C,D into the output pins QA,QB,QC,QD. JMPs are implemented by moving values into the PC register, which is fed back into the ROM address decoder.
Carry Flip Flop
An interesting piece is the Carry circuit which has a D flip flop for storing the overflow carry bit from the previous operation. This is needed to implement the JNC “jump if no carry” functionality. This operates by feeding into the command decoder circuit and disabling the LOADPC coming from the instruction if there is a carry.
Note that there appears to be a error on this version of the schematic for the command decoder. Can you find it?
Clock
Down in the bottom right is the clock circuit. The switches change from manual clock to auto clock and also change the clock speed. The clock is a RC controlled multivibrator. I haven’t actually seriously analyzed it.
Simple Programs
We ran a couple experimental programs. First its good to try something very simple.
For reference again, this is the instruction set
instruction bit7-bit4 bit3-bit0 ADD A, Im 0000 Im MOV A, B 0001 0000 IN A 0010 0000 MOV A, Im 0011 Im MOV B, A 0100 0000 ADD B, Im 0101 Im IN B 0110 0000 MOV B, Im 0111 Im OUT B 1001 0000 OUT Im 1011 Im JNC Im 1110 Im JMP Im 1111 Im
Output to LED
Try getting this program to work OUT 0101
Looking up the opcode for OUT Im on the table it is 1011.
This translated to the first dip switch being in 1010 1101 The first piece is the constant immediate, and the second piece is the reverse of that opcode.
Simple Flashing
Now we can try a simple blinking program with a jump.
We output two different constants and then JMP back to instruction 0
Which translates to
# Im | Com #------|----- OUT 0001 # 1000 | 1101 OUT 0010 # 0100 | 1101 JMP 0 # 0000 | 1111
Counting Up
Now we can trying counting
ADD B 1 OUT B JMP 0
Counting Down
Counting down can be done once you realize that adding 15 is the same as subtracting 1 modulo 16. require realizing modular arithmetic
ADD B 15 OUT B JMP 0
Counting Up then Down
This is much trickier strangely. We screwed up a lot getting this simple program to work. In our defense we were tired. But remember to update your jump addresses, put things in the right order, manually assemble, it’s a lot.
Mistakes we made
we fed will ice cream too early. We flipped the labels going into JNC vs JMP We had the OUT in places they couldn’t be executed (at the bottom of the program) We moved OUT after the add, destroying the carry for ADD A 1 We realized we should think of ADD B 1, OUT B a a single unit. A pseudoinstruction. And in fact after examining the circuit, thee two can be compressed to OUT B 1 because OUT actually takes immediates. We lucked out that we zeroed the immediates on MOV and OUT.
Remember the program counter was 0 indexed
00: ADD B 1 1000 1010 01: OUT B xxxx 1001 02: MOV A B xxxx 1000 # test if at 15 03: ADD A 1 1000 0000 # adding 1 to 15 makes a carry 04: JNC 0 0000 0111 # jump to 0 if no overflow (B !
= 15) 05: ADD B 15 1111 1010 # Go down 1 by adding 15 06: OUT B xxxx 1001 07: MOV A B xxxx 1000 # test if at 0 08: ADD A 15 1111 0000 # only 0 does not overflow when add 15 09: JNC 0 0000 0111 # jump to ascending at 0 if not overflow (B = 0) 10: JMP 5 0110 1111 # default jump to 5
An alternative possibly buggy version. Using the idea JC ~ JNC;JMP and can we directly test the overflow on B without using A as scratch? Do we miss some numbers though? The ends of the loop are tricky.
00: ADD B 1 01: JNC 3 02: JMP 5 03: OUT B 04: JMP 0 05: MOV B 15 06: ADD B 15 07: JNC 0 08: OUT B 09: JMP 6
Python Assembler and Simulator
Here’s a little python simulator and assembler that Ben and I slammed out. Well, Ben mostly wrote. Maybe using regex like this is a bit goofy. Maybe its nice? match statements rule. We should return to this and make it better, but it’s a start. There is also an assembler in the TD4 repo but I haven’t tried using it.
import re from collections import namedtuple
from types import SimpleNamespace
instruction_regex = re.compile(r"(?P<cmd>ADD|OUT|MOV|JMP|JNC|IN) +(?P<arg1>A|B|\d{1,2})(?: +(?P<arg2>A|B|\d{1,2}))? *(?:#.*)?")