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

Subversion Repositories rsa_512

[/] [rsa_512/] [trunk/] [rtl/] [montgomery_step.vhd] - Blame information for rev 3

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:    10:40:41 11/01/2009 
6
-- Design Name: 
7
-- Module Name:    module_with_fifo - 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_step is
31
  port(
32
    clk       : in  std_logic;
33
    reset     : in  std_logic;
34
    valid_in  : in  std_logic;
35
    a         : in  std_logic_vector(15 downto 0);
36
    b         : in  std_logic_vector(15 downto 0);
37
    n         : in  std_logic_vector(15 downto 0);
38
    s_prev    : in  std_logic_vector(15 downto 0);
39
    n_c       : in  std_logic_vector(15 downto 0);
40
    s         : out std_logic_vector( 15 downto 0);
41
    valid_out : out std_logic;          -- es le valid out TODO : cambiar nombre
42
    busy      : out std_logic;
43
    b_req     : out std_logic;
44
    a_out     : out std_logic_vector(15 downto 0);
45
    n_out     : out std_logic_vector(15 downto 0);  --señal que indica que el modulo está ocupado y no puede procesar nuevas peticiones
46
    c_step    : out std_logic;          --genera un pulso cuando termina su computo para avisar al modulo superior
47
    stop      : in  std_logic
48
    );
49
end montgomery_step;
50
 
51
architecture Behavioral of montgomery_step is
52
 
53
  component pe_wrapper
54
    port(
55
      clk          : in  std_logic;
56
      reset        : in  std_logic;
57
      ab_valid     : in  std_logic;
58
      valid_in     : in  std_logic;
59
      a            : in  std_logic_vector(15 downto 0);
60
      b            : in  std_logic_vector(15 downto 0);
61
      n            : in  std_logic_vector(15 downto 0);
62
      s_prev       : in  std_logic_vector(15 downto 0);
63
      n_c          : in  std_logic_vector(15 downto 0);
64
      s            : out std_logic_vector( 15 downto 0);
65
      data_ready   : out std_logic;
66
      fifo_req     : out std_logic;
67
      m_val        : out std_logic;
68
      reset_the_PE : in  std_logic);    -- estamos preparados para aceptar el siguiente dato
69
  end component;
70
 
71
  --Inputs
72
 
73
  signal ab_valid                                               : std_logic;
74
  signal valid_mont, fifo_read, m_val, valid_mont_out, reset_pe : std_logic;
75
  --Outputs
76
 
77
 
78
  --definimos los estados
79
  type state_type is (wait_valid, wait_m, mont_proc, getting_results, prep_m, b_stable);
80
  signal state, next_state     : state_type;
81
  signal counter, next_counter : std_logic_vector(7 downto 0);  -- cuenta las palabras que han salido para ir cortando
82
 
83
 
84
  --Señales nuevas
85
  signal mont_input_a, mont_input_n, mont_input_s                   : std_logic_vector(15 downto 0);
86
  signal reg_constant, next_reg_constant, next_reg_input, reg_input : std_logic_vector(47 downto 0);
87
  signal reg_out, reg_out_1, reg_out_2, reg_out_3, reg_out_4        : std_logic_vector(31 downto 0);
88
  signal next_reg_out                                               : std_logic_vector(31 downto 0);
89
 
90
  --Cadena de registros hacia fuera
91
  signal reg_input_1, reg_input_2, reg_input_3, reg_input_4, reg_input_5 : std_logic_vector(47 downto 0);
92
 
93
 
94
begin
95
 
96
  mont : pe_wrapper port map (
97
    clk          => clk,
98
    reset        => reset,
99
    ab_valid     => ab_valid,
100
    a            => mont_input_a,
101
    b            => b,
102
    n            => mont_input_n,
103
    s_prev       => mont_input_s,
104
    n_c          => n_c,
105
    s            => s,
106
    valid_in     => valid_mont,
107
    data_ready   => valid_mont_out,
108
    m_val        => m_val,
109
    reset_the_PE => reset_pe
110
    );
111
 
112
  process(clk, reset)
113
  begin
