Skip to content

ch32v: Fix: Add clobbers to bss/data loops#878

Merged
mattnite merged 1 commit intomainfrom
fix_ch32v_start
Feb 8, 2026
Merged

ch32v: Fix: Add clobbers to bss/data loops#878
mattnite merged 1 commit intomainfrom
fix_ch32v_start

Conversation

@Grazfather
Copy link
Collaborator

I noticed that ch32v examples were broken. git bisect got me to #868, but some debugging with @kibels suggested something else was going on.

Looks like those changes just exposed a bug where we weren't clobbering properly, so a subsequent CSR, which was using a register, had had it clobbered.

I haven't exactly tracked why this exposed the issue, since the csrwi instructions (only changed ones) are before the problematic function.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 Lint Results

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 Lint Results

@Grazfather
Copy link
Collaborator Author

Making this change causes the issue to be exhibited (for me on zig 0.15.2). The clobbers fix the issue.

diff --git i/port/wch/ch32v/src/cpus/main.zig w/port/wch/ch32v/src/cpus/main.zig
index a477503d..c1192fd2 100644
--- i/port/wch/ch32v/src/cpus/main.zig
+++ w/port/wch/ch32v/src/cpus/main.zig
@@ -236,7 +236,7 @@ pub const startup_logic = struct {
         );

         // NOTE: this can only be called once. Otherwise, we get a linker error for duplicate symbols
-        startup_logic.initialize_system_memories();
+        microzig.utilities.initialize_system_memories(.auto);

         // Configure the CPU.
         switch (cpu_name) {

Even though the issue is 'far away', for me this caused the jal _system_init to happen AFTER putting &microzig_main into a register for the subsequent write to mepc.

@mattnite mattnite merged commit 4db37e8 into main Feb 8, 2026
103 of 105 checks passed
@mattnite mattnite deleted the fix_ch32v_start branch February 8, 2026 20:25
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.

2 participants