OpenCores
URL https://opencores.org/ocsvn/neorv32/neorv32/trunk

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [rtl/] [core/] [neorv32_debug_dm.vhd] - Blame information for rev 65

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 59 zero_gravi
-- #################################################################################################
2
-- # << NEORV32 - RISC-V-Compatible Debug Module (DM) >>                                           #
3
-- # ********************************************************************************************* #
4
-- # Compatible to the "Minimal RISC-V External Debug Spec. Version 0.13.2"                        #
5
-- # -> "Execution-based" debugging scheme                                                         # 
6
-- # ********************************************************************************************* #
7
-- # Key features:                                                                                 #
8
-- # * register access commands only                                                               #
9
-- # * auto-execution commands                                                                     #
10
-- # * for a single hart only                                                                      #
11
-- # * 2 general purpose program buffer entries                                                    #
12
-- # * 1 general purpose data buffer entry                                                         #
13
-- #                                                                                               #
14
-- # CPU access:                                                                                   #
15
-- # * ROM for "park loop" code                                                                    #
16
-- # * program buffer                                                                              #
17
-- # * data buffer                                                                                 #
18
-- # * control and status register                                                                 #
19
-- # ********************************************************************************************* #
20
-- # BSD 3-Clause License                                                                          #
21
-- #                                                                                               #
22
-- # Copyright (c) 2021, Stephan Nolting. All rights reserved.                                     #
23
-- #                                                                                               #
24
-- # Redistribution and use in source and binary forms, with or without modification, are          #
25
-- # permitted provided that the following conditions are met:                                     #
26
-- #                                                                                               #
27
-- # 1. Redistributions of source code must retain the above copyright notice, this list of        #
28
-- #    conditions and the following disclaimer.                                                   #
29
-- #                                                                                               #
30
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
31
-- #    conditions and the following disclaimer in the documentation and/or other materials        #
32
-- #    provided with the distribution.                                                            #
33
-- #                                                                                               #
34
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
35
-- #    endorse or promote products derived from this software without specific prior written      #
36
-- #    permission.                                                                                #
37
-- #                                                                                               #
38
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
39
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
40
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
41
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
42
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
43
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
44
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
45
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
46
-- # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
47
-- # ********************************************************************************************* #
48
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
49
-- #################################################################################################
50
 
51
library ieee;
52
use ieee.std_logic_1164.all;
53
use ieee.numeric_std.all;
54
 
55
library neorv32;
56
use neorv32.neorv32_package.all;
57
 
58
entity neorv32_debug_dm is
59
  port (
60
    -- global control --
61
    clk_i            : in  std_ulogic; -- global clock line
62
    rstn_i           : in  std_ulogic; -- global reset line, low-active
63
    -- debug module interface (DMI) --
64
    dmi_rstn_i       : in  std_ulogic;
65
    dmi_req_valid_i  : in  std_ulogic;
66
    dmi_req_ready_o  : out std_ulogic; -- DMI is allowed to make new requests when set
67
    dmi_req_addr_i   : in  std_ulogic_vector(06 downto 0);
68
    dmi_req_op_i     : in  std_ulogic; -- 0=read, 1=write
69
    dmi_req_data_i   : in  std_ulogic_vector(31 downto 0);
70
    dmi_resp_valid_o : out std_ulogic; -- response valid when set
71
    dmi_resp_ready_i : in  std_ulogic; -- ready to receive respond
72
    dmi_resp_data_o  : out std_ulogic_vector(31 downto 0);
73
    dmi_resp_err_o   : out std_ulogic; -- 0=ok, 1=error
74
    -- CPU bus access --
75
    cpu_addr_i       : in  std_ulogic_vector(31 downto 0); -- address
76
    cpu_rden_i       : in  std_ulogic; -- read enable
77
    cpu_wren_i       : in  std_ulogic; -- write enable
78
    cpu_data_i       : in  std_ulogic_vector(31 downto 0); -- data in
79
    cpu_data_o       : out std_ulogic_vector(31 downto 0); -- data out
80
    cpu_ack_o        : out std_ulogic; -- transfer acknowledge
81
    -- CPU control --
82
    cpu_ndmrstn_o    : out std_ulogic; -- soc reset
83
    cpu_halt_req_o   : out std_ulogic  -- request hart to halt (enter debug mode)
84
  );
85
end neorv32_debug_dm;
86
 
87
architecture neorv32_debug_dm_rtl of neorv32_debug_dm is
88
 
89
  -- DM configuration --
90
  constant nscratch_c   : std_ulogic_vector(03 downto 0) := "0001"; -- number of dscratch* registers in CPU = 1
91
  constant dataaccess_c : std_ulogic                     := '1';    -- 1: abstract data is memory-mapped, 0: abstract data is CSR-mapped
92
  constant datasize_c   : std_ulogic_vector(03 downto 0) := "0001"; -- number of data registers in memory/CSR space = 1
93
  constant dataaddr_c   : std_ulogic_vector(11 downto 0) := dm_data_base_c(11 downto 0); -- signed base address of data registers in memory/CSR space
94
 
95
  -- available DMI registers --
96
  constant addr_data0_c        : std_ulogic_vector(6 downto 0) := "000" & x"4";
97
  constant addr_dmcontrol_c    : std_ulogic_vector(6 downto 0) := "001" & x"0";
