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

Subversion Repositories mod_mult_exp

[/] [mod_mult_exp/] [trunk/] [rtl/] [vhdl/] [communication/] [ModExpDataCtrlSM.vhd] - Blame information for rev 6

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 6 gajos
-----------------------------------------------------------------------
2
----                                                               ----
3
---- Montgomery modular multiplier and exponentiator               ----
4
----                                                               ----
5
---- This file is part of the Montgomery modular multiplier        ----
6
---- and exponentiator project                                     ----
7
---- http://opencores.org/project,mod_mult_exp                     ----
8
----                                                               ----
9
---- Description:                                                  ----
10
----   This module is state machine for the example implementation ---- 
11
----   of the Montgomery modular exponentiatorcombined with the    ----
12
----   RS-232 communication with PC.                               ----
13
----                                                               ----
14
---- To Do:                                                        ----
15
----                                                               ----
16
---- Author(s):                                                    ----
17
---- - Krzysztof Gajewski, gajos@opencores.org                     ----
18
----                       k.gajewski@gmail.com                    ----
19
----                                                               ----
20
-----------------------------------------------------------------------
21
----                                                               ----
22
---- Copyright (C) 2019 Authors and OPENCORES.ORG                  ----
23
----                                                               ----
24
---- This source file may be used and distributed without          ----
25
---- restriction provided that this copyright statement is not     ----
26
---- removed from the file and that any derivative work contains   ----
27
---- the original copyright notice and the associated disclaimer.  ----
28
----                                                               ----
29
---- This source file is free software; you can redistribute it    ----
30
---- and-or modify it under the terms of the GNU Lesser General    ----
31
---- Public License as published by the Free Software Foundation;  ----
32
---- either version 2.1 of the License, or (at your option) any    ----
33
---- later version.                                                ----
34
----                                                               ----
35
---- This source is distributed in the hope that it will be        ----
36
---- useful, but WITHOUT ANY WARRANTY; without even the implied    ----
37
---- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR       ----
38
---- PURPOSE. See the GNU Lesser General Public License for more   ----
39
---- details.                                                      ----
40
----                                                               ----
41
---- You should have received a copy of the GNU Lesser General     ----
42
---- Public License along with this source; if not, download it    ----
43
---- from http://www.opencores.org/lgpl.shtml                      ----
44
----                                                               ----
45
-----------------------------------------------------------------------
46
library IEEE;
47
use IEEE.STD_LOGIC_1164.ALL;
48
use work.properties.ALL;
49
 
50
-- Uncomment the following library declaration if using
51
-- arithmetic functions with Signed or Unsigned values
52
--use IEEE.NUMERIC_STD.ALL;
53
 
54
-- Uncomment the following library declaration if instantiating
55
-- any Xilinx primitives in this code.
56
--library UNISIM;
57
--use UNISIM.VComponents.all;
58
 
59
entity ModExpDataCtrlSM is
60
    port(
61
        clk                 : in  STD_LOGIC;
62
        reset               : in  STD_LOGIC;
63
        RDAsig              : in  STD_LOGIC;
64
        TBEsig              : in  STD_LOGIC;
65
        RDsig               : out STD_LOGIC;
66
        WRsig               : out STD_LOGIC;
67
        data_in_ready       : out STD_LOGIC;
68
        readySig            : in  STD_LOGIC;
69
        modExpCtrlRegEn     : out STD_LOGIC;
70
        dataToModExpEn      : out STD_LOGIC;
71
        dataToModExpShift   : out STD_LOGIC;
72
        dataFromModExpEn    : out STD_LOGIC;
73
        dataFromModExpShift : out STD_LOGIC;
74
        muxCtrl             : out STD_LOGIC;
75
        opcodes             : in  STD_LOGIC_VECTOR(2 downto 0);
76
        controlStateOut     : out STD_LOGIC_VECTOR(2 downto 0)
77
);
78
end ModExpDataCtrlSM;
79
 
80
architecture Behavioral of ModExpDataCtrlSM is
81
 
82
-- Counters are used for both bit counting in byte 
83
-- and composing full length word in exponentiator
84
component counter is
85
    generic(
86
        size : integer := 4
87
    );
