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

Subversion Repositories rsa_512

[/] [rsa_512/] [trunk/] [rtl/] [montgomery_mult.vhd] - Blame information for rev 10

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

Line No. Rev Author Line
1 3 jcastillo
----------------------------------------------------------------------------------
2
-- Company: 
3
-- Engineer: 
4
-- 
5
-- Create Date:    20:03:35 11/02/2009 
6
-- Design Name: 
7
-- Module Name:    etapas - Behavioral 
8
-- Project Name: 
9
-- Target Devices: 
10
-- Tool versions: 
11
-- Description: 
12
--
13
-- Dependencies: 
14
--
15
-- Revision: 
16
-- Revision 0.01 - File Created
17
-- Additional Comments: 
18
--
19
----------------------------------------------------------------------------------
20
library IEEE;
21
use IEEE.STD_LOGIC_1164.all;
22
use IEEE.STD_LOGIC_ARITH.all;
23
use IEEE.STD_LOGIC_UNSIGNED.all;
24
 
25
---- Uncomment the following library declaration if instantiating
26
---- any Xilinx primitives in this code.
27
--library UNISIM;
28
--use UNISIM.VComponents.all;
29
 
30
entity montgomery_mult is
31
 
32
  port(
33
    clk       : in  std_logic;
34
    reset     : in  std_logic;
35
    valid_in  : in  std_logic;
36
    a         : in  std_logic_vector(15 downto 0);
37
    b         : in  std_logic_vector(15 downto 0);
38
    n         : in  std_logic_vector(15 downto 0);
39
    s_prev    : in  std_logic_vector(15 downto 0);
40
    n_c       : in  std_logic_vector(15 downto 0);
41
    s         : out std_logic_vector( 15 downto 0);
42
    valid_out : out std_logic           -- es le valid out TODO : cambiar nombre
43
    );
44
 
45
end montgomery_mult;
46
 
47
architecture Behavioral of montgomery_mult is
48
 
49
  component montgomery_step is
50 9 jcastillo
                              port(
51
                                clk       : in  std_logic;
52
                                reset     : in  std_logic;
53
                                valid_in  : in  std_logic;
54
                                a         : in  std_logic_vector(15 downto 0);
55
                                b         : in  std_logic_vector(15 downto 0);
56
                                n         : in  std_logic_vector(15 downto 0);
57
                                s_prev    : in  std_logic_vector(15 downto 0);
58
                                n_c       : in  std_logic_vector(15 downto 0);
59
                                s         : out std_logic_vector( 15 downto 0);
60
                                valid_out : out std_logic;  -- es le valid out TODO : cambiar nombre
61
                                busy      : out std_logic;
62
                                b_req     : out std_logic;
63
                                a_out     : out std_logic_vector(15 downto 0);
64
                                n_out     : out std_logic_vector(15 downto 0);  --seńal que indica que el modulo está ocupado y no puede procesar nuevas peticiones
65
                                c_step    : out std_logic;  --genera un pulso cuando termina su computo para avisar al modulo superior
66
                                stop      : in  std_logic
67
                                );
68 3 jcastillo
  end component;
69
 
70
  component fifo_512_bram
71
    port (
72
      clk   : in  std_logic;
73
      rst   : in  std_logic;
74
      din   : in  std_logic_vector(15 downto 0);
75
      wr_en : in  std_logic;
76
      rd_en : in  std_logic;
77
      dout  : out std_logic_vector(15 downto 0);
78
      full  : out std_logic;
79
      empty : out std_logic);
80
  end component;
81
 
82
  component fifo_256_feedback
83
    port (
84
      clk   : in  std_logic;
85
      rst   : in  std_logic;
86
      din   : in  std_logic_vector(48 downto 0);
87
      wr_en : in  std_logic;
88
      rd_en : in  std_logic;
89
      dout  : out std_logic_vector(48 downto 0);
90
      full  : out std_logic;
91
      empty : out std_logic);
92
  end component;
93
 
94
  type arr_dat_out is array(0 to 7) of std_logic_vector(15 downto 0);
95
  type arr_val is array(0 to 7) of std_logic;
96
  type arr_b is array(0 to 7) of std_logic_vector(15 downto 0);
97
 
98
  signal b_reg, next_b_reg                                              : arr_b;
99
  signal valid_mid, fifo_reqs, fifo_reqs_reg, next_fifo_reqs_reg, stops : arr_val;
100
  signal a_out_mid, n_out_mid, s_out_mid                                : arr_dat_out;  --std_logic_vector(15 downto 0);
101
 
102
  --Seńales a la fifo
103
  signal wr_en, rd_en, empty : std_logic;
104
  signal fifo_out            : std_logic_vector(15 downto 0);
105
 
106
  signal fifo_out_feedback, fifo_in_feedback : std_logic_vector(48 downto 0);
107
  signal read_fifo_feedback, empty_feedback  : std_logic;
108
 
109
  --Seńales de entrada al primer PE
110
  signal a_in, s_in, n_in : std_logic_vector(15 downto 0);
111
  signal f_valid, busy_pe : std_logic;
112
 
113
  --salida c_step del primer PE para ir contando y saber cuando sacar el valor correcto.
