*Code Window
******************************
** User Program Starts Here **
******************************
* USER PROGRAM must start with the version number
* User supplied program must poll for new messages
*	 (OSPOLL) at least every RDELAY delay cycles
* User program can use loader supplied subroutines
* User Program must be no larger then 3840 bytes or
*	 run over USERPROGEND
* Program has access to FOOA - FOOD, contiguous 16-bit vars
***************************************************

 ORG USERPROG

*
* Contagious Loader
*

*
* FOOA accumulates the total delay
* FOOB contains the drift direction
* FOOD contains my delay
*
* This firefly program implements:
*				   
*

VER		FCB $09                 version number is 9
PROGBEGIN	LDD SENDID		get a unique ID
		CLRA
		STD FOOD		store it as my delay

		CLRA
		PSHA
STARTLOOP	PULA
		COMA			blink
		STAA PORTB		flash lights
		PSHA

		LDB #$01		broadcast the number in A
		LDY #$0010		broadcast only 16 times
		JSR BROADCAST		flash on the air

		LDY #$0600		set natural frequency
		JSR YDELAY		(don't listen to world)

		LDY FOOD
DELAYLOOP	LDAB #$01		specify delay factor
		JSR DELAYINT		delay
		CMPB #$04
		BHS SENSEHIT		was the sensor hit?
		CMPB #$00
		BEQ DONELOOP		done loop?
		LDAB #$00		was it a flash?
		CMPB INMSGNUM
		BEQ DONEMSG		false alarm if not
		COMB
		CMPB INMSGNUM
		BNE DELAYLOOP

DONEMSG		LDD FOOD
		SUBD #$02
		PULA
		LDAA INMSGNUM		set flash to that of neighbors
		COMA
		PSHA

FIXNUM		CLRA
		STD FOOD
		BRA STARTLOOP

DONELOOP	LDD FOOD		drift up if no flash is heard
		ADDD #$01
		BRA FIXNUM

SENSEHIT	LDD FOOD		mess up the frequency if sensor hit
		ADDD #$10
		BRA FIXNUM		mess up the phase

*
* Must have this to signal end of user program.
*

PROGEND		NOP

*