summaryrefslogtreecommitdiff
path: root/hdl/testing/shift_reg.py
blob: de2ed3f71c361db3f93534c4a0cb19f983c3278b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from myhdl import *
from random import randrange
import os

from constants import *

class ShiftReg():
    def __init__(self):
        self.class_name = self.__class__.__name__

    # Main code, this is the actual logic
    @staticmethod
    @block
    def ShiftReg(reset: Signal, clk: Signal, in0: Signal, out0: Signal, left_right: Signal):

        width = len(out0)

        @always_seq(clk.posedge, reset=reset)
        def shifter():
            if not left_right:
                out0.next[width:1] = out0[width-1:0]
                out0.next[0] = in0
            else:
                out0.next[width-1:0] = out0[width:1]
                out0.next[width-1] = in0

        return shifter


    @block
    def tb(self, func):
        reset = ResetSignal(0, 0, True)
        clk = Signal(bool(0))
        in0 = Signal(0)
        out0 = Signal(modbv(0)[8:])
        left_right = Signal(bool(0))

        dut = getattr(self, str(func))(reset=reset, clk=clk, in0=in0, out0=out0, left_right=left_right)

        @always(delay(2))
        def clock_gen():
            clk.next = not clk

        @instance
        def monitor():
            while True:
                yield clk.posedge
                yield delay(1)
                print(bin(out0, 8))

        @instance
        def stimulus():
            yield clk.negedge
            reset.next = 1

            for i in range(9):
                yield clk.negedge
                in0.next = 1

            for i in range(9):
                yield clk.negedge
                in0.next = 0

            raise StopSimulation

        return dut, clock_gen, monitor, stimulus

    def convert(self):
        reset = ResetSignal(0, 0, True)
        clk = Signal(bool(0))
        in0 = Signal(bool(0))
        out0 = Signal(modbv(0)[8:])
        left_right = Signal(bool(0))

        inst = self.ShiftReg(reset=reset, clk=clk, in0=in0, out0=out0, left_right=left_right)
        inst.convert(hdl='Verilog', path=GEN_VERILOG, name=f"{self.class_name}")


    # This function links myhdl to icarus verilog sim
    def _cosim(self, **kargs): #these should have the same signals as logic(), 

        iverilog_cmd = IVERILOG + f"-o {SIM_DIR}{self.class_name}.o {GEN_VERILOG}{self.class_name}.v {GEN_VERILOG}tb_{self.class_name}.v"
        vvp_cmd = VVP + f"{SIM_DIR}{self.class_name}.o"

        os.system(iverilog_cmd)
        return Cosimulation(vvp_cmd, **kargs)

    def sim(self):
        tb = self.tb(self.class_name)
        tb.config_sim(trace=True, tracebackup=False, directory=SIM_DIR, filename=f"{self.class_name}_sim")
        tb.run_sim()

    def cosim(self):
        tb = self.tb('_cosim')
        tb.run_sim()


def test_shift_reg():
    hdl = ShiftReg()
    hdl.sim()
    hdl.convert()
    hdl.cosim()


test_shift_reg()