1. 程式人生 > >八.乙太網組播地址過濾暫存器的計算

八.乙太網組播地址過濾暫存器的計算

//作者laogu  http://www.laogu.com
//程式的功能是根據多播地址multicast_address[6]的值,計算出MAR0-MAR7,就是multicast_filter[8];
//本程式指計算一個多播地址。如果有多個多播地址,將每個多播地址生成的multicast_filter[8]相或就可以了,
//例如根據多播地址 01:00:5e:00:00:01生成的 value1=multicast_filter[8];
//根據多播地址     01:00:5e:00:00:02生成的 value2=multicast_filter[8];
//那麼對這兩個多播地址生成的multicast_filter[8]=value1  |  value2 ;將兩個值相或
//很容易從這裡得到如果要接收所有多播地址的資料包,MAR0--MAR7必須設定為0xff,就是說
//multicast_filter[8]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
#define al ax_value.bytes.low
#define bl bx_value.bytes.low
#define cl cx_value.bytes.low
#define ah ax_value.bytes.high
#define bh bx_value.bytes.high
#define dh dx_value.bytes.high
#define ax ax_value.word
#define bx bx_value.word
#define cx cx_value.word
#define dx dx_value.word
#define shl_bx if((bx&0x8000)!=0){cf=1;}else{cf=0;};bx=bx<<1;
#define shl_al if((al&0x80)!=0){cf=1;}else{cf=0;};al=al<<1;
#define shr_al if((al&0x01)!=0){cf=1;}else{cf=0;};al=al>>1;
#define rcl_dx if((dx&0x8000)!=0){cf_temp=1;}else{cf_temp=0;};dx=dx<<1;if(cf){dx=dx+1;};cf=cf_temp;
#define rcl_ah if((ah&0x80)!=0){cf_temp=1;}else{cf_temp=0;};ah=ah<<1;if(cf){ah=ah+1;};cf=cf_temp;
#define rcr_ah if((ah&0x01)!=0){cf_temp=1;}else{cf_temp=0;};ah=ah>>1;if(cf){ah=ah+0x80;};cf=cf_temp;
#define rol_al if((al&0x80)!=0){cf=1;}else{cf=0;};al=al<<1;if(cf){al=al+1;};
#define ror_al if((al&0x01)!=0){cf=1;}else{cf=0;};al=al>>1;if(cf){al=al+0x80;};
union u {unsigned int word;
         struct{unsigned char high;unsigned char low;}bytes;//位元組順序為高位在前的2byte結構
   };
union u ax_value;
union u bx_value;
union u cx_value;
union u dx_value;
bit cf;
bit cf_temp;
unsigned char multicast_address[6]={0x01,0x00,0x5e,0x00,0x00,0x00};
//unsigned char multicast_address[6]={0x00,0x00,0x00,0x5e,0x00,0x01};  //只計算一個多播地址
//unsigned char multicast_address[6]={0x01,0x80,0xc2,0x00,0x00,0x00};
unsigned char multicast_filter[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
                                      //結果放在這裡,就是MAR0--MAR7的值
//;;0x41,0x00,0x00,0x80,0x00,0x00,0x00,0x00;
void up_crc(unsigned char al_byte)
{
al=al_byte;
ah=0;