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

Subversion Repositories aes_pipe

[/] [aes_pipe/] [trunk/] [bench/] [vhdl/] [tb_aes.vhdl] - Blame information for rev 12

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 subhasis25
----------------------------------------------------------------------
2
----                                                              ----
3 9 subhasis25
---- Pipelined Aes IP Core                                        ----
4
----                                                              ----
5
---- This file is part of the Pipelined AES project               ----
6
---- http://www.opencores.org/cores/aes_pipe/                     ----
7
----                                                              ----
8
---- Description                                                  ----
9
---- Implementation of AES IP core according to                   ----
10
---- FIPS PUB 197 specification document.                         ----
11
----                                                              ----
12
---- To Do:                                                       ----
13
----   -                                                          ----
14
----                                                              ----
15
---- Author:                                                      ----
16
----      - Subhasis Das, subhasis256@gmail.com                   ----
17
----                                                              ----
18
----------------------------------------------------------------------
19
----                                                              ----
20
---- Copyright (C) 2009 Authors and OPENCORES.ORG                 ----
21
----                                                              ----
22 2 subhasis25
---- This source file may be used and distributed without         ----
23
---- restriction provided that this copyright statement is not    ----
24 9 subhasis25
---- removed from the file and that any derivative work contains ----
25 2 subhasis25
---- the original copyright notice and the associated disclaimer. ----
26
----                                                              ----
27
---- This source file is free software; you can redistribute it   ----
28
---- and/or modify it under the terms of the GNU Lesser General   ----
29
---- Public License as published by the Free Software Foundation; ----
30
---- either version 2.1 of the License, or (at your option) any   ----
31
---- later version.                                               ----
32
----                                                              ----
33
---- This source is distributed in the hope that it will be       ----
34
---- useful, but WITHOUT ANY WARRANTY; without even the implied   ----
35
---- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ----
36 9 subhasis25
---- PURPOSE. See the GNU Lesser General Public License for more ----
37 2 subhasis25
---- details.                                                     ----
38
----                                                              ----
39
---- You should have received a copy of the GNU Lesser General    ----
40
---- Public License along with this source; if not, download it   ----
41 9 subhasis25
---- from http://www.opencores.org/lgpl.shtml                     ----
42 2 subhasis25
----                                                              ----
43
----------------------------------------------------------------------
44
------------------------------------------------------
45
-- Project: AESFast
46
-- Author: Subhasis
47 9 subhasis25
-- Last Modified: 25/03/10
48 2 subhasis25
-- Email: subhasis256@gmail.com
49
--
50
-- TODO: Test with NIST test vectors
51
------------------------------------------------------
52
--
53
-- Description: Testbench for AESFast
54 9 subhasis25
-- Takes in data and keys from ../src/vectors.dat
55
-- Takes in true output values from ../src/cipher.dat
56
-- Writes all the output to ../log/output.log
57 2 subhasis25
------------------------------------------------------
58
 
59
library IEEE;
60
use IEEE.std_logic_1164.all;
61 9 subhasis25
use IEEE.std_logic_textio.all;
62
use std.textio.all;
63 2 subhasis25
 
64
library work;
65
use work.aes_pkg.all;
66
 
67
entity tb_aes is
68
end tb_aes;
69
 
70
architecture rtl of tb_aes is
71 9 subhasis25
signal clk: std_logic; -- clock
72 2 subhasis25
signal plaintext: datablock;
73
signal key: datablock;
74
signal cipher: datablock;
75 9 subhasis25
signal rst: std_logic; -- reset input
76 12 subhasis25
signal op_start: std_logic; -- signal that output started
77
signal sim_end: std_logic := '0'; -- signal that simulation ended
78 9 subhasis25
constant clk_period: time := 10 ns;
79 2 subhasis25
 
80 5 subhasis25
component aes_top is
81 2 subhasis25
port(
82 5 subhasis25
        clk_i: in std_logic;
83 9 subhasis25
        rst_i: in std_logic;
84 5 subhasis25
        plaintext_i: in datablock;
85
        keyblock_i: in datablock;
86
        ciphertext_o: out datablock
87 2 subhasis25
        );
88
end component;
89
 
90
begin
91 9 subhasis25
        -- The wiring of the top module
92
        DUT: aes_top port map(
93
                                                 clk_i => clk,
94
                                                 rst_i => rst,
95
                                                 plaintext_i => plaintext,
96
                                                 keyblock_i => key,
97
                                                 ciphertext_o => cipher
98
                                                 );
99
        -- Generate clock
100 2 subhasis25
        gen_clk: process
101
        begin
102 12 subhasis25
                if(sim_end = '0') then
103
                        clk <= '1';
104
                        wait for clk_period/2;
105
                        clk <= '0';
106
                        wait for clk_period/2;
