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

Subversion Repositories present

[/] [present/] [trunk/] [32BitIO/] [bench/] [vhdl/] [PresentEncTB.vhd] - Blame information for rev 9

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 gajos
-----------------------------------------------------------------------
2
----                                                               ----
3
---- Present - a lightweight block cipher project                  ----
4
----                                                               ----
5
---- This file is part of the Present - a lightweight block        ----
6
---- cipher project                                                ----
7
---- http://www.http://opencores.org/project,present               ----
8
----                                                               ----
9
---- Description:                                                  ----
10
----     Test bench of Present encoder with 32 bit IO.             ----
11
---- To Do:                                                        ----
12
----                                                               ----
13
---- Author(s):                                                    ----
14
---- - Krzysztof Gajewski, gajos@opencores.org                     ----
15
----                       k.gajewski@gmail.com                    ----
16
----                                                               ----
17
-----------------------------------------------------------------------
18
----                                                               ----
19
---- Copyright (C) 2013 Authors and OPENCORES.ORG                  ----
20
----                                                               ----
21
---- This source file may be used and distributed without          ----
22
---- restriction provided that this copyright statement is not     ----
23
---- removed from the file and that any derivative work contains   ----
24
---- the original copyright notice and the associated disclaimer.  ----
25
----                                                               ----
26
---- This source file is free software; you can redistribute it    ----
27
---- and-or modify it under the terms of the GNU Lesser General    ----
28
---- Public License as published by the Free Software Foundation;  ----
29
---- either version 2.1 of the License, or (at your option) any    ----
30
---- later version.                                                ----
31
----                                                               ----
32
---- This source is distributed in the hope that it will be        ----
33
---- useful, but WITHOUT ANY WARRANTY; without even the implied    ----
34
---- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR       ----
35
---- PURPOSE. See the GNU Lesser General Public License for more   ----
36
---- details.                                                      ----
37
----                                                               ----
38
---- You should have received a copy of the GNU Lesser General     ----
39
---- Public License along with this source; if not, download it    ----
40
---- from http://www.opencores.org/lgpl.shtml                      ----
41
----                                                               ----
42
-----------------------------------------------------------------------
43 2 gajos
LIBRARY ieee;
44
USE ieee.std_logic_1164.ALL;
45
USE std.textio.all;
46
USE work.txt_util.all;
47
USE ieee.std_logic_textio.all;
48
use work.kody.ALL;
49
 
50
ENTITY PresentEncTB IS
51
END PresentEncTB;
52
 
53
ARCHITECTURE behavior OF PresentEncTB IS
54
 
55
    -- Component Declaration for the Unit Under Test (UUT)
56
 
57
    COMPONENT PresentEnc
58
    PORT(
59
         input : IN  std_logic_vector(31 downto 0);
60
         output : OUT  std_logic_vector(31 downto 0);
61
         ctrl : IN  std_logic_vector(3 downto 0);
62
         clk : IN  std_logic;
63
         reset : IN  std_logic;
64
         ready : out  std_logic
65
        );
66
    END COMPONENT;
67
 
68
        -- Clock period definitions
69
   constant clk_period : time := 1ns;
70
        constant p10 : time := clk_period/10;
71
        constant edge : time := clk_period-p10;
72
 
73
   --Inputs
74
   signal input : std_logic_vector(31 downto 0) := (others => '0');
75
   signal ctrl : std_logic_vector(3 downto 0) := (others => '0');
76
   signal clk : std_logic := '0';
77
   signal reset : std_logic := '0';
78
        signal strobe : std_logic;
79
 
80
        --Outputs
81
   signal output : std_logic_vector(31 downto 0);
82
        signal ready : std_logic := '0';
83
 
84
BEGIN
85
 
86
        -- Instantiate the Unit Under Test (UUT)
87
   uut: PresentEnc PORT MAP (
88
          input => input,
89
          output => output,
90
          ctrl => ctrl,
91
          clk => clk,
92
          reset => reset,
93
          ready => ready
94
        );
95
 
96
   -- Clock process definitions
97
   clk_process :process
98
   begin
99
                clk <= '0';
100
                wait for clk_period/2;
101
                clk <= '1';
102
                wait for clk_period/2;
103
   end process;
104
 
105
 
106
   -- Stimulus process
107
   stim_proc: process
108
 
109 5 gajos
        file infile :text is in "input.txt";
110 2 gajos
        variable line_in :line;
111 9 gajos
        variable line_string : string(1 to 12);
112
        variable bytes : std_logic_vector(31 downto 0);
113 2 gajos
        variable bytes2 : std_logic_vector(3 downto 0);
114
        variable xbit : std_logic;
115
 
116
 
