-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.6502
390 lines (315 loc) · 7.89 KB
/
main.6502
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; constants
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
PRG_COUNT = 2
MODE_TITLE = 0
MODE_CHAR_CREATE = 1
MODE_PLAY = 2
MODE_DEATH = 3
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; vars
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
HARD_SPIKE_FLOOR .equ #2
KONAMI_LENGTH .equ #10
.enum $0000 ; engine vars
waiting_for_frame .dsb 1
ppu_mask .dsb 1
ppu_ctrl .dsb 1
nt_update_buf_wi .dsb 1
nt_update_buf_wc .dsb 1
nt_buffer_overflow .dsb 1
game_mode .dsb 1
rng_seed .dsb 2
buttons .dsb 1
button_ondown .dsb 1
paused .dsb 1
generic_pointer .dsb 2
draw_req .dsb 1
register_backup .dsb 3
arguments .dsb 4
returns .dsb 4
nt_buf_in_hi .dsb 1
nt_buf_in_lo .dsb 1
nt_buf_in_dt .dsb 1
.ende
.enum $0300
nt_buffer .dsb 256
.ende
.enum $0400
dungeon_map .dsb 256
.ende
ENEMY_REACTION_TIME .equ 16
.enum $0500
delayed_px .dsb 0
past_px .dsb ENEMY_REACTION_TIME
delayed_py .dsb 0
past_py .dsb ENEMY_REACTION_TIME
.ende
.enum $0600 ; playtime vars
boss_room .dsb 1
player_room .dsb 1
player_floor .dsb 1
player_money .dsb 1
player_jewels .dsb 1
enemies_in_room .dsb 1
actor_data_pointer .dsb 1
actor_sprite_pointer .dsb 1
actor_sprite_rom_pointer .dsb 1
actor_control .dsb 1
.incsrc "src/actor_vars.6502"
readout_update_req .dsb 1
room_update_req .dsb 1
can_move_up_room .dsb 1
can_move_left_room .dsb 1
can_move_right_room .dsb 1
can_move_down_room .dsb 1
; +0 +1 +2
offerings .dsb 3 ; 00|00|0000 0000|0000 0000|0000
; +0[0-1] Spirit resource. 00: gems, 01: healing, 10: mana, 11: agility
; +0[2-3] Fountain resource. 00: defence, 01: strength, 10: max magic, 11: max hp
; +0[4-7] Chest ammount
;
; +1[0-3] Spirit offer cost
; +1[4-7] Spirit offer amount
;
; +2[0-3] Fountain offer
; +2[4-7] Fountain offer amount cost
current_offer_amounts .dsb 1 ; [0-3] cost [4-7] amount
current_offer_type .dsb 1 ; [0] IS TREASURE
; [1] OFFER EXISTS
; [4-6] GIVES
; 000 gems
; 001 healing
; 010 mana
; 011 agl
; 100 def
; 101 str
; 110 max mag
; 111 max hp
; [7] COSTS
; 0: coin, 1: gem
standing_on_stairs .dsb 1
boss_alive .dsb 1
player_dead .dsb 1 ; doubles as death animation frame counter
death_curtain_pos .dsb 1
death_curtain_pos_msb .dsb 1
ai_state_timer .dsb 1
ai_state_msb .dsb 1
; character creation vars
cc_points_spent .dsb 1
cc_cursor_x .dsb 1
cc_cursor_y .dsb 1
;terrible code *requires* you do not reorder this block
cc_hp_sel .dsb 1
cc_mp_sel .dsb 1
cc_str_sel .dsb 1
cc_atk_sel .dsb 1
cc_def_sel .dsb 1
cc_cursor_flash_timer .dsb 1
dun_gen .dsb 1 ; 0=not generated, 1=gen in process, 2=gen complete
gen_working_room .dsb 1
cheat_progress .dsb 1
max_cc_points .dsb 1
.ende
; this was a mistake and I sincerely apologize
.enum $0700
generic_bytes .dsb 255
.ende
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ines header
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.db "NES", $1a ;id
.db PRG_COUNT ;16k prg pages
.db $01 ;8k chr pages
.db $01 ;mirror
.dsb 9, $00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; prg-rom
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.base $10000-(PRG_COUNT*$4000)
.incsrc "src/macros.6502"
Reset:
SEI ; disable IRQs
CLD ; disable decimal mode
LDX #$40
STX $4017 ; disable APU frame IRQ
LDX #$FF
TXS ; Set up stack
INX ; now X = 0
STX $2000 ; disable NMI
STX $2001 ; disable rendering
STX $4010 ; disable DMC IRQs
@vblankwait1: ; First wait for vblank to make sure PPU is ready
BIT $2002
BPL @vblankwait1
@clrmem:
LDA #$00
STA $0000, x
STA $0100, x
STA $0200, x
STA $0300, x
STA $0400, x
STA $0500, x
STA $0600, x
STA $0700, x
INX
BNE @clrmem
@vblankwait2: ; Second wait for vblank, PPU is ready after this
BIT $2002
BPL @vblankwait2
init:
load_pal colorpal1
LDA #1
STA draw_req
;load_nametable 0, room_name
LDA #%10001000 ;enable NMI, sprites from Pattern 1, background from Pattern 0
STA ppu_ctrl
STA $2000
LDA #%00011000 ; enable sprites, enable background
STA ppu_mask
STA $2001
;rng_seed can't be 0
LDA #$42
STA rng_seed+0
; load sprite OAM
LDA #<sprite_data
STA generic_pointer+0
LDA #>sprite_data
STA generic_pointer+1
LDY #0
-:
LDA (generic_pointer),y
STA $0200,y
INY
BNE -
main:
.incsrc "src/controller.6502"
; tick forward prng and xor with controller every frame for more entropy
JSR rng
EOR buttons ;rng returned low byte into A
STA rng_seed+0
;STA byte_to_dec_in
;JSR byte_to_dec
LDA game_mode
CMP #0
BEQ +
CMP #1
BEQ ++
CMP #2
BEQ +++
CMP #3
BEQ ++++
+:
JSR title_code
JMP end_main
++:
JSR cc_code
JMP end_main
+++:
JSR play_code
JMP end_main
++++:
JMP end_main
end_main:
LDA #1
STA waiting_for_frame
-:
LDA waiting_for_frame
BNE -
JMP main
NMI:
PHP
STA register_backup
STX register_backup+1
STY register_backup+2
LDA 255
CLC
ADC #1
STA 255
LDA #0
STA $2001
LDA draw_req
CMP #1
BNE +
load_nametable 0, titlescreen_name
LDA #0
STA draw_req
+:
CMP #2
BNE +
load_nametable 1, char_create_name
LDA #0
STA draw_req
+:
CMP #3
BNE +
load_nametable 0, room_name
LDA #0
STA draw_req
+:
JSR pull_nt_buffer
LDA ppu_mask
STA $2001
LDA #0
STA waiting_for_frame
JSR reset_scroll
LDA #$00
STA $2003 ; set the low byte (00) of the RAM address for OAMDMA
LDA #$02
STA $4014 ; set the high byte (02) of the RAM address, start the transfer
LDA register_backup
LDX register_backup+1
LDY register_backup+2
PLP
RTI
IRQ:
RTI
.incsrc "src/utils.6502"
.incsrc "src/reset_scroll.6502"
.incsrc "src/title_code.6502"
.incsrc "src/cc_code.6502"
.incsrc "src/generator.6502"
.incsrc "src/play_code.6502"
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; data
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.org $E000
colorpal1:
.incbin "dat/pal.dat"
colorpal2:
.incbin "dat/grey_pal.dat"
titlescreen_name:
.incbin "dat/title.nam"
char_create_name:
.incbin "dat/char_create.nam"
room_name:
.incbin "dat/room.nam"
cc_stat_options:
.incbin "dat/player_stat.dat"
enemy_starting_stats:
.incbin "dat/enemy_stat.dat"
sprite_data:
.incbin "dat/sprite_data.dat"
konami:
.db UP_BUTTON
.db UP_BUTTON
.db DOWN_BUTTON
.db DOWN_BUTTON
.db LEFT_BUTTON
.db RIGHT_BUTTON
.db LEFT_BUTTON
.db RIGHT_BUTTON
.db B_BUTTON
.db A_BUTTON
.db 0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; interupt vectors
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.org $fffa
.dw NMI
.dw Reset
.dw IRQ
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; chr-rom
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.incbin "dat/sprites.chr"