#include #include //#define DEBUG int ctoi(char c) { int ret = 0; if(c >= '0' && c <= '9') { ret = c - '0'; } else if(c >= 'A' && c <= 'F') { ret = c - 'A' + 10; } return ret; } int main(int argc, char* argv[]) { FILE *infile, *outfile; char fname[40], buff[80]; int rsize, address, pc, type; if(argc != 2) { fprintf(stderr, "Usage: %s filename\n", argv[0]); return 1; } infile = fopen(argv[1], "r"); if(infile == NULL) { fprintf(stderr, "%s open error!\n", argv[1]); return 1; } strcpy(fname, argv[1]); strcat(fname, ".txt"); outfile = fopen(fname, "w"); if(outfile == NULL) { fprintf(stderr, "File output error!"); fclose(infile); return 1; } pc = 0; while(fgets(buff, sizeof(buff), infile)) { if(buff[0] != ':') { fprintf(stderr, "File is not Intel HEX!"); fclose(infile); fclose(outfile); return 1; } rsize = ctoi(buff[1]) * 0x10 + ctoi(buff[2]); rsize /= 2; //バイト単位なのでワード単位に変換 address = ctoi(buff[3]) * 0x1000 + ctoi(buff[4]) * 0x100 + ctoi(buff[5]) * 0x10 + ctoi(buff[6]); address /= 2; //バイト単位なのでワード単位に変換 type = ctoi(buff[7]) * 0x10 + ctoi(buff[8]); if(type ==1) break; else if(type == 0) { if(address == 0x2007) { #ifdef DEBUG fprintf(outfile, "\n\nconfiguration = %c%c%c%c;\n", buff[11], buff[12], buff[9], buff[10]); #else fseek(outfile, -2, SEEK_CUR); //最後の','を消す fprintf(outfile, "\n};\n\n"); fprintf(outfile, "const PROGMEM uint16_t configuration = 0x%c%c%c%c;\n", buff[11], buff[12], buff[9], buff[10]); #endif } else { if(address == 0) { #ifdef DEBUG fprintf(outfile, "0000 : "); #else fprintf(outfile, "const PROGMEM uint16_t program[] = {\n"); #endif } //不連続なアドレスを3FFFで埋める while(address != pc) { #ifdef DEBUG fprintf(outfile, "3FFF "); #else fprintf(outfile, "0x3FFF, "); #endif ++pc; if((pc % 8) == 0) { fprintf(outfile, "\n"); #ifdef DEBUG fprintf(outfile, "%04X : ", pc); #endif } } for(int i=0; i