-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathraster.acme
554 lines (472 loc) · 15.1 KB
/
raster.acme
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
!ifdef DEBUG {
!warn "bytes wasted: ", IRQ-*
}
!if SPRITES = 0 {
!macro inc_vic_control_y {
lda VIC_CONTROL_Y ; 4
clc ; 2
adc #1 ; 2
and #%00000111 ; 2
adc #CONTROL_Y_INVALID ; 2
sta VIC_CONTROL_Y ; 4
} ;--> 16
} else {
inc_vic_control_y_zone !zone {
lda VIC_CONTROL_Y ; 4
clc ; 2
adc #1 ; 2
and #%00000111 ; 2
adc #CONTROL_Y_INVALID ; 2
sta VIC_CONTROL_Y ; 4
rts ; 6
} ;--> 22
}
!align 255, 0
IRQ !zone {
;-------------------------------------------------------------------------------
; LINE_0
;-------------------------------------------------------------------------------
; irq event ; 7
; last instruction ; 1 (min)
sta restore_regs + 1 ; 4
stx restore_regs + 3 ; 4
sty restore_regs + 5 ; 4
lda VIC_RASTER ; 4
cmp #LINE_0 ; 2
beq + ; 3
jam
+ ;--> 29
; ack interrupt
inc VIC_IRQ_STATUS ; 6
inc VIC_RASTER ; 6
+set16 .irq_line_0_plus_1, VECTOR_IRQ ; 12
;--> 24
; save stack state
tsx ; 2
; begin raster stabilization
cli ; 2
;--> 4
; somewhere here the next interrupt will hit
+wait_even 63 - 29 - 24 - 4
;-------------------------------------------------------------------------------
; LINE_0 + 1
;-------------------------------------------------------------------------------
.irq_line_0_plus_1
; irq event ; 7
; last instruction ;1-2 (because of the nop field above)
; restore stack state
txs ; 2
;--> 10-11
; calculate the number of nops to skip in VSP
lda #SCR_COLS-1 ; 2
sec ; 2
HARD_X = *+1
sbc #0 ; 2
lsr ; 2
sta .self_modifying_branch__nops + 1 ; 4
;--> 12
; introduce an extra cycle in VSP if carry is set
bcs .set_bcc ;2-3
.set_bcs
ldx #BCS_OPCODE ; 2
bne + ; always true ; 3
.set_bcc
ldx #BCC_OPCODE ; 2
nop ; make this path as long ; 2
+
stx .self_modifying_branch__lsb ; 4
;--> 11
; set up soft scroll
lda #CONTROL_X ; 2
clc ; 2
SOFT_X = *+1
adc #0 ; 2
sta VIC_CONTROL_X ; 4
;--> 10
+wait 63-11-12-11-10-6
; wobble check
lda #LINE_0 + 2 ; 2
cmp VIC_RASTER ; 4
;--> 6
+bne ;3-2
; -> the raster interrupt is stable now with 3 cycles off
;-------------------------------------------------------------------------------
; LINE_0 + 2
;-------------------------------------------------------------------------------
; wobble check from above ;--> 3
; make FIRST_BADLINE a bad line
!if SPRITES = 0 {
lda #CONTROL_Y_INVALID ; 2
} else {
lda CTRL_Y_INVALID ; 3
}
sta VIC_CONTROL_Y ; 4
;--> 6/7
lda #SCR_ROWS ; 2
sec ; 2
HARD_Y = *+1
sbc #0 ; 2
tax ; 2
;--> 8
; pre-compute VIC_CONTROL_Y value for soft-scrolling
lda #7 + (SCR_ROWS+1) % 8 ; 2
sec ; 2
SOFT_Y = *+1
sbc #0 ; 2
and #%00000111 ; 2
ora #CONTROL_Y_INVALID ; 2
; spill pre-computed value
pha ; 3
;--> 13/14
!if SPRITES = 0 {
+wait_loop 63 - 3 - 6 - 8 - 13 - 20 - 4
}
;-------------------------------------------------------------------------------
; FLD & line crunch
;-------------------------------------------------------------------------------
.fld_loop
cpx #0 ; 2
beq + ; 2 (in this loop)
!if SPRITES = 0 {
+inc_vic_control_y ; 16
+wait_loop 63 - 20 - 2 - 3
} else {
jsr inc_vic_control_y_zone ; 22 + 6 (jsr)
lda VIC_RASTER ; 4
cmp #SPRITES_TOP_Y ; 2
bcs .sprite_rows_fld_wait ; 2/3
+wait_loop 63 - 32 - 2 - 3 - 4 - 2 - 2
}
dex ; 2
jmp .fld_loop ; 3
+
ldx HARD_Y
.crunch_loop
cpx #0 ; 2
beq + ; 2 (in this loop)
!if SPRITES = 0 {
+inc_vic_control_y ; 16
+wait_loop 63 - 20 - 2 - 3
} else {
jsr inc_vic_control_y_zone ; 22 + 6 (jsr)
lda VIC_RASTER ; 4
cmp #SPRITES_TOP_Y ; 2
bcs .sprite_rows_crunch_wait ; 2/3
+wait_loop 63 - 32 - 4 - 2 - 2 - 2 - 3
}
dex ; 2
jmp .crunch_loop ; 3
+
!if SPRITES != 0 {
.sprite_rows_fld_loop
cpx #0 ; 2
beq + ; 2 (in this loop)
jsr inc_vic_control_y_zone ; 22 + 6 (jsr)
.sprite_rows_fld_wait
+wait_loop 44 - 32 - 2 - 3
dex ; 2
jmp .sprite_rows_fld_loop ; 3
+
ldx HARD_Y
.sprite_rows_crunch_loop
cpx #0 ; 2
beq + ; 2 (in this loop)
jsr inc_vic_control_y_zone ; 22 + 6 (jsr)
.sprite_rows_crunch_wait
+wait_loop 44 - 32 - 2 - 3
dex ; 2
jmp .sprite_rows_crunch_loop ; 3
+
}
; ^^^ these are always 25 raster lines ^^^
;-------------------------------------------------------------------------------
; VSP
;-------------------------------------------------------------------------------
; make VSP line not a bad line
inc VIC_CONTROL_Y ;6
!if SPRITES = 0 {
+wait 5 ;5
} else {
+wait 4 ;4
}
sec ;2
; introduce an extra cycle if 39 - HARD_X is odd
.self_modifying_branch__lsb
+bcs ;2-3
.self_modifying_branch__nops
; always true and jump into nop field
+bcs ;3
+wait 38 ;0-38
!if (>IRQ) != (>*) {
!error "critical irq code in different pages"
}
; generate bad line
dec VIC_CONTROL_Y ;6
; 6 + 4/5 + 2 + 2/3 + 3 + 0/38 + 6 = 23-25/63
;-------------------------------------------------------------------------------
; prepare soft scroll
;-------------------------------------------------------------------------------
; reload pre-computed VIC_CONTROL_Y value
pla
sta VIC_CONTROL_Y
and #CONTROL_Y_MASK
pha
;stx VIC_BORDER ;4
;-------------------------------------------------------------------------------
; use wait time to setup sprites
;-------------------------------------------------------------------------------
; todo
;-------------------------------------------------------------------------------
; issue soft scroll
;-------------------------------------------------------------------------------
pla
; wait till end of AGSP region
ldy #FIRST_BADLINE+SCR_ROWS+8+1
-
cpy VIC_RASTER
bne -
sta VIC_CONTROL_Y
;-------------------------------------------------------------------------------
; sprite multiplexing
;-------------------------------------------------------------------------------
!ifdef DEBUG {
ldx #3
stx VIC_BORDER
; inc VIC_BORDER
}
!if SPRITES < 4 {
ldx #SPRITES
} else {
ldx #4
}
; sprite with y = 28:
; 27--------------------|... dma disabled
; 28--------------------|... dma enabled
; 29------line 1--------|... dma enabled
; ..
; 49------line 21-------|... dma disabled
; 50---ready for next---|<-- triggerline = 28+22
rotate
!for .i, 0, 3 {
!set r = 3-.i
!zone {
.check_done
cpx #SPRITES
bne .next_sprite
.last_irq
!ifdef DEVELOP {
ldy VIC_RASTER
cpy #$f0
bcc +
jam
+
}
ldy #$f0+2
sty VIC_RASTER
+set16 last_irq, VECTOR_IRQ
jmp out
.next_sprite
lda VIC_SPR_0_Y + r*4 ; get y position of physical sprite
clc
adc #22 ; calculate when sprite will be done, see above diagram
bcs .last_irq ; if we overflow we can be sure triggerline is too high
cmp #SPRITES_MAX_Y ; $f9 is the last visible line: ensure triggerline < $f8
bcs .last_irq ; nothing left to do
tay ; y = triggerline for next sprite
sbc VIC_RASTER ; a = triggerline - rasterline - 1 (omit sec)
bcc .display_sprite ; rasterline >= triggerline: display next sprite
cmp #2 ; rasterline < triggerline, a = #lines left - 1
bcs .set_interrupt ; at least 3 lines left to set an interrupt
dey ; fewer than 3 lines left, prepare to poll
-
cpy VIC_RASTER
bcs - ; we might be on a badline: use dey, bcs instead of bne
bcc .display_sprite ; branch always
.set_interrupt
stx SPR_COUNTER
sty VIC_RASTER
+set16 .next_irq, VECTOR_IRQ
jmp out
.next_irq
sta restore_regs + 1
stx restore_regs + 3
sty restore_regs + 5
; !ifdef DEBUG {
; inc VIC_BORDER
; }
ldx SPR_COUNTER
!ifdef DEBUG {
stx VIC_BORDER
}
.display_sprite
ldy SPR_I, x
inx
; y-coord
lda SPR_Y, y
; are we too late?
cmp VIC_RASTER
bcs .do_display_sprite
lda #SPRITES_MAX_Y
cmp VIC_RASTER
bcc .last_irq
jmp .check_done
.do_display_sprite
sta VIC_SPR_0_Y + r*4
sta VIC_SPR_0_Y + r*4 + 2
; x-coord
lda SPR_X, y
asl
sta VIC_SPR_0_X + r*4
sta VIC_SPR_0_X + r*4 + 2
; x-msb
bcc .clear_x_msb
.set_x_msb
lda # %00000011 << 2*r
ora VIC_SPR_X_MSB
bne + ; always true
.clear_x_msb
lda # <!(%00000011 << 2*r)
and VIC_SPR_X_MSB
+
sta VIC_SPR_X_MSB
; colors
lda SPR_A, y
sta VIC_SPR_0_COLOR + r*2 + 1
lda SPR_B, y
sta VIC_SPR_0_COLOR + r*2
; sprite frames
lda SPR_F, y
sta SPR_PTR + r*2 + 1
clc
adc #1
sta SPR_PTR + r*2
!if r = 0 {
jmp rotate
}
}
}
;-------------------------------------------------------------------------------
; we are finishing the last irq this frame
;-------------------------------------------------------------------------------
last_irq
sta restore_regs + 1
stx restore_regs + 3
sty restore_regs + 5
!ifdef DEBUG {
lda #10
sta VIC_BORDER
}
jsr JOYSTICK
!ifdef DEBUG {
lda #11
sta VIC_BORDER
}
jsr COPY_TILES
!ifdef DEBUG {
lda #0
sta VIC_BORDER
}
jsr PLAY_SONG
!if SPRITES != 0 {
!for .i, 0, 3 {
!set .r = 3-.i
; index
ldy SPR_I + .i
; y-coord
lda SPR_Y, y
sta VIC_SPR_0_Y + 4*.r
sta VIC_SPR_0_Y + 4*.r + 2
; x-coord
lda SPR_X, y
asl
sta VIC_SPR_0_X + 4*.r
sta VIC_SPR_0_X + 4*.r + 2
; x-coord MSB
!if .i != 0 {
txa
}
php
rol
plp
rol
tax
; colors
lda SPR_A, y
sta VIC_SPR_0_COLOR + .r*2 + 1
lda SPR_B, y
sta VIC_SPR_0_COLOR + .r*2
; set sprite pointers
lda SPR_F, y
tay
sty SPR_PTR + .r*2 + 1
iny
sty SPR_PTR + .r*2
}
stx VIC_SPR_X_MSB
}
;-------------------------------------------------------------------------------
; sort sprites
;-------------------------------------------------------------------------------
!ifdef DEBUG {
ldx #2
stx VIC_BORDER
; inc VIC_BORDER
}
!if SPRITES > 1 {
!for .i, 0, SPRITES-2 {
ldy SPR_I + .i + 1
!if .i % 2 = 0 {
!set .back_0 = *
} else {
!set .back_1 = *
}
ldx SPR_I + .i
lda SPR_Y, y ; A: r-elem
cmp SPR_Y, x ; M: l-elem
; swap if A < M
; skip if A >= M
bcs +
stx SPR_I + .i + 1
sty SPR_I + .i
!if .i != 0 {
!if .i % 2 = 0 {
bcc .back_1 ; always true
} else {
bcc .back_0 ; always true
}
}
+
}
}
!ifdef DEBUG {
ldx #1
stx VIC_BORDER
; dec VIC_BORDER
}
!ifdef DEVELOP {
lda $d011
and #$80
bne +
lda VIC_RASTER
bmi +
cmp #LINE_0-1 ; ok if VIC_RASTER < LINE_0-1
bcc +
jam ; if there is too little time, stop execution
+
}
+set_raster_line_8 LINE_0
+set16 IRQ, VECTOR_IRQ
out
!ifdef DEBUG {
ldx #0
stx VIC_BORDER
; dec VIC_BORDER
}
inc VIC_IRQ_STATUS
restore_regs
lda #0
ldx #0
ldy #0
rti
EMPTY_INTERRUPT = *-1
}