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

Subversion Repositories present

[/] [present/] [trunk/] [PureTesting/] [rtl/] [vhdl/] [PresentCommSM.vhd] - Blame information for rev 20

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
----     State machine of 'pure' Present cipher with RS-232        ----
11
---- communication with PC. For more informations see below.       ----
12
---- To Do:                                                        ----
13
----                                                               ----
14
---- Author(s):                                                    ----
15
---- - Krzysztof Gajewski, gajos@opencores.org                     ----
16
----                       k.gajewski@gmail.com                    ----
17
----                                                               ----
18
-----------------------------------------------------------------------
19
----                                                               ----
20
---- Copyright (C) 2013 Authors and OPENCORES.ORG                  ----
21
----                                                               ----
22
---- This source file may be used and distributed without          ----
23
---- restriction provided that this copyright statement is not     ----
24
---- removed from the file and that any derivative work contains   ----
25
---- the original copyright notice and the associated disclaimer.  ----
26
----                                                               ----
27
---- This source file is free software; you can redistribute it    ----
28
---- and-or modify it under the terms of the GNU Lesser General    ----
29
---- Public License as published by the Free Software Foundation;  ----
30
---- either version 2.1 of the License, or (at your option) any    ----
31
---- later version.                                                ----
32
----                                                               ----
33
---- This source is distributed in the hope that it will be        ----
34
---- useful, but WITHOUT ANY WARRANTY; without even the implied    ----
35
---- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR       ----
36
---- PURPOSE. See the GNU Lesser General Public License for more   ----
37
---- details.                                                      ----
38
----                                                               ----
39
---- You should have received a copy of the GNU Lesser General     ----
40
---- Public License along with this source; if not, download it    ----
41
---- from http://www.opencores.org/lgpl.shtml                      ----
42
----                                                               ----
43
-----------------------------------------------------------------------
44 3 gajos
library IEEE;
45
use IEEE.STD_LOGIC_1164.ALL;
46
use work.kody.ALL;
47
 
48
-- Uncomment the following library declaration if using
49
-- arithmetic functions with Signed or Unsigned values
50
--use IEEE.NUMERIC_STD.ALL;
51
 
52
-- Uncomment the following library declaration if instantiating
53
-- any Xilinx primitives in this code.
54
--library UNISIM;
55
--use UNISIM.VComponents.all;
56
 
57
entity PresentCommSM is
58
        port (
59
                clk                             : in STD_LOGIC;
60
                reset                           : in STD_LOGIC;
61
                RDAsig                  : in STD_LOGIC;
62
                TBEsig                  : in STD_LOGIC;
63
                RDsig                           : out STD_LOGIC;
64
                WRsig                           : out STD_LOGIC;
65
                textDataEn     : out STD_LOGIC;
66
                textDataShift   : out STD_LOGIC;
67
                keyDataEn               : out STD_LOGIC;
68
                keyDataShift    : out STD_LOGIC;
69
                ciphDataEn     : out STD_LOGIC;
70
                ciphDataShift  : out STD_LOGIC;
71
                startSig                        : out STD_LOGIC;
72
                readySig                        : in STD_LOGIC
73
        );
74
end PresentCommSM;
75
 
76
architecture Behavioral of PresentCommSM is
77
 
78 4 gajos
-- counter used for determine number of readed/sended data (key, text, result)
79 3 gajos
component counter is
80
        generic (
81
                w_5 : integer := 5
82
        );
83
        port (
84
                clk, reset, cnt_res : in std_logic;
85
                num : out std_logic_vector (w_5-1 downto 0)
86
        );
87
end component counter;
88
 
89 4 gajos
-- signals
90
 
91 3 gajos
signal state      : stany_comm := NOP;
92
signal next_state : stany_comm := NOP;
93
 
