upid 0 Newbie Poster
bool
I8051::Hex2Short(const char* buf, unsigned &val) 
{    
    int i;

    if( sscanf(buf, "%x", &i) != 1 ) {
      cerr << "Error: hex file error." << endl;
        return false;
    }
    val = i;
    return true;
}

//----------------------------------------------------------------------------

bool 
I8051::Load(const char* buf, unsigned char* rom, unsigned& prgSize) 
{   
    unsigned temp;
    unsigned len, base, type;
    unsigned char checksum = 0;
    char hex[16];

    if( buf[0] != ':' ) {

        cerr << "Error: hex file error." << endl;
        return false;
    }

    hex[0] = buf[1];
    hex[1] = buf[2];
    hex[2] = 0;
    if( !(Hex2Short(hex, len)) ) return false;

    hex[0] = buf[3];
    hex[1] = buf[4];
    hex[2] = buf[5];
    hex[3] = buf[6];
    hex[4] = 0;
    if( !(Hex2Short(hex, base)) ) return false;

    hex[0] = buf[7];
    hex[1] = buf[8];
    hex[2] = 0;
    if( !Hex2Short(hex, type) ) return false;

    if( type == 1 ) return true;

    if ( (base+len) > (prgSize) ) {
        (prgSize) = base + len + 2;
    }

    if( (prgSize) >= RomSize || (base+len) >= RomSize ) {
        printf("program too large\n");
        exit(1);
    }

    for(unsigned i=0; i<len; i++) {
        hex[0] = buf[ 9 + i * 2];
        hex[1] = buf[10 + i * 2];
    if( !Hex2Short(hex, temp) ) return false;
        rom[base + i] = (unsigned char)temp;
    }

    //-----------------------------------------------------------

    for(unsigned i=0; buf[i * 2 + 1] && buf[i * 2 + 2]; i++) {

        hex[0] = buf[i * 2 + 1];
        hex[1] = buf[i * 2 + 2];
    unsigned temp;
    if( !Hex2Short(hex, temp) ) return false;
        checksum += (unsigned char)temp;
    }

    if( checksum != 0 ) {

        cerr << "Error: checksum failed." << endl;
        return false;
    }
    return false;
}

this is a part of the whole code for simulator of 8051 . when i compile it , it compiles successfully with DEVC++ ide, but at the run time it shows inifinite times or the "Error:hex file error" . I cant understand how to solve this . I want your help to solve this . plz help me soon.Thank you .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.