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

📄 ne2000.lst

📁 51单片机控制网卡实现上网程序代码
💻 LST
📖 第 1 页 / 共 5 页
字号:
C51 COMPILER V7.09   NE2000                                                                03/27/2005 16:17:14 PAGE 1   


C51 COMPILER V7.09, COMPILATION OF MODULE NE2000
OBJECT MODULE PLACED IN ne2000.OBJ
COMPILER INVOKED BY: E:\学习工具\Keil51\C51\BIN\C51.EXE ne2000.c LARGE DEBUG OBJECTEXTEND CODE SYMBOLS

line level    source

   1          // ********************************************************
   2          // ***                                                  ***
   3          // *** This program is intended to demonstrate an       ***
   4          // *** Internet appliance.  Communication with the      ***
   5          // *** Internet will be via an NE2000 compatible        ***
   6          // *** ethernet card.                                   ***
   7          // ***                                                  ***
   8          // ***                                                  ***
   9          // ********************************************************
  10          
  11          #include "hostmcu.h"
  12          #include "8019.h"
  13          #include "ylystd.h"
  14          
  15          // ********************************************************
  16          
  17          #define DEBUG 1
  18          
  19          // ********************************************************
  20          
  21          #define READ    FALSE
  22          #define WRITE   TRUE
  23          
  24          
  25          // NIC Other Defines
  26                                                      // 8-bit mode - wasting ram but saving I/O count
  27          #define RCV_BUF_START   0x40                // Room for three full ethernet packets
  28          #define XMT_BUF_START   0x54                // Leave room for two full packet transmit buffers
  29          
  30          
  31          // ********************************************************
  32          // *** Protocol Defines                                 ***
  33          // ********************************************************
  34          
  35          #define ARP     0x0806
  36          #define IP      0x0800
  37          
  38          #define ICMP    0x01
  39          #define TCP     0x06
  40          #define UDP     0x17
  41          
  42          #define ECHO    0x08
  43          #define REPLY   0x00
  44          
  45          #define TCP_FIN 0x01
  46          #define TCP_SYN 0x02
  47          #define TCP_RST 0x04
  48          #define TCP_PSH 0x08
  49          #define TCP_ACK 0x10
  50          #define TCP_URG 0x20
  51          
  52          
  53          // ********************************************************
  54          // *** My IP address                                    ***
  55          // ********************************************************
C51 COMPILER V7.09   NE2000                                                                03/27/2005 16:17:14 PAGE 2   

  56          
  57          // 192.168.1.124
  58          
  59          #define MY_IP0  192
  60          #define MY_IP1  168
  61          #define MY_IP2  1
  62          #define MY_IP3  124
  63          
  64          char tmpstr[128];
  65          
  66          // ********************************************************
  67          
  68          // ********************************************************
  69          
  70          void    init_pic(void);
  71          void    init_nic(void);
  72          //void    myoutportb(u16_t address, u8_t  dat);
  73          void    myoutportstr(u16_t address, u8_t *str);
  74          void    myoutportw(u16_t address, u16_t dat);
  75          //u8_t    myinportb(u16_t address);
  76          void    remote_dma_setup(short w_r, u16_t address);
  77          void    read_phy_addr(void);
  78          short   poll_nic(void);
  79          void    arp_response(void);
  80          void    load_ethernet_header(void);
  81          void    send_packet(u16_t len);
  82          void    ping_response(void);
  83          void    load_IP_header(u16_t IP_packet_length, u16_t IP_send_protocol);
  84          void    tcp_response(void);
  85          void    load_TCP_header(u16_t data_flags, u16_t tcp_chksum, u16_t tcp_length);
  86          void    calc_chksum(u16_t dat);
  87          void    tcp_listen(void);
  88          void    tcp_syn_rcvd(void);
  89          void    tcp_estab(void);
  90          
  91          // ********************************************************
  92          // *** Ethernet variables
  93          
  94          u16_t  physical_address[6];
  95          u16_t  source_address[6];
  96          
  97          u16_t  my_ip[4];
  98          
  99          // ********************************************************
 100          // *** IP Header variables
 101          
 102          u16_t  source_ip[4];
 103          u16_t  dest_ip[4];
 104          u16_t  version;
 105          u16_t  IP_length;
 106          u16_t  identification;
 107          u16_t  fragment;
 108          u16_t  IP_protocol;
 109          u16_t  hdr_len;
 110          u16_t  opt_len;
 111          u16_t  chksum;
 112          
 113          
 114          // ********************************************************
 115          // *** ICMP - ECHO variables
 116          
 117          u16_t  icmp_type;
