Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; The computer-scientifically-minded may wish to advance.
- ;
- ; It's dangerous to go alone, take this:
- ; http://6502.org/tutorials/6502opcodes.html
- ; So, hey, let's start somewhat simple. This is a loop in 6502 ASM!
- ; It writes 123 to memory addresses $100,$101,$102,$103
- ;
- ;lda #123
- ;ldy #0
- ;@loop:
- ; sta $100,y ;store 123 at memory address $100+y
- ; iny
- ; cpy #3
- ; bne @loop
- ; It would take less CPU cycles to simply write:
- ;
- ;lda #123
- ;sta $100
- ;sta $101
- ;sta $102
- ;sta $103
- ;
- ; And it even frees up a precious CPU register (y) to potentially do other
- ; things with! Anyway, this is what's called an unrolled loop.
- ; The ca65 assembler provides a neat way to write these:
- ;
- ;lda #123
- ;.repeat 4,I
- ; sta $100+I
- ;.endrepeat
- ;===================================================================
- ; ADAPTED FROM BÖBL
- ; Rendering water surface CHR tiles from a series of surface points
- .segment "BSS_SURFACE_POINTS" ;page-aligned memory for faster access
- surfacePoints: .res 32*8 ;256 pixels of water surface in total
- .segment "ZEROPAGE"
- spBackup: .res 1
- .code
- generateChrTiles:
- ; Backup stack pointer
- tsx
- stx spBackup
- ; Set stack pointer to $00, will cause the following code
- ; to reuse the unused lower portion of the stack (address $100+)
- ; to store the CHR tiles
- ldx #$00
- txs
- ldy #0 ; Index into surfacePoints
- ; Render all tiles at once for this example. Böbl renders 16 on even, 16 on odd frames.
- lda #32
- sta tileCount
- ; Generate CHR RAM update data
- @nextTile:
- ; Render one entire tile (height: 8 pixels)
- .repeat 8,J
- ; Render one row of pixels (width: 8 pixels)
- .repeat 8,I
- ; Set or clear pixel depending on whether this pixel > the y-pos of the water surface
- ldx surfacePoints+I,y ;4 CPU cycles
- cpx #$04+J ;2 CPU cycles
- rol ;2 CPU cycles
- .endrepeat
- pha ; Push byte to stack
- .endrepeat
- ; Advance surface point index by one full tile (8 pixels)
- tya
- adc #8
- tay
- dec tileCount
- beq @break
- jmp @nextTile
- @break:
- ; restore stack pointer so we can resume execution normally
- ldx spBackup
- txs
- ; CHR RAM updates may only occur during VBLANK.
- ; So now, on the next VBLANK, you'd copy the bytes we just pushed to the stack to CHR RAM.
- ; Oh, but after every 8 bytes copied, you'll have to advance the CHR RAM address by 8.
- ; We only use 2 colors, i.e. only the first half of the 16 bytes that make up a CHR tile.
- rts
Advertisement
Comments
-
- A question from the bottom of my ignorance why don't you just put 256 number instead of doing the multiplication on this part or 128+128 ?
- surfacePoints: .res 32*8 ;256 pixels of water surface in total
-
- Sorry, way late on this one!
- I just like to write things this way sometimes, in this case to signal "32 tiles, 8 pixels each".
- Since the operands are constants, it can be resolved by ca65 at compile time, no multiplications are happening on the actual NES CPU. Cheers!
Add Comment
Please, Sign In to add comment