An Ethernet MAC in TxHDL

#Ethernet#FPGA#Vivado#Rust#TxHDL#auto

We implemented a Gigabit Ethernet Media Access Controller (MAC) in TxHDL. The architecture consists of a transmitter unit and a receiver unit of approximately eighty lines each, fronted by a three-register AXI-Lite peripheral. Automated builds simulate all three components against a loopback harness with injected frame corruption, cross-checking lowered Verilog and VHDL netlists. For the Alinx AX7A200B FPGA board, a hardware echo design wraps the MAC subsystems, compiling through Vivado into a validated bitstream. This post describes the MAC implementation, sizing optimizations, and board verification status.

Frame framing and physical signaling

An Ethernet client processes frames containing a destination MAC address, a source MAC address, an EtherType field, and payload data. At the physical framing layer, the MAC prepends a seven-byte preamble (0x55), a one-byte Start Frame Delimiter (0xD5), zero-padding up to the 60-byte minimum, and a four-byte CRC-32 Frame Check Sequence (FCS). Following each frame, an Inter-Packet Gap (IPG) of at least twelve idle bytes ensures receiver recovery.

The MAC connects to an external PHY over a Gigabit Media Independent Interface (GMII). GMII transfers eight bits per clock edge at 125 MHz, asserting an out-of-band data valid flag when frame bytes traverse the bus.

Separate clock domains for transmit and receive

The transmitter and receiver operate as decoupled TxHDL units in separate clock domains. The transmitter runs on an internal 125 MHz clock synthesized by the FPGA PLL. The receiver runs on a recovered 125 MHz clock driven by the external PHY.

Both units implement store-and-forward frame buffering:

  • The transmitter accepts incoming bytes from client logic into a 2,048-byte buffer. Because Ethernet transmissions cannot stall mid-frame, transmission starts only after the client asserts the end-of-frame flag. The unit then streams the preamble, frame bytes, padding, FCS checksum, and inter-packet gap without interruption.
  • The receiver captures incoming bytes continuously, updating an active CRC-32 accumulator on each cycle. When the frame terminates, the receiver presents data to client logic only if the FCS checksum matches and the PHY signaled no error. It discards corrupted packets, overlapping frames, or truncated preambles, incrementing internal error counters.

Buffering entire frames decouples client transfer rates from physical line rates, providing clean clock-domain isolation on the board.

Synthesizable parallel CRC-32 calculation

The standard Ethernet polynomial (0xedb88320) calculates CRC-32 by shifting one bit per cycle. Evaluating a full byte sequentially requires eight iterative feedback steps.

Inlining eight iterative steps in lowered HDL code creates deeply nested expressions. To optimize synthesis, the byte calculation is unrolled into 32 parallel XOR equations. Each bit of the updated register evaluates as the parity sum of at most fourteen bits from the prior register and the input byte. Unit tests verify these parallel equations against sequential references across all 256 byte values and multiple seed states.

The receiver reuses this accumulator for frame validation without secondary comparison registers. Feeding the packet payload and appended FCS through the CRC engine yields the fixed residual constant 0xdebb20e3 on valid frames.

AXI-Lite peripheral interface

The EthLite unit exposes an AXI-Lite interface with three memory-mapped registers: a status register, a transmit data port, and a receive data port. Writing to the transmit register responds only after the transmit buffer accepts the byte, applying backpressure to fast producers. Reading the receive register extracts the oldest queued byte, setting a valid bit in the response word. A level interrupt line asserts whenever unread bytes remain in the receive queue.

Although single-byte AXI-Lite transfers could constrain large 1,500-byte frames, TxHDL’s LiteBridge translates multi-beat bursts into sequential single-beat writes. A DMA host can stream an entire Ethernet packet to the transmit register via a single burst request.

Loopback testbench and netlist verification

The automated testbench connects the transmitter and receiver through an AXI host and an AXI-Lite bridge in a closed loopback topology. The host transmits three test frames:

  1. A 20-byte payload, which returns padded to 60 bytes.
  2. A corrupted frame with an inverted bit, which fails FCS validation and is discarded.
  3. A full 72-byte payload, which returns intact.

The test verifies all three outcomes against expected cycle traces. The build system then compiles lowered Verilog and VHDL netlists, simulating both under nvc and Verilator to confirm equivalent behavioral results.

FPGA implementation on the AX7A200B board

The Alinx AX7A200B board interfaces with a JL2121 PHY over Reduced Gigabit Media Independent Interface (RGMII). RGMII transfers four-bit nibbles on both rising and falling clock edges. Supporting double data rate I/O requires vendor-specific I/O primitives. Three compact Verilog wrapper files implement these hardware requirements:

  • An RGMII wrapper with an ODDR-driven transmit clock shifted 90 degrees.
  • An asynchronous clock-domain crossing FIFO with Gray-coded read and write pointers.
  • A frame echo controller that reflects validated packets back to the line.

Vivado synthesizes the design with zero errors and zero critical warnings. Post-implementation timing analysis reports 1.491 ns of positive setup slack. Hardware resource utilization totals 1,330 LUTs, 232 flip-flops, and 19 package pins.

Verification bounds and future work

Several operational aspects remain slated for future testing:

  • Physical cable testing with an active link partner on hardware.
  • Board-level I/O pin delay constraints for PHY-to-FPGA trace lengths.
  • Multi-speed support: the current MAC targets 1,000 Mbps exclusively, requiring additional nibble-rate packing for 10/100 Mbps links.
  • Implementation of the MDIO PHY management interface.

Full implementation source code, test benches, and timing diagrams are documented in the Ethernet chapter of the TxHDL documentation, mirrored in the repository.