C51 COMPILER V7.09   NE2000                                                                03/27/2005 16:17:14 PAGE 3   

 118          u16_t  icmp_code;
 119          u16_t  icmp_checksum;
 120          u16_t  icmp_identifier;
 121          u16_t  icmp_sequence;
 122          
 123          // ********************************************************
 124          // *** TCP Variables
 125          
 126          u16_t  tcp_source_port;
 127          u16_t  tcp_dest_port;
 128          u16_t  seq[2];
 129          u16_t  ack[2];
 130          u16_t  offset;
 131          u16_t    tcp_options;
 132          u16_t  flags;
 133          u16_t  window0;
 134          u16_t  max_seg=0;
 135          u16_t  tcp_data_len;
 136          
 137          // ********************************************************
 138          // *** Socket Variables
 139          
 140          u16_t       xm_ack[2];
 141          u16_t       xm_seq[2];
 142          u16_t       port;
 143          u16_t               ip[4];
 144          
 145          enum tcp_states {LISTEN,
 146                          SYN_SENT,
 147                          SYN_RCVD,
 148                          ESTAB,
 149                          FIN_WAIT_1,
 150                          FIN_WAIT_2,
 151                          CLOSING,
 152                          TIME_WAIT,
 153                          CLOSE_WAIT,
 154                          LAST_ACK,
 155                          CLOSED} html_socket;
 156          
 157          //tcp_states html_socket;                   // Create variable to track html socket state
 158          
 159          void n2k_main(void) {
 160   1      
 161   1          my_ip[0] = MY_IP0;                      // Set my IP address
 162   1          my_ip[1] = MY_IP1;
 163   1          my_ip[2] = MY_IP2;
 164   1          my_ip[3] = MY_IP3;
 165   1      
 166   1          init_nic();                             // Initialize the NIC
 167   1          read_phy_addr();                        // Read the Physical Address from the NIC
 168   1          init_nic();                             // Initialize again.  This is a quick way to
 169   1                                                  // set the Physical Address since we didn't have
 170   1                                                  // it the first time
 171   1          myoutportb(TCR, 0x00);                      // Take the NIC out of Loop Back.  This is the
 172   1                                                  // last step of the Initialization
 173   1      
 174   1          html_socket = LISTEN;
 175   1          
 176   1          while (TRUE)
 177   1          {
 178   2              poll_nic();
 179   2          }
C51 COMPILER V7.09   NE2000                                                                03/27/2005 16:17:14 PAGE 4   

 180   1      }
 181          
 182          
 183          void init_nic(void){
 184   1      
 185   1          myoutportb(CR, 0x21);                       // Page 0, Abort DMA, Stop NIC
 186   1      
 187   1          delay_ms(25);                           // Wait for it to stop
 188   1          myoutportb(NIC_RESET, 0xFF);                // Reset the NIC
 189   1          delay_ms(25);                           // Wait for it to reset
 190   1      
 191   1          myoutportb(DCR, 0x48);                      // byte DMA, Byte order 8086,
 192   1                                                  // Dual 16-bit DMA mode, Normal Operation
 193   1                                                  // Send CMD not executed, FIFO theshold 8 bytes
 194   1      
 195   1          myoutportb(RBCR0, 0x00);                    // Clear remote byte count registers
 196   1          myoutportb(RBCR1, 0x00);
 197   1      
 198   1          myoutportb(RCR, 0x0C);                      // Monitor off, Promiscuous off
 199   1                                                  // Accept Multicast, Accept Broadcast
 200   1                                                  // Reject Runts, Reject Errors
 201   1      
 202   1          myoutportb(TCR, 0x02);                      // Internal Loop Back
 203   1      
 204   1          myoutportb(BNDRY,   RCV_BUF_START);         // Start of Buffer RAM
 205   1          myoutportb(PSTART, RCV_BUF_START);          // Start of Buffer RAM
 206   1          myoutportb(PSTOP,   XMT_BUF_START);         // 8-bit mode so we need room for transmit buffers
 207   1          
 208   1      
 209   1          myoutportb(ISR, 0xFF);                      // Clear Interrupt Status Register
 210   1          myoutportb(IMR, 0x00);                      // Setup Interrupt Mask Register (No Interrupts)
 211   1      
 212   1          myoutportb(CR, 0x61);                       // Page 1
 213   1      
 214   1          myoutportb(PAR0, physical_address[0]);      // Physical_Address is not initialized on the
 215   1          myoutportb(PAR1, physical_address[1]);      // first pass.  We need to read it from the
 216   1          myoutportb(PAR2, physical_address[2]);      // EPROM and set it later
 217   1          myoutportb(PAR3, physical_address[3]);
 218   1          myoutportb(PAR4, physical_address[4]);
 219   1          myoutportb(PAR5, physical_address[5]);
 220   1      
 221   1          myoutportb(CURR, 0x40);
 222   1      
 223   1          myoutportb(CR, 0x22);                       // Put the NIC in Start mode
 224   1                                                  // We're still in Loop Back so nothing should happen    
 225   1      }
 226          
 227          // ********************************************************
 228          void  myoutportstr(u16_t address, u8_t *str)
 229          {
 230   1          u16_t idx;
 231   1          for(idx=0;idx<strlen(str);idx++)
 232   1          {
 233   2              myoutportb(address,(u8_t)str[idx]);
 234   2          }
 235   1      }
 236          // ********************************************************
 237          // *** myoutportw                                       ***
 238          // *** Output one word of 'dat' to the specified       ***
 239          // *** 'address'                                        ***
 240          // ********************************************************
 241          
