
; --------------------
; RTC.4th
; --------------------

PWR_STATE

[DEFINED] {RTC} [IF]  {RTC} [THEN]

MARKER {RTC}

[UNDEFINED] IF [IF]
CODE IF
SUB #2,R15
MOV R14,0(R15)
MOV &$1DC6,R14
ADD #4,&$1DC6
MOV #$4042,0(R14)
ADD #2,R14
MOV @R13+,R0
ENDCODE IMMEDIATE

CODE THEN
MOV &$1DC6,0(R14)
MOV @R15+,R14
MOV @R13+,R0
ENDCODE IMMEDIATE
[THEN]

: NORTC
IF
    {RTC}
    ECHO $0D EMIT
    ABORT" no RTC on this device !"
THEN
;

[UNDEFINED] AND [IF]
CODE AND
AND @R15+,R14
MOV @R13+,R0
ENDCODE
[THEN]

[UNDEFINED] OR [IF]
CODE OR
BIS @R15+,R14
MOV @R13+,R0
ENDCODE
[THEN]

[UNDEFINED] @ [IF]
CODE @
MOV @R14,R14
MOV @R13+,R0
ENDCODE
[THEN]

[UNDEFINED] U< [IF]
CODE U<
SUB @R15+,R14
0<> IF
    MOV #-1,R14
    U< IF
        AND #0,R14
    THEN
THEN
MOV @R13+,R0
ENDCODE
[THEN]

                        ; search devide ID:
$81EF $1A04 @ U<        ; MSP430FR2433 or...
$1A04 @ $8241 U<        ; ...MSP430FR4133
AND
$830B $1A04 @ U<        ; MSP430FR21xx/23xx/24xx/25xx/26xx
OR                      ; -- flag       0 ==> RTC, -1 ==> no RTC
NORTC

[UNDEFINED] = [IF]
CODE =
SUB @R15+,R14
0<> IF
    AND #0,R14
    MOV @R13+,R0
THEN
XOR #-1,R14
MOV @R13+,R0
ENDCODE
[THEN]

[UNDEFINED] SWAP [IF]
CODE SWAP
MOV @R15,R10
MOV R14,0(R15)
MOV R10,R14
MOV @R13+,R0
ENDCODE
[THEN]

[UNDEFINED] OVER [IF]
CODE OVER
MOV R14,-2(R15)
MOV @R15,R14
SUB #2,R15
MOV @R13+,R0
ENDCODE
[THEN]

[UNDEFINED] EXECUTE [IF]
CODE EXECUTE
MOV R14,R10
MOV @R15+,R14
MOV R10,R0
ENDCODE
[THEN]

[UNDEFINED] DO [IF]
CODE DO
SUB #2,R15
MOV R14,0(R15)
ADD #2,&$1DC6
MOV &$1DC6,R14
MOV #$404C,-2(R14)
ADD #2,&$1C00
MOV &$1C00,R10
MOV #0,0(R10)
MOV @R13+,R0
ENDCODE IMMEDIATE

CODE LOOP
    MOV #$406E,R9
BW1 ADD #4,&$1DC6
    MOV &$1DC6,R10
    MOV R9,-4(R10)
    MOV R14,-2(R10)
BEGIN
    MOV &$1C00,R14
    SUB #2,&$1C00
    MOV @R14,R14
    CMP #0,R14
0<> WHILE
    MOV R10,0(R14)
REPEAT
    MOV @R15+,R14
    MOV @R13+,R0
ENDCODE IMMEDIATE

CODE +LOOP
MOV #$405C,R9
GOTO BW1
ENDCODE IMMEDIATE
[THEN]

[UNDEFINED] - [IF]
CODE -
SUB @R15+,R14
XOR #-1,R14
ADD #1,R14
MOV @R13+,R0
ENDCODE
[THEN]

[UNDEFINED] MAX [IF]

CODE MAX
    CMP @R15,R14
    S<  ?GOTO FW1
BW1 ADD #2,R15
    MOV @R13+,R0
ENDCODE

CODE MIN
    CMP @R15,R14
    S<  ?GOTO BW1
FW1 MOV @R15+,R14
    MOV @R13+,R0
ENDCODE

[THEN]

[UNDEFINED] SPACES [IF]
CODE SPACES
CMP #0,R14
0<> IF
    PUSH R13
    BEGIN
        LO2HI
        $20 EMIT
        HI2LO
        SUB #2,R13 
        SUB #1,R14
    0= UNTIL
    MOV @R1+,R13
