OSDN Git Service

v206
[fast-forth/master.git] / MSP430-FORTH / ANS_COMP.f
1 ; ------------------------------------------------------------------------------
2 ; ANS_COMP.f                               words complement to pass CORETEST.4th
3 ; ------------------------------------------------------------------------------
4
5 \ TARGET SELECTION (used by preprocessor GEMA to load \config\gema\TARGET.pat)
6 \ MSP_EXP430FR5739  MSP_EXP430FR5969    MSP_EXP430FR5994    MSP_EXP430FR6989
7 \ MSP_EXP430FR2433  MSP_EXP430FR4133    MSP_EXP430FR2355    CHIPSTICK_FR2433
8 \ MY_MSP430FR5738_1 MY_MSP430FR5738     MY_MSP430FR5948     MY_MSP430FR5948_1   
9 \ JMJ_BOX
10
11 \ REGISTERS USAGE
12 \ rDODOES to rEXIT must be saved before use and restored after
13 \ scratch registers Y to S are free for use
14 \ under interrupt, IP is free for use
15
16 \ PUSHM order : PSP,TOS, IP,  S,  T,  W,  X,  Y, rEXIT, rDOVAR, rDOCON, rDODOES
17 \ example : PUSHM #6,IP pushes IP,S,T,W,X,Y registers to return stack
18 \
19 \ POPM  order :  rDODOES, rDOCON, rDOVAR, rEXIT,  Y,  X,  W,  T,  S, IP,TOS,PSP
20 \ example : POPM #6,IP   pulls Y,X,W,T,S,IP registers from return stack
21
22 \ FORTH conditionnals:  unary{ 0= 0< 0> }, binary{ = < > U< }
23
24 \ ASSEMBLER conditionnal usage with IF UNTIL WHILE  S<  S>=  U<   U>=  0=  0<>  0>=
25
26 \ ASSEMBLER conditionnal usage with ?JMP ?GOTO      S<  S>=  U<   U>=  0=  0<>  <0
27
28     \
29 PWR_STATE
30     \
31 [DEFINED] {ANS_COMP} [IF] {ANS_COMP} [THEN] \ remove {ANS_COMP} if outside core  
32     \
33 [UNDEFINED] ASM [IF]
34 ECHO 
35 ASM \ assembler is required! 
36 [THEN]
37     \
38 [UNDEFINED] {ANS_COMP} [IF]
39
40 MARKER {ANS_COMP}
41     \
42
43 \ https://forth-standard.org/standard/core/AND
44 \ C AND    x1 x2 -- x3           logical AND
45 CODE AND
46 AND @PSP+,TOS
47 MOV @IP+,PC
48 ENDCODE
49     \
50
51 \ https://forth-standard.org/standard/core/OR
52 \ C OR     x1 x2 -- x3           logical OR
53 CODE OR
54 BIS @PSP+,TOS
55 MOV @IP+,PC
56 ENDCODE
57     \
58
59 \ https://forth-standard.org/standard/core/XOR
60 \ C XOR    x1 x2 -- x3           logical XOR
61 CODE XOR
62 XOR @PSP+,TOS
63 MOV @IP+,PC
64 ENDCODE
65     \
66
67 \ https://forth-standard.org/standard/core/INVERT
68 \ INVERT   x1 -- x2            bitwise inversion
69 CODE INVERT
70 XOR #-1,TOS
71 MOV @IP+,PC
72 ENDCODE
73     \
74
75 \ https://forth-standard.org/standard/core/LSHIFT
76 \ LSHIFT  x1 u -- x2    logical L shift u places
77 CODE LSHIFT
78             MOV @PSP+,W
79             AND #$1F,TOS        \ no need to shift more than 16
80 0<> IF
81     BEGIN   ADD W,W
82             SUB #1,TOS
83     0= UNTIL
84 THEN        MOV W,TOS
85             MOV @IP+,PC
86 ENDCODE
87     \
88
89 \ https://forth-standard.org/standard/core/RSHIFT
90 \ RSHIFT  x1 u -- x2    logical R7 shift u places
91 CODE RSHIFT
92             MOV @PSP+,W
93             AND #$1F,TOS       \ no need to shift more than 16
94 0<> IF
95     BEGIN   BIC #C,SR           \ Clr Carry
96             RRC W
97             SUB #1,TOS
98     0= UNTIL
99 THEN        MOV W,TOS
100             MOV @IP+,PC
101 ENDCODE
102     \
103
104 [UNDEFINED] MAX [IF]
105 \ https://forth-standard.org/standard/core/MAX
106 \ MAX    n1 n2 -- n3       signed maximum
107 CODE MAX
108     CMP @PSP,TOS    \ n2-n1
109     S<  ?GOTO FW1   \ n2<n1
110 BW1 ADD #2,PSP
111     MOV @IP+,PC
112 ENDCODE
113     \
114
115 \ https://forth-standard.org/standard/core/MIN
116 \ MIN    n1 n2 -- n3       signed minimum
117 CODE MIN
118     CMP @PSP,TOS    \ n2-n1
119     S< ?GOTO BW1    \ n2<n1
120 FW1 MOV @PSP+,TOS
121     MOV @IP+,PC
122 ENDCODE
123 [THEN]
124     \
125
126 \ https://forth-standard.org/standard/core/TwoTimes
127 \ 2*      x1 -- x2         arithmetic left shift
128 CODE 2*
129 ADD TOS,TOS            
130 MOV @IP+,PC            
131 ENDCODE
132     \
133
134 \ https://forth-standard.org/standard/core/TwoDiv
135 \ 2/      x1 -- x2        arithmetic right shift
136 CODE 2/
137 RRA TOS
138 MOV @IP+,PC
139 ENDCODE
140     \
141
142 \ --------------------
143 \ ARITHMETIC OPERATORS
144 \ --------------------
145
146 $1A04 C@ $EF > [IF] ; test tag value MSP430FR413x subfamily without hardware_MPY 
147     \
148 \ https://forth-standard.org/standard/core/MTimes
149 \ M*     n1 n2 -- dlo dhi  signed 16*16->32 multiply
150 CODE M*            
151 MOV @PSP,S          \ S= n1
152 CMP #0,S            \ n1 > -1 ?
153 S< IF
154     XOR #-1,0(PSP)  \ n1 --> u1
155     ADD #1,0(PSP)   \
156 THEN
157 XOR TOS,S           \ S contains sign of result
158 CMP #0,TOS          \ n2 > -1 ?
159 S< IF
160     XOR #-1,TOS     \ n2 --> u2 
161     ADD #1,TOS      \
162 THEN
163 \ PUSHM IP,S          \ UMSTAR use S,T,W,X,Y
164 PUSHM #2,IP         \ UMSTAR use S,T,W,X,Y
165 LO2HI               \ -- ud1 u2
166 UM*       
167 HI2LO
168 POPM #2,IP           \ pop S,IP
169 CMP #0,S            \ sign of result > -1 ?
170 S< IF
171     XOR #-1,0(PSP)  \ ud --> d
172     XOR #-1,TOS
173     ADD #1,0(PSP)
174     ADDC #0,TOS
175 THEN
176 MOV @IP+,PC
177 ENDCODE
178     \
179 [ELSE]              ; MSP430FRxxxx with hardware_MPY
180     \
181 \ https://forth-standard.org/standard/core/UMTimes
182 \ UM*     u1 u2 -- udlo udhi   unsigned 16x16->32 mult.
183 CODE UM*
184     MOV @PSP,&MPY       \ Load 1st operand for unsigned multiplication
185 BW1 MOV TOS,&OP2        \ Load 2nd operand
186     MOV &RES0,0(PSP)    \ low result on stack
187     MOV &RES1,TOS       \ high result in TOS
188     MOV @IP+,PC
189 ENDCODE
190     \
191
192 \ https://forth-standard.org/standard/core/MTimes
193 \ M*     n1 n2 -- dlo dhi  signed 16*16->32 multiply
194 CODE M*
195     MOV @PSP,&MPYS      \ Load 1st operand for signed multiplication
196     GOTO BW1
197 ENDCODE
198     \
199 [THEN]
200     \
201
202 HERE
203
204 \ https://forth-standard.org/standard/core/SMDivREM
205 \ SM/REM   d1lo d1hi n2 -- r3 q4  symmetric signed div
206 CODE SM/REM
207 MOV TOS,S           \           S=divisor
208 MOV @PSP,T          \           T=dividend_sign==>rem_sign
209 CMP #0,TOS          \           n2 >= 0 ?
210 S< IF               \
211     XOR #-1,TOS
212     ADD #1,TOS      \ -- d1 u2
213 THEN
214 CMP #0,0(PSP)       \           d1hi >= 0 ?
215 S< IF               \
216     XOR #-1,2(PSP)  \           d1lo
217     XOR #-1,0(PSP)  \           d1hi
218     ADD #1,2(PSP)   \           d1lo+1
219     ADDC #0,0(PSP)  \           d1hi+C
220 THEN                \ -- uDVDlo uDVDhi uDIVlo
221 \ PUSHM IP,T          \           save IP,S,T
222 PUSHM #3,IP         \           save IP,S,T
223 LO2HI
224     UM/MOD          \ -- uREMlo uQUOTlo
225 HI2LO
226 \ POPM T,IP           \           restore T,S,IP
227 POPM #3,IP          \           restore T,S,IP
228 CMP #0,T            \           T=rem_sign
229 S< IF
230     XOR #-1,0(PSP)
231     ADD #1,0(PSP)
232 THEN
233 XOR S,T             \           S=divisor T=quot_sign
234 CMP #0,T            \ -- n3 u4  T=quot_sign
235 S< IF
236 BW1
237 BW2
238     XOR #-1,TOS
239     ADD #1,TOS
240 THEN                \ -- n3 n4  S=divisor
241 MOV @IP+,PC
242 ENDCODE
243     \
244
245 HERE OVER - DUMP
246
247 \ https://forth-standard.org/standard/core/NEGATE
248 \ C NEGATE   x1 -- x2            two's complement
249 CODE NEGATE
250 GOTO BW1 
251 ENDCODE
252     \
253
254 \ https://forth-standard.org/standard/core/ABS
255 \ C ABS     n1 -- +n2     absolute value
256 CODE ABS
257 CMP #0,TOS       \  1
258 0< ?GOTO BW2
259 MOV @IP+,PC
260 ENDCODE
261     \
262
263 \ https://forth-standard.org/standard/core/FMDivMOD
264 \ FM/MOD   d1 n1 -- r q   floored signed div'n
265 : FM/MOD
266 SM/REM
267 HI2LO               \ -- remainder quotient       S=divisor
268 CMP #0,0(PSP)       \ remainder <> 0 ?
269 0<> IF
270     CMP #1,TOS      \ quotient < 1 ?
271     S< IF
272       ADD S,0(PSP)  \ add divisor to remainder
273       SUB #1,TOS    \ decrement quotient
274     THEN
275 THEN
276 MOV @RSP+,IP
277 MOV @IP+,PC
278 ENDCODE
279     \
280
281 \ https://forth-standard.org/standard/core/Times
282 \ *      n1 n2 -- n3       signed multiply
283 : *
284 M* DROP
285 ;
286     \
287
288 \ https://forth-standard.org/standard/core/DivMOD
289 \ /MOD   n1 n2 -- r3 q4     signed division
290 : /MOD
291 >R DUP 0< R> FM/MOD
292 ;
293     \
294
295 \ https://forth-standard.org/standard/core/Div
296 \ /      n1 n2 -- n3       signed quotient
297 : /
298 >R DUP 0< R> FM/MOD NIP
299 ;
300     \
301
302 \ https://forth-standard.org/standard/core/MOD
303 \ MOD    n1 n2 -- n3       signed remainder
304 : MOD
305 >R DUP 0< R> FM/MOD DROP
306 ;
307     \
308
309 \ https://forth-standard.org/standard/core/TimesDivMOD
310 \ */MOD  n1 n2 n3 -- r4 q5    signed mult/div
311 : */MOD
312 >R M* R> FM/MOD
313 ;
314     \
315
316 \ https://forth-standard.org/standard/core/TimesDiv
317 \ */     n1 n2 n3 -- n4        n1*n2/q3
318 : */
319 >R M* R> FM/MOD NIP
320 ;
321     \
322
323 \ ----------------------------------------------------------------------
324 \ DOUBLE OPERATORS
325 \ ----------------------------------------------------------------------
326
327 \ https://forth-standard.org/standard/core/StoD
328 \ S>D    n -- d          single -> double prec.
329 : S>D
330     DUP 0<
331 ;
332     \
333
334 \ https://forth-standard.org/standard/core/TwoFetch
335 \ 2@    a-addr -- x1 x2    fetch 2 cells ; the lower address will appear on top of stack
336 CODE 2@
337 SUB #2, PSP
338 MOV 2(TOS),0(PSP)
339 MOV @TOS,TOS
340 MOV @IP+,PC
341 ENDCODE
342     \
343
344 \ https://forth-standard.org/standard/core/TwoStore
345 \ 2!    x1 x2 a-addr --    store 2 cells ; the top of stack is stored at the lower adr
346 CODE 2!
347 MOV @PSP+,0(TOS)
348 MOV @PSP+,2(TOS)
349 MOV @PSP+,TOS
350 MOV @IP+,PC
351 ENDCODE
352     \
353
354 \ https://forth-standard.org/standard/core/TwoDUP
355 \ 2DUP   x1 x2 -- x1 x2 x1 x2   dup top 2 cells
356 CODE 2DUP
357 SUB #4,PSP          \ -- x1 x x x2
358 MOV TOS,2(PSP)      \ -- x1 x2 x x2
359 MOV 4(PSP),0(PSP)   \ -- x1 x2 x1 x2
360 MOV @IP+,PC
361 ENDCODE
362     \
363
364 \ https://forth-standard.org/standard/core/TwoDROP
365 \ 2DROP  x1 x2 --          drop 2 cells
366 CODE 2DROP
367 ADD #2,PSP
368 MOV @PSP+,TOS
369 MOV @IP+,PC
370 ENDCODE
371     \
372
373 \ https://forth-standard.org/standard/core/TwoSWAP
374 \ 2SWAP  x1 x2 x3 x4 -- x3 x4 x1 x2
375 CODE 2SWAP
376 MOV @PSP,W          \ -- x1 x2 x3 x4    W=x3
377 MOV 4(PSP),0(PSP)   \ -- x1 x2 x1 x4
378 MOV W,4(PSP)        \ -- x3 x2 x1 x4
379 MOV TOS,W           \ -- x3 x2 x1 x4    W=x4
380 MOV 2(PSP),TOS      \ -- x3 x2 x1 x2    W=x4
381 MOV W,2(PSP)        \ -- x3 x4 x1 x2
382 MOV @IP+,PC
383 ENDCODE
384     \
385
386 \ https://forth-standard.org/standard/core/TwoOVER
387 \ 2OVER  x1 x2 x3 x4 -- x1 x2 x3 x4 x1 x2
388 CODE 2OVER
389 SUB #4,PSP          \ -- x1 x2 x3 x x x4
390 MOV TOS,2(PSP)      \ -- x1 x2 x3 x4 x x4
391 MOV 8(PSP),0(PSP)   \ -- x1 x2 x3 x4 x1 x4
392 MOV 6(PSP),TOS      \ -- x1 x2 x3 x4 x1 x2
393 MOV @IP+,PC
394 ENDCODE
395     \
396
397 \ ----------------------------------------------------------------------
398 \ ALIGNMENT OPERATORS
399 \ ----------------------------------------------------------------------
400
401 \ https://forth-standard.org/standard/core/ALIGNED
402 \ ALIGNED  addr -- a-addr       align given addr
403 CODE ALIGNED
404 BIT #1,TOS
405 ADDC #0,TOS
406 MOV @IP+,PC
407 ENDCODE
408     \
409
410 \ https://forth-standard.org/standard/core/ALIGN
411 \ ALIGN    --                         align HERE
412 CODE ALIGN
413 BIT #1,&DP  \ 3
414 ADDC #0,&DP \ 4
415 MOV @IP+,PC
416 ENDCODE
417     \
418
419 \ ---------------------
420 \ PORTABILITY OPERATORS
421 \ ---------------------
422
423 \ https://forth-standard.org/standard/core/CHARS
424 \ CHARS    n1 -- n2            chars->adrs units
425 CODE CHARS
426 MOV @IP+,PC
427 ENDCODE
428     \
429
430 \ https://forth-standard.org/standard/core/CHARPlus
431 \ CHAR+    c-addr1 -- c-addr2   add char size
432 CODE CHAR+
433 ADD #1,TOS
434 MOV @IP+,PC
435 ENDCODE
436     \
437
438 \ https://forth-standard.org/standard/core/CELLS
439 \ CELLS    n1 -- n2            cells->adrs units
440 CODE CELLS
441 ADD TOS,TOS
442 MOV @IP+,PC
443 ENDCODE
444     \
445
446 \ https://forth-standard.org/standard/core/CELLPlus
447 \ CELL+    a-addr1 -- a-addr2      add cell size
448 CODE CELL+
449 ADD #2,TOS
450 MOV @IP+,PC
451 ENDCODE
452     \
453 \ ---------------------------
454 \ BLOCK AND STRING COMPLEMENT
455 \ ---------------------------
456
457 \ https://forth-standard.org/standard/core/CHAR
458 \ CHAR   -- char           parse ASCII character
459 : CHAR
460     BL WORD 1+ C@
461 ;
462
463 \ https://forth-standard.org/standard/core/BracketCHAR
464 \ [CHAR]   --          compile character literal
465 : [CHAR]
466     CHAR lit lit , ,
467 ; IMMEDIATE
468
469     \
470
471 \ https://forth-standard.org/standard/core/PlusStore
472 \ +!     n/u a-addr --       add n/u to memory
473 CODE +!
474 ADD @PSP+,0(TOS)
475 MOV @PSP+,TOS
476 MOV @IP+,PC
477 ENDCODE
478     \ 
479
480 \ https://forth-standard.org/standard/core/FILL
481 \ FILL   c-addr u char --  fill memory with char
482 CODE FILL
483 MOV @PSP+,X     \ count
484 MOV @PSP+,W     \ address
485 CMP #0,X
486 0<> IF
487     BEGIN
488         MOV.B TOS,0(W)    \ store char in memory
489         ADD #1,W
490         SUB #1,X
491     0= UNTIL
492 THEN
493 MOV @PSP+,TOS     \ empties stack
494 MOV @IP+,PC
495 ENDCODE
496     \ 
497
498 \ --------------------
499 \ INTERPRET COMPLEMENT
500 \ --------------------
501
502 \ https://forth-standard.org/standard/core/HEX
503 CODE HEX
504 MOV #$10,&BASE
505 MOV @IP+,PC
506 ENDCODE
507     \
508
509 \ https://forth-standard.org/standard/core/DECIMAL
510 CODE DECIMAL
511 MOV #$0A,&BASE
512 MOV @IP+,PC
513 ENDCODE
514     \
515
516 \ https://forth-standard.org/standard/core/p
517 \ (         --          skip input until char ) or EOL
518 : ( 
519 $29 WORD DROP
520 ; IMMEDIATE
521     \
522
523 [DEFINED] CAPS_ON [IF]
524     \
525 \ https://forth-standard.org/standard/core/Dotp
526 \ .(        --          type comment immediatly.
527 : .(
528 CAPS_OFF
529 $29 WORD
530 COUNT TYPE
531 CAPS_ON
532 ; IMMEDIATE
533     \
534 [ELSE]
535 \ https://forth-standard.org/standard/core/Dotp
536 \ .(        --          type comment immediatly.
537 : .(
538 $29 WORD
539 COUNT TYPE
540 ; IMMEDIATE
541     \
542 [THEN]
543     \
544
545 \ https://forth-standard.org/standard/core/SOURCE
546 \ SOURCE    -- adr u    of current input buffer
547 CODE SOURCE
548 SUB #4,PSP
549 MOV TOS,2(PSP)
550 MOV &SOURCE_LEN,TOS
551 MOV &SOURCE_ADR,0(PSP)
552 MOV @IP+,PC
553 ENDCODE
554     \
555
556 \ https://forth-standard.org/standard/core/toIN
557 \ C >IN     -- a-addr       holds offset in input stream
558 TOIN CONSTANT >IN
559     \
560
561 [UNDEFINED] PAD [IF]
562
563 \ https://forth-standard.org/standard/core/PAD
564 \  PAD           --  addr
565 PAD_ORG CONSTANT PAD
566
567 [THEN]
568
569     \
570 RST_HERE
571 [THEN]
572
573     \