FPGA JTAG and ZTEX support
Overview
This area covers the FPGA-specific mining paths for X6500 and ZTEX boards, the shared FT232R-backed JTAG helper used by X6500 programming and register access, the bitstream asset directory, and the user-facing FPGA setup notes.
driver-x6500.c registers the x6500/XBS device driver. Discovery is through the low-level FT232R layer: x6500_lowl_match matches the USB product string X6500 FPGA Miner, and x6500_lowl_probe creates one logical cgpu_info device with two processors for the two FPGAs. x6500_prepare opens the FT232R device, switches it into synchronous bitbang mode, allocates shared JTAG state, and shares that FTDI/JTAG state across both processor records. Each processor then builds its own struct jtag_port in x6500_thread_init, with different pin offsets for the two FPGAs.
X6500 bitstream validation and upload are JTAG-driven. x6500_thread_init resets and detects the JTAG chain, reads the FPGA user code, and compares it to the bit-flipped X6500_BITSTREAM_USERID. If the user ID does not match, or --force-dev-init is active on the root device, x6500_fpga_upload_bitstream opens bitstreams/fpgaminer_x6500-overclocker-0402.bit via open_xilinx_bitstream, issues Xilinx JTAG instructions (JPROGRAM, CFG_IN, JSTART), and streams the bitstream to both FPGAs concurrently. The upload path deliberately switches the FT232R from synchronous to asynchronous bitbang mode for the bulk data phase, then switches back before checking startup state; the progress byte stored just before the shared JTAG state is exposed in the status line by get_x6500_upload_percent.
X6500 mining uses JTAG register writes as the device I/O path. x6500_job_prepare writes the work midstate and block data words into FPGA registers, reads temperature over FT232R CBUS pins, and stores the final data word for x6500_job_start. x6500_job_start writes that last register to start hashing, tracks elapsed hashes based on the dynamic clock multiplier, and schedules async polling. x6500_process_results repeatedly reads nonce register 0xE, submits valid nonces for current or previous work, counts hardware errors for invalid nonces, and feeds dynclock so x6500_dclk_change_clock can adjust register 0xD. Temperature handling in x6500_get_temperature can lower the dynamic clock ceiling when the board exceeds the target plus hysteresis, and later restore it when the board cools.
jtag.c and jtag.h provide the low-level TAP operations used by driver-x6500.c. A struct jtag_port_a owns shared FT232R state, deferred read accounting, and an async-mode flag; each struct jtag_port maps TCK/TMS/TDI/TDO bit masks for one or more devices. jtag_clock emits FT232R bitbang transitions, optionally reads TDO, and batches unnecessary reads to reduce USB latency. _jtag_rw moves to IR or DR capture and delegates bit shifting to _jtag_llrw; the macros in jtag.h wrap complete and staged read/write sequences. jtag_reset, jtag_detect, and jtag_run provide the TAP reset, simple chain detection, and Run-Test/Idle transitions used by the X6500 driver.
driver-ztex.c registers the ztex/ZTX device driver and delegates USB protocol details to libztex.c. Discovery starts with ztex_lowl_match looking for the btcminer for ZTEX USB product through the generic USB low-level layer. ztex_lowl_probe calls libztex_checkDevice to verify ZTEX vendor/product IDs and descriptors; if dummy firmware is detected, libztex_checkDevice may load mining firmware and return CHECK_RESCAN, causing a new detection pass. Once firmware is ready, libztex_prepare_device2 opens the device, reads ZTEX and BTCMiner descriptors, records the bitstream base filename, nonce layout, frequency parameters, hash-per-clock metadata, and supported FPGA count. The driver then claims the USB address and creates one cgpu_info with one thread per FPGA.
ZTEX multi-FPGA access is serialized in driver-ztex.c. ztex_selectFpga locks the root device mutex when a board has more than one FPGA, calls libztex_selectFpga, and ztex_releaseFpga clears the selected FPGA and unlocks. ztex_prepare names each logical processor as <serial>-<fpga-index>, selects the FPGA, calls libztex_configureFpga, sets the default frequency with libztex_setFreq, initializes the work restart notifier, and marks the processor initialized. ztex_shutdown reference-counts the shared libztex_device through handles so the USB handle and mutex are destroyed only after all FPGA threads have shut down.
ZTEX mining sends compact work payloads over vendor control transfers rather than JTAG. ztex_scanhash builds a 44-byte buffer from work->data + 64 and work->midstate, sends it with libztex_sendHashData, then polls libztex_readHashData until nonce overflow or work restart. Read results include per-core progress nonce, hash word, and one or more golden nonce slots; ztex_checkNonce double-SHA256s candidate work data to verify the firmware-reported hash word, while duplicate golden nonces are suppressed with a rotating backlog. Hardware errors feed dynclock, valid nonces are submitted through the miner core, and ztex_updateFreq adjusts frequency or resets the selected FPGA if the frequency update fails.
libztex.c owns the ZTEX USB protocol. libztex_checkDevice reads the ZTEX descriptor using control request 0x22, accepts product families 1.15d/x and 1.15y, distinguishes dummy firmware by descriptor byte 8, and loads the matching mining firmware (ztex_ufm1_15d4 or ztex_ufm1_15y1) from the bitstream loader before asking the detector to rescan. FPGA configuration is handled by libztex_configureFpga, which appends .bit to the descriptor-provided bitstream basename and first tries high-speed configuration (libztex_configureFpgaHS, bulk transfers after requests 0x34/0x35) before falling back to low-speed control transfers (libztex_configureFpgaLS, request 0x32). Both paths detect Xilinx bitstream byte order from sync words and bit-swap when required, verify DONE state with libztex_getFpgaState, and reclaim interface 0 for mining after configuration.
The bitstreams/** owned area is the runtime asset drop location. In this checkout it only contains bitstreams/.gitignore; required .bit and .ihx assets are expected to be supplied by the user or extracted from vendor packages. README.FPGA documents that X6500 and ModMiner-style boards do not persist bitstreams and need the named X6500 bitstream in bitstreams/, and that ZTEX users may need to install dummy firmware with ZTEX BTCMiner before copying the main ZTEX .ihx/.bit files into bitstreams/. todo_ztex.txt records known follow-ups: configurable bitstream directory, high-speed FPGA configuration work, configurable overheat threshold, and hotplug support.
Coupling
- If the X6500 bitstream filename or embedded user ID changes, update both
X6500_BITSTREAM_FILENAMEandX6500_BITSTREAM_USERIDin driver-x6500.c, and update the matching user instructions inREADME.FPGA; otherwisex6500_thread_initwill either reprogram unnecessarily or mine with an unexpected image. - If FT232R bitbang mode handling changes in jtag.c, verify
x6500_fpga_upload_bitstreamin driver-x6500.c: it depends on staged JTAG writes continuing correctly across the synchronous-to-asynchronous switch and onjtag_clocknot trying to read TDO whilejp->a->asyncis true. - If
struct jtag_portbit numbering changes in jtag.h or jtag.c, verifyx6500_jtag_setin driver-x6500.c; X6500 uses pin offsets1,0x10, and combined0x11to address individual FPGAs and program both FPGAs together. - If the ZTEX BTCMiner descriptor format changes, update
libztex_prepare_device2and driver-ztex.c together. driver-ztex.c sizes nonce buffers and submits extra solutions based onnumNonces,offsNonces,hashesPerClock, andextraSolutionsparsed in libztex.c. - If ZTEX firmware or bitstream naming changes, keep
libztex_checkDevice,libztex_configureFpga,README.FPGA, and thebitstreams/asset expectations aligned. The code derives the FPGA.bitfilename from the firmware descriptor but hard-codes the dummy-to-mining firmware names used during detection. - If miner core work layout changes, check both FPGA drivers: driver-x6500.c writes midstate and selected block words into JTAG registers, while driver-ztex.c sends exactly 12 bytes from
work->data + 64plus 32 bytes ofwork->midstateand independently reconstructs data inztex_checkNonce.
Risks
- libztex.c has a licensing warning in its file header explaining that some contributions were originally GPLv2-only and later compatibility was assumed. Reusing or relicensing this module without resolving that history can create a distribution/legal issue.
libztex_checkDevicein libztex.c can write mining firmware to a ZTEX board when dummy firmware is detected, then returnsCHECK_RESCAN. If the firmware filenames inbitstreams/are missing or mismatched, detection will fail before driver-ztex.c can create a miner device.ztex_scanhashin driver-ztex.c allocatesstruct libztex_hash_data hdata[GOLDEN_BACKLOG]withGOLDEN_BACKLOGfixed at 5, but loops overztex->numNoncesreturned bylibztex_prepare_device2. A firmware descriptor reporting more nonce slots than this array can overrun local storage duringlibztex_readHashDataprocessing.x6500_fpga_upload_bitstreamin driver-x6500.c leaves the FT232R in asynchronous bitbang mode until the bulk stream completes. Any early return between the async switch and the synchronous restore can leave later JTAG operations unable to read TDO correctly.
Coverage
- driver-x6500.c
- driver-ztex.c
- libztex.c
- libztex.h
- jtag.c
- jtag.h
bitstreams/**- todo_ztex.txt
README.FPGA