Add core logging
[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 : positive := 2048
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
95 -- Some internal wires
96 signal stat_reg : std_ulogic_vector(63 downto 0);
97
98 -- Some internal latches
99 signal stopping : std_ulogic;
100 signal do_step : std_ulogic;
101 signal do_reset : std_ulogic;
102 signal do_icreset : std_ulogic;
103 signal terminated : std_ulogic;
104 signal do_gspr_rd : std_ulogic;
105 signal gspr_index : gspr_index_t;
106
107 -- Logging RAM
108 constant LOG_INDEX_BITS : natural := log2(LOG_LENGTH);
109 subtype log_ptr_t is unsigned(LOG_INDEX_BITS - 1 downto 0);
110 type log_array_t is array(0 to LOG_LENGTH - 1) of std_ulogic_vector(255 downto 0);
111 signal log_array : log_array_t;
112 signal log_rd_ptr : log_ptr_t;
113 signal log_wr_ptr : log_ptr_t;
114 signal log_toggle : std_ulogic;
115 signal log_wr_enable : std_ulogic;
116 signal log_rd_ptr_latched : log_ptr_t;
117 signal log_rd : std_ulogic_vector(255 downto 0);
118 signal log_dmi_addr : std_ulogic_vector(31 downto 0);
119 signal log_dmi_data : std_ulogic_vector(63 downto 0);
120 signal do_dmi_log_rd : std_ulogic;
121 signal log_dmi_reading : std_ulogic;
122 signal log_dmi_read_done : std_ulogic;
123 signal dmi_read_log_data : std_ulogic;
124 signal dmi_read_log_data_1 : std_ulogic;
125
126 function select_dword(data : std_ulogic_vector(255 downto 0);
127 addr : std_ulogic_vector(31 downto 0)) return std_ulogic_vector is
128 variable firstbit : integer;
129 begin
130 firstbit := to_integer(unsigned(addr(1 downto 0))) * 64;
131 return data(firstbit + 63 downto firstbit);
132 end;
133
134 attribute ram_style : string;
135 attribute ram_style of log_array : signal is "block";
136 attribute ram_decomp : string;
137 attribute ram_decomp of log_array : signal is "power";
138
139 begin
140 -- Single cycle register accesses on DMI except for GSPR data
141 dmi_ack <= dmi_req when dmi_addr /= DBG_CORE_GSPR_DATA
142 else dbg_gpr_ack;
143 dbg_gpr_req <= dmi_req when dmi_addr = DBG_CORE_GSPR_DATA
144 else '0';
145
146 -- Status register read composition
147 stat_reg <= (2 => terminated,
148 1 => core_stopped,
149 0 => stopping,
150 others => '0');
151
152 -- DMI read data mux
153 with dmi_addr select dmi_dout <=
154 stat_reg when DBG_CORE_STAT,
155 nia when DBG_CORE_NIA,
156 msr when DBG_CORE_MSR,
157 dbg_gpr_data when DBG_CORE_GSPR_DATA,
158 log_write_addr & log_dmi_addr when DBG_CORE_LOG_ADDR,
159 log_dmi_data when DBG_CORE_LOG_DATA,
160 (others => '0') when others;
161
162 -- DMI writes
163 reg_write: process(clk)
164 begin
165 if rising_edge(clk) then
166 -- Reset the 1-cycle "do" signals
167 do_step <= '0';
168 do_reset <= '0';
169 do_icreset <= '0';
170 do_dmi_log_rd <= '0';
171
172 if (rst) then
173 stopping <= '0';
174 terminated <= '0';
175 else
176 -- Edge detect on dmi_req for 1-shot pulses
177 dmi_req_1 <= dmi_req;
178 if dmi_req = '1' and dmi_req_1 = '0' then
179 if dmi_wr = '1' then
180 report("DMI write to " & to_hstring(dmi_addr));
181
182 -- Control register actions
183 if dmi_addr = DBG_CORE_CTRL then
184 if dmi_din(DBG_CORE_CTRL_RESET) = '1' then
185 do_reset <= '1';
186 terminated <= '0';
187 end if;
188 if dmi_din(DBG_CORE_CTRL_STOP) = '1' then
189 stopping <= '1';
190 end if;
191 if dmi_din(DBG_CORE_CTRL_STEP) = '1' then
192 do_step <= '1';
193 terminated <= '0';
194 end if;
195 if dmi_din(DBG_CORE_CTRL_ICRESET) = '1' then
196 do_icreset <= '1';
197 end if;
198 if dmi_din(DBG_CORE_CTRL_START) = '1' then
199 stopping <= '0';
200 terminated <= '0';
201 end if;
202 elsif dmi_addr = DBG_CORE_GSPR_INDEX then
203 gspr_index <= dmi_din(gspr_index_t'left downto 0);
204 elsif dmi_addr = DBG_CORE_LOG_ADDR then
205 log_dmi_addr <= dmi_din(31 downto 0);
206 do_dmi_log_rd <= '1';
207 end if;
208 else
209 report("DMI read from " & to_string(dmi_addr));
210 end if;
211
212 elsif dmi_read_log_data = '0' and dmi_read_log_data_1 = '1' then
213 -- Increment log_dmi_addr after the end of a read from DBG_CORE_LOG_DATA
214 log_dmi_addr(LOG_INDEX_BITS + 1 downto 0) <=
215 std_ulogic_vector(unsigned(log_dmi_addr(LOG_INDEX_BITS+1 downto 0)) + 1);
216 do_dmi_log_rd <= '1';
217 end if;
218 dmi_read_log_data_1 <= dmi_read_log_data;
219 if dmi_req = '1' and dmi_addr = DBG_CORE_LOG_DATA then
220 dmi_read_log_data <= '1';
221 else
222 dmi_read_log_data <= '0';
223 end if;
224
225 -- Set core stop on terminate. We'll be stopping some time *after*
226 -- the offending instruction, at least until we can do back flushes
227 -- that preserve NIA which we can't just yet.
228 if terminate = '1' then
229 stopping <= '1';
230 terminated <= '1';
231 end if;
232 end if;
233 end if;
234 end process;
235
236 dbg_gpr_addr <= gspr_index;
237
238 -- Core control signals generated by the debug module
239 core_stop <= stopping and not do_step;
240 core_rst <= do_reset;
241 icache_rst <= do_icreset;
242 terminated_out <= terminated;
243
244 -- Use MSB of read addresses to stop the logging
245 log_wr_enable <= not (log_read_addr(31) or log_dmi_addr(31));
246
247 log_ram: process(clk)
248 begin
249 if rising_edge(clk) then
250 if log_wr_enable = '1' then
251 log_array(to_integer(log_wr_ptr)) <= log_data;
252 end if;
253 log_rd <= log_array(to_integer(log_rd_ptr_latched));
254 end if;
255 end process;
256
257
258 log_buffer: process(clk)
259 variable b : integer;
260 variable data : std_ulogic_vector(255 downto 0);
261 begin
262 if rising_edge(clk) then
263 if rst = '1' then
264 log_wr_ptr <= (others => '0');
265 log_toggle <= '0';
266 elsif log_wr_enable = '1' then
267 if log_wr_ptr = to_unsigned(LOG_LENGTH - 1, LOG_INDEX_BITS) then
268 log_toggle <= not log_toggle;
269 end if;
270 log_wr_ptr <= log_wr_ptr + 1;
271 end if;
272 if do_dmi_log_rd = '1' then
273 log_rd_ptr_latched <= unsigned(log_dmi_addr(LOG_INDEX_BITS + 1 downto 2));
274 else
275 log_rd_ptr_latched <= unsigned(log_read_addr(LOG_INDEX_BITS + 1 downto 2));
276 end if;
277 if log_dmi_read_done = '1' then
278 log_dmi_data <= select_dword(log_rd, log_dmi_addr);
279 else
280 log_read_data <= select_dword(log_rd, log_read_addr);
281 end if;
282 log_dmi_read_done <= log_dmi_reading;
283 log_dmi_reading <= do_dmi_log_rd;
284 end if;
285 end process;
286 log_write_addr(LOG_INDEX_BITS - 1 downto 0) <= std_ulogic_vector(log_wr_ptr);
287 log_write_addr(LOG_INDEX_BITS) <= '1';
288 log_write_addr(31 downto LOG_INDEX_BITS + 1) <= (others => '0');
289 end behave;
290