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

Subversion Repositories aes_highthroughput_lowarea

[/] [aes_highthroughput_lowarea/] [trunk/] [verilog/] [bench/] [tb_kat.v] - Blame information for rev 7

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

Line No. Rev Author Line
1 7 motilito
`timescale 1ns / 10ps
2
module test ();
3
 
4
// define input list file name 
5
`define IN_FILE         "KAT_files.txt"
6
 
7
// global definitions 
8
`define EOF                             -1
9
`define CHAR_CR                 8'h0d
10
`define CHAR_LF                 8'h0a
11
`define CHAR_0                  8'h30
12
`define CHAR_9                  8'h39
13
`define CHAR_Eq                 8'h3D
14
`define CHAR_A                  8'h41
15
`define CHAR_F                  8'h46
16
`define CHAR_Z                  8'h5A
17
`define CHAR_a                  8'h61
18
`define CHAR_f                  8'h66
19
`define CHAR_z                  8'h7A
20
 
21
// test bench signals 
22
reg clk;
23
reg reset;
24
reg [255:0] in_file_name;
25
reg key_start, enc_dec, data_in_valid, enable, test_start;
26
reg [255:0] key_in;
27
reg [1:0] key_mode;
28
reg [127:0] data_in, exp_data_out, init_vec, tmp_val;
29
wire [127:0] data_out;
30
wire key_ready, ready_out;
31
reg [15:0] param_name;
32
integer listfid, infid, intmp, tst_count, key_len;
33
 
34
// function to read a line from an input file 
35
function [255:0] fgetl;
36
input [31:0] fileid;
37
integer intint;
38
reg [255:0] outline;
39
begin
40
        // init output line 
41
        outline = 0;
42
 
43
        // pass over any CR and LF characters 
44
        intint = $fgetc(fileid);
45
        while (((intint == `CHAR_CR) || (intint == `CHAR_LF)) && (intint != `EOF))
46
                intint = $fgetc(fileid);
47
 
48
        // update output line 
49
        while ((intint != `CHAR_CR) && (intint != `CHAR_LF) && (intint != `EOF))
50
        begin
51
                outline[255:8] = outline[247:0];
52
                outline[7:0] = intint;
53
                intint = $fgetc(fileid);
54
        end
55
 
56
        // return the line read 
57
        fgetl = outline;
58
end
59
endfunction
60
 
61
// function to read the next field name from the input file 
62
function [15:0] fgetfield;
63
input [31:0] fileid;
64
integer intint;
65
reg [15:0] outfield;
66
begin
67
        // search for the start of the new line 
68
        intint = $fgetc(fileid);
69
        while ((intint != `CHAR_CR) && (intint != `CHAR_LF) && (intint != `EOF))
70
                intint = $fgetc(fileid);
71
        while (((intint < `CHAR_A) || (intint > `CHAR_Z)) && (intint != `EOF))
72
                intint = $fgetc(fileid);
73
 
74
        // the first two characters of the new line are the next field name 
75
        outfield[15:8] = intint;
76
        intint = $fgetc(fileid);
77
        outfield[7:0] = intint;
78
 
79
        // return the result 
80
        fgetfield = outfield;
81
end
82
endfunction
83
 
84
// function to read the file to the start of the parameter value 
85
function [7:0] fgetparam;
86
input [31:0] fileid;
87
integer intint;
88
begin
89
        // search for the equal sign 
90
        intint = $fgetc(fileid);
91
        while (intint != `CHAR_Eq)
92
                intint = $fgetc(fileid);
93
        // read first char of parameter 
94
        while ((intint < `CHAR_0) || ((intint > `CHAR_9) && (intint < `CHAR_A)) ||
