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