-
Notifications
You must be signed in to change notification settings - Fork 0
/
Clip.asm
642 lines (583 loc) · 26.9 KB
/
Clip.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
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
;-------------------------------------------------------------------------------
;@doc:file
;
; === Clip.asm ===
;
; Provides methods for clipping. Written by qarnos.
;
;@doc:end
;-------------------------------------------------------------------------------
.module Clip
;-------------------------------------------------------------------------------
;
; === Variables ===
;
;-------------------------------------------------------------------------------
g_line16X1 = TempTile+0
g_line16Y1 = TempTile+2
g_line16X2 = TempTile+4
g_line16Y2 = TempTile+6
g_line16Pt1 = g_line16X1
g_line16Pt2 = g_line16X2
;-------------------------------------------------------------------------------
;
; === Clip2DLine16 ===
;
; WARNING:
;
; All Y co-ordinates are reversed from TI-OS _ILine.
;
; INPUTS:
;
; REGISTERS
; * DE - address of point 1
; * HL - address of point 2
;
; OUTPUTS
;
; REGISTERS
; * B - x1
; * C - y1
; * D - x2
; * E - y2
; * F - Carry flag set if line was culled.
;
; MEMORY
; * (g_line16X1) - clipped x co-ordinate of point 1 (16-bit version)
; * (g_line16Y1) - clipped y co-ordinate of point 1 (16-bit version)
; * (g_line16X2) - clipped x co-ordinate of point 2 (16-bit version)
; * (g_line16Y2) - clipped y co-ordinate of point 2 (16-bit version)
;
; DESTROYED:
; * A, HL, IX
;
; === Clip2DLine16Ex ===
;
; INPUTS:
;
; MEMORY
; * (g_line16X1) - x co-ordinate of point 1
; * (g_line16Y1) - y co-ordinate of point 1
; * (g_line16X2) - x co-ordinate of point 2
; * (g_line16Y2) - y co-ordinate of point 2
;
; OUTPUTS:
;
; Same as Clip3DLine16.
;
; DESTROYED:
;
; Same as Clip3DLine16.
;
;-------------------------------------------------------------------------------
Clip2DLine16:
;-------------------------------------------------------------------
; Copy 16-bit co-ordinates to temporary buffer
;-------------------------------------------------------------------
push de ; [11]
ld de, g_line16Pt2 ; [10]
ld bc, 4 ; [10]
ldir ; [79]
pop hl ; [10]
ld de, g_line16Pt1 ; [10]
ld c, 4 ; [7]
ldir ; [79]
Clip2DLine16Ex:
;-------------------------------------------------------------------
; Generate initial clipping codes for points.
;-------------------------------------------------------------------
ld de, g_line16Pt1 ; [10]
ld hl, g_line16Pt2 ; [10]
call MakeClippingCode ; [*] make code for g_line16Pt2
ld c, a ; [4]
ex de, hl ; [4] g_line16Pt2 <-> g_line16Pt1
call MakeClippingCode ; [*]
;-------------------------------------------------------------------
; Loop while point 1 is out
;-------------------------------------------------------------------
_pt1Loop: jr z, _pt2Loop ; [12/7]
;-------------------------------------------------------------------
; Reject the line if both end points lie outside a common plane.
;-------------------------------------------------------------------
ld b, a ; [4]
and c ; [4]
scf ; [4]
ret nz ; [11/5]
;-------------------------------------------------------------------
; Exit the loop if point 1 is in.
;-------------------------------------------------------------------
or b ; [4]
jr z, _pt2Loop ; [12/7]
;-------------------------------------------------------------------
; Interpolate point 1 towards point 2 and store result
;-------------------------------------------------------------------
push bc ; [11] save g_line16Pt2 code
call InterpolateLine ; [*]
jr nc, $ + $0004 ; [12/7] just in case...
pop bc ; [10]
ret ; [10]
ld (g_line16X1), bc ; [20]
ld (g_line16Y1), de ; [20]
;-------------------------------------------------------------------
; Generate new code for point 1 and loop
;-------------------------------------------------------------------
call MakeClippingCodeEx ; [*]
pop bc ; [10]
ld hl, g_line16Pt1 ; [10]
ld de, g_line16Pt2 ; [10]
jp _pt1Loop ; [10]
;-------------------------------------------------------------------
; Loop while point 2 is out
;-------------------------------------------------------------------
_pt2Loop: ld a, c ; [4] assume clipping code for
or a ; [4] point 1 is zero
jr z, _allDone ; [12/7]
;-------------------------------------------------------------------
; Interpolate point 2 towards point 1 and store result
;-------------------------------------------------------------------
ld hl, g_line16Pt2 ; [10]
ld de, g_line16Pt1 ; [10]
call InterpolateLine ; [*]
ret c ; [11/5] just in case...
ld (g_line16X2), bc ; [20]
ld (g_line16Y2), de ; [20]
;-------------------------------------------------------------------
; Generate new code for point 2 and loop
;-------------------------------------------------------------------
call MakeClippingCodeEx ; [*]
jp _pt2Loop + 1 ; [10]
;-------------------------------------------------------------------
; Load the low bytes of the 16-bit co-ordinates into output
; registers (high bytes are now all zero).
;-------------------------------------------------------------------
_allDone: or a ; [4] reset carry
ld a, (g_line16X1) ; [13]
ld b, a ; [4] x1
ld a, (g_line16Y1) ; [13]
ld c, a ; [4] y1
ld a, (g_line16X2) ; [13]
ld d, a ; [4] x2
ld a, (g_line16Y2) ; [13]
ld e, a ; [4] y2
ret ; [10]
;-------------------------------------------------------------------------------
; End of Clip2DLine16/Ex
;-------------------------------------------------------------------------------
;-------------------------------------------------------------------------------
;
; === NegateHL ===
;
;-------------------------------------------------------------------------------
NegateHL: ld a, h ; [4]
cpl ; [4]
ld h, a ; [4]
ld a, l ; [4]
cpl ; [4]
ld l, a ; [4]
inc hl ; [6]
ret ; [10]
;-------------------------------------------------------------------------------
; End of NegateHL
;-------------------------------------------------------------------------------
;-------------------------------------------------------------------------------
;
; === MakeClippingCode ===
;
; See MakeClippingCodeEx for description.
;
; INPUTS:
;
; * HL - Address of x,y coordinates
;
; OUTPUTS:
;
; * A - Clipping code.
; * F - Zero flag set for A.
;
;-------------------------------------------------------------------------------
MakeClippingCode:
push hl ; [11]
push de ; [11]
push bc ; [11]
ld c, (hl) ; [7] load x low
inc hl ; [6]
ld b, (hl) ; [7] load h high
inc hl ; [6]
ld e, (hl) ; [7] load y low
inc hl ; [6]
ld d, (hl) ; [7] load y high
call MakeClippingCodeEx ; [*]
pop bc ; [10]
pop de ; [10]
pop hl ; [10]
ret ; [10]
;-------------------------------------------------------------------------------
; End of MakeClippingCode
;-------------------------------------------------------------------------------
;-------------------------------------------------------------------------------
; === MakeClippingCodeEx ===
;
; INPUTS:
;
; REGISTERS
; * BC - 16-bit x-coordinate (signed)
; * DE - 16-bit y-coordinate (signed)
;
; MEMORY
; * (g_wndXMin) - X co-ordinate of top left corner clipping window
; * (g_wndXMax) - X co-ordinate of bottom right corner clipping window
; * (g_wndYMin) - Y co-ordinate of top left corner clipping window
; * (g_wndYMax) - Y co-ordinate of bottom right corner clipping window
;
; OUTPUTS:
;
; REGISTERS
; * A - Clipping flags in bits 7-4, bits 3-0 zero
; - Bit 7 - point lies outside g_wndXMin
; - Bit 6 - point lies outside g_wndXMax
; - Bit 5 - point lies outside g_wndYMin
; - Bit 4 - point lies outside g_wndYMax
; * F - Zero flag set according to A
;
; DESTROYED:
;
; REGISTERS
; * HL
;-------------------------------------------------------------------------------
MakeClippingCodeEx:
;-------------------------------------------------------------------
; Check Y against (g_wndYMax)
;-------------------------------------------------------------------
ld h, $80 ; [7] for speed (since we trash L anyway)
ld a, (g_wndYMax) ; [13]
sub e ; [4]
ld a, $00 ; [7]
sbc a, d ; [4]
jp po, $ + $0004 ; [10] invert sign when overflow TRUE
xor h ; [4]
and h ; [4] sign bit set if out
rrca ; [4]
ld l, a ; [4]
;-------------------------------------------------------------------
; Check Y against (g_wndYMin)
;-------------------------------------------------------------------
ld a, (g_wndYMin) ; [13]
scf ; [4]
sbc a, e ; [4]
ld a, 0 ; [7]
sbc a, d ; [4]
jp pe, $ + $0004 ; [10] invert sign when overflow FALSE
xor h ; [4]
and h ; [4] sign bit set when out
or l ; [4]
rrca ; [4]
ld l, a ; [4]
;-------------------------------------------------------------------
; Check X against (g_wndXMax)
;-------------------------------------------------------------------
ld a, (g_wndXMax) ; [13]
sub c ; [4]
ld a, $00 ; [7]
sbc a, b ; [4]
jp po, $ + $0004 ; [10] invert sign when overflow TRUE
xor h ; [4]
and h ; [4] sign bit set if out
or l ; [4]
rrca ; [4]
ld l, a ; [4]
;-------------------------------------------------------------------
; Check X against (g_wndXMin)
;-------------------------------------------------------------------
ld a, (g_wndXMin) ; [13]
scf ; [4]
sbc a, c ; [4]
ld a, 0 ; [7]
sbc a, b ; [4]
jp pe, $ + $0004 ; [10] invert sign when overflow FALSE
xor h ; [4]
and h ; [4] sign bit set when out
or l ; [4]
ret ; [10]
;-------------------------------------------------------------------------------
; End of MakeClippingCodeEx
;-------------------------------------------------------------------------------
;-------------------------------------------------------------------------------
;
; === InterpolateLine ===
;
; INPUTS:
;
; REGISTERS
; * A - candidate clipping planes
; * DE - address of point 1
; * HL - address of point 2
;
; OUTPUTS:
;
; REGISTERS
; * BC - interpolated x
; * DE - interpolated y
;
; DEESTROYED:
;
; REGISTERS
; * AF, HL, IX
;
;-------------------------------------------------------------------------------
InterpolateLine:
;-------------------------------------------------------------------
; Rotate through the clipping flags until we find one which is set.
;-------------------------------------------------------------------
ld bc, g_wndXMin ; [10]
ld ix, $0002 ; [14]
add a, a ; [4] rotate g_wndXMin flag to carry
jr c, _intrpForX ; [12/7]
inc bc ; [6] advance BC to g_wndXMax
add a, a ; [4] rotate g_wndXMan flag to carry
jr c, _intrpForX ; [12/7]
inc bc ; [6] advance BC to g_wndYMin
inc de ; [6]
inc de ; [6] advance DE to v1->y
inc hl ; [6]
inc hl ; [6] advance HL to v2->y
ld ix, $fffe ; [14]
add a, a ; [4] rotate g_wndYMin flag to carry
jr c, _intrpForY ; [12/7]
inc bc ; [6] advance BC to g_wndYMax
add a, a ; [4] rotate g_wndYMax flag to carry
jr c, _intrpForY ; [12/7]
;-------------------------------------------------------------------
; This should never happen, but return with carry just in case.
;-------------------------------------------------------------------
scf ; [4] set error state
ret ; [10]
;-------------------------------------------------------------------
; Handle interpolation through the x-planes
;-------------------------------------------------------------------
_intrpForX: call _interpol8 ; [*]
ld b, 0 ; [7]
ld c, a ; [4]
ret ; [10]
;-------------------------------------------------------------------
; Handle interpolation through the y-planes
;-------------------------------------------------------------------
_intrpForY: call _interpol8 ; [*]
ld b, d ; [4]
ld c, e ; [4]
ld d, 0 ; [7]
ld e, a ; [4]
ret ; [10]
;-------------------------------------------------------------------
; This is the actual interpolation code
;-------------------------------------------------------------------
_interpol8: or a
ld a, (bc) ; [7] load plane value
push af ; [11] save plane value
push hl ; [11] save v2
push de ; [11] save v1
;-------------------------------------------------------------------
; Calculate interpolation factor as proper fraction:
;
; abs(plane - v1->n) / abs(v1->n - v2->n)
;
; Result in DE/HL
;-------------------------------------------------------------------
ld c, (hl) ; [7] load v2->n low
inc hl ; [6]
ld b, (hl) ; [7] load v2->n high
ex de, hl ; [4] v2 <-> v1
ld e, (hl) ; [7] load v1->n low
inc hl ; [6]
ld d, (hl) ; [7] load v1->n high
ld l, a ; [4]
xor a ; [4]
ld h, a ; [4]
sbc hl, de ; [15] plane - v1->n
jp po, $ + $0005 ; [10] jump if overflow FALSE
or $80 ; [7]
xor h ; [4]
call m, NegateHL ; [57/10]
ex de, hl ; [4] d1 <-> v1->n
xor a ; [4]
sbc hl, bc ; [15] v1->n - v2->n
jp po, $ + $0005 ; [10] jump if overflow FALSE
or $80 ; [7]
xor h ; [4]
call m, NegateHL ; [57/10]
ld b, ixh ; [4]
ld c, ixl ; [4]
pop ix ; [14] restore v1
ex (sp), hl ; [19] denominator <-> v2
add ix, bc ; [15]
add hl, bc ; [11]
;-------------------------------------------------------------------
; Calculate (v1 - v2)
;-------------------------------------------------------------------
ld a, (ix + $00) ; [19]
sub (hl) ; [7]
inc hl ; [6]
push af ; [11]
ld a, (ix + $01) ; [19]
sbc a, (hl) ; [7]
pop hl ; [10]
ld l, h ; [4]
ld h, a ; [4]
;-------------------------------------------------------------------
; Call the clipping function
;-------------------------------------------------------------------
jp po, $ + $0005 ; [10] jump if overflow FALSE
xor $80 ; [7]
pop bc ; [19] restore denominator
push af ; [11] save sign flag
call m, NegateHL ; [57/10]
call UMulByFraction ; [*]
pop af ; [10] restore sign flag
call m, NegateHL ; [57/10]
;-------------------------------------------------------------------
; Add result to origin
;-------------------------------------------------------------------
ld a, (ix + $00) ; [19]
sub l ; [4]
ld e, a ; [4]
ld a, (ix + $01) ; [19]
sbc a, h ; [4]
ld d, a ; [4]
pop af ; [11] restore x/y plane value
ret ; [10]
;-------------------------------------------------------------------------------
; End of InterpolateLine
;-------------------------------------------------------------------------------
;-------------------------------------------------------------------------------
;
; === UMulByFraction ===
;
; Multiplies HL by proper fraction DE/BC.
;
; LIMITS:
; * BC > 0
; * DE < BC
;
; INPUTS:
;
; REGISTERS
; * HL - multiplicand
; * DE - numerator
; * BC - denominator
;
; OUTPUTS:
;
; REGISTERS
; * HL - quotient of HL * DE / BC
; * DE - remainder of HL * DE / BC
;
; DESTROYED:
;
; REGISTERS
; * AF
;
;-------------------------------------------------------------------------------
UMulByFraction:
;-------------------------------------------------------------------
; This initial block of code is used to return the output in more
; convenient registers. It may be removed, in which case the
; output will be returned as followed:
;
; IX - quotient
; HL - remainder
; DE - numerator (unchanged)
; BC - denominator (unchanged)
;-------------------------------------------------------------------
push ix ; [15] preserve IX
call _mainRtn ; [17] run main code.
ex (sp), ix ; [23] quotient <-> preserved IX
ex de, hl ; [4] remainder <-> numerator
pop hl ; [10] pop quotient
ret ; [10]
;-------------------------------------------------------------------
; The actual routine starts here.
;-------------------------------------------------------------------
_mainRtn: ld ix, $0000 ; [14] initial quotient
ld a, h ; [4]
or a ; [4]
jr z, _highZero ; [12/7] jump if high byte is zero.
ld h, l ; [4] push contents of L to stack so
push hl ; [11] we can pop it into A later on.
ld hl, $0000 ; [10] initial remainder
;-------------------------------------------------------------------
; Skip to first significant bit to avoid useless work. We also
; set the low bit of A to act as a marker bit for the main loop.
;-------------------------------------------------------------------
scf ; [4] for marker bit
_findHiBit: adc a, a ; [4] searching for first significant
jp nc, _findHiBit ; [10] bit to avoid useless work.
call _mainLoop ; [*] process high byte.
pop af ; [10] pop low byte
;-------------------------------------------------------------------
; Now process the low byte.
;
; Here we duplicate a small part of the the main loop. This is
; because we need to run this code, but we want to shift a 1
; into the accumulator instead of a 0.
;-------------------------------------------------------------------
add ix, ix ; [15] quotient * 2
add hl, hl ; [11] remainder * 2
jr c, _ov3 ; [12/7] <<
sbc hl, bc ; [15] remainder -= denominator
jr nc, _nc3 ; [12/7]
add hl, bc ; [11] remainder += denominator
jp _rotMul2 ; [10]
_nc3: inc ix ; [10] quotient + 1
_rotMul2: sl1 a ; [8]
jp _mainLoop ; [10]
_ov3: or a ; [4] <<
sbc hl, bc ; [15] <<
jp _nc3 ; [10] <<
;-------------------------------------------------------------------
; High byte was zero.
; Go straight to low byte. Do not collect $200.
;-------------------------------------------------------------------
_highZero: ld a, l ; [4] load low byte
or a ; [4]
ld hl, $0000 ; [10] initial remainder
ret z ; [11/5] return if multiplicand zero
;-------------------------------------------------------------------
; Search for first significant bit to avoid useless work. We also
; set the low bit of A to act as a marker bit for the main loop.
;-------------------------------------------------------------------
scf ; [4] for marker bit
_findLoBit: adc a, a ; [4] searching for first significant
jp nc, _findLoBit ; [10] bit to avoid useless work.
;-------------------------------------------------------------------
; Main processing loop. We loop here for each significant bit
; of the multiplicand (hence, small multiplicands are fast).
;-------------------------------------------------------------------
_mainLoop: jr nc, _loopTail ; [12/7]
add hl, de ; [11] remainder += numerator
jr c, _ov2 ; [12/7]
sbc hl, bc ; [15] remainder -= denominator
jr nc, _nc2 ; [12/7]
add hl, bc ; [11] remainder += denominator
jp _loopTail ; [10]
_nc2: inc ix ; [10]
_loopTail: cp $80 ; [7] once only the marker bit is
ret z ; [10] left we end the loop
add ix, ix ; [15] quotient * 2
add hl, hl ; [11] remainder * 2
jr c, _ov1 ; [12/7]
sbc hl, bc ; [15] remainder -= denominator
jr nc, _nc1 ; [12/7]
add hl, bc ; [11] remainder += denominator
jp _rotMulBit ; [10]
_nc1: inc ix ; [10] quotient + 1
_rotMulBit: add a, a ; [4] shift next bit and loop
jp _mainLoop ; [10]
_ov1: or a ; [4]
sbc hl, bc ; [15] remainder -= denominator
jp _nc1 ; [10]
_ov2: or a ; [4]
sbc hl, bc ; [15] remainder -= denominator
jp _nc2 ; [10]
;-------------------------------------------------------------------------------
; End of UMulByFraction
;-------------------------------------------------------------------------------
.endmodule