(* Convert frequency to real MSP430 values

Example:

decimal  actual
16  DCO   \ dcofreq
8 32  SWITCH   \ mask addr
38,4  BAUDRATE    \ baud

*)

hex     \ >DCO en >FLASH werken allen ok, nu nog BAUDRATE
value FREQ                      \ Convert DCO frequency
: >DCO      ( +n -- addr )
    to freq
    freq 01 = if  10FE  else     \ 1 MHz, etc.
    freq 08 = if  10FC  else
    freq 12 = if  10FA  else
    freq 16 = if  10F8  else
                 true ?abort
    then then then then ;

: >FLASH    ( -- 'flash )    \ Calculate flash programming divider
    freq dm 1000 dm 470 */mod \ Do it
    dm 30 < if  1-  then    \ Round down when small remainder only
    A540 or ;               \ Add access key & clock source

: .DCO      ( byte -- )     \ Print DCO freq. decimaal
    base @ >r  decimal
    >r  r@ F8 = if  10  else
        r@ FA = if  0C  else
        r@ FC = if  08  else
        r@ FE = if  01  then
        then then then
    r> drop  dup to freq  .  r> base ! ;

(* decimal  \ Overflow in berekening al bij 8 MHz frequentie
: MHZ       ( -- h )    freq 5,000 * ;
: >BAUD     ( baudr -- x y )
    mhz swap 32 swap */ aligned 1 rshift
    dup 3 rshift .
    7 and . ;
*)

\ Nog een baud bereken test routine heeft voor de rest
\ een afwijkende uitkomst van +1 of -1
: >BAUD     ( "baud" -- )   \ Deze heeft geen overflow dankzij UM/MOD
    >r  freq  dm 10,000 um*  r@ um/mod .     \ Baudrate deler
    8 * r@ /mod  swap r> 2/ > 1 and + .  ; \ De rest

\ Een baudrate afdruk routine
: .BAUD     ( baud-hi baud-lo -- )
    base @ >r  decimal
    swap >< or >r   freq dm 10,000 um*   r> um/mod nip
    0 <# # ch , hold #s #> type space
    r> base ! ;


hex
create TEMP  0C allot
: .CONFIG       \ Print configuration data for MSP430!
    cr temp 2 + count .dco ."  DCO   \ dcofreq"
    cr count u.  @+ u. ."  SWITCH   \ mask addr"
    cr count >r count >r  drop  r>  r> .baud ."  BAUDRATE    \ baud"
    cr ;

: DCO       ( dcofreq -- )      >dco temp 2 + c!  >flash temp ! ;
: SWITCH    ( mask addr -- )    temp 4 + !  temp 3 + c! ;
\ : BAUDRATE  ( br2 br1 br0 -- )  temp 6 + c!  temp 7 + c!  temp 8 + c! ;

: TEST      ( -- )              temp config ;

: ACTUAL    ( -- )
    frozen2 temp 0C move  .config ;

: RETAIN    ( -- )
    frozen  here  frozen2 frozen -  tidy)
    temp  frozen2 0C rommove ;
