File size: 6,798 Bytes
8129903
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

`define FILE_COUNT 10


module tb_random_data_source (
    input  wire        clk,
    // output : AXI stream
    input  wire        tready,
    output reg         tvalid,
    output reg  [ 7:0] tdata,
    output reg         tlast
);



initial tvalid = 1'b0;
initial tdata  = 1'b0;
initial tlast  = 1'b0;



//---------------------------------------------------------------------------------------------------------------------------------------------------------------
// function : generate random unsigned integer
//---------------------------------------------------------------------------------------------------------------------------------------------------------------
function  [31:0] randuint;
    input [31:0] min;
    input [31:0] max;
begin
    randuint = $random;
    if ( min != 0 || max != 'hFFFFFFFF )
        randuint = (randuint % (1+max-min)) + min;
end
endfunction



//---------------------------------------------------------------------------------------------------------------------------------------------------------------
// tasks : send random data
//---------------------------------------------------------------------------------------------------------------------------------------------------------------
task gen_chunk_rand;
    input [31:0] max_bubble_cnt;
    input [31:0] length;
    input [ 7:0] min;
    input [ 7:0] max;
begin
    while (length>0) begin
        @ (posedge clk);
        if (tready) begin
            repeat (randuint(0, max_bubble_cnt)) begin
                tvalid <= 1'b0;
                @ (posedge clk);
            end
        end
        if (~tvalid | tready) begin
            length = length - 1;
            tvalid <= 1'b1;
            tlast  <= 1'b0;
            tdata <= randuint(min, max);
        end
    end
end
endtask


task gen_chunk_sqrt4_rand;
    input [31:0] max_bubble_cnt;
    input [31:0] length;
begin
    while (length>0) begin
        @ (posedge clk);
        if (tready) begin
            repeat (randuint(0, max_bubble_cnt)) begin
                tvalid <= 1'b0;
                @ (posedge clk);
            end
        end
        if (~tvalid | tready) begin
            length = length - 1;
            tvalid <= 1'b1;
            tlast  <= 1'b0;
            tdata  <= $sqrt($sqrt(randuint(0, 'hFFFFFFFF)));
        end
    end
end
endtask


task gen_chunk_inc;
    input [31:0] max_bubble_cnt;
    input [31:0] length;
    input [ 7:0] min;
    input [ 7:0] max;
begin
    while (length>0) begin
        @ (posedge clk);
        if (tready) begin
            repeat (randuint(0, max_bubble_cnt)) begin
                tvalid <= 1'b0;
                @ (posedge clk);
            end
        end
        if (~tvalid | tready) begin
            length = length - 1;
            tvalid <= 1'b1;
            tlast  <= 1'b0;
            if ( randuint(0,1) )
                tdata <= tdata + randuint(min, max);
            else
                tdata <= tdata - randuint(min, max);
        end
    end
end
endtask


task gen_chunk_scatter;
    input [31:0] max_bubble_cnt;
    input [31:0] length;
    input [31:0] prob;
begin
    while (length>0) begin
        @ (posedge clk);
        if (tready) begin
            repeat (randuint(0, max_bubble_cnt)) begin
                tvalid <= 1'b0;
                @ (posedge clk);
            end
        end
        if (~tvalid | tready) begin
            length = length - 1;
            tvalid <= 1'b1;
            tlast  <= 1'b0;
            if ( randuint(0, prob) == 0 )
                tdata <= randuint(1, 255);
            else
                tdata <= 8'h0;
        end
    end
end
endtask


task gen_stream;
    input [31:0] length;
    input [31:0] max_bubble_cnt;
    reg   [31:0] remain_length;
    reg   [31:0] chunk_length;
    reg   [ 7:0] min;
    reg   [ 7:0] max;
begin
    if (length > 0) begin
        remain_length = length - 1;
        
        while (remain_length > 0) begin
            chunk_length   = randuint(1, 10000);
            if (chunk_length > remain_length)
                chunk_length = remain_length;
            remain_length = remain_length - chunk_length;
            
            case ( randuint(0, 3) )
                0       : begin
                    min = randuint(0  , 255);
                    max = randuint(min, 255);
                    gen_chunk_rand      (max_bubble_cnt, chunk_length, min, max);
                end
                1       :
                    gen_chunk_sqrt4_rand(max_bubble_cnt, chunk_length);
                2       : begin
                    min = randuint(1  , 3);
                    max = randuint(min, 4);
                    gen_chunk_inc       (max_bubble_cnt, chunk_length, min, max);
                end
                default :
                    gen_chunk_scatter   (max_bubble_cnt, chunk_length, randuint(1, 600));
            endcase
        end
        
        remain_length = 1;
        while (remain_length>0) begin
            @ (posedge clk);
            if (~tvalid | tready) begin
                remain_length = 0;
                tvalid <= 1'b1;
                tlast  <= 1'b1;
                tdata  <= randuint(0,255);
            end
        end
    end
end
endtask



//---------------------------------------------------------------------------------------------------------------------------------------------------------------
// send random data
//---------------------------------------------------------------------------------------------------------------------------------------------------------------
initial begin
    repeat (1000) @ (posedge clk);
    
    repeat (`FILE_COUNT) begin
        case ( randuint(0,7) )
            0       : gen_stream(randuint(32, 1000000)    , randuint(0,3) );
            1       : gen_stream(32  +randuint(0,2)       , randuint(0,3) );
            2       : gen_stream(4095+randuint(0,2)       , randuint(0,3) );
            3       : gen_stream(randuint(1, 2)*16384 - 1 , randuint(0,3) );
            4       : gen_stream(randuint(1, 2)*16384     , randuint(0,3) );
            5       : gen_stream(randuint(1, 2)*16384 + 1 , randuint(0,3) );
            6       : gen_stream(randuint(9,20)*16384 + 1 , randuint(0,3) );
            7       : gen_stream(randuint(32, 32768)      , randuint(50,100) );
            8       : gen_stream(randuint(1,31)           , randuint(0,3) );     // the stream shorter than 32 will be ignore
        endcase
    end
    
    @ (posedge clk);
    while ( !(~tvalid | tready) ) begin
        @ (posedge clk);
    end
    tvalid <= 1'b0;
end


endmodule