
$03 constant IFG2
$6E constant UCB0RXBUF
$6F constant UCB0TXBUF

: leds    ( c -- ) $29 c! $FF $2A c! ;
: buttons ( -- x ) $18 c@ ;

: cycles  ( -- u ) $01F0 @ ;

   40 constant cycles/us  \ For 40 MHz
40000 constant cycles/ms

: delay-cycles ( cycles -- )
  cycles ( cycles start )
  begin
    pause
    2dup ( cycles start cycles start )
    cycles ( cycles start cycles start current )
    swap - ( cycles start cycles elapsed )
    u<=
  until
  2drop
;

: us ( u -- )       cycles/us *  delay-cycles      ;
: ms ( u -- ) 0 ?do cycles/ms    delay-cycles loop ;

: random ( -- x ) 0 32 0 do 10 us 2* $1C c@ or loop ;

\ -----------------------------------------------------------------------------
\  Terminal IO
\ -----------------------------------------------------------------------------

: usb-connected? ( -- ? ) pause IFG2 c@ 16 and 0<> ; \ Are we configured already?

: usb-emit? ( -- ? ) pause IFG2 c@ 8 and 0<> ;
: usb-key?  ( -- ? ) pause IFG2 c@ 4 and 0<> ;
: usb-emit  ( c -- ) begin usb-emit? until UCB0TXBUF c! ;
: usb-key   ( -- c ) begin usb-key?  until UCB0RXBUF c@ ;

\ -----------------------------------------------------------------------------
\  Terminal redirection
\ -----------------------------------------------------------------------------

: serial-terminal ( -- )
  ['] serial-key?  hook-key?  !
  ['] serial-emit? hook-emit? !
  ['] serial-key   hook-key   !
  ['] serial-emit  hook-emit  !
;

: serial-hdmi-emit ( c -- ) dup hdmi-emit serial-emit ;

: serial-hdmi-terminal ( -- )
  ['] serial-key?      hook-key?  !
  ['] serial-emit?     hook-emit? !
  ['] serial-key       hook-key   !
  ['] serial-hdmi-emit hook-emit  !
;


: usb-terminal ( -- )
  ['] usb-key?  hook-key?  !
  ['] usb-emit? hook-emit? !
  ['] usb-key   hook-key   !
  ['] usb-emit  hook-emit  !
;

: usb-hdmi-emit ( c -- ) dup hdmi-emit usb-emit ;

: usb-hdmi-terminal ( -- )
  ['] usb-key?       hook-key?  !
  ['] usb-emit?      hook-emit? !
  ['] usb-key        hook-key   !
  ['] usb-hdmi-emit  hook-emit  !
;

\ -----------------------------------------------------------------------------
\  Terminal selection and prompt
\ -----------------------------------------------------------------------------

: +hdmi ( -- ) usb-connected? if usb-hdmi-terminal else serial-hdmi-terminal then ;
: -hdmi ( -- ) usb-connected? if      usb-terminal else      serial-terminal then ;

: prompt ( -- )
  begin
    highlight
    query
    normal
    interpret
    ."  ok." cr
  again
;

\ -----------------------------------------------------------------------------
\  Initialisation
\ -----------------------------------------------------------------------------

: Flamingo cr
  ."      _" cr
  ."     ^-)" cr
  ."      (.._          .._" cr
  ."       \`\\        (\`\\        (" cr
  ."        |>         ) |>        |)" cr
  ." ______/|________ (7 |` ______\|/_______a:f" cr
;

: welcome ( -- )
  ." Mecrisp by Matthias Koch"
  cr
  Flamingo
  cr
  ." Have a nice day !" cr
;

: init ( -- )
  page +hdmi welcome
  begin key? while key drop repeat \ Alle aufgelaufenen Zeichen verwerfen
  ['] prompt hook-quit ! quit
;

cornerstone eraseflash
reset
