2 use ieee.std_logic_1164.all;
3 use ieee.numeric_std.all;
11 -- Length of log buffer
12 LOG_LENGTH : natural := 512
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;
26 core_stop : out std_ulogic;
27 core_rst : out std_ulogic;
28 icache_rst : out std_ulogic;
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);
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);
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);
49 terminated_out : out std_ulogic
53 architecture behave of core_debug is
54 -- DMI needs fixing... make a one clock pulse
55 signal dmi_req_1: std_ulogic;
57 -- CTRL register (direct actions, write 1 to act, read back 0)
59 -- bit 1 : Core reset (doesn't clear stop)
60 -- bit 2 : Icache reset
61 -- bit 3 : Single step
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;
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;
79 -- NIA register (read only for now)
80 constant DBG_CORE_NIA : std_ulogic_vector(3 downto 0) := "0010";
83 constant DBG_CORE_MSR : std_ulogic_vector(3 downto 0) := "0011";
85 -- GSPR register index
86 constant DBG_CORE_GSPR_INDEX : std_ulogic_vector(3 downto 0) := "0100";
89 constant DBG_CORE_GSPR_DATA : std_ulogic_vector(3 downto 0) := "0101";
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";
96 constant LOG_INDEX_BITS : natural := log2(LOG_LENGTH);
98 -- Some internal wires
99 signal stat_reg : std_ulogic_vector(63 downto 0);
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;
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;
119 -- Single cycle register accesses on DMI except for GSPR data
120 dmi_ack <= dmi_req when dmi_addr /= DBG_CORE_GSPR_DATA
122 dbg_gpr_req <= dmi_req when dmi_addr = DBG_CORE_GSPR_DATA
125 -- Status register read composition
126 stat_reg <= (2 => terminated,
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;
143 reg_write: process(clk)
145 if rising_edge(clk) then
146 -- Reset the 1-cycle "do" signals
150 do_dmi_log_rd <= '0';
156 if do_log_trigger = '1' then
157 log_dmi_trigger(1) <= '1';
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
163 report("DMI write to " & to_hstring(dmi_addr));
165 -- Control register actions
166 if dmi_addr = DBG_CORE_CTRL then
167 if dmi_din(DBG_CORE_CTRL_RESET) = '1' then
171 if dmi_din(DBG_CORE_CTRL_STOP) = '1' then
174 if dmi_din(DBG_CORE_CTRL_STEP) = '1' then
178 if dmi_din(DBG_CORE_CTRL_ICRESET) = '1' then
181 if dmi_din(DBG_CORE_CTRL_START) = '1' then
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;
194 report("DMI read from " & to_string(dmi_addr));
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';
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';
207 dmi_read_log_data <= '0';
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
221 dbg_gpr_addr <= gspr_index;
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;
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;
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;
247 firstbit := to_integer(unsigned(addr(1 downto 0))) * 64;
248 return data(firstbit + 63 downto firstbit);
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";
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));
260 log_ram: process(clk)
262 if rising_edge(clk) then
263 if log_wr_enable = '1' then
264 log_array(to_integer(log_wr_ptr)) <= log_data;
266 log_rd <= log_array(to_integer(log_rd_ptr_latched));
271 log_buffer: process(clk)
272 variable b : integer;
273 variable data : std_ulogic_vector(255 downto 0);
275 if rising_edge(clk) then
277 log_wr_ptr <= (others => '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;
283 log_wr_ptr <= log_wr_ptr + 1;
285 if do_dmi_log_rd = '1' then
286 log_rd_ptr_latched <= unsigned(log_dmi_addr(LOG_INDEX_BITS + 1 downto 2));
288 log_rd_ptr_latched <= unsigned(log_read_addr(LOG_INDEX_BITS + 1 downto 2));
290 if log_dmi_read_done = '1' then
291 log_dmi_data <= select_dword(log_rd, log_dmi_addr);
293 log_read_data <= select_dword(log_rd, log_read_addr);
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';
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');
310 no_log: if LOG_LENGTH = 0 generate
312 log_read_data <= (others => '0');
313 log_write_addr <= x"00000001";