diff --git a/code/duffle/dsl.h b/code/duffle/dsl.h index b558402..b1095fa 100644 --- a/code/duffle/dsl.h +++ b/code/duffle/dsl.h @@ -135,16 +135,17 @@ enum { false = 0, true = 1, true_overflow, }; typedef void Proc_(VoidFn) (void); -#define kilo(n) (C_(U4, n) << 10) -#define mega(n) (C_(U4, n) << 20) -#define giga(n) (C_(U4, n) << 30) -#define tera(n) (C_(U4, n) << 40) -#define null C_(U4, 0) -#define nullptr C_(void*, 0) -#define O_(type, field) (C_(U4, & C_(type*,0)->field)) - -#define OT_(field) O_(typeof_ptr(& field), filed)) -#define S_(data) C_(U4, sizeof(data)) +#define kilo(n) (C_(U4, n) << 10) +#define mega(n) (C_(U4, n) << 20) +#define giga(n) (C_(U4, n) << 30) +#define tera(n) (C_(U4, n) << 40) + +#define null C_(U4, 0) +#define nullptr C_(void*, 0) +#define O_(type, field) C_(U4, & C_(type*,0)->field) +#define OA_(type, member, idx) C_(U4, & C_(type*,0)->member[idx]) +#define OT_(field) O_(typeof_ptr(& field), filed)) +#define S_(data) C_(U4, sizeof(data)) #define sop_1(op,a,b) C_(U1, s1_(a) op s1_(b)) #define sop_2(op,a,b) C_(U2, s2_(a) op s2_(b)) diff --git a/code/duffle/gp.h b/code/duffle/gp.h index da00405..01dbd3b 100644 --- a/code/duffle/gp.h +++ b/code/duffle/gp.h @@ -406,8 +406,8 @@ typedef Struct_(RGB8) { B1 r; B1 g; B1 b; }; /* ---------- PolyTag (the OT-link header; 1 word) ---------- */ // enum { - // PolyTag_len_bits = 8, - // PolyTag_addr_bits = 24, +// PolyTag_len_bits = 8, +// PolyTag_addr_bits = 24, // }; typedef Struct_(PolyTag) { union { @@ -570,6 +570,9 @@ enum { gp0_tpage_color_8bpp = 0x1, gp0_tpage_color_16bpp = 0x2, + /* Default TPage value libpsyx's SetDefDrawEnv writes (matches the `li v1, 10; sh v1, 20(v0)` sequence at C11_only.elf:0x8001273C). */ + gp0_tpage_default = 10, + /* TPage semi-transparency mode payload values (NOT bit positions). */ gp0_tpage_semi_trans_none = 0x0, gp0_tpage_semi_trans_alpha = 0x1, diff --git a/code/duffle/lottes_tape.h b/code/duffle/lottes_tape.h index c91bc94..7f0c0ce 100644 --- a/code/duffle/lottes_tape.h +++ b/code/duffle/lottes_tape.h @@ -32,12 +32,42 @@ typedef Slice_MipsCode MipsAtom; /* Register aliases */ enum { - R_AtomJmp = R_T9 atom_reg, /* debug-visible; tape yield handshake scratch */ - R_TapePtr = R_T8 atom_reg, /* The Instruction Stream Pointer */ - R_InCursor = R_T4, + R_AtomJmp = R_T8 atom_reg, /* debug-visible; tape yield handshake scratch */ + R_TapePtr = R_T9 atom_reg, /* The Instruction Stream Pointer */ /* Stringification codes for the GCC inline assembler clobber lists. */ -#define R_TapePtr_Code R_T8_Code -#define R_InCursor_Code R_T4_Code +#define R_AtomJmp_Code R_T8_Code +#define R_TapePtr_Code R_T9_Code + +// R_InCursor = R_T4, +// #define R_InCursor_Code R_T4_Code + +// Reserved Registers (Callee-saved): +// - R_T9: Holds the Tape Ptr which we need to increment +// If we hit a wall with register allocations we can clobber V0 & V1 (return values), defering as opt-in by user. +// - R_RA: Not sure?? +// Needed by ac_yield but can be used as atom scratch: +// - R_T8: Will be used as the atom jump register. + +// All allocatable registers for mips atoms: + R_TScratchVolatile = R_AT, // This one is reserved for psuedo instructions, but you can technically use it. + R_TScratch0 = R_T0, + R_TScratch1 = R_T1, + R_TScratch2 = R_T2, + R_TScratch3 = R_T3, + R_TScratch4 = R_T4, + R_TScratch5 = R_T5, + R_TScratch6 = R_T6, + R_TScratch7 = R_T7, + R_TScratch8 = R_T8, + // Note(Ed): We can technically clobber these, but don't unless we hit a bottleneck. + // R_TScratch10 = R_V0, + // R_TScratch10 = R_V1, + // R_TScratch11 = R_A0, + // R_TScratch12 = R_A1, + // R_TScratch13 = R_A3, + // TODO(Ed): Review S0-S7, they are technically avaialble, we just have to snapshot them at the ABI boundary. + // TODO(Ed): This is technically a waste of cycles for most work? so maybe only do this for expensive atoms on-demand or atom phases. +// TODO(Ed): Sort out the other available registers... (Not sure how much is left avail) }; #pragma region Tape Drive @@ -48,25 +78,26 @@ enum { /* The 'Exit' Atom */ atom_dbg_skip MipsAtom_(tape_exit) { jump_reg(rret_addr), nop }; +//TODO(Ed): Do we backup R_S0-7 here? Have it in a heavier tape run as a opt-in? Same with V0-1 and A0-3? /* Generalized Tape Engine Runner */ -FI_ void tape_run(Slice_MipsCode tape) { register U4* tp rgcc(R_TapePtr) = u4_r(tape.ptr); asm volatile( +FI_ void tape_run(Slice_MipsCode tape) { register U4* tape_ptr rgcc(R_TapePtr) = u4_r(tape.ptr); asm volatile( + // Removed, not needed + /*add_ui( R_SP, R_SP, -MipsStackAlignment)*/ /* Allocate stack space */ + /*, store_word( R_RA, R_SP, 0)*/ /* Safely backup $ra to the stack */ 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 */ + 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) + // Removed, not needed + /*, load_word( R_RA, R_SP, 0)* /* Restore $ra from stack */ + /*, add_ui_self(R_SP, MipsStackAlignment)*/ /* Deallocate stack space */ + asm_rpins, r_use(tape_ptr) asm_clobber: rlit(R_AT) - , rlit(R_V0), rlit(R_V1) - , rlit(R_T0), rlit(R_T1), rlit(R_T2), rlit(R_T3) - , rlit(R_T4), rlit(R_T5), rlit(R_T6), rlit(R_T7) - , rlit(R_T9) + , rlit(R_T0), rlit(R_T1), rlit(R_T2), rlit(R_T3), rlit(R_T4) + , rlit(R_T5), rlit(R_T6), rlit(R_T7), rlit(R_T8) , clb_mem_drain ); } diff --git a/code/duffle/math.h b/code/duffle/math.h index 3b000a8..c1ffcd7 100644 --- a/code/duffle/math.h +++ b/code/duffle/math.h @@ -37,6 +37,7 @@ typedef Struct_(Rect_S4) { S4 x; S4 y; S4 width; S4 height; }; typedef Struct_(M3_S2) { A3x3_S2 m; A3_S4 t; }; +typedef Array_(V2_S2, 2); typedef Array_(V2_S2, 3); typedef Array_(V2_S2, 4); diff --git a/code/hello_joypad/gen/hello_joypad.macs.h b/code/hello_joypad/gen/hello_joypad.macs.h index 492ec7c..b6e91a5 100644 --- a/code/hello_joypad/gen/hello_joypad.macs.h +++ b/code/hello_joypad/gen/hello_joypad.macs.h @@ -9,6 +9,27 @@ #define WORD_COUNT(name, count) enum { words_##name = (count) }; #endif +/* atom_dbg_skip */ +#define mac_store_v2s2(rt_x, rt_y, base, offset) \ + store_half(rt_x, base, offset + O_(V2_S2,x)) \ +, store_half(rt_y, base, offset + O_(V2_S2,y)) +WORD_COUNT(mac_store_v2s2, 2) + +/* atom_dbg_skip */ +#define mac_store_rects2(rt_x, rt_y, rt_width, rt_height, base, offset) \ + store_half(rt_x, base, offset + O_(Rect_S2,x)) \ +, store_half(rt_y, base, offset + O_(Rect_S2,y)) \ +, store_half(rt_width, base, offset + O_(Rect_S2,width)) \ +, store_half(rt_height, base, offset + O_(Rect_S2,height)) +WORD_COUNT(mac_store_rects2, 4) + +/* atom_dbg_skip */ +#define mac_store_rgb8(rr, rg, rb, base, offset) \ + store_byte(rr, base, offset + O_(DrawEnv,initial_bg_color.r)) \ +, store_byte(rg, base, offset + O_(DrawEnv,initial_bg_color.g)) \ +, store_byte(rb, base, offset + O_(DrawEnv,initial_bg_color.b)) +WORD_COUNT(mac_store_rgb8, 3) + #define mac_gcmd_push(cmd, reg_transfer, reg_base, port) \ load_upper_i(reg_transfer, cmd >> 16) \ , or_i_self( reg_transfer, cmd & 0xFFFF) \ diff --git a/code/hello_joypad/hello_joypad.c b/code/hello_joypad/hello_joypad.c index be0ee05..83ccc7f 100644 --- a/code/hello_joypad/hello_joypad.c +++ b/code/hello_joypad/hello_joypad.c @@ -431,8 +431,9 @@ int main(void) { smem = (SMemory){0}; smem.scratchpad = C_(U4_V, 0x1F800000); - smem.primitives.used = 0; - { + // smem.primitives.used = 0; + // smem.active_buf_id = 0; + /*Persistent Entity Setup*/{ ent_cube128_init(& smem.cube.verts, & smem.cube.faces); { Ent_Cube* cube = & smem.cube; cube->rot = v3s2(0, 0, 0); @@ -448,23 +449,12 @@ int main(void) floor->scale = v3s4_fp_one(); } } - { + TapeBuilder tb = tb_make(slice_ut_arr(smem.MemTape)); { reset_graph(0); - // TODO(Ed): Move to an atom - { - smem.active_buf_id = 0; - displayenv_init(& r_(smem.screen_buf.display)[0], 0, 0, ScreenRes_X, ScreenRes_Y); - drawenv_init (& r_(smem.screen_buf.draw )[0], 0, ScreenRes_Y, ScreenRes_X, ScreenRes_Y); - displayenv_init(& r_(smem.screen_buf.display)[1], 0, ScreenRes_Y, ScreenRes_X, ScreenRes_Y); - drawenv_init (& r_(smem.screen_buf.draw )[1], 0, 0, ScreenRes_X, ScreenRes_Y); - smem.screen_buf.draw[0].enable_auto_clear = true; - smem.screen_buf.draw[1].enable_auto_clear = true; - smem.screen_buf.draw[0].initial_bg_color = rgb8( .r = 7, .g = 7, .b = 7 ); - smem.screen_buf.draw[1].initial_bg_color = rgb8( .r = 7, .g = 7, .b = 7 ); - } - TapeBuilder tb = tb_make(slice_ut_arr(smem.MemTape)); - register U4* io_base_addr rgcc(R_IO_BaseAddr) = u4_r(IO_BASE_ADDR); + register U4* io_base_addr rgcc(R_IO_BaseAddr) = u4_r(IO_BASE_ADDR); + register DoubleBuffer* screen_buf rgcc(R_ScreenBuf) = & smem.screen_buf; tb.used = 0; tb_scope_run(& tb) { + tb_emit(& tb, screen_env_init); tb_emit(& tb, gp_screen_init); } pad_init(0); diff --git a/code/hello_joypad/hello_joypad.h b/code/hello_joypad/hello_joypad.h index 633194f..fe939e0 100644 --- a/code/hello_joypad/hello_joypad.h +++ b/code/hello_joypad/hello_joypad.h @@ -13,10 +13,10 @@ enum { typedef Struct_(DrawEnv_Packed) { U4 tag; U4 code[15]; }; typedef Struct_(DrawEnv) { Rect_S2 clip_area; - A2_S2 drawing_offset; + V2_S2 drawing_offset[2]; Rect_S2 texture_window; S2 texture_page; - B1 flag_dither; + B1 flag_dither; B1 flag_draw_on_display; B1 enable_auto_clear; RGB8 initial_bg_color; @@ -32,6 +32,7 @@ typedef Struct_(DisplayEnv) { }; typedef Array_(DrawEnv, 2); typedef Array_(DisplayEnv, 2); + typedef Struct_(DoubleBuffer) { A2_DrawEnv draw; A2_DisplayEnv display; diff --git a/code/hello_joypad/hello_joypad.tape.c b/code/hello_joypad/hello_joypad.tape.c index 1688b53..37c94c0 100644 --- a/code/hello_joypad/hello_joypad.tape.c +++ b/code/hello_joypad/hello_joypad.tape.c @@ -15,6 +15,25 @@ #pragma region MACs (Mips Atom components) + +FI_ MipsAtom ac_store_v2s2(U4 rt_x, U4 rt_y, U4 base, U4 offset) atom_dbg_skip MipsAtomComp_Proc_(ac_store_v2s2, { + store_half(rt_x, base, offset + O_(V2_S2,x)), + store_half(rt_y, base, offset + O_(V2_S2,y)), +}) + +FI_ MipsAtom ac_store_rects2(U4 rt_x, U4 rt_y, U4 rt_width, U4 rt_height, U4 base, U4 offset) atom_dbg_skip MipsAtomComp_Proc_(ac_store_rects2, { + store_half(rt_x, base, offset + O_(Rect_S2,x)), + store_half(rt_y, base, offset + O_(Rect_S2,y)), + store_half(rt_width, base, offset + O_(Rect_S2,width)), + store_half(rt_height, base, offset + O_(Rect_S2,height)), +}) + +FI_ MipsAtom ac_store_rgb8(U1 rr, U1 rg, U1 rb, U4 base, U4 offset) atom_dbg_skip MipsAtomComp_Proc_(ac_store_rgb8, { + store_byte(rr, base, offset + O_(DrawEnv,initial_bg_color.r)), + store_byte(rg, base, offset + O_(DrawEnv,initial_bg_color.g)), + store_byte(rb, base, offset + O_(DrawEnv,initial_bg_color.b)), +}) + FI_ MipsAtom ac_gcmd_push(U4 cmd, U4 reg_transfer, U4 reg_base, U2 port) MipsAtomComp_Proc_(ac_gcmd_push, { load_upper_i(reg_transfer, cmd >> 16), @@ -35,9 +54,7 @@ MipsAtomComp_Proc_(ac_put_disp_env, { FI_ MipsAtom ac_put_draw_env(U4 reg_transfer, U4 reg_base, U2 port) MipsAtomComp_Proc_(ac_put_draw_env, { - /* DR_ENV (16-word packet) — emitted at boot by the gp_screen_init atom. - * The values below are pre-baked for the demo's buffer 0: auto_clear=true, bg=(7,7,7), clip=(0,240,320,240), ofs=(0,0), tw=(0,0,0,0). - * + /* * ORIGIN: each code word corresponds to the EXACT value libpsyx's PutDrawEnv function would compute for the same DrawEnv settings. * References: * - libpsyx source: `toolchain/psyq-4_7/lib/libgpu.a` (binary, function `PutDrawEnv`) @@ -92,12 +109,71 @@ MipsAtomComp_Proc_(ac_put_draw_env, { #pragma region Baked Atoms +enum { + R_ScreenX = R_T5 atom_reg atom_type(U2), + R_ScreenY = R_T6 atom_reg atom_type(U2), + R_ScreenBuf = R_T7 atom_reg, /* Caller-pinned: &smem.screen_buf */ +#define R_ScreenBuf_Code R_T7_Code +}; +//screen_env_init. Mirrors the libpsyx's SetDefDispEnv + SetDefDrawEnv + the manual enable_auto_clear / initial_bg_color writes. +internal MipsAtom_(screen_env_init) atom_info(atom_phase(screen_init) +, atom_reads(R_T0, R_ScreenX, R_ScreenY, R_ScreenBuf) +, atom_writes(R_T0, R_ScreenX, R_ScreenY) +) { + /* display[0] = (0, 0, 320, 240); rest of struct zeroed. */ + add_ui(R_ScreenX, R_0, ScreenRes_X), add_ui(R_ScreenY, R_0, ScreenRes_Y), + mac_store_v2s2(R_ScreenX, R_ScreenY, R_ScreenBuf, O_(DisplayEnv,display_area.width) + OA_(DoubleBuffer,display,0)), + store_word(R_0, R_ScreenBuf, O_(DisplayEnv,display_area) + OA_(DoubleBuffer,display,0)), + store_word(R_0, R_ScreenBuf, O_(DisplayEnv,screen) + OA_(DoubleBuffer,display,0)), + store_word(R_0, R_ScreenBuf, O_(DisplayEnv,vinterlace) + OA_(DoubleBuffer,display,0)), + + /* display[1] = (0, 240, 320, 240); rest of struct zeroed. */ + mac_store_rects2(R_0, R_ScreenY, R_ScreenX, R_ScreenY, R_ScreenBuf, O_(DisplayEnv,display_area) + OA_(DoubleBuffer,display,1)), + store_word(R_0, R_ScreenBuf, O_(DisplayEnv,screen) + OA_(DoubleBuffer,display,1)), + store_word(R_0, R_ScreenBuf, O_(DisplayEnv,vinterlace) + OA_(DoubleBuffer,display,1)), + + mac_store_rects2(R_0, R_ScreenY, R_ScreenX, R_ScreenY, R_ScreenBuf, O_(DrawEnv,clip_area) + OA_(DoubleBuffer,draw,0)), /* draw[0].clip_area = (0, 240, 320, 240). C11's SetDefDrawEnv writes clip.y = y_arg. */ + mac_store_v2s2( R_0, R_ScreenY, R_ScreenBuf, O_(DrawEnv,drawing_offset[0]) + OA_(DoubleBuffer,draw,0)), /* draw[0].drawing_offset[0] = (0, 240); C11 passes y_arg as ofs. */ + + mac_store_v2s2(R_ScreenX, R_ScreenY, R_ScreenBuf, O_(DrawEnv,clip_area.width) + OA_(DoubleBuffer,draw,1)), + + /* draw[0].texture_window = (0, 0, 0, 0); two word-zeroes cover the full 8-byte tw field. */ + store_word(R_0, R_ScreenBuf, O_(DrawEnv,texture_window.x) + OA_(DoubleBuffer,draw,0)), + store_word(R_0, R_ScreenBuf, O_(DrawEnv,texture_window.width) + OA_(DoubleBuffer,draw,0)), + + store_word(R_0, R_ScreenBuf, O_(DrawEnv,drawing_offset[0].x) + OA_(DoubleBuffer,draw,1)), + store_word(R_0, R_ScreenBuf, O_(DrawEnv,texture_window.x) + OA_(DoubleBuffer,draw,1)), + store_word(R_0, R_ScreenBuf, O_(DrawEnv,texture_window.width) + OA_(DoubleBuffer,draw,1)), + + /* draw[0].texture_page = 10 (gp0_tpage_default). C11 SetDefDrawEnv at C11_only.elf:0x8001273C writes the same 0x0A. . */ + add_ui(R_T0, R_0, gp0_tpage_default), + store_half(R_T0, R_ScreenBuf, O_(DrawEnv,texture_page) + OA_(DoubleBuffer,draw,0)), + store_half(R_T0, R_ScreenBuf, O_(DrawEnv,texture_page) + OA_(DoubleBuffer,draw,1)), + + /* draw[0] control bytes: flag_dither=1, flag_draw_on_display=1 (the dfe bit per psx-spx; libpsyx sets it via `SetDefDrawEnv`'s conditional at C11_only.elf:0x80012728), enable_auto_clear=1. Each byte is named; + * the previous `store_word(R_0, ..., +20)` overwrote all four with zero. */ + add_ui(R_T0, R_0, 1), + store_byte(R_T0, R_ScreenBuf, O_(DrawEnv,flag_dither) + OA_(DoubleBuffer,draw,0)), + store_byte(R_T0, R_ScreenBuf, O_(DrawEnv,flag_draw_on_display) + OA_(DoubleBuffer,draw,0)), + store_byte(R_T0, R_ScreenBuf, O_(DrawEnv,enable_auto_clear) + OA_(DoubleBuffer,draw,0)), + store_byte(R_T0, R_ScreenBuf, O_(DrawEnv,flag_dither) + OA_(DoubleBuffer,draw,1)), + store_byte(R_T0, R_ScreenBuf, O_(DrawEnv,flag_draw_on_display) + OA_(DoubleBuffer,draw,1)), + store_byte(R_T0, R_ScreenBuf, O_(DrawEnv,enable_auto_clear) + OA_(DoubleBuffer,draw,1)), + + /* draw[0].initial_bg_color = (r=7, g=7, b=7). */ + add_ui(R_T0, R_0, 7), + mac_store_rgb8(R_T0,R_T0,R_T0, R_ScreenBuf, O_(DrawEnv,initial_bg_color) + OA_(DoubleBuffer,draw,0)), + mac_store_rgb8(R_T0,R_T0,R_T0, R_ScreenBuf, O_(DrawEnv,initial_bg_color) + OA_(DoubleBuffer,draw,1)), + + mac_yield(), +}; + enum { R_IO_BaseAddr = R_T4 atom_reg, /* Caller-pinned: IO_BASE_ADDR = 0x1F800000 */ #define R_IO_BaseAddr_Code R_T4_Code }; -internal MipsAtom_(gp_screen_init) atom_info(atom_phase(screen_init)) { - store_word(R_0, R_IO_BaseAddr, GPIO_PORT1_OFFSET), /* GP1(00h) Reset */ +internal MipsAtom_(gp_screen_init) atom_info(atom_phase(screen_init), atom_reads(R_IO_BaseAddr)) { + store_word(R_0, R_IO_BaseAddr, GPIO_PORT1_OFFSET), /* GP1(00h) Reset */ mac_gcmd_push(gp1_word_ResetCmdBuffer(), R_T5, R_IO_BaseAddr, GPIO_PORT1_OFFSET), /* GP1(01h) ClearFIFO */ mac_gcmd_push(gp1_word_AcknowledgeIRQ(), R_T5, R_IO_BaseAddr, GPIO_PORT1_OFFSET), /* GP1(02h) AckIRQ */ mac_gcmd_push(gp1_word_DisplayOn(), R_T5, R_IO_BaseAddr, GPIO_PORT1_OFFSET), /* GP1(03h) Display ON */ @@ -170,7 +246,7 @@ typedef Struct_(Binds_CubeTri) { }; internal MipsAtom_(rbind_cube_g4_face) atom_info(atom_bind(Binds_CubeTri), atom_phase(cube_g4) , atom_reads(R_TapePtr) -, atom_writes(R_PrimCursor, R_FaceCursor, R_VertBase, R_OtBase) +, atom_writes(R_PrimCursor, R_FaceCursor, R_VertBase, R_OtBase, R_TapePtr) ){ /* Pop 4 arguments from the tape directly into the workspace registers */ load_word(R_PrimCursor, R_TapePtr, O_(Binds_CubeTri,PrimCursor)), @@ -237,7 +313,7 @@ typedef Struct_(Binds_FloorTri) { internal MipsAtom_(rbind_floor_f3_face) atom_info(atom_bind(Binds_FloorTri), atom_phase(floor_f3) , atom_reads(R_TapePtr) - , atom_writes(R_PrimCursor, R_FaceCursor, R_VertBase, R_OtBase) + , atom_writes(R_PrimCursor, R_FaceCursor, R_VertBase, R_OtBase, R_TapePtr) ){ /* Pop 4 arguments from the tape directly into the workspace registers */ load_word(R_PrimCursor, R_TapePtr, O_(Binds_FloorTri,PrimCursor)),