#include "common.h"

unsigned short bit_phase=0;	/* bit phase */

int get_bit_raw()
{
	/* This function implements a digital PLL for data recovery */
        
	static float x0;
	int i,j,f,p;
        float x,ii=0.0;

        j=bit_phase;
        if (j>0x8000) j-=0x10000;
	for (f=p=0;j<0x10000;j+=fskdp->phs){
		x=demodulator();
                ii+=x;
		if ((x*x0<0.0)) {	/* Data Change */
			if (!f) {
				if (j>0x8000) p=2048;	/* Early */
				else p=-2048;		/* Late */
                                f=1;
			}
		}
		x0=x;
	}
        bit_phase=(j+p)&0xffff;
        return (ii>0)?0x80:0;
}
