-
Notifications
You must be signed in to change notification settings - Fork 1
/
pottendos_utils.asm
753 lines (695 loc) · 11.3 KB
/
pottendos_utils.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
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
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
#importonce
#import "globals.asm"
.namespace CIA1 {
.label base = $dc00
.label PORTA = base
.label PORTB = base + 1
.label DIRA = base + 2
.label DIRB = base + 3
.label TIA = base + 4
.label TIB = base + 6
.label SDR = base + 12
.label ICR = base + 13
.label CRA = base + 14
.label CRB = base + 15
}
.namespace CIA2 {
.label base = $dd00
.label PORTA = base
.label PORTB = base + 1
.label DIRA = base + 2
.label DIRB = base + 3
.label TIA = base + 4
.label TIB = base + 6
.label SDR = base + 12
.label ICR = base + 13
.label CRA = base + 14
.label CRB = base + 15
}
.namespace STD {
.label IRQ = $ea31
.label NMI = $fe47 // orignal address: $fe47, $fe56
.label CONTNMI = $fe56 // jmp after save regs and some CIA1 handling
.label IRQ_VEC = $314
.label NMI_VEC = $318
.label BSOUT = $ffd2
.label CLSCR = $e544
.label PLOT = $fff0
.label GETIN = $ffe4
.label BASIN = $ffcf
.label PI = $aea8
.label FAC1 = $61
.label FAC2 = $69
.label LFAC1 = $bba2 // load FAC1 from a/y
.label SFAC1 = $bbd4 // store FAC1 to x/y
.label LINT = $b391 // load FAC1 from y/a
.label LSACC = $bc3c // 8bit SIGNED acc -> FAC1
.label LUY = $b3a2 // 8bit UNSIGNED Y -> FAC1
.label LSYA = $b391 // 16bit SIGNED y/a -> FAC1
.label F2INT = $bc9b // F2INT -> BigEndian $68-$65
.label FAC2STR = $bddd // FAC1 -> $100
.label FDIV = $bb0f // div mem a/y by FAC1
.label FMUL = $ba28 // mul mem a/y with FAC1
.label FADD = $b867 // add mem a/y with FAC1
.label FSUB = $b850 // sub mem a/y with FAC1
.label SIN = $e26b // SIN(FAC1), in Radians
}
.namespace VIC {
.label base = $d000
.label MEM = base + $18
.label IRR = base + $19
.label IMR = base + $1A
.label CR1 = base + $11
.label RASTER = base + $12
.label SprEnable = base + $15
.label CR2 = base + $16
.label VideoAdr = base + $18
.label SprExpY = base + $17
.label SprExpX = base + $1D
.label BoC = base + $20
.label BgC = base + $21
.label SprColBase = base + $27
}
// utility functions
// .segment _pottendo_utils
.namespace P {
.label zpp1 = $fe // zero-page general purpose pointers
.label zpp2 = $fc
_print: // prints '0' terminated strings from ($fe/$ff) via BSOUT
ldy #$0
!:
lda (zpp1), y
beq done
jsr STD.BSOUT
inc zpp1
bne !-
inc zpp1 + 1
jmp !-
done:
rts
_wscreen:
ldy #$0
!next:
lda (P.zpp1),y
beq !+
jsr STD.BSOUT
inc P.zpp1
bne !next-
inc P.zpp1 + 1
jmp !next-
!:
rts
_readstr:
ldx #$00
_nc:
jsr $E112
cmp #$0d
beq !+
_rdst:
sta $BEEF,x
inx
cpx #$ff
bcc _nc
!:
rts
_readnum:
poke16_(P.zpp1, 0)
sta digitseen // acc is 0 here
l1:
jsr STD.BASIN
bit digitseen
bmi notfirst
cmp #' '
beq l1
notfirst:
cmp #'0' // check valid number literal
bcc ex
sbc #'0'
cmp #10
bcs ex
tax // OK digit 0-9
lda #$ff
sta digitseen
asl P.zpp1
rol P.zpp1 + 1
bcs err
lda P.zpp1 + 1
sta dezcount
lda P.zpp1
asl P.zpp1
rol P.zpp1 + 1
bcs err
asl P.zpp1
rol P.zpp1 + 1
bcs err
adc P.zpp1
sta P.zpp1
lda P.zpp1 + 1
adc dezcount
sta P.zpp1 + 1
bcs err
txa
adc P.zpp1
sta P.zpp1
bcc l2
inc P.zpp1 + 1
beq err
l2:
iny
cpy #6
bcc l1
err:
sec
rts
ex:
bit digitseen
bpl err
clc
lda P.zpp1
ldy P.zpp1 + 1
rts
digitseen: .byte $00
dezcount: .byte $00
binaries: .byte $01, $02, $04, $08, $10, $20, $40, $80
joy_ack: .byte $01
joyaction: .byte $00
tmp: .word $0000
}
// macros
.macro save_regs() {
pha
txa
pha
tya
pha
}
.macro restore_regs() {
pla
tay
pla
tax
pla
}
.macro jsr_ind(addr)
{
lda #<ret
pha
lda #>ret
pha
jmp (addr)
ret:
}
.macro poke8_(addr, val) {
lda #val
sta addr
}
.macro poke8(addr1, addr2) {
lda addr2
sta addr1
}
.macro poke16_(addr, val) {
lda #<val
sta addr
lda #>val
sta addr + 1
}
.macro poke16(addr1, addr2) {
lda addr2
sta addr1
lda addr2 + 1
sta addr1 + 1
}
.macro poke32(addr1, addr2)
{
poke16(addr1, addr2)
poke16(addr1+2, addr2+2)
}
// input best as binary val: e.g. %00010001
.macro setbits(addr, val) {
lda addr
ora #val
sta addr
}
// input best as binary val: e.g. %11101110
.macro clearbits(addr, val) {
lda addr
and #val
sta addr
}
.macro tyx(save) {
.if (save == 1) pha
tya
tax
.if (save == 1) pla
}
.macro txy(save) {
.if (save == 1) pha
txa
tay
.if (save == 1) pla
}
.macro deb(c) {
pha
lda #c
sta $0400 + 1000 - 1
pla
}
// addr, scalar, addr of res-meme
.macro adc16(a, b, res)
{
clc
lda a
adc #<b
sta res
lda a + 1
adc #>b
sta res + 1
}
// addr, addr, addr of res-meme
.macro adc16m(a, b, res)
{
clc
lda a
adc b
sta res
lda a + 1
adc b + 1
sta res + 1
}
// scalar, scalar, addr of res-meme
.macro adc16_(a, b, res)
{
clc
lda #<a
adc #<b
sta res
lda #>a
adc #>b
sta res + 1
}
.macro adc8(a, b, res)
{
clc
lda a
adc #<b
sta res
}
// addr, scalar, addr of res-meme
.macro sbc16(a, b, res)
{
sec
lda a
sbc #<b
sta res
lda a + 1
sbc #>b
sta res + 1
}
// addr, addr, addr of res-meme
.macro sbc16m(a, b, res)
{
sec
lda a
sbc b
sta res
lda a + 1
sbc b + 1
sta res + 1
}
// addr, scalar, addr of res-meme
.macro sbc8(a, b, res)
{
sec
lda a
sbc #<b
sta res
}
// addr, addr, addr of res-meme
.macro sbc8m(a, b, res)
{
sec
lda a
sbc b
sta res
}
// dump acc as bits via BSOUT
.macro dumpbits()
{
tax
ldy #%10000000
sty $fc
ldy #$8
nb:
bit $fc
beq zero
lda #'1'
jmp !+
zero:
lda #'0'
!:
jsr STD.BSOUT
dey
beq !+
txa
ror $fc
jmp nb
!:
lda #13
jsr STD.BSOUT
}
// print '0' terminated string via BSOUT
.macro print(loc)
{
poke16_(P.zpp1, loc)
jsr P._print
}
.macro cmp16_(a, b)
{
lda a + 1
cmp #>b
bne !+
lda a
cmp #<b
!:
}
// compare 16 bit vals a/a+1 vs. b/b+1,
// check BCC lower, BNE higher, BEQ same
.macro cmp16(a, b)
{
lda a + 1
cmp b + 1
bne !+
lda a
cmp b
!:
}
// compare 8 bit in mem with scalar
.macro cmp8_(a, b)
{
lda a
cmp #b
}
// compare 8 bit vars in mem
.macro cmp8(a, b)
{
lda a
cmp b
}
.macro inc16(a)
{
inc a
bne !+
inc a + 1
!:
}
.macro dec16(a)
{
dec a
lda a
cmp #$ff
bne !+
dec a + 1
!:
}
.macro show_screen(clscr, what)
{
.if (clscr == 1) {
jsr STD.CLSCR
}
lda #<what
sta P.zpp1
lda #>what
sta P.zpp1 + 1
jsr P._wscreen
}
.macro set_cursor(x, y)
{
ldx #y
ldy #x
clc
jsr STD.PLOT
}
.macro wstring(x, y, str)
{
set_cursor(x, y)
show_screen(0, str)
}
.macro rstring(addr)
{
lda #<addr
sta P._rdst + 1
lda #>addr
sta P._rdst + 2
jsr P._readstr
lda #$00
sta addr, x
}
.macro rnum(addr)
{
rep:
jsr P._readnum
bcc !+
lda #'?'
jsr STD.BSOUT
jmp rep
!: sta addr
sty addr + 1
}
// fill memory with scalar val
.macro memset_(addr, val, no)
{
poke16_(P.zpp1, addr)
adc16(P.zpp1, no, P.zpp2)
ldy #$00
!l1:
cmp16(P.zpp1, P.zpp2)
beq !+
lda #val
sta (P.zpp1), y
inc16(P.zpp1)
jmp !l1-
!:
}
// fill memory with content of addr pointed to val
.macro memset(addr, val, no)
{
poke16_(P.zpp1, addr)
adc16(P.zpp1, no, P.zpp2)
ldy #$00
!l1:
cmp16(P.zpp1, P.zpp2)
beq !+
lda val
sta (P.zpp1), y
inc16(P.zpp1)
jmp !l1-
!:
}
.macro memcpy(dst, src, n)
{
poke16_(P.zpp1, dst)
poke16_(P.zpp2, src)
adc16(P.zpp1, n, P.tmp)
ldy #$00
l1:
cmp16(P.zpp1, P.tmp)
beq !+
lda (P.zpp2), y
sta (P.zpp1), y
inc16(P.zpp2)
inc16(P.zpp1)
jmp l1
!:
}
.macro memcpy_d(dst, src, n)
{
.if (n == 0)
{
.error "memcpy with 0 length"
}
poke16_(P.tmp, dst)
adc16_(dst, n-1, P.zpp1) // 0'th byte is also copied
adc16_(src, n-1, P.zpp2)
ldy #$00
l1:
lda (P.zpp2), y
sta (P.zpp1), y
cmp16(P.zpp1, P.tmp)
beq !+
dec16(P.zpp2)
dec16(P.zpp1)
jmp l1
!:
}
.macro memcpy_f(dst, src, n)
{
ldx #n
!: lda (src - 1), x
sta (dst - 1), x
dex
bne !-
}
// Spr# 0-7, "on"/"off"
.macro sprite(sprno, a, b)
{
.if (a == "on")
{
ldx #sprno
lda P.binaries, x
ora VIC.SprEnable
sta VIC.SprEnable
}
.if (a == "off")
{
ldx #sprno
lda P.binaries, x
eor #$ff
and VIC.SprEnable
sta VIC.SprEnable
}
.if (a == "color_")
{
ldx #sprno
lda #b
sta VIC.SprColBase, x
}
.if (a == "color")
{
ldx #sprno
lda b
sta VIC.SprColBase, x
}
.if (a == "expx")
{
ldx #sprno
lda P.binaries, x
.if (b == "on")
{
ora VIC.SprExpX
sta VIC.SprExpX
} else {
eor #$ff
and VIC.SprExpX
sta VIC.SprExpX
}
}
.if (a == "expy")
{
ldx #sprno
lda P.binaries, x
.if (b == "on")
{
ora VIC.SprExpY
sta VIC.SprExpY
} else {
eor #$ff
and VIC.SprExpY
sta VIC.SprExpY
}
}
}
.macro sprite_pos(sprno, x, y)
{
lda x
sta VIC.base + sprno * 2 // spr# 1-8
lda y
sta VIC.base + sprno * 2 + 1
ldx #sprno
lda P.binaries, x
ldx x + 1
beq clhb
ora VIC.base + $10
sta VIC.base + $10
jmp !+
clhb:
eor #$ff
and VIC.base + $10
sta VIC.base + $10
!:
}
// immediate addressing
.macro sprite_pos_(sprno, x, y)
{
lda #<x
sta VIC.base + sprno * 2 // spr# 1-8
lda #<y
sta VIC.base + sprno * 2 + 1
ldx #sprno
lda P.binaries, x
.if ((x & $ff00) > 0)
{
ora VIC.base + $10
sta VIC.base + $10
}
else {
eor #$ff
and VIC.base + $10
sta VIC.base + $10
}
}
.macro sprite_sel_(vram, sprbase, sprno, sprptr)
{
lda #((sprbase + sprptr * 64) / 64)
sta vram + $3f8 + sprno
}
// select sprite from acc
.macro sprite_sel_acc(vram, sprno)
{
sta vram + $3f8 + sprno
}
.macro on_joy(a, fn)
{
.if (a == "right"){
lda CIA1.PORTA
and #$08
}
.if (a == "left"){
lda CIA1.PORTA
and #$04
}
.if (a == "down"){
lda CIA1.PORTA
and #$02
}
.if (a == "up"){
lda CIA1.PORTA
and #$01
}
bne !+
jsr fn
poke8_(P.joyaction, 1)
!:
}
.macro on_joyfire(a, fn)
{
.if (a == "fire"){
lda #$10
bit CIA1.PORTA
bne not_pressed
lda P.joy_ack
beq out
jsr fn
dec P.joy_ack
jmp out
not_pressed:
poke8_(P.joy_ack, 1)
out:
}
}
.macro roms_off()
{
sei
lda $01
pha
and #%11111100
ora #%00000010
sta $01
cli
}
.macro roms_on()
{
sei
pla
sta $01
cli
}