diff --git a/memory.c b/memory.c index 6c9f02b..55c5da4 100644 --- a/memory.c +++ b/memory.c @@ -7,23 +7,17 @@ static char primbuff[2][PRIMBUFLEN]; // Primitive buffer, just a raw buffer of b static char *nextpri; static char *primbuff_bounds; -static char* const scratch_start = (char*)0x1F800000; -static char *scratch = scratch_start; +char* const scratchpad = (char*)0x1F800000; -void mem_scratch_reset() -{ - scratch = scratch_start; -} - -void *mem_scratch(unsigned short size) +void *mem_scratch(char **scratch_offset, unsigned short size) { #if _DEBUG - if (scratch + size > scratch_start + 1024) + if (*scratch_offset + size > scratchpad + 1024) return NULL; #endif - void *result = scratch; - scratch += size; + void *result = *scratch_offset; + *scratch_offset += size; return result; } diff --git a/memory.h b/memory.h index 2dd498f..12b6768 100644 --- a/memory.h +++ b/memory.h @@ -1,15 +1,16 @@ #ifndef __MEMORY_H__ #define __MEMORY_H__ +extern char* const scratchpad; // Starting address of scratchpad memory + /** * @brief Allocate memory in the fast scratchpad RAM buffer. * + * @param scratch_offset Pointer to a variable holding the current offset position in scratch memory. * @param size The number of bytes to allocate. Be aware that the scratchpad memory only has 1024 bytes available. * @return Pointer to the allocated buffer in scratchpad memory. */ -void *mem_scratch(unsigned short size); - -void mem_scratch_reset(); +void *mem_scratch(char **scratch_offset, unsigned short size); /** * @brief Allocate a single primitive in the primitive buffer. diff --git a/world.c b/world.c index c7c5568..2d1c3cb 100644 --- a/world.c +++ b/world.c @@ -46,8 +46,8 @@ void world_draw(const world_t *world) gte_SetRotMatrix(&vp_matrix); gte_SetTransMatrix(&vp_matrix); - mem_scratch_reset(); - SVECTOR *vecs = mem_scratch(sizeof(SVECTOR) * 3); + char *scratch = scratchpad; + SVECTOR *vecs = (SVECTOR*)mem_scratch(&scratch, sizeof(SVECTOR) * 3); for (int faceIdx = 0; faceIdx < world->header->numFaces; ++faceIdx) {