
\ ------------------------------------------------------------------------------
\   Blinky: Let it shine !
\ ------------------------------------------------------------------------------

\ Pins 0 to 15:

$10080000 constant GPIO0_DATA
$100BFFFC constant GPIO0_DATA_ALL
$100C0000 constant GPIO0_DIR

\ Pins 16 to 31:

$10180000 constant GPIO1_DATA
$101BFFFC constant GPIO1_DATA_ALL
$101C0000 constant GPIO1_DIR

\ Direction is selects 0: Input, 1: Output.
\ Data bits are masked using 16 address bits, shifted two places,
\ which are ORed to the base address.

\ Active low RGB LED is connected to GPIO pins 22, 23 and 24.


\ ------------------------------------------------------------------------------
\   Accurate delays
\ ------------------------------------------------------------------------------

   100 constant cycles/us  \ For 100 MHz
100000 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 -- ) cycles/ms * delay-cycles ;


: blinky ( -- )

  %111 22 16 - lshift GPIO1_DIR !

  0
  begin
    1+
    dup
      dup 1 rshift xor \ Convert to gray code
      %111 and 22 16 - lshift GPIO1_DATA_ALL !
    500 ms
  key? until
  drop

;
