summaryrefslogtreecommitdiff
path: root/hdl/testing/shift_reg.py
blob: a98fb4c6755a8d48085565f396b3e3a85b609d83 (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
from fileinput import filename
from myhdl import *
import os

from sympy import Si

from constants import *

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

    # Main code, this is the actual logic
    @staticmethod
    @block
    def logic(reset: Signal, clk: Signal, in0: Signal, out0: Signal, left_rigt: bool = 1, width: int = 8):

        @always_seq(clk.posedge, reset=reset)
        def shifter():
            if not left_rigt:
                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:])

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

        @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:])

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


    # This function links myhdl to icarus verilog sim
    def _cosim(self, reset: Signal, clk: Signal, in0: Signal, out0: Signal): #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"

        signals = locals()
        signals.pop('self', None)
        signals.pop('iverilog_cmd', None)
        signals.pop('vvp_cmd', None)

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

    def sim(self):
        tb = self.tb('logic')
        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()

hdl = ShiftReg()
hdl.sim()
hdl.convert()
hdl.cosim()