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

Subversion Repositories present

[/] [present/] [trunk/] [DecodeTesting/] [rtl/] [vhdl/] [PresentDecodeCommSM.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 decoder with RS-232       ----
11
---- communication with PC. Some names can be unclear because of   ----
12
---- the same type used as in PresentCommSM (in fact the same cycle----
13
---- For more informations see below.                              ----
14
---- To Do:                                                        ----
15
----                                                               ----
16
---- Author(s):                                                    ----
17
---- - Krzysztof Gajewski, gajos@opencores.org                     ----
18
----                       k.gajewski@gmail.com                    ----
19
----                                                               ----
20
-----------------------------------------------------------------------
21
----                                                               ----
22
---- Copyright (C) 2013 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 3 gajos
library IEEE;
47
use IEEE.STD_LOGIC_1164.ALL;
48
use work.kody.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 PresentDecodeCommSM 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
                textDataEn     : out STD_LOGIC;
68
                textDataShift   : out STD_LOGIC;
69
                keyDataEn               : out STD_LOGIC;
70
                keyDataShift    : out STD_LOGIC;
71
                ciphDataEn     : out STD_LOGIC;
72
                ciphDataShift  : out STD_LOGIC;
73
                startSig                        : out STD_LOGIC;
74
                readySig                        : in STD_LOGIC
75
        );
76
end PresentDecodeCommSM;
77
 
78
architecture Behavioral of PresentDecodeCommSM is
79
 
80 4 gajos
-- counter used for determine number of readed/sended data (key, cipher, result)
81 3 gajos
component counter is
82
        generic (
83
                w_5 : integer := 5
84
        );
85
        port (
86
                clk, reset, cnt_res : in std_logic;
87
                num : out std_logic_vector (w_5-1 downto 0)
88
        );
89
end component counter;
90
 
91 4 gajos
-- signals
92
 
93 3 gajos
signal state      : stany_comm := NOP;
94
signal next_state : stany_comm := NOP;
95
 
96
-- modify for variable key size
97
signal serialDataCtrCt    : STD_LOGIC;
98
signal serialDataCtrOut   : STD_LOGIC_VECTOR(3 downto 0);
99
signal serialDataCtrReset : STD_LOGIC;
100
signal ctrReset                   : STD_LOGIC;
101
-- DO NOT MODIFY!!!
102
signal shiftDataCtrCt    : STD_LOGIC;
103
signal shiftDataCtrOut   : STD_LOGIC_VECTOR(2 downto 0);
104
 
105
begin
106
        ctrReset <= serialDataCtrReset or reset;
107
        SM : process(state, RDAsig, TBEsig, shiftDataCtrOut, serialDataCtrOut, readySig)
108
                begin
109 4 gajos
                        case state is
110
                            -- No operation - waiting for incoming data
111 3 gajos
                                when NOP =>
112
                                        RDsig                                    <= '0';
113
                                        WRsig                                    <= '0';
114
                                        textDataEn                       <= '0';
115
                                        textDataShift            <= '0';
116
                                        keyDataEn                        <= '0';
117
                                        keyDataShift             <= '0';
118
                                        ciphDataEn                       <= '0';
119
                                        ciphDataShift            <= '0';
120
                                        startSig                                 <= '0';
121
                                        serialDataCtrCt  <= '0';
122
                                        shiftDataCtrCt           <= '0';
123
                                        serialDataCtrReset <= '0';
124 4 gajos
                                        -- data has come
125 3 gajos
                                        if (RDAsig = '1') then
126
                                                next_state <= READ_DATA_TEXT;
127
                                        else
128
                                                next_state <= NOP;
129
                                        end if;
130 4 gajos
                                -- Cipher data enable and read data
131 3 gajos
                                when READ_DATA_TEXT =>
132
                                        RDsig                                    <= '1';
133
                                        WRsig                                    <= '0';
134
                                        textDataEn                       <= '1';
135
                                        textDataShift            <= '0';
136
                                        keyDataEn                        <= '0';
137
                                        keyDataShift             <= '0';
138
                                        ciphDataEn                       <= '0';
139
                                        ciphDataShift            <= '0';
140
                                        startSig                                 <= '0';
141 4 gajos
                                        -- counter of retrieved bytes
142 3 gajos
                                        serialDataCtrCt  <= '1';
143
                                        shiftDataCtrCt           <= '0';
144
                                        serialDataCtrReset <= '0';
145
                                        next_state <= DECODE_READ_TEXT;
146 4 gajos
                                -- Cipher data readed, stop counter and check if proper number of byte
147
                                -- was readed
148 3 gajos
                                when DECODE_READ_TEXT =>
149
                                        RDsig                                    <= '0';
150
                                        WRsig                                    <= '0';
151
                                        textDataEn                       <= '0';
152
                                        textDataShift            <= '0';
153
                                        keyDataEn                        <= '0';
154
                                        keyDataShift             <= '0';
155
                                        ciphDataEn                       <= '0';
156
                                        ciphDataShift            <= '0';
157
                                        startSig                                 <= '0';
158
                                        serialDataCtrCt  <= '0';
159
                                        shiftDataCtrCt           <= '0';
160 4 gajos
                                        -- 8 bytes should be readed
161
                                        if (serialDataCtrOut(3 downto 0) = "1000") then
162
                                            -- 8 bytes was readed
163 3 gajos
                                                next_state <= TEMP_STATE;
164 4 gajos
                                        else
165
                                            -- 8 bytes was not readed
166 3 gajos
                                                next_state <= MOVE_TEXT;
167
                                        end if;
168 4 gajos
                                -- Reset counter for next reading
169 3 gajos
                                when TEMP_STATE =>
170
                                        RDsig                                    <= '0';
171
                                        WRsig                                    <= '0';
172
                                        textDataEn                       <= '0';
173
                                        textDataShift            <= '0';
174
                                        keyDataEn                        <= '0';
175
                                        keyDataShift             <= '0';
176
                                        ciphDataEn                       <= '0';
177
                                        ciphDataShift            <= '0';
178
                                        startSig                                 <= '0';
179
                                        serialDataCtrCt  <= '0';
180
                                        shiftDataCtrCt           <= '0';
181
                                        serialDataCtrReset <= '1';
182
                                        next_state <= NOP_FOR_KEY;
183 4 gajos
                                -- Here data are shfted in shift register - another shift counter are used
184 3 gajos
                                when MOVE_TEXT =>
185
                                        RDsig                                    <= '0';
186
                                        WRsig                                    <= '0';
187
                                        textDataEn                       <= '0';
188
                                        textDataShift            <= '1';
189
                                        keyDataEn                        <= '0';
190
                                        keyDataShift             <= '0';
191
                                        ciphDataEn                       <= '0';
192
                                        ciphDataShift            <= '0';
193
                                        startSig                                 <= '0';
194
                                        serialDataCtrCt  <= '0';
195
                                        shiftDataCtrCt   <= '1';
196
                                        serialDataCtrReset <= '0';
197
                                        if (shiftDataCtrOut(2 downto 0) = "111") then
198
                                                next_state <= NOP;
199
                                        else
200
                                                next_state <= MOVE_TEXT;
201
                                        end if;
202 4 gajos
                                -- "No operation 2" waiting for data - it could be optimized in way,
203
                                -- that waiting for key and text could be the same state, but it was
204
                                -- intentionally separated.
205 3 gajos
                                when NOP_FOR_KEY        =>
206
                                        RDsig                                    <= '0';
207
                                        WRsig                                    <= '0';
208
                                        textDataEn                       <= '0';
209
                                        textDataShift            <= '0';
210
                                        keyDataEn                        <= '0';
211
                                        keyDataShift             <= '0';
212
                                        ciphDataEn                       <= '0';
213
                                        ciphDataShift            <= '0';
214
                                        startSig                                 <= '0';
215
                                        serialDataCtrCt  <= '0';
216
                                        shiftDataCtrCt           <= '0';
217
                                        serialDataCtrReset <= '0';
218 4 gajos
                                        -- data has come
219 3 gajos
                                        if (RDAsig = '1') then
220
                                                next_state <= READ_DATA_KEY;
221
                                        else
222
                                                next_state <= NOP_FOR_KEY;
223
                                        end if;
224 4 gajos
                                -- Key data enable and read data
225 3 gajos
                                when READ_DATA_KEY =>
226
                                        RDsig                                    <= '1';
227
                                        WRsig                                    <= '0';
228
                                        textDataEn                       <= '0';
229
                                        textDataShift            <= '0';
230
                                        keyDataEn                        <= '1';
231
                                        keyDataShift             <= '0';
232
                                        ciphDataEn                       <= '0';
233
                                        ciphDataShift            <= '0';
234
                                        startSig                                 <= '0';
235 4 gajos
                                        -- counter of retrieved bytes
236 3 gajos
                                        serialDataCtrCt  <= '1';
237
                                        shiftDataCtrCt           <= '0';
238
                                        serialDataCtrReset <= '0';
239
                                        next_state <= DECODE_READ_KEY;
240 4 gajos
                                -- Data readed, stop counter and check if proper number of byte
241
                                -- was readed
242 3 gajos
                                when DECODE_READ_KEY =>
243
                                        RDsig                                    <= '0';
244
                                        WRsig                                    <= '0';
245
                                        textDataEn                       <= '0';
246
                                        textDataShift            <= '0';
247
                                        keyDataEn                        <= '0';
248
                                        keyDataShift             <= '0';
249
                                        ciphDataEn                       <= '0';
250
                                        ciphDataShift            <= '0';
251
                                        startSig                                 <= '0';
252
                                        serialDataCtrCt  <= '0';
253
                                        shiftDataCtrCt           <= '0';
254
                                        serialDataCtrReset <= '0';
255 4 gajos
                                        -- 10 bytes should be readed
256
                                        if (serialDataCtrOut(3 downto 0) = "1010") then
257
                                            -- 10 bytes was readed
258 3 gajos
                                                next_state <= TEMP2_STATE;
259 4 gajos
                                        else
260
                                            -- 10 bytes was not readed
261 3 gajos
                                                next_state <= MOVE_KEY;
262
                                        end if;
263 4 gajos
                                -- Reset counter for next reading
264 3 gajos
                                when TEMP2_STATE =>
265
                                        RDsig                                    <= '0';
266
                                        WRsig                                    <= '0';
267
                                        textDataEn                       <= '0';
268
                                        textDataShift            <= '0';
269
                                        keyDataEn                        <= '0';
270
                                        keyDataShift             <= '0';
271
                                        ciphDataEn                       <= '0';
272
                                        ciphDataShift            <= '0';
273
                                        startSig                                 <= '0';
274
                                        serialDataCtrCt  <= '0';
275
                                        shiftDataCtrCt           <= '0';
276
                                        serialDataCtrReset <= '1';
277
                                        next_state <= PRESENT_ENCODE;
278 4 gajos
                                -- Here data are shfted in shift register - another shift counter are used
279 3 gajos
                                when MOVE_KEY =>
280
                                        RDsig                                    <= '0';
281
                                        WRsig                                    <= '0';
282
                                        textDataEn                       <= '0';
283
                                        textDataShift            <= '0';
284
                                        keyDataEn                        <= '0';
285
                                        keyDataShift             <= '1';
286
                                        ciphDataEn                       <= '0';
287
                                        ciphDataShift            <= '0';
288
                                        startSig                                 <= '0';
289
                                        serialDataCtrCt  <= '0';
290
                                        shiftDataCtrCt           <= '1';
291
                                        serialDataCtrReset <= '0';
292
                                        if (shiftDataCtrOut(2 downto 0) = "111") then
293
                                                next_state <= NOP_FOR_KEY;
294
                                        else
295
                                                next_state <= MOVE_KEY;
296
                                        end if;
297 4 gajos
                                -- All suitable data was readed Present encode start
298 3 gajos
                                when PRESENT_ENCODE =>
299
                                        RDsig                                    <= '0';
300
                                        WRsig                                    <= '0';
301
                                        textDataEn                       <= '0';
302
                                        textDataShift            <= '0';
303
                                        keyDataEn                        <= '0';
304
                                        keyDataShift             <= '0';
305
                                        ciphDataShift            <= '0';
306
                                        startSig                                 <= '1';
307
                                        serialDataCtrCt  <= '0';
308
                                        shiftDataCtrCt           <= '0';
309
                                        serialDataCtrReset <= '0';
310 4 gajos
                                        -- change state if Present result ready
311 3 gajos
                                        if (readySig = '1') then
312
                                                ciphDataEn                       <= '1';
313
                                                next_state <= WRITE_OUT;
314
                                        else
315
                                                ciphDataEn                       <= '0';
316
                                                next_state <= PRESENT_ENCODE;
317
                                        end if;
318 4 gajos
                                -- similar control of writing result as during reading
319 3 gajos
                                when WRITE_OUT =>
320
                                        RDsig                                    <= '0';
321
                                        textDataEn                       <= '0';
322
                                        textDataShift            <= '0';
323
                                        keyDataEn                        <= '0';
324
                                        keyDataShift             <= '0';
325
                                        ciphDataEn                       <= '0';
326
                                        ciphDataShift            <= '0';
327
                                        startSig                                 <= '1';
328
                                        serialDataCtrCt  <= '1';
329
                                        shiftDataCtrCt           <= '0';
330
                                        serialDataCtrReset <= '0';
331
                                        if (serialDataCtrOut = "1000") then
332
                                                WRsig                                    <= '0';
333
                                                next_state <= TEMP_OUT;
334
                                        else
335
                                                WRsig                                    <= '1';
336
                                                next_state <= MOVE_OUT;
337
                                        end if;
338 4 gajos
                                -- all data was sended - start new Present encode cycle
339 3 gajos
                                when TEMP_OUT =>
340
                                        RDsig                                    <= '0';
341
                                        WRsig                                    <= '0';
342
                                        textDataEn                       <= '0';
343
                                        textDataShift            <= '0';
344
                                        keyDataEn                        <= '0';
345
                                        keyDataShift             <= '0';
346
                                        ciphDataEn                       <= '0';
347
                                        ciphDataShift            <= '0';
348
                                        startSig                                 <= '1';
349
                                        serialDataCtrCt  <= '0';
350
                                        shiftDataCtrCt           <= '0';
351
                                        serialDataCtrReset <= '1';
352
                                        next_state <= NOP;
353
                                when MOVE_OUT =>
354
                                        if (TBEsig = '0') then
355
                                                RDsig                                    <= '0';
356
                                                WRsig                                    <= '0';
357
                                                textDataEn                       <= '0';
358
                                                textDataShift            <= '0';
359
                                                keyDataEn                        <= '0';
360
                                                keyDataShift             <= '0';
361
                                                ciphDataEn                       <= '0';
362
                                                ciphDataShift            <= '0';
363
                                                startSig                                 <= '1';
364
                                                serialDataCtrCt  <= '0';
365
                                                shiftDataCtrCt           <= '0';
366
                                                serialDataCtrReset <= '0';
367
                                                next_state <= MOVE_OUT;
368
                                        else
369
                                                RDsig                                    <= '0';
370
                                                WRsig                                    <= '0';
371
                                                textDataEn                       <= '0';
372
                                                textDataShift            <= '0';
373
                                                keyDataEn                        <= '0';
374
                                                keyDataShift             <= '0';
375
                                                ciphDataEn                       <= '0';
376
                                                ciphDataShift            <= '1';
377
                                                startSig                                 <= '1';
378
                                                serialDataCtrCt  <= '0';
379
                                                shiftDataCtrCt           <= '1';
380
                                                serialDataCtrReset <= '0';
381
                                                if (shiftDataCtrOut = "111") then
382
                                                        next_state <= WRITE_OUT;
383
                                                else
384
                                                        next_state <= MOVE_OUT;
385
                                                end if;
386
                                        end if;
387
                        end case;
388
                end process SM;
389
 
390
        state_modifier : process (clk, reset)
391
                begin
392 20 gajos
                   if (reset = '1') then
393
                                        state <= NOP;
394
                        elsif (clk = '1' and clk'Event) then
395
                                        state <= next_state;
396
                        end if;
397 3 gajos
                end process state_modifier;
398 4 gajos
 
399
    -- counter for controling number of bytes of readed data
400 3 gajos
        dataCounter : counter
401
                generic map(
402
                        w_5 => 4
403
                )
404
                port map (
405
                        cnt_res  => serialDataCtrCt,
406
                        num => serialDataCtrOut,
407
                        clk    => clk,
408
                        reset  => ctrReset
409
                );
410 4 gajos
 
411
        -- counter for controling number of shifted bits of readed data
412 3 gajos
        shiftCounter : counter
413
                generic map(
414
                        w_5 => 3
415
                )
416
                port map (
417
                        cnt_res  => shiftDataCtrCt,
418
                        num => shiftDataCtrOut,
419
                        clk    => clk,
420
                        reset  => reset
421
                );
422
 
423
end Behavioral;
424
 

powered by: WebSVN 2.1.0

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