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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [rtl/] [core/] [neorv32_debug_dtm.vhd] - Blame information for rev 62

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

Line No. Rev Author Line
1 59 zero_gravi
-- #################################################################################################
2
-- # << NEORV32 - RISC-V Debug Transport Module (DTM) >>                                           #
3
-- # ********************************************************************************************* #
4
-- # Provides a JTAG-compatible TAP to access the DMI register interface.                          #
5
-- # Compatible to the RISC-V debug specification.                                                 #
6
-- # ********************************************************************************************* #
7
-- # BSD 3-Clause License                                                                          #
8
-- #                                                                                               #
9
-- # Copyright (c) 2021, Stephan Nolting. All rights reserved.                                     #
10
-- #                                                                                               #
11
-- # Redistribution and use in source and binary forms, with or without modification, are          #
12
-- # permitted provided that the following conditions are met:                                     #
13
-- #                                                                                               #
14
-- # 1. Redistributions of source code must retain the above copyright notice, this list of        #
15
-- #    conditions and the following disclaimer.                                                   #
16
-- #                                                                                               #
17
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
18
-- #    conditions and the following disclaimer in the documentation and/or other materials        #
19
-- #    provided with the distribution.                                                            #
20
-- #                                                                                               #
21
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
22
-- #    endorse or promote products derived from this software without specific prior written      #
23
-- #    permission.                                                                                #
24
-- #                                                                                               #
25
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
26
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
27
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
28
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
29
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
30
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
31
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
32
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
33
-- # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
34
-- # ********************************************************************************************* #
35
-- # https://github.com/stnolting/riscv-debug-dtm                              (c) Stephan Nolting #
36
-- #################################################################################################
37
 
38
library ieee;
39
use ieee.std_logic_1164.all;
40
use ieee.numeric_std.all;
41
 
42
entity neorv32_debug_dtm is
43
  generic (
44 62 zero_gravi
    IDCODE_VERSION : std_ulogic_vector(03 downto 0); -- version
45
    IDCODE_PARTID  : std_ulogic_vector(15 downto 0); -- part number
46
    IDCODE_MANID   : std_ulogic_vector(10 downto 0)  -- manufacturer id
47 59 zero_gravi
  );
48
  port (
49
    -- global control --
50
    clk_i            : in  std_ulogic; -- global clock line
51
    rstn_i           : in  std_ulogic; -- global reset line, low-active
52
    -- jtag connection --
53
    jtag_trst_i      : in  std_ulogic;
54
    jtag_tck_i       : in  std_ulogic;
55
    jtag_tdi_i       : in  std_ulogic;
56
    jtag_tdo_o       : out std_ulogic;
57
    jtag_tms_i       : in  std_ulogic;
58
    -- debug module interface (DMI) --
59
    dmi_rstn_o       : out std_ulogic;
60
    dmi_req_valid_o  : out std_ulogic;
61
    dmi_req_ready_i  : in  std_ulogic; -- DMI is allowed to make new requests when set
62
    dmi_req_addr_o   : out std_ulogic_vector(06 downto 0);
63
    dmi_req_op_o     : out std_ulogic; -- 0=read, 1=write
64
    dmi_req_data_o   : out std_ulogic_vector(31 downto 0);
65
    dmi_resp_valid_i : in  std_ulogic; -- response valid when set
66
    dmi_resp_ready_o : out std_ulogic; -- ready to receive respond
67
    dmi_resp_data_i  : in  std_ulogic_vector(31 downto 0);
68
    dmi_resp_err_i   : in  std_ulogic -- 0=ok, 1=error
69
  );
70
end neorv32_debug_dtm;
71
 
72
architecture neorv32_debug_dtm_rtl of neorv32_debug_dtm is
73
 
74
  -- DMI Configuration (fixed!) --
75
  constant dmi_idle_c    : std_ulogic_vector(02 downto 0) := "010"; -- minimum number if idle cycles
76
  constant dmi_version_c : std_ulogic_vector(03 downto 0) := "0001"; -- version (0.13)
77
  constant dmi_abits_c   : std_ulogic_vector(05 downto 0) := "000111"; -- number of DMI address bits (7)
78
 
79
  -- tap controller - fsm --
