
tt_um_yangzhou08_tetris plays Tetris. The host names the current piece (and, whenever it wants, the complete
20x10 board); the chip answers with the placement (rotation and column). Everything that decides the placement runs
on the chip:
The chip holds a copy of the board. The host can load the complete board for any decision (LOAD), which makes
the decision a function of the transferred state only. To save transfer time it can instead send only the next
piece (NEXT): the chip then first applies its own previous answer to its board copy (placement and line removal),
exactly as the host does with its board. NEW starts a game with an empty board. A LOAD at any time
resynchronises the chip with the host; reset and NEW put the chip in a known state.
| Pin | Dir | Name | Function |
|---|---|---|---|
ui_in[7:0] |
in | D[7:0] |
data word, low 8 bits |
uio[0], uio[1] |
in | D[8], D[9] |
data word, high 2 bits |
uio[2] |
in | WR |
a rising edge transfers the word D[9:0] |
uio[3] |
in | SOF |
sampled with the word: 1 = command word, 0 = board row of a LOAD frame |
uo_out[3:0] |
out | COL |
column of the chosen placement (0..9), 0 unless VALID |
uo_out[5:4] |
out | ROT |
rotation of the chosen placement (0..3), 0 unless VALID |
uo_out[6] |
out | VALID |
the answer is on the outputs; stays until the next accepted command or reset |
uo_out[7] |
out | BUSY |
a decision (or the board update before it) is being computed |
uio[4] |
out | TOPOUT |
the chosen placement ends the game (every legal placement does); 0 unless VALID |
uio[7:5] |
out | LINES |
lines cleared by the chosen placement (0..4); 0 unless VALID |
uio[7:4] |
out | readback | only while SOF = 1 and D[9:8] = 11 are on the pins: a nibble of internal state selected by D[6:0] (section "Readback") |
ui_in[0], ui_in[7], uio[4], uio[7:5] |
in, in, out, out | scan-in, EXIT, scan-out, 101 |
only in scan mode, which starts with the two words 0x3A5, 0x35A (section "Scan chain") |
uio_oe is the constant 0xF0. ena is not used. TOPOUT and LINES are a cross-check for the host and a
convenience for the demonstration; the evaluation needs only ROT, COL and VALID. TOPOUT = 1 together with
LINES = 7 (and action 0) is not a placement: it is the answer "NEXT refused".
Use a 10 MHz clock. Hold reset low for at least eight cycles with WR low. Release reset and wait four cycles before sending the first word. All outputs should read zero.
For each word: put D and SOF on the pins, raise WR, lower WR. D and SOF must be stable from one clock
cycle before the rising edge of WR until WR falls; keep WR high for at least 4 clock cycles and low for at
least 4 clock cycles (the chip synchronises WR with two flip-flops and samples the data two to three cycles after
the edge; simulations pass with 2 cycles high and 2 low). There is no upper limit on any of these times, so a
slow host needs no delays at all.
Send a command with SOF high. D[9:8] selects LOAD (0), NEXT (1), or NEW (2). D[2:0] selects I, O, T, S, Z, J, or L in that order. For LOAD, send 20 ten-bit row words with SOF low, top row first and column zero in bit zero. On an empty board, send NEW with J as the first piece. Wait for VALID, then read the output byte again. The expected placement is rotation zero, column zero. The longest decision takes 1,252 clock cycles.
With SOF high and D[9:8] equal to 3, hold WR low and set D[6:0] to the desired address. Read the selected nibble on UIO[7:4] with the project clock stopped.
The host must control the clock. In scan mode every rising clock edge shifts the chain by one position.
Stop the project clock and step it by hand (clock_project_stop, clock_project_once on the demo board), as for
the stepped readback. With a free-running clock the state would rotate uncontrolled (reset recovers).
Entry (in-band, there is no free pin). Two consecutive words, both with SOF = 1 and their WR edge:
D = 0x3A5, then D = 0x35A (command code 3 with D[7:0] = 0xA5, then 0x5A). Any other word in between cancels
the first one. No command and no readback access contains a WR edge with code 3, so the normal protocol cannot
start scan mode. Both words are otherwise ignored like any word with code 3: the chip keeps working (a running
decision goes on) until scan mode starts, which is at the third rising clock edge after WR of the second word
went high. Keep WR, SOF and D[9:8] as they are while scan mode is active.
Pins in scan mode. ui_in[0] = scan-in (taken by the first flop at every clock edge), uio[4] = scan-out (the
last flop of the chain), uio[7:5] = 101 (says "scan mode"), ui_in[7] = EXIT. uo_out shows whatever the
shifting state produces and means nothing. Directions do not change: uio_oe stays 0xF0.
One pass is exactly 788 clock edges: before each edge read scan-out, put the next bit on scan-in. Shifting the bit just read back in restores the state (dump); shifting other bits in replaces it (load). The first bit read is chain position 787, the last one position 0; the bits are shifted in in the same order.
Exit and resume. Raise EXIT (ui_in[7]) before the third-last clock edge of the pass and keep it high:
it is sampled through two flip-flops, and the 788th edge is then the last shift. From the next edge on the chip
runs normally from the state that is in the chain. The count must be exact. The chain includes the input
registers; at entry they hold WR = 1, so leave with WR still high (a restored state then does not see a new
WR edge), then lower WR and take the word off the pins as after any word.
Reset (rst_n) always ends scan mode: the mode flip-flops are cleared asynchronously like everything else.
Use the Tiny Tapeout demo board or a digital host that can step the project clock.
| # | Input | Output | Bidirectional |
|---|---|---|---|
| 0 | D[0] (data word; D[6:0] select the readback view; scan-in while in scan mode) | COL[0] (column of the placement) | D[8] (input) |
| 1 | D[1] | COL[1] | D[9] (input) |
| 2 | D[2] | COL[2] | WR (input, word strobe) |
| 3 | D[3] | COL[3] | SOF (input, 1 = command word; SOF=1 with D[9:8]=11 selects the readback; words 0x3A5, 0x35A start scan mode) |
| 4 | D[4] | ROT[0] (rotation of the placement) | TOPOUT (output); readback bit 0 while SOF=1 and D[9:8]=11; scan-out while in scan mode |
| 5 | D[5] | ROT[1] | LINES[0] (output); readback bit 1; 1 in scan mode |
| 6 | D[6] | VALID | LINES[1] (output); readback bit 2; 0 in scan mode |
| 7 | D[7] (EXIT while in scan mode) | BUSY | LINES[2] (output); readback bit 3; 1 in scan mode |