1 \ -*- coding: utf-8 -*-
3 \ ==============================================================================
4 \ routines RTC for MSP430FR5xxx
5 \ your target must have a LF_XTAL 32768Hz
6 \ ==============================================================================
8 \ to see kernel options, download FastForthSpecs.f
9 \ FastForth kernel minimal addons: MSP430ASSEMBLER, CONDCOMP
11 \ TARGET SELECTION ( = the name of \INC\target.pat file without the extension)
12 \ MSP_EXP430FR5739 MSP_EXP430FR5969 MSP_EXP430FR5994 MSP_EXP430FR6989
14 \ from scite editor : copy your target selection in (shift+F8) parameter 1:
16 \ or, from windows explorer:
17 \ drag and drop this file onto SendSourceFileToTarget.bat
18 \ then select your TARGET when asked.
20 \ ASSEMBLER REGISTERS USAGE
21 \ R4 to R7 must be saved before use and restored after
22 \ scratch registers Y to S are free for use
23 \ under interrupt, IP is free for use
25 \ PUSHM order : PSP,TOS, IP, S, T, W, X, Y, rEXIT,rDOVAR,rDOCON, rDODOES, R3, SR,RSP, PC
26 \ PUSHM order : R15,R14,R13,R12,R11,R10, R9, R8, R7 , R6 , R5 , R4 , R3, R2, R1, R0
28 \ example : PUSHM #6,IP pushes IP,S,T,W,X,Y registers to return stack
30 \ POPM order : PC,RSP, SR, R3, rDODOES,rDOCON,rDOVAR,rEXIT, Y, X, W, T, S, IP,TOS,PSP
31 \ POPM order : R0, R1, R2, R3, R4 , R5 , R6 , R7 , R8, R9,R10,R11,R12,R13,R14,R15
33 \ example : POPM #6,IP pop Y,X,W,T,S,IP registers from return stack
36 \ FORTH conditionnals: unary{ 0= 0< 0> }, binary{ = < > U< }
38 \ ASSEMBLER conditionnal usage with IF UNTIL WHILE S< S>= U< U>= 0= 0<> 0>=
39 \ ASSEMBLER conditionnal usage with ?JMP ?GOTO S< S>= U< U>= 0= 0<> 0<
48 0<> IF MOV #0,TOS THEN \ if TOS <> 0 (FIXPOINT input), set TOS = 0
51 SUB #309,TOS \ FastForth V3.9
53 $0D EMIT \ return to column 1 without CR
54 ABORT" FastForth V3.9 please!"
55 ABORT" target without LF_XTAL !"
56 RST_RET \ if no abort remove this word
61 ; --------------------
63 ; --------------------
66 \ to set date, type : d m y DATE!
67 \ to view date, type DATE?
68 \ to set time, type : h m [s] TIME!
69 \ to view time, type TIME?
74 \ https://forth-standard.org/standard/core/OR
75 \ C OR x1 x2 -- x3 logical OR
84 \ https://forth-standard.org/standard/core/CFetch
85 \ C@ c-addr -- char fetch char from memory
94 \ https://forth-standard.org/standard/core/CStore
95 \ C! char c-addr -- store char in memory
99 MOV.B @PSP+,0(TOS) \ 4
106 \ https://forth-standard.org/standard/core/SWAP
107 \ SWAP x1 x2 -- x2 x1 swap top two items
118 \ https://forth-standard.org/standard/core/OVER
119 \ OVER x1 x2 -- x1 x2 x1
123 MOV TOS,-2(PSP) \ 3 -- x1 (x2) x2
124 MOV @PSP,TOS \ 2 -- x1 (x2) x1
125 SUB #2,PSP \ 1 -- x1 x2 x1
130 \ https://forth-standard.org/standard/core/DUP
131 \ DUP x -- x x duplicate top of stack
133 [IF] \define DUP and DUP?
135 BW1 SUB #2,PSP \ 2 push old TOS..
136 MOV TOS,0(PSP) \ 3 ..onto stack
140 \ https://forth-standard.org/standard/core/qDUP
141 \ ?DUP x -- 0 | x x DUP if nonzero
143 CMP #0,TOS \ 2 test for TOS nonzero
149 \ https://forth-standard.org/standard/core/DROP
150 \ DROP x -- drop top of stack
159 \ https://forth-standard.org/standard/core/DEPTH
160 \ DEPTH -- +n number of items on stack, must leave 0 if stack empty
166 SUB PSP,TOS \ PSP-S0--> TOS
167 RRA TOS \ TOS/2 --> TOS
168 SUB #2,PSP \ post decrement stack...
173 \ https://forth-standard.org/standard/core/toR
174 \ >R x -- R: -- x push to return stack
184 \ https://forth-standard.org/standard/core/Rfrom
185 \ R> -- x R: x -- pop from return stack ; CALL #RFROM performs DOVAR
196 \ https://forth-standard.org/standard/core/OnePlus
197 \ 1+ n1/u1 -- n2/u2 add 1 to TOS
206 \ https://forth-standard.org/standard/core/OneMinus
207 \ 1- n1/u1 -- n2/u2 subtract 1 from TOS
219 SUB @PSP+,TOS \ 2 u2-u1
223 AND #0,TOS \ 1 flag Z = 1
230 \ https://forth-standard.org/standard/core/Equal
231 \ = x1 x2 -- flag test x1=x2
240 XOR #-1,TOS \ 1 flag Z = 1
245 \ https://forth-standard.org/standard/core/IF
246 \ IF -- IFadr initialize conditional forward branch
248 [IF] \ define IF THEN
252 MOV &DP,TOS \ -- HERE
253 ADD #4,&DP \ compile one word, reserve one word
254 MOV #QFBRAN,0(TOS) \ -- HERE compile QFBRAN
255 ADD #2,TOS \ -- HERE+2=IFadr
259 \ https://forth-standard.org/standard/core/THEN
260 \ THEN IFadr -- resolve forward branch
261 CODE THEN \ immediate
262 MOV &DP,0(TOS) \ -- IFadr
268 \ https://forth-standard.org/standard/core/ELSE
269 \ ELSE IFadr -- ELSEadr resolve forward IF branch, leave ELSEadr on stack
272 CODE ELSE \ immediate
273 ADD #4,&DP \ make room to compile two words
276 MOV W,0(TOS) \ HERE+4 ==> [IFadr]
278 MOV W,TOS \ -- ELSEadr
283 \ https://forth-standard.org/standard/core/DO
284 \ DO -- DOadr L: -- 0
286 [IF] \ define DO LOOP +LOOP
287 HDNCODE XDO \ DO run time
288 MOV #$8000,X \ 2 compute 8000h-limit = "fudge factor"
290 MOV TOS,Y \ 1 loop ctr = index+fudge
291 ADD X,Y \ 1 Y = INDEX
292 PUSHM #2,X \ 4 PUSHM X,Y, i.e. PUSHM LIMIT, INDEX
300 ADD #2,&DP \ make room to compile xdo
301 MOV &DP,TOS \ -- HERE+2
302 MOV #XDO,-2(TOS) \ compile xdo
303 ADD #2,&LEAVEPTR \ -- HERE+2 LEAVEPTR+2
305 MOV #0,0(W) \ -- HERE+2 L-- 0
309 \ https://forth-standard.org/standard/core/LOOP
310 \ LOOP DOadr -- L-- an an-1 .. a1 0
311 HDNCODE XLOOP \ LOOP run time
312 ADD #1,0(RSP) \ 4 increment INDEX
313 BW1 BIT #$100,SR \ 2 is overflow bit set?
314 0= IF \ branch if no overflow
318 ADD #4,RSP \ 1 empties RSP
319 ADD #2,IP \ 1 overflow = loop done, skip branch ofs
320 MOV @IP+,PC \ 4 14~ taken or not taken xloop/loop
325 BW2 ADD #4,&DP \ make room to compile two words
327 MOV X,-4(W) \ xloop --> HERE
328 MOV TOS,-2(W) \ DOadr --> HERE+2
329 BEGIN \ resolve all "leave" adr
330 MOV &LEAVEPTR,TOS \ -- Adr of top LeaveStack cell
331 SUB #2,&LEAVEPTR \ --
332 MOV @TOS,TOS \ -- first LeaveStack value
333 CMP #0,TOS \ -- = value left by DO ?
335 MOV W,0(TOS) \ move adr after loop as UNLOOP adr
341 \ https://forth-standard.org/standard/core/PlusLOOP
342 \ +LOOP adrs -- L-- an an-1 .. a1 0
343 HDNCODE XPLOO \ +LOOP run time
344 ADD TOS,0(RSP) \ 4 increment INDEX by TOS value
345 MOV @PSP+,TOS \ 2 get new TOS, doesn't change flags
351 GOTO BW2 \ goto BW1 LOOP
355 \ https://forth-standard.org/standard/core/BEGIN
356 \ BEGIN -- BEGINadr initialize backward branch
358 [IF] \ define BEGIN UNTIL AGAIN WHILE REPEAT
364 \ https://forth-standard.org/standard/core/UNTIL
365 \ UNTIL BEGINadr -- resolve conditional backward branch
368 BW1 ADD #4,&DP \ compile two words
370 MOV X,-4(W) \ compile Bran or QFBRAN at HERE
371 MOV TOS,-2(W) \ compile bakcward adr at HERE+2
376 \ https://forth-standard.org/standard/core/AGAIN
377 \ AGAIN BEGINadr -- resolve uncondionnal backward branch
383 \ https://forth-standard.org/standard/core/WHILE
384 \ WHILE BEGINadr -- WHILEadr BEGINadr
389 \ https://forth-standard.org/standard/core/REPEAT
390 \ REPEAT WHILEadr BEGINadr -- resolve WHILE loop
392 POSTPONE AGAIN POSTPONE THEN
396 \ https://forth-standard.org/standard/core/CASE
401 ; IMMEDIATE \ -- #of-1
403 \ https://forth-standard.org/standard/core/OF
404 : OF \ #of-1 -- orgOF #of
406 >R \ move off the stack in case the control-flow stack is the data stack.
407 POSTPONE OVER POSTPONE = \ copy and test case value
408 POSTPONE IF \ add orig to control flow stack
409 POSTPONE DROP \ discards case value if =
410 R> \ we can bring count back now
413 \ https://forth-standard.org/standard/core/ENDOF
414 : ENDOF \ orgOF #of -- orgENDOF #of
415 >R \ move off the stack in case the control-flow stack is the data stack.
417 R> \ we can bring count back now
420 \ https://forth-standard.org/standard/core/ENDCASE
421 : ENDCASE \ orgENDOF1..orgENDOFn #of --
429 \ https://forth-standard.org/standard/core/Plus
430 \ + n1/u1 n2/u2 -- n3/u3
439 \ https://forth-standard.org/standard/core/Minus
440 \ - n1/u1 n2/u2 -- n3/u3 n3 = n1-n2
444 SUB @PSP+,TOS \ 2 -- n2-n1 ( = -n3)
446 ADD #1,TOS \ 1 -- n3 = -(n2-n1) = n1-n2
452 [IF] \define MAX and MIN
453 CODE MAX \ n1 n2 -- n3 signed maximum
460 CODE MIN \ n1 n2 -- n3 signed minimum
469 \ https://forth-standard.org/standard/core/TwoTimes
470 \ 2* x1 -- x2 arithmetic left shift
479 \ https://forth-standard.org/standard/core/UMTimes
480 \ UM* u1 u2 -- udlo udhi unsigned 16x16->32 mult.
482 [IF] \ case of hardware_MPY
484 MOV @PSP,&MPY \ Load 1st operand for unsigned multiplication
485 BW1 MOV TOS,&OP2 \ Load 2nd operand
486 MOV &RES0,0(PSP) \ low result on stack
487 MOV &RES1,TOS \ high result in TOS
491 \ https://forth-standard.org/standard/core/MTimes
492 \ M* n1 n2 -- dlo dhi signed 16*16->32 multiply
494 MOV @PSP,&MPYS \ Load 1st operand for signed multiplication
499 \ https://forth-standard.org/standard/core/UMDivMOD
500 \ UM/MOD udlo|udhi u1 -- ur uq unsigned 32/16->r16 q16
505 MOV #MUSMOD,PC \ execute MUSMOD then return to DROP
509 \ U*/ u1 u2 u3 -- uq u1*u2/u3
511 >R UM* R> UM/MOD SWAP DROP
514 \ U/MOD u1 u2 -- ur uq unsigned division
519 \ UMOD u1 u2 -- ur unsigned division
524 \ https://forth-standard.org/standard/core/Div
525 \ U/ u1 u2 -- uq signed quotient
530 \ https://forth-standard.org/standard/core/SPACES
531 \ SPACES n -- output n spaces
546 : U.R \ u n -- display u unsigned in n width (n >= 2)
548 R> OVER - 0 MAX SPACES TYPE
554 BIT.B #RTCRDY,&RTCCTL1
555 0<> UNTIL \ wait until RTCRDY high
557 RTCHOUR C@ 2 U.R ':' EMIT
558 RTCMIN C@ 2 U.R ':' EMIT
564 U< IF \ if 3 numbers on stack
572 CODE DATE? \ display date
574 BIT.B #RTCRDY,&RTCCTL1
575 0<> UNTIL \ wait until windows time RTC_ReaDY is high
580 \ ==============================================================================
581 \ end of RTC software|harware calendar
582 \ ==============================================================================
583 \ resume with common part of DATE? definition:
585 RTCDOW C@ \ -- weekday {0=Sat...6=Fri}
597 RTCDAY C@ \ -- year mon day
599 2 U.R '/' EMIT \ -- year mon
600 2 U.R '/' EMIT \ -- year
604 : DATE! \ year mon day --
606 U< IF \ if 3 numbers on stack
613 RTCYEAR @ \ -- day mon year
614 \ ------------------------------------------
615 \ Zeller's congruence for gregorian calendar
616 \ see https://www.rosettacode.org/wiki/Day_of_the_week#Forth
617 \ : ZELLER \ day mon year -- weekday {0=Sat, ..., 6=Fri}
619 \ IF 1- SWAP 12 + SWAP
620 \ THEN \ -- d m' y' with m' {3=March, ..., 14=february}
621 \ 100 /MOD \ -- d m' K J with K = y' in century, J = century
622 \ DUP 4 / SWAP 2* - \ -- d m' K (J/4 - 2J)
623 \ SWAP DUP 4 / + + \ -- d m' ((J/4 - 2J) + (K + K/4))
624 \ SWAP 1+ 13 5 */ + + \ -- (d + (((J/4 - 2J) + (K + K/4)) + (m+1)*13/5))
625 \ 7 MOD \ -- weekday = {0=Sat, ..., 6=Fri}
626 \ ------------------------------------------
628 IF 1 - SWAP 12 + SWAP
629 THEN \ -- d m' y' with m' {3=March, ..., 14=february}
630 100 U/MOD \ -- d m' K J with K = y' in century, J = century
631 DUP 4 U/ SWAP 2* - \ -- d m' K (J/4 - 2J)
632 SWAP DUP 4 U/ + + \ -- d m' ((J/4 - 2J) + (K + K/4))
633 SWAP 1+ 13 5 U*/ + + \ -- (d + (((J/4 - 2J) + (K + K/4)) + (m+1)*13/5))
634 7 UMOD \ -- weekday = {0=Sat, ..., 6=Fri}
635 \ ------------------------------------------
644 CODE S_ \ Squote alias with blank instead quote separator
645 MOV #0,&CAPS \ turn CAPS OFF
647 XSQUOTE , \ compile run-time code
648 $20 WORD \ -- c-addr (= HERE)
650 MOV.B @TOS,TOS \ -- len compile string
651 ADD #1,TOS \ -- len+1
653 ADDC TOS,&DP \ store aligned DP
655 MOV @RSP+,IP \ pop paired with push COLON
656 MOV #$20,&CAPS \ turn CAPS ON (default state)
665 0= IF MOV @IP+,PC \ interpret time usage disallowed
669 POSTPONE LITERAL \ compile-time code : lit $1B
670 POSTPONE EMIT \ compile-time code : EMIT
671 POSTPONE S_ \ compile-time code : S_ <escape_sequence>
672 POSTPONE TYPE \ compile-time code : TYPE
676 \ https://forth-standard.org/standard/core/toBODY
677 \ >BODY -- addr leave BODY of a CREATEd word\ also leave default ACTION-OF primary DEFERred word
686 \ https://forth-standard.org/standard/core/EXECUTE
687 \ EXECUTE i*x xt -- j*x execute Forth word at 'xt'
693 MOV @RSP+,PC \ 4 xt --> PC
697 \ https://forth-standard.org/standard/core/EVALUATE
698 \ EVALUATE \ i*x c-addr u -- j*x interpret string
702 MOV #SOURCE_LEN,X \ 2
703 MOV @X+,S \ 2 S = SOURCE_LEN
704 MOV @X+,T \ 2 T = SOURCE_ORG
705 MOV @X+,W \ 2 W = TOIN
706 PUSHM #4,IP \ 6 PUSHM IP,S,T,W
708 [ ' \ 8 + , ] \ compile INTERPRET = BACKSLASH + 8
711 MOV @RSP+,&SOURCE_ORG \ 4
712 MOV @RSP+,&SOURCE_LEN \ 4
718 \ https://forth-standard.org/standard/core/CR
719 \ CR -- send CR+LF to the output device
723 \ DEFER CR \ DEFERed definition, by default executes that of :NONAME
724 CODE CR \ create a DEFER definition of CR
728 :NONAME \ starts at BODY address of DEFERed CR
730 ; IS CR \ CR executes :NONAME by default
734 ESC [8;40;80t \ set terminal display 42L * 80C
735 39 0 DO CR LOOP \ to avoid erasing any line of source, create 42 empty lines
736 ESC [H \ then set cursor home
739 ['] ACCEPT >BODY \ find default part of deferred ACCEPT (terminal input)
740 EXECUTE \ wait human input for D M Y
741 EVALUATE \ interpret this input
745 ['] ACCEPT >BODY \ find default part of deferred ACCEPT (terminal input)
746 EXECUTE \ wait human input for H M S
747 EVALUATE \ interpret this input
749 RST_RET \ remove code beyond RST_HERE