C51 COMPILER V7.09   NE2000                                                                03/27/2005 16:17:14 PAGE 5   

 242          void myoutportw(u16_t address, u16_t dat) {
 243   1      
 244   1          u16_t prev_chksum;                  // Declare temporary storage for checksum
 245   1      
 246   1          prev_chksum = chksum;                   // Keep a copy of the current checksum
 247   1      
 248   1          myoutportb(address,dat>>8);
 249   1          myoutportb(address,dat);
 250   1      
 251   1          chksum += dat;                         // Add the dat word to the checksum
 252   1          if (chksum < prev_chksum )              // Check if a carry was generated
 253   1              chksum++;                           // Do the ones compliment if requred
 254   1      
 255   1      }
 256          
 257          // ********************************************************
 258          void calc_chksum(u16_t dat) {
 259   1      
 260   1          u16_t prev_chksum;
 261   1      
 262   1          chksum += dat;
 263   1          if (chksum < prev_chksum)
 264   1              chksum ++;
 265   1          }
 266          
 267          // ********************************************************
 268          void remote_dma_setup(short w_r, u16_t address){
 269   1      
 270   1          myoutportb(CR, 0x22);                       // Abort Remote DMA
 271   1          myoutportb(RBCR0, 0xFF);                    // Set maximum number of bytes to read/write
 272   1          myoutportb(RBCR1, 0xFF);
 273   1          myoutportb(RSAR0, address);
 274   1          myoutportb(RSAR1, address>>8);
 275   1      
 276   1          if (w_r) {

⌨️ 快捷键说明

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