← projectsATmega328P on KX2

ATmega328P on an FPGA: an Arduino Uno clone on the Mercury+ KX2

An ATmega328P rebuilt as a digital circuit and loaded into the Kintex-7 of an Enclustra Mercury+ KX2 module on its Mercury+ PE1 base board. It runs the Arduino Uno's own binaries, cycle for cycle: the stock optiboot boot loader is in the boot section, avrdude -c arduino uploads to it at 115 200 baud, and the Arduino IDE or arduino-cli treat the board as an Arduino Uno. The Uno's pins come out on the PE1's 40-pin header J1401.

Download: atmega328p-kx2-dist.tar.gz (572K): the bitstream, the Verilog, the core alone, the Arduino setup and the Makefile.

3.3 V ONLY. Every J1401 signal goes straight to an FPGA ball at 3.3 V (bank 12, LVCMOS33). No pin is 5 V tolerant (absolute maximum 3.85 V). A 5 V shield or a 5 V USB-serial adapter needs a level shifter on every signal. J1401 pin 2 is the PE1's 12 V input (VCC_MAIN): never a shield's 5V. J1401 has no 5 V pin. The 3.3 V holds only with the PE1's jumper J2000 on 9-10 (VCC_IO_A = 3.3 V).

The ATmega328P as it is built in the FPGA

What is in the archive

path what
bit/top.bit the bitstream: the ATmega328P with optiboot and the Uno's fuses, built from build/
build/ the Vivado inputs of that bitstream: top.v (the board top), m328p_kx2_optiboot.v (the design), top.xdc (balls, I/O standards, clocks, pull-ups), top.tcl (the flow)
core/ the AVR core alone as a reusable Verilog unit (ucore_avr_m328p, three files), its interface descriptor ucore_avr_m328p.json and core/tb/, a Verilator testbench that runs a C program on it
sketches/Blink/ the Arduino IDE's Blink example
scripts/arduino_setup.sh builds arduino-cli from source and sets up the board package kx2:avr
Makefile make prog, make arduino-cli, make blinky PORT=..., make ports, make bit, make core-sim

Quick start

You need: the KX2 on a PE1 (J2000 on 9-10) and the module's USB (J602) plugged into the PC. Nothing else: no USB-serial adapter, no wires. On the PC: go, gcc-avr, avr-libc, avrdude, git, and either openFPGALoader or Vivado's hardware manager with a Xilinx Platform Cable on the PE1's JTAG header J1700.

  1. Load the FPGA. The load is volatile: it is gone at power-off.
    make prog            # openFPGALoader over the module's FT2232H (needs the module's JTAG mode)
    make prog-vivado     # or: Vivado + a Platform Cable on J1700 (the way this board was tested)
  2. Build the Arduino command line once (from source, into ~/opt/arduino-tools):
    make arduino-cli
  3. Upload Blink over the module's own USB serial port:
    make ports
    make blinky PORT=/dev/ttyUSB1
    The port is the module's FT2232H UART channel ("FTDI module link, MCT and UART"; /dev/ttyUSB1 when it is the only FTDI device). There is no DTR on that channel, so the board resets the chip into optiboot by itself when avrdude's sync bytes arrive (see "Auto-reset" below). LED0 on the module (the Uno's "L", D13) blinks once a second.

The Arduino IDE works the same way: board "Arduino Uno", the module's port, Upload. The Serial Monitor is the same port at the sketch's baud rate.

Tested on the board (2026-11-08): make blinky with a serial sketch uploaded through optiboot over /dev/ttyUSB1 with no button pressed; the sketch printed its counter at 115 200 baud and echoed what was typed.

The bench

The bench: the PC and the KX2 on the PE1, one USB cable

  • Clocks. The module's 100 MHz oscillator feeds a PLL: x 8 to 800 MHz, / 50 = 16 MHz, the chip's clock (an Uno's crystal), and / 25 = 32 MHz in phase with it, on which the flash and SRAM block RAMs are read in the middle of each 16 MHz cycle. So every instruction takes exactly the cycles the datasheet gives, and delay(1000) is one second.
  • The serial line is the module's own UART: the FT2232H's second channel on the module's USB, whose two FPGA balls (A20 TX, B20 RX) carry USART0's TXD and RXD, synchronised into the chip's 16 MHz domain. J1401's D0 / D1 (pins 31 / 32) carry the same signals at the same time, joined as an Uno joins its USB chip: D1 drives both, RX is the AND of both lines, so a shield or an external 3.3 V adapter on J1401 still works (one host at a time).
  • Auto-reset. That channel has no DTR. A small detector on RXD watches for avrdude's STK500 sync (0x30 0x20) after an idle line and then pulls RESET for the bootloader, quiet for a second afterwards. Cost: a sketch that itself receives 0 followed by a space after a pause is reset. An external adapter's DTR# on J1401 pin 8 still resets the chip as on an Uno.
  • RESET is the RESET pin (J1401 pin 7), the PE1's button S1104, the auto-reset above and a falling DTR# (a 1.3 ms pulse, as the Uno's 100 nF into its 10 kOhm). When the board pulls RESET, it also drives pin 7 low, so a shield sees the reset.
  • The LEDs are the module's four (active low): LED0 = L (the D13 pin's level), LED1 = TX (D1 low, stretched), LED2 = RX (D0 low, stretched), LED3 = ON.

