

MicroCPU is a bit-serial 4-bit accumulator CPU that fits in a sky130 'micro' tile. A micro
tile has only a clock, a reset, eight inputs and eight outputs, so the program is kept outside
the chip: a host (the RP2040 on the demo board) reads the program counter from uo_out and
presents the word at that address on ui_in.
The CPU has a 4-bit accumulator A, four 4-bit registers R0 to R3, a carry flag C, a zero flag Z and a 12-bit program counter (PC), a 4096-word address space. The host also keeps the data memory, the input and output ports and the subroutine return addresses: it sees every register, and R1 and R0 together are the 8-bit address into that memory.
An instruction takes four clocks and works on one bit per clock, least significant bit first. Every register rotates one bit per clock and the PC three, so after four clocks they are back in place. Because nothing ever has to hold still, no flip-flop needs a hold multiplexer, and the ALU is one bit wide; that is what makes it fit. A 2-bit phase counter numbers the four clocks 0 to 3; it is 0 after reset.
Reset clears the PC, the phase and the jump state. It does not clear the registers or flags, so a program sets the ones it uses.
uo_out shows different things through an instruction. Together these five are the whole
state of the CPU:
| When | uo_out |
|---|---|
Phase 1 (the second clock), clk high |
R3 in [7:4], R2 in [3:0], as they were before this instruction. If the instruction writes one of them (ST, INC, DEC), that register's bit 0 is already the new one |
Phase 3 (the last clock), clk high |
[4] = C and [5] = Z as they will be after this instruction |
Phase 3, clk low |
[3:0] = PC[11:8] of the next instruction, [7:4] = A as it will be after this one |
Phase 0, clk high |
R1 in [7:4], R0 in [3:0] |
Phase 0, clk low |
PC[7:0] |
At other times uo_out shows the registers and the PC part-way through their rotation.
An instruction is a 4-bit opcode in ui_in[7:4] and a 4-bit operand n in ui_in[3:0]. For
the register instructions the operand is a 2-bit function and a 2-bit register number rr.
| Opcode | n | Instruction | Operation | Flags |
|---|---|---|---|---|
| 0 | n | LDI n | A = n | Z |
| 1 | n | ADI n | A = A + n | Z C |
| 2 | n | ANI n | A = A & n | Z |
| 3 | n | ORI n | A = A | n | Z |
| 4 | n | XRI n | A = A ^ n | Z |
| 5 | n | CPI n | compare A with n | Z C |
| 6 | 00rr | ADD r | A = A + r | Z C |
| 6 | 01rr | ADC r | A = A + r + C | Z C |
| 6 | 10rr | SUB r | A = A - r | Z C |
| 6 | 11rr | SBC r | A = A - r - (not C) | Z C |
| 7 | 00rr | AND r | A = A & r | Z |
| 7 | 01rr | OR r | A = A | r | Z |
| 7 | 10rr | XOR r | A = A ^ r | Z |
| 7 | 11rr | CMP r | compare A with r | Z C |
| 8 | 00rr | LD r | A = r | Z |
| 8 | 01rr | ST r | r = A | |
| 8 | 10rr | INC r | r = r + 1 | Z |
| 8 | 11rr | DEC r | r = r - 1 | Z |
| 9 | 0000 | SHR | A = A >> 1 | Z C |
| 9 | 0001 | RRC | A = A >> 1, C into bit 3 | Z C |
| 9 | 0010 | SHL | A = A << 1 | Z C |
| 9 | 0011 | RLC | A = A << 1, C into bit 0 | Z C |
| 9 | 4 + p | OUT p | output port p = A (p = 0 to 9) | |
| 9 | 1110 | STN | memory[R3:R2] = A | |
| 9 | 1111 | STM | memory[R1:R0] = A | |
| A | n | BR n | PC[3:0] = n | |
| B | n | BZ n | the same if Z is set | |
| C | n | BNZ n | the same if Z is clear | |
| D | n | BC n | the same if C is set | |
| E | p | IN p | A = input port p (p = 0 to 14) | Z |
| E | 1111 | LDM | A = memory[R1:R0] | Z |
| F | 0 to 4 | JMP, JZ, JNZ, JC, JNC | PC = the next word, on a condition | |
| F | 0101 | CALL | the same, and the host keeps the return address | |
| F | 0110 | RET | PC = the address the host kept last | |
| F | 0111 | JPX | PC = A:R1:R0 |
C is the carry out of an addition and the bit shifted out by a shift. After a subtraction or
a compare it is set when there was no borrow, so CPI n then BC branches when A >= n.
The data memory and the ports belong to the host, which is what lets them fit: the chip only has to show the host an address and a value.
IN p and LDM load A from ui_in[3:0] like LDI. When the host sees one of them in the
program it puts the value there: input port p, or the memory nibble at address R1:R0.OUT p, STM and STN do nothing in the chip. The host, which reads the registers from
uo_out, takes A as output port p, or stores it at address R1:R0, or at address R3:R2.R1:R0 is therefore a pointer into 256 nibbles of read-write memory, and R3:R2 a second one
for stores, which makes a copy loop LDM, STN and two INCs; INC and DEC walk them.
A host can offer more than 256 by using an output port as a bank select.
The branches BR, BZ, BNZ and BC go to word n of the 16-word page they are in. A branch to
itself is a halt.
JMP reaches anywhere. It is two words: the second is a 12-bit address, which the host
presents three bits per clock on ui_in[2:0], bits 2:0 first. The operand of JMP is a
condition: 0 always, 1 if Z is set, 2 if Z is clear, 3 if C is set, 4 if C is clear (the
assembler's JMP, JZ, JNZ, JC and JNC). The address word is consumed whether or not the jump
is taken.
Conditions 5, 6 and 7 are always taken too. To the chip they are plain jumps; because the host supplies the address, it can give them a meaning:
CALL: the host presents the address in the program, and keeps the address of the word
after it.RET: the host presents the address it kept last. Calls nest as deep as the host allows.JPX: the host presents A:R1:R0, a computed jump for tables.tools/microcpu.py in the repository is an assembler and a reference model of the CPU.
Reset the chip with clk low. Then, for each word of the program:
uo_out (clk is low, phase 0). After reset PC[11:8] is 0; from then
on it was read in step 3 of the word before.ui_in and give
four clocks. For the address after a JMP, give four clocks with bits 2:0, 5:3, 8:6 and
11:9 on ui_in[2:0] in turn.clk is high, the flags can be read from uo_out.
Then, with clk low before the fourth rising edge, read the next PC[11:8] and A.clk is high, read R1 and R0 from uo_out.OUT p, STM or STN, act on it with the registers just read.
R3 and R2, for STN, are read after the first rising edge, while clk is high.For IN p or LDM in step 2, replace the low four bits of the instruction with the value.
For the address after RET or JPX, present the kept address or A:R1:R0.
test/fib.asm computes the 8-bit Fibonacci numbers in R1:R0 and halts with 233.
test/mem.asm fills 32 nibbles of memory, reads them back, and puts their sum on output
ports 0 and 1. test/call.asm calls subroutines two deep, copies memory through the second
pointer and takes a computed jump. The cocotb bench in test/ is such a host: it runs those
programs and 30000 random words, and checks the PC, the flags and every register against
the reference model after every word.
tools/microcpu.py has the same host as a Python class. host/microcpu_host.py is the host for
the demo board's RP2 in MicroPython, with the four clocks of a word done by one PIO state
machine; host/fpga_test.py runs the same tests with it on the FPGA breakout or the chip.
A host for the program, the data memory and the ports: the RP2040 on the Tiny Tapeout demo board.
| # | Input | Output | Bidirectional |
|---|---|---|---|
| 0 | INS[0] (operand; JMP address bit) | PC[0] / R0[0] / next PC[8] | |
| 1 | INS[1] (operand; JMP address bit) | PC[1] / R0[1] / next PC[9] | |
| 2 | INS[2] (operand; JMP address bit) | PC[2] / R0[2] / next PC[10] | |
| 3 | INS[3] (operand) | PC[3] / R0[3] / next PC[11] | |
| 4 | INS[4] (opcode) | PC[4] / R1[0] / A[0] / C | |
| 5 | INS[5] (opcode) | PC[5] / R1[1] / A[1] / Z | |
| 6 | INS[6] (opcode) | PC[6] / R1[2] / A[2] | |
| 7 | INS[7] (opcode) | PC[7] / R1[3] / A[3] |