begin working on linux verilator simulation
[microwatt.git] / fetch1.vhdl
1 library ieee;
2 use ieee.std_logic_1164.all;
3 use ieee.numeric_std.all;
4
5 library work;
6 use work.common.all;
7
8 entity fetch1 is
9 generic(
10 RESET_ADDRESS : std_logic_vector(63 downto 0) := (others => '0');
11 ALT_RESET_ADDRESS : std_logic_vector(63 downto 0) := (others => '0');
12 HAS_BTC : boolean := true
13 );
14 port(
15 clk : in std_ulogic;
16 rst : in std_ulogic;
17
18 -- Control inputs:
19 stall_in : in std_ulogic;
20 flush_in : in std_ulogic;
21 inval_btc : in std_ulogic;
22 stop_in : in std_ulogic;
23 alt_reset_in : in std_ulogic;
24
25 -- redirect from writeback unit
26 w_in : in WritebackToFetch1Type;
27
28 -- redirect from decode1
29 d_in : in Decode1ToFetch1Type;
30
31 -- Request to icache
32 i_out : out Fetch1ToIcacheType;
33
34 -- outputs to logger
35 log_out : out std_ulogic_vector(42 downto 0)
36 );
37 end entity fetch1;
38
39 architecture behaviour of fetch1 is
40 type reg_internal_t is record
41 mode_32bit: std_ulogic;
42 rd_is_niap4: std_ulogic;
43 predicted_taken: std_ulogic;
44 pred_not_taken: std_ulogic;
45 predicted_nia: std_ulogic_vector(63 downto 0);
46 end record;
47 signal r, r_next : Fetch1ToIcacheType;
48 signal r_int, r_next_int : reg_internal_t;
49 signal advance_nia : std_ulogic;
50 signal log_nia : std_ulogic_vector(42 downto 0);
51
52 constant BTC_ADDR_BITS : integer := 10;
53 constant BTC_TAG_BITS : integer := 62 - BTC_ADDR_BITS;
54 constant BTC_TARGET_BITS : integer := 62;
55 constant BTC_SIZE : integer := 2 ** BTC_ADDR_BITS;
56 constant BTC_WIDTH : integer := BTC_TAG_BITS + BTC_TARGET_BITS + 1;
57 type btc_mem_type is array (0 to BTC_SIZE - 1) of std_ulogic_vector(BTC_WIDTH - 1 downto 0);
58
59 signal btc_rd_data : std_ulogic_vector(BTC_WIDTH - 1 downto 0) := (others => '0');
60 signal btc_rd_valid : std_ulogic := '0';
61
62 begin
63
64 regs : process(clk)
65 begin
66 if rising_edge(clk) then
67 log_nia <= r.nia(63) & r.nia(43 downto 2);
68 if r /= r_next then
69 report "fetch1 rst:" & std_ulogic'image(rst) &
70 " IR:" & std_ulogic'image(r_next.virt_mode) &
71 " P:" & std_ulogic'image(r_next.priv_mode) &
72 " E:" & std_ulogic'image(r_next.big_endian) &
73 " 32:" & std_ulogic'image(r_next_int.mode_32bit) &
74 " R:" & std_ulogic'image(w_in.redirect) & std_ulogic'image(d_in.redirect) &
75 " S:" & std_ulogic'image(stall_in) &
76 " T:" & std_ulogic'image(stop_in) &
77 " nia:" & to_hstring(r_next.nia);
78 end if;
79 if rst = '1' or w_in.redirect = '1' or d_in.redirect = '1' or stall_in = '0' then
80 r.virt_mode <= r_next.virt_mode;
81 r.priv_mode <= r_next.priv_mode;
82 r.big_endian <= r_next.big_endian;
83 r_int.mode_32bit <= r_next_int.mode_32bit;
84 end if;
85 if advance_nia = '1' then
86 r.predicted <= r_next.predicted;
87 r.pred_ntaken <= r_next.pred_ntaken;
88 r.nia <= r_next.nia;
89 r_int.predicted_taken <= r_next_int.predicted_taken;
90 r_int.pred_not_taken <= r_next_int.pred_not_taken;
91 r_int.predicted_nia <= r_next_int.predicted_nia;
92 r_int.rd_is_niap4 <= r_next_int.rd_is_niap4;
93 end if;
94 -- always send the up-to-date stop mark and req
95 r.stop_mark <= stop_in;
96 r.req <= not rst;
97 end if;
98 end process;
99 log_out <= log_nia;
100
101 btc : if HAS_BTC generate
102 signal btc_memory : btc_mem_type;
103 attribute ram_style : string;
104 attribute ram_style of btc_memory : signal is "block";
105
106 signal btc_valids : std_ulogic_vector(BTC_SIZE - 1 downto 0);
107 attribute ram_style of btc_valids : signal is "distributed";
108
109 signal btc_wr : std_ulogic;
110 signal btc_wr_data : std_ulogic_vector(BTC_WIDTH - 1 downto 0);
111 signal btc_wr_addr : std_ulogic_vector(BTC_ADDR_BITS - 1 downto 0);
112 begin
113 btc_wr_data <= w_in.br_taken &
114 w_in.br_nia(63 downto BTC_ADDR_BITS + 2) &
115 w_in.redirect_nia(63 downto 2);
116 btc_wr_addr <= w_in.br_nia(BTC_ADDR_BITS + 1 downto 2);
117 btc_wr <= w_in.br_last;
118
119 btc_ram : process(clk)
120 variable raddr : unsigned(BTC_ADDR_BITS - 1 downto 0);
121 begin
122 if rising_edge(clk) then
123 raddr := unsigned(r.nia(BTC_ADDR_BITS + 1 downto 2)) +
124 to_unsigned(2, BTC_ADDR_BITS);
125 if advance_nia = '1' then
126 btc_rd_data <= btc_memory(to_integer(raddr));
127 btc_rd_valid <= btc_valids(to_integer(raddr));
128 end if;
129 if btc_wr = '1' then
130 btc_memory(to_integer(unsigned(btc_wr_addr))) <= btc_wr_data;
131 end if;
132 if inval_btc = '1' or rst = '1' then
133 btc_valids <= (others => '0');
134 elsif btc_wr = '1' then
135 btc_valids(to_integer(unsigned(btc_wr_addr))) <= '1';
136 end if;
137 end if;
138 end process;
139 end generate;
140
141 comb : process(all)
142 variable v : Fetch1ToIcacheType;
143 variable v_int : reg_internal_t;
144 begin
145 v := r;
146 v_int := r_int;
147 v.predicted := '0';
148 v.pred_ntaken := '0';
149 v_int.predicted_taken := '0';
150 v_int.pred_not_taken := '0';
151 v_int.rd_is_niap4 := '0';
152
153 if rst = '1' then
154 if alt_reset_in = '1' then
155 v.nia := ALT_RESET_ADDRESS;
156 else
157 v.nia := RESET_ADDRESS;
158 end if;
159 v.virt_mode := '0';
160 v.priv_mode := '1';
161 v.big_endian := '0';
162 v_int.mode_32bit := '0';
163 v_int.predicted_nia := (others => '0');
164 elsif w_in.redirect = '1' then
165 v.nia := w_in.redirect_nia(63 downto 2) & "00";
166 if w_in.mode_32bit = '1' then
167 v.nia(63 downto 32) := (others => '0');
168 end if;
169 v.virt_mode := w_in.virt_mode;
170 v.priv_mode := w_in.priv_mode;
171 v.big_endian := w_in.big_endian;
172 v_int.mode_32bit := w_in.mode_32bit;
173 elsif d_in.redirect = '1' then
174 v.nia := d_in.redirect_nia(63 downto 2) & "00";
175 if r_int.mode_32bit = '1' then
176 v.nia(63 downto 32) := (others => '0');
177 end if;
178 elsif r_int.predicted_taken = '1' then
179 v.nia := r_int.predicted_nia;
180 v.predicted := '1';
181 else
182 v_int.rd_is_niap4 := '1';
183 v.pred_ntaken := r_int.pred_not_taken;
184 v.nia := std_ulogic_vector(unsigned(r.nia) + 4);
185 if r_int.mode_32bit = '1' then
186 v.nia(63 downto 32) := x"00000000";
187 end if;
188 if btc_rd_valid = '1' and r_int.rd_is_niap4 = '1' and
189 btc_rd_data(BTC_WIDTH - 2 downto BTC_TARGET_BITS)
190 = v.nia(BTC_TAG_BITS + BTC_ADDR_BITS + 1 downto BTC_ADDR_BITS + 2) then
191 v_int.predicted_taken := btc_rd_data(BTC_WIDTH - 1);
192 v_int.pred_not_taken := not btc_rd_data(BTC_WIDTH - 1);
193 end if;
194 end if;
195 v_int.predicted_nia := btc_rd_data(BTC_TARGET_BITS - 1 downto 0) & "00";
196
197 -- If the last NIA value went down with a stop mark, it didn't get
198 -- executed, and hence we shouldn't increment NIA.
199 advance_nia <= rst or w_in.redirect or d_in.redirect or (not r.stop_mark and not stall_in);
200
201 r_next <= v;
202 r_next_int <= v_int;
203
204 -- Update outputs to the icache
205 i_out <= r;
206
207 end process;
208
209 end architecture behaviour;