txhdlc

Capsim Block Documentation

Short Description

Generate HDLC frame. Also queue (8 level) for retransmission

Top
Input Connections
Port Type Name
0 float x
1 int r
Top
Output Connections
Port Type Name
0 float y
Top
Parameters
Num Description Type Name Default Value
0 Number of bits per frame (Info Only) int numberOfBits 128
1 Debug:1=true,0=false int debugFlag 0
Top
States
Num Type Name Initial Value Description
0 UINT16 rx 0
1 UINT16 outBits 0
2 int outBitsCount 0
3 int outFlag 0
4 int count 0
5 int insertZero 0
6 int shreg 0
7 int bitCount 0
8 int endFrame 0
9 int begFlag 1
10 int endFlag 0
11 int fcs 0
12 int frameCount 0
13 int contFlag
14 int nss 0
15 int nt 0
16 int infoLengthBytes
17 char* queue_A[8]
18 int numberBitsCollected 0
19 UINT16 buildByte 0
20 int buildByteBits 0
21 int byteCount 0
22 int bitsProc 0
23 int infoComplete 0
24 int nr 1
25 int rej 0
26 int rr 1
27 int first 1
28 FILE* debug_F
29 int totalBitsOutput 0
Top

Declarations


 

unsigned short	carry;
int	bit;
int	i;
unsigned int	m;
int	numberOfSamples;
UINT16	checkStuff;
int	mask;
int	done;
int	bitPos;
int	bytePos;
int	rc;
int	wait;



Top

Initialization Code



 

if(debugFlag) {
        debug_F=fopen("txhdlc.dbg","w");
        if(debug_F == NULL) return(2);
}
rx=0;
outBitsCount=8;
outBits=0x7e00;
begFlag=1;
infoLengthBytes = numberOfBits/8+1;
for(i=0; i<8; i++) {
	queue_A[i]=(char*)calloc(infoLengthBytes,sizeof(char));
	if(queue_A[i]==NULL) {
		fprintf(stderr,"txhdlc could not allocate space\n");
		return(1);
	}
}




Top

Main Code



 


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);




Top

Wrapup Code



 

	fprintf(stderr,"TxHDLC: number of outputted frames=%d\n",frameCount);
	fprintf(stderr,"TxHDLC: number of bits outputted =%d\n",totalBitsOutput);
for(i=0; i<8; i++) 
	free(queue_A[i]);
	if(debugFlag)
		fclose(debug_F);




Top

License



/*  Capsim (r) Text Mode Kernel (TMK) Star Library (Blocks)
    Copyright (C) 1989-2017  Silicon DSP Corporation

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    http://www.silicondsp.com
    Silicon DSP  Corporation
    Las Vegas, Nevada
*/


Top

Description



 

/*
 * 
 * Written by: Sasan H. Ardalan
 *(c) Copyright 1993-2000 XCAD Corporation, Raleigh, NC All Rights Reserved
 * Date: November 30, 1993

txhdlc


Generate HDLC frame. Also queue (8 level) for retransmission


 Written by: Sasan H. Ardalan
 Date: November 30, 1993

 */