95
                        ((intint > `CHAR_Z) && (intint < `CHAR_a)) || (intint > `CHAR_z))
96
                intint = $fgetc(fileid);
97
 
98
        // return the read character 
99
        fgetparam = intint;
100
end
101
endfunction
102
 
103
// function to convert a character to its HEX value 
104
function [7:0] char2val;
105
input [7:0] char_val;
106
integer out_val;
107
begin
108
        if ((char_val >= `CHAR_0) && (char_val <= `CHAR_9))
109
                out_val = char_val - `CHAR_0;
110
        else if ((char_val >= `CHAR_A) && (char_val <= `CHAR_F))
111
                out_val = char_val - `CHAR_A + 'd10;
112
        else if ((char_val >= `CHAR_a) && (char_val <= `CHAR_f))
113
                out_val = char_val - `CHAR_a + 'd10;
114
        else
115
                out_val = 0;
116
 
117
        // return the resulting value 
118
        char2val = out_val;
119
end
120
endfunction
121
 
122
// global clock generator 
123
initial         clk = 1'b1;
124
always          #10 clk = ~clk;
125
 
126
// gloabl reset generator 
127
initial
128
begin
129
        reset = 1'b1;
130
        #100;
131
        reset = 1'b0;
132
end
133
 
134
// cosmetics 
135
initial
136
begin
137
        // announce start of simulation 
138
        $display("");
139
        $display("-------------------------------------");
140
        $display("        AES_HT_LA Simulation");
141
        $display("-------------------------------------");
142
        $display("");
143
 
144
        // VCD dump 
145
        $dumpfile("test.vcd");
146
        $dumpvars(0, test);
147
        $display("");
148
end
149
 
150
// main test bench contorl module 
151
initial
152
begin
153
        // signals reset values 
154
        enc_dec = 1'b0;                 // 0: encryption; 1: decryption
155
        key_mode = 'b0;                 // 0: 128; 1: 192; 2: 256 
156
        key_in = 'b0;
157
        key_start = 1'b0;
158
        data_in_valid = 1'b0;
159
        data_in = 'b0;
160
        enable = 1;
161
        test_start = 0;
162
        @(posedge clk);
163
 
164
        // wait for global reset 
165
        wait (reset == 1'b0);
166
        repeat (10) @(posedge clk);
167
 
168
        // open input list file 
169
        listfid = $fopen(`IN_FILE, "rb");
170
 
171
        // read first input filename 
172
        in_file_name = fgetl(listfid);
173
 
174
        // loop through input files 
175
        while (in_file_name != 0)
176
        begin
177
                // announce start of simulation for the current file 
178
                $display("Starting simulation for input file: %0s", in_file_name);
179
                $display("--------------------------------------------------------------------------");
180
                $display("");
181
 
182
                // open current simulation input file 
183
                infid = $fopen(in_file_name, "rb");
184
 
185
                // read core mode for the current file 
186
                intmp = $fgetc(infid);  // first char is "[" 
187
                intmp = $fgetc(infid);  // second char should be either "E" or "D" 
188
                // check read character for mode of operation 
189
                if (intmp == "E")
190
                        // set flag accordingly 
191
                        enc_dec = 1'b0;
192
                else if (intmp == "D")
193
                        // set flag accordingly 
194
                        enc_dec = 1'b1;
195
                else
196
                begin
197
                        // no valid mode was found, announce error and quit simulation 
198
                        $display("ERROR: Simulation mode could not be determined!");
199
                        $finish;
200
                end
201
 
202
                // repeat reading the file till end of file 
203
                param_name = fgetfield(infid);
204
                while (param_name != `EOF)
205
                begin
206
                        // clear test start flag 
207
                        test_start = 0;
208
 
209
                        // check read parameter name  
210
                        if (param_name == "CO")
211
                        begin
212
                                // init test count 
213
                                tst_count = 0;
214
                                // get file pointer to the start of value 
215
                                intmp = fgetparam(infid);
216
                                // update test count value 
217
                                while ((intmp >= `CHAR_0) && (intmp <= `CHAR_9))
218
                                begin
219
                                        tst_count = (tst_count * 10) + (intmp - `CHAR_0);
220
                                        intmp = $fgetc(infid);
221
                                end
222
                        end
223
                        else if (param_name == "KE")
224
                        begin
225
                                // init key value and length 
226
                                key_in = 0;
227
                                key_len = 0;
228
 
229
                                // get file pointer to the start of value 
230
                                intmp = fgetparam(infid);
231
 
232
                                // update key value & length 
233
                                while (((intmp >= `CHAR_0) && (intmp <= `CHAR_9)) ||
234
                                       ((intmp >= `CHAR_A) && (intmp <= `CHAR_F)) ||
235
                                       ((intmp >= `CHAR_a) && (intmp <= `CHAR_f)))
236
                                begin
237
                                        key_in[255:4] = key_in[251:0];
238
                                        key_in[3:0] = char2val(intmp);
239
                                        key_len = key_len + 4;
240
                                        intmp = $fgetc(infid);
241
                                end
242
 
243
                                // check key length to see if it is legal and if the key needs zero padding 
244
                                if (key_len == 'd128)
245
                                begin
246
                                        // update key value and mode 
247
                                        key_in = {key_in[127:0], 128'b0};
248
                                        key_mode = 2'd0;
249
                                end
250
                                else if (key_len == 'd192)
251
                                begin
252
                                        // update key value and mode 
253
                                        key_in = {key_in[191:0], 64'b0};
254
                                        key_mode = 2'd1;
255
                                end
256
                                else if (key_len == 'd256)
257
                                begin
258
                                        // update key mode 
259
                                        key_mode = 2'd2;
260
                                end
261
                                else
262
                                begin
263
                                        // illegal key length error 
264
                                        $display("ERROR: Illegal key length at test %0d (%0d)", tst_count, key_len);
265
                                        $finish;
266
                                end
267
                        end
268
                        else if (param_name == "IV")
269
                        begin
270
                                // init init vector value 
271
                                init_vec = 0;
272
 
273
                                // get file pointer to the start of value 
274
                                intmp = fgetparam(infid);
275
                                // update init vector value 
276
                                while (((intmp >= `CHAR_0) && (intmp <= `CHAR_9)) ||
277
                                       ((intmp >= `CHAR_A) && (intmp <= `CHAR_F)) ||
278
                                       ((intmp >= `CHAR_a) && (intmp <= `CHAR_f)))
279
                                begin
280
                                        init_vec[127:4] = init_vec[123:0];
281
                                        init_vec[3:0] = char2val(intmp);
282
                                        intmp = $fgetc(infid);
283
                                end
284
                        end
285
                        else if (param_name == "CI")
286
                        begin
287
                                // init temp value 
288
                                tmp_val = 0;
289
 
290
                                // get file pointer to the start of value 
291
                                intmp = fgetparam(infid);
292
                                // update temp value 
293
                                while (((intmp >= `CHAR_0) && (intmp <= `CHAR_9)) ||
294
                                       ((intmp >= `CHAR_A) && (intmp <= `CHAR_F)) ||
295
                                       ((intmp >= `CHAR_a) && (intmp <= `CHAR_f)))
296
                                begin
297
                                        tmp_val[127:4] = tmp_val[123:0];
298
                                        tmp_val[3:0] = char2val(intmp);
299
                                        intmp = $fgetc(infid);
300
                                end
301
 
302
                                // check simulation mode to determine if this is the last value and if 
303
                                // it is the data input or the expected data 
304
                                if (enc_dec == 1'b0)
305
                                begin
306
                                        // for encryption the CIPHERTEXT is the expected result and the 
307
                                        // simulation should start 
308
                                        exp_data_out = tmp_val;
309
                                        test_start = 1'b1;
310
                                end
311
                                else
312
                                        // for decryption the CIPHERTEXT is the input data 
313
                                        data_in = tmp_val;
314
                        end
315
                        else if (param_name == "PL")
316
                        begin
317
                                // init temp value 
318
                                tmp_val = 0;
319
 
320
                                // get file pointer to the start of value 
321
                                intmp = fgetparam(infid);
322
                                // update temp value 
323
                                while (((intmp >= `CHAR_0) && (intmp <= `CHAR_9)) ||
324
                                       ((intmp >= `CHAR_A) && (intmp <= `CHAR_F)) ||
325
                                       ((intmp >= `CHAR_a) && (intmp <= `CHAR_f)))
326
                                begin
327
                                        tmp_val[127:4] = tmp_val[123:0];
328
                                        tmp_val[3:0] = char2val(intmp);
329
                                        intmp = $fgetc(infid);
330
                                end
331
 
332
                                // check simulation mode to determine if this is the last value and if 
333
                                // it is the data input or the expected data 
334
                                if (enc_dec == 1'b0)
335
                                        // for encryption the PLAINTEXT is the input data 
336
                                        data_in = tmp_val;
337
                                else
338
                                begin
339
                                        // for decryption the PLAINTEXT is the expected result and the 
340
                                        // simulation should start 
341
                                        exp_data_out = tmp_val;
342
                                        test_start = 1'b1;
343
                                end
344
                        end
345
                        else
346
                        begin
347
                                // no matching parameter was found 
348
                                $display("ERROR: Could not find a matching parameter after test %0d", tst_count);
349
                                $finish;
350
                        end
351
 
352
                        // check if simulation should start 
353
                        if (test_start)
354
                        begin
355
                                // run core simulation 
356
                                repeat (10) @(posedge clk);
357
                                // update input key 
358
                                key_start <= 1'b1;
359
                                @ (posedge clk);
360
                                key_start <= 1'b0;
361
                                @ (posedge clk);
362
                                // wait for key to be ready 
363
                                while (!key_ready)
364
                                        @(posedge clk);
365
                                // sign input data is valid 
366
                                data_in_valid <= 1'b1;
367
                                @(posedge clk);
368
                            data_in_valid <= 1'b0;
369
                                repeat (3) @ (posedge clk);
370
                                // wait for result to be ready 
371
                                while (!data_out_valid)
372
                                        @ (posedge clk);
373
                                @ (posedge clk);
374
                                // check expected result 
375
                                if (exp_data_out != data_out)
376
                                begin
377
                                        // data output error 
378
                                        $display("ERROR: Expected data output error at test %0d", tst_count);
379
                                        repeat (10) @(posedge clk);
380
                                        $finish;
381
                                end
382
                                else
383
                                begin
384
                                        $display("Test finished OK!");
385
                                        $display("");
386
                                end
387
                        end
388
 
389
                        // read next parameter name 
390
                        param_name = fgetfield(infid);
391
                end
392
 
393
                // close input file 
394
                $fclose(infid);
395
 
396
                // read next input filename 
397
                in_file_name = fgetl(listfid);
398
        end
399
 
400
        // close input list file 
401
        $fclose(listfid);
402
        // finish simulation 
403
        $finish;
404
end
405
 
406
aes dut(
407
   .clk(clk),
408
   .reset(reset),
409
   .i_start(key_start),
410
   .i_enable(enable), //TBD
411
   .i_ende(enc_dec),
412
   .i_key(key_in),
413
   .i_key_mode(key_mode),
414
   .i_data(data_in),
415
   .i_data_valid(data_in_valid),
416
   .o_ready(ready_out),
417
   .o_data(data_out),
418
   .o_data_valid(data_out_valid),
419
   .o_key_ready(key_ready)
420
);
421
 
422
// display mode of operation, input key length and value 
423
always @ (posedge clk)
424
        if (key_start)
425
        begin
426
                // display mode of operation 
427
                if (enc_dec)
428
                        $display("Decryption test, count %0d, in file %0s", tst_count, in_file_name);
429
                else
430
                        $display("Encryption test, count %0d, in file %0s", tst_count, in_file_name);
431
 
432
                // display key size 
433
                case (key_mode)
434
                        2'b00:  $display("Key size is 128 bits");
435
                        2'b01:  $display("Key size is 192 bits");
436
                        2'b10:  $display("Key size is 256 bits");
437
                        2'b11:  $display("ERROR: Illegal key size");
438
                endcase
439
                // display key value 
440
                $display("Key In:      %16h",key_in);
441
        end
442
 
443
// display input data 
444
always @ (posedge clk)
445
        if (data_in_valid)
446
                $display("Data In:     %16h",data_in);
447
 
448
// display output data 
449
always @ (posedge clk)
450
        if (data_out_valid)
451
                $display("Data Out:    %16h",data_out);
452
 
453
endmodule

powered by: WebSVN 2.1.0

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