THEN
MOV @R15+,R14
MOV @R13+,R0              
ENDCODE
[THEN]

[UNDEFINED] DUP [IF]
CODE DUP
BW1 SUB #2,R15
    MOV R14,0(R15)
    MOV @R13+,R0
ENDCODE

CODE ?DUP
CMP #0,R14
0<> ?GOTO BW1
MOV @R13+,R0
ENDCODE
[THEN]

[UNDEFINED] DEPTH [IF]
CODE DEPTH
MOV R14,-2(R15)
MOV #$1C80,R14
SUB R15,R14
RRA R14
SUB #2,R15
MOV @R13+,R0
ENDCODE
[THEN]

[UNDEFINED] >R [IF]
CODE >R
PUSH R14
MOV @R15+,R14
MOV @R13+,R0
ENDCODE
[THEN]

[UNDEFINED] R> [IF]
CODE R>
SUB #2,R15
MOV R14,0(R15)
MOV @R1+,R14
MOV @R13+,R0
ENDCODE
[THEN]

[UNDEFINED] >BODY [IF]
CODE >BODY
ADD #4,R14
MOV @R13+,R0
ENDCODE
[THEN]

[UNDEFINED] EXECUTE [IF]
CODE EXECUTE
MOV R14,R10
MOV @R15+,R14
MOV R10,R0
ENDCODE
[THEN]

[UNDEFINED] U.R [IF]
: U.R
  >R  <# 0 # #S #>  
  R> OVER - 0 MAX SPACES TYPE
;
[THEN]

CODE DATE?
    SUB     #6,R15
    MOV     R14,4(R15)
    BEGIN
        BIT.B #$10,&$4A2
    0<> UNTIL
    MOV     &$4B6,2(R15)
    MOV.B   &$4B5,R14
    MOV     R14,0(R15)
    MOV.B   &$4B4,R14
COLON
    2 U.R $2F EMIT
    2 U.R $2F EMIT . 
;

: DATE!
2 DEPTH U< IF
    HI2LO
    MOV     R14,&$4B6
    MOV.B   @R15,&$4B5
    MOV.B   2(R15),&$4B4
    ADD     #4,R15
    MOV     @R15+,R14
    LO2HI
THEN
    ." we are on " DATE? 
;

CODE TIME?
    SUB     #6,R15
    MOV     R14,4(R15)
    BEGIN
        BIT.B #$10,&$4A2
    0<> UNTIL
    MOV.B   &$4B0,R14
    MOV     R14,2(R15)
    MOV.B   &$4B1,R14
    MOV     R14,0(R15)
    MOV.B   &$4B2,R14
COLON
    2 U.R $3A EMIT 
    2 U.R $3A EMIT 2 U.R 
;

: TIME!
2 DEPTH U< IF
    HI2LO
    MOV     R14,&$4B0
    MOV.B   @R15,&$4B1
    MOV.B   2(R15),&$4B2
    ADD     #4,R15
    MOV     @R15+,R14
    LO2HI
THEN
    ." it is " TIME? 
;

PWR_HERE


[UNDEFINED] S_ [IF]
CODE S_
MOV #0,&$1DB4
COLON
$4018 ,
$20 WORD
HI2LO
MOV.B @R14,R14
ADD #1,R14
BIT #1,R14
ADDC R14,&$1DC6
MOV @R15+,R14
MOV @R1+,R13
MOV #$20,&$1DB4
MOV @R13+,R0
ENDCODE IMMEDIATE
[THEN]

[UNDEFINED] ESC [IF]
CODE ESC
CMP #0,&$1DBE
0= IF MOV @R13+,R0
THEN
COLON          
$1B
POSTPONE LITERAL
POSTPONE EMIT
POSTPONE S_
POSTPONE TYPE
; IMMEDIATE
[THEN]

: PAD_ACCEPT
$1CE4
DUP #84
    ['] ACCEPT DUP @
        $4030 =
        IF >BODY
        THEN
    EXECUTE
;

: GET_TIME
PWR_STATE
42
0 DO CR LOOP
ESC [H

CR ." DATE (DMY): "
PAD_ACCEPT
EVALUATE CR DATE!

CR ." TIME (HMS): "
PAD_ACCEPT
EVALUATE CR TIME!
;

ECHO GET_TIME
