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

Subversion Repositories mod_sim_exp

[/] [mod_sim_exp/] [trunk/] [bench/] [vhdl/] [multiplier_tb.vhd] - Blame information for rev 66

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

Line No. Rev Author Line
1 26 JonasDC
----------------------------------------------------------------------  
2 43 JonasDC
----  multiplier_tb                                               ---- 
3 26 JonasDC
----                                                              ---- 
4
----  This file is part of the                                    ----
5
----    Modular Simultaneous Exponentiation Core project          ---- 
6
----    http://www.opencores.org/cores/mod_sim_exp/               ---- 
7
----                                                              ---- 
8
----  Description                                                 ---- 
9 43 JonasDC
----    testbench for the Montgomery multiplier                   ----
10
----    Performs some multiplications to verify the design        ----
11
----    Takes input parameters from sim_mult_input.txt and writes ----
12
----    result and output to sim_mult_output.txt                  ----
13 26 JonasDC
----                                                              ----
14
----  Dependencies:                                               ----
15 43 JonasDC
----    - mont_multiplier                                         ----
16 26 JonasDC
----                                                              ----
17
----  Authors:                                                    ----
18
----      - Geoffrey Ottoy, DraMCo research group                 ----
19
----      - Jonas De Craene, JonasDC@opencores.org                ---- 
20
----                                                              ---- 
21
---------------------------------------------------------------------- 
22
----                                                              ---- 
23
---- Copyright (C) 2011 DraMCo research group and OPENCORES.ORG   ---- 
24
----                                                              ---- 
25
---- This source file may be used and distributed without         ---- 
26
---- restriction provided that this copyright statement is not    ---- 
27
---- removed from the file and that any derivative work contains  ---- 
28
---- the original copyright notice and the associated disclaimer. ---- 
29
----                                                              ---- 
30
---- This source file is free software; you can redistribute it   ---- 
31
---- and/or modify it under the terms of the GNU Lesser General   ---- 
32
---- Public License as published by the Free Software Foundation; ---- 
33
---- either version 2.1 of the License, or (at your option) any   ---- 
34
---- later version.                                               ---- 
35
----                                                              ---- 
36
---- This source is distributed in the hope that it will be       ---- 
37
---- useful, but WITHOUT ANY WARRANTY; without even the implied   ---- 
38
---- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ---- 
39
---- PURPOSE.  See the GNU Lesser General Public License for more ---- 
40
---- details.                                                     ---- 
41
----                                                              ---- 
42
---- You should have received a copy of the GNU Lesser General    ---- 
43
---- Public License along with this source; if not, download it   ---- 
44
---- from http://www.opencores.org/lgpl.shtml                     ---- 
45
----                                                              ---- 
46
----------------------------------------------------------------------
47
 
48
library ieee;
49
use ieee.std_logic_1164.all;
50
use ieee.std_logic_unsigned.all;
51
use ieee.std_logic_arith.all;
52
 
53
library std;
54
use std.textio.all;
55
 
56
library ieee;
57
use ieee.std_logic_textio.all;
58
 
59
library mod_sim_exp;
60
use mod_sim_exp.mod_sim_exp_pkg.all;
61
 
62
entity multiplier_tb is
63
end multiplier_tb;
64
 
65
architecture test of multiplier_tb is
66 43 JonasDC
  constant CLK_PERIOD : time := 10 ns;
67 26 JonasDC
  signal clk          : std_logic := '0';
68
  signal reset        : std_logic := '1';
69
  file input          : text open read_mode is "src/sim_mult_input.txt";
70
  file output         : text open write_mode is "out/sim_mult_output.txt";
71
 
72
  ------------------------------------------------------------------
73 43 JonasDC
  -- Core parameters
74
  ------------------------------------------------------------------
75
  constant NR_BITS_TOTAL   : integer := 1536;
76
  constant NR_STAGES_TOTAL : integer := 96;
77
  constant NR_STAGES_LOW   : integer := 32;
78
  constant SPLIT_PIPELINE  : boolean := true;
79
 
80
  -- extra calculated constants
81
  constant NR_BITS_LOW : integer := (NR_BITS_TOTAL/NR_STAGES_TOTAL)*NR_STAGES_LOW;
82
  constant NR_BITS_HIGH : integer := NR_BITS_TOTAL-NR_BITS_LOW;
83
 
84
  -- the width of the input operand for the mulitplier test
