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

📄 rtl8019as_net420.h

📁 浙江大学的悟空嵌入式系统模拟器
💻 H
字号:
/*//*/

#ifndef RTL8019_NET_H
#define RTL8019_NET_H

#include <Core/Global.h>
#include "Core/Device.h"
#include <packet32.h>
#include <pcap.h>
#include <ntddndis.h>
#include <stdio.h>
#include <conio.h>
#pragma comment(lib,"ws2_32")


#ifdef WIN32

#ifdef NIC_EXPORTS
	#define NIC_API __declspec(dllexport)
#else
	#define NIC_API __declspec(dllimport)
#endif

#endif

#ifdef LINUX

	#define NIC_API

#endif



namespace DEVICE
{
	#define  NETCARDINT  0x00010000  
	#define PAGE_SIZE 256
	
	//for net
	#define ETH_IP       0x0800
	#define ETH_ARP      0x0806
	#define ARP_REQUEST  0x0001
	#define ARP_REPLY    0x0002
	#define ARP_HARDWARE 0x0001
	#define max_num_adapter  10

	#pragma pack(push,1)


	typedef struct ethdr
	{
		unsigned char   eh_dst[6];
		unsigned char   eh_src[6];
		unsigned short  eh_type;
	}ETHDR,*PETHDR;



	typedef struct arphdr
	{
		unsigned short  arp_hardtype;
		unsigned short  arp_protype;
		unsigned char   arp_hardlen;
		unsigned char   arp_prolen;
		unsigned short  arp_optype;
		unsigned char   arp_sendermac[6];
		unsigned long   arp_senderip;
		unsigned char   arp_targetmac[6];
		unsigned long   arp_targetip;
	}ARPHDR,*PARPHDR;

	typedef struct iphdr
	{
		unsigned char  h_lenver;
		unsigned char  tos;
		unsigned short total_len;
		unsigned short ident;
		unsigned short frag_and_flags;
		unsigned char  ttl;
		unsigned char  proto;
		unsigned short checksum;
		unsigned int   sourceip;
		unsigned int   destip;
	}IPHDR,*PIPHDR;



	/*//! for CR register
	//judge page number
	*/
	#define CR_PAGE_NUM   0xc0
	#define CR_PAGE_ZERO  0x00
	#define CR_PAGE_ONE   0x40
	#define CR_PAGE_TWO   0x80
	#define CR_PAGE_THREE 0xc0
	/*  //command for CR */
	#define CR_STOP       0x01  /* //置1表示网卡停止工作 */
	#define CR_START      0x02   /* //网卡开始工作 */
	#define CR_TXP        0x04   /* //置位时发送数据包,发完清0    (虚拟网卡-->以太网) */
	#define CR_FINISH     0x20   /* //结束或完成远程DMA */
	#define CR_REMOTE_R   0x08   /* //远程DMA读                    (虚拟网卡-->模拟机器)*/
	#define CR_REMOTE_W   0x10   /* //远程DMA写                    (模拟机器-->虚拟网卡)*/
	#define CR_SEND       0x18   /* //可代替 远程DMA读             (虚拟网卡-->模拟机器)*/

	/* //网卡内部ram的分配*/
	#define TPSR_START 0x4000  /* //接受部分位最大帧长度,所以分配6页,共1536字节*/
	/* //其余为网卡接收缓冲*/
	#define PSTART_START 0x4600
	#define PSTOP_STOP   0x6000


	/* //! isr register 中断状态寄存器*/
	#define ISR_RST 0x80 /* //缓冲溢出,nic复位时置位*/
	#define ISR_RDC 0x40 /* //远程DMA完成后置位*/
	#define ISR_CNT 0x20 /* //当MSB即大尾网络计数器启动时置位*/
	#define ISR_OVW 0x10 /* //缓冲区满,不是溢出,置位*/
	#define ISR_TXE 0x08 /* //发送包失败置位*/
	#define ISR_RXE 0x04 /* //接受包错误,crc错等,置位*/
	#define ISR_PTX 0x02 /* //发送无错*/
	#define ISR_PRX 0x01 /* //接受无错*/

