-
Notifications
You must be signed in to change notification settings - Fork 0
/
claw_machine_tb.sv
332 lines (292 loc) · 14.2 KB
/
claw_machine_tb.sv
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
`timescale 1ns/1ps
module claw_machine_tb(rstn, clk, enable, one, two, catch, refund, strength, balance, strength_LED, balance_units_LED, balance_tens_LED, max_count);
//I/O ports
input logic rstn;
input logic enable;
input logic clk;
input logic one;
input logic two;
input logic catch;
input logic refund;
output logic [3:0] strength; //up to 9 strength
output logic [3:0] balance; //up tp 14 dollars refund
output logic [6:0] strength_LED; //7 segments to display strength
output logic [6:0] balance_units_LED; //7 segments to display balance
output logic [6:0] balance_tens_LED; //7 segments to display balance
output logic [31:0] max_count; //counter
//dut
claw_machine_map dut(
.rstn(rstn),
.enable(enable),
.clk(clk),
.one(one),
.two(two),
.catch(catch),
.refund(refund),
.strength(strength),
.balance(balance),
.strength_LED(strength_LED),
.balance_units_LED(balance_units_LED),
.balance_tens_LED(balance_tens_LED),
.max_count(max_count)
);
//clock generator
initial begin
clk <= 0;
forever begin
#50 clk = ~clk; //clk at 100Mhz
end
end
//reset trigger
initial begin
#100 rstn <= 0;
repeat(10) @(posedge clk);
rstn <= 1;
end
//enable trigger
initial begin
#100 enable <= 0;
repeat(10) @(posedge clk);
enable <= 1;
end
//task for five combinations of sequence_generator
task sequence_generator(input reg[3:0] data_seq); //data_seq 4 bits which represents signal: one, two, catch, refund seperatly
casez(data_seq)
4'b0000: begin //No operation
one <= 0;
two <= 0;
catch <= 0;
refund <= 0;
@(posedge clk);
end
4'b10??: begin //C1, input one dollar
one <= 1;
two <= 0;
catch <= data_seq[1];
refund <= data_seq[0];
@(posedge clk);
end
4'b01??: begin //C2, input two dollars
one <= 0;
two <= 1;
catch <= data_seq[1];
refund <= data_seq[1];
@(posedge clk);
end
4'b11??: begin //C3, input three dollars
one <= 1;
two <= 1;
catch <= data_seq[1];
refund <= data_seq[0];
@(posedge clk);
end
4'b0010: begin //C4, catch
one <= 0;
two <= 0;
catch <= 1;
refund <= 0;
@(posedge clk);
end
4'b0001: begin //C5, refund
one <= 0;
two <= 0;
catch <= 0;
refund <= 1;
@(posedge clk);
end
default: begin //other should maintain as it's current state
one <= data_seq[3];
two <= data_seq[2];
catch <= data_seq[1];
refund <= data_seq[0];
@(posedge clk);
end
endcase
$display("current data_seqiton input is one=%b, two=%b, catch=%b, refund=%b", one, two, catch, refund);
endtask
//test
initial begin
@(posedge rstn);
//let's verify the no input function-------------------------------------------------------------------------------
$display("\nlet's generate 5 IDEL state");
repeat(5) sequence_generator(4'b0000); //it should stay at the IDEL state
if(strength != 4'b0000 && balance != 4'b0000)
$display("Sorry, this function is worng! The supposed output is: strengh=4'b0000, balance=4'b0000\n");
else
$display("Congradulations! This function is pass.\n");
//-----------------------------------------------------------------------------------------------------------------
//let's verify the one dollar state transfer function--------------------------------------------------------------
$display("let's generate 10 one dollar and this will refound 10 dollars");
repeat(10) sequence_generator(4'b1000); //it should branch from IDEL state to S9 state, and refund 10 dollars and return to IDEL
if(strength != 4'b0000 && balance != 4'b1010)
$display("Sorry, this function is worng! The supposed output is: strengh=4'b0000, balance=4'b1010\n");
else
$display("Congradulations! This function is pass.\n");
//-----------------------------------------------------------------------------------------------------------------
//let's verify the two dollars state transfer function-------------------------------------------------------------
$display("let's generate 5 two dollar and this will refound 10 dollars");
repeat(6) sequence_generator(4'b0100); //refund 10 dollars and then return to IDLE
if(strength != 4'b0000 && balance != 4'b1100)
$display("Sorry, this function is worng! The supposed output is: strengh=4'b0000, balance=4'b1010\n");
else
$display("Congradulations! This function is pass.\n");
//-----------------------------------------------------------------------------------------------------------------
//let's verify the three dollar state transfer function------------------------------------------------------------
$display("let's generate 4 three dollar and this will refound 12 dollars");
repeat(4) sequence_generator(4'b1100); //refund 12 dollars and then return to IDLE
if(strength != 4'b0000 && balance != 4'b1100)
$display("Sorry, this function is worng! The supposed output is: strengh=4'b0000, balance=4'b1100\n");
else
$display("Congradulations! This function is pass.\n");
//-----------------------------------------------------------------------------------------------------------------
//let's verify the catch function-----------------------------------------------------------------------------------
//let's verify the catch function: strength 1----------------------------------------------------------------------
$display("let's generate catch with strength 1");
sequence_generator(4'b1000); //branch to S1
sequence_generator(4'b0010); //strength 1 to catch
if(strength != 4'b0001 && balance != 4'b0000)
$display("Sorry, this function is worng! The supposed output is: strengh=4'b0001, balance=4'b0000\n");
else
$display("Congradulations! This function is pass.\n");
//let's verify the catch function: strength 2----------------------------------------------------------------------
$display("let's generate catch with strength 2");
sequence_generator(4'b0100); //branch to S2
sequence_generator(4'b0010); //strength 2 to catch
if(strength != 4'b0010 && balance != 4'b0000)
$display("Sorry, this function is worng! The supposed output is: strengh=4'b0010`, balance=4'b0000\n");
else
$display("Congradulations! This function is pass.\n");
//let's verify the catch function: strength 3----------------------------------------------------------------------
$display("let's generate catch with strength 3");
repeat(3) sequence_generator(4'b1000); //branch to S3
sequence_generator(4'b0010); //strength 3 to catch
if(strength != 4'b0011 && balance != 4'b0000)
$display("Sorry, this function is worng! The supposed output is: strengh=4'b0011`, balance=4'b0000\n");
else
$display("Congradulations! This function is pass.\n");
//let's verify the catch function: strength 4----------------------------------------------------------------------
$display("let's generate catch with strength 4");
repeat(2) sequence_generator(4'b0100); //branch to S4
sequence_generator(4'b0010); //strength 4 to catch
if(strength != 4'b0100 && balance != 4'b0000)
$display("Sorry, this function is worng! The supposed output is: strengh=4'b0100`, balance=4'b0000\n");
else
$display("Congradulations! This function is pass.\n");
//let's verify the catch function: strength 5----------------------------------------------------------------------
$display("let's generate catch with strength 5");
repeat(5) sequence_generator(4'b1000); //branch to S5
sequence_generator(4'b0010); //strength 5 to catch
if(strength != 4'b0101 && balance != 4'b0000)
$display("Sorry, this function is worng! The supposed output is: strengh=4'b0101`, balance=4'b0000\n");
else
$display("Congradulations! This function is pass.\n");
//let's verify the catch function: strength 6----------------------------------------------------------------------
$display("let's generate catch with strength 6");
repeat(3) sequence_generator(4'b0100); //branch to S6
sequence_generator(4'b0010); //strength 6 to catch
if(strength != 4'b0110 && balance != 4'b0000)
$display("Sorry, this function is worng! The supposed output is: strengh=4'b0110`, balance=4'b0000\n");
else
$display("Congradulations! This function is pass.\n");
//let's verify the catch function: strength 7----------------------------------------------------------------------
$display("let's generate catch with strength 7");
repeat(7) sequence_generator(4'b1000); //branch to S7
sequence_generator(4'b0010); //strength 7 to catch
if(strength != 4'b0111 && balance != 4'b0000)
$display("Sorry, this function is worng! The supposed output is: strengh=4'b0111`, balance=4'b0000\n");
else
$display("Congradulations! This function is pass.\n");
//let's verify the catch function: strength 8----------------------------------------------------------------------
$display("let's generate catch with strength 8");
repeat(4) sequence_generator(4'b0100); //branch to S8
sequence_generator(4'b0010); //strength 8 to catch
if(strength != 4'b1000 && balance != 4'b0000)
$display("Sorry, this function is worng! The supposed output is: strengh=4'b1000`, balance=4'b0000\n");
else
$display("Congradulations! This function is pass.\n");
//let's verify the catch function: strength 9----------------------------------------------------------------------
$display("let's generate catch with strength 9");
repeat(9) sequence_generator(4'b1000); //branch to S9
sequence_generator(4'b0010); //strength 9 to catch
if(strength != 4'b1001 && balance != 4'b0000)
$display("Sorry, this function is worng! The supposed output is: strengh=4'b1001`, balance=4'b0000\n");
else
$display("Congradulations! This function is pass.\n");
//-----------------------------------------------------------------------------------------------------------------
//let's verify the refund function-----------------------------------------------------------------------------------
//let's verify the refund function: balance 1------------------------------------------------------------------------
$display("let's generate catch with balance 1");
sequence_generator(4'b1000); //branch to S1
sequence_generator(4'b0001); //balance 1
if(strength != 4'b0000 && balance != 4'b0001)
$display("Sorry, this function is worng! The supposed output is: strengh=4'b0000`, balance=4'b0001\n");
else
$display("Congradulations! This function is pass.\n");
//let's verify the refund function: balance 2------------------------------------------------------------------------
$display("let's generate catch with balance 2");
sequence_generator(4'b0100); //branch to S2
sequence_generator(4'b0001); //balance 2
if(strength != 4'b0000 && balance != 4'b0010)
$display("Sorry, this function is worng! The supposed output is: strengh=4'b0000`, balance=4'b0010\n");
else
$display("Congradulations! This function is pass.\n");
//let's verify the refund function: balance 3------------------------------------------------------------------------
$display("let's generate catch with balance 3");
repeat(3) sequence_generator(4'b1000); //branch to S3
sequence_generator(4'b0001); //balance 3
if(strength != 4'b0000 && balance != 4'b0011)
$display("Sorry, this function is worng! The supposed output is: strengh=4'b0000`, balance=4'b0011\n");
else
$display("Congradulations! This function is pass.\n");
//let's verify the refund function: balance 4------------------------------------------------------------------------
$display("let's generate catch with balance 4");
repeat(2) sequence_generator(4'b0100); //branch to S4
sequence_generator(4'b0001); //balance 4
if(strength != 4'b0000 && balance != 4'b0100)
$display("Sorry, this function is worng! The supposed output is: strengh=4'b0000`, balance=4'b0100\n");
else
$display("Congradulations! This function is pass.\n");
//let's verify the refund function: balance 5------------------------------------------------------------------------
$display("let's generate catch with balance 5");
repeat(5) sequence_generator(4'b1000); //branch to S5
sequence_generator(4'b0001); //balance 5
if(strength != 4'b0000 && balance != 4'b0101)
$display("Sorry, this function is worng! The supposed output is: strengh=4'b0000`, balance=4'b0101\n");
else
$display("Congradulations! This function is pass.\n");
//let's verify the refund function: balance 6------------------------------------------------------------------------
$display("let's generate catch with balance 6");
repeat(3) sequence_generator(4'b0100); //branch to S6
sequence_generator(4'b0001); //balance 6
if(strength != 4'b0000 && balance != 4'b0110)
$display("Sorry, this function is worng! The supposed output is: strengh=4'b0000`, balance=4'b0110\n");
else
$display("Congradulations! This function is pass.\n");
//let's verify the refund function: balance 7------------------------------------------------------------------------
$display("let's generate catch with balance 7");
repeat(7) sequence_generator(4'b1000); //branch to S7
sequence_generator(4'b0001); //balance 7
if(strength != 4'b0000 && balance != 4'b0111)
$display("Sorry, this function is worng! The supposed output is: strengh=4'b0000`, balance=4'b0111\n");
else
$display("Congradulations! This function is pass.\n");
//let's verify the refund function: balance 8------------------------------------------------------------------------
$display("let's generate catch with balance 8");
repeat(4) sequence_generator(4'b0100); //branch to S8
sequence_generator(4'b0001); //balance 8
if(strength != 4'b0000 && balance != 4'b1000)
$display("Sorry, this function is worng! The supposed output is: strengh=4'b0000`, balance=4'b1000\n");
else
$display("Congradulations! This function is pass.\n");
//let's verify the refund function: balance 9------------------------------------------------------------------------
$display("let's generate catch with balance 9");
repeat(9) sequence_generator(4'b1000); //branch to S9
sequence_generator(4'b0001); //balance 9
if(strength != 4'b0000 && balance != 4'b1001)
$display("Sorry, this function is worng! The supposed output is: strengh=4'b0000`, balance=4'b1001\n");
else
$display("Congradulations! This function is pass.\n");
//-----------------------------------------------------------------------------------------------------------------
$finish;
end
endmodule