The pins

Uno R3 header -> J1401 pin -> FPGA ball

Uno pin 328P J1401 FPGA ball Uno pin 328P J1401 FPGA ball
D0 / RX PD0 31 AB21 (pull-up) A0 PC0 15 AE22
D1 / TX PD1 32 AC21 A1 PC1 16 AF22
D2 PD2 29 AB22 A2 PC2 11 V21
D3 PD3 30 AC22 A3 PC3 12 W21
D4 PD4 27 AE23 A4 / SDA PC4 9 V23 (pull-up)
D5 PD5 28 AF23 A5 / SCL PC5 10 V24 (pull-up)
D6 PD6 25 AF24 RESET PC6 7 U24 (pull-up)
D7 PD7 26 AF25 DTR# (adapter) -- 8 U25 (pull-up)
D8 PB0 21 AD25 3.3V / IOREF -- 3 / 23 --
D9 PB1 22 AE25 GND -- 33, 34, 24 --
D10 PB2 19 W20 VIN -- 2 (12 V!) --
D11 PB3 20 Y21 5V, AREF -- none --
D12 PB4 17 AD21
D13 PB5 18 AE21
  • The ICSP header and the R3's extra SDA / SCL pins are the same nets as D11-D13, RESET, A4 and A5, as on the Uno: ICSP MISO 17, SCK 18, MOSI 20, RESET 7, VCC 3 (3.3 V), GND 33.
  • Pins 37-40 belong to the board (its own I2C bus and system controller): never wire a shield's SDA / SCL there; they go to 9 / 10.
  • Pull-ups. RX, SDA, SCL, RESET and DTR# have a fixed pull-up on the FPGA pad. The fabric cannot switch a pull-up at run time, so pinMode(pin, INPUT_PULLUP) sets the chip's register but adds no pull-up on the other pins: an unconnected input floats there.
  • A0..A5 are digital pins. The FPGA has no analog input on this header; the ADC's registers, timing and interrupts are the chip's, and each channel reads a fixed stand-in value (ADCk = 100 k + 23).

What is modelled

The core is the avr5 instruction set of the ATmega328P (every instruction, including MUL, MOVW, LPM, SPM), cycle-exact to the AVR Instruction Set Manual's AVRe column. It was checked instruction for instruction against qemu-system-avr -M uno (no difference over about 158 000 instructions: a C program and random-operand programs), its decoder against avr-objdump on all 65 536 instruction words, and every row of the manual's cycle table.

