OpenCores
URL https://opencores.org/ocsvn/steelcore/steelcore/trunk

Subversion Repositories steelcore

[/] [rtl/] [bench/] [tb_branch_unit.v] - Blame information for rev 11

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 11 rafaelcalc
//////////////////////////////////////////////////////////////////////////////////
2
// Engineer: Rafael de Oliveira Calçada (rafaelcalcada@gmail.com)
3
// 
4
// Create Date: 26.04.2020 18:45:28
5
// Module Name: tb_branch_unit
6
// Project Name: Steel Core
7
// Description: Branch decision unit testbench
8
// 
9
// Dependencies: globals.vh
10
//               branch_unit.v
11
// 
12
// Version 0.01
13
// 
14
//////////////////////////////////////////////////////////////////////////////////
15
 
16
/*********************************************************************************
17
 
18
MIT License
19
 
20
Copyright (c) 2020 Rafael de Oliveira Calçada
21
 
22
Permission is hereby granted, free of charge, to any person obtaining a copy
23
of this software and associated documentation files (the "Software"), to deal
24
in the Software without restriction, including without limitation the rights
25
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
26
copies of the Software, and to permit persons to whom the Software is
27
furnished to do so, subject to the following conditions:
28
 
29
The above copyright notice and this permission notice shall be included in all
30
copies or substantial portions of the Software.
31
 
32
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38
SOFTWARE.
39
 
40
********************************************************************************/
41
 
42
`timescale 1ns / 1ps
43
`include "../globals.vh"
44
 
45
module tb_branch_unit();
46
 
47
    reg [6:2] OPCODE_6_TO_2;
48
    reg [31:0] RS1;
49
    reg [31:0] RS2;
50
    reg [2:0] FUNCT3;
51
        wire BRANCH_TAKEN;
52
 
53
        integer i = 0;
54
        reg signed [31:0] rs1s;
55
        reg signed [31:0] rs2s;
56
 
57
    branch_unit dut(
58
        .OPCODE_6_TO_2(OPCODE_6_TO_2),
59
        .RS1(RS1),
60
        .RS2(RS2),
61
        .FUNCT3(FUNCT3),
62
        .BRANCH_TAKEN(BRANCH_TAKEN)
63
    );
64
 
65
    initial
66
    begin
67
 
68
        $display("Testing Branch Unit...");
69
 
70
        $display("Testing branch comparator unit for BEQ.");
71
 
72
        FUNCT3 = 3'b000;
73
        OPCODE_6_TO_2 = `OPCODE_BRANCH;
74
 
75
        for(i = 0; i < 10000; i=i+1)
76
        begin
77
            RS1 = $random;
78
            RS2 = $random;
79
 
80
            #10;
81
 
82
            if(RS1 == RS2 && BRANCH_TAKEN != 1'b1)
83
            begin
84
                $display("FAIL. Expected BRANCH_TAKEN = 1.");
85
                $finish;
86
            end
87
            if(RS1 != RS2 && BRANCH_TAKEN != 1'b0)
88
            begin
89
                $display("FAIL. Expected BRANCH_TAKEN = 0.");
90
                $finish;
91
            end
92
        end
93
 
94
        RS1 = $random;
95
        RS2 = RS1;
96
        #10;
97
        if(BRANCH_TAKEN != 1'b1)
98
        begin
99
            $display("FAIL. Expected BRANCH_TAKEN = 1.");
100
            $finish;
101
        end
102
 
103
        $display("Branch comparator works fine for BEQ.");
104
 
105
        $display("Testing branch comparator unit for BNE.");
106
 
107
        FUNCT3 = 3'b001;
108
        OPCODE_6_TO_2 = `OPCODE_BRANCH;
109
 
110
        for(i = 0; i < 10000; i=i+1)
111
        begin
112
            RS1 = $random;
113
            RS2 = $random;
114
 
115
            #10;
116
 
