
\ #######   MEMORY   ##########################################

: unused ( -- u ) $4000 here - ;

\ #######   IO   ##############################################

: ms   ( u -- ) 0 do 4545 0 do loop loop ; \ 11 cycles per loop run. 1 ms * 100 MHz / 11 = 4545.45

: randombit ( -- 0 | 1 ) $2000 io@ 2 rshift 1 and ;
: random ( -- x ) 0  16 0 do 2* randombit or 100 0 do loop loop ;

: ticks ( -- u ) $4000 io@ ;

: nextirq ( cycles -- ) \ Trigger the next interrupt u cycles after the last one.
  ticks  \ Read current tick
  -       \ Subtract the cycles already elapsed
  4 -      \ Correction for the cycles neccessary to do this
  negate    \ Timer counts up to zero to trigger the interrupt
  $4000 io!  \ Prepare timer for the next irq
;

: reset ( -- ) 1 $8000 io! ;

$608C $3FFE ! \ Location $3FFE is an interrupt vector ! Place ALU exit opcode here.