98
  constant addr_dmstatus_c     : std_ulogic_vector(6 downto 0) := "001" & x"1";
99
  constant addr_hartinfo_c     : std_ulogic_vector(6 downto 0) := "001" & x"2";
100
  constant addr_abstractcs_c   : std_ulogic_vector(6 downto 0) := "001" & x"6";
101
  constant addr_command_c      : std_ulogic_vector(6 downto 0) := "001" & x"7";
102
  constant addr_abstractauto_c : std_ulogic_vector(6 downto 0) := "001" & x"8";
103
  constant addr_nextdm_c       : std_ulogic_vector(6 downto 0) := "001" & x"d";
104
  constant addr_progbuf0_c     : std_ulogic_vector(6 downto 0) := "010" & x"0";
105
  constant addr_progbuf1_c     : std_ulogic_vector(6 downto 0) := "010" & x"1";
106
  constant addr_sbcs_c         : std_ulogic_vector(6 downto 0) := "011" & x"8";
107
  constant addr_haltsum0_c     : std_ulogic_vector(6 downto 0) := "100" & x"0";
108
 
109
  -- RISC-V 32-bit instruction prototypes --
110
  constant instr_nop_c    : std_ulogic_vector(31 downto 0) := x"00000013"; -- nop
111
  constant instr_lw_c     : std_ulogic_vector(31 downto 0) := x"00002003"; -- lw zero, 0(zero)
112
  constant instr_sw_c     : std_ulogic_vector(31 downto 0) := x"00002023"; -- sw zero, 0(zero)
113
  constant instr_ebreak_c : std_ulogic_vector(31 downto 0) := x"00100073"; -- ebreak
114
 
115
  -- debug module controller --
116
  type dm_ctrl_state_t is (CMD_IDLE, CMD_EXE_CHECK, CMD_EXE_PREPARE, CMD_EXE_TRIGGER, CMD_EXE_BUSY, CMD_EXE_ERROR);
117
  type dm_ctrl_t is record
118
    -- fsm --
119
    state           : dm_ctrl_state_t;
120
    busy            : std_ulogic;
121
    ldsw_progbuf    : std_ulogic_vector(31 downto 0);
122
    pbuf_en         : std_ulogic;
123
    -- error flags --
124
    illegal_state   : std_ulogic;
125
    illegal_cmd     : std_ulogic;
126
    cmderr          : std_ulogic_vector(02 downto 0);
127
    -- hart status --
128
    hart_halted     : std_ulogic;
129
    hart_resume_req : std_ulogic;
130
    hart_resume_ack : std_ulogic;
131
    hart_reset      : std_ulogic;
132
  end record;
133
  signal dm_ctrl : dm_ctrl_t;
134
 
135
  -- debug module DMI registers / access --
136
  type progbuf_t is array (0 to 1) of std_ulogic_vector(31 downto 0);
137
  type dm_reg_t is record
138
    dmcontrol_ndmreset : std_ulogic;
139
    dmcontrol_dmactive : std_ulogic;
140
    abstractauto_autoexecdata    : std_ulogic;
141
    abstractauto_autoexecprogbuf : std_ulogic_vector(01 downto 0);
142
    progbuf     : progbuf_t;
143
    command     : std_ulogic_vector(31 downto 0);
144
    --
145
    halt_req    : std_ulogic;
146
    resume_req  : std_ulogic;
147
    reset_ack   : std_ulogic;
148
    wr_acc_err  : std_ulogic;
149
    rd_acc_err  : std_ulogic;
150
    clr_acc_err : std_ulogic;
151
    autoexec_wr : std_ulogic;
152
    autoexec_rd : std_ulogic;
153
  end record;
154
  signal dm_reg : dm_reg_t;
155
 
156
  -- cpu program buffer --
157
  type cpu_progbuf_t is array (0 to 4) of std_ulogic_vector(31 downto 0);
158
  signal cpu_progbuf : cpu_progbuf_t;
159
 
160
  -- **********************************************************
161
  -- CPU Bus Interface
162
  -- **********************************************************
163
 
164
  -- Debug Core Interface
165
  type dci_t is record
166
    halt_ack      : std_ulogic; -- CPU (re-)entered HALT state (single-shot)
167
    resume_req    : std_ulogic; -- DM wants the CPU to resume when set
168
    resume_ack    : std_ulogic; -- CPU starts resuming when set (single-shot)
169
    execute_req   : std_ulogic; -- DM wants CPU to execute program buffer when set
170
    execute_ack   : std_ulogic; -- CPU starts executing program buffer when set (single-shot)
171
    exception_ack : std_ulogic; -- CPU has detected an exception (single-shot)
172
    progbuf       : std_ulogic_vector(255 downto 0); -- program buffer, 4 32-bit entries
173
    data_we       : std_ulogic; -- write abstract data
174
    wdata         : std_ulogic_vector(31 downto 0); -- abstract write data
175
    rdata         : std_ulogic_vector(31 downto 0); -- abstract read data
176
  end record;
177
  signal dci : dci_t;
178
 
179
  -- IO space: module base address --
180
  constant hi_abb_c : natural := 31; -- high address boundary bit
181
  constant lo_abb_c : natural := index_size_f(dm_size_c); -- low address boundary bit
182
 
183
  -- status and control register - bits --
184
  constant sreg_halt_ack_c      : natural := 0; -- -/w: CPU is halted in debug mode and waits in park loop
