axi_present.vhd 5.81 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
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;
tebina nasreddine's avatar
Cleanup    
tebina nasreddine committed
39
		S_AXI_BREADY : in std_logic
tebina nasreddine's avatar
tebina nasreddine committed
40
		
tebina nasreddine's avatar
cleanup    
tebina nasreddine committed
41
	);
tebina nasreddine's avatar
tebina nasreddine committed
42
end entity;
tebina nasreddine's avatar
cleanup    
tebina nasreddine committed
43
44
45
46
47
48
49
50
51
52
53
54
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
55
	type text_buffer is array (0 to 5) of std_logic_vector(DATAWIDTH - 1 downto 0) ;
tebina nasreddine's avatar
cleanup    
tebina nasreddine committed
56
57
	type key_buffer is array (0 to 3) of std_logic_vector(DATAWIDTH - 1 downto 0);

tebina nasreddine's avatar
tebina nasreddine committed
58
59
60
	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
61
62
63
64
65
66
67
68
69
70

	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
71
	type state_type is (idle, read_plaintext, read_key, stabilize, stabilize_read,active, write_ciphertext);
tebina nasreddine's avatar
cleanup    
tebina nasreddine committed
72
	signal state : state_type;
tebina nasreddine's avatar
Cleanup    
tebina nasreddine committed
73
    signal ciphertext_temp_buf : std_logic_vector(DATAWIDTH - 1 downto 0) ;
tebina nasreddine's avatar
tebina nasreddine committed
74
begin
tebina nasreddine's avatar
cleanup    
tebina nasreddine committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
	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
90
			ip_reset <= '0' when state = active else '1';
tebina nasreddine's avatar
tebina nasreddine committed
91
92
			S_AXI_BRESP <= "00";
            S_AXI_RRESP <= "00";
tebina nasreddine's avatar
Cleanup    
tebina nasreddine committed
93

tebina nasreddine's avatar
tebina nasreddine committed
94
95
			
			state_machine : process (S_AXI_ACLK)
tebina nasreddine's avatar
cleanup    
tebina nasreddine committed
96
97
98
			begin
				if rising_edge(S_AXI_ACLK) then
					if S_AXI_ARESETN = '0' then
tebina nasreddine's avatar
tebina nasreddine committed
99
100
101
						state <= idle;
						counter <= 0;
					else
tebina nasreddine's avatar
cleanup    
tebina nasreddine committed
102
103
104
						case state is
							when idle => 
								S_AXI_RDATA <= (others => '0');
tebina nasreddine's avatar
tebina nasreddine committed
105
106
								S_AXI_RVALID <= '0';
                                S_AXI_ARREADY <= '0';
tebina nasreddine's avatar
tebina nasreddine committed
107
108
109
                                S_AXI_BVALID <= '0';
								S_AXI_WREADY <= '0';
								S_AXI_AWREADY <= '0' ;
tebina nasreddine's avatar
cleanup    
tebina nasreddine committed
110
111
112
113
114
115
116
								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
117
118
119
									S_AXI_BVALID <= '1';
									S_AXI_WREADY <= '1';
									S_AXI_AWREADY <= '1' ;
tebina nasreddine's avatar
cleanup    
tebina nasreddine committed
120
									plaintext_buf(counter) <= S_AXI_WDATA;
tebina nasreddine's avatar
tebina nasreddine committed
121
									state <= stabilize;
tebina nasreddine's avatar
cleanup    
tebina nasreddine committed
122
123
124
125
								end if;
 
							when read_key => 
								if S_AXI_AWVALID = '1' and S_AXI_WVALID = '1' then
tebina nasreddine's avatar
tebina nasreddine committed
126
									key_buf(counter-plaintext_reads-1) <= S_AXI_WDATA;
tebina nasreddine's avatar
tebina nasreddine committed
127
128
129
									S_AXI_BVALID <= '1';
									S_AXI_WREADY <= '1';
									S_AXI_AWREADY <= '1' ;
tebina nasreddine's avatar
tebina nasreddine committed
130
									state <= stabilize;
tebina nasreddine's avatar
cleanup    
tebina nasreddine committed
131
132
								end if;
 
tebina nasreddine's avatar
tebina nasreddine committed
133
134
135
136
137
138
139
140
141
142
143
144
145
							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 ;
tebina nasreddine's avatar
tebina nasreddine committed
146
147
                                S_AXI_RVALID <= '0';
                                S_AXI_ARREADY <= '0';
tebina nasreddine's avatar
tebina nasreddine committed
148
                                state <= write_ciphertext;
tebina nasreddine's avatar
tebina nasreddine committed
149
150
151
                                if counter = ciphertext_writes-1  then 
                                state <= idle ;
                                end if;
tebina nasreddine's avatar
cleanup    
tebina nasreddine committed
152
153
							when active => 
								if counter = active_cycles - 1 then
tebina nasreddine's avatar
tebina nasreddine committed
154
155
156
157
158
								S_AXI_RDATA <= ciphertext_buf(0);
								ciphertext_temp_buf <= ciphertext_buf(1);
						        S_AXI_RVALID <= '1';									
                                S_AXI_ARREADY <= '1';
									state <= stabilize_read;
tebina nasreddine's avatar
tebina nasreddine committed
159
									counter <= 0;									
tebina nasreddine's avatar
cleanup    
tebina nasreddine committed
160
161
162
163
164
								else
									counter <= counter + 1;
								end if;
 
							when write_ciphertext => 
tebina nasreddine's avatar
tebina nasreddine committed
165
								--if S_AXI_ARVALID = '1' and S_AXI_RREADY = '1' then 
tebina nasreddine's avatar
tebina nasreddine committed
166
								S_AXI_RDATA <= ciphertext_temp_buf;
tebina nasreddine's avatar
tebina nasreddine committed
167
168
169
						        S_AXI_RVALID <= '1';									
                                S_AXI_ARREADY <= '1';
                                state <= stabilize_read;
tebina nasreddine's avatar
tebina nasreddine committed
170
                                --end if ;
tebina nasreddine's avatar
cleanup    
tebina nasreddine committed
171
						end case;
tebina nasreddine's avatar
tebina nasreddine committed
172
173
					end if;
				end if;
tebina nasreddine's avatar
cleanup    
tebina nasreddine committed
174
175
176
177
			end process;
 


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