| 1 |
13 |
serginhofr |
library ieee;
|
| 2 |
|
|
use ieee.std_logic_1164.all;
|
| 3 |
|
|
use ieee.std_logic_unsigned.all;
|
| 4 |
|
|
|
| 5 |
|
|
entity hellfire_cpu_if is
|
| 6 |
|
|
generic(
|
| 7 |
|
|
address_width: integer := 14;
|
| 8 |
|
|
memory_file : string := "code.txt";
|
| 9 |
|
|
uart_support : string := "yes"
|
| 10 |
|
|
);
|
| 11 |
|
|
port ( clk_in: in std_logic;
|
| 12 |
|
|
reset_in: in std_logic;
|
| 13 |
|
|
int_in: in std_logic;
|
| 14 |
|
|
uart_read: in std_logic;
|
| 15 |
|
|
uart_write: out std_logic
|
| 16 |
|
|
);
|
| 17 |
|
|
end hellfire_cpu_if;
|
| 18 |
|
|
|
| 19 |
|
|
architecture interface of hellfire_cpu_if is
|
| 20 |
|
|
signal clock, boot_enable, ram_enable_n, stall, stall_cpu, busy_cpu, irq_cpu, irq_ack_cpu, data_access_cpu, ram_dly, rff1, reset: std_logic;
|
| 21 |
|
|
signal address, data_read, data_write, data_read_boot, data_read_ram, irq_vector_cpu, inst_addr_cpu, inst_in_cpu, data_addr_cpu, data_in_cpu, data_out_cpu: std_logic_vector(31 downto 0);
|
| 22 |
|
|
signal ext_irq: std_logic_vector(7 downto 0);
|
| 23 |
|
|
signal data_we, data_w_n_ram, data_w_cpu: std_logic_vector(3 downto 0);
|
| 24 |
|
|
|
| 25 |
|
|
signal ext_periph, ext_periph_dly, ready: std_logic;
|
| 26 |
|
|
signal key: std_logic_vector(127 downto 0);
|
| 27 |
|
|
signal input, output: std_logic_vector(63 downto 0);
|
| 28 |
|
|
signal data_read_xtea: std_logic_vector(31 downto 0);
|
| 29 |
|
|
signal control: std_logic_vector(1 downto 0);
|
| 30 |
|
|
begin
|
| 31 |
|
|
-- clock divider (25MHz clock from 50MHz main clock for Spartan3 Starter Kit)
|
| 32 |
|
|
process (reset_in, clk_in, clock)
|
| 33 |
|
|
begin
|
| 34 |
|
|
if reset_in = '1' then
|
| 35 |
|
|
clock <= '0';
|
| 36 |
|
|
else
|
| 37 |
|
|
if clk_in'event and clk_in='1' then
|
| 38 |
|
|
clock <= not clock;
|
| 39 |
|
|
end if;
|
| 40 |
|
|
end if;
|
| 41 |
|
|
end process;
|
| 42 |
|
|
|
| 43 |
|
|
-- reset synchronizer
|
| 44 |
|
|
process (clock, reset_in)
|
| 45 |
|
|
begin
|
| 46 |
|
|
if (reset_in = '1') then
|
| 47 |
|
|
rff1 <= '1';
|
| 48 |
|
|
reset <= '1';
|
| 49 |
|
|
elsif (clock'event and clock = '1') then
|
| 50 |
|
|
rff1 <= '0';
|
| 51 |
|
|
reset <= rff1;
|
| 52 |
|
|
end if;
|
| 53 |
|
|
end process;
|
| 54 |
|
|
|
| 55 |
|
|
|
| 56 |
|
|
process (reset, clock, ext_irq, ram_enable_n)
|
| 57 |
|
|
begin
|
| 58 |
|
|
if reset = '1' then
|
| 59 |
|
|
ram_dly <= '0';
|
| 60 |
|
|
ext_periph_dly <= '0';
|
| 61 |
|
|
ext_irq <= x"00";
|
| 62 |
|
|
elsif clock'event and clock = '1' then
|
| 63 |
|
|
ram_dly <= not ram_enable_n;
|
| 64 |
|
|
ext_periph_dly <= ext_periph;
|
| 65 |
|
|
ext_irq <= "0000000" & int_in;
|
| 66 |
|
|
end if;
|
| 67 |
|
|
end process;
|
| 68 |
|
|
|
| 69 |
|
|
|
| 70 |
|
|
|
| 71 |
|
|
process (data_addr_cpu, key, input, output)
|
| 72 |
|
|
begin
|
| 73 |
|
|
case data_addr_cpu(7 downto 4) is
|
| 74 |
|
|
when "0000" => -- control 0xfa000000 (bit2 - ready (R), bit1 - encrypt (RW), bit0 - start (RW)
|
| 75 |
|
|
data_read_xtea <= x"000000" & "00000" & ready & control;
|
| 76 |
|
|
when "0001" => -- key[0] 0xfa000010
|
| 77 |
|
|
data_read_xtea <= key(127 downto 96);
|
| 78 |
|
|
when "0010" => -- key[1] 0xfa000020
|
| 79 |
|
|
data_read_xtea <= key(95 downto 64);
|
| 80 |
|
|
when "0011" => -- key[2] 0xfa000030
|
| 81 |
|
|
data_read_xtea <= key(63 downto 32);
|
| 82 |
|
|
when "0100" => -- key[3] 0xfa000040
|
| 83 |
|
|
data_read_xtea <= key(31 downto 0);
|
| 84 |
|
|
when "0101" => -- input[0] 0xfa000050
|
| 85 |
|
|
data_read_xtea <= input(63 downto 32);
|
| 86 |
|
|
when "0110" => -- input[1] 0xfa000060
|
| 87 |
|
|
data_read_xtea <= input(31 downto 0);
|
| 88 |
|
|
when "0111" => -- output[0] 0xfa000070
|
| 89 |
|
|
data_read_xtea <= output(63 downto 32);
|
| 90 |
|
|
when "1000" => -- output[1] 0xfa000080
|
| 91 |
|
|
data_read_xtea <= output(31 downto 0);
|
| 92 |
|
|
when others =>
|
| 93 |
|
|
data_read_xtea <= (others => '0');
|
| 94 |
|
|
end case;
|
| 95 |
|
|
end process;
|
| 96 |
|
|
|
| 97 |
|
|
process (clock, reset, data_addr_cpu, control, key, input, output)
|
| 98 |
|
|
begin
|
| 99 |
|
|
if reset = '1' then
|
| 100 |
|
|
key <= (others => '0');
|
| 101 |
|
|
input <= (others => '0');
|
| 102 |
|
|
control <= "00";
|
| 103 |
|
|
elsif clock'event and clock = '1' then
|
| 104 |
|
|
if (ext_periph = '1' and data_we /= "0000") then -- XTEA is at 0xfa000000
|
| 105 |
|
|
case data_addr_cpu(7 downto 4) is
|
| 106 |
|
|
when "0000" => -- control 0xfa000000 (bit2 - ready (R), bit1 - encrypt (RW), bit0 - start (RW)
|
| 107 |
|
|
control <= data_write(1 downto 0);
|
| 108 |
|
|
when "0001" => -- key[0] 0xfa000010
|
| 109 |
|
|
key(127 downto 96) <= data_write;
|
| 110 |
|
|
when "0010" => -- key[1] 0xfa000020
|
| 111 |
|
|
key(95 downto 64) <= data_write;
|
| 112 |
|
|
when "0011" => -- key[2] 0xfa000030
|
| 113 |
|
|
key(63 downto 32) <= data_write;
|
| 114 |
|
|
when "0100" => -- key[3] 0xfa000040
|
| 115 |
|
|
key(31 downto 0) <= data_write;
|
| 116 |
|
|
when "0101" => -- input[0] 0xfa000050
|
| 117 |
|
|
input(63 downto 32) <= data_write;
|
| 118 |
|
|
when "0110" => -- input[1] 0xfa000060
|
| 119 |
|
|
input(31 downto 0) <= data_write;
|
| 120 |
|
|
when others =>
|
| 121 |
|
|
end case;
|
| 122 |
|
|
end if;
|
| 123 |
|
|
end if;
|
| 124 |
|
|
end process;
|
| 125 |
|
|
|
| 126 |
|
|
|
| 127 |
|
|
|
| 128 |
|
|
stall <= '0';
|
| 129 |
|
|
boot_enable <= '1' when address(31 downto 28) = "0000" else '0';
|
| 130 |
|
|
ram_enable_n <= '0' when address(31 downto 28) = "0100" else '1';
|
| 131 |
|
|
ext_periph <= '1' when address(31 downto 24) = x"fa" else '0';
|
| 132 |
|
|
data_read <= data_read_xtea when ext_periph = '1' or ext_periph_dly = '1' else data_read_boot when address(31 downto 28) = "0000" and ram_dly = '0' else data_read_ram;
|
| 133 |
|
|
data_w_n_ram <= not data_we;
|
| 134 |
|
|
|
| 135 |
|
|
-- HF-RISC core
|
| 136 |
|
|
core: entity work.datapath
|
| 137 |
|
|
port map( clock => clock,
|
| 138 |
|
|
reset => reset,
|
| 139 |
|
|
stall => stall_cpu,
|
| 140 |
|
|
busy => busy_cpu,
|
| 141 |
|
|
irq_vector => irq_vector_cpu,
|
| 142 |
|
|
irq => irq_cpu,
|
| 143 |
|
|
irq_ack => irq_ack_cpu,
|
| 144 |
|
|
inst_addr => inst_addr_cpu,
|
| 145 |
|
|
inst_in => inst_in_cpu,
|
| 146 |
|
|
data_addr => data_addr_cpu,
|
| 147 |
|
|
data_in => data_in_cpu,
|
| 148 |
|
|
data_out => data_out_cpu,
|
| 149 |
|
|
data_w => data_w_cpu,
|
| 150 |
|
|
data_access => data_access_cpu
|
| 151 |
|
|
);
|
| 152 |
|
|
|
| 153 |
|
|
-- peripherals / busmux logic
|
| 154 |
|
|
peripherals_busmux: entity work.busmux
|
| 155 |
|
|
generic map(
|
| 156 |
|
|
uart_support => uart_support
|
| 157 |
|
|
)
|
| 158 |
|
|
port map(
|
| 159 |
|
|
clock => clock,
|
| 160 |
|
|
reset => reset,
|
| 161 |
|
|
|
| 162 |
|
|
stall => stall,
|
| 163 |
|
|
|
| 164 |
|
|
stall_cpu => stall_cpu,
|
| 165 |
|
|
busy_cpu => busy_cpu,
|
| 166 |
|
|
irq_vector_cpu => irq_vector_cpu,
|
| 167 |
|
|
irq_cpu => irq_cpu,
|
| 168 |
|
|
irq_ack_cpu => irq_ack_cpu,
|
| 169 |
|
|
inst_addr_cpu => inst_addr_cpu,
|
| 170 |
|
|
inst_in_cpu => inst_in_cpu,
|
| 171 |
|
|
data_addr_cpu => data_addr_cpu,
|
| 172 |
|
|
data_in_cpu => data_in_cpu,
|
| 173 |
|
|
data_out_cpu => data_out_cpu,
|
| 174 |
|
|
data_w_cpu => data_w_cpu,
|
| 175 |
|
|
data_access_cpu => data_access_cpu,
|
| 176 |
|
|
|
| 177 |
|
|
addr_mem => address,
|
| 178 |
|
|
data_read_mem => data_read,
|
| 179 |
|
|
data_write_mem => data_write,
|
| 180 |
|
|
data_we_mem => data_we,
|
| 181 |
|
|
extio_in => ext_irq,
|
| 182 |
|
|
extio_out => open,
|
| 183 |
|
|
uart_read => uart_read,
|
| 184 |
|
|
uart_write => uart_write
|
| 185 |
|
|
);
|
| 186 |
|
|
|
| 187 |
|
|
-- XTEA core
|
| 188 |
|
|
crypto_core: entity work.xtea
|
| 189 |
|
|
port map( clock => clock,
|
| 190 |
|
|
reset => reset,
|
| 191 |
|
|
start => control(0),
|
| 192 |
|
|
encrypt => control(1),
|
| 193 |
|
|
key => key,
|
| 194 |
|
|
input => input,
|
| 195 |
|
|
output => output,
|
| 196 |
|
|
ready => ready
|
| 197 |
|
|
);
|
| 198 |
|
|
|
| 199 |
|
|
-- instruction and data memory (boot RAM)
|
| 200 |
|
|
boot_ram: entity work.ram
|
| 201 |
|
|
generic map (memory_type => "DEFAULT")
|
| 202 |
|
|
port map (
|
| 203 |
|
|
clk => clock,
|
| 204 |
|
|
enable => boot_enable,
|
| 205 |
|
|
write_byte_enable => "0000",
|
| 206 |
|
|
address => address(31 downto 2),
|
| 207 |
|
|
data_write => (others => '0'),
|
| 208 |
|
|
data_read => data_read_boot
|
| 209 |
|
|
);
|
| 210 |
|
|
|
| 211 |
|
|
-- instruction and data memory (external RAM)
|
| 212 |
|
|
memory0lb: entity work.bram
|
| 213 |
|
|
generic map ( memory_file => memory_file,
|
| 214 |
|
|
data_width => 8,
|
| 215 |
|
|
address_width => address_width,
|
| 216 |
|
|
bank => 0)
|
| 217 |
|
|
port map(
|
| 218 |
|
|
clk => clock,
|
| 219 |
|
|
addr => address(address_width -1 downto 2),
|
| 220 |
|
|
cs_n => ram_enable_n,
|
| 221 |
|
|
we_n => data_w_n_ram(0),
|
| 222 |
|
|
data_i => data_write(7 downto 0),
|
| 223 |
|
|
data_o => data_read_ram(7 downto 0)
|
| 224 |
|
|
);
|
| 225 |
|
|
|
| 226 |
|
|
memory0ub: entity work.bram
|
| 227 |
|
|
generic map ( memory_file => memory_file,
|
| 228 |
|
|
data_width => 8,
|
| 229 |
|
|
address_width => address_width,
|
| 230 |
|
|
bank => 1)
|
| 231 |
|
|
port map(
|
| 232 |
|
|
clk => clock,
|
| 233 |
|
|
addr => address(address_width -1 downto 2),
|
| 234 |
|
|
cs_n => ram_enable_n,
|
| 235 |
|
|
we_n => data_w_n_ram(1),
|
| 236 |
|
|
data_i => data_write(15 downto 8),
|
| 237 |
|
|
data_o => data_read_ram(15 downto 8)
|
| 238 |
|
|
);
|
| 239 |
|
|
|
| 240 |
|
|
memory1lb: entity work.bram
|
| 241 |
|
|
generic map ( memory_file => memory_file,
|
| 242 |
|
|
data_width => 8,
|
| 243 |
|
|
address_width => address_width,
|
| 244 |
|
|
bank => 2)
|
| 245 |
|
|
port map(
|
| 246 |
|
|
clk => clock,
|
| 247 |
|
|
addr => address(address_width -1 downto 2),
|
| 248 |
|
|
cs_n => ram_enable_n,
|
| 249 |
|
|
we_n => data_w_n_ram(2),
|
| 250 |
|
|
data_i => data_write(23 downto 16),
|
| 251 |
|
|
data_o => data_read_ram(23 downto 16)
|
| 252 |
|
|
);
|
| 253 |
|
|
|
| 254 |
|
|
memory1ub: entity work.bram
|
| 255 |
|
|
generic map ( memory_file => memory_file,
|
| 256 |
|
|
data_width => 8,
|
| 257 |
|
|
address_width => address_width,
|
| 258 |
|
|
bank => 3)
|
| 259 |
|
|
port map(
|
| 260 |
|
|
clk => clock,
|
| 261 |
|
|
addr => address(address_width -1 downto 2),
|
| 262 |
|
|
cs_n => ram_enable_n,
|
| 263 |
|
|
we_n => data_w_n_ram(3),
|
| 264 |
|
|
data_i => data_write(31 downto 24),
|
| 265 |
|
|
data_o => data_read_ram(31 downto 24)
|
| 266 |
|
|
);
|
| 267 |
|
|
|
| 268 |
|
|
end interface;
|
| 269 |
|
|
|