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

Subversion Repositories integer_square_root

[/] [integer_square_root/] [trunk/] [src/] [test_ISR.sv] - Blame information for rev 3

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ayka
`define HALF_CYCLE 1000
2
`timescale 1ns / 1ps
3
//////////////////////////////////////////////////////////////////////////////////
4
// Company:
5
// Engineer: Yihua Liu
6
//
7
// Create Date: 2022/06/08 16:51:12
8
// Design Name:
9
// Module Name: testbench
10
// Project Name: lab_3_b
11
// Target Devices: xczu7eg-ffvf1517-2-i
12
// Tool Versions:
13
// Description:
14
//
15
// Dependencies:
16
//
17
// Revision:
18
// Revision 0.01 - File Created
19
// Additional Comments:
20
//
21
//////////////////////////////////////////////////////////////////////////////////
22
 
23
 
24
module testbench();
25
 
26
        logic [63:0] value;
27
        logic clock, reset;
28
 
29
        logic [31:0] result;
30
        logic done;
31
 
32
    logic [31:0] cres;
33
 
34
        wire correct = (cres===result)|~done|reset;
35
 
36
        integer value_test, j;
37
        time start_time = $time, finish_time;
38
 
39
    task ISR_test;
40
        input [63:0] value;
41
        output [31:0] cres;
42
                cres = 0;
43
        for (integer i = 31; i >= 0; i--) begin
44
                        cres[i] = 1;
45
                        if (cres * cres > value)
46
                            cres[i] = 0;
47
        end
48
    endtask
49
 
50
    ISR isr(
51
        .reset(reset),
52
        .value(value),
53
        .clock(clock),
54
        .result(result),
55
        .done(done)
56
    );
57
 
58
        always @(posedge clock)
59
                #2 if(!correct) begin
60
                        $display("Incorrect at time %4.0f",$time);
61
                        $display("cres = %h result = %h",cres,result);
62
                        $display("@@@Failed");
63
                        $finish;
64
                end
65
 
66
        always begin
67
                #`HALF_CYCLE;
68
                clock=~clock;
69
        end
70
 
71
        task wait_until_done;
72
                forever begin : wait_loop
73
                        @(posedge done);
74
                        @(negedge clock);
75
                        if(done) begin
76
                                finish_time = $time;
77
                                $display("Done one calculation of ISR");
78
                            // Calculate the total number of cycles you need to do one calculation of ISR
79
                            // Note that in this way the additional cycles added during simulation will be counted
80
                            // So the displayed value may be 1 to 3 cycles more than 600 cycles
81
                                $display("Number of cycles needed: %d", (finish_time - start_time) / (2 * `HALF_CYCLE));
82
                                start_time = finish_time;
83
                                disable wait_until_done;
84
                        end
85
                end
86
        endtask
87
 
88
        initial begin
89
                $monitor("Time:%4.0f done:%b cres:%h result:%h reset:%h",$time,done,cres,result,reset);
90
                // Test 1
91
                value = 0;  // square number
92
                reset = 1;
93
                clock = 0;
94
                ISR_test(value, cres);
95
                #2000;
96
                @(negedge clock);
97
                reset = 0;
98
                wait_until_done();
99
                @(negedge clock);
100
                // Test 2
101
        reset = 1;
102
                value = 4;  // square number
103
                ISR_test(value, cres);
104
                @(negedge clock);
105
                reset = 0;
106
                wait_until_done();
107
                @(negedge clock);
108
                // Test 3
109
                reset = 1;
110
                value = 121;  // square number
111
                ISR_test(value, cres);
112
                @(negedge clock);
113
                reset = 0;
114
                wait_until_done();
115
                @(negedge clock);
116
                // Test 4
117
        reset = 1;
118
                value = 1000000;  // square number
119
        ISR_test(value, cres);
120
                @(negedge clock);
121
                reset = 0;
122
                wait_until_done();
123
                // Test 5
124
                reset = 1;
125
                value = 130;  // non square number
126
        ISR_test(value, cres);
127
                @(negedge clock);
128
                reset = 0;
129
                wait_until_done();
130
                // Test 6
131
                @(negedge clock);
132
                reset = 1;
133
                value = 2;  // non square number (value to be discarded)
134
        ISR_test(value, cres);
135
                @(negedge clock);
136
                reset = 0;
137
                @(negedge clock);
138
                @(negedge clock);
139
                @(negedge clock);
140
                reset = 1;  // reset is asserted part way through a computation
141
                value = 3;  // non square number (new value to be latched into)
142
        ISR_test(value, cres);
143
                @(negedge clock);
144
                reset = 0;
145
                wait_until_done();
146
                @(negedge clock);
147
                @(negedge clock);
148
                // Test 7
149
                reset = 1;
150
                value = 64'h7FFF_FFFF_FFFF_FFFF;
151
        ISR_test(value, cres);
152
                @(negedge clock);
153
                reset = 0;
154
                wait_until_done();
155
                @(negedge clock);
156
                value_test = 64'hFFFF_FFFF_FFFF_FFFF;
157
                // Short loops
158
                for (j = 0; j < 100; j++) begin
159
                        reset = 1;
160
                        value = value_test;
161
            ISR_test(value, cres);
162
                        @(negedge clock);
163
                        reset = 0;
164
                        wait_until_done();
165
                        value_test = value_test - 64'h0000_0000_7FFF_FFFF;
166
                end
167
                // Random testing
168
                for (j = 0; j < 1000; j++) begin
169
                        reset = 1;
170
                        value = {$random, $random};
171
            ISR_test(value, cres);
172
                        @(negedge clock);
173
                        reset = 0;
174
                        wait_until_done();
175
                end
176
                $display("@@@Passed");
177
                $finish;
178
        end
179
 
180
endmodule

powered by: WebSVN 2.1.0

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