diff --git a/src/a23d2.c b/src/a23d2.c index d44954e..7ec501b 100644 --- a/src/a23d2.c +++ b/src/a23d2.c @@ -26,7 +26,7 @@ #include // For memcopy -volatile cameraT *_camera = (cameraT *)0x200; // Simulation copy of camera. +volatile cameraT *_camera = (cameraT *)CAMERA_SHARED_START; // Simulation copy of camera. volatile cameraT *_cameraInDatabase; volatile byte *_pointer; @@ -132,7 +132,7 @@ void a23d2Init(void) { POKE(MMU_MEM_BANK_3, A23D2_FAR_BLOCK); // We're going to clobber from 0x80fb to 0x8101. Back it up. - memcpy((byte *)(0x2ff - 0x06), (byte *)A23D2_TEST_DATABASE, 0x06); + scdMemCpy((byte *)(SCRATCH_END - 0x06), (byte *)A23D2_TEST_DATABASE, 0x06); // Initialize A2-3D2 so we can use the "fast entry point" (NXTPT) when rendering. _bytes = 0; @@ -144,11 +144,21 @@ void a23d2Init(void) { _pointer[_bytes++] = 0; _pointer[_bytes++] = END; // Setup complete! -// __attribute__((leaf)) asm volatile("jsr %[addy]":: [addy] "i"(A23D2_ENTRYS)); // Call ENTRYS. This preserves the ZP for us. - __attribute__((leaf)) asm volatile("jsr 0x606c"); // Call ENTRYS. This preserves the ZP for us. + // Save ZP that A2-3D2 is going to clobber. + scdMemCpy((byte *)(SCRATCH_END - COMPILER_ZP_LENGTH), (byte *)COMPILER_ZP_START, COMPILER_ZP_LENGTH); + + // Call ENTRYN. This sets up A2-3D2. We need to preserve processor state and ZP. + __attribute__((leaf)) asm volatile("jsr %[addy]":: [addy] "i"(A23D2_ENTRYN)); + + // Save A2-3D2s variables for later. + scdMemCpy((byte *)VARS_1_START, (byte *)A23D2_VARS_1_START, A23D2_VARS_1_LENGTH); + scdMemCpy((byte *)VARS_2_START, (byte *)A23D2_VARS_2_START, A23D2_VARS_2_LENGTH); + + // Restore ZP that A2-3D2 clobbered. + scdMemCpy((byte *)COMPILER_ZP_START, (byte *)(SCRATCH_END - COMPILER_ZP_LENGTH), COMPILER_ZP_LENGTH); // Put back the RAM we clobbered. - memcpy((byte *)A23D2_TEST_DATABASE, (byte *)(0x2ff - 0x06), 0x06); + scdMemCpy((byte *)A23D2_TEST_DATABASE, (byte *)(SCRATCH_END - 0x06), 0x06); // Move our 3D/2D data buffer at 0x56000 into slot 4. POKE(MMU_MEM_BANK_4, DATABASE_FAR_BLOCK); @@ -174,8 +184,6 @@ void a23d2Init(void) { void a23d2Render(void) { - uint16_t i; - // We need to manually page 0x54000 into 0x6000. // This isn't actually large enough for A2-3D2 but what we should only // lose Apple ][ stuff we're not using anyway. @@ -195,30 +203,25 @@ void a23d2Render(void) { _cameraInDatabase->b = _camera->b; _cameraInDatabase->h = _camera->h; - //memcpy((byte *)(0x2ff - A23D2_ZP_LENGTH), (byte *)A23D2_ZP_START, A23D2_ZP_LENGTH); // Save the ZP we're going to clobber. + // Save ZP that A2-3D2 is going to clobber. + scdMemCpy((byte *)(SCRATCH_END - COMPILER_ZP_LENGTH), (byte *)COMPILER_ZP_START, COMPILER_ZP_LENGTH); - for (i=0; i