88
    port (
89
        count  : in  STD_LOGIC;
90
        zero   : in  STD_LOGIC;
91
        output : out STD_LOGIC_VECTOR (size - 1 downto 0);
92
        clk    : in  STD_LOGIC;
93
        reset  : in  STD_LOGIC
94
    );
95
end component counter;
96
 
97
-- some constants for temp_state signal which is used in TEMPORARY_STATE.
98
-- This state is used as something like "wait" command due to data 
99
-- propagation in the core
100
constant rd_data      : STD_LOGIC_VECTOR(2 downto 0) := "000";
101
constant mk_fin       : STD_LOGIC_VECTOR(2 downto 0) := "001";
102
constant dat_out_prop : STD_LOGIC_VECTOR(2 downto 0) := "010";
103
constant info_st      : STD_LOGIC_VECTOR(2 downto 0) := "011";
104
constant mv_dat       : STD_LOGIC_VECTOR(2 downto 0) := "100";
105
constant nothing      : STD_LOGIC_VECTOR(2 downto 0) := "101";
106
 
107
signal state      : comm_ctrl_states := NOP;
108
signal next_state : comm_ctrl_states := NOP;
109
 
110
signal temp_state : STD_LOGIC_VECTOR (2 downto 0) := nothing;
111
 
112
-- This signals are used for control the counters for data shifting
113
-- in shift registers (by bytes). This length have to be modified 
114
-- with changing the used word size.
115
-- Modify for variable key size
116
-- In fact it is modified from the properties file
117
signal serialDataCtrCt   : STD_LOGIC;
118
signal serialDataCtrZero : STD_LOGIC;
119
signal serialDataCtrOut  : STD_LOGIC_VECTOR(WORD_INT_LOG downto 0);
120
 
121
-- This signals are used for control the counters for data shifting - bits in 
122
-- bytes.
123
-- DO NOT MODIFY!!!
124
signal shiftDataCtrCt    : STD_LOGIC;
125
signal shiftDataCtrZero  : STD_LOGIC;
126
signal shiftDataCtrOut   : STD_LOGIC_VECTOR(3 downto 0);
127
 
128
begin
129
    -- State machine process
130
    SM : process(state, RDAsig, TBEsig, shiftDataCtrOut,
131
         serialDataCtrOut, opcodes, readySig)
132
        begin
133
            case state is
134
                -- This state prepares whoole core before calculations
135
                -- 'No operation' state
136
                when NOP =>
137
                     WRsig <= '0';
138
                     modExpCtrlRegEn <= '0';
139
                     dataToModExpEn <= '0';
140
                     dataToModExpShift <= '0';
141
                     dataFromModExpEn <= '0';
142
                     dataFromModExpShift <= '0';
143
                     serialDataCtrZero <= '1';
144
                     serialDataCtrCt <= '0';
145
                     shiftDataCtrZero <= '1';
146
                     shiftDataCtrCt <= '0';
147
                     RDsig <= '0';
148
                                                        -- This is something like 'info' word
149
                     if (readySig = '1') then
150
                        controlStateOut <= "100";
151
                     else
152
                        controlStateOut <= "000";
153
                     end if;
154
                     muxCtrl <= '1';
155
                     data_in_ready <= '0';
156
                     temp_state <= nothing; -- not important
157
                     -- RDAsig = '1' means that some data
158
                     -- appeard in the RS-232 input
159
                     if (RDAsig = '1') then
160
                        next_state <= DECODE_IN;
161
                     else
162
                        next_state <= NOP;
163
                     end if;
164
                when DECODE_IN =>
165
                     WRsig <= '0';
166
                     dataToModExpEn <= '0';
167
                     dataToModExpShift <= '0';
168
                     dataFromModExpEn <= '0';
169
                     dataFromModExpShift <= '0';
170
                     serialDataCtrZero <= '1';
171
                     serialDataCtrCt <= '0';
172
                     shiftDataCtrZero <= '1';
173
                     shiftDataCtrCt <= '0';
174
                     RDsig <= '1';
175
                     controlStateOut <= "000";
176
                     muxCtrl <= '1';
177
                     data_in_ready <= '0';
