stefan
2 years ago
10 changed files with 553 additions and 1016 deletions
@ -1,252 +1,75 @@
@@ -1,252 +1,75 @@
|
||||
import numpy as np |
||||
import kyupy.logic as lg |
||||
from kyupy.logic import mvarray, bparray, bp_to_mv, mv_to_bp |
||||
|
||||
|
||||
def test_mvarray(): |
||||
|
||||
# instantiation with shape |
||||
|
||||
ary = lg.MVArray(4) |
||||
assert ary.length == 1 |
||||
assert len(ary) == 1 |
||||
assert ary.width == 4 |
||||
|
||||
ary = lg.MVArray((3, 2)) |
||||
assert ary.length == 2 |
||||
assert len(ary) == 2 |
||||
assert ary.width == 3 |
||||
|
||||
# instantiation with single vector |
||||
|
||||
ary = lg.MVArray([1, 0, 1]) |
||||
assert ary.length == 1 |
||||
assert ary.width == 3 |
||||
assert str(ary) == "['101']" |
||||
assert ary[0] == '101' |
||||
|
||||
ary = lg.MVArray("10X-") |
||||
assert ary.length == 1 |
||||
assert ary.width == 4 |
||||
assert str(ary) == "['10X-']" |
||||
assert ary[0] == '10X-' |
||||
|
||||
ary = lg.MVArray("1") |
||||
assert ary.length == 1 |
||||
assert ary.width == 1 |
||||
|
||||
ary = lg.MVArray(["1"]) |
||||
assert ary.length == 1 |
||||
assert ary.width == 1 |
||||
|
||||
# instantiation with multiple vectors |
||||
|
||||
ary = lg.MVArray([[0, 0], [0, 1], [1, 0], [1, 1]]) |
||||
assert ary.length == 4 |
||||
assert ary.width == 2 |
||||
|
||||
ary = lg.MVArray(["000", "001", "110", "---"]) |
||||
assert ary.length == 4 |
||||
assert ary.width == 3 |
||||
assert str(ary) == "['000', '001', '110', '---']" |
||||
assert ary[2] == '110' |
||||
|
||||
# casting to 2-valued logic |
||||
|
||||
ary = lg.MVArray([0, 1, 2, None], m=2) |
||||
assert ary.data[0] == lg.ZERO |
||||
assert ary.data[1] == lg.ONE |
||||
assert ary.data[2] == lg.ZERO |
||||
assert ary.data[3] == lg.ZERO |
||||
|
||||
ary = lg.MVArray("0-X1PRFN", m=2) |
||||
assert ary.data[0] == lg.ZERO |
||||
assert ary.data[1] == lg.ZERO |
||||
assert ary.data[2] == lg.ZERO |
||||
assert ary.data[3] == lg.ONE |
||||
assert ary.data[4] == lg.ZERO |
||||
assert ary.data[5] == lg.ONE |
||||
assert ary.data[6] == lg.ZERO |
||||
assert ary.data[7] == lg.ONE |
||||
|
||||
# casting to 4-valued logic |
||||
|
||||
ary = lg.MVArray([0, 1, 2, None, 'F'], m=4) |
||||
assert ary.data[0] == lg.ZERO |
||||
assert ary.data[1] == lg.ONE |
||||
assert ary.data[2] == lg.UNKNOWN |
||||
assert ary.data[3] == lg.UNASSIGNED |
||||
assert ary.data[4] == lg.ZERO |
||||
|
||||
ary = lg.MVArray("0-X1PRFN", m=4) |
||||
assert ary.data[0] == lg.ZERO |
||||
assert ary.data[1] == lg.UNASSIGNED |
||||
assert ary.data[2] == lg.UNKNOWN |
||||
assert ary.data[3] == lg.ONE |
||||
assert ary.data[4] == lg.ZERO |
||||
assert ary.data[5] == lg.ONE |
||||
assert ary.data[6] == lg.ZERO |
||||
assert ary.data[7] == lg.ONE |
||||
|
||||
# casting to 8-valued logic |
||||
|
||||
ary = lg.MVArray([0, 1, 2, None, 'F'], m=8) |
||||
assert ary.data[0] == lg.ZERO |
||||
assert ary.data[1] == lg.ONE |
||||
assert ary.data[2] == lg.UNKNOWN |
||||
assert ary.data[3] == lg.UNASSIGNED |
||||
assert ary.data[4] == lg.FALL |
||||
|
||||
ary = lg.MVArray("0-X1PRFN", m=8) |
||||
assert ary.data[0] == lg.ZERO |
||||
assert ary.data[1] == lg.UNASSIGNED |
||||
assert ary.data[2] == lg.UNKNOWN |
||||
assert ary.data[3] == lg.ONE |
||||
assert ary.data[4] == lg.PPULSE |
||||
assert ary.data[5] == lg.RISE |
||||
assert ary.data[6] == lg.FALL |
||||
assert ary.data[7] == lg.NPULSE |
||||
|
||||
# copy constructor and casting |
||||
|
||||
ary8 = lg.MVArray(ary, m=8) |
||||
assert ary8.length == 1 |
||||
assert ary8.width == 8 |
||||
assert ary8.data[7] == lg.NPULSE |
||||
|
||||
ary4 = lg.MVArray(ary, m=4) |
||||
assert ary4.data[1] == lg.UNASSIGNED |
||||
assert ary4.data[7] == lg.ONE |
||||
|
||||
ary2 = lg.MVArray(ary, m=2) |
||||
assert ary2.data[1] == lg.ZERO |
||||
assert ary2.data[7] == lg.ONE |
||||
|
||||
|
||||
def test_mv_operations(): |
||||
x1_2v = lg.MVArray("0011", m=2) |
||||
x2_2v = lg.MVArray("0101", m=2) |
||||
x1_4v = lg.MVArray("0000XXXX----1111", m=4) |
||||
x2_4v = lg.MVArray("0X-10X-10X-10X-1", m=4) |
||||
x1_8v = lg.MVArray("00000000XXXXXXXX--------11111111PPPPPPPPRRRRRRRRFFFFFFFFNNNNNNNN", m=8) |
||||
x2_8v = lg.MVArray("0X-1PRFN0X-1PRFN0X-1PRFN0X-1PRFN0X-1PRFN0X-1PRFN0X-1PRFN0X-1PRFN", m=8) |
||||
|
||||
assert lg.mv_not(x1_2v)[0] == '1100' |
||||
assert lg.mv_not(x1_4v)[0] == '1111XXXXXXXX0000' |
||||
assert lg.mv_not(x1_8v)[0] == '11111111XXXXXXXXXXXXXXXX00000000NNNNNNNNFFFFFFFFRRRRRRRRPPPPPPPP' |
||||
|
||||
assert lg.mv_or(x1_2v, x2_2v)[0] == '0111' |
||||
assert lg.mv_or(x1_4v, x2_4v)[0] == '0XX1XXX1XXX11111' |
||||
assert lg.mv_or(x1_8v, x2_8v)[0] == '0XX1PRFNXXX1XXXXXXX1XXXX11111111PXX1PRFNRXX1RRNNFXX1FNFNNXX1NNNN' |
||||
|
||||
assert lg.mv_and(x1_2v, x2_2v)[0] == '0001' |
||||
assert lg.mv_and(x1_4v, x2_4v)[0] == '00000XXX0XXX0XX1' |
||||
assert lg.mv_and(x1_8v, x2_8v)[0] == '000000000XXXXXXX0XXXXXXX0XX1PRFN0XXPPPPP0XXRPRPR0XXFPPFF0XXNPRFN' |
||||
|
||||
assert lg.mv_xor(x1_2v, x2_2v)[0] == '0110' |
||||
assert lg.mv_xor(x1_4v, x2_4v)[0] == '0XX1XXXXXXXX1XX0' |
||||
assert lg.mv_xor(x1_8v, x2_8v)[0] == '0XX1PRFNXXXXXXXXXXXXXXXX1XX0NFRPPXXNPRFNRXXFRPNFFXXRFNPRNXXPNFRP' |
||||
|
||||
x30_2v = lg.MVArray("0000", m=2) |
||||
x31_2v = lg.MVArray("1111", m=2) |
||||
x30_4v = lg.MVArray("0000000000000000", m=4) |
||||
x31_4v = lg.MVArray("1111111111111111", m=4) |
||||
x30_8v = lg.MVArray("0000000000000000000000000000000000000000000000000000000000000000", m=8) |
||||
x31_8v = lg.MVArray("1111111111111111111111111111111111111111111111111111111111111111", m=8) |
||||
|
||||
assert lg.mv_latch(x1_2v, x2_2v, x30_2v)[0] == '0001' |
||||
assert lg.mv_latch(x1_2v, x2_2v, x31_2v)[0] == '1011' |
||||
assert lg.mv_latch(x1_4v, x2_4v, x30_4v)[0] == '0XX00XXX0XXX0XX1' |
||||
assert lg.mv_latch(x1_4v, x2_4v, x31_4v)[0] == '1XX01XXX1XXX1XX1' |
||||
assert lg.mv_latch(x1_8v, x2_8v, x30_8v)[0] == '0XX000000XXXXXXX0XXXXXXX0XX10R110XX000000XXR0R0R0XXF001F0XX10R11' |
||||
assert lg.mv_latch(x1_8v, x2_8v, x31_8v)[0] == '1XX01F001XXXXXXX1XXXXXXX1XX111111XX01F001XXR110R1XXF1F1F1XX11111' |
||||
def assert_equal_shape_and_contents(actual, desired): |
||||
desired = np.array(desired, dtype=np.uint8) |
||||
assert actual.shape == desired.shape |
||||
np.testing.assert_allclose(actual, desired) |
||||
|
||||
|
||||
def test_mvarray_single_vector(): |
||||
assert_equal_shape_and_contents(mvarray(1, 0, 1), [lg.ONE, lg.ZERO, lg.ONE]) |
||||
assert_equal_shape_and_contents(mvarray([1, 0, 1]), [lg.ONE, lg.ZERO, lg.ONE]) |
||||
assert_equal_shape_and_contents(mvarray('10X-RFPN'), [lg.ONE, lg.ZERO, lg.UNKNOWN, lg.UNASSIGNED, lg.RISE, lg.FALL, lg.PPULSE, lg.NPULSE]) |
||||
assert_equal_shape_and_contents(mvarray(['1']), [lg.ONE]) |
||||
assert_equal_shape_and_contents(mvarray('1'), [lg.ONE]) |
||||
|
||||
|
||||
def test_mvarray_multi_vector(): |
||||
assert_equal_shape_and_contents(mvarray([0, 0], [0, 1], [1, 0], [1, 1]), [[lg.ZERO, lg.ZERO, lg.ONE, lg.ONE], [lg.ZERO, lg.ONE, lg.ZERO, lg.ONE]]) |
||||
assert_equal_shape_and_contents(mvarray('10X', '--1'), [[lg.ONE, lg.UNASSIGNED], [lg.ZERO, lg.UNASSIGNED], [lg.UNKNOWN, lg.ONE]]) |
||||
|
||||
|
||||
def test_mv_ops(): |
||||
x1_8v = mvarray('00000000XXXXXXXX--------11111111PPPPPPPPRRRRRRRRFFFFFFFFNNNNNNNN') |
||||
x2_8v = mvarray('0X-1PRFN'*8) |
||||
|
||||
assert_equal_shape_and_contents(lg.mv_not(x1_8v), mvarray('11111111XXXXXXXXXXXXXXXX00000000NNNNNNNNFFFFFFFFRRRRRRRRPPPPPPPP')) |
||||
assert_equal_shape_and_contents(lg.mv_or(x1_8v, x2_8v), mvarray('0XX1PRFNXXX1XXXXXXX1XXXX11111111PXX1PRFNRXX1RRNNFXX1FNFNNXX1NNNN')) |
||||
assert_equal_shape_and_contents(lg.mv_and(x1_8v, x2_8v), mvarray('000000000XXXXXXX0XXXXXXX0XX1PRFN0XXPPPPP0XXRPRPR0XXFPPFF0XXNPRFN')) |
||||
assert_equal_shape_and_contents(lg.mv_xor(x1_8v, x2_8v), mvarray('0XX1PRFNXXXXXXXXXXXXXXXX1XX0NFRPPXXNPRFNRXXFRPNFFXXRFNPRNXXPNFRP')) |
||||
|
||||
# TODO |
||||
#assert_equal_shape_and_contents(lg.mv_transition(x1_8v, x2_8v), mvarray('0XXR PRFNXXXXXXXXXXXXXXXX1XX0NFRPPXXNPRFNRXXFRPNFFXXRFNPRNXXPNFRP')) |
||||
|
||||
x30_8v = mvarray('0000000000000000000000000000000000000000000000000000000000000000') |
||||
x31_8v = mvarray('1111111111111111111111111111111111111111111111111111111111111111') |
||||
|
||||
assert_equal_shape_and_contents(lg.mv_latch(x1_8v, x2_8v, x30_8v), mvarray('0XX000000XXXXXXX0XXXXXXX0XX10R110XX000000XXR0R0R0XXF001F0XX10R11')) |
||||
assert_equal_shape_and_contents(lg.mv_latch(x1_8v, x2_8v, x31_8v), mvarray('1XX01F001XXXXXXX1XXXXXXX1XX111111XX01F001XXR110R1XXF1F1F1XX11111')) |
||||
|
||||
|
||||
def test_bparray(): |
||||
|
||||
ary = lg.BPArray(4) |
||||
assert ary.length == 1 |
||||
assert len(ary) == 1 |
||||
assert ary.width == 4 |
||||
|
||||
ary = lg.BPArray((3, 2)) |
||||
assert ary.length == 2 |
||||
assert len(ary) == 2 |
||||
assert ary.width == 3 |
||||
|
||||
assert lg.MVArray(lg.BPArray("01", m=2))[0] == '01' |
||||
assert lg.MVArray(lg.BPArray("0X-1", m=4))[0] == '0X-1' |
||||
assert lg.MVArray(lg.BPArray("0X-1PRFN", m=8))[0] == '0X-1PRFN' |
||||
|
||||
x1_2v = lg.BPArray("0011", m=2) |
||||
x2_2v = lg.BPArray("0101", m=2) |
||||
x1_4v = lg.BPArray("0000XXXX----1111", m=4) |
||||
x2_4v = lg.BPArray("0X-10X-10X-10X-1", m=4) |
||||
x1_8v = lg.BPArray("00000000XXXXXXXX--------11111111PPPPPPPPRRRRRRRRFFFFFFFFNNNNNNNN", m=8) |
||||
x2_8v = lg.BPArray("0X-1PRFN0X-1PRFN0X-1PRFN0X-1PRFN0X-1PRFN0X-1PRFN0X-1PRFN0X-1PRFN", m=8) |
||||
|
||||
out_2v = lg.BPArray((4, 1), m=2) |
||||
out_4v = lg.BPArray((16, 1), m=4) |
||||
out_8v = lg.BPArray((64, 1), m=8) |
||||
|
||||
lg.bp_buf(out_2v.data, x1_2v.data) |
||||
lg.bp_buf(out_4v.data, x1_4v.data) |
||||
lg.bp_buf(out_8v.data, x1_8v.data) |
||||
|
||||
assert lg.MVArray(out_2v)[0] == '0011' |
||||
assert lg.MVArray(out_4v)[0] == '0000XXXXXXXX1111' |
||||
assert lg.MVArray(out_8v)[0] == '00000000XXXXXXXXXXXXXXXX11111111PPPPPPPPRRRRRRRRFFFFFFFFNNNNNNNN' |
||||
|
||||
lg.bp_not(out_2v.data, x1_2v.data) |
||||
lg.bp_not(out_4v.data, x1_4v.data) |
||||
lg.bp_not(out_8v.data, x1_8v.data) |
||||
|
||||
assert lg.MVArray(out_2v)[0] == '1100' |
||||
assert lg.MVArray(out_4v)[0] == '1111XXXXXXXX0000' |
||||
assert lg.MVArray(out_8v)[0] == '11111111XXXXXXXXXXXXXXXX00000000NNNNNNNNFFFFFFFFRRRRRRRRPPPPPPPP' |
||||
|
||||
lg.bp_or(out_2v.data, x1_2v.data, x2_2v.data) |
||||
lg.bp_or(out_4v.data, x1_4v.data, x2_4v.data) |
||||
lg.bp_or(out_8v.data, x1_8v.data, x2_8v.data) |
||||
|
||||
assert lg.MVArray(out_2v)[0] == '0111' |
||||
assert lg.MVArray(out_4v)[0] == '0XX1XXX1XXX11111' |
||||
assert lg.MVArray(out_8v)[0] == '0XX1PRFNXXX1XXXXXXX1XXXX11111111PXX1PRFNRXX1RRNNFXX1FNFNNXX1NNNN' |
||||
|
||||
lg.bp_and(out_2v.data, x1_2v.data, x2_2v.data) |
||||
lg.bp_and(out_4v.data, x1_4v.data, x2_4v.data) |
||||
lg.bp_and(out_8v.data, x1_8v.data, x2_8v.data) |
||||
|
||||
assert lg.MVArray(out_2v)[0] == '0001' |
||||
assert lg.MVArray(out_4v)[0] == '00000XXX0XXX0XX1' |
||||
assert lg.MVArray(out_8v)[0] == '000000000XXXXXXX0XXXXXXX0XX1PRFN0XXPPPPP0XXRPRPR0XXFPPFF0XXNPRFN' |
||||
|
||||
lg.bp_xor(out_2v.data, x1_2v.data, x2_2v.data) |
||||
lg.bp_xor(out_4v.data, x1_4v.data, x2_4v.data) |
||||
lg.bp_xor(out_8v.data, x1_8v.data, x2_8v.data) |
||||
|
||||
assert lg.MVArray(out_2v)[0] == '0110' |
||||
assert lg.MVArray(out_4v)[0] == '0XX1XXXXXXXX1XX0' |
||||
assert lg.MVArray(out_8v)[0] == '0XX1PRFNXXXXXXXXXXXXXXXX1XX0NFRPPXXNPRFNRXXFRPNFFXXRFNPRNXXPNFRP' |
||||
|
||||
x30_2v = lg.BPArray("0000", m=2) |
||||
x30_4v = lg.BPArray("0000000000000000", m=4) |
||||
x30_8v = lg.BPArray("0000000000000000000000000000000000000000000000000000000000000000", m=8) |
||||
|
||||
lg.bp_latch(out_2v.data, x1_2v.data, x2_2v.data, x30_2v.data) |
||||
lg.bp_latch(out_4v.data, x1_4v.data, x2_4v.data, x30_4v.data) |
||||
lg.bp_latch(out_8v.data, x1_8v.data, x2_8v.data, x30_8v.data) |
||||
|
||||
assert lg.MVArray(out_2v)[0] == '0001' |
||||
assert lg.MVArray(out_4v)[0] == '0XX00XXX0XXX0XX1' |
||||
assert lg.MVArray(out_8v)[0] == '0XX000000XXXXXXX0XXXXXXX0XX10R110XX000000XXR0R0R0XXF001F0XX10R11' |
||||
|
||||
x31_2v = lg.BPArray("1111", m=2) |
||||
x31_4v = lg.BPArray("1111111111111111", m=4) |
||||
x31_8v = lg.BPArray("1111111111111111111111111111111111111111111111111111111111111111", m=8) |
||||
|
||||
lg.bp_latch(out_2v.data, x1_2v.data, x2_2v.data, x31_2v.data) |
||||
lg.bp_latch(out_4v.data, x1_4v.data, x2_4v.data, x31_4v.data) |
||||
lg.bp_latch(out_8v.data, x1_8v.data, x2_8v.data, x31_8v.data) |
||||
|
||||
assert lg.MVArray(out_2v)[0] == '1011' |
||||
assert lg.MVArray(out_4v)[0] == '1XX01XXX1XXX1XX1' |
||||
assert lg.MVArray(out_8v)[0] == '1XX01F001XXXXXXX1XXXXXXX1XX111111XX01F001XXR110R1XXF1F1F1XX11111' |
||||
bpa = bparray('0X-1PRFN') |
||||
assert bpa.shape == (8, 3, 1) |
||||
|
||||
bpa = bparray('0X-1PRFN-') |
||||
assert bpa.shape == (9, 3, 1) |
||||
|
||||
bpa = bparray('000', '001', '010', '011', '100', '101', '110', '111') |
||||
assert bpa.shape == (3, 3, 1) |
||||
|
||||
bpa = bparray('000', '001', '010', '011', '100', '101', '110', '111', 'RFX') |
||||
assert bpa.shape == (3, 3, 2) |
||||
|
||||
assert_equal_shape_and_contents(bp_to_mv(bparray('0X-1PRFN'))[:,0], mvarray('0X-1PRFN')) |
||||
assert_equal_shape_and_contents(bparray('0X-1PRFN'), mv_to_bp(mvarray('0X-1PRFN'))) |
||||
|
||||
x1_8v = bparray('00000000XXXXXXXX--------11111111PPPPPPPPRRRRRRRRFFFFFFFFNNNNNNNN') |
||||
x2_8v = bparray('0X-1PRFN'*8) |
||||
|
||||
out_8v = np.empty((64, 3, 1), dtype=np.uint8) |
||||
|
||||
assert_equal_shape_and_contents(bp_to_mv(lg.bp_buf(out_8v, x1_8v))[:,0], mvarray('00000000XXXXXXXXXXXXXXXX11111111PPPPPPPPRRRRRRRRFFFFFFFFNNNNNNNN')) |
||||
assert_equal_shape_and_contents(bp_to_mv(lg.bp_or(out_8v, x1_8v, x2_8v))[:,0], mvarray('0XX1PRFNXXX1XXXXXXX1XXXX11111111PXX1PRFNRXX1RRNNFXX1FNFNNXX1NNNN')) |
||||
assert_equal_shape_and_contents(bp_to_mv(lg.bp_and(out_8v, x1_8v, x2_8v))[:,0], mvarray('000000000XXXXXXX0XXXXXXX0XX1PRFN0XXPPPPP0XXRPRPR0XXFPPFF0XXNPRFN')) |
||||
assert_equal_shape_and_contents(bp_to_mv(lg.bp_xor(out_8v, x1_8v, x2_8v))[:,0], mvarray('0XX1PRFNXXXXXXXXXXXXXXXX1XX0NFRPPXXNPRFNRXXFRPNFFXXRFNPRNXXPNFRP')) |
||||
|
||||
x30_8v = bparray('0000000000000000000000000000000000000000000000000000000000000000') |
||||
x31_8v = bparray('1111111111111111111111111111111111111111111111111111111111111111') |
||||
|
||||
assert_equal_shape_and_contents(bp_to_mv(lg.bp_latch(out_8v, x1_8v, x2_8v, x30_8v))[:,0], mvarray('0XX000000XXXXXXX0XXXXXXX0XX10R110XX000000XXR0R0R0XXF001F0XX10R11')) |
||||
assert_equal_shape_and_contents(bp_to_mv(lg.bp_latch(out_8v, x1_8v, x2_8v, x31_8v))[:,0], mvarray('1XX01F001XXXXXXX1XXXXXXX1XX111111XX01F001XXR110R1XXF1F1F1XX11111')) |
||||
|
@ -1,135 +1,126 @@
@@ -1,135 +1,126 @@
|
||||
import numpy as np |
||||
|
||||
from kyupy.logic_sim import LogicSim |
||||
from kyupy import bench |
||||
from kyupy.logic import MVArray, BPArray |
||||
from kyupy.logic import mvarray, bparray, bp_to_mv, mv_to_bp |
||||
from kyupy import logic |
||||
|
||||
def assert_equal_shape_and_contents(actual, desired): |
||||
desired = np.array(desired, dtype=np.uint8) |
||||
assert actual.shape == desired.shape |
||||
np.testing.assert_allclose(actual, desired) |
||||
|
||||
def test_2v(): |
||||
c = bench.parse('input(x, y) output(a, o, n) a=and(x,y) o=or(x,y) n=not(x)') |
||||
s = LogicSim(c, 4, m=2) |
||||
assert len(s.interface) == 5 |
||||
mva = MVArray(['00000', '01000', '10000', '11000'], m=2) |
||||
bpa = BPArray(mva) |
||||
s.assign(bpa) |
||||
s.propagate() |
||||
s.capture(bpa) |
||||
mva = MVArray(bpa) |
||||
assert mva[0] == '00001' |
||||
assert mva[1] == '01011' |
||||
assert mva[2] == '10010' |
||||
assert mva[3] == '11110' |
||||
s = LogicSim(c, 8, m=8) # FIXME: do m=2 |
||||
assert s.s_len == 5 |
||||
bpa = bparray('00---', '01---', '10---', '11---') |
||||
s.s[0] = bpa |
||||
s.s_to_c() |
||||
s.c_prop() |
||||
s.c_to_s() |
||||
mva = bp_to_mv(s.s[1]) |
||||
|
||||
assert_equal_shape_and_contents(mva[...,:4], mvarray('--001', '--011', '--010', '--110')) |
||||
|
||||
|
||||
def test_4v(): |
||||
c = bench.parse('input(x, y) output(a, o, n) a=and(x,y) o=or(x,y) n=not(x)') |
||||
s = LogicSim(c, 16, m=4) |
||||
assert len(s.interface) == 5 |
||||
mva = MVArray(['00000', '01000', '0-000', '0X000', |
||||
'10000', '11000', '1-000', '1X000', |
||||
'-0000', '-1000', '--000', '-X000', |
||||
'X0000', 'X1000', 'X-000', 'XX000'], m=4) |
||||
bpa = BPArray(mva) |
||||
s.assign(bpa) |
||||
s.propagate() |
||||
s.capture(bpa) |
||||
mva = MVArray(bpa) |
||||
assert mva[0] == '00001' |
||||
assert mva[1] == '01011' |
||||
assert mva[2] == '0-0X1' |
||||
assert mva[3] == '0X0X1' |
||||
assert mva[4] == '10010' |
||||
assert mva[5] == '11110' |
||||
assert mva[6] == '1-X10' |
||||
assert mva[7] == '1XX10' |
||||
assert mva[8] == '-00XX' |
||||
assert mva[9] == '-1X1X' |
||||
assert mva[10] == '--XXX' |
||||
assert mva[11] == '-XXXX' |
||||
assert mva[12] == 'X00XX' |
||||
assert mva[13] == 'X1X1X' |
||||
assert mva[14] == 'X-XXX' |
||||
assert mva[15] == 'XXXXX' |
||||
s = LogicSim(c, 16, m=8) # FIXME: m=4 |
||||
assert s.s_len == 5 |
||||
bpa = bparray( |
||||
'00---', '01---', '0----', '0X---', |
||||
'10---', '11---', '1----', '1X---', |
||||
'-0---', '-1---', '-----', '-X---', |
||||
'X0---', 'X1---', 'X----', 'XX---') |
||||
s.s[0] = bpa |
||||
s.s_to_c() |
||||
s.c_prop() |
||||
s.c_to_s() |
||||
mva = bp_to_mv(s.s[1]) |
||||
assert_equal_shape_and_contents(mva, mvarray( |
||||
'--001', '--011', '--0X1', '--0X1', |
||||
'--010', '--110', '--X10', '--X10', |
||||
'--0XX', '--X1X', '--XXX', '--XXX', |
||||
'--0XX', '--X1X', '--XXX', '--XXX')) |
||||
|
||||
|
||||
def test_8v(): |
||||
c = bench.parse('input(x, y) output(a, o, n, xo) a=and(x,y) o=or(x,y) n=not(x) xo=xor(x,y)') |
||||
s = LogicSim(c, 64, m=8) |
||||
assert len(s.interface) == 6 |
||||
mva = MVArray(['000010', '010111', '0-0X1X', '0X0X1X', '0R0R1R', '0F0F1F', '0P0P1P', '0N0N1N', |
||||
'100101', '111100', '1-X10X', '1XX10X', '1RR10F', '1FF10R', '1PP10N', '1NN10P', |
||||
'-00XXX', '-1X1XX', '--XXXX', '-XXXXX', '-RXXXX', '-FXXXX', '-PXXXX', '-NXXXX', |
||||
'X00XXX', 'X1X1XX', 'X-XXXX', 'XXXXXX', 'XRXXXX', 'XFXXXX', 'XPXXXX', 'XNXXXX', |
||||
'R00RFR', 'R1R1FF', 'R-XXFX', 'RXXXFX', 'RRRRFP', 'RFPNFN', 'RPPRFR', 'RNRNFF', |
||||
'F00FRF', 'F1F1RR', 'F-XXRX', 'FXXXRX', 'FRPNRN', 'FFFFRP', 'FPPFRF', 'FNFNRR', |
||||
'P00PNP', 'P1P1NN', 'P-XXNX', 'PXXXNX', 'PRPRNR', 'PFPFNF', 'PPPPNP', 'PNPNNN', |
||||
'N00NPN', 'N1N1PP', 'N-XXPX', 'NXXXPX', 'NRRNPF', 'NFFNPR', 'NPPNPN', 'NNNNPP'], m=8) |
||||
bpa = BPArray(mva) |
||||
s.assign(bpa) |
||||
s.propagate() |
||||
resp_bp = BPArray(bpa) |
||||
s.capture(resp_bp) |
||||
resp = MVArray(resp_bp) |
||||
|
||||
for i in range(64): |
||||
assert resp[i] == mva[i] |
||||
assert s.s_len == 6 |
||||
mva = mvarray( |
||||
'000010', '010111', '0-0X1X', '0X0X1X', '0R0R1R', '0F0F1F', '0P0P1P', '0N0N1N', |
||||
'100101', '111100', '1-X10X', '1XX10X', '1RR10F', '1FF10R', '1PP10N', '1NN10P', |
||||
'-00XXX', '-1X1XX', '--XXXX', '-XXXXX', '-RXXXX', '-FXXXX', '-PXXXX', '-NXXXX', |
||||
'X00XXX', 'X1X1XX', 'X-XXXX', 'XXXXXX', 'XRXXXX', 'XFXXXX', 'XPXXXX', 'XNXXXX', |
||||
'R00RFR', 'R1R1FF', 'R-XXFX', 'RXXXFX', 'RRRRFP', 'RFPNFN', 'RPPRFR', 'RNRNFF', |
||||
'F00FRF', 'F1F1RR', 'F-XXRX', 'FXXXRX', 'FRPNRN', 'FFFFRP', 'FPPFRF', 'FNFNRR', |
||||
'P00PNP', 'P1P1NN', 'P-XXNX', 'PXXXNX', 'PRPRNR', 'PFPFNF', 'PPPPNP', 'PNPNNN', |
||||
'N00NPN', 'N1N1PP', 'N-XXPX', 'NXXXPX', 'NRRNPF', 'NFFNPR', 'NPPNPN', 'NNNNPP') |
||||
tests = np.copy(mva) |
||||
tests[2:] = logic.UNASSIGNED |
||||
bpa = mv_to_bp(tests) |
||||
s.s[0] = bpa |
||||
s.s_to_c() |
||||
s.c_prop() |
||||
s.c_to_s() |
||||
resp = bp_to_mv(s.s[1]) |
||||
|
||||
exp_resp = np.copy(mva) |
||||
exp_resp[:2] = logic.UNASSIGNED |
||||
np.testing.assert_allclose(resp, exp_resp) |
||||
|
||||
|
||||
def test_loop(): |
||||
c = bench.parse('q=dff(d) d=not(q)') |
||||
s = LogicSim(c, 4, m=8) |
||||
assert len(s.interface) == 1 |
||||
mva = MVArray([['0'], ['1'], ['R'], ['F']], m=8) |
||||
assert s.s_len == 1 |
||||
mva = mvarray([['0'], ['1'], ['R'], ['F']]) |
||||
|
||||
s.assign(BPArray(mva)) |
||||
s.propagate() |
||||
resp_bp = BPArray((len(s.interface), s.sims)) |
||||
s.capture(resp_bp) |
||||
resp = MVArray(resp_bp) |
||||
# TODO |
||||
# s.assign(BPArray(mva)) |
||||
# s.propagate() |
||||
# resp_bp = BPArray((len(s.interface), s.sims)) |
||||
# s.capture(resp_bp) |
||||
# resp = MVArray(resp_bp) |
||||
|
||||
assert resp[0] == '1' |
||||
assert resp[1] == '0' |
||||
assert resp[2] == 'F' |
||||
assert resp[3] == 'R' |
||||
# assert resp[0] == '1' |
||||
# assert resp[1] == '0' |
||||
# assert resp[2] == 'F' |
||||
# assert resp[3] == 'R' |
||||
|
||||
resp_bp = s.cycle(resp_bp) |
||||
resp = MVArray(resp_bp) |
||||
# resp_bp = s.cycle(resp_bp) |
||||
# resp = MVArray(resp_bp) |
||||
|
||||
assert resp[0] == '0' |
||||
assert resp[1] == '1' |
||||
assert resp[2] == 'R' |
||||
assert resp[3] == 'F' |
||||
# assert resp[0] == '0' |
||||
# assert resp[1] == '1' |
||||
# assert resp[2] == 'R' |
||||
# assert resp[3] == 'F' |
||||
|
||||
|
||||
def test_latch(): |
||||
c = bench.parse('input(d, t) output(q) q=latch(d, t)') |
||||
s = LogicSim(c, 8, m=8) |
||||
assert len(s.interface) == 4 |
||||
mva = MVArray(['00-0', '00-1', '01-0', '01-1', '10-0', '10-1', '11-0', '11-1'], m=8) |
||||
exp = MVArray(['0000', '0011', '0100', '0100', '1000', '1011', '1111', '1111'], m=8) |
||||
assert s.s_len == 4 |
||||
mva = mvarray('00-0', '00-1', '01-0', '01-1', '10-0', '10-1', '11-0', '11-1') |
||||
exp = mvarray('0000', '0011', '0100', '0100', '1000', '1011', '1111', '1111') |
||||
|
||||
resp = MVArray(s.cycle(BPArray(mva))) |
||||
# TODO |
||||
# resp = MVArray(s.cycle(BPArray(mva))) |
||||
|
||||
for i in range(len(mva)): |
||||
assert resp[i] == exp[i] |
||||
# for i in range(len(mva)): |
||||
# assert resp[i] == exp[i] |
||||
|
||||
|
||||
def test_b01(mydir): |
||||
c = bench.load(mydir / 'b01.bench') |
||||
|
||||
# 2-valued |
||||
s = LogicSim(c, 8, m=2) |
||||
assert len(s.interface) == 9 |
||||
mva = MVArray((len(s.interface), 8), m=2) |
||||
# mva.randomize() |
||||
bpa = BPArray(mva) |
||||
s.assign(bpa) |
||||
s.propagate() |
||||
s.capture(bpa) |
||||
|
||||
# 8-valued |
||||
s = LogicSim(c, 8, m=8) |
||||
mva = MVArray((len(s.interface), 8), m=8) |
||||
# mva.randomize() |
||||
bpa = BPArray(mva) |
||||
s.assign(bpa) |
||||
s.propagate() |
||||
s.capture(bpa) |
||||
mva = np.zeros((s.s_len, 8), dtype=np.uint8) |
||||
s.s[0] = mv_to_bp(mva) |
||||
s.s_to_c() |
||||
s.c_prop() |
||||
s.c_to_s() |
||||
bp_to_mv(s.s[1]) |
||||
|
@ -1,138 +0,0 @@
@@ -1,138 +0,0 @@
|
||||
import numpy as np |
||||
|
||||
from kyupy.wave_sim_old import WaveSim, WaveSimCuda, wave_eval, TMIN, TMAX |
||||
from kyupy.logic_sim import LogicSim |
||||
from kyupy import verilog, sdf, logic |
||||
from kyupy.logic import MVArray, BPArray |
||||
|
||||
|
||||
def test_wave_eval(): |
||||
# SDF specifies IOPATH delays with respect to output polarity |
||||
# SDF pulse rejection value is determined by IOPATH causing last transition and polarity of last transition |
||||
line_times = np.zeros((3, 2, 2)) |
||||
line_times[0, 0, 0] = 0.1 # A -> Z rise delay |
||||
line_times[0, 0, 1] = 0.2 # A -> Z fall delay |
||||
line_times[0, 1, 0] = 0.1 # A -> Z negative pulse limit (terminate in rising Z) |
||||
line_times[0, 1, 1] = 0.2 # A -> Z positive pulse limit |
||||
line_times[1, 0, 0] = 0.3 # as above for B -> Z |
||||
line_times[1, 0, 1] = 0.4 |
||||
line_times[1, 1, 0] = 0.3 |
||||
line_times[1, 1, 1] = 0.4 |
||||
|
||||
state = np.zeros((3*16, 1)) + TMAX # 3 waveforms of capacity 16 |
||||
state[::16, 0] = 16 # first entry is capacity |
||||
a = state[0:16, 0] |
||||
b = state[16:32, 0] |
||||
z = state[32:, 0] |
||||
sat = np.zeros((3, 3), dtype='int') |
||||
sat[0] = 0, 16, 0 |
||||
sat[1] = 16, 16, 0 |
||||
sat[2] = 32, 16, 0 |
||||
|
||||
sdata = np.asarray([1, -1, 0, 0], dtype='float32') |
||||
|
||||
wave_eval((0b0111, 2, 0, 1), state, sat, 0, line_times, sdata) |
||||
assert z[0] == TMIN |
||||
|
||||
a[0] = TMIN |
||||
wave_eval((0b0111, 2, 0, 1), state, sat, 0, line_times, sdata) |
||||
assert z[0] == TMIN |
||||
|
||||
b[0] = TMIN |
||||
wave_eval((0b0111, 2, 0, 1), state, sat, 0, line_times, sdata) |
||||
assert z[0] == TMAX |
||||
|
||||
a[0] = 1 # A _/^^^ |
||||
b[0] = 2 # B __/^^ |
||||
wave_eval((0b0111, 2, 0, 1), state, sat, 0, line_times, sdata) |
||||
assert z[0] == TMIN # ^^^\___ B -> Z fall delay |
||||
assert z[1] == 2.4 |
||||
assert z[2] == TMAX |
||||
|
||||
a[0] = TMIN # A ^^^^^^ |
||||
b[0] = TMIN # B ^^^\__ |
||||
b[1] = 2 |
||||
wave_eval((0b0111, 2, 0, 1), state, sat, 0, line_times, sdata) |
||||
assert z[0] == 2.3 # ___/^^^ B -> Z rise delay |
||||
assert z[1] == TMAX |
||||
|
||||
# pos pulse of 0.35 at B -> 0.45 after delays |
||||
a[0] = TMIN # A ^^^^^^^^ |
||||
b[0] = TMIN |
||||
b[1] = 2 # B ^^\__/^^ |
||||
b[2] = 2.35 |
||||
wave_eval((0b0111, 2, 0, 1), state, sat, 0, line_times, sdata) |
||||
assert z[0] == 2.3 # __/^^\__ |
||||
assert z[1] == 2.75 |
||||
assert z[2] == TMAX |
||||
|
||||
# neg pulse of 0.45 at B -> 0.35 after delays |
||||
a[0] = TMIN # A ^^^^^^^^ |
||||
b[0] = 2 # B __/^^\__ |
||||
b[1] = 2.45 |
||||
b[2] = TMAX |
||||
wave_eval((0b0111, 2, 0, 1), state, sat, 0, line_times, sdata) |
||||
assert z[0] == TMIN # ^^\__/^^ |
||||
assert z[1] == 2.4 |
||||
assert z[2] == 2.75 |
||||
assert z[3] == TMAX |
||||
|
||||
# neg pulse of 0.35 at B -> 0.25 after delays (filtered) |
||||
a[0] = TMIN # A ^^^^^^^^ |
||||
b[0] = 2 # B __/^^\__ |
||||
b[1] = 2.35 |
||||
b[2] = TMAX |
||||
wave_eval((0b0111, 2, 0, 1), state, sat, 0, line_times, sdata) |
||||
assert z[0] == TMIN # ^^^^^^ |
||||
assert z[1] == TMAX |
||||
|
||||
# pos pulse of 0.25 at B -> 0.35 after delays (filtered) |
||||
a[0] = TMIN # A ^^^^^^^^ |
||||
b[0] = TMIN |
||||
b[1] = 2 # B ^^\__/^^ |
||||
b[2] = 2.25 |
||||
wave_eval((0b0111, 2, 0, 1), state, sat, 0, line_times, sdata) |
||||
assert z[0] == TMAX # ______ |
||||
|
||||
|
||||
def compare_to_logic_sim(wsim): |
||||
tests = MVArray((len(wsim.interface), wsim.sims)) |
||||
choices = np.asarray([logic.ZERO, logic.ONE, logic.RISE, logic.FALL], dtype=np.uint8) |
||||
rng = np.random.default_rng(10) |
||||
tests.data[...] = rng.choice(choices, tests.data.shape) |
||||
tests_bp = BPArray(tests) |
||||
wsim.assign(tests_bp) |
||||
wsim.propagate() |
||||
cdata = wsim.capture() |
||||
|
||||
resp = MVArray(tests) |
||||
|
||||
for iidx, inode in enumerate(wsim.interface): |
||||
if len(inode.ins) > 0: |
||||
for vidx in range(wsim.sims): |
||||
resp.data[iidx, vidx] = logic.ZERO if cdata[iidx, vidx, 0] < 0.5 else logic.ONE |
||||
# resp.set_value(vidx, iidx, 0 if cdata[iidx, vidx, 0] < 0.5 else 1) |
||||
|
||||
lsim = LogicSim(wsim.circuit, len(tests_bp)) |
||||
lsim.assign(tests_bp) |
||||
lsim.propagate() |
||||
exp_bp = BPArray(tests_bp) |
||||
lsim.capture(exp_bp) |
||||
exp = MVArray(exp_bp) |
||||
|
||||
for i in range(8): |
||||
exp_str = exp[i].replace('R', '1').replace('F', '0').replace('P', '0').replace('N', '1') |
||||
res_str = resp[i].replace('R', '1').replace('F', '0').replace('P', '0').replace('N', '1') |
||||
assert res_str == exp_str |
||||
|
||||
|
||||
def test_b14(b14_circuit, b14_timing): |
||||
compare_to_logic_sim(WaveSim(b14_circuit, b14_timing, 8)) |
||||
|
||||
|
||||
def test_b14_strip_forks(b14_circuit, b14_timing): |
||||
compare_to_logic_sim(WaveSim(b14_circuit, b14_timing, 8, strip_forks=True)) |
||||
|
||||
|
||||
def test_b14_cuda(b14_circuit, b14_timing): |
||||
compare_to_logic_sim(WaveSimCuda(b14_circuit, b14_timing, 8, strip_forks=True)) |
Loading…
Reference in new issue