-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathF16m.f
5966 lines (4974 loc) · 131 KB
/
F16m.f
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
\ ______________________________________________________________________
\
.( v-Forth 1.6 MDR/MGT version ) CR
.( build 20240420 ) CR
.( ZX Microdrive version + MGT DISCiPLE version ) CR
\ ______________________________________________________________________
\
\ MIT License
\
\ Copyright (c) 1990-2024 Matteo Vitturi
\
\ Permission is hereby granted, free of charge, to any person obtaining a copy
\ of this software and associated documentation files (the "Software"), to deal
\ in the Software without restriction, including without limitation the rights
\ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\ copies of the Software, and to permit persons to whom the Software is
\ furnished to do so, subject to the following conditions:
\
\ The above copyright notice and this permission notice shall be included in all
\ copies or substantial portions of the Software.
\
\ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
\ SOFTWARE.
\ ______________________________________________________________________
\
\ https://sites.google.com/view/vforth/vforth15-next
\ https://www.oocities.org/matteo_vitturi/english/index.htm
\
\ This is the complete compiler for v.Forth for SINCLAIR ZX Spectrum 48/128K.
\ Each line of this source list mustn't exceed 80 bytes.
\ Z80N (ZX Spectrum Next) extension is available.
\
\ This list has been tested using the following configuration:
\ - Windows 7 or 10 + DosBox 0.74
\ - Z80 Emulator ver.4.00 with Interface 1
\ - Microdrive BLOCKS cartridge to be linked to stream #4
\ OPEN #4,"M",1,"!Blocks" to enable Forth's BLOCK storage system
\ - Redirect this file to RS232 Input
\ OPEN #11,"B" to link this file to Spectrum's channel #11
\ to be LOADed using -3 LOAD
\ - RealSpec emulator ver. 0.98.14
\ - Two .IMG file attached using direct hook-code #44 #45
\
\ There are a few modifications to keep in mind since previous v. 1.2
\ ' (tick) returns CFA, instead of PFA as previously was
\ -FIND returns CFA, instead of PFA as previously was
\ SP! must be passed with the address to initialize SP register
\ RP! must be passed with the address to initialize RP.
\ WORD now returns address HERE: a few blocks must be corrected
\ CREATE now creates a definition that returns its PFA.
\ ______________________________________________________________________
\
\ Z80 Registers usage map
\
\ AF
\ BC - Instruction Pointer: it must be preserved during ROM/OS calls
\ DE - Return Stack Pointer: it must be preserved during ROM/OS calls
\ HL - Working (High word when used for 32-bit manipulations)
\
\ AF'- Sometime used for backup purpose
\ BC'- Sometime used in tricky definitions
\ DE'- Sometime used in tricky definitions
\ HL'- Sometime used in tricky definitions
\
\ SP - Calc Stack Pointer
\ IX - Inner interpreter next-address pointer. This is 2T-state faster than JP
\ it must be preserved during ROM/OS calls
\ IY - (ZX System: must be preserved to use standard Interrupts )
\ From this version vForth is a "direct-thread" instead of an "indirect-thread"
\ Forth system. It brings a +25% speed improvement.
\ Any Low-Level definition saves two bytes, but any non Low-Level definitions
\ needs one additional byte because CFA does not contain an address anymore,
\ instead CFA contains the real machine-code to be executed.
\ For non Low-level definitions CFA is a three byte instruction "CALL aaaa" to
\ the ;CODE part routine that handles that kind of definition.
\
\ ______________________________________________________________________
\ _________________
\
FORTH DEFINITIONS
\ _________________
DECIMAL 9 LOAD \ NEEDS ASSEMBLER
\ NEEDS RENAME
\ NEEDS VALUE
\ NEEDS TO
CASEON \ we must be in Case-sensitive option to compile this file.
0 WARNING ! \ avoid verbose messaging
\ The following word makes dynamic the location of "Origin", so that we can
\ compile this Forth *twice* to rebuild it completely in itself.
\ At second run, it forces DP to re-start at a lower-address such as the
\ following. (N.B. an open microdrive channel requires 627 bytes)
HEX
: SET-DP-TO-ORG
0 +ORIGIN $8000 U< 0=
IF
\ 6100 \ 24832 \ = origin of v1.2. it allows 1 microdrive channel
6400 \ 25600 \ = origin of v1.413. it allows 2 microdrive channels
\ $6363 \ 25443 \ = origin of v1.5. Allow 64 bytes of OS-call stack.
\ 6500 \ 25856 \ = plus another 256 page to host TIB/RP/User vars.
\ 6A00 \ 27136 \ = room for 3 buffers below ORIGIN
\ 8000 \ 32768 \ = final
DP !
THEN
;
\ _________________
\
.( Key & Emit table-pointer )
\ This table is used later to insert a value in a conversion table for EMIT
\ that is a two areas table, EMIT-A are vector of address and EMIT-C are
\ characters. If you scan EMIT-C for a matching character you can get an
\ address to be used as a conversion subroutine
DECIMAL
8 CONSTANT EMIT-N \ EMIT table length
0 VALUE EMIT-C^ \ char table
0 VALUE EMIT-A^ \ addr table
0 VALUE EMIT-2^ \ comodo
0 VALUE KEY-1^ \ KEY decode table
0 VALUE KEY-2^
\ \ this gives the "end" of EMIT-table
\ : EMIT-Z^
\ EMIT-C^ EMIT-N + 1 -
\ ;
\
.( Forward Pointers )
\ These __ forward __ pointers are used to keep track of things we are compiling
\
0 VALUE org^ \ ptr to origin of Forth
0 VALUE cold^ \ ptr to cold start routine
0 VALUE warm^ \ ptr to warm start routine
0 VALUE vars^ \ ptr to user's vars ptr
0 VALUE rp^ \ ptr to RP register
0 VALUE next^ \ ptr to NEXT in inner interpreter
0 VALUE exec^ \ ptr to exec xt
0 VALUE branch^ \ PFA of BRANCH
0 VALUE branch^^ \ ptr to be patched with PFA of BRANCH
0 VALUE loop^ \ entry-point for compiled (+LOOP)
0 VALUE do^ \ entry-point for compiled (DO)
0 VALUE emitc^ \ entry-point for EMITC
0 VALUE upper^ \ entry-point for UPPER
\ some pointers are used to patch words later...
\ Since we need to define a word using "forward" definitions
\ we simply use the previous definitions with the promise
\ we'll patch the just created word using the forward definitions
\ as soon as we have them available (later during the compilation)
\ 0 VALUE lit~ \ some CFA
\ 0 VALUE branch~ \ CFA of BRANCH
\ 0 VALUE 0branch~ \ CFA of 0BRANCH
\ 0 VALUE (loop)~ \ CFA of (LOOP)
\ 0 VALUE (do)~ \ CFA of (DO)
\ 0 VALUE (?do)~ \ CFA of (?DO)
0 VALUE msg1^ \ patch of ERROR
0 VALUE msg2^ \ patch of CODE/MCOD (CREATE)
0 VALUE enter^ \ resolved within : definition.
0 VALUE error^ \ patch of ?ERROR with ERROR
0 VALUE asm^ \ patch of ;CODE with ASSEMBLER
0 VALUE block^ \ patch of WORD with BLOCK
0 VALUE block2^ \ patch of WORD with (LINE)
0 VALUE quit1^ \ patch of ERROR with QUIT
0 VALUE quit2^ \ patch of INTERPRET with QUIT
0 VALUE abort^ \ patch of (ABORT)
\ 0 VALUE xi/o^ \ patch of XI/O and WARM in COLD
0 VALUE xi/o2^ \ patch of BLK-INIT in WARM
0 VALUE y^ \ patch of COLD/WARM start
0 VALUE splash^ \ patch of SPLASH in WARM
0 VALUE autoexec^ \ patch of NOOP in ABORT
0 VALUE .^ \ patch of . in MESSAGE
0 VALUE pdom^
0 VALUE pcdm^
0 VALUE boolean_exit^
.( Psh2 Psh1 Next )
\ compile CODE words for Jump to the Psh2, Psh1 or Next addresses.
\ : Psh2 ASSEMBLER JP next^ 2- AA, ;
\ : Psh1 ASSEMBLER JP next^ 1- AA, ;
\ : Next ASSEMBLER JP next^ AA, ;
\ : Psh2 ASSEMBLER PUSH DE| PUSH HL| JPIX ;
\ : Psh1 ASSEMBLER PUSH HL| JPIX ;
: Next ASSEMBLER JPIX ;
\ ______________________________________________________________________
\
\ HERE HEX U.
\ HP@ U.
\ KEY DROP
\ force origin to an even address?
\ HERE 1 AND ALLOT
SET-DP-TO-ORG
CR HERE HEX U. CR CR
\ HP@ U.
\ KEY DROP
\ ______________________________________________________________________
\
\ Interrupt vector 6363
\ ASSEMBLER
\ JP HEX 0038 AA, \ 6363h
\ \ 6366h
\
.( Origin )
\
\ ______________________________________________________________________
HERE TO org^
\ 6100h or 6366h or 8000h depending on which version is compiling
\ this addresses simply are a "remind" for myself.
.( +000 )
ASSEMBLER
ANDA A|
JP HERE TO cold^
HEX 7530 AA,
SCF
JP HERE TO warm^
HEX 7530 AA,
.( +008 )
HEX 0101 , \ saved Basic SP
.( +00A )
HEX 0E00 , \ ?
\ 610Ch
.( +00C )
0 , \ LATEST word used in COLD start
.( +00E )
HEX 000C , \ "DEL" character
.( +010 )
DECIMAL 36 BASE ! Z80 , DECIMAL
\ start of data for COLD start
.( +012 )
S0 @ , \ HEX D0E8 , \ S0
.( +014 )
R0 @ , \ HEX D188 , \ R0
.( +016 )
TIB @ , \ HEX D0E8 , \ TIB
.( +018 )
DECIMAL 31 , \ WIDTH
.( +01A )
1 , \ WARNING
.( +01C )
0 , \ FENCE
.( +01E )
0 , \ DP
.( +020 )
0 , \ VOC-LINK
.( +022 )
FIRST @ ,
.( +024 )
LIMIT @ ,
\ .( +026 )
\ HP @ ,
\ end of data for COLD start
.( +026 )
HEX 8F C, 8C C, \ Cursor faces used by KEY
.( +028 )
HEX 5F C, 00 C, \ Used by KEY
.( +02A ) ( Saved SP during NextZXOS call )
0 ,
.( +02C )
0 ,
.( +02E ) ( User Variable Pointer )
HERE TO vars^ R0 @ , \ HEX EAE0 ,
.( +030 ) ( Return Stack Pointer )
HEX 030 +ORIGIN TO rp^
R0 @ , \ 2 - \ was HEX EADE ,
\ .( +032 ) ( Echoed IX after NextZXOS call )
\ 0 ,
\ from this point we can use LDHL,RP and LDRP,HL Assembler macros
\ instead of their equivalent long sequences.
ASSEMBLER
\ 6128h
\ hook for Next - inner interpreter
HERE TO next^
LDA(X) BC|
INCX BC|
LD L'| A|
LDA(X) BC|
INCX BC|
LD H'| A|
\ 612Eh
\ Execute "xt" i.e. CFA held in HL
HERE TO exec^
JPHL
\ 6133h
.( LIT )
\ puts on top of stack the value of the next location.
\ it is compiled in colon definition before a literal number
CODE lit ( -- n )
LDA(X) BC|
INCX BC|
LD L'| A|
LDA(X) BC|
INCX BC|
LD H'| A|
PUSH HL|
Next
C;
\ ' lit TO lit~
\ let's patch the previous definition of LITERAL so that it will compile the
\ new definition of LIT. We use RENAME to be sure to not
' lit ' LITERAL >BODY 5 CELLS + !
\ 6144h
.( EXECUTE )
\ execution token. usually xt is given by CFA
CODE execute ( xt -- )
RET
\ in general, we use JR within the same definition, JP to go beyond.
\ but it can be useful using JR in some closest words to save a few bytes
C;
\ 6181h
." (+LOOP) "
\ compiled by +LOOP. it uses the top two values of return-stack to
\ keep track of index and limit, they are accessed via I and I'
CODE (+loop) ( n -- )
HERE TO loop^
POP HL| \ HL is increment DE is RP
EXDEHL \ DE is increment HL is RP
PUSH BC| \ save IP
LD B'| D|
LD C'| E| \ BC is increment
PUSH HL| \ save original RP
LD E'| (HL)| \ DE keeps index before increment
LD A'| E| \ index is incremented in memory.
ADDA C|
LD (HL)'| A|
INCX HL|
LD D'| (HL)|
LD A'| D|
ADCA B|
LD (HL)'| A|
INCX HL|
LD A'| E|
SUBA (HL)|
LD E'| A|
INCX HL|
LD A'| D|
SBCA (HL)|
LD D'| A| \ DE is index - limit : limit is the "new zero"
EXDEHL \ DE is RP, HL is index - limit
ADDHL BC| \ HL is index - limit + increment
BIT 7| B| \ doesn't affect carry flag
JRF Z'| HOLDPLACE \ if increment is negative then
CCF \ complement carry flag
HERE DISP, \ THEN,
JRF CY'| HOLDPLACE
\ stay in loop, increment index
POP DE| \ restore RP
POP BC| \ restore IP
JR HERE TO branch^^ 26 D,
\ JP branch^ AA,
HERE DISP, \ THEN,
POP BC| \ discard original RP
EXDEHL \ HL is RP
INCX HL|
EXDEHL \ DE is RP
POP BC| \ restore IP
INCX BC|
INCX BC|
Next
C;
\ 61BAh
." (LOOP) "
\ same as (+LOOP) but index is incremented by 1
CODE (loop) ( -- )
LDX HL| 1 NN,
PUSH HL|
\ JP loop^ AA,
JR loop^ HERE 1 + - D,
C;
\ ' (loop) TO (loop)~
' (loop) ' LOOP >BODY 3 CELLS + !
\ 6153h
.( BRANCH )
\ unconditional branch in colon definition
\ compiled by ELSE, AGAIN and some other immediate words
CODE branch ( -- )
\ 615E
HERE TO branch^
LDA(X) BC|
LD L'| A|
INCX BC|
LDA(X) BC|
LD H'| A|
DECX BC|
ADDHL BC|
LD C'| L|
LD B'| H|
Next
C;
\ ' branch TO branch~
' branch ' ELSE >BODY 4 CELLS + !
' branch ' AGAIN >BODY 4 CELLS + !
branch^ branch^^ 1+ - branch^^ C! \ fixed previous "30"
\ 616Ah
.( 0BRANCH )
\ conditional branch if the top-of-stack is zero.
\ compiled by IF, UNTIL and some other immediate words
CODE 0branch ( f -- )
POP HL|
LD A'| L|
ORA H|
\ JPF Z| branch^ AA,
JRF Z'| branch^ HERE 1 + - D,
INCX BC|
INCX BC|
Next
C;
\ ' 0branch TO 0branch~
' 0branch ' IF >BODY 1 CELLS + !
' 0branch ' UNTIL >BODY 4 CELLS + !
." LEAVE"
CODE (leave) ( -- )
LDX HL| 4 NN, \ UNLOOP index & limit from Return Stack
ADDHL DE|
EXDEHL
\ JP branch^ AA, \ jump out of loop
JR branch^ HERE 1 + - D,
Next
C;
\ new
." (?DO) "
\ compiled by ?DO to make a loop checking for lim == ind first
\ at run-time (?DO) must be followed by a BRANCH offset
\ used to skip the loop if lim == ind
CODE (?do) ( lim ind -- )
EXX
\ copy lim to HL and ind to DE.
POP DE| \ ind
POP HL| \ lim
LD B'| H|
LD C'| L|
PUSH HL|
PUSH DE|
ANDA A|
SBCHL DE| \ lim - ind
EXX
JRF NZ'| HOLDPLACE \ if lim == ind
POP HL|
POP HL|
\ JP branch^ AA,
JR branch^ HERE 1 + - D,
HERE DISP, \ THEN,
HERE TO do^
\ prepares return-stack-pointer
\ EXDEHL
DECX DE| \ this is 1T faster than
DECX DE| \ ld de,-4 (10T)
DECX DE| \ add hl,de (15T)
DECX DE| \ since dec hl is 6T.
PUSH DE|
\ EXDEHL
EXX
POP HL|
\ stores ind as top RP
POP DE|
LD (HL)'| E|
INCX HL|
LD (HL)'| D|
INCX HL|
\ stores lim as second
POP DE|
LD (HL)'| E|
INCX HL|
LD (HL)'| D|
EXX
\ skips 0branch offset
INCX BC|
INCX BC|
Next
C;
\ ' (?do) TO (?do)~
' (?do) ' ?DO >BODY 1 CELLS + !
\ 61CAh
." (DO) "
\ compiled by DO to make a loop checking for lim == ind first
\ this is a simpler version of (?DO)
CODE (do) ( lim ind -- )
DECX BC| \ balance the two INC BC at end of (?DO)
DECX BC|
\ JP do^ AA,
JR do^ HERE 1 + - D,
C;
\ ' (do) TO (do)~
' (do) ' DO >BODY 1 CELLS + !
RENAME LITERAL Literal
RENAME DLITERAL Dliteral
RENAME IF If
RENAME THEN Then
RENAME ELSE Else
RENAME BEGIN Begin
RENAME AGAIN Again
RENAME UNTIL Until
RENAME WHILE While
RENAME REPEAT Repeat
RENAME DO Do
RENAME LOOP Loop
RENAME ?DO ?Do
\ 61E9h
.( I )
\ used between DO and LOOP or between DO e +LOOP to copy on top of stack
\ the current value of the index-loop
CODE i ( -- ind )
LD H'| D|
LD L'| E|
HERE
LD A'| (HL)|
INCX HL|
LD H'| (HL)|
LD L'| A|
PUSH HL|
Next
C;
.( I' )
\ used between DO and LOOP or between DO e +LOOP to copy on top of stack
\ the limit of the index-loop
CODE i' ( -- lim )
LD H'| D|
LD L'| E|
INCX HL|
INCX HL|
JR BACK, \ jump to "HERE" on I
C;
\ 61F9h
.( DIGIT )
\ convert a character c using base n
\ returns a unsigned number and a true flag
\ or just a false flag if the conversion fails
CODE digit ( c n -- u 1 | 0 )
EXX
POP HL| \ base
POP DE| \ char
LD A'| E|
CPN HEX 60 N, \ if lowercase
JRF CY'| HOLDPLACE
SUBN HEX 20 N, \ quick'n'dirty uppercase
HERE DISP, \ THEN,
SUBN HEX 30 N,
JRF CY'| HOLDPLACE \ good when greater than 30
CPN HEX 0A N,
JRF CY'| HOLDPLACE \ if >= 10 try to subtract 7
SUBN 7 N,
CPN HEX 0A N, \ fail if now it is < 10
JRF CY'| HOLDPLACE SWAP \ hide this holdplace... (!!)
HERE DISP, \ THEN,
CPA L| \ compare with base
JRF NC'| HOLDPLACE \ if less than base, good
LD E'| A|
\ LDX HL| 1 NN,
SBCHL HL|
PUSH DE|
PUSH HL|
EXX
Next
HERE DISP, \ THEN,
HERE DISP, HERE DISP, \ THEN, THEN, \ ... (!!)
LDX HL| 0 NN,
PUSH HL|
EXX
Next
C;
\ uppercase routine
\ character in A is forced to Uppercase.
\ no other register is altered
ASSEMBLER
HERE TO upper^
NOP \ This location is patched
\ at runtime by CASEON and CASEOFF
CPN HEX 61 N, \ lower-case "a"
RETF CY|
CPN HEX 7B N, \ lower-case "z" + 1
RETF NC|
SUBN HEX 20 N, \ substract 20h if lowercase [a-z]
RET
.( CASEON )
\ set case-sensitivity on
\ it patches a RET at the beginning of the uppercase-routine
CODE caseon
LDN A'| HEX C9 N, \ patch for RET at upper^ location
LD()A upper^ AA,
Next
C;
.( CASEOFF )
\ set case-sensitivity off
\ it patches a NOP at the beginning of the uppercase-routine
CODE caseoff
LDN A'| HEX 00 N, \ patch for NOP at upper^ location
LD()A upper^ AA,
Next
C;
.( UPPER )
\ character on top of stack is forced to Uppercase.
CODE upper ( c1 -- c2 )
POP HL| \ char
LD A'| L|
CALL upper^ 1+ AA,
LD L'| A|
PUSH HL|
Next
C;
\ 6228h
." (FIND) "
\ vocabulary search,
\ - voc is starting word's NFA
\ - addr is the counted-string to be searched for
\ On success, it returns the CFA of found word, the first NFA byte
\ (which contains length and some flags) and a true flag.
\ On fail, a false flag (no more: leaves addr unchanged)
CODE (find) ( addr voc -- ff | cfa b tf )
EXX
\ now get vocabulary starting address (NFA)
POP DE| \ dictionary
HERE
POP HL| \ text to search
PUSH HL|
LDA(X) DE| \ save NFA length byte
EXAFAF \ for later use (!)
LDA(X) DE|
XORA (HL)|
ANDN HEX 3F N,
JRF NZ'| HOLDPLACE \ if same length then
HERE \ BEGIN,
INCX HL|
INCX DE|
LDA(X) DE|
\ case sensitive option
\ XORA (HL)|
\ case insensitive option
\ PUSH BC|
ANDN HEX 80 N, \ split A in msb and the rest.
LD B'| A| \ store msb in B and the rest in C
LDA(X) DE|
ANDN HEX 7F N,
CALL upper^ AA, \ uppercase routine
LD C'| A|
LD A'| (HL)|
CALL upper^ AA, \ uppercase routine
XORA C|
XORA B|
\ POP BC|
\ case option - end
ADDA A| \ ignore msb in compare
JRF NZ'| HOLDPLACE SWAP \ didn't match, jump (*)
JRF NC'| HOLDPLACE SWAP DISP,
\ loop until last byte msb is found set
\ that bit marks the ending char of this word
\ match found!
\ LDX HL| 5 NN, \ 5 for PFA (was before)
LDX HL| 3 NN, \ 3 for CFA
ADDHL DE|
EX(SP)HL
EXAFAF \ retrieve NFA byte (!)
LD E'| A|
LDN D'| 0 N,
LDX HL| -1 NN,
PUSH DE|
PUSH HL|
EXX
Next
HERE DISP, \ THEN, \ didn't match (*)
JRF CY'| HOLDPLACE SWAP \ not the end of word, jump (**)
HERE DISP, \ THEN,
HERE \ BEGIN, \ find LFA
INCX DE|
LDA(X) DE|
ADDA A|
JRF NC'| HOLDPLACE SWAP DISP,
\ loop until last byte msb is set
\ consume chars until the end of the word
HERE DISP, \ THEN, \ (**)
\ take LFA and use it
INCX DE|
EXDEHL
LD E'| (HL)|
INCX HL|
LD D'| (HL)|
LD A'| D|
ORA E|
JRF NZ'| HOLDPLACE SWAP DISP,
\ loop until end of vocabulary
POP HL| \ with this, it leaves addr unchanged
LDX HL| 0 NN,
PUSH HL|
EXX
Next
C;
\ 6276h
.( ENCLOSE )
\ starting from a, using delimiter c, determines the offsets:
\ n1 the first character non-delimiter
\ n2 the first delimiter after the text
\ n3 the first character non enclosed.
\ This procedure does not go beyond a 'nul' ASCII (0x00) that represents
\ an uncoditional delimiter.
\ Examples:
\ i: c c x x x c x -- 2 5 6
\ ii: c c x x x 'nul' -- 2 5 5
\ iii: c c 'nul' -- 2 3 2
CODE enclose ( a c -- a n1 n2 n3 )
HEX
EXX
POP DE| ( char )
POP HL| ( addr )
PUSH HL|
LD A'| E|
LDX DE| HEX FFFF NN,
DECX HL|
HERE \ BEGIN,
INCX HL|
INCX DE|
CPA (HL)|
JRF Z'| HOLDPLACE SWAP DISP, ( 1st non-delimiter )
\ UNTIL,
PUSH DE|
\ PUSH BC| \ save BC
LD C'| A| \ save a ( char )
LD A'| (HL)|
ANDA A| ( stops if null )
JRF NZ'| HOLDPLACE ( iii. no more character in string )
\ POP BC| \ retrieve BC
INCX DE|
PUSH DE|
DECX DE|
PUSH DE|
EXX
Next
HERE DISP, \ THEN,
HERE \ BEGIN,
LD A'| C|
INCX HL|
INCX DE|
CPA (HL)|
JRF NZ'| HOLDPLACE ( separator )
\ POP BC| \ retrieve BC
PUSH DE| ( i. first non enclosed )
INCX DE|
PUSH DE|
EXX
Next
HERE DISP, \ THEN,
LD A'| (HL)|
ANDA A|
JRF NZ'| HOLDPLACE SWAP DISP,
( ii. separator & terminator )
\ POP BC| \ retrieve BC
PUSH DE|
PUSH DE|
EXX
Next
C;
." (MAP) "
\ translate character c1 using mapping strings a2 and a2
\ if c1 is not present within string a1 then
\ c2 = c2 if it is not translated. n is the length of both a1 and a2.
CODE (map) ( a2 a1 n c1 -- c2 )
EXX
POP HL|
LD A'| L|
POP BC|
POP HL|
LD D'| B|
LD E'| C|
CPIR
POP HL|
JRF NZ'| HOLDPLACE
ADDHL DE|
DECX HL|
SBCHL BC|
LD A'| (HL)|
HERE DISP,
LD L'| A|
LDN H'| HEX 00 N,
PUSH HL|
EXX
Next
C;
." (COMPARE) "
\ this word performs a lexicographic compare of n bytes of text at address a1
\ with n bytes of text at address a2. It returns numeric a value:
\ 0 : if strings are equal
\ +1 : if string at a1 greater than string at a2
\ -1 : if string at a1 less than string at a2
\ strings can be 256 bytes in length at most.
CODE (compare) ( a1 a2 n -- b )
EXX
POP HL|
LD A'| L|
POP HL| \ string a2
POP DE| \ string a1
\ PUSH BC|
LD B'| A|
HERE \ begin
\ LDA(X) DE|
\ CPA (HL)|
LD A'| (HL)|
CALL upper^ AA, \ uppercase routine
LD C'| A|
LDA(X) DE|
CALL upper^ AA, \ uppercase routine
CPA C| \ compare both uppercased chars
INCX DE|
INCX HL|
JRF Z'| HOLDPLACE \ match
JRF CY'| HOLDPLACE
LDX HL| 1 NN,
JR HOLDPLACE SWAP HERE DISP, \ ELSE,
LDX HL| -1 NN,
HERE DISP, \ THEN,
PUSH HL|
EXX \ POP BC|
Next
HERE DISP, \ THEN,
DJNZ BACK, \ until
LDX HL| 0 NN,
PUSH HL|
EXX \ POP BC|
Next
C;
\ 62BDh
.( EMITC )
\ low level emit, calls ROM routine at #10 to send a character to
\ the the current channel (see SELECT to change stream-channel)
CODE emitc ( c -- )
POP HL|
LD A'| L|
HERE TO emitc^
PUSH BC|
PUSH DE|
PUSH IX|
\ PUSH IX|
\ EXX
\ POP HL|
\ EXX
RST 10| \ standard ROM current-channel print routine
POP IX|
POP DE|
POP BC|
LDN A'| HEX FF N,
LD()A HEX 5C8C AA, \ SCR-CT system variable
Next
C;
\ 6396h
.( CR )
\ sends a CR via EMITC.
CODE cr ( -- )
LDN A'| HEX 0D N,
JR emitc^ HERE 1 + - D,
C;
\ conversion table for (?EMIT)
HERE TO EMIT-A^
EMIT-N EMIT-N + ALLOT
HERE TO EMIT-C^