178
                     modExpCtrlRegEn <= '1';
179
 
180
                     -- firstly from the RS-232 input comes OPCODE informing the core
181
                     -- what to do. Data can appeard in any order. This opcode are saved
182
                     -- in the suitable register at the input of the modular exponentiator
183
                     if (opcodes = mn_read_base) or (opcodes = mn_read_modulus) or
184
                         (opcodes = mn_read_exponent) or (opcodes = mn_read_residuum) then
185
                         next_state <= TEMPORARY_STATE;
186
                         temp_state <= rd_data;
187
                     elsif (opcodes = mn_count_power)  then
188
                         next_state <= TEMPORARY_STATE;
189
                         temp_state <= mk_fin;
190
                     elsif (opcodes = mn_show_result)  then
191
                         if (readySig = '1') then
192
                             next_state <= TEMPORARY_STATE;
193
                             temp_state <= dat_out_prop;
194
                         else
195
                             next_state <= TEMPORARY_STATE;
196
                             temp_state <= info_st;
197
                         end if;
198
                     elsif (opcodes = mn_prepare_for_data) then
199
                         next_state <= TEMPORARY_STATE;
200
                         temp_state <= nothing;
201
                     else
202
                         next_state <= NOP;
203
                         temp_state <= nothing; -- not important
204
                     end if;
205
                when READ_DATA =>
206
                        -- For now need to 'restart' all the flow of reading data
207
                        modExpCtrlRegEn <= '0';
208
                        RDsig <= '0';
209
                        WRsig <= '0';
210
                        serialDataCtrCt <= '0';
211
                        serialDataCtrZero <= '0';
212
                        shiftDataCtrCt <= '0';
213
                        shiftDataCtrZero <= '0';
214
                        dataToModExpEn <= '1';
215
                        dataToModExpShift <= '0';
216
                        dataFromModExpEn <= '0';
217
                        dataFromModExpShift <= '0';
218
 
219
                        controlStateOut <= "000";
220
                        muxCtrl <= '1';
221
                        data_in_ready <= '0';
222
                        temp_state <= nothing; -- not important
223
                        if (RDAsig = '0') then
224
                            next_state <= READ_DATA;
225
                        else
226
                            next_state <= DECODE_READ;
227
                        end if;
228
                -- This state is for the control of number of the 8-bit 'packets'
229
                -- of the input data for the modular exponentiator
230
                when DECODE_READ =>
231
                      modExpCtrlRegEn <= '0';
232
                      WRsig <= '0';
233
                      serialDataCtrCt <= '1';
234
                      serialDataCtrZero <= '0';
235
                      shiftDataCtrCt <= '0';
236
                      shiftDataCtrZero <= '0';
237
                      dataToModExpShift <= '0';
238
                      dataFromModExpEn <= '0';
239
                      dataFromModExpShift <= '0';
240
                      RDsig <= '1';
241
                      dataToModExpEn <= '1';
242
                      controlStateOut <= "000";
243
                      muxCtrl <= '1';
244
                      data_in_ready <= '0';
245
                      -- Data reading X times 8 bit -> modify for variable key length
246
                      -- In fact it is modified from the properties file
247
                      if (serialDataCtrOut(WORD_INT_LOG - 1 downto 0) = WORD_INT_LOG_STR) then
248
                          next_state <= DECODE_READ_PROP;
249
                          temp_state <= nothing; -- not important
250
                      else
251
                          next_state <= TEMPORARY_STATE;
252
                          temp_state <= mv_dat;
253
                      end if;
254
                -- Some info state for the modular exponentiator core,
255
                -- that some data are at the input - after the end of the 
256
                -- reading data
257
                when DECODE_READ_PROP =>
258
                      modExpCtrlRegEn <= '0';
259
                      WRsig <= '0';
260
                      serialDataCtrCt <= '0';
261
                      serialDataCtrZero <= '0';
262
                      shiftDataCtrCt <= '0';
263
                      shiftDataCtrZero <= '0';
264
                      dataToModExpShift <= '0';
265
                      dataFromModExpEn <= '0';
266
                      dataFromModExpShift <= '0';
267
                      RDsig <= '0';
