2009年10月1日 星期四

PING 8051 Step 0 ( Keil Compiler Bug )

Sometimes you will never think about the bug is there , and hard to image that.
Yester I want to Implement PING function to 8051+DM9000B solution.
I can get a  packet from Ethernet with Broadcast command, but I can't get ICMP packet
Then I started to debug , why  ????

I try to look at the packet read pointer of DM9000, When I got first ARP packet and reply RARP to
Sender, and Sender re-send ICMP packet to me, but I will lost this packet.and there is two possibility:
1. The packet is not correct.
2. my MAC address has problem ..
3. Buffer overflow.

So I try to list down all of input buffer reader pointer, the packet pointer is work fine..
then I try to print it the value of My MAC address, and I found the problem is from My wrong
MAC address..

This is MAC address wirte function, it looks no any problem , is it ?

void dm9k_hash_table(void)
{
UC i;
        for(i = 0; i < 6; i++)
                 iow(DM9000_PAR + i, DEF_MAC_ADDR[i]);

        for(i = 0; i < 8; i++)
                  iow(DM9000_MAR + i, 0x00);
         iow(DM9000_MAR + 7, 0x80);
}

But I try to look at and assemeby CODE, then I got this :

; FUNCTION dm9k_hash_table (BEGIN)

; SOURCE LINE # 81
; SOURCE LINE # 82
; SOURCE LINE # 85

0000 E4              CLR A
0001 F500 R      MOV i,A
0003 ?C0016:
0003 E500 R      MOV A,i
0005 C3             CLR C
0006 9406          SUBB A,#06H
0008 500C         JNC ?C0017

; SOURCE LINE # 86

000A E500 R     MOV A,i
000C 2410         ADD A,#010H
000E 908000     MOV DPTR,#DM9000_Index
0011 F0             MOVX @DPTR,A
0012 0500 R      INC i
0014 80ED         SJMP ?C0016
0016 ?C0017:
0016 7400 R      MOV A,#LOW DEF_MAC_ADDR
0018 2500 R      ADD A,i
001A F8             MOV R0,A
001B E6             MOV A,@R0
001C 908001     MOV DPTR,#DM9000_Data
001F F0             MOVX @DPTR,A

now you should see the problem , Keil C optimize the function, and let separate two block
and only write a data .
So I modify the code as it ..

void dm9k_hash_table(void)

{
UC i;
      for(i = 0; i < 6; i++)
     {
           iow(DM9000_PAR + i, DEF_MAC_ADDR[i]);
      }
      for(i = 0; i < 8; i++)
    {
           iow(DM9000_MAR + i, 0x00);
     }
     iow(DM9000_MAR + 7, 0x80);
}

and I got assemebly as below :

; FUNCTION dm9k_hash_table (BEGIN)

; SOURCE LINE # 81
; SOURCE LINE # 82
; SOURCE LINE # 85
0000 E4              CLR A
0001 F500 R       MOV i,A
0003 ?C0016:
0003 E500 R       MOV A,i
0005 C3              CLR C
0006 9406           SUBB A,#06H
0008 5014           JNC ?C0017
; SOURCE LINE # 86
; SOURCE LINE # 87
000A E500 R      MOV A,i
000C 2410          ADD A,#010H
000E 908000      MOV DPTR,#DM9000_Index
0011 F0              MOVX @DPTR,A
0012 7400 R       MOV A,#LOW DEF_MAC_ADDR
0014 2500 R       ADD A,i
0016 F8              MOV R0,A
0017 E6              MOV A,@R0
0018 A3             INC DPTR
0019 F0             MOVX @DPTR,A

; SOURCE LINE # 88
001A 0500 R      INC i
001C 80E5        SJMP ?C0016

So be careful for coding ...

BR,Jones  joens.hsu@gmail.com

沒有留言:

張貼留言