185
  constant sreg_resume_req_c    : natural := 1; -- r/-: DM requests CPU to resume
186
  constant sreg_resume_ack_c    : natural := 2; -- -/w: CPU starts resuming
187
  constant sreg_execute_req_c   : natural := 3; -- r/-: DM requests to execute program buffer
188
  constant sreg_execute_ack_c   : natural := 4; -- -/w: CPU starts to execute program buffer
189
  constant sreg_exception_ack_c : natural := 5; -- -/w: CPU has detected an exception
190
 
191 60 zero_gravi
  -- code ROM containing "park loop" --
192 59 zero_gravi
  type code_rom_file_t is array (0 to 31) of std_ulogic_vector(31 downto 0);
193
  constant code_rom_file : code_rom_file_t := (
194
    00000000 => x"0180006f",
195
    00000001 => x"7b241073",
196
    00000002 => x"02000413",
197
    00000003 => x"98802023",
198
    00000004 => x"7b202473",
199
    00000005 => x"00100073",
200
    00000006 => x"7b241073",
201
    00000007 => x"00100413",
202
    00000008 => x"98802023",
203
    00000009 => x"98002403",
204
    00000010 => x"00847413",
205
    00000011 => x"02041263",
206
    00000012 => x"98002403",
207
    00000013 => x"00247413",
208
    00000014 => x"00041463",
209
    00000015 => x"fe9ff06f",
210
    00000016 => x"00400413",
211
    00000017 => x"98802023",
212
    00000018 => x"7b202473",
213
    00000019 => x"7b200073",
214
    00000020 => x"01000413",
215
    00000021 => x"98802023",
216
    00000022 => x"7b202473",
217 61 zero_gravi
    00000023 => x"0000100f",
218
    00000024 => x"88000067",
219 59 zero_gravi
    others   => x"00100073"  -- ebreak
220
  );
221
 
222
  -- global access control --
223
  signal acc_en : std_ulogic;
224
  signal rden   : std_ulogic;
225
  signal wren   : std_ulogic;
226
  signal maddr  : std_ulogic_vector(01 downto 0);
227
 
228
  -- data buffer --
229
  signal data_buf : std_ulogic_vector(31 downto 0);
230
 
231
  -- program buffer access --
232
  type prog_buf_t is array (0 to 3) of std_ulogic_vector(31 downto 0);
233
  signal prog_buf : prog_buf_t;
234
 
235
begin
236
 
237
  -- Debug Module Command Controller --------------------------------------------------------
238
  -- -------------------------------------------------------------------------------------------
239
  dm_controller: process(clk_i)
240
  begin
241
    if rising_edge(clk_i) then
242
      if (dm_reg.dmcontrol_dmactive = '0') or (dmi_rstn_i = '0') then -- DM reset / DM disabled
243
        dm_ctrl.state        <= CMD_IDLE;
244 60 zero_gravi
        dm_ctrl.ldsw_progbuf <= (others => '-');
245 59 zero_gravi
        dci.execute_req      <= '0';
246 60 zero_gravi
        dm_ctrl.pbuf_en      <= '-';
247 59 zero_gravi
        --
248
        dm_ctrl.illegal_cmd   <= '-';
249
        dm_ctrl.illegal_state <= '-';
250
        dm_ctrl.cmderr        <= "000";
251
        --
252
        dm_ctrl.hart_reset      <= '0';
253
        dm_ctrl.hart_halted     <= '0';
254
        dm_ctrl.hart_resume_req <= '0';
255
        dm_ctrl.hart_resume_ack <= '0';
256
      else -- DM active
257
 
258
        -- defaults --
259
        dci.execute_req       <= '0';
260
        dm_ctrl.illegal_cmd   <= '0';
261
        dm_ctrl.illegal_state <= '0';
262
 
263
        -- command execution fsm --
264
        case dm_ctrl.state is
265
 
266
          when CMD_IDLE => -- wait for new abstract command
267
          -- ------------------------------------------------------------
268
            if (dmi_req_valid_i = '1') and (dmi_req_op_i = '1') then -- valid DM write access
269
              if (dmi_req_addr_i = addr_command_c) then
270
                if (dm_ctrl.cmderr = "000") then -- only execute if no error
271
                  dm_ctrl.state <= CMD_EXE_CHECK;
272
                end if;
273
              end if;
274
            elsif (dm_reg.autoexec_rd = '1') or (dm_reg.autoexec_wr = '1') then -- auto execution trigger
275
              dm_ctrl.state <= CMD_EXE_CHECK;
276
            end if;
277
 
278
          when CMD_EXE_CHECK => -- check if command is valid / supported
279
          -- ------------------------------------------------------------
280
            if (dm_reg.command(31 downto 24) = x"00") and -- cmdtype: register access
281
               (dm_reg.command(23) = '0') and -- reserved
282
               (dm_reg.command(22 downto 20) = "010") and -- aarsize: has to be 32-bit
283
               (dm_reg.command(19) = '0') and -- aarpostincrement: not supported
284
               ((dm_reg.command(17) = '0') or (dm_reg.command(15 downto 05) = "00010000000")) then -- regno: only GPRs are supported: 0x1000..0x101f if transfer is set
285
              if (dm_ctrl.hart_halted = '1') then -- CPU is halted
286
                dm_ctrl.state <= CMD_EXE_PREPARE;
