1 |
21 |
arif_endro |
-- ------------------------------------------------------------------------
|
2 |
15 |
arif_endro |
-- Copyright (C) 2005 Arif Endro Nugroho
|
3 |
21 |
arif_endro |
-- All rights reserved.
|
4 |
2 |
arif_endro |
--
|
5 |
21 |
arif_endro |
-- Redistribution and use in source and binary forms, with or without
|
6 |
|
|
-- modification, are permitted provided that the following conditions
|
7 |
|
|
-- are met:
|
8 |
2 |
arif_endro |
--
|
9 |
21 |
arif_endro |
-- 1. Redistributions of source code must retain the above copyright
|
10 |
|
|
-- notice, this list of conditions and the following disclaimer.
|
11 |
|
|
-- 2. Redistributions in binary form must reproduce the above copyright
|
12 |
|
|
-- notice, this list of conditions and the following disclaimer in the
|
13 |
|
|
-- documentation and/or other materials provided with the distribution.
|
14 |
|
|
-- 3. The name of Arif Endro Nugroho may not be used to endorse or promote
|
15 |
|
|
-- products derived from this software without specific prior written
|
16 |
|
|
-- permission.
|
17 |
2 |
arif_endro |
--
|
18 |
21 |
arif_endro |
-- THIS SOFTWARE IS PROVIDED BY ARIF ENDRO NUGROHO "AS IS" AND ANY EXPRESS
|
19 |
|
|
-- OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
20 |
|
|
-- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
21 |
|
|
-- DISCLAIMED. IN NO EVENT SHALL ARIF ENDRO NUGROHO BE LIABLE FOR ANY
|
22 |
|
|
-- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
23 |
|
|
-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
24 |
|
|
-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
25 |
|
|
-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
26 |
|
|
-- STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
27 |
|
|
-- ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
28 |
|
|
-- POSSIBILITY OF SUCH DAMAGE.
|
29 |
2 |
arif_endro |
--
|
30 |
21 |
arif_endro |
-- End Of License.
|
31 |
|
|
-- ------------------------------------------------------------------------
|
32 |
2 |
arif_endro |
|
33 |
|
|
library ieee;
|
34 |
|
|
use ieee.std_logic_1164.all;
|
35 |
|
|
use ieee.std_logic_unsigned.all;
|
36 |
|
|
|
37 |
|
|
entity folded_register is
|
38 |
|
|
port (
|
39 |
|
|
clk_i : in std_logic;
|
40 |
|
|
enc_i : in std_logic;
|
41 |
|
|
load_i : in std_logic;
|
42 |
|
|
data_i : in std_logic_vector (127 downto 000);
|
43 |
|
|
key_i : in std_logic_vector (127 downto 000);
|
44 |
|
|
di_0_i : in std_logic_vector (007 downto 000);
|
45 |
|
|
di_1_i : in std_logic_vector (007 downto 000);
|
46 |
|
|
di_2_i : in std_logic_vector (007 downto 000);
|
47 |
|
|
di_3_i : in std_logic_vector (007 downto 000);
|
48 |
|
|
do_0_o : out std_logic_vector (007 downto 000);
|
49 |
|
|
do_1_o : out std_logic_vector (007 downto 000);
|
50 |
|
|
do_2_o : out std_logic_vector (007 downto 000);
|
51 |
|
|
do_3_o : out std_logic_vector (007 downto 000)
|
52 |
|
|
);
|
53 |
|
|
end folded_register;
|
54 |
|
|
|
55 |
|
|
architecture data_flow of folded_register is
|
56 |
|
|
|
57 |
|
|
component counter2bit
|
58 |
|
|
port (
|
59 |
|
|
clock : in std_logic;
|
60 |
|
|
clear : in std_logic;
|
61 |
|
|
count : out std_logic_vector (1 downto 0)
|
62 |
|
|
);
|
63 |
|
|
end component;
|
64 |
|
|
|
65 |
|
|
type fifo16x8 is array (00 to 15) of std_logic_vector (7 downto 0);
|
66 |
|
|
type addr4x4 is array (3 downto 0) of std_logic_vector (3 downto 0);
|
67 |
|
|
type addr4x8 is array (3 downto 0) of std_logic_vector (7 downto 0);
|
68 |
|
|
|
69 |
|
|
signal foldreg0 : fifo16x8 :=
|
70 |
|
|
(
|
71 |
|
|
X"00", X"00", X"00", X"00", X"00", X"00", X"00", X"00",
|
72 |
|
|
X"00", X"00", X"00", X"00", X"00", X"00", X"00", X"00"
|
73 |
|
|
);
|
74 |
|
|
--
|
75 |
|
|
signal foldreg1 : fifo16x8 :=
|
76 |
|
|
(
|
77 |
|
|
X"00", X"00", X"00", X"00", X"00", X"00", X"00", X"00",
|
78 |
|
|
X"00", X"00", X"00", X"00", X"00", X"00", X"00", X"00"
|
79 |
|
|
);
|
80 |
|
|
--
|
81 |
|
|
signal foldreg2 : fifo16x8 :=
|
82 |
|
|
(
|
83 |
|
|
X"00", X"00", X"00", X"00", X"00", X"00", X"00", X"00",
|
84 |
|
|
X"00", X"00", X"00", X"00", X"00", X"00", X"00", X"00"
|
85 |
|
|
);
|
86 |
|
|
|
87 |
|
|
signal round0 : std_logic_vector (127 downto 000) := ( X"00000000_00000000_00000000_00000000" );
|
88 |
|
|
--
|
89 |
|
|
signal a_0_i : std_logic_vector (3 downto 0) := ( B"0000" );
|
90 |
|
|
signal a_1_i : std_logic_vector (3 downto 0) := ( B"0000" );
|
91 |
|
|
signal a_2_i : std_logic_vector (3 downto 0) := ( B"0000" );
|
92 |
|
|
signal a_3_i : std_logic_vector (3 downto 0) := ( B"0000" );
|
93 |
|
|
--
|
94 |
|
|
constant fifo_addr_cons : std_logic_vector (63 downto 00) := ( X"05AF_49E3_8D27_C16B" );
|
95 |
|
|
constant fifo_addr_cons_inv : std_logic_vector (63 downto 00) := ( X"0DA7_41EB_852F_C963" );
|
96 |
|
|
signal fifo_addr : std_logic_vector (63 downto 00) := ( X"0000_0000_0000_0000" );
|
97 |
|
|
--
|
98 |
|
|
signal tmp : addr4x8 := ( X"00", X"00", X"00", X"00" );
|
99 |
|
|
signal addr : addr4x4 := ( X"0", X"0", X"0", X"0" );
|
100 |
|
|
--
|
101 |
|
|
signal count : std_logic_vector (1 downto 0);
|
102 |
|
|
signal switch : std_logic := '0';
|
103 |
|
|
signal reg_i : std_logic := '0';
|
104 |
|
|
|
105 |
|
|
begin
|
106 |
|
|
|
107 |
|
|
sect : counter2bit
|
108 |
|
|
port map (
|
109 |
|
|
clock => clk_i,
|
110 |
|
|
clear => load_i,
|
111 |
|
|
count => count
|
112 |
|
|
);
|
113 |
|
|
|
114 |
|
|
round0 <= data_i xor key_i;
|
115 |
|
|
|
116 |
|
|
process(clk_i, load_i)
|
117 |
|
|
begin
|
118 |
|
|
if (load_i = '1') then
|
119 |
|
|
foldreg0 <= (
|
120 |
|
|
round0 (127 downto 120), round0 (119 downto 112), round0 (111 downto 104), round0 (103 downto 096),
|
121 |
|
|
round0 (095 downto 088), round0 (087 downto 080), round0 (079 downto 072), round0 (071 downto 064),
|
122 |
|
|
round0 (063 downto 056), round0 (055 downto 048), round0 (047 downto 040), round0 (039 downto 032),
|
123 |
|
|
round0 (031 downto 024), round0 (023 downto 016), round0 (015 downto 008), round0 (007 downto 000)
|
124 |
|
|
);
|
125 |
|
|
--
|
126 |
|
|
foldreg1 <= (
|
127 |
|
|
round0 (127 downto 120), round0 (119 downto 112), round0 (111 downto 104), round0 (103 downto 096),
|
128 |
|
|
round0 (095 downto 088), round0 (087 downto 080), round0 (079 downto 072), round0 (071 downto 064),
|
129 |
|
|
round0 (063 downto 056), round0 (055 downto 048), round0 (047 downto 040), round0 (039 downto 032),
|
130 |
|
|
round0 (031 downto 024), round0 (023 downto 016), round0 (015 downto 008), round0 (007 downto 000)
|
131 |
|
|
);
|
132 |
|
|
--
|
133 |
|
|
if (enc_i = '0') then
|
134 |
|
|
fifo_addr <= fifo_addr_cons;
|
135 |
|
|
else
|
136 |
|
|
fifo_addr <= fifo_addr_cons_inv;
|
137 |
|
|
end if;
|
138 |
|
|
reg_i <= '0';
|
139 |
|
|
elsif (clk_i = '1' and clk_i'event) then
|
140 |
|
|
if (reg_i = '1') then
|
141 |
|
|
foldreg0 (00 to 11) <= ( foldreg0 (04 to 15) );
|
142 |
|
|
foldreg0 (12 to 15) <= ( di_0_i, di_1_i, di_2_i, di_3_i );
|
143 |
|
|
else
|
144 |
|
|
foldreg1 (00 to 11) <= ( foldreg1 (04 to 15) );
|
145 |
|
|
foldreg1 (12 to 15) <= ( di_0_i, di_1_i, di_2_i, di_3_i );
|
146 |
|
|
end if;
|
147 |
|
|
fifo_addr (63 downto 16) <= fifo_addr (47 downto 00);
|
148 |
|
|
fifo_addr (15 downto 00) <= fifo_addr (63 downto 48);
|
149 |
|
|
if (switch = '1') then
|
150 |
|
|
reg_i <= not(reg_i);
|
151 |
|
|
end if;
|
152 |
|
|
end if;
|
153 |
|
|
end process;
|
154 |
|
|
|
155 |
|
|
a_0_i <= addr(0);
|
156 |
|
|
a_1_i <= addr(1);
|
157 |
|
|
a_2_i <= addr(2);
|
158 |
|
|
a_3_i <= addr(3);
|
159 |
|
|
--
|
160 |
|
|
foldreg2 (00 to 11) <= ( foldreg0 (04 to 15) ) when (reg_i = '1') else ( foldreg1 (04 to 15) );
|
161 |
|
|
foldreg2 (12 to 15) <= ( di_0_i, di_1_i, di_2_i, di_3_i );
|
162 |
|
|
--
|
163 |
|
|
switch <= (count(1)) and (count(0));
|
164 |
|
|
--
|
165 |
|
|
addr(0) <= fifo_addr (51 downto 48) when ( load_i = '1' ) else fifo_addr (35 downto 32);
|
166 |
|
|
addr(1) <= fifo_addr (55 downto 52) when ( load_i = '1' ) else fifo_addr (39 downto 36);
|
167 |
|
|
addr(2) <= fifo_addr (59 downto 56) when ( load_i = '1' ) else fifo_addr (43 downto 40);
|
168 |
|
|
addr(3) <= fifo_addr (63 downto 60) when ( load_i = '1' ) else fifo_addr (47 downto 44);
|
169 |
|
|
--
|
170 |
|
|
tmp(0) <= foldreg1(conv_integer(a_0_i)) when ( reg_i = '1' ) else foldreg0(conv_integer(a_0_i));
|
171 |
|
|
tmp(1) <= foldreg1(conv_integer(a_1_i)) when ( reg_i = '1' ) else foldreg0(conv_integer(a_1_i));
|
172 |
|
|
tmp(2) <= foldreg1(conv_integer(a_2_i)) when ( reg_i = '1' ) else foldreg0(conv_integer(a_2_i));
|
173 |
|
|
tmp(3) <= foldreg1(conv_integer(a_3_i)) when ( reg_i = '1' ) else foldreg0(conv_integer(a_3_i));
|
174 |
|
|
--
|
175 |
|
|
do_0_o <= tmp(3) when ( switch = '0') else foldreg2(conv_integer(a_3_i));
|
176 |
|
|
do_1_o <= tmp(2) when ( switch = '0') else foldreg2(conv_integer(a_2_i));
|
177 |
|
|
do_2_o <= tmp(1) when ( switch = '0') else foldreg2(conv_integer(a_1_i));
|
178 |
|
|
do_3_o <= tmp(0) when ( switch = '0') else foldreg2(conv_integer(a_0_i));
|
179 |
|
|
|
180 |
|
|
end data_flow;
|