OSDN Git Service

added the line number when an error occurs
[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/UMDivMOD
176 \ UM/MOD   udlo|udhi u1 -- r q   unsigned 32/16->16
177 CODE UM/MOD
178     CALL #MU/MOD  \ -- REMlo QUOTlo QUOThi
179     MOV @PSP+,TOS
180     MOV @IP+,PC
181 ENDCODE
182     \
183
184 \ https://forth-standard.org/standard/core/SMDivREM
185 \ SM/REM   d1lo d1hi n2 -- r3 q4  symmetric signed div
186 CODE SM/REM
187 MOV TOS,S           \           S=divisor
188 MOV @PSP,T          \           T=dividend_sign==>rem_sign
189 CMP #0,TOS          \           n2 >= 0 ?
190 S< IF               \
191     XOR #-1,TOS
192     ADD #1,TOS      \ -- d1 u2
193 THEN
194 CMP #0,0(PSP)       \           d1hi >= 0 ?
195 S< IF               \
196     XOR #-1,2(PSP)  \           d1lo
197     XOR #-1,0(PSP)  \           d1hi
198     ADD #1,2(PSP)   \           d1lo+1
199     ADDC #0,0(PSP)  \           d1hi+C
200 THEN                \ -- uDVDlo uDVDhi uDIVlo
201 PUSHM S,T           \
202 CALL #MU/MOD        \ -- uREMlo uQUOTlo uQUOThi
203 MOV @PSP+,TOS       \ -- uREMlo uQUOTlo
204 POPM T,S            \
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     XOR #-1,TOS
214     ADD #1,TOS
215 THEN                \ -- n3 n4  S=divisor
216 MOV @IP+,PC
217 ENDCODE
218     \
219
220 \ https://forth-standard.org/standard/core/FMDivMOD
221 \ FM/MOD   d1 n1 -- r q   floored signed div'n
222 : FM/MOD
223 SM/REM
224 HI2LO               \ -- remainder quotient       S=divisor
225 CMP #0,0(PSP)       \ remainder <> 0 ?
226 0<> IF
227     CMP #1,TOS      \ quotient < 1 ?
228     S< IF
229       ADD S,0(PSP)  \ add divisor to remainder
230       SUB #1,TOS    \ decrement quotient
231     THEN
232 THEN
233 MOV @RSP+,IP
234 MOV @IP+,PC
235 ENDCODE
236     \
237
238 \ https://forth-standard.org/standard/core/Times
239 \ *      n1 n2 -- n3       signed multiply
240 : *
241 M* DROP
242 ;
243     \
244
245 \ https://forth-standard.org/standard/core/DivMOD
246 \ /MOD   n1 n2 -- r3 q4     signed division
247 : /MOD
248 >R DUP 0< R> FM/MOD
249 ;
250     \
251
252 \ https://forth-standard.org/standard/core/Div
253 \ /      n1 n2 -- n3       signed quotient
254 : /
255 >R DUP 0< R> FM/MOD NIP
256 ;
257     \
258
259 \ https://forth-standard.org/standard/core/MOD
260 \ MOD    n1 n2 -- n3       signed remainder
261 : MOD
262 >R DUP 0< R> FM/MOD DROP
263 ;
264     \
265
266 \ https://forth-standard.org/standard/core/TimesDivMOD
267 \ */MOD  n1 n2 n3 -- r4 q5    signed mult/div
268 : */MOD
269 >R M* R> FM/MOD
270 ;
271     \
272
273 \ https://forth-standard.org/standard/core/TimesDiv
274 \ */     n1 n2 n3 -- n4        n1*n2/q3
275 : */
276 >R M* R> FM/MOD NIP
277 ;
278     \
279
280 \ ----------------------------------------------------------------------
281 \ DOUBLE OPERATORS
282 \ ----------------------------------------------------------------------
283
284 \ https://forth-standard.org/standard/core/StoD
285 \ S>D    n -- d          single -> double prec.
286 : S>D
287     DUP 0<
288 ;
289     \
290
291 \ https://forth-standard.org/standard/core/TwoFetch
292 \ 2@    a-addr -- x1 x2    fetch 2 cells ; the lower address will appear on top of stack
293 CODE 2@
294 SUB #2, PSP
295 MOV 2(TOS),0(PSP)
296 MOV @TOS,TOS
297 MOV @IP+,PC
298 ENDCODE
299     \
300
301 \ https://forth-standard.org/standard/core/TwoStore
302 \ 2!    x1 x2 a-addr --    store 2 cells ; the top of stack is stored at the lower adr
303 CODE 2!
304 MOV @PSP+,0(TOS)
305 MOV @PSP+,2(TOS)
306 MOV @PSP+,TOS
307 MOV @IP+,PC
308 ENDCODE
309     \
310
311 \ https://forth-standard.org/standard/core/TwoDUP
312 \ 2DUP   x1 x2 -- x1 x2 x1 x2   dup top 2 cells
313 CODE 2DUP
314 SUB #4,PSP          \ -- x1 x x x2
315 MOV TOS,2(PSP)      \ -- x1 x2 x x2
316 MOV 4(PSP),0(PSP)   \ -- x1 x2 x1 x2
317 MOV @IP+,PC
318 ENDCODE
319     \
320
321 \ https://forth-standard.org/standard/core/TwoDROP
322 \ 2DROP  x1 x2 --          drop 2 cells
323 CODE 2DROP
324 ADD #2,PSP
325 MOV @PSP+,TOS
326 MOV @IP+,PC
327 ENDCODE
328     \
329
330 \ https://forth-standard.org/standard/core/TwoSWAP
331 \ 2SWAP  x1 x2 x3 x4 -- x3 x4 x1 x2
332 CODE 2SWAP
333 MOV @PSP,W          \ -- x1 x2 x3 x4    W=x3
334 MOV 4(PSP),0(PSP)   \ -- x1 x2 x1 x4
335 MOV W,4(PSP)        \ -- x3 x2 x1 x4
336 MOV TOS,W           \ -- x3 x2 x1 x4    W=x4
337 MOV 2(PSP),TOS      \ -- x3 x2 x1 x2    W=x4
338 MOV W,2(PSP)        \ -- x3 x4 x1 x2
339 MOV @IP+,PC
340 ENDCODE
341     \
342
343 \ https://forth-standard.org/standard/core/TwoOVER
344 \ 2OVER  x1 x2 x3 x4 -- x1 x2 x3 x4 x1 x2
345 CODE 2OVER
346 SUB #4,PSP          \ -- x1 x2 x3 x x x4
347 MOV TOS,2(PSP)      \ -- x1 x2 x3 x4 x x4
348 MOV 8(PSP),0(PSP)   \ -- x1 x2 x3 x4 x1 x4
349 MOV 6(PSP),TOS      \ -- x1 x2 x3 x4 x1 x2
350 MOV @IP+,PC
351 ENDCODE
352     \
353
354
355 \ ----------------------------------------------------------------------
356 \ ALIGNMENT OPERATORS
357 \ ----------------------------------------------------------------------
358
359 \ https://forth-standard.org/standard/core/ALIGNED
360 \ ALIGNED  addr -- a-addr       align given addr
361 CODE ALIGNED
362 BIT #1,TOS
363 ADDC #0,TOS
364 MOV @IP+,PC
365 ENDCODE
366     \
367
368 \ https://forth-standard.org/standard/core/ALIGN
369 \ ALIGN    --                         align HERE
370 CODE ALIGN
371 BIT #1,&DP  \ 3
372 ADDC #0,&DP \ 4
373 MOV @IP+,PC
374 ENDCODE
375     \
376
377 \ ---------------------
378 \ PORTABILITY OPERATORS
379 \ ---------------------
380
381 \ https://forth-standard.org/standard/core/CHARS
382 \ CHARS    n1 -- n2            chars->adrs units
383 CODE CHARS
384 MOV @IP+,PC
385 ENDCODE
386     \
387
388 \ https://forth-standard.org/standard/core/CHARPlus
389 \ CHAR+    c-addr1 -- c-addr2   add char size
390 CODE CHAR+
391 ADD #1,TOS
392 MOV @IP+,PC
393 ENDCODE
394     \
395
396 \ https://forth-standard.org/standard/core/CELLS
397 \ CELLS    n1 -- n2            cells->adrs units
398 CODE CELLS
399 ADD TOS,TOS
400 MOV @IP+,PC
401 ENDCODE
402     \
403
404 \ https://forth-standard.org/standard/core/CELLPlus
405 \ CELL+    a-addr1 -- a-addr2      add cell size
406 CODE CELL+
407 ADD #2,TOS
408 MOV @IP+,PC
409 ENDCODE
410     \
411 \ ---------------------------
412 \ BLOCK AND STRING COMPLEMENT
413 \ ---------------------------
414
415 \ https://forth-standard.org/standard/core/CHAR
416 \ CHAR   -- char           parse ASCII character
417 : CHAR
418     BL WORD 1+ C@
419 ;
420
421 \ https://forth-standard.org/standard/core/BracketCHAR
422 \ [CHAR]   --          compile character literal
423 : [CHAR]
424     CHAR lit lit , ,
425 ; IMMEDIATE
426
427     \
428
429 \ https://forth-standard.org/standard/core/PlusStore
430 \ +!     n/u a-addr --       add n/u to memory
431 CODE +!
432 ADD @PSP+,0(TOS)
433 MOV @PSP+,TOS
434 MOV @IP+,PC
435 ENDCODE
436     \ 
437
438
439 \ https://forth-standard.org/standard/core/FILL
440 \ FILL   c-addr u char --  fill memory with char
441 CODE FILL
442 MOV @PSP+,X     \ count
443 MOV @PSP+,W     \ address
444 CMP #0,X
445 0<> IF
446     BEGIN
447         MOV.B TOS,0(W)    \ store char in memory
448         ADD #1,W
449         SUB #1,X
450     0= UNTIL
451 THEN
452 MOV @PSP+,TOS     \ empties stack
453 MOV @IP+,PC
454 ENDCODE
455     \ 
456
457 \ --------------------
458 \ INTERPRET COMPLEMENT
459 \ --------------------
460
461 \ https://forth-standard.org/standard/core/HEX
462 CODE HEX
463 MOV #$10,&BASE
464 MOV @IP+,PC
465 ENDCODE
466     \
467
468 \ https://forth-standard.org/standard/core/DECIMAL
469 CODE DECIMAL
470 MOV #$0A,&BASE
471 MOV @IP+,PC
472 ENDCODE
473     \
474
475 \ https://forth-standard.org/standard/core/p
476 \ (         --          skip input until char ) or EOL
477 : ( 
478 $29 WORD DROP
479 ; IMMEDIATE
480     \
481
482 [DEFINED] CAPS_ON [IF]
483     \
484 \ https://forth-standard.org/standard/core/Dotp
485 \ .(        --          type comment immediatly.
486 : .(
487 CAPS_OFF
488 $29 WORD
489 COUNT TYPE
490 CAPS_ON
491 ; IMMEDIATE
492     \
493 [ELSE]
494 \ https://forth-standard.org/standard/core/Dotp
495 \ .(        --          type comment immediatly.
496 : .(
497 $29 WORD
498 COUNT TYPE
499 ; IMMEDIATE
500     \
501 [THEN]
502     \
503
504 \ https://forth-standard.org/standard/core/SOURCE
505 \ SOURCE    -- adr u    of current input buffer
506 CODE SOURCE
507 SUB #4,PSP
508 MOV TOS,2(PSP)
509 MOV &SOURCE_LEN,TOS
510 MOV &SOURCE_ADR,0(PSP)
511 MOV @IP+,PC
512 ENDCODE
513     \
514
515 \ https://forth-standard.org/standard/core/toBODY
516 \ >BODY     -- PFA      leave PFA of created word
517 CODE >BODY
518 ADD #4,TOS
519 MOV @IP+,PC
520 ENDCODE
521     \
522 PWR_HERE
523     \
524 ECHO