Guest User

Untitled

a guest
Sep 25th, 2023
1,176
1
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; The computer-scientifically-minded may wish to advance.
  2. ;
  3. ; It's dangerous to go alone, take this:
  4. ; http://6502.org/tutorials/6502opcodes.html
  5.  
  6.  
  7. ; So, hey, let's start somewhat simple. This is a loop in 6502 ASM!
  8. ; It writes 123 to memory addresses $100,$101,$102,$103
  9. ;
  10. ;lda #123
  11. ;ldy #0
  12. ;@loop:
  13. ;   sta $100,y ;store 123 at memory address $100+y
  14. ;   iny
  15. ;   cpy #3
  16. ;   bne @loop
  17.  
  18. ; It would take less CPU cycles to simply write:
  19. ;
  20. ;lda #123
  21. ;sta $100
  22. ;sta $101
  23. ;sta $102
  24. ;sta $103
  25. ;
  26. ; And it even frees up a precious CPU register (y) to potentially do other
  27. ; things with! Anyway, this is what's called an unrolled loop.
  28.  
  29. ; The ca65 assembler provides a neat way to write these:
  30. ;
  31. ;lda #123
  32. ;.repeat 4,I
  33. ;  sta $100+I
  34. ;.endrepeat
  35.  
  36.  
  37. ;===================================================================
  38. ; ADAPTED FROM BÖBL
  39. ; Rendering water surface CHR tiles from a series of surface points
  40.  
  41.  
  42. .segment "BSS_SURFACE_POINTS" ;page-aligned memory for faster access
  43.     surfacePoints: .res 32*8 ;256 pixels of water surface in total
  44. .segment "ZEROPAGE"
  45.     spBackup: .res 1
  46. .code
  47.  
  48.  
  49. generateChrTiles:
  50.     ; Backup stack pointer
  51.     tsx
  52.     stx spBackup
  53.    
  54.     ; Set stack pointer to $00, will cause the following code
  55.     ; to reuse the unused lower portion of the stack (address $100+)
  56.     ; to store the CHR tiles
  57.     ldx #$00
  58.     txs
  59.    
  60.     ldy #0 ; Index into surfacePoints
  61.    
  62.     ; Render all tiles at once for this example. Böbl renders 16 on even, 16 on odd frames.
  63.     lda #32
  64.     sta tileCount
  65.    
  66.     ; Generate CHR RAM update data
  67.     @nextTile:
  68.         ; Render one entire tile (height: 8 pixels)
  69.         .repeat 8,J
  70.             ; Render one row of pixels (width: 8 pixels)
  71.             .repeat 8,I
  72.                 ; Set or clear pixel depending on whether this pixel > the y-pos of the water surface
  73.                 ldx surfacePoints+I,y ;4 CPU cycles
  74.                 cpx #$04+J ;2 CPU cycles
  75.                 rol ;2 CPU cycles
  76.             .endrepeat
  77.             pha ; Push byte to stack
  78.         .endrepeat
  79.        
  80.         ; Advance surface point index by one full tile (8 pixels)
  81.         tya
  82.         adc #8
  83.         tay
  84.        
  85.         dec tileCount
  86.         beq @break
  87.             jmp @nextTile
  88.         @break:
  89.    
  90.     ; restore stack pointer so we can resume execution normally
  91.     ldx spBackup
  92.     txs
  93.    
  94.     ; CHR RAM updates may only occur during VBLANK.
  95.     ; So now, on the next VBLANK, you'd copy the bytes we just pushed to the stack to CHR RAM.
  96.     ; Oh, but after every 8 bytes copied, you'll have to advance the CHR RAM address by 8.
  97.     ; We only use 2 colors, i.e. only the first half of the 16 bytes that make up a CHR tile.
  98.     rts
  99.  
Advertisement
Comments
  • allende
    2 years
    # text 0.20 KB | 0 0
    1. 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 ?
    2.  
    3. surfacePoints: .res 32*8 ;256 pixels of water surface in total
    • morphcat
      2 years
      # text 0.27 KB | 0 0
      1. Sorry, way late on this one!
      2.  
      3. I just like to write things this way sometimes, in this case to signal "32 tiles, 8 pixels each".
      4.  
      5. 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