94
-- modify for variable key size
95
signal serialDataCtrCt    : STD_LOGIC;
96
signal serialDataCtrOut   : STD_LOGIC_VECTOR(3 downto 0);
97
signal serialDataCtrReset : STD_LOGIC;
98
signal ctrReset                   : STD_LOGIC;
99
-- DO NOT MODIFY!!!
100
signal shiftDataCtrCt    : STD_LOGIC;
101
signal shiftDataCtrOut   : STD_LOGIC_VECTOR(2 downto 0);
102
 
103
begin
104 4 gajos
    -- In this state machine is determined, that firs data should be 64 bit text
105
        -- after text, key should appear. After receiving last byte of key, ciphertext
106
        -- is counted. And later it is sended back to PC.
107 3 gajos
        ctrReset <= serialDataCtrReset or reset;
108
        SM : process(state, RDAsig, TBEsig, shiftDataCtrOut, serialDataCtrOut, readySig)
109
                begin
110 4 gajos
                        case state is
111
                            -- No operation - waiting for incoming data
112 3 gajos
                                when NOP =>
113
                                        RDsig                                    <= '0';
114
                                        WRsig                                    <= '0';
115
                                        textDataEn                       <= '0';
116
                                        textDataShift            <= '0';
117
                                        keyDataEn                        <= '0';
118
                                        keyDataShift             <= '0';
119
                                        ciphDataEn                       <= '0';
120
                                        ciphDataShift            <= '0';
121
                                        startSig                                 <= '0';
122
                                        serialDataCtrCt  <= '0';
123
                                        shiftDataCtrCt           <= '0';
124
                                        serialDataCtrReset <= '0';
125 4 gajos
                                        -- data has come
126 3 gajos
                                        if (RDAsig = '1') then
127
                                                next_state <= READ_DATA_TEXT;
128
                                        else
129
                                                next_state <= NOP;
130
                                        end if;
131 4 gajos
                                -- Text data enable and read data
132 3 gajos
                                when READ_DATA_TEXT =>
133
                                        RDsig                                    <= '1';
134
                                        WRsig                                    <= '0';
135
                                        textDataEn                       <= '1';
136
                                        textDataShift            <= '0';
137
                                        keyDataEn                        <= '0';
138
                                        keyDataShift             <= '0';
139
                                        ciphDataEn                       <= '0';
140
                                        ciphDataShift            <= '0';
141
                                        startSig                                 <= '0';
142 4 gajos
                                        -- counter of retrieved bytes
143 3 gajos
                                        serialDataCtrCt  <= '1';
144
                                        shiftDataCtrCt           <= '0';
145
                                        serialDataCtrReset <= '0';
146
                                        next_state <= DECODE_READ_TEXT;
147 4 gajos
                                -- Data readed, stop counter and check if proper number of byte
148
                                -- was readed
149 3 gajos
                                when DECODE_READ_TEXT =>
150
                                        RDsig                                    <= '0';
151
                                        WRsig                                    <= '0';
152
                                        textDataEn                       <= '0';
153
                                        textDataShift            <= '0';
154
                                        keyDataEn                        <= '0';
155
                                        keyDataShift             <= '0';
156
                                        ciphDataEn                       <= '0';
157
                                        ciphDataShift            <= '0';
158
                                        startSig                                 <= '0';
159
                                        serialDataCtrCt  <= '0';
160
                                        shiftDataCtrCt           <= '0';
161 4 gajos
                                        -- 8 bytes should be readed
162
                                        if (serialDataCtrOut(3 downto 0) = "1000") then
163
                                            -- 8 bytes was readed
164 3 gajos
                                                next_state <= TEMP_STATE;
165 4 gajos
                                        else
166
                                            -- 8 bytes was not readed
167 3 gajos
                                                next_state <= MOVE_TEXT;
168
                                        end if;
169 4 gajos
                                -- Reset counter for next reading
170 3 gajos
                                when TEMP_STATE =>
171
                                        RDsig                                    <= '0';