85
  constant TEST_NR_BITS : integer := NR_BITS_LOW;
86
 
87
  ------------------------------------------------------------------
88 26 JonasDC
  -- Signals for multiplier core memory space
89
  ------------------------------------------------------------------
90
  -- data busses
91 43 JonasDC
  signal xy   : std_logic_vector(NR_BITS_TOTAL-1 downto 0);  -- x and y operand data bus RAM -> multiplier
92
  signal m    : std_logic_vector(NR_BITS_TOTAL-1 downto 0);  -- modulus data bus RAM -> multiplier
93
  signal r    : std_logic_vector(NR_BITS_TOTAL-1 downto 0);  -- result data bus RAM <- multiplier
94 26 JonasDC
 
95
  -- control signals
96 43 JonasDC
  signal p_sel          : std_logic_vector(1 downto 0); -- operand selection
97
  signal result_dest_op : std_logic_vector(1 downto 0); -- result destination operand
98
  signal ready          : std_logic;
99
  signal start          : std_logic;
100
  signal load_op        : std_logic;
101 26 JonasDC
  signal load_x         : std_logic;
102 43 JonasDC
  signal load_m         : std_logic;
103
  signal load_result    : std_logic;
104 26 JonasDC
begin
105
 
106
------------------------------------------
107
-- Generate clk
108
------------------------------------------
109
clk_process : process
110
begin
111
  while (true) loop
112
    clk <= '0';
113 43 JonasDC
    wait for CLK_PERIOD/2;
114 26 JonasDC
    clk <= '1';
115 43 JonasDC
    wait for CLK_PERIOD/2;
116 26 JonasDC
  end loop;
117
end process;
118
 
119
------------------------------------------
120
-- Stimulus Process
121
------------------------------------------
122
stim_proc : process
123
  procedure waitclk(n : natural := 1) is
124
  begin
125
    for i in 1 to n loop
126
      wait until rising_edge(clk);
127
    end loop;
128
  end waitclk;
129
 
130
  function ToString(constant Timeval : time) return string is
131
    variable StrPtr : line;
132
  begin
133
    write(StrPtr,Timeval);
134
    return StrPtr.all;
135
  end ToString;
136
 
137
  -- variables to read file
138
  variable L : line;
139
  variable Lw : line;
140 43 JonasDC
  variable x_op : std_logic_vector((NR_BITS_TOTAL-1) downto 0) := (others=>'0');
141
  variable y_op : std_logic_vector((NR_BITS_TOTAL-1) downto 0) := (others=>'0');
142
  variable m_op : std_logic_vector((NR_BITS_TOTAL-1) downto 0) := (others=>'0');
143
  variable result : std_logic_vector((NR_BITS_TOTAL-1) downto 0) := (others=>'0');
144 26 JonasDC
  variable good_value : boolean;
145
  variable param_count : integer := 0;
146
 
147
  variable timer : time;
148
begin
149
  -- initialisation
150
  xy <= (others=>'0');
151
  m <= (others=>'0');
152
  start <='0';
153
  reset <= '0';
154
  load_x <= '0';