268
                      dataToModExpEn <= '0';
269
                      serialDataCtrCt <= '0';
270
                      muxCtrl <= '1';
271
                      data_in_ready <= '1';
272
                      temp_state <= nothing; -- not important
273
                      controlStateOut <= "000";
274
                      next_state <= INFO_STATE;
275
                -- This state is for moving bits in data word for the 
276
                -- modular exponentiator counter counts to 8 while data
277
                -- are shifted
278
                when MOVE_DATA =>
279
                    modExpCtrlRegEn <= '0';
280
                    RDsig <= '0';
281
                    WRsig <= '0';
282
                    serialDataCtrCt <= '0';
283
                    dataToModExpEn <= '0';
284
                    dataToModExpShift <= '1';
285
                    dataFromModExpEn <= '0';
286
                    dataFromModExpShift <= '0';
287
                    serialDataCtrZero <= '0';
288
                    temp_state <= nothing;
289
                    controlStateOut <= "000";
290
                    muxCtrl <= '1';
291
                    data_in_ready <= '0';
292
                    --- shifting data in register -> DO NOT MODIFY!!!
293
                    if (shiftDataCtrOut(2 downto 0) = "111") then
294
                        shiftDataCtrZero <= '1';
295
                        shiftDataCtrCt <= '0';
296
                        next_state <= READ_DATA;
297
                    else
298
                        shiftDataCtrZero <= '0';
299
                        shiftDataCtrCt <= '1';
300
                        next_state <= MOVE_DATA;
301
                    end if;
302
                -- If all the needed data appeared at the input
303
                -- and 'mn_count_power' command appeared modular exponentiation
304
                -- is performed. This state is present until modular exponentiation
305
                -- is calculated
306
                when MAKE_MOD_EXP =>
307
                    modExpCtrlRegEn <= '0';
308
                    RDsig <= '0';
309
                    WRsig <= '0';
310
                    dataToModExpEn <= '0';
311
                    dataToModExpShift <= '0';
312
                    dataFromModExpEn <= '0';
313
                    dataFromModExpShift <= '0';
314
                    serialDataCtrCt <= '0';
315
                    serialDataCtrZero <= '0';
316
                    shiftDataCtrCt <= '0';
317
                    shiftDataCtrZero <= '0';
318
                    muxCtrl <= '1';
319
                    data_in_ready <= '1';
320
 
321
                    -- Here 
322
                    if (readySig = '1') then
323
                        controlStateOut <= "100";
324
                        next_state <= TEMPORARY_STATE;
325
                        temp_state <= info_st;
326
                    else
327
                        controlStateOut <= "001";
328
                        next_state <= MAKE_MOD_EXP;
329
                        temp_state <= nothing;
330
                    end if;
331
                -- When 'mn_show_result' command appears in the core input, 
332
                -- the result from the modular exponentiation feeds the output
333
                -- Here and below state are also for 'data propagation'
334
                when DATA_TO_OUT_PROPAGATE =>
335
                    modExpCtrlRegEn <= '0';
336
                    RDsig <= '0';
337
                    WRsig <= '0';
338
                    dataToModExpEn <= '0';
339
                    dataToModExpShift <= '0';
340
                    shiftDataCtrCt <= '0';
341
                    shiftDataCtrZero <= '0';
342
                    serialDataCtrCt <= '0';
343
                    serialDataCtrZero <= '0';
344
                    dataFromModExpEn <= '1';
345
                    dataFromModExpShift <= '0';
346
                    next_state <= DATA_TO_OUT_PROPAGATE2;
347
                    temp_state <= nothing;
348
                    controlStateOut <= "000";
349
                    muxCtrl <= '0';
350
                    data_in_ready <= '0';
351
                    temp_state <= nothing; -- not important
352
                when DATA_TO_OUT_PROPAGATE2 =>
353
                    modExpCtrlRegEn <= '0';
354
                    RDsig <= '0';
355
                    WRsig <= '1';
356
                    dataToModExpEn <= '0';
357
                    dataToModExpShift <= '0';
358
                    dataFromModExpEn <= '0';
359
                    dataFromModExpShift <= '0';
360
                    serialDataCtrCt <= '0';