80
  type tap_ctrl_state_t is (LOGIC_RESET, DR_SCAN, DR_CAPTURE, DR_SHIFT, DR_EXIT1, DR_PAUSE, DR_EXIT2, DR_UPDATE,
81
                               RUN_IDLE, IR_SCAN, IR_CAPTURE, IR_SHIFT, IR_EXIT1, IR_PAUSE, IR_EXIT2, IR_UPDATE);
82
  type tap_ctrl_t is record
83
    state      : tap_ctrl_state_t;
84
    state_prev : tap_ctrl_state_t;
85
    trst_sync  : std_ulogic_vector(01 downto 0);
86
    tck_sync   : std_ulogic_vector(02 downto 0);
87
    tdi_sync   : std_ulogic_vector(01 downto 0);
88
    tdo_sync   : std_ulogic;
89
    tms_sync   : std_ulogic_vector(01 downto 0);
90
  end record;
91
  signal tap_ctrl : tap_ctrl_t;
92
 
93
  -- tap registers --
94
  type tap_reg_t is record
95
    ireg             : std_ulogic_vector(04 downto 0);
96
    bypass           : std_ulogic;
97
    idcode           : std_ulogic_vector(31 downto 0);
98
    dtmcs, dtmcs_nxt : std_ulogic_vector(31 downto 0);
99
    dmi,   dmi_nxt   : std_ulogic_vector((7+32+2)-1 downto 0); -- 7-bit address + 32-bit data + 2-bit operation
100
  end record;
101
  signal tap_reg : tap_reg_t;
102
 
103
  -- debug module interface --
104
  type dmi_ctrl_state_t is (DMI_IDLE, DMI_READ_WAIT, DMI_READ, DMI_READ_BUSY,
105
                            DMI_WRITE_WAIT, DMI_WRITE, DMI_WRITE_BUSY);
106
  type dmi_ctrl_t is record
107
    state        : dmi_ctrl_state_t;
108
    --
109
    dmihardreset : std_ulogic;
110
    dmireset     : std_ulogic;
111
    --
112
    err          : std_ulogic; -- sticky error
113
    rdata        : std_ulogic_vector(31 downto 0);
114
    wdata        : std_ulogic_vector(31 downto 0);
115
    addr         : std_ulogic_vector(06 downto 0);
116
  end record;
117
  signal dmi_ctrl : dmi_ctrl_t;
118
 
119
begin
120
 
121
  -- Tap Control FSM ------------------------------------------------------------------------
122
  -- -------------------------------------------------------------------------------------------
123
  tap_control: process(rstn_i, clk_i)
124
  begin
125
    if (rstn_i = '0') then
126
      tap_ctrl.trst_sync <= (others => '0');
127
      tap_ctrl.tck_sync  <= (others => '0');
128
      tap_ctrl.tdi_sync  <= (others => '0');
129
      tap_ctrl.tms_sync  <= (others => '0');
130
      jtag_tdo_o         <= '0';
131
      --
132
      tap_ctrl.state      <= LOGIC_RESET;
133
      tap_ctrl.state_prev <= LOGIC_RESET;
134
    elsif rising_edge(clk_i) then
135
      -- synchronizer --
136
      tap_ctrl.trst_sync <= tap_ctrl.trst_sync(0) & jtag_trst_i;
137
      tap_ctrl.tck_sync  <= tap_ctrl.tck_sync(1 downto 0) & jtag_tck_i;
138
      tap_ctrl.tdi_sync  <= tap_ctrl.tdi_sync(0) & jtag_tdi_i;
139
      tap_ctrl.tms_sync  <= tap_ctrl.tms_sync(0) & jtag_tms_i;
140
      jtag_tdo_o         <= tap_ctrl.tdo_sync;
141
 
142
      -- state machine --
143
      tap_ctrl.state_prev <= tap_ctrl.state;
144
      if (tap_ctrl.trst_sync(1) = '0') then -- reset
145
        tap_ctrl.state <= LOGIC_RESET;
146
      elsif (tap_ctrl.tck_sync(2) = '0') and (tap_ctrl.tck_sync(1) = '1') then -- clock pulse (trigger on rising edge)
147
        case tap_ctrl.state is -- JTAG state machine
148
          when LOGIC_RESET => if (tap_ctrl.tms_sync(1) = '0') then tap_ctrl.state <= RUN_IDLE;   else tap_ctrl.state <= LOGIC_RESET; end if;
149
          when RUN_IDLE    => if (tap_ctrl.tms_sync(1) = '0') then tap_ctrl.state <= RUN_IDLE;   else tap_ctrl.state <= DR_SCAN;     end if;
