⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mnpdrvr.c

📁 汇编源代码大全2
💻 C
📖 第 1 页 / 共 2 页
字号:
/*=====================================================================

                      The Microcom MNP Library
        				(Microsoft C Version)


	11/4/87 - explicit data typing...gp
	8/27/87 - rcv_lt() modified to use lcb.lcl_credit..gp

-----------------------------------------------------------------------

        LDRIVER - MNP Link RS-232 Interrupt Service Routines

     This module implements asynchronous I/O for IBM PC-compatible
     machines and thus much of this code may not be portable to 
     other dissimilar environments.

     This code implements Class 2 of the MNP link protocol,
     i.e. it uses the stop/start byte-oriented framing technique.
     A Class 3 implementation is not possible in the IBM PC 
     environment since the communications hardware is not capable
     of switching to synchronous (bit-oriented) operation.
     Protocol messages and procedures, however, are independent
     of the framining technique.		

     Global routines:

	- ascode: service I/O interrupt 
	- lne_stat: check carrier status 
	- trigger_sf: initiate transmit interrupts

=====================================================================*/

/* Header files
*/
#include <dos.h>
#include <mnpdat.h>

/* Interrupt-related definitions
*/
#define INT_8259A_PORT	0x20
#define EOI		0x20
#define RCV_DATA	4
#define THR_EMPTY	2
#define MODSTAT_CHNG	0

/* Send framer state names
*/
#define SF_INIT 	0
#define SF_DLE		1
#define SF_STX		2
#define SF_INFO 	3
#define SF_DLEINFO	4
#define SF_ETX		5
#define SF_FCS1 	6
#define SF_FCS2 	7

/* Receive framer state names
*/
#define RF_INIT 	0
#define RF_DLE		1
#define RF_STX		2
#define RF_HDLEN	3
#define RF_HEADER	4
#define RF_INFOL	5
#define RF_INFO 	6
#define RF_FCS1 	7
#define RF_FCS2 	8

/* Frame octet definitions
*/
#define SYN		0x16
#define DLE		0x10
#define ETX		3
#define STX		2

/* Length of header buffer (maximum acceptable LPDU header)
*/
#define MAX_HDRLEN	100

/* Bit set, clear and test definitions
*/
#define SETBIT1(bit)	lcb.status_1 |= bit;	   
#define SETBIT2(bit)	lcb.status_2 |= bit;
#define SETBIT3(bit)	lcb.status_3 |= bit;
#define CLRBIT1(bit)	lcb.status_1 &= ~bit;	     
#define CLRBIT2(bit)	lcb.status_2 &= ~bit;
#define CLRBIT3(bit)	lcb.status_3 &= ~bit;
#define BIT1SET(bit)	(lcb.status_1 & bit)	     
#define BIT2SET(bit)	(lcb.status_2 & bit)
#define BIT3SET(bit)	(lcb.status_3 & bit)

/* External references
*/
extern USIGN_16 iir_add;			/* int id reg address */
extern USIGN_8 linestat;		/* line status var */
extern USIGN_16 fcw_tm;			/* window timer */
extern struct link_ctl_blk lcb;  /* link control block */
extern struct BUFFER rb, ftb, rlkb;  /* buffers */
extern USIGN_8 *rb_iptr;		/* rcv buf insert pointer */
extern USIGN_16 rb_cnt,tbcnt;		/* buffer counts */
extern USIGN_8 rbuf[RBUF_LEN];	/* receive buffer */
extern USIGN_16 ln_tm;

/* Function definitions
*/
void snd_framer();
void rcv_framer();
void mod_stat();
void rcv_lt();
void rcv_la();
void rcv_lna();
void rcv_ln();

/* Local data
*/
USIGN_8 *sf_ptr,
     *rf_hptr,
     *rf_dptr;
USIGN_8 snd_char,
     rcv_char;
struct 
	{
	USIGN_8 low;
	USIGN_8 hi;
	}
		snd_fcs,			/* send fcs */
  		rcv_fcs,			/* calculated receive fcs */
  		rfcs;			/* received fcs */
