(* Protection for offscreen printen
*)

value X value Y

code OFFSCR?    ( -- fl )
    tos sp -) mov
    80 # adr x & cmp  u>? if,       \ X within 0 to 7F
        8 # adr y & cmp  u>? if,    \ Y within 0 to 7
            #0 tos mov next
        then,
    then,
    #-1 tos mov  next
end-code

code C>N        ( char -- +n )
    80 # tos cmp
    u<eq? if,  80 # tos mov  then,
    bl # tos sub  next
end-code

\ Get u1 from R-stack, decr by 1. When u2 is not zero
\ leave false and u2 on the stack, otherwise only true!
code DONE?      ( r:+u1 -- +u2 false | true )
    tos sp -) mov
    rp )+ tos mov
    #1 tos sub
    0<>? if,
        tos sp -) mov
        #-1 tos mov  next
    then,
    #0 tos mov  next
end-code

code G4<    ( -- flag )
    tos sp -) mov
    80 # tos mov
    adr x & tos sub
    #4 tos cmp
    >? if,  #-1 tos mov  next
    then,   #0 tos mov   next
end-code


: I2C   ( fl -- )   ?abort ;        \ I2C error message

\ 04 = wait for data received to RX; 08 = wait for TX to be sent
\ : I2READY   ( bit -- )
\    80  begin
\        over 003 bit* if  2drop exit  then
\    1- dup 0= until  true ?abort ;
\ code I2READY   ( bit -- )
\    #-1 day .b mov      \ 255 to counter (day)
\    begin,
\        tos 003 & .b bit  cs? if,  sp )+ tos mov  next  then,
\        #1 day sub
\    0=? until,
\ chere >r  \ Reuse of code
\    ip push
\    ' i2c >body # ip mov
\    next
\ end-code
code I2READY    ( bit -- )
    4378 ,  B7C2 ,  3 ,  2802 ,  4437 ,  4F00 ,
    8318 ,  23F9 ,  ( chere >r )  1205 ,
    4035 ,  ' i2c >body ,  4F00 ,
end-code

\ 02 = wait for startcond. to finish; 04 = wait for stopcond. to finish
\ : I2DONE    ( bit -- )  \ wait until startcond. or stopcond. is done
\    80  begin
\        over 069 bit* 0= if  2drop exit  then
\    1- dup 0= until  true ?abort ;
\ code I2DONE     ( bit -- )
\    #-1 day .b mov
\    begin,
\        tos 069 & .b bit  cc? if,  sp )+ tos mov  next  then,
\        #1 day sub
\    0=? until,
\    r> jmp
\ end-code
code I2DONE     ( bit -- )
    4378 ,  B7C2 ,  69 ,  2C02 ,  4437 ,
    4F00 ,  8318 ,  23F9 ,  3FEE ,  ( r> jmp )
end-code


\ Set DCO to 8 MHz, current use of CPU without leds = 2,56mA
: 8MHZ      ( -- )  \ Baudrate 38K4
    int-off
    10FD c@ 0057 c! \ BCSCTL1   Set dco = 8 mhz
    10FC c@ 0056 c! \ DCOCTL
    0000 0058 c!    \ UCA0CTL2  DCO on
    0081 0061 *bis  \ UCA0CTL1  Uart use SMCLK
    00D0 0062 c!    \ UCA0BR0   8 mhz, baudrate 38400
    0000 0063 c!    \ UCA0BR1   idem
    0006 0064 c!    \ UCAMCTL   modulation ucbrsx = 3
    0001 0061 *bic  \ UCA0CTL   enable USCI
    A550 012A !     \ FCTL2     mclk/17=470KHz flash timing
    ;

\ End

code OFFSCR?    ( -- flag )
8324 ,  4784 ,  0 ,
90B2 ,  80 ,  adr X ,
2C06 ,  90B2 ,  8 ,  adr Y ,
2C02 ,  4307 ,  4F00 ,  4337 ,
4F00 ,  end-code

code C>N        ( c -- +n )
9037 ,  80 ,  2802 ,
4037 ,  80 ,  8037 ,  20 ,
4F00 ,  end-code

\ Get u1 from R-stack, decr by 1. When u2 is not zero
\ leave false and u2 on the stack, otherwise only true!
code DONE?      ( r:+u1 -- +u2 false | true )
8324 ,  4784 ,  0 ,  4137 ,
8317 ,  2405 ,  8324 ,  4784 ,
0 ,  4337 ,  4F00 ,  4307 ,
4F00 ,  end-code

code G4<        ( -- flag )
8324 ,  4784 ,  0 ,
4037 ,  80 ,  8217 ,  252 ,
9227 ,  3402 ,  4337 ,  4F00 ,
4307 ,  4F00 ,  end-code

