Say hello to D-Pad Hero 2 repository
[dpadhero2.git] / common / nmi.asm
bloba6737cf6c9e143e9cb5c02acf022c9e65838eed2
1 .include "ppu.h"
2 .include "sprite.h"
3 .include "tablecall.h"
5 .ifdef MMC
6 .if MMC == 3
7 .include "../mmc/mmc3.h"
8 .endif
9 .endif
11 .dataseg
13 .extrn ppu:ppu_state
14 .extrn sprites:byte
16 .ifdef MMC
17 .if MMC == 3
18 .extrn chr_banks:byte
19 .extrn chr_addr_toggle:byte
20 .extrn irq_count:byte
21 .endif
22 .endif
24 in_nmi .byte
25 frame_count .byte
26 main_cycle .byte
28 .public frame_count
29 .public main_cycle
31 .codeseg
33 .public nmi
35 .extrn flush_ppu_buffer:proc
36 .ifndef NO_JOYPAD0
37 .extrn read_joypad:proc
38 .endif
39 .ifndef NO_JOYPAD1
40 .extrn read_joypad1:proc
41 .endif
42 .ifndef NO_SOUND
43 .extrn update_sound:proc
44 .endif
45 .extrn table_call:proc
46 .extrn update_timers:proc
48 nmi:
49 sei
50 pha ; preserve A
51 txa
52 pha ; preserve X
53 tya
54 pha ; preserve Y
56 lda ppu.ctrl1
57 sta PPU_CTRL1_REG
59 lda in_nmi
60 bne skip_nmi ; skip the next part if the frame couldn't
61 ; finish before the NMI was triggered
62 inc in_nmi
63 inc frame_count
65 jsr flush_ppu_buffer
67 ; update PPU control register 0
68 lda ppu.ctrl0
69 sta PPU_CTRL0_REG
71 ; update scroll registers
72 lda PPU_STATUS_REG ; reset H/V scroll flip flop
73 lda ppu.scroll_x
74 sta PPU_SCROLL_REG
75 lda ppu.scroll_y
76 sta PPU_SCROLL_REG
78 .ifndef NO_SPRITE_DMA
79 lda #0
80 sta SPRITE_ADDR_REG ; reset SPR-RAM address
81 lda #>sprites
82 sta SPRITE_DMA_REG
83 .endif
85 .ifdef MMC
86 .if MMC == 3
87 ; set CHR banks
88 ldx #$05
89 - txa
90 ora chr_addr_toggle
91 sta MMC3_CTRL_REG
92 lda chr_banks,x
93 sta MMC3_PAGE_REG
94 dex
95 bpl -
97 ; set up IRQ if requested
98 lda irq_count
99 beq +
100 sta MMC3_IRQ_COUNTER_REG
101 sta MMC3_IRQ_LATCH_REG
102 sta MMC3_IRQ_ENABLE_REG
104 .endif
105 .endif
107 ; read joypad(s)
108 .ifndef NO_JOYPAD0
109 jsr read_joypad
110 .endif
111 .ifndef NO_JOYPAD1
112 jsr read_joypad1
113 .endif
115 .ifndef NO_SOUND
116 jsr update_sound
117 .endif
119 ; update timers
120 jsr update_timers
122 jsr go_main_cycle
124 dec in_nmi ; = 0, NMI done
125 skip_nmi:
127 tay ; restore Y
129 tax ; restore X
130 pla ; restore A
133 go_main_cycle:
134 lda main_cycle
135 jsr table_call
136 .include "maincycletable.asm"
138 .end