
$40000010 constant porta-in
$40000020 constant porta-out
$40000040 constant porta-dir
$40000080 constant leds-out

$40000100 constant portb-in
$40000200 constant portb-out
$40000400 constant portb-dir
$40000800 constant buttons

$40001000 constant oled-in
$40002000 constant oled-out
$40004000 constant oled-dir
$40008000 constant analog-out

$40040000 constant ticks
$40080000 constant reload

$40100000 constant adc-in
$40200000 constant adc-out
$40400000 constant sd-in
$40800000 constant sd-out

$41000000 constant usb-data
$42000000 constant usb-ctrl

: clear  ( x addr -- )  4 + ! inline ;
: set    ( x addr -- )  8 + ! inline ;
: toggle ( x addr -- ) 12 + ! inline ;

: leds ( x -- ) leds-out ! ;

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

: ms ( u -- ) 40000 * delay-ticks ;
: us ( u -- )    40 * delay-ticks ;

: random ( -- x ) 0 32 0 do 10 us 2* buttons @ 6 rshift or loop ;

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

: usb-connected? ( -- ? ) pause usb-ctrl @ $400 and 0<> ; \ Are we configured already?

: usb-emit? ( -- ? ) pause usb-ctrl @ $200 and 0= ;
: usb-key?  ( -- ? ) pause usb-ctrl @ $100 and 0<> ;
: usb-emit  ( c -- ) begin usb-emit? until usb-data c! ;
: usb-key   ( -- c ) begin usb-key?  until usb-data 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
\ -----------------------------------------------------------------------------

: init ( -- )

  [ifdef] init
  init
  [then]

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