114
  signal c_step, reg_c_step : std_logic;
115
 
116
  --contador para saber cuando cońo acabamos :)
117
  signal count, next_count : std_logic_vector(7 downto 0);
118
 
119
  --seńal para escribir el loopback en la fifo de entrada de feedback
120
  signal wr_fifofeed : std_logic;
121
 
122 9 jcastillo
  type state_type is (rst_fifos, wait_start, process_data, dump_feed);
123 3 jcastillo
  signal state, next_state                   : state_type;
124
  signal reg_busy                            : std_logic;
125
  signal reset_fifos                         : std_logic;
126
  signal count_feedback, next_count_feedback : std_logic_vector(15 downto 0);
127
 
128
begin
129
 
130
--Fifo para almacenar b
131
  fifo_b : fifo_512_bram port map (
132
    clk   => clk,
133
    rst   => reset_fifos,
134
    din   => b,
135
    wr_en => wr_en,
136
    rd_en => rd_en,
137
    dout  => fifo_out,
138
    empty => empty
139
    );
140
 
141
--Fifo para el feedback al primer PE
142
  fifo_feed : fifo_256_feedback port map (
143
    clk   => clk,
144
    rst   => reset_fifos,
145
    din   => fifo_in_feedback,
146
    wr_en => wr_fifofeed,
147
    rd_en => read_fifo_feedback,
148
    dout  => fifo_out_feedback,
149
    empty => empty_feedback
150
    );
151
 
152
 
153
 
154
--Primer PE
155
  et_first : montgomery_step port map(
156
    clk       => clk,
157
    reset     => reset,
158
    valid_in  => f_valid,
159
    a         => a_in,
160
    b         => b_reg(0),
161
    n         => n_in,
162
    s_prev    => s_in,
163
    n_c       => n_c,
164
    s         => s_out_mid(0),
165
    valid_out => valid_mid(0),
166
    busy      => busy_pe,
167
    b_req     => fifo_reqs(0),
168
    a_out     => a_out_mid(0),
169
    n_out     => n_out_mid(0),
170
    c_step    => c_step,
171
    stop      => stops(0)
172
    );
173
 
174
--Ultimo PE
175
  et_last : montgomery_step port map(
176
    clk       => clk,
177
    reset     => reset,
178
    valid_in  => valid_mid(6),
179
    a         => a_out_mid(6),
180
    b         => b_reg(7),
181
    n         => n_out_mid(6),
182
    s_prev    => s_out_mid(6),
183
    n_c       => n_c,
184
    s         => s_out_mid(7),
185
    valid_out => valid_mid(7),
186
    b_req     => fifo_reqs(7),
187
    a_out     => a_out_mid(7),
188
    n_out     => n_out_mid(7),
189
    stop      => stops(7)
190
    );
191
 
192
  g1     : for i in 1 to 6 generate
193
    et_i : montgomery_step port map(
194
      clk       => clk,
195
      reset     => reset,
196
      valid_in  => valid_mid(i-1),
197
      a         => a_out_mid(i-1),
198
      b         => b_reg(i),
199
      n         => n_out_mid(i-1),
200
      s_prev    => s_out_mid(i-1),
201
      n_c       => n_c,
202
      s         => s_out_mid(i),
203
      valid_out => valid_mid(i),
204
      b_req     => fifo_reqs(i),
205
      a_out     => a_out_mid(i),
206
      n_out     => n_out_mid(i),
207
      stop      => stops(i)
208
      );
209
 
210
  end generate g1;
211
 
212
 
213
  process(clk, reset)
214
  begin
215
 
