-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Atari2600.spin2
2658 lines (2229 loc) · 115 KB
/
Atari2600.spin2
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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
Atari 2600 Emulator
Copyright (c) 2024 by Marco Maccaferri <[email protected]>
TERMS OF USE: MIT License
}
CON
_CLKFREQ = 320_000_000
'_DEBUGGER
VGA_BASE_PIN = 48
AUDIO_BASE_PIN = 38
USB_BASE_PIN = 40
ACTIVITY_LED = 57
ERROR_LED = 56
VSYNC_LINES = 3 + 32
VISIBLE_LINES = 210
REGP_N = 7
REGP_V = 6
REGP_B = 4
REGP_D = 3
REGP_I = 2
REGP_Z = 1
REGP_C = 0
CTL_CLK = 24
CTL_INT = 25
CTL_NMI = 26
CTL_RESET = 27
CTL_STEP = 28
UART_RX = 63 '8 { I }
UART_TX = 62 '9 { O }
UART_BAUD = 2_000_000
VAR
long fb_ptr
byte fb0[160 * VISIBLE_LINES]
byte fb1[160 * VISIBLE_LINES]
long m6532_io
byte m6532_ram[128]
long tia_aud0
long tia_aud1
long tia_inpt
long pad0
long pad1
long cpu_cog
long audio_cog
long debug_cog
OBJ
usb : "USB" | USB_BASE_PIN = 40, EMUPAD_MAX_PLAYER = 2, EMUPAD_BUILTIN_RULES = true, ...
EMUPAD_KB_BT0 = $E0, EMUPAD_KB_BT1 = $2C, EMUPAD_KB_BT2 = $5D, ...
EMUPAD_KB_BT3 = $00, EMUPAD_KB_BT4 = $00, EMUPAD_KB_BT5 = $00, EMUPAD_KB_BT6 = $00, ...
EMUPAD_KB_BT7 = $00, EMUPAD_KB_BT8 = $3A, EMUPAD_KB_BT9 = $3B, EMUPAD_KB_BT6 = $00
video : "VGA" | VISIBLE_LINES = VISIBLE_LINES
audio : "Audio"
dbg : "Debugger"
PUB main() | msg_expire
bytefill(@fb0, $80 >> 1, 160 * VISIBLE_LINES)
bytefill(@fb1, $80 >> 1, 160 * VISIBLE_LINES)
m6532_io := %00000000_00111111_00000000_1111_1111
' |||||||| |||||||| |||||||| ++++-++++-- (B0) SWCHA
' |||||||| |||||||| |||||||| |||| |||+-- (#0) P1 UP
' |||||||| |||||||| |||||||| |||| ||+--- (#1) P1 DOWN
' |||||||| |||||||| |||||||| |||| |+---- (#2) P1 LEFT
' |||||||| |||||||| |||||||| |||| +----- (#3) P1 RIGHT
' |||||||| |||||||| |||||||| |||+------- (#4) P0 UP
' |||||||| |||||||| |||||||| ||+-------- (#5) P0 DOWN
' |||||||| |||||||| |||||||| |+--------- (#6) P0 LEFT
' |||||||| |||||||| |||||||| +---------- (#7) P0 RIGHT
' |||||||| |||||||| ++++++++------------ (B1) SWACNT
' |||||||| ++++++++--------------------- (B2) SWCHB
' |||||||| |+--------------------- (#16) RESET
' |||||||| +---------------------- (#17) SELECT
' ++++++++------------------------------ (B3) SWBCNT
tia_inpt := %00000000_00000000_00000000_00110000
' |||||+--- (#0) INPT0
' ||||+---- (#1) INPT1
' |||+----- (#2) INPT2
' ||+------ (#3) INPT3
' |+------- (#4) INPT4 P0 BUTTON
' +-------- (#5) INPT5 P1 BUTTON
usb.set_emupad_ptr(@pad0)
usb.start()
fb_ptr := @fb0
video.start(VGA_BASE_PIN, @fb_ptr)
cpu_cog := audio_cog := -1
debug_cog := -1
tia_fbp := @fb_ptr
tia_fb0 := @fb0
tia_fb1 := @fb1
tia_inptp := @tia_inpt
aud_addr := @tia_aud0
ppa_addr := @m6532_io
ram_addr := @m6532_ram
rom_addr := @cartridge_rom
#ifndef _DEBUGGER
'audio.start(AUDIO_BASE_PIN, aud_addr)
'coginit(COGEXEC_NEW, @m6507, 0)
#endif
video.set_colors($87, $80 >> 1)
video.prints_at((80 - 35) / 2, 3, string("P2 Atari 2600 Video Computer System"))
video.prints_at((80 - 46) / 2, 4, string("Copyright (c) 2024 Marco Maccaferri and Others"))
showMenu()
repeat
m6532_io, tia_inpt := update_joysticks(pad0, pad1, m6532_io, tia_inpt)
if usb.keystate(usb.KEY_F3) ' Color TV
m6532_io.[19] := 1 ' |
video.clear()
video.prints_at(35, 28, " Color TV ")
repeat while usb.keystate(usb.KEY_F3)
msg_expire := getct() + CLKFREQ * 2
if usb.keystate(usb.KEY_F4) ' Black & White TV
m6532_io.[19] := 0 ' |
video.clear()
video.prints_at(36, 28, " B&W TV ")
repeat while usb.keystate(usb.KEY_F4)
msg_expire := getct() + CLKFREQ * 2
if usb.keystate(usb.KEY_F5) ' Left Difficulty A
m6532_io.[22] := 1 ' |
video.clear()
video.prints_at(30, 28, " Left Difficulty A ")
repeat while usb.keystate(usb.KEY_F5)
msg_expire := getct() + CLKFREQ * 2
if usb.keystate(usb.KEY_F6) ' Left Difficulty B
m6532_io.[22] := 0 ' |
video.clear()
video.prints_at(30, 28, " Left Difficulty B ")
repeat while usb.keystate(usb.KEY_F6)
msg_expire := getct() + CLKFREQ * 2
if usb.keystate(usb.KEY_F7) ' Right Difficulty A
m6532_io.[23] := 1 ' |
video.clear()
video.prints_at(30, 28, " Right Difficulty A ")
repeat while usb.keystate(usb.KEY_F7)
msg_expire := getct() + CLKFREQ * 2
if usb.keystate(usb.KEY_F8) ' Right Difficulty B
m6532_io.[23] := 0 ' |
video.clear()
video.prints_at(30, 28, " Right Difficulty B ")
repeat while usb.keystate(usb.KEY_F8)
msg_expire := getct() + CLKFREQ * 2
if usb.keystate(usb.KEY_ESC)
showMenu()
if pad0.[usb.EMUPAD_BT8_BIT] and pad0.[usb.EMUPAD_BT9_BIT]
showMenu()
if pad1.[usb.EMUPAD_BT8_BIT] and pad1.[usb.EMUPAD_BT9_BIT]
showMenu()
if msg_expire <> 0 and pollct(msg_expire)
video.clear()
msg_expire := 0
PRI update_joysticks(pad0, pad1, io, inpt) : r_io, r_inpt
org
getnib temp, pad0, #0 ' P0 DIRECTION
xor temp, #%1111 ' |
setnib io, temp, #1 ' |
test pad0, all wz ' P0 BUTTON
bitz inpt, #4 ' |
getnib temp, pad1, #0 ' P1 DIRECTION
xor temp, #%1111 ' |
setnib io, temp, #0 ' |
test pad1, all wz ' P1 BUTTON
bitz inpt, #5 ' |
testb pad0, #usb.EMUPAD_BT8_BIT wz ' SELECT
testb pad1, #usb.EMUPAD_BT8_BIT orz ' |
bitnz io, #17 ' |
testb pad0, #usb.EMUPAD_BT9_BIT wz ' RESET
testb pad1, #usb.EMUPAD_BT9_BIT orz ' |
bitnz io, #16 ' |
all long %0000_000000000000000000001111_0000
temp long 0
end
return io, inpt
VAR
long current
PRI showMenu() | ptr, selection, src, size, i
if cpu_cog <> -1
cogstop(cpu_cog)
if audio_cog <> -1
cogstop(audio_cog)
cpu_cog := audio_cog := -1
if debug_cog <> -1
cogstop(debug_cog)
debug_cog := -1
video.set_colors($87, $80)
video.box(18, 6, 44, 20)
ptr := @rom0
repeat while long[ptr] <> 0
video.prints_at(19, selection + 7, ptr + 4)
ptr += long[ptr]
selection++
repeat while usb.keystate(usb.KEY_ESC) or ...
pad0.[usb.EMUPAD_BT8_BIT] or pad0.[usb.EMUPAD_BT9_BIT] or ...
pad1.[usb.EMUPAD_BT8_BIT] or pad1.[usb.EMUPAD_BT9_BIT]
ptr := @rom0
selection := 0
if current <> 0
repeat while ptr <> current
ptr += long[ptr]
selection++
repeat
video.set_colors($80, $87)
video.prints_at(19, selection + 7, ptr + 4)
if usb.keystate(usb.KEY_UP) or pad0.[usb.EMUPAD_UP_BIT] or pad1.[usb.EMUPAD_UP_BIT]
if selection > 0
video.set_colors($87, $80)
video.prints_at(19, selection + 7, ptr + 4)
selection--
ptr := @rom0
i := 0
repeat while i <> selection
i++
ptr += long[ptr]
repeat while usb.keystate(usb.KEY_UP) or pad0.[usb.EMUPAD_UP_BIT] or pad1.[usb.EMUPAD_UP_BIT]
if usb.keystate(usb.KEY_DOWN) or pad0.[usb.EMUPAD_DOWN_BIT] or pad1.[usb.EMUPAD_DOWN_BIT]
if long[ptr + long[ptr]] <> 0
video.set_colors($87, $80)
video.prints_at(19, selection + 7, ptr + 4)
selection++
ptr := @rom0
i := 0
repeat while i <> selection
i++
ptr += long[ptr]
repeat while usb.keystate(usb.KEY_DOWN) or pad0.[usb.EMUPAD_DOWN_BIT] or pad1.[usb.EMUPAD_DOWN_BIT]
if usb.keystate(usb.KEY_ENTER) or (pad0 & %1111_0000) or (pad1 & %1111_0000)
rom_addr &= !(1 << 12)
src := ptr + 4 + strsize(ptr + 4) + 1
size := long[ptr] - (4 + strsize(ptr + 4) + 1)
if size == 2048
bytemove(@cartridge_rom, src, 2048)
bytemove(@cartridge_rom + 2048, src, 2048)
elseif size == 4096
bytemove(@cartridge_rom, src, 4096)
bytemove(@cartridge_rom + 4096, src, 4096)
elseif size == 8192
bytemove(@cartridge_rom, src, 8192)
rom_addr |= (1 << 12)
bytefill(@fb0, $80 >> 1, 160 * VISIBLE_LINES)
bytefill(@fb1, $80 >> 1, 160 * VISIBLE_LINES)
video.clear()
m6532_io.[7..0] := %1111_1111
m6532_io.[16] := m6532_io.[17] := 1
tia_inpt.[4] := tia_inpt.[5] := 1
#ifndef _DEBUGGER
audio_cog := audio.start(AUDIO_BASE_PIN, aud_addr)
cpu_cog := coginit(COGEXEC_NEW, @m6507, 0)
#else
debug_cog := dbg.start(@m6507, ram_addr, rom_addr, tia_fb0)
#endif
current := ptr
repeat while usb.keystate(usb.KEY_ENTER) or (pad0 & %1111_0000) or (pad1 & %1111_0000)
quit
video.set_colors($87, $84)
DAT ' M6507
org $000
m6507
loc pb, #@m6507_lut - @m6507
add pb, ptrb
setq2 #(@m6507_lut_end - @m6507_lut - 4) / 4
rdlong 0, pb
mov ea, ##$0FFC ' read reset vector (atari rom)
add ea, rom_addr ' |
rdword _PC, ea ' |
wrfast #0, tia_fb0
#ifdef _DEBUGGER
jmp #.resume1
#endif
.loop
call #\_fetch ' fetch instruction
shl t1, #2 ' decode instruction
add t1, i_table ' |
rdlong t1, t1 ' |
getnib t2, t1, #7 ' process cycles
.l2 callpa #3, #_inc_cycles ' |
djnz t2, #.l2 ' |
setnib t1, #0, #7
push #.resume
execf t1 ' execute instruction
.resume
bitl _SYSF, #2 wcz
if_1x call #\i_wsync
#ifdef _DEBUGGER
.resume1 rdlong t1, ptra
testb t1, #CTL_STEP wz
if_x1 getptr ptrb
if_x1 call #\@@m6507_debug
#endif
jmp #.loop
'
'
' System Tasks
'
_inc_cycles
' add _I, #1 ' update instruction and total cycles count
' M6532
m6532_timer skipf m6532_normal ' ------------+-+
getbyte r0, _TIMER, #0 ' a b
decmod r0, #$FF ' | b
setbyte _TIMER, r0, #0 ' | b
getword r1, _TIMER, #1 ' a |
cmpsub _TC, r1 wc ' a |
if_nc jmp #_no_timint ' a |
decmod r0, #$FF wc ' a |
setbyte _TIMER, r0, #0 ' a |
if_c bith _TIMER, #14 addbits 1 ' a | TIMINT
if_c setd m6532_timer, #m6532_timint ' a |
_no_timint
' TIA
tia_clock skipf #%0 ' ----------------+
add _TC, #1 ' a
add _HC, #3 ' |
cmpsub _HC, #228 wc ' |
if_c add _VC, #1 ' |
cmp _VC, #VSYNC_LINES wcz ' |
if_ae cmpr _VC, #(VSYNC_LINES + VISIBLE_LINES) -1 wcz ' |
if_ae setd tia_clock, #%1111111_0 ' | scanline
ret wcz ' |
' loop counter set to pa
_loop cmp _HC, #68 wc ' horizontal blank
testb _SYSF, #1 wz ' VBLANK
if_nc_and_z wfbyte #$00 ' |
if_c_or_z jmp #_skip ' |
' Playfield
tia_pfbl skipf tia_pfbl_0 ' ------------+-+-+-+
_colubk mov t_pixel, #0-0 ' a b c d COLUBK (S/B0)
_pfs testb t_pf, t_cnt20 wc ' a b c |
if_1x setbyte t_pixel, t_pf_colu, #0 ' a b c |
if_1x bith t_pixel, #29 ' a b c | signal playfield active
' Ball
_bl_res cmp _HC, #0-0 wz ' a | | d RESBL (S/B0)
if_nz jmp #_no_ball ' a | | d
getbyte t_cntb, _SYSF, #1 ' a | | d BALL SIZE
setd tia_pfbl, #tia_bl_draw ' a | | d
_colubl setbyte t_pixel, #0-0, #0 ' a b | d
bith t_pixel, #24 ' a b | d signal ball active
decmod t_cntb, #0 wc ' a b | d
if_c setd tia_pfbl, #tia_pfbl_0 ' a b | d
_no_ball
' Player 1
tia_p1 skipf tia_px_a ' --------------+-+---+
_p1_res_alt cmp _HC, #0-0 wz ' a | |
if_e skipf #%01_1111_0 ' c ----------- a |-+ |
_p1_size_alt mov t_cnt1, #0-0 ' a | c |
_p1_res cmp _HC, #0-0 wz ' a | | e
if_ne cmp _HC, #0-0 wz ' | | | e
if_ne cmp _HC, #0-0 wz ' | | | e
if_ne jmp #\_no_p1 ' a | | e
mov t_cnt1, #0 ' a | | e
mov t_mask1, t_mask1+1 ' a | c e
setd tia_p1, #tia_px_b ' a | c e
testb t_mask1, t_cnt1 wc ' a b c e
_colup1 if_1x setbyte t_pixel, #0-0, #0 ' a b c e COLUP1 (S/B0)
if_1x bith t_pixel, #27 ' a b c e signal player1 active
incmod t_cnt1, t_size1 wc ' a b c e
if_c setd tia_p1, #tia_px_e ' a b c e
_no_p1
' Missile 1
_m1_enable jmp #\_no_m1 ' M1 ENABLED
tia_m1 skipf #%0000_00_0110 ' -----------+-+-+
_m1_res cmp _HC, #0-0 wz ' a | e
if_ne cmp _HC, #0-0 wz ' | | e
if_ne cmp _HC, #0-0 wz ' | | e
if_ne jmp #\_no_m1 ' a | e
mov t_cntm1, #%0 ' a | e
setd tia_m1, #%0000_11_1111 ' a | e
setbyte t_pixel, _colup1, #0 ' a b e COLUP1 (S/B0)
bith t_pixel, #25 ' a b e signal missile1 active
_m1_size incmod t_cntm1, #0 wc ' a b e
if_c setd tia_m1, #%0000_00_0000 ' a b e
_no_m1
' Player 0
tia_p0 skipf tia_px_a ' --------------+-+---+
_p0_res_alt cmp _HC, #0-0 wz ' a | |
if_e skipf #%01_1111_0 ' c ----------- a |-+ |
_p0_size_alt mov t_cnt0, #0-0 ' a | c |
_p0_res cmp _HC, #0-0 wz ' a | | e
if_ne cmp _HC, #0-0 wz ' | | | e
if_ne cmp _HC, #0-0 wz ' | | | e
if_ne jmp #\_no_p0 ' a | | e
mov t_cnt0, #0 ' a | | e
mov t_mask0, t_mask0+1 ' a | c e
setd tia_p0, #tia_px_b ' a | c e
testb t_mask0, t_cnt0 wc ' a b c e
_colup0 if_1x setbyte t_pixel, #0-0, #0 ' a b c e COLUP0 (S/B0)
if_1x bith t_pixel, #28 ' a b c e signal player0 active
incmod t_cnt0, t_size0 wc ' a b c e
if_c setd tia_p0, #tia_px_e ' a b c e
_no_p0
' Missile 0
_m0_enable jmp #\_no_m0 ' M0 ENABLED
tia_m0 skipf #%0000_00_0110 ' -----------+-+-+
_m0_res cmp _HC, #0-0 wz ' a | e
if_ne cmp _HC, #0-0 wz ' | | e
if_ne cmp _HC, #0-0 wz ' | | e
if_ne jmp #\_no_m0 ' a | e
mov t_cntm0, #%0 ' a | e
setd tia_m0, #%0000_11_1111 ' a | e
setbyte t_pixel, _colup0, #0 ' a b e COLUP0 (S/B0)
bith t_pixel, #26 ' a b e signal missile0 active
_m0_size incmod t_cntm0, #0 wc ' a b e
if_c setd tia_m0, #%0000_00_0000 ' a b e
_no_m0
' Collisions
getbyte r0, t_pixel, #3 ' update collision latches
altgw r0, #_COLL_TBL ' |
getword r0 ' |
or _COLL, r0 ' |
' Output
testb _PF, #26 wc ' PFP
testb t_pixel, #29 wz ' playfield active
testb t_pixel, #24 orz ' ball active
if_11 setbyte t_pixel, _colubl, #0 ' |
cmp _HC, #68 + 8 wc
testb _SYSF, #16 andc ' HMOVE hit blank
if_c wfbyte #$00
if_nc wfbyte t_pixel
incmod t_cnt4, #4-1 wc
if_c mov t_pf, t_pf+2 ' update playfield every 4th clock
if_c mov t_pf+1, t_pf+3 ' |
if_c incmod t_cnt20, #20-1 wc
if_c bitnot _pfs, #9 ' toggle left(0)/right(1) playfields
if_c bitnot _pfs+1, #0 ' toggle left(0)/right(1) colors
_skip incmod _HC, #228-1 wc
djnz pa, #_loop
if_nc ret wcz
bitl _SYSF, #17 wcz
bitc _SYSF, #16
mov t_cnt4, #0
mov t_cnt20, #0
bitl _pfs, #9 ' toggle left(0)/right(1) playfields
bitl _pfs+1, #0 ' toggle left(0)/right(1) colors
setd tia_p0, #tia_px_a
setd tia_m0, #%0000_00_0110
setd tia_p1, #tia_px_a
setd tia_m1, #%0000_00_0110
setd tia_pfbl, #tia_pfbl_0
add _VC, #1
cmp _VC, #(VSYNC_LINES + VISIBLE_LINES) wcz
if_ae setd tia_clock, #0
ret wcz
orgf ($ + 1) & !1 ' must be aligned on even address
t_pf long %00000000000000000000, %00000000000000000000 ' |
long %00000000000000000000, %00000000000000000000 ' |
t_pf_colu long $00, $00 ' |
t_cnt4 long 0
t_cnt20 long 0
t_cntb long 0
t_cntm1 long 0
t_cntm0 long 0
t_hmove_clk long 0
tia_pfbl_0 long %1111_1111_000_0 ' a/c (default ball disabled)
tia_bl_draw long %0000_1111_000_0 ' b
tia_bl_disable long %1111_1111_000_0 ' c (will be muxed to tia_pfbl_0)
tia_pf_disable long %0000_0000_111_0 ' d (will be muxed to tia_pfbl_0 and tia_bl_draw)
tia_resp0 long 0
tia_resp1 long 0
tia_resm0 long 0
tia_resm1 long 0
tia_px_a long %00000_000_0110_000
tia_px_b long %00000_111_1111_111
tia_px_e long %00000_000_0000_111
tia_m0_disable jmp #\_no_m0
tia_m1_disable jmp #\_no_m1
t_pixel long %00000000_0000000000000000_00000000
' |||||| ++++++++-- COLU
' |||||+---------------------------- BL (#24)
' ||||+----------------------------- M1 (#25)
' |||+------------------------------ M0 (#26)
' ||+------------------------------- P1 (#27)
' |+-------------------------------- P0 (#28)
' +--------------------------------- PF (#29)
t_mask0 long 0[2]
t_mask1 long 0[2]
t_size0 long 0
t_size1 long 0
t_cnt0 long 0
t_cnt1 long 0
m6532_normal long %0000000_110 ' a -> 14 clk (no timint) 20 clk (timint)
m6532_timint long %1111111_000 ' b -> 10 clk
'
'
' Instructions
'
t_vsync testb t1, #1 wc
bitc _SYSF, #0 wcz
if_nc ret wcz
rdlong _SW, ppa_addr
rdlong _INPT, tia_inptp
#ifdef _DEBUGGER
wrfast #0, tia_fb0
#else
bitnot _SYSF, #31 wcz ' c=old display buffer
if_0x wrlong tia_fb0, tia_fbp ' old is fb1, display fb0
if_0x wrfast #0, tia_fb1 ' write to fb1
if_1x wrlong tia_fb1, tia_fbp ' old is fb0, display fb1
if_1x wrfast #0, tia_fb0 ' write to fb0
#endif
.w rdlong r0, tia_fbp wz ' wait buffer ack
if_nz jmp #.w ' |
mov _VC, #0
_ret_ setd tia_clock, #0
i_wsync callpa #3, #_inc_cycles
cmp _HC, #0 wz
if_nz jmp #\i_wsync
ret
' C NVZC
i_branch call #\_fetch ' BCC: %0_1110_0
' BCS: %1_1110_0
' BNE: %0_1101_0
' BEQ: %1_1101_0
testb _P, #REGP_C wc ' BPL: %0_0111_0
testb _P, #REGP_Z wc ' BMI: %1_0111_0
testb _P, #REGP_V wc ' BVC: %0_1011_0
testb _P, #REGP_N wc ' BVS: %1_1011_0
modc _nc wc ' Negate condition
if_0x ret
testb _PC, #8 wc
signx t1, #7
add _PC, t1
testb _PC, #8 wz
if_z_ne_c callpa #3, #_inc_cycles
callpa #3, #_inc_cycles
_ret_ and _PC, i_ffff
_push_t1 getbyte ea, _S, #0
add ea, #$100
decmod _S, #$FF
call #\_write
mov pa, #3
jmp #\_inc_cycles
_pop_t1 incmod _S, #$FF
getbyte ea, _S, #0
add ea, #$100
jmp #\_read
t_setup_1c mov t2, pa
_ret_ mov r0, t2
t_setup_2c mov r0, pa
mov r1, r0
add r1, pb
cmpsub r1, #228 wc
if_c jmp #.l1
mov t2, r0
mov t3, r1
_ret_ mov r0, t3
.l1 add r1, #68
mov t2, r1
mov t3, r0
_ret_ mov r0, t3
t_setup_3c mov r0, pa
mov r1, r0
add r1, pb
cmpsub r1, #228 wc
if_c jmp #.l1
mov r2, r1
add r2, pb
cmpsub r2, #228 wc
if_c jmp #.l2
mov t2, r0
mov t3, r1
mov t4, r2
_ret_ mov r0, t4
.l1 add r1, #68
mov t2, r1
add r1, pb
mov t3, r1
mov t4, r0
_ret_ mov r0, t4
.l2 add r2, #68
mov t2, r2
mov t3, r0
mov t4, r1
_ret_ mov r0, t4
t_update_p0 testb _P0, #25 wc ' VDEL
if_1x getbyte t_mask0+1, _P0, #0 ' GRP
if_0x getbyte t_mask0+1, _P0, #1 ' GRPd
testb _P0, #24 wz ' REFP
if_x0 rev t_mask0+1
if_x0 shr t_mask0+1, #24
getnib r0, _P0, #4 ' NUSIZ (D2-D0)
and r0, #%111
mov t_size0, #8
mov r1, #5
cmp r0, #%101 wz ' double size player
if_ne cmp r0, #%111 wz ' quad sized player
if_e setword t_mask0+1, t_mask0+1, #1
if_e mergew t_mask0+1
if_e mov t_size0, #16
if_e mov r1, #5+1 ' delayed one extra pixel
cmp r0, #%111 wz ' quad sized player
if_e setword t_mask0+1, t_mask0+1, #1
if_e mergew t_mask0+1
if_e mov t_size0, #32
getbyte r2, t_res_p0o, #0 ' adjust position based on player size
setbyte t_res_p0o, r1, #0 ' |
sub r1, r2 ' |
add tia_resp0, r1 ' |
mov pa, tia_resp0
call #\t_setup_position
sets _p0_res+0, t2
sets _p0_res+1, t3
sets _p0_res+2, t4
sets _p0_res_alt, #$FF ' alt sprite is wrapped from right side
add r0, t_size0 ' |
cmpsub r0, #228 wcz ' |
if_c_and_nz sets _p0_res_alt, #68 ' alt trigger always first pixel
if_c_and_nz mov t5, t_size0 ' |
if_c_and_nz sub t5, r0 ' |
sets _p0_size_alt, t5 ' |
sub t_size0, #1
testb _MB, #27 wc
if_c jmp #t_resmp0_update
ret
t_update_p1 testb _P1, #25 wc ' VDEL
if_1x getbyte t_mask1+1, _P1, #0 ' GRP
if_0x getbyte t_mask1+1, _P1, #1 ' GRPd
testb _P1, #24 wz ' REFP
if_x0 rev t_mask1+1
if_x0 shr t_mask1+1, #24
getnib r0, _P1, #4 ' NUSIZ (D2-D0)
and r0, #%111
mov t_size1, #8
mov r1, #5
cmp r0, #%101 wz ' double size player
if_ne cmp r0, #%111 wz ' quad sized player
if_e setword t_mask1+1, t_mask1+1, #1
if_e mergew t_mask1+1
if_e mov t_size1, #16
if_e mov r1, #5+1 ' delayed one extra pixel
cmp r0, #%111 wz ' quad sized player
if_e setword t_mask1+1, t_mask1+1, #1
if_e mergew t_mask1+1
if_e mov t_size1, #32
getbyte r2, t_res_p1o, #0 ' adjust position based on player size
setbyte t_res_p1o, r1, #0 ' |
sub r1, r2 ' |
add tia_resp1, r1 ' |
mov pa, tia_resp1
call #\t_setup_position
sets _p1_res+0, t2
sets _p1_res+1, t3
sets _p1_res+2, t4
sets _p1_res_alt, #$FF ' alt sprite is wrapped from right side
add r0, t_size1 ' |
cmpsub r0, #228 wcz ' |
if_c_and_nz sets _p1_res_alt, #68 ' alt trigger always first pixel
if_c_and_nz mov t5, t_size1 ' |
if_c_and_nz sub t5, r0 ' |
sets _p1_size_alt, t5 ' |
sub t_size1, #1
testb _MB, #28 wc
if_c jmp #t_resmp1_update
ret
t_setup_position
' On Enter: pa = position
' r0 = NUSIZ
' On Exit: t2 = first copy position
' t3 = second copy position (or 255)
' t4 = third copy position (or 255)
' r0 = last copy position
mov t3, #$FF
mov t4, #$FF
mov t5, #$FF
and r0, #%00_00_0_111
altgb r0, #i_tia_width
getbyte pb
altgb r0, #i_tia_setup
getbyte r0
jmp r0
t_resm0_update
getbyte r0, _P0, #2 ' NUSIZ (D2-D0)
mov pa, tia_resm0
t_resm0_update2
call #\t_setup_position
sets _m0_res+0, t2
sets _m0_res+1, t3
_ret_ sets _m0_res+2, t4
t_resm1_update
getbyte r0, _P1, #2 ' NUSIZ (D2-D0)
mov pa, tia_resm1
t_resm1_update2
call #\t_setup_position
sets _m1_res+0, t2
sets _m1_res+1, t3
_ret_ sets _m1_res+2, t4
t_res call #\get_resx ' a b c d e
t_res_p0o add r0, #5 ' a | | | |
t_res_p1o add r0, #5 ' | b | | |
add r0, #4 ' | | c d e
cmpsub r0, #228 wc ' a b c d e
if_c add r0, #68 ' a b c d e
mov tia_resp0, r0 ' a | | | |
jmp #t_update_p0 ' a | | | | RESP0: %00001100
mov tia_resp1, r0 ' b | | |
jmp #t_update_p1 ' b | | | RESP1: %0011001010
mov tia_resm0, r0 ' c | |
jmp #t_resm0_update ' c | | RESM0: %001111000110
mov tia_resm1, r0 ' d |
jmp #t_resm1_update ' d | RESM1: %00111111000110
_ret_ setbyte _bl_res, r0, #0 ' e RESBL: %011111111000110
i_jmp_imm call #\_fetch2
_ret_ getword _PC, t4, #0
i_jmp_ind call #\_fetch2
getword ea, t4, #0
call #\_read
getbyte _PC, t1, #0
incmod ea, i_ffff
call #\_read
_ret_ setbyte _PC, t1, #1
i_jsr call #\_fetch2 ' t4 = new PC
decmod _PC, i_ffff
getbyte t1, _PC, #1 ' PCH -> (S)
call #_push_t1
getbyte t1, _PC, #0 ' PCL -> (S)
call #_push_t1
_ret_ getword _PC, t4, #0
i_pha getbyte t1, _A, #0 ' a PHA: %010
i_php getbyte t1, _P, #0 ' | b PHP: %00
jmp #_push_t1 ' a b
i_pop call #\_pop_t1 ' a b PLA: %01100
getbyte _A, t1, #0 ' a | PLP: %010010
getbyte _P, t1, #0 ' | b
or _P, #%00110000 ' | b
jmp #_flags ' a |
ret ' b
i_sec _ret_ bith _P, #REGP_C
i_sed _ret_ bith _P, #REGP_D
i_sei _ret_ bith _P, #REGP_I
i_clc _ret_ bitl _P, #REGP_C
i_cld _ret_ bitl _P, #REGP_D
i_cli _ret_ bitl _P, #REGP_I
i_clv _ret_ bitl _P, #REGP_V
i_jam
i_halt jmp #\@@i_trap
'
'
' Parameters
'
ram_addr long 0
rom_addr long 0
ppa_addr long 0
aud_addr long 0
tia_fbp long 0
tia_fb0 long 0
tia_fb1 long 0
tia_inptp long 0
'
'
' CPU Registers
'
_A long $00 ' 8-bit working registers
_X long $00
_Y long $00
_S long $FF ' 8-bit stack pointer
_P long %00110000 ' 8-bit flag register
'|||||||+---- 0 = C - carry
'||||||+----- 1 = Z - zero
'|||||+------ 2 = I - interrupt
'||||+------- 3 = D - decimal
'|||+-------- 4 = B - break
'||+--------- 5 = 1
'|+---------- 6 = V - overflow
'+----------- 7 = N - negative
_PC long $FFFC ' 16-bit program counter
_I long %000000000000000000000000_00000000
' |||||||||||||||||||||||| ++++++++-- instruction cycles
' ++++++++++++++++++++++++----------- instruction time
_PF long %00000000_00000000_00000000_00000000
' || ||| |||||||| |||||||| ++++------ (N1) PF0
' || ||| |||||||| ++++++++----------- (B1) PF1
' || ||| ++++++++-------------------- (B2) PF2
' || ||+----------------------------- (#24) CTRLPF REF (D0)
' || |+------------------------------ (#25) CTRLPF SCORE (D1)
' || +------------------------------- (#26) CTRLPF PFP (D2)
' ++--------------------------------- (N7) CTRLPF BALL SIZE (D5-4) 00=1 01=2 10=4 11=8 (#29,#28)
_P0 long %00000000_00000000_00000000_00000000
_P1 long %00000000_00000000_00000000_00000000
' || || ||| |||||||| ++++++++-- (B0) GRP: graphics player (D7-0)
' || || ||| ++++++++----------- (B1) GRPd
' || ++-+++-------------------- (B2) NUSIZ: number-size player-missle (D5-0)
' |+----------------------------- (#24) REFP: reflect player (D3)
' +------------------------------ (#25) VDELP: vertical delay player (D0)
_HM long %00000000_0000_0000_0000_0000_0000_0000
' |||| |||| |||| |||| ++++-- (N0) HMP0
' |||| |||| |||| ++++------- (N1) HMP1
' |||| |||| ++++------------ (N2) HMM0
' |||| ++++----------------- (N3) HMM1
' ++++---------------------- (N4) HMBL
_MB long %00000000_00000000_00000000_00000000
' ||||||| |||||||| |||||||| ++++++++-- (B0) RESM0
' ||||||| |||||||| ++++++++----------- (B1) RESM1
' ||||||| ++++++++-------------------- (B2) RESBL
' ||||||+----------------------------- (#24) ENAM0 (D1)
' |||||+------------------------------ (#25) ENAM1 (D1)
' ||||+------------------------------- (#26) ENABL (D1)
' |||+-------------------------------- (#27) RESMP0 (D1)
' ||+--------------------------------- (#28) RESMP1 (D1)
' |+---------------------------------- (#29) VDELBL (D0)
' +----------------------------------- (#30) ENABLd
_HC long 0 ' horizontal counter
_VC long 0 ' vertical (scanline) counter
_COLL long %00_00_00_00_00_00_00_00
' || || || || || || || ++ D7=(P0,P1); D6=(M0,M1)
' || || || || || || ++--- D7=(BL,PF); D6=(unused)
' || || || || || ++------ D7=(M1,PF); D6=(M1,BL)
' || || || || ++--------- D7=(M0,PF); D6=(M0,BL)
' || || || ++------------ D7=(P1,PF); D6=(P1,BL)
' || || ++--------------- D7=(P0,PF); D6=(P0,BL)
' || ++------------------ D7=(M1,P0); D6=(M1,P1)
' ++--------------------- D7=(M0,P1); D6=(M0,P0)
_COLL_TBL
' ++--------------------- D7=(M0,P1); D6=(M0,P0)
' || ++------------------ D7=(M1,P0); D6=(M1,P1)
' || || ++--------------- D7=(P0,PF); D6=(P0,BL)
' || || || ++------------ D7=(P1,PF); D6=(P1,BL)
' || || || || ++--------- D7=(M0,PF); D6=(M0,BL)