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
106
107
108
109
110
111
112
113
114
115
116
117
118
|
from myhdl import *
from myhdl_wrap import Myhdl_Wrapper
import random
from random import randint
random.seed(63)
class ShiftReg(Myhdl_Wrapper):
def __init__(self):
super().__init__()
# Main code, this is the actual logic
@staticmethod
@block
def ShiftReg(reset: Signal, clk: Signal, load: Signal, in0: Signal, out0: Signal, left_right: Signal):
width = len(out0)
@instance
def shifter():
while True:
yield clk.posedge, reset.negedge
if not reset:
out0.next = load
else:
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 = Signal(False)
clk = Signal(bool(0))
load = Signal(intbv(0xA5)[8:])
in0 = Signal(bool(0))
out0 = Signal(modbv(int(load))[8:])
left_right = Signal(bool(0))
dut = func(reset=reset, clk=clk, load=load, in0=in0, out0=out0, left_right=left_right)
@always(delay(2))
def clock_gen():
clk.next = not clk
@instance
def monitor():
last_val = out0
while True:
yield clk.posedge, reset.negedge
yield delay(1)
if reset == False:
assert out0 == load, "Is output is not zero at reset"
else:
if not left_right:
assert int(out0) == ((last_val << 1) & 0xFF) | int(in0), "Not shifting left correctly"
else:
assert int(out0) == ((last_val >> 1) & 0xFF) | (int(in0) << 7), "Not shifting rigth correctly"
last_val = int(out0)
@instance
def reset_test():
yield clk.negedge
reset.next = True
while True:
reset.next = True
yield delay(randint(25, 28))
reset.next = False
yield delay(randint(1,4))
@instance
def stimulus():
for i in range(20):
yield clk.negedge
left_right.next = 0
in0.next = randint(0, 1)
for i in range(20):
yield clk.negedge
left_right.next = 1
in0.next = randint(0, 1)
raise StopSimulation
return dut, clock_gen, monitor, stimulus, reset_test
def export(self):
reset = Signal(False)
clk = Signal(bool(0))
load = Signal(intbv(0x00)[8:])
in0 = Signal(bool(0))
out0 = Signal(modbv(int(load))[8:])
left_right = Signal(bool(0))
self._export(reset=reset, clk=clk, load=load, in0=in0, out0=out0, left_right=left_right)
def test_shift_reg_sim():
hdl = ShiftReg()
hdl.sim()
def test_shift_reg_cosim():
hdl = ShiftReg()
hdl.export()
hdl.cosim()
|