$60000000	constant	fl_base
&0		constant	addr_status	
&1		constant	addr_reset
&2		constant	addr_cpld_id
&3		constant	addr_mode_select
&4		constant	addr_spi
&5		constant	addr_dcdc_ctrl
&6		constant	addr_chop
&8		constant	addr_delay_adjust
&9		constant	addr_delay_config0
&10		constant	addr_delay_config1
&11		constant	addr_delay_config2
&12		constant	addr_mux
&13		constant	addr_mux_en
&14		constant 	addr_le_dp

\ Turn on the Flasher Board
: Fon 2 $500000f9 c! ;

\ Turn off the Flasher Board
: Foff 0 $500000f9 c! ; : foff disableFL ;

\ Buzz Adr Lines
: aBuzzw0 64 0 ?DO 0 fl_base i + c! LOOP  ;
: aBuzzw1 64 0 ?DO $FF fl_base i + c! LOOP  ;
: aBuzzr 64 0 ?DO fl_base i + c@ drop LOOP  ;
: aTst 0 ?DO aBuzzw0 aBuzzw1 aBuzzr LOOP ;


\ Reset the Flasher Board
: Freset 1 fl_base addr_reset + c! ;

\ Read the Flasher Board CPLD firmware version number
: Fver fl_base addr_cpld_id + c@ . drop ;

\ TUrn on DCDC
: onDCDC 1 fl_base  addr_dcdc_ctrl + c! ;

\ TUrn off DCDC
: offDCDC 0 fl_base  addr_dcdc_ctrl + c! ;

\ Write a byte to the programmable delay line
\ value setDelay
: DelayOn 1 fl_base addr_le_dp + c! ;
: setDelay fl_base addr_delay_adjust + c! ;

\ Write to L[3..0]
\ value setConfig0
: setConfig0 fl_base addr_delay_config0 + c! ;

\ Write to L[7..4]
\ value setConfig1
: setConfig1 fl_base addr_delay_config1 + c! ;

\ Write to L[11..8]
\ value setConfig2
: setConfig2 fl_base addr_delay_config2 + c! ;

\ SPI Support  Bits are: CLk = 0, Data = 1, CS = 2

: spi fl_base addr_spi + c! ;
: ss 0 spi ; \ selected, data low, clock low, cs on
: sd 4 spi ; \ selected, data low, clock low, cs off
\ SPI Mode ON ( - ) 
: onSPI 1 fl_base addr_mode_select + c! sd ;
: offSPI 0 fl_base addr_mode_select + c! sd ;
: hidata 2 spi 3 spi 2 spi ;
: lodata 0 spi 1 spi 0 spi ;
: s7 dup 128 and if hidata  else lodata  endif ;
: s6 dup 64  and if hidata  else lodata  endif ;
: s5 dup 32  and if hidata  else lodata  endif ;
: s4 dup 16  and if hidata  else lodata  endif ;
: s3 dup 8   and if hidata  else lodata  endif ;
: s2 dup 4   and if hidata  else lodata  endif ;
: s1 dup 2   and if hidata  else lodata  endif ;
: s0 dup 1   and if hidata  else lodata  endif ;
\ setV (data -) \ 0 to 255
: sss ss s7 ss s6 ss s5 ss s4 ss s3 ss s2 ss s1 ss s0 ss sd ;
: setV onSPI sss drop offSPI ;

\ Trigger stuff ( - )
: t0 $0000000 $90081018 ! ;
: t1 $1000000 $90081018 ! ;
: trig t0 t1 t0 ;
: pt0 $0000000 $90081018 ! ;
: pt1 $4000000 $90081018 ! ;
: aux_reset pt0 pt1 pt0 ;


\ Write to the mux enable byte
\ value setflmux
: setflmux   fl_base addr_mux + c! ;
: setflmuxen fl_base addr_mux_en + c! ;

: muxoff $FC setflmuxen ;

\ (n - ) 0,1,2, or 3 == mux input to select.
: muxaon $18 setflmuxen  $0 + setflmux ;
: muxbon $14 setflmuxen  $4 + setflmux ;
: muxcon $0C setflmuxen  $8 + setflmux ;
: ttl $1C setflmuxen  $C setflmux ;

: V1  $0 muxaon ;
: V2  $1 muxaon ;
: V3  $0 muxbon ;
: V4  $1 muxbon ;
: V5  $0 muxcon ;
: V6  $1 muxcon ;

