Figuring out the TD4 4-bit DIY CPU made from logic chips...

https://emalliab.wordpress.com/2025/09/18/td4-4-bit-diy-cpu/

#TD4

TD4 4-bit DIY CPU

I was looking for DIY CPU projects, as I like kits that help me think at the lowest level of processing. It helps keep me grounded in how far technology has come over the years. Some of the options…

Kevin's Blog

My TD4 kit has arrived.

And wow, that really is a lot of diodes...

#TD4

It works! That was a great build :)

Details here: https://emalliab.wordpress.com/2025/09/28/td4-4-bit-diy-cpu-part-2/

#TD4

A simple 4-bit "Larson Scanner" - just a sequence of OUT instructions and a jump.

https://makertube.net/w/gwjtRveafD98M4PdqEcaoR

#TD4

TD4 4-bit CPU "Larson Scanner"

PeerTube

And now with some notes on programming and some example programs.

BTW - Anyone think of a way to multiply two numbers given only two registers and no other storage...?

https://emalliab.wordpress.com/2025/10/01/td4-4-bit-diy-cpu-part-3/

#TD4

TD4 4-bit DIY CPU – Part 3

Having now spent some time thinking about and building my TD4 4-bit CPU this has a look at some code that I can run on it. Part 1 – Introduction, Discussion and Analysis Part 2 – B…

Kevin's Blog

Now with added bling! Part 4 has some simple hardware mods that add LEDs in some useful places to help debugging.

https://emalliab.wordpress.com/2025/10/05/td4-4-bit-diy-cpu-part-4/

#TD4

Returning to this for a moment, this is my own PCB with built-in bling!

https://emalliab.wordpress.com/2025/10/29/td4-4-bit-diy-cpu-part-5/

#TD4

Kevin's TD4 4-bit CPU

PeerTube

And here is my PCB alongside the original.

#TD4 #4bit

@emalliab Maybe it's possible to add the logarithm of both? Don't know if it's possible to do that accurately.

@Segebodo I'd guess that would need some kind of look up table to do?

To be honest, my solution so far is a look up table for the only four sums I'm actually able to process anyway on a 4-bit computer with a single input :)

@emalliab Does the Add command use both registers or add the value straight from the program code to one register?
If that's the case you can run a for loop for multiplication.
@emalliab I just discovered your blog and I'm gonna read further before writing more...
@emalliab I think there's an error in the instruction set:
shouldn't it be 0000 instead of data?
(https://emalliab.wordpress.com/2025/09/18/td4-4-bit-diy-cpu/)

@emalliab So here's a program that will multiply two numbers x and y that are written into the code:

MOV B <x>

ADD B 15 //for (b=x-1; b>=0; b--)
JNC 5
ADD A <y> //a += y
JMP 1

MOV B A //output
OUT B

@emalliab Since one side of the adder is always connected to the ROM the input for the multiplication has to be in ROM.
The instruction set is really impractical for doing maths. It would be quite useful for array operations but since the ROM is so small has little use.
@Segebodo Officially these are specified as zero to define the instruction, but as far as I can see, as the adder is used to load registers (including PC) in reality if there is immediate data here it can be added.

@emalliab Here's a program that sums the log 2 of any amount of consecutive inputs.
It uses

IN A, x (which is A = IN + data)
JNC label

to implement

if A <= (15-x) goto label

@emalliab
//multiply any amount of numbers
//returns the sum of the approximate log 2 values (log2 of the product)
//any input that's zero is ignored
next_number:
OUT B //show result
OUT 15 //signal for next input

IN A 14
JNC next_number //if x<=1 -> log2(x)=0
ADD B 1
IN A 12
JNC next_number //if x<=3 -> log2(x)=1
ADD B 1
IN A 9
JNC next_number //if x<=6 -> log2(x)=2
ADD B 1
IN A 3
JNC next_number //if x<=12 -> log2(x)=3
ADD B 1
JMP next_number //else log2=4

@emalliab
I used the assembler compiler and simulator from https://www.philipzucker.com/td4-4bit-cpu/
and made a lot of changes (added compiler errors, added label functionality).
Here's the python code:

https://www.onlineide.pro/playground/share/9e52afd6-ab3f-4f06-946a-e9eb05cc307d

(Link expires after 1 month)
I don't have the full or claim any copyright for the code.

Guide to the TD4 4-bit DIY CPU

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.

Hey There Buddo!
@Segebodo Haha - great - I'll take a look a bit later :)

@Segebodo I've added your algorithm to the post - many thanks!

Am right though - the answer is the log2 sum answer?

Anyway, all good fun :)