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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [docs/] [datasheet/] [on_chip_debugger.adoc] - Blame information for rev 61

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

Line No. Rev Author Line
1 60 zero_gravi
<<<
2
:sectnums:
3
== On-Chip Debugger (OCD)
4
 
5
The NEORV32 Processor features an _on-chip debugger_ (OCD) implementing **execution-based debugging** that is compatible
6
to the **Minimal RISC-V Debug Specification Version 0.13.2**.
7
Please refer to this spec for in-deep information.
8
A copy of the specification is available in `docs/references/riscv-debug-release.pdf`.
9
The NEORV32 OCD provides the following key features:
10
 
11
* JTAG test access port
12
* run-control of the CPU: halting, single-stepping and resuming
13
* executing arbitrary programs during debugging
14
* accessing core registers (direct access to GPRs, indirect access to CSRs via program buffer)
15
* indirect access to the whole processor address space (via program buffer))
16
* compatible to the https://github.com/riscv/riscv-openocd[RISC-V port of OpenOCD];
17
  pre-built binaries can be obtained for example from https://www.sifive.com/software[SiFive]
18
 
19 61 zero_gravi
.OCD Security Note
20
[IMPORTANT]
21
Access via the OCD is _always authenticated_ (`dmstatus.authenticated` == `1`). Hence, the
22
_whole system_ can always be accessed via the on-chip debugger.
23
 
24 60 zero_gravi
[NOTE]
25
The OCD requires additional resources for implementation and _might_ also increase the critical path resulting in less
26
performance. If the OCD is not really required for the _final_ implementation, it can be disabled and thus,
27
discarded from implementation. In this case all circuitry of the debugger is completely removed (no impact
28
on area, energy or timing at all).
29
 
30
[TIP]
31
A simple example on how to use NEORV32 on-chip debugger in combination with `OpenOCD` and `gdb`
32
is shown in chapter <<_debugging_using_the_on_chip_debugger>>.
33
 
34
The NEORV32 on-chip debugger complex is based on three hardware modules:
35
 
36
.NEORV32 on-chip debugger complex
37
image::neorv32_ocd_complex.png[align=center]
38
 
39
[start=1]
40
. <<_debug_transport_module_dtm>> (`rtl/core/neorv32_debug_dtm.vhd`): External JTAG access tap to allow an external
41
  adapter to interface with the _debug module(DM)_ using the _debug module interface (dmi)_.
42
. <<_debug_module_dm>> (`rtl/core/neorv32_debug_tm.vhd`): Debugger control unit that is configured by the DTM via the
43
  the _dmi_. Form the CPU's "point of view" this module behaves as a memory-mapped "peripheral" that can be accessed
44
  via the processor-internal bus. The memory-mapped registers provide an internal _data buffer_ for data transfer
45
  from/to the DM, a _code ROM_ containing the "park loop" code,   a _program buffer_ to allow the debugger to
46
  execute small programs defined by the DM and a _status register_ that is used to communicate
47
  _halt_, _resume_ and _execute_ requests/acknowledges from/to the DM.
48
. CPU <<_cpu_debug_mode>> extension (part of`rtl/core/neorv32_cpu_control.vhd`):
49
  This extension provides the "debug execution mode" which executes the "park loop" code from the DM.
50
  The mode also provides additional CSRs.
51
 
52
**Theory of Operation**
53
 
54
When debugging the system using the OCD, the debugger issues a halt request to the CPU (via the CPU's
55
`db_halt_req_i` signal) to make the CPU enter _debug mode_. In this state, the application-defined architectural
56
state of the system/CPU is "frozen" so the debugger can monitor and even modify it.
57
While in debug mode, the CPU executes the "park loop" code from the _code ROM_ of the DM.
58
This park loop implements an endless loop, in which the CPU polls the memory-mapped _status register_ that is
59
controlled by the _debug module (DM)_. The flags of these register are used to communicate _requests_ from
60
the DM and to _acknowledge_ them by the CPU: trigger execution of the program buffer or resume the halted
61
application.
62
 
63
 
64
 
