From 74f390c3b1fe6e5d8168e7136881354423b2b6f4 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Wed, 8 Jul 2026 17:14:11 -0400 Subject: [PATCH] Reviewing post-dsl refactors, more pseudo instructions --- code/duffle/lottes_tape.h | 68 ++++++++++++++++----------------- code/duffle/mips.h | 5 +++ code/gte_hello/hello_gte.h | 9 ----- code/gte_hello/hello_gte_tape.c | 4 +- 4 files changed, 41 insertions(+), 45 deletions(-) diff --git a/code/duffle/lottes_tape.h b/code/duffle/lottes_tape.h index 11a2129..d4b4d41 100644 --- a/code/duffle/lottes_tape.h +++ b/code/duffle/lottes_tape.h @@ -44,14 +44,14 @@ MipsAtom_(tape_exit) { jump_reg(rret_addr), nop }; /* Generalized Tape Engine Runner */ FI_ void tape_run(Slice_U4 tape) { register U4* tp rgcc(R_TapePtr) = tape.ptr; asm volatile( asm_words( - add_ui( R_SP, R_SP, -MipsStackAlignment) /* Allocate stack space */ - , store_word(R_RA, R_SP, 0) /* Safely backup $ra to the stack */ - , load_word( R_AtomJmp, R_TapePtr, 0) /* Bootstrap the first jump */ - , add_ui_self( R_TapePtr, S_(MipsCode)) /* Advance tape */ - , call_reg( R_AtomJmp) /* jalr $t9 */ - , nop /* Branch delay slot */ - , load_word(R_RA, R_SP, 0) /* Restore $ra from stack */ - , add_ui_self( R_SP, MipsStackAlignment) /* Deallocate stack space */ + add_ui( R_SP, R_SP, -MipsStackAlignment) /* Allocate stack space */ + , store_word( R_RA, R_SP, 0) /* Safely backup $ra to the stack */ + , load_word( R_AtomJmp, R_TapePtr, 0) /* Bootstrap the first jump */ + , add_ui_self(R_TapePtr, S_(MipsCode)) /* Advance tape */ + , call_reg( R_AtomJmp) /* jalr $t9 */ + , nop /* Branch delay slot */ + , load_word( R_RA, R_SP, 0) /* Restore $ra from stack */ + , add_ui_self(R_SP, MipsStackAlignment) /* Deallocate stack space */ ) asm_rpins, r_use(tp) asm_clobber: @@ -123,10 +123,10 @@ FI_ Slice_U4 tb_slice(TapeBuilder tb) { return (Sli * Args: _r, _g, _b are 8-bit RGB byte values (not raw 16-bit fields). * Migrated from hello_gte_tape.c; takes RGB form per the Phase 3 * convention. */ -#define mac_format_f3_color(_r, _g, _b) \ - load_upper_i( R_AT, gp0_cmd_poly_f3 << 8 | (_b)) \ - , or_i( R_AT, R_AT, ((_g) << 8) | (_r)) \ - , store_word(R_AT, R_PrimCursor, O_(Poly_F3,color)) +#define mac_format_f3_color(r,g,b) \ + load_upper_i(R_AT, gp0_cmd_poly_f3 << 8 | (b)) \ + , or_i_self( R_AT, ((g) << 8) | (r)) \ + , store_word( R_AT, R_PrimCursor, O_(Poly_F3,color)) /* Words: 3; Stores the 3 transformed (V2_S2 screen) vertices to the F3 */ #define mac_gte_store_f3() \ @@ -204,16 +204,16 @@ typedef Struct_(Binds_SetGteWorld) { internal MipsAtom_(set_gte_world) { /* Pop matrix address from tape into R_T3 ($11) */ load_word(R_T3, R_TapePtr, O_(Binds_SetGteWorld,transform)), - add_ui_self( R_TapePtr, S_(Binds_SetGteWorld)), + add_ui_self( R_TapePtr, S_(Binds_SetGteWorld)), // TODO(Ed): Annotate magic offsets. /* Load 3x3 Rotation + 3x1 Translation from R_T3 into GTE CONTROL Regs (ctc2) */ - load_word(R_T0, R_T3, 0), load_word(R_T1, R_T3, 4), - gte_mv_to_ctrl_r( R_T0, gte_cr_RT11), gte_mv_to_ctrl_r( R_T1, gte_cr_RT12), - load_word(R_T0, R_T3, 8), load_word(R_T1, R_T3, 12), load_word(R_T2, R_T3, 16), - gte_mv_to_ctrl_r( R_T0, gte_cr_RT13), gte_mv_to_ctrl_r( R_T1, gte_cr_RT21), gte_mv_to_ctrl_r( R_T2, gte_cr_RT22), - load_word(R_T0, R_T3, 20), load_word(R_T1, R_T3, 24), load_word(R_T2, R_T3, 28), - gte_mv_to_ctrl_r( R_T0, gte_cr_TRX), gte_mv_to_ctrl_r( R_T1, gte_cr_TRY), gte_mv_to_ctrl_r( R_T2, gte_cr_TRZ), + load_word(R_T0, R_T3, 0), load_word(R_T1, R_T3, 4), + gte_mv_to_ctrl_r(R_T0, gte_cr_RT11), gte_mv_to_ctrl_r(R_T1, gte_cr_RT12), + load_word(R_T0, R_T3, 8), load_word(R_T1, R_T3, 12), load_word(R_T2, R_T3, 16), + gte_mv_to_ctrl_r(R_T0, gte_cr_RT13), gte_mv_to_ctrl_r(R_T1, gte_cr_RT21), gte_mv_to_ctrl_r(R_T2, gte_cr_RT22), + load_word(R_T0, R_T3, 20), load_word(R_T1, R_T3, 24), load_word(R_T2, R_T3, 28), + gte_mv_to_ctrl_r(R_T0, gte_cr_TRX), gte_mv_to_ctrl_r(R_T1, gte_cr_TRY), gte_mv_to_ctrl_r(R_T2, gte_cr_TRZ), mac_yield() }; @@ -228,25 +228,25 @@ internal MipsAtom_(diag_yield) { mac_yield() }; internal MipsAtom_(diag_color) { store_word( R_0, R_T7, 0), load_upper_i(R_AT, gp0_cmd_poly_f3 << 8 | 0xFF), /* High: MipsCode Poly_F3(0x20) + Color B:FF */ - or_i( R_AT, R_AT, 0xFF00), /* Low: Color G:FF, R:00 (Cyan) */ + or_i_self( R_AT, 0xFF00), /* Low: Color G:FF, R:00 (Cyan) */ store_word( R_AT, R_T7, 4), /* Fake coordinates - Swapped winding order to prevent GPU culling! */ - load_upper_i(R_AT, 0x0010), or_i(R_AT, R_AT, 0x0010), store_word(R_AT, R_T7, 8), /* (16, 16) */ - load_upper_i(R_AT, 0x0050), or_i(R_AT, R_AT, 0x0010), store_word(R_AT, R_T7, 12), /* (80, 16) */ - load_upper_i(R_AT, 0x0010), or_i(R_AT, R_AT, 0x0050), store_word(R_AT, R_T7, 16), /* (16, 80) */ + load_upper_i(R_AT, 0x0010), or_i_self(R_AT, 0x0010), store_word(R_AT, R_T7, 8), /* (16, 16) */ + load_upper_i(R_AT, 0x0050), or_i_self(R_AT, 0x0010), store_word(R_AT, R_T7, 12), /* (80, 16) */ + load_upper_i(R_AT, 0x0010), or_i_self(R_AT, 0x0050), store_word(R_AT, R_T7, 16), /* (16, 80) */ - add_ui( R_T1, R_0, 10), - shift_lleft(R_T1, R_T1, 2), - add_u( R_T1, R_T1, R_T6), + add_ui( R_T1, R_0, 10), + shift_lleft_self(R_T1, 2), + add_u_self( R_T1, R_T6), load_word( R_AT, R_T1, 0), load_upper_i(R_V0, 0x0400), // <--- Fills load delay slot! store_word( R_AT, R_T7, 0), - shift_lleft( R_AT, R_T7, 8), shift_lright(R_AT, R_AT, 8), - or_u( R_AT, R_AT, R_V0), - store_word(R_AT, R_T1, 0), + shift_lleft(R_AT, R_T7, 8), shift_lright(R_AT, R_AT, 8), + or_u_self( R_AT, R_V0), + store_word( R_AT, R_T1, 0), add_ui(R_T7, R_T7, 20), @@ -264,15 +264,15 @@ internal MipsAtom_(diag_gte) { /* Load Vertices into GTE */ shift_lleft( R_AT, R_T0, 3), add_u( R_AT, R_AT, R_T5), load_word(R_V0, R_AT, 0), load_word(R_V1, R_AT, 4), - gte_mv_to_data_r( R_V0, C2_VXY0), gte_mv_to_data_r( R_V1, C2_VZ0), + gte_mv_to_data_r(R_V0, C2_VXY0), gte_mv_to_data_r(R_V1, C2_VZ0), - shift_lleft( R_AT, R_T1, 3), add_u( R_AT, R_AT, R_T5), + shift_lleft( R_AT, R_T1, 3), add_u(R_AT, R_AT, R_T5), load_word(R_V0, R_AT, 0), load_word(R_V1, R_AT, 4), - gte_mv_to_data_r( R_V0, C2_VXY1), gte_mv_to_data_r( R_V1, C2_VZ1), + gte_mv_to_data_r(R_V0, C2_VXY1), gte_mv_to_data_r(R_V1, C2_VZ1), - shift_lleft( R_AT, R_T2, 3), add_u( R_AT, R_AT, R_T5), + shift_lleft(R_AT, R_T2, 3), add_u(R_AT, R_AT, R_T5), load_word(R_V0, R_AT, 0), load_word(R_V1, R_AT, 4), - gte_mv_to_data_r( R_V0, C2_VXY2), gte_mv_to_data_r( R_V1, C2_VZ2), + gte_mv_to_data_r(R_V0, C2_VXY2), gte_mv_to_data_r(R_V1, C2_VZ2), /* Run Math */ nop, nop, gte_cmdw_rtpt, diff --git a/code/duffle/mips.h b/code/duffle/mips.h index d3a5952..90dc402 100644 --- a/code/duffle/mips.h +++ b/code/duffle/mips.h @@ -355,6 +355,7 @@ enum { _BitOffsets = 0 #define load_u4 load_word // Ergonomic add to the same register. +#define or_i_self(rt_rs, imm) enc_i(op_ori, (rt_rs), (rt_rs), (imm)) #define add_ui_self(rt_rs, imm) enc_i(op_addiu, (rt_rs), (rt_rs), (imm)) /* Logic Opcodes */ @@ -364,11 +365,15 @@ enum { _BitOffsets = 0 #define xor_u(rd, rs, rt) enc_r(op_special, (rs), (rt), (rd), 0, fc_xor) #define nor_u(rd, rs, rt) enc_r(op_special, (rs), (rt), (rd), 0, fc_nor) +#define or_u_self(rd_rs, rt) enc_r(op_special, (rd_rs), (rt), (rd_rs), 0, fc_or) + /* Shift family (R-type). shift_lleft/lright/aright: `sll/srl/sra rd, rt, shamt` */ #define shift_lleft(rd, rt, shamt) enc_r(op_special, R_0, (rt), (rd), (shamt), fc_sll) #define shift_lright(rd, rt, shamt) enc_r(op_special, R_0, (rt), (rd), (shamt), fc_srl) #define shift_aright(rd, rt, shamt) enc_r(op_special, R_0, (rt), (rd), (shamt), fc_sra) +#define shift_lleft_self(rd_rt, shamt) enc_r(op_special, R_0, (rd_rt), (rd_rt), (shamt), fc_sll) + #define mask_upper(rd, rt, shamt) shift_lleft(rd, rt, shamt), shift_lright(rd, rt, shamt) /* jr rs — jump to address in rs. */ diff --git a/code/gte_hello/hello_gte.h b/code/gte_hello/hello_gte.h index af1479c..9fabd0a 100644 --- a/code/gte_hello/hello_gte.h +++ b/code/gte_hello/hello_gte.h @@ -63,14 +63,6 @@ U4 vsync(U4 mode) __asm__("VSync"); void draw_orderingtbl(U4* buf) __asm__("DrawOTag"); -/* Primitive Handling Macros - * All primitive types (PolyTag, Poly_F3, Poly_F4, Poly_G3, Poly_G4, - * Poly_FT3, Poly_FT4, Poly_GT3, Poly_GT4) and the set_poly_* setters, - * set_len / set_addr / get_len / get_addr macros, and the - * orderingtbl_add_primitive(s) helpers all live in `duffle/gp.h` - * now (per the Phase 3 gp.h overhaul). This file no longer duplicates - * those definitions. */ - typedef Struct_(Tile) { U4 tag; RGB8 color; @@ -78,7 +70,6 @@ typedef Struct_(Tile) { Rect_S2 rect; }; - /* Linear Algebra */ diff --git a/code/gte_hello/hello_gte_tape.c b/code/gte_hello/hello_gte_tape.c index fa5c0d4..34d271d 100644 --- a/code/gte_hello/hello_gte_tape.c +++ b/code/gte_hello/hello_gte_tape.c @@ -35,7 +35,7 @@ internal MipsAtom_(rbind_cube_tri) { load_word(R_FaceCursor, R_TapePtr, O_(Binds_CubeTri,FaceCursor)), load_word(R_VertBase, R_TapePtr, O_(Binds_CubeTri,VertBase)), load_word(R_OtBase, R_TapePtr, O_(Binds_CubeTri,OtBase)), - add_ui_self( R_TapePtr, S_(Binds_CubeTri)), + add_ui_self( R_TapePtr, S_(Binds_CubeTri)), // Note(Ed): This entire thing is argument shuffle? // TODO(Ed): Eliminate mac_yield() @@ -225,7 +225,7 @@ typedef Struct_(Binds_SyncPrimitiveArena) { U4 used; U4 cursor; }; internal MipsAtom_(sync_primitive_arena) { load_word(R_AT, R_TapePtr, O_(Binds_SyncPrimitiveArena,used)), load_word(R_T0, R_TapePtr, O_(Binds_SyncPrimitiveArena,cursor)), - add_ui_self( R_TapePtr, S_(Binds_SyncPrimitiveArena)), + add_ui_self( R_TapePtr, S_(Binds_SyncPrimitiveArena)), /* Calculate byte offset and store directly back to RAM */ sub_u( R_T0, R_PrimCursor, R_T0), // R_T0 = R_PrimCursor - binds.cursor store_word(R_T0, R_AT, 0), // R_AT[0] = R_T0