172
                                        WRsig                                    <= '0';
173
                                        textDataEn                       <= '0';
174
                                        textDataShift            <= '0';
175
                                        keyDataEn                        <= '0';
176
                                        keyDataShift             <= '0';
177
                                        ciphDataEn                       <= '0';
178
                                        ciphDataShift            <= '0';
179
                                        startSig                                 <= '0';
180
                                        serialDataCtrCt  <= '0';
181
                                        shiftDataCtrCt           <= '0';
182
                                        serialDataCtrReset <= '1';
183
                                        next_state <= NOP_FOR_KEY;
184 4 gajos
                                -- Here data are shfted in shift register - another shift counter are used
185 3 gajos
                                when MOVE_TEXT =>
186
                                        RDsig                                    <= '0';
187
                                        WRsig                                    <= '0';
188
                                        textDataEn                       <= '0';
189
                                        textDataShift            <= '1';
190
                                        keyDataEn                        <= '0';
191
                                        keyDataShift             <= '0';
192
                                        ciphDataEn                       <= '0';
193
                                        ciphDataShift            <= '0';
194
                                        startSig                                 <= '0';
195
                                        serialDataCtrCt  <= '0';
196
                                        shiftDataCtrCt   <= '1';
197
                                        serialDataCtrReset <= '0';
198
                                        if (shiftDataCtrOut(2 downto 0) = "111") then
199
                                                next_state <= NOP;
200
                                        else
201
                                                next_state <= MOVE_TEXT;
202
                                        end if;
203 4 gajos
                                -- "No operation 2" waiting for data - it could be optimized in way,
204
                                -- that waiting for key and text could be the same state, but it was
205
                                -- intentionally separated.
206 3 gajos
                                when NOP_FOR_KEY        =>
207
                                        RDsig                                    <= '0';
208
                                        WRsig                                    <= '0';
209
                                        textDataEn                       <= '0';
210
                                        textDataShift            <= '0';
211
                                        keyDataEn                        <= '0';
212
                                        keyDataShift             <= '0';
213
                                        ciphDataEn                       <= '0';
214
                                        ciphDataShift            <= '0';
215
                                        startSig                                 <= '0';
216
                                        serialDataCtrCt  <= '0';
217
                                        shiftDataCtrCt           <= '0';
218
                                        serialDataCtrReset <= '0';
219 4 gajos
                                        if (RDAsig = '1') then
220
                                            -- data has come
221 3 gajos
                                                next_state <= READ_DATA_KEY;
222
                                        else
223
                                                next_state <= NOP_FOR_KEY;
224
                                        end if;
225 4 gajos
                            -- Key data enable and read data
226 3 gajos
                                when READ_DATA_KEY =>
227
                                        RDsig                                    <= '1';
228
                                        WRsig                                    <= '0';
229
                                        textDataEn                       <= '0';
230
                                        textDataShift            <= '0';
231
                                        keyDataEn                        <= '1';
232
                                        keyDataShift             <= '0';
233
                                        ciphDataEn                       <= '0';
234
                                        ciphDataShift            <= '0';
235
                                        startSig                                 <= '0';
236 4 gajos
                                        -- counter of retrieved bytes
237 3 gajos
                                        serialDataCtrCt  <= '1';
238
                                        shiftDataCtrCt           <= '0';
239
                                        serialDataCtrReset <= '0';
240
                                        next_state <= DECODE_READ_KEY;
241 4 gajos
                                -- Data readed, stop counter and check if proper number of byte
242
                                -- was readed
243 3 gajos
                                when DECODE_READ_KEY =>
244
                                        RDsig                                    <= '0';
245
                                        WRsig                                    <= '0';
246
                                        textDataEn                       <= '0';
247
                                        textDataShift            <= '0';
248
                                        keyDataEn                        <= '0';
249
                                        keyDataShift             <= '0';
250
                                        ciphDataEn                       <= '0';