107
                else
108
                        wait;
109
                end if;
110 2 subhasis25
        end process;
111 9 subhasis25
        -- Generate Reset
112
        gen_rst: process
113
        begin
114
                rst <= '1';
115
                wait for clk_period/2; -- generate reset
116
                rst <= '0';
117
                wait;
118
        end process;
119 2 subhasis25
 
120 9 subhasis25
        -- generate the inputs and check against expected output
121 2 subhasis25
        gen_in: process
122 9 subhasis25
        file testfile: text open read_mode is "../src/vectors.dat";
123
        variable line_in: line;
124 12 subhasis25
        variable plaintext_block, key_block: std_logic_vector(127 downto 0);
125 2 subhasis25
        begin
126 9 subhasis25
                if(endfile(testfile)) then
127
                        file_close(testfile);
128
                        wait;
129
                end if;
130 12 subhasis25
                readline(testfile, line_in);
131
                hread(line_in, plaintext_block);
132
                hread(line_in, key_block);
133 9 subhasis25
 
134
                for i in 3 downto 0 loop
135
                        for j in 3 downto 0 loop
136 12 subhasis25
                                plaintext(3-j,3-i) <= plaintext_block((i*32 + j*8 + 7) downto (i*32 + j*8));
137 9 subhasis25
                        end loop;
138
                end loop;
139
                for i in 3 downto 0 loop
140
                        for j in 3 downto 0 loop
141 12 subhasis25
                                key(3-j,3-i) <= key_block((i*32 + j*8 + 7) downto (i*32 + j*8));
142 9 subhasis25
                        end loop;
143
                end loop;
144
 
145
                wait for clk_period;
146
        end process;
147
 
148
        -- Generate a signal to indicate that valid output has begun
149
        op_begin: process
150
        begin
151
                wait for 30*clk_period;
152
                wait for clk_period/2;
153
                op_start <= '1';
154 2 subhasis25
                wait;
155
        end process;
156 9 subhasis25
 
157
        -- Compare output with actual output file
158
        op_chk: process
159 12 subhasis25
        file opfile: text open read_mode is "../src/cipher.dat";
160
        file logfile: text open write_mode is "../log/output.log";
161
        variable line_in, line_out, line_out_file: line;
162
        variable exp_cipher_block: std_logic_vector(127 downto 0);
163 9 subhasis25
        variable succeded: boolean;
164 12 subhasis25
        variable all_ok: boolean := true;
165 9 subhasis25
        begin
166
                -- if required cycles have passed
167
                if(op_start = '1') then
168 12 subhasis25
                        if(endfile(opfile)) then -- end of simulation
169
                                file_close(opfile);
170
                                if(all_ok = true) then
171
                                        write(line_out, string'("OK"));
172
                                        writeline(OUTPUT, line_out);
173
                                        write(line_out_file, string'("OK"));
174
                                        writeline(logfile, line_out_file);
175
                                else
176
                                        write(line_out, string'("FAIL"));
177
                                        writeline(OUTPUT, line_out);
178
                                        write(line_out_file, string'("FAIL"));
179
                                        writeline(logfile, line_out_file);
180
                                end if;
181
                                sim_end <= '1';
182 9 subhasis25
                                wait;
183
                        end if;
184
                        succeded := true;
185 12 subhasis25
                        readline(opfile, line_in); -- read in one expected result
186
                        hread(line_in, exp_cipher_block); -- read in one byte
187 9 subhasis25
                        for i in 3 downto 0 loop
188
                                for j in 3 downto 0 loop
189 12 subhasis25
                                        if(exp_cipher_block((i*32 + j*8 + 7) downto (i*32 + j*8)) /= cipher(3-j,3-i)) then
190 9 subhasis25
                                                succeded := false; -- check failed
191 12 subhasis25
                                                all_ok := false;
192 9 subhasis25
                                        end if;
193
                                end loop;
194
                        end loop;
195
                        -- writing the output line
196
                        for i in 3 downto 0 loop
197
                                for j in 3 downto 0 loop
198 12 subhasis25
                                        hwrite(line_out, cipher(3-j,3-i));
199 9 subhasis25
                                        hwrite(line_out_file, cipher(3-j,3-i));
200
                                end loop;
201
                        end loop;
202 12 subhasis25
                        write(line_out, ' ');
203 9 subhasis25
                        write(line_out_file, ' ');
204
                        -- writing the comparison result
205
                        write(line_out, succeded);
206
                        writeline(OUTPUT, line_out);
207 12 subhasis25
                        write(line_out_file, succeded);
208
                        writeline(logfile, line_out_file);
209 9 subhasis25
                end if;
210
                wait for clk_period;
211
        end process;
212 2 subhasis25
end rtl;

powered by: WebSVN 2.1.0

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