00001
00047 #include <iostream>
00048 #include <string>
00049 #include <ctype.h>
00050 #include "config_files.h"
00051
00052 using namespace std;
00053
00054 const bool _VERBOSE_ = false;
00055
00067 int CConfigFiles::ihxOpen(string fileName)
00068 {
00069
00070 ihxFile = fopen(fileName.c_str(), "r");
00071 if(ihxFile == NULL){
00073 std::cerr << "FileIO Error " << fileName << " Not Found" << std::endl;
00074 return -1;
00075 }
00076
00077 return 0;
00078 }
00079
00080
00122 int CConfigFiles::ihxRead(byte* b)
00123 {
00124 char c;
00125 int i, length;
00126 int input, data, check;
00127 byte sum;
00128
00129 if(ihxFile == NULL){
00131 std::cerr << "Hex File Not Open!!" << std::endl;
00132 return -1;
00133 }
00134
00135 c = getc(ihxFile);
00136 if(c != ':'){
00138 std::cerr << "ihxRead: Hex file missing start colon! found "<< c << std::endl;
00139 return -1;
00140 }
00141
00142 input = fscanf(ihxFile, "%2X%4X%2X", &ihxLen, &ihxAddress, &ihxType);
00143 if(input != 3){
00145 std::cerr << "ihxRead: Preamble Error" << std::endl;
00146 return -1;
00147 }
00148
00149 sum = ihxLen + ((ihxAddress >> 8) & 0xFF) + (ihxAddress & 0xFF) + ihxType;
00150
00151 length = ihxLen;
00152 if(length > MAX_IHX_RECORD_LEN) {
00154 std::cerr << "ihxRead: Recorod too long " << length << " max " << MAX_IHX_RECORD_LEN << std::endl;
00155 return -1;
00156 }
00157
00158 for(i=0; i<length; i++){
00159 input = fscanf(ihxFile, "%2X", &data);
00160
00161 if(input != 1){
00163 std::cerr << "ihxRead: Incomplete Line" << std::endl;
00164 return -1;
00165 }
00166
00167 b[i] = data;
00168 sum += data;
00169 }
00170
00171 input = fscanf(ihxFile, "%2X\n", &check);
00172 sum = 0x100 - sum;
00173
00174 if((input != 1) || (check != sum)){
00176 std::cerr << "ihxRead: Checksum Error, read " << check <<
00177 " calc'd " << sum << std::endl;
00178 return -1;
00179 }
00180
00181 return 0;
00182 }
00183
00184
00190 int CConfigFiles::ihxClose()
00191 {
00192
00193 if(ihxFile){
00194 fclose(ihxFile);
00195 }
00196
00197 ihxFile = NULL;
00198
00199 return 0;
00200 }
00201
00202
00208 void hexDump(byte *b, int n)
00209 {
00210 int i, j;
00211
00212 printf("\nDumping %d bytes\n", n);
00213 for(i=0; i<n; i+=16){
00214
00215 printf("%08x: ", i);
00216
00217 for(j=0; j<16; j++){
00218 if((i+j) >=(n))
00219 printf(" ");
00220 else
00221 printf("%02X ", b[i+j]);
00222 }
00223
00224 printf(" ");
00225
00226 for(j=0; j<16; j++){
00227 if((i+j) >=(n))
00228 printf(" ");
00229 else
00230 printf("%c", (isalnum(b[i+j]))?b[i+j]:'.');
00231 }
00232
00233 printf("\n");
00234 }
00235
00236 }
00237
00238
00244 int CConfigFiles::xbtOpen(string fileName)
00245 {
00246 static const int sz = 78;
00247 int n;
00248 byte b[sz];
00249
00250 xbtFile = fopen(fileName.c_str(), "rb");
00251 if(xbtFile == NULL){
00252 cerr << "FileIO Error " << fileName << " Not Found" << endl;
00253 return -1;
00254 }
00255
00256
00257
00260 n = fread(b, 1, sz, xbtFile);
00261
00262 if(_VERBOSE_)
00263 hexDump(b,n);
00264
00265 xbtTotalBytes = n;
00266
00267 return 0;
00268 }
00269
00270
00276 int CConfigFiles::xbtRead(byte *b, int& N)
00277 {
00278
00279 N = fread(b, 1, N, xbtFile);
00280
00281 if(_VERBOSE_){
00282 hexDump(b,N);
00283
00284 if(N == 0)
00285 printf("Done, No More Bytes to Read??\n");
00286 }
00287
00288 xbtTotalBytes += N;
00289
00290 return 0;
00291 }
00292
00293
00299 int CConfigFiles::xbtClose()
00300 {
00301
00302 if(_VERBOSE_)
00303 printf("Total Number of Bytes Read %d, Total Send to FPGA %d\n",
00304 xbtTotalBytes, xbtTotalBytes-78);
00305
00306 if(xbtFile){
00307 fclose(xbtFile);
00308 }
00309
00310 xbtFile = NULL;
00311
00312 return 0;
00313
00314 }