RISC-v microcontroller trajectory compression tracking device and method
By employing a two-stage hardware cascade structure of sequential PC suppression and repeated back-edge compression units in the RISC-V microcontroller, the problems of excessive hardware weight and repeated output of short cycle back edges in RISC-V microcontroller trajectory tracking are solved, achieving low-cost and efficient trajectory observation and control flow inflection point preservation.
Patent Information
- Application Number
- CN202611674953.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-10-28
- Publication Date
- 2026-11-27
AI Technical Summary
In existing RISC-V microcontroller trajectory tracing solutions, complete trace hardware is too heavy and the low-speed debug port is easily refreshed by sequential PC and short loop back edges. On the other hand, the compression solution based on branch prediction relies on predictors, small cores are difficult to implement, and loop exit may be missed.
It adopts a two-stage hardware cascade structure of sequential PC suppression unit and repeated back edge compression unit. It silences sequential PC changes by determining whether curr_PC is prev_PC+2 or +4, and performs matching compression on discontinuous events. The output comes only from control flow discontinuities such as jumps and branches, avoiding reliance on branch predictors.
It significantly reduces the trajectory tracking output event rate, lowers chip cost and interface constraints, retains key control flow inflection points, facilitates low-cost observation of program trajectories, solves the problem of repeated output of short loop back edges, and ensures that the first appearance, change and exit of back edges are all visible.
Smart Images