251
                                        ciphDataShift            <= '0';
252
                                        startSig                                 <= '0';
253
                                        serialDataCtrCt  <= '0';
254
                                        shiftDataCtrCt           <= '0';
255
                                        serialDataCtrReset <= '0';
256 4 gajos
                                        -- 10 bytes should be readed
257
                                        if (serialDataCtrOut(3 downto 0) = "1010") then
258
                                            -- 10 bytes was readed
259 3 gajos
                                                next_state <= TEMP2_STATE;
260 4 gajos
                                        else
261
                                            -- 10 bytes was not readed
262 3 gajos
                                                next_state <= MOVE_KEY;
263
                                        end if;
264 4 gajos
                                -- Reset counter for next reading
265 3 gajos
                                when TEMP2_STATE =>
266
                                        RDsig                                    <= '0';
267
                                        WRsig                                    <= '0';
268
                                        textDataEn                       <= '0';
269
                                        textDataShift            <= '0';
270
                                        keyDataEn                        <= '0';
271
                                        keyDataShift             <= '0';
272
                                        ciphDataEn                       <= '0';
273
                                        ciphDataShift            <= '0';
274
                                        startSig                                 <= '0';
275
                                        serialDataCtrCt  <= '0';
276
                                        shiftDataCtrCt           <= '0';
277
                                        serialDataCtrReset <= '1';
278
                                        next_state <= PRESENT_ENCODE;
279 4 gajos
                                -- Here data are shfted in shift register - another shift counter are used
280 3 gajos
                                when MOVE_KEY =>
281
                                        RDsig                                    <= '0';
282
                                        WRsig                                    <= '0';
283
                                        textDataEn                       <= '0';
284
                                        textDataShift            <= '0';
285
                                        keyDataEn                        <= '0';
286
                                        keyDataShift             <= '1';
287
                                        ciphDataEn                       <= '0';
288
                                        ciphDataShift            <= '0';
289
                                        startSig                                 <= '0';
290
                                        serialDataCtrCt  <= '0';
291
                                        shiftDataCtrCt           <= '1';
292
                                        serialDataCtrReset <= '0';
293
                                        if (shiftDataCtrOut(2 downto 0) = "111") then
294
                                                next_state <= NOP_FOR_KEY;
295
                                        else
296
                                                next_state <= MOVE_KEY;
297
                                        end if;
298 4 gajos
                                -- All suitable data was readed Present encode start
299 3 gajos
                                when PRESENT_ENCODE =>
300
                                        RDsig                                    <= '0';
301
                                        WRsig                                    <= '0';
302
                                        textDataEn                       <= '0';
303
                                        textDataShift            <= '0';
304
                                        keyDataEn                        <= '0';
305
                                        keyDataShift             <= '0';
306
                                        ciphDataShift            <= '0';
307
                                        startSig                                 <= '1';
308
                                        serialDataCtrCt  <= '0';
309
                                        shiftDataCtrCt           <= '0';
310
                                        serialDataCtrReset <= '0';
311 4 gajos
                                        -- change state if Present result ready
312 3 gajos
                                        if (readySig = '1') then
313
                                                ciphDataEn                       <= '1';
314
                                                next_state <= WRITE_OUT;
315
                                        else
316
                                                ciphDataEn                       <= '0';
317
                                                next_state <= PRESENT_ENCODE;
318
                                        end if;
319 4 gajos
                                -- similar control of writing result as during reading
320 3 gajos
                                when WRITE_OUT =>
321
                                        RDsig                                    <= '0';
322
                                        textDataEn                       <= '0';
323
                                        textDataShift            <= '0';
324
                                        keyDataEn                        <= '0';
325
                                        keyDataShift             <= '0';
326
                                        ciphDataEn                       <= '0';
327
                                        ciphDataShift            <= '0';
328
                                        startSig                                 <= '1';
329
                                        serialDataCtrCt  <= '1';
