OSDN Git Service

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