150
          when DR_SCAN     => if (tap_ctrl.tms_sync(1) = '0') then tap_ctrl.state <= DR_CAPTURE; else tap_ctrl.state <= IR_SCAN;     end if;
151
          when DR_CAPTURE  => if (tap_ctrl.tms_sync(1) = '0') then tap_ctrl.state <= DR_SHIFT;   else tap_ctrl.state <= DR_EXIT1;    end if;
152
          when DR_SHIFT    => if (tap_ctrl.tms_sync(1) = '0') then tap_ctrl.state <= DR_SHIFT;   else tap_ctrl.state <= DR_EXIT1;    end if;
153
          when DR_EXIT1    => if (tap_ctrl.tms_sync(1) = '0') then tap_ctrl.state <= DR_PAUSE;   else tap_ctrl.state <= DR_UPDATE;   end if;
154
          when DR_PAUSE    => if (tap_ctrl.tms_sync(1) = '0') then tap_ctrl.state <= DR_PAUSE;   else tap_ctrl.state <= DR_EXIT2;    end if;
155
          when DR_EXIT2    => if (tap_ctrl.tms_sync(1) = '0') then tap_ctrl.state <= DR_SHIFT;   else tap_ctrl.state <= DR_UPDATE;   end if;
156
          when DR_UPDATE   => if (tap_ctrl.tms_sync(1) = '0') then tap_ctrl.state <= RUN_IDLE;   else tap_ctrl.state <= DR_SCAN;     end if;
157
          when IR_SCAN     => if (tap_ctrl.tms_sync(1) = '0') then tap_ctrl.state <= IR_CAPTURE; else tap_ctrl.state <= LOGIC_RESET; end if;
158
          when IR_CAPTURE  => if (tap_ctrl.tms_sync(1) = '0') then tap_ctrl.state <= IR_SHIFT;   else tap_ctrl.state <= IR_EXIT1;    end if;
159
          when IR_SHIFT    => if (tap_ctrl.tms_sync(1) = '0') then tap_ctrl.state <= IR_SHIFT;   else tap_ctrl.state <= IR_EXIT1;    end if;
160
          when IR_EXIT1    => if (tap_ctrl.tms_sync(1) = '0') then tap_ctrl.state <= IR_PAUSE;   else tap_ctrl.state <= IR_UPDATE;   end if;
161
          when IR_PAUSE    => if (tap_ctrl.tms_sync(1) = '0') then tap_ctrl.state <= IR_PAUSE;   else tap_ctrl.state <= IR_EXIT2;    end if;
162
          when IR_EXIT2    => if (tap_ctrl.tms_sync(1) = '0') then tap_ctrl.state <= IR_SHIFT;   else tap_ctrl.state <= IR_UPDATE;   end if;
163
          when IR_UPDATE   => if (tap_ctrl.tms_sync(1) = '0') then tap_ctrl.state <= RUN_IDLE;   else tap_ctrl.state <= DR_SCAN;     end if;
164
          when others      => tap_ctrl.state <= LOGIC_RESET;
165
        end case;
166
      end if;
167
    end if;
168
  end process tap_control;
169
 
170
 
171
  -- Tap Register Access --------------------------------------------------------------------
172
  -- -------------------------------------------------------------------------------------------
173
  reg_access: process(clk_i)
174
  begin
175
    if rising_edge(clk_i) then
176
      if (tap_ctrl.trst_sync(1) = '0') then -- reset
177
        tap_reg.ireg <= "00001"; -- IDCODE
178
      elsif (tap_ctrl.tck_sync(2) = '0') and (tap_ctrl.tck_sync(1) = '1') then -- clock pulse (trigger on rising edge)
179
 
180
        -- instruction register --
181
        if (tap_ctrl.state = LOGIC_RESET) then -- reset
182
          tap_reg.ireg <= "00001"; -- IDCODE
183
        elsif (tap_ctrl.state = IR_CAPTURE) then -- preload phase
184
          tap_reg.ireg <= "00001"; -- IDCODE
185
        elsif (tap_ctrl.state = IR_SHIFT) then -- access phase
