/
smux.asm
548 lines (424 loc) · 12.7 KB
/
smux.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
;
; RRPGE User Library functions - Display List sprite multiplexer
;
; Author Sandor Zsuga (Jubatian)
; Copyright 2013 - 2015, GNU GPLv3 (version 3 of the GNU General Public
; License) extended as RRPGEvt (temporary version of the RRPGE
; License): see LICENSE.GPLv3 and LICENSE.RRPGEvt in the project
; root.
;
;
; Simple sprite management system for the Graphics Display Generator. It is
; capable to automatically multiplex sprites. For the proper function the
; Display List Clear should be set up appropriately to clear the managed
; columns.
;
; Uses the following CPU RAM locations:
; 0xFB00 - 0xFC8F: Occupation data
; 0xFDCF: First column to use.
; 0xFDCE: Count of columns to use.
; 0xFDCD: Bit0: if clear, indicates the occupation data is dirty.
;
; Also uses the followings from the Display list manager (dlist.asm):
; 0xFDAC: Vertical limit, low
; 0xFDAD: Vertical limit, high
;
; Also adds a Page flip hook (to clear the occupation data).
;
; Occupation data format:
;
; Low 400 bytes are the bottom limits, High 400 bytes are the top limits (as
; first occupied locations, so the row is full when they equal).
;
include "rrpge.asm"
section code
; 0xFB00 - 0xFC8F: Occupation data
us_smux_ola equ 0xFB00
us_smux_ole equ 0xFBC8
us_smux_oha equ 0xFBC8
us_smux_ohe equ 0xFC90
; 0xFDCF: Column to start at
us_smux_cs equ 0xFDCF
; 0xFDCE: Count of columns
us_smux_cc equ 0xFDCE
; 0xFDCD: Dirty flag on bit 0: clear if dirty.
us_smux_df equ 0xFDCD
; 0xFDAC: Vertical limit, low
us_smux_vl equ 0xFDAC
; 0xFDAD: Vertical limit, high
us_smux_vh equ 0xFDAD
;
; Internal function to set up display list pointer.
;
; Param1: Y position
; Param0: Display List Definition
; Ret.X3: Display list row size in bits (used to advance rows)
;
; The display list pointer is set up to incrementing 16 bits.
;
us_smux_setptr_i:
.psy equ 0 ; Y position
.dld equ 1 ; Display List Definition
; Save CPU regs
psh a, b, d
; Load display list size & prepare masks
mov a, [$.dld]
and a, 3 ; Display list entry size
add a, 7 ; 0 => 4 * 32 bit entries etc.
; Calculate bit offset within display list
shl c:[$.psy], a ; Bit add value for start offset by Y position
mov b, c ; Y high in 'b'
; Calculate absolute display list offset
mov d, 0xFFFC
and d, [$.dld] ; Offset bits recovered as 512 bit offset
shl c:d, 9 ; Bit offset of display list
add b, c
add c:d, [$.psy]
add b, c ; Start offset in b:d acquired
; Fill PRAM pointer 3
mov x3, P3_AH
mov [x3], b ; P3_AH
mov [x3], d ; P3_AL
mov c, 0
mov [x3], c ; P3_IH
mov c, 16 ; 16 bit increments
mov [x3], c ; P3_IL
mov c, 4 ; 16 bit pointer
mov [x3], c ; P3_DS
mov x3, 1
shl x3, a ; Return value (display list size in bits)
; Restore CPU regs & exit
pop a, b, d
rfn c:x3, x3
;
; Implementation of us_smux_reset
;
us_smux_reset_i:
; Check dirty, do nothing unless it is necessary to clear
xbc [us_smux_df], 0
rfn ; No need to clear, already OK
bts [us_smux_df], 0
; Save CPU regs
psh a, d, x2, xm
; Get Display list size
mov c, [P_GDG_DLDEF]
mov x3, 4 ; Smallest display list size is 4 entries
and c, 3
shl x3, c ; 'x3': Count of entries on a display list row
; Calculate bottom end fill value
mov a, [us_smux_cs]
mov c, a ; For top end
xug x3, a
mov a, x3 ; Too large: constrain
mov x2, a
shl x2, 8
or a, x2 ; 'a': Bottom end fill value
; Calculate top end fill value
add c, [us_smux_cc]
xug x3, c
mov c, x3 ; Too large: constrain
mov x2, c
shl x2, 8
or c, x2 ; 'c': Top end fill value
; Prepare pointers
mov xm, 0x6666 ; All pointers PTR16I
mov x2, us_smux_ola
mov x3, us_smux_oha
mov d, 200
add d, x2 ; Loop terminator
; Clear loop
.lp: mov [x2], a
mov [x3], c
mov [x2], a
mov [x3], c
mov [x2], a
mov [x3], c
mov [x2], a
mov [x3], c
xeq x2, d
jms .lp
; Restore CPU regs & exit
pop a, d, x2, xm
rfn c:x3, 0
;
; Implementation of us_smux_setbounds
;
us_smux_setbounds_i:
.cls equ 0 ; Start column
.clc equ 1 ; Count of columns
mov x3, [$.cls]
mov [us_smux_cs], x3
mov x3, [$.clc]
mov [us_smux_cc], x3
btc [us_smux_df], 0 ; Mark dirty
rfn x3, 0
;
; Implementation of us_smux_add
;
us_smux_add_i:
.rch equ 0 ; Render command, high
.rcl equ 1 ; Render command, low
.hgt equ 2 ; Height
.btp equ 3 ; Bottom or Top add (bit 0 zero: bottom)
.psy equ 4 ; Y position (2's complement)
.mul equ 5 ; Width multiplier
.dld equ 6 ; Display list definition
mov sp, 7
; Save CPU regs
psh a, b, d, x0, x1, x2, xm, xb
; Load display list definition
jfa us_dbuf_getlist_i
mov [$.dld], x3
; Retrieve source width to know how much to add to the source line
; select to advance one line. Shift source is ignored (in this routine
; a shift source is useless).
mov x3, [$.rcl]
shr x3, 13 ; Source definition select
add x3, P_GDG_SA0
mov a, [x3]
mov d, 0x3F
and d, a
shl d, 1 ; Size in cells instead of cell pairs
xbs a, 6 ; Tiled mode: Always like X expanded
xbc a, 11 ; X expanded mode
shr d, 1 ; X expanded mode: a cell pair needs one cell source
mov [$.mul], d
.entr: ; Clip the graphics component if needed. If partial from the top, the
; render command itself also alters so respecting the first visible
; line.
mov a, [us_smux_vl]
xsg a, [$.psy]
jms .ntc ; No top clip required
xch a, [$.psy] ; New Y start set in $.psy
sub a, [$.psy] ; 'a': Vertical relocation (negative)
add [$.hgt], a ; New height
mov x3, 0
xbs [$.hgt], 15
xne [$.hgt], x3
jms .exit ; Turned negative or zero: off display to the top
mul a, [$.mul] ; For new source line select
sub [$.rch], a ; OK, new source start calculated
.ntc: mov a, [us_smux_vh]
xug a, [$.psy] ; Completely off display to the bottom?
jms .exit
sub a, [$.psy] ; Number of px. available for the source
xug a, [$.hgt]
mov [$.hgt], a ; Truncate height if necessary
; Rows will be added, so dirty flag will indicate the need to clear
btc [us_smux_df], 0
; Set up PRAM pointer 3
jfa us_smux_setptr_i {[$.psy], [$.dld]}
; Set up X0 and X1 for pointing into the occupation data
mov xb, 0x0000 ; Low bits of 8 bit pointers: even
xbc [$.psy], 0
mov xb, 0x8888 ; Start position is odd
mov x0, [$.psy]
shr x0, 1 ; To word offset
add x0, us_smux_ola ; Low bounds offset
mov x1, 200
add x1, x0 ; High bounds offset
; Init data to add
mov a, [$.rch] ; Start of high part
mov b, [$.rcl] ; Low part (does not change)
; Loop init (in x3 the add value for display list row walking was
; prepared by us_smux_setptr_i)
mov x2, [$.hgt]
mov d, [P3_AL] ; Tracks P3_AL during the loop
xbc [$.btp], 0 ; Add to bottom if 0
jms .t ; Add to top if 1
.b: ; Add to bottom end
mov xm, 0x448C ; X3: PTR16, X2: PTR16, X1: PTR8I, X0: PTR8W
.lpb: mov c, [x0]
xne c, [x1]
jms .lxb ; Equal column offsets: row has no more sprites free
shl c, 5 ; Bit offset of display list column
add c, d
mov [P3_AL], c
mov [P3_RW], a
mov c, 1
mov [P3_RW_NI], b ; Avoid address low wraparound
add [x0], c
.leb: add c:d, x3
add [P3_AH], c
add a, [$.mul]
sub x2, 1
jnz x2, .lpb
jms .exit
.lxb: mov [x0], c ; Just to increment x0:xb0
jms .leb
.lxt: mov [x1], c ; Just to increment x1:xb1
jms .let
.t: ; Add to top end
mov xm, 0x44C8 ; X3: PTR16, X2: PTR16, X1: PTR8W, X0: PTR8I
.lpt: mov c, [x1]
xne c, [x0]
jms .lxt ; Equal column offsets: row has no more sprites free
sub c, 1
mov [x1], c
shl c, 5 ; Bit offset of display list column
add c, d
mov [P3_AL], c
mov [P3_RW], a
mov [P3_RW_NI], b ; Avoid address low wraparound
.let: add c:d, x3
add [P3_AH], c
add a, [$.mul]
sub x2, 1
jnz x2, .lpt
.exit: ; Restore CPU regs & exit
pop a, b, d, x0, x1, x2, xm, xb
rfn c:x3, 0
;
; Implementation of us_smux_addxy
;
us_smux_addxy_i:
.rch equ 0 ; Render command, high
.rcl equ 1 ; Render command, low
.hgt equ 2 ; Height
.btp equ 3 ; Bottom or Top add (bit 0 zero: bottom)
.psx equ 4 ; X position (2's complement)
.psy equ 5 ; Y position (2's complement)
.mul equ 5 ; Width multiplier
.dld equ 6 ; Display list definition
mov sp, 7
; Save CPU regs
psh a, b, d, x0, x1, x2, xm, xb
; Push stuff around a bit to make it right for jumping into
; us_dlist_add_i: load X position in A, and fill the Y position in
; it's place.
mov a, [$.psy]
xch a, [$.psx]
; Load display list definition
jfa us_dbuf_getlist_i
mov [$.dld], x3
; Retrieve source width to know how much to add to the source line
; select to advance one line. Shift source and Tiled mode are ignored
; (in this routine these are useless).
mov x3, [$.rcl]
shr x3, 13 ; Source definition select
add x3, P_GDG_SA0
mov x3, [x3]
mov d, 0x3F
and d, x3
mov c, d ; Store away cell pair width, the output width
xbs x3, 11 ; X expanded mode
shl d, 1 ; Non X expanded mode needs 2 cells per cell pair
mov [$.mul], d
; Check on-screen
xug 640, a ; Off-screen to the right?
jms us_smux_add_i.exit
xbs a, 15 ; Signed? If so, maybe partly on-screen on left.
jms .onsc
; Negative X: possibly partly on-screen. Need to check this situation.
shl c, 4 ; Output width in pixels
add c, a
xsg c, 0 ; 1 or more (signed): graphics is on-screen
jms us_smux_add_i.exit
; Graphics on-screen, render it
.onsc: and a, 0x03FF ; 10 bits for shift / position
mov d, 0xFC00 ; Preserve high part of command
and [$.rcl], d
or [$.rcl], a
jms us_smux_add_i.entr
;
; Implementation of us_smux_addlist
;
us_smux_addlist_i:
.clh equ 0 ; Command list offset, high
.cll equ 1 ; Command list offset, low
.hgt equ 2 ; Height
.btp equ 3 ; Bottom or Top add (bit 0 zero: bottom)
.psy equ 4 ; Y position (2's complement)
.dld equ 5 ; Display list definition
mov sp, 6
; Save CPU regs
psh a, b, d, x0, x1, x2, xm, xb
; Load display list definition
jfa us_dbuf_getlist_i
mov [$.dld], x3
; Clip the graphics component if needed. If partial from the top, the
; render command itself also alters so respecting the first visible
; line.
mov a, [us_smux_vl]
xsg a, [$.psy]
jms .ntc ; No top clip required
xch a, [$.psy] ; New Y start set in $.psy
sub a, [$.psy] ; 'a': Vertical relocation (negative)
add [$.hgt], a ; New height
mov x3, 0
xbs [$.hgt], 15
xne [$.hgt], x3
jms .exit ; Turned negative or zero: off display to the top
shl a, 1 ; To command list offset
sub c:[$.cll], a
add [$.clh], c ; Adjust command list start (carry is 0xFFFF on borrow)
.ntc: mov a, [us_smux_vh]
xug a, [$.psy] ; Completely off display to the bottom?
jms .exit
sub a, [$.psy] ; Number of px. available for the source
xug a, [$.hgt]
mov [$.hgt], a ; Truncate height if necessary
; Rows will be added, so dirty flag will indicate the need to clear
btc [us_smux_df], 0
; Set up PRAM pointers
jfa us_smux_setptr_i {[$.psy], [$.dld]}
jfa us_ptr_setwi_i {2, [$.clh], [$.cll]}
; Set up X0 and X1 for pointing into the occupation data
mov xb, 0x0000 ; Low bits of 8 bit pointers: even
xbc [$.psy], 0
mov xb, 0x8888 ; Start position is odd
mov x0, [$.psy]
shr x0, 1 ; To word offset
add x0, us_smux_ola ; Low bounds offset
mov x1, 200
add x1, x0 ; High bounds offset
; Loop init (in x3 the add value for display list row walking was
; prepared by us_smux_setptr_i)
mov x2, [$.hgt]
mov d, [P3_AL] ; Tracks P3_AL during the loop
xbc [$.btp], 0 ; Add to bottom if 0
jms .t ; Add to top if 1
.b: ; Add to bottom end
mov xm, 0x448C ; X3: PTR16, X2: PTR16, X1: PTR8I, X0: PTR8W
.lpb: mov a, [P2_RW]
mov b, [P2_RW]
mov c, [x0]
xne c, [x1]
jms .lxb ; Equal column offsets: row has no more sprites free
shl c, 5 ; Bit offset of display list column
add c, d
mov [P3_AL], c
mov [P3_RW], a
mov c, 1
mov [P3_RW_NI], b ; Avoid address low wraparound
add [x0], c
.leb: add c:d, x3
add [P3_AH], c
sub x2, 1
jnz x2, .lpb
jms .exit
.lxb: mov [x0], c ; Just to increment x0:xb0
jms .leb
.lxt: mov [x1], c ; Just to increment x1:xb1
jms .let
.t: ; Add to top end
mov xm, 0x44C8 ; X3: PTR16, X2: PTR16, X1: PTR8W, X0: PTR8I
.lpt: mov a, [P2_RW]
mov b, [P2_RW]
mov c, [x1]
xne c, [x0]
jms .lxt ; Equal column offsets: row has no more sprites free
sub c, 1
mov [x1], c
shl c, 5 ; Bit offset of display list column
add c, d
mov [P3_AL], c
mov [P3_RW], a
mov [P3_RW_NI], b ; Avoid address low wraparound
.let: add c:d, x3
add [P3_AH], c
sub x2, 1
jnz x2, .lpt
.exit: ; Restore CPU regs & exit
pop a, b, d, x0, x1, x2, xm, xb
rfn c:x3, 0