OSDN Git Service

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