186
          tap_reg.ireg <= tap_ctrl.tdi_sync(1) & tap_reg.ireg(tap_reg.ireg'left downto 1);
187
        end if;
188
 
189
        -- data register --
190
        if (tap_ctrl.state = DR_CAPTURE) then -- preload phase
191
          case tap_reg.ireg is
192
            when "00001" => tap_reg.idcode <= IDCODE_VERSION & IDCODE_PARTID & IDCODE_MANID & '1'; -- IDCODE (LBS has to be always set!)
193
            when "10000" => tap_reg.dtmcs  <= tap_reg.dtmcs_nxt;-- dtmcs
194
            when "10001" => tap_reg.dmi    <= tap_reg.dmi_nxt; -- dmi
195
            when others  => tap_reg.bypass <= '0'; -- BYPASS
196
          end case;
197
        elsif (tap_ctrl.state = DR_SHIFT) then -- access phase
198
          case tap_reg.ireg is
199
            when "00001" => tap_reg.idcode <= tap_ctrl.tdi_sync(1) & tap_reg.idcode(tap_reg.idcode'left downto 1); -- IDCODE
200
            when "10000" => tap_reg.dtmcs  <= tap_ctrl.tdi_sync(1) & tap_reg.dtmcs(tap_reg.dtmcs'left downto 1); -- dtmcs
201
            when "10001" => tap_reg.dmi    <= tap_ctrl.tdi_sync(1) & tap_reg.dmi(tap_reg.dmi'left downto 1); -- dmi
202
            when others  => tap_reg.bypass <= tap_ctrl.tdi_sync(1); -- BYPASS
203
          end case;
204
        end if;
205
      end if;
206
 
207
      -- serial data output --
208
      if (tap_ctrl.state = IR_SHIFT) then
209
        tap_ctrl.tdo_sync <= tap_reg.ireg(0);
210
      else
211
        case tap_reg.ireg is
212
          when "00001" => tap_ctrl.tdo_sync <= tap_reg.idcode(0); -- IDCODE
213
          when "10000" => tap_ctrl.tdo_sync <= tap_reg.dtmcs(0); -- dtmcs
214
          when "10001" => tap_ctrl.tdo_sync <= tap_reg.dmi(0); -- dmi
215
          when others  => tap_ctrl.tdo_sync <= tap_reg.bypass; -- BYPASS
216
        end case;
217
      end if;
218
    end if;
219
  end process reg_access;
220
 
221
 
222
  -- Debug Module Interface -----------------------------------------------------------------
223
  -- -------------------------------------------------------------------------------------------
224
 
225
  -- DTM Control and Status Register (dtmcs) --
226
  tap_reg.dtmcs_nxt(31 downto 18) <= (others => '0'); -- unused
227
  tap_reg.dtmcs_nxt(17)           <= '0'; -- dmihardreset, always reads as zero
228
  tap_reg.dtmcs_nxt(16)           <= '0'; -- dmireset, always reads as zero
229
  tap_reg.dtmcs_nxt(15)           <= '0'; -- unused
230
  tap_reg.dtmcs_nxt(14 downto 12) <= dmi_idle_c; -- minimum number if idle cycles
231
  tap_reg.dtmcs_nxt(11 downto 10) <= tap_reg.dmi_nxt(1 downto 0); -- dmistat
232
  tap_reg.dtmcs_nxt(09 downto 04) <= dmi_abits_c; -- number of DMI address bits
233
  tap_reg.dtmcs_nxt(03 downto 00) <= dmi_version_c; -- version
234
 
235
 
236
  -- Debug Module Interface Access Register (dmi) --
237
  dmi_controller: process(rstn_i, clk_i)
238
  begin
239
    if (rstn_i = '0') then
240
      dmi_ctrl.state        <= DMI_IDLE;
241
      dmi_ctrl.dmihardreset <= '1';
242
      dmi_ctrl.dmireset     <= '1';
243
      dmi_ctrl.err          <= '0';
244
      dmi_ctrl.rdata        <= (others => '-');
245
      dmi_ctrl.wdata        <= (others => '-');
246
      dmi_ctrl.addr         <= (others => '-');
247
    elsif rising_edge(clk_i) then
248
 
249
      -- DMI status and control --
250
      dmi_ctrl.dmihardreset <= '0'; -- default
251
      dmi_ctrl.dmireset     <= '0'; -- default
252
      if (tap_ctrl.state = DR_UPDATE) and (tap_ctrl.state_prev /= DR_UPDATE) and (tap_reg.ireg = "10000") then
253
        dmi_ctrl.dmireset     <= tap_reg.dtmcs(16);
254
        dmi_ctrl.dmihardreset <= tap_reg.dtmcs(17);
255
      end if;
256
 
257
      -- DMI interface arbiter --
258
      if (dmi_ctrl.dmihardreset = '1') then -- DMI hard reset
259
        dmi_ctrl.state <= DMI_IDLE;
260
        dmi_ctrl.err   <= '0';
261
      else
262
        case dmi_ctrl.state is
263
 
264
          when DMI_IDLE => -- waiting for new request
265
            if (tap_ctrl.state = DR_UPDATE) and (tap_ctrl.state_prev /= DR_UPDATE) and (tap_reg.ireg = "10001") then -- update <dmi>
266
              case tap_reg.dmi(1 downto 0) is -- op field
267
                when "01"   => dmi_ctrl.state <= DMI_READ_WAIT; -- read
268
                when "10"   => dmi_ctrl.state <= DMI_WRITE_WAIT; -- write
269
                when others => NULL;
270
              end case;
271
              dmi_ctrl.addr   <= tap_reg.dmi(40 downto 34);
272
              dmi_ctrl.wdata  <= tap_reg.dmi(33 downto 02);
273
            end if;
274
 
275
          when DMI_READ_WAIT => -- wait for DMI to become ready
276
            if (dmi_req_ready_i = '1') then
277
              dmi_ctrl.state <= DMI_READ;
278
            end if;
279
 
280
          when DMI_READ => -- start read access
281
            dmi_ctrl.state <= DMI_READ_BUSY;
282
 
283
          when DMI_READ_BUSY => -- pending read access
284
            if (dmi_resp_valid_i = '1') then
285
              dmi_ctrl.rdata <= dmi_resp_data_i;
286
              dmi_ctrl.err   <= dmi_ctrl.err or dmi_resp_err_i; -- sticky error
287
              dmi_ctrl.state <= DMI_IDLE;
288
            end if;
289
 
290
          when DMI_WRITE_WAIT => -- wait for DMI to become ready
291
            if (dmi_req_ready_i = '1') then
292
              dmi_ctrl.state <= DMI_WRITE;
293
            end if;
294
 
295
          when DMI_WRITE => -- start write access
296
            dmi_ctrl.state <= DMI_WRITE_BUSY;
297
 
298
          when DMI_WRITE_BUSY => -- pending write access
299
            if (dmi_resp_valid_i = '1') then
300
              dmi_ctrl.err   <= dmi_ctrl.err or dmi_resp_err_i; -- sticky error
301
              dmi_ctrl.state <= DMI_IDLE;
302
            end if;
303
 
304
          when others => -- undefined
305
            dmi_ctrl.state <= DMI_IDLE;
306
 
307
        end case;
308
        -- override sticky error flag --
309
        if (dmi_ctrl.dmireset = '1') then
310
          dmi_ctrl.err <= '0';
311
        end if;
312
      end if;
313
    end if;
314
  end process dmi_controller;
315
 
316
  -- DMI register read access --
317
  tap_reg.dmi_nxt(40 downto 34) <= dmi_ctrl.addr; -- address
318
  tap_reg.dmi_nxt(33 downto 02) <= dmi_ctrl.rdata; -- read data
319
  tap_reg.dmi_nxt(01 downto 00) <= "11" when (dmi_ctrl.state /= DMI_IDLE) else (dmi_ctrl.err & '0'); -- status
320
 
321
  -- direct DMI output --
322
  dmi_rstn_o       <= '0' when (dmi_ctrl.dmihardreset = '1') else '1';
323
  dmi_req_valid_o  <= '1' when (dmi_ctrl.state = DMI_READ) or (dmi_ctrl.state = DMI_WRITE) else '0';
324
  dmi_req_op_o     <= '1' when (dmi_ctrl.state = DMI_WRITE) or (dmi_ctrl.state = DMI_WRITE_BUSY) else '0';
325
  dmi_resp_ready_o <= '1' when (dmi_ctrl.state = DMI_READ_BUSY) or (dmi_ctrl.state = DMI_WRITE_BUSY) else '0';
326
  dmi_req_addr_o   <= dmi_ctrl.addr;
327
  dmi_req_data_o   <= dmi_ctrl.wdata;
328
 
329
 
330
end neorv32_debug_dtm_rtl;

powered by: WebSVN 2.1.0

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