bigmessowires Posted January 21 Share Posted January 21 I'm looking for help with optimizing or rewriting a key piece of code in a puzzle game that I'm working on. The game involves drawing horizontal rows of colored beans. There are 10 beans per row, with 8 possible colors for each bean, which makes 3 x 10 = 30 bits of state information per row, stored in 4 bytes of RAM with 2 extra bits unused. The three bits of state for each bean is used as an index into a ROM palette table to get the actual Atari color value to draw. The 10 color values for the current row of beans are stored in RAM, to be used by the kernel. My code for this works, but it's super slow, and requires a whopping 8 scan lines worth of time to set up the colors for the next row of beans. Ugh. How can I do better? My algorithm is: get the state bits for the current row of beans into A initialize the bean index Loop: save A to Temp mask the bottom 3 bits transfer the masked value to an index register perform an indexed lookup into the Palette table, to find the color store the color in the Color table entry for the current bean restore A from Temp shift the state bits right by 3 places adjust the bean index for the next bean continue Loop until all 10 beans are done Here's the actual code for this particular section. The full code file is also attached if you want to see some colorful but boring static beans. ; RowNum (byte) - index of the next row of beans to be drawn ; State (48 bytes) - state info for each row of beans, 30 bits per row, 4 bytes ; Colors (10 bytes) - the Atari color for each bean in the row, used by kernel ; Start a new row of beans ; Set up the Colors for the row. ; Several blank lines will be drawn while this is all happening. NormalRow lda RowNum ; each row has 4 bytes of state, so multiply RowNum by 4 for state index asl asl tax ; get the 4 bytes of state for this row lda State,X sta Temp0 lda State+1,X sta Temp1 lda State+2,X sta Temp2 lda State+3,X ; set up Colors from the row's state ; the state info has three bits for each of the ten beans (A-J), like this: ; S0 S1 S2 S3 (accumulator) ; 0AAABBBC CCDDDEEE 0FFFGGGH HHIIIJJJ ldx #9 ; begin with bean column 9 and work down to 0 ; state of first 5 beans come from accumulator and Temp2 jsr StoreColorFromState ; JJJ ror Temp2 ror lsr lsr jsr StoreColorFromState ; III lsr lsr lsr jsr StoreColorFromState ; HHH lda Temp2 jsr StoreColorFromState ; GGG lsr lsr lsr jsr StoreColorFromState ; FFF ; state of second 5 beans come from Temp1 and Temp0 lda Temp1 jsr StoreColorFromState ; EEE ror Temp0 ror lsr lsr jsr StoreColorFromState ; DDD lsr lsr lsr jsr StoreColorFromState ; CCC lda Temp0 jsr StoreColorFromState ; BBB lsr lsr lsr jsr StoreColorFromState ; AAA ; all done, now continue into the kernel (not shown)... ; helper subroutine for bean coloring, it's very slow StoreColorFromState pha ; save A before masking and #7 ; mask low 3 bits tay lda Palette,Y ; lookup into 8-entry palette table sta Colors,X ; save the color entry for this bean dex ; next bean pla ; restore A to its original unmasked value rts ; these are the Atari color values for the 8 bean types Palette .byte $42, $C6, $86, $1A, $56, $36, $4E, $AC The beans themselves are drawn as playfield pixels with a kernel that rapidly changes COLUPF, using the values in the Colors array. I couldn't get the CPU updates of COLUPF to align correctly with all 10 beans, so one of the beans is actually a player sprite that sits on top of a broken bean. beans.dasm Quote Link to comment Share on other sites More sharing options...
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.