287
              else -- error! CPU is still running
288
                dm_ctrl.illegal_state <= '1';
289
                dm_ctrl.state         <= CMD_EXE_ERROR;
290
              end if;
291
            else -- invalid command
292
              dm_ctrl.illegal_cmd <= '1';
293
              dm_ctrl.state       <= CMD_EXE_ERROR;
294
            end if;
295
 
296
          when CMD_EXE_PREPARE => -- setup program buffer
297
          -- ------------------------------------------------------------
298
            if (dm_reg.command(17) = '1') then -- "transfer"
299
              if (dm_reg.command(16) = '0') then -- "write" = 0 -> read from GPR
300
                dm_ctrl.ldsw_progbuf <= instr_sw_c;
301
                dm_ctrl.ldsw_progbuf(31 downto 25) <= dataaddr_c(11 downto 05); -- destination address
302
                dm_ctrl.ldsw_progbuf(24 downto 20) <= dm_reg.command(4 downto 0); -- "regno" = source register
303
                dm_ctrl.ldsw_progbuf(11 downto 07) <= dataaddr_c(04 downto 00); -- destination address
304
              else -- "write" = 0 -> write to GPR
305
                dm_ctrl.ldsw_progbuf <= instr_lw_c;
306
                dm_ctrl.ldsw_progbuf(31 downto 20) <= dataaddr_c; -- source address
307
                dm_ctrl.ldsw_progbuf(11 downto 07) <= dm_reg.command(4 downto 0); -- "regno" = destination register
308
              end if;
309
            else
310 60 zero_gravi
              dm_ctrl.ldsw_progbuf <= instr_nop_c; -- NOP - do nothing
311 59 zero_gravi
            end if;
312
            --
313 60 zero_gravi
            if (dm_reg.command(18) = '1') then -- "postexec" - execute program buffer
314 59 zero_gravi
              dm_ctrl.pbuf_en <= '1';
315 60 zero_gravi
            else -- execute all program buffer entries as NOPs
316 59 zero_gravi
              dm_ctrl.pbuf_en <= '0';
317
            end if;
318
            --
319
            dm_ctrl.state <= CMD_EXE_TRIGGER;
320
 
321
          when CMD_EXE_TRIGGER => -- request CPU to execute command
322
          -- ------------------------------------------------------------
323
            dci.execute_req <= '1'; -- request execution
324
            if (dci.execute_ack = '1') then -- CPU starts execution
325
              dm_ctrl.state <= CMD_EXE_BUSY;
326
            end if;
327
 
328
          when CMD_EXE_BUSY => -- wait for CPU to finish
329
          -- ------------------------------------------------------------
330 60 zero_gravi
            if (dci.halt_ack = '1') then -- CPU is parked (halted) again -> execution done
331 59 zero_gravi
              dm_ctrl.state <= CMD_IDLE;
332
            end if;
333
 
334
          when CMD_EXE_ERROR => -- delay cycle for error to arrive abstracts.cmderr
335
          -- ------------------------------------------------------------
336
            dm_ctrl.state <= CMD_IDLE;
337
 
338
          when others => -- undefined
339
          -- ------------------------------------------------------------
340
            dm_ctrl.state <= CMD_IDLE;
341
 
342
        end case;
343
 
344
 
345 60 zero_gravi
        -- error flags --
346 59 zero_gravi
        -- ------------------------------------------------------------
347 60 zero_gravi
        if (dm_ctrl.cmderr = "000") then -- set new error
348 59 zero_gravi
          if (dm_ctrl.illegal_state = '1') then -- cannot execute since hart is not in expected state
349
            dm_ctrl.cmderr <= "100";
350
          elsif (dci.exception_ack = '1') then -- exception during execution
351
            dm_ctrl.cmderr <= "011";
352
          elsif (dm_ctrl.illegal_cmd = '1') then -- unsupported command
353
            dm_ctrl.cmderr <= "010";
354
          elsif (dm_reg.rd_acc_err = '1') or (dm_reg.wr_acc_err = '1') then -- invalid read/write while command is executing
355
            dm_ctrl.cmderr <= "001";
356
          end if;
357
        elsif (dm_reg.clr_acc_err = '1') then -- acknowledge/clear error flags
358
          dm_ctrl.cmderr <= "000";
359
        end if;
360
 
361
 
362
        -- hart status --
363
        -- ------------------------------------------------------------
364
 
365
        -- HALTED --
366
        if (dm_reg.dmcontrol_ndmreset = '1') then
367
          dm_ctrl.hart_halted <= '0';
368
        elsif (dci.halt_ack = '1') then
369
          dm_ctrl.hart_halted <= '1';
370
        elsif (dci.resume_ack = '1') then
371
          dm_ctrl.hart_halted <= '0';
372
        end if;
373
 
374
        -- RESUME REQ --
375
        if (dm_reg.dmcontrol_ndmreset = '1') then
376
          dm_ctrl.hart_resume_req <= '0';
377
        elsif (dm_reg.resume_req = '1') then
378
          dm_ctrl.hart_resume_req <= '1';
379
        elsif (dci.resume_ack = '1') then
380
          dm_ctrl.hart_resume_req <= '0';
381
        end if;
382
 
383
        -- RESUME ACK --
384
        if (dm_reg.dmcontrol_ndmreset = '1') then
