Zig-PSP icon indicating copy to clipboard operation
Zig-PSP copied to clipboard

Using integrer division in code generates unsupported instruction `teq`

Open Ryp opened this issue 9 months ago • 5 comments

Applying this patch:

diff --git a/src/main.zig b/src/main.zig
index 7436f6f..39cbab4 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -89,6 +89,8 @@ pub fn main() !void {

     var i: u32 = 0;
     while (true) : (i += 1) {
+        i = 1024 / (i + 1);
+
         psp.sceGuStart(psp.GuContextType.Direct, @as(*anyopaque, @ptrCast(&display_list)));

         psp.sceGuClearColor(psp.rgba(32, 32, 32, 0xFF));

Crashes at illegal instruction with PSPLink:

Load/Start host0:/app.prx UID: 0x04759F35 Name: Zig PSP App
host0:/> Exception - Reserved instruction
Thread ID - 0x04751541
Th Name   - zig_user_main
Module ID - 0x04759F35
Mod Name  - Zig PSP App
EPC       - 0x08B4ECF8
Cause     - 0x10000028
BadVAddr  - 0x5029060E
Status    - 0x40088613
Address   - 0x00000EF8
zr:0x00000000 at:0x00000001 v0:0x00000000 v1:0x55FF0000
a0:0x493F0000 a1:0xC9000000 a2:0xB8000000 a3:0xC6000000
t0:0x483F0000 t1:0x08C60000 t2:0x08B60000 t3:0xDEADBEEF
t4:0xDEADBEEF t5:0xDEADBEEF t6:0xDEADBEEF t7:0xDEADBEEF
s0:0x00000000 s1:0x00000004 s2:0x3D000000 s3:0x00000200
s4:0x00110000 s5:0x00FF0000 s6:0x41000000 s7:0x0000000C
t8:0xDEADBEEF t9:0xDEADBEEF k0:0x0B9FCF00 k1:0x00000000
gp:0x08B6CDD0 sp:0x0B9FCDA0 fp:0x3B000000 ra:0x08B4EB54
0x08B4ECF8: 0x002001F4 '.. .' - Unknown

It also produces the following output in PPSSPP:

21:39:399 root         N[BOOT]: UI/EmuScreen.cpp:385 Booted zig-out/bin/EBOOT.PBP...
21:39:449 zig_user_mai E[JIT]: x86/Jit.cpp:701 Trying to compile instruction 002001f4 that can't be interpreted

Digging a bit into this, I found out that this instruction corresponds to teq (source page 312). According to https://www.psdevwiki.com/psp/Allegrex, this shouldn't be supported by the CPU, since it's a subset of MIPS-II. AFAIK, rust-psp works around this by using the GCC backend with a flag to not emit this instruction (-mno-check-zero-division see source and here)

From a glance it doesn't look like LLVM supports this toggle, but I didn't look very deep. If I have some time this weekend I'll have a look. But maybe @IridescentRose you already have some more information about this.

Not that using any build type (ReleaseSmall or ReleaseFast) doesn't fix the issue.

Ryp avatar Mar 29 '25 22:03 Ryp