	/* //! imr    中断屏蔽寄存器,置位为1时就使能isr相应位*/
	#define IMR_RST 0x80  /* //缓冲溢出,nic复位时置位*/
	#define IMR_RDC 0x40 /* //远程DMA完成后置位*/
	#define IMR_CNT 0x20 /* //当MSB即大尾网络计数器启动时置位*/
	#define IMR_OVW 0x10 /* //缓冲区满,不是溢出,置位*/
	#define IMR_TXE 0x08 /* //发送包失败置位*/
	#define IMR_RXE 0x04 /* //接受包错误,crc错等,置位*/
	#define IMR_PTX 0x02 /* //发送无错*/
	#define IMR_PRX 0x01 /* //接受无错*/



	/* //this is the address of the virtual nic's registers*/

	#define NET_BASE_ADDRESS 0xfffa0000

	#define CR_ADDRESS  NET_BASE_ADDRESS
	#define DATA_ADDRESS   (NET_BASE_ADDRESS+ 0x40)
	#define RESET_ADDRESS    (NET_BASE_ADDRESS+ 0x7c)

	/* //page 0 (write only )*/

	/* //接受缓冲,存放的是页号*/
	#define PSTART_ADDRESS  (NET_BASE_ADDRESS + 0x04)
	#define PSTOP_ADDRESS   (NET_BASE_ADDRESS + 0x08)

	#define BNRY_ADDRESS    (NET_BASE_ADDRESS + 0x0c)
	#define TPSR_ADDRESS    (NET_BASE_ADDRESS + 0x10)
	#define TBCR0_ADDRESS   (NET_BASE_ADDRESS + 0x14)
	#define TBCR1_ADDRESS   (NET_BASE_ADDRESS + 0x18)
	#define ISR_ADDRESS     (NET_BASE_ADDRESS + 0x1c)
	#define RSAR0_ADDRESS   (NET_BASE_ADDRESS + 0x20)
	#define RSAR1_ADDRESS   (NET_BASE_ADDRESS + 0x24)
	#define RBCR0_ADDRESS   (NET_BASE_ADDRESS + 0x28)
	#define RBCR1_ADDRESS   (NET_BASE_ADDRESS + 0x2c)

	/* //接收配置寄存器,初始为 0x04*/
	#define RCR_ADDRESS     (NET_BASE_ADDRESS + 0x30)
	/* //发送配置寄存器,初始为 0x02,后为0x00(即正常发送数据包并加上CRC)*/
	#define TCR_ADDRESS     (NET_BASE_ADDRESS + 0x34)
	/* //数据配置寄存器,初始0x48,表示8位DMA,FIFOS深度8字节,DMA方式*/
	#define DCR_ADDRESS     (NET_BASE_ADDRESS + 0x38)

	#define IMR_ADDRESS     (NET_BASE_ADDRESS + 0x3c)

	/* page 1 read & write */
    /*网卡的物理地址*/
	#define PAR0_ADDRESS    (NET_BASE_ADDRESS + 0x04)
	#define PAR1_ADDRESS    (NET_BASE_ADDRESS + 0x08)
	#define PAR2_ADDRESS    (NET_BASE_ADDRESS + 0x0c)
	#define PAR3_ADDRESS    (NET_BASE_ADDRESS + 0x10)
	#define PAR4_ADDRESS    (NET_BASE_ADDRESS + 0x14)
	#define PAR5_ADDRESS    (NET_BASE_ADDRESS + 0x18)
               
	#define CURR_ADDRESS    (NET_BASE_ADDRESS + 0x1c)
	/*multicast address 多播地址*/
	#define MAR0_ADDRESS    (NET_BASE_ADDRESS + 0x20)
	#define MAR1_ADDRESS    (NET_BASE_ADDRESS + 0x24)
	#define MAR2_ADDRESS    (NET_BASE_ADDRESS + 0x28)
	#define MAR3_ADDRESS    (NET_BASE_ADDRESS + 0x2c)
	#define MAR4_ADDRESS    (NET_BASE_ADDRESS + 0x30)
	#define MAR5_ADDRESS    (NET_BASE_ADDRESS + 0x34)
	#define MAR6_ADDRESS    (NET_BASE_ADDRESS + 0x38)
	#define MAR7_ADDRESS    (NET_BASE_ADDRESS + 0x3c)