385
          dm_ctrl.hart_resume_ack <= '0';
386
        elsif (dci.resume_ack = '1') then
387
          dm_ctrl.hart_resume_ack <= '1';
388
        elsif (dm_reg.resume_req = '1') then
389
          dm_ctrl.hart_resume_ack <= '0';
390
        end if;
391
 
392
        -- hart has been RESET --
393
        if (dm_reg.dmcontrol_ndmreset = '1') then
394
          dm_ctrl.hart_reset <= '1';
395
        elsif (dm_reg.reset_ack = '1') then
396
          dm_ctrl.hart_reset <= '0';
397
        end if;
398
 
399
      end if;
400
    end if;
401
  end process dm_controller;
402
 
403
  -- controller busy flag --
404
  dm_ctrl.busy <= '0' when (dm_ctrl.state = CMD_IDLE) else '1';
405
 
406
 
407
  -- Debug Module Interface - Write Access --------------------------------------------------
408
  -- -------------------------------------------------------------------------------------------
409
  dmi_write_access: process(rstn_i, clk_i)
410
  begin
411
    if (rstn_i = '0') then
412
      dm_reg.dmcontrol_ndmreset <= '0';
413
      dm_reg.dmcontrol_dmactive <= '0'; -- DM is in reset state after hardware reset
414
      --
415
      dm_reg.abstractauto_autoexecdata    <= '0';
416
      dm_reg.abstractauto_autoexecprogbuf <= "00";
417
      --
418
      dm_reg.command <= (others => '0');
419
      dm_reg.progbuf <= (others => instr_nop_c);
420
      --
421
      dm_reg.halt_req    <= '0';
422
      dm_reg.resume_req  <= '0';
423
      dm_reg.reset_ack   <= '0';
424
      dm_reg.wr_acc_err  <= '0';
425
      dm_reg.clr_acc_err <= '0';
426
      dm_reg.autoexec_wr <= '0';
427
    elsif rising_edge(clk_i) then
428
 
429
      -- default --
430
      dm_reg.resume_req  <= '0';
431
      dm_reg.reset_ack   <= '0';
432
      dm_reg.wr_acc_err  <= '0';
433
      dm_reg.clr_acc_err <= '0';
434
      dm_reg.autoexec_wr <= '0';
435
 
436
      -- DMI access --
437
      if (dmi_req_valid_i = '1') and (dmi_req_op_i = '1') then -- valid DMI write request
438
 
439
        -- debug module control --
440
        if (dmi_req_addr_i = addr_dmcontrol_c) then
441 64 zero_gravi
          dm_reg.halt_req           <= dmi_req_data_i(31); -- haltreq (-/w): write 1 to request halt; has to be cleared again by debugger
442 59 zero_gravi
          dm_reg.resume_req         <= dmi_req_data_i(30); -- resumereq (-/w1): write 1 to request resume
443
          dm_reg.reset_ack          <= dmi_req_data_i(28); -- ackhavereset (-/w1)
444
          dm_reg.dmcontrol_ndmreset <= dmi_req_data_i(01); -- ndmreset (r/w): soc reset
445
          dm_reg.dmcontrol_dmactive <= dmi_req_data_i(00); -- dmactive (r/w): DM reset
446
        end if;
447
 
448
        -- write abstract command --
449
        if (dmi_req_addr_i = addr_command_c) then
450
          if (dm_ctrl.busy = '0') and (dm_ctrl.cmderr = "000") then -- idle and no errors yet
451
            dm_reg.command <= dmi_req_data_i;
452
          end if;
453
        end if;
454
 
455
        -- write abstract command autoexec --
456
        if (dmi_req_addr_i = addr_abstractauto_c) then
457
          if (dm_ctrl.busy = '0') then -- idle and no errors yet
458
            dm_reg.abstractauto_autoexecdata       <= dmi_req_data_i(00);
459
            dm_reg.abstractauto_autoexecprogbuf(0) <= dmi_req_data_i(16);
460
            dm_reg.abstractauto_autoexecprogbuf(1) <= dmi_req_data_i(17);
461
          end if;
462
        end if;
463
 
464
        -- auto execution trigger --
465
        if ((dmi_req_addr_i = addr_data0_c)    and (dm_reg.abstractauto_autoexecdata = '1')) or
466
           ((dmi_req_addr_i = addr_progbuf0_c) and (dm_reg.abstractauto_autoexecprogbuf(0) = '1')) or
467
           ((dmi_req_addr_i = addr_progbuf1_c) and (dm_reg.abstractauto_autoexecprogbuf(1) = '1')) then
468
          dm_reg.autoexec_wr <= '1';
469
        end if;
470
 
471
        -- acknowledge command error --
472
        if (dmi_req_addr_i = addr_abstractcs_c) then
473
          if (dmi_req_data_i(10 downto 8) = "111") then
474
            dm_reg.clr_acc_err <= '1';
475
          end if;
476
        end if;
477
 
478
        -- write program buffer --
479
        if (dmi_req_addr_i(dmi_req_addr_i'left downto 1) = addr_progbuf0_c(dmi_req_addr_i'left downto 1)) then
480
          if (dm_ctrl.busy = '0') then -- idle
481
            if (dmi_req_addr_i(0) = addr_progbuf0_c(0)) then
482
              dm_reg.progbuf(0) <= dmi_req_data_i;
483
            else