117
   begin
118
      -- hold reset state for 100ms.
119
      wait for 100ns;
120
                reset <= '1';
121
                wait for 10ns;
122
                reset <= '0';
123
                wait for 10ns;
124
      -- insert stimulus here 
125 9 gajos
                -- Below loop which iterates through "input.txt" file
126 2 gajos
                        while not (endfile(infile)) loop
127 9 gajos
 
128
                                readline(infile, line_in);  -- info line
129
                                read(line_in, line_string);
130
                                report line_string;
131
 
132
                                readline(infile, line_in);  -- info line
133
                                read(line_in, line_string);
134
                                report line_string;
135
 
136
                                readline(infile, line_in);  -- command line "no operation to prepare encoder"
137 2 gajos
                                hread(line_in, bytes2);
138
                                ctrl <= bytes2;
139
                                wait for clk_period;
140 9 gajos
 
141
                                readline(infile, line_in);  -- info line
142
                                read(line_in, line_string);
143
                                report line_string;
144
 
145
                                readline(infile, line_in);  -- command line for reading key 1/3
146 2 gajos
                                hread(line_in, bytes2);
147
                                ctrl <= bytes2;
148 9 gajos
                                readline(infile, line_in); -- read data
149 2 gajos
                                read(line_in, xbit);
150
                                input <= (others => xbit);
151
                                wait for clk_period;
152 9 gajos
 
153
                                readline(infile, line_in);  -- info line
154
                                read(line_in, line_string);
155
                                report line_string;
156
 
157
                                readline(infile, line_in);  -- command line for reading key 2/3
158 2 gajos
                                hread(line_in, bytes2);
159
                                ctrl <= bytes2;
160 9 gajos
                                readline(infile, line_in);  -- read data
161 2 gajos
                                read(line_in, xbit);
162
                                input <= (others => xbit);
163
                                wait for clk_period;
164 9 gajos
 
165
                                readline(infile, line_in);  -- info line
166
                                read(line_in, line_string);
167
                                report line_string;
168
 
169
                                readline(infile, line_in);  -- command line for reading key 3/3
170 2 gajos
                                hread(line_in, bytes2);
171
                                ctrl <= bytes2;
172 9 gajos
                                readline(infile, line_in);  -- read data
173 2 gajos
                                read(line_in, xbit);
174
                                input <= (others => xbit);
175
                                wait for clk_period;
176 9 gajos
 
177
                                readline(infile, line_in);  -- info line
178
                                read(line_in, line_string);
179
                                report line_string;
180
 
181
                                readline(infile, line_in);  -- command line for reading text 1/2
182 2 gajos
                                hread(line_in, bytes2);
183
                                ctrl <= bytes2;
184 9 gajos
                                readline(infile, line_in);  --read data
185 2 gajos
                                read(line_in, xbit);
186
                                input <= (others => xbit);
187
                                wait for clk_period;
188 9 gajos
 
189
                                readline(infile, line_in);  -- info line
190
                                read(line_in, line_string);
191
                                report line_string;
192
 
193
                                readline(infile, line_in);  -- command line for reading text 2/2
194 2 gajos
                                hread(line_in, bytes2);
195
                                ctrl <= bytes2;
196 9 gajos
                                readline(infile, line_in);  --read data
197 2 gajos
                                read(line_in, xbit);
198
                                input <= (others => xbit);
199
                                wait for clk_period;
200 9 gajos
 
201
                                readline(infile, line_in);  -- info line
202
                                read(line_in, line_string);
203
                                report line_string;
204
 
205
                                readline(infile, line_in);  -- command line for coding input text to ciphertext
206 2 gajos
                                hread(line_in, bytes2);
207
                                ctrl <= bytes2;
208
                                wait for clk_period*33;
209 9 gajos
 
210
                                readline(infile, line_in);  -- info line
211
                                read(line_in, line_string);
212
                                report line_string;
213
 
214
                                readline(infile, line_in);  -- command line for retrieving output 1/2
215 2 gajos
                                hread(line_in, bytes2);
216
                                ctrl <= bytes2;
217
                                wait for clk_period;
218 9 gajos
 
219
                                readline(infile, line_in);  -- retrieve expected value 1/2 from input file
220
                                hread(line_in, bytes);
221
 
222
                                if output /= bytes then
223
                                    report "RESULT MISMATCH! Least significant bytes failed" severity ERROR;
224
                               assert false severity failure;
225
                                else
226
                                    report "Least significant bytes successful" severity note;
227
                                end if;
228
 
229
                                readline(infile, line_in);  -- info line
230
                                read(line_in, line_string);
231
                                report line_string;
232
 
233
                                readline(infile, line_in);  -- command line for retrieving output 2/2
