09e3e4831fa1e7a6915a4bde2ec78882623edbff
[microwatt.git] / core_debug.vhdl
1 library ieee;
2 use ieee.std_logic_1164.all;
3 use ieee.numeric_std.all;
4
5 library work;
6 use work.utils.all;
7 use work.common.all;
8
9 entity core_debug is
10 generic (
11 -- Length of log buffer
12 LOG_LENGTH : natural := 512
13 );
14 port (
15 clk : in std_logic;
16 rst : in std_logic;
17
18 dmi_addr : in std_ulogic_vector(3 downto 0);
19 dmi_din : in std_ulogic_vector(63 downto 0);
20 dmi_dout : out std_ulogic_vector(63 downto 0);
21 dmi_req : in std_ulogic;
22 dmi_wr : in std_ulogic;
23 dmi_ack : out std_ulogic;
24
25 -- Debug actions
26 core_stop : out std_ulogic;
27 core_rst : out std_ulogic;
28 icache_rst : out std_ulogic;
29
30 -- Core status inputs
31 terminate : in std_ulogic;
32 core_stopped : in std_ulogic;
33 nia : in std_ulogic_vector(63 downto 0);
34 msr : in std_ulogic_vector(63 downto 0);
35
36 -- GSPR register read port
37 dbg_gpr_req : out std_ulogic;
38 dbg_gpr_ack : in std_ulogic;
39 dbg_gpr_addr : out gspr_index_t;
40 dbg_gpr_data : in std_ulogic_vector(63 downto 0);
41
42 -- Core logging data
43 log_data : in std_ulogic_vector(255 downto 0);
44 log_read_addr : in std_ulogic_vector(31 downto 0);
45 log_read_data : out std_ulogic_vector(63 downto 0);
46 log_write_addr : out std_ulogic_vector(31 downto 0);
47
48 -- Misc
49 terminated_out : out std_ulogic
50 );
51 end core_debug;
52
53 architecture behave of core_debug is
54 -- DMI needs fixing... make a one clock pulse
55 signal dmi_req_1: std_ulogic;
56
57 -- CTRL register (direct actions, write 1 to act, read back 0)
58 -- bit 0 : Core stop
59 -- bit 1 : Core reset (doesn't clear stop)
60 -- bit 2 : Icache reset
61 -- bit 3 : Single step
62 -- bit 4 : Core start
63 constant DBG_CORE_CTRL : std_ulogic_vector(3 downto 0) := "0000";
64 constant DBG_CORE_CTRL_STOP : integer := 0;
65 constant DBG_CORE_CTRL_RESET : integer := 1;
66 constant DBG_CORE_CTRL_ICRESET : integer := 2;
67 constant DBG_CORE_CTRL_STEP : integer := 3;
68 constant DBG_CORE_CTRL_START : integer := 4;
69
70 -- STAT register (read only)
71 -- bit 0 : Core stopping (wait til bit 1 set)
72 -- bit 1 : Core stopped
73 -- bit 2 : Core terminated (clears with start or reset)
74 constant DBG_CORE_STAT : std_ulogic_vector(3 downto 0) := "0001";
75 constant DBG_CORE_STAT_STOPPING : integer := 0;
76 constant DBG_CORE_STAT_STOPPED : integer := 1;
77 constant DBG_CORE_STAT_TERM : integer := 2;
78
79 -- NIA register (read only for now)
80 constant DBG_CORE_NIA : std_ulogic_vector(3 downto 0) := "0010";
81
82 -- MSR (read only)
83 constant DBG_CORE_MSR : std_ulogic_vector(3 downto 0) := "0011";
84
85 -- GSPR register index
86 constant DBG_CORE_GSPR_INDEX : std_ulogic_vector(3 downto 0) := "0100";
87
88 -- GSPR register data
89 constant DBG_CORE_GSPR_DATA : std_ulogic_vector(3 downto 0) := "0101";
90
91 -- Log buffer address and data registers
92 constant DBG_CORE_LOG_ADDR : std_ulogic_vector(3 downto 0) := "0110";
93 constant DBG_CORE_LOG_DATA : std_ulogic_vector(3 downto 0) := "0111";
94 constant DBG_CORE_LOG_TRIGGER : std_ulogic_vector(3 downto 0) := "1000";
95
96 constant LOG_INDEX_BITS : natural := log2(LOG_LENGTH);
97
98 -- Some internal wires
99 signal stat_reg : std_ulogic_vector(63 downto 0);
100
101 -- Some internal latches
102 signal stopping : std_ulogic;
103 signal do_step : std_ulogic;
104 signal do_reset : std_ulogic;
105 signal do_icreset : std_ulogic;
106 signal terminated : std_ulogic;
107 signal do_gspr_rd : std_ulogic;
108 signal gspr_index : gspr_index_t;
109
110 signal log_dmi_addr : std_ulogic_vector(31 downto 0) := (others => '0');
111 signal log_dmi_data : std_ulogic_vector(63 downto 0) := (others => '0');
112 signal log_dmi_trigger : std_ulogic_vector(63 downto 0) := (others => '0');
113 signal do_log_trigger : std_ulogic := '0';
114 signal do_dmi_log_rd : std_ulogic;
115 signal dmi_read_log_data : std_ulogic;
116 signal dmi_read_log_data_1 : std_ulogic;
117
118 begin
119 -- Single cycle register accesses on DMI except for GSPR data
120 dmi_ack <= dmi_req when dmi_addr /= DBG_CORE_GSPR_DATA
121 else dbg_gpr_ack;
122 dbg_gpr_req <= dmi_req when dmi_addr = DBG_CORE_GSPR_DATA
123 else '0';
124
125 -- Status register read composition
126 stat_reg <= (2 => terminated,
127 1 => core_stopped,
128 0 => stopping,
129 others => '0');
130
131 -- DMI read data mux
132 with dmi_addr select dmi_dout <=
133 stat_reg when DBG_CORE_STAT,
134 nia when DBG_CORE_NIA,
135 msr when DBG_CORE_MSR,
136 dbg_gpr_data when DBG_CORE_GSPR_DATA,
137 log_write_addr & log_dmi_addr when DBG_CORE_LOG_ADDR,
138 log_dmi_data when DBG_CORE_LOG_DATA,
139 log_dmi_trigger when DBG_CORE_LOG_TRIGGER,
140 (others => '0') when others;
141
142 -- DMI writes
143 reg_write: process(clk)
144 begin
145 if rising_edge(clk) then
146 -- Reset the 1-cycle "do" signals
147 do_step <= '0';
148 do_reset <= '0';
149 do_icreset <= '0';
150 do_dmi_log_rd <= '0';
151
152 if (rst) then
153 stopping <= '0';
154 terminated <= '0';
155 else
156 if do_log_trigger = '1' then
157 log_dmi_trigger(1) <= '1';
158 end if;
159 -- Edge detect on dmi_req for 1-shot pulses
160 dmi_req_1 <= dmi_req;
161 if dmi_req = '1' and dmi_req_1 = '0' then
162 if dmi_wr = '1' then
163 report("DMI write to " & to_hstring(dmi_addr));
164
165 -- Control register actions
166 if dmi_addr = DBG_CORE_CTRL then
167 if dmi_din(DBG_CORE_CTRL_RESET) = '1' then
168 do_reset <= '1';
169 terminated <= '0';
170 end if;
171 if dmi_din(DBG_CORE_CTRL_STOP) = '1' then
172 stopping <= '1';
173 end if;
174 if dmi_din(DBG_CORE_CTRL_STEP) = '1' then
175 do_step <= '1';
176 terminated <= '0';
177 end if;
178 if dmi_din(DBG_CORE_CTRL_ICRESET) = '1' then
179 do_icreset <= '1';
180 end if;
181 if dmi_din(DBG_CORE_CTRL_START) = '1' then
182 stopping <= '0';
183 terminated <= '0';
184 end if;
185 elsif dmi_addr = DBG_CORE_GSPR_INDEX then
186 gspr_index <= dmi_din(gspr_index_t'left downto 0);
187 elsif dmi_addr = DBG_CORE_LOG_ADDR then
188 log_dmi_addr <= dmi_din(31 downto 0);
189 do_dmi_log_rd <= '1';
190 elsif dmi_addr = DBG_CORE_LOG_TRIGGER then
191 log_dmi_trigger <= dmi_din;
192 end if;
193 else
194 report("DMI read from " & to_string(dmi_addr));
195 end if;
196
197 elsif dmi_read_log_data = '0' and dmi_read_log_data_1 = '1' then
198 -- Increment log_dmi_addr after the end of a read from DBG_CORE_LOG_DATA
199 log_dmi_addr(LOG_INDEX_BITS + 1 downto 0) <=
200 std_ulogic_vector(unsigned(log_dmi_addr(LOG_INDEX_BITS+1 downto 0)) + 1);
201 do_dmi_log_rd <= '1';
202 end if;
203 dmi_read_log_data_1 <= dmi_read_log_data;
204 if dmi_req = '1' and dmi_addr = DBG_CORE_LOG_DATA then
205 dmi_read_log_data <= '1';
206 else
207 dmi_read_log_data <= '0';
208 end if;
209
210 -- Set core stop on terminate. We'll be stopping some time *after*
211 -- the offending instruction, at least until we can do back flushes
212 -- that preserve NIA which we can't just yet.
213 if terminate = '1' then
214 stopping <= '1';
215 terminated <= '1';
216 end if;
217 end if;
218 end if;
219 end process;
220
221 dbg_gpr_addr <= gspr_index;
222
223 -- Core control signals generated by the debug module
224 core_stop <= stopping and not do_step;
225 core_rst <= do_reset;
226 icache_rst <= do_icreset;
227 terminated_out <= terminated;
228
229 -- Logging RAM
230 maybe_log: if LOG_LENGTH > 0 generate
231 subtype log_ptr_t is unsigned(LOG_INDEX_BITS - 1 downto 0);
232 type log_array_t is array(0 to LOG_LENGTH - 1) of std_ulogic_vector(255 downto 0);
233 signal log_array : log_array_t;
234 signal log_rd_ptr : log_ptr_t;
235 signal log_wr_ptr : log_ptr_t;
236 signal log_toggle : std_ulogic;
237 signal log_wr_enable : std_ulogic;
238 signal log_rd_ptr_latched : log_ptr_t;
239 signal log_rd : std_ulogic_vector(255 downto 0);
240 signal log_dmi_reading : std_ulogic;
241 signal log_dmi_read_done : std_ulogic;
242
243 function select_dword(data : std_ulogic_vector(255 downto 0);
244 addr : std_ulogic_vector(31 downto 0)) return std_ulogic_vector is
245 variable firstbit : integer;
246 begin
247 firstbit := to_integer(unsigned(addr(1 downto 0))) * 64;
248 return data(firstbit + 63 downto firstbit);
249 end;
250
251 attribute ram_style : string;
252 attribute ram_style of log_array : signal is "block";
253 attribute ram_decomp : string;
254 attribute ram_decomp of log_array : signal is "power";
255
256 begin
257 -- Use MSB of read addresses to stop the logging
258 log_wr_enable <= not (log_read_addr(31) or log_dmi_addr(31) or log_dmi_trigger(1));
259
260 log_ram: process(clk)
261 begin
262 if rising_edge(clk) then
263 if log_wr_enable = '1' then
264 log_array(to_integer(log_wr_ptr)) <= log_data;
265 end if;
266 log_rd <= log_array(to_integer(log_rd_ptr_latched));
267 end if;
268 end process;
269
270
271 log_buffer: process(clk)
272 variable b : integer;
273 variable data : std_ulogic_vector(255 downto 0);
274 begin
275 if rising_edge(clk) then
276 if rst = '1' then
277 log_wr_ptr <= (others => '0');
278 log_toggle <= '0';
279 elsif log_wr_enable = '1' then
280 if log_wr_ptr = to_unsigned(LOG_LENGTH - 1, LOG_INDEX_BITS) then
281 log_toggle <= not log_toggle;
282 end if;
283 log_wr_ptr <= log_wr_ptr + 1;
284 end if;
285 if do_dmi_log_rd = '1' then
286 log_rd_ptr_latched <= unsigned(log_dmi_addr(LOG_INDEX_BITS + 1 downto 2));
287 else
288 log_rd_ptr_latched <= unsigned(log_read_addr(LOG_INDEX_BITS + 1 downto 2));
289 end if;
290 if log_dmi_read_done = '1' then
291 log_dmi_data <= select_dword(log_rd, log_dmi_addr);
292 else
293 log_read_data <= select_dword(log_rd, log_read_addr);
294 end if;
295 log_dmi_read_done <= log_dmi_reading;
296 log_dmi_reading <= do_dmi_log_rd;
297 do_log_trigger <= '0';
298 if log_data(42) = log_dmi_trigger(63) and
299 log_data(41 downto 0) = log_dmi_trigger(43 downto 2) and
300 log_dmi_trigger(0) = '1' then
301 do_log_trigger <= '1';
302 end if;
303 end if;
304 end process;
305 log_write_addr(LOG_INDEX_BITS - 1 downto 0) <= std_ulogic_vector(log_wr_ptr);
306 log_write_addr(LOG_INDEX_BITS) <= '1';
307 log_write_addr(31 downto LOG_INDEX_BITS + 1) <= (others => '0');
308 end generate;
309
310 no_log: if LOG_LENGTH = 0 generate
311 begin
312 log_read_data <= (others => '0');
313 log_write_addr <= x"00000001";
314 end generate;
315
316 end behave;
317