484
              dm_reg.progbuf(1) <= dmi_req_data_i;
485
            end if;
486
          end if;
487
        end if;
488
 
489
        -- invalid access (while command is executing) --
490
        if (dm_ctrl.busy = '1') then -- busy
491
          if (dmi_req_addr_i = addr_abstractcs_c) or
492
             (dmi_req_addr_i = addr_command_c) or
493
             (dmi_req_addr_i = addr_abstractauto_c) or
494
             (dmi_req_addr_i = addr_data0_c) or
495
             (dmi_req_addr_i = addr_progbuf0_c) or
496
             (dmi_req_addr_i = addr_progbuf1_c) then
497
            dm_reg.wr_acc_err <= '1';
498
          end if;
499
        end if;
500
 
501
      end if;
502
    end if;
503
  end process dmi_write_access;
504
 
505
 
506
  -- Direct Control -------------------------------------------------------------------------
507
  -- -------------------------------------------------------------------------------------------
508
  -- write to abstract data register --
509
  dci.data_we <= '1' when (dmi_req_valid_i = '1') and (dmi_req_op_i = '1') and (dmi_req_addr_i = addr_data0_c) and (dm_ctrl.busy = '0') else '0';
510
  dci.wdata   <= dmi_req_data_i;
511
 
512
  -- CPU halt/resume request --
513
  cpu_halt_req_o <= dm_reg.halt_req and dm_reg.dmcontrol_dmactive; -- single shot
514
  dci.resume_req <= dm_ctrl.hart_resume_req; -- permanent
515
 
516
  -- SoC reset --
517
  cpu_ndmrstn_o <= not (dm_reg.dmcontrol_ndmreset and dm_reg.dmcontrol_dmactive);
518
 
519
  -- build program buffer array for cpu access --
520
  cpu_progbuf(0) <= dm_ctrl.ldsw_progbuf; -- pseudo program buffer for GPR access
521
  cpu_progbuf(1) <= instr_nop_c when (dm_ctrl.pbuf_en = '0') else dm_reg.progbuf(0);
522
  cpu_progbuf(2) <= instr_nop_c when (dm_ctrl.pbuf_en = '0') else dm_reg.progbuf(1);
523
  cpu_progbuf(3) <= instr_ebreak_c; -- implicit ebreak instruction
524
 
525
  -- DMI status --
526
  dmi_resp_err_o  <= '0'; -- what can go wrong?
527
  dmi_req_ready_o <= '1'; -- always ready for new read/write accesses
528
 
529
 
530
  -- Debug Module Interface - Read Access ---------------------------------------------------
531
  -- -------------------------------------------------------------------------------------------
532
  dmi_read_access: process(clk_i)
533
  begin
534
    if rising_edge(clk_i) then
535
      dmi_resp_valid_o   <= dmi_req_valid_i; -- DMI transfer ack
536
      dmi_resp_data_o    <= (others => '0'); -- default
537
      dm_reg.rd_acc_err  <= '0';
538
      dm_reg.autoexec_rd <= '0';
539
 
540
      case dmi_req_addr_i is
541
 
542
        -- debug module status register --
543
        when addr_dmstatus_c =>
544
          dmi_resp_data_o(31 downto 23) <= (others => '0'); -- reserved (r/-)
545 60 zero_gravi
          dmi_resp_data_o(22)           <= '1'; -- impebreak (r/-): there is an implicit ebreak instruction after the visible program buffer
546 59 zero_gravi
          dmi_resp_data_o(21 downto 20) <= (others => '0'); -- reserved (r/-)
547
          dmi_resp_data_o(19)           <= dm_ctrl.hart_reset; -- allhavereset (r/-): there is only one hart that can be reset
548
          dmi_resp_data_o(18)           <= dm_ctrl.hart_reset; -- anyhavereset (r/-): there is only one hart that can be reset
549
          dmi_resp_data_o(17)           <= dm_ctrl.hart_resume_ack; -- allresumeack (r/-): there is only one hart that can acknowledge resume request
550
          dmi_resp_data_o(16)           <= dm_ctrl.hart_resume_ack; -- anyresumeack (r/-): there is only one hart that can acknowledge resume request
551
          dmi_resp_data_o(15)           <= '0'; -- allnonexistent (r/-): there is only one hart that is always existent
552
          dmi_resp_data_o(14)           <= '0'; -- anynonexistent (r/-): there is only one hart that is always existent
553
          dmi_resp_data_o(13)           <= dm_reg.dmcontrol_ndmreset; -- allunavail (r/-): there is only one hart that is unavailable during reset
554
          dmi_resp_data_o(12)           <= dm_reg.dmcontrol_ndmreset; -- anyunavail (r/-): there is only one hart that is unavailable during reset
555
          dmi_resp_data_o(11)           <= not dm_ctrl.hart_halted; -- allrunning (r/-): there is only one hart that can be RUNNING or HALTED
556
          dmi_resp_data_o(10)           <= not dm_ctrl.hart_halted; -- anyrunning (r/-): there is only one hart that can be RUNNING or HALTED
557
          dmi_resp_data_o(09)           <= dm_ctrl.hart_halted; -- allhalted (r/-): there is only one hart that can be RUNNING or HALTED
558
          dmi_resp_data_o(08)           <= dm_ctrl.hart_halted; -- anyhalted (r/-): there is only one hart that can be RUNNING or HALTED
