forked from TimRudy/ice-chips-verilog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
74160-tb.v
581 lines (541 loc) · 13.1 KB
/
74160-tb.v
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
// Test: 4-bit BCD decade counter with parallel load, asynchronous clear
module test;
`TBASSERT_METHOD(tbassert)
`TBASSERT_2_METHOD(tbassert2)
`CASE_TBASSERT_2_METHOD(case_tbassert2, tbassert2)
`TBCLK_WAIT_TICK_METHOD(wait_tick)
localparam WIDTH = 4; // do not pass this to the module because it is not variable
// DUT inputs
reg Clear_bar;
reg Load_bar;
reg ENT;
reg ENP;
reg [WIDTH-1:0] D;
reg Clk;
// DUT outputs
wire RCO;
wire [WIDTH-1:0] Q;
// DUT
ttl_74160 #(.DELAY_RISE(5), .DELAY_FALL(3)) dut(
.Clear_bar(Clear_bar),
.Load_bar(Load_bar),
.ENT(ENT),
.ENP(ENP),
.D(D),
.Clk(Clk),
.RCO(RCO),
.Q(Q)
);
initial Clk = 1'b0;
always #50 Clk = ~Clk;
task parallel_load_and_tick(input [WIDTH-1:0] D_next);
Load_bar = 1'b0;
D = D_next;
repeat (2) @(posedge Clk);
#7
Load_bar = 1'b1;
endtask
initial
begin
reg [WIDTH-1:0] D_next;
reg [WIDTH-1:0] Q_expected;
integer i;
$dumpfile("74160-tb.vcd");
$dumpvars;
// the following set of tests are for: load
#225
// steady state, enough time for clock pulse
tbassert(Q === 4'bxxxx, "Test 1");
tbassert(RCO === 1'bx, "Test 1");
#0
// load all zeroes, steady state before clock edge
Load_bar = 1'b0;
D = 4'b0000;
#25
tbassert(Q === 4'bxxxx, "Test 1");
tbassert(RCO === 1'bx, "Test 1");
#2
// load all zeroes, at clock edge, not enough time for output to fall
tbassert(Q === 4'bxxxx, "Test 1");
tbassert(RCO === 1'bx, "Test 1");
#2
// load all zeroes -> outputs 0
tbassert(Q == 4'b0000, "Test 1");
tbassert(RCO == 1'b0, "Test 1");
#140
// steady state, enough time for clock pulse -> no change to outputs after load signal ends
Load_bar = 1'b1;
#175
tbassert(Q == 4'b0000, "Test 2");
tbassert(RCO == 1'b0, "Test 2");
#0
// load all ones (special input ENT set) -> outputs 1s and 0
Load_bar = 1'b0;
ENT = 1'b1;
D = 4'b1111;
#125
Load_bar = 1'b1;
#110
tbassert(Q == 4'b1111, "Test 3");
tbassert(RCO == 1'b0, "Test 3");
#0
// load terminal count value (special input ENT set) -> outputs terminal count value and 1
Load_bar = 1'b0;
// ENT = 1'b1;
D = 4'b1001;
#90
Load_bar = 1'b1;
#110
tbassert(Q == 4'b1001, "Test 4");
tbassert(RCO == 1'b1, "Test 4");
#0
// repeat tests: the other control inputs take on values, but not clear mode, not count mode
D_next = 4'b1001; // initial value to start the loop
for (i = 1; i <= 6; i++)
begin
Q_expected = D_next;
D_next = (Q_expected + 1) ^ 5; // use a random value for next input
case (i)
1:
begin
ENT = 1'b0;
end
2:
begin
ENT = 1'b0;
ENP = 1'b0;
end
3:
begin
ENT = 1'b1;
ENP = 1'b0;
end
4:
begin
ENT = 1'b0;
ENP = 1'b1;
end
5:
begin
Clear_bar = 1'b1;
ENT = 1'b0;
ENP = 1'b1;
end
6:
begin
Clear_bar = 1'b1;
ENT = 1'b1;
ENP = 1'b0;
end
endcase
#75
tbassert2(Q == Q_expected, "Test", i, "5");
tbassert2(RCO == 1'b0, "Test", i, "5");
#0
// load next input -> outputs correspond to the input
Load_bar = 1'b0;
D = D_next;
#100
tbassert2(Q == D_next, "Test", i, "5");
tbassert2(RCO == 1'b0, "Test", i, "5");
#0
// steady state, enough time for clock pulse -> no change to outputs after load signal ends
Load_bar = 1'b1;
#105
tbassert2(Q == D_next, "Test", i, "5");
tbassert2(RCO == 1'b0, "Test", i, "5");
end
// end repeat tests
tbassert2(Q == 4'b1101, "Test", 6, "5"); // actual value at exit of the loop
#175
// the following set of tests are for: clear
// asynchronous clear from 1101, not enough time for output to fall
Clear_bar = 1'b0;
#2
tbassert(Q == 4'b1101, "Test 6");
tbassert(RCO == 1'b0, "Test 6");
#2
// asynchronous clear from 1101 -> outputs 0, not enough time for clock pulse
tbassert(Q == 4'b0000, "Test 6");
tbassert(RCO == 1'b0, "Test 6");
#150
Clear_bar = 1'b1;
#150
// asynchronous clear from 0110 with input ENT set -> outputs 0, enough time for clock pulse
ENT = 1'b1;
parallel_load_and_tick(4'b0110);
#50
tbassert(Q == 4'b0110, "Test 7");
tbassert(RCO == 1'b0, "Test 7");
#0
Clear_bar = 1'b0;
#120
tbassert(Q == 4'b0000, "Test 7");
tbassert(RCO == 1'b0, "Test 7");
#15
Clear_bar = 1'b1;
#15
// asynchronous clear from 1001 with input ENT set -> outputs 0, enough time for clock pulse
ENT = 1'b1;
parallel_load_and_tick(4'b1001);
#50
tbassert(Q == 4'b1001, "Test 8");
tbassert(RCO == 1'b1, "Test 8");
#0
Clear_bar = 1'b0;
#250
tbassert(Q == 4'b0000, "Test 8");
tbassert(RCO == 1'b0, "Test 8");
#20
Clear_bar = 1'b1;
#15
// asynchronous clear from 1001 with input ENT set -> outputs 0, not enough time for clock pulse
ENT = 1'b1;
parallel_load_and_tick(4'b1001);
#20
tbassert(Q == 4'b1001, "Test 9");
tbassert(RCO == 1'b1, "Test 9");
#0
Clear_bar = 1'b0;
#20
tbassert(Q == 4'b0000, "Test 9");
tbassert(RCO == 1'b0, "Test 9");
#10
// steady state -> remains clear after asynchronous clear signal ends
Clear_bar = 1'b1;
#120
tbassert(Q == 4'b0000, "Test 10");
tbassert(RCO == 1'b0, "Test 10");
#50
// the following set of tests are for: clear from initial state
Clear_bar = 1'bx;
Load_bar = 1'bx;
ENT = 1'bx;
ENP = 1'bx;
#15
parallel_load_and_tick(4'bxxxx);
#0
Load_bar = 1'bx;
#100
tbassert(Q === 4'bxxxx, "Test 11");
tbassert(RCO === 1'bx, "Test 11");
#0
// asynchronous clear from initial state, not enough time for output to fall
Clear_bar = 1'b0;
#2
tbassert(Q === 4'bxxxx, "Test 11");
tbassert(RCO === 1'bx, "Test 11");
#2
// asynchronous clear from initial state -> outputs 0, no clock edge nearby
tbassert(Q == 4'b0000, "Test 11");
tbassert(RCO == 1'b0, "Test 11");
#75
Clear_bar = 1'b1;
#50
Clear_bar = 1'bx;
// Load_bar = 1'bx;
// ENT = 1'bx;
// ENP = 1'bx;
#15
parallel_load_and_tick(4'bxxxx);
#0
Load_bar = 1'bx;
#92
tbassert(Q === 4'bxxxx, "Test 12");
tbassert(RCO === 1'bx, "Test 12");
#0
// asynchronous clear from initial state, not enough time for output to fall
Clear_bar = 1'b0;
#2
tbassert(Q === 4'bxxxx, "Test 12");
tbassert(RCO === 1'bx, "Test 12");
#2
// asynchronous clear from initial state -> outputs 0, near or at clock edge
tbassert(Q == 4'b0000, "Test 12");
tbassert(RCO == 1'b0, "Test 12");
#75
// steady state -> remains clear after asynchronous clear signal ends
Clear_bar = 1'b1;
#120
tbassert(Q == 4'b0000, "Test 13");
tbassert(RCO == 1'b0, "Test 13");
#0
Load_bar = 1'b1;
#80
tbassert(Q == 4'b0000, "Test 13");
tbassert(RCO == 1'b0, "Test 13");
#0
// the following set of tests are for: steady state
// change to different control inputs with null effect on output 0s
ENT = 1'b0;
ENP = 1'b1;
#7
tbassert(Q == 4'b0000, "Test 14");
tbassert(RCO == 1'b0, "Test 14");
#50
tbassert(Q == 4'b0000, "Test 14");
tbassert(RCO == 1'b0, "Test 14");
#100
tbassert(Q == 4'b0000, "Test 14");
tbassert(RCO == 1'b0, "Test 14");
#15
// same, the inputs reversed
ENT = 1'b1;
ENP = 1'b0;
#7
tbassert(Q == 4'b0000, "Test 15");
tbassert(RCO == 1'b0, "Test 15");
#50
tbassert(Q == 4'b0000, "Test 15");
tbassert(RCO == 1'b0, "Test 15");
#100
tbassert(Q == 4'b0000, "Test 15");
tbassert(RCO == 1'b0, "Test 15");
#0
// transient (unclocked) load input with null effect on output 0s
wait_tick();
#15
Load_bar = 1'b0;
D = 4'b1110;
#15
Load_bar = 1'b1;
#7
tbassert(Q == 4'b0000, "Test 16");
tbassert(RCO == 1'b0, "Test 16");
#50
tbassert(Q == 4'b0000, "Test 16");
tbassert(RCO == 1'b0, "Test 16");
#100
tbassert(Q == 4'b0000, "Test 16");
tbassert(RCO == 1'b0, "Test 16");
#0
// transient (unclocked) count mode input with null effect on output 0s
wait_tick();
#20
ENT = 1'b1;
ENP = 1'b1;
#15
ENP = 1'b0;
#15
tbassert(Q == 4'b0000, "Test 17");
tbassert(RCO == 1'b0, "Test 17");
#50
tbassert(Q == 4'b0000, "Test 17");
tbassert(RCO == 1'b0, "Test 17");
#100
tbassert(Q == 4'b0000, "Test 17");
tbassert(RCO == 1'b0, "Test 17");
#20
// change to different control inputs with null effect on output 1s and 0
ENT = 1'b0;
parallel_load_and_tick(4'b1001);
#50
tbassert(Q == 4'b1001, "Test 18");
tbassert(RCO == 1'b0, "Test 18");
#175
ENP = 1'b1;
#50
tbassert(Q == 4'b1001, "Test 18");
tbassert(RCO == 1'b0, "Test 18");
#100
tbassert(Q == 4'b1001, "Test 18");
tbassert(RCO == 1'b0, "Test 18");
#0
// transient (unclocked) load input with null effect on output
wait_tick();
#25
Load_bar = 1'b0;
D = 4'b0010;
#15
Load_bar = 1'b1;
#7
tbassert(Q == 4'b1001, "Test 19");
tbassert(RCO == 1'b0, "Test 19");
#50
tbassert(Q == 4'b1001, "Test 19");
tbassert(RCO == 1'b0, "Test 19");
#100
tbassert(Q == 4'b1001, "Test 19");
tbassert(RCO == 1'b0, "Test 19");
#0
// transient (unclocked) count mode input with null effect on output
wait_tick();
#15
ENT = 1'b1;
ENP = 1'b1;
#15
ENT = 1'b0;
#7
tbassert(Q == 4'b1001, "Test 20");
tbassert(RCO == 1'b0, "Test 20");
#50
tbassert(Q == 4'b1001, "Test 20");
tbassert(RCO == 1'b0, "Test 20");
#100
tbassert(Q == 4'b1001, "Test 20");
tbassert(RCO == 1'b0, "Test 20");
#0
// the following set of tests are for: counting
wait_tick();
#10
// after 100ns: first increment -> 0
ENT = 1'b1;
ENP = 1'b1;
#40
tbassert(Q == 4'b1001, "Test 21");
tbassert(RCO == 1'b1, "Test 21");
#50
tbassert(Q == 4'b1001, "Test 21");
tbassert(RCO == 1'b1, "Test 21");
#7
tbassert(Q == 4'b0000, "Test 21");
tbassert(RCO == 1'b0, "Test 21");
#90
// after 100ns: next increment -> 1
tbassert(Q == 4'b0000, "Test 22");
tbassert(RCO == 1'b0, "Test 22");
#10
tbassert(Q == 4'b0001, "Test 22");
tbassert(RCO == 1'b0, "Test 22");
#90
// after 100ns: next increment -> 2
tbassert(Q == 4'b0001, "Test 23");
tbassert(RCO == 1'b0, "Test 23");
#10
tbassert(Q == 4'b0010, "Test 23");
tbassert(RCO == 1'b0, "Test 23");
#7
// load during count -> 7
parallel_load_and_tick(4'b0111);
#0
tbassert(Q == 4'b0111, "Test 24");
tbassert(RCO == 1'b0, "Test 24");
#100
// after 100ns: next increment -> 8
tbassert(Q == 4'b1000, "Test 25");
tbassert(RCO == 1'b0, "Test 25");
#100
// after 100ns: next increment -> 9
tbassert(Q == 4'b1001, "Test 26");
tbassert(RCO == 1'b1, "Test 26");
#7
// pause during count -> 9
ENP = 1'b0;
#50
tbassert(Q == 4'b1001, "Test 27");
tbassert(RCO == 1'b1, "Test 27");
#50
tbassert(Q == 4'b1001, "Test 27");
tbassert(RCO == 1'b1, "Test 27");
#200
tbassert(Q == 4'b1001, "Test 27");
tbassert(RCO == 1'b1, "Test 27");
#0
// after 100ns: resume count and next increment -> 0
ENP = 1'b1;
#85
tbassert(Q == 4'b1001, "Test 28");
tbassert(RCO == 1'b1, "Test 28");
#15
tbassert(Q == 4'b0000, "Test 28");
tbassert(RCO == 1'b0, "Test 28");
#100
// after 100ns: next increment -> 1
tbassert(Q == 4'b0001, "Test 29");
tbassert(RCO == 1'b0, "Test 29");
#0
// asynchronous clear during count -> 0
Clear_bar = 1'b0;
#50
tbassert(Q == 4'b0000, "Test 30");
tbassert(RCO == 1'b0, "Test 30");
#0
// after 100ns: resume count and next increment -> 1
Clear_bar = 1'b1;
#10
tbassert(Q == 4'b0000, "Test 31");
tbassert(RCO == 1'b0, "Test 31");
#40
tbassert(Q == 4'b0001, "Test 31");
tbassert(RCO == 1'b0, "Test 31");
#50
// asynchronous clear then load during count -> 5
Clear_bar = 1'b0;
#50
Clear_bar = 1'b1;
parallel_load_and_tick(4'b0101);
#90
// after 100ns: next increment -> 6
tbassert(Q == 4'b0101, "Test 32");
tbassert(RCO == 1'b0, "Test 32");
#10
tbassert(Q == 4'b0110, "Test 32");
tbassert(RCO == 1'b0, "Test 32");
#20
// transient (unclocked) different control inputs during count with null effect on output
// and on next increment -> 7
ENP = 1'b0;
#50
tbassert(Q == 4'b0110, "Test 33");
tbassert(RCO == 1'b0, "Test 33");
#0
ENP = 1'b1;
#2
tbassert(Q == 4'b0110, "Test 33");
tbassert(RCO == 1'b0, "Test 33");
#50
tbassert(Q == 4'b0111, "Test 33");
tbassert(RCO == 1'b0, "Test 33");
#0
// the following set of tests are for: accepted behaviour outside normal usage
// repeat tests: load values above BCD 9 -> outputs go back on track to within the
// BCD decade count range at the next clock edge
for (i = 10; i <= 15; i++)
begin
parallel_load_and_tick(i);
#20
tbassert2(Q > 4'b1001, "Test", (i - 9), "34");
tbassert2(RCO == 1'b0, "Test", (i - 9), "34");
#80
tbassert2(Q >= 0 && Q <= 4'b1001, "Test", (i - 9), "34");
case_tbassert2(Q < 4'b1001, RCO == 1'b0, "Test", (i - 9), "34");
case_tbassert2(Q == 4'b1001, RCO == 1'b1, "Test", (i - 9), "34");
end
// end repeat tests
#0
// repeat tests: load values above BCD 9, not count mode -> outputs remain in steady state
ENT = 1'b1;
ENP = 1'b0;
for (i = 10; i <= 15; i++)
begin
parallel_load_and_tick(i);
#20
tbassert2(Q > 4'b1001, "Test", (i - 9), "35");
tbassert2(RCO == 1'b0, "Test", (i - 9), "35");
#120
tbassert2(Q > 4'b1001, "Test", (i - 9), "35");
tbassert2(RCO == 1'b0, "Test", (i - 9), "35");
end
// end repeat tests
#0
// output RCO tracks input ENT asynchronously
ENT = 1'b0;
ENP = 1'b1;
parallel_load_and_tick(4'b1001);
#100
tbassert(RCO == 1'b0, "Test 36");
#10
ENT = 1'b1;
#15
tbassert(Q == 4'b1001, "Test 36");
tbassert(RCO == 1'b1, "Test 36");
#0
ENT = 1'b0;
#15
tbassert(RCO == 1'b0, "Test 36");
#0
wait_tick();
#50
$finish;
end
endmodule