361
                    serialDataCtrZero <= '0';
362
                    shiftDataCtrCt <= '0';
363
                    shiftDataCtrZero <= '0';
364
                    next_state <= OUTPUT_DATA;
365
                    temp_state <= nothing;
366
                    controlStateOut <= "000";
367
                    muxCtrl <= '0';
368
                    data_in_ready <= '0';
369
                    temp_state <= nothing; -- not important
370
                -- Here data from parallel form are transformed to serial form.
371
                -- This state is for the control of number of the 8-bit 'packets'
372
                -- of the input data for the modular exponentiator
373
                when OUTPUT_DATA =>
374
                    modExpCtrlRegEn <= '0';
375
                    dataToModExpEn <= '0';
376
                    dataToModExpShift <= '0';
377
                    dataFromModExpEn <= '0';
378
                    dataFromModExpShift <= '0';
379
                    shiftDataCtrCt <= '0';
380
                    shiftDataCtrZero <= '0';
381
                    serialDataCtrZero <= '0';
382
                    RDsig <= '0';
383
                    WRsig <= '1';
384
                    serialDataCtrCt <= '1';
385
                    temp_state <= nothing;
386
                    controlStateOut <= "000";
387
                    muxCtrl <= '0';
388
                    data_in_ready <= '0';
389
                    if (serialDataCtrOut(WORD_INT_LOG) = '1') then
390
                        next_state <= NOP;
391
                    else
392
                        next_state <= MOVE_OUTPUT_DATA;
393
                    end if;
394
                -- This state is for moving bits in data word for the 
395
                -- modular exponentiator counter counts to 8 while data
396
                -- are shifted
397
                when MOVE_OUTPUT_DATA =>
398
                    if (TBEsig = '0') then
399
                        -- Here we have to wait for the sending the previous serial data
400
                        modExpCtrlRegEn <= '0';
401
                        RDsig <= '0';
402
                        WRsig <= '0';
403
                        serialDataCtrCt <= '0';
404
                        dataToModExpEn <= '0';
405
                        dataToModExpShift <= '0';
406
                        dataFromModExpEn <= '0';
407
                        dataFromModExpShift <= '0';
408
                        serialDataCtrZero <= '0';
409
                        shiftDataCtrCt <= '0';
410
                        shiftDataCtrZero <= '0';
411
                        next_state <= MOVE_OUTPUT_DATA;
412
                        controlStateOut <= "000";
413
                        muxCtrl <= '0';
414
                        data_in_ready <= '0';
415
                        temp_state <= nothing; -- not important
416
                    else
417
                        -- Here data are shifted in the output data word
418
                        modExpCtrlRegEn <= '0';
419
                        RDsig <= '0';
420
                        WRsig <= '0';
421
                        serialDataCtrCt <= '0';
422
                        dataToModExpEn <= '0';
423
                        dataToModExpShift <= '0';
424
                        dataFromModExpEn <= '0';
425
                        dataFromModExpShift <= '1';
426
                        shiftDataCtrCt <= '1';
427
                        serialDataCtrZero <= '0';
428
                        controlStateOut <= "000";
429
                        muxCtrl <= '0';
430
                        data_in_ready <= '0';
431
                        temp_state <= nothing; -- not important
432
                        -- Output register shifting DO NOT MODIFY!!!
433
                        if (shiftDataCtrOut(3) = '1') then
434
                            shiftDataCtrCt <= '0';
435
                            shiftDataCtrZero <= '1';
436
                            dataFromModExpShift <= '0';
437
                            next_state <= DATA_TO_OUT_PROPAGATE2;
438
                        else
439
                            shiftDataCtrZero <= '0';
440
                            next_state <= MOVE_OUTPUT_DATA;
441
                        end if;
442
                    end if;
443
                -- State for informing 'the world' about the end of
444
                -- the modular exponentiation
445
                when INFO_STATE =>
446
                    modExpCtrlRegEn <= '0';
447
                    dataToModExpEn <= '0';
448
                    dataToModExpShift <= '0';
449
                    dataFromModExpEn <= '0';
450
                    dataFromModExpShift <= '0';
451
                    serialDataCtrCt <= '0';
