-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrotate.inc
596 lines (533 loc) · 12.2 KB
/
rotate.inc
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
#importonce
.filenamespace rotate
.import source "init.inc"
.import source "common.inc"
.const R = $2d80
.const @MAX_SPRITES = 64
.const @MATRIX_STRIDE = 32
.const @MAX_PCOORDS = MAX_SPRITES
.const MAX_EDGES = 32
.const CORNER_OFFSET = MAX_EDGES
.segment Object
edge_start: .fill 32, 0
edge_end: .fill 32, 0
.macro @ROTATE_ZP() {
@matrix:
.label @matrix_lo = *
.label @matrix_hi = * + 1
//Identity matrix:
//.word R, 0, 0; .fill MAX_VERTICES - 6, -1
//.word 0, R, 0; .fill MAX_VERTICES - 6, -1
//.word 0, 0, R; .fill MAX_VERTICES - 6, -1
/*MATRIX_ROW_0*/ .word ( 10283 * R) / $4600, ( 6399 * R) / $4600, ( 13207 * R) / $4600
@coords_xyz:
@coords_x: .fill MATRIX_STRIDE-6, 0
/*MATRIX_ROW_1*/ .word ( 14493 * R) / $4600, ( -1890 * R) / $4600, (-10368 * R) / $4600
@coords_y: .fill MATRIX_STRIDE-6, 0
/*MATRIX_ROW_2*/ .word ( -2309 * R) / $4600, ( 16631 * R) / $4600, ( -6260 * R) / $4600
@coords_z: .fill MATRIX_STRIDE-6, 0
@matrix_end:
@src_matrix_ptr: .word matrix_lo & $ff00
@dst_matrix_ptr: .word matrix_lo & $ff00
@visible_vertex_start: .byte 0
@visible_vertex_end: .byte 0
@z_clip: .byte 0
@vertices_start: .byte 0
@vertices_end: .byte 0
@rotation: .byte $26
@vertices_count_minus_one: .byte $ff
@stride: .byte 0
@sign: .byte 0
@tmp_1: .byte 0
@tmp_2: .byte 0
@tmp_3: .byte 0
// For object initialization / code generation:
@dest_lo: .byte <compute_vertices
@dest_hi: .byte >compute_vertices
@log_table_x: .byte 0
@xd1: .word sqr_tables
@xd2: .word sqr_tables+$0400
@xd3: .word sqr_tables+$0200
@xd4: .word sqr_tables+$0600
// for sorting
@radix_ptr: .for(var i = 0; i < 16; i++) {.word lonext}
}
.macro @ROTATE_SCRATCH() {
@pcoords:
@pcoords_x:
.fill MAX_PCOORDS, 0
@pcoords_y:
.fill MAX_PCOORDS, $ff // $ff for sorting
@pshape:
.fill MAX_PCOORDS, 0
@compute_vertices:
.fill 1024, 0
}
.macro @ROTATE_TABLES() {
.fill 256, i
}
.macro @GENERATE_OBJECT() {
ldy @read_object_pos_lo:#$00
ldx #$00
!:
lda all_objects, y
sta edge_start, x
bmi done
iny
lda all_objects, y
sta edge_end, x
iny
inx
bne !-
done:
iny
lda all_objects, y
sta visible_vertex_start
iny
lda all_objects, y
sta visible_vertex_end
iny
lda all_objects, y
sta z_clip
iny
lda all_objects, y
sta vertices_start
iny
lda all_objects, y
sta vertices_end
iny
lda all_objects, y
sta rotation
iny
sty read_object_pos_lo
/* We generate code that looks like this:
LDA #$00
LDY matrix_hi+4, x
SEC
SBC scale, y
LDY matrix_hi+2, x
SEC
SBC scale, y
LDY matrix_hi, x
SEC
SBC scale, y
STA coords_xyz+i, x
Followed by:
RTS
*/
ldx vertices_start
// assume we always have at least one vertex
vertex_loop:
// 2
lda #LDA_IMM
pha
lda #$00
pha
.for (var i = 0 ; i < 3 ; i++) {
// 6
lda #LDY_ZPX
pha
lda #matrix_hi + i * 2
pha
lda all_vertices, x // t0
lsr
tay
bcc plus
lda #SEC
pha
lda #SBC_ABSY
pha
always_bcs store_table_addr
plus: lda #CLC
pha
lda #ADC_ABSY
pha
store_table_addr:
anc #$00 // table_lo
pha
tya
adc #>mul0
pha
inx
}
// 3
lda #STA_ZPX
pha
lda #<coords_xyz
sec
adc vertices_count_minus_one
inc vertices_count_minus_one
pha
ldy #$15
!:
pla
sta (dest_lo), y
dey
bpl !-
lda dest_lo
sec
adc #$15
sta dest_lo
bcc !+
inc dest_hi
!:
cpx vertices_end
bne_far vertex_loop
done2:
ldy #$00
lda #RTS
sta (dest_lo), y
}
.macro MATRIX_ADD() {
ldx #$03
ldy #$00
loop2:
lda (src_matrix_ptr), y
eor sign
sec
sbc sign
sta tmp_1
iny // hi
lda.zp (src_matrix_ptr), y
eor sign
sbc sign
sta tmp_2
lda.zp (src_matrix_ptr), y
bpl !+
lda #$ff //>> 3
bne s
!: lda #$00 //>> 3
s:
eor sign
sbc sign
asl tmp_1
rol tmp_2
rol
sta tmp_3
lda tmp_2
asl tmp_1
rol
rol tmp_3
asl tmp_1
rol
rol tmp_3
clc
dey // lo
adc (dst_matrix_ptr), y
sta (dst_matrix_ptr), y
iny // hi
lda tmp_3
adc (dst_matrix_ptr), y
sta (dst_matrix_ptr), y
tya
clc
adc stride
tay
dex
bne loop2
}
.macro @ROTATE() {
// ----------------------------------------------------------------------------
// Rotate Matrix
// ----------------------------------------------------------------------------
// Byte: AABBCCDD
// AA: row
// BB: row
// CC: column
// DD: column
// First add rows, then columns:
// Add row AA to row BB
// Subtract row BB from row AA
// Add column CC to column DD
// Subtract column DD from column CC
lda rotation
alr #$c0
adc #<matrix_lo
sta src_matrix_ptr
lda rotation
and #$30
asl
adc #<matrix_lo
sta dst_matrix_ptr
ldx #$02-1
stx stride
dex
stx sign
rotate_more:
MATRIX_ADD()
ldx sign
bmi inner_done
dec sign
ldx src_matrix_ptr
ldy dst_matrix_ptr
sty src_matrix_ptr
stx dst_matrix_ptr
always_bne rotate_more
inner_done:
lda #MATRIX_STRIDE-1
cmp stride
beq done_with_rotation
sta stride
lda rotation
alr #$0c
adc #<matrix_lo
sta src_matrix_ptr
lda rotation
and #$03
asl
adc #<matrix_lo
sta dst_matrix_ptr
inc sign
always_beq rotate_more
done_with_rotation:
// ----------------------------------------------------------------------------
// Compute Vertices
// ----------------------------------------------------------------------------
ldx #0
jsr compute_vertices
ldx #MATRIX_STRIDE
jsr compute_vertices
ldx #MATRIX_STRIDE*2
jsr compute_vertices
// ----------------------------------------------------------------------------
// Prepare for sorting
// ----------------------------------------------------------------------------
ldx #$ff-4
ldy #>lonext
.for(var i = 0; i < 16; i += 2) {
entry:
lda #<joinlo+1+i*4+4
sax radix_ptr+(15-i)*2
sty radix_ptr+(15-i)*2+1
sta radix_ptr+(14-i)*2
sty radix_ptr+(14-i)*2+1
}
// ----------------------------------------------------------------------------
// Divide by Z
// ----------------------------------------------------------------------------
// "Divide" by z to get perspective-adjusted 2D coords
ldx vertices_count_minus_one
perspective:
// So yeah, we don't actually divide. We just multiply by (C-z).
// Wouldn't work for a 3D world engine, but works just fine for model
// view, and it saves a handful of bytes.
lda coords_z, x
lsr
eor #$ff^($80>>1)
//tay
//lda divz, y
sta xd1
sta xd3
eor #$ff
sta xd2
sta xd4
ldy coords_x, x
lda (xd1), y
sec
sbc (xd2), y
sta tmp
lda (xd3), y
sbc (xd4), y
cpy #$00
bpl !+
sbc xd1
!:
asl tmp
rol
eor #$80
store_x:
sta pcoords_x + MAX_EDGES, x
ldy coords_y, x
lda (xd1), y
sec
sbc (xd2), y
sta tmp
lda (xd3), y
sbc (xd4), y
cpy #$00
bpl !+
sbc xd1
!:
asl tmp
rol
eor #$80
store_y:
sta pcoords_y + MAX_EDGES, x
cpx visible_vertex_start
bcc skip
cpx visible_vertex_end
bcs skip
stx tmp
and #$0f
asl
tax
lda tmp
clc
adc #MAX_EDGES
sta (radix_ptr, x)
sta radix_ptr, x
ldx tmp
ldy coords_z, x
lda z_to_sprite, y
sta pshape + MAX_EDGES, x
skip:
dex
bpl perspective
// ----------------------------------------------------------------------------
// Generate edges
// ----------------------------------------------------------------------------
ldy #$00
clc
jmp check_for_sentinel
edge_xy_loop:
// ldx edge_start, y
lda pcoords_x + MAX_EDGES, x
clc
ldx edge_end, y
adc pcoords_x + MAX_EDGES, x
ror
sta pcoords_x, y
lda pcoords_y + MAX_EDGES, x
ldx edge_start, y
clc
adc pcoords_y + MAX_EDGES, x
ror
sta pcoords_y, y
lda z_clip
beq no_z_clip
lda coords_z, x
eor #$80
sta tmp
ldx edge_end, y
lda coords_z, x
eor #$80
clc
adc tmp
arr #$fe
cmp #$72
bcs skip_edge2
ldx edge_start, y
no_z_clip:
lda pcoords_y + MAX_EDGES, x
sec
ldx edge_end, y
sbc pcoords_y + MAX_EDGES, x
bcs notinverted
eor #$ff
adc #$01
sta log_table_x
lda pcoords_x + MAX_EDGES, x
sec
ldx edge_start, y
sbc pcoords_x + MAX_EDGES, x
jmp after_sbc
notinverted:
sta log_table_x
lax edge_start, y
lda pcoords_x + MAX_EDGES, x
sec
ldx edge_end, y
sbc pcoords_x + MAX_EDGES, x
after_sbc:
bcs notinverted2
eor #$ff
adc #$01
tax // log_table_y
lda log_table, x
eor #$ff
sec
ldx log_table_x
adc log_table, x
tax
lda atan_xy1, x
jmp store
notinverted2:
tax
lda log_table, x
sec
ldx log_table_x
sbc log_table, x
tax
lda atan_xy2, x
store:
sta pshape, y
lda pcoords_y, y
and #$0f
asl
tax
tya
sta (radix_ptr, x)
sta radix_ptr, x
skip_edge2:
iny
check_for_sentinel:
ldx edge_start, y
bpl_far edge_xy_loop
skip_edges:
// ----------------------------------------------------------------------------
// Sort.
// See https://codebase64.org/doku.php?id=base:a_faster_radix_sort for
// the original algorithm. Thank you, LFT!
// ----------------------------------------------------------------------------
// add sentinel
ldx #$0f*2
lda #$ff
sta (radix_ptr, x)
sta radix_ptr, x
ldy #0
jmp joinlo
.align 256
@lonext:
.fill MAX_SPRITES, 0
@joinlo:
loop1:
.const LINK_LISTS = 4
.for(var i = 15-1; i >= 0; i--) {
entry:
lda #0 // operand is first[i+1]
sta (radix_ptr+2*i),y
.errorif (*) - entry != LINK_LISTS , "LINK_LISTS is wrong, should be " + (* - entry)
}
lda #0 // operand is first[0]
pha
ldx #$ff-4
ldy #>hinext
.for(var i = 0; i < 16; i += 2) {
entry:
.if(i < 16) {
lda #<joinlo+1+i*4+4
sax radix_ptr+(16-1-i)*2
sty radix_ptr+(16-1-i)*2+1
sta radix_ptr+(16-2-i)*2
sty radix_ptr+(16-2-i)*2+1
}
}
pla
tax
hiloop:
ldy pcoords_y, x
ldx shr_4_times_2, y
sta (radix_ptr,x)
sta radix_ptr,x
tay
lax lonext,y
bpl hiloop
// add sentinel
ldx #$0f*2
lda #$ff
sta (radix_ptr, x)
sta radix_ptr, x
ldy #0
jmp joinhi
.align 256
@hinext:
// TODO: why can't this be 0?
.fill MAX_SPRITES, $ff
@joinhi:
loop2:
.for(var i = 16-2; i >= 0; i--) {
lda #0 // operand is first[i+1]
sta (radix_ptr+2*i),y
}
lda #0 // operand is first[0]
}