Figure CN123178152A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of program tracing technology, and in particular relates to a RISC-V microcontroller trajectory compression and tracing device and method. Background Technology
[0002] With the widespread application of RISC-V microcontrollers (MCUs) in the embedded field, the demand for tracing and debugging program execution paths is increasing. Program tracing (Trace) can be used to observe whether the program branches or jumps as expected, and whether abnormal situations such as program crashes occur. It is an important means of chip debugging.
[0003] However, implementing program trace in cost-sensitive RISC-V MCUs faces numerous challenges. A complete instruction trace macrocell is large and typically requires a dedicated high-speed debug interface; if every instruction or program counter (PC) change is output through a low-speed debug port, bandwidth will be quickly exhausted, failing to meet real-time debugging requirements. Several high-level trace encoding schemes exist, such as trace encoding and loop compression buffers used in some processors, achieving high compression ratios, but their implementation is complex and unsuitable for the simple trace modules in cost-sensitive MCUs. In existing technologies, schemes that only record jumps and not sequential execution have similar publicly available methods in program counter compression or breakpoint tracing; "loop / repeating branch compression" has also been discussed in some trace buffers and Nexus-like optimizations.
[0004] Existing technologies have the following drawbacks: First, complete tracing is too cumbersome, requiring large area and interfaces; second, when only sequential suppression is performed, short loop back edges are still output every time, and low-speed debug ports are still repeatedly refreshed; third, compressed dependency predictors based on branch prediction success / failure are often not available in small MCUs or the signals are difficult to extract, and exits may be missed when correctly guessed. Patent CN100570577C provides a high-speed program tracing scheme that only outputs trajectory information when discontinuous, and uses a linearly increasing run length value to represent the number of instructions executed sequentially between two discontinuous points. However, it only records sequential execution without recording, and only records jumps, similar to existing technologies, and suffers from the same drawbacks.
[0005] Therefore, how to provide a simple RISC-V compressed trace module that does not rely on a branch predictor, and significantly reduce the output event rate by hardware-level compression filtering while retaining key control flow inflection points, so as to facilitate low-cost debugging and observation of the trajectory, is a problem that urgently needs to be solved by those in this technical field. Summary of the Invention
[0006] To address the shortcomings of existing technologies, the present invention aims to provide a RISC-V microcontroller compression tracking device to solve the problems in existing RISC-V microcontroller trajectory tracking schemes, such as the heavy hardware of the complete trace and the low-speed debugging port being easily refreshed by sequential PC and short loop back edges, while the compression scheme based on branch prediction relies on the predictor, the small core is difficult to implement, and there is a possibility of missing loop exits; in addition, the present invention also provides a RISC-V microcontroller compression tracking method.
[0007] To solve the above-mentioned technical problems, the present invention adopts the following technical solution:
[0008] In a first aspect, the present invention provides a RISC-V microcontroller compression tracking device, comprising: a device disposed next to a RISC-V CPU core, comprising:
[0009] A configuration register is used to store the enable / reset of the device, and an optional back-edge compression enable;
[0010] The PC bypass interface is used to collect the current valid program counter curr_PC and valid indication of the RISC-V CPU core, and to maintain the previous valid program counter prev_PC.
[0011] The sequential PC suppression unit is used to determine whether curr_PC is equal to prev_PC+2 or prev_PC+4 when curr_PC is valid. If so, it is determined to be sequential execution without generating an output event, and only prev_PC is updated to curr_PC; otherwise, it is determined to be a discontinuous control flow, generating a discontinuous event and carrying the discontinuous event of PC S=prev_PC before the transfer and PC T=curr_PC to the target.
[0012] The repeating back-edge compression unit is used to match and compress the discontinuous events according to the transition pairs (S,T), and output the compressed trajectory events;
[0013] The event output path unit is used to output the compressed trajectory event to an off-chip tool.
[0014] Furthermore, the repeated back-edge compression unit maintains the saved transfer pair (S_saved, T_saved), the count COUNT, and the compression activation state, and executes:
[0015] Rule A: If compression is active and (S,T) equals (S_saved, T_saved), then COUNT is incremented by 1 and PC is not output.
[0016] Rule B: If compression is not activated, or (S,T) is different from (S_saved, T_saved), output the target PC T, save (S,T), set COUNT to 1 and activate the compression state;
[0017] Rule C: During compression activation, if the back edge is detected to no longer be valid, the loop exits / back edge ends, the exit point PC and / or the count termination event are output, and the compression state is cleared.
[0018] Furthermore, in rule B, if the original compression state COUNT is greater than 0, the back edge termination event or count value is output first, and then the new target PC T is output.
[0019] Furthermore, in rule C, the detection method for when the back edge no longer holds is: at PC S before the transfer, curr_PC falls into the branch instruction address in a +2 or +4 order, instead of jumping to the target PC T.
[0020] Furthermore, the sequential PC suppression unit and the repeated back edge compression unit are a two-stage series structure, and the processing order is fixed as follows: the sequential PC is first filtered by the sequential PC suppression unit, and only discontinuous events are sent to the repeated back edge compression unit.
[0021] Furthermore, the trajectory event output by the event output path unit includes the target PC and an optional back edge count value, and the event output path unit is a serial debug port or debug port.
[0022] Secondly, the present invention also provides a RISC-V microcontroller trajectory compression and tracking method, comprising the following steps:
[0023] S10. Collect the current valid program counter curr_PC of the RISC-V CPU core and maintain the previous valid program counter prev_PC;
[0024] S20, Sequential PC Suppression: If curr_PC equals prev_PC+2 or prev_PC+4, it is determined to be sequential execution, no output event is generated, and only prev_PC is updated to curr_PC; otherwise, it is determined to be a discontinuous control flow, and a discontinuous event carrying the pre-transfer PC S=prev_PC and the target PC T=curr_PC is generated.
[0025] S30, Repeated Back Edge Compression: The discontinuous events are matched and compressed according to the transition pair (S,T). If compression is activated and (S,T) is equal to the saved transition pair (S_saved, T_saved), the count COUNT is incremented by 1 and no PC is output. If compression is not activated or (S,T) are different, the target PC T is output, (S,T) is saved, COUNT is set to 1 and compression is activated. If a back edge is detected to no longer be valid during compression activation, the exit point PC and / or the closing event is counted and the compression state is cleared.
[0026] S40. Output the compressed trajectory event to the off-chip tool via the event output path unit.
[0027] Furthermore, in step S20, the method for determining whether curr_PC is equal to prev_PC+2 or prev_PC+4 is as follows: first calculate d2=prev_PC+2 and d4=prev_PC+4, and then compare whether curr_PC is equal to d2 or d4.
[0028] Furthermore, it also includes off-screen reconstruction steps:
[0029] R1. Collect the target PC and optional COUNT or exit flag in the output events in the order of reception;
[0030] R2. Map the PC to the function symbol to obtain the control flow inflection point trajectory;
[0031] R3. If COUNT is included, the same back edge is expanded to the logical iteration count by repeating COUNT times.
[0032] Furthermore, silent sequential instructions and repeated back edges are not restored line by line; the actual events reported by the hardware are taken as the authoritative data.
[0033] Compared with the prior art, the RISC-V microcontroller trajectory compression and tracking device and method provided by the present invention have at least the following advantages:
[0034] In existing RISC-V microcontroller trajectory tracking solutions, complete trace hardware is too heavy, and the low-speed debug port is easily overwhelmed by sequential PCs and short loop back edges. Branch prediction-based compression schemes rely on predictors, are difficult to implement with small cores, and may miss loop exits. This invention achieves trajectory compression using simplified comparison and register logic, requiring less area and allowing direct connection to low-speed interfaces such as serial debug ports, significantly reducing chip cost and interface constraints. This invention uses a sequential PC suppression unit to determine whether curr_PC is prev_PC+2 or +4, silencing a large number of sequential PC changes in the straight-line code segment. Output only comes from control flow discontinuities such as jumps, branches, indirect jumps, and interrupt / exception entry points, compressing the number of trajectory events at the source, effectively suppressing sequential PC output, and significantly reducing the event rate. This invention adds a repeated back edge compression unit to the sequential suppression, accumulating counts for repeated iterations of the same transition pair (S,T) without repeatedly outputting PCs, essentially decoupling short loop back edge output events from the number of loop iterations. Compared to solutions that only perform +2 / +4 sequential suppression, this invention further solves the problem of low-speed port congestion caused by repeated output of the same target PC by hot back edges such as short for loops. This invention does not rely on a predictor and can be implemented on small cores without a predictor. Through deterministic rules of "first-time occurrence, repeated silence, and exit must occur," it ensures that the first appearance of a back edge, back edge changes, and loop exits are all visible, avoiding missed jump information due to correct predictions. In this invention, sequential PC suppression and repeated back edge compression are cascaded, suppressing both straight code segments and hot back edges. While preserving key control flow inflection points, it significantly reduces the output event rate. Off-chip tools can reconstruct the control flow inflection point trajectory and logic iteration count based on the event sequence and optional COUNT, facilitating low-cost observation of whether the program branches / jumps as expected or runs out of control. Attached Figure Description
[0035] To more clearly illustrate the solution of the present invention, a brief introduction will be given to the drawings used in the description of the embodiments below. Obviously, the drawings described below are some embodiments of the present invention. For those skilled in the art, other drawings can be obtained from these drawings without creative effort.
[0036] Figure 1 An architecture diagram of a RISC-V microcontroller trajectory compression and tracking device provided in an embodiment of the present invention;
[0037] Figure 2 A flowchart of sequential PC suppression (+2 / +4) determination for a RISC-V microcontroller trajectory compression tracking device provided in an embodiment of the present invention;
[0038] Figure 3A flowchart of the repetitive back-edge compression determination process of a RISC-V microcontroller trajectory compression tracking device provided in an embodiment of the present invention;
[0039] Figure 4 A short-cycle example of a RISC-V microcontroller trajectory compression and tracking device provided for an embodiment of the present invention (comparison of output before and after two-stage compression);
[0040] Figure 5 A flowchart of a RISC-V microcontroller trajectory compression and tracking method provided in an embodiment of the present invention. Detailed Implementation
[0041] To facilitate understanding of the present invention, a more complete description will be given below with reference to the accompanying drawings. Preferred embodiments of the invention are shown in the drawings. However, the invention can be implemented in many different forms and is not limited to the embodiments described herein. Rather, these embodiments are provided to provide a thorough and complete understanding of the disclosure of the invention.
[0042] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this invention pertains. The terminology used herein in the description of the invention is for the purpose of describing particular embodiments only and is not intended to be limiting of the invention.
[0043] This invention provides a RISC-V microcontroller trajectory compression and tracking device, applied to lightweight trajectory tracking hardware next to the RISC-V CPU (RV32) core. The RISC-V microcontroller trajectory compression and tracking device is located next to the RISC-V CPU core and includes: a configuration register for enabling / resetting the storage device and optionally enabling back-edge compression; a PC bypass interface for acquiring the current valid program counter curr_PC and its validity indication, and maintaining the previous valid program counter prev_PC; and a sequential PC suppression unit for determining whether curr_PC equals prev_PC+2 or prev_PC+4 when curr_PC is valid. If so, sequential execution is determined without generating an output event, and only prev_PC is updated to curr_PC; otherwise, control flow is determined to be discontinuous, a discontinuity event is generated, and the event carries the pre-transfer PC S=prev_PC and the target PC. T=curr_PC discontinuous events; repeated back-edge compression unit, used to match and compress discontinuous events according to transition pairs (S,T), and output the compressed trajectory events; event output path unit, used to output the compressed trajectory events to external tools.
[0044] This invention does not rely on branch predictors or BTB. Through two-stage hardware cascade compression, it significantly reduces the low-speed debug port output event rate while retaining key control flow inflection points, making it easy for low-cost MCUs to observe program trajectories and program crashes.
[0045] To enable those skilled in the art to better understand the present invention, the technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings.
[0046] This invention provides a RISC-V microcontroller trajectory compression and tracking device, applied to lightweight trajectory tracking hardware alongside a RISC-V CPU (RV32) core, such as... Figure 1 As shown, in this embodiment, the RISC-V microcontroller trajectory compression and tracking device is located next to the RISC-V CPU core, and collects curr_PC and valid indications via bypass, including:
[0047] The system includes: a configuration register for enabling / resetting the storage device and enabling optional back-edge compression; a PC bypass interface for acquiring the current valid program counter curr_PC and its validity indicator from the RISC-V CPU core, and maintaining the previous valid program counter prev_PC; a sequential PC suppression unit for determining whether curr_PC equals prev_PC+2 or prev_PC+4 when curr_PC is valid; if so, sequential execution is determined without generating an output event, and only prev_PC is updated to curr_PC; otherwise, control flow is determined to be discontinuous, generating a discontinuous event and carrying the discontinuous event of PC S=prev_PC before the transfer and PC T=curr_PC before the transfer; a repeated back-edge compression unit for matching and compressing discontinuous events according to the transfer pair (S,T), and outputting the compressed trajectory event; and an event output path unit for outputting the compressed trajectory event to an external tool.
[0048] Specifically, in this embodiment, the hardware stores the previous valid PC as prev_PC. Whenever curr_PC is valid, if curr_PC∈{prev_PC+2, prev_PC+4}, it is determined that the next instruction will be executed sequentially (corresponding to the address stepping of compressed 16-bit instructions and 32-bit instructions respectively), and no output event is generated. Only prev_PC is updated to curr_PC; otherwise, it is determined that the control flow is discontinuous, a discontinuity event is generated, and S=prev_PC (PC before the transfer) and T=curr_PC (target PC) are sent to the repeated back-edge compression unit.
[0049] Effect: A large number of PC changes in linear code segments are consumed, and the output can only come from discontinuous points such as jumps / branches / indirect jumps / interrupt exception entries.
[0050] In this embodiment, as Figure 2 As shown, first calculate d2 = prev_PC + 2 and d4 = prev_PC + 4, then compare whether curr_PC is equal to d2 or d4. If it is, remain silent; otherwise, enter the second level with (S,T) instead of immediately and unconditionally sending it out, in order to achieve seamless contraction and back-edge connection. Here, +2 corresponds to a 16-bit compressed instruction step, +4 corresponds to a 32-bit instruction step, and other PC changes are considered discontinuous.
[0051] Furthermore, in this embodiment, when only the sequential PC suppression unit is present, each conditional transfer (taken) in the short for loop returns to the beginning of the loop, which is discontinuous and will repeatedly output the same target PC, causing congestion at the low-speed debugging port. The repeated back-edge compression unit maintains the saved transfer pair (S_saved, T_saved), the count (COUNT), and the compression activation state, and executes:
[0052] Rule A (Repeated Silence): If compression is active and (S,T)==(S_saved,T_saved), then COUNT is incremented by 1 and PC is not output.
[0053] Rule B (First See Must Send): If compression is not activated, or (S,T) is different from the saved pair, then: if the original compressed state COUNT>0, first output the end of the back edge / count value; then output the new target T (or output the new pair); save (S,T), COUNT=1, and activate the compressed state.
[0054] Rule C (Exit Must Be Issued): During compression activation, if the back edge is detected to no longer be valid, the loop exit / back edge end is determined, the exit point PC is output and / or the count termination event is output, and the compression state is cleared.
[0055] The detection method for when the back edge is no longer valid is to change the step PC S to sequential fall (curr_PC satisfies +2 / +4 relative to the branch instruction address, instead of jumping to T). In this way, even if the exit is manifested as "sequential PC", it will not be swallowed by the sequential PC suppression unit and lose the "jump out" information.
[0056] The correspondence with "correct prediction silence, prediction failure / miss / first-time occurrence": This embodiment uses a determined (S,T) repeated matching to achieve the same engineering goal—repeated and unchanged back edges are considered as silencing; first-time occurrence, back edge change, and exit are considered as occurrences—but the implementation does not depend on prediction success / failure signals, making it more suitable for small cores without BTB. This embodiment deliberately enables BTB / prediction success silence on the other side for the following reasons:
[0057] Many MCU4s lack BTB or cannot generate mispredict; if the exit is correctly guessed, it will be missed; repeated back-edge compression only compares whether (S,T) is repeated, does not rely on the predictor, the hardware is simpler, and the behavior is deterministic;
[0058] Equivalent effect: If a short for loop repeatedly takes the same edge, it will only be executed once on the first occurrence, and the iteration will be silent; if the target is actually changed or the loop exits through fall-through, it will definitely be executed.
[0059] In this embodiment, as Figure 3 As shown, the second level only handles discontinuous events, remaining silent during the same (S,T) iteration; outputting the first occurrence or change; and a separate exit rule is listed to ensure visibility after exiting the loop.
[0060] Furthermore, in this embodiment, as Figure 4 As shown, the sequential PC suppression unit and the repeated back-edge compression unit are a two-stage cascaded structure. The processing order is fixed: first, the sequential PC suppression unit filters sequential PCs, and only discontinuous events are sent to the repeated back-edge compression unit. Sequential suppression is performed first, followed by repeated back-edge compression for discontinuous events. Both are indispensable to cover the short-cycle refresh problem. If the sequential PC suppression unit is missing, there will be too many sequential PCs. If the repeated back-edge compression unit is missing, there will be short-cycle back-edge refresh. For example, if the cycle back-edge bnez retrieves L a total of N times, and then exits sequentially, the sequential PC suppression unit will output T approximately N times. After two stages, it is usually "T is output once on the first occurrence + once on the exit" (COUNT can be added), which is basically decoupled from N.
[0061] Furthermore, in this embodiment, the control flow inflection point sequence process for off-chip reconstruction (host computer reconstruction) is as follows:
[0062] R1: Collect the target PC (and optional COUNT / exit flag) in the output events in the order they are received.
[0063] R2: Map PC to function symbols to obtain the inflection point trajectory.
[0064] R3: If COUNT is included, "repeated COUNT times on the same back edge" can be expanded into a logical iteration count, without having to repeatedly transmit the PC on the link.
[0065] R4: Authoritative data is based on the actual events reported by the hardware; silent sequential instructions and repeated back edges are not restored one by one.
[0066] Furthermore, in this embodiment, the key RTL semantics are represented by the following pseudocode:
[0067] on_valid(curr):
[0068] if curr==prev+2 or curr==prev+4: handle_seq_maybe_loop_exit(); prev=curr; return;
[0069] S=prev;
[0070] T = curr;
[0071] if active and S==Ss and T==Ts:
[0072] COUNT++;
[0073] else:
[0074] flush_if_needed();
[0075] emit(T);
[0076] Ss,Ts=S,T;
[0077] COUNT=1;
[0078] active=1;
[0079] prev=curr;
[0080] The flush_if_needed() function is used to output the end / count information of the original back edge before the compression state switch; emit(T) outputs the new target PC. Those skilled in the art can implement this unit using conventional sequential logic and comparators based on the above semantics.
[0081] Furthermore, in this embodiment, the trajectory event output by the event output path unit includes the target PC and an optional back-edge count value, and the event output path unit is a serial debug port or debug port.
[0082] This invention also provides a RISC-V microcontroller trajectory compression and tracking method, combined with... Figure 1 and Figure 5 In this embodiment, the RISC-V microcontroller trajectory compression and tracking method includes the following steps:
[0083] S10. Collect the current valid program counter curr_PC of the RISC-V CPU core and maintain the previous valid program counter prev_PC;
[0084] S20, Sequential PC Suppression: If curr_PC equals prev_PC+2 or prev_PC+4, it is determined to be sequential execution, no output event is generated, and only prev_PC is updated to curr_PC; otherwise, it is determined to be a discontinuous control flow, and a discontinuous event carrying the pre-transfer PC S=prev_PC and the target PC T=curr_PC is generated.
[0085] Specifically, in this embodiment, the method for determining whether curr_PC is equal to prev_PC+2 or prev_PC+4 is as follows: first calculate d2=prev_PC+2 and d4=prev_PC+4, and then compare whether curr_PC is equal to d2 or d4.
[0086] S30, Repeated Back Edge Compression: For discontinuous events, perform matching compression based on the transition pair (S,T). If compression is active and (S,T) equals the saved transition pair (S_saved, T_saved), then the count COUNT is incremented by 1 and no PC is output. If compression is not active or (S,T) are different, then the target PC T is output, (S,T) is saved, COUNT is set to 1, and compression is activated. If a back edge is detected to no longer be valid during compression activation, then the exit point PC and / or the closing event is counted and the compression state is cleared.
[0087] Specifically, in this embodiment, if the original compression state COUNT is greater than 0, the back edge termination event or count value is output first, and then the new target PC T is output.
[0088] Furthermore, in this embodiment, the detection method for when the back edge no longer holds is as follows: at PC S before the transfer, curr_PC falls into the branch instruction address in a +2 or +4 order, instead of jumping to the target PC T.
[0089] Furthermore, in this embodiment, repeated iterations of the same transition pair (S,T) are silenced and accumulated COUNT. The first appearance of a transition pair, changes in the transition pair, and exiting the back edge all generate output events.
[0090] S40. Output the compressed trajectory event to the off-chip tool via the event output path unit.
[0091] Furthermore, this embodiment also includes an off-chip reconstruction step:
[0092] R1: Collect the target PC (and optional COUNT / exit flag) in the output events in the order they are received.
[0093] R2: Map PC to function symbols to obtain the inflection point trajectory.
[0094] R3: If COUNT is included, "repeated COUNT times on the same back edge" can be expanded into a logical iteration count, without having to repeatedly transmit the PC on the link.
[0095] R4: Authoritative data is based on the actual events reported by the hardware; silent sequential instructions and repeated back edges are not restored one by one.
[0096] Furthermore, in this embodiment, the silent sequential instructions and repeated back edges are not restored one by one, and the actual events reported by the hardware are taken as the authoritative data.
[0097] Compared with existing technologies, the RISC-V microcontroller trajectory compression and tracking device and method described in the above embodiments have advantages. Existing RISC-V microcontroller trajectory tracking schemes suffer from heavy hardware for complete traces and the low-speed debug port is easily refreshed by sequential PCs and short loop back edges. Branch prediction-based compression schemes rely on predictors, are difficult to implement with small cores, and may miss loop exits. This invention achieves trajectory compression with simplified comparison and register logic, requiring less area and allowing direct connection to low-speed interfaces such as serial debug ports, significantly reducing chip cost and interface constraints. This invention uses a sequential PC suppression unit to determine whether curr_PC is prev_PC+2 or +4, silencing a large number of sequential PC changes in the straight-line code segment. Output only comes from control flow discontinuities such as jumps, branches, indirect jumps, and interrupt / exception entry points, compressing the number of trajectory events from the source, effectively suppressing sequential PC output, and significantly reducing the event rate. This invention adds a repeated back edge compression unit on top of sequential suppression, accumulating counts for repeated iterations of the same transition pair (S,T) without repeatedly outputting PCs, essentially decoupling short loop back edge output events from the number of loop iterations. Compared to solutions that only perform +2 / +4 sequential suppression, this invention further solves the problem of low-speed port congestion caused by repeated output of the same target PC by hot back edges such as short for loops. This invention does not rely on a predictor and can be implemented on small cores without a predictor. Through deterministic rules of "first-time occurrence, repeated silence, and exit must occur," it ensures that the first appearance of a back edge, back edge changes, and loop exits are all visible, avoiding missed jump information due to correct predictions. In this invention, sequential PC suppression and repeated back edge compression are cascaded, suppressing both straight code segments and hot back edges. While preserving key control flow inflection points, it significantly reduces the output event rate. Off-chip tools can reconstruct the control flow inflection point trajectory and logic iteration count based on the event sequence and optional COUNT, facilitating low-cost observation of whether the program branches / jumps as expected or runs out of control.
[0098] Obviously, the embodiments described above are merely preferred embodiments of the present invention, and not all embodiments. The accompanying drawings illustrate preferred embodiments of the present invention, but do not limit the scope of the patent. The present invention can be implemented in many different forms; rather, these embodiments are provided to provide a more thorough and complete understanding of the disclosure of the present invention. Although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art can still modify the technical solutions described in the foregoing specific embodiments, or make equivalent substitutions for some of the technical features. Any equivalent structures made using the content of this specification and drawings, directly or indirectly applied to other related technical fields, are similarly within the scope of patent protection of this invention.
Claims
1. A RISC-V microcontroller trajectory compression and tracking device, characterized in that, Located next to the RISC-V CPU core, including: A configuration register is used to store the enable / reset of the device, and an optional back-edge compression enable; The PC bypass interface is used to collect the current valid program counter curr_PC and valid indication of the RISC-V CPU core, and to maintain the previous valid program counter prev_PC. The sequential PC suppression unit is used to determine whether curr_PC is equal to prev_PC+2 or prev_PC+4 when curr_PC is valid. If so, it is determined to be sequential execution without generating an output event, and only prev_PC is updated to curr_PC; otherwise, it is determined to be a discontinuous control flow, generating a discontinuous event and carrying the discontinuous event of PC S=prev_PC before the transfer and PC T=curr_PC to the target. The repeating back-edge compression unit is used to match and compress the discontinuous events according to the transition pairs (S,T), and output the compressed trajectory events; The event output path unit is used to output the compressed trajectory event to an off-chip tool.
2. The RISC-V microcontroller trajectory compression and tracking device according to claim 1, characterized in that, The repeated back-edge compression unit maintains the saved transfer pairs (S_saved, T_saved), the count COUNT, and the compression activation state, and executes: Rule A: If compression is active and (S,T) equals (S_saved, T_saved), then COUNT is incremented by 1 and PC is not output. Rule B: If compression is not activated, or (S,T) is different from (S_saved, T_saved), output the target PC T, save (S,T), set COUNT to 1 and activate the compression state; Rule C: During compression activation, if the back edge is detected to no longer be valid, the loop exits / back edge ends, the exit point PC and / or the count termination event are output, and the compression state is cleared.
3. The RISC-V microcontroller trajectory compression and tracking device according to claim 2, characterized in that, In rule B, if the original compression state COUNT is greater than 0, the back edge termination event or count value is output first, and then the new target PC T is output.
4. The RISC-V microcontroller trajectory compression and tracking device according to claim 2, characterized in that, In rule C, the detection method for when the back edge no longer holds is: at PC S before the transfer, curr_PC falls into the branch instruction address in a +2 or +4 order, instead of jumping to the target PC T.
5. The RISC-V microcontroller trajectory compression and tracking device according to claim 1, characterized in that, The sequential PC suppression unit and the repeated back edge compression unit are a two-stage series structure. The processing order is fixed so that the sequential PC is first filtered by the sequential PC suppression unit, and only discontinuous events are sent to the repeated back edge compression unit.
6. The RISC-V microcontroller trajectory compression and tracking device according to claim 1, characterized in that, The trajectory event output by the event output path unit includes the target PC and an optional back edge count value, and the event output path unit is a serial debug port or debug port.
7. A RISC-V microcontroller trajectory compression and tracking method applied to the device according to any one of claims 1 to 6, characterized in that, Includes the following steps: S10. Collect the current valid program counter curr_PC of the RISC-V CPU core and maintain the previous valid program counter prev_PC; S20, Sequential PC Suppression: If curr_PC equals prev_PC+2 or prev_PC+4, it is determined to be sequential execution, no output event is generated, and only prev_PC is updated to curr_PC; otherwise, it is determined to be a discontinuous control flow, and a discontinuous event carrying the pre-transfer PC S=prev_PC and the target PC T=curr_PC is generated. S30, Repeated Back Edge Compression: The discontinuous events are matched and compressed according to the transition pair (S,T). If compression is activated and (S,T) is equal to the saved transition pair (S_saved, T_saved), the count COUNT is incremented by 1 and no PC is output. If compression is not activated or (S,T) are different, the target PC T is output, (S,T) is saved, COUNT is set to 1 and compression is activated. If a back edge is detected to no longer be valid during compression activation, the exit point PC and / or the closing event is counted and the compression state is cleared. S40. Output the compressed trajectory event to the off-chip tool via the event output path unit.
8. The method according to claim 7, characterized in that, In step S20, the method for determining whether curr_PC is equal to prev_PC+2 or prev_PC+4 is as follows: first calculate d2=prev_PC+2 and d4=prev_PC+4, and then compare whether curr_PC is equal to d2 or d4.
9. The method according to claim 7, characterized in that, It also includes off-screen reconstruction steps: R1. Collect the target PC and optional COUNT or exit flag in the output events in the order of reception; R2. Map the PC to the function symbol to obtain the control flow inflection point trajectory; R3. If COUNT is included, the same back edge is expanded to the logical iteration count by repeating COUNT times.
10. The method according to claim 9, characterized in that, Silent sequential instructions and repeated back edges are not restored line by line; the data based on the actual events reported by the hardware is considered authoritative.
Citation Information
Patent Citations
High-speed program tracing
CN100570577C