: V7  $2 muxaon ;
: V8  $3 muxaon ;
: V9  $2 muxbon ;
: V10 $3 muxbon ;
: V11 $2 muxcon ;
: V12 $3 muxcon ;



\ Startup Sequence Flasher Board
: ledson 15 setConfig0 15 setConfig1 15 setConfig2 ;
: ledsoff 0 setConfig0 0 setConfig1 0 setConfig2 ;
\ no video mux
: d0  ledsoff muxoff ;
: d1  ledsoff 1 setConfig0  muxoff ; 
: d2  ledsoff 2 setConfig0  muxoff ; 
: d3  ledsoff 4 setConfig0  muxoff ; 
: d4  ledsoff 8 setConfig0  muxoff ; 
: d5  ledsoff 1 setConfig1  muxoff ; 
: d6  ledsoff 2 setConfig1  muxoff ; 
: d7  ledsoff 4 setConfig1  muxoff ; 
: d8  ledsoff 8 setConfig1  muxoff ; 
: d9  ledsoff 1 setConfig2  muxoff ; 
: d10 ledsoff 2 setConfig2  muxoff ; 
: d11 ledsoff 4 setConfig2  muxoff ; 
: d12 ledsoff 8 setConfig2  muxoff ;
: da  ledsoff 1 setConfig0 4 setConfig1 ;
: db  ledsoff 2 setConfig0 8 setConfig1 ;
: dc  ledsoff 4 setConfig0 1 setConfig2 ;
: dd  ledsoff 8 setConfig0 2 setConfig2 ;
: de  ledsoff 1 setConfig1 4 setConfig2 ;
: df  ledsoff 2 setConfig1 8 setConfig2 ;
: dleft  ledsoff 7 setConfig0 12 setConfig1 1 setConfig2 ;
: dright ledsoff 1 8 setConfig0 3 setConfig1 14 setConfig2 ;
\ With video mux
: D0  d0 ;
: D1  d1 V1 ; 
: D2  d2 V2 ; 
: D3  d3 V3 ; 
: D4  d4 V4 ; 
: D5  d5 V5 ; 
: D6  d6 V6 ; 
: D7  d7 V7 ; 
: D8  d8 V8 ; 
: D9  d9 V9 ; 
: D10 d10 V10 ; 
: D11 d11 V11 ; 
: D12 d12 V12 ;
: pupw 5000 usleep ;
: Pup Fon pupw Freset pupw  aux_reset pupw  onDCDC pupw  ;
: startFL Pup  muxoff  0 setV DelayOn  ;

: bright 127 setV ;
: see 96 setV ;
: dim 0 setV ;
: long 127 setDelay ;
: med 64 setDelay ;
: short 32 setDelay ;

: 1ms 1000 usleep ;
: 1sec 1000 0 ?DO 1ms LOOP ;

: vramp dim 1sec see 1sec bright 1sec ;
: nramp 0 ?DO vramp LOOP ;

\ trig once wait 1ms
: flash trig 1000 usleep ;
\ ( n -) flash n times at 1ms rate
: nflash 0 ?DO  flash LOOP ;
\ ( n -) flash for n seconds
: sflash 1000 * nflash ;

\ Flash one at a time 
: L0 D0 100 nflash ;
: L1 D1 100 nflash ;
: L2 D2 100 nflash ;
: L3 D3 100 nflash ;
: L4 D4 100 nflash ;
: L5 D5 100 nflash ;
: L6 D6 100 nflash ;
: L7 D7 100 nflash ;
: L8 D8 100 nflash ;
: L9 D9 100 nflash ;
: L10 D10 100 nflash ;
: L11 D11 100 nflash ;
: L12 D12 100 nflash ;

: Lall L0 L1 L2 L3 L4 L5 L6 L7 L8 L9 L10 L11 L12 ;
: sLall  0 ?DO Lall LOOP ;

: Z0 L0 D0 nflash ;
: Z1 L1 D1 nflash ;
: Z2 L2 D2 nflash ;
: Z3 L3 D3 nflash ;
: Z4 L4 D4 nflash ;
: Z5 L5 D5 nflash ;
: Z6 L6 D6 nflash ;
: Z7 L7 D7 nflash ;
: Z8 L8 D8 nflash ;
: Z9 L9 D9 nflash ;
: Z10 L10 D10 nflash ;
: Z11 L11 D11 nflash ;
: Z12 L12 D12 nflash ;

: Pass ;