216
    if(clk = '1' and clk'event) then
217
 
218
      if(reset = '1')then
219
        state               <= wait_start;
220
        count_feedback      <= (others => '0');
221
        reg_busy            <= '0';
222
        for i in 0 to 7 loop
223
          b_reg(i)          <= (others => '0');
224
          fifo_reqs_reg (i) <= '0';
225
          count             <= (others => '0');
226
          reg_c_step        <= '0';
227
 
228
        end loop;
229
      else
230
        state               <= next_state;
231
        reg_busy            <= busy_pe;
232
        count_feedback      <= next_count_feedback;
233
        for i in 0 to 7 loop
234
          b_reg(i)          <= next_b_reg(i);
235
          fifo_reqs_reg (i) <= next_fifo_reqs_reg(i);
236
          count             <= next_count;
237
          reg_c_step        <= c_step;
238
        end loop;
239
      end if;
240
    end if;
241
 
242
  end process;
243
 
244
 
245
  --Proceso combinacional que controla las lecturas a la fifo
246
  process(fifo_reqs_reg, fifo_out, b, fifo_reqs, b_reg, state, empty)
247
  begin
248
 
249
 
250
    for i in 0 to 7 loop
251
      next_b_reg(i)         <= b_reg(i);
252
      next_fifo_reqs_reg(i) <= fifo_reqs(i);
253
    end loop;
254
 
255
    if(state = wait_start) then
256
      next_b_reg(0)           <= b;
257
      next_fifo_reqs_reg(0)   <= '0';   --anulamos la peticion de b
258
      for i in 1 to 7 loop
259
        next_b_reg(i)         <= (others => '0');
260
        next_fifo_reqs_reg(i) <= '0';
261
      end loop;
262
    else
263
      for i in 0 to 7 loop
264
        if(fifo_reqs_reg(i) = '1' and empty = '0') then
265
          next_b_reg(i)       <= fifo_out;
266
        end if;
267
      end loop;
268
    end if;
269
  end process;
270
 
271
  --Proceso combinacional fsm principal
272
  process( valid_in, b, state, fifo_reqs, a_out_mid, n_out_mid, s_out_mid, valid_mid, a, s_prev, n, busy_pe, empty_feedback, fifo_out_feedback, count, reg_c_step, reset, reg_busy, count_feedback )
273
  begin
274
 
275
    --las peticiones a la fifo son las or de los modulos
276
    rd_en              <= fifo_reqs(0) or fifo_reqs(1) or fifo_reqs(2) or fifo_reqs(3) or fifo_reqs(4) or fifo_reqs(5) or fifo_reqs(6) or fifo_reqs(7);
277
    next_state         <= state;
278
    wr_en              <= '0';
279
    fifo_in_feedback   <= a_out_mid(7)&n_out_mid(7)&s_out_mid(7)&valid_mid(7);
280
    read_fifo_feedback <= '0';
281
    wr_fifofeed        <= '0';
282
    --Controlamos el primer PE
283
    a_in               <= a;
284
    s_in               <= s_prev;
285
    n_in               <= n;
286
    f_valid            <= valid_in;
287
    reset_fifos        <= reset;
288
    --ponemos las salidas
289
    s                  <= (others => '0');
290
    valid_out          <= '0';
291
 
292
    --Incrementamos el contador solo cuando el primer PE termina su cuenta
293
    next_count          <= count;
294
    next_count_feedback <= count_feedback;
295
    --El contador solo se incrementa una vez por ciclo de la pipeline, así me evito que cada PE lo incremente
296
    if(reg_c_step = '1') then
297
      next_count        <= count + 8;
298
    end if;
299
    --durante el ciclo de la pipeline que sea considerado el ultimo, sacamos los datos
300
    if( count = x"20") then
301
      s                 <= s_out_mid(0);
302
      valid_out         <= valid_mid(0);
303
    end if;
304
 
305
    for i in 0 to 7 loop
306
      stops(i) <= '0';
307
    end loop;
308
 
309
    case state is
310
      --Esperamos a que tengamos un input y vamos cargando las b's
311
      when wait_start =>
312 8 jcastillo
        --reset_fifos   <= '1';
313 3 jcastillo
        if(valid_in = '1') then
314
          reset_fifos <= '0';
315
          --next_b_reg(0) <= b;
316
          next_state  <= process_data;
317
                                        --wr_en <= '1';
318
        end if;
319
 
320
      when process_data =>
321 9 jcastillo
        wr_fifofeed           <= valid_mid(7);
322 3 jcastillo
        if(valid_in = '1') then
323 9 jcastillo
          wr_en               <= '1';
324 3 jcastillo
        end if;
325
        --Miramos si hay que volver a meter datos a la b
326
        if(empty_feedback = '0' and reg_busy = '0') then
327 9 jcastillo
          read_fifo_feedback  <= '1';
328
          next_state          <= dump_feed;
329 3 jcastillo
          next_count_feedback <= x"0000";
330
        end if;
331 9 jcastillo
 
332 3 jcastillo
        --Si ya hemos sobrepasado el limite paramos y volvemos a la espera
333 9 jcastillo
        if( count > x"23") then
334 3 jcastillo
          next_state <= wait_start;
335 9 jcastillo
                                        --y
336 3 jcastillo
          for i in 0 to 7 loop
337
            stops(i) <= '1';
338
          end loop;
339
          next_count <= (others => '0');
340
        end if;
341
 
342
        --Vacia la fifo de feedback
343 9 jcastillo
      when dump_feed =>
344
        if(empty_feedback = '0')
345 3 jcastillo
        then
346
          next_count_feedback <= count_feedback+1;
347
        end if;
348 9 jcastillo
        wr_fifofeed           <= valid_mid(7);
349
        read_fifo_feedback    <= '1';
350
        a_in                  <= fifo_out_feedback(48 downto 33);
351
        n_in                  <= fifo_out_feedback(32 downto 17);
352
        s_in                  <= fifo_out_feedback(16 downto 1);
353
        f_valid               <= fifo_out_feedback(0);
354
        if(empty_feedback = '1') then
355
          next_state          <= process_data;
356
 
357 3 jcastillo
        end if;
358
        if(count_feedback = x"22") then
359
          read_fifo_feedback <= '0';
360 9 jcastillo
          next_state         <= process_data;
361 3 jcastillo
        end if;
362 9 jcastillo
      when rst_fifos =>
363
        next_state           <= wait_start;
364
        reset_fifos          <= '1';
365 3 jcastillo
    end case;
366 9 jcastillo
 
367 3 jcastillo
  end process;
368
end Behavioral;
369
 

powered by: WebSVN 2.1.0

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