559
          dmi_resp_data_o(07)           <= '1'; -- authenticated (r/-): authentication passed since there is no authentication
560
          dmi_resp_data_o(06)           <= '0'; -- authbusy (r/-): always ready since there is no authentication
561
          dmi_resp_data_o(05)           <= '0'; -- hasresethaltreq (r/-): halt-on-reset not implemented
562
          dmi_resp_data_o(04)           <= '0'; -- confstrptrvalid (r/-): no configuration string available
563
          dmi_resp_data_o(03 downto 00) <= "0010"; -- version (r/-): compatible to version 0.13
564
 
565
        -- debug module control --
566
        when addr_dmcontrol_c =>
567
          dmi_resp_data_o(31)           <= '0'; -- haltreq (-/w): write-only
568
          dmi_resp_data_o(30)           <= '0'; -- resumereq (-/w1): write-only
569
          dmi_resp_data_o(29)           <= '0'; -- hartreset (r/w): not supported
570
          dmi_resp_data_o(28)           <= '0'; -- ackhavereset (-/w1): write-only
571
          dmi_resp_data_o(27)           <= '0'; -- reserved (r/-)
572
          dmi_resp_data_o(26)           <= '0'; -- hasel (r/-) - there is a single currently selected hart
573
          dmi_resp_data_o(25 downto 16) <= (others => '0'); -- hartsello (r/-) - there is only one hart
574
          dmi_resp_data_o(15 downto 06) <= (others => '0'); -- hartselhi (r/-) - there is only one hart
575
          dmi_resp_data_o(05 downto 04) <= (others => '0'); -- reserved (r/-)
576
          dmi_resp_data_o(03)           <= '0'; -- setresethaltreq (-/w1): halt-on-reset request - halt-on-reset not implemented
577
          dmi_resp_data_o(02)           <= '0'; -- clrresethaltreq (-/w1): halt-on-reset ack - halt-on-reset not implemented
578
          dmi_resp_data_o(01)           <= dm_reg.dmcontrol_ndmreset; -- ndmreset (r/w): soc reset
579
          dmi_resp_data_o(00)           <= dm_reg.dmcontrol_dmactive; -- dmactive (r/w): DM reset
580
 
581
        -- hart info --
582
        when addr_hartinfo_c =>
583
          dmi_resp_data_o(31 downto 24) <= (others => '0'); -- reserved (r/-)
584
          dmi_resp_data_o(23 downto 20) <= nscratch_c; -- nscratch (r/-): number of dscratch CSRs
585
          dmi_resp_data_o(19 downto 17) <= (others => '0'); -- reserved (r/-)
586
          dmi_resp_data_o(16)           <= dataaccess_c; -- dataaccess (r/-): 1: data registers are memory-mapped, 0: data reisters are CSR-mapped
587
          dmi_resp_data_o(15 downto 12) <= datasize_c; -- datasize (r/-): number data registers in memory/CSR space
588
          dmi_resp_data_o(11 downto 00) <= dataaddr_c; -- dataaddr (r/-): data registers base address (memory/CSR)
589
 
590
        -- abstract control and status --
591
        when addr_abstractcs_c =>
592
          dmi_resp_data_o(31 downto 24) <= (others => '0'); -- reserved (r/-)
593
          dmi_resp_data_o(28 downto 24) <= "00010"; -- progbufsize (r/-): number of words in program buffer = 2
594
          dmi_resp_data_o(12)           <= dm_ctrl.busy; -- busy (r/-): abstract command in progress (1) / idle (0)
595
          dmi_resp_data_o(11)           <= '0'; -- reserved (r/-)
596
          dmi_resp_data_o(10 downto 08) <= dm_ctrl.cmderr; -- cmderr (r/w1c): any error during execution?
597
          dmi_resp_data_o(07 downto 04) <= (others => '0'); -- reserved (r/-)
598
          dmi_resp_data_o(03 downto 00) <= "0001"; -- datacount (r/-): number of implemented data registers = 1
599
 
600
--      -- abstract command (-/w) --
601
--      when addr_command_c =>
602
--        dmi_resp_data_o <= (others => '0'); -- register is write-only
603
 
604
        -- abstract command autoexec (r/w) --
605
        when addr_abstractauto_c =>
606
          dmi_resp_data_o(00) <= dm_reg.abstractauto_autoexecdata; -- autoexecdata(0): read/write access to data0 triggers execution of program buffer
607
          dmi_resp_data_o(16) <= dm_reg.abstractauto_autoexecprogbuf(0); -- autoexecprogbuf(0): read/write access to progbuf0 triggers execution of program buffer
608
          dmi_resp_data_o(17) <= dm_reg.abstractauto_autoexecprogbuf(1); -- autoexecprogbuf(1): read/write access to progbuf1 triggers execution of program buffer
609
 
610
--      -- next debug module (r/-) --
611
--      when addr_nextdm_c =>
612
--        dmi_resp_data_o <= (others => '0'); -- this is the only DM
613
 
614
        -- abstract data 0 (r/w) --
615
        when addr_data0_c =>
616
          dmi_resp_data_o <= dci.rdata;
617
 
618
        -- program buffer (r/w) --
619
        when addr_progbuf0_c =>
620
          dmi_resp_data_o <= dm_reg.progbuf(0); -- program buffer 0
621
        when addr_progbuf1_c =>
