Example #1
0
 def _REGISTER() -> Array[bool, 1, 4]:
     load_ = False
     q: Array[bool, 1, 4] = utils.bastr2ba('0000')
     while True:
         ck, reset_ = yield
         load_, input_arr = yield q  # return q when clock passed
         if load_ is False:
             q = input_arr
         if reset_ is False:
             q = utils.bastr2ba('0000')
Example #2
0
    def test_build_REGISTER_for_REGISTER(self):
        args = (False, False)
        REGISTER = units.build_REGISTER(*args)

        # load
        REGISTER.send((True, True))
        REGISTER.send((False, utils.bastr2ba('1010')[::-1]))
        actual = REGISTER.send((True, True))
        expected = utils.bastr2ba('1010')[::-1]
        assert_array_equal(expected, actual)

        # hold
        REGISTER.send((True, utils.bastr2ba('0101')[::-1]))
        actual = REGISTER.send((True, True))
        expected = utils.bastr2ba('1010')[::-1]  # must be previous state
        assert_array_equal(expected, actual)

        # hold
        REGISTER.send((True, utils.bastr2ba('0000')[::-1]))
        actual = REGISTER.send((True, True))
        expected = utils.bastr2ba('1010')[::-1]  # must be previous state
        assert_array_equal(expected, actual)

        # reset
        REGISTER.send((True, utils.bastr2ba('1010')[::-1]))
        REGISTER.send((True, False))
        REGISTER.send((True, utils.bastr2ba('1010')[::-1]))
        actual = REGISTER.send((True, True))
        expected = utils.bastr2ba('0000')[::-1]
        assert_array_equal(expected, actual)
Example #3
0
    def test_build_REGISTER_for_COUNTER(self):
        args = (True, True)
        COUNTER = units.build_REGISTER(*args)

        # count
        COUNTER.send((True, True))
        COUNTER.send((True, utils.bastr2ba('1010')[::-1]))
        actual = COUNTER.send((True, True))
        expected = utils.bastr2ba('0001')[::-1]
        assert_array_equal(expected, actual)

        # load
        COUNTER.send((False, utils.bastr2ba('0101')[::-1]))
        actual = COUNTER.send((True, True))
        expected = utils.bastr2ba('0101')[::-1]
        assert_array_equal(expected, actual)

        # count
        COUNTER.send((True, utils.bastr2ba('1010')[::-1]))
        actual = COUNTER.send((True, True))
        expected = utils.bastr2ba('0110')[::-1]
        assert_array_equal(expected, actual)

        # reset
        COUNTER.send((True, utils.bastr2ba('1010')[::-1]))
        COUNTER.send((True, False))
        COUNTER.send((True, utils.bastr2ba('1010')[::-1]))
        actual = COUNTER.send((True, True))
        expected = utils.bastr2ba('0000')[::-1]
        assert_array_equal(expected, actual)
Example #4
0
 def _COUNTER() -> Array[bool, 1, 4]:
     load_ = False
     q: Array[bool, 1, 4] = utils.bastr2ba('0000')
     while True:
         ck, reset_ = yield
         load_, input_arr = yield q  # return q when clock passed
         if load_ is False:
             q = input_arr
         else:
             res = ALU(False, q, utils.bastr2ba('1000'))  # count up
             q = res[1:]  # res[0] is carry
         if reset_ is False:
             q = utils.bastr2ba('0000')
Example #5
0
def run_TD4(cc: ClockCycle):
    CLOCK_GENERATOR = units.build_CLOCK_GENERATOR(cc)
    ROM = units.build_ROM(assembler.assemble(CONFIG['program_file']))
    REGISTER_A = units.build_REGISTER(False, False)
    REGISTER_B = units.build_REGISTER(False, False)
    REGISTER_C = units.build_REGISTER(False, False)
    PC = units.build_REGISTER(True, True)
    D_FF_C = units.build_D_FF()

    q_d = utils.bastr2ba('0000')  # MUX input; cd is fixed with 0000
    q_c_in = utils.bastr2ba('0000')  # input of REGISTER_C is fixed with 0000
    step = 0
    max_step = CONFIG['max_step']
    for ck, reset in CLOCK_GENERATOR:
        if step > max_step or ck is False:
            break
        q_PC, q_a, q_b, q_c_out, c_flag = PC.send((ck, reset)), REGISTER_A.send((ck, reset)),\
            REGISTER_B.send((ck, reset)), REGISTER_C.send((ck, reset)), D_FF_C.send((ck, reset))
        op_arr = ROM(q_PC)
        decoded_arr = units.DECODER(op_arr[4:], units.NOT(c_flag))
        select_a, select_b, load0_, load1_, load2_, load3_ = \
            (bool(b) for b in decoded_arr)
        selected_arr = units.MUX(select_a, select_b, q_a, q_b, q_c_in, q_d)
        res_arr = units.ALU(c_flag, selected_arr, op_arr[:4])
        c, sum_arr = bool(res_arr[0]), res_arr[1:]
        D_FF_C.send(c)
        REGISTER_A.send((load0_, sum_arr))
        REGISTER_B.send((load1_, sum_arr))
        REGISTER_C.send((load2_, sum_arr))
        PC.send((load3_, sum_arr))
        units.DISPLAY(cc,
                      step=step,
                      PC=utils.ba2str(q_PC[::-1]),
                      output=utils.ba2str(q_c_out[::-1]),
                      REGISTER_A=utils.ba2str(q_a[::-1]),
                      REGISTER_B=utils.ba2str(q_b[::-1]),
                      c_flag=int(c_flag),
                      fetched_op=utils.ba2str(op_arr[::-1]),
                      decode_res=utils.ba2str(decoded_arr),
                      MUX_res=utils.ba2str(selected_arr[::-1]),
                      carry=int(c),
                      ALU_res=utils.ba2str(sum_arr))
        step += 1
