.ResetScreen JSR WaitFor3xVBlank ; Wait for three VBlanks to pass LDA #HI(20*32) ; Set iconBarRow(1 0) = 20*32 STA iconBarRow+1 ; LDA #LO(20*32) ; So the icon bar is on row 20 STA iconBarRow ; We now want to set all the colours in all the palettes ; to black, to hide anything that's on-screen LDA #$3F ; Set PPU_ADDR = $3F00, so it points to the background STA PPU_ADDR ; colour palette entry in the PPU LDA #$00 STA PPU_ADDR LDA #$0F ; Set A to $0F, which is the HSV value for black LDX #31 ; There are 32 bytes in the background and sprite ; palettes in the PPU, so set a loop counter in X to ; count through them all .rscr1 STA PPU_DATA ; Send A to the PPU to set palette entry X to black DEX ; Decrement the loop counter BPL rscr1 ; Loop back until we have set all the palette entries to ; black ; We now want to reset both PPU nametables to show blank ; tiles (i.e. tile 0), so nothing is shown on-screen ; ; The two nametables and associated attribute tables are ; structured like this in the PPU: ; ; * PPU_NAME_0 ($2000 to $23BF) ; * PPU_ATTR_0 ($23C0 to $23FF) ; * PPU_NAME_1 ($2400 to $27BF) ; * PPU_ATTR_1 ($27C0 to $27FF) ; ; Each nametable/attribute table consists of 1024 bytes ; (i.e. four pages of 256 bytes), and because the tables ; are consecutive in PPU memory, we can zero the whole ; lot by sending eight pages of zeroes to the PPU, ; tarting at the start of nametable 0 at PPU_NAME_0 LDA #HI(PPU_NAME_0) ; Set PPU_ADDR to PPU_NAME_0, so it points to nametable STA PPU_ADDR ; 0 in the PPU LDA #LO(PPU_NAME_0) STA PPU_ADDR LDA #0 ; Set A = 0 so we can send it to the PPU to fill both ; PPU nametables with blank tiles LDX #8 ; We want to zero 8 pages of 256 bytes, so set a page ; counter in X LDY #0 ; Set Y as a byte counter to count through each of the ; 256 bytes in a page of memory .rscr2 STA PPU_DATA ; Zero the next entry in the nametable DEY ; Decrement the byte counter BNE rscr2 ; Loop back until we have zeroed a whole page JSR WaitFor3xVBlank ; Wait for three VBlanks to pass LDA #0 ; Set A = 0 again, as it gets changed by WaitFor3xVBlank DEX ; Decrement the page counter BNE rscr2 ; Loop back until we have zeroed 8 pages of nametable in ; the PPU LDA #245 ; Set screenReset = 245, though this is not used STA screenReset ; anywhere, so this has no effect on anything STA imageSentToPPU ; Set imageSentToPPU = 245 to denote that we have sent ; the inventory icon image to the PPU ; We now send patterns 0 to 4 to the PPU, to set up the ; blank tile (pattern 0), the three box edges (patterns ; 1 to 3) and the top-left corner of the icon bar ; (pattern 4) ; ; We do this for both pattern tables LDA #HI(PPU_PATT_0) ; Set PPU_ADDR to PPU_PATT_0, so it points to pattern STA PPU_ADDR ; table 0 in the PPU LDA #LO(PPU_PATT_0) STA PPU_ADDR LDY #0 ; Set Y to use as an index counter as we work through ; the boxEdgeImages table and send its data to the PPU LDX #80 ; The boxEdgeImages table contains five patterns with 16 ; bytes per pattern, so that's a total of 80 bytes to ; send to the PPU, so set X as a byte counter .rscr3 LDA boxEdgeImages,Y ; Send the Y-th entry from the boxEdgeImages table to STA PPU_DATA ; the PPU INY ; Increment the index into the boxEdgeImages table to ; point at the next byte DEX ; Decrement the byte counter BNE rscr3 ; Loop back until we have sent all 80 bytes to the PPU LDA #HI(PPU_PATT_1) ; Set PPU_ADDR to PPU_PATT_1, so it points to pattern STA PPU_ADDR ; table 1 in the PPU LDA #LO(PPU_PATT_1) STA PPU_ADDR LDY #0 ; Set Y to use as an index counter as we work through ; the boxEdgeImages table and send its data to the PPU LDX #80 ; The boxEdgeImages table contains five patterns with 16 ; bytes per pattern, so that's a total of 80 bytes to ; send to the PPU, so set X as a byte counter .rscr4 LDA boxEdgeImages,Y ; Send the Y-th entry from the boxEdgeImages table to STA PPU_DATA ; the PPU INY ; Increment the index into the boxEdgeImages table to ; point at the next byte DEX ; Decrement the byte counter BNE rscr4 ; Loop back until we have sent all 80 bytes to the PPU ; We now reset the sprite buffer by setting all 64 ; sprites as follows: ; ; * Set the coordinates to (0, 240), which is just ; below the bottom of the screen, so the sprite is ; hidden from view ; ; * Set the pattern number to 254 (this value seems to ; be arbitrary, but is possibly 254 so that sprite 0 ; keeps using pattern 254 throughout the reset) ; ; * Set the attributes so the sprite uses palette 3, ; is shown in front of the background, and is not ; flipped in either direction LDY #0 ; We are about to loop through the sprite buffer, so set ; a byte index in Y .rscr5 LDA #240 ; Set the y-coordinate for this sprite to 240, to move STA ySprite0,Y ; it off the bottom of the screen INY ; Increment Y to point to the second byte for this ; sprite, i.e. pattSprite0,Y LDA #254 ; Set the pattern number for this sprite to 254 STA ySprite0,Y INY ; Increment Y to point to the third byte for this ; sprite, i.e. attrSprite0,Y LDA #%00000011 ; Set the attributes for this sprite as follows: STA ySprite0,Y ; ; * Bits 0-1 = sprite palette 3 ; * Bit 5 clear = show in front of background ; * Bit 6 clear = do not flip horizontally ; * Bit 7 clear = do not flip vertically INY ; Increment Y to point to the fourth byte for this ; sprite, i.e. xSprite0,Y LDA #0 ; Set the x-coordinate for this sprite to 0 STA ySprite0,Y INY ; Increment Y to point to the first byte for the next ; sprite BNE rscr5 JSR SendDashImageToPPU ; Unpack the dashboard image and send it to patterns 69 ; to 255 in pattern table 0 in the PPU ; We now set up sprite 0, which is used to detect when ; the PPU starts drawing the icon bar, so this places ; the sprite at the right side of the screen, just ; above the icon bar, so when the PPU gets to this part ; of the screen, it will set the sprite 0 collision flag ; which we can then detect LDA #157+YPAL ; Set sprite 0 as follows: STA ySprite0 ; LDA #254 ; * Set the coordinates to (248, 157) STA pattSprite0 ; LDA #248 ; * Set the pattern number to 254 STA xSprite0 ; LDA #%00100011 ; * Set the attributes as follows: STA attrSprite0 ; ; * Bits 0-1 = sprite palette 3 ; * Bit 5 set = show behind background ; * Bit 6 clear = do not flip horizontally ; * Bit 7 clear = do not flip vertically ; We now set sprites 1 to 4 so they contain the four ; corners of the icon bar pointer: ; ; * Sprite 1 = top-left corner ; * Sprite 2 = top-right corner ; * Sprite 3 = bottom-left corner ; * Sprite 4 = bottom-right corner LDA #251 ; Set sprites 1 and 2 to use pattern 251 STA pattSprite1 STA pattSprite2 LDA #253 ; Set sprites 3 and 4 to use pattern 253 STA pattSprite3 STA pattSprite4 LDA #%00000011 ; Set the attributes for sprite 1 as follows: STA attrSprite1 ; ; * Bits 0-1 = sprite palette 3 ; * Bit 5 clear = show in front of background ; * Bit 6 clear = do not flip horizontally ; * Bit 7 clear = do not flip vertically LDA #%01000011 ; Set the attributes for sprite 2 as follows: STA attrSprite2 ; ; * Bits 0-1 = sprite palette 3 ; * Bit 5 clear = show in front of background ; * Bit 6 set = flip horizontally ; * Bit 7 clear = do not flip vertically LDA #%01000011 ; Set the attributes for sprite 3 as follows: STA attrSprite3 ; ; * Bits 0-1 = sprite palette 3 ; * Bit 5 clear = show in front of background ; * Bit 6 set = flip horizontally ; * Bit 7 clear = do not flip vertically LDA #%00000011 ; Set the attributes for sprite 4 as follows: STA attrSprite4 ; ; * Bits 0-1 = sprite palette 3 ; * Bit 5 clear = show in front of background ; * Bit 6 clear = do not flip horizontally ; * Bit 7 clear = do not flip vertically JSR WaitFor3xVBlank ; Wait for three VBlanks to pass LDA #0 ; Write 0 to OAM_ADDR so we can use OAM_DMA to send STA OAM_ADDR ; sprite data to the PPU LDA #$02 ; Write $02 to OAM_DMA to upload 256 bytes of sprite STA OAM_DMA ; data from the sprite buffer at $02xx into the PPU LDA #0 ; Reset all the bitplanes to 0 STA nmiBitplane STA drawingBitplane STA hiddenBitplane LDA #HI(PPU_PATT_1) ; Set ppuPatternTableHi to the high byte of PPU pattern STA ppuPatternTableHi ; table 1, which is the table we use for drawing dynamic ; tiles LDA #0 ; Set nmiBitplane8 to 8 * nmiBitplane, which is 0 STA nmiBitplane8 LDA #HI(PPU_NAME_0) ; Set ppuNametableAddr(1 0) to the address of pattern STA ppuNametableAddr+1 ; table 0 in the PPU LDA #LO(PPU_NAME_0) STA ppuNametableAddr LDA #%00101000 ; Set both bitplane flags as follows: STA bitplaneFlags ; STA bitplaneFlags+1 ; * Bit 2 clear = send tiles up to configured numbers ; * Bit 3 set = clear buffers after sending data ; * Bit 4 clear = we've not started sending data yet ; * Bit 5 set = we have already sent all the data ; * Bit 6 clear = only send pattern data to the PPU ; * Bit 7 clear = do not send data to the PPU ; ; Bits 0 and 1 are ignored and are always clear ; ; The NMI handler will now start sending data to the PPU ; according to the above configuration, splitting the ; process across multiple VBlanks if necessary LDA #4 ; Set the number of the first and last tiles to send STA clearingPattern ; from the PPU to 4, which is the first tile after the STA clearingPattern+1 ; blank tile (tile 0) and the box edges (tiles 1 to 3), STA clearingNameTile ; which are the only fixed tiles in both bitplanes STA clearingNameTile+1 ; STA sendingPattern ; This ensures that both buffers are almost entirely STA sendingPattern+1 ; cleared out by the NMI, as we set bit 3 in the STA sendingNameTile ; bitplane flags above STA sendingNameTile+1 LDA #$0F ; Set the hidden and visible colours to $0F, which is STA hiddenColour ; the HSV value for black, and do the same for the STA visibleColour ; colours to use for palette entries 2 and 3 in the STA paletteColour2 ; non-space views STA paletteColour3 LDA #0 ; Configure the NMI handler not to send palette data to STA updatePaletteInNMI ; the PPU STA QQ11a ; Set the old view type in QQ11a to $00 (Space view with ; no fonts loaded) LDA #$FF ; Set bit 7 of screenFadedToBlack to indicate that we STA screenFadedToBlack ; have faded the screen to black JSR WaitFor3xVBlank ; Wait for three VBlanks to pass LDA #%10010000 ; Set A to use as the new value for PPU_CTRL below STA ppuCtrlCopy ; Store the new value of PPU_CTRL in ppuCtrlCopy so we ; can check its value without having to access the PPU STA PPU_CTRL ; Configure the PPU by setting PPU_CTRL as follows: ; ; * Bits 0-1 = base nametable address %00 ($2000) ; * Bit 2 clear = increment PPU_ADDR by 1 each time ; * Bit 3 clear = sprite pattern table is at $0000 ; * Bit 4 set = background pattern table is at $1000 ; * Bit 5 clear = sprites are 8x8 pixels ; * Bit 6 clear = use PPU 0 (the only option on a NES) ; * Bit 7 set = enable VBlank NMI generation RTS ; Return from the subroutineName: ResetScreen [Show more] Type: Subroutine Category: Start and end Summary: Reset the screen by clearing down the PPU, setting all colours to black, and resetting the screen-related variablesContext: See this subroutine in context in the source code References: This subroutine is called as follows: * ResetScreen_b3 calls ResetScreen
[X]
Configuration variable OAM_ADDR = $2003
The OAM address port (this is not used in Elite)
[X]
Configuration variable OAM_DMA = $4014
The OAM DMA register, which is used to initiate a DMA transfer of sprite data to the PPU
[X]
Configuration variable PPU_ADDR = $2006
The PPU address register, which is used to set the destination address within the PPU when sending data to the PPU
[X]
Configuration variable PPU_CTRL = $2000
The PPU control register, which allows us to choose which nametable and pattern table is being displayed, and to toggle the NMI interrupt at VBlank
[X]
Configuration variable PPU_DATA = $2007
The PPU data register, which is used to send data to the PPU, to the address specified in PPU_ADDR
[X]
Configuration variable PPU_NAME_0 = $2000
The address of nametable 0 in the PPU
[X]
Configuration variable PPU_PATT_0 = $0000
The address of pattern table 0 in the PPU
[X]
Configuration variable PPU_PATT_1 = $1000
The address of pattern table 1 in the PPU
[X]
Subroutine SendDashImageToPPU (category: PPU)
Unpack the dashboard image and send it to patterns 69 to 255 in pattern table 0 in the PPU
[X]
Subroutine WaitFor3xVBlank (category: Utility routines)
Wait for three VBlanks to pass
[X]
Configuration variable YPAL = 6 AND _PAL
A margin of 6 pixels that is applied to a number of y-coordinates for the PAL version only (as the PAL version has a taller screen than NTSC)
[X]
Variable attrSprite0 in workspace Sprite buffer
Attributes for sprite 0
[X]
Variable attrSprite1 in workspace Sprite buffer
Attributes for sprite 1
[X]
Variable attrSprite2 in workspace Sprite buffer
Attributes for sprite 2
[X]
Variable attrSprite3 in workspace Sprite buffer
Attributes for sprite 3
[X]
Variable attrSprite4 in workspace Sprite buffer
Attributes for sprite 4
[X]
Variable bitplaneFlags in workspace WP
Flags for bitplane 0 that control the sending of data for this bitplane to the PPU during VBlank in the NMI handler
[X]
Variable boxEdgeImages (category: Drawing the screen)
Image data for patterns 0 to 4
[X]
Variable clearingNameTile in workspace ZP
The number of the first tile to clear in nametable buffer 0 when the NMI handler clears tiles, divided by 8
[X]
Variable clearingPattern in workspace ZP
The number of the first pattern to clear in pattern buffer 0 when the NMI handler clears patterns
[X]
Variable drawingBitplane in workspace ZP
Flipped manually by calling FlipDrawingPlane, controls whether we are showing nametable/palette buffer 0 or 1
[X]
Variable iconBarRow in workspace ZP
The row on which the icon bar appears
[X]
Variable imageSentToPPU in workspace WP
Records when images have been sent to the PPU or unpacked into the buffers, so we don't repeat the process unnecessarily
[X]
Variable nmiBitplane in workspace ZP
The number of the bitplane (0 or 1) that is currently being processed in the NMI handler during VBlank
[X]
Variable nmiBitplane8 in workspace ZP
Used when sending patterns to the PPU to calculate the address offset of bitplanes 0 and 1
[X]
Variable paletteColour2 in workspace ZP
Contains the colour to use for palette entry 2 in the current (non-space) view
[X]
Variable paletteColour3 in workspace ZP
Contains the colour to use for palette entry 3 in the current (non-space) view
[X]
Variable pattSprite0 in workspace Sprite buffer
Pattern number for sprite 0
[X]
Variable pattSprite1 in workspace Sprite buffer
Pattern number for sprite 1
[X]
Variable pattSprite2 in workspace Sprite buffer
Pattern number for sprite 2
[X]
Variable pattSprite3 in workspace Sprite buffer
Pattern number for sprite 3
[X]
Variable pattSprite4 in workspace Sprite buffer
Pattern number for sprite 4
[X]
Variable ppuCtrlCopy in workspace ZP
Contains a copy of PPU_CTRL, so we can check the PPU configuration without having to access the PPU
[X]
Variable ppuNametableAddr in workspace ZP
Address of the current PPU nametable
[X]
Variable ppuPatternTableHi in workspace ZP
High byte of the address of the PPU pattern table to which we send patterns
[X]
Label rscr1 is local to this routine
[X]
Label rscr2 is local to this routine
[X]
Label rscr3 is local to this routine
[X]
Label rscr4 is local to this routine
[X]
Label rscr5 is local to this routine
[X]
Variable screenFadedToBlack in workspace WP
Records whether the screen has been faded to black
[X]
Variable screenReset in workspace WP
Gets set to 245 when the screen is reset, but this value is only read once (in SetupViewInNMI) and the value is ignored, so this doesn't have any effect
[X]
Variable sendingNameTile in workspace ZP
The number of the most recent tile that was sent to the PPU nametable by the NMI handler for bitplane 0 (or the number of the first tile to send if none have been sent), divided by 8
[X]
Variable sendingPattern in workspace ZP
The number of the most recent pattern that was sent to the PPU pattern table by the NMI handler for bitplane 0 (or the number of the first pattern to send if none have been sent)
[X]
Variable updatePaletteInNMI in workspace ZP
A flag that controls whether to send the palette data from XX3 to the PPU during NMI
[X]
Variable visibleColour in workspace ZP
Contains the colour to use for pixels that are visible in palette 0, e.g. $2C for cyan
[X]
Variable xSprite0 in workspace Sprite buffer
Screen x-coordinate for sprite 0
[X]
Variable ySprite0 in workspace Sprite buffer
Screen y-coordinate for sprite 0