FPGA: Added a workaround for the GW1N-4D IDCODE(Flaws of Apicula).

This commit is contained in:
dxl committed 2026-10-12 19:32:41 +08:00
1 parent e69b791612
commit 4f01016608
4 files changed
+118 -3

No files matched your search

+9
View File
@@ -59,6 +59,15 @@ jobs:
test "$fs_size" -gt $((bin_size * 6))
test "$fs_size" -lt $((bin_size * 10))
# The device only accepts a bitstream naming its own IDCODE and
# otherwise stays unconfigured, with nothing driving the antennas.
# apycula's GW1N-4 database writes the plain GW1N-4, so the build
# stamps the part's in (fpga/common/gowin_devid.py) - keep it that way.
# The IDCODE is the four bytes at 0x1C of the header.
idcode=$(od -An -tx1 -j 28 -N 4 fpga/fpga_pm5.bin | tr -d ' \n')
echo "device idcode in the bitstream: 0x${idcode}"
test "$idcode" = "1100381b"
# A rebuild from scratch has to produce the same bytes; the tools carry no
# timestamp, so a difference means something started leaking one in, and
# a released bitstream could no longer be checked against the sources.
+95
View File
@@ -0,0 +1,95 @@
#!/usr/bin/env python3
# Stamp the real device IDCODE into a Gowin bitstream (.fs from apycula's
# gowin_pack), in place, and repair the CRC that depends on it.
#
# apycula's GW1N-4 database writes 0x0100381B, the plain GW1N-4, but the
# GW1N-4B/4D silicon on a PM5 reports 0x1100381B. The device compares that
# field with its own IDCODE and refuses an image that disagrees: the FPGA stays
# unconfigured (Gowin status register done_final = 0), so nothing drives the
# antenna and `hw tune` reads a couple of volts instead of tens. `hw fpga
# config` prints the value to use here as "gowin_jtag OK: idcode = ...".
#
# Only the IDCODE changes. The running CRC that apycula's write_bitstream()
# carries into the first row covers the header entries, so that one CRC is
# recomputed. Every other row is verified rather than rewritten, so a change to
# the apycula bitstream layout is reported instead of silently producing an
# image that will not configure.
#
# usage: gowin_devid.py <file.fs> <idcode>
import sys
PREAMBLE = 3 # write_bitstream() starts folding header entries into the CRC after these
SKIP = 0xd2 # ... and leaves the SPI address entry out
def crc16_arc(data):
"""CRC-16/ARC: poly 0x8005 reflected, init 0, xorout 0, as apycula computes it."""
crc = 0
for byte in data:
crc ^= byte
for _ in range(8):
crc = (crc >> 1) ^ 0xA001 if crc & 1 else crc >> 1
return crc
def to_bytes(line):
"""8 ASCII '0'/'1' characters per byte, most significant bit first"""
return bytearray(int(line[i:i + 8], 2) for i in range(0, len(line), 8))
def to_line(data):
return "".join("{:08b}".format(b) for b in data)
def split(blocks):
"""header, rows, footer: the rows are the repeated long lines in the middle"""
lengths = [len(b) for b in blocks]
row_len = max(set(lengths), key=lengths.count)
first = next(n for n, b in enumerate(blocks) if len(b) == row_len)
last = len(blocks) - 1 - next(n for n, b in enumerate(reversed(blocks)) if len(b) == row_len)
head, rows, foot = blocks[:first], blocks[first:last + 1], blocks[last + 1:]
if len(head) <= PREAMBLE or not rows or not foot:
sys.exit("unexpected .fs layout: {} header, {} row(s), {} footer entries".format(
len(head), len(rows), len(foot)))
for row in rows:
if row[-6:] != b"\xff" * 6:
sys.exit("unexpected .fs row: not terminated by {} CRC + 6 padding bytes".format(2))
return head, rows, foot
def main(path, idcode):
blocks = [to_bytes(l) for l in open(path).read().splitlines() if l]
head, rows, foot = split(blocks)
# the first entry write_bitstream() folds into the CRC, right after the preamble
chip = head[PREAMBLE]
old = int.from_bytes(chip[-4:], "big")
if old == idcode:
print("[=] DEVID {} already 0x{:08X}".format(path, idcode))
return
chip[-4:] = idcode.to_bytes(4, "big")
# what write_bitstream() has accumulated once it reaches the first row
seed = b"".join(bytes(entry) for entry in head[PREAMBLE:] if entry[0] != SKIP)
for n, row in enumerate(rows):
data = bytes(row[:-8])
want = crc16_arc(seed + data if n == 0 else b"\xff" * 6 + data)
if n == 0:
row[-8:-6] = want.to_bytes(2, "little")
elif row[-8:-6] != want.to_bytes(2, "little"):
sys.exit("row {} CRC mismatch: apycula changed the bitstream layout".format(n))
with open(path, "w") as f:
for block in head + rows + foot:
f.write(to_line(block) + "\n")
print("[=] DEVID {} 0x{:08X} -> 0x{:08X}".format(path, old, idcode))
if __name__ == "__main__":
if len(sys.argv) != 3:
sys.exit("usage: gowin_devid.py <file.fs> <idcode>")
main(sys.argv[1], int(sys.argv[2], 16))
+2 -2
View File
@@ -1,14 +1,14 @@
1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
1111111111111111
1010010111000011
0000011000000000000000000000000000000001000000000011100000011011
0000011000000000000000000000000000010001000000000011100000011011
0001000000000000000000000000000000000000101011100000000000000000
0101000100000000111111111111111111111111111111111111111111111111
00001011000000000000000000000000
1101001000000000111111111111111100000000000000000000000000000000
00010010000000000000000000000000
00111011100000000000001011101110
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011000000010000000000000000000001000011111000010100000011110000000000000000011000000000000000010000000000001100000000000011110000000111110001100101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001100010110000110111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011000000010000000000000000000001000011111000010100000011110000000000000000011000000000000000010000000000001100000000000011110000000111110001100101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000010010001011111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111010101010100111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100001010000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100001010000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000110000000000101000011110000000011000000011010010100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100001010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100001010000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000010000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111100011110111111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001010000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001010000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001010000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100100100011100001100111111111111111111111111111111111111111111111111111111111
+12 -1
View File
@@ -8,6 +8,7 @@
# OSS_CAD_SUITE oss-cad-suite root, holds yosys/nextpnr-himbaechel/gowin_pack
# PM5_FAMILY Gowin family, selects the chip database in nextpnr
# PM5_PART exact part, must match the board
# PM5_IDCODE IDCODE the bitstream declares, must match the silicon
# PM5_CST pin constraints, generated by the Gowin IDE
# PM5_SDC timing constraints, generated by the Gowin IDE
# PM5_FREQ fallback clock constraint, used when PM5_SDC is empty
@@ -26,6 +27,7 @@ OSS_CAD_SUITE ?= $(firstword $(wildcard \
PM5_FAMILY ?= GW1N-4
PM5_PART ?= GW1N-UV4QN48C6/I5
PM5_IDCODE ?= 0x1100381B
PM5_CST ?= $(PM5_DIR)/gowin.cst
PM5_SDC ?= $(PM5_DIR)/gowin.sdc
PM5_FREQ ?= 24
@@ -110,9 +112,18 @@ $(PM5_OUT)/$(PM5_NAME)_pnr.json: $(PM5_OUT)/$(PM5_NAME).json $(PM5_CST) $(if $(P
--json $(PM5_NAME).json \
--write $(PM5_NAME)_pnr.json
$(PM5_OUT)/$(PM5_NAME).fs: $(PM5_OUT)/$(PM5_NAME)_pnr.json
# TODO: workaround, drop once apycula can target GW1N-4D. The GW1N-4 database
# apycula ships stamps the plain GW1N-4 IDCODE (0x0100381B), which the silicon
# on a PM5 (GW1N-4B/4D, 0x1100381B) rejects: done_final stays 0 and nothing
# drives the antennas. gowin_devid.py rewrites that field to PM5_IDCODE here so
# the image is accepted at all. Remove this step, PM5_IDCODE and the workflow's
# IDCODE check once apycula gains a GW1N-4B/4D target or stops discarding the
# C6/I5 device version from the part number. Until then, suspect the fuse map
# first when a bitstream misbehaves: it comes from the non-D database entry.
$(PM5_OUT)/$(PM5_NAME).fs: $(PM5_OUT)/$(PM5_NAME)_pnr.json $(COMMON_DIR)/gowin_devid.py
$(info [=] GOWIN_PACK $@)
$(Q)cd $(PM5_OUT) && $(OC_ENV) gowin_pack -d $(PM5_FAMILY) -o $(PM5_NAME).fs $(PM5_NAME)_pnr.json
$(Q)$(OC_ENV) $(OC_PYTHON) $(COMMON_DIR)/gowin_devid.py $(PM5_OUT)/$(PM5_NAME).fs $(PM5_IDCODE)
$(PM5_OUT)/$(PM5_NAME).bin: $(PM5_OUT)/$(PM5_NAME).fs
$(info [=] FS2BIN $@)