-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.asm
499 lines (396 loc) · 7.58 KB
/
main.asm
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
;**************************************
; WEC 24H LE MANS
;**************************************
.65816
.include define.inc
.include registers.inc
.include macros.inc
.include var.asm
.include assets.asm
.org 808000
.base 0000
.include vectors.asm
.include clear.asm
.include dma.asm
.include math.asm
.include joypad.asm
.include level.asm
.include horizon.asm
.include object.asm
.include hud.asm
.include music.asm
.include decompress.asm
MainLoop:
jsr @WaitNextVBlank
jsr @HandleInput
jsr @UpdatePlayer
jsr @UpdatePlayerOAM
jsr @UpdateM7Params
jsr @UpdateM7HDMATables
jmp @MainLoop
UpdatePlayer:
.call RESERVE_STACK_FRAME 0c
; 01/02 -> prev player_x
; 03/04 -> prev player_y
; 05/06 -> buffer.x / -> @ax ?
; 07/08 -> buffer.y / -> @bx ?
; 09/0a -> src_x_offset
; 0b/0c -> src_y_offset
.call M16
; player_prev_x = player.x;
lda @player_x
sta 01
; player_prev_y = player.y;
lda @player_y
sta 03
; X coord
; player.x = player.fx >> 10;
lda @player_fx_lo
sta @ax
lda @player_fx_hi
sta @bx
lda #000a ; fixed point scale is 1024
sta @cx
jsr @Lsr32
lda @ax
sta @player_x
; Y coord
; player.y = player.fy >> 10;
lda @player_fy_lo
sta @ax
lda @player_fy_hi
sta @bx
lda #000a ; fixed point scale is 1024
sta @cx
jsr @Lsr32
lda @ax
sta @player_y
jsr @CenterCam
; buffer.x = camera.x - SCREEN_OFFSET_X;
lda @camera_x
sec
sbc #0188
sta 05
; buffer.y = camera.y - SCREEN_OFFSET_Y;
lda @camera_y
sec
sbc #0128
sta 07
; ---- Check horizontal offset
lda @player_x
bit #000f
bne @skip_column_update
cmp 01
beq @skip_column_update
bcc @going_left_cc
going_right_cc:
; next_col_x = ((screen.x + SCREEN_W + 360) % 1024) // 16;
lda @screen_x
clc
adc #0268 ; SCREEN_W + 360
and #03ff
.call LSR3
sta @next_col_x
inc
inc
and #007f
sta @next_dst_x
; src_x_offset = 1008
lda #03f0
sta 09
bra @end_column_update
going_left_cc:
; next_col_x = ((screen.x - 392) % 1024) // 16;
lda @screen_x
sec
sbc #0188
and #03ff
.call LSR3
sta @next_col_x
sta @next_dst_x
; src_x_offset = 0;
stz 09
end_column_update:
; CopyNextCol(buffer_x + src_x_offset,
; buffer_y,
; next_row_y);
; buffer_x + src_x_offset,
lda 05
and #0fff
.call LSR4
sta @next_src_x
lda 05
clc
adc 09
and #0fff
.call LSR4
sta @ax
; buffer.y
lda @next_src_y
sta @bx
; next_row_y
lda @next_dst_y
sta @cx
jsr @CopyNextCol
lda @need_update
ora #0001
sta @need_update
skip_column_update:
; ---- Check vertical offset
lda @player_y
bit #000f
bne @skip_row_update
cmp 03
beq @skip_row_update
bcc @going_up_cc
going_down_cc:
; next_row_y = screen.y + (SCREEN_H + 488);
lda @screen_y
clc
adc #02c8
and #03ff
.call LSR3
sta @next_row_y
inc
inc
and #007f
sta @next_dst_y
; src_y_offset = 1008;
lda #03f0
sta 0b
bra @end_row_update
going_up_cc:
; next_row_y = screen.y - 296;
lda @screen_y
sec
sbc #128
and #03ff
.call LSR3
sta @next_row_y
sta @next_dst_y
; src_y_offset = 0;
stz 0b
end_row_update:
; CopyNextRow(buffer_x,
; buffer_y + src_y_offset,
; next_col_x);
; buffer_x
lda @next_src_x
sta @ax
; buffer_y + src_y_offset
lda 07
and #0fff
.call LSR4
sta @next_src_y
lda 07
clc
adc 0b
and #0fff
.call LSR4
sta @bx
; next_col_x
lda @next_dst_x
sta @cx
jsr @CopyNextRow
lda @need_update
ora #0100
sta @need_update
skip_row_update:
.call M8
.call RESTORE_STACK_FRAME 0c
rts
CenterCam:
php
.call M8
.call RESERVE_STACK_FRAME 04
; 01/02 -> prev camera_x
; 03/04 -> prev camera_y
.call M16
lda @camera_x
sta 01
lda @camera_y
sta 03
; ---- X coordinate
; camera.x = player.x - SCREEN_W/2
lda @player_x
sec
sbc #0080
sta @camera_x
; screen.x += (camera.x - camera.prev_x) % 1024
sec
sbc 01
clc
adc @screen_x
and #03ff
sta @screen_x
; ---- Y coordinate
; camera.y = player.y - SCREEN_H/2
lda @player_y
sec
sbc #0070
sta @camera_y
; screen.y += (camera.y - camera.prev_y) % 1024
sec
sbc 03
clc
adc @screen_y
and #03ff
sta @screen_y
.call M8
.call RESTORE_STACK_FRAME 04
plp
rts
UpdateM7Params:
php
.call M16
; matrix_angle = - player_angle - 90
lda @player_angle
eor #ffff
inc
sec
sbc #0040
and #00ff
sta @ax
; A = cos(matrix_angle)
jsr @GetCosM7
sta @m7_a
sta @m7_d ; acts as a m7_a backup to check sign later...
lda @ax
; B = sin(matrix_angle)
jsr @GetSinM7
sta @m7_b
sta @m7_c ; acts as a m7_b backup to check sign later...
; X = screen_x + SCREEN_W/2
lda @screen_x
clc
adc #0080
sta @m7_x
; Y = screen_y + SCREEN_H/2
lda @screen_y
; Offset transformation towards the bottom
clc
adc #00a8
sta @m7_y
plp
rts
UpdateM7HDMATables:
php
; for 78 scanlines...
lda #4e
sta !m7_a_hdma_table
sta !m7_b_hdma_table
sta !m7_c_hdma_table
sta !m7_d_hdma_table
; set default params for M7 A,D (0x100)
.call M16
lda #0100
sta !m7_a_hdma_table+1
sta !m7_d_hdma_table+1
; set default params for M7 A,D (0x0)
lda #0000
sta !m7_b_hdma_table+1
sta !m7_c_hdma_table+1
; ---- Check if angle is positive
stz 01 ; neg cos = false, neg sin = false
lda @m7_a
bpl @m7_a_positive
; if cos_a < 0: cos_a = -cos_a
eor #ffff
inc
sta @m7_a
m7_a_positive:
lda @m7_b
bpl @m7_b_positive
; if sin_a < 0: sin_a = -sin_a
eor #ffff
inc
sta @m7_b
m7_b_positive:
; ---- Fill Tables loop
ldy #0000
ldx #0003 ; start from index 3 of the table
fill_tables_loop:
.call M8
lda #01
sta !m7_a_hdma_table,x
sta !m7_b_hdma_table,x
sta !m7_c_hdma_table,x
sta !m7_d_hdma_table,x
; lam = lam_lut[i]
phx ; save x
tyx
lda !lambda_lut,x
sta @ah
plx ; restore x
inx
jsr @SaveADParams
jsr @SaveBCParams
inx
inx
iny
cpy #0092; for 146 scanlines
bne @fill_tables_loop
.call M16
; terminate hdma tables with 0x00 0x00
lda #0000
sta !m7_a_hdma_table,x
sta !m7_b_hdma_table,x
sta !m7_c_hdma_table,x
sta !m7_d_hdma_table,x
plp
rts
SaveADParams:
php
lda @ah ; lam
sta WRMPYA
lda @m7_a
sta WRMPYB
.call WAIT8
; A = (cos_a * lam) >> 6
.call M16
lda RDMPYL
.call LSR3
.call LSR3
sta @bx ; save result
;if neg_cos_result: A = (A ^ 0xffff) + 1
lda @m7_d
bpl @skip_neg_cos_result
lda @bx
eor #ffff
inc
sta @bx
skip_neg_cos_result:
lda @bx
sta !m7_a_hdma_table,x
sta !m7_d_hdma_table,x
plp
rts
SaveBCParams:
php
lda @ah ; lam
sta WRMPYA
lda @m7_b
sta WRMPYB
.call WAIT8
; B = (sin_a * lam) >> 6
.call M16
lda RDMPYL
.call LSR3
.call LSR3
sta @bx ; save result
;if neg_sin_result: B = (B ^ 0xffff) + 1
lda @m7_c
bpl @skip_neg_sin_result
lda @bx
eor #ffff
inc
sta @bx
skip_neg_sin_result:
lda @bx
sta !m7_b_hdma_table,x
eor #ffff
inc
sta !m7_c_hdma_table,x
plp
rts
.include info.asm