axi_present.vhd 6.69 KB
Newer Older
tebina nasreddine's avatar
cleanup    
tebina nasreddine committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
library ieee;
use ieee.std_logic_1164.all;
use work.util.all;

entity axi_present is

	generic (
		ADRWIDTH : integer := 8;
		DATAWIDTH : integer := 32
	);
	port (
		-- AXI SLAVE INTERFACE ----------------------------
		-- Clock and Reset
		S_AXI_ACLK : in std_logic;
		S_AXI_ARESETN : in std_logic;
		-- Write Address Channel
		S_AXI_AWADDR : in std_logic_vector(ADRWIDTH - 1 downto 0);
		S_AXI_AWVALID : in std_logic;
		S_AXI_AWREADY : out std_logic;
		S_AXI_AWPROT : in std_logic_vector(2 downto 0); --addr write protection
		-- Write Data Channel
		S_AXI_WDATA : in std_logic_vector(DATAWIDTH - 1 downto 0);
		S_AXI_WSTRB : in std_logic_vector(3 downto 0);
		S_AXI_WVALID : in std_logic;
		S_AXI_WREADY : out std_logic;
		-- Read Address Channel
		S_AXI_ARADDR : in std_logic_vector(ADRWIDTH - 1 downto 0);
		S_AXI_ARVALID : in std_logic;
		S_AXI_ARREADY : out std_logic;
		S_AXI_ARPROT : in std_logic_vector(2 downto 0); --addr read protection
		-- Read Data Channel
		S_AXI_RDATA : out std_logic_vector(DATAWIDTH - 1 downto 0);
		S_AXI_RRESP : out std_logic_vector(1 downto 0);
		S_AXI_RVALID : out std_logic;
		S_AXI_RREADY : in std_logic;
		-- Write Response Channel
		S_AXI_BRESP : out std_logic_vector(1 downto 0);
		S_AXI_BVALID : out std_logic;
		S_AXI_BREADY : in std_logic;
tebina nasreddine's avatar
tebina nasreddine committed
40
41
		state_signal : out std_logic_vector (2 downto 0);
		ip_plaintext_signal : out std_logic_vector (63 downto 0);
tebina nasreddine's avatar
tebina nasreddine committed
42
		ip_ciphertext_buffer : out std_logic_vector (31 downto 0);
tebina nasreddine's avatar
tebina nasreddine committed
43
44
45
46
47
		counter_signal : out natural;
		ip_key_signal :  out std_logic_vector (127 downto 0);
		ip_key_buffer : out std_logic_vector (31 downto 0);
		ip_ciphertext_signal : out std_logic_vector (63 downto 0)
		
tebina nasreddine's avatar
cleanup    
tebina nasreddine committed
48
	);
tebina nasreddine's avatar
tebina nasreddine committed
49
end entity;
tebina nasreddine's avatar
cleanup    
tebina nasreddine committed
50
51
52
53
54
55
56
57
58
59
60
61
architecture behavioral of axi_present is

	component present_top is
		generic (k : key_enum);
		port (
			plaintext : in std_logic_vector(63 downto 0);
			key : in std_logic_vector(key_bits(k) - 1 downto 0);
			clk : in std_logic;
			reset : in std_logic;
			ciphertext : out std_logic_vector(63 downto 0)
		);
	end component;
tebina nasreddine's avatar
tebina nasreddine committed
62
	type text_buffer is array (0 to 5) of std_logic_vector(DATAWIDTH - 1 downto 0) ;
tebina nasreddine's avatar
cleanup    
tebina nasreddine committed
63
64
	type key_buffer is array (0 to 3) of std_logic_vector(DATAWIDTH - 1 downto 0);

tebina nasreddine's avatar
tebina nasreddine committed
65
66
67
	signal plaintext_buf : text_buffer ;--:= (others => ( others => '0')) ;
	signal ciphertext_buf : text_buffer;--:= (others => ( others => '0'));
	signal key_buf : key_buffer ;--:= (others => ( others => '0'));
