- Verilog 44.6%
- C 29%
- Python 23%
- Tcl 2.1%
- Makefile 1.3%
The design runs on the i9plus v6.1: gigabit link, ping and the UDP protocol with no bad frames on the default RGMII settings, SDRAM calibrated (clean at read latency 5 over all 32 taps) and memtested, VGA, HDMI up to 1280x720, and fpgamon as an extra KDE Plasma monitor. * docs: the MuseLab PMOD-ETHERNET does not match header P1 (the board runs each MDI pair down a column, the module across a row), which splits every pair: the link only reaches a flapping 100 Mbit. Wiring table for the jumper adapter that fixes it; note that H2 does not reset the PHYs. README status, HDMI limits and verification table updated. * fpgamon: cursors larger than 32x32 (e.g. Plasma at size 48) were cropped, cutting off the bottom of the pointer. They are now scaled down to fit, averaging alpha and colour, with the hot spot kept exact. * make flash: pass --fpga-part so openFPGALoader can load its SPI-over-JTAG bridge. * ignore the timing reports Vivado leaves in the working directory. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> |
||
|---|---|---|
| build | ||
| constraints | ||
| docs | ||
| host | ||
| rtl | ||
| sim | ||
| .gitignore | ||
| LICENSE | ||
| Makefile | ||
| PLAN.md | ||
| README.md | ||
i9plus-gpu
An Ethernet-attached graphics card built on the Colorlight i9plus v6.1 (Xilinx XC7A50T) and its ext-board. A laptop sends drawing commands over a direct gigabit link; the FPGA renders into its 8 MB SDRAM and drives a monitor over HDMI (DVI) or a PMOD VGA module.
Status: everything below passes simulation, runs against the software
emulator, and works on the real board: gigabit link, ping and the UDP
protocol with no bad frames, SDRAM calibrated and memtested, VGA and HDMI
output (HDMI including 1280x720), and fpgamon as an extra KDE Plasma
monitor with the hardware cursor. The roadmap is in
PLAN.md; wiring and bring-up are in docs/hardware.md.
Features
- Network: RGMII MAC on both PHYs, ARP, ICMP echo (up to 8000 bytes), a UDP command protocol with optional exactly-once, in-order delivery
- Memory: SDR SDRAM controller (125 MHz, CL3) with run-time read calibration
- Video: RGB565 framebuffer, tear-free page flip at vblank, 32x32 hardware cursor, test patterns; DVI on the ext-board HDMI connector (pseudo-differential, with a pin crossbar) or 12-bit PMOD VGA
- Display modes from 640x480 up to 1280x720 and 1024x768@70, switched at run time by reprogramming the pixel-clock MMCM
- 2D engine: queued fill, overlap-safe copy with colour key, Bresenham lines, fences
- 3D engine: triangle setup, rasteriser, perspective-correct bilinear texturing, Gouraud shading, fog, Z and alpha test, blending, dither; tile-based rendering in on-chip 64x64 tile buffers; several textures resident in the texture RAM
- Host software:
gpu.py: Python library and CLI, plus an emulator that speaks the same protocol and saves monitor snapshotslibfpgagpu: C client library (batched commands, windowed retransmit)fpgamon: makes the FPGA an extra monitor for the laptop desktop via EVDIfg3d: a GL-1.x-style renderer (transform, lighting, clipping, textures)fgl: a subset of the OpenGL 1.x API (realgl*names) on top of fg3d- demos:
demo3d,gldemo
Quick start
make sim # all simulations (iverilog), a few minutes
make lint # verilator lint
make build # Vivado on the build host over SSH (REMOTE=user@host)
make program # load into SRAM via the ext-board CH347 (openFPGALoader)
make flash # or write it to the SPI flash so it loads at power-up
make host # C library, fpgamon, demos (EVDI support if available)
make host-test # host tests against the emulator
With the board connected directly to the laptop (see docs/hardware.md for the network setup):
sudo ip addr add 192.168.77.1/24 dev <iface>
ping 192.168.77.2
python3 host/gpu.py info
python3 host/gpu.py sdram-cal --apply && python3 host/gpu.py memtest
python3 host/gpu.py pattern 2
python3 host/gpu.py show picture.jpg # upload and display an image
python3 host/gpu.py anim # double-buffered streaming, prints frames/s
python3 host/gpu.py mode 1024x768 # switch the display mode (no argument lists them)
No board? python3 host/emulator.py --snapshot monitor.png answers the same
protocol on 127.0.0.1 and saves what the monitor would show; point the tools
at it with gpu.py --ip 127.0.0.1 ....
Extra monitor for the laptop (fpgamon)
fpgamon registers a virtual monitor with the desktop through the EVDI kernel
module and keeps the FPGA framebuffer equal to what the compositor draws.
yay -S linux-headers evdi-dkms # DKMS needs the headers of the running kernel
sudo modprobe evdi initial_device_count=1 # to persist: "options evdi initial_device_count=1"
# in /etc/modprobe.d/evdi.conf
make host
host/out/fpgamon --stats # the desktop now offers an "i9plus GPU" monitor
The user needs read/write access to the EVDI /dev/dri/card* node (group
video); without a pre-created device fpgamon has to run as root.
--mode NAME selects a display mode, otherwise fpgamon uses the FPGA's
current mode and builds its EDID from it.
For each frame, fpgamon:
- compares the new frame with the back buffer in 16x16 tiles;
- fills each differing tile from the cheapest source: the same tile of the front buffer (an on-FPGA COPY), the front buffer at an offset (scrolled or dragged content, found by hashing 16-pixel row segments), or an upload cropped to the changed pixels;
- waits until every packet is acknowledged, then flips at the next vblank.
The pointer goes to the hardware cursor (--sw-cursor draws it into the
frames instead). If the FPGA stops answering, fpgamon waits and then resends
the whole screen. Without EVDI, fpgamon --synth 300 --verify drives a
synthetic desktop and reads the FPGA memory back; make host-test runs it
against the emulator with packet loss, a restart, and a check that nothing is
ever written into the buffer on screen.
3D
host/out/demo3d --scene all # or cube, torus; prints frames/s and statistics
host/out/gldemo # GL immediate mode, vertex arrays, lighting, fog, two viewports
Host side: fg3d
The laptop does what a GL 1.x driver does on the CPU
(host/libfpgagpu/fg3d.h): vertex transform, one directional light per
vertex, fog factors, clipping against the near/far planes and a guard band,
and viewport mapping. It sends screen-space triangles of 15 words: position
in 1/16 pixel, 16-bit Z, ARGB colour, fog factor, and S/T/Q for
perspective-correct texturing.
Tiled rendering (default on FPGA version 7 or later; FG3D_TILES=0 or
fg3d_tiles(g, 0) turns it off): fg3d records a frame's commands and, at
fg3d_end, sends them tile by tile. Every 64x64 tile of the back buffer
gets the triangles whose bounding box touches it, each preceded by the
state and texture it was recorded with; the tile starts cleared (when the
frame began with a full clear) or loaded from the frame buffer. A texture
update in the middle of a frame first sends what was recorded so far. The
picture is identical word for word (colour and depth) to direct rendering;
the price is about 25% more triangles on the network (a triangle goes to
every tile it touches).
fg3d also keeps textures in the texture RAM: each texture gets an aligned range of the 64 slots of 1024 texels (the TEX state's base field) and is loaded only when its slots hold something else.
FPGA side: rtl/g3d/
- Setup (about 100 clocks per triangle): edge functions with the top-left rule, culling, bounding box clipped to the scissor rectangle, plane equations for Z, R, G, B, A, fog, S, T, Q (20 fraction bits, one reciprocal of the area, one shared 48x33 multiplier).
- Span walker: 16-pixel spans; whole spans are accepted or rejected in one clock, partial ones walked 4 pixels per clock.
- Span engine: the SDRAM has no byte masks, so each span is read in one burst, shaded, and written back in one burst, with several spans in flight and read-after-write hazards between triangles checked.
- Tiles (
g3d_tile.v): inside a TILE ... TILE_END bracket the span engine works on a 64x64-word block RAM instead: only covered pixels enter the pipeline, which reads the destination word at stage 30 and writes the result at stage 35 (a pixel waits only if the same pixel was sent in the last 5 clocks). Two tile buffers: one renders while the other is written back to SDRAM, and is pre-cleared during that write-back, so the next tile with the same clear value starts at once. - Pixel pipeline (35 stages, one pixel per clock): per-pixel 1/Q (table
- Newton step), texel addressing (wrap / clamp), bilinear filtering from a 128 KB texture RAM in 4 banks (a 2x2 footprint is one texel per bank), 565 / 1555 / 4444 texels, combine with the vertex colour, fog, alpha test, Z test (8 functions), blending (alpha, add, alpha-add), 4x4 ordered dither.
The texture RAM holds 64K texels: one 256x256 texture, or several smaller ones at different bases (64 slots of 1024 texels); TEX_LOAD copies a texture from SDRAM to its base.
Speed, from a real demo3d frame (make sim-g3d-frame: 640x480, 3,100
triangles, 276,000 shaded pixels) run through the RTL in simulation:
| clocks at 125 MHz | frames/s (3D engine alone) | |
|---|---|---|
| direct: SDRAM read-modify-write per span, plus the 2D clear | 1,325,000 | 94 |
| tiled | 599,000 | 209 |
Direct rendering costs about 3.5 clocks per covered pixel. A tiled frame is now limited by writing the 307,200 words of the frame to SDRAM (each tile's write-back) and by triangle setup.
host/fpgagpu/ref3d.py is the bit-exact reference model and defines the
command format. make sim-g3d compares random scenes against it word for
word, and the emulator draws with it, so emulator snapshots show exactly what
the FPGA would draw.
OpenGL-style API: fgl
host/libfpgagpu/fgl.h puts a small OpenGL 1.x API on top of fg3d. The
functions and constants have the real GL names and values, so simple
fixed-function GL 1.1 code compiles against it; only context setup differs
(fglInit(dev, 640, 480) and fglSwapBuffers(vsync)).
Supported: glBegin/glEnd (triangles, strips, fans, quads, quad strips,
polygons), vertex arrays, the three matrix stacks and the glu matrix
helpers, viewport and scissor, colour / depth clears, depth test, culling,
alpha test, blending, linear fog, flat and smooth shading, one directional
light, and 2D textures (glTexImage2D, glTexSubImage2D; RGB / RGBA /
luminance sources stored as 565, 4444 or 1555).
Not supported: points, lines, stencil, mipmaps, display lists and the less
common blend / alpha functions. Unsupported values set GL_INVALID_ENUM and
are reported once on stderr; the exact list is at the top of fgl.h.
host/tests/fgl_test.c draws small shapes through GL calls and checks the
pixels read back (part of make host-test).
Display modes
The FPGA reprograms the pixel-clock MMCM through its dynamic reconfiguration
port (rtl/video/video_clk.v, following XAPP888): pixel clock =
25 MHz x M / (5 k), with the 5x serial clock from the same VCO. Writing
VID_CLK (0x046) holds the MMCM in reset, rewrites the counter, lock and
filter registers and waits for lock; scanout resumes at the next frame.
| Mode | Pixel clock | Refresh | Notes |
|---|---|---|---|
| 640x480@60 | 25.0 MHz | 59.5 Hz | power-up mode |
| 800x600@60 / @72 | 40.0 / 50.0 MHz | 60.3 / 72.2 Hz | VESA, exact |
| 1024x768@60 / @70 | 65.0 / 75.0 MHz | 60.0 / 70.1 Hz | VESA, exact |
| 1280x720@60 | 75.0 MHz | 60.0 Hz | CEA timing (74.25 MHz) with a 17-pixel longer back porch |
Modes are switched with gpu.py mode NAME, GPU.set_mode() (Python),
fg_set_mode() (C), or --mode NAME on fpgamon, demo3d and gldemo; without
--mode those tools use the FPGA's current mode. Limits:
- Memory: two 1280x720 buffers take 7.0 of the 8 MB, leaving about 1 MB for textures.
- Bandwidth: scanout reads 4 bytes per pixel, about 230 MB/s at 75 MHz, so the 2D and 3D engines get noticeably less of the SDRAM in the large modes.
- HDMI: the connector is driven pseudo-differentially from LVCMOS33 pins (250 Mbit/s per lane at 640x480, 750 at 75 MHz). On the real board it works up to 1280x720 (750 Mbit/s per lane), though with little margin, so a different cable or monitor may behave differently; VGA works in every mode.
Protocol
UDP port 18256, little-endian 32-bit words.
request: [magic 0x5047 | ver 1 <<16 | flags <<24] [seq] {commands}
command: [op | n << 16] operands...
0 NOP 1 REG_WR n×{addr,val} 2 REG_RD n×{addr}
3 MEM_WR {addr, n×data} 4 MEM_RD {addr} 5 MEM_FILL {addr, count, value}
6 MEM_WR16 {addr, ceil(n/2)×data}: n RGB565 pixels, 2 per word -> one word each
7 MEM_WR16_RECT {addr, stride | width << 16, data}
0x10 FILL 0x11 COPY 0x12 LINE 2D engine (queued)
0x13 WAIT_IDLE wait for the 2D and 3D engines
0x20 G3D {n words} 3D command stream (host/fpgagpu/ref3d.py)
flags: 0x01 ACK 0x02 ORDERED 0x04 SYNC
reply: [magic | ver | flags|0x80] [seq] [status] {read data}
- The FPGA replies when the ACK flag is set, when data was read, or when an error occurred.
- Memory addresses are 32-bit word addresses (2M words).
- Memory commands wait until both engines are idle; 2D and 3D commands wait for each other, so drawing follows packet order.
- Ordered packets run exactly once and in sequence order: a SYNC packet
sets the expected sequence number; a packet with an older number is only
acknowledged (its reply was lost); one with a newer number is dropped (an
earlier one was lost) and the host resends from the gap (go-back-N).
libfpgagpu uses this when
fg_opts.orderedis set; fg3d always does. - Framebuffer: one pixel per word, RGB565 in bits 15:0 (bits 31:16 are reserved for Z), stride = width. The first buffer is at word 0, the second at width × height rounded up to 256 words (0x04B000 at 640x480). Write FB_NEXT (0x062) to flip at the next vblank; FB_BASE (0x061) reports the buffer on screen.
The full definitions are in host/fpgagpu/proto.py and rtl/core/net_exec.v.
Verification
| Area | How it is tested |
|---|---|
| Ethernet, ARP, ICMP, UDP protocol | pin-level simulation |
| SDRAM controller | simulation against a timing-checking model |
| Video timing, DVI, VGA | simulation; TMDS decoded back to pixels |
| Framebuffer path | end-to-end system simulation (UDP upload -> HDMI/VGA pixels) |
| 2D engine | random regression against a Python reference model |
| Hardware cursor | simulated VGA and HDMI frames |
| 3D engine | random regression, bit-exact against ref3d.py (also with tiles and texture bases); a directed test of the tile read-after-write interlock; through the network in the system simulation |
| Tiled rendering in fg3d | demo3d, fgl_test and gldemo frames identical with and without tiles (emulator); a demo3d frame through the RTL in both modes |
| Ordered packets | system simulation (drop, gap, duplicate, wrap); host test with 3% loss |
| Display modes | unit test; xsim against Xilinx's MMCME2_ADV model (make sim-vclk-unisim); clock switch during scanout in the system simulation |
libfpgagpu, fpgamon |
against the emulator (loss, FPGA restart, tearing); EVDI on KDE Plasma (Wayland) |
fg3d, demo3d, fgl |
against the emulator (76 fgl pixel checks, also with 3% loss) |
| Real hardware | works: gigabit link with default RGMII settings, ping, gpu.py info, SDRAM calibration (clean at latency 5 over all 32 taps) and full memtest, VGA, HDMI up to 1280x720, fpgamon with the hardware cursor on KDE Plasma (Wayland) |
Vivado meets timing with the system clock at 125 MHz and the video clocks at their fastest setting, 75 / 375 MHz (WNS +0.111 ns). The design uses 54% of the LUTs, 79% of the block RAM (the tile buffers take 8 of the 75 RAMB36) and 30 DSP slices of the XC7A50T.
Repository layout
rtl/top_i9plus.v board top: clocks, resets, wiring
rtl/common/ CDC helpers, RAM and FIFO templates
rtl/eth/ RGMII, MAC, RX ring, TX engine
rtl/mem/ SDRAM controller, I/O PHY, arbiter
rtl/video/ timing, pixel clock (MMCM DRP), scanout, test patterns, TMDS, HDMI serialiser, VGA
rtl/core/ network command processor, register file
rtl/gfx/ 2D engine
rtl/g3d/ 3D engine: setup, span walker, span engine, pixel pipeline, tile buffers
constraints/ pins and timing (Vivado)
build/vivado.tcl non-project batch build
sim/ testbenches, primitive and SDRAM models, Python checkers
host/gpu.py CLI
host/emulator.py software stand-in for the board
host/fpgagpu/ Python library, protocol definitions, 3D reference model
host/libfpgagpu/ C client library, fg3d renderer, fgl GL shim
host/fpgamon/ EVDI monitor daemon, fbsync (tile diff, moved-content search)
host/demos/ demo3d, gldemo
host/tests/ host tests (run by make host-test)
docs/ hardware setup and bring-up
Licence
MIT; see LICENSE.