Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pepijndevos committed Sep 11, 2019
0 parents commit 6d28ad2
Show file tree
Hide file tree
Showing 15 changed files with 229 additions and 0 deletions.
38 changes: 38 additions & 0 deletions bslib.py
Original file line number Diff line number Diff line change
@@ -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)

14 changes: 14 additions & 0 deletions display.py
Original file line number Diff line number Diff line change
@@ -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")
17 changes: 17 additions & 0 deletions empty/device.cfg
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions empty/empty.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module top();
endmodule
7 changes: 7 additions & 0 deletions empty/pnr.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-sdf
-oc
-ibs
-posp
-o
-warning_all
-timing
6 changes: 6 additions & 0 deletions empty/run.tcl
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions indices.py
Original file line number Diff line number Diff line change
@@ -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()))
21 changes: 21 additions & 0 deletions json_display.py
Original file line number Diff line number Diff line change
@@ -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")
51 changes: 51 additions & 0 deletions lut4.sh
Original file line number Diff line number Diff line change
@@ -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 ..

17 changes: 17 additions & 0 deletions lut4/device.cfg
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions lut4/lut4.cst.mk
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 6 additions & 0 deletions lut4/lut4.sdc
Original file line number Diff line number Diff line change
@@ -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}]
15 changes: 15 additions & 0 deletions lut4/lut4.v
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions lut4/pnr.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-sdf
-oc
-ibs
-posp
-o
-warning_all
-timing
8 changes: 8 additions & 0 deletions lut4/run.tcl
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6d28ad2

Please sign in to comment.