Example #6
0
    def test_build_ROM(self):
        _arg = np.array(tuple(utils.int2bat(i, 8) for i in range(16)))
        ROM = units.build_ROM(_arg)

        # line 0
        arg = utils.bastr2ba('0000')[::-1]
        actual = ROM(arg)
        expected = _arg[0]
        assert_array_equal(expected, actual)

        # line 5
        arg = utils.bastr2ba('0101')[::-1]
        actual = ROM(arg)
        expected = _arg[5]
        assert_array_equal(expected, actual)

        # line 11
        arg = utils.bastr2ba('1011')[::-1]
        actual = ROM(arg)
        expected = _arg[11]
        assert_array_equal(expected, actual)
Example #7
0
 def test_bastr2ba(self):
     arg = '01010'
     actual = utils.bastr2ba(arg)
     expected = np.array((False, True, False, True, False))
     assert_array_equal(expected, actual)
Example #8
0
 def test_DECODER(self):
     args = (
         (utils.bastr2ba('0011')[::-1], True),  # MOV A Im
         (utils.bastr2ba('0111')[::-1], True),  # MOV B Im
         (utils.bastr2ba('0001')[::-1], True),  # MOV A B
         (utils.bastr2ba('0100')[::-1], True),  # MOV B A
         (utils.bastr2ba('0000')[::-1], True),  # ADD A Im
         (utils.bastr2ba('0101')[::-1], True),  # ADD B Im
         (utils.bastr2ba('0010')[::-1], True),  # IN A
         (utils.bastr2ba('0110')[::-1], True),  # IN B
         (utils.bastr2ba('1011')[::-1], True),  # OUT Im
         (utils.bastr2ba('1001')[::-1], True),  # OUT B
         (utils.bastr2ba('1111')[::-1], True),  # JMP Im
         (utils.bastr2ba('1110')[::-1],
          True),  # JNC Im with c = 0; c_flog_ = 1
         (utils.bastr2ba('1110')[::-1],
          False),  # JNC Im with c = 1; c_flog_ = 0
     )
     actuals = (units.DECODER(*arg) for arg in args)
     expecteds = (
         utils.bastr2ba('110111'),  # MOV A Im
         utils.bastr2ba('111011'),  # MOV B Im
         utils.bastr2ba('100111'),  # MOV A B
         utils.bastr2ba('001011'),  # MOV B A
         utils.bastr2ba('000111'),  # ADD A Im
         utils.bastr2ba('101011'),  # ADD B Im
         utils.bastr2ba('010111'),  # IN A
         utils.bastr2ba('011011'),  # IN B
         utils.bastr2ba('111101'),  # OUT Im
         utils.bastr2ba('101101'),  # OUT B
         utils.bastr2ba('111110'),  # JMP Im
         utils.bastr2ba('111110'),  # JNC Im with c = 0
         utils.bastr2ba('111111'),  # JNC Im with c = 1 (xx1111 is OK)
     )
     for e, a in zip(expecteds, actuals):
         assert_array_equal(e, a)
Example #9
0
    def test_MUX(self):
        args = (
            # c0
            (False, False, utils.bastr2ba('1010'), utils.bastr2ba('1011'),
             utils.bastr2ba('1100'), utils.bastr2ba('1101')),

            # c1
            (True, False, utils.bastr2ba('1010'), utils.bastr2ba('1011'),
             utils.bastr2ba('1100'), utils.bastr2ba('1101')),

            # c2
            (False, True, utils.bastr2ba('1010'), utils.bastr2ba('1011'),
             utils.bastr2ba('1100'), utils.bastr2ba('1101')),

            # c3
            (True, True, utils.bastr2ba('1010'), utils.bastr2ba('1011'),
             utils.bastr2ba('1100'), utils.bastr2ba('1101')),
        )
        actuals = (units.MUX(*arg) for arg in args)
        expecteds = (utils.bastr2ba('1010'), utils.bastr2ba('1011'),
                     utils.bastr2ba('1100'), utils.bastr2ba('1101'))
        for e, a in zip(expecteds, actuals):
            assert_array_equal(e, a)