-
Notifications
You must be signed in to change notification settings - Fork 5
/
basic.asm
executable file
·4509 lines (4213 loc) · 170 KB
/
basic.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
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
include "lib/header.asm"
include "lib/strap.asm"
org 02071h
seek 00871h
_splash_screen_data:
DB 0ffh
DB 003h, 00Dh, _scrattr_ascii_n
DB "BASIC-80", 000h
DB 006h, 009h, _scrattr_ascii_n
DB "for the HP 4952", 000h
DB 007h, 008h, _scrattr_ascii_n
DB "Protocol Analyzer", 000h
DB 00ah, 008h, _scrattr_ascii_n, 0abh
DB " Copyright 1978", 000h
DB 00bh, 00ch, _scrattr_ascii_n
DB "Microsoft", 000h
DB 000h ;; End of Screen Data
_splash_menu_data:
DB "Re-!BERT!Remote!Mass !Run !Self~"
DB "set!Menu!&Print!Store!Lang!Test|"
_p_main_menu_page_one:
DW 08336h ;; First Page Menu Data
_p_mm_autoconfig:
DW 0141ch ;; Ordinal 120h Auto Config
_p_mm_setup:
DW 0b5a8h ;; Entry Point for Setup
_p_mm_mon:
DW 0100dh ;; Entry Point for Monitor Menu
_p_mm_sim:
DW 01013h ;; Entry Point for Sim Menu
_p_mm_run:
DW 0b9ffh ;; Entry Point for Run Menu
_p_mm_exam:
DW 013cdh ;; Ordinal 12eh Examine Data
_p_mm_next1:
DW _p_main_menu_page_two ;; Next Page
_p_main_menu_page_two:
DW _splash_menu_data ;; Second Page Menu Data
_p_mm_reset:
DW 0bb1ah ;; Entry Point for Re-Set
_p_mm_bert:
DW 0b22ch ;; Entry Point for BERT Menu
_p_mm_remote:
DW 0d963h ;; Entry Point for Remote & Print
_p_mm_masstorage:
DW 00f0ch ;; Entry Point for Mass Storage
_p_mm_launch_app:
DW _launch_app ;; Entry Point for Application
_p_mm_selftest:
DW 0136fh ;; Ordinal 12ah Self Test
_p_mm_next2:
DW _p_main_menu_page_one ;; Next Page
_launch_app:
ld a, 006h
call 00e60h ; Page in 6 - Really 2 (Application "ROM" in RAM)
ld hl,0aa00H ; Copy application to Work RAM
ld de,_code_start ;
ld bc,_code_end-_code_start ;
ldir ;
jp _app_main ; Run the application
DW _code_end
;; End of menu section
_splash_end:
;; Main Application
;; org 2160h
seek 0a00h
_code_start:
_app_main:
call _clear_screen
ld a, 001h
ld (_cursor_enabled), a
_main_loop:
di
ld a,00eh ; access desired memory bank
out (020h),a ;
ld SP, 0f000h
jp COLD
_return_to_rom:
ld a, 000h
ld (_cursor_enabled), a
call _clear_screen
ld hl,_splash_screen_data ;
push hl ;
call 01cf8h ; Patched to 2d50 -> 01cf8h
jp 014d5h ; Return to main menu.
include "lib/screen.asm"
include "lib/keyb.asm"
;==================================================================================
; The updates to the original BASIC within this file are copyright Grant Searle
;
; You have permission to use this for NON COMMERCIAL USE ONLY
; If you wish to use it elsewhere, please include an acknowledgement to myself.
;
; http://searle.hostei.com/grant/index.html
;
; eMail: home.micros01@btinternet.com
;
; If the above don't work, please perform an Internet search to see if I have
; updated the web page hosting service.
;
;==================================================================================
; NASCOM ROM BASIC Ver 4.7, (C) 1978 Microsoft
; Scanned from source published in 80-BUS NEWS from Vol 2, Issue 3
; (May-June 1983) to Vol 3, Issue 3 (May-June 1984)
; Adapted for the freeware Zilog Macro Assembler 2.10 to produce
; the original ROM code (checksum A934H). PA
; GENERAL EQUATES
CTRLC: equ 03H ; Control "C"
CTRLG: equ 07H ; Control "G"
BKSP: equ 08H ; Back space
LF: equ 0AH ; Line feed
CS: equ 0CH ; Clear screen
CR: equ 0DH ; Carriage return
CTRLO: equ 0FH ; Control "O"
CTRLQ: equ 11H ; Control "Q"
CTRLR: equ 12H ; Control "R"
CTRLS: equ 13H ; Control "S"
CTRLU: equ 15H ; Control "U"
CTRLX: equ 18H ; Control "X"
ESC: equ 1BH ; Escape
DEL: equ 7FH ; Delete
; BASIC WORK SPACE LOCATIONS
BASICSTARTED: equ 08000h
WRKSPC: equ 08001H ; BASIC Work space
USR: equ WRKSPC+3H ; "USR (x)" jump
OUTSUB: equ WRKSPC+6H ; "OUT p,n"
OTPORT: equ WRKSPC+7H ; PORt (p)
DIVSUP: equ WRKSPC+9H ; Division support routine
DIV1: equ WRKSPC+0AH ; <- Values
DIV2: equ WRKSPC+0EH ; <- to
DIV3: equ WRKSPC+12H ; <- be
DIV4: equ WRKSPC+15H ; <-inserted
SEED: equ WRKSPC+17H ; Random number seed
LSTRND: equ WRKSPC+3AH ; Last random number
INPSUB: equ WRKSPC+3EH ; #INP (x)" Routine
INPORT: equ WRKSPC+3FH ; PORT (x)
NULLS: equ WRKSPC+41H ; Number of nulls
LWIDTH: equ WRKSPC+42H ; Terminal width
COMMAN: equ WRKSPC+43H ; Width for commas
NULFLG: equ WRKSPC+44H ; Null after input DB flag
CTLOFG: equ WRKSPC+45H ; Control "O" flag
LINESC: equ WRKSPC+46H ; Lines counter
LINESN: equ WRKSPC+48H ; Lines number
CHKSUM: equ WRKSPC+4AH ; Array load/save check sum
NMIFLG: equ WRKSPC+4CH ; Flag for NMI break routine
BRKFLG: equ WRKSPC+4DH ; Break flag
RINPUT: equ WRKSPC+4EH ; Input reflection
DCATLG: equ WRKSPC+51H ; Disk Catalog reflection
DLOAD: equ WRKSPC+54H ; Disk Load reflection
DSAVE: equ WRKSPC+57H ; Disk Save reflection
STRSPC: equ WRKSPC+5AH ; Bottom of string space
LINEAT: equ WRKSPC+5CH ; Current line number
BASTXT: equ WRKSPC+5EH ; Pointer to start of program
BUFFER: equ WRKSPC+61H ; Input buffer
STACK: equ WRKSPC+66H ; Initial stack
CURPOS: equ WRKSPC+0ABH ; Character position on line
LCRFLG: equ WRKSPC+0ACH ; Locate/Create flag
TYPE: equ WRKSPC+0ADH ; Data type flag
DATFLG: equ WRKSPC+0AEH ; Literal statement flag
LSTRAM: equ WRKSPC+0AFH ; Last available RAM
TMSTPT: equ WRKSPC+0B1H ; Temporary string pointer
TMSTPL: equ WRKSPC+0B3H ; Temporary string pool
TMPSTR: equ WRKSPC+0BFH ; Temporary string
STRBOT: equ WRKSPC+0C3H ; Bottom of string space
CUROPR: equ WRKSPC+0C5H ; Current operator in EVAL
LOOPST: equ WRKSPC+0C7H ; First statement of loop
DATLIN: equ WRKSPC+0C9H ; Line of current DATA item
FORFLG: equ WRKSPC+0CBH ; "FOR" loop flag
LSTBIN: equ WRKSPC+0CCH ; Last DB entered
READFG: equ WRKSPC+0CDH ; Read/Input flag
BRKLIN: equ WRKSPC+0CEH ; Line of break
NXTOPR: equ WRKSPC+0D0H ; Next operator in EVAL
ERRLIN: equ WRKSPC+0D2H ; Line of error
CONTAD: equ WRKSPC+0D4H ; Where to CONTinue
PROGND: equ WRKSPC+0D6H ; End of program
VAREND: equ WRKSPC+0D8H ; End of variables
ARREND: equ WRKSPC+0DAH ; End of arrays
NXTDAT: equ WRKSPC+0DCH ; Next data item
FNRGNM: equ WRKSPC+0DEH ; Name of FN argument
FNARG: equ WRKSPC+0E0H ; FN argument value
FPREG: equ WRKSPC+0E4H ; Floating point register
FPEXP: equ FPREG+3 ; Floating point exponent
SGNRES: equ WRKSPC+0E8H ; Sign of result
PBUFF: equ WRKSPC+0E9H ; Number print buffer
MULVAL: equ WRKSPC+0F6H ; Multiplier
PROGST: equ WRKSPC+0F9H ; Start of program text area
STLOOK: equ WRKSPC+15DH ; Start of memory test
; BASIC ERROR CODE VALUES
NF: equ 00H ; NEXT without FOR
SN: equ 02H ; Syntax error
RG: equ 04H ; RETURN without GOSUB
OD: equ 06H ; Out of DATA
FC: equ 08H ; Function call error
OV: equ 0AH ; Overflow
OM: equ 0CH ; Out of memory
UL: equ 0EH ; Undefined line number
BS: equ 10H ; Bad subscript
DD: equ 12H ; Re-DIMensioned array
DZ: equ 14H ; Division by zero (/0)
ID: equ 16H ; Illegal direct
TM: equ 18H ; Type miss-match
OS: equ 1AH ; Out of string space
LS: equ 1CH ; String too long
ST: equ 1EH ; String formula too complex
CN: equ 20H ; Can't CONTinue
UF: equ 22H ; UnDEFined FN function
MO: equ 24H ; Missing operand
HX: equ 26H ; HEX error
BN: equ 28H ; BIN error
COLD: jp STARTA ; Jump for cold start
WARM: jp WARMST ; Jump for warm start
;STARTA:
STARTB:
ld IX,0 ; Flag cold start
jp CSTART ; Jump to initialise
DW DEINT ; Get integer -32768 to 32767
DW ABPASS ; Return integer in AB
STARTA:
ld A,(BASICSTARTED); Check the BASIC STARTED flag
cp 'Y' ; to see if this is power-up
jr Z, WARM
; jr NZ,COLDSTART ; If not BASIC started then always do cold start
;CORW:
; ld HL,CLDMSG ; Cold/warm message
; call PRS ; Output string if extra given
; call PROMPT ; Get input with '?'
; call GETCHR ; Get next character
; AND %11011111 ; lower to uppercase
; cp 'C'
; jr Z, COLDSTART
; cp 'W'
; jr Z, WARM
; jr CORW
;COLDSTART:
ld A,'Y' ; Set the BASIC STARTED flag
ld (BASICSTARTED),A
jr STARTB ; Start BASIC COLD
;CHECKWARM:
INIT: ld DE,INITAB ; Initialise workspace
ld B,INITBE-INITAB+3; DBs to copy
ld HL,WRKSPC ; Into workspace RAM
COPY: ld A,(DE) ; Get source
ld (HL),A ; To destination
inc HL ; Next destination
inc DE ; Next source
dec B ; Count DBs
jr NZ,COPY ; More to move
ld SP,HL ; Temporary stack
call CLREG ; Clear registers and stack
call PRNTCRLF ; Output CRLF
ld (BUFFER+72+1),A ; Mark end of buffer
ld (PROGST),A ; Initialise program area
ld HL,00000h ; Try to use all RAM between
jr SETTOP ; Bootloader & Basic (52KB or so)
MSIZE: ld HL,MEMMSG ; Point to message
call PRS ; Output "Memory size"
call PROMPT ; Get input with '?'
call GETCHR ; Get next character
or A ; Set flags
jr NZ,TSTMEM ; If number - Test if RAM there
ld HL,STLOOK ; Point to start of RAM
MLOOP: inc HL ; Next DB
ld A,H ; Above address FFFF ?
or L
jr Z,SETTOP ; Yes - 64K RAM
ld A,(HL) ; Get contents
ld B,A ; Save it
cpl ; Flip all bits
ld (HL),A ; Put it back
cp (HL) ; RAM there if same
ld (HL),B ; Restore old contents
jr Z,MLOOP ; If RAM - test next DB
jr SETTOP ; Top of RAM found
TSTMEM: call ATOH ; Get high memory into DE
or A ; Set flags on last DB
jp NZ,SNERR ; ?SN Error if bad character
ex DE,HL ; Address into HL
dec HL ; Back one DB
ld A,11011001B ; Test DB
ld B,(HL) ; Get old contents
ld (HL),A ; Load test DB
cp (HL) ; RAM there if same
ld (HL),B ; Restore old contents
jr NZ,MSIZE ; Ask again if no RAM
SETTOP: dec HL ; Back one DB
ld DE,STLOOK-1 ; See if enough RAM
call CPDEHL ; Compare DE with HL
jp C,MSIZE ; Ask again if not enough RAM
ld DE,0-50 ; 50 DBs string space
ld (LSTRAM),HL ; Save last available RAM
add HL,DE ; Allocate string space
ld (STRSPC),HL ; Save string space
call CLRPTR ; Clear program area
ld HL,(STRSPC) ; Get end of memory
ld DE,0-17 ; Offset for free DBs
add HL,DE ; Adjust HL
ld DE,PROGST ; Start of program text
ld A,L ; Get LSB
sub E ; Adjust it
ld L,A ; Re-save
ld A,H ; Get MSB
sbc A,D ; Adjust it
ld H,A ; Re-save
push HL ; Save bytes free
ld HL,SIGNON ; Sign-on message
call PRS ; Output string
pop HL ; Get bytes free back
call PRNTHL ; Output amount of free memory
ld HL,BFREE ; " bytes free" message
call PRS ; Output string
WARMST: ld SP,STACK ; Temporary stack
BRKRET: call CLREG ; Clear registers and stack
jp PRNTOK ; Go to get command line
BFREE: DB " bytes free",CR,LF,0,0
SIGNON: DB "Z80 BASIC Ver 4.7b",CR,LF
DB "Copyright ",0abh
DB " 1978 by Microsoft",CR,LF,0,0
MEMMSG: DB "Memory top",0
;CLDMSG: DB "Cold or warm start (C or W)",0
; FUNCTION addRESS TABLE
FNCTAB: DW SGN
DW INT
DW ABS
DW USR
DW FRE
DW INP
DW POS
DW SQR
DW RND
DW LOG
DW EXP
DW COS
DW SIN
DW TAN
DW ATN
DW PEEK
DW DEEK
DW POINT
DW LEN
DW STR
DW VAL
DW ASC
DW CHR
DW HEX
DW BIN
DW LEFT
DW RIGHT
DW MID
; RESERVED DW LIST
DWS: DB ('E'+80H),"ND"
DB ('F'+80H),"OR"
DB ('N'+80H),"EXT"
DB ('D'+80H),"ATA"
DB ('I'+80H),"NPUT"
DB ('D'+80H),"IM"
DB ('R'+80H),"EAD"
DB ('L'+80H),"ET"
DB ('G'+80H),"OTO"
DB ('R'+80H),"UN"
DB ('I'+80H),"F"
DB ('R'+80H),"ESTORE"
DB ('G'+80H),"OSUB"
DB ('R'+80H),"ETURN"
DB ('R'+80H),"EM"
DB ('S'+80H),"TOP"
DB ('O'+80H),"UT"
DB ('O'+80H),"N"
DB ('N'+80H),"ULL"
DB ('W'+80H),"AIT"
DB ('D'+80H),"EF"
DB ('P'+80H),"OKE"
DB ('D'+80H),"OKE"
DB ('S'+80H),"CREEN"
DB ('L'+80H),"INES"
DB ('C'+80H),"LS"
DB ('W'+80H),"IDTH"
DB ('C'+80H),"ALL"
DB ('C'+80H),"AT"
DB ('R'+80H),"ESET"
DB ('P'+80H),"RINT"
DB ('C'+80H),"ONT"
DB ('L'+80H),"IST"
DB ('C'+80H),"LEAR"
DB ('D'+80H),"LOAD"
DB ('D'+80H),"SAVE"
DB ('N'+80H),"EW"
DB ('T'+80H),"AB("
DB ('T'+80H),"O"
DB ('F'+80H),"N"
DB ('S'+80H),"PC("
DB ('T'+80H),"HEN"
DB ('N'+80H),"OT"
DB ('S'+80H),"TEP"
DB ('+'+80H)
DB ('-'+80H)
DB ('*'+80H)
DB ('/'+80H)
DB ('^'+80H)
DB ('A'+80H),"ND"
DB ('O'+80H),"R"
DB ('>'+80H)
DB ('='+80H)
DB ('<'+80H)
DB ('S'+80H),"GN"
DB ('I'+80H),"NT"
DB ('A'+80H),"BS"
DB ('U'+80H),"SR"
DB ('F'+80H),"RE"
DB ('I'+80H),"NP"
DB ('P'+80H),"OS"
DB ('S'+80H),"QR"
DB ('R'+80H),"ND"
DB ('L'+80H),"OG"
DB ('E'+80H),"XP"
DB ('C'+80H),"OS"
DB ('S'+80H),"IN"
DB ('T'+80H),"AN"
DB ('A'+80H),"TN"
DB ('P'+80H),"EEK"
DB ('D'+80H),"EEK"
DB ('P'+80H),"OINT"
DB ('L'+80H),"EN"
DB ('S'+80H),"TR$"
DB ('V'+80H),"AL"
DB ('A'+80H),"SC"
DB ('C'+80H),"HR$"
DB ('H'+80H),"ex$"
DB ('B'+80H),"IN$"
DB ('L'+80H),"EFT$"
DB ('R'+80H),"IGHT$"
DB ('M'+80H),"ID$"
DB 80H ; End of list marker
; KEYDW addRESS TABLE
DWTB: DW PEND
DW FOR
DW NEXT
DW DATA
DW INPUT
DW DIM
DW READ
DW LET
DW GOTO
DW RUN
DW IF
DW RESTOR
DW GOSUB
DW RETURN
DW REM
DW STOP
DW POUT
DW ON
DW NULL
DW WAIT
DW DEF
DW POKE
DW DOKE
DW REM
DW LINES
DW CLS
DW WIDTH
DW C_A_L_L
DW DCATLG
DW MONITR
DW PRINT
DW CONT
DW LIST
DW CLEAR
DW DLOAD
DW DSAVE
DW NEW
; RESERVED DW TOKEN VALUES
ZEND: equ 080H ; END
ZFOR: equ 081H ; FOR
ZDATA: equ 083H ; DATA
ZGOTO: equ 088H ; GOTO
ZGOSUB: equ 08CH ; GOSUB
ZREM: equ 08EH ; REM
ZPRINT: equ 09EH ; PRINT
ZNEW: equ 0A4H ; NEW
ZTAB: equ 0A5H ; TAB
ZTO: equ 0A6H ; TO
ZFN: equ 0A7H ; FN
ZSPC: equ 0A8H ; SPC
ZTHEN: equ 0A9H ; THEN
ZNOT: equ 0AAH ; NOT
ZSTEP: equ 0ABH ; STEP
ZPLUS: equ 0ACH ; +
ZMINUS: equ 0ADH ; -
ZTIMES: equ 0AEH ; *
ZDIV: equ 0AFH ; /
ZOR: equ 0B2H ; OR
ZGTR: equ 0B3H ; >
ZEQUAL: equ 0B4H ; M
ZLTH: equ 0B5H ; <
ZSGN: equ 0B6H ; SGN
ZPOINT: equ 0C7H ; POINT
ZLEFT: equ 0CDH +2 ; LEFT$
; ARITHMETIC PRECEDENCE TABLE
PRITAB: DB 79H ; Precedence value
DW PADD ; FPREG = <last> + FPREG
DB 79H ; Precedence value
DW PSUB ; FPREG = <last> - FPREG
DB 7CH ; Precedence value
DW MULT ; PPREG = <last> * FPREG
DB 7CH ; Precedence value
DW DIV ; FPREG = <last> / FPREG
DB 7FH ; Precedence value
DW POWER ; FPREG = <last> ^ FPREG
DB 50H ; Precedence value
DW PAND ; FPREG = <last> and FPREG
DB 46H ; Precedence value
DW POR ; FPREG = <last> or FPREG
; BASIC ERROR CODE LIST
ERRORS: DB "NF" ; NEXT without FOR
DB "SN" ; Syntax error
DB "RG" ; RETURN without GOSUB
DB "OD" ; Out of DATA
DB "FC" ; Illegal function call
DB "OV" ; Overflow error
DB "OM" ; Out of memory
DB "UL" ; Undefined line
DB "BS" ; Bad subscript
DB "DD" ; Re-DIMensioned array
DB "/0" ; Division by zero
DB "ID" ; Illegal direct
DB "TM" ; Type mis-match
DB "OS" ; Out of string space
DB "LS" ; String too long
DB "ST" ; String formula too complex
DB "CN" ; Can't CONTinue
DB "UF" ; Undefined FN function
DB "MO" ; Missing operand
DB "HX" ; HEX error
DB "BN" ; BIN error
; INITIALISATION TABLE -------------------------------------------------------
INITAB: jp WARMST ; Warm start jump
jp FCERR ; "USR (X)" jump (Set to Error)
OUT (0),A ; "OUT p,n" skeleton
ret
sub 0 ; Division support routine
ld L,A
ld A,H
sbc A,0
ld H,A
ld A,B
sbc A,0
ld B,A
ld A,0
ret
DB 0,0,0 ; Random number seed table used by RND
DB 035H,04AH,0CAH,099H ;-2.65145E+07
DB 039H,01CH,076H,098H ; 1.61291E+07
DB 022H,095H,0B3H,098H ;-1.17691E+07
DB 00AH,0DDH,047H,098H ; 1.30983E+07
DB 053H,0D1H,099H,099H ;-2-01612E+07
DB 00AH,01AH,09FH,098H ;-1.04269E+07
DB 065H,0BCH,0CDH,098H ;-1.34831E+07
DB 0D6H,077H,03EH,098H ; 1.24825E+07
DB 052H,0C7H,04FH,080H ; Last random number
IN A,(0) ; INP (x) skeleton
ret
DB 1 ; POS (x) number (1)
DB 255 ; Terminal width (255 = no auto CRLF)
DB 28 ; Width for commas (3 columns)
DB 0 ; No nulls after input DBs
DB 0 ; Output enabled (^O off)
DW 20 ; Initial lines counter
DW 20 ; Initial lines number
DW 0 ; Array load/save check sum
DB 0 ; Break not by NMI
DB 0 ; Break flag
jp TTYLIN ; Input reflection (set to TTY)
jp REM ; Disk Catalog reflection unused
jp REM ; Disk Load reflection
jp REM ; Disk Save reflection
DW STLOOK ; Temp string space
DW -2 ; Current line number (cold)
DW PROGST+1 ; Start of program text
INITBE:
; END OF INITIALISATION TABLE ---------------------------------------------------
ERRMSG: DB " Error",0
INMSG: DB " in ",0
ZERBYT: equ $-1 ; A zero DB
OKMSG: DB "Ok",CR,LF,0,0
BRKMSG: DB "Break",0
BAKSTK: ld HL,4 ; Look for "FOR" block with
add HL,SP ; same index as specified
LOKFOR: ld A,(HL) ; Get block ID
inc HL ; Point to index address
cp ZFOR ; Is it a "FOR" token
ret NZ ; No - exit
ld C,(HL) ; BC = Address of "FOR" index
inc HL
ld B,(HL)
inc HL ; Point to sign of STEP
push HL ; Save pointer to sign
ld L,C ; HL = address of "FOR" index
ld H,B
ld A,D ; See if an index was specified
or E ; DE = 0 if no index specified
ex DE,HL ; Specified index into HL
jr Z,INDFND ; Skip if no index given
ex DE,HL ; Index back into DE
call CPDEHL ; Compare index with one given
INDFND: ld BC,16-3 ; Offset to next block
pop HL ; Restore pointer to sign
ret Z ; Return if block found
add HL,BC ; Point to next block
jr LOKFOR ; Keep on looking
MOVUP: call ENFMEM ; See if enough memory
MOVSTR: push BC ; Save end of source
ex (SP),HL ; Swap source and dest" end
pop BC ; Get end of destination
MOVLP: call CPDEHL ; See if list moved
ld A,(HL) ; Get DB
ld (BC),A ; Move it
ret Z ; Exit if all done
dec BC ; Next DB to move to
dec HL ; Next DB to move
jp MOVLP ; Loop until all DBs moved
CHKSTK: push HL ; Save code string address
ld HL,(ARREND) ; Lowest free memory
ld B,0 ; BC = Number of levels to test
add HL,BC ; 2 DBs for each level
add HL,BC
DB 3EH ; Skip "push HL"
ENFMEM: push HL ; Save code string address
ld A,0D0H ;LOW -48 ; 48 DBs minimum RAM
sub L
ld L,A
ld A,0FFH; HIGH (-48) ; 48 DBs minimum RAM
sbc A,H
jp C,OMERR ; Not enough - ?OM Error
ld H,A
add HL,SP ; Test if stack is overflowed
pop HL ; Restore code string address
ret C ; Return if enough mmory
OMERR: ld E,OM ; ?OM Error
jr ERROR
DATSNR: ld HL,(DATLIN) ; Get line of current DATA item
ld (LINEAT),HL ; Save as current line
SNERR: ld E,SN ; ?SN Error
DB 01H ; Skip "ld E,DZ"
DZERR: ld E,DZ ; ?/0 Error
DB 01H ; Skip "ld E,NF"
NFERR: ld E,NF ; ?NF Error
DB 01H ; Skip "ld E,DD"
DDERR: ld E,DD ; ?DD Error
DB 01H ; Skip "ld E,UF"
UFERR: ld E,UF ; ?UF Error
DB 01H ; Skip "ld E,OV
OVERR: ld E,OV ; ?OV Error
DB 01H ; Skip "ld E,TM"
TMERR: ld E,TM ; ?TM Error
ERROR: call CLREG ; Clear registers and stack
ld (CTLOFG),A ; Enable output (A is 0)
call STTLIN ; Start new line
ld HL,ERRORS ; Point to error codes
ld D,A ; D = 0 (A is 0)
ld A,'?'
call OUTC ; Output '?'
add HL,DE ; Offset to correct error code
ld A,(HL) ; First character
call OUTC ; Output it
call GETCHR ; Get next character
call OUTC ; Output it
ld HL,ERRMSG ; "Error" message
ERRIN: call PRS ; Output message
ld HL,(LINEAT) ; Get line of error
ld DE,-2 ; Cold start error if -2
call CPDEHL ; See if cold start error
jp Z,CSTART ; Cold start error - Restart
ld A,H ; Was it a direct error?
and L ; Line = -1 if direct error
inc A
call NZ,LINEIN ; No - output line of error
DB 3EH ; Skip "pop BC"
POPNOK: pop BC ; Drop address in input buffer
PRNTOK: xor A ; Output "Ok" and get command
ld (CTLOFG),A ; Enable output
call STTLIN ; Start new line
ld HL,OKMSG ; "Ok" message
call PRS ; Output "Ok"
GETCMD: ld HL,-1 ; Flag direct mode
ld (LINEAT),HL ; Save as current line
call GETLIN ; Get an input line
jp C,GETCMD ; Get line again if break
call GETCHR ; Get first character
inc A ; Test if end of line
dec A ; Without affecting Carry
jr Z,GETCMD ; Nothing entered - Get another
push AF ; Save Carry status
call ATOH ; Get line number into DE
push DE ; Save line number
call CRUNCH ; Tokenise rest of line
ld B,A ; Length of tokenised line
pop DE ; Restore line number
pop AF ; Restore Carry
jp NC,EXCUTE ; No line number - Direct mode
push DE ; Save line number
push BC ; Save length of tokenised line
xor A
ld (LSTBIN),A ; Clear last DB input
call GETCHR ; Get next character
or A ; Set flags
push AF ; And save them
call SRCHLN ; Search for line number in DE
jr C,LINFND ; Jump if line found
pop AF ; Get status
push AF ; And re-save
jp Z,ULERR ; Nothing after number - Error
or A ; Clear Carry
LINFND: push BC ; Save address of line in prog
jp NC,INEWLN ; Line not found - Insert new
ex DE,HL ; Next line address in DE
ld HL,(PROGND) ; End of program
SFTPRG: ld A,(DE) ; Shift rest of program down
ld (BC),A
inc BC ; Next destination
inc DE ; Next source
call CPDEHL ; All done?
jr NZ,SFTPRG ; More to do
ld H,B ; HL - New end of program
ld L,C
ld (PROGND),HL ; Update end of program
INEWLN: pop DE ; Get address of line,
pop AF ; Get status
jp Z,SETPTR ; No text - Set up pointers
ld HL,(PROGND) ; Get end of program
ex (SP),HL ; Get length of input line
pop BC ; End of program to BC
add HL,BC ; Find new end
push HL ; Save new end
call MOVUP ; Make space for line
pop HL ; Restore new end
ld (PROGND),HL ; Update end of program pointer
ex DE,HL ; Get line to move up in HL
ld (HL),H ; Save MSB
pop DE ; Get new line number
inc HL ; Skip pointer
inc HL
ld (HL),E ; Save LSB of line number
inc HL
ld (HL),D ; Save MSB of line number
inc HL ; To first DB in line
ld DE,BUFFER ; Copy buffer to program
MOVBUF: ld A,(DE) ; Get source
ld (HL),A ; Save destinations
inc HL ; Next source
inc DE ; Next destination
or A ; Done?
jr NZ,MOVBUF ; No - Repeat
SETPTR: call RUNFST ; Set line pointers
inc HL ; To LSB of pointer
ex DE,HL ; Address to DE
PTRLP: ld H,D ; Address to HL
ld L,E
ld A,(HL) ; Get LSB of pointer
inc HL ; To MSB of pointer
or (HL) ; Compare with MSB pointer
jp Z,GETCMD ; Get command line if end
inc HL ; To LSB of line number
inc HL ; Skip line number
inc HL ; Point to first DB in line
xor A ; Looking for 00 DB
FNDEND: cp (HL) ; Found end of line?
inc HL ; Move to next DB
jp NZ,FNDEND ; No - Keep looking
ex DE,HL ; Next line address to HL
ld (HL),E ; Save LSB of pointer
inc HL
ld (HL),D ; Save MSB of pointer
jp PTRLP ; Do next line
SRCHLN: ld HL,(BASTXT) ; Start of program text
SRCHLP: ld B,H ; BC = Address to look at
ld C,L
ld A,(HL) ; Get address of next line
inc HL
or (HL) ; End of program found?
dec HL
ret Z ; Yes - Line not found
inc HL
inc HL
ld A,(HL) ; Get LSB of line number
inc HL
ld H,(HL) ; Get MSB of line number
ld L,A
call CPDEHL ; Compare with line in DE
ld H,B ; HL = Start of this line
ld L,C
ld A,(HL) ; Get LSB of next line address
inc HL
ld H,(HL) ; Get MSB of next line address
ld L,A ; Next line to HL
CCF
ret Z ; Lines found - Exit
CCF
ret NC ; Line not found,at line after
jr SRCHLP ; Keep looking
NEW: ret NZ ; Return if any more on line
CLRPTR: ld HL,(BASTXT) ; Point to start of program
xor A ; Set program area to empty
ld (HL),A ; Save LSB = 00
inc HL
ld (HL),A ; Save MSB = 00
inc HL
ld (PROGND),HL ; Set program end
RUNFST: ld HL,(BASTXT) ; Clear all variables
dec HL
INTVAR: ld (BRKLIN),HL ; Initialise RUN variables
ld HL,(LSTRAM) ; Get end of RAM
ld (STRBOT),HL ; Clear string space
xor A
call RESTOR ; Reset DATA pointers
ld HL,(PROGND) ; Get end of program
ld (VAREND),HL ; Clear variables
ld (ARREND),HL ; Clear arrays
CLREG: pop BC ; Save return address
ld HL,(STRSPC) ; Get end of working RAN
ld SP,HL ; Set stack
ld HL,TMSTPL ; Temporary string pool
ld (TMSTPT),HL ; Reset temporary string ptr
xor A ; A = 00
ld L,A ; HL = 0000
ld H,A
ld (CONTAD),HL ; No CONTinue
ld (FORFLG),A ; Clear FOR flag
ld (FNRGNM),HL ; Clear FN argument
push HL ; HL = 0000
push BC ; Put back return
DOAGN: ld HL,(BRKLIN) ; Get address of code to RUN
ret ; Return to execution driver
PROMPT: ld A,'?' ; '?'
call OUTC ; Output character
ld A,' ' ; Space
call OUTC ; Output character
jp RINPUT ; Get input line
CRUNCH: xor A ; Tokenise line @ HL to BUFFER
ld (DATFLG),A ; Reset literal flag
ld C,2+3 ; 2 DB number and 3 nulls
ld DE,BUFFER ; Start of input buffer
CRNCLP: ld A,(HL) ; Get DB
cp ' ' ; Is it a space?
jp Z,MOVDIR ; Yes - Copy direct
ld B,A ; Save character
cp '"' ; Is it a quote?
jp Z,CPYLIT ; Yes - Copy literal string
or A ; Is it end of buffer?
jp Z,ENDBUF ; Yes - End buffer
ld A,(DATFLG) ; Get data type
or A ; Literal?
ld A,(HL) ; Get DB to copy
jp NZ,MOVDIR ; Literal - Copy direct
cp '?' ; Is it '?' short for PRINT
ld A,ZPRINT ; "PRINT" token
jp Z,MOVDIR ; Yes - replace it
ld A,(HL) ; Get DB again
cp '0' ; Is it less than '0'
jr C,FNDWRD ; Yes - Look for reserved DWs
cp 60; ";"+1 ; Is it "0123456789:;" ?
jp C,MOVDIR ; Yes - copy it direct
FNDWRD: push DE ; Look for reserved DWs
ld DE,DWS-1 ; Point to table
push BC ; Save count
ld BC,RETNAD ; Where to return to
push BC ; Save return address
ld B,ZEND-1 ; First token value -1
ld A,(HL) ; Get DB
cp 'a' ; Less than 'a' ?
jr C,SEARCH ; Yes - search for DWs
cp 'z'+1 ; Greater than 'z' ?
jr NC,SEARCH ; Yes - search for DWs
and 01011111B ; FORce upper case
ld (HL),A ; Replace DB
SEARCH: ld C,(HL) ; Search for a DW
ex DE,HL
GETNXT: inc HL ; Get next reserved DW
or (HL) ; Start of DW?
jp P,GETNXT ; No - move on
inc B ; Increment token value
ld A, (HL) ; Get DB from table
and 01111111B ; Strip bit 7
ret Z ; Return if end of list
cp C ; Same character as in buffer?
jr NZ,GETNXT ; No - get next DW
ex DE,HL
push HL ; Save start of DW
NXTBYT: inc DE ; Look through rest of DW
ld A,(DE) ; Get DB from table
or A ; End of DW ?
jp M,MATCH ; Yes - Match found
ld C,A ; Save it
ld A,B ; Get token value
cp ZGOTO ; Is it "GOTO" token ?
jr NZ,NOSPC ; No - Don't allow spaces
call GETCHR ; Get next character
dec HL ; Cancel increment from GETCHR
NOSPC: inc HL ; Next DB
ld A,(HL) ; Get DB
cp 'a' ; Less than 'a' ?
jr C,NOCHNG ; Yes - don't change
and 01011111B ; Make upper case
NOCHNG: cp C ; Same as in buffer ?
jp Z,NXTBYT ; Yes - keep testing
pop HL ; Get back start of DW
jp SEARCH ; Look at next DW
MATCH: ld C,B ; DW found - Save token value
pop AF ; Throw away return
ex DE,HL
ret ; Return to "RETNAD"
RETNAD: ex DE,HL ; Get address in string
ld A,C ; Get token value
pop BC ; Restore buffer length
pop DE ; Get destination address
MOVDIR: inc HL ; Next source in buffer
ld (DE),A ; Put DB in buffer
inc DE ; Move up buffer