Subversion Repositories HomeAutomation

Rev

Rev 1398 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. #define STRICT
  2. #include "HexFile.h"
  3.  
  4.  
  5. HexFile::HexFile()
  6. {
  7.     // Constructior
  8. }
  9.  
  10. HexFile::~HexFile()
  11. {
  12.     //Destructor
  13. }
  14.  
  15. bool HexFile::isValid()
  16. {
  17.     // Returns if file is a valid hex file.
  18.     // TBD!
  19.  
  20.     return true;
  21. }
  22.  
  23. bool HexFile::loadHex(char filepath[], bool isMC18)
  24. {
  25.     // Populate buffer
  26.  
  27.     unsigned char lineDataCount = 0;
  28.     unsigned long lineAddrHigh = 0;
  29.     unsigned long lineAddr = 0;
  30.     unsigned char lineCode = 0;
  31.     unsigned long fullAddr = 0;
  32.  
  33.    
  34.  
  35.     strcpy_s(HexFile::filepath,FILE_PATH_SIZE,filepath);
  36.     HexFile::length = 0;
  37.     HexFile::addrLower = 0x7FFFFFFF;
  38.     HexFile::addrUpper = 0;
  39.  
  40.     // Prime the buffer
  41.     for (int i = 0; i < BUFFER_SIZE; i++) buff[i] = 0xFF;
  42.  
  43.  
  44.     // Try opening file
  45.     ifstream a_file (filepath);
  46.  
  47.     if ( !a_file.is_open() )
  48.     {
  49.         // The file could not be opened
  50.         strcpy_s(lastError,ERROR_BUFFER_SIZE,"Err! Unable to open file.");
  51.         return false;
  52.     }
  53.     else
  54.     {
  55.         // Safely use the file stream
  56.  
  57.         char oneLine[LINE_MAX_SIZE];
  58.  
  59.         // For each line, read until eof.
  60.         while (!a_file.eof())
  61.         {
  62.             // read one line
  63.             a_file.getline(&oneLine[0],LINE_MAX_SIZE);
  64.            
  65.             // If valid line
  66.             if (strcmp(oneLine,"")!=0 && oneLine[0]==':')
  67.             {
  68.  
  69.                 lineDataCount = (unsigned char)HexFile::hexToLong(oneLine,1,2);
  70.                 lineAddr = (unsigned long)HexFile::hexToLong(oneLine,3,4) & 65535;      
  71.                 lineCode = (unsigned char)HexFile::hexToLong(oneLine,7,2);    
  72.        
  73.                 fullAddr = (lineAddrHigh * 65536) + lineAddr;
  74.            
  75.                 switch(lineCode)
  76.                 {
  77.                         case 0:
  78.                             HexFile::length +=lineDataCount;
  79.                             if ((fullAddr + lineDataCount - 1) > HexFile::addrUpper) HexFile::addrUpper = fullAddr + lineDataCount - 1;
  80.                             if (lineCode == 0 && fullAddr < HexFile::addrLower) HexFile::addrLower = fullAddr;
  81.                             for (int j = 0; j < lineDataCount; j++)
  82.                             {
  83.                                 buff[fullAddr] = (unsigned char)HexFile::hexToLong(oneLine,j * 2 + 9, 2); //byte.Parse(oneLine.Substring(j * 2 + 9, 2), System.Globalization.NumberStyles.HexNumber);
  84.                                 buff[fullAddr] &= 255; //(byte)255;
  85.                                 fullAddr++;
  86.                             }
  87.                             break;
  88.                         case 4:
  89.                             lineAddrHigh = (unsigned long)HexFile::hexToLong(oneLine,9, 4) & 65535; //long.Parse(oneLine.Substring(9, 4), System.Globalization.NumberStyles.HexNumber) & 65535;
  90.                             break;
  91.                         case 1: break; break;
  92.                    
  93.                 }
  94.  
  95.             }
  96.        
  97.         }
  98.        
  99.     }
  100.  
  101.     // Force lower addr to start
  102.     HexFile::addrLower = USER_RESET_ADDR;
  103.  
  104.     return true;
  105. }
  106.  
  107.  
  108. unsigned long HexFile::getAddrUpper(void) { return addrUpper; }
  109.  
  110. unsigned long HexFile::getAddrLower(void) { return addrLower; }
  111.  
  112. unsigned long HexFile::getLength(void) { return length; }
  113.  
  114. unsigned char HexFile::getByte(unsigned long addr)
  115. {
  116.     if (addr>=BUFFER_SIZE) return 0xFF;
  117.     return HexFile::buff[addr];
  118. }
  119.  
  120. // hex to integer
  121. unsigned long HexFile::hexToLong(char *str,unsigned int start,unsigned int length)
  122. {
  123.     unsigned long lng = 0;
  124.  
  125.     for (unsigned int i=0;i<length;i++)
  126.     {
  127.         unsigned int newi=i+start;
  128.         if (newi>=strlen(str)) break;
  129.  
  130.         if(str[newi]>='0' && str[newi]<='9')     { lng +=(unsigned long)pow(16.0,(double)(length-i-1))*(str[newi]-'0'+00); }
  131.         else if(str[newi]>='a' && str[newi]<='f'){ lng +=(unsigned long)pow(16.0,(double)(length-i-1))*(str[newi]-'a'+10); }
  132.         else if(str[newi]>='A' && str[newi]<='F'){ lng +=(unsigned long)pow(16.0,(double)(length-i-1))*(str[newi]-'A'+10); }
  133.         else { lng = 0; break; }
  134.     }
  135.  
  136.     return lng;
  137. }