
\ -----------------------------------------------------------------------------
\   Darkness measurement on Icebreaker with assistance of timers in logic
\ -----------------------------------------------------------------------------

\ Connections of four LEDs to PMOD1B pins:

\ Pin 7 | A1 A2 A3 A4 (GND) (3.3) | Pin 12
\ Pin 1 | C1 C2 C3 C4 (GND) (3.3) | Pin 6

\ -----------------------------------------------------------------------------
\   Millisecond timebase
\ -----------------------------------------------------------------------------

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

   24 constant cycles/us  \ For 24 MHz
24000 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 ;


\ 24 MHz hinein, Teile durch 24000.

: 2variable ( d -- ) create , , 0 foldable ;

0. 2variable (ms)

: interrupt ( -- )
  (ms) 2@ 1. d+ (ms) 2!
  cycles/ms nextirq
;

: time ( -- ud )
  begin
    (ms) @        \ High-Teil
    (ms) cell+ @  \  Low-Teil
    over ( high low high )
    (ms) @ ( high low high high* )
    =
  until
  swap
;

' interrupt 1 rshift $3BFE ! \ Generate JMP opcode for vector location

\ -----------------------------------------------------------------------------
\   Darkness measurement with logic support
\ -----------------------------------------------------------------------------

$8011 constant led01in
$8012 constant led01out
$8013 constant led01dir
$8014 constant led01timer-low
$8015 constant led01timer-high

$8021 constant led02in
$8022 constant led02out
$8023 constant led02dir
$8024 constant led02timer-low
$8025 constant led02timer-high

$8031 constant led03in
$8032 constant led03out
$8033 constant led03dir
$8034 constant led03timer-low
$8035 constant led03timer-high

$8041 constant led04in
$8042 constant led04out
$8043 constant led04dir
$8044 constant led04timer-low
$8045 constant led04timer-high

\ -----------------------------------------------------------------------------

: esc? ( -- ? ) key? if key 27 = else false then ;

 8 constant cleartimer
 4 constant runmeasurement

 2 constant cathode
 1 constant anode

: darkness ( -- )

  cr cr
  dint 0. (ms) 2! eint \ Clear millisecond counter

  begin

    time 16 d.r 5 spaces

    anode cathode or cleartimer or led01dir io!  anode led01out io!
    anode cathode or cleartimer or led02dir io!  anode led02out io!
    anode cathode or cleartimer or led03dir io!  anode led03out io!
    anode cathode or cleartimer or led04dir io!  anode led04out io!

    1 ms

    anode cathode or led01dir io!
          cathode    led01out io!

    anode cathode or led02dir io!
          cathode    led02out io!

    anode cathode or led03dir io!
          cathode    led03out io!

    anode cathode or led04dir io!
          cathode    led04out io!

    10 0 do loop

    cathode runmeasurement or led01dir io! \ Switch anode as input
    cathode runmeasurement or led02dir io!
    cathode runmeasurement or led03dir io!
    cathode runmeasurement or led04dir io!

    begin led01in io@ anode and 0<> until \ Wait for discharge on all four channels
    begin led02in io@ anode and 0<> until
    begin led03in io@ anode and 0<> until
    begin led04in io@ anode and 0<> until

    0 led01dir io! \ Off
    0 led02dir io!
    0 led03dir io!
    0 led04dir io!

    led01timer-low io@ led01timer-high io@  16 d.r \ Read and print the timer values
    led02timer-low io@ led02timer-high io@  16 d.r
    led03timer-low io@ led03timer-high io@  16 d.r
    led04timer-low io@ led04timer-high io@  16 d.r

    5 spaces \ Same again in hexadecimal
    led01timer-high io@ .x led01timer-low io@ .x space
    led02timer-high io@ .x led02timer-low io@ .x space
    led03timer-high io@ .x led03timer-low io@ .x space
    led04timer-high io@ .x led04timer-low io@ .x space

    cr

  esc? until
;

darkness

