Add tools/ and manual/, move sources to src/
[dpadhero2.git] / src / buttonconfig.asm
blob99ec18fd7bd7cffee13623e7ad73ff3a6a5653ba
2 ; Copyright (C) 2010 Kent Hansen.
4 ; This program is free software; you can redistribute it and/or modify
5 ; it under the terms of the GNU General Public License as published by
6 ; the Free Software Foundation; either version 3 of the License, or
7 ; (at your option) any later version.
9 ; This program is distributed in the hope that it will be useful,
10 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ; GNU General Public License for more details.
14 ; You should have received a copy of the GNU General Public License
15 ; along with this program. If not, see <http://www.gnu.org/licenses/>.
18 .include "common/fade.h"
19 .include "common/joypad.h"
20 .include "common/ldc.h"
21 .include "common/palette.h"
22 .include "common/ppu.h"
23 .include "common/ppubuffer.h"
24 .include "common/sprite.h"
25 .include "common/timer.h"
26 .include "sound/mixer.h"
27 .include "sound/sound.h"
28 .include "sound/sfx.h"
30 .ifdef MMC
31 .if MMC == 3
32 .include "mmc/mmc3.h"
33 .endif
34 .endif
36 .dataseg
38 config_player_index .db ; 0 or 1
39 lane_index .db
40 temp_button_mapping .db[5]
42 .codeseg
44 .public buttonconfig_init
45 .public buttonconfig_main
47 .extrn wipeout:proc
48 .extrn draw_lane_indicator:proc
49 .extrn button_mapping:byte
50 .extrn game_type:byte
52 .extrn pad3d_data:byte
54 .extrn main_cycle:byte
55 .extrn frame_count:byte
57 .proc buttonconfig_init
58 jsr wipeout
60 ; lda #1
61 ; sta config_player_index
63 .ifdef MMC
64 .if MMC == 3
65 lda #16 : sta chr_banks[0]
66 lda #38 : sta chr_banks[1]
67 lda #20 : sta chr_banks[2]
68 lda #21 : sta chr_banks[3]
69 lda #22 : sta chr_banks[4]
70 lda #23 : sta chr_banks[5]
71 .endif
72 .endif
74 jsr draw_bg
76 jsr set_black_palette
77 ldcay @@palette
78 jsr load_palette
80 lda #0 : ldy #31
81 jsr set_fade_range
82 lda #6
83 jsr set_fade_delay
84 jsr start_fade_from_black
86 lda #0
87 sta lane_index
89 inc main_cycle
91 jsr screen_on
92 jsr nmi_on
93 rts
95 @@palette:
96 ; bg
97 .db $0f,$06,$36,$37 ; logo, hearts, VU
98 .db $0f,$1B,$12,$22
99 .db $0f,$2B,$20,$10 ; points, progress bar
100 .db $0f,$38,$10,$00 ; pad, VU
101 ; sprites
102 .db $0f,$02,$22,$30 ; target - blue
103 .db $0f,$0A,$2A,$30 ; target - green
104 .db $0f,$08,$28,$30 ; target - yellow
105 .db $0f,$06,$16,$30 ; target - red
106 .endp
108 .proc draw_bg
109 ldcay pad3d_data
110 jsr write_ppu_data_at
111 ldcay @@data
112 jsr write_ppu_data_at
113 lda game_type
114 beq +
116 + ldcay @@player_data
117 jsr write_ppu_data_at
118 ldy #$20 : lda #$93 : ldx #1
119 jsr begin_ppu_string
120 lda config_player_index
121 clc : adc #$D1
122 jsr put_ppu_string_byte
123 jsr end_ppu_string
124 jmp flush_ppu_buffer
126 @@data:
127 .charmap "font.tbl"
128 .db $20,$E7,19
129 .char "PUSH DESIRED BUTTON"
130 ; L R U D SEL B A (buttons initially available)
131 .db $21,$C9,$0E,$B0,$B1,$B2,$B3,$B4,$B5,$B6,$B7,$B8,$B9,$BA,$BB,$BC,$BD
132 .db $21,$E9,$0E,$C0,$C1,$C2,$C3,$C4,$C5,$C6,$C7,$C8,$C9,$CA,$CB,$CC,$CD
133 .db $23,$C0,$50,$FF
134 .db $23,$E8,$48,$F0
135 .db 0
137 @@player_data:
138 .db $20,$8C,6 : .char "PLAYER"
139 .db 0
140 .endp
142 ; In: A = bitmask (only one 1)
143 ; Out: Y = bit index (0..7)
144 .proc joypad_mask_to_button
145 ldy #$FF
146 - lsr
148 bcc -
150 .endp
152 ; In: A = physical button index (0..7)
153 ; Out: CF=1 if mapped, CF=0 if not
154 .proc is_p1_physical_button_mapped
155 ldy lane_index
156 - dey
157 bmi +
158 cmp button_mapping,y
159 bne -
162 + clc
164 .endp
166 ; In: A = physical button index (0..7)
167 ; Out: CF=1 if mapped, CF=0 if not
168 .proc is_p2_physical_button_mapped
169 ldy lane_index
170 - dey
171 bmi +
172 cmp button_mapping+5,y
173 bne -
176 + clc
178 .endp
180 ; In: A = physical button index (0..7)
181 ; Out: CF=1 if mapped, CF=0 if not
182 .proc is_physical_button_mapped
183 ldy config_player_index
184 bne +
185 jmp is_p1_physical_button_mapped
186 + jmp is_p2_physical_button_mapped
187 .endp
189 ; A = NES button (0..7)
190 .proc move_display_button
192 ; erase the button from the on-screen available list
194 lda @@display_button_index,y
196 adc #$C9
198 ; upper half
199 ldy #$21 : ldx #$42
200 jsr begin_ppu_string
201 lda #0
202 jsr put_ppu_string_byte
203 jsr end_ppu_string
204 ; lower half
207 adc #$20
208 ldy #$21 : ldx #$42
209 jsr begin_ppu_string
210 lda #0
211 jsr put_ppu_string_byte
212 jsr end_ppu_string
214 ; draw button at its mapped position
215 ldy lane_index
216 lda @@virtual_button_offset,y
218 adc #$80
219 ldy #$22 : ldx #2
220 jsr begin_ppu_string
223 lda @@display_button_index,y
225 adc #$B0
226 sta ppu_buffer,x
228 ora #1
229 sta ppu_buffer,x
231 jsr end_ppu_string
232 ; lower half
233 lda ppu_buffer-5,x
235 lda ppu_buffer-4,x
237 adc #$20
238 ldx #2
239 jsr begin_ppu_string
240 lda ppu_buffer-5,x
242 adc #$10
243 sta ppu_buffer,x
245 ora #1
246 sta ppu_buffer,x
248 jmp end_ppu_string
250 @@display_button_index:
251 .db 1,0,3,2,0,4,5,6
253 @@virtual_button_offset:
254 .db 3,10,15,21,27
255 .endp
257 .proc check_input
258 ldy config_player_index
259 lda joypad0_posedge,y
260 bne +
262 + and #JOYPAD_BUTTON_START
263 bne @@start
265 lda joypad0_posedge,y
266 jsr joypad_mask_to_button
268 pha ; save button index
269 jsr is_physical_button_mapped
270 bcc @@map_it
271 ; this button is already mapped
272 ; ### play error SFX?
276 @@map_it:
277 lda config_player_index
278 asl : asl : adc config_player_index ; * 5
279 ora lane_index
281 pla ; restore button index
282 ; ### use temp_button_mapping
283 sta button_mapping,y
285 jsr move_display_button
287 inc lane_index
288 lda lane_index
289 cmp #5
290 beq @@done
293 @@done:
294 ; ### ask for confirmation
295 ; ### go back to game?
296 lda #7
297 sta main_cycle
300 @@start:
301 ; abort?
303 .endp
305 .proc highlight_current_lane
306 lda frame_count
307 and #2
308 beq +
309 ldy lane_index
310 jsr draw_lane_indicator
311 + rts
312 .endp
314 .proc buttonconfig_main
315 jsr reset_sprites
316 jsr highlight_current_lane
317 jsr check_input
319 .endp
321 .end