OSDN Git Service

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