USIGN_16 rdle_flg;		/* 'previous char DLE' flag */
USIGN_16 rf_hdrlen,				/* receive header length  */
    hdrcnt,			
    rf_datlen,
    sf_state,				/* send framer state var */
    rf_state,				/* receive framer state var */
    sf_len,
    sf_busy,				/* 'send frame' flag */
    sf_lt,				/* 'sending LT' flag */
    frame_snt,				/* 'info field sent' flag */ 
    frame_rcvd,				/* 'frame received' flag */
    frame_dne;				/* 'frame sent' flag */
USIGN_8 *rf_tiptr;

struct MNP_CB *p_mnpcb;
struct BLST *dat_struct, *hdr_struct;

USIGN_16 modem_out_busy;	/* RS-232 hardware status flag
					          0=transmitter not active
					          1=transmitter active */

/* Function declarations
*/
USIGN_8 get_char();

/*GLOBAL***************************************************************

	ascode - MNP RS-232 interrupt service routine

     This routine is called by 'async' (in module 'async.asm')
     to service an async I/O interrupt.

**********************************************************************/

ascode()
{

USIGN_8 int_id_reg;			/* value in int id reg */

/* Handle interrupts for as long as there are any. 
*/
for (;;)
	{

/* Read the Interrupt Identification Register.  If bit 0 is on, 
** there is no interrupt.  In this case cancel the interrupt by writing ** the "end-of-interrupt" byte to the Oper Ctl Word 2 and return. 
*/
	if ((int_id_reg = inp(iir_add)) & BIT0_ON)
		{
		outp(INT_8259A_PORT, EOI);
		return;
		}

/* Since bit 0 is off, there is an interrupt.  Take action based
** on the type of interrupt. 
*/
	switch (int_id_reg)
		{

/* Receive buffer full interrupt
*/
		case RCV_DATA:				
			rcv_framer();
			break;

/* Transmit buffer empty interrupt
*/
		case THR_EMPTY:
			snd_framer();
			break;

/* Modem status change interrupt
*/
		case MODSTAT_CHNG:			
			mod_stat();
			break;
		}
	}
}

/*GLOBAL***************************************************************

	lne_stat - physical-connection status routine

**********************************************************************/

SIGN_16 lne_stat()
{

/* If the carrier detect bit is 1, return success (physical-connection
** still active).  Otherwise, return with a failure indication. 
*/
if (linestat & 0x80)
    return (SUCCESS);
else
    return (1);
}

/*GLOBAL***************************************************************

	trigger_sf - initiate send framer

**********************************************************************/

void trigger_sf()
{

extern USIGN_16 port_add;

/* If the RS-232 transmitter is not already busy, then a byte must 
** be placed in the transmit buffer to begin interrupt-driven sending.
** This is the start of a frame and thus the first byte to be sent is
** the SYN which begins the frame start flag.  Also the send framer's
** state variable is initialed here.
*/ 
if (!modem_out_busy)
	{
	modem_out_busy = 1;
	sf_state = SF_DLE;				
	outp(port_add, SYN);
	}
}

/*LOCAL----------------------------------------------------------------

	get_char - get byte from send buffer

---------------------------------------------------------------------*/

USIGN_8 get_char()
{

/* Return the byte at the current send framer pointer position.
** In the process, advance the point for next time and reduce the
** number of bytes remaining by one.
*/
sf_len--;					
return (*sf_ptr++);
}

/*LOCAL----------------------------------------------------------------

	mod_stat - modem status change interrupt service routine

---------------------------------------------------------------------*/

void mod_stat()
{

/* Read modem status register and save the value.
*/
linestat = inp(iir_add + 4);

/* If carrier has been lost, signal the mainline that the link
** also has been lost by lowering the 'link established' flag. 
*/
if (!(linestat & 0x80))
    CLRBIT1(LINK_EST)	

}

/*LOCAL----------------------------------------------------------------

	put_char - store an LT data byte

---------------------------------------------------------------------*/

