From d89d68c1151736be029a515fff5c5785827af080 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Thu, 19 Sep 2024 13:03:46 -0400 Subject: [PATCH] More code cleanups --- main.c | 183 +++++++++++++++++++++++++----------------------- smakefile | 4 +- system/copper.h | 22 ++++++ system/debug.c | 11 +++ system/debug.h | 6 ++ system/sprite.h | 7 ++ types.h | 8 +++ 7 files changed, 151 insertions(+), 90 deletions(-) create mode 100644 system/debug.c create mode 100644 system/debug.h create mode 100644 system/sprite.h diff --git a/main.c b/main.c index a551049..2ee1bbf 100644 --- a/main.c +++ b/main.c @@ -13,6 +13,7 @@ #include "system/blitter.h" #include "system/copper.h" #include "system/system.h" +#include "system/sprite.h" #include "screen.h" #include "types.h" @@ -24,8 +25,6 @@ extern struct CIA far ciaa; // change to 0 to not render sprites #define RENDER_SPRITES (1) -volatile short *dbg = (volatile short *)0x100; - struct ScreenDefinition screenDefinition; struct ActiveScreenBufferDetails activeScreenBufferDetails; @@ -38,19 +37,10 @@ struct ActiveScreenBufferDetails activeScreenBufferDetails; */ void *copperlistBitplanePointers[8][2]; -#define offsetof(s, m) &((struct s *)0)->m uint16_t custom_color = (uint16_t)offsetof(Custom, color); uint16_t custom_sprite = (uint16_t)offsetof(Custom, sprpt); uint16_t custom_sprite_control = (uint16_t)offsetof(Custom, spr); -#define SPRPOS(x, y) (((y & 0xff) << 8) + ((x & 0x1fe) >> 1)) -#define SPRCTL(x, y, height) ( \ - ((height & 0xff) << 8) + \ - ((y & 0x100) >> 6) + \ - ((height & 0x100) >> 7) + \ - (x & 1) \ -) - extern uint8_t chip TopazBitplanes[]; extern uint16_t chip CopperColors[]; extern uint16_t chip SpriteCopperlist[]; @@ -61,6 +51,8 @@ extern uint8_t chip MaskBitplane[]; #define TOPAZ_WIDTH_BYTES (TOPAZ_WIDTH_PIXELS / 8) #define TOPAZ_WIDTH_WORDS (TOPAZ_WIDTH_PIXELS / 16) +#define BLTSIZE(w, h) (w + (h << 6)) + void renderTopaz(void) { int plane; @@ -84,7 +76,7 @@ void renderTopaz(void) { custom.bltbmod = 0; custom.bltcmod = bltcmod; custom.bltdmod = bltcmod; - custom.bltsize = TOPAZ_WIDTH_WORDS + (256 << 6); + custom.bltsize = BLTSIZE(TOPAZ_WIDTH_WORDS, 256); bltbpt += TOPAZ_WIDTH_BYTES * 256; @@ -92,10 +84,96 @@ void renderTopaz(void) { } } -int main(void) { - uint16_t *copperlist, *currentCopperlist, *currentCopperColors, result, *currentSpriteCopperlist; +#define COPPERLIST_SIZE (10000) + +uint16_t *copperlist; + +void buildCopperlist(void) { uint32_t spriteDataPointer; - int i, x, y, height, plane; + uint16_t *currentCopperlist, + *currentCopperColors, + *currentSpriteCopperlist; + int i, j, y; + uint16_t spriteSetupRegisters[3]; + + copperlist = prepareNewCopperlist(COPPERLIST_SIZE); + + currentCopperlist = addDisplayToCopperlist( + copperlist, + &screenDefinition, + &activeScreenBufferDetails, + &copperlistBitplanePointers + ); + + currentCopperColors = CopperColors; + currentSpriteCopperlist = SpriteCopperlist; + + COPPERLIST_MOVE(currentCopperlist, custom_color, 0x3a6); + COPPERLIST_MOVE(currentCopperlist, custom_color + 2, 0x000); + COPPERLIST_MOVE(currentCopperlist, custom_color + 4, 0xfff); + + if (RENDER_SPRITES) { + for (i = 0; i < 8; ++i) { + spriteDataPointer = (uint32_t)&SpriteData; + spriteDataPointer += ((256 + 2) * 4) * i; + + COPPERLIST_MOVE_POINTER( + currentCopperlist, + custom_sprite + i * 4, + spriteDataPointer + ); + } + } else { + printf("Skipping render\n"); + currentCopperlist = setUpEmptySpritesInCopperlist(currentCopperlist); + } + + for (y = 0; y < 256; ++y) { + if (RENDER_SPRITES) { + for (i = 0; i < 8; ++i) { + spriteSetupRegisters[0] = custom_color + 32 + ( + // sprite color group + (i / 2) * 4 + + // 0 is transparent + 1 + + i % 2 + ) * 2; + spriteSetupRegisters[1] = custom_sprite_control + i * 8; + spriteSetupRegisters[2] = custom_sprite_control + i * 8 + 2; + + for (j = 0; j < 3; ++j) { + COPPERLIST_MOVE( + currentCopperlist, + spriteSetupRegisters[j], + *(currentSpriteCopperlist++) + ); + } + } + } else { + printf("Skipping render\n"); + } + + for (i = 3; i < 8; ++i) { + COPPERLIST_MOVE( + currentCopperlist, + custom_color + (i * 2), + *(currentCopperColors++) + ); + } + + COPPERLIST_WAIT( + currentCopperlist, + (31 + (256 / 4)), + (44 + y), + 0xFFFE + ); + } + + endCopperlist(currentCopperlist); +} + +int main(void) { + int i; struct BunRenderer bunRenderer; @@ -135,79 +213,8 @@ int main(void) { &activeScreenBufferDetails ); - // blitter copy the first bitplane row down to the second + buildCopperlist(); - copperlist = prepareNewCopperlist(10000); - - currentCopperlist = addDisplayToCopperlist( - copperlist, - &screenDefinition, - &activeScreenBufferDetails, - &copperlistBitplanePointers - ); - - currentCopperColors = CopperColors; - currentSpriteCopperlist = SpriteCopperlist; - - *(currentCopperlist++) = custom_color + 2; - *(currentCopperlist++) = 0x000; - - *(currentCopperlist++) = custom_color + 4; - *(currentCopperlist++) = 0xfff; - - if (RENDER_SPRITES) { - for (i = 0; i < 8; ++i) { - spriteDataPointer = (uint32_t)&SpriteData; - spriteDataPointer += ((256 + 2) * 4) * i; - - *(currentCopperlist++) = custom_sprite + i * 4; - *(currentCopperlist++) = (spriteDataPointer >> 16); - - *(currentCopperlist++) = custom_sprite + 2 + i * 4; - *(currentCopperlist++) = (spriteDataPointer & 0xffff); - } - } else { - printf("Skipping render\n"); - currentCopperlist = setUpEmptySpritesInCopperlist(currentCopperlist); - } - - *(currentCopperlist++) = custom_color; - *(currentCopperlist++) = 0x3a6; - - for (y = 0; y < 256; ++y) { - if (RENDER_SPRITES) { - for (i = 0; i < 8; ++i) { - // set sprite color - *(currentCopperlist++) = custom_color + 32 + ( - // sprite color group - (i / 2) * 4 + - // 0 is transparent - 1 + - i % 2 - ) * 2; - *(currentCopperlist++) = *(currentSpriteCopperlist++); - - // set sprite position - *(currentCopperlist++) = custom_sprite_control + i * 8; - *(currentCopperlist++) = *(currentSpriteCopperlist++); - - *(currentCopperlist++) = custom_sprite_control + i * 8 + 2; - *(currentCopperlist++) = *(currentSpriteCopperlist++); - } - } else { - printf("Skipping render\n"); - } - - for (i = 3; i < 8; ++i) { - *(currentCopperlist++) = custom_color + (i * 2); - *(currentCopperlist++) = *(currentCopperColors++); - } - - *(currentCopperlist++) = 1 + ((31 + (256 / 4)) << 1) + ((44 + y) << 8); - *(currentCopperlist++) = 0xFFFE; - } - - endCopperlist(currentCopperlist); /* copperlist debugging @@ -248,7 +255,7 @@ int main(void) { if ((ciaa.ciapra >> 6) != 3) break; - //i++; + i++; i %= FRAMES_FOR_SCREEN; } diff --git a/smakefile b/smakefile index 62587b1..5f663d9 100644 --- a/smakefile +++ b/smakefile @@ -8,8 +8,8 @@ all: main .s.o: genam -l $*.s -system.lib: system/system.o system/copper.o system/blitter.o - sc objectlibrary=system.lib system/system.o system/copper.o system/blitter.o +system.lib: system/system.o system/copper.o system/blitter.o system/debug.o + sc objectlibrary=system.lib system/system.o system/copper.o system/blitter.o system/debug.o main: $(MAIN_OBJS) sc link to main math=standard $(MAIN_OBJS) diff --git a/system/copper.h b/system/copper.h index 3b82d7e..0d86ebf 100644 --- a/system/copper.h +++ b/system/copper.h @@ -3,6 +3,28 @@ #include "../types.h" +/** +* Sets register to value over two Copper words. +* Side-effect: Advances ptr by 4. +*/ +#define COPPERLIST_MOVE(ptr,register,value) \ + *(ptr++) = register; \ + *(ptr++) = value; + +#define COPPERLIST_WAIT(ptr,x,y,mask) \ + *(ptr++) = 1 + (x << 1) + (y << 8); \ + *(ptr++) = mask; + +/** +* Sets high/low registers to pointer value over four Copper words. +* Side-effect: Advances ptr by 8. +*/ +#define COPPERLIST_MOVE_POINTER(ptr,register,pointerValue) \ + *(ptr++) = register; \ + *(ptr++) = (pointerValue >> 16); \ + *(ptr++) = register + 2; \ + *(ptr++) = (pointerValue & 0xffff); + uint16_t * prepareNewCopperlist(uint16_t size_b); void setCopperlist(uint16_t *copperlist); void freeCopperlist(uint16_t *copperlist); diff --git a/system/debug.c b/system/debug.c new file mode 100644 index 0000000..7096087 --- /dev/null +++ b/system/debug.c @@ -0,0 +1,11 @@ +#include "debug.h" + +volatile short *dbg = (volatile short *)0x100; + +/** +* Create a two-word write watch in the UAE debugger at memory location 0x100 +* and call this function. +*/ +void triggerUAEDebugger() { + *(dbg) = 0; +} diff --git a/system/debug.h b/system/debug.h new file mode 100644 index 0000000..8f57936 --- /dev/null +++ b/system/debug.h @@ -0,0 +1,6 @@ +#ifndef __SYSTEM_DEBUG__ +#define __SYSTEM_DEBUG__ + +void triggerUAEDebugger(void); + +#endif diff --git a/system/sprite.h b/system/sprite.h new file mode 100644 index 0000000..15172e3 --- /dev/null +++ b/system/sprite.h @@ -0,0 +1,7 @@ +#define SPRPOS(x, y) (((y & 0xff) << 8) + ((x & 0x1fe) >> 1)) +#define SPRCTL(x, y, height) ( \ + ((height & 0xff) << 8) + \ + ((y & 0x100) >> 6) + \ + ((height & 0x100) >> 7) + \ + (x & 1) \ +) diff --git a/types.h b/types.h index 314a17e..bd1a802 100644 --- a/types.h +++ b/types.h @@ -3,6 +3,14 @@ #include +/** +* Get the offset from the beginning of a struct. Useful for building +* references to custom chip registers dynamically for copperlists. +* +* @see Custom +*/ +#define offsetof(s, m) &((struct s *)0)->m + typedef unsigned short color_t; typedef unsigned long uint32_t; typedef unsigned short uint16_t;