tebina nasreddine's avatar
cleanup    
tebina nasreddine committed
68
69
70
71
72
73
74
75
76
77

	constant plaintext_reads : natural := 2;
	constant key_reads : natural := 4;
	constant active_cycles : natural := 33;
	constant ciphertext_writes : natural := 2;
	signal counter : natural range 0 to 32;
	signal ip_plaintext : std_logic_vector(63 downto 0);
	signal ip_key : std_logic_vector(127 downto 0);
	signal ip_reset : std_logic;
	signal ip_ciphertext : std_logic_vector(63 downto 0);
tebina nasreddine's avatar
tebina nasreddine committed
78
	type state_type is (idle, read_plaintext, read_key, stabilize, stabilize_read,active, write_ciphertext);
tebina nasreddine's avatar
cleanup    
tebina nasreddine committed
79
	signal state : state_type;
tebina nasreddine's avatar
tebina nasreddine committed
80
    signal done : boolean := false ;
tebina nasreddine's avatar
tebina nasreddine committed
81
begin
tebina nasreddine's avatar
cleanup    
tebina nasreddine committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
	BLOCK_CIPHER : present_top
		generic map(
		k => K_128
		)
		port map(
			plaintext => ip_plaintext, 
			key => ip_key, 
			clk => S_AXI_ACLK, 
			reset => ip_reset, 
			ciphertext => ip_ciphertext
		);

			ip_plaintext <= plaintext_buf(0) & plaintext_buf(1);
			ciphertext_buf (0) <= ip_ciphertext (31 downto 0);
			ciphertext_buf (1) <= ip_ciphertext (63 downto 32);
			ip_key <= key_buf(0) & key_buf(1) & key_buf(2) & key_buf(3);
tebina nasreddine's avatar
tebina nasreddine committed
98
99
100
			ip_reset <= '0' when state = active else '1';
			S_AXI_BRESP <= "00";
            S_AXI_RRESP <= "00";
tebina nasreddine's avatar
tebina nasreddine committed
101
			ip_plaintext_signal <= ip_plaintext;
tebina nasreddine's avatar
tebina nasreddine committed
102
			ip_ciphertext_buffer <= ciphertext_buf(0);
tebina nasreddine's avatar
tebina nasreddine committed
103
104
105
106
			ip_key_signal <= ip_key;
			ip_key_buffer <= key_buf(1);
			counter_signal <= counter;
			ip_ciphertext_signal <= ip_ciphertext;
tebina nasreddine's avatar
tebina nasreddine committed
107
108
			
			state_machine : process (S_AXI_ACLK)
tebina nasreddine's avatar
cleanup    
tebina nasreddine committed
109
110
111
			begin
				if rising_edge(S_AXI_ACLK) then
					if S_AXI_ARESETN = '0' then
tebina nasreddine's avatar
tebina nasreddine committed
112
113
114
						state <= idle;
						counter <= 0;
					else
tebina nasreddine's avatar
cleanup    
tebina nasreddine committed
115
116
117
						case state is
							when idle => 
								S_AXI_RDATA <= (others => '0');
tebina nasreddine's avatar
tebina nasreddine committed
118
119
120
121
122
								S_AXI_RVALID <= '0';
                                S_AXI_ARREADY <= '0';
                                S_AXI_BVALID <= '0';
								S_AXI_WREADY <= '0';
								S_AXI_AWREADY <= '0' ;
tebina nasreddine's avatar
cleanup    
tebina nasreddine committed
123
124
125
126
127
128
129
								if S_AXI_WVALID = '1' then
									state <= read_plaintext;
									counter <= 0;
								end if;
 
							when read_plaintext => 
								if S_AXI_AWVALID = '1' and S_AXI_WVALID = '1' then
tebina nasreddine's avatar
tebina nasreddine committed
130
131
132
									S_AXI_BVALID <= '1';
									S_AXI_WREADY <= '1';
									S_AXI_AWREADY <= '1' ;