622
          dmi_resp_data_o <= dm_reg.progbuf(1); -- program buffer 1
623
 
624 60 zero_gravi
--      -- system bus access control and status (r/-) --
625
--      when addr_sbcs_c =>
626
--        dmi_resp_data_o <= (others => '0'); -- bus access not implemented
627 59 zero_gravi
 
628
        -- halt summary 0 (r/-) --
629
        when addr_haltsum0_c =>
630
          dmi_resp_data_o(0) <= dm_ctrl.hart_halted; -- hart is halted
631
 
632
        -- not implemented (r/-) --
633
        when others =>
634
          dmi_resp_data_o <= (others => '0');
635
      end case;
636
 
637
      -- invalid read access (while command is executing)
638
      -- ------------------------------------------------------------
639
      if (dmi_req_valid_i = '1') and (dmi_req_op_i = '0') then -- valid DMI read request
640
        if (dm_ctrl.busy = '1') then -- busy
641
          if (dmi_req_addr_i = addr_data0_c) or
642
             (dmi_req_addr_i = addr_progbuf0_c) or
643
             (dmi_req_addr_i = addr_progbuf1_c) then
644
            dm_reg.rd_acc_err <= '1';
645
          end if;
646
        end if;
647
      end if;
648
 
649
      -- auto execution trigger --
650
      -- ------------------------------------------------------------
651
      if (dmi_req_valid_i = '1') and (dmi_req_op_i = '0') then -- valid DMI read request
652
        if ((dmi_req_addr_i = addr_data0_c)    and (dm_reg.abstractauto_autoexecdata = '1')) or
653
           ((dmi_req_addr_i = addr_progbuf0_c) and (dm_reg.abstractauto_autoexecprogbuf(0) = '1')) or
654
           ((dmi_req_addr_i = addr_progbuf1_c) and (dm_reg.abstractauto_autoexecprogbuf(1) = '1')) then
655
          dm_reg.autoexec_rd <= '1';
656
        end if;
657
      end if;
658
 
659
    end if;
660
  end process dmi_read_access;
661
 
662
 
663
  -- **************************************************************************************************************************
664
  -- CPU Bus Interface
665
  -- **************************************************************************************************************************
666
 
667
  -- Access Control ------------------------------------------------------------------------
668
  -- -------------------------------------------------------------------------------------------
669
  acc_en <= '1' when (cpu_addr_i(hi_abb_c downto lo_abb_c) = dm_base_c(hi_abb_c downto lo_abb_c)) else '0';
670 60 zero_gravi
  maddr  <= cpu_addr_i(lo_abb_c-1 downto lo_abb_c-2); -- (sub-)module select address
671 59 zero_gravi
  rden   <= acc_en and cpu_rden_i;
672
  wren   <= acc_en and cpu_wren_i;
673
 
674
 
675
  -- Write Access ---------------------------------------------------------------------------
676
  -- -------------------------------------------------------------------------------------------
677
  write_access: process(clk_i)
678
  begin
679
    if rising_edge(clk_i) then
680
      -- Data buffer --
681
      if (dci.data_we = '1') then -- DM write access
682
        data_buf <= dci.wdata;
683
      elsif (acc_en = '1') and (maddr = "10") and (wren = '1') then -- BUS write access
684
        data_buf <= cpu_data_i;
685
      end if;
686
      -- Control and Status Register --
687
      dci.halt_ack      <= '0'; -- all writable flags auto-clear
688
      dci.resume_ack    <= '0';
689
      dci.execute_ack   <= '0';
690
      dci.exception_ack <= '0';
691
      if (acc_en = '1') and (maddr = "11") and (wren = '1') then
692
        dci.halt_ack      <= cpu_data_i(sreg_halt_ack_c);
693
        dci.resume_ack    <= cpu_data_i(sreg_resume_ack_c);
694
        dci.execute_ack   <= cpu_data_i(sreg_execute_ack_c);
695
        dci.exception_ack <= cpu_data_i(sreg_exception_ack_c);
696
      end if;
697
    end if;
698
  end process write_access;
699
 
700
  -- DM data buffer read access --
701
  dci.rdata <= data_buf;
702
 
703
 
704
  -- Read Access ----------------------------------------------------------------------------
705
  -- -------------------------------------------------------------------------------------------
706
  read_access: process(clk_i)
707
  begin
708
    if rising_edge(clk_i) then
709
      cpu_ack_o  <= rden or wren;
710
      cpu_data_o <= (others => '0');
711
      if (rden = '1') then -- output gate
712 60 zero_gravi
        case maddr is -- module select
713 59 zero_gravi
          when "00" => -- code ROM
714
            cpu_data_o <= code_rom_file(to_integer(unsigned(cpu_addr_i(6 downto 2))));
715
          when "01" => -- program buffer
716
            cpu_data_o <= cpu_progbuf(to_integer(unsigned(cpu_addr_i(3 downto 2))));
717
          when "10" => -- data buffer
718
            cpu_data_o <= data_buf;
719
          when others => -- status/control register
720
            cpu_data_o(sreg_resume_req_c)  <= dci.resume_req;
721
            cpu_data_o(sreg_execute_req_c) <= dci.execute_req;
722
        end case;
723
      end if;
724
    end if;
725
  end process read_access;
726
 
727
 
728
end neorv32_debug_dm_rtl;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.