155 43 JonasDC
  write(Lw, string'("----- Selecting pipeline: "));
156
  writeline(output, Lw);
157
  case (TEST_NR_BITS) is
158
    when NR_BITS_TOTAL =>  p_sel <= "11"; write(Lw, string'("  Full pipeline selected"));
159
    when NR_BITS_HIGH =>  p_sel <= "10"; write(Lw, string'("  Upper pipeline selected"));
160
    when NR_BITS_LOW  =>  p_sel <= "01"; write(Lw, string'("  Lower pipeline selected"));
161
    when others =>
162
      write(Lw, string'("  Invallid bitwidth for design"));
163
      assert false report "impossible basewidth!" severity failure;
164
  end case;
165
  writeline(output, Lw);
166 26 JonasDC
 
167
  -- Generate active high reset signal
168
  reset <= '1';
169
  waitclk(10);
170
  reset <= '0';
171
  waitclk(10);
172
 
173
  while not endfile(input) loop
174
    readline(input, L); -- read next line
175
    next when L(1)='-'; -- skip comment lines
176
    -- read input values
177
    case param_count is
178 43 JonasDC
      when 0 =>
179
        hread(L, x_op(TEST_NR_BITS-1 downto 0), good_value);
180 26 JonasDC
        assert good_value report "Can not read x operand" severity failure;
181
        assert false report "Simulating multiplication" severity note;
182
        write(Lw, string'("----------------------------------------------"));
183
        writeline(output, Lw);
184
        write(Lw, string'("--              MULTIPLICATION              --"));
185
        writeline(output, Lw);
186
        write(Lw, string'("----------------------------------------------"));
187
        writeline(output, Lw);
188
        write(Lw, string'("----- Variables used:"));
189
        writeline(output, Lw);
190
        write(Lw, string'("x: "));
191 43 JonasDC
        hwrite(Lw, x_op(TEST_NR_BITS-1 downto 0));
192 26 JonasDC
        writeline(output, Lw);
193
 
194
      when 1 =>
195 43 JonasDC
        hread(L, y_op(TEST_NR_BITS-1 downto 0), good_value);
196 26 JonasDC
        assert good_value report "Can not read y operand" severity failure;
197
        write(Lw, string'("y: "));
198 43 JonasDC
        hwrite(Lw, y_op(TEST_NR_BITS-1 downto 0));
199 26 JonasDC
        writeline(output, Lw);
200
 
201
      when 2 =>
202 43 JonasDC
        hread(L, m_op(TEST_NR_BITS-1 downto 0), good_value);
203 26 JonasDC
        assert good_value report "Can not read m operand" severity failure;
204
        write(Lw, string'("m: "));
205 43 JonasDC
        hwrite(Lw, m_op(TEST_NR_BITS-1 downto 0));
206 26 JonasDC
        writeline(output, Lw);
207
 
208
        -- load in x
209
        xy <= x_op;
210
        wait until rising_edge(clk);
211
        load_x <='1';
212
        wait until rising_edge(clk);
213
        load_x <='0';
214
 
215
        -- put y and m on the bus
216
        xy <= y_op;
217
        m <= m_op;
218
        wait until rising_edge(clk);
219
 
220
        -- start multiplication and wait for result
221
        start <= '1';
222
        wait until rising_edge(clk);
223
        start <= '0';
224
 
225
        wait until ready='1';
226
        wait until rising_edge(clk);
227
        writeline(output, Lw);
228
        write(Lw, string'("  Computed result: "));
229 43 JonasDC
        hwrite(Lw, r(TEST_NR_BITS-1 downto 0));
230 26 JonasDC
        writeline(output, Lw);
231
 
232
      when 3 =>
233 43 JonasDC
        hread(L, result(TEST_NR_BITS-1 downto 0), good_value);
234 26 JonasDC
        assert good_value report "Can not read result" severity failure;
235
        write(Lw, string'("  Read result:     "));
236 43 JonasDC
        hwrite(Lw, result(TEST_NR_BITS-1 downto 0));
237 26 JonasDC
        writeline(output, Lw);
238
 
239 43 JonasDC
        if (r(TEST_NR_BITS-1 downto 0) = result(TEST_NR_BITS-1 downto 0)) then
240 26 JonasDC
          write(Lw, string'("  => result is correct!")); writeline(output, Lw);
241
        else
242
          write(Lw, string'("  => Error: result is incorrect!!!")); writeline(output, Lw);
243
          assert false report "result is incorrect!!!" severity error;
244
        end if;
245
 
246
      when others =>
247
        assert false report "undefined state!" severity failure;
248
    end case;
249
 
250
    if (param_count = 3) then
251
      param_count := 0;
252
    else
253
      param_count := param_count+1;
254
    end if;
255
  end loop;
256
 
257
  wait for 1 us;
258
  assert false report "End of simulation" severity failure;
259
 
260
end process;
261
 
262
------------------------------------------
263
-- Multiplier instance
264
------------------------------------------
265
the_multiplier : mont_multiplier
266
  generic map(
267 43 JonasDC
    n     => NR_BITS_TOTAL,
268
    t     => NR_STAGES_TOTAL,
269
    tl    => NR_STAGES_LOW,
270
    split => SPLIT_PIPELINE
271 26 JonasDC
  )
272
  port map(
273
    core_clk => clk,
274
    xy       => xy,
275
    m        => m,
276
    r        => r,
277
    start    => start,
278
    reset    => reset,
279
    p_sel    => p_sel,
280
    load_x   => load_x,
281
    ready    => ready
282
  );
283
 
284
end test;

powered by: WebSVN 2.1.0

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