

MicroComputer is a complete 4-bit computer. Its processor is the MicroCPU, the bit-serial CPU of
the micro tile tt_um_pettit_microcpu, which leaves its program, its memory, its ports and its
return addresses to a host outside the chip. Here that host is on the chip too:
| Program | 65536 words in an SPI flash on the bidirectional pins (the Tiny Tapeout QSPI Pmod) |
| Memory | 256 nibbles, addressed by R1:R0 (LDM, STM) and by R3:R2 (STN). A reset clears it |
| Stack | 8 return addresses for CALL and RET; a ninth call overwrites the oldest |
| Inputs | IN 0 reads ui_in[3:0], IN 1 reads ui_in[7:4]; IN 2 to IN 8 read 28 pins of a MAX7301 port expander; IN 9 to IN 11 read the timer; ports 12 to 14 read 0 |
| Outputs | OUT 0 writes uo_out[3:0], OUT 1 writes uo_out[7:4]; OUT 2 to OUT 8 write 28 pins of a second MAX7301; OUT 9 clears the timer |
| Timer | a 24-bit counter of the clock |
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 program counter, which is 16 bits here (12 in the micro tile). An instruction is 8 bits, a 4-bit opcode and a 4-bit operand, and the core works through it one bit per clock in four clocks.
For every word of the program the chip
03h and a 24-bit address;IN and LDM the operand is replaced by
the port or the memory nibble; after RET the address comes from the stack, and after JPX
it is A:R1:R0 in the 4096 words the JPX is in;OUT sets the output pins, and for STM and STN writes the memory (four more clocks).A word takes 39 clocks when it follows the one before and 105 after a jump: between 95,000 and 256,000 words a second with a 10 MHz clock.
After a reset the chip first clears the memory (1024 clocks), sets the port expanders up (about 800 clocks) and then starts at address 0 with every register and flag cleared.
The timer is a 24-bit counter of the clock that never stops. A program reads three nibbles of it and can clear it:
| Bits | With a 10 MHz clock | |
|---|---|---|
IN 9 |
15:12 | a count every 0.41 ms, round in 6.6 ms |
IN 10 |
19:16 | a count every 6.6 ms, round in 105 ms |
IN 11 |
23:20 | a count every 105 ms, round in 1.68 s |
OUT 9 |
clears all 24 bits, whatever A is |
A delay is OUT 9 and then a loop on one of the three until it reaches a value. A reset
clears the timer too. It stands still while debug mode has the program halted, so a program
that is single-stepped sees only the clocks of its own words.
Two MAX7301 (the 28-port ones) can share the
flash's SCK and MOSI, each with its own select, to give the program 28 more outputs and 28 more
inputs. They are optional: without them OUT 2 to OUT 8 do nothing and IN 2 to IN 8 read
whatever uio[4] is.
| Select | Ports | Pins | |
|---|---|---|---|
| Output expander | uio[6] |
OUT 2 to OUT 8 |
port p is P(4p - 4) to P(4p - 1): OUT 2 is P4 to P7, OUT 8 is P28 to P31 |
| Input expander | uio[7] |
IN 2 to IN 8 |
the same; its DOUT goes to uio[4] |
Bit 0 of the nibble is the lowest pin. After a reset the chip makes every pin of the output
expander an output and low, and every pin of the input expander an input with its pull-up, and
takes both out of shutdown. OUT is one 16-bit frame and IN two; the flash is deselected
for them, so the word after an IN or OUT to an expander starts a new read (about 100 clocks
more).
A MAX7301 always drives its DOUT, which is why the input expander has its own pin instead of sharing the flash's MISO. The output expander's DOUT is not connected.
If ui_in is A8 (hex) at the end of the memory clear, 1024 clocks after a reset, the chip is
in debug mode until the next reset, and ui_in and uo_out are a debug port:
| Pin | |
|---|---|
ui_in[3] |
HALT: high stops the program between two words. With A8 it starts halted, before the first word |
ui_in[4] |
STEP: a rising edge while halted runs one word |
ui_in[2:0] |
what uo_out shows |
ui_in[2:0] |
uo_out |
|---|---|
| 0 | OUT 1, OUT 0, as without debug mode |
| 1 | PC[7:0] |
| 2 | PC[15:8] |
| 3 | A in [7:4], Z in [3], C in [2], [1] the next word is a jump's address, [0] halted |
| 4 | R1 in [7:4], R0 in [3:0] |
| 5 | R3 in [7:4], R2 in [3:0] |
| 6 | the last instruction, as the core got it (an IN or LDM with the value it read) |
| 7 | the memory nibble at R1:R0 in [7:4], the stack pointer in [2:0] |
While the program is halted everything is still, so the views can be read one after another;
a host makes a breakpoint by stepping until the PC is the one it wants. IN 0 and IN 1
still read ui_in, so in debug mode a program sees the debug port there. The inputs go
through two flops: allow three clocks after a change.
| 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, 1) | |
| 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, 1; the others read 0) | 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 return address is kept on the stack | |
| F | 0110 | RET | PC = the address kept last | |
| F | 0111 | JPX | PC[11:0] = 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 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. The jumps are two words, the second the 16-bit address, and reach anywhere.
tools/microcpu.py in the repository is the assembler and a reference model of the CPU, and
tools/mkflash.py prog.asm prog.bin makes the flash image of a program.
python3 tools/mkflash.py examples/blink.asm blink.bin.blink.bin to the flash of a QSPI Pmod at address 0. While rst_n is low the chip
makes all its bidirectional pins inputs, so the demo board's RP2350 can program the flash
through them with the project held in reset.examples/blink.asm counts on uo_out[3:0],
a step every 0.1 s (it waits on the timer), and copies ui_in[3:0] to uo_out[7:4].Any clock up to 10 MHz works; the program runs in proportion.
The assembler's lo(), mid() and hi() give the three low nibbles of an address, which is
what JPX uses; a program above 4096 words reaches another 4096-word block with JMP or CALL.
To debug a program, hold ui_in at A8 through the reset and for 1100 clocks after it, then
use the debug port described above.
The cocotb bench in test/ has a model of the flash. It runs the test programs of the MicroCPU
and 60 random ROMs, and checks every word the chip reads (its address is the program counter,
so every branch and jump), the output pins, and in the RTL simulation every register and flag,
against the reference model.
An SPI flash that has the 03h read command on the bidirectional pins, with the pinout of the
QSPI Pmod, and optionally the two MAX7301:
| Pin | |
|---|---|
uio[0] |
flash CS (output) |
uio[1] |
MOSI (output): the flash and both expanders |
uio[2] |
flash MISO (input) |
uio[3] |
SCK (output): the flash and both expanders |
uio[4] |
DOUT of the input expander (input) |
uio[5] |
high: the flash's HOLD |
uio[6] |
CS of the output expander (output) |
uio[7] |
CS of the input expander (output) |
The three selects need pull-ups, because the pins are inputs while the chip is in reset.
The QSPI Pmod itself can hold the program. Its two RAMs are then selected by uio[6] and
uio[7] and receive the expanders' 16-bit frames (the set-up after a reset, and any IN or
OUT to ports 2 to 8); none of those is a complete RAM command, so they should be ignored,
but that has not been tried. The flash's WP on uio[4] is left undriven, which reading does
not need.
| # | Input | Output | Bidirectional |
|---|---|---|---|
| 0 | IN 0 bit 0 (debug: view select 0) | OUT 0 bit 0 | flash CS (out) |
| 1 | IN 0 bit 1 (debug: view select 1) | OUT 0 bit 1 | MOSI (out) |
| 2 | IN 0 bit 2 (debug: view select 2) | OUT 0 bit 2 | flash MISO (in) |
| 3 | IN 0 bit 3 (debug: HALT) | OUT 0 bit 3 | SCK (out) |
| 4 | IN 1 bit 0 (debug: STEP) | OUT 1 bit 0 | input expander DOUT (in) |
| 5 | IN 1 bit 1 | OUT 1 bit 1 | high (flash HOLD) |
| 6 | IN 1 bit 2 | OUT 1 bit 2 | output expander CS (out) |
| 7 | IN 1 bit 3 | OUT 1 bit 3 | input expander CS (out) |