-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcvbasic_prologue.asm
More file actions
4483 lines (4290 loc) · 69.5 KB
/
cvbasic_prologue.asm
File metadata and controls
4483 lines (4290 loc) · 69.5 KB
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
;
; CVBasic prologue (BASIC compiler for Colecovision and other consoles)
;
; by Oscar Toledo G.
; https://nanochess.org/
;
; Creation date: Feb/27/2024.
; Revision date: Feb/29/2024. Turns off sound. Now it reads the controllers.
; Added 16-bit multiply, division, modulo, and abs.
; Added random generator. Added sound routines.
; Revision date: Mar/03/2024. Removed fname directive to use gasm80.
; Revision date: Mar/05/2024. Added support for Sega SG1000.
; Revision date: Mar/06/2024. Added ENASCR, DISSCR, and CPYBLK.
; Revision date: Mar/08/2024. Added modes 0, 1 and 2.
; Revision date: Mar/12/2024. Added support for MSX.
; Revision date: Mar/14/2024. Added _sgn16.
; Revision date: Mar/15/2024. Added upper 16k enable for MSX.
; Revision date: Apr/11/2024. Added support for formatting numbers. Added
; support for Super Game Module.
; Revision date: Apr/13/2024. Saved bytes in SG-1000 ROMs. Faster LDIRVM.
; Shorter mode setting subroutines.
; Revision date: Apr/26/2024. Interruption handler saves current bank.
; Revision date: Apr/27/2024. Music player now supports bank switching.
; Revision date: May/17/2024. Added delay for SG1000 and SC3000 controller
; support with keyboard (code by SiRioKD)
; Revision date: Jun/04/2024. SGM supported deleted NTSC flag.
; Revision date: Jun/07/2024. Keys 0-9, = and - emulate keypad in MSX.
; Revision date: Jun/17/2024. Added SVI-328 support.
; Revision date: Aug/01/2024. Added Sord M5 support.
; Revision date: Aug/02/2024. PSG label now defined by CVBasic. Added Memotech
; support.
; Revision date: Aug/08/2024. Added Soundic/Hanimex Pencil II support.
; Revision date: Aug/15/2024. Added support for Tatung Einstein. Added support
; for Casio PV-2000.
; Revision date: Aug/21/2024. Added keypad support for Memotech, Tatung Einstein,
; and Casio PV-2000.
; Revision date: Aug/30/2024. Changed mode bit to bit 3 (avoids collision
; with flicker flag).
; Revision date: Oct/15/2024. Added LDIRMV. Solved bug where asterisk and number
; keys values were inverted.
; Revision date: Nov/12/2024. Saves the VDP status.
; Revision date: Feb/05/2026. Added support for spinners and roller controller
; (Colecovision).
; Revision date: Feb/09/2026. Added support for palette in MSX2.
; Revision date: Feb/10/2026. Added support for sprites in MSX2 and 64K VRAM
; access.
; Revision date: Feb/26/2026. Faster CPYBLK, LDIRVM3, and FILVRM.
; Revision date: Mar/09/2026. Corrections on joystick read for MSX.
;
;
; Resources:
; * Z80 instructions timing: https://map.grauw.nl/resources/z80instr.php
; * Use the Timing Z80+M1 for MSX and Colecovision (both insert one
; wait state in each M1 cycle)
;
; Technical requirements:
; * Minimum of 28 cycles between VDP writes for 3.58 mhz. systems.
; * Minimum of 31 cycles between VDP writes for 4.00 mhz. systems.
; * For SMS minimum of 28 cycles between VDP writes (3.58 mhz)
; * For MSX2 (V9938) minimum of 23 cycles between VDP writes (3.58 mhz)
;
; Dependances:
; * LDIRVM, assumed to keep DE unchanged (target VRAM address)
; * For speed. Required by CPYBLK and LDIRVM3.
;
if COLECO+SG1000+SMS
JOYSEL: equ $c0
KEYSEL: equ $80
JOY1: equ $fc-$20*(SG1000+SMS)
JOY2: equ $ff-$22*(SG1000+SMS)
endif
if COLECO
org $8000
if PENCIL
db "COPYRIGHT SOUNDIC"
jp START
jp nmi_handler
jp 0 ; rst $08
jp 0 ; rst $10
jp 0 ; rst $18
jp 0 ; rst $20
jp 0 ; rst $28
jp 0 ; rst $30
jp 0 ; rst $38
dw $0000
dw $0000
dw $0000
dw $0000
db "CVBASIC!POWERED BY!2024!"
else
db $55,$aa
dw 0
dw 0
dw 0
dw 0
dw START
jp 0 ; rst $08
jp 0 ; rst $10
jp 0 ; rst $18
jp 0 ; rst $20
jp 0 ; rst $28
jp 0 ; rst $30
if COLECO_SPINNER
jp spinner_handler
else
jp 0 ; rst $38
endif
jp nmi_handler
if COLECO_SPINNER
spinner_handler:
PUSH AF
PUSH HL
LD A,$FF
LD (spinner_data),A
OUT (JOYSEL),A
PUSH AF
POP AF
LD HL,spinner_data+1
IN A,(JOY1)
BIT 4,A
JR NZ,.1
BIT 5,A
JR NZ,$+4
INC (HL)
DB $3E
DEC (HL)
.1: INC HL
IN A,(JOY2)
BIT 4,A
JR NZ,.2
BIT 5,A
JR NZ,$+4
INC (HL)
DB $3E
DEC (HL)
.2: POP HL
POP AF
EI
RETI
endif
endif
endif
if SG1000+SMS+SVI
org $0000
di
ld sp,STACK
jp START
db $ff
jp 0
db $ff,$ff,$ff,$ff,$ff
jp 0
db $ff,$ff,$ff,$ff,$ff
jp 0
db $ff,$ff,$ff,$ff,$ff
jp 0
db $ff,$ff,$ff,$ff,$ff
jp 0
db $ff,$ff,$ff,$ff,$ff
jp 0
db $ff,$ff,$ff,$ff,$ff
jp nmi_handler ; It should be called int_handler.
endif
if MSX
ORG $4000
db "AB"
dw START
dw $0000
dw $0000
dw $0000
dw $0000
WRTPSG: equ $0093
RDPSG: equ $0096
endif
if SORD
ORG $2000
db $02 ; Avoid checking $4000
dw START ; Start address.
dw $002e ; Prestart address (just point to RET in BIOS).
endif
if PV2000
ORG $C000
jp START
endif
if MEMOTECH
if CPM
org $0100
else
org $40fc
dw rom_start
dw rom_end-rom_start
endif
rom_start:
jp START
db 0,0,0,0,0
dw nmi_handler
dw null_vector
dw null_vector
dw rom_start
null_vector:
ei
reti
endif
if EINSTEIN
org $0100
rom_start:
jp START
db 0,0,0,0,0
dw $0000
dw $0000
dw $0000
dw $0000
endif
if NABU
if CPM
org $0100
else
org $140d
endif
nop
nop
nop
jp START
times $100-($&255) db $ff
nabu_int:
dw null_vector
dw null_vector
dw keyboard_handler
dw nmi_handler
dw null_vector
dw null_vector
dw null_vector
dw null_vector
null_vector:
ei
reti
endif
if SVI
WRTPSG:
out ($88),a
push af
ld a,e
out ($8c),a
pop af
ret
RDPSG:
out ($88),a
push af
pop af
in a,($90)
ret
endif
if EINSTEIN
WRTPSG:
out ($02),a
push af
ld a,e
out ($03),a
pop af
ret
RDPSG:
out ($02),a
push af
pop af
in a,($02)
ret
endif
if NABU
WRTPSG:
out ($41),a
push af
ld a,e
out ($40),a
pop af
ret
RDPSG:
out ($41),a
push af
pop af
in a,($40)
ret
endif
if PV2000
; The Casio PV2000 has the VDP ports mapped into main memory (MREQ)
WRTVDP:
ld a,b
ld (VDP+1),a
ld a,c
or $80
ld (VDP+1),a
ret
SETWRT:
ld a,l
ld (VDP+1),a
ld a,h
or $40
ld (VDP+1),a
ret
SETRD:
ld a,l
ld (VDP+1),a
ld a,h
and $3f
ld (VDP+1),a
ret
WRTVRM:
push af
call SETWRT
pop af
ld (VDP),a
ret
RDVRM:
push af
call SETRD
pop af
ex (sp),hl
ex (sp),hl
ld a,(VDP)
ret
FILVRM:
push af
call SETWRT
pop af
dec bc ; T-states (normal / M1)
inc c
inc b
.1: ld (VDP),a ; 13 14
dec c ; 4 5
jr nz,.1 ; 12 13
; -- --
; 29 32
djnz .1
ret
LDIRMV:
ex de,hl
call SETRD
ex (sp),hl
ex (sp),hl
.1:
ld a,(VDP)
ld (de),a
inc de
dec bc
ld a,b
or c
jp nz,.1
ret
LDIRVM:
EX DE,HL
CALL SETWRT
EX DE,HL
DEC BC
INC C
LD A,B
LD B,C
INC A
LD C,A
.1:
LD A,(HL) ; 7 8
LD (VDP),A ; 13 14
INC HL ; 6 7
DJNZ .1 ; 13 14
DEC C ; 4 5
JP NZ,.1 ; 10 11
RET
else
; Normal platforms with VDP connected via ports (IORQ)
WRTVDP:
if COLECO_SPINNER
di
endif
ld a,b
out (VDP+1),a
ld a,c
or $80
out (VDP+1),a
if COLECO_SPINNER
ei
endif
ret
SETWRT:
if COLECO_SPINNER
di
endif
if MSX&2
ld a,h
rlca
rlca
and 3
out (VDP+1),a
ld a,$8e
out (VDP+1),a
ld a,l
out (VDP+1),a
ld a,h
and $3f
or $40
out (VDP+1),a
else
ld a,l
out (VDP+1),a
ld a,h
or $40
out (VDP+1),a
endif
if COLECO_SPINNER
ei
endif
ret
SETRD:
if COLECO_SPINNER
di
endif
if MSX&2
ld a,h
rlca
rlca
and 3
out (VDP+1),a
ld a,$8e
out (VDP+1),a
endif
ld a,l
out (VDP+1),a
ld a,h
and $3f
out (VDP+1),a
if COLECO_SPINNER
ei
endif
ret
WRTVRM:
push af
call SETWRT
pop af
out (VDP),a
ret
if SG1000+SMS
db $ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff
; Located at $0066
ei ; NMI handler (pause button)
retn
endif
RDVRM:
push af
call SETRD
pop af
ex (sp),hl
ex (sp),hl
in a,(VDPR)
ret
FILVRM:
push af
call SETWRT
pop af
dec bc ; T-states (normal / M1)
inc c
inc b
if MSX+COLECO
.1: out (VDP),a ; 12
dec c ; 5
jp nz,.1 ; 11
; --
; 28
else
.1: out (VDP),a ; 11 12
nop ; 4 5
dec c ; 4 5
jr nz,.1 ; 12 13
; -- --
; 31 35
endif
djnz .1
ret
LDIRMV:
ex de,hl
call SETRD
ex (sp),hl
ex (sp),hl
.1:
in a,(VDP)
ld (de),a
if SORD+SMS
nop
endif
if SG1000+MEMOTECH+EINSTEIN
nop ; SG1000 is 3.58 mhz, but SC3000 is 4 mhz.
nop
endif
inc de
dec bc
ld a,b
or c
jp nz,.1
ret
LDIRVM:
ex de,hl
call SETWRT
ex de,hl
dec bc
inc c
ld a,b
ld b,c
inc a
ld c,VDP
.1:
if MSX&2
otir ; 23 cycles per byte.
else
if SORD+SMS
nop
endif
if SG1000+MEMOTECH+EINSTEIN
nop ; SG1000 is 3.58 mhz, but SC3000 is 4 mhz.
nop
endif
outi ; 18
jp nz,.1 ; 11
endif
dec a
jp nz,.1
ret
endif
if SMS
else
LDIRVM3:
call .1
call .1
.1: push hl
push bc
call LDIRVM
pop bc
ld a,d
add a,8
ld d,a
pop hl
ret
endif
DISSCR:
call nmi_off
ld bc,$a201
call WRTVDP
jp nmi_on
ENASCR:
call nmi_off
ld bc,$e201
call WRTVDP
jp nmi_on
CPYBLK:
pop hl
ex af,af'
pop af
ld b,a
pop af
ld c,a
pop de
ex (sp),hl
call nmi_off
.1: push bc
push hl
ld b,0
call LDIRVM
ex de,hl
if SMS
ld bc,$0040
else
ld bc,$0020
endif
add hl,bc
ex de,hl
pop hl
ex af,af'
ld c,a
ld b,0
add hl,bc
ex af,af'
pop bc
djnz .1
jp nmi_on
nmi_off:
if COLECO+PV2000
push hl
ld hl,mode
set 0,(hl)
pop hl
endif
if SG1000+SMS+MSX+SVI+SORD+MEMOTECH+NABU
di
endif
ret
nmi_on:
if COLECO+PV2000
push af
push hl
ld hl,mode
res 0,(hl)
nop
bit 1,(hl)
jp nz,nmi_handler.0
pop hl
pop af
endif
if SG1000+SMS+MSX+SVI+SORD+MEMOTECH+NABU
ei
endif
ret
if COLECO
keypad_table:
db $0f,$08,$04,$05,$0c,$07,$0b,$02
db $0d,$0a,$00,$09,$03,$01,$06,$0f
endif
cls:
if SMS
ld hl,$3800
ld (cursor),hl
di
call SETWRT
.1: ld a,$20 ; 7
out (VDP),a ; 11
inc hl ; 6
inc hl ; 6
ld a,$00 ; 7
out (VDP),a ; 11
ld a,h ; 4
cp $3e ; 7
jp nz,.1 ; 10
ei
ret
else
ld hl,$1800
ld (cursor),hl
ld bc,$0300
ld a,$20
call nmi_off
call FILVRM
jp nmi_on
endif
print_string:
ld c,a
ld b,0
ld de,(cursor)
if SMS
ld a,d
and $07
or $38
ld d,a
else
ld a,d
and $07
or $18
ld d,a
endif
push de
push bc
call nmi_off
if SMS
ex de,hl
.1: ld a,(de)
call WRTVRM
inc de
inc hl
xor a
call WRTVRM
inc hl
dec bc
ld a,b
or c
jp nz,.1
else
call LDIRVM
endif
call nmi_on
pop bc
pop hl
add hl,bc
if SMS
add hl,bc
endif
ld (cursor),hl
ret
print_number:
ld b,0
call nmi_off
print_number5:
ld de,10000
call print_digit
print_number4:
ld de,1000
call print_digit
print_number3:
ld de,100
call print_digit
print_number2:
ld de,10
call print_digit
print_number1:
ld de,1
ld b,e
call print_digit
jp nmi_on
print_digit:
ld a,$2f
or a
.2: inc a
sbc hl,de
jp nc,.2
add hl,de
cp $30
jr nz,.3
ld a,b
or a
ret z
dec a
jr z,.4
ld a,c
jr print_char
.4:
ld a,$30
.3: ld b,1
print_char:
push hl
ld hl,(cursor)
ex af,af'
ld a,h
and $07
if SMS
or $38
else
or $18
endif
ld h,a
ex af,af'
call WRTVRM
inc hl
if SMS
xor a
call WRTVRM
inc hl
endif
ld (cursor),hl
pop hl
ret
define_char:
ex de,hl
ld l,a
ld h,0
add hl,hl ; x2
add hl,hl ; x4
add hl,hl ; x8
if SMS
add hl,hl ; x16
add hl,hl ; x32
endif
ld c,l
ld b,h
pop af
pop hl
push af
add hl,hl ; x2
add hl,hl ; x4
add hl,hl ; x8
if SMS
add hl,hl ; x16
add hl,hl ; x32
endif
ex de,hl
if SMS
di
call LDIRVM
ei
ret
else
call nmi_off
ld a,(mode)
and 8
jr nz,.1
call LDIRVM3
jp nmi_on
.1: call LDIRVM
jp nmi_on
endif
define_color:
if SMS
ret
else
ex de,hl
ld l,a
ld h,0
add hl,hl ; x2
add hl,hl ; x4
add hl,hl ; x8
ld c,l
ld b,h
pop af
pop hl
push af
add hl,hl ; x2
add hl,hl ; x4
add hl,hl ; x8
ex de,hl
set 5,d
call nmi_off
call LDIRVM3
jp nmi_on
endif
define_sprite:
ex de,hl
ld l,a
ld h,0
add hl,hl ; x2
add hl,hl ; x4
add hl,hl ; x8
add hl,hl ; x16
add hl,hl ; x32
if SMS
add hl,hl ; x64
endif
ld c,l
ld b,h
pop af
pop hl
push af
add hl,hl ; x2
add hl,hl ; x4
if SMS
set 1,h
else
ld h,$07
endif
add hl,hl ; x8
add hl,hl ; x16
add hl,hl ; x32
if SMS
add hl,hl ; x64
endif
ex de,hl
call nmi_off
call LDIRVM
jp nmi_on
endif
update_sprite:
if SMS
pop bc ; Pop return address.
pop de ; 3th. argument in D (Y-coordinate)
ld e,a ; 4th. argument in E (frame)
pop af ; 2nd. argument in A (X-coordinate)
pop hl ; 1st. argument in H (sprite number)
push bc ; Push return address.
ld l,h
res 7,l
res 6,l
ld h,sprites>>8
ld (hl),a
sla l
set 7,l
ld (hl),d
inc l
ld (hl),e
else
pop bc
ld (sprite_data+3),a
pop af
ld (sprite_data+2),a
pop af
ld (sprite_data+1),a
pop af
ld (sprite_data),a
pop af
; A = Sprite number
push bc
ld d,sprites>>8
add a,a
add a,a
if SORD
or $80
endif
ld e,a
ld hl,sprite_data
ldi
ldi
ldi
ldi
endif
ret
;
; Fast 16-bit multiplication by Oscar Toledo G.
; Input:
; HL = Operand 1.
; DE = Operand 2.
; Output:
; HL = HL x DE.
;
; This is a trick I've discovered.
; 1. The smallest operand goes into DE.
; The biggest operand goes into BC.
; HL becomes zero.
; 2. If D is non-zero, calculate H = D x C with 8-bit arithmetic.
; B can be ignored as this routine doesn't generate a 24-bit result.
; 3. Calculate HL = HL + E x BC with early exit as it shifts E to the right.
;
_mul16:
or a ; 5
sbc hl,de ; 17
add hl,de ; 12
jr nc,$+3 ; 13/8
ex de,hl ; 5 Smallest operand in DE.
ld b,h ; 5
ld c,l ; 5
ld hl,0 ; 11
ld a,d
or a ; High-byte is zero?
jp z,.2 ; Yes, jump.
xor a ; 5
sla d ; 10
jp nc,$+4 ; 11
add a,c ; 5
add a,a ; 5
sla d ; 10
jp nc,$+4 ; 11
add a,c ; 5
add a,a ; 5
sla d ; 10
jp nc,$+4 ; 11
add a,c ; 5
add a,a ; 5
sla d ; 10
jp nc,$+4 ; 11
add a,c ; 5
add a,a ; 5
sla d ; 10
jp nc,$+4 ; 11
add a,c ; 5
add a,a ; 5
sla d ; 10
jp nc,$+4 ; 11
add a,c ; 5
add a,a ; 5
sla d ; 10
jp nc,$+4 ; 11
add a,c ; 5
add a,a ; 5
sla d ; 10
jp nc,$+4 ; 11
add a,c ; 5
ld h,a ; 5
.2: ;
.3: srl e ; 10
jp nc,$+4 ; 11