You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
126 lines
3.8 KiB
126 lines
3.8 KiB
import numpy as np |
|
|
|
from kyupy.logic_sim import LogicSim |
|
from kyupy import bench |
|
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, 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=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 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 s.s_len == 1 |
|
mva = mvarray([['0'], ['1'], ['R'], ['F']]) |
|
|
|
# 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' |
|
|
|
# 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' |
|
|
|
|
|
def test_latch(): |
|
c = bench.parse('input(d, t) output(q) q=latch(d, t)') |
|
s = LogicSim(c, 8, 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') |
|
|
|
# TODO |
|
# resp = MVArray(s.cycle(BPArray(mva))) |
|
|
|
# for i in range(len(mva)): |
|
# assert resp[i] == exp[i] |
|
|
|
|
|
def test_b01(mydir): |
|
c = bench.load(mydir / 'b01.bench') |
|
|
|
# 8-valued |
|
s = LogicSim(c, 8, m=8) |
|
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])
|
|
|