
The design renders the Mandelbrot set or a Julia set, one escape iteration
count per pixel, in a frame of width x height pixels (800 x 480 after
reset, up to 2047 x 2047). ui_in[7] selects what controls the view: an
SNES-style controller on the Tiny Tapeout Gamepad Pmod (ui_in[6:4]), or
an SPI master on uio[3:0]. In both modes the pixels are read out over SPI.
The frame centre c = cx + cy*i is held as two signed Q4.28 values and
starts at 0 + 0i after reset. step is the distance between neighbouring
pixels. The D-pad pans the view by about an eighth of the frame: each press
of Up or Down adds or subtracts step from cy height/8 times, and each
press of Right or Left does the same for cx width/8 times (the size shifted
right by 3 bits, and at least once, so frames narrower than 16 pixels pan
by one pixel). With the reset size a press pans 100 pixels sideways or 60
pixels up or down. The additions run one per clock cycle on the adder the
frame scan uses, so the pan needs no multiplier and is always a whole number
of pixels. After reset step is 0x00147000 (327 x 2^12, about 0.00499):
0.005 rounded down to a multiple of 2^12, so that the 800 x 480 frame lies
within +/-2 by +/-1.2, the first 12 halvings are exact, and doubling back
returns exactly to the reset value. X halves step (zoom in) and B doubles
it (zoom out). Halving rounds down and does nothing when step is 1.
Doubling only happens while step is below 2^30 (4.0), so the last doubling
can go past 4.0: from reset, the tenth B press reaches 327 x 2^22 (about
5.11).
A doubles the number of escape iterations per pixel, N, and Y halves it: 16
after reset, within 1 to 1024. The max_iter register holds N - 1 (10
bits). A does nothing once N is above 512, and Y does nothing when N is odd
(N = 1, or an odd value written over SPI). A press counts when a button
changes from released to pressed, so holding a button acts only once.
Opposite directions, X and B, or A and Y pressed in the same report cancel
out. Select switches between the Mandelbrot set and a Julia set (below), and
L and R are ignored.
A report acts over several clock cycles. In the cycle it is decoded, A and Y change N and cy takes its first step. cy then takes its remaining steps, then cx takes its steps. X/B, Select and Start act in the last cycle: the cycle of cx's last step, or, without a cx pan, the cycle after the cy pan (or after the press cycle). So both pans of a diagonal press use the step from before the report, and Start renders the panned view. Up to a size of 15 x 15 every pan is one step, and a report takes at most 2 cycles. With the reset size a diagonal press takes 160 cycles, and at 2047 x 2047 it takes 510 (255 steps per axis). A button pressed while a report is still being applied acts as soon as that report is done, if it is still held then. With the real Pmod, which takes over 100 us per report, it always is.
Both fractals iterate z -> z^2 + c and colour a pixel by the iteration in which z leaves the box |x| <= 2, |y| <= 2. For the Mandelbrot set (the mode after reset) z starts at 0 and c is the pixel. For a Julia set z starts at the pixel and c is the Julia constant J = jx + jy*i, the same for the whole frame.
Select toggles between the two. Entering Julia mode copies the current frame centre (including a D-pad pan in the same report) into J and leaves the view where it is. A Julia set looks like the Mandelbrot set around the point it was taken from, so the frame first shows that similarity; zoom out (B) to see the whole set. Leaving Julia mode keeps J and the view. Select stops a running frame like the other buttons, and Start in the same report renders the new mode. J is stored with the core's precision (signed Q4.24), so the low 4 bits of the centre are dropped.
In Julia mode the core starts from the pixel, so a pixel outside the escape box escapes at once: it takes a single core cycle and is reported as iteration 1, the same as a point that escapes in the first iteration. Otherwise the iteration count and timing are as for the Mandelbrot set.
All coordinate arithmetic is two's-complement Q4.28, so the plane wraps around at +/-8 (modulo 16): moving past +8 continues from -8, and a frame that extends past either edge continues from the opposite one. Every pixel is exactly at cx + (col - width/2) * step, cy + (row - height/2) * step (width/2 and height/2 rounded down), taken modulo 16 into [-8, 8).
The gamepad receiver (src/gamepad_pmod.v) synchronizes the Pmod's latch,
clock and data signals to clk and shifts in the serial report. It keeps the
last 12 bits before each latch pulse, which belong to controller 1; a second
controller is ignored. A report of all ones means no controller is connected
and reads as no buttons pressed.
Start (or a ctrl write in SPI mode) scans a frame of width x height
pixels row by row. With the reset size, x runs from cx - 400 * step to
cx + 399 * step along a row, and y from cy - 240 * step for the first row to
cy + 239 * step for the last. Row 0 therefore has the lowest imaginary part:
a host that draws row 0 at the top shows the image upside down. When the
frame is finished, or a view change stops it (a D-pad, X/B, A/Y or Select
press, or an SPI write to registers 0 to 5, 8 or 9), the scan stops and
waits for the next Start. Start during a frame restarts it, and Start in the
same report as a change renders with the change (after the pans). The left
edge of each row is reached without a multiplier, by subtracting step
from cx width/2 times: width/2 + 1 clock cycles (401 for width 800) after
the previous row's last pixel. Before the first row, the bottom edge is
reached the same way (height/2 + 1 cycles).
The top 28 bits of each pixel coordinate (signed Q4.24, rounded toward minus infinity) are evaluated by a sequential core with a single radix-4 (Booth) shift-add multiplier. One iteration takes 30 clock cycles: two 28 x 28 multiplications of 15 cycles each, 2x * y for the new y and (x + y) * (x - y) for the new x. The core stops as soon as the point escapes. The new y is known half way through an iteration and the new x at its end, so a pixel that escapes in iteration k takes 30 * (k - 1) + 15 or 30 * k cycles, a pixel that stays bounded takes 30 * N, and the next pixel starts one cycle later. N is read throughout a pixel: if it changes, the pixel ends after the first iteration beyond the new limit.
Frame time therefore depends on the view. At 50 MHz the reset 800 x 480 view (16 to 19 % inside the set) takes 1.3 s with 16 iterations, 5.6 s with 128 and 38 s with 1024. A view that is entirely inside the set takes 3.7 s, 30 s and 3.9 minutes. These times assume the SPI master reads the pixels as fast as they are made (see below).
With 24 fraction bits, the pixel spacing at 4096x zoom (12 X presses,
step = 327) is still about 20 core LSBs, so frames stay clean to about
8192x and usable to about 16384x. Beyond that, neighbouring pixels round to
the same or unevenly spaced core coordinates and the image smears.
The pixels are only output through the SPI pixel stream (below), in both
modes. Each pixel is sent as 16 bits: the escape iteration (1 to 1024), or 0
when the point stayed bounded through all N iterations. A low rst_n
synchronously resets the registers to the values above and clears the scan,
the core and the stream.
uo_out[0] (FRAME_IRQ_N) is an active-low frame interrupt, in both modes.
It is high after reset and while no frame is rendering. It goes low on the
clock edge that accepts a frame start (a Start press in gamepad mode, or a
ctrl write with bit 0 set in SPI mode) and goes back high once the frame
has ended and its last pixel has been read: one clock after the ctrl busy
bit is 0 and the stream FIFO is empty. The frame ends when its last pixel
has been computed, or when it is stopped (any view change, or a ctrl
write with bit 0 clear); after a stop, the pixels already in the FIFO and
the one that was running still have to be read. A pixel counts as read
once its low byte has been clocked out, so the rise comes 4 clocks after the
last SCK edge of the stream read that empties the FIFO. A restart during a
frame keeps it low. The other dedicated outputs, uo_out[7:1],
are unused and stay 0.
ui_in[7] (SPI_MODE) selects what controls the view: 0 for the gamepad (as
above), 1 for SPI. In SPI mode the gamepad is ignored and the frame registers
are written over SPI. In gamepad mode SPI writes are ignored. In both modes
every register can be read over SPI and the pixels are streamed over SPI.
The registers keep their values across a mode change, so a view found with
the gamepad can be read over SPI after switching. Change the mode only while
no frame is rendering and no gamepad press is being applied (a pan takes up
to 510 clock cycles).
The SPI slave (src/spi_slave.v) uses the Pmod SPI pinout on the
bidirectional pins: uio[0] CS_N, uio[1] MOSI, uio[2] MISO, uio[3]
SCK. It works in SPI mode 0 or mode 3 without configuration: MOSI is sampled
on the rising SCK edge, MISO changes on the falling edge, MSB first. MISO is
only driven while CS_N is low. The shift registers are clocked by SCK itself,
so the SPI clock does not depend on the system clock: the RP2040's fastest
SPI master clock (62.5 MHz, half of the 125 MHz peripheral clock) works, and
the register hand-over to the system clock only needs clk > SCK / 2
(SCK up to 100 MHz with the 50 MHz clock). The simulations run SCK at 1,
25 and 62.5 MHz. How fast the Tiny Tapeout I/O pads and multiplexer can run
is a separate limit.
CS_N must stay low for a whole transaction. A transaction starts with a
command byte: bit 7 is 1 for a read and 0 for a write, bits 3:0 are the
register address and bits 6:4 should be 0. A write follows with 4 data bytes
per register; a read follows with one dummy byte and then 4 data bytes per
register. Values are 32 bits, MSB first, and a transaction may continue with
the next address after each 4 bytes (burst; after 15 it wraps to 0). A burst
that reaches address 7 stays there: a read burst continues as a stream read,
and the rest of a write burst is ignored, so jx and jy need a write of
their own. MISO sends 0 during the command and dummy bytes and during writes.
| Address | Register | Description |
|---|---|---|
| 0 | cx |
Frame centre, real part, signed Q4.28 (0 after reset) |
| 1 | cy |
Frame centre, imaginary part, signed Q4.28 (0 after reset) |
| 2 | step |
Distance between pixels, signed Q4.28 (0x00147000, about 0.00499, after reset). Writes are clamped to 1 .. 2^30: zero and negative values store 1, larger values store 2^30 |
| 3 | max_iter |
Escape iterations minus 1, bits 9:0: 0 to 1023 means 1 to 1024 iterations (15, i.e. 16 iterations, after reset). Every value is valid |
| 4 | width |
Frame width in pixels, bits 10:0, up to 2047 (800 after reset). Writing 0 stores 1 |
| 5 | height |
Frame height in pixels, bits 10:0, up to 2047 (480 after reset). Writing 0 stores 1 |
| 6 | ctrl |
Write bit 0: 1 starts a frame, 0 stops it; bit 1 selects the fractal, 0 Mandelbrot, 1 Julia (every ctrl write sets it). Read: bit 0 busy, bit 1 SPI mode, bit 2 Julia, bits 10:8 pixels waiting in the stream FIFO (0 to 4) |
| 7 | stream |
Read only: pixel stream |
| 8 | jx |
Julia constant, real part, signed Q4.28 (0 after reset). Bits 3:0 are not stored and read as 0 |
| 9 | jy |
Julia constant, imaginary part, likewise |
| 10-15 | Reserved: read as ctrl, writes are ignored |
Writing any of registers 0 to 5, 8 or 9 stops a running frame, as the
gamepad buttons do; write ctrl = 1 (Mandelbrot) or 3 (Julia) to render
with the new values. Writes only act in SPI mode. A start clears
the stream, and a pixel that was still running from an earlier frame is
dropped, so the stream after a start begins with the new frame's first pixel.
A pixel that is running when jx, jy or the fractal changes is dropped
too, because it would have used two different values of c; after any other
stop the running pixel still finishes and is streamed.
Reading address 7 (command 0x87, then the dummy byte) returns two bytes
per pixel (16 bits, MSB first), in scan order, for as long as CS_N stays low.
Each pixel is the escape iteration, or 0 for a pixel that stayed bounded.
0xFFFF means no pixel was ready yet; since a pixel is at most 1024 it
is never a pixel value, and the next two bytes try again. Finished pixels
wait in a 4-entry FIFO. A new pixel only starts when there is room for its
result, so a slow reader (or none) pauses the frame instead of losing
pixels. A pixel only leaves the FIFO once both of its bytes have been sent;
one that was being sent when CS_N went high is sent again, from its high
byte, by the next stream read, so read whole pixels (an even number of
bytes). The master can either read fixed-size blocks and drop the 0xFFFF
words, or read ctrl and then read exactly as many pixels as bits 10:8 say
are ready.
Each pixel takes 16 SCK periods on the stream, so a master that reads continuously at 62.5 MHz takes 3.9 million pixels per second. That is faster than the core produces them, except for Julia pixels that start outside the escape box (2 clock cycles each), where the frame waits for the reader.
ui_in[7] = 0)Connect the Gamepad Pmod to the input Pmod header with a controller in port 1,
and an SPI master to uio[3:0] to read the pixels (as in SPI mode below,
but without writing any registers). After reset nothing is rendered. Press
Start: the first pixel, at (-1.996, -1.198), is in the stream FIFO about
690 clock cycles after the Pmod's latch pulse, and the next ones follow
every 46 cycles at the start of the first row (later pixels take longer, up
to 481 cycles for a point inside the set at 16 iterations) while the master
keeps reading the stream. Pan with the D-pad (an eighth of the frame per
press; zoom in for finer pans), zoom with X (in)
and B (out), change the iterations with A (double) and Y (halve), and press
Start again to render the new view. Press Select (and Start) to render the
Julia set of the current centre, zoom out with B to see all of it, and press
Select again to return to the Mandelbrot set.
Known limitation: if Start restarts a frame while the master is in the
middle of a stream read, a pixel of the old frame that was already on its
way out can still arrive after the restart, and FRAME_IRQ_N stays low
across a restart. In gamepad mode, end the stream read (raise CS_N) before
pressing Start, or discard the first pixel after a restart. SPI mode is not
affected, because the ctrl write that starts a frame is its own
transaction.
ui_in[7] = 1)Connect an SPI master such as an RP2040 to uio[3:0]. On the RP2040's SPI
peripheral, use mode 0 with CS_N driven as a GPIO, or mode 3 (CPOL = 1,
CPHA = 1) with the hardware chip select: in mode 0 the RP2040's hardware chip
select goes high between frames, which would end each transaction after one
byte. An example in MicroPython, with CS_N on a GPIO:
from machine import SPI, Pin
spi = SPI(0, baudrate=62_500_000, polarity=0, phase=0)
cs = Pin(17, Pin.OUT, value=1)
def write(addr, *values):
cs(0)
spi.write(bytes([addr]) + b"".join(v.to_bytes(4, "big") for v in values))
cs(1)
def read_stream(n):
"""Read n stream words; return the pixels (0xFFFF filler dropped)."""
cs(0)
spi.write(b"\x87\x00") # Stream read command and dummy byte.
data = spi.read(2 * n)
cs(1)
words = (int.from_bytes(data[i:i + 2], "big") for i in range(0, 2 * n, 2))
return [w for w in words if w != 0xFFFF]
def q4_28(x):
return round(x * (1 << 28)) & 0xFFFFFFFF
# 320 x 240 pixels centred on -0.5 + 0i, step 0.01, 64 iterations
# (max_iter = 63), then start.
write(0, q4_28(-0.5), 0, q4_28(0.01), 63, 320, 240)
write(6, 1)
pixels = []
while len(pixels) < 320 * 240:
pixels += read_stream(128)
# The Julia set of c = -0.8 + 0.156i in the same view. jx and jy (8, 9)
# need their own write: a burst from 0 stops at address 7.
write(8, q4_28(-0.8), q4_28(0.156))
write(6, 3) # Start, Julia.
To know when a frame is complete without polling ctrl, connect
uo_out[0] (FRAME_IRQ_N) to a GPIO interrupt: it goes low when the frame
starts and rises once every pixel of the frame has been read from the
stream. The FIFO holds only 4 pixels, so a frame only progresses while the
stream is being read: keep reading it until the rising edge.
Gamepad Pmod
with an SNES-style controller, on the input Pmod (ui_in[4] latch, ui_in[5]
clock, ui_in[6] data), for gamepad mode. Optionally, a GPIO interrupt input
on uo_out[0] (FRAME_IRQ_N). For SPI mode, an SPI master (for
example the RP2040 on the Tiny Tapeout demo board) on the bidirectional Pmod
(uio[0] CS_N, uio[1] MOSI, uio[2] MISO, uio[3] SCK), and ui_in[7]
high. The SPI master is needed in gamepad mode too, to read the pixels.
| # | Input | Output | Bidirectional |
|---|---|---|---|
| 0 | FRAME_IRQ_N | SPI_CS_N | |
| 1 | SPI_MOSI | ||
| 2 | SPI_MISO | ||
| 3 | SPI_SCK | ||
| 4 | GAMEPAD_LATCH | ||
| 5 | GAMEPAD_CLK | ||
| 6 | GAMEPAD_DATA | ||
| 7 | SPI_MODE |