452
                    serialDataCtrZero <= '0';
453
                    shiftDataCtrCt <= '0';
454
                    shiftDataCtrZero <= '0';
455
                    if (readySig = '1') then
456
                        controlStateOut <= "100";
457
                    else
458
                        controlStateOut <= "000";
459
                    end if;
460
                    muxCtrl <= '1';
461
                    data_in_ready <= '0';
462
                    temp_state <= nothing; -- not important
463
                    RDsig <= '0';
464
                    WRsig <= '1';
465
                    next_state <= NOP;
466
                -- This state is mostly used for 'data propagation'
467
                -- and control of work of the modular exponentiator
468
                -- its work/state depends on the 'temp_state' signal.
469
                -- temp_state = nothing means that this state is not used
470
                when TEMPORARY_STATE =>
471
                    modExpCtrlRegEn <= '0';
472
                    RDsig <= '0';
473
                    WRsig <= '0';
474
                    dataToModExpEn <= '0';
475
                    dataToModExpShift <= '0';
476
                    dataFromModExpEn <= '0';
477
                    dataFromModExpShift <= '0';
478
                    serialDataCtrCt <= '0';
479
                    serialDataCtrZero <= '0';
480
                    shiftDataCtrCt <= '0';
481
                    shiftDataCtrZero <= '0';
482
                    if (readySig = '1') then
483
                        controlStateOut <= "100";
484
                        next_state <= TEMPORARY_STATE;
485
                        temp_state <= info_st;
486
                    else
487
                        controlStateOut <= "001";
488
                        next_state <= MAKE_MOD_EXP;
489
                        temp_state <= nothing;
490
                    end if;
491
 
492
                    if (temp_state = rd_data) then
493
                        muxCtrl <= '0';
494
                        data_in_ready <= '0';
495
                        next_state <= READ_DATA;
496
                        temp_state <= rd_data;
497
                    elsif (temp_state = mk_fin)  then
498
                       muxCtrl <= '0';
499
                       data_in_ready <= '1';
500
                       next_state <= MAKE_MOD_EXP;
501
                       temp_state <= mk_fin;
502
                    elsif (temp_state = dat_out_prop)  then
503
                       muxCtrl <= '1';
504
                       data_in_ready <= '0';
505
                       next_state <= DATA_TO_OUT_PROPAGATE;
506
                       temp_state <= dat_out_prop;
507
                    elsif (temp_state = info_st) then
508
                       muxCtrl <= '0';
509
                       data_in_ready <= '0';
510
                       next_state <= INFO_STATE;
511
                       temp_state <= info_st;
512
                    elsif (temp_state = mv_dat) then
513
                       muxCtrl <= '0';
514
                       data_in_ready <= '0';
515
                       next_state <= MOVE_DATA;
516
                       temp_state <= mv_dat;
517
                    else
518
                       muxCtrl <= '0';
519
                       data_in_ready <= '0';
520
                       next_state <= NOP;
521
                       temp_state <= nothing;
522
                    end if;
523
            end case;
524
        end process SM;
525
 
526
    state_modifier : process (clk, reset)
527
        begin
528
            if (clk = '1' and clk'Event) then
529
                if (reset = '1') then
530
                    state <= NOP;
531
                else
532
                    state <= next_state;
533
                end if;
534
            end if;
535
        end process state_modifier;
536
 
537
    -- modify for changing width of the hey
538
    -- in fact it is modified from the properties file
539
    dataCounter : counter
540
        generic map(
541
            size => WORD_INT_LOG + 1
542
        )
543
        port map (
544
           count  => serialDataCtrCt,
545
           zero   => serialDataCtrZero,
546
           output => serialDataCtrOut,
547
           clk    => clk,
548
           reset  => reset
549
        );
550
 
551
    shiftCounter : counter
552
        generic map(
553
            size => 4
554
        )
555
        port map (
556
            count  => shiftDataCtrCt,
557
            zero   => shiftDataCtrZero,
558
            output => shiftDataCtrOut,
559
            clk    => clk,
560
            reset  => reset
561
        );
562
 
563
end Behavioral;

powered by: WebSVN 2.1.0

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