block status
core: two stages, fetch and execute; 32 registers, SREG, SP; interrupts with the 4-cycle entry; sleep built
flash 32 KB with the boot section, SPM self-programming, BOOTSZ / BOOTRST, signature 1E 95 0F, fuse and lock reads built; the flash is the bitstream's content, an upload lasts until the FPGA is reloaded
the fuses at run time: low / high / extended fuse and lock bits are registers (power-up values: the Uno's), latched like the chip's; CKDIV8, WDTON, BOOTSZ / BOOTRST, RSTDISBL and DWEN obeyed from them built; an ISP write changes them until the FPGA is reloaded
lock bits: LB modes 2 / 3 against ISP, the boot lock bits BLB0x / BLB1x against SPM and LPM, the interrupt hold-off between sections; IVSEL / IVCE (the interrupt vectors in the boot section) built
SRAM 2 KB built
GPIO B, C, D (PORTx / DDRx / PINx, the two-flop synchronizer) built
TIMER0, TIMER2 (8-bit) and TIMER1 (16-bit): every waveform mode (normal, CTC, fast and phase-correct PWM, TIMER1's phase and frequency correct), the OC pins, TIMER1's input capture (from ICP1 or, with ACIC, the analog comparator) built
TIMER2's asynchronous mode (AS2): ASSR's busy flags, the delayed register transfer, counting through power-save built; the 32 768 Hz clock is derived from the 16 MHz clock (no crystal pins, EXCLK not modelled)
USART0: run-time UBRR0, U2X, 5 to 9 data bits (TXB8 / RXB8), parity, stop bits, the two-byte receive buffer, multi-processor mode (MPCM), Master SPI mode (MSPIM: XCK on D4, the four SPI modes, both bit orders) built; synchronous USART mode (UMSEL 01) not yet
SPI master and slave, all four modes, SPI2X built
TWI (I2C) master and slave, every status code of the datasheet's tables built
ADC and analog comparator registers, timing and interrupts built; the values are stand-ins (no analog input)
EEPROM 1 KB with its write timing built; erased at every load (not kept across power-off in this bitstream)
watchdog, external interrupts INT0 / INT1, pin-change interrupts, sleep modes, PRR, CLKPR, MCUSR built
ISP serial programming (the datasheet's serial programming instruction set on SCK / MOSI / MISO while RESET is low: flash, EEPROM, fuses, lock bits, signature; a page write ANDs into the cells) built; avrdude -c stk500v2 against it checked in simulation only
debugWIRE (one-wire debug on RESET) built and always in the part, as on the chip; it takes the RESET pin only when the DWEN fuse is programmed -- off in this bitstream (DWEN is a run-time fuse: ISP can program it)
non-volatile flash / EEPROM / fuses in the module's SPI flash (an append log replayed at power-up) built and routed as a SEPARATE design variant only, never run on hardware; not in this bitstream: here an upload, EEPROM writes and fuse writes last until the FPGA is reloaded
brown-out detector, oscillator selection, analog pins not cloned: no analog front end in the fabric

Status of this release: the same Verilog has run the stock optiboot and an avrdude -c arduino upload of Blink in simulation (Verilator, 974 bytes written and verified), and the bitstream is built and meets timing. A run on the real board has not been reported yet.

Resources and timing

The routed design on the XC7K160T-2 (Vivado 2026.1, the shipped build/):

LUTs of them LUTRAM flip-flops block RAM (36 Kb) DSP
the whole design 5442 502 2778 16.5 1
the ATmega328P 5293 502 2473 16.5 1
the AVR core with its debug window 1696 24 231 0 1
debugWIRE unit (always present; RESET stays RESET while DWEN is off) 622 0 254 0 0
flash 32 KB, SPM, boot section, fuses (block RAM) 187 22 109 16 0
EEPROM 1 KB 510 384 64 0 0
ISP programming engine 856 24 631 0 0
fuse / lock registers, image port 134 0 82 0 0
TIMER0 86 0 69 0 0
TIMER1 224 0 140 0 0
TIMER2 110 0 103 0 0
USART0 190 0 178 0 0
SPI 66 0 57 0 0
TWI 189 0 113 0 0
ADC + analog comparator 93 0 104 0 0
external + pin-change interrupts 36 0 85 0 0
the plain I/O registers 59 48 0 0 0
GPIO B / C / D, WDT, system control, address decode 103 0 68 0 0
board glue: reset from DTR# / button, the LEDs 85 0 133 0 0
SRAM 2 KB 0.5

Of the XC7K160T: 101 400 LUTs, 202 800 flip-flops, 325 block RAMs, 600 DSPs. The rest of the whole design is the board's idle infrastructure.

Timing: all constraints met: setup WNS +7.153 ns (TNS 0.000 ns, 0 failing of 11783 endpoints), hold WHS +0.075 ns. The chip itself runs at 16 MHz with its memories at 32 MHz, both from the 100 MHz oscillator; the board's own idle infrastructure (the Ethernet PHY's management) runs at 100 / 25 MHz.

The core alone, out of context on an Artix-7 (xc7a100t-1) with the debug window: about 1 090 LUTs (32 of them the register file as distributed RAM, two read ports and one write port), 253 flip-flops, 1 DSP, about 58 MHz; without the debug window about 780 LUTs and 164 flip-flops. 16 MHz needs well under a third of that speed. In the board build the debugWIRE unit is always present (as on the chip), so the core keeps its debug window there: 1 697 LUTs and 231 flip-flops, the debugWIRE unit 623 LUTs and 254 flip-flops (the table above).

The ucore API: using the core in your own design

core/ holds the AVR core without any peripheral, as plain Verilog-2001: ucore_avr_m328p (the top, no parameters), ucore_avr_m328p_1_1_1_1 and gen_ram_8x16_r2_w1_d (the register file: two read ports, one write port). It speaks the ucore interface: one request / response bundle per port, the shape of Wishbone classic, and nothing else about the system. AHB, AXI, APB, Wishbone and register maps are adapters you put outside; the core never changes with the system around it. core/ucore_avr_m328p.json lists the same in machine-readable form.

The port groups of ucore_avr_m328p

The port groups

group ports meaning
system clk, rst (synchronous, active high), rst_n (synchronous, active low), ce, rstv[13:0], sleeping rst_n is the core's reset (low: the core restarts at rstv); rst sets every flip-flop to its initial value, needed only where flip-flops have no power-up value (on an FPGA tie it to 0, as the testbench does). ce is the cycle enable: nothing in the core moves on a clk edge with ce = 0, so a 16 MHz chip can run on a faster clock with ce high one clock in N. rstv is the reset vector as a word address (0, or 0x3F00 for optiboot's boot section with BOOTSZ = 256 words). sleeping is high from a sleep until an interrupt wakes the core.
P p_q_valid, p_q_addr[13:0], p_q_we, p_q_be[1:0], p_q_wdata[15:0], p_q_space[2:0], p_q_priv, p_q_lock; p_r_ready, p_r_rdata[15:0], p_r_resp[1:0] the program port: every instruction fetch (one 16-bit word a cycle), lpm reads and spm writes; the address is a word address
D d_q_valid, d_q_addr[15:0], d_q_we, d_q_be, d_q_wdata[7:0], d_q_space[2:0], d_q_priv, d_q_lock; d_r_ready, d_r_rdata[7:0], d_r_resp[1:0] the data port: SRAM and the I/O registers at their data-space addresses (I/O 0x00 at data 0x20, extended I/O 0x60..0xFF, SRAM from 0x100); byte address
irq irq[25:0], nmi; irq_ack, irq_id[7:0] 26 level request lines; line k is the 328P's vector k (line 0 is RESET's slot and never taken), the lowest line wins. When the core takes one, irq_ack pulses for one clock with irq_id = the line: the peripheral clears its flag on that edge. nmi is ignored (the AVR has none).
evt evt[2:0] one-clock pulses: evt[0] = wdr, evt[1] = sleep, evt[2] = spm (the watchdog, the sleep controller and the flash controller see them)
hold hold_req; hold_ack hold_req asks the core to stop at the next instruction boundary; hold_ack is high while it stays stopped (neither port asks)
dbg dbg_valid, dbg_we, dbg_addr[7:0], dbg_wdata[31:0], dbg_halt_req; dbg_ready, dbg_rdata[31:0], dbg_halted the debug window (below): a slave with the same handshake, no space or byte enables
trace tr_valid, tr_pc[13:0] tr_valid pulses when an instruction retires, tr_pc is its word address

