Advertisement
tepples

How to wait for NMI

Dec 27th, 2016
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; // Summary
  2. ; // A Very Basic NES ROM that displays 1 color: Blue
  3.  
  4. ; ////////////////////////////////////////////////
  5. ; // Works fine!
  6. .segment "INESHDR"
  7.   .byt "NES",$1A
  8.   .byt 1                ; 1 x 16kB PRG block.
  9.   .byt 1                ; 1 x 8kB CHR block.
  10. ; ////////////////////////////////////////////////
  11.  
  12. .segment "VECTORS"
  13.   .addr nmi, reset, irq
  14.  
  15. .segment "ZEROPAGE"
  16.   retraces:        .res 1
  17.  
  18. PPUCTRL = $2000
  19. PPUMASK = $2001
  20. PPUSTATUS = $2002
  21.  
  22. .segment "CODE"
  23.   .proc reset
  24.     ; Disable interrupts:
  25.     SEI
  26.  
  27.     ; Basic init:
  28.     LDX #0
  29.     STX PPUCTRL     ; General init state; NMIs (bit 7) disabled.
  30.     STX PPUMASK     ; Disable rendering, i.e. turn off background & sprites.
  31.  
  32.     ; PPU Warmup (29658 Cycles)
  33.     BIT PPUSTATUS   ; Clear the VPL Flag if it was set at reset time.
  34.   vwait1:
  35.     BIT PPUSTATUS   ; 27384 Cycles has passed.
  36.     BPL vwait1
  37.   vwait2:
  38.     BIT PPUSTATUS   ; 57165 Cycles has passed.
  39.     BPL vwait2
  40.  
  41.     ; Set stack pointer:
  42.     ;DEX                ; X = $FF
  43.     ;TXS
  44.  
  45.     LDA #%01000000 ; Intensify greens
  46.     STA PPUMASK
  47.  
  48.     LDA #%10000000
  49.     STA PPUCTRL
  50.  
  51.     LDX #60  ; one second
  52.     JSR WaitXFrames
  53.  
  54.     LDA #%10000000 ; Intensify blues
  55.     STA $2001
  56.  
  57.     Forever:
  58.       JMP Forever     ; Infinite loop
  59.   .endproc
  60.  
  61.   .proc nmi
  62.     INC nmi
  63.     RTI
  64.   .endproc
  65.  
  66.   .proc irq
  67.     RTI
  68.   .endproc
  69.  
  70.   .proc WaitXFrames
  71.     LDA retraces
  72.     wait:       ; Wait for a frame
  73.       CMP retraces  ; when the NMI handler changes the value
  74.       BEQ wait      ; in this variable, one frame has elapsed
  75.       DEX       ; X = X - 1
  76.       BNE WaitXFrames  ; if X is not zero, keep going...
  77.       RTS
  78.   .endproc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement