/
MINT.asm
1433 lines (1282 loc) · 41.4 KB
/
MINT.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
; *************************************************************************
;
; MINT Minimal Interpreter for the Z80
;
; Ken Boak, John Hardy and Craig Jones.
;
; GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007
;
; see the LICENSE file in this repo for more information
;
; *****************************************************************************
DSIZE EQU $80
RSIZE EQU $80
TIBSIZE EQU $100
TRUE EQU 1
FALSE EQU 0
NUMGRPS EQU 5
GRPSIZE EQU $40
; **************************************************************************
; Page 0 Initialisation
; **************************************************************************
.ORG ROMSTART + $180
start:
mint:
LD SP,DSTACK
CALL initialize
CALL printStr
.cstr "MINT V1.0\r\n"
JR interpret
; ***********************************************************************
; Initial values for user mintVars
; ***********************************************************************
iSysVars:
DW dStack ; a vS0
DW FALSE ; b vBase16
DW 0 ; c vTIBPtr
DW DEFS ; d vDEFS
DW 0 ; e vEdited the last command to be edited
DW 0 ; f
DW 0 ; g
DW HEAP ; h vHeapPtr
initialize:
LD IX,RSTACK
LD IY,NEXT ; IY provides a faster jump to NEXT
LD HL,iSysVars
LD DE,sysVars
LD BC,8 * 2
LDIR
LD HL,DEFS
LD B,GRPSIZE/2 * NUMGRPS
init1:
LD (HL),lsb(empty_)
INC HL
LD (HL),msb(empty_)
INC HL
DJNZ init1
RET
macro: ;=25
LD (vTIBPtr),BC
LD HL,ctrlCodes
ADD A,L
LD L,A
LD E,(HL)
LD D,msb(macros)
PUSH DE
call ENTER
.cstr "\\G"
LD BC,(vTIBPtr)
JR interpret2
interpret:
call ENTER
.cstr "\\N`> `"
interpret1: ; used by tests
LD BC,0 ; load BC with offset into TIB
LD (vTIBPtr),BC
interpret2: ; calc nesting (a macro might have changed it)
LD E,0 ; initilize nesting value
PUSH BC ; save offset into TIB,
; BC is also the count of chars in TIB
LD HL,TIB ; HL is start of TIB
JR interpret4
interpret3:
LD A,(HL) ; A = char in TIB
INC HL ; inc pointer into TIB
DEC BC ; dec count of chars in TIB
call nesting ; update nesting value
interpret4:
LD A,C ; is count zero?
OR B
JR NZ, interpret3 ; if not loop
POP BC ; restore offset into TIB
; *******************************************************************
; Wait for a character from the serial input (keyboard)
; and store it in the text buffer. Keep accepting characters,
; increasing the instruction pointer BC - until a newline received.
; *******************************************************************
waitchar:
CALL getchar ; loop around waiting for character
CP $20
JR NC,waitchar1
CP $0 ; is it end of string?
JR Z,waitchar4
CP '\r' ; carriage return?
JR Z,waitchar3
LD D,0
JR macro
waitchar1:
LD HL,TIB
ADD HL,BC
LD (HL),A ; store the character in textbuf
INC BC
CALL putchar ; echo character to screen
CALL nesting
JR waitchar ; wait for next character
waitchar3:
LD HL,TIB
ADD HL,BC
LD (HL),"\r" ; store the crlf in textbuf
INC HL
LD (HL),"\n"
INC HL ; ????
INC BC
INC BC
CALL crlf ; echo character to screen
LD A,E ; if zero nesting append and ETX after \r
OR A
JR NZ,waitchar
LD (HL),$03 ; store end of text ETX in text buffer
INC BC
waitchar4:
LD (vTIBPtr),BC
LD BC,TIB ; Instructions stored on heap at address HERE
DEC BC
JP NEXT
; ********************************************************************************
;
; Dispatch Routine.
;
; Get the next character and form a 1 byte jump address
;
; This target jump address is loaded into HL, and using JP (HL) to quickly
; jump to the selected function.
;
; Individual handler routines will deal with each category:
;
; 1. Detect characters A-Z and jump to the User Command handler routine
;
; 2. Detect characters a-z and jump to the variable handler routine
;
; 3. All other characters are punctuation and cause a jump to the associated
; primitive code.
;
; Instruction Pointer IP BC is incremented
;
; *********************************************************************************
NEXT: ; 9
INC BC ; 6t Increment the IP
LD A, (BC) ; 7t Get the next character and dispatch
LD L,A ; 4t Index into table
LD H,msb(opcodes) ; 7t Start address of jump table
LD L,(HL) ; 7t get low jump address
LD H,msb(page4) ; 7t Load H with the 1st page address
JP (HL) ; 4t Jump to routine
; ARRAY compilation routine
compNEXT: ;=20
POP DE ; DE = return address
LD HL,(vHeapPtr) ; load heap ptr
LD (HL),E ; store lsb
LD A,(vByteMode)
INC HL
OR A
JR NZ,compNext1
LD (HL),D
INC HL
compNext1:
LD (vHeapPtr),HL ; save heap ptr
JR NEXT
; **************************************************************************
; calculate nesting value
; A is char to be tested,
; E is the nesting value (initially 0)
; E is increased by ( and [
; E is decreased by ) and ]
; E has its bit 7 toggled by `
; limited to 127 levels
; **************************************************************************
nesting: ;= 44
CP '`'
JR NZ,nesting1
BIT 7,E
JR Z,nesting1a
RES 7,E
RET
nesting1a:
SET 7,E
RET
nesting1:
BIT 7,E
RET NZ
CP ':'
JR Z,nesting2
CP '['
JR Z,nesting2
CP '('
JR NZ,nesting3
nesting2:
INC E
RET
nesting3:
CP ';'
JR Z,nesting4
CP ']'
JR Z,nesting4
CP ')'
RET NZ
nesting4:
DEC E
RET
prompt: ;=9
call printStr
.cstr "\r\n> "
RET
; **************************************************************************
; Macros must be written in Mint and end with ;
; this code must not span pages
; **************************************************************************
macros:
.include "MINT-macros.asm"
; **************************************************************************
; Page 2 Jump Tables
; **************************************************************************
.align $100
opcodes:
; ***********************************************************************
; Initial values for user mintVars
; ***********************************************************************
DB lsb(exit_) ; NUL
DB lsb(nop_) ; SOH
DB lsb(nop_) ; STX
DB lsb(etx_) ; ETX
DB lsb(nop_) ; EOT
DB lsb(nop_) ; ENQ
DB lsb(nop_) ; ACK
DB lsb(nop_) ; BEL
DB lsb(nop_) ; BS
DB lsb(nop_) ; TAB
DB lsb(nop_) ; LF
DB lsb(nop_) ; VT
DB lsb(nop_) ; FF
DB lsb(nop_) ; CR
DB lsb(nop_) ; SO
DB lsb(nop_) ; SI
DB lsb(nop_) ; DLE
DB lsb(nop_) ; DC1
DB lsb(nop_) ; DC2
DB lsb(nop_) ; DC3
DB lsb(nop_) ; DC4
DB lsb(nop_) ; NAK
DB lsb(nop_) ; SYN
DB lsb(nop_) ; ETB
DB lsb(nop_) ; CAN
DB lsb(nop_) ; EM
DB lsb(nop_) ; SUB
DB lsb(nop_) ; ESC
DB lsb(nop_) ; FS
DB lsb(nop_) ; GS
DB lsb(nop_) ; RS
DB lsb(nop_) ; US
DB lsb(nop_) ; SP
DB lsb(store_) ; !
DB lsb(dup_) ; "
DB lsb(hex_) ; #
DB lsb(swap_) ; $
DB lsb(over_) ; %
DB lsb(and_) ; &
DB lsb(drop_) ; '
DB lsb(begin_) ; (
DB lsb(again_) ; )
DB lsb(mul_) ; *
DB lsb(add_) ; +
DB lsb(hdot_) ; ,
DB lsb(sub_) ; -
DB lsb(dot_) ; .
DB lsb(div_) ; /
DB lsb(num_) ; 0
DB lsb(num_) ; 1
DB lsb(num_) ; 2
DB lsb(num_) ; 3
DB lsb(num_) ; 4
DB lsb(num_) ; 5
DB lsb(num_) ; 6
DB lsb(num_) ; 7
DB lsb(num_) ; 8
DB lsb(num_) ; 9
DB lsb(def_) ; :
DB lsb(ret_) ; ;
DB lsb(lt_) ; <
DB lsb(eq_) ; =
DB lsb(gt_) ; >
DB lsb(getRef_) ; ?
DB lsb(fetch_) ; @
DB lsb(call_) ; A
DB lsb(call_) ; B
DB lsb(call_) ; C
DB lsb(call_) ; D
DB lsb(call_) ; E
DB lsb(call_) ; F
DB lsb(call_) ; G
DB lsb(call_) ; H
DB lsb(call_) ; I
DB lsb(call_) ; J
DB lsb(call_) ; K
DB lsb(call_) ; L
DB lsb(call_) ; M
DB lsb(call_) ; N
DB lsb(call_) ; O
DB lsb(call_) ; P
DB lsb(call_) ; Q
DB lsb(call_) ; R
DB lsb(call_) ; S
DB lsb(call_) ; T
DB lsb(call_) ; U
DB lsb(call_) ; V
DB lsb(call_) ; W
DB lsb(call_) ; X
DB lsb(call_) ; Y
DB lsb(call_) ; Z
DB lsb(arrDef_) ; [
DB lsb(alt_) ; \
DB lsb(arrEnd_) ; ]
DB lsb(xor_) ; ^
DB lsb(neg_) ; _
DB lsb(str_) ; `
DB lsb(var_) ; a
DB lsb(var_) ; b
DB lsb(var_) ; c
DB lsb(var_) ; d
DB lsb(var_) ; e
DB lsb(var_) ; f
DB lsb(var_) ; g
DB lsb(var_) ; h
DB lsb(var_) ; i
DB lsb(var_) ; j
DB lsb(var_) ; k
DB lsb(var_) ; l
DB lsb(var_) ; m
DB lsb(var_) ; n
DB lsb(var_) ; o
DB lsb(var_) ; p
DB lsb(var_) ; q
DB lsb(var_) ; r
DB lsb(var_) ; s
DB lsb(var_) ; t
DB lsb(var_) ; u
DB lsb(var_) ; v
DB lsb(var_) ; w
DB lsb(var_) ; x
DB lsb(var_) ; y
DB lsb(var_) ; z
DB lsb(shl_) ; {
DB lsb(or_) ; |
DB lsb(shr_) ; }
DB lsb(inv_) ; ~
DB lsb(nop_) ; backspace
; ***********************************************************************
; Alternate function codes
; ***********************************************************************
ctrlCodes:
altCodes:
DB lsb(empty_) ; NUL ^@
DB lsb(empty_) ; SOH ^A
DB lsb(toggleBase_) ; STX ^B
DB lsb(empty_) ; ETX ^C
DB lsb(empty_) ; EOT ^D
DB lsb(edit_) ; ENQ ^E
DB lsb(empty_) ; ACK ^F
DB lsb(empty_) ; BEL ^G
DB lsb(backsp_) ; BS ^H
DB lsb(empty_) ; TAB ^I
DB lsb(reedit_) ; LF ^J
DB lsb(empty_) ; VT ^K
DB lsb(list_) ; FF ^L
DB lsb(empty_) ; CR ^M
DB lsb(empty_) ; SO ^N
DB lsb(empty_) ; SI ^O
DB lsb(printStack_) ; DLE ^P
DB lsb(empty_) ; DC1 ^Q
DB lsb(empty_) ; DC2 ^R
DB lsb(empty_) ; DC3 ^S
DB lsb(empty_) ; DC4 ^T
DB lsb(empty_) ; NAK ^U
DB lsb(empty_) ; SYN ^V
DB lsb(empty_) ; ETB ^W
DB lsb(empty_) ; CAN ^X
DB lsb(empty_) ; EM ^Y
DB lsb(empty_) ; SUB ^Z
DB lsb(empty_) ; ESC ^[
DB lsb(empty_) ; FS ^\
DB lsb(empty_) ; GS ^]
DB lsb(empty_) ; RS ^^
DB lsb(empty_) ; US ^_)
DB lsb(aNop_) ; SP ^`
DB lsb(cStore_) ; !
DB lsb(aNop_) ; "
DB lsb(aNop_) ; #
DB lsb(aNop_) ; $ ( -- adr ) text input ptr
DB lsb(aNop_) ; %
DB lsb(aNop_) ; &
DB lsb(aNop_) ; '
DB lsb(ifte_) ; ( ( b -- )
DB lsb(aNop_) ; )
DB lsb(aNop_) ; *
DB lsb(incr_) ; + ( adr -- ) decrements variable at address
DB lsb(aNop_) ; ,
DB lsb(aNop_) ; -
DB lsb(aNop_) ; .
DB lsb(aNop_) ; /
DB lsb(aNop_) ; 0
DB lsb(aNop_) ; 1
DB lsb(aNop_) ; 2
DB lsb(aNop_) ; 3
DB lsb(aNop_) ; 4
DB lsb(aNop_) ; 5
DB lsb(aNop_) ; 6
DB lsb(aNop_) ; 7
DB lsb(aNop_) ; 8
DB lsb(aNop_) ; 9
DB lsb(aNop_) ; : start defining a macro
DB lsb(aNop_) ; ;
DB lsb(aNop_) ; <
DB lsb(aNop_) ; =
DB lsb(aNop_) ; >
DB lsb(aNop_) ; ?
DB lsb(cFetch_) ; @
DB lsb(aNop_) ; A
DB lsb(break_) ; B
DB lsb(nop_) ; C
DB lsb(depth_) ; D ( -- val ) depth of data stack
DB lsb(emit_) ; E ( val -- ) emits a char to output
DB lsb(aNop_) ; F
DB lsb(go_) ; G ( -- ? ) execute mint definition
DB lsb(aNop_) ; H
DB lsb(inPort_) ; I ( port -- val )
DB lsb(aNop_) ; J
DB lsb(key_) ; K ( -- val ) read a char from input
DB lsb(aNop_) ; L
DB lsb(aNop_) ; M
DB lsb(newln_) ; N ; prints a newline to output
DB lsb(outPort_) ; O ( val port -- )
DB lsb(printStk_) ; P ( -- ) non-destructively prints stack
DB lsb(aNop_) ; Q quits from Mint REPL
DB lsb(rot_) ; R ( a b c -- b c a )
DB lsb(aNop_) ; S
DB lsb(aNop_) ; T
DB lsb(aNop_) ; U
DB lsb(aNop_) ; V
DB lsb(aNop_) ; W ; ( b -- ) if false, skip to end of loop
DB lsb(exec_) ; X
DB lsb(aNop_) ; Y
DB lsb(editDef_) ; Z
DB lsb(cArrDef_) ; [
DB lsb(comment_) ; \ comment text, skips reading until end of line
DB lsb(aNop_) ; ]
DB lsb(charCode_) ; ^
DB lsb(aNop_) ; _
DB lsb(aNop_) ; `
DB lsb(sysVar_) ; a ; start of data stack variable
DB lsb(sysVar_) ; b ; base16 variable
DB lsb(sysVar_) ; c ; TIBPtr variable
DB lsb(sysVar_) ; d
DB lsb(sysVar_) ; e
DB lsb(sysVar_) ; f
DB lsb(sysVar_) ; g
DB lsb(sysVar_) ; h ; heap ptr variable
DB lsb(i_) ; i ; returns index variable of current loop
DB lsb(j_) ; j ; returns index variable of outer loop
DB lsb(sysVar_) ; k
DB lsb(sysVar_) ; l
DB lsb(sysVar_) ; m ( a b -- c ) return the minimum value
DB lsb(sysVar_) ; n
DB lsb(sysVar_) ; o
DB lsb(sysVar_) ; p
DB lsb(sysVar_) ; q
DB lsb(sysVar_) ; r
DB lsb(sysVar_) ; s
DB lsb(sysVar_) ; t
DB lsb(sysVar_) ; u
DB lsb(sysVar_) ; v
DB lsb(sysVar_) ; w
DB lsb(sysVar_) ; x
DB lsb(sysVar_) ; y
DB lsb(sysVar_) ; z
DB lsb(group_) ; {
DB lsb(aNop_) ; |
DB lsb(endGroup_) ; }
DB lsb(aNop_) ; ~
DB lsb(aNop_) ; BS
; **********************************************************************
; Page 4 primitive routines
; **********************************************************************
.align $100
page4:
alt_:
JP alt
and_:
POP DE ; 10t Bitwise AND the top 2 elements of the stack
POP HL ; 10t
LD A,E ; 4t
AND L ; 4t
LD L,A ; 4t
LD A,D ; 4t
AND H ; 4t
and1:
LD H,A ; 4t
PUSH HL ; 11t
JP (IY) ; 8t
; 63t
or_:
POP DE ; Bitwise OR the top 2 elements of the stack
POP HL
LD A,E
OR L
LD L,A
LD A,D
OR H
JR and1
xor_:
POP DE ; Bitwise XOR the top 2 elements of the stack
xor1:
POP HL
LD A,E
XOR L
LD L,A
LD A,D
XOR H
JR and1
inv_: ; Bitwise INVert the top member of the stack
LD DE, $FFFF ; by xoring with $FFFF
JR xor1
add_: ; Add the top 2 members of the stack
POP DE ; 10t
POP HL ; 10t
ADD HL,DE ; 11t
PUSH HL ; 11t
JP (IY) ; 8t
; 50t
arrDef_:
arrDef: ;= 18
LD A,FALSE
arrDef1:
LD IY,compNEXT
LD (vByteMode),A
LD HL,(vHeapPtr) ; HL = heap ptr
CALL rpush ; save start of array \[ \]
JP NEXT ; hardwired to NEXT
arrEnd_: JP arrEnd
begin_: JP begin
call_:
LD HL,BC
CALL rpush ; save Instruction Pointer
LD A,(BC)
CALL lookupDef1
LD C,(HL)
INC HL
LD B,(HL)
DEC BC
JP (IY) ; Execute code from User def
def_: JP def
hdot_: ; print hexadecimal
POP HL
CALL printhex
JR dot2
dot_:
POP HL
CALL printdec
dot2:
LD A,' '
CALL writeChar1
JP (IY)
drop_: ; Discard the top member of the stack
POP HL
JP (IY)
dup_:
POP HL ; Duplicate the top member of the stack
PUSH HL
PUSH HL
JP (IY)
etx_:
etx:
LD HL,-DSTACK
ADD HL,SP
JR NC,etx1
LD SP,DSTACK
etx1:
JP interpret
exit_:
INC BC
LD DE,BC
CALL rpop ; Restore Instruction pointer
LD BC,HL
EX DE,HL
JP (HL)
fetch_: ; Fetch the value from the address placed on the top of the stack
POP HL ; 10t
fetch1:
LD E,(HL) ; 7t
INC HL ; 6t
LD D,(HL) ; 7t
PUSH DE ; 11t
JP (IY) ; 8t
hex_: JP hex
nop_: JP NEXT ; hardwire white space to always go to NEXT (important for arrays)
num_:
JP number
over_:
POP HL ; Duplicate 2nd element of the stack
POP DE
PUSH DE
PUSH HL
PUSH DE ; And push it to top of stack
JP (IY)
ret_:
CALL rpop ; Restore Instruction pointer
LD BC,HL
JP (IY)
store_: ; Store the value at the address placed on the top of the stack
POP HL ; 10t
POP DE ; 10t
LD (HL),E ; 7t
INC HL ; 6t
LD (HL),D ; 7t
JP (IY) ; 8t
; 48t
; $ swap ; a b -- b a Swap the top 2 elements of the stack
swap_:
POP HL
EX (SP),HL
PUSH HL
JP (IY)
; Left shift { is multply by 2
shl_:
POP HL ; Duplicate the top member of the stack
ADD HL,HL
PUSH HL ; shift left fallthrough into add_
JP (IY) ; 8t
; Right shift } is a divide by 2
shr_:
POP HL ; Get the top member of the stack
shr1:
SRL H
RR L
PUSH HL
JP (IY) ; 8t
neg_: LD HL, 0 ; NEGate the value on top of stack (2's complement)
POP DE ; 10t
JR SUB_2 ; use the SUBtract routine
sub_: ; Subtract the value 2nd on stack from top of stack
POP DE ; 10t
sub_1: POP HL ; 10t Entry point for INVert
sub_2: AND A ; 4t Entry point for NEGate
SBC HL,DE ; 15t
PUSH HL ; 11t
JP (IY) ; 8t
; 58t
eq_: POP HL
POP DE
AND A ; reset the carry flag
SBC HL,DE ; only equality sets HL=0 here
JR Z, equal
LD HL, 0
JR less ; HL = 1
getRef_:
JP getRef
gt_: POP DE
POP HL
JR cmp_
lt_: POP HL
POP DE
cmp_: AND A ; reset the carry flag
SBC HL,DE ; only equality sets HL=0 here
JR Z,less ; equality returns 0 KB 25/11/21
LD HL, 0
JP M,less
equal: INC L ; HL = 1
less:
PUSH HL
JP (IY)
var_:
LD A,(BC)
SUB "a" - ((VARS - mintVars)/2)
ADD A,A
LD L,A
LD H,msb(mintVars)
PUSH HL
JP (IY)
div_:
JR div
mul_:
JP mul
again_:
JP again
str_:
str: ;= 15
INC BC
nextchar:
LD A, (BC)
INC BC
CP "`" ; ` is the string terminator
JR Z,str2
CALL putchar
JR nextchar
str2:
DEC BC
JP (IY)
;*******************************************************************
; Page 5 primitive routines
;*******************************************************************
;falls through
getRef: ;= 8
INC BC
LD A,(BC)
CALL lookupDef
JP fetch1
alt: ;= 11
INC BC
LD A,(BC)
LD HL,altCodes
ADD A,L
LD L,A
LD L,(HL) ; 7t get low jump address
LD H, msb(page6) ; Load H with the 5th page address
JP (HL) ; 4t Jump to routine
; ********************************************************************
; 16-bit multiply
mul: ;=19
POP DE ; get first value
POP HL
PUSH BC ; Preserve the IP
LD B,H ; BC = 2nd value
LD C,L
LD HL,0
LD A,16
Mul_Loop_1:
ADD HL,HL
RL E
RL D
JR NC,$+6
ADD HL,BC
JR NC,$+3
INC DE
DEC A
JR NZ,Mul_Loop_1
POP BC ; Restore the IP
PUSH HL ; Put the product on the stack - stack bug fixed 2/12/21
JP (IY)
; ********************************************************************
; 16-bit division subroutine.
;
; BC: divisor, DE: dividend, HL: remainder
; *********************************************************************
; This divides DE by BC, storing the result in DE, remainder in HL
; *********************************************************************
; 1382 cycles
; 35 bytes (reduced from 48)
div: ;=24
POP DE ; get first value
POP HL ; get 2nd value
PUSH BC ; Preserve the IP
LD B,H ; BC = 2nd value
LD C,L
ld hl,0 ; Zero the remainder
ld a,16 ; Loop counter
div_loop: ;shift the bits from BC (numerator) into HL (accumulator)
sla c
rl b
adc hl,hl
sbc hl,de ;Check if remainder >= denominator (HL>=DE)
jr c,div_adjust
inc c
jr div_done
div_adjust: ; remainder is not >= denominator, so we have to add DE back to HL
add hl,de
div_done:
dec a
jr nz,div_loop
LD D,B ; Result from BC to DE
LD E,C
div_end:
POP BC ; Restore the IP
PUSH DE ; Push Result
PUSH HL ; Push remainder
JP (IY)
; **************************************************************************
; def is used to create a colon definition
; When a colon is detected, the next character (usually uppercase alpha)
; is looked up in the vector table to get its associated code field address
; This CFA is updated to point to the character after uppercase alpha
; The remainder of the characters are then skipped until after a semicolon
; is found.
; ***************************************************************************
;= 31
def: ; Create a colon definition
INC BC
LD A,(BC) ; Get the next character
INC BC
CALL lookupDef
LD DE,(vHeapPtr) ; start of defintion
LD (HL),E ; Save low byte of address in CFA
INC HL
LD (HL),D ; Save high byte of address in CFA+1
def1: ; Skip to end of definition
LD A,(BC) ; Get the next character
INC BC ; Point to next character
LD (DE),A
INC DE
CP ";" ; Is it a semicolon
JR Z, def2 ; end the definition
JR def1 ; get the next element
def2:
DEC BC
def3:
LD (vHeapPtr),DE ; bump heap ptr to after definiton
JP (IY)
; ********************************************************************************
; Number Handling Routine - converts numeric ascii string to a 16-bit number in HL
; Read the first character.
;
; Number characters ($30 to $39) are converted to digits by subtracting $30
; and then added into the L register. (HL forms a 16-bit accumulator)
; Fetch the next character, if it is a number, multiply contents of HL by 10
; and then add in the next digit. Repeat this until a non-number character is
; detected. Add in the final digit so that HL contains the converted number.
; Push HL onto the stack and proceed to the dispatch routine.
; ********************************************************************************
number: ;= 23
LD HL,$0000 ; 10t Clear HL to accept the number
LD A,(BC) ; 7t Get the character which is a numeral
number1: ; corrected KB 24/11/21
SUB $30 ; 7t Form decimal digit
ADD A,L ; 4t Add into bottom of HL
LD L,A ; 4t
LD A,00 ; 4t Clear A
ADC A,H ; Add with carry H-reg
LD H,A ; Put result in H-reg
INC BC ; 6t Increment IP
LD A, (BC) ; 7t and get the next character
CP $30 ; 7t Less than $30
JR C, endnum ; 7/12t Not a number / end of number
CP $3A ; 7t Greater or equal to $3A
JR NC, endnum ; 7/12t Not a number / end of number
times10: ; Multiply digit(s) in HL by 10
ADD HL,HL ; 11t 2X
LD E,L ; 4t LD DE,HL
LD D,H ; 4t
ADD HL,HL ; 11t 4X
ADD HL,HL ; 11t 8X
ADD HL,DE ; 11t 2X + 8X = 10X
; 52t cycles
JR number1
endnum:
DEC BC
PUSH HL ; 11t Put the number on the stack
JP (IY) ; and process the next character
; *************************************
; Loop Handling Code
; *************************************
;= 23
begin: ; Left parentesis begins a loop
POP HL
LD A,L ; zero?
OR H
JR Z,begin1
DEC HL
LD DE,-6
ADD IX,DE
LD (IX+0),0 ; loop var
LD (IX+1),0
LD (IX+2),L ; loop limit
LD (IX+3),H
LD (IX+4),C ; loop address
LD (IX+5),B
JP (IY)
begin1:
LD E,1
begin2:
INC BC
LD A,(BC)
CALL nesting
XOR A
OR E
JR NZ,begin2
begin3:
JP (IY)
again:
LD E,(IX+0) ; peek loop var
LD D,(IX+1)