114
    if(clk = '1' and clk'event) then
115
 
116
      if(reset = '1')then
117
        state        <= wait_valid;
118
        counter      <= (others => '0');
119
        reg_constant <= (others => '0');
120
        reg_input    <= (others => '0');
121
        reg_input_1  <= (others => '0');
122
        reg_input_2  <= (others => '0');
123
        reg_input_3  <= (others => '0');
124
        reg_input_4  <= (others => '0');
125
 
126
        reg_out   <= (others => '0');
127
        reg_out_1 <= (others => '0');
128
        reg_out_2 <= (others => '0');
129
        reg_out_3 <= (others => '0');
130
        reg_out_4 <= (others => '0');
131
 
132
      else
133
        reg_input   <= next_reg_input;
134
        reg_input_1 <= reg_input;
135
        reg_input_2 <= reg_input_1;
136
        reg_input_3 <= reg_input_2;
137
        reg_input_4 <= reg_input_3;
138
        reg_input_5 <= reg_input_4;
139
 
140
        reg_out   <= reg_input_4(47 downto 32) & reg_input_4(31 downto 16);
141
        reg_out_1 <= reg_out;
142
        reg_out_2 <= reg_out_1;
143
        reg_out_3 <= reg_out_2;
144
        reg_out_4 <= reg_out_3;
145
 
146
        state        <= next_state;
147
        counter      <= next_counter;
148
        reg_constant <= next_reg_constant;
149
      end if;
150
    end if;
151
  end process;
152
 
153
  process(state, valid_in, m_val, a, n, s_prev, counter, valid_mont_out, stop, reg_constant, reg_input_5, reg_out_4)
154
  begin
155
    --reset_fifo <= '0';
156
    next_reg_input <= a&n&s_prev;       --Propagación de la entrada TODO add variable 
157
    --next_reg_out <= a&n;              --Vamos retrasando la entrada TODO add variable 
158
 
159
 
160
    a_out <= reg_out_4(31 downto 16);
161
    n_out <= reg_out_4(15 downto 0);
162
 
163
    next_state   <= state;
164
    next_counter <= counter;
165
    --write_fifos <= valid_in;
166
    ab_valid     <= '0';
167
    valid_mont   <= '0';
168
    valid_out    <= '0';
169
    reset_pe     <= '0';
170
    busy         <= '1';
171
    b_req        <= '0';
172
    c_step       <= '0';
173
 
174
    --Todo esto es nuevo
175
    mont_input_a      <= (others => '0');
176
    mont_input_n      <= (others => '0');
177
    mont_input_s      <= (others => '0');
178
    next_reg_constant <= reg_constant;
179
 
180
    case state is
181
      when wait_valid =>
182
        busy     <= '0';                --esperamos la peticion
183
        reset_pe <= '1';
184
        if(valid_in = '1') then
185
 
186
          b_req             <= '1';     --Solicitamos al modulo externo la b
187
          next_state        <= b_stable;
188
          next_reg_constant <= a&n&s_prev;  --TODO add variable                                                          
189
        end if;
190
      when b_stable =>
191
        next_state          <= prep_m;
192
      when prep_m   =>
193
        mont_input_a        <= reg_constant(47 downto 32);  --TODO add this to sensitivity
194
        mont_input_n        <= reg_constant(31 downto 16);
195
        mont_input_s        <= reg_constant(15 downto 0);
196
        ab_valid            <= '1';
197
        next_state          <= wait_m;
198
      when wait_m   =>
199
 
200
        --Mantenemos las entradas para que nos calcule m correctamente
201
        mont_input_a <= reg_constant(47 downto 32);  --TODO add this to sensitivity
202
        mont_input_n <= reg_constant(31 downto 16);
203
        mont_input_s <= reg_constant(15 downto 0);
204
 
205
 
206
        if (m_val = '1') then
207
 
208
          valid_mont   <= '1';
209
          next_state   <= mont_proc;
210
          mont_input_a <= reg_input_5(47 downto 32);
211
          mont_input_n <= reg_input_5(31 downto 16);
212
          mont_input_s <= reg_input_5(15 downto 0);
213
 
214
        end if;
215
 
216
      when mont_proc =>
217
 
218
        valid_mont   <= '1';
219
        mont_input_a <= reg_input_5(47 downto 32);
220
        mont_input_n <= reg_input_5(31 downto 16);
221
        mont_input_s <= reg_input_5(15 downto 0);
222
 
223
        if(valid_mont_out = '1') then
224
 
225
          next_counter <= x"00";
226
          next_state   <= getting_results;
227
        end if;
228
 
229
      when getting_results =>
230
 
231
        valid_out    <= '1';
232
        next_counter <= counter+1;
233
        valid_mont   <= '1';
234
 
235
        mont_input_a <= reg_input_5(47 downto 32);
236
        mont_input_n <= reg_input_5(31 downto 16);
237
        mont_input_s <= reg_input_5(15 downto 0);
238
 
239
        if(counter = (x"22")) then
240
          next_state <= wait_valid;
241
          c_step <= '1';
242
          reset_pe <= '1';
243
        end if;
244
 
245
    end case;
246
 
247
    if(stop='1') then
248
      next_state <= wait_valid;
249
      --reset_fifo <= '1';
250
      reset_pe <= '1';
251
    end if;
252
 
253
  end process;
254
 
255
end Behavioral;

powered by: WebSVN 2.1.0

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