65
<<<
66
// ####################################################################################################################
67
:sectnums:
68
=== Debug Transport Module (DTM)
69
 
70
The debug transport module (VHDL module: `rtl/core/neorv32_debug_dtm.vhd`) provides a JTAG test access port (TAP).
71
The DTM is the first entity in the debug system, which connects and external debugger via JTAG to the next debugging
72
entity: the debug module (DM).
73 61 zero_gravi
External JTAG access is provided by the following top-level ports.
74 60 zero_gravi
 
75
.JTAG top level signals
76
[cols="^2,^2,^2,<8"]
77
[options="header",grid="rows"]
78
|=======================
79
| Name          | Width | Direction | Description
80
| `jtag_trst_i` | 1     | in        | TAP reset (low-active); this signal is optional, make sure to pull it _high_ if it is not used
81
| `jtag_tck_i`  | 1     | in        | serial clock
82
| `jtag_tdi_i`  | 1     | in        | serial data input
83
| `jtag_tdo_o`  | 1     | out       | serial data output
84
| `jtag_tms_i`  | 1     | in        | mode select
85
|=======================
86
 
87
.JTAG Clock
88
[IMPORTANT]
89
The actual JTAG clock signal is **not** used as primary clock. Instead it is used to synchronize
90
JTGA accesses, while all internal operations trigger on the system clock. Hence, no additional clock domain is required
91
for integration of this module.
92
However, this constraints the maximal JTAG clock (`jtag_tck_i`) frequency to be less than or equal to
93
1/4 of the system clock (`clk_i`) frequency.
94
 
95
[NOTE]
96
If the on-chip debugger is disabled (_ON_CHIP_DEBUGGER_EN_ = false) the JTAG serial input `jtag_tdi_i` is directly
97
connected to the JTAG serial output `jtag_tdo_o` to maintain the JTAG chain.
98
 
99
[WARNING]
100
The NEORV32 JTAG TAP does not provide a _boundary check_ function (yet?). Hence, physical device pins cannot be accessed.
101
 
102
The DTM uses the "debug module interface (dmi)" to access the actual debug module (DM).
103
These accesses are controlled by TAP-internal registers.
104
Each registers is selected by the JTAG instruction register (`IR`) and accessed through the JTAG data register (`DR`).
105
 
106
[NOTE]
107
The DTM's instruction and data registers can be accessed using OpenOCDs `irscan` and `drscan` commands.
108
The RISC-V port of OpenOCD also provides low-level command (`riscv dmi_read` & `riscv dmi_write`) to access the _dmi_
109
debug module interface.
110
 
111
JTAG access is conducted via the *instruction register* `IR`, which is 5 bit wide, and several *data registers* `DR`
112
with different sizes.
113
The data registers are accessed by writing the according address to the instruction register.
114
The following table shows the available data registers:
115
 