	/* //this is the structure of the rtl8019as,and we only simulate the part 
	//of the nic which compatible with ne2000
	*/
	typedef struct 
	{
	/*	// the control register,available in all four page registers */
		Core::u8 cr; /* //0x00,read & write,选择寄存器页号,开始接受数据、发送数据命令*/
	
	/* //!page0 registers////////*/
	
	/*	//!网络芯片从以太网读数据 */

		Core::u8 pstart; /* // 存放ram的页号(0x40-0x5f),     0x01,write 读在2页 */
		Core::u8 pstop; /* // 存放ram的页号(0x40-0x5f),     0x02,write 读在2页 */
		Core::u8 bnry;  /* // 读取指针,初始值为 pstart,page0也有此寄存器,0x03,read&write */
		/* //curr 在page1,0x07,read only,接受缓冲区写指针,初始值为 pstart */
	
		/* //!网络芯片发数据到以太网 */
		Core::u8 tpsr; /* //要发送数据的起始页号               0x04,write  读在2页 */
		Core::u8 tbcr0;/*  //要发送数据的长度                  0x05,write */
		Core::u8 tbcr1;/*  //the same as tbcr0                 0x06,write */
	
		/* //!CPU读/写数据到网络芯片 */
		Core::u8 rsar0;/* //存放数据在网络芯片中的起始地址,是0x4000,而不是页号,0x08,write */
		Core::u8 rsar1;/* //                                  0x09,write */
		Core::u8 rbcr0;/* //要读写数据的长度                  0x0A,write */
		Core::u8 rbcr1;/* //                                  0x0B,write*/
	

		Core::u8 isr;/* //中断状态寄存器,                    0x07,write */
		Core::u8 imr;/* //屏蔽寄存器                          0x0F,write 读在2页 */
	

		Core::u8 rcr;  /* //接受配置寄存器                    0x0C,write 读在2页 */
		Core::u8 tcr;  /* //传输配置寄存器                    0x0D,write 读在2页*/
		Core::u8 dcr; /* //数据配置寄存器                    0x0E,write 读在2页 */
	
	/* //!page 1////////*/
		Core::u8 par[6]; /* //存放mac地址,最高字节不能是奇数,地址区间 0x01-0x06,read&write */
		Core::u8 curr;   /*  //0x07,read&write,接受缓冲区写指针,初始值为 pstart */
		Core::u8 mar[8]; /* //组播地址 */
  
	/* //!page 2 all registers are defined in page 0,都是只读寄存器/// */

	/*  //!NIC RAM */
		Core::u8 nic_ram[32*PAGE_SIZE];/* //only for 8 bit DMA  */
	
	}RTL8019as_net;
	class NIC_API nic8019: public Core::Device_32Bit	{	private:		RTL8019as_net rtl8019_net;		
		Core::s32  read_total_count;
		Core::s32  read_total_page;
		Core::s32  read_offset;
		Core::s32  start_page;		Core::s32  page_to_read;
		Core::s32  write_total_count;
		Core::s32  write_offset;		Core::s32  nic_state;	public:		nic8019();	public://inherite fuction		virtual void on_create(void){ name_ = "NIC8019"; }

		virtual void on_destroy(void){}

		virtual void on_reset(void){}

		virtual void notify(void){}		virtual void on_mapped_memory_read(Core::u32 start, size_t size, Core::Bytecode_Type & buffer);

		//! 
		virtual void on_mapped_memory_write(Core::u32 start, size_t  size, Core::Bytecode_Type & buffer);		virtual Core::u32 get_state(Core::u32 state=0);	public://
		void rtl8019_reset(void);

		Core::s8 rtl8019_read(Core::u32 addr);

		Core::s8 rtl8019_write(Core::u32 addr, Core::s8 data);

		Core::s8 read_from_rtl8019();

		Core::s8 write_to_rtl8019( Core::s8 data);

		void write_cr(Core::s8 data);

		//will be called every io cycle determining whether there is any input
		Core::s32  is_nic_input();

		//fetch data from ethernet
		Core::s32  ethernet_input();

		//
		Core::s32 recv_from_ethernet( Core::u8 *buffer, Core::s32 size);
		//
		Core::s32 write_to_ethernet(Core::u8  *buffer, Core::s32 size);

		void armulator_update_int();
		Core::s32 tuntap_input( );

		Core::s32 tuntap_output( );

		

	};	typedef nic8019 Nic8019;	}	
#endif

⌨️ 快捷键说明

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