From 6d28ad21535fa3f4634dbbcbe6c3e89285e61a6c Mon Sep 17 00:00:00 2001 From: Pepijn de Vos Date: Wed, 11 Sep 2019 11:35:08 +0200 Subject: [PATCH] initial commit --- bslib.py | 38 ++++++++++++++++++++++++++++++++++++ display.py | 14 +++++++++++++ empty/device.cfg | 17 ++++++++++++++++ empty/empty.v | 2 ++ empty/pnr.cfg | 7 +++++++ empty/run.tcl | 6 ++++++ indices.py | 12 ++++++++++++ json_display.py | 21 ++++++++++++++++++++ lut4.sh | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ lut4/device.cfg | 17 ++++++++++++++++ lut4/lut4.cst.mk | 8 ++++++++ lut4/lut4.sdc | 6 ++++++ lut4/lut4.v | 15 ++++++++++++++ lut4/pnr.cfg | 7 +++++++ lut4/run.tcl | 8 ++++++++ 15 files changed, 229 insertions(+) create mode 100644 bslib.py create mode 100644 display.py create mode 100644 empty/device.cfg create mode 100644 empty/empty.v create mode 100644 empty/pnr.cfg create mode 100644 empty/run.tcl create mode 100644 indices.py create mode 100644 json_display.py create mode 100644 lut4.sh create mode 100644 lut4/device.cfg create mode 100644 lut4/lut4.cst.mk create mode 100644 lut4/lut4.sdc create mode 100644 lut4/lut4.v create mode 100644 lut4/pnr.cfg create mode 100644 lut4/run.tcl diff --git a/bslib.py b/bslib.py new file mode 100644 index 00000000..9981f309 --- /dev/null +++ b/bslib.py @@ -0,0 +1,38 @@ +import numpy as np +from crcmod.predefined import mkPredefinedCrcFun + +crc16arc = mkPredefinedCrcFun('crc-16') + +def chunks(l, n): + """Yield successive n-sized chunks from l.""" + for i in range(0, len(l), n): + yield l[i:i + n] + +def bytearr(frame): + "array of all bytes of the frame" + return bytearray([int(n, base=2) for n in chunks(frame.strip(), 8)]) + +def crc(frame): + bs = bytearr(frame) + data = bs[-6:] + bs[:-8] + crc = (bs[-7] << 8) + bs[-8] + #print(data) + return crc, crc16arc(data) + +def bitarr(frame): + "Array of *content* bits" + data = frame.strip()[4:-64] + return [int(n, base=2) for n in data] + + +def read_bitstream(fname): + bitmap = [] + with open(fname) as inp: + for line in inp: + if line.startswith("//") or len(line) < 1000: continue + crc1, crc2 = crc(line) + #if crc1 != crc2: print(crc1, crc2) + bitmap.append(bytearr(line)[:-8]) + + return np.array(bitmap) + diff --git a/display.py b/display.py new file mode 100644 index 00000000..6bf512d7 --- /dev/null +++ b/display.py @@ -0,0 +1,14 @@ +import sys +from bslib import read_bitstream +from PIL import Image + +arr = read_bitstream(sys.argv[1]) +if len(sys.argv) > 2: + diff = read_bitstream(sys.argv[2]) + arr ^= diff + +size = (arr.shape[1]*8, arr.shape[0]) +print(size) +im = Image.frombytes(mode='1', size=size, data=arr) +#im.show() +im.save("bitmap.png","PNG") diff --git a/empty/device.cfg b/empty/device.cfg new file mode 100644 index 00000000..e2368f92 --- /dev/null +++ b/empty/device.cfg @@ -0,0 +1,17 @@ +set JTAG regular_io = false +set SSPI regular_io = false +set MSPI regular_io = false +set READY regular_io = false +set DONE regular_io = false +set RECONFIG_N regular_io = false +set MODE regular_io = false +set CRC_check = true +set compress = false +set encryption = false +set security_bit_enable = true +set bsram_init_fuse_print = true +set download_speed = 250/100 +set spi_flash_address = 0x00FFF000 +set format = txt +set background_programming = false +set secure_mode = false diff --git a/empty/empty.v b/empty/empty.v new file mode 100644 index 00000000..dc95c3d2 --- /dev/null +++ b/empty/empty.v @@ -0,0 +1,2 @@ +module top(); +endmodule diff --git a/empty/pnr.cfg b/empty/pnr.cfg new file mode 100644 index 00000000..509726dc --- /dev/null +++ b/empty/pnr.cfg @@ -0,0 +1,7 @@ +-sdf +-oc +-ibs +-posp +-o +-warning_all +-timing diff --git a/empty/run.tcl b/empty/run.tcl new file mode 100644 index 00000000..c1554264 --- /dev/null +++ b/empty/run.tcl @@ -0,0 +1,6 @@ +# gw_sh run.tcl +add_file -vm empty.v +add_file -cfg device.cfg +set_option -device GW1NR-9-QFN88-6 +set_option -pn GW1NR-LV9QN88C6/I5 +run_pnr -opt pnr.cfg diff --git a/indices.py b/indices.py new file mode 100644 index 00000000..4e94eef7 --- /dev/null +++ b/indices.py @@ -0,0 +1,12 @@ +import json +import sys +import numpy as np +from bslib import read_bitstream + +arr = read_bitstream(sys.argv[1]) +diff = read_bitstream(sys.argv[2]) +arr ^= diff +arr = np.unpackbits(arr, axis=1) + +indices = np.transpose(np.nonzero(arr)).astype(int) +print(json.dumps(indices.tolist())) diff --git a/json_display.py b/json_display.py new file mode 100644 index 00000000..07e8585c --- /dev/null +++ b/json_display.py @@ -0,0 +1,21 @@ +import sys +import json +import numpy as np +from bslib import read_bitstream +from PIL import Image + +image = np.zeros([712, 2840], dtype="byte") +for fname in sys.argv[1:]: + print(fname) + with open(fname) as f: + try: + data = json.load(f) + except json.decoder.JSONDecodeError: + continue + for x, y in data: + image[x][y] += 1 + +print(np.nonzero(image > 1)) +im = Image.frombytes(mode='1', size=image.shape[::-1], data=np.packbits(image)) +#im.show() +im.save("bitmap.png","PNG") diff --git a/lut4.sh b/lut4.sh new file mode 100644 index 00000000..d4cd49b6 --- /dev/null +++ b/lut4.sh @@ -0,0 +1,51 @@ +function loop { + cp -r lut4 lut4_$1 + cd lut4_$1 + for row in $1 $2 + do + for col in {2..46} + do + for cls in {0..3} + do + for lut in A B + do + location="R${row}C${col}[${cls}][${lut}]" + if [[ ! -f "../data/fs/$location.fs" ]]; then + sed s/LOCATION/$location/ lut4.cst.mk > lut4.cst + ~/bin/gowin/IDE/bin/gw_sh run.tcl + mv impl/pnr/lut4.fs ../data/fs/$location.fs + fi + if [[ ! -f "../data/bits/$location.json" ]]; then + python ../indices.py ../empty.fs ../data/fs/$location.fs > ../data/bits/$location.json + fi + done + done + done + done +} + +set -x +cd empty +~/bin/gowin/IDE/bin/gw_sh run.tcl +cd .. +cp empty/impl/pnr/empty.fs . + +mkdir -p data/fs +mkdir -p data/bits +#R2C2 +#R27C46 +loop 2 3 & +loop 4 5 & +loop 6 7 & +loop 8 9 & +loop 10 11 & +loop 12 13 & +loop 14 15 & +loop 16 17 & +loop 18 19 & +loop 20 21 & +loop 22 23 & +loop 24 25 & +loop 26 27 & +cd .. + diff --git a/lut4/device.cfg b/lut4/device.cfg new file mode 100644 index 00000000..e2368f92 --- /dev/null +++ b/lut4/device.cfg @@ -0,0 +1,17 @@ +set JTAG regular_io = false +set SSPI regular_io = false +set MSPI regular_io = false +set READY regular_io = false +set DONE regular_io = false +set RECONFIG_N regular_io = false +set MODE regular_io = false +set CRC_check = true +set compress = false +set encryption = false +set security_bit_enable = true +set bsram_init_fuse_print = true +set download_speed = 250/100 +set spi_flash_address = 0x00FFF000 +set format = txt +set background_programming = false +set secure_mode = false diff --git a/lut4/lut4.cst.mk b/lut4/lut4.cst.mk new file mode 100644 index 00000000..11495fd6 --- /dev/null +++ b/lut4/lut4.cst.mk @@ -0,0 +1,8 @@ +//Copyright (C)2014-2019 Gowin Semiconductor Corporation. +//All rights reserved. +//File Title: Physical Constraints file +//GOWIN Version: V1.9.1.01Beta +//Part Number: GW1NR-LV9QN88C6/I5 +//Created Time: Mon Sep 9 11:16:22 2019 + +INS_LOC "mylut" LOCATION; diff --git a/lut4/lut4.sdc b/lut4/lut4.sdc new file mode 100644 index 00000000..3e022a69 --- /dev/null +++ b/lut4/lut4.sdc @@ -0,0 +1,6 @@ +//Copyright (C)2014-2019 GOWIN Semiconductor Corporation. +//All rights reserved. +//File Title: Timing Constraints file +//GOWIN Version: 1.9.1.01 Beta +//Created Time: 2019-08-06 14:29:38 +create_clock -name clock -period 10 -waveform {0 5} [get_ports {clk}] diff --git a/lut4/lut4.v b/lut4/lut4.v new file mode 100644 index 00000000..de55d8bb --- /dev/null +++ b/lut4/lut4.v @@ -0,0 +1,15 @@ +module top(); + wire gen_000_; + wire gen_001_; + wire gen_002_; + wire gen_003_; + wire gen_004_; + LUT4 mylut ( + .F(gen_000_), + .I0(gen_001_), + .I1(gen_002_), + .I2(gen_003_), + .I3(gen_004_ ) + ); + defparam mylut.INIT = 16'h0000; +endmodule diff --git a/lut4/pnr.cfg b/lut4/pnr.cfg new file mode 100644 index 00000000..509726dc --- /dev/null +++ b/lut4/pnr.cfg @@ -0,0 +1,7 @@ +-sdf +-oc +-ibs +-posp +-o +-warning_all +-timing diff --git a/lut4/run.tcl b/lut4/run.tcl new file mode 100644 index 00000000..af1f4d11 --- /dev/null +++ b/lut4/run.tcl @@ -0,0 +1,8 @@ +# gw_sh run.tcl +add_file -cst lut4.cst +add_file -sdc lut4.sdc +add_file -vm lut4.v +add_file -cfg device.cfg +set_option -device GW1NR-9-QFN88-6 +set_option -pn GW1NR-LV9QN88C6/I5 +run_pnr -opt pnr.cfg