diff --git a/src/gu/Makefile.am b/src/gu/Makefile.am index f0389db4..80fd80e7 100644 --- a/src/gu/Makefile.am +++ b/src/gu/Makefile.am @@ -12,10 +12,11 @@ CFLAGS = @PSPSDK_CFLAGS@ libpspguincludedir = @PSPSDK_INCLUDEDIR@ libpspguinclude_HEADERS = pspgu.h -lib_LIBRARIES = libpspgu.a +lib_LIBRARIES = libpspgu.a libpspgud.a noinst_HEADERS = guInternal.h +# Regular library (no debug assertions) libpspgu_a_SOURCES = \ guInternal.c \ sceGuAlphaFunc.c \ @@ -112,3 +113,7 @@ libpspgu_a_SOURCES = \ sceGuViewport.c \ vram.c +# Debug library (with debug assertions) +libpspgud_a_SOURCES = $(libpspgu_a_SOURCES) +libpspgud_a_CFLAGS = $(CFLAGS) -DGU_DEBUG + diff --git a/src/gu/guInternal.c b/src/gu/guInternal.c index 1c70f45e..2b88e79f 100644 --- a/src/gu/guInternal.c +++ b/src/gu/guInternal.c @@ -19,5 +19,5 @@ int gu_display_on; int gu_call_mode; int gu_states; GuDrawBuffer gu_draw_buffer; -unsigned int* gu_object_stack[32]; +unsigned int* gu_object_stack[GU_OBJECT_STACK_SIZE]; int gu_object_stack_depth; diff --git a/src/gu/guInternal.h b/src/gu/guInternal.h index 019b5aa7..a8942537 100644 --- a/src/gu/guInternal.h +++ b/src/gu/guInternal.h @@ -11,6 +11,13 @@ #include "pspgu.h" +#ifdef GU_DEBUG +#include +#include +#endif + +#define GU_OBJECT_STACK_SIZE 32 + typedef void (*GuCallback)(int); typedef struct diff --git a/src/gu/sceGuAlphaFunc.c b/src/gu/sceGuAlphaFunc.c index 4baee0b0..4d6b1b38 100644 --- a/src/gu/sceGuAlphaFunc.c +++ b/src/gu/sceGuAlphaFunc.c @@ -10,6 +10,12 @@ void sceGuAlphaFunc(int func, int value, int mask) { +#ifdef GU_DEBUG + printf("sceGuAlphaFunc(%d, %d, %d);\n", func, value, mask); + assert(gu_init && "GU not initialized"); + assert(func >= GU_NEVER && func <= GU_GEQUAL && "Invalid alpha function"); +#endif + int arg = func | ((value & 0xff) << 8) | ((mask & 0xff) << 16); sendCommandi(ALPHA_TEST, arg); } diff --git a/src/gu/sceGuAmbient.c b/src/gu/sceGuAmbient.c index 2211201c..633e7310 100644 --- a/src/gu/sceGuAmbient.c +++ b/src/gu/sceGuAmbient.c @@ -10,6 +10,11 @@ void sceGuAmbient(unsigned int color) { +#ifdef GU_DEBUG + printf("sceGuAmbient(%08X);\n", color); + assert(gu_init && "GU not initialized"); +#endif + sendCommandi(AMBIENT_LIGHT_COLOR,(color)); sendCommandi(AMBIENT_LIGHT_ALPHA,(color >> 24)); } diff --git a/src/gu/sceGuAmbientColor.c b/src/gu/sceGuAmbientColor.c index 54b66d20..2a625098 100644 --- a/src/gu/sceGuAmbientColor.c +++ b/src/gu/sceGuAmbientColor.c @@ -10,6 +10,11 @@ void sceGuAmbientColor(unsigned int color) { +#ifdef GU_DEBUG + printf("sceGuAmbientColor(0x%08X);\n", color); + assert(gu_init && "GU not initialized"); +#endif + sendCommandi(AMBIENT_COLOR, color); sendCommandi(AMBIENT_ALPHA, color >> 24); } diff --git a/src/gu/sceGuBeginObject.c b/src/gu/sceGuBeginObject.c index 2907b962..0a9aa478 100644 --- a/src/gu/sceGuBeginObject.c +++ b/src/gu/sceGuBeginObject.c @@ -10,6 +10,13 @@ void sceGuBeginObject(int vertex_type, int count, const void *indices, const void *vertices) { +#ifdef GU_DEBUG + printf("sceGuBeginObject(%d, %d, %p, %p);\n", vertex_type, count, indices, vertices); + assert(gu_init && "GU not initialized"); + assert(count > 0 && count <= 255 && (count % 8) == 0 && "Invalid vertex count - must be positive, <= 65535, and multiple of 8"); + assert(gu_object_stack_depth < GU_OBJECT_STACK_SIZE && "Object stack overflow"); +#endif + if (vertex_type) sendCommandi(VERTEX_TYPE, vertex_type); diff --git a/src/gu/sceGuBlendFunc.c b/src/gu/sceGuBlendFunc.c index af66d5d8..55baedbe 100644 --- a/src/gu/sceGuBlendFunc.c +++ b/src/gu/sceGuBlendFunc.c @@ -10,6 +10,14 @@ void sceGuBlendFunc(int op, int src, int dest, unsigned int srcfix, unsigned int destfix) { +#ifdef GU_DEBUG + printf("sceGuBlendFunc(%d, %d, %d, 0x%08X, 0x%08X);\n", op, src, dest, srcfix, destfix); + assert(gu_init && "GU not initialized"); + assert(op >= GU_ADD && op <= GU_ABS && "Invalid blend operation"); + assert(src >= GU_OTHER_COLOR && src <= GU_FIX && "Invalid source blend factor"); + assert(dest >= GU_OTHER_COLOR && dest <= GU_FIX && "Invalid destination blend factor"); +#endif + sendCommandi(BLEND_MODE, src | (dest << 4) | (op << 8)); sendCommandi(BLEND_FIXED_A, srcfix); sendCommandi(BLEND_FIXED_B, destfix); diff --git a/src/gu/sceGuBoneMatrix.c b/src/gu/sceGuBoneMatrix.c index dc3ec15a..770a4ddd 100644 --- a/src/gu/sceGuBoneMatrix.c +++ b/src/gu/sceGuBoneMatrix.c @@ -10,6 +10,12 @@ void sceGuBoneMatrix(unsigned int index, const ScePspFMatrix4 *matrix) { +#ifdef GU_DEBUG + printf("sceGuBoneMatrix(%d, %p);\n", index, matrix); + assert(gu_init && "GU not initialized"); + assert(index < 8 && "Invalid bone matrix index"); +#endif + unsigned int offset = ((index << 1) + index) << 2; // 3*4 matrix unsigned int i, j; const float *fmatrix = (const float *)matrix; diff --git a/src/gu/sceGuCallList.c b/src/gu/sceGuCallList.c index 7633c1b2..ff9268e9 100644 --- a/src/gu/sceGuCallList.c +++ b/src/gu/sceGuCallList.c @@ -10,6 +10,11 @@ int sceGuCallList(const void *list) { +#ifdef GU_DEBUG + printf("sceGuCallList(%p);\n", list); + assert(gu_init && "GU not initialized"); +#endif + int res; unsigned int list_addr = (unsigned int)list; diff --git a/src/gu/sceGuCallMode.c b/src/gu/sceGuCallMode.c index 7721d5c2..1f48d7d1 100644 --- a/src/gu/sceGuCallMode.c +++ b/src/gu/sceGuCallMode.c @@ -10,5 +10,11 @@ void sceGuCallMode(int mode) { +#ifdef GU_DEBUG + printf("sceGuCallMode(%d);\n", mode); + assert(gu_init && "GU not initialized"); + assert(mode >= GU_CALL_NORMAL && mode <= GU_CALL_SIGNAL && "Invalid call mode"); +#endif + gu_call_mode = mode; } diff --git a/src/gu/sceGuClear.c b/src/gu/sceGuClear.c index 4ae8190c..dd1bd4c2 100644 --- a/src/gu/sceGuClear.c +++ b/src/gu/sceGuClear.c @@ -10,6 +10,13 @@ void sceGuClear(int flags) { +#ifdef GU_DEBUG + printf("sceGuClear(%d);\n", flags); + assert(gu_init && "GU not initialized"); + assert(flags >= 0 && "Invalid clear flags"); + assert((flags & ~(GU_COLOR_BUFFER_BIT | GU_STENCIL_BUFFER_BIT | GU_DEPTH_BUFFER_BIT | GU_FAST_CLEAR_BIT)) == 0 && "Invalid clear flags"); +#endif + GuContext *context = &gu_contexts[gu_curr_context]; unsigned int filter; struct Vertex diff --git a/src/gu/sceGuClearColor.c b/src/gu/sceGuClearColor.c index 1655c55e..b7e36a80 100644 --- a/src/gu/sceGuClearColor.c +++ b/src/gu/sceGuClearColor.c @@ -10,6 +10,11 @@ void sceGuClearColor(unsigned int color) { +#ifdef GU_DEBUG + printf("sceGuClearColor(0x%08X);\n", color); + assert(gu_init && "GU not initialized"); +#endif + GuContext* context = &gu_contexts[gu_curr_context]; context->clear_color = color; } diff --git a/src/gu/sceGuClearDepth.c b/src/gu/sceGuClearDepth.c index 3b61831e..843e4b78 100644 --- a/src/gu/sceGuClearDepth.c +++ b/src/gu/sceGuClearDepth.c @@ -10,6 +10,12 @@ void sceGuClearDepth(unsigned int depth) { +#ifdef GU_DEBUG + printf("sceGuClearDepth(%u);\n", depth); + assert(gu_init && "GU not initialized"); + assert(depth <= 65535 && "Invalid depth value"); +#endif + GuContext* context = &gu_contexts[gu_curr_context]; context->clear_depth = depth; } diff --git a/src/gu/sceGuClearStencil.c b/src/gu/sceGuClearStencil.c index 25272898..a871f2cd 100644 --- a/src/gu/sceGuClearStencil.c +++ b/src/gu/sceGuClearStencil.c @@ -10,6 +10,29 @@ void sceGuClearStencil(unsigned int stencil) { +#ifdef GU_DEBUG + printf("sceGuClearStencil(%u);\n", stencil); + assert(gu_init && "GU not initialized"); + switch (gu_draw_buffer.pixel_size) + { + case GU_PSM_8888: + assert(stencil <= 255 && "Invalid stencil value for GU_PSM_8888"); + break; + case GU_PSM_4444: + assert(stencil <= 15 && "Invalid stencil value for GU_PSM_4444"); + break; + case GU_PSM_5551: + assert(stencil <= 1 && "Invalid stencil value for GU_PSM_5551"); + break; + case GU_PSM_5650: + assert(0 && "Stencil not supported for GU_PSM_5650"); + break; + default: + assert(0 && "Invalid pixel format"); + break; + } +#endif + GuContext* context = &gu_contexts[gu_curr_context]; context->clear_stencil = stencil; } diff --git a/src/gu/sceGuClutLoad.c b/src/gu/sceGuClutLoad.c index 7748891e..1509b263 100644 --- a/src/gu/sceGuClutLoad.c +++ b/src/gu/sceGuClutLoad.c @@ -10,6 +10,12 @@ void sceGuClutLoad(int num_blocks, const void *cbp) { +#ifdef GU_DEBUG + printf("sceGuClutLoad(%d, %p);\n", num_blocks, cbp); + assert(gu_init && "GU not initialized"); + assert(num_blocks > 0 && num_blocks <= 65535 && "Invalid number of CLUT blocks"); +#endif + sendCommandi(CLUT_BUF_PTR, ((unsigned int)cbp)); sendCommandi(CLUT_BUF_WIDTH, (((unsigned int)cbp) >> 8) & 0xf0000); sendCommandi(CLUT_LOAD, num_blocks); diff --git a/src/gu/sceGuClutMode.c b/src/gu/sceGuClutMode.c index 0fddf3f5..ec512f65 100644 --- a/src/gu/sceGuClutMode.c +++ b/src/gu/sceGuClutMode.c @@ -10,6 +10,12 @@ void sceGuClutMode(unsigned int cpsm, unsigned int shift, unsigned int mask, unsigned int csa) { +#ifdef GU_DEBUG + printf("sceGuClutMode(%08X, %08X, %08X, %08X);\n", cpsm, shift, mask, csa); + assert(gu_init && "GU not initialized"); + assert(cpsm >= GU_PSM_5650 && cpsm <= GU_PSM_8888 && "Invalid CLUT pixel format"); +#endif + unsigned int argument = (cpsm) | (shift << 2) | (mask << 8) | (csa << 16); sendCommandi(CLUT_FORMAT, argument); } diff --git a/src/gu/sceGuColor.c b/src/gu/sceGuColor.c index 946bd39e..ae19dd48 100644 --- a/src/gu/sceGuColor.c +++ b/src/gu/sceGuColor.c @@ -10,5 +10,9 @@ void sceGuColor(unsigned int color) { +#ifdef GU_DEBUG + assert(gu_init && "GU not initialized"); +#endif + sceGuMaterial(GU_AMBIENT | GU_DIFFUSE | GU_SPECULAR, color); } diff --git a/src/gu/sceGuColorFunc.c b/src/gu/sceGuColorFunc.c index 30e8c12a..981779b6 100644 --- a/src/gu/sceGuColorFunc.c +++ b/src/gu/sceGuColorFunc.c @@ -10,6 +10,12 @@ void sceGuColorFunc(int func, unsigned int color, unsigned int mask) { +#ifdef GU_DEBUG + printf("sceGuColorFunc(%d, 0x%08X, 0x%08X);\n", func, color, mask); + assert(gu_init && "GU not initialized"); + assert(func >= GU_NEVER && func <= GU_NOTEQUAL && "Invalid color function"); +#endif + sendCommandi(COLOR_TEST, func & 0x03); sendCommandi(COLOR_REF, color); sendCommandi(COLOR_TESTMASK, mask); diff --git a/src/gu/sceGuColorMaterial.c b/src/gu/sceGuColorMaterial.c index bbb48f7b..1f056227 100644 --- a/src/gu/sceGuColorMaterial.c +++ b/src/gu/sceGuColorMaterial.c @@ -10,5 +10,11 @@ void sceGuColorMaterial(int components) { +#ifdef GU_DEBUG + printf("sceGuColorMaterial(%d);\n", components); + assert(gu_init && "GU not initialized"); + assert((components >= 0) && ((components & ~(GU_AMBIENT | GU_DIFFUSE | GU_SPECULAR)) == 0) && "Invalid material components"); +#endif + sendCommandi(MATERIAL_COLOR, components); } diff --git a/src/gu/sceGuContinue.c b/src/gu/sceGuContinue.c index e6197170..ba016101 100644 --- a/src/gu/sceGuContinue.c +++ b/src/gu/sceGuContinue.c @@ -6,9 +6,13 @@ * Copyright (c) 2005 Jesper Svennevid */ -#include "pspge.h" +#include "guInternal.h" int sceGuContinue(void) { +#ifdef GU_DEBUG + printf("sceGuContinue();\n"); + assert(gu_init && "GU not initialized"); +#endif return sceGeContinue(); } diff --git a/src/gu/sceGuCopyImage.c b/src/gu/sceGuCopyImage.c index 51eef944..ae877b48 100644 --- a/src/gu/sceGuCopyImage.c +++ b/src/gu/sceGuCopyImage.c @@ -10,6 +10,20 @@ void sceGuCopyImage(int psm, int sx, int sy, int width, int height, int srcw, void *src, int dx, int dy, int destw, void *dest) { +#ifdef GU_DEBUG + printf("sceGuCopyImage(%d, %d, %d, %d, %d, %d, %p, %d, %d, %d, %p);\n", psm, sx, sy, width, height, srcw, src, dx, dy, destw, dest); + assert(gu_init && "GU not initialized"); + assert(psm >= GU_PSM_5650 && psm <= GU_PSM_8888 && "Invalid pixel format"); + assert(width > 0 && width <= 1023 && "Invalid width"); + assert(height > 0 && height <= 1023 && "Invalid height"); + assert(srcw > 8 && srcw <= 1024 && (srcw & 0x7) == 0 && "Invalid source width, must be multiple of 8"); + assert(destw > 8 && destw <= 1024 && (destw & 0x7) == 0 && "Invalid destination width, must be multiple of 8"); + assert(sx >= 0 && sx < 1023 && "Invalid source X"); + assert(sy >= 0 && sy < 1023 && "Invalid source Y"); + assert(dx >= 0 && dx < 1023 && "Invalid destination X"); + assert(dy >= 0 && dy < 1023 && "Invalid destination Y"); +#endif + sendCommandi(TRANSFER_SRC, ((unsigned int)src)); sendCommandi(TRANSFER_SRC_W, ((((unsigned int)src) & 0xff000000) >> 8) | srcw); sendCommandi(TRANSFER_SRC_OFFSET, (sy << 10) | sx); diff --git a/src/gu/sceGuDepthBuffer.c b/src/gu/sceGuDepthBuffer.c index 76829d04..79993986 100644 --- a/src/gu/sceGuDepthBuffer.c +++ b/src/gu/sceGuDepthBuffer.c @@ -10,6 +10,13 @@ void sceGuDepthBuffer(void *zbp, int zbw) { +#ifdef GU_DEBUG + printf("sceGuDepthBuffer(%p, %d);\n", zbp, zbw); + assert(gu_init && "GU not initialized"); + assert((zbw & 0x1FFF) == 0 && "Depth buffer pointer must be 8192-byte aligned"); + assert(zbw > 64 && zbw <= 1024 && (zbw & 0x3F) == 0 && "Invalid depth buffer width, must be multiple of 64"); +#endif + sendCommandi(Z_BUF_PTR, ((unsigned int)zbp)); sendCommandi(Z_BUF_WIDTH, ((((unsigned int)zbp) & 0xff000000) >> 8) | zbw); diff --git a/src/gu/sceGuDepthFunc.c b/src/gu/sceGuDepthFunc.c index f410d429..4f55c47c 100644 --- a/src/gu/sceGuDepthFunc.c +++ b/src/gu/sceGuDepthFunc.c @@ -10,5 +10,11 @@ void sceGuDepthFunc(int function) { +#ifdef GU_DEBUG + printf("sceGuDepthFunc(%d);\n", function); + assert(gu_init && "GU not initialized"); + assert(function >= GU_NEVER && function <= GU_GEQUAL && "Invalid depth function"); +#endif + sendCommandi(Z_TEST, function); } diff --git a/src/gu/sceGuDepthMask.c b/src/gu/sceGuDepthMask.c index a4046b60..413ece44 100644 --- a/src/gu/sceGuDepthMask.c +++ b/src/gu/sceGuDepthMask.c @@ -10,5 +10,11 @@ void sceGuDepthMask(int mask) { +#ifdef GU_DEBUG + printf("sceGuDepthMask(%d);\n", mask); + assert(gu_init && "GU not initialized"); + assert((mask == 0 || mask == 1) && "Invalid depth mask"); +#endif + sendCommandi(Z_MASK, mask); } diff --git a/src/gu/sceGuDepthOffset.c b/src/gu/sceGuDepthOffset.c index e794be17..a9336784 100644 --- a/src/gu/sceGuDepthOffset.c +++ b/src/gu/sceGuDepthOffset.c @@ -10,6 +10,11 @@ void sceGuDepthOffset(unsigned int offset) { +#ifdef GU_DEBUG + printf("sceGuDepthOffset(%08X);\n", offset); + assert(gu_init && "GU not initialized"); +#endif + GuContext* context = &gu_contexts[gu_curr_context]; context->depth_offset = offset; diff --git a/src/gu/sceGuDepthRange.c b/src/gu/sceGuDepthRange.c index e5642a22..8a3e2748 100644 --- a/src/gu/sceGuDepthRange.c +++ b/src/gu/sceGuDepthRange.c @@ -10,6 +10,13 @@ void sceGuDepthRange(int near, int far) { +#ifdef GU_DEBUG + printf("sceGuDepthRange(%d, %d);\n", near, far); + assert(gu_init && "GU not initialized"); + assert(near >= 0 && near <= 65535 && "Invalid near plane"); + assert(far >= 0 && far <= 65535 && "Invalid far plane"); +#endif + GuContext *context = &gu_contexts[gu_curr_context]; unsigned int max = (unsigned int)near + (unsigned int)far; diff --git a/src/gu/sceGuDisable.c b/src/gu/sceGuDisable.c index c978e0a6..985d6629 100644 --- a/src/gu/sceGuDisable.c +++ b/src/gu/sceGuDisable.c @@ -10,6 +10,12 @@ void sceGuDisable(int state) { +#ifdef GU_DEBUG + printf("sceGuDisable(%d);\n", state); + assert(gu_init && "GU not initialized"); + assert(state >= 0 && state < GU_MAX_STATUS && "Invalid state"); +#endif + switch (state) { case GU_ALPHA_TEST: diff --git a/src/gu/sceGuDispBuffer.c b/src/gu/sceGuDispBuffer.c index ea6306f3..cd87bd10 100644 --- a/src/gu/sceGuDispBuffer.c +++ b/src/gu/sceGuDispBuffer.c @@ -13,6 +13,13 @@ void sceGuDispBuffer(int width, int height, void *dispbp, int dispbw) { +#ifdef GU_DEBUG + printf("sceGuDispBuffer(%d, %d, %p, %d);\n", width, height, dispbp, dispbw); + assert(gu_init && "GU not initialized"); + assert(((unsigned int)dispbp & 0x1F) == 0 && "Display buffer pointer must be 32-byte aligned"); + assert(dispbw >= 64 && dispbw <= 1024 && (dispbw & 0x3F) == 0 && "Display buffer width must be multiple of 64"); +#endif + gu_draw_buffer.width = width; gu_draw_buffer.height = height; gu_draw_buffer.disp_buffer = dispbp; diff --git a/src/gu/sceGuDisplay.c b/src/gu/sceGuDisplay.c index 90987c4e..b24cdd3a 100644 --- a/src/gu/sceGuDisplay.c +++ b/src/gu/sceGuDisplay.c @@ -13,6 +13,12 @@ int sceGuDisplay(int state) { +#ifdef GU_DEBUG + printf("sceGuDisplay(%d);\n", state); + assert(gu_init && "GU not initialized"); + assert((state == GU_TRUE || state == GU_FALSE) && "Invalid display state"); +#endif + if (state == GU_TRUE) sceDisplaySetFrameBuf((void *)((unsigned int)ge_edram_address + (unsigned int)gu_draw_buffer.disp_buffer), gu_draw_buffer.frame_width, gu_draw_buffer.pixel_size, PSP_DISPLAY_SETBUF_NEXTVSYNC); else diff --git a/src/gu/sceGuDrawArray.c b/src/gu/sceGuDrawArray.c index 2dcdb872..89ef84d2 100644 --- a/src/gu/sceGuDrawArray.c +++ b/src/gu/sceGuDrawArray.c @@ -10,6 +10,13 @@ void sceGuDrawArray(int prim, int vtype, int count, const void *indices, const void *vertices) { +#ifdef GU_DEBUG + printf("sceGuDrawArray(%d, %d, %d, %p, %p);\n", prim, vtype, count, indices, vertices); + assert(gu_init && "GU not initialized"); + assert(count > 0 && count <= 65535 && "Invalid vertex count"); + assert(prim >= GU_POINTS && prim <= GU_SPRITES && "Invalid primitive type"); +#endif + if (vtype) sendCommandi(VERTEX_TYPE, vtype); diff --git a/src/gu/sceGuDrawArrayN.c b/src/gu/sceGuDrawArrayN.c index dd5ecc45..0e5298a4 100644 --- a/src/gu/sceGuDrawArrayN.c +++ b/src/gu/sceGuDrawArrayN.c @@ -10,6 +10,14 @@ void sceGuDrawArrayN(int primitive_type, int vertex_type, int vcount, int primcount, const void *indices, const void *vertices) { +#ifdef GU_DEBUG + printf("sceGuDrawArrayN(%d, %d, %d, %d, %p, %p);\n", primitive_type, vertex_type, vcount, primcount, indices, vertices); + assert(gu_init && "GU not initialized"); + assert(vcount > 0 && vcount <= 65535 && "Invalid vertex count"); + assert(primcount > 0 && primcount <= 65535 && "Invalid primitive count"); + assert(primitive_type >= GU_POINTS && primitive_type <= GU_TRIANGLE_FAN && "Invalid primitive type"); +#endif + if (vertex_type) sendCommandi(VERTEX_TYPE, vertex_type); diff --git a/src/gu/sceGuDrawBezier.c b/src/gu/sceGuDrawBezier.c index 0c32f51a..ac479c91 100644 --- a/src/gu/sceGuDrawBezier.c +++ b/src/gu/sceGuDrawBezier.c @@ -10,6 +10,13 @@ void sceGuDrawBezier(int vertex_type, int ucount, int vcount, const void *indices, const void *vertices) { +#ifdef GU_DEBUG + printf("sceGuDrawBezier(%d, %d, %d, %p, %p);\n", vertex_type, ucount, vcount, indices, vertices); + assert(gu_init && "GU not initialized"); + assert(ucount > 1 && ucount <= 255 && (ucount % 3) == 1 && "Invalid U count, must be 3*N+1"); + assert(vcount > 1 && vcount <= 255 && (vcount % 3) == 1 && "Invalid V count, must be 3*N+1"); +#endif + if (vertex_type) sendCommandi(VERTEX_TYPE, vertex_type); diff --git a/src/gu/sceGuDrawBuffer.c b/src/gu/sceGuDrawBuffer.c index bb7e85d9..3b584228 100644 --- a/src/gu/sceGuDrawBuffer.c +++ b/src/gu/sceGuDrawBuffer.c @@ -10,6 +10,15 @@ void sceGuDrawBuffer(int psm, void *fbp, int frame_width) { +#ifdef GU_DEBUG + printf("sceGuDrawBuffer(%d, %p, %d);\n", psm, fbp, frame_width); + assert(gu_init && "GU not initialized"); + assert(((unsigned int)fbp & 0x1FFF) == 0 && "Frame buffer pointer must be 8192-byte aligned"); + assert(frame_width >= 64 && frame_width <= 1024 && "Frame width must be between 64 and 1024"); + assert((frame_width & 63) == 0 && "Frame width must be a multiple of 64"); + assert(psm >= GU_PSM_5650 && psm <= GU_PSM_8888 && "Invalid pixel format"); +#endif + gu_draw_buffer.pixel_size = psm; gu_draw_buffer.frame_width = frame_width; gu_draw_buffer.frame_buffer = fbp; diff --git a/src/gu/sceGuDrawBufferList.c b/src/gu/sceGuDrawBufferList.c index 351ad37f..03eb122e 100644 --- a/src/gu/sceGuDrawBufferList.c +++ b/src/gu/sceGuDrawBufferList.c @@ -10,6 +10,14 @@ void sceGuDrawBufferList(int psm, void *fbp, int fbw) { +#ifdef GU_DEBUG + printf("sceGuDrawBufferList(%d, %p, %d);\n", psm, fbp, fbw); + assert(gu_init && "GU not initialized"); + assert(((unsigned int)fbp & 0x1FFF) == 0 && "Frame buffer pointer must be 8192-byte aligned"); + assert(fbw >= 64 && fbw <= 1024 && (fbw & 0x3F) == 0 && "Frame buffer width must be multiple of 64"); + assert(psm >= GU_PSM_5650 && psm <= GU_PSM_8888 && "Invalid pixel format"); +#endif + sendCommandi(FRAMEBUF_PIX_FORMAT, psm); sendCommandi(FRAME_BUF_PTR, (unsigned int)fbp); sendCommandi(FRAME_BUF_WIDTH, ((((unsigned int)fbp) & 0xff000000) >> 8) | fbw); diff --git a/src/gu/sceGuDrawSpline.c b/src/gu/sceGuDrawSpline.c index 92e0e70a..1c1d71f7 100644 --- a/src/gu/sceGuDrawSpline.c +++ b/src/gu/sceGuDrawSpline.c @@ -10,6 +10,15 @@ void sceGuDrawSpline(int vertex_type, int ucount, int vcount, int uedge, int vedge, const void *indices, const void *vertices) { +#ifdef GU_DEBUG + printf("sceGuDrawSpline(%d, %d, %d, %d, %d, %p, %p);\n", vertex_type, ucount, vcount, uedge, vedge, indices, vertices); + assert(gu_init && "GU not initialized"); + assert(ucount >= 4 && ucount <= 255 && "Invalid U count - must be between 4 and 255"); + assert(vcount >= 4 && vcount <= 255 && "Invalid V count - must be between 4 and 255"); + assert(uedge >= 0 && uedge <= 3 && "Invalid U edge"); + assert(vedge >= 0 && vedge <= 3 && "Invalid V edge"); +#endif + if (vertex_type) sendCommandi(VERTEX_TYPE, vertex_type); diff --git a/src/gu/sceGuEnable.c b/src/gu/sceGuEnable.c index 84e1c33c..b006a14a 100644 --- a/src/gu/sceGuEnable.c +++ b/src/gu/sceGuEnable.c @@ -10,6 +10,12 @@ void sceGuEnable(int state) { +#ifdef GU_DEBUG + printf("sceGuEnable(%d);\n", state); + assert(gu_init && "GU not initialized"); + assert(state >= 0 && state < GU_MAX_STATUS && "Invalid enable state"); +#endif + switch (state) { case GU_ALPHA_TEST: diff --git a/src/gu/sceGuEndObject.c b/src/gu/sceGuEndObject.c index 0f3db5d3..df6f6642 100644 --- a/src/gu/sceGuEndObject.c +++ b/src/gu/sceGuEndObject.c @@ -12,6 +12,12 @@ int sceGuEndObject(void) { +#ifdef GU_DEBUG + printf("sceGuEndObject();\n"); + assert(gu_init && "GU not initialized"); + assert(gu_object_stack_depth > 0 && "Object stack underflow"); +#endif + int res; gu_object_stack_depth--; diff --git a/src/gu/sceGuFinishId.c b/src/gu/sceGuFinishId.c index 2d63ca8c..8df0d21d 100644 --- a/src/gu/sceGuFinishId.c +++ b/src/gu/sceGuFinishId.c @@ -12,6 +12,11 @@ int sceGuFinishId(unsigned int id) { +#ifdef GU_DEBUG + printf("sceGuFinishId(%08X);\n", id); + assert(gu_init && "GU not initialized"); +#endif + int ret; int intr; diff --git a/src/gu/sceGuFog.c b/src/gu/sceGuFog.c index 184b1f8b..75773eb5 100644 --- a/src/gu/sceGuFog.c +++ b/src/gu/sceGuFog.c @@ -10,6 +10,14 @@ void sceGuFog(float near, float far, unsigned int color) { +#ifdef GU_DEBUG + printf("sceGuFog(%f, %f, %08X);\n", near, far, color); + assert(gu_init && "GU not initialized"); + assert(near >= 0.0f && "Invalid fog near value"); + assert(far > near && "Fog far must be greater than near"); + assert(far <= 65535.0f && "Fog far value too large"); +#endif + float distance = far - near; if (distance) diff --git a/src/gu/sceGuFrontFace.c b/src/gu/sceGuFrontFace.c index e2aede14..66d86270 100644 --- a/src/gu/sceGuFrontFace.c +++ b/src/gu/sceGuFrontFace.c @@ -10,5 +10,11 @@ void sceGuFrontFace(int order) { +#ifdef GU_DEBUG + printf("sceGuFrontFace(%d);\n", order); + assert(gu_init && "GU not initialized"); + assert((order == GU_CW || order == GU_CCW) && "Invalid front face order"); +#endif + sendCommandi(CULL, order ? 0 : 1); } diff --git a/src/gu/sceGuInit.c b/src/gu/sceGuInit.c index 4cb47323..c5e4c512 100644 --- a/src/gu/sceGuInit.c +++ b/src/gu/sceGuInit.c @@ -308,15 +308,27 @@ void callbackSig(int id, void *arg) int sceGuInit(void) { +#ifdef GU_DEBUG + printf("sceGuInit();\n"); + assert(!gu_init && "GU already initialized"); +#endif + int res; PspGeCallbackData callback; ge_edram_address = sceGeEdramGetAddr(); +#ifdef GU_DEBUG + assert(ge_edram_address != NULL && "Failed to get EDRAM address"); +#endif + sceGuResetGlobalVariables(); res = sceKernelCreateEventFlag("SceGuSignal", PSP_EVENT_WAITMULTIPLE, 3, 0); if (res < 0) { +#ifdef GU_DEBUG + printf("Failed to create event flag: %d\n", res); +#endif return res; } gu_settings.kernel_event_flag = res; @@ -328,6 +340,9 @@ int sceGuInit(void) res = sceGeSetCallback(&callback); if (res < 0) { +#ifdef GU_DEBUG + printf("Failed to set GE callback: %d\n", res); +#endif sceKernelDeleteEventFlag(gu_settings.kernel_event_flag); gu_settings.kernel_event_flag = -1; return res; @@ -338,6 +353,9 @@ int sceGuInit(void) res = sceGeListEnQueue((void *)((unsigned int)ge_init_list & 0x1fffffff), NULL, gu_settings.ge_callback_id, NULL); if (res < 0) { +#ifdef GU_DEBUG + printf("Failed to enqueue GE list: %d\n", res); +#endif sceKernelDeleteEventFlag(gu_settings.kernel_event_flag); sceGeUnsetCallback(gu_settings.ge_callback_id); gu_settings.ge_callback_id = -1; @@ -352,5 +370,6 @@ int sceGuInit(void) gu_settings.swapBuffersCallback = NULL; gu_settings.swapBuffersBehaviour = PSP_DISPLAY_SETBUF_NEXTHSYNC; + gu_init = 1; return 0; } diff --git a/src/gu/sceGuLight.c b/src/gu/sceGuLight.c index dbe5d3d2..685e0b00 100644 --- a/src/gu/sceGuLight.c +++ b/src/gu/sceGuLight.c @@ -14,6 +14,14 @@ void sceGuLight(int light, int type, int components, const ScePspFVector3 *position) { +#ifdef GU_DEBUG + printf("sceGuLight(%d, %d, %d, %p);\n", light, type, components, position); + assert(gu_init && "GU not initialized"); + assert(light >= 0 && light <= 3 && "Invalid light index"); + assert(type >= GU_DIRECTIONAL && type <= GU_SPOTLIGHT && "Invalid light type"); + assert(components >= GU_AMBIENT && components <= GU_DIFFUSE_AND_SPECULAR && "Invalid light components"); +#endif + int offset = light * 3; int ltype; diff --git a/src/gu/sceGuLightAtt.c b/src/gu/sceGuLightAtt.c index 6a8abe9a..020c9575 100644 --- a/src/gu/sceGuLightAtt.c +++ b/src/gu/sceGuLightAtt.c @@ -10,6 +10,12 @@ void sceGuLightAtt(int light, float atten0, float atten1, float atten2) { +#ifdef GU_DEBUG + printf("sceGuLightAtt(%d, %f, %f, %f);\n", light, atten0, atten1, atten2); + assert(gu_init && "GU not initialized"); + assert(light >= 0 && light <= 3 && "Invalid light index"); +#endif + int offset = light * 3; sendCommandf(LIGHT0_CONSTANT_ATTEN + offset, atten0); diff --git a/src/gu/sceGuLightColor.c b/src/gu/sceGuLightColor.c index 0734f2bf..44cee530 100644 --- a/src/gu/sceGuLightColor.c +++ b/src/gu/sceGuLightColor.c @@ -10,6 +10,12 @@ void sceGuLightColor(int light, int component, unsigned int color) { +#ifdef GU_DEBUG + printf("sceGuLightColor(%d, %d, 0x%08X);\n", light, component, color); + assert(gu_init && "GU not initialized"); + assert(light >= 0 && light <= 3 && "Invalid light index"); + assert(component >= GU_AMBIENT && component <= GU_DIFFUSE_AND_SPECULAR && "Invalid light component"); +#endif int offset = light * 3; diff --git a/src/gu/sceGuLightMode.c b/src/gu/sceGuLightMode.c index 1059972d..29f18093 100644 --- a/src/gu/sceGuLightMode.c +++ b/src/gu/sceGuLightMode.c @@ -10,5 +10,11 @@ void sceGuLightMode(int mode) { +#ifdef GU_DEBUG + printf("sceGuLightMode(%d);\n", mode); + assert(gu_init && "GU not initialized"); + assert(mode >= GU_SINGLE_COLOR && mode <= GU_SEPARATE_SPECULAR_COLOR && "Invalid light mode"); +#endif + sendCommandi(LIGHT_MODE, mode); } diff --git a/src/gu/sceGuLightSpot.c b/src/gu/sceGuLightSpot.c index 7283455e..d3a0c899 100644 --- a/src/gu/sceGuLightSpot.c +++ b/src/gu/sceGuLightSpot.c @@ -9,7 +9,13 @@ #include "guInternal.h" void sceGuLightSpot(int light, const ScePspFVector3 *direction, float exponent, float cutoff) -{ +{ +#ifdef GU_DEBUG + printf("sceGuLightSpot(%d, %p, %f, %f);\n", light, direction, exponent, cutoff); + assert(gu_init && "GU not initialized"); + assert(light >= 0 && light <= 3 && "Invalid light index"); +#endif + int offset = light * 3; sendCommandf(LIGHT0_EXPONENT_ATTEN + light, exponent); sendCommandf(LIGHT0_CUTOFF_ATTEN + light, cutoff); diff --git a/src/gu/sceGuLogicalOp.c b/src/gu/sceGuLogicalOp.c index d524ff26..6de97998 100644 --- a/src/gu/sceGuLogicalOp.c +++ b/src/gu/sceGuLogicalOp.c @@ -10,5 +10,11 @@ void sceGuLogicalOp(int op) { +#ifdef GU_DEBUG + printf("sceGuLogicalOp(%d);\n", op); + assert(gu_init && "GU not initialized"); + assert(op >= GU_CLEAR && op <= GU_SET && "Invalid logical operation"); +#endif + sendCommandi(LOGIC_OP, op & 0x0f); } diff --git a/src/gu/sceGuMaterial.c b/src/gu/sceGuMaterial.c index 241d6030..3b54648f 100644 --- a/src/gu/sceGuMaterial.c +++ b/src/gu/sceGuMaterial.c @@ -10,6 +10,13 @@ void sceGuMaterial(int mode, int color) { +#ifdef GU_DEBUG + printf("sceGuMaterial(%d, %08X);\n", mode, color); + assert(gu_init && "GU not initialized"); + assert(mode >= 0 && "Invalid material mode"); + assert((mode & ~(GU_AMBIENT | GU_DIFFUSE | GU_SPECULAR)) == 0 && "Invalid material mode flags"); +#endif + if (mode & GU_AMBIENT) { sendCommandi(AMBIENT_COLOR, color); sendCommandi(AMBIENT_ALPHA, color >> 24); diff --git a/src/gu/sceGuModelColor.c b/src/gu/sceGuModelColor.c index 04814cfa..74bdd8e6 100644 --- a/src/gu/sceGuModelColor.c +++ b/src/gu/sceGuModelColor.c @@ -10,6 +10,11 @@ void sceGuModelColor(unsigned int emissive, unsigned int ambient, unsigned int diffuse, unsigned int specular) { +#ifdef GU_DEBUG + printf("sceGuModelColor(%08X, %08X, %08X, %08X);\n", emissive, ambient, diffuse, specular); + assert(gu_init && "GU not initialized"); +#endif + sendCommandi(MATERIAL_EMISSIVE, emissive); sendCommandi(MATERIAL_DIFFUSE, diffuse); sendCommandi(AMBIENT_COLOR, ambient); diff --git a/src/gu/sceGuMorphWeight.c b/src/gu/sceGuMorphWeight.c index 626c38f7..f08e1c0c 100644 --- a/src/gu/sceGuMorphWeight.c +++ b/src/gu/sceGuMorphWeight.c @@ -10,5 +10,11 @@ void sceGuMorphWeight(int index, float weight) { +#ifdef GU_DEBUG + printf("sceGuMorphWeight(%d, %f);\n", index, weight); + assert(gu_init && "GU not initialized"); + assert(index >= 0 && index <= 7 && "Invalid morph weight index"); +#endif + sendCommandf(MORPH_WEIGHT0 + index, weight); } diff --git a/src/gu/sceGuOffset.c b/src/gu/sceGuOffset.c index b3dec082..5b28fe66 100644 --- a/src/gu/sceGuOffset.c +++ b/src/gu/sceGuOffset.c @@ -10,6 +10,13 @@ void sceGuOffset(unsigned int x, unsigned int y) { +#ifdef GU_DEBUG + printf("sceGuOffset(%08X, %08X);\n", x, y); + assert(gu_init && "GU not initialized"); + assert(x < 4096 && "X offset too large"); + assert(y < 4096 && "Y offset too large"); +#endif + sendCommandi(OFFSET_X, x << 4); sendCommandi(OFFSET_Y, y << 4); } diff --git a/src/gu/sceGuPatchDivide.c b/src/gu/sceGuPatchDivide.c index 8002d3ce..9333a025 100644 --- a/src/gu/sceGuPatchDivide.c +++ b/src/gu/sceGuPatchDivide.c @@ -10,5 +10,12 @@ void sceGuPatchDivide(unsigned int ulevel, unsigned int vlevel) { +#ifdef GU_DEBUG + printf("sceGuPatchDivide(%08X, %08X);\n", ulevel, vlevel); + assert(gu_init && "GU not initialized"); + assert(ulevel >= 1 && ulevel <= 64 && "Invalid U level - must be between 1 and 64"); + assert(vlevel >= 1 && vlevel <= 64 && "Invalid V level - must be between 1 and 64"); +#endif + sendCommandi(PATCH_DIVISION, (vlevel << 8) | ulevel); } diff --git a/src/gu/sceGuPatchFrontFace.c b/src/gu/sceGuPatchFrontFace.c index 54552ead..65d9873d 100644 --- a/src/gu/sceGuPatchFrontFace.c +++ b/src/gu/sceGuPatchFrontFace.c @@ -10,5 +10,11 @@ void sceGuPatchFrontFace(unsigned int mode) { +#ifdef GU_DEBUG + printf("sceGuPatchFrontFace(%u);\n", mode); + assert(gu_init && "GU not initialized"); + assert((mode == GU_CW || mode == GU_CCW) && "Invalid patch front face mode"); +#endif + sendCommandi(PATCH_FACING, mode); } diff --git a/src/gu/sceGuPatchPrim.c b/src/gu/sceGuPatchPrim.c index ba0d4285..b3ea1ba9 100644 --- a/src/gu/sceGuPatchPrim.c +++ b/src/gu/sceGuPatchPrim.c @@ -10,6 +10,12 @@ void sceGuPatchPrim(int prim) { +#ifdef GU_DEBUG + printf("sceGuPatchPrim(%d);\n", prim); + assert(gu_init && "GU not initialized"); + assert((prim == GU_POINTS || prim == GU_LINE_STRIP || prim == GU_TRIANGLE_STRIP) && "Invalid patch primitive"); +#endif + switch (prim) { case GU_POINTS: diff --git a/src/gu/sceGuPixelMask.c b/src/gu/sceGuPixelMask.c index 606ff667..72c772cb 100644 --- a/src/gu/sceGuPixelMask.c +++ b/src/gu/sceGuPixelMask.c @@ -10,6 +10,11 @@ void sceGuPixelMask(unsigned int mask) { +#ifdef GU_DEBUG + printf("sceGuPixelMask(%08X);\n", mask); + assert(gu_init && "GU not initialized"); +#endif + sendCommandi(MASK_COLOR, mask); sendCommandi(MASK_ALPHA, mask >> 24); } diff --git a/src/gu/sceGuScissor.c b/src/gu/sceGuScissor.c index 8f76a8a5..0d09b156 100644 --- a/src/gu/sceGuScissor.c +++ b/src/gu/sceGuScissor.c @@ -10,6 +10,14 @@ void sceGuScissor(int x, int y, int w, int h) { +#ifdef GU_DEBUG + printf("sceGuScissor(%d, %d, %d, %d);\n", x, y, w, h); + assert(gu_init && "GU not initialized"); + assert(w > 0 && h > 0 && "Invalid scissor dimensions"); + assert(x >= 0 && y >= 0 && "Invalid scissor position"); + assert(x + w <= 480 && y + h <= 272 && "Scissor region too large"); +#endif + GuContext *context = &gu_contexts[gu_curr_context]; context->scissor_start[0] = x; diff --git a/src/gu/sceGuSendCommandf.c b/src/gu/sceGuSendCommandf.c index 47a17558..f4bba0dc 100644 --- a/src/gu/sceGuSendCommandf.c +++ b/src/gu/sceGuSendCommandf.c @@ -10,5 +10,9 @@ void sceGuSendCommandf(int cmd, float argument) { +#ifdef GU_DEBUG + assert(gu_init && "GU not initialized"); +#endif + sendCommandf(cmd,argument); } diff --git a/src/gu/sceGuSendCommandi.c b/src/gu/sceGuSendCommandi.c index 3e7b0765..0262c87a 100644 --- a/src/gu/sceGuSendCommandi.c +++ b/src/gu/sceGuSendCommandi.c @@ -10,5 +10,9 @@ void sceGuSendCommandi(int cmd, int argument) { +#ifdef GU_DEBUG + assert(gu_init && "GU not initialized"); +#endif + sendCommandi(cmd, argument); } diff --git a/src/gu/sceGuSendList.c b/src/gu/sceGuSendList.c index 71b781b5..0e333c91 100644 --- a/src/gu/sceGuSendList.c +++ b/src/gu/sceGuSendList.c @@ -13,6 +13,12 @@ int sceGuSendList(int mode, const void *list, PspGeContext *context) { +#ifdef GU_DEBUG + printf("sceGuSendList(%d, %p, %p);\n", mode, list, context); + assert(gu_init && "GU not initialized"); + assert((mode == GU_HEAD || mode == GU_TAIL) && "Invalid list mode"); +#endif + PspGeListArgs args; int list_id; int callback; diff --git a/src/gu/sceGuSetAllStatus.c b/src/gu/sceGuSetAllStatus.c index f25b4bd3..6c85f8c6 100644 --- a/src/gu/sceGuSetAllStatus.c +++ b/src/gu/sceGuSetAllStatus.c @@ -10,6 +10,11 @@ void sceGuSetAllStatus(int status) { +#ifdef GU_DEBUG + printf("sceGuSetAllStatus(%d);\n", status); + assert(gu_init && "GU not initialized"); +#endif + unsigned int i; for (i = 0; i < GU_MAX_STATUS; ++i) { diff --git a/src/gu/sceGuSetCallback.c b/src/gu/sceGuSetCallback.c index e75607ab..71643de9 100644 --- a/src/gu/sceGuSetCallback.c +++ b/src/gu/sceGuSetCallback.c @@ -10,6 +10,12 @@ void *sceGuSetCallback(int signal, GuCallback callback) { +#ifdef GU_DEBUG + printf("sceGuSetCallback(%d, %p);\n", signal, callback); + assert(gu_init && "GU not initialized"); + assert((signal == GU_CALLBACK_SIGNAL || signal == GU_CALLBACK_FINISH) && "Invalid signal"); +#endif + GuCallback old_callback = NULL; switch (signal) diff --git a/src/gu/sceGuSetDither.c b/src/gu/sceGuSetDither.c index ace9d695..54013172 100644 --- a/src/gu/sceGuSetDither.c +++ b/src/gu/sceGuSetDither.c @@ -10,6 +10,11 @@ void sceGuSetDither(const ScePspIMatrix4 *matrix) { +#ifdef GU_DEBUG + printf("sceGuSetDither(%p);\n", matrix); + assert(gu_init && "GU not initialized"); +#endif + sendCommandi(DITH0, (matrix->x.x & 0x0f) | ((matrix->x.y & 0x0f) << 4) | ((matrix->x.z & 0x0f) << 8) | ((matrix->x.w & 0x0f) << 12)); sendCommandi(DITH1, (matrix->y.x & 0x0f) | ((matrix->y.y & 0x0f) << 4) | ((matrix->y.z & 0x0f) << 8) | ((matrix->y.w & 0x0f) << 12)); sendCommandi(DITH2, (matrix->z.x & 0x0f) | ((matrix->z.y & 0x0f) << 4) | ((matrix->z.z & 0x0f) << 8) | ((matrix->z.w & 0x0f) << 12)); diff --git a/src/gu/sceGuSetMatrix.c b/src/gu/sceGuSetMatrix.c index 76a6d658..4657565a 100644 --- a/src/gu/sceGuSetMatrix.c +++ b/src/gu/sceGuSetMatrix.c @@ -10,6 +10,12 @@ void sceGuSetMatrix(int type, const ScePspFMatrix4 *matrix) { +#ifdef GU_DEBUG + printf("sceGuSetMatrix(%d, %p);\n", type, matrix); + assert(gu_init && "GU not initialized"); + assert(type >= GU_PROJECTION && type <= GU_TEXTURE && "Invalid matrix type"); +#endif + unsigned int i, j; const float *fmatrix = (const float *)matrix; diff --git a/src/gu/sceGuSetStatus.c b/src/gu/sceGuSetStatus.c index b127f032..9a42ce4c 100644 --- a/src/gu/sceGuSetStatus.c +++ b/src/gu/sceGuSetStatus.c @@ -10,6 +10,13 @@ void sceGuSetStatus(int state, int status) { +#ifdef GU_DEBUG + printf("sceGuSetStatus(%d, %d);\n", state, status); + assert(gu_init && "GU not initialized"); + assert(state >= 0 && state < GU_MAX_STATUS && "Invalid state"); + assert((status == 0 || status == 1) && "Invalid status"); +#endif + if (status) sceGuEnable(state); else diff --git a/src/gu/sceGuShadeModel.c b/src/gu/sceGuShadeModel.c index faef3007..19c9383b 100644 --- a/src/gu/sceGuShadeModel.c +++ b/src/gu/sceGuShadeModel.c @@ -10,5 +10,11 @@ void sceGuShadeModel(int mode) { +#ifdef GU_DEBUG + printf("sceGuShadeModel(%d);\n", mode); + assert(gu_init && "GU not initialized"); + assert((mode == GU_FLAT || mode == GU_SMOOTH) && "Invalid shade model"); +#endif + sendCommandi(SHADE_MODE, mode); } diff --git a/src/gu/sceGuSignal.c b/src/gu/sceGuSignal.c index 58dbc531..07d12cab 100644 --- a/src/gu/sceGuSignal.c +++ b/src/gu/sceGuSignal.c @@ -10,6 +10,13 @@ void sceGuSignal(int mode, int id) { +#ifdef GU_DEBUG + printf("sceGuSignal(%d, %d);\n", mode, id); + assert(gu_init && "GU not initialized"); + assert(mode >= GU_SIGNAL_WAIT && mode <= GU_SIGNAL_PAUSE && "Invalid signal mode"); + assert(id >= 0 && id <= 65535 && "Invalid signal ID, must be between 0 and 65535"); +#endif + sendCommandi(SIGNAL, ((mode & 0xff) << 16) | (id & 0xffff)); sendCommandi(END, 0); diff --git a/src/gu/sceGuSpecular.c b/src/gu/sceGuSpecular.c index f8d9c6cd..3245a989 100644 --- a/src/gu/sceGuSpecular.c +++ b/src/gu/sceGuSpecular.c @@ -10,5 +10,10 @@ void sceGuSpecular(float power) // specular power { +#ifdef GU_DEBUG + printf("sceGuSpecular(%f);\n", power); + assert(gu_init && "GU not initialized"); +#endif + sendCommandf(MATERIAL_SPECULAR_COEF, power); } diff --git a/src/gu/sceGuStart.c b/src/gu/sceGuStart.c index e4c8cf4f..ab405d56 100644 --- a/src/gu/sceGuStart.c +++ b/src/gu/sceGuStart.c @@ -14,6 +14,12 @@ int sceGuStart(int ctype, void *list) { +#ifdef GU_DEBUG + printf("sceGuStart(%d, %p);\n", ctype, list); + assert(gu_init && "GU not initialized"); + assert((ctype == GU_DIRECT || ctype == GU_CALL || ctype == GU_SEND) && "Invalid context type"); +#endif + int intr; GuContext *context = &gu_contexts[ctype]; unsigned int *local_list = (unsigned int *)(((unsigned int)list) | 0x40000000); diff --git a/src/gu/sceGuStencilFunc.c b/src/gu/sceGuStencilFunc.c index d065053b..9a9a76be 100644 --- a/src/gu/sceGuStencilFunc.c +++ b/src/gu/sceGuStencilFunc.c @@ -10,5 +10,14 @@ void sceGuStencilFunc(int func, int ref, int mask) { - sendCommandi(STENCIL_TEST, func | ((ref & 0xff) << 8) | ((mask & 0xff) << 16)); +#ifdef GU_DEBUG + printf("sceGuStencilFunc(%d, %d, %d);\n", func, ref, mask); + assert(gu_init && "GU not initialized"); + assert(func >= GU_NEVER && func <= GU_GEQUAL && "Invalid stencil function"); + assert(ref >= 0 && ref <= 255 && "Invalid stencil reference"); + assert(mask >= 0 && mask <= 255 && "Invalid stencil mask"); +#endif + + int arg = func | ((ref & 0xff) << 8) | ((mask & 0xff) << 16); + sendCommandi(STENCIL_TEST, arg); } diff --git a/src/gu/sceGuStencilOp.c b/src/gu/sceGuStencilOp.c index e144cce4..0e705c3d 100644 --- a/src/gu/sceGuStencilOp.c +++ b/src/gu/sceGuStencilOp.c @@ -10,5 +10,14 @@ void sceGuStencilOp(int fail, int zfail, int zpass) { - sendCommandi(STENCIL_OP, fail | (zfail << 8) | (zpass << 16)); +#ifdef GU_DEBUG + printf("sceGuStencilOp(%d, %d, %d);\n", fail, zfail, zpass); + assert(gu_init && "GU not initialized"); + assert(fail >= GU_KEEP && fail <= GU_DECR && "Invalid stencil fail operation"); + assert(zfail >= GU_KEEP && zfail <= GU_DECR && "Invalid stencil zfail operation"); + assert(zpass >= GU_KEEP && zpass <= GU_DECR && "Invalid stencil zpass operation"); +#endif + + int arg = fail | (zfail << 8) | (zpass << 16); + sendCommandi(STENCIL_OP, arg); } diff --git a/src/gu/sceGuSwapBuffers.c b/src/gu/sceGuSwapBuffers.c index b10dade0..d6ceb95d 100644 --- a/src/gu/sceGuSwapBuffers.c +++ b/src/gu/sceGuSwapBuffers.c @@ -13,6 +13,11 @@ void *sceGuSwapBuffers(void) { +#ifdef GU_DEBUG + printf("sceGuSwapBuffers();\n"); + assert(gu_init && "GU not initialized"); +#endif + if (gu_settings.swapBuffersCallback) { gu_settings.swapBuffersCallback(&gu_draw_buffer.disp_buffer, &gu_draw_buffer.frame_buffer); diff --git a/src/gu/sceGuSync.c b/src/gu/sceGuSync.c index d23a753f..4b90c853 100644 --- a/src/gu/sceGuSync.c +++ b/src/gu/sceGuSync.c @@ -13,6 +13,13 @@ int sceGuSync(int mode, int what) { +#ifdef GU_DEBUG + printf("sceGuSync(%d, %d);\n", mode, what); + assert(gu_init && "GU not initialized"); + assert((mode == GU_SYNC_FINISH || mode == GU_SYNC_LIST || mode == GU_SYNC_SEND || mode == GU_SYNC_SIGNAL || mode == GU_SYNC_DONE) && "Invalid sync mode"); + assert((what == GU_SYNC_WAIT || what == GU_SYNC_NOWAIT) && "Invalid sync what"); +#endif + switch (mode) { case GU_SYNC_FINISH: diff --git a/src/gu/sceGuTerm.c b/src/gu/sceGuTerm.c index 6e9c6900..96c9e1aa 100644 --- a/src/gu/sceGuTerm.c +++ b/src/gu/sceGuTerm.c @@ -13,6 +13,13 @@ void sceGuTerm(void) { +#ifdef GU_DEBUG + printf("sceGuTerm();\n"); + assert(gu_init && "GU not initialized"); + assert(gu_settings.kernel_event_flag >= 0 && "Invalid kernel event flag"); + assert(gu_settings.ge_callback_id >= 0 && "Invalid GE callback ID"); +#endif + sceKernelDeleteEventFlag(gu_settings.kernel_event_flag); sceGeUnsetCallback(gu_settings.ge_callback_id); } diff --git a/src/gu/sceGuTexEnvColor.c b/src/gu/sceGuTexEnvColor.c index 2bdf1cac..5f143a8c 100644 --- a/src/gu/sceGuTexEnvColor.c +++ b/src/gu/sceGuTexEnvColor.c @@ -10,5 +10,10 @@ void sceGuTexEnvColor(unsigned int color) { +#ifdef GU_DEBUG + printf("sceGuTexEnvColor(0x%08X);\n", color); + assert(gu_init && "GU not initialized"); +#endif + sendCommandi(TEX_ENV_COLOR, color); } diff --git a/src/gu/sceGuTexFilter.c b/src/gu/sceGuTexFilter.c index 31b2d281..07899fea 100644 --- a/src/gu/sceGuTexFilter.c +++ b/src/gu/sceGuTexFilter.c @@ -10,5 +10,13 @@ void sceGuTexFilter(int min, int mag) { - sendCommandi(TEX_FILTER, (mag << 8) | min); +#ifdef GU_DEBUG + printf("sceGuTexFilter(%d, %d);\n", min, mag); + assert(gu_init && "GU not initialized"); + assert(min >= GU_NEAREST && min <= GU_LINEAR_MIPMAP_LINEAR && "Invalid minification filter"); + assert(mag >= GU_NEAREST && mag <= GU_LINEAR && "Invalid magnification filter"); +#endif + + int arg = min | (mag << 8); + sendCommandi(TEX_FILTER, arg); } diff --git a/src/gu/sceGuTexFlush.c b/src/gu/sceGuTexFlush.c index 51f39796..837de922 100644 --- a/src/gu/sceGuTexFlush.c +++ b/src/gu/sceGuTexFlush.c @@ -10,5 +10,10 @@ void sceGuTexFlush(void) { +#ifdef GU_DEBUG + printf("sceGuTexFlush();\n"); + assert(gu_init && "GU not initialized"); +#endif + sendCommandi(TEX_FLUSH, 0); } diff --git a/src/gu/sceGuTexFunc.c b/src/gu/sceGuTexFunc.c index 0ffa11b6..cfb12f56 100644 --- a/src/gu/sceGuTexFunc.c +++ b/src/gu/sceGuTexFunc.c @@ -10,8 +10,14 @@ void sceGuTexFunc(int tfx, int tcc) { - GuContext *context = &gu_contexts[gu_curr_context]; - context->texture_function = (tcc << 8) | tfx; +#ifdef GU_DEBUG + printf("sceGuTexFunc(%d, %d);\n", tfx, tcc); + assert(gu_init && "GU not initialized"); + assert(tfx >= GU_TFX_MODULATE && tfx <= GU_TFX_ADD && "Invalid texture function"); + assert(tcc >= GU_TCC_RGB && tcc <= GU_TCC_RGBA && "Invalid texture color component"); +#endif - sendCommandi(TEX_FUNC, ((tcc << 8) | tfx) | context->fragment_2x); + GuContext *context = &gu_contexts[gu_curr_context]; + context->texture_function = tfx | (tcc << 8); + sendCommandi(TEX_FUNC, context->texture_function | context->fragment_2x); } diff --git a/src/gu/sceGuTexImage.c b/src/gu/sceGuTexImage.c index 756bd64e..1dfed07e 100644 --- a/src/gu/sceGuTexImage.c +++ b/src/gu/sceGuTexImage.c @@ -15,6 +15,18 @@ static inline int getExp(int val) void sceGuTexImage(int mipmap, int width, int height, int tbw, const void *tbp) { +#ifdef GU_DEBUG + printf("sceGuTexImage(%d, %d, %d, %d, %p);\n", mipmap, width, height, tbw, tbp); + assert(gu_init && "GU not initialized"); + assert(((unsigned int)tbp & 0xF) == 0 && "Texture buffer pointer must be 16-byte aligned"); + assert(mipmap >= 0 && mipmap <= 7 && "Invalid mipmap level"); + assert(width >= 1 && width <= 512 && "Invalid texture width"); + assert(height >= 1 && height <= 512 && "Invalid texture height"); + assert(tbw >= 1 && tbw <= 1024 && "Invalid texture buffer width"); + assert((width & (width - 1)) == 0 && "Texture width must be power of 2"); + assert((height & (height - 1)) == 0 && "Texture height must be power of 2"); +#endif + GECommand texAddr = (GECommand)(TEX_ADDR0 + mipmap); GECommand texBufWidth = (GECommand)(TEX_BUF_WIDTH0 + mipmap); GECommand texSize = (GECommand)(TEX_SIZE0 + mipmap); diff --git a/src/gu/sceGuTexLevelMode.c b/src/gu/sceGuTexLevelMode.c index a2d15d66..0566bfce 100644 --- a/src/gu/sceGuTexLevelMode.c +++ b/src/gu/sceGuTexLevelMode.c @@ -12,6 +12,12 @@ void sceGuTexLevelMode(unsigned int mode, float bias) { +#ifdef GU_DEBUG + printf("sceGuTexLevelMode(%08X, %f);\n", mode, bias); + assert(gu_init && "GU not initialized"); + assert((mode == GU_TEXTURE_AUTO || mode == GU_TEXTURE_CONST || mode == GU_TEXTURE_SLOPE) && "Invalid texture level mode"); +#endif + int offset = (int)truncf(bias * 16.0f); // mip map bias? diff --git a/src/gu/sceGuTexMapMode.c b/src/gu/sceGuTexMapMode.c index 3f4b5871..227ea550 100644 --- a/src/gu/sceGuTexMapMode.c +++ b/src/gu/sceGuTexMapMode.c @@ -10,6 +10,12 @@ void sceGuTexMapMode(int mode, unsigned int lu, unsigned int lv) { +#ifdef GU_DEBUG + printf("sceGuTexMapMode(%d, %08X, %08X);\n", mode, lu, lv); + assert(gu_init && "GU not initialized"); + assert((mode == GU_TEXTURE_COORDS || mode == GU_TEXTURE_MATRIX || mode == GU_ENVIRONMENT_MAP) && "Invalid texture map mode"); +#endif + GuContext *context = &gu_contexts[gu_curr_context]; context->texture_map_mode = mode & 0x03; diff --git a/src/gu/sceGuTexMode.c b/src/gu/sceGuTexMode.c index 8d79e49c..faefe668 100644 --- a/src/gu/sceGuTexMode.c +++ b/src/gu/sceGuTexMode.c @@ -10,6 +10,16 @@ void sceGuTexMode(int tpsm, int maxmips, int mc, int swizzle) { +#ifdef GU_DEBUG + printf("sceGuTexMode(%d, %d, %d, %d);\n", tpsm, maxmips, mc, swizzle); + assert(gu_init && "GU not initialized"); + assert((tpsm == GU_PSM_5650 || tpsm == GU_PSM_5551 || tpsm == GU_PSM_4444 || + tpsm == GU_PSM_8888 || tpsm == GU_PSM_T4 || tpsm == GU_PSM_T8) && "Invalid texture pixel format"); + assert(maxmips >= 0 && maxmips <= 7 && "Invalid max mipmaps"); + assert(mc >= 0 && mc <= 1 && "Invalid multiclut value"); + assert(swizzle >= 0 && swizzle <= 1 && "Invalid texture swizzle"); +#endif + GuContext *context = &gu_contexts[gu_curr_context]; context->texture_mode = tpsm; diff --git a/src/gu/sceGuTexOffset.c b/src/gu/sceGuTexOffset.c index 6d75ab4c..357d9181 100644 --- a/src/gu/sceGuTexOffset.c +++ b/src/gu/sceGuTexOffset.c @@ -10,6 +10,11 @@ void sceGuTexOffset(float u, float v) { +#ifdef GU_DEBUG + printf("sceGuTexOffset(%f, %f);\n", u, v); + assert(gu_init && "GU not initialized"); +#endif + sendCommandf(TEX_OFFSET_U, u); sendCommandf(TEX_OFFSET_V, v); } diff --git a/src/gu/sceGuTexProjMapMode.c b/src/gu/sceGuTexProjMapMode.c index 7ed0d360..8c11fd3a 100644 --- a/src/gu/sceGuTexProjMapMode.c +++ b/src/gu/sceGuTexProjMapMode.c @@ -10,6 +10,12 @@ void sceGuTexProjMapMode(int mode) { +#ifdef GU_DEBUG + printf("sceGuTexProjMapMode(%d);\n", mode); + assert(gu_init && "GU not initialized"); + assert((mode == GU_POSITION || mode == GU_UV || mode == GU_NORMALIZED_NORMAL || mode == GU_NORMAL) && "Invalid texture projection map mode"); +#endif + GuContext *context = &gu_contexts[gu_curr_context]; context->texture_proj_map_mode = ((mode & 0x03) << 8); diff --git a/src/gu/sceGuTexScale.c b/src/gu/sceGuTexScale.c index 032b63a2..f804acf3 100644 --- a/src/gu/sceGuTexScale.c +++ b/src/gu/sceGuTexScale.c @@ -10,6 +10,11 @@ void sceGuTexScale(float u, float v) { +#ifdef GU_DEBUG + printf("sceGuTexScale(%f, %f);\n", u, v); + assert(gu_init && "GU not initialized"); +#endif + sendCommandf(TEX_SCALE_U, u); sendCommandf(TEX_SCALE_V, v); } diff --git a/src/gu/sceGuTexSlope.c b/src/gu/sceGuTexSlope.c index d12299c8..51557fe2 100644 --- a/src/gu/sceGuTexSlope.c +++ b/src/gu/sceGuTexSlope.c @@ -10,5 +10,10 @@ void sceGuTexSlope(float slope) { +#ifdef GU_DEBUG + printf("sceGuTexSlope(%f);\n", slope); + assert(gu_init && "GU not initialized"); +#endif + sendCommandf(TEX_LOD_SLOPE, slope); } diff --git a/src/gu/sceGuTexSync.c b/src/gu/sceGuTexSync.c index b93d84d0..c211a94f 100644 --- a/src/gu/sceGuTexSync.c +++ b/src/gu/sceGuTexSync.c @@ -10,5 +10,10 @@ void sceGuTexSync() { +#ifdef GU_DEBUG + printf("sceGuTexSync();\n"); + assert(gu_init && "GU not initialized"); +#endif + sendCommandi(TEX_SYNC, 0); } diff --git a/src/gu/sceGuTexWrap.c b/src/gu/sceGuTexWrap.c index b375b233..f3ec7b26 100644 --- a/src/gu/sceGuTexWrap.c +++ b/src/gu/sceGuTexWrap.c @@ -10,5 +10,13 @@ void sceGuTexWrap(int u, int v) { - sendCommandi(TEX_WRAP, (v << 8) | (u)); +#ifdef GU_DEBUG + printf("sceGuTexWrap(%d, %d);\n", u, v); + assert(gu_init && "GU not initialized"); + assert((u == GU_CLAMP || u == GU_REPEAT) && "Invalid U wrap mode"); + assert((v == GU_CLAMP || v == GU_REPEAT) && "Invalid V wrap mode"); +#endif + + int arg = u | (v << 8); + sendCommandi(TEX_WRAP, arg); } diff --git a/src/gu/sceGuViewport.c b/src/gu/sceGuViewport.c index bb582929..1eb00669 100644 --- a/src/gu/sceGuViewport.c +++ b/src/gu/sceGuViewport.c @@ -10,6 +10,15 @@ void sceGuViewport(int cx, int cy, int width, int height) { +#ifdef GU_DEBUG + printf("sceGuViewport(%d, %d, %d, %d);\n", cx, cy, width, height); + assert(gu_init && "GU not initialized"); + assert(width > 0 && height > 0 && "Invalid viewport dimensions"); + assert(width <= 4096 && height <= 4096 && "Viewport dimensions too large"); + assert(cx >= 0 && cx <= 4095 && "Invalid viewport X coordinate"); + assert(cy >= 0 && cy <= 4095 && "Invalid viewport Y coordinate"); +#endif + float sx, sy, tx, ty; sx = (float)(width) * 0.5f; sy = (float)(height) * -0.5f;