Unused outputs of a port are driven 0: p_q_priv, p_q_lock, d_q_priv, d_q_lock are always 0, d_q_be is always 1, p_q_be is 2'b11.

The handshake

A zero-wait access and an access with two wait states

  1. An access completes on the rising clk edge where valid && ready && ce.
  2. While valid && !(ready && ce), the request (addr, we, be, wdata, space, priv, lock) does not change, and valid is not withdrawn.
  3. ready may be a combinational function of the request: a zero-wait access completes in the cycle it is asked. That is how the AVR keeps its cycle counts: every fetch, in, out, sbis complete in their own cycle, so memories and I/O must answer at once (the shipped design reads its block RAMs on a doubled clock for that).
  4. rdata and resp are sampled in the completing cycle.
  5. One access at a time per port: no bursts, nothing outstanding. A read-modify-write instruction (sbi, cbi) is two accesses.
  6. A wait state on either port (ready low) freezes the whole core, so a slow slave stretches the instruction, exactly as a wait state would on a bus.
  7. A slave's side effects (a read that clears a flag, a FIFO pop) belong to the completing edge: qualify them with ce too.

resp: 0 OK, 1 ERR (the access is refused: the core halts into debug, see STATUS), 2 and 3 not used by the AVR.

The spaces

space name on the AVR
0 mem every D access: SRAM and I/O registers
1 fetch every instruction fetch on P
4 pdata lpm reads and spm writes on P
5 cpu fuse, lock and signature rows (lpm with BLBSET / SIGRD in SPMCSR) on P
2, 3, 6, 7 io, iack, reserved not used by the AVR

What stays inside the core

R0..R31 (data 0x00..0x1F), SPL / SPH (I/O 0x3D / 0x3E) and SREG (I/O 0x3F) are the core's own: every instruction that reaches them (ld / st / lds / sts / in / out, and the debug window's MEM) is served inside, and they never appear on D. Your system decodes everything else. SP resets to 0x08FF (RAMEND), SREG to 0; the register file is not reset. The SMCR's SE bit (data 0x53) is kept by the core from its own writes (it decides whether sleep sleeps); the write still goes out on D.