tebina nasreddine's avatar
cleanup    
tebina nasreddine committed
133
									plaintext_buf(counter) <= S_AXI_WDATA;
tebina nasreddine's avatar
tebina nasreddine committed
134
									state <= stabilize;
tebina nasreddine's avatar
cleanup    
tebina nasreddine committed
135
136
137
138
								end if;
 
							when read_key => 
								if S_AXI_AWVALID = '1' and S_AXI_WVALID = '1' then
tebina nasreddine's avatar
tebina nasreddine committed
139
									key_buf(counter-plaintext_reads-1) <= S_AXI_WDATA;
tebina nasreddine's avatar
tebina nasreddine committed
140
141
142
									S_AXI_BVALID <= '1';
									S_AXI_WREADY <= '1';
									S_AXI_AWREADY <= '1' ;
tebina nasreddine's avatar
tebina nasreddine committed
143
									state <= stabilize;
tebina nasreddine's avatar
cleanup    
tebina nasreddine committed
144
145
								end if;
 
tebina nasreddine's avatar
tebina nasreddine committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
							when stabilize =>
							     counter <= counter +1;
							     S_AXI_BVALID <= '0';
								 S_AXI_WREADY <= '0';
							     S_AXI_AWREADY <= '0';
							     case counter is  
							         when 0 to 1 => state <= read_plaintext ;
							         when 2 to 5 => state <= read_key ;							                     
							         when others => state <= active; 
							                        counter <= 0 ;
							     end case;
                            when stabilize_read => 
                                counter <= counter +1 ;
                                S_AXI_RVALID <= '0';
                                S_AXI_ARREADY <= '0';
                                state <= write_ciphertext;
tebina nasreddine's avatar
tebina nasreddine committed
162
163
164
                                if counter = ciphertext_writes-1  then 
                                state <= idle ;
                                end if;
tebina nasreddine's avatar
cleanup    
tebina nasreddine committed
165
166
167
							when active => 
								if counter = active_cycles - 1 then
									state <= write_ciphertext;
tebina nasreddine's avatar
tebina nasreddine committed
168
									counter <= 0;									
tebina nasreddine's avatar
cleanup    
tebina nasreddine committed
169
170
171
172
173
								else
									counter <= counter + 1;
								end if;
 
							when write_ciphertext => 
tebina nasreddine's avatar
tebina nasreddine committed
174
175
								--if S_AXI_ARVALID = '1' and S_AXI_RREADY = '1' then 
								S_AXI_RDATA <= ciphertext_buf(counter);
tebina nasreddine's avatar
tebina nasreddine committed
176
177
178
						        S_AXI_RVALID <= '1';									
                                S_AXI_ARREADY <= '1';
                                state <= stabilize_read;
tebina nasreddine's avatar
tebina nasreddine committed
179
                                --end if ;
tebina nasreddine's avatar
cleanup    
tebina nasreddine committed
180
						end case;
tebina nasreddine's avatar
tebina nasreddine committed
181
182
					end if;
				end if;
tebina nasreddine's avatar
cleanup    
tebina nasreddine committed
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
			end process;
 

			process (state)
				begin
					case state is
						when idle => 
							state_signal <= "000";
						when read_plaintext => 
							state_signal <= "001";
						when read_key => 
							state_signal <= "010";
						when stabilize => 
							state_signal <= "011";
						when active => 
							state_signal <= "100";
						when write_ciphertext => 
							state_signal <= "101";
tebina nasreddine's avatar
tebina nasreddine committed
201
202
						when stabilize_read =>
						    state_signal <= "110";
tebina nasreddine's avatar
cleanup    
tebina nasreddine committed
203
204
205
206
207
208
						when others => 
							state_signal <= "XXX";

					end case;
				end process;

tebina nasreddine's avatar
tebina nasreddine committed
209
end architecture;