-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforth.s
5525 lines (5050 loc) · 95.1 KB
/
forth.s
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
/**************************************************************************
Copyright Jacques Deschênes 2021,2022
This file is part of beyond-Jupiter
beyond-Jupiter is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
beyond-Jupiter is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with beyond-Jupiter. If not, see <http://www.gnu.org/licenses/>.
***************************************************************************/
/*****************************************************
* STM32eForth version 7.20
* Adapted to blue pill board by Picatout
* date: 2020-11-22
* IMPLEMENTATION NOTES:
*
* This version use indirect threaded model. This model enable
* leaving the core Forth system in FLASH memory while the users
* definitions reside in RAM.
* Use USART1 for console I/O
* port config: 115200 8N1
* TX on PA9, RX on PA10
*
******************************************************/
/***********************************************************
* STM32eForth version 7.20
* Chen-Hanson Ting, July 2014
* Subroutine Threaded Forth Model
* Adapted to STM32F407-Discovery Board
* Assembled by Keil uVision 5.10
* Version 4.03
* Direct Threaded Forth Model
* Derived from 80386 eForth versin 4.02
* and Chien-ja Wu's ARM7 eForth version 1.01
* Version 5.02, 09oct04cht
* fOR ADuC702x from Analog Devices
* Version 6.01, 10apr08cht a
* .p2align 2 to at91sam7x256
* Tested on Olimax SAM7-EX256 Board with LCD display
* Running under uVision3 RealView from Keil
* Version 7.01, 29jun14cht
* Ported to STM32F407-Discovery Board, under uVision 5.10
* .p2aligned to eForth 2 Model
* Assembled to flash memory and executed therefrom.
* Version 7.10, 30jun14cht
* Flash memory mapped to Page 0 where codes are executed
* Version 7.20, 02jul14cht
* Irreducible Complexity
* Code copied from flash to RAM, RAM mapped to Page 0.
* TURNKEY saves current application from RAM to flash.
*********************************************************/
.syntax unified
.cpu cortex-m4
.fpu vfpv4
.thumb
.include "stm32f411ce.inc"
.section .text, "ax", %progbits
/***********************************
Start of eForth system
***********************************/
.p2align 2
// hi level word enter
NEST:
STMFD RSP!,{IP} // save return address
ADD IP,WP,#3
// inner interprer
INEXT:
LDR WP,[IP],#4
BX WP
UNNEST: // exit hi level word
LDMFD RSP!,{IP}
LDR WP,[IP],#4
BX WP
.p2align 2
// compile "BX INX\nNOP.N "
// this is the only way
// a colon defintion in RAM
// can jump to NEST
// INX register is initialized
// to NEST address
// and must be preserved
COMPI_NEST:
add T1,UP,#USER_CTOP // pointer HERE
ldr T1,[T1] // address in here
mov T2,#0x4700+(10<<3) // binary code for BX INX
strh T2,[T1],#2 // store code at HERE, ptr+2
mov T2,#0xbf00 // NOP.N instruction
strh T2,[T1],#2 // store code at HERE, ptr+2
add T2,UP,#USER_CTOP
str T1,[T2] // save update HERE value
_NEXT
// ' STDIN
// stdin vector
TSTDIN:
_PUSH
ADD TOS,UP,#STDIN
_NEXT
// ' STDOUT
// stdout vector
TSTDOUT:
_PUSH
ADD TOS,UP,#STDOUT
_NEXT
/***************************
CFSR ( -- u )
stack CFSR register
***************************/
_HEADER CFSR,4,"CFSR"
_MOV32 T0,SCB_BASE_ADR
_PUSH
ldr TOS,[T0,#SCB_CFSR]
eor T1,T1
str T1,[T0,#SCB_CFSR]
_NEXT
/*****************************
BFAR ( -- u )
stack BFAR register
*****************************/
_HEADER BFAR,4,"BFAR"
_MOV32 T0,SCB_BASE_ADR
_PUSH
ldr TOS,[T0,#SCB_BFAR]
eor T1,T1
str T1,[T0,#SCB_BFAR]
_NEXT
/********************************************
KEY? ( -- c T | F )
check if available character
********************************************/
_HEADER QKEY,4,"KEY?"
_NEST
_ADR TSTDIN // ' STDIN
_ADR ATEXE
_UNNEST
/********************************************
KEY ( -- c )
Wait for and return an input character.
********************************************/
_HEADER KEY,3,"KEY"
_NEST
KEY1:
_ADR CAPS_LED
_ADR QKEY
_QBRAN KEY1
/* add this code to filter out control characters
_ADR DUPP
_DOLIT 13
_ADR EQUAL
_TBRAN KEY2
_ADR DUPP
_DOLIT 32
_ADR LESS
_QBRAN KEY2
_ADR DROP
_BRAN KEY1
*/
KEY2:
_UNNEST
/**********************************************
EMIT ( c -- )
transmit a character to console
**********************************************/
_HEADER EMIT,4,"EMIT"
_NEST
_ADR TSTDOUT
_ADR ATEXE
_UNNEST
/************************************************
GET-IP ( n - c )
return interrupt priority of IRQn
************************************************/
/*
_HEADER GETIP,6,"GET-IP"
_NEST
_ADR DUPP
_ADR ZLESS
_QBRAN 1f
_DOLIT 15
_ADR ANDD
_DOLIT 4
_ADR SUBB
_DOLIT 0xE000ED18
_BRAN 2f
1: _DOLIT 0xE000E400
2: _ADR PLUS
_ADR CAT
_DOLIT 4
_ADR RSHIFT
_UNNEST
*/
/***********************************************
RANDOM ( n+ -- {0..n+ - 1} )
return pseudo random number
REF: https://en.wikipedia.org/wiki/Xorshift
************************************************/
_HEADER RAND,6,"RANDOM"
_NEST
_ADR ABSS
_ADR SEED
_ADR AT
_ADR DUPP
_DOLIT 13
_ADR LSHIFT
_ADR XORR
_ADR DUPP
_DOLIT 17
_ADR RSHIFT
_ADR XORR
_ADR DUPP
_DOLIT 5
_ADR LSHIFT
_ADR XORR
_ADR DUPP
_ADR SEED
_ADR STORE
_DOLIT 0x7FFFFFFF
_ADR ANDD
_ADR SWAP
_ADR MODD
_UNNEST
/****************************************
MS ( u -- )
suspend execution for u milliseconds
****************************************/
_HEADER PAUSE,2,"MS"
_NEST
_ADR TIMER
_ADR STORE
PAUSE_LOOP:
_ADR TIMER
_ADR AT
_QBRAN PAUSE_EXIT
_BRAN PAUSE_LOOP
PAUSE_EXIT:
_UNNEST
/******************************************
ULED ( T|F -- )
control user LED, -1 ON, 0 OFF
*******************************************/
_HEADER ULED,4,"ULED"
mov T0,#(1<<LED_PIN)
_MOV32 T1,LED_GPIO
movs TOS,TOS
_POP
beq ULED_OFF
lsl T0,#16
str T0,[T1,#GPIO_BSRR]
_NEXT
ULED_OFF:
str T0,[T1,#GPIO_BSRR]
_NEXT
/**************************
JOYSTK ( -- u )
read joystick port
**************************/
_HEADER JOYSTK,6,"JOYSTK"
_NEST
_DOLIT (GPIOA_BASE_ADR+GPIO_IDR)
_ADR AT
_DOLIT 0x100f
_ADR ANDD
_UNNEST
/****************************
BEEP ( msec freq -- )
input:
freq frequence hertz
msec durration in msec
*****************************/
_HEADER BEEP,4,"BEEP"
_MOV32 r0,6000000 // Fclk
udiv r0,r0,TOS
_POP
_MOV32 r1,TIM4_BASE_ADR
str r0,[r1,#TIM_ARR]
lsr r0,#1
str r0,[r1,#TIM_CCR1]
mov r0,#1
str r0,[r1,#TIM_CCER]
str r0,[r1,#TIM_CR1]
ldr r0,[r1,#TIM_DIER]
str TOS,[UP,#BEEP_DTMR]
_POP
_NEXT
/***************
// The kernel
***************/
/********************
NOP ( -- )
do nothing.
*********************/
_HEADER NOP,3,"NOP"
_NEXT
/********************
doLIT ( -- w )
Push an inline literal.
hidden word used by compiler
*********************/
DOLIT:
_PUSH // store TOS on data stack
LDR TOS,[IP],#4 // get literal at word boundary
_NEXT
/*******************************
EXECUTE ( ca -- )
Execute the word at ca.
*******************************/
_HEADER EXECU,7,"EXECUTE"
ORR WP,TOS,#1
_POP
BX WP
_NEXT
/**********************************************************
donext ( -- ) counter on R:
Run time code for the single index loop.
: next ( -- ) \ hilevel model
r> r> dup if 1 - >r @ >r exit then drop cell+ >r //
hidden word used by compiler
*********************************************************/
DONXT:
LDR T2,[RSP] // ( -- u )
CBNZ T2,NEXT1
/* loop done */
ADD RSP,RSP,#4 // drop counter
ADD IP,IP,#4 // skip after loop address
_NEXT
NEXT1:
/* decrement loop counter */
SUB T2,T2,#1
STR T2,[RSP]
LDR IP,[IP] // go begining of loop
_NEXT
/**************************************
?branch ( f -- )
Branch if flag is zero.
hiddend word used by compiler
**************************************/
QBRAN:
MOVS TOS,TOS
_POP
BNE QBRAN1
LDR IP,[IP]
_NEXT
QBRAN1:
ADD IP,IP,#4
_NEXT
/***********************************
tbranch ( f -- )
branch if flag is true
***********************************/
TBRAN:
MOVS TOS,TOS
_POP
BEQ 1f
LDR IP,[IP]
_NEXT
1: ADD IP,IP,#4
_NEXT
/***********************************
branch ( -- )
Branch to an inline address.
hidden word used by compiler
***********************************/
BRAN:
LDR IP,[IP]
_NEXT
/******************************************
EXIT ( -- )
Exit the currently executing command.
******************************************/
_HEADER EXIT,4,"EXIT"
b UNNEST
/***********************************
! ( w a -- )
Pop the data stack to memory.
************************************/
_HEADER STORE,1,"!"
LDR WP,[DSP],#4
STR WP,[TOS]
_POP
_NEXT
/********************************************
@ ( a -- w )
Push memory location to the data stack.
*********************************************/
_HEADER AT,1,"@"
LDR TOS,[TOS]
_NEXT
/*******************************************
C! ( c b -- )
Pop the data stack to byte memory.
*******************************************/
_HEADER CSTOR,2,"C!"
LDR WP,[DSP],#4
STRB WP,[TOS]
_POP
_NEXT
/*********************************************
C@ ( b -- c )
Push byte memory location to the data stack.
**********************************************/
_HEADER CAT,2,"C@"
LDRB TOS,[TOS]
_NEXT
/*********************************************
R> ( -- w R: w -- )
push from rstack.
**********************************************/
_HEADER RFROM,2,"R>"
_PUSH
LDR TOS,[RSP],#4
_NEXT
/***********************************************
2R> ( -- x1 x2 ) R: x1 x2 --
push a double from rstack
***********************************************/
_HEADER DRFROM,3,"2R>"
_PUSH
LDR TOS,[RSP,#CELLL]
_PUSH
LDR TOS,[RSP]
ADD RSP,#2*CELLL
_NEXT
/************************************************
R@ ( -- w )
Copy top of return stack to the data stack.
************************************************/
_HEADER RAT,2,"R@"
_PUSH
LDR TOS,[RSP]
_NEXT
/***********************************************
2R@ ( -- x2 x1 ) ( R: x1 x2 -- )
Copy 2 element of return stack to data stack
***********************************************/
_HEADER DRAT,3,"2R@"
_PUSH
LDR TOS,[RSP,#CELLL]
_PUSH
LDR TOS,[RSP]
_NEXT
/***********************************************
>R ( w -- ) R: -- w
pop to rstack.
************************************************/
_HEADER TOR,2,">R"
STR TOS,[RSP,#-CELLL]!
_POP
_NEXT
/*********************************************
2>R ( x1 x2 -- ) R: -- x1 x2
pop a double to rstack
*********************************************/
_HEADER DTOR,3,"2>R"
LDR T0, [DSP],#CELLL
STR T0,[RSP,#-CELLL]!
STR TOS,[RSP,#-CELLL]!
_POP
_NEXT
/*******************************
// RP! ( u -- )
// initialize RPP with u
*******************************/
_HEADER RPSTOR,3,"RP!"
MOV RSP,TOS
_POP
_NEXT
/********************************
SP! ( u -- )
initialize SPP with u
********************************/
_HEADER SPSTOR,3,"SP!"
MOV DSP,TOS
EOR TOS,TOS,TOS
_NEXT
/**************************************
SP@ ( -- a )
Push the current data stack pointer.
***************************************/
_HEADER SPAT,3,"SP@"
_PUSH
MOV TOS,DSP
_NEXT
/**************************************
RP@ ( -- a )
push current rstack pointer
**************************************/
_HEADER RPAT,3,"RP@"
_PUSH
MOV TOS,RSP
_NEXT
/********************************
DROP ( w -- )
Discard top stack item.
********************************/
_HEADER DROP,4,"DROP"
_POP
_NEXT
/*********************************
CORE EXTENSION
NIP ( x1 x2 -- x2 )
throw next element
*********************************/
_HEADER NIP,3,"NIP"
ADD DSP,#CELLL
_NEXT
/*********************************
DUP ( w -- w w )
Duplicate the top stack item.
*********************************/
_HEADER DUPP,3,"DUP"
_PUSH
_NEXT
/**********************************
SWAP ( w1 w2 -- w2 w1 )
Exchange top two stack items.
**********************************/
_HEADER SWAP,4,"SWAP"
LDR WP,[DSP]
STR TOS,[DSP]
MOV TOS,WP
_NEXT
/***********************************
2SWAP ( d2 d1 -- d1 d2 )
swap double integer
***************************************/
_HEADER DSWAP,5,"2SWAP"
mov T0,TOS
ldr T1,[DSP]
ldr TOS,[DSP,#4]
ldr WP,[DSP,#8]
str WP,[DSP]
str T0,[DSP,#4]
str T1,[DSP,#8]
_NEXT
/***********************************
OVER ( w1 w2 -- w1 w2 w1 )
Copy second stack item to top.
***********************************/
_HEADER OVER,4,"OVER"
_PUSH
LDR TOS,[DSP,#4]
_NEXT
/***********************************
2OVER ( d2 d1 -- d2 d1 d2 )
copy a double integer to TOS
**********************************************/
_HEADER DOVER,5,"2OVER"
ldr T0,[DSP,#4]
ldr WP,[DSP,#8]
_PUSH
mov TOS,WP
_PUSH
mov TOS,T0
_NEXT
/***********************************
0< ( n -- t )
Return true if n is negative.
***********************************/
_HEADER ZLESS,2,"0<"
ASR TOS,#31
_NEXT
/**********************************
0> ( n -- flag )
true if n > 0
**********************************/
_HEADER ZGREAT,2,"0>"
CBZ TOS, 1f
ASR TOS,#31
MVN TOS,TOS
1: _NEXT
/**********************************
0<> ( n -- flag )
true if n <> 0
*********************************/
_HEADER ZNEQU,3,"0<>"
CBZ TOS,1f
MOV TOS,#-1
1: _NEXT
/*********************************
<> ( x1 x2 -- flag )
true fi x1 <> x2
********************************/
_HEADER NEQU,2,"<>"
LDR T0,[DSP],#CELLL
EORS TOS,T0
BEQ 1f
MOV TOS,#-1
1: _NEXT
/********************************
AND ( w w -- w )
Bitwise AND.
********************************/
_HEADER ANDD,3,"AND"
LDR WP,[DSP],#4
AND TOS,TOS,WP
_NEXT
/******************************
OR ( w w -- w )
Bitwise inclusive OR.
******************************/
_HEADER ORR,2,"OR"
LDR WP,[DSP],#4
ORR TOS,TOS,WP
_NEXT
/*****************************
XOR ( w w -- w )
Bitwise exclusive OR.
*****************************/
_HEADER XORR,3,"XOR"
LDR WP,[DSP],#4
EOR TOS,TOS,WP
_NEXT
/**************************************************
UM+ ( w w -- w cy )
Add two numbers, return the sum and carry flag.
***************************************************/
_HEADER UPLUS,3,"UM+"
LDR WP,[DSP]
ADDS WP,WP,TOS
MOV TOS,#0
ADC TOS,TOS,#0
STR WP,[DSP]
_NEXT
/*********************************
RSHIFT ( w # -- w )
logical Right shift # bits.
**********************************/
_HEADER RSHIFT,6,"RSHIFT"
LDR WP,[DSP],#4
MOV TOS,WP,LSR TOS
_NEXT
/****************************
LSHIFT ( w # -- w )
left shift # bits.
****************************/
_HEADER LSHIFT,6,"LSHIFT"
LDR WP,[DSP],#4
MOV TOS,WP,LSL TOS
_NEXT
/*************************
+ ( w w -- w )
Add.
*************************/
_HEADER PLUS,1,"+"
LDR WP,[DSP],#4
ADD TOS,TOS,WP
_NEXT
/************************
- ( w w -- w )
Subtract.
************************/
_HEADER SUBB,1,"-"
LDR WP,[DSP],#4
RSB TOS,TOS,WP
_NEXT
/************************
* ( w w -- w )
Multiply.
***********************/
_HEADER STAR,1,"*"
LDR WP,[DSP],#4
MUL TOS,WP,TOS
_NEXT
/***************************
UM* ( w w -- ud )
Unsigned multiply.
****************************/
_HEADER UMSTA,3,"UM*"
LDR WP,[DSP]
UMULL T2,T3,TOS,WP
STR T2,[DSP]
MOV TOS,T3
_NEXT
/***************************
M* ( w w -- d )
signed multiply.
hold double result
***************************/
_HEADER MSTAR,2,"M*"
LDR WP,[DSP]
SMULL T2,T3,TOS,WP
STR T2,[DSP]
MOV TOS,T3
_NEXT
/***************************
1+ ( w -- w+1 )
Add 1.
***************************/
_HEADER ONEP,2,"1+"
ADD TOS,TOS,#1
_NEXT
/***************************
1- ( w -- w-1 )
Subtract 1.
***************************/
_HEADER ONEM,2,"1-"
SUB TOS,TOS,#1
_NEXT
/***************************
2+ ( w -- w+2 )
Add 2.
**************************/
_HEADER TWOP,2,"2+"
ADD TOS,TOS,#2
_NEXT
/**************************
2- ( w -- w-2 )
Subtract 2.
**************************/
_HEADER TWOM,2,"2-"
SUB TOS,TOS,#2
_NEXT
/***************************
CELL+ ( w -- w+4 )
Add CELLL.
***************************/
_HEADER CELLP,5,"CELL+"
ADD TOS,TOS,#CELLL
_NEXT
/***************************
CELL- ( w -- w-4 )
Subtract CELLL.
**************************/
_HEADER CELLM,5,"CELL-"
SUB TOS,TOS,#CELLL
_NEXT
/****************************
BL ( -- 32 )
Blank (ASCII space).
*****************************/
_HEADER BLANK,2,"BL"
_PUSH
MOV TOS,#32
_NEXT
/**************************
CELLS ( w -- w*4 )
Multiply CELLL
***************************/
_HEADER CELLS,5,"CELLS"
LSL TOS,#2
_NEXT
/***************************
CELL/ ( w -- w/4 )
Divide by CELLL.
***************************/
_HEADER CELLSL,5,"CELL/"
ASR TOS,#2
_NEXT
/*************************
2* ( w -- w*2 )
Multiply 2.
*************************/
_HEADER TWOST,2,"2*"
MOV TOS,TOS,LSL#1
_NEXT
/*************************
2/ ( w -- w/2 )
Divide by 2.
***********************/
_HEADER TWOSL,2,"2/"
MOV TOS,TOS,ASR#1
_NEXT
/****************************
?DUP ( w -- w w | 0 )
Conditional duplicate.
*****************************/
_HEADER QDUP,4,"?DUP"
MOVS WP,TOS
IT NE
STRNE TOS,[DSP,#-4]!
_NEXT
/************************************
CORE EXTENSION
TUCK ( x1 x2 -- x2 x1 x2 )
***********************************/
_HEADER TUCK,4,"TUCK"
_PUSH
LDR TOS, [DSP,#CELLL]
_NEXT
/************************************
CORE EXTENSION
ROLL ( Xu Xu-1 .. X0 u -- Xu-1 ... X0 Xu )
**********************************************/
_HEADER ROLL,4,"ROLL"
_NEST
_ADR QDUP
_QBRAN 9f
_ADR DUPP
_DOLIT 1
_ADR EQUAL
_QBRAN 1f
_ADR DROP
_ADR SWAP
_BRAN 9f
1: _ADR TOR
_ADR RAT
_ADR PICK
_ADR SPAT
_ADR DUPP
_ADR CELLP
_ADR RFROM
_ADR ONEP
_ADR CELLS
_ADR WMOVE
_ADR DROP
9: _UNNEST
/*
MOV T0, TOS
CBZ T0, 9f
MOV T1, T0 // COUNTER
SUBB T0, #1
SLL T0, #2
_POP
MOV T2, TOS
LDR TOS,[DSP,T0]
MOV T2, [DSP,T0]
SUBS T1,#1
BEQ 9f
9: _NEXT
*/
/***********************************
ROT ( w1 w2 w3 -- w2 w3 w1 )
Rotate top 3 items.
*************************************/
_HEADER ROT,3,"ROT"
LDR T0,[DSP] // w2
STR TOS,[DSP] // w3 replace w2
LDR TOS,[DSP,#4] // w1 replace w3
STR T0,[DSP,#4] // w2 rpelace w1
_NEXT
/*********************************
-ROT ( w1 w2 w3 -- w3 w1 w2 )
left rotate top 3 elements
********************************/
_HEADER NROT,4,"-ROT"
LDR T0,[DSP,#4]
STR TOS,[DSP,#4]
LDR TOS,[DSP]
STR T0,[DSP]
_NEXT
/*********************************
2DROP ( w1 w2 -- )
Drop top 2 items.
*********************************/
_HEADER DDROP,5,"2DROP"
_POP
_POP
_NEXT
/********************************
3DROP ( w1 w2 w3 -- )
drop top 3 items
********************************/
_HEADER TDROP,5,"3DROP"
add DSP,#8
_POP
_NEXT
/***********************************
2DUP ( w1 w2 -- w1 w2 w1 w2 )
Duplicate top 2 items.
************************************/
_HEADER DDUP,4,"2DUP"
LDR T0,[DSP] // w1
STR TOS,[DSP,#-4]! // push w2
STR T0,[DSP,#-4]! // push w1
_NEXT
/******************************
D+ ( d1 d2 -- d3 )
Add top 2 double numbers.
******************************/
_HEADER DPLUS,2,"D+"
LDR WP,[DSP],#4
LDR T2,[DSP],#4
LDR T3,[DSP]
ADDS WP,WP,T3
STR WP,[DSP]
ADC TOS,TOS,T2
_NEXT
/******************************
DABS ( d -- ud )
absolute value double
*****************************/
_HEADER DABS,4,"DABS"
tst TOS,#(1<<31)
beq 9f
mvn TOS,TOS