The debug window

A 256-word register window, the same map for every ucore core, so a debugger needs one driver. Behind a transport of your choice (debugWIRE, JTAG, a UART). Halting happens at instruction boundaries.

addr register bits
0x00 ID (read) kind 0..7 (2 = AVR), variant 8..11 (5 = avr5), version 12..15 (1), NREG 16..22 (33), NBP 24..27 (1), INJECT 28, TRACE 29, HOLD 30. This core reads 0x71211502.
0x01 CTRL (write; a 1 does it) HALT 0, RESUME 1, STEP 2, RESET core 3 (with HALT: stops at the reset address), RESET system 4 (not wired)
0x02 STATUS (read) halted 0, running 1, cause 4..7 (1 halt request, 2 step done, 3 breakpoint, 4 break instruction, 5 illegal instruction, 6 bus ERR), breakpoint index 8..11, ERR port 12 (0 P, 1 D), ERR space 13..15, MEM busy 16, INJECT waits for its second word 17, an injected instruction runs 18
0x03 PC (read / write while halted) the word address of the next instruction
0x04 FLAGS SREG
0x05 ERRADDR (read) the address of the refused access
0x10 + i REG[i] Ri for i < 32; REG[32] (0x30) = SP
0x50 + k BP[k] the word address in bits 0..15, enabled = bit 31 (one breakpoint)
0x60 MADDR MEM: the address of one access
0x61 MDATA the data to write, or the data read
0x62 MCTL space 0..2, we 3
0x63 MGO write: do the access (halted only); read: busy 0, resp 4..5. The access goes on P for fetch / pdata (word address), otherwise through the data path exactly as the program sees it (R0..R31, SREG, SP inside, the rest on D)
0x64 INJECT write: execute this instruction word while halted (a two-word instruction takes its second word in a second write); the PC does not move

dbg_halt_req (a level) halts like CTRL HALT; dbg_halted is high while halted. A break instruction halts before it executes, with the PC on it. After a resume or a step, the first instruction does not break on its own address.

A minimal system

ucore_avr_m328p cpu (
  .clk(clk), .rst(1'b0), .rst_n(rst_n), .ce(1'b1), .rstv(14'd0),
  .p_q_valid(pv), .p_q_addr(pa), .p_q_we(pw), .p_q_be(), .p_q_wdata(pwd), .p_q_space(), .p_q_priv(), .p_q_lock(),
  .p_r_ready(1'b1), .p_r_rdata(flash[pa]), .p_r_resp(2'd0),          // zero wait: an asynchronous read
  .d_q_valid(dv), .d_q_addr(da), .d_q_we(dw), .d_q_be(), .d_q_wdata(dwd), .d_q_space(), .d_q_priv(), .d_q_lock(),
  .d_r_ready(1'b1), .d_r_rdata(drd), .d_r_resp(2'd0),
  .irq(irq), .nmi(1'b0), .irq_ack(irq_ack), .irq_id(irq_id), .evt(evt),
  .hold_req(1'b0), .hold_ack(),
  .dbg_valid(1'b0), .dbg_we(1'b0), .dbg_addr(8'd0), .dbg_wdata(32'd0), .dbg_halt_req(1'b0),
  .dbg_ready(), .dbg_rdata(), .dbg_halted(),
  .tr_valid(), .tr_pc(), .sleeping(sleeping));

make core-sim builds core/tb/: the core under Verilator with a 16 K-word program memory on P and 4 KB on D, both zero wait, UDR0 (0xC6) as a putchar. It compiles hello.c with avr-gcc and prints:

debug ID 0x71211502
hello from the core
table 01df5e76 fib(15) 0262 1000000/7 00022e09

cycles 59205 instructions 37817

Rebuilding the bitstream

make bit runs Vivado on build/ (top.tcl: synthesis, placement, routing, the reports, build/top.bit); then make prog BIT=build/top.bit. The part is the XC7K160T-2 FFG676 (Vivado's free edition should cover it: check your version's device list). The Verilog has no comments and no source references: it is generated.

Credits

The boot loader in the bitstream is the stock Optiboot (GPL v2, https://github.com/Optiboot/optiboot), built from its source for the ATmega328P at 16 MHz, 115 200 baud. make arduino-cli fetches ArduinoCore-avr (LGPL) and builds arduino-cli (GPL v3), serial-discovery and ctags from their sources.