-
Notifications
You must be signed in to change notification settings - Fork 0
/
vsp.s
737 lines (595 loc) · 10.4 KB
/
vsp.s
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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
// heavily based on Shallan's VSP code
// major differences
// scrolling left & right
// colour shift code is generated by code vs the assembler
* = $02 virtual
mappos: .byte $00
VSP_offset: .byte $00
VSP_offset_old: .byte $00
pixel_xscroll: .byte $00
pixel_speed: .byte $00
VIC_buffer: .byte $00
triggerColourShift: .byte $00
timer: .byte $00
edgeToUpdate: .byte 0
asmdest: .word 0
source: .word 0
dest: .word 0
cdest: .word 0
// max speeds
// anything above 8 will break ( probably )
.label maxspeed = 7
.label negspeed = -7
// vic screen locations
.label PAGE0_LOCATION = $4000
.label PAGE1_LOCATION = $4400
// memory addresses for color shift generation
.label ShiftColorRamR = $7000
.label ShiftColorRamL = ShiftColorRamR+$1800
#import "modules/c64.s"
BasicUpstart2(Entry)
Entry:
sei
//Disable CIA interrupts
lda #$7f
sta CIA1.irq_ctrl
sta CIA2.irq_ctrl
//Enable Raster Interrupts
lda #$01
sta VIC.irq_enable
//set IRQ
lda #<IRQ
ldx #>IRQ
sta $fffe
stx $ffff
//Set line
lda VIC.cr1
and #$7f
sta VIC.cr1
lda #IRQLine
sta VIC.raster
//Bank out kernel and basic
lda #$35
sta $01
//VIC Bank setup
lda #$02
sta CIA2.pra //Vic bank 2
lda #$02 //Screen at $4000, chars at $4800
sta VIC.mem
lda #$d0
sta VIC.cr2 //38 columns, mc mode
//Set colors
lda #$00
sta VIC.border
sta VIC.bg_color0
lda #$9
sta VIC.bg_color1
lda #$0c
sta VIC.bg_color2
jsr ScrollBuild
//ack interrupt
asl VIC.irq
cli
//Reset variables
lda #$00
sta VSP_offset
sta mappos
sta VIC_buffer
sta triggerColourShift
lda #$07
sta pixel_xscroll
lda #$fe
sta pixel_speed
//Initialise map
jsr copyInitialScreen
!Loop:
lda triggerColourShift
cmp #$01 //Only shift color when needed and after all other map update is done
bne !Loop-
lda #$00
sta triggerColourShift
inc VIC.border
jsr ColorShift:ShiftColorRamR
dec VIC.border
jmp !Loop-
copyInitialScreen: {
//Copy first full screen of map, including colors
ldx #39
!:
.for(var i=0; i<24; i++) {
lda MAP + i * $100, x
sta PAGE0_LOCATION + i * $28, x
tay
lda COLORS, y
sta $d800 + i * $28, x
}
dex
bmi !+
jmp !-
!:
//Ensure last line of screen is black to hide garbage
ldx #39
lda #$00
!:
sta $d800 + 24 * 40, x
dex
bpl !-
rts
}
UpdateScroll: {
//Apply the pixel_xscroll value
lda VIC.cr2
and #$f8
ora pixel_xscroll
sta VIC.cr2
rts
}
.label IRQLine = $2f
IRQ:
//save state
pha
txa
pha
tya
pha
lda #$1b
sta VIC.cr1
lda #<StableIRQ
ldx #>StableIRQ
sta $fffe
stx $ffff
inc VIC.raster
asl VIC.irq
tsx
cli
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
.align $100
StableIRQ:
// 0-1 cycles of jitter here now
txs
ldx #$07 //X * 5 + 1
!:
dex
bne !-
nop
nop
nop
clv
//Force a branch if 0 jitter and not if 1 jitter
//causing either 3 or 2 cycles to be added
lda VIC.raster
cmp VIC.raster
beq !+
!:
//Raster is stable here!
//Waste more cycles
ldx #$0a
!:
dex
bne !-
nop
nop
//Waste an exact number of cycles + VSP_offset
lda VSP_offset
lsr
sta selfmod_branch + 1
bcc !+ //2 cycles if VSP_offset was odd, 3 if even
!:
selfmod_branch:
bvc * //relative becomes VSP_offset /2
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
dec VIC.cr1
inc VIC.cr1
//Ack interrupt
asl VIC.irq
//Point back to original IRQ
lda #<ShiftIRQ
ldx #>ShiftIRQ
sta $fffe
stx $ffff
lda #$f1
sta VIC.raster
//restore state
pla
tay
pla
tax
pla
rti
ShiftIRQ:
//save state
pha
txa
pha
tya
pha
//Wait for next line and force invalid vic display mode
//to hide the glitch at the bottom of thje screen
lda VIC.raster
cmp VIC.raster
beq *-3
lda #$7b
sta VIC.cr1
inc VIC.border
//JOYSTICK SPEED CONTROLS
inc timer
lda timer
and #$07
bne !+
lda $dc00
lsr
lsr
!CheckLeft:
lsr
bcs !CheckRight+
ldx pixel_speed
cpx #negspeed
beq !CheckDone+
dec pixel_speed
!CheckRight:
lsr
bcs !CheckDone+
ldx pixel_speed
cpx #maxspeed
beq !CheckDone+
inc pixel_speed
!CheckDone:
!:
lda VSP_offset
sta VSP_offset_old
lda pixel_speed
bne shouldscroll
jmp out
shouldscroll:
bpl scrollright
scrollleft:
{
lda #<updateEdge.Left
sta edgeUpdate
lda #>updateEdge.Left
sta edgeUpdate+1
lda #00
sta edgeToUpdate
sec
lda pixel_xscroll
sbc pixel_speed
sta pixel_xscroll
cmp #$8
bmi noup
sec
sbc #8
sta pixel_xscroll
sec
lda VSP_offset
sbc #1
cmp #$ff
bne noreset
lda #<ShiftColorRamL
sta ColorShift
lda #>ShiftColorRamL
sta ColorShift+1
lda #$01
sta triggerColourShift
sec
lda mappos
sbc #40
sta mappos
lda VIC_buffer
eor #$01
sta VIC_buffer
lda #39
noreset:
sta VSP_offset
noup:
}
jmp out
scrollright:
{
lda #<updateEdge.Right
sta edgeUpdate
lda #>updateEdge.Right
sta edgeUpdate+1
lda #39
sta edgeToUpdate
sec
lda pixel_xscroll
sbc pixel_speed
sta pixel_xscroll
bpl noup
clc
adc #8
sta pixel_xscroll
clc
lda VSP_offset
adc #1
cmp #40
bne noreset
lda #$01
sta triggerColourShift
lda #<ShiftColorRamR
sta ColorShift
lda #>ShiftColorRamR
sta ColorShift+1
clc
lda mappos
adc #40
sta mappos
lda VIC_buffer
eor #$01
sta VIC_buffer
lda #$00
noreset:
sta VSP_offset
noup:
}
out:
ldy VSP_offset
cpy VSP_offset_old
beq noedge
tya
clc
adc mappos
adc edgeToUpdate
tax
jsr edgeUpdate:$0000
noedge:
jsr UpdateScroll
//Point back to original IRQ
lda #<IRQ
ldx #>IRQ
sta $fffe
stx $ffff
lda #IRQLine
sta VIC.raster
asl VIC.irq
dec VIC.border
//restore state
pla
tay
pla
tax
pla
rti
updateEdge:
{
Right:
lda VIC_buffer
asl
asl
asl
asl
ora #$02
sta $d018
lda VIC_buffer
beq buffer1
jmp buffer2
buffer1:
//Map Column copy
.for(var i=0; i<24; i++) {
//Copy nex t column from map
lda MAP + i * $100, x
//To relevant column on screen (back 1 char and down q1 char)
sta PAGE0_LOCATION + $28 + i * $28 - 1, y
//Also to other VIC_buffer
sta PAGE1_LOCATION + i * $28 - 1,y
}
//set column colors
.for(var i=0; i<24; i++) {
lda PAGE0_LOCATION + $28 + i * $28 - 1, y
tax
lda COLORS, x
sta $d828 + i * $28 - 1, y
}
rts
buffer2:
//Map Column copy
.for(var i=0; i<24; i++) {
//Copy nex t column from map
lda MAP + i * $100, x
//To relevant column on screen (back 1 char and down q1 char)
sta PAGE1_LOCATION + $0028 + i * $28 - 1, y
//Also to other VIC_buffer
sta PAGE0_LOCATION + i * $28 - 1,y
}
//set column colors
.for(var i=0; i<24; i++) {
lda PAGE1_LOCATION + $0028 + i * $28 - 1, y
tax
lda COLORS, x
sta $d828 + i * $28 - 1, y
}
rts
Left:
lda VIC_buffer
asl
asl
asl
asl
ora #$02
sta $d018
lda VIC_buffer
beq lbuffer1
jmp lbuffer2
lbuffer1:
//Map Column copy
.for(var i=0; i<24; i++) {
//Copy nex t column from map
lda MAP + i * $100, x
//To relevant column on screen (back 1 char and down q1 char)
sta PAGE0_LOCATION + i * $28 , y
//Also to other VIC_buffer
sta PAGE1_LOCATION+$28 + i * $28 ,y
}
//set column colors
.for(var i=0; i<24; i++) {
lda PAGE0_LOCATION + i * $28 , y
tax
lda COLORS, x
sta $d800 + i * $28 , y
}
rts
lbuffer2:
//Map Column copy
.for(var i=0; i<24; i++) {
//Copy nex t column from map
lda MAP + i * $100, x
//To relevant column on screen (back 1 char and down q1 char)
sta PAGE1_LOCATION + i * $28 , y
//Also to other VIC_buffer
sta PAGE0_LOCATION+ $28 + i * $28 ,y
}
//set column colors
.for(var i=0; i<24; i++) {
lda PAGE1_LOCATION + i * $28 , y
tax
lda COLORS, x
sta $d800 + i * $28 , y
}
rts
}
.macro _setwi(value,dest) {
lda #<value
sta dest
lda #>value
sta dest+1
}
.macro _addwi(source,addition,dest) {
clc
lda source
adc #<addition
sta dest
lda source+1
adc #>addition
sta dest+1
}
// generate color scroll code
// a huge lump of lda $d828 sta $d800 etc.
ScrollBuild:
{
_setwi(ShiftColorRamR,asmdest)
_setwi($d828,source)
_setwi($d800,dest)
ldx #$00
rloop:
ldy #$00
lineloop:
// lda $ram
lda #$ad
sta (asmdest),y
iny
lda source
sta (asmdest),y
iny
lda source+1
sta (asmdest),y
iny
// sta $ram
lda #$8d
sta (asmdest),y
iny
lda dest
sta (asmdest),y
iny
lda dest+1
sta (asmdest),y
iny
_addwi(dest,1,dest)
_addwi(source,1,source)
cpy #$ea
bne lineloop
_addwi(asmdest,$ea,asmdest)
_addwi(dest,1,dest)
_addwi(source,1,source)
inx
cpx #24
beq exitloop
jmp rloop
exitloop:
// rts
ldy #$00
lda #$60
sta (asmdest),y
// left style
_setwi(ShiftColorRamL,asmdest)
_setwi($db98,source)
_setwi($dbc0,dest)
ldx #$00
rloop2:
ldy #$00
lineloop2:
// lda $ram
lda #$ad
sta (asmdest),y
iny
lda source
sta (asmdest),y
iny
lda source+1
sta (asmdest),y
iny
// sta $ram
lda #$8d
sta (asmdest),y
iny
lda dest
sta (asmdest),y
iny
lda dest+1
sta (asmdest),y
iny
_addwi(dest,1,dest)
_addwi(source,1,source)
cpy #$ea
bne lineloop2
_addwi(asmdest,$ea,asmdest)
_addwi(dest,-79,dest)
_addwi(source,-79,source)
inx
cpx #24
beq exitloop2
jmp rloop2
exitloop2:
ldy #$00
lda #$60
sta (asmdest),y
rts
}
.align 256
MAP:
.import binary "data/mw dragon - Map (8bpc, 256x25).bin"
.align 256
COLORS:
.import binary "data/mw dragon - CharAttribs.bin"
* = $4800
.import binary "data/mw dragon - Chars.bin"