\ SPI Driver                                            
REQUIRED FC!
XASM
MACRO: SO       PORTB 5  MACRO;         \ Data Output
MACRO: SI       PORTB 3  MACRO;         \ Data Input
MACRO: SCK      PORTB 2  MACRO;         \ Clock Output
IN-META
T: SPI-SELECT   M[ BCF PORTB 4 ]M  T;   \ Select line 
T: SPI-DESELECT M[ BSF PORTB 4 ]M  T;
 
: SOFT-SPI-INIT $10 PORTB C! \ Set port and direction
$CB TRISB C! ;
 
CODE SPI ( b - 'b )
N 8 D0 RLF (FSR) \ Get next bit to send
BCF (FSR) 0 \ Assume bit in is zero
BCF SO \ Assume bit out is zero
BTFSC STATUS CY \ Check bit out
BSF SO \ Was one so set out high
BSF SCK \ Set CLK high
NOP \ May need a delay if >4MHz PIC
BTFSC SI \ Check SI input
BSF (FSR) 0 \ Input high so bit in is one
BCF SCK \ Return CLK low
LOOP RETURN C; \ Loop for 8 bits, answer on stack