void put_char()
{

/* Ignore this byte if the amount of received data exceeds the
** negotiated maximum user data size for the LT LPDU.  This could occur
** if the frame is broken or if the correspondent is misbehaving. 
*/
if (rf_datlen >= lcb.max_data_sz)
	return;

/* Otherwise, accept this byte. 
*/
rf_datlen++;				
fcscalc (&rcv_fcs, rcv_char);

/* Now store the data byte in the receive buffer using the temporary
** insert pointer.  Advance the pointer, checking for wrap.
*/
*rf_tiptr++ = rcv_char;			
if (rf_tiptr >= rbuf + RBUF_LEN)
	rf_tiptr = rbuf;

}

/*LOCAL----------------------------------------------------------------

	put_hdr - store an LPDU header byte

---------------------------------------------------------------------*/

void put_hdr()
{

/* Store header byte in header buffer.  Increment count of bytes in
** the buffer and include the byte in the receive fcs.
*/
rf_hdrlen++;
*rf_hptr++ = rcv_char;			      
fcscalc (&rcv_fcs, rcv_char);

}

/*LOCAL----------------------------------------------------------------

	RCV_FRAMER - Receive Framer

---------------------------------------------------------------------*/

void rcv_framer()
{

extern USIGN_16 port_add;

/* Get received data byte and save it for later
*/
rcv_char = inp(port_add);

/* Take action based on current state of receive finite state machine.
** 'rf_state' is the state variable.
*/
switch (rf_state)
	{

/* State RF_INIT - This is the initial state.  Search the received
** data stream for a SYN character which may be the beginning
** of the start flag sequence of a byte mode frame. 
*/
	case RF_INIT:
		if (rcv_char == SYN)
			rf_state++;	
		break;

/* State RF_DLE - Search for the second byte in the start flag, a DLE
** byte.  Go back to the initial state (SYN search) if this byte is
** something other than a DLE.
*/
	case RF_DLE:
		if (rcv_char == DLE)
			 rf_state++; 
		else 
			rf_state--;		
		break;

/* State RF_STX - If the received byte is an STX then a frame start flag
** has been found.	In this case, initialize to receive an LPDU.
** Otherwise, go back to looking for a SYN. 
*/
	case RF_STX:
		if (rcv_char == STX)
			{
			get_b(&rlkb,&hdr_struct);
			rf_hptr = hdr_struct->bptr;
			hdrcnt=rf_hdrlen=rf_datlen=rdle_flg=0;
			rcv_fcs.hi=rcv_fcs.low=NULL;
	    		rf_state++; 		
	    		}
		else				
	    		rf_state = RF_INIT; 	    
		break;

/* State RF_HDLEN - Get the first byte of the LPDU, the length
** indicator.  Wait for the next byte if this happens to be a DLE (in a
** good frame the DLE will be doubled).  If the value  received is
** greater than this implementation's maximum header length, it must be
** a protocol error or the value is broken.  In either case, ignore this
** LPDU. This is like a broken frame, so force the sender to ack. 
*/
	case RF_HDLEN:
		if ((rcv_char != DLE) || ((rcv_char == DLE) && rdle_flg))
			{
			rdle_flg = 0;		
		
			if (rcv_char > MAX_HDRLEN)
				{
				ret_b(&rlkb, hdr_struct);	
				SETBIT2(FORCE_ACK)
				rf_state = RF_INIT;		
				}
	    		else
				{
				hdrcnt = rcv_char;
				put_hdr();
				rf_state++;
				}
	    		}
		else
	    		rdle_flg = 1;
		break;

/* State RF_HEADER - Receive header bytes, assuming the length indicator
** was correct.  Any bytes following the header will temporarily be
** assumed to be data.
*/
	case RF_HEADER:
		if ((rcv_char != DLE) || ((rcv_char == DLE) && rdle_flg)) 
			{
	    		rdle_flg = 0;
	    		put_hdr();
	    		if (!(--hdrcnt))
				rf_state++;
	    		}
		else
	    		rdle_flg = 1;
		break;

/* State RF_INFOL -  Receive first data byte.  Note that we must check
** for the stop flag just in case the length indicator byte was broken.  ** When the first data byte is received, the temporary insert pointer
** into the data receive buffer is initialized to the current location

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -