Skip to navigation

Graphics: DrawRandomDots

Name: DrawRandomDots [Show more] Type: Subroutine Category: Graphics Summary: Draw 80 randomly positioned dots on the screen
Context: See this subroutine in context in the source code References: This subroutine is called as follows: * DrawStars calls DrawRandomDots

Arguments: A The colour of the dots: * Bit 7 clear = colour 1 (black) * Bit 7 set = colour 2 (white, yellow, cyan or red)
.DrawRandomDots STA dotColour \ Set dotColour to the value of A from above, so we \ draw coloured dots if bit 7 is set or black dots if \ bit 7 is clear LDA #80 \ Set dotCounter = 80 so we draw 80 dots on the screen STA dotCounter .dots1 JSR GetRandomNumber \ Set A to a random number STA screenAddr \ Set the low byte of screenAddr(1 0) to the random \ number in A LDA randomGenerator+1 \ Set randomPixel to the second byte of the random STA randomPixel \ number generator, so this is also a random number AND #&1F \ Reduce the random number to the range 0 to &1F CMP #&1E \ If A >= &1E then loop back to dots1 to choose another BCS dots1 \ random number STA screenAddr+1 \ Set the high byte of screenAddr(1 0) to A, so we now \ have screenAddr(1 0) set to a random number in the \ range 0 to &1DFF \ We can now add this to the address of the start of \ screen memory in viewScreenAddr(1 0) to get the \ address of a random pixel byte within screen memory LDA viewScreenAddr \ Set screenAddr(1 0) = viewScreenAddr(1 0) CLC \ + screenAddr(1 0) ADC screenAddr STA screenAddr LDA viewScreenAddr+1 ADC screenAddr+1 CMP #&80 \ If the high byte in A >= &80 then the new address is BCC dots2 \ past the end of screen memory, so subtract &20 from SBC #&20 \ the high byte so the address wraps around within the \ range of screen memory between &6000 and &8000 .dots2 STA screenAddr+1 \ Store the high byte of the result, so we now have: \ \ screenAddr(1 0) = viewScreenAddr(1 0) \ + screenAddr(1 0) \ \ So screenAddr(1 0) is the address of a random pixel \ byte within screen memory LDA randomPixel \ Take bits 6 and 7 of randomPixel and put them into ROL A \ bits 0 and 1 of X (with all the other bits clear), so ROL A \ X is a random number in the range 0 to 3 that we can ROL A \ use as the pixel number within the pixel byte for the AND #%00000011 \ dot we want to draw TAX LDY #0 \ Set Y = 0 so we can use (screenAddr),Y below to behave \ like AND (screenAddr) or STA (screenAddr) LDA pixelBitMask,X \ Take the existing pixel byte from screenAddr(1 0) and EOR #%11111111 \ clear the two bits for pixel number X (the EOR inverts AND (screenAddr),Y \ the mask in pixelBitMask so that AND'ing the mask will \ clear pixel X while leaving the other pixels alone ORA pixelByteColour1,X \ Change the two bits for pixel X into colour 1 (%01), \ which is black, so that drawing lots of dots will \ slowly turn the screen to black BIT dotColour \ If bit 7 of dotColour is set, flip the two bits of the BPL dots3 \ pixel so they are drawn in colour 2 (%10), for when we EOR pixelBitMask,X \ we want to draw coloured stars instead .dots3 STA (screenAddr),Y \ Store the updated pixel byte in screenAddr(1 0) to \ draw a black dot or a coloured star on-screen DEC dotCounter \ Decrement the dot counter BNE dots1 \ Loop back until we have drawn all 80 dots RTS \ Return from the subroutine