116
.JTAG TAP registers
117
[cols="^2,^2,^2,<8"]
118
[options="header",grid="rows"]
119
|=======================
120
| Address (via `IR`) | Name     | Size [bits] | Description
121
| `00001`            | `IDCODE` | 32          | identifier, default: `0x0CAFE001` (configurable via package's `jtag_tap_idcode_*` constants)
122
| `10000`            | `DTMCS`  | 32          | debug transport module control and status register
123
| `10001`            | `DMI`    | 41          | debug module interface (_dmi_); 7-bit address, 32-bit read/write data, 2-bit operation (`00` = NOP; `10` = write; `01` = read)
124
| others             | `BYPASS` | 1           | default JTAG bypass register
125
|=======================
126
 
127
[INFO]
128
See the https://github.com/riscv/riscv-debug-spec[RISC-V debug specification] for more information regarding the data
129
registers and operations.
130
A local copy can be found in `docs/references`.
131
 
132
 
133
 
134
<<<
135
// ####################################################################################################################
136
:sectnums:
137
=== Debug Module (DM)
138
 
139
According to the RISC-V debug specification, the DM (VHDL module: `rtl/core/neorv32_debug_dm.vhd`)
140
acts as a translation interface between abstract operations issued by the debugger and the platform-specific
141
debugger implementation. It supports the following features (excerpt from the debug spec):
142
 
143
* Gives the debugger necessary information about the implementation.
144
* Allows the hart to be halted and resumed and provides status of the current state.
145
* Provides abstract read and write access to the halted hart's GPRs.
146
* Provides access to a reset signal that allows debugging from the very first instruction after reset.
147
* Provides a mechanism to allow debugging the hart immediately out of reset. (_still experimental_)
148
* Provides a Program Buffer to force the hart to execute arbitrary instructions.
149
* Allows memory access from a hart's point of view.
150
 
151
The NEORV32 DM follows the "Minimal RISC-V External Debug Specification" to provide full debugging
152
capabilities while keeping resource (area) requirements at a minimum level.
153
It implements the **execution based debugging scheme** for a single hart and provides the following
154
hardware features:
155
 
156
* program buffer with 2 entries and implicit `ebreak` instruction afterwards
157
* no _direct_ bus access (indirect bus access via the CPU)
158
* abstract commands: "access register" plus auto-execution
159
* no _dedicated_ halt-on-reset capabilities yet (but can be emulated)
160
 
161
The DM provides two "sides of access": access from the DTM via the _debug module interface (dmi)_ and access from the
162
CPU via the processor-internal bus. From the DTM's point of view, the DM implements a set of <<_dm_registers>> that
163
are used to control and monitor the actual debugging. From the CPU's point of view, the DM implements several
164
memory-mapped registers (within the _normal_ address space) that are used for communicating debugging control
165
and status (<<_dm_cpu_access>>).
166
 
167
 
168
:sectnums:
169
==== DM Registers
170
 
171
The DM is controlled via a set of registers that are accessed via the DTM's _dmi_.
172
The "Minimal RISC-V Debug Specification" requires only a subset of the registers specified in the spec.
173
The following registers are implemented.
174
Write accesses to any other registers are ignored and read accesses will always return zero.
175
Register names that are encapsulated in "( )" are not actually implemented; however, they are listed to explicitly show
176
their functionality.
177
 
178
.Available DM registers
179
[cols="^2,^3,<7"]
180
[options="header",grid="rows"]
181
|=======================
182
| Address | Name           | Description
183
|  `0x04` | `data0`        | Abstract data 0, used for data transfer between debugger and processor
184
|  `0x10` | `dmcontrol`    | Debug module control
185
|  `0x11` | `dmstatus`     | Debug module status
186
|  `0x12` | `hartinfo`     | Hart information
187
|  `0x16` | `abstracts`    | Abstract control and status
188
|  `0x17` | `command`      | Abstract command
189
|  `0x18` | `abstractauto` | Abstract command auto-execution
190
|  `0x1d` | (`nextdm`)     | Base address of _next_ DM; read as zero to indicate there is only _one_ DM
191
|  `0x20` | `progbuf0`     | Program buffer 0
192
|  `0x21` | `progbuf1`     | Program buffer 1
193
|  `0x38` | (`sbcs`)       | System bus access control and status; read as zero to indicate there is no _direct_ system bus access
194
|  `0x40` | `haltsum0`     | Halt summary 0
195
|=======================
196
 
197
 
198
:sectnums!:
199
===== **`data`**
200
 
201
[cols="4,27,>7"]
202
[frame="topbot",grid="none"]
203
|======
204
| 0x04 | **Abstract data 0** | `data0`
205
3+| Reset value: _UNDEFINED_
206
3+| Basic read/write registers to be used with abstract command (for example to read/write data from/to CPU GPRs).
207
|======
208
 
209
 
210
:sectnums!:
211
===== **`dmcontrol`**
212
 
213
[cols="4,27,>7"]
214
[frame="topbot",grid="none"]
215
|======
216
| 0x10 | **Debug module control register** | `dmcontrol`
217
3+| Reset value: 0x00000000
218
3+| Control of the overall debug module and the hart. The following table shows all implemented bits. All remaining bits/bit-fields are configures as "zero" and are
219
read-only. Writing '1' to these bits/fields will be ignored.
220
|======
221
 
222
.`dmcontrol` - debug module control register bits
223
[cols="^1,^2,^1,<8"]
224
[options="header",grid="rows"]
225
|=======================
226
| Bit | Name [RISC-V]  | R/W | Description
227
| 31  | `haltreq`      | -/w | set/clear hart halt request
228
| 30  | `resumereq`    | -/w | request hart to resume
229
| 28  | `ackhavereset` | -/w | write `1` to clear `*havereset` flags
230
|  1  | `ndmreset`     | r/w | put whole processor into reset when `1`
231
|  0  | `dmactive`     | r/w | DM enable; writing `0`-`1` will reset the DM
232
|=======================
233
 
234
 
235
:sectnums!:
236
===== **`dmstatus`**
237
 
238
[cols="4,27,>7"]
239
[frame="topbot",grid="none"]
240
|======
241
| 0x11 | **Debug module status register** | `dmstatus`
242
3+| Reset value: 0x00000000
243
3+| Current status of the overall debug module and the hart. The entire register is read-only.
244
|======
245
 
246
.`dmstatus` - debug module status register bits
247
[cols="^1,^2,<10"]
248
[options="header",grid="rows"]
249
|=======================
250
| Bit   | Name [RISC-V]     | Description
251
| 31:23 | _reserved_        | reserved; always zero
252
| 22    | `impebreak`       | always `1`; indicates an implicit `ebreak` instruction after the last program buffer entry
253
| 21:20 | _reserved_        | reserved; always zero
254
| 19    | `allhavereset`    .2+| `1` when the hart is in reset
255
| 18    | `anyhavereset`
256
| 17    | `allresumeack`    .2+| `1` when the hart has acknowledged a resume request
257
| 16    | `anyresumeack`
258
| 15    | `allnonexistent`  .2+| always zero to indicate the hart is always existent
259
| 14    | `anynonexistent`
260
| 13    | `allunavail`      .2+| `1` when the DM is disabled to indicate the hart is unavailable
261
| 12    | `anyunavail`
262
| 11    | `allrunning`      .2+| `1` when the hart is running
263
| 10    | `anyrunning`
264
|  9    | `allhalted`       .2+| `1` when the hart is halted
265
|  8    | `anyhalted`
266
|  7    | `authenticated`   | always `1`; there is no authentication
267
|  6    | `authbusy`        | always `0`; there is no authentication
268
|  5    | `hasresethaltreq` | always `0`; halt-on-reset is not supported (directly)
269
|  4    | `confstrptrvalid` | always `0`; no configuration string available
270
| 3:0   | `version`         | `0010` - DM is compatible to version 0.13
271
|=======================
272
 
273
 
274
:sectnums!:
275
===== **`hartinfo`**
276
 
277
[cols="4,27,>7"]
278
[frame="topbot",grid="none"]
279
|======
280
| 0x12 | **Hart information** | `hartinfo`
281
3+| Reset value: see below
282
3+| This register gives information about the hart. The entire register is read-only.
283
|======
284
 
285
.`hartinfo` - hart information register bits
286
[cols="^1,^2,<8"]
287
[options="header",grid="rows"]
288
|=======================
289
| Bit   | Name [RISC-V] | Description
290
| 31:24 | _reserved_    | reserved; always zero
291
| 23:20 | `nscratch`    | `0001`, number of `dscratch*` CPU registers = 1
292
| 19:17 | _reserved_    | reserved; always zero
293
| 16    | `dataccess`   | `0`, the `data` registers are shadowed in the hart's address space
294
| 15:12 | `datasize`    | `0001`, number of 32-bit words in the address space dedicated to shadowing the `data` registers = 1
295
| 11:0  | `dataaddr`    | = `dm_data_base_c(11:0)`, signed base address of `data` words (see address map in <<_dm_cpu_access>>)
296
|=======================
297
 
298
 
299
:sectnums!:
300
===== **`abstracts`**
301
 
302
[cols="4,27,>7"]
303
[frame="topbot",grid="none"]
304
|======
305
| 0x16 | **Abstract control and status** | `abstracts`
306
3+| Reset value: see below
307
3+| Command execution info and status.
308
|======
309
 
310
.`abstracts` - abstract control and status register bits
311
[cols="^1,^2,^1,<8"]
312
[options="header",grid="rows"]
313
|=======================
314
| Bit   | Name [RISC-V] | R/W | Description
315
| 31:29 | _reserved_    | r/- | reserved; always zero
316
| 28:24 | `progbufsize` | r/- | `0010`; size of the program buffer (`progbuf`) = 2 entries
317
| 23:11 | _reserved_    | r/- | reserved; always zero
318
| 12    | `busy`        | r/- | `1` when a command is being executed
319
| 11    | _reserved_    | r/- | reserved; always zero
320
| 10:8  | `cmerr`       | r/w | error during command execution (see below); has to be cleared by writing `111`
321
| 7:4   | _reserved_    | r/- | reserved; always zero
322
| 3:0   | `datacount`   | r/- | `0001`; number of implemented `data` registers for abstract commands = 1
323
|=======================
324
 
325
Error codes in `cmderr` (highest priority first):
326
 
327
* `000` - no error
328
* `100` - command cannot be executed since hart is not in expected state
329
* `011` - exception during command execution
330
* `010` - unsupported command
331
* `001` - invalid DM register read/write while command is/was executing
332
 
333
 
334
:sectnums!:
335
===== **`command`**
336
 
337
[cols="4,27,>7"]
338
[frame="topbot",grid="none"]
339
|======
340
| 0x17 | **Abstract command** | `command`
341
3+| Reset value: 0x00000000
342
3+| Writing this register will trigger the execution of an abstract command. New command can only be executed if
343
`cmderr` is zero. The entire register in write-only (reads will return zero).
344
|======
345
 
346
[NOTE]
347
The NEORV32 DM only supports **Access Register** abstract commands. These commands can only access the
348
hart's GPRs (abstract command register index `0x1000` - `0x101f`).
349
 
350
.`command` - abstract command register - "access register" commands only
351
[cols="^1,^2,<8"]
352
[options="header",grid="rows"]
353
|=======================
354
| Bit   | Name [RISC-V]      | Description / required value
355
| 31:24 | `cmdtype`          | `00000000` to indicate "access register" command
356
| 23    | _reserved_         | reserved, has to be `0` when writing
357
| 22:20 | `aarsize`          | `010` to indicate 32-bit accesses
358
| 21    | `aarpostincrement` | `0`, postincrement is not supported
359
| 18    | `postexec`         | if set the program buffer is executed _after_ the command
360
| 17    | `transfer`         | if set the operation in `write` is conducted
361
| 16    | `write`            | `1`: copy `data0` to `[regno]`; `0` copy `[regno]` to `data0`
362
| 15:0  | `regno`            | GPR-access only; has to be `0x1000` - `0x101f`
363
|=======================
364
 
365
 
366
:sectnums!:
367
===== **`abstractauto`**
368
 
369
[cols="4,27,>7"]
370
[frame="topbot",grid="none"]
371
|======
372
| 0x18 | **Abstract command auto-execution** | `abstractauto`
373
3+| Reset value: 0x00000000s
374
3+| Register to configure when a read/write access to a DM repeats execution of the last abstract command.
375
|======
376
 
377
.`abstractauto` - Abstract command auto-execution register bits
378
[cols="^1,^2,^1,<8"]
379
[options="header",grid="rows"]
380
|=======================
381
| Bit   | Name [RISC-V]        | R/W | Description
382
| 17    | `autoexecprogbuf[1]` | r/w | when set reading/writing from/to `progbuf1` will execute `command again`
383
| 16    | `autoexecprogbuf[0]` | r/w | when set reading/writing from/to `progbuf0` will execute `command again`
384
|  0    | `autoexecdata[0]`    | r/w | when set reading/writing from/to `data0` will execute `command again`
385
|=======================
386
 
387
 
388
:sectnums!:
389
===== **`progbuf`**
390
 
391
[cols="4,27,>7"]
392
[frame="topbot",grid="none"]
393
|======
394
| 0x20 | **Program buffer 0** | `progbuf0`
395
| 0x21 | **Program buffer 1** | `progbuf1`
396
3+| Reset value: `NOP`-instruction
397
3+| General purpose program buffer for the DM.
398
|======
399
 
400
 
401
:sectnums!:
402
===== **`haltsum0`**
403
 
404
[cols="4,27,>7"]
405
[frame="topbot",grid="none"]
406
|======
407
| 0x40 | **Halt summary 0** | `haltsum0`
408
3+| Reset value: _UNDEFINED_
409
3+| Bit 0 of this register is set if the hart is halted (all remaining bits are always zero). The entire register is read-only.
410
|======
411
 
412
:sectnums:
413
==== DM CPU Access
414
 
415
From the CPU's point of view, the DM behaves as a memory-mapped peripheral that includes
416
 
417
* a small ROM that contains the code for the "park loop", which is executed when the CPU is _in_ debug mode.
418
* a program buffer populated by the debugger host to execute small programs
419
* a data buffer to transfer data between the processor and the debugger host
420
* a status register to communicate debugging requests
421
 
422
.Park Loop Code Sources
423
[NOTE]
424
The assembly sources of the **park loop code** are available in `sw/ocd-firmware/park_loop.S`. Please note, that these
425
sources are not intended to be changed by the used. Hence, the makefile does not provide an automatic option
426
to compile and "install" the debugger ROM code into the HDL sources and require a manual copy
427
(see `sw/ocd-firmware/README.md`).
428
 
429
The DM uses a total address space of 128 words of the CPU's address space (= 512 bytes) divided into four sections
430
of 32 words (= 128 bytes) each.
431
Please note, that the program buffer, the data buffer and the status register only uses a few effective words in this
432
address space. However, these effective addresses are mirrored to fill up the whole 128 bytes of the section.
433
Hence, any CPU access within this address space will succeed.
434
 
435
.DM CPU access - address map (divided into four sections)
436
[cols="^2,^4,^2,<7"]
437
[options="header",grid="rows"]
438
|=======================
439
| Base address | Name [VHDL package]              | Actual size | Description
440
| `0xfffff800` | `dm_code_base_c` (= `dm_base_c`) |   128 bytes | Code ROM for the "park loop" code
441
| `0xfffff880` | `dm_pbuf_base_c`                 |    16 bytes | Program buffer, provided by DM
442
| `0xfffff900` | `dm_data_base_c`                 |     4 bytes | Data buffer (`dm.data0`)
443
| `0xfffff980` | `dm_sreg_base_c`                 |     4 bytes | Control and status register
444
|=======================
445
 
446
[NOTE]
447
From the CPU's point of view, the DM is mapped to an _"unused"_ address range within the processor's
448
<<_address_space>> right between the bootloader ROM (BOOTROM) and the actual processor-internal IO
449
space at addresses `0xfffff800` - `0xfffff9ff`
450
 
451
When the CPU enters or re-enters (for example via `ebreak` in the DM's program buffer) debug mode, it jumps to
452
the beginning of the DM's "park loop" code ROM at `dm_code_base_c`. This is the _normal entry point_ for the
453
park loop code. If an exception is encountered during debug mode, the CPU jumps to `dm_code_base_c + 4`,
454
which is the _exception entry point_.
455
 
456
**Status Register**
457
 
458
The status register provides a direct communication channel between the CPU executing the park loop and the
459
host-controlled controller of the DM. Note that all bits that can be written by the CPU (acknowledge flags)
460
cause a single-shot (1-cycle) signal to the DM controller and auto-clear (always read as zero).
461
The bits that are driven by the DM controller and are read-only to the CPU and keep their state until the CPU
462
acknowledges the according request.
463
 
464
.DM CPU access - status register
465
[cols="^2,^2,^2,<8"]
466
[options="header",grid="rows"]
467
|=======================
468
| Bit | Name            | CPU access | Description
469
| 0   | `halt_ack`      | -/w        | Set by the CPU to indicate that the CPU is halted and keeps iterating in the park loop
470
| 1   | `resume_req`    | r/-        | Set by the DM to tell the CPU to resume normal operation (leave parking loop and leave debug mode via `dret` instruction)
471
| 2   | `resume_ack`    | -/w        | Set by the CPU to acknowledge that the CPU is now going to leave parking loop & debug mode
472
| 3   | `execute_req`   | r/-        | Set by the DM to tell the CPU to leave debug mode and execute the instructions from the program buffer; CPU will re-enter parking loop afterwards
473
| 4   | `execute_ack`   | -/w        | Set by the CPU to acknowledge that the CPU is now going to execute the program buffer
474
| 5   | `exception_ack` | -/w        | Set by the CPU to inform the DM that an exception occurred during execution of the park loop or during execution of the program buffer
475
|=======================
476
 
477
 
478
 
479
<<<
480
// ####################################################################################################################
481
:sectnums:
482
=== CPU Debug Mode
483
 
484
The NEORV32 CPU Debug Mode `DB` (part of `rtl/core/neorv32_cpu_control.vhd`) is compatible to the "Minimal RISC-V Debug Specification 0.13.2".
485
It is enabled/implemented by setting the CPU generic _CPU_EXTENSION_RISCV_DEBUG_ to "true" (done by setting processor
486
generic _ON_CHIP_DEBUGGER_EN_).
487
It provides a new operation mode called "debug mode".
488
When enabled, three additional CSRs are available (section <<_cpu_debug_mode_csrs>>) and also the "return from debug mode"
489
instruction `dret` is available when the CPU is "in" debug mode.
490
 
491
[IMPORTANT]
492
The CPU _debug mode_ requires the `Zicsr` CPU extension to be implemented (top generic _CPU_EXTENSION_RISCV_Zicsr_ = true).
493
 
494
The CPU debug mode is entered when one of the following events appear:
495
 
496
[start=1]
497
. executing `ebreak` instruction (when `dcsr.ebreakm` is set and in machine mode OR when `dcsr.ebreaku` is set and in user mode)
498
. debug halt request from external DM (via CPU signal `db_halt_req_i`, high-active, triggering on rising-edge)
499
. finished executing of a single instruction while in single-step debugging mode (enabled via `dcsr.step`)
500
 
501
From a hardware point of view, these "entry conditions" are special synchronous (`ebreak` instruction) or asynchronous
502
(single-stepping "interrupt"; halt request "interrupt") traps, that are handled invisibly by the control logic.
503
 
504
Whenever the CPU **enters debug mode** it performs the following operations:
505
 
506
* move `pc` to `dpcs`
507
* copy the hart's current privilege level to `dcsr.prv`
508
* set `dcrs.cause` according to the cause why debug mode is entered
509
* **no update** of `mtval`, `mcause`, `mtval` and `mstatus` CSRs
510
* load the address configured via the CPU _CPU_DEBUG_ADDR_ generic to the `pc` to jump to "debugger park loop" code in the debug module (DM)
511
 
512
When the CPU **is in debug mode** the following things are important:
513
 
514
* while in debug mode, the CPU executes the parking loop and the program buffer provided by the DM if requested
515
* effective CPU privilege level is `machine` mode, PMP is not active
516
* if an exception occurs
517
  * if the exception was caused by any debug-mode entry action the CPU jumps to the _normal entry point_
518
    ( = _CPU_DEBUG_ADDR_) of the park loop again (for example when executing `ebreak` in debug mode)
519
  * for all other exception sources the CPU jumps to the _exception entry point_ ( = _CPU_DEBUG_ADDR_ + 4)
520
    to signal an exception to the DM and restarts the park loop again afterwards
521
* interrupts _including_ non-maskable interrupts are disabled; however, they will be buffered and executed when the CPU has left debug mode
522
* if the DM makes a resume request, the park loop exits and the CPU leaves debug mode (executing `dret`)
523
 
524
Debug mode is left either by executing the `dret` instruction footnote:[`dret` should only be executed _inside_ the debugger
525
"park loop" code (-> code ROM in the debug module (DM).)] (_in_ debug mode) or by performing
526
a hardware reset of the CPU. Executing `dret` outside of debug mode will raise an illegal instruction exception.
527
Whenever the CPU **leaves debug mode** the following things happen:
528
 
529
* set the hart's current privilege level according to `dcsr.prv`
530
* restore `pc` from `dpcs`
531
* resume normal operation at `pc`
532
 
533
 
534
:sectnums:
535
==== CPU Debug Mode CSRs
536
 
537
Two additional CSRs are required by the _Minimal RISC-V Debug Specification_: The debug mode control and status register
538
`dcsr` and the program counter `dpc`. Providing a general purpose scratch register for debug mode (`dscratch0`) allows
539
faster execution of program provided by the debugger, since _one_ general purpose register can be backup-ed and
540
directly used.
541
 
542
[NOTE]
543
The debug-mode control and status registers (CSRs) are only accessible when the CPU is _in_ debug mode.
544
If these CSRs are accessed outside of debug mode (for example when in `machine` mode) an illegal instruction exception
545
is raised.
546
 
547
 
548
:sectnums!:
549
===== **`dcsr`**
550
 
551
[cols="4,27,>7"]
552
[frame="topbot",grid="none"]
553
|======
554
| 0x7b0 | **Debug control and status register** | `dcsr`
555
3+| Reset value: 0x00000000
556
3+| The `dcsr` CSR is compatible to the RISC-V debug spec. It is used to configure debug mode and provides additional status information.
557
The following bits are implemented. The reaming bits are read-only and always read as zero.
558
|======
559
 
560
.Debug control and status register bits
561
[cols="^1,^2,^1,<8"]
562
[options="header",grid="rows"]
563
|=======================
564
| Bit   | Name [RISC-V] | R/W | Event
565
| 31:28 | `xdebugver` | r/- | always `0100` - indicates external debug support exists
566
| 27:16 | -           | r/- | _reserved_, read as zero
567
| 15    | `ebereakm`  | r/w | `ebreak` instructions in `machine` mode will _enter_ debug mode when set
568
| 14    | [line-through]#`ebereakh`# | r/- | `0` - hypervisor mode not supported
569
| 13    | [line-through]#`ebereaks`# | r/- | `0` - supervisor mode not supported
570
| 12    | `ebereaku`  | r/w | `ebreak` instructions in `user` mode will _enter_ debug mode when set
571
| 11    | [line-through]#`stepie`# | r/- | `0` - IRQs are disabled during single-stepping
572
| 10    | [line-through]#`stopcount`# | r/- | `0` - counters increment as usual
573
| 9     | [line-through]#`stoptime`#  | r/- | `0` - timers increment as usual
574
| 8:6   | `cause`     | r/- | cause identifier - why was debug mode entered
575
| 5     | -           | r/- | _reserved_, read as zero
576
| 4     | `mprven`    | r/- | `0` - `mstatus.mprv` is ignored when in debug mode
577
| 3     | `nmip`      | r/- | set when the non-maskable CPU/processor interrupt is pending
578
| 2     | `step`      | r/w | enable single-stepping when set
579
| 1:0   | `prv`       | r/w | CPU privilege level before/after debug mode
580
|=======================
581
 
582
 
583
:sectnums!:
584
===== **`dpc`**
585
 
586
[cols="4,27,>7"]
587
[frame="topbot",grid="none"]
588
|======
589
| 0x7b1 | **Debug program counter** | `dpc`
590
3+| Reset value: _UNDEFINED_
591
3+| The `dcsr` CSR is compatible to the RISC-V debug spec. It is used to store the current program counter when
592
debug mode is entered. The `dret` instruction will return to `dpc` by moving `dpc` to `pc`.
593
|======
594
 
595
 
596
:sectnums!:
597
===== **`dscratch0`**
598
 
599
[cols="4,27,>7"]
600
[frame="topbot",grid="none"]
601
|======
602
| 0x7b2 | **Debug scratch register 0** | `dscratch0`
603
3+| Reset value: _UNDEFINED_
604
3+| The `dscratch0` CSR is compatible to the RISC-V debug spec. It provides a general purpose debug mode-only scratch register.
605
|======
606
 
607
 

powered by: WebSVN 2.1.0

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