234 2 gajos
                                hread(line_in, bytes2);
235
                                ctrl <= bytes2;
236
                                wait for clk_period*2;
237 9 gajos
 
238
                                readline(infile, line_in);  -- retrieve expected value 3/2 from input file
239
                                hread(line_in, bytes);
240
 
241
                                if output /= bytes then
242
                                    report "RESULT MISMATCH! Most significant bytes failed" severity ERROR;
243
                               assert false severity failure;
244
                                else
245
                                    report "Most significant bytes successful" severity note;
246
                                end if;
247
 
248
                                report "";      -- "new line"
249
 
250 2 gajos
                        end loop;
251
                assert false severity failure;
252
   end process;
253
 
254
        strobe <= TRANSPORT clk AFTER edge;
255
 
256
        outs: PROCESS (strobe)
257
                variable str :string(1 to 29);
258
                variable lineout :line;
259
                variable init_file :std_logic := '1';
260 5 gajos
                file outfile :text is out "output.txt";
261 2 gajos
 
262 5 gajos
                -------- conversion function: std_logic_vector => character --------
263 2 gajos
                function conv_to_hex_char (sig: std_logic_vector(3 downto 0)) RETURN character IS
264
                        begin
265
                        case sig is
266
                                when "0000" => return '0';
267
                                when "0001" => return '1';
268
                                when "0010" => return '2';
269
                                when "0011" => return '3';
270
                                when "0100" => return '4';
271
                                when "0101" => return '5';
272
                                when "0110" => return '6';
273
                                when "0111" => return '7';
274
                                when "1000" => return '8';
275
                                when "1001" => return '9';
276
                                when "1010" => return 'A';
277
                                when "1011" => return 'B';
278
                                when "1100" => return 'C';
279
                                when "1101" => return 'D';
280
                                when "1110" => return 'E';
281
                                when others => return 'F';
282
                        end case;
283
                end conv_to_hex_char;
284
 
285 5 gajos
                -------- conversion function: std_logic => character --------
286 2 gajos
                function conv_to_char (sig: std_logic) RETURN character IS
287
                        begin
288
                        case sig is
289
                                when '1' => return '1';
290
                                when '0' => return '0';
291
                                when 'Z' => return 'Z';
292
                                when others => return 'X';
293
                        end case;
294
                end conv_to_char;
295
 
296 5 gajos
                -------- conversion function: std_logic_vector => string --------
297 2 gajos
                function conv_to_string (inp: std_logic_vector; length: integer) RETURN string IS
298
                        variable x : integer := length/4;
299
                        variable s : string(1 to x);
300
                        begin
301
                                for i in 0 to (x-1) loop
302
                                s(x-i) := conv_to_hex_char(inp(4*i+3 downto 4*i));
303
                                end loop;
304
                        return s;
305
                end conv_to_string;
306
 
307
                -------------------------------------
308
                begin
309 5 gajos
                -------- output file header (columns) --------
310 2 gajos
                        if init_file = '1' then
311
                                str:="clk                          ";
312
                                write(lineout,str); writeline(outfile,lineout);
313
                                str:="| reset                      ";
314
                                write(lineout,str); writeline(outfile,lineout);
315
                                str:="| | ready                    ";
316
                                write(lineout,str); writeline(outfile,lineout);
317
                                str:="| | | ctrl                   ";
318
                                write(lineout,str); writeline(outfile,lineout);
319
                                str:="| | | | input                ";
320
                                write(lineout,str); writeline(outfile,lineout);
321
                                str:="| | | | |        output      ";
322
                                write(lineout,str); writeline(outfile,lineout);
323
                                str:="| | | | |        |           ";
324
                                write(lineout,str); writeline(outfile,lineout);
325
                                init_file := '0';
326
                        end if;
327
 
328 9 gajos
                -------- write to output "output" --------
329 2 gajos
                        if (strobe'EVENT and strobe='0') then
330
                                str := (others => ' ');
331
                                str(1) := conv_to_char(clk);
332
                                str(2) := '|';
333
                                str(3) := conv_to_char(reset);
334
                                str(4) := '|';
335
                                str(5) := conv_to_char(ready);
336
                                str(6) := '|';
337
                                str(7) := conv_to_hex_char(ctrl);
338
                                str(8) := '|';
339
                                str(9 to 16) := conv_to_string(input,32);
340
                                str(17) := '|';
341
                                str(18 to 25) := conv_to_string(output,32);
342
                                str(26) := '|';
343
                                write(lineout,str);
344
                                writeline(outfile,lineout);
345
                        end if;
346
        end process outs;
347
end;

powered by: WebSVN 2.1.0

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