330
                                        shiftDataCtrCt           <= '0';
331
                                        serialDataCtrReset <= '0';
332
                                        if (serialDataCtrOut = "1000") then
333
                                                WRsig                                    <= '0';
334
                                                next_state <= TEMP_OUT;
335
                                        else
336
                                                WRsig                                    <= '1';
337
                                                next_state <= MOVE_OUT;
338
                                        end if;
339 4 gajos
                                -- all data was sended - start new Present encode cycle
340 3 gajos
                                when TEMP_OUT =>
341
                                        RDsig                                    <= '0';
342
                                        WRsig                                    <= '0';
343
                                        textDataEn                       <= '0';
344
                                        textDataShift            <= '0';
345
                                        keyDataEn                        <= '0';
346
                                        keyDataShift             <= '0';
347
                                        ciphDataEn                       <= '0';
348
                                        ciphDataShift            <= '0';
349
                                        startSig                                 <= '1';
350
                                        serialDataCtrCt  <= '0';
351
                                        shiftDataCtrCt           <= '0';
352
                                        serialDataCtrReset <= '1';
353
                                        next_state <= NOP;
354
                                when MOVE_OUT =>
355
                                        if (TBEsig = '0') then
356
                                                RDsig                                    <= '0';
357
                                                WRsig                                    <= '0';
358
                                                textDataEn                       <= '0';
359
                                                textDataShift            <= '0';
360
                                                keyDataEn                        <= '0';
361
                                                keyDataShift             <= '0';
362
                                                ciphDataEn                       <= '0';
363
                                                ciphDataShift            <= '0';
364
                                                startSig                                 <= '1';
365
                                                serialDataCtrCt  <= '0';
366
                                                shiftDataCtrCt           <= '0';
367
                                                serialDataCtrReset <= '0';
368
                                                next_state <= MOVE_OUT;
369
                                        else
370
                                                RDsig                                    <= '0';
371
                                                WRsig                                    <= '0';
372
                                                textDataEn                       <= '0';
373
                                                textDataShift            <= '0';
374
                                                keyDataEn                        <= '0';
375
                                                keyDataShift             <= '0';
376
                                                ciphDataEn                       <= '0';
377
                                                ciphDataShift            <= '1';
378
                                                startSig                                 <= '1';
379
                                                serialDataCtrCt  <= '0';
380
                                                shiftDataCtrCt           <= '1';
381
                                                serialDataCtrReset <= '0';
382
                                                if (shiftDataCtrOut = "111") then
383
                                                        next_state <= WRITE_OUT;
384
                                                else
385
                                                        next_state <= MOVE_OUT;
386
                                                end if;
387
                                        end if;
388
                        end case;
389
                end process SM;
390
 
391
        state_modifier : process (clk, reset)
392
                begin
393 20 gajos
                   if (reset = '1') then
394
                                        state <= NOP;
395
                        elsif (clk = '1' and clk'Event) then
396
                                        state <= next_state;
397 3 gajos
                        end if;
398
                end process state_modifier;
399 4 gajos
 
400
    -- counter for controling number of bytes of readed data
401 3 gajos
        dataCounter : counter
402
                generic map(
403
                        w_5 => 4
404
                )
405
                port map (
406
                        cnt_res  => serialDataCtrCt,
407
                        num => serialDataCtrOut,
408
                        clk    => clk,
409
                        reset  => ctrReset
410
                );
411 4 gajos
 
412
        -- counter for controling number of shifted bits of readed data
413 3 gajos
        shiftCounter : counter
414
                generic map(
415
                        w_5 => 3
416
                )
417
                port map (
418
                        cnt_res  => shiftDataCtrCt,
419
                        num => shiftDataCtrOut,
420
                        clk    => clk,
421
                        reset  => reset
422
                );
423
 
424
end Behavioral;
425
 

powered by: WebSVN 2.1.0

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