From 274e0f31f5230a0e5fdad20c7939b2f9d43ecdce Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sat, 11 Oct 2025 00:18:44 -0400 Subject: [PATCH] Update convention yet again Doing stuff inspired by Timothy Lottes's fixing c vod --- .gitignore | 1 + .vscode/settings.json | 59 ---------- code/duffle/dsl.h | 163 ++++++++++++++++----------- code/duffle/farena.h | 14 +-- code/duffle/gp.h | 11 +- code/duffle/math.h | 33 +++--- code/duffle/memory.h | 70 ++++++------ code/factorial/factorial.s | 3 +- code/graphics_hello_psyq/hello_gpu.c | 160 ++++++++++++++++++-------- code/graphics_hello_psyq/hello_gpu.h | 89 ++++++++------- 10 files changed, 327 insertions(+), 276 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.gitignore b/.gitignore index af65465..0411a49 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ toolchain/PSn00bSDK *.o *.a .sentry-native +.vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index f735159..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,59 +0,0 @@ -{ - "files.associations": { - "*.rmd": "markdown", - "*.s": "gas", - "*.dasm": "lldb.disassembly", - "*.asm": "gas", - "stdlib.h": "c", - "libetc.h": "c", - "libgpu.h": "c", - "stddef.h": "c", - "abs.h": "c", - "qsort.h": "c", - "malloc.h": "c", - "convert.h": "c", - "type_traits": "c", - "xlocmon": "c", - "iterator": "c", - "regex": "c", - "dsl.h": "c", - "xlocale": "c", - "gp.h": "c", - "array": "c", - "initializer_list": "c", - "string_view": "c", - "functional": "c", - "tuple": "c", - "hello_gpu.h": "c", - "compare": "c", - "ratio": "c", - "scoped_allocator": "c", - "variant": "c", - "math.h": "c", - "cstdint": "c", - "*.ch": "c", - "ranges": "c", - "span": "c", - "bitset": "c" - }, - "workbench.colorCustomizations": { - "activityBar.activeBackground": "#ffd427", - "activityBar.background": "#ffd427", - "activityBar.foreground": "#15202b", - "activityBar.inactiveForeground": "#15202b99", - "activityBarBadge.background": "#009f80", - "activityBarBadge.foreground": "#e7e7e7", - "commandCenter.border": "#15202b99", - "sash.hoverBorder": "#ffd427", - "statusBar.background": "#f3c300", - "statusBar.foreground": "#15202b", - "statusBarItem.hoverBackground": "#c09a00", - "statusBarItem.remoteBackground": "#f3c300", - "statusBarItem.remoteForeground": "#15202b", - "titleBar.activeBackground": "#f3c300", - "titleBar.activeForeground": "#15202b", - "titleBar.inactiveBackground": "#f3c30099", - "titleBar.inactiveForeground": "#15202b99" - }, - "peacock.color": "#F3C300" -} \ No newline at end of file diff --git a/code/duffle/dsl.h b/code/duffle/dsl.h index d78fa3d..89d25d3 100644 --- a/code/duffle/dsl.h +++ b/code/duffle/dsl.h @@ -3,65 +3,104 @@ # include "assert.h" #endif -typedef unsigned char U8; -typedef signed char S8; -typedef unsigned short U16; -typedef signed short S16; -typedef unsigned int U32; -typedef signed int S32; -typedef unsigned char BYTE; -typedef unsigned int USIZE; -typedef signed int SSIZE; -typedef S8 B8; -typedef S16 B16; -typedef S32 B32; -enum { - false = 0, - true = 1, - true_overflow, -}; -#define glue_impl(A, B) A ## B -#define glue(A, B) glue_impl(A, B) -#define stringify_impl(S) #S -#define stringify(S) stringify_impl(S) -#define tmpl(prefix, type) prefix ## _ ## type +#define align_(value) __attribute__((aligned (value))) // for easy alignment +#define expect_(x, y) __builtin_expect(x, y) // so compiler knows the common path +#define finline static inline __attribute__((always_inline)) // force inline +#define no_inline static __attribute__((noinline)) // force no inline [used in thread api] +#define R_ __restrict // pointers are either restricted or volatile and nothing else +#define V_ volatile // pointers are either restricted or volatile and nothing else + +#define glue_impl(A, B) A ## B +#define glue(A, B) glue_impl(A, B) +#define stringify_impl(S) #S +#define stringify(S) stringify_impl(S) +#define tmpl(prefix, type) prefix ## _ ## type + +#define local_persist static +#define internal static +#define global +#define gknown + +#define offset_of(type, member) cast(SSIZE, & (((type*) 0)->member)) +#define static_assert _Static_assert +#define typeof __typeof__ +#define typeof_ptr(ptr) typeof((ptr)[0]) +#define typeof_same(a, b) _Generic((a), typeof((b)): 1, default: 0) + +#define def_R_(type) type*restrict type ## _R +#define def_V_(type) type*volatile type ## _V +#define def_ptr_set(type) def_R_(type); typedef def_V_(type) +#define def_tset(type) type; typedef def_ptr_set(type) + +typedef __UINT8_TYPE__ def_tset(U1); typedef __UINT16_TYPE__ def_tset(U2); typedef __UINT32_TYPE__ def_tset(U4); +typedef __INT8_TYPE__ def_tset(S1); typedef __INT16_TYPE__ def_tset(S2); typedef __INT32_TYPE__ def_tset(S4); +typedef unsigned char def_tset(B1); typedef __UINT16_TYPE__ def_tset(B2); typedef __UINT32_TYPE__ def_tset(B4); +enum { false = 0, true = 1, true_overflow, }; + +#define u1_r(value) cast(U1_R, value) +#define u2_r(value) cast(U2_R, value) +#define u4_r(value) cast(U4_R, value) +#define u1_v(value) cast(U1_V, value) +#define u2_v(value) cast(U2_V, value) +#define u4_v(value) cast(U4_V, value) + +#define u1_(value) cast(U1, value) +#define u2_(value) cast(U2, value) +#define u4_(value) cast(U4, value) +#define s1_(value) cast(S1, value) +#define s2_(value) cast(S2, value) +#define s4_(value) cast(S4, value) -#define alignas _Alignas -#define alignof _Alignof -#define byte_pad(amount, ...) BYTE glue(_PAD_, __VA_ARGS__) [amount] #define farray_len(array) (SSIZE)sizeof(array) / size_of( typeof((array)[0])) #define farray_init(type, ...) (type[]){__VA_ARGS__} -#define def_farray(type, len) type A ## len ## _ ## type[len] +#define def_farray_impl(_type, _len) _type A ## _len ## _ ## _type[_len] +#define def_farray(type, len) def_farray_impl(type, len) #define def_enum(underlying_type, symbol) underlying_type symbol; enum symbol #define def_struct(symbol) struct symbol symbol; struct symbol #define def_union(symbol) union symbol symbol; union symbol #define def_proc(symbol) symbol #define opt_args(symbol, ...) &(symbol){__VA_ARGS__} #define ret_type(type) type -#define local_persist static -#define internal static -#define global -#define gknown -#define offset_of(type, member) cast(SSIZE, & (((type*) 0)->member)) -#define static_assert _Static_assert -#define typeof __typeof__ -#define typeof_ptr(ptr) typeof(ptr[0]) -#define typeof_same(a, b) _Generic((a), typeof((b)): 1, default: 0) +#define o_(field) offset_of(typeof_ptr(& field), filed)) + +#define alignas _Alignas +#define alignof _Alignof +#define byte_pad(amount, ...) B1 glue(_PAD_, __VA_ARGS__) [amount] #define cast(type, data) ((type)(data)) #define pcast(type, data) * cast(type*, & (data)) #define nullptr cast(void*, 0) -#define size_of(data) cast(SSIZE, sizeof(data)) -#define kilo(n) (cast(SSIZE, n) << 10) -#define mega(n) (cast(SSIZE, n) << 20) -#define giga(n) (cast(SSIZE, n) << 30) -#define tera(n) (cast(SSIZE, n) << 40) +#define size_of(data) cast(U4, sizeof(data)) -#if 1 -# define dbg_args(...) __VA_ARGS__ -#else -# define dbg_args(...) -#endif +#define r_(ptr) cast(typeof_ptr(ptr)*R_, ptr) +#define v_(ptr) cast(typeof_ptr(ptr)*V_, ptr) + +#define kilo(n) (cast(U4, n) << 10) +#define mega(n) (cast(U4, n) << 20) +#define giga(n) (cast(U4, n) << 30) +#define tera(n) (cast(U4, n) << 40) + +#define dbg_args(...) __VA_ARGS__ + +#define sop_1(op, a, b) cast(U1, s1_(a) op s1_(b)) +#define sop_2(op, a, b) cast(U2, s2_(a) op s2_(b)) +#define sop_4(op, a, b) cast(U4, s4_(a) op s4_(b)) + +#define def_signed_op(id, op, width) finline U ## width id ## _s ## width(U ## width a, U ## width b) {return sop_ ## width(op, a, b); } +#define def_signed_ops(id, op) def_signed_op(id, op, 1) def_signed_op(id, op, 2) def_signed_op(id, op, 4) +def_signed_ops(add, +) def_signed_ops(sub, -) +def_signed_ops(mut, *) def_signed_ops(div, /) +def_signed_ops(gt, >) def_signed_ops(lt, <) +def_signed_ops(ge, >=) def_signed_ops(le, <=) + +#define def_generic_sop(op, a, ...) _Generic((a), U1: op ## _s1, U2: op ## _s2, U4: op ## _s4) (a, __VA_ARGS__) +#define add_s(a,b) def_generic_sop(add,a,b) +#define sub_s(a,b) def_generic_sop(sub,a,b) +#define mut_s(a,b) def_generic_sop(mut,a,b) +#define gt_s(a,b) def_generic_sop(gt, a,b) +#define lt_s(a,b) def_generic_sop(lt, a,b) +#define ge_s(a,b) def_generic_sop(ge, a,b) +#define le_s(a,b) def_generic_sop(le, a,b) #define span_iter(type, iter, m_begin, op, m_end) \ tmpl(Iter_Span,type) iter = { \ @@ -74,28 +113,28 @@ enum { def_struct(tmpl( Span,type)) { type begin; type end; }; \ typedef def_struct(tmpl(Iter_Span,type)) { tmpl(Span,type) r; type cursor; } -typedef def_span(S32); -typedef def_span(U32); -typedef def_span(SSIZE); +typedef def_span(S4); +typedef def_span(U4); typedef void def_proc(VoidFn) (void); -#define def_Slice(type) \ -def_struct(tmpl(Slice,type)) { \ - type* ptr; \ - SSIZE len; \ -} +typedef unsigned char def_tset(UTF8); +typedef def_struct(Str8) { UTF8* ptr; U4 len; }; typedef Str8 def_tset(Slice_UTF8); +typedef def_struct(Slice_Str8) { Str8* ptr; U4 len; }; +#define txt(string_literal) (Str8){ (UTF8*) string_literal, size_of(string_literal) - 1 } + +#define def_Slice(type) def_struct(tmpl(Slice,type)) { type* ptr; U4 len; } #define slice_assert(slice) do { assert((slice).ptr != nullptr); assert((slice).len > 0); } while(0) #define slice_end(slice) ((slice).ptr + (slice).len) #define size_of_slice_type(slice) size_of( * (slice).ptr ) typedef def_Slice(void); -typedef def_Slice(BYTE); -#define slice_byte(slice) ((Slice_BYTE){cast(Byte*, (slice).ptr), (slice).len * size_of_slice_type(slice)}) -#define slice_fmem(mem) ((Slice_BYTE){ mem, size_of(mem) }) +typedef def_Slice(B1); +#define slice_byte(slice) ((Slice_B1){cast(B1*, (slice).ptr), (slice).len * size_of_slice_type(slice)}) +#define slice_fmem(mem) ((Slice_B1){ mem, size_of(mem) }) -void slice__copy(Slice_BYTE dest, SSIZE dest_typewidth, Slice_BYTE src, SSIZE src_typewidth); -void slice__zero(Slice_BYTE mem, SSIZE typewidth); +void slice__copy(Slice_B1 dest, U4 dest_typewidth, Slice_B1 src, U4 src_typewidth); +void slice__zero(Slice_B1 mem, U4 typewidth); #define slice_copy(dest, src) do { \ static_assert(typeof_same(dest, src)); \ slice__copy(slice_byte(dest), size_of_slice_type(dest), slice_byte(src), size_of_slice_type(src)); \ @@ -106,13 +145,7 @@ void slice__zero(Slice_BYTE mem, SSIZE typewidth); typeof((container).ptr) iter = (container).ptr; \ iter != slice_end(container); \ ++ iter -#define slice_arg_from_array(type, ...) & (tmpl(Slice,type)) { \ +#define slice_from_farray(type, ...) & (tmpl(Slice,type)) { \ .ptr = farray_init(type, __VA_ARGS__), \ .len = farray_len( farray_init(type, __VA_ARGS__)) \ } - -typedef unsigned char UTF8; -typedef def_Slice(UTF8); -typedef Slice_UTF8 Str8; -typedef def_Slice(Str8); -#define txt(string_literal) (Str8){ (UTF8*) string_literal, size_of(string_literal) - 1 } diff --git a/code/duffle/farena.h b/code/duffle/farena.h index 08fd614..b6b1a37 100644 --- a/code/duffle/farena.h +++ b/code/duffle/farena.h @@ -6,17 +6,17 @@ #endif typedef def_struct(Opts_farena) { - Str8 type_name; - SSIZE alignment; + Str8 type_name; + U4 alignment; }; typedef def_struct(FArena) { void* start; - SSIZE capacity; - SSIZE used; + U4 capacity; + U4 used; }; -FArena farena_make (Slice_BYTE mem); -void farena_init (FArena* arena, Slice_BYTE byte); -Slice_BYTE farena__push (FArena* arena, SSIZE amount, SSIZE type_width, Opts_farena* opts); +FArena farena_make (Slice_B1 mem); +void farena_init (FArena* arena, Slice_B1 byte); +Slice_B1 farena__push (FArena* arena, U4 amount, U4 type_width, Opts_farena* opts); void farena_reset (FArena* arena); void farena_rewind(FArena* arena, AllocatorSP save_point); AllocatorSP farena_save (FArena arena); diff --git a/code/duffle/gp.h b/code/duffle/gp.h index 19ccbec..48c3943 100644 --- a/code/duffle/gp.h +++ b/code/duffle/gp.h @@ -4,7 +4,7 @@ # include "math.h" #endif -typedef def_enum(U32, gp_Commands) { +typedef def_enum(U4, gp_Commands) { gcmd_Reset = 0b000, gcmd_Polygon = 0b001, gcmd_Line = 0b010, @@ -73,19 +73,18 @@ typedef def_enum(U32, gp_Commands) { #define gp_SetArea_TopLeft (gcmd_SetDrawArea_TopLeft << gcmd_offset) #define gp_SetArea_BottomRight (gcmd_SetDrawArea_BotRight << gcmd_offset) -typedef def_struct(RGB8) { BYTE r; BYTE g; BYTE b; }; +typedef def_struct(RGB8) { B1 r; B1 g; B1 b; }; #define rgba8(r, g, b) (RGB8){ r, g, b } -typedef BYTE gp_Pixel16[1]; -typedef BYTE gp_Pixel24[3]; +typedef B1 gp_Pixel16[1]; +typedef B1 gp_Pixel24[3]; #define gp_b10_X 0 #define gp_b10_Y 10 #define gp_b16_X 0 #define gp_b16_Y 16 -typedef def_struct(gp_Vec2) { U16 y; U16 x; }; - +typedef def_struct(gp_Vec2) { U2 y; U2 x; }; #if 1 void gp_screen_init(void) __asm__("gp_screen_init_asm"); diff --git a/code/duffle/math.h b/code/duffle/math.h index e9b887e..2e81428 100644 --- a/code/duffle/math.h +++ b/code/duffle/math.h @@ -7,21 +7,24 @@ #define max(A, B) (((A) > (B)) ? (A) : (B)) #define clamp_bot(X, B) max(X, B) -typedef def_farray(S16, 2); -typedef def_farray(S32, 2); -// typedef def_farray(F32, 2); +typedef def_farray(S2, 2); +typedef def_farray(S2, 3); +typedef def_farray(S4, 2); +typedef def_farray(S4, 3); +typedef S2 A3A3_S2[3][3]; -typedef def_struct(Extent_2S16) { S16 width; S16 height; }; -typedef def_struct(Extent_2S32) { S32 width; S32 height; }; -// typedef def_struct(Extent_2F32) { F32 width; F32 height; } -typedef def_struct(Vec_2S16) { S16 x; S16 y; }; -typedef def_struct(Vec_2S32) { S32 x; S32 y; }; -// typedef def_struct(Vec_2F32) { F32 x; F32 y; }; -typedef def_struct(Range_2S16) { Vec_2S16 p0; Vec_2S16 p1; }; -typedef def_struct(Range_2S32) { Vec_2S32 p0; Vec_2S32 p1; }; -// typedef def_struct(Range_2F32) { Vec_2F32 p0; Vec_2F32 p1; }; +typedef def_struct(Extent2_S2) { S2 width; S2 height; }; +typedef def_struct(Extent2_S4) { S4 width; S4 height; }; +typedef def_struct(V2_S2) { S2 x; S2 y; }; +typedef def_struct(V2_S4) { S4 x; S4 y; }; +typedef def_struct(V3_S2) { S2 x; S2 y; S2 z; S2 pad; }; +typedef def_struct(V3_S4) { S4 x; S4 y; S4 z; S4 pad; }; +typedef def_struct(R2_S2) { V2_S2 p0; V2_S2 p1; }; +typedef def_struct(R2_S4) { V2_S4 p0; V2_S4 p1; }; -typedef def_struct(Rect_S16) { S16 x; S16 y; S16 width; S16 height; }; -typedef def_struct(Rect_S32) { S32 x; S32 y; S32 width; S32 height; }; +typedef def_struct(Rect_S2) { S2 x; S2 y; S2 width; S2 height; }; +typedef def_struct(Rect_S4) { S4 x; S4 y; S4 width; S4 height; }; -#define vec_2s16(x, y) (Vec_2S16){ x, y } +typedef def_struct(M3_S2) { A3A3_S2 m; A3_S4 t; }; + +#define v2_s2(x, y) (V2_S2){ x, y } diff --git a/code/duffle/memory.h b/code/duffle/memory.h index e0157dc..27dcc38 100644 --- a/code/duffle/memory.h +++ b/code/duffle/memory.h @@ -3,25 +3,25 @@ # include "dsl.h" #endif -inline SSIZE align_pow2(SSIZE x, SSIZE b) { +inline U4 align_pow2(U4 x, U4 b) { assert(b != 0); assert((b & (b - 1)) == 0); // Check power of 2 return ((x + b - 1) & (~(b - 1))); } -#define align_struct(type_width) ((SSIZE)(((type_width) + 3) & ~3)) +#define align_struct(type_width) ((U4)(((type_width) + 3) & ~3)) #define assert_bounds(point, start, end) do { \ - SSIZE pos_point = cast(SSIZE, point); \ - SSIZE pos_start = cast(SSIZE, start); \ - SSIZE pos_end = cast(SSIZE, end); \ - assert(pos_start <= pos_point); \ - assert(pos_point <= pos_end); \ + U4 pos_point = cast(U4, point); \ + U4 pos_start = cast(U4, start); \ + U4 pos_end = cast(U4, end); \ + assert(pos_start <= pos_point); \ + assert(pos_point <= pos_end); \ } while(0) -// void* memory_copy (void* restrict dest, void const* restrict src, USIZE length); -// void* memory_copy_overlapping(void* restrict dest, void const* restrict src, USIZE length); -// B32 memory_zero (void* dest, USIZE length); +void* memory_copy (void* restrict dest, void const* restrict src, U4 length) __asm__("memcpy"); +void* memory_copy_overlapping(void* restrict dest, void const* restrict src, U4 length); +B4 memory_zero (void* dest, U4 length); #define check_nil(nil, p) ((p) == 0 || (p) == nil) #define set_nil(nil, p) ((p) = nil) @@ -43,7 +43,7 @@ inline SSIZE align_pow2(SSIZE x, SSIZE b) { #define sll_queue_push_n(f, l, n, next) sll_queue_push_nz(0, f, l, n, next) #pragma region Allocator Interface -typedef def_enum(U32, AllocatorOp) { +typedef def_enum(U4, AllocatorOp) { AllocatorOp_Alloc_NoZero = 0, // If Alloc exist, so must No_Zero AllocatorOp_Alloc, AllocatorOp_Free, @@ -55,7 +55,7 @@ typedef def_enum(U32, AllocatorOp) { AllocatorOp_SavePoint, AllocatorOp_Query, // Must always be implemented }; -typedef def_enum(U32, AllocatorQueryFlags) { +typedef def_enum(U4, AllocatorQueryFlags) { AllocatorQuery_Alloc = (1 << 0), AllocatorQuery_Free = (1 << 1), // Wipe the allocator's state @@ -72,14 +72,14 @@ typedef struct AllocatorProc_Out AllocatorProc_Out; typedef void def_proc(AllocatorProc) (AllocatorProc_In In, AllocatorProc_Out* Out); typedef def_struct(AllocatorSP) { AllocatorProc* type_sig; - SSIZE slot; + U4 slot; }; struct AllocatorProc_In { void* data; - SSIZE requested_size; - SSIZE alignment; + U4 requested_size; + U4 alignment; union { - Slice_BYTE old_allocation; + Slice_B1 old_allocation; AllocatorSP save_point; }; AllocatorOp op; @@ -87,28 +87,28 @@ struct AllocatorProc_In { }; struct AllocatorProc_Out { union { - Slice_BYTE allocation; + Slice_B1 allocation; AllocatorSP save_point; }; AllocatorQueryFlags features; - SSIZE left; // Contiguous memory left - SSIZE max_alloc; - SSIZE min_alloc; - B32 continuity_break; // Whether this allocation broke continuity with the previous (address space wise) + U4 left; // Contiguous memory left + U4 max_alloc; + U4 min_alloc; + B4 continuity_break; // Whether this allocation broke continuity with the previous (address space wise) byte_pad(4); }; typedef def_struct(AllocatorInfo) { AllocatorProc* proc; void* data; }; -static_assert(size_of(AllocatorSP) <= size_of(Slice_BYTE)); +static_assert(size_of(AllocatorSP) <= size_of(Slice_B1)); typedef def_struct(AllocatorQueryInfo) { AllocatorSP save_point; AllocatorQueryFlags features; - SSIZE left; // Contiguous memory left - SSIZE max_alloc; - SSIZE min_alloc; - B32 continuity_break; // Whether this allocation broke continuity with the previous (address space wise) + U4 left; // Contiguous memory left + U4 max_alloc; + U4 min_alloc; + B4 continuity_break; // Whether this allocation broke continuity with the previous (address space wise) byte_pad(4); }; static_assert(size_of(AllocatorProc_Out) == size_of(AllocatorQueryInfo)); @@ -117,20 +117,20 @@ static_assert(size_of(AllocatorProc_Out) == size_of(AllocatorQueryInfo)); AllocatorQueryInfo allocator_query(AllocatorInfo ainfo); -void mem_free (AllocatorInfo ainfo, Slice_BYTE mem); +void mem_free (AllocatorInfo ainfo, Slice_B1 mem); void mem_reset (AllocatorInfo ainfo); void mem_rewind (AllocatorInfo ainfo, AllocatorSP save_point); AllocatorSP mem_save_point(AllocatorInfo ainfo); -typedef def_struct(Opts_mem_alloc) { SSIZE alignment; B32 no_zero; byte_pad(4); }; -typedef def_struct(Opts_mem_grow) { SSIZE alignment; B32 no_zero; byte_pad(4); }; -typedef def_struct(Opts_mem_shrink) { SSIZE alignment; }; -typedef def_struct(Opts_mem_resize) { SSIZE alignment; B32 no_zero; byte_pad(4); }; +typedef def_struct(Opts_mem_alloc) { U4 alignment; B4 no_zero; byte_pad(4); }; +typedef def_struct(Opts_mem_grow) { U4 alignment; B4 no_zero; byte_pad(4); }; +typedef def_struct(Opts_mem_shrink) { U4 alignment; }; +typedef def_struct(Opts_mem_resize) { U4 alignment; B4 no_zero; byte_pad(4); }; -Slice_BYTE mem__alloc (AllocatorInfo ainfo, SSIZE size, Opts_mem_alloc* opts); -Slice_BYTE mem__grow (AllocatorInfo ainfo, Slice_BYTE mem, SSIZE size, Opts_mem_grow* opts); -Slice_BYTE mem__resize(AllocatorInfo ainfo, Slice_BYTE mem, SSIZE size, Opts_mem_resize* opts); -Slice_BYTE mem__shrink(AllocatorInfo ainfo, Slice_BYTE mem, SSIZE size, Opts_mem_shrink* opts); +Slice_B1 mem__alloc (AllocatorInfo ainfo, U4 size, Opts_mem_alloc* opts); +Slice_B1 mem__grow (AllocatorInfo ainfo, Slice_B1 mem, U4 size, Opts_mem_grow* opts); +Slice_B1 mem__resize(AllocatorInfo ainfo, Slice_B1 mem, U4 size, Opts_mem_resize* opts); +Slice_B1 mem__shrink(AllocatorInfo ainfo, Slice_B1 mem, U4 size, Opts_mem_shrink* opts); #define mem_alloc(ainfo, size, ...) mem__alloc (ainfo, size, opt_args(Opts_mem_alloc, __VA_ARGS__)) #define mem_grow(ainfo, mem, size, ...) mem__grow (ainfo, mem, size, opt_args(Opts_mem_grow, __VA_ARGS__)) diff --git a/code/factorial/factorial.s b/code/factorial/factorial.s index 6c243b3..9d26dad 100644 --- a/code/factorial/factorial.s +++ b/code/factorial/factorial.s @@ -58,6 +58,7 @@ main: idle: jump idle :: nop + ; args: ; num: rarg_0 .func factorial @@ -73,7 +74,7 @@ sum equ rtmp_3 li sum, 0 li id_product, 0 loop_prod: branch_gt_equal id_product, id_term, break_loop_prod :: nop - add_s sum, sum, term + add_s sum, sum, term add_si id_product, id_product, 1 jump loop_prod :: nop :: break_loop_prod: move term, sum diff --git a/code/graphics_hello_psyq/hello_gpu.c b/code/graphics_hello_psyq/hello_gpu.c index c29b39d..892e595 100644 --- a/code/graphics_hello_psyq/hello_gpu.c +++ b/code/graphics_hello_psyq/hello_gpu.c @@ -10,104 +10,150 @@ #include "duffle/gp.h" #include "hello_gpu.h" -typedef def_farray(Vec_2S16, 3); +typedef def_farray(V2_S2, 3); typedef def_struct(TriFlat) { - U32 tag; + U4 tag; RGB8 color; - BYTE code; + B1 code; union { struct { - Vec_2S16 p0; - Vec_2S16 p1; - Vec_2S16 p2; + V2_S2 p0; + V2_S2 p1; + V2_S2 p2; }; - A3_Vec_2S16 points; + A3_V2_S2 points; }; }; -typedef def_farray(Vec_2S16, 4); +typedef def_farray(V2_S2, 4); typedef def_struct(QuadFlat) { - U32 tag; + U4 tag; RGB8 color; - BYTE code; + B1 code; union { struct { - Vec_2S16 p0; - Vec_2S16 p1; - Vec_2S16 p2; - Vec_2S16 p3; + V2_S2 p0; + V2_S2 p1; + V2_S2 p2; + V2_S2 p3; }; - A4_Vec_2S16 points; + A4_V2_S2 points; }; }; typedef def_struct(QuadGouraud) { - U32 tag; RGB8 c0; BYTE code; - Vec_2S16 p0; RGB8 c1; BYTE pad1; - Vec_2S16 p1; RGB8 c2; BYTE pad2; - Vec_2S16 p2; RGB8 c3; BYTE pad3; - Vec_2S16 p3; + U4 tag; RGB8 c0; B1 code; + V2_S2 p0; RGB8 c1; B1 pad1; + V2_S2 p1; RGB8 c2; B1 pad2; + V2_S2 p2; RGB8 c3; B1 pad3; + V2_S2 p3; }; typedef def_struct(Tile) { - U32 tag; - RGB8 color; - BYTE code; - Rect_S16 rect; + U4 tag; + RGB8 color; + B1 code; + Rect_S2 rect; }; #define PrimitiveBuff_Len 2048 -#define OrderingTbl_Len 32 +#define OrderingTbl_Len 256 -typedef U32 OrderingTable_Buffer[OrderingTbl_Len]; +typedef U4 OrderingTable_Buffer[OrderingTbl_Len]; typedef def_farray(OrderingTable_Buffer, 2); -typedef BYTE PrimitiveBuffer[PrimitiveBuff_Len]; +typedef B1 PrimitiveBuffer[PrimitiveBuff_Len]; typedef def_farray(PrimitiveBuffer, 2); typedef def_struct(PrimitiveArena) { A2_PrimitiveBuffer buf; - SSIZE used; + U4 used; }; +#define Cube_num_verts 8 +#define Cube_num_faces 12 +typedef def_farray(V3_S2, Cube_num_verts); +typedef def_farray(V3_S2, Cube_num_faces); + +void cube128_init(A8_V3_S2* verts, A12_V3_S2* faces) { + memory_copy(verts, & (A8_V3_S2) { + { -128, -128, -128 }, + { 128, -128, -128 }, + { 128, -128, 128 }, + { -128, -128, 128 }, + { -128, 128, -128 }, + { 128, 128, -128 }, + { 128, 128, 128 }, + { -128, 128, 128 } + }, + size_of(A8_V3_S2) + ); + memory_copy(faces, & (A12_V3_S2) { + { 0, 3, 2 }, // top + { 0, 2, 1 }, // top + { 4, 0, 1 }, // front + { 4, 1, 5 }, // front + { 7, 4, 5 }, // bottom + { 7, 5, 6 }, // bottom + { 5, 1, 2 }, // right + { 5, 2, 6 }, // right + { 2, 3, 7 }, // back + { 2, 7, 6 }, // back + { 0, 4, 7 }, // left + { 0, 7, 3 } // left + }, + size_of(A12_V3_S2) + ); + return; +} + typedef def_struct(SMemory) { DoubleBuffer screen_buf; A2_OrderingTable_Buffer ordering_tbl; PrimitiveArena primitives; - S16 active_buf_id; + S2 active_buf_id; + + V3_S2 rotation; + V3_S4 translation; + V3_S4 scale; + + M3_S2 tform_world; + + A8_V3_S2 cube_verts; + A12_V3_S2 cube_faces; }; global SMemory static_mem; extern SMemory static_mem; -BYTE* prim__alloc(SSIZE type_width, dbg_args(Str8 type_name)) { - gknown PrimitiveArena* pa = & static_mem.primitives; - gknown BYTE* buf = (BYTE*) static_mem.primitives.buf[static_mem.active_buf_id]; +B1* prim__alloc(U4 type_width, Str8 type_name) { + gknown PrimitiveArena* pa = & static_mem.primitives; + gknown B1* buf = (B1*) r_(static_mem.primitives.buf)[static_mem.active_buf_id]; assert(pa->used + type_width < PrimitiveBuff_Len); - BYTE* next = buf + pa->used; - pa->used += type_width; + B1* next = buf + pa->used; + pa->used += type_width; return next; } #define prim_alloc(type) (type*)prim__alloc(size_of(type), txt( stringify(type))) -void gp_screen_init_c11(DoubleBuffer* screen_buf, S16* active_buf_id) +void gp_screen_init_c11(DoubleBuffer* screen_buf, S2* active_buf_id) { reset_graph(0); set_display_enabled(1); // gp_DisplayEnabled // Set the current initial buffer - * active_buf_id = 0; + active_buf_id[0] = 0; // Just setting env data, not interacting with console hw. // First buffer area - displayenv_init(& screen_buf->display[0], 0, 0, ScreenRes_X, ScreenRes_Y); - drawenv_init (& screen_buf->draw [0], 0, ScreenRes_Y, ScreenRes_X, ScreenRes_Y); + displayenv_init(& r_(screen_buf->display)[0], 0, 0, ScreenRes_X, ScreenRes_Y); + drawenv_init (& r_(screen_buf->draw )[0], 0, ScreenRes_Y, ScreenRes_X, ScreenRes_Y); // Second buffer area - displayenv_init(& screen_buf->display[1], 0, ScreenRes_Y, ScreenRes_X, ScreenRes_Y); - drawenv_init (& screen_buf->draw [1], 0, 0, ScreenRes_X, ScreenRes_Y); + displayenv_init(& r_(screen_buf->display)[1], 0, ScreenRes_Y, ScreenRes_X, ScreenRes_Y); + drawenv_init (& r_(screen_buf->draw )[1], 0, 0, ScreenRes_X, ScreenRes_Y); // Set the back/drawing buffer screen_buf->draw[0].enable_auto_clear = true; screen_buf->draw[1].enable_auto_clear = true; // Set the background clear color screen_buf->draw[0].initial_bg_color = rgba8( .r = 63, .g = 0, .b = 127 ); screen_buf->draw[1].initial_bg_color = rgba8( .r = 127, .g = 63, .b = 0 ); - displayenv_put(& screen_buf->display[* active_buf_id]); - drawenv_put (& screen_buf->draw [* active_buf_id]); + displayenv_put(& r_(screen_buf->display)[ active_buf_id[0] ]); + drawenv_put (& r_(screen_buf->draw )[ active_buf_id[0] ]); // Initialize and setup the GTE geometry offsets geom_init(); @@ -115,11 +161,11 @@ void gp_screen_init_c11(DoubleBuffer* screen_buf, S16* active_buf_id) geom_set_screen(ScreenZ); } -void gp_display_frame(DoubleBuffer* screen_buf, S16* active_buf_id, U32* ordering_buf, PrimitiveArena* pa) { +void gp_display_frame(DoubleBuffer* screen_buf, S2* active_buf_id, U4* ordering_buf, PrimitiveArena* pa) { draw_sync(0); vsync(0); - displayenv_put(& screen_buf->display[* active_buf_id]); - drawenv_put (& screen_buf->draw [* active_buf_id]); + displayenv_put(& r_(screen_buf->display)[active_buf_id[0] ]); + drawenv_put (& r_(screen_buf->draw) [active_buf_id[0] ]); { draw_orderingtbl(ordering_buf + OrderingTbl_Len - 1); pa->used = 0; @@ -130,10 +176,24 @@ void gp_display_frame(DoubleBuffer* screen_buf, S16* active_buf_id, U32* orderin void render(void) { } -void update(PrimitiveArena* pa, U32* ordering_buf) { - +void update(PrimitiveArena* pa, U4* ordering_buf) +{ orderingtbl_clear_reverse(ordering_buf, OrderingTbl_Len); + m3s2_rotation (& static_mem.rotation, & static_mem.tform_world); + m3s2_translation(& static_mem.tform_world, & static_mem.translation); + m3s2_scale (& static_mem.tform_world, & static_mem.scale); + + gte_matrix_set_rotation (& static_mem.tform_world); + gte_matrix_set_translation(& static_mem.tform_world); + + TriFlat* tri = prim_alloc(TriFlat); set_tri_flat(tri); + tri->color = rgba8(255, 0, 0); + + S4 otz = 0; + // otz += vec_3s16_rtp(& ) + +#if 0 Tile* tile = prim_alloc(Tile); set_tile(tile); tile->rect = (Rect_S16){ 82, 32, 64, 64 }; tile->color = (RGB8){ 0, 255, 0}; @@ -163,17 +223,19 @@ void update(PrimitiveArena* pa, U32* ordering_buf) { quadf->p3 = vec_2s16(220 + 15, 80 + 9); quadf->color = rgba8(22, 22, 22); orderingtbl_add_primitive(ordering_buf, quadf); +#endif } int main(void) { static_mem.primitives.used = 0; + cube128_init(& static_mem.cube_verts, & static_mem.cube_faces); gknown gp_screen_init(); // gp_screen_init_c11(& static_mem.screen_buf, & static_mem.active_screen_buf); while (1) { - gknown S16* active_buf_id = & static_mem.active_buf_id; - gknown U32* ordering_buf = static_mem.ordering_tbl[* active_buf_id]; + gknown S2* active_buf_id = & static_mem.active_buf_id; + gknown U4* ordering_buf = r_(static_mem.ordering_tbl)[active_buf_id[0]]; gknown PrimitiveArena* pa = & static_mem.primitives; update(pa, ordering_buf); render(); diff --git a/code/graphics_hello_psyq/hello_gpu.h b/code/graphics_hello_psyq/hello_gpu.h index 95e938e..f27bca7 100644 --- a/code/graphics_hello_psyq/hello_gpu.h +++ b/code/graphics_hello_psyq/hello_gpu.h @@ -5,25 +5,25 @@ # include "duffle/gp.h" #endif -typedef def_struct(DrawEnv_Packed) { U32 tag; U32 code[15]; }; +typedef def_struct(DrawEnv_Packed) { U4 tag; U4 code[15]; }; typedef def_struct(DrawEnv) { - Rect_S16 clip_area; - A2_S16 drawing_offset; - Rect_S16 texture_window; - S16 texture_page; - BYTE flag_dither; - BYTE flag_draw_on_display; - BYTE enable_auto_clear; - RGB8 initial_bg_color; + Rect_S2 clip_area; + A2_S2 drawing_offset; + Rect_S2 texture_window; + S2 texture_page; + B1 flag_dither; + B1 flag_draw_on_display; + B1 enable_auto_clear; + RGB8 initial_bg_color; DrawEnv_Packed dr_env; // reserved }; typedef def_struct(DisplayEnv) { - Rect_S16 display_area; - Rect_S16 screen; - BYTE vinterlace; - BYTE color24; - BYTE pad0; - BYTE pad1; + Rect_S2 display_area; + Rect_S2 screen; + B1 vinterlace; + B1 color24; + B1 pad0; + B1 pad1; }; typedef def_farray(DrawEnv, 2); typedef def_farray(DisplayEnv, 2); @@ -38,51 +38,49 @@ typedef def_struct(DoubleBuffer) { #define ScreenRes_CenterX (ScreenRes_X >> 1) #define ScreenRes_CenterY (ScreenRes_Y >> 1) - -DisplayEnv* displayenv_init(DisplayEnv* env, S32 x, S32 y, S32 w, S32 h) __asm__("SetDefDispEnv"); -DrawEnv* drawenv_init (DrawEnv* env, S32 x, S32 y, S32 w, S32 h) __asm__("SetDefDrawEnv"); +DisplayEnv* displayenv_init(DisplayEnv* env, S4 x, S4 y, S4 w, S4 h) __asm__("SetDefDispEnv"); +DrawEnv* drawenv_init (DrawEnv* env, S4 x, S4 y, S4 w, S4 h) __asm__("SetDefDrawEnv"); DisplayEnv* displayenv_put(DisplayEnv* env) __asm__("PutDispEnv"); DrawEnv* drawenv_put (DrawEnv* env) __asm__("PutDrawEnv"); -U32 geom_init(void) __asm__("InitGeom"); -void geom_set_offset(U32 x, U32 y) __asm__("SetGeomOffset"); -void geom_set_screen(U32 h) __asm__("SetGeomScreen"); +U4 geom_init(void) __asm__("InitGeom"); +void geom_set_offset(U4 x, U4 y) __asm__("SetGeomOffset"); +void geom_set_screen(U4 h) __asm__("SetGeomScreen"); -U32* orderingtbl_clear_reverse(U32* ot, SSIZE len) __asm__("ClearOTagR"); +U4* orderingtbl_clear_reverse(U4* ot, U4 len) __asm__("ClearOTagR"); -U32 reset_graph(U32 mode) __asm__("ResetGraph"); -void set_display_enabled(U32 mask) __asm__("SetDispMask"); +U4 reset_graph(U4 mode) __asm__("ResetGraph"); +void set_display_enabled(U4 mask) __asm__("SetDispMask"); -U32 draw_sync(U32 mode) __asm__("DrawSync"); -U32 vsync(U32 mode) __asm("VSync"); +U4 draw_sync(U4 mode) __asm__("DrawSync"); +U4 vsync(U4 mode) __asm__("VSync"); -void draw_orderingtbl(U32* buf) __asm__("DrawOTag"); +void draw_orderingtbl(U4* buf) __asm__("DrawOTag"); typedef def_struct(PolyTag) { - U32 addr: 24; - U32 len: 8; + U4 addr: 24; + U4 len: 8; RGB8 color; - BYTE code; + B1 code; }; /* * Primitive Handling Macros */ -#define set_len( p, _len) (((PolyTag*)(p))->len = (BYTE)(_len)) -#define set_addr(p, _addr) (((PolyTag*)(p))->addr = (U32 )(_addr)) -#define set_code(p, _code) (((PolyTag*)(p))->code = (BYTE)(_code)) -#define get_len(p) (BYTE)(((PolyTag*)(p))->len) -#define get_code(p) (BYTE)(((PolyTag*)(p))->code) -#define get_addr(p) (U32 )(((PolyTag*)(p))->addr) +#define set_len( p, _len) (((PolyTag*)(p))->len = (B1)(_len)) +#define set_addr(p, _addr) (((PolyTag*)(p))->addr = (U4)(_addr)) +#define set_code(p, _code) (((PolyTag*)(p))->code = (B1)(_code)) + +#define get_len(p) (B1)(((PolyTag*)(p))->len) +#define get_code(p) (B1)(((PolyTag*)(p))->code) +#define get_addr(p) (U4)(((PolyTag*)(p))->addr) #define orderingtbl_add_primitive(ot, p) set_addr(p, get_addr(ot)), set_addr(ot, p) #define orderingtbl_add_primitives(ot, p0, p1) set_addr(p1, get_addr(ot)), set_addr(ot, p0) -/* Primitive Lentgh Code */ -/*-------------------------------------------------------------------- */ -/* */ +/* Primitive Length Code */ #define set_tri_flat(p) set_len(p, 4), set_code(p, 0x20) // #define setPolyFT3(p) set_len(p, 7), set_code(p, 0x24) // #define setPolyG3(p) set_len(p, 6), set_code(p, 0x30) @@ -106,3 +104,16 @@ typedef def_struct(PolyTag) { // #define setLineG3(p) set_len(p, 7), set_code(p, 0x58),(p)->pad = 0x55555555, (p)->p2 = 0 // #define setLineF4(p) set_len(p, 6), set_code(p, 0x4c),(p)->pad = 0x55555555 // #define setLineG4(p) set_len(p, 9), set_code(p, 0x5c),(p)->pad = 0x55555555, (p)->p2 = 0, (p)->p3 = 0 + +/* + Linear Algebra +*/ + +M3_S2* m3s2_rotation (V3_S2* vec, M3_S2* mat) __asm__("RotMatrix"); +M3_S2* m3s2_translation(M3_S2* mat, V3_S4* vec) __asm__("TransMatrix"); +M3_S2* m3s2_scale (M3_S2* mat, V3_S4* vec) __asm__("ScaleMatrix"); + +S4 v3s2_rtp(V3_S2* vec, A2_S2 xy, A2_S2* pp, S4* flag) __asm__("RotTransPers"); // Rotation, Translate, Perspective + +void gte_matrix_set_rotation (M3_S2* mat) __asm__("SetRotMatrix"); +void gte_matrix_set_translation(M3_S2* mat) __asm__("SetTransMatrix");