//////////////////////////////////////////////////////////////////////
// RANDOM NUMBERS WITH THE PIC
// Author: Peter Best
// Version: 1.0
// Date: 04/01/04
// Description: LFSR Random Number Routines
//////////////////////////////////////////////////////////////////////

#include <16f877.h>
#include <f877.h>
#device ICD=TRUE
#device *=16

#fuses DEBUG,HS,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOWRT
#use delay(clock=20000000)
#use rs232(baud=57600,xmit=PIN_C6, rcv=PIN_C7)
#id 0x0401

int8 shift_reg_data_low,shift_reg_data_high,result,shift_reg[2];

#bit tap15 = shift_reg_data_high.7
#bit tap14 = shift_reg_data_high.6
#bit tap13 = shift_reg_data_high.5
#bit tap12 = shift_reg_data_high.4
#bit tap11 = shift_reg_data_high.3
#bit tap10 = shift_reg_data_high.2
#bit tap9 = shift_reg_data_high.1
#bit tap8 = shift_reg_data_high.0

#bit tap7 = shift_reg_data_low.7
#bit tap6 = shift_reg_data_low.6
#bit tap5 = shift_reg_data_low.5
#bit tap4 = shift_reg_data_low.4
#bit tap3 = shift_reg_data_low.3
#bit tap2 = shift_reg_data_low.2
#bit tap1 = shift_reg_data_low.1
#bit tap0 = shift_reg_data_low.0

#bit accum7 = result.7
#bit accum6 = result.6
#bit accum5 = result.5
#bit accum4 = result.4
#bit accum3 = result.3
#bit accum2 = result.2
#bit accum1 = result.1
#bit accum0 = result.0

void main()
{

	shift_reg[0] = 0x00;
	shift_reg[1] = 0x00;
	shift_reg_data_low = 0x00;
	shift_reg_data_high = 0x00;
	result = 0;
	shift_left(shift_reg,2,1);
	while(1)
	{ 
	    
		accum0 = tap15 ^ tap14;
		accum1 = tap12 ^ tap3;
		accum7 = accum0 ^ accum1;
		
		shift_left(shift_reg,2,accum7);
		shift_reg_data_low = shift_reg[0];
	    	shift_reg_data_high = shift_reg[1];
	    	printf("%X%X\r\n",shift_reg[1],shift_reg[0]); 
	}
	
}
