Skip to content

fix reversely PC wrap problems#2

Open
linkaishengmri wants to merge 1 commit intovnegnev:masterfrom
linkaishengmri:master
Open

fix reversely PC wrap problems#2
linkaishengmri wants to merge 1 commit intovnegnev:masterfrom
linkaishengmri:master

Conversation

@linkaishengmri
Copy link
Copy Markdown

Problem

When the PC is paused (e.g., due to a wait instruction), and the old_pc_hw value is 0, the next pc_hw value may wrap backward by 4 bytes (e.g., from 0x0 to 0x3fffc), due to hardware behavior. This inverse wrap was not correctly handled in the code, and pc_offset remained unchanged.

As a result, the program counter (pc) appeared to advance incorrectly by 0x40000, causing pc > mem_offset, and ultimately triggering a memory buffer underrun.

Fix

Added the following check in the PC tracking logic to detect and correct inverse wrap-around:

if (pc_hw > old_pc_hw + MARGA_MEM_SIZE - 16) {
    // PC has jumped backward due to a wrap from waiting/pausing 
    debug_printf("PC Inversely wrapped: Loop %u | Mem: 0x%zx | RxReads: %u | Exec: %u | PC_HW: 0x%zx | OLD_PC_HW: 0x%zx\n",
        execution_loops, mem_offset, rx_reads_per_loop, exec, pc_hw, old_pc_hw);
    pc_offset -= MARGA_MEM_SIZE;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant