PIC16F877A on an FPGA: a PICDEM 2 Plus on the Mercury+ KX2
A PIC16F877A rebuilt as a digital circuit, with the parts of
Microchip's PICDEM 2 Plus demonstration board around
it, loaded into the Kintex-7 of an Enclustra Mercury+ KX2 module on its
Mercury+ PE1 base board. It runs the 877A's own programs cycle for
cycle: assemble one with gpasm (or compile it with
sdcc -mpic14 or XC8), put its hex into the design, and it
runs at 4 MHz, 1 MIPS, as on the board. The PICDEM 2 Plus's parts that
an FPGA ball cannot be are modelled next to the chip: the 2 x 16
character LCD, the 24LC256 EEPROM, the TC74 temperature sensor and the
pot. Its port pins come out on the PE1's 40-pin header J1401. Its serial
port is the board's own USB serial (the PE1's micro USB), and a
bootloader in the top of the program memory takes new programs over it,
with no FPGA rebuild. The LCD's screen and the pot are on the PC, over
the module's Ethernet.
Download: pic16f877a-kx2-dist.tar.gz (728K): the bitstream, the Verilog, the core alone, the bootloader and its loader, the LCD / pot host program, the example programs 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), and the PICDEM 2 Plus is a 5 V board: nothing of a 5 V circuit goes onto J1401 without a level shifter. Never wire an RS-232 line (a DB9 / COM port, ±10 V) to J1401. J1401 pin 2 is the PE1's 12 V input (VCC_MAIN). The 3.3 V holds only with the PE1's jumper J2000 on 9-10 (VCC_IO_A = 3.3 V).
What is in the archive
| path | what |
|---|---|
bit/top.bit |
the bitstream: the PIC16F877A with the PICDEM 2 Plus's parts, the
bootloader and programs/lcd.asm in its program memory,
built from build/ |
build/ |
the Vivado inputs of that bitstream: top.v (the board
top), p877a_kx2_picdem2_img.v (the design, the program
memory's contents included), top.xdc (balls, I/O standards,
clocks, pull-ups), top.tcl (the flow) |
core/ |
the PIC core alone as a reusable Verilog unit
(ucore_pic_p16f877a, three files), its interface descriptor
ucore_pic_p16f877a.json and core/tb/, a
Verilator testbench that runs a program on it |
programs/ |
the example programs (gpasm), pd2.inc (the board's
routines: delays, USART, LCD, I2C), and gpsim/ (each
example on gpsim with its expectations) |
boot/pd2boot.asm |
the serial bootloader (gpasm), at 1F00h..1FFFh |
scripts/pd2boot.py |
the bootloader's loader (upload) and the image tool
(image: the bootloader + a program); Python 3, its standard
library only |
host/ |
the PC side of the LCD's screen and the pot: system.cpp
with its parts, the bridge and its UDP adapter (C++17) |
scripts/hex2v.py |
puts an image's hex into the design's program memory |
Makefile |
make prog, make upload HEX=...,
make host-lcd, make programs,
make gpsim, make image HEX=...,
make bit, make core-sim |
Quick start
You need: the KX2 on a PE1 (J2000 on 9-10), the module's USB (J602,
the JTAG) and the PE1's micro USB (J603, the serial port) plugged into
the PC, and on the PC openFPGALoader, Python 3 and a
terminal program; gputils (gpasm) for your own programs; a
C++ compiler for the LCD host program and the module's Ethernet on the
PC's network (192.168.77.0/24) to see the LCD.
- Load the FPGA. The load is volatile: it is gone at power-off.
The board's serial port is the PE1's micro USB: open it (make prog/dev/ttyUSB1on Linux) at 9600 8N1. After a reset (PE1 button 0 = MCLR) the bootloader listens for about 0.6 s, then the shipped program writes "PICDEM 2 Plus" / "PIC16F877A KX2" to the LCD and the same two lines to the serial port, and lights all four LEDs. - See the LCD and set the pot on the PC:
make host-lcd # prints the 2 x 16 screen at each change echo 512 > /tmp/pot; PD2_POT=/tmp/pot make host-lcd # AN0 = 512 of 1023 (2.50 V) - Another program, through the bootloader (close the terminal first:
the loader needs the port):
The program is in the FPGA's program memory until the FPGA is reloaded.make programs make upload HEX=out/temp.hex # press PE1 button 0 when it asks
The bench
- Clocks. The module's 100 MHz oscillator feeds a PLL: x 8 to 800 MHz, / 32 = 25 MHz. The chip divides it by 25 / 4 into its oscillator (a Q phase every 6 or 7 clocks), Fosc = 4 MHz, the PICDEM 2 Plus's canned oscillator: four Q phases an instruction cycle, 1 MIPS, so every delay loop of a PICDEM 2 Plus program takes the time it says. The modelled parts and the debug link's side run on the same 25 MHz.
- The serial line (RC6 / RC7) is the module UART
(balls B20 / A20) behind the PE1's micro USB J603
(
/dev/ttyUSB1), taken in the chip's own clock through two flops. The same TX is also on J1401 pin 32, and RX is J1401 pin 31 AND the module's line, so a 3.3 V USB-serial adapter on the header still works. The PICDEM 2 Plus's MAX232A and DB9 are not there. No DTR or RTS is used. - The bootloader (
boot/pd2boot.asm) sits at 1F00h..1FFFh. After a reset it listens for about 0.6 s for the loader's hello (C1h), then runs the program.scripts/pd2boot.pywrites a program in the chip's own 4-word flash blocks (EECON1 / EECON2), reads every block back, and starts it. A program's first four words (its reset vector, agoto) are moved into the bootloader's slot; programs must leave 1F00h.. free. - The LCD's screen and the pot travel over the module's Ethernet (UDP to 192.168.77.3), or over JTAG. The chip's side writes the 32 characters at each change to the PC and asks the PC for each A/D sample.
- MCLR is J1401 pin 7 and PE1 button 0 (the board's S1). A pressed button also pulls pin 7 low.
- The switches: PE1 button 1 is S2 on RA4 and button 2 is S3 on RB0. Both pull their pins low, as the board's switches do.
- The LEDs are the module's four: LED0..LED3 are the red LEDs D2..D5 on RB0..RB3, lit high as on the board. They show the pins' levels.
The pins
| PIC pin | PICDEM 2 Plus | J1401 | FPGA ball | PIC pin | PICDEM 2 Plus | J1401 | FPGA ball | |
|---|---|---|---|---|---|---|---|---|
| RC7 / RX | RS-232 RX | 31 | AB21 (pull-up) | RC4 / SDA | I2C: 24LC256, TC74 | 9 | V23 (pull-up) | |
| RC6 / TX | RS-232 TX | 32 | AC21 | RC3 / SCL | I2C | 10 | V24 (pull-up) | |
| RB0 | LED D2, switch S3 | 29 | AB22 (pull-up) | MCLR | switch S1 | 7 | U24 (pull-up) | |
| RB1 | LED D3 | 30 | AC22 | RA4 | switch S2 (open drain) | 8 | U25 (pull-up) | |
| RB2 | LED D4 | 27 | AE23 | RA5 | -- | 5 | AC23 | |
| RB3 | LED D5 | 28 | AF23 | RE0 | -- | 6 | AC24 | |
| RB4 | -- | 25 | AF24 | RE1 | -- | 35 | AA23 | |
| RB5 | -- | 26 | AF25 | RE2 | -- | 36 | AB24 | |
| RB6 | ICD PGC | 21 | AD25 | RD4 | -- | 15 | AE22 | |
| RB7 | ICD PGD | 22 | AE25 | RD5 | -- | 16 | AF22 | |
| RC0 | T1OSO (32 kHz crystal) | 19 | W20 | RD6 | -- | 11 | V21 | |
| RC1 | T1OSI | 20 | Y21 | RD7 | -- | 12 | W21 | |
| RC2 | CCP1: the buzzer | 17 | AD21 | 3.3 V / GND | 3, 14, 23 / 33, 34, 24 | -- | ||
| RC5 | -- | 18 | AE21 | VCC_MAIN | 12 V! | 2 | -- |
- Inside the FPGA, not on the header: RA0 (AN0, the pot), RA1..RA3 and RD0..RD3 (the LCD). These are exactly the eight pins J1401 has no room for.
- The I2C bus on pins 9 / 10 is the one the modelled 24LC256 (0x50) and TC74 (0x4D) sit on. An external I2C device there joins them.
- Pins 37-40 belong to the board (its own I2C bus and system controller): never wire anything there.
- Pull-ups. RX, RB0, RA4, MCLR, SDA and SCL have a fixed pull-up on the FPGA pad: the board's resistors. The fabric cannot switch a pull-up at run time, so PORTB's weak pull-ups (OPTION_REG RBPU) add none on RB1..RB7.
What is modelled
The core is the PIC mid-range instruction set of the PIC16F877A (all 35 instructions), cycle-exact to the data sheet's Table 15-2. A fetch and an execute overlap, four Q phases an instruction cycle, two cycles for a jump or a taken skip, and the 8-level stack. Its decoder covers all 16 384 instruction words.
| block | status |
|---|---|
core: W, STATUS, FSR, PCLATH, the 8-level stack, the interrupt
entry, sleep / clrwdt |
built |
| program memory 8 K words | built, in block RAM; its contents are the bitstream's
(make image, make bit) |
| file RAM 368 bytes, the banks and their mirrors | built |
| PORTA..PORTE with TRIS, RA4 open drain, RB0/INT and the RB<7:4> change interrupt, the parallel slave port | built |
| TMR0 with its prescaler, TMR1 (synchronous and asynchronous counter), TMR2 with PR2 | built |
| CCP1, CCP2: capture, compare, PWM | built |
| MSSP: SPI master / slave (CKE, SMP) and I2C: master with bus-collision detection, slave with 7- and 10-bit addresses | built |
| USART: asynchronous (8 / 9 bits, address detect) and synchronous master / slave | built |
| ADC 10-bit, 8 channels, the comparators and CVREF | registers, timing and interrupts built; the analog values come from a table (AN0 = 3.30 V of 5.00 V) |
| data EEPROM 256 bytes, flash self-read / self-write | built; erased at every load |
| watchdog, power-up timer, oscillator start-up, MCLR; every register's value after a power-on, MCLR or watchdog reset (the data sheet's Table 14-6) | built |
| brown-out, LVP / ICSP programming, the ICD's DEBUG bit, code protection | not cloned |
| the PICDEM 2 Plus's LCD (HD44780, 4-bit), 24LC256 EEPROM (32 KB, page writes, acknowledge polling), TC74 sensor (22 °C) | modelled next to the chip, on its pins |
| the pot on AN0 | a fixed 3.30 V |
| the buzzer, the 32 kHz crystal, the RS-232 level shifter | not on the board: RC2, RC0 / RC1, RC6 / RC7 are header pins |
The example programs (programs/) do
what the PICDEM 2 Plus User's Guide (DS51275) describes for the
board:
| program | what it does |
|---|---|
led_count.asm |
D2..D5 count in binary, 250 ms a step |
button.asm |
D3 lit while S2 is held, D4 while S3 is held; D5 toggles on each S2 press, debounced in software |
echo.asm |
9600 8N1: a banner, then every byte echoed, its low bits on the LEDs |
lcd.asm |
the LCD in 4-bit mode: two lines of text, the same text on the serial port (the shipped program) |
temp.asm |
the TC74 through the MSSP at 100 kHz, "Temp = 022 C" on the LCD and the serial port every second |
eeprom.asm |
16 bytes as one page write to the 24LC256, acknowledge polling, a sequential read back, "EEPROM ok" |
volt.asm |
AN0 through the ADC as "Volts = 3.30V" |
All seven run on gpsim with their expectations met
(make gpsim; gpsim has no LCD and no TC74, so there the
text is checked on the serial port and the TC74 read is the
not-acknowledged path). All seven also ran in a simulation of this whole
design: the chip, the LCD, the EEPROM and the sensor on their pins, the
buttons pressed by the bench. Their LCD lines, serial text and LEDs came
out as above ("Temp = 022 C", "EEPROM ok", "Volts = 3.30V").
Status of this release: the bitstream is built from
the shipped build/ 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 | 9605 | 1756 | 7052 | 13.5 | 0 |
| the PIC16F877A | 2079 | 154 | 1191 | 3.5 | 0 |
| the PIC core (its debug window has no master in this build) | 713 | 10 | 179 | 0 | 0 |
| file RAM 368 bytes | 112 | 96 | 0 | 0 | 0 |
| data EEPROM 256 bytes, flash self-write | 161 | 48 | 119 | 0 | 0 |
| MSSP (SPI, I2C) | 254 | 0 | 140 | 0 | 0 |
| USART | 183 | 0 | 149 | 0 | 0 |
| ADC, comparators, CVREF | 231 | 0 | 207 | 0 | 0 |
| the PICDEM 2 Plus's parts | 2116 | 0 | 1982 | 8 | 0 |
| 24LC256 EEPROM (block RAM) | 533 | 0 | 681 | 8 | 0 |
| TC74 sensor | 94 | 0 | 87 | 0 | 0 |
| HD44780 LCD | 1489 | 0 | 1214 | 0 | 0 |
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.150 ns (TNS 0.000 ns, 0 failing of 16930 endpoints), hold WHS +0.052 ns. The chip and the modelled parts run at 25 MHz (Fosc 4 MHz as the chip's enable); the board's own idle infrastructure (the Ethernet PHY's management) runs at 100 / 25 MHz.
The ucore API: using the core in your own design
core/ holds the PIC core without any peripheral, as
plain Verilog-2001: ucore_pic_p16f877a (the top, no
parameters), ucore_pic_0_9__1__1__1__1_1_1_1_0 and
gen_ram_13x8_r1_w1_d (the stack). 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_pic_p16f877a.json lists the same in
machine-readable form. This export has the debug window built, with one
breakpoint.
The port groups
| group | ports | meaning |
|---|---|---|
| system | clk, rst (synchronous, active high),
rst_n (synchronous, active low), ce,
sleeping |
ce is the oscillator: one
ce is one Q phase, four make an instruction cycle (Q1 the
file read, Q2 the fetch, Q3 nothing, Q4 the file write and the commit).
Nothing moves on a clk edge with ce = 0, so a
4 MHz PIC runs on a 16 MHz clock with ce high one clock in
four. rst_n is the reset, taken on a ce edge:
the core restarts at address 0, and the first instruction executes in
the second cycle. 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). sleeping is high from a sleep
until irq rises. |
| P | p_q_valid, p_q_addr[12:0],
p_q_we, p_q_be, p_q_wdata[13:0],
p_q_space[2:0], p_q_priv,
p_q_lock; p_r_ready,
p_r_rdata[13:0], p_r_resp[1:0] |
the program port: the instruction fetch, one 14-bit word in Q2 of every cycle; a word address |
| D | d_q_valid, d_q_addr[8: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: the file, at the unfolded address. That is RP1:RP0:f for a direct access and IRP:FSR for an indirect one. The read is in Q1, the write in Q4. Your system decodes the mirrors, the common RAM 0x70..0x7F of every bank and the unimplemented addresses, so one core serves every mid-range map. |
| irq | irq, nmi; irq_ack,
irq_id |
one level request: your system ORs its enabled
flags (T0IE & T0IF, INTE & INTF, RBIE & RBIF, PEIE &
(PIE & PIR)), and GIE does not mask it. At a commit with GIE = 1 the
core takes it: the word behind is dropped, a forced call to 0x0004 runs,
GIE is cleared, irq_ack pulses for one clock
(irq_id = 0). No flag is cleared: the handler clears its
own. irq high also wakes a sleeping core. nmi
is not used by the PIC. |
| evt | evt[1:0] |
one-clock pulses: evt[0] = sleep (/PD
cleared, /TO set), evt[1] = clrwdt (the
watchdog sees them) |
| hold | hold_req; hold_ack |
hold_req asks the core to stop at the next commit;
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[12:0] |
tr_valid pulses at the commit of an instruction,
tr_pc is its word address |
Unused outputs of a port are driven 0: p_q_we,
p_q_priv, p_q_lock, d_q_priv,
d_q_lock are always 0, and p_q_be,
d_q_be are 1.
The handshake
- An access completes on the rising
clkedge wherevalid && ready && ce. - While
valid && !(ready && ce), the request (addr,we,be,wdata,space,priv,lock) does not change, andvalidis not withdrawn. readymay be a combinational function of the request: a zero-wait access completes in the cycle it is asked. Withcehigh one clock in four, a memory has three idle clocks before the next phase, so a block RAM with a registered read, clocked onclk, answers in time.rdataandrespare sampled in the completing cycle.- One access at a time per port: no bursts, nothing
outstanding. A read-modify-write instruction (
bsf,incf f,1) is two D accesses, the read in Q1 and the write in Q4, as on the chip.movwfandclrfdo not read. - A wait state (
readylow) holds the Q phase: the cycle stretches, and no count of cycles changes. - A slave's side effects (a read that clears a flag, a FIFO pop)
belong to the completing edge: qualify them with
cetoo.
resp: 0 OK. This build ignores ERR: the PIC has no bus
exception. 2 and 3 are not used.
The spaces
space |
name | on the PIC |
|---|---|---|
| 0 | mem | every D access: the file (RAM and the special function registers) |
| 1 | fetch | every instruction fetch on P |
| 4 | pdata | the debug window's program-word access on P (MEM, below) |
| 2, 3, 5, 6, 7 | io, iack, cpu, reserved | not used by the PIC |
What stays inside the core
INDF, PCL, STATUS, FSR and PCLATH (file addresses 0x00, 0x02, 0x03, 0x04, 0x0A in every bank), W and the 8-level stack are the core's own. Every instruction that reaches them, and the debug window's MEM, is served inside: they never appear on D.
- INDF read through FSR = INDF reads 0.
- PCL reads the low byte of the next instruction's address. A computed
addwf PCL, ftable starts after the instruction. - A write of PCL takes PC<12:8> from PCLATH;
gotoandcalltake PC<12:11> from PCLATH<4:3>. - INTCON (0x0B) is on D, because its flags belong to your peripherals. The core overlays bit 7, GIE, which it owns: a read gets your bits 6..0 with the core's GIE, and a write goes out whole while the core keeps bit 7.
- STATUS powers up as 0001 1xxx (/TO, /PD set).
The debug window
A 256-word register window, the same map for every ucore core, so a debugger needs one driver. Put it behind a transport of your choice (JTAG, a UART). A halt is taken at a commit, the Q4 that ends an instruction cycle.
| addr | register | bits |
|---|---|---|
| 0x00 | ID (read) | kind 0..7 (4 = PIC), family 8..11 (1 = mid-range), version 12..15 (1), NREG 16..22 (12 = 4 + the stack's 8), NBP 24..27 (1), INJECT 28 (0), TRACE 29, HOLD 30. This core reads 0x610C1104. |
| 0x01 | CTRL (write; a 1 does it) | HALT 0, RESUME 1, STEP 2 (exactly one instruction retires), RESET core 3 (with HALT: stops at address 0 before anything executes), RESET system 4 (not wired) |
| 0x02 | STATUS (read) | halted 0, running 1, cause 4..7 (1 halt request, 2 step done, 3 breakpoint), breakpoint index 8..11, MEM busy 16 |
| 0x03 | PC (read / write while halted) | the 13-bit address of the next instruction; a write drops the fetched word, PCLATH stays |
| 0x04 | FLAGS | STATUS, all eight bits (a debugger may set /TO and /PD) |
| 0x05 | ERRADDR (read) | the address of a refused access |
| 0x10 + i | REG[i] | 0 W, 1 FSR, 2 PCLATH, 3..10 the stack entries, 11 the stack pointer |
| 0x50 + k | BP[k] | the program address in bits 0..12, 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. For fetch / pdata it is a program word on P; otherwise it is the file as the program sees it at the unfolded 9-bit address (INDF, PCL, STATUS, FSR, PCLATH inside, the rest on D) |
| 0x64 | INJECT | 0: not built |
A breakpoint halts before the instruction at its address executes.
dbg_halt_req is latched: a one-clock pulse is enough. A
halt wakes a sleeping core. dbg_halted is high while
halted.
A minimal system
ucore_pic_p16f877a cpu (
.clk(clk), .rst(1'b0), .rst_n(rst_n), .ce(ce), // ce: Fosc, e.g. 1 clock in 4
.p_q_valid(pv), .p_q_addr(pa), .p_q_we(), .p_q_be(), .p_q_wdata(), .p_q_space(), .p_q_priv(), .p_q_lock(),
.p_r_ready(1'b1), .p_r_rdata(prog[pa]), .p_r_resp(2'd0), // zero wait
.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(), .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 an 8 K-word program memory on P and the 512-byte
file on D, both zero wait, ce every clock, TXREG (0x19) as
a putchar and PIR1 (0x0C) reading TXIF. It assembles
hello.asm with gpasm and prints:
debug ID 0x610c1104
hello from the PIC16F877A core
clocks 2039 instructions 319
Rebuilding the bitstream
make upload needs no rebuild. To make a program the
bitstream's own: make image HEX=f.hex writes the bootloader
and the program into the program memory's contents in
build/p877a_kx2_picdem2_img.v (the first version is kept as
.orig). make bit runs Vivado on
build/ (top.tcl: synthesis, placement,
routing, the reports, build/top.bit); then
make prog BIT=build/top.bit. The configuration word is part
of the built design (XT, WDT off, PWRT on, BOR off, LVP off): a
program's own __CONFIG is reported when it differs, and not
applied. 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 board is Microchip's PICDEM 2 Plus (DM163022), as its User's
Guide DS51275 describes it. The example programs are new code written
from that guide. The PIC16F877A is Microchip's (data sheet DS39582).
gputils (gpasm) and gpsim are the GNU PIC
utilities and simulator.