-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestbench.v
More file actions
186 lines (153 loc) · 3.29 KB
/
testbench.v
File metadata and controls
186 lines (153 loc) · 3.29 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
module testbench;
localparam VERBOSE_SPI = 0;
reg clk = 1;
always #5 clk = ~clk;
reg reset;
integer cycle;
always @(posedge clk) cycle <= reset ? 0 : cycle + 1;
reg spi_ss;
reg spi_clk;
reg spi_mosi;
wire spi_miso;
wire [5:0] motor_left;
wire [5:0] motor_right;
wire [5:0] motor_reset;
reg [5:0] motor_pulse;
reg [5:0] motor_fault;
reg [5:0] motor_otw;
wire motor_left_0 = motor_left[0];
wire motor_right_0 = motor_right[0];
wire motor_pulse_0 = motor_pulse[0];
dcmctrl uut (
.clk(clk),
.reset(reset),
.spi_ss(spi_ss),
.spi_clk(spi_clk),
.spi_mosi(spi_mosi),
.spi_miso(spi_miso),
.motor_left(motor_left),
.motor_right(motor_right),
.motor_reset(motor_reset),
.motor_pulse(motor_pulse),
.motor_fault(motor_fault),
.motor_otw(motor_otw)
);
reg [7:0] spidata;
task spi_xfer_begin;
begin
spi_ss <= 0;
#50;
if (VERBOSE_SPI)
$display("spi begin");
end
endtask
task spi_xfer_byte(input [7:0] d);
integer i;
begin
spidata <= d;
#50;
for (i = 0; i < 8; i = i+1) begin
spi_mosi <= spidata[7];
spi_clk <= 0;
#50;
spi_clk <= 1;
spidata <= {spidata, spi_miso};
#50;
end
if (VERBOSE_SPI)
$display("spi read: %02x", spidata);
end
endtask
task spi_xfer_end;
begin
spi_ss <= 1;
#100;
if (VERBOSE_SPI)
$display("spi end\n\n");
end
endtask
integer i;
reg [15:0] pos = 2500;
initial begin
$dumpfile("testbench.vcd");
$dumpvars(0, testbench);
spi_ss <= 1;
spi_clk <= 1;
spi_mosi <= 0;
reset <= 1;
motor_pulse <= 0;
motor_fault <= 0;
motor_otw <= 0;
repeat (100) @(posedge clk);
reset <= 0;
// Zero initialize register file and reset
spi_xfer_begin;
spi_xfer_byte(128);
for (i = 0; i < 128; i = i+1)
spi_xfer_byte(0);
spi_xfer_end;
repeat (10) @(posedge clk);
reset <= 1;
repeat (10) @(posedge clk);
reset <= 0;
repeat (7) begin
// Clear flags for channel 0
spi_xfer_begin;
spi_xfer_byte(128 | 0);
spi_xfer_byte(0); // flags
spi_xfer_end;
#555;
// Set position for channel 0
spi_xfer_begin;
spi_xfer_byte(128 | 64 | 0);
spi_xfer_byte(250); // speed
spi_xfer_byte(0); // target position [23:16]
spi_xfer_byte(pos[15:8]); // target position [15:8]
spi_xfer_byte(pos[7:0]); // target position [7:0]
spi_xfer_end;
#555;
repeat (1000) begin
if (VERBOSE_SPI)
$display("T = %d", $time);
spi_xfer_begin;
spi_xfer_byte(0 | 0);
spi_xfer_byte(0); // flags
spi_xfer_byte(0); // current position [23:16]
spi_xfer_byte(0); // current position [15:8]
spi_xfer_byte(0); // current position [7:0]
spi_xfer_end;
#991;
end
// Set speed for channel 0
spi_xfer_begin;
spi_xfer_byte(128 | 64 | 0);
spi_xfer_byte(0); // speed
spi_xfer_end;
#1841;
repeat (11) begin
if (VERBOSE_SPI)
$display("T = %d", $time);
spi_xfer_begin;
spi_xfer_byte(0 | 0);
spi_xfer_byte(0); // flags
spi_xfer_byte(0); // current position [23:16]
spi_xfer_byte(0); // current position [15:8]
spi_xfer_byte(0); // current position [7:0]
spi_xfer_end;
#991;
end
pos = 2500 - pos;
end
$finish;
end
initial begin
motor_pulse <= 0;
#500;
forever begin
motor_pulse <= motor_left | motor_right;
#123;
motor_pulse <= 0;
#9999;
end
end
endmodule