Merge pull request #169 from paulusmack/mmu
[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 );
13 port(
14 clk : in std_ulogic;
15 rst : in std_ulogic;
16
17 -- Control inputs:
18 stall_in : in std_ulogic;
19 flush_in : in std_ulogic;
20 stop_in : in std_ulogic;
21 alt_reset_in : in std_ulogic;
22
23 -- redirect from execution unit
24 e_in : in Execute1ToFetch1Type;
25
26 -- Request to icache
27 i_out : out Fetch1ToIcacheType
28 );
29 end entity fetch1;
30
31 architecture behaviour of fetch1 is
32 type stop_state_t is (RUNNING, STOPPED, RESTARTING);
33 type reg_internal_t is record
34 stop_state: stop_state_t;
35 end record;
36 signal r, r_next : Fetch1ToIcacheType;
37 signal r_int, r_next_int : reg_internal_t;
38 begin
39
40 regs : process(clk)
41 begin
42 if rising_edge(clk) then
43 if r /= r_next then
44 report "fetch1 rst:" & std_ulogic'image(rst) &
45 " IR:" & std_ulogic'image(e_in.virt_mode) &
46 " P:" & std_ulogic'image(e_in.priv_mode) &
47 " R:" & std_ulogic'image(e_in.redirect) &
48 " S:" & std_ulogic'image(stall_in) &
49 " T:" & std_ulogic'image(stop_in) &
50 " nia:" & to_hstring(r_next.nia) &
51 " SM:" & std_ulogic'image(r_next.stop_mark);
52 end if;
53 r <= r_next;
54 r_int <= r_next_int;
55 end if;
56 end process;
57
58 comb : process(all)
59 variable v : Fetch1ToIcacheType;
60 variable v_int : reg_internal_t;
61 variable increment : boolean;
62 begin
63 v := r;
64 v_int := r_int;
65
66 if rst = '1' then
67 if alt_reset_in = '1' then
68 v.nia := ALT_RESET_ADDRESS;
69 else
70 v.nia := RESET_ADDRESS;
71 end if;
72 v.virt_mode := '0';
73 v.priv_mode := '1';
74 v_int.stop_state := RUNNING;
75 elsif e_in.redirect = '1' then
76 v.nia := e_in.redirect_nia;
77 v.virt_mode := e_in.virt_mode;
78 v.priv_mode := e_in.priv_mode;
79 elsif stall_in = '0' then
80
81 -- For debug stop/step to work properly we need a little bit of
82 -- trickery here. If we just stop incrementing and send stop marks
83 -- when stop_in is set, then we'll increment on the cycle it clears
84 -- and end up never executing the instruction we were stopped on.
85 --
86 -- Avoid this along with the opposite issue when stepping (stop is
87 -- cleared for only one cycle) is handled by the state machine below
88 --
89 -- By default, increment addresses
90 increment := true;
91 case v_int.stop_state is
92 when RUNNING =>
93 -- If we are running and stop_in is set, then stop incrementing,
94 -- we are now stopped.
95 if stop_in = '1' then
96 increment := false;
97 v_int.stop_state := STOPPED;
98 end if;
99 when STOPPED =>
100 -- When stopped, never increment. If stop is cleared, go to state
101 -- "restarting" but still don't increment that cycle. stop_in is
102 -- now 0 so we'll send the NIA down without a stop mark.
103 increment := false;
104 if stop_in = '0' then
105 v_int.stop_state := RESTARTING;
106 end if;
107 when RESTARTING =>
108 -- We have just sent the NIA down, we can start incrementing again.
109 -- If stop_in is still not set, go back to running normally.
110 -- If stop_in is set again (that was a one-cycle "step"), go
111 -- back to "stopped" state which means we'll stop incrementing
112 -- on the next cycle. This ensures we increment the PC once after
113 -- sending one instruction without a stop mark. Since stop_in is
114 -- now set, the new PC will be sent with a stop mark and thus not
115 -- executed.
116 if stop_in = '0' then
117 v_int.stop_state := RUNNING;
118 else
119 v_int.stop_state := STOPPED;
120 end if;
121 end case;
122
123 if increment then
124 v.nia := std_logic_vector(unsigned(v.nia) + 4);
125 end if;
126 end if;
127
128 v.req := not rst;
129 v.stop_mark := stop_in;
130
131 r_next <= v;
132 r_next_int <= v_int;
133
134 -- Update outputs to the icache
135 i_out <= r;
136
137 end process;
138
139 end architecture behaviour;