def test_types(circuit): a, b = Line("a") >> Line("b") >> circuit_module.with_type(("a",), "b") assert a.is_type("b") assert a.is_type(("a",)) assert not a.is_type("a") assert not a.is_type(("b",)) assert b.is_type("b") assert b.is_type(("a",)) assert not b.is_type("a") assert not b.is_type(("b",)) assert a.is_types("b", ("a",)) assert a.is_types("b") assert not a.is_types("b", "c") assert not a.is_types("c") assert len(Lines(a, b).typed("a")) == 0 assert set(Lines(a, b).typed("b")) == {a, b} assert set(Lines(a, b).typed("b", ("a",))) == {a, b} assert set(Lines(a, b).typed("b", ("c",))) == set() assert set(Lines(a, b, Line("a")).typed("b")) == {a, b} assert set(Lines(a, b).typed("b")) == {a, b} a.add_type("c") assert set(Lines(a, b).typed("b")) == {a, b} assert set(Lines(a, b).typed("c")) == {a} assert set(Lines(a, b).typed("c")) == {a} d, e = Line("d") >> Line("e") dt, et = d >> e >> circuit_module.with_type("d") assert d is dt assert e is et
def test_lines_lookup(circuit): lines = Lines("abc") a, _, c = lines assert a.name == "a" assert lines["a"] == a assert lines["c"] == c assert lines["d"] is None
def test_comparator_gate(circuit): inputs = Lines("abel") output = inputs >> comp_gate truth_table_test( inputs, output, ( ((0, 0, 0, 0), (0, 0, 0)), ((0, 0, 1, 0), (1, 0, 0)), ((0, 0, 0, 1), (0, 1, 0)), ((0, 0, 1, 1), (1, 1, 0)), ((1, 0, 0, 0), (0, 0, 1)), ((1, 0, 1, 0), (0, 1, 1)), ((1, 0, 0, 1), (0, 1, 1)), ((1, 0, 1, 1), (0, 1, 1)), ((0, 1, 0, 0), (0, 0, 1)), ((0, 1, 1, 0), (0, 0, 1)), ((0, 1, 0, 1), (0, 1, 1)), ((0, 1, 1, 1), (0, 1, 1)), ((1, 1, 0, 0), (0, 0, 0)), ((1, 1, 1, 0), (1, 0, 0)), ((1, 1, 0, 1), (0, 1, 0)), ((1, 1, 1, 1), (1, 1, 0)), ), draw=1, )
def test_io_unit(circuit): s, e = Lines("se") bus_ = bus() inputs = s >> e >> bus_ ins, outs = (inputs >> circuit_module.IoUnit()).split() outs >>= bus_ inputs >>= ins truth_table_test( inputs, outs, ( ((0, 0, "3"), ("0", "3")), ((0, 0, "3", "42"), ("0", "3")), ((0, 0, "3"), ("0", "3")), ((1, 0, "3"), ("3", "3")), ((0, 0, "3"), ("3", "3")), ((1, 0, "3"), ("3", "3")), ((0, 0, "3"), ("3", "3")), ((0, 0, "52"), ("3", "52")), ((0, 0, "52"), ("3", "52")), ((1, 0, "52"), []), ((1, 0, "52"), ("52", "52")), ((0, 0, "52"), ("52", "52")), ((0, 1, "52"), ("52", "52")), ((0, 0, "52"), ("52", "52")), ((1, 0, "42"), []), ((1, 0, "42"), ("42", "42")), ), draw=1, )
def run_controller(circuit, instruction: str, flags=""): parsed, _ = parser.parse_line(instruction) print(instruction, parsed) clock = circuit_module.Clock() with scope("IR"): ir = bus() with scope("FLAGS"): flags_in = Lines("CAEZ") with scope("STEPPER"): stepper = bus(6) inputs = clock.clk >> clock.clk_s >> clock.clk_e >> ir >> flags_in >> stepper output = inputs >> circuit_module.controller simulation = {} es = tag_outputs(circuit, ["CONTROL", "ENABLER"]) ss = tag_outputs(circuit, ["CONTROL", "SELECTOR"]) rounds = [] for step in range(6): e_found = set() s_found = set() for clock_round in range(4): f = clock.step() f.update({stepper[j]: int(j == step) for j in range(6)}) f.update({ir[j]: k for j, k in enumerate(parsed)}) f.update( {flags_in[j]: int(letter in flags) for j, letter in enumerate("CAEZ")} ) simulation.update(simulate(f, circuit, simulation)) new_es = set(e for e in es if simulation[e]) new_ss = set(s for s in ss if simulation[s]) if clock_round == 3: assert ( len( [ line for line in new_es if line not in Lines(new_es).typed(("E", "B1")) and line not in Lines(new_es).typed(("ALU", "OP")) ] ) == 0 ) if clock_round != 1: assert len(new_ss) == 0 e_found.update(new_es) s_found.update(new_ss) rounds.append({"e": Lines(e_found), "s": Lines(s_found)}) return rounds
def test_register(circuit): inputs = Lines("se") >> bus() outputs = inputs >> register truth_table_test( inputs, outputs, ( ((1, 1, "1"), "1"), ((1, 0, "1"), "0"), ((0, 0, "1"), "0"), ((0, 1, "1"), "1"), ((0, 1, "25"), "1"), ((0, 0, "25"), "0"), ((0, 1, "25"), "1"), ((1, 0, "25"), "0"), ((0, 1, "4"), "25"), ((1, 0, "4"), "0"), ((0, 0, "4"), "0"), ((0, 1, "12"), "4"), ((1, 1, "12"), "12"), ), )
def is_types(lines: Lines, types: List[List[Any]]): assert len(lines) == len(types), (lines, types) for line_type in types: assert len(lines.typed(*line_type)) == 1, (lines, line_type)
def tag_outputs(circuit, tags, name_filter=""): return Lines( l for l in sorted(set(circuit.lines.typed(*tags)), key=lambda x: x.name) if name_filter in l.name )
def test_string_lines(circuit): assert "".join(line.name for line in Lines("abc")) == "abc"
def test_line_eq(): line_a1, line_a2 = Lines("aa") assert line_a1 == line_a1 assert line_a2 == line_a2 assert line_a1 != line_a2 assert line_a2 != line_a1
def test_lines(): a, b = Line("a"), Line("b") lines = Lines(a, b) assert list(lines) == [a, b] lines = Lines(b, a) assert list(lines) == [b, a]