117
            if(RS1 != RS2 && BRANCH_TAKEN != 1'b1)
118
            begin
119
                $display("FAIL. Expected BRANCH_TAKEN = 1.");
120
                $finish;
121
            end
122
            if(RS1 == RS2 && BRANCH_TAKEN != 1'b0)
123
            begin
124
                $display("FAIL. Expected BRANCH_TAKEN = 0.");
125
                $finish;
126
            end
127
        end
128
 
129
        RS1 = $random;
130
        RS2 = RS1;
131
        #10;
132
        if(BRANCH_TAKEN != 1'b0)
133
        begin
134
            $display("FAIL. Expected BRANCH_TAKEN = 0.");
135
            $finish;
136
        end
137
 
138
        $display("Branch comparator works fine for BNE.");
139
 
140
        $display("Testing branch comparator unit for BLT.");
141
 
142
        FUNCT3 = 3'b100;
143
        OPCODE_6_TO_2 = `OPCODE_BRANCH;
144
 
145
        for(i = 0; i < 10000; i=i+1)
146
        begin
147
            RS1 = $random;
148
            RS2 = $random;
149
            rs1s = RS1;
150
            rs2s = RS2;
151
            #10;
152
 
153
            if(rs1s < rs2s && BRANCH_TAKEN != 1'b1)
154
            begin
155
                $display("FAIL. Expected BRANCH_TAKEN = 1.");
156
                $finish;
157
            end
158
            if(rs1s >= rs2s && BRANCH_TAKEN != 1'b0)
159
            begin
160
                $display("FAIL. Expected BRANCH_TAKEN = 0.");
161
                $finish;
162
            end
163
        end
164
 
165
        RS1 = 32'b11111111_11111111_11111111_11111000;
166
        RS2 = 32'b11111111_11111111_11111111_11111100;
167
        #10;
168
        if(BRANCH_TAKEN != 1'b1)
169
        begin
170
            $display("FAIL. Expected BRANCH_TAKEN = 1.");
171
            $finish;
172
        end
173
 
174
        RS1 = 32'b11111111_11111111_11111111_11111100;
175
        RS2 = 32'b11111111_11111111_11111111_11111000;
176
        #10;
177
        if(BRANCH_TAKEN != 1'b0)
178
        begin
179
            $display("FAIL. Expected BRANCH_TAKEN = 0.");
180
            $finish;
181
        end
182
 
183
        RS1 = 32'b00000000_00000000_00000000_00001000;
184
        RS2 = 32'b00000000_00000000_00000000_00001100;
185
        #10;
186
        if(BRANCH_TAKEN != 1'b1)
187
        begin
188
            $display("FAIL. Expected BRANCH_TAKEN = 1.");
189
            $finish;
190
        end
191
 
192
        RS1 = 32'b00000000_00000000_00000000_00001100;
193
        RS2 = 32'b00000000_00000000_00000000_00001000;
194
        #10;
195
        if(BRANCH_TAKEN != 1'b0)
196
        begin
197
            $display("FAIL. Expected BRANCH_TAKEN = 0.");
198
            $finish;
199
        end
200
 
201
        RS1 = 32'b11111111_11111111_11111111_11111000;
202
        RS2 = 32'b00000000_00000000_00000000_00001100;
203
        #10;
204
        if(BRANCH_TAKEN != 1'b1)
205
        begin
206
            $display("FAIL. Expected BRANCH_TAKEN = 1.");
207
            $finish;
208
        end
209
 
210
        RS1 = 32'b00000000_00000000_00000000_00001100;
211
        RS2 = 32'b11111111_11111111_11111111_11111000;
212
        #10;
213
        if(BRANCH_TAKEN != 1'b0)
214
        begin
215
            $display("FAIL. Expected BRANCH_TAKEN = 0.");
216
            $finish;
217
        end
218
 
219
        $display("Branch comparator works fine for BLT.");
220
 
221
        $display("Testing branch comparator unit for BGE.");
222
 
223
        FUNCT3 = 3'b101;
224
        OPCODE_6_TO_2 = `OPCODE_BRANCH;
225
 
226
        for(i = 0; i < 10000; i=i+1)
227
        begin
228
            RS1 = $random;
229
            RS2 = $random;
230
            rs1s = RS1;
231
            rs2s = RS2;
232
            #10;
233
 
234
            if(rs1s >= rs2s && BRANCH_TAKEN != 1'b1)
235
            begin
236
                $display("FAIL. Expected BRANCH_TAKEN = 1.");
237
                $finish;
238
            end
239
            if(rs1s < rs2s && BRANCH_TAKEN != 1'b0)
240
            begin
241
                $display("FAIL. Expected BRANCH_TAKEN = 0.");
242
                $finish;
243
            end
244
        end
245
 
246
        RS1 = 32'b11111111_11111111_11111111_11111000;
247
        RS2 = 32'b11111111_11111111_11111111_11111100;
248
        #10;
249
        if(BRANCH_TAKEN != 1'b0)
250
        begin
251
            $display("FAIL. Expected BRANCH_TAKEN = 0.");
252
            $finish;
253
        end
254
 
255
        RS1 = 32'b11111111_11111111_11111111_11111100;
256
        RS2 = 32'b11111111_11111111_11111111_11111000;
257
        #10;
258
        if(BRANCH_TAKEN != 1'b1)
259
        begin
260
            $display("FAIL. Expected BRANCH_TAKEN = 1.");
261
            $finish;
262
        end
263
 
264
        RS1 = 32'b00000000_00000000_00000000_00001000;
265
        RS2 = 32'b00000000_00000000_00000000_00001100;
266
        #10;
267
        if(BRANCH_TAKEN != 1'b0)
268
        begin
269
            $display("FAIL. Expected BRANCH_TAKEN = 0.");
270
            $finish;
271
        end
272
 
273
        RS1 = 32'b00000000_00000000_00000000_00001100;
274
        RS2 = 32'b00000000_00000000_00000000_00001000;
275
        #10;
276
        if(BRANCH_TAKEN != 1'b1)
277
        begin
278
            $display("FAIL. Expected BRANCH_TAKEN = 1.");
279
            $finish;
280
        end
281
 
282
        RS1 = 32'b11111111_11111111_11111111_11111000;
283
        RS2 = 32'b00000000_00000000_00000000_00001100;
284
        #10;
285
        if(BRANCH_TAKEN != 1'b0)
286
        begin
287
            $display("FAIL. Expected BRANCH_TAKEN = 0.");
288
            $finish;
289
        end
290
 
291
        RS1 = 32'b00000000_00000000_00000000_00001100;
292
        RS2 = 32'b11111111_11111111_11111111_11111000;
293
        #10;
294
        if(BRANCH_TAKEN != 1'b1)
295
        begin
296
            $display("FAIL. Expected BRANCH_TAKEN = 1.");
297
            $finish;
298
        end
299
 
300
        $display("Branch comparator works fine for BGE.");
301
 
302
        $display("Testing branch comparator unit for BLTU.");
303
 
304
        FUNCT3 = 3'b110;
305
        OPCODE_6_TO_2 = `OPCODE_BRANCH;
306
 
307
        for(i = 0; i < 10000; i=i+1)
308
        begin
309
            RS1 = $random;
310
            RS2 = $random;
311
 
312
            #10;
313
 
314
            if(RS1 < RS2 && BRANCH_TAKEN != 1'b1)
315
            begin
316
                $display("FAIL. Expected BRANCH_TAKEN = 1.");
317
                $finish;
318
            end
319
            if(RS1 >= RS2 && BRANCH_TAKEN != 1'b0)
320
            begin
321
                $display("FAIL. Expected BRANCH_TAKEN = 0.");
322
                $finish;
323
            end
324
        end
325
 
326
        RS1 = 32'b11111111_11111111_11111111_11111000;
327
        RS2 = 32'b11111111_11111111_11111111_11111100;
328
        #10;
329
        if(BRANCH_TAKEN != 1'b1)
330
        begin
331
            $display("FAIL. Expected BRANCH_TAKEN = 1.");
332
            $finish;
333
        end
334
 
335
        RS1 = 32'b11111111_11111111_11111111_11111100;
336
        RS2 = 32'b11111111_11111111_11111111_11111000;
337
        #10;
338
        if(BRANCH_TAKEN != 1'b0)
339
        begin
340
            $display("FAIL. Expected BRANCH_TAKEN = 0.");
341
            $finish;
342
        end
343
 
344
        RS1 = 32'b00000000_00000000_00000000_00001000;
345
        RS2 = 32'b00000000_00000000_00000000_00001100;
346
        #10;
347
        if(BRANCH_TAKEN != 1'b1)
348
        begin
349
            $display("FAIL. Expected BRANCH_TAKEN = 1.");
350
            $finish;
351
        end
352
 
353
        RS1 = 32'b00000000_00000000_00000000_00001100;
354
        RS2 = 32'b00000000_00000000_00000000_00001000;
355
        #10;
356
        if(BRANCH_TAKEN != 1'b0)
357
        begin
358
            $display("FAIL. Expected BRANCH_TAKEN = 0.");
359
            $finish;
360
        end
361
 
362
        RS1 = 32'b11111111_11111111_11111111_11111000;
363
        RS2 = 32'b00000000_00000000_00000000_00001100;
364
        #10;
365
        if(BRANCH_TAKEN != 1'b0)
366
        begin
367
            $display("FAIL. Expected BRANCH_TAKEN = 0.");
368
            $finish;
369
        end
370
 
371
        RS1 = 32'b00000000_00000000_00000000_00001100;
372
        RS2 = 32'b11111111_11111111_11111111_11111000;
373
        #10;
374
        if(BRANCH_TAKEN != 1'b1)
375
        begin
376
            $display("FAIL. Expected BRANCH_TAKEN = 1.");
377
            $finish;
378
        end
379
 
380
        $display("Branch comparator works fine for BLTU.");
381
 
382
        $display("Testing branch comparator unit for BGEU.");
383
 
384
        FUNCT3 = 3'b111;
385
        OPCODE_6_TO_2 = `OPCODE_BRANCH;
386
 
387
        for(i = 0; i < 10000; i=i+1)
388
        begin
389
            RS1 = $random;
390
            RS2 = $random;
391
 
392
            #10;
393
 
394
            if(RS1 >= RS2 && BRANCH_TAKEN != 1'b1)
395
            begin
396
                $display("FAIL. Expected BRANCH_TAKEN = 1.");
397
                $finish;
398
            end
399
            if(RS1 < RS2 && BRANCH_TAKEN != 1'b0)
400
            begin
401
                $display("FAIL. Expected BRANCH_TAKEN = 0.");
402
                $finish;
403
            end
404
        end
405
 
406
        RS1 = 32'b11111111_11111111_11111111_11111000;
407
        RS2 = 32'b11111111_11111111_11111111_11111100;
408
        #10;
409
        if(BRANCH_TAKEN != 1'b0)
410
        begin
411
            $display("FAIL. Expected BRANCH_TAKEN = 0.");
412
            $finish;
413
        end
414
 
415
        RS1 = 32'b11111111_11111111_11111111_11111100;
416
        RS2 = 32'b11111111_11111111_11111111_11111000;
417
        #10;
418
        if(BRANCH_TAKEN != 1'b1)
419
        begin
420
            $display("FAIL. Expected BRANCH_TAKEN = 1.");
421
            $finish;
422
        end
423
 
424
        RS1 = 32'b00000000_00000000_00000000_00001000;
425
        RS2 = 32'b00000000_00000000_00000000_00001100;
426
        #10;
427
        if(BRANCH_TAKEN != 1'b0)
428
        begin
429
            $display("FAIL. Expected BRANCH_TAKEN = 0.");
430
            $finish;
431
        end
432
 
433
        RS1 = 32'b00000000_00000000_00000000_00001100;
434
        RS2 = 32'b00000000_00000000_00000000_00001000;
435
        #10;
436
        if(BRANCH_TAKEN != 1'b1)
437
        begin
438
            $display("FAIL. Expected BRANCH_TAKEN = 1.");
439
            $finish;
440
        end
441
 
442
        RS1 = 32'b11111111_11111111_11111111_11111000;
443
        RS2 = 32'b00000000_00000000_00000000_00001100;
444
        #10;
445
        if(BRANCH_TAKEN != 1'b1)
446
        begin
447
            $display("FAIL. Expected BRANCH_TAKEN = 1.");
448
            $finish;
449
        end
450
 
451
        RS1 = 32'b00000000_00000000_00000000_00001100;
452
        RS2 = 32'b11111111_11111111_11111111_11111000;
453
        #10;
454
        if(BRANCH_TAKEN != 1'b0)
455
        begin
456
            $display("FAIL. Expected BRANCH_TAKEN = 0.");
457
            $finish;
458
        end
459
 
460
        $display("Branch comparator works fine for BGEU.");
461
 
462
        $display("Testing branch comparator unit for JAL.");
463
 
464
        FUNCT3 = 3'bxxx;
465
        OPCODE_6_TO_2 = `OPCODE_JAL;
466
 
467
        #10;
468
 
469
        if(BRANCH_TAKEN != 1'b1)
470
        begin
471
            $display("FAIL. Expected BRANCH_TAKEN = 1.");
472
            $finish;
473
        end
474
 
475
        $display("Branch comparator works fine for JAL.");
476
 
477
        $display("Testing branch comparator unit for JALR.");
478
 
479
        FUNCT3 = 3'bxxx;
480
        OPCODE_6_TO_2 = `OPCODE_JALR;
481
 
482
        #10;
483
 
484
        if(BRANCH_TAKEN != 1'b1)
485
        begin
486
            $display("FAIL. Expected BRANCH_TAKEN = 1.");
487
            $finish;
488
        end
489
 
490
        $display("Branch comparator works fine for JALR.");
491
 
492
        $display("Branch Unit successfully tested.");
493
 
494
    end
495
 
496
endmodule

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.