if(IT_IN(1)) {
if(first) {
nt=0;
nr=0;
wait=FALSE;
rr=1;
rej=0;
first=0;
if(debugFlag)
fprintf(debug_F,"txhdlc: first nt=%d nr=%d \n",nt,nr);
} else {
wait=FALSE;
rc=r(0);
nr=rc&0x07;
rc &= 0xf0;
rej=(rc == 0x90) ? 1:0;
rr= (rc == 0x80) ? 1:0;
if(rej) {
if(debugFlag)
fprintf(debug_F,"txhdlc: reject nt=%d nr=%d \n",nt,nr);
nt= nr;
if(nt < 0) nt=7;
}
else {
if(debugFlag)
fprintf(debug_F,"txhdlc: accept nt=%d nr=%d \n",nt,nr);
nt=nr;
}
}
} else {
wait=TRUE;
}
if(!rej ) {
while(IT_IN(0)) {
/*
* collect the bits
* and store them in a buffer
* first info frames are stored in queue zero
*/
bit=(int)(x(0)+0.001);
buildByte |= bit;
buildByteBits++;
if(buildByteBits == 8) {
queue_A[nss][byteCount]=buildByte;
buildByte=0;
buildByteBits=0;
byteCount++;
}
buildByte <<= 1;
bitCount++;
if(bitCount == numberOfBits) {
nss++;
nss %= 8;
infoComplete=1;
byteCount=0;
buildByte=0;
buildByteBits=0;
bitCount=0;
break;
}
}
}
if(wait)
return(0);
if(( infoComplete) || rej) {
if(rr) infoComplete=0;
done=0;
bitsProc=0;
while(!done) {
/*
* output HDLC Frame
*/
contFlag=1;
while(contFlag) {
if(!insertZero) {
if(outBitsCount) {
bit= (0x8000 & outBits) ? 1:0;
outBits <<= 1;
outBitsCount--;
if(begFlag && !outBitsCount) {
begFlag=0;
/*
* send address and control
*/
outBits=0x0000 | (nt << 3);
outBitsCount=16;
}
if(fcs && !outBitsCount) {
/*
* Finished outputting FCS now get ready to
* output end flag with no zero stuffing
*/
outBits=0x7e7e;
outBitsCount=16;
fcs=0;
endFlag=1;
}
} else {
/*
* collect the bits
*/
bytePos=bitsProc/8;
bitPos = 7-(bitsProc % 8);
mask=1; mask <<= bitPos;
bit = (queue_A[nt][bytePos] & mask) ? 1:0;
bitsProc++;
if(bitsProc == numberOfBits) {
endFrame=1;
bitsProc=0;
}
}
/*
* compute FCS without bit stuffing
*/
if( !(endFlag || begFlag || fcs )) {
/*
* ignore flags when computing FCS
* also do not compute when outputting FCS itself
*/
carry=(rx&0x8000) ? 1:0;
rx <<= 1;
rx ^= ((bit ^ carry) ? GENERATOR_POLY:0);
}
} else {
/*
* stuff a zero and reset flag
*/
bit=0;
insertZero=0;
}
shreg |= bit;
checkStuff= shreg & 0x1f;
if(checkStuff == 0x1f && !begFlag && !endFlag) {
/*
* five consecutive ones
* set zero insert flag
*/
insertZero=1;
}
/*
* Output bit
*/
if(IT_OUT(0) ){ KrnOverflow("txhdlc",0); return(99); }
y(0) = bit;
totalBitsOutput++;
if(endFrame) {
/*
* set fcs so that FCS is output but remainder is not computed
*/
fcs=1;
outBits=rx;
outBitsCount=16;
endFrame=0;
}
if(endFlag && !outBitsCount) {
/*
* Finished outputting Info and FCS
* and end flag
* Reset everything
*/
begFlag=1;
outBits=0x7e00;
outBitsCount=8;
rx=0;
fcs=0;
shreg=0;
frameCount++;
endFlag=0;
done=1;
contFlag=0;
insertZero=0;
}
#if 0
fprintf(stderr,"shreg=%x bit=%d\n",shreg,bit);
#endif
if(!done) {
shreg <<= 1;
shreg &= 0x1f;
if(outBitsCount )
contFlag=1;
else
contFlag=0;
}
}
}
}
return(0);