10 extern int cdi_programmer(conf_opts_t *conf,
char *filename);
12 void usage(
char *prg_name)
14 printf(
"\nUsage: %s -f ihex file\n", prg_name);
15 printf(
"General options:\n");
16 printf(
" -V/--version Get converter version\n");
19 conf_opts_t conf_opts;
21 static int option_index = 0;
25 #define OPTIONS_STRING "Vhf:"
27 static struct option long_options[] =
29 {
"version", 0,
NULL,
'V'},
30 {
"file", 1,
NULL,
'f'},
31 {
"help", 0,
NULL,
'h'},
35 int parse_opts(
int count,
char* param[])
40 conf_opts.target_type = UNDEFINED;
41 while ((opt = getopt_long(count, param, OPTIONS_STRING,
42 long_options, &option_index)) != -1)
47 conf_opts.target_type = VERSION;
51 conf_opts.target_type = UNDEFINED;
55 strcpy(conf_opts.ihex_file, optarg);
56 conf_opts.target_type = CONVERT;
61 if (!error && (conf_opts.target_type == CONVERT) )
63 printf(
"File: %s.\n", conf_opts.ihex_file);
69 int main(
int argc,
char *argv[])
73 conf_opts.target_type = 0;
76 if ( (argc < 1) || (error = parse_opts(argc, argv)) )
79 if (error < 0)
return error;
83 if(conf_opts.target_type == CONVERT)
89 unsigned char check = 0;
90 unsigned long ext_addr=0;
91 unsigned short int addr=0;
92 unsigned char page_buffer[128*1024];
93 unsigned char page_table[64];
94 unsigned char buffer[256];
98 bzero(buffer,
sizeof(buffer));
101 memset(page_table, 0, 64);
102 memset(page_buffer, 0xFF,
sizeof(page_buffer));
105 ihex = fopen(conf_opts.ihex_file,
"rb");
108 printf(
"Can't open file.\n");
112 while((!error) && ((retval = fscanf(ihex,
"%s", buffer)) == 1) )
114 unsigned char data_len = 0;
116 if (memcmp(&buffer[7],
"00", 2) == 0)
119 sscanf((
char *)&buffer[1],
"%2hhx", &data_len);
120 sscanf((
char *)&(buffer[3]),
"%4hx", &addr);
123 uint32_t absolute_address = ext_addr+addr+i;
125 if (page_table[absolute_address/2048] == 0)
127 page_table[absolute_address/2048] = 1;
130 sscanf((
char *)&buffer[2*i+9],
"%2hhx", &page_buffer[absolute_address]);
134 else if (memcmp(&buffer[7],
"01", 2) == 0)
136 printf(
"\nFile read complete.\n");
139 else if (memcmp(&buffer[7],
"04", 2) == 0)
141 sscanf((
char *)&(buffer[3]),
"%4hx", &addr);
142 sscanf((
char *)&(buffer[9]),
"%4lx", &ext_addr);
143 printf(
"\rExtended page address: 0x%8.8lX\r", ext_addr*0x8000 );
145 if (ext_addr >= 0x0002) sdcc_file = 1;
147 if (ext_addr) ext_addr--;
154 printf(
"Read error\n");
158 printf(
"Not a SDCC banked file.\n");
161 printf(
"Writing %d pages.\n", pages);
163 char *ptr = strrchr(conf_opts.ihex_file,
'.');
169 strcat(conf_opts.ihex_file,
"_linear.hex");
170 printf(
"Output file: %s.\n", conf_opts.ihex_file);
171 ihex = fopen(conf_opts.ihex_file,
"wb");
180 addr = (i & 0x1F) * 2048;
181 if ( ((i / 32) * 0x10000) != ext_addr)
183 printf(
"Ext: %4.4X\n", ((i / 32) * 0x10000));
184 ext_addr = (i / 32) * 0x10000;
185 fprintf(ihex,
":02000004%4.4X%2.2X\r\n",
186 (
int)(ext_addr>>16), (
int)(0xFA-(ext_addr>>16)));
189 if (page_table[i] != 0)
191 printf(
"%4.4X", addr & 0xF800);
192 for (j=0; j<2048; j++)
194 addr =(i & 0x1F) * 2048 + j;
199 check -= (uint8_t) (addr >> 8);
200 check -= (uint8_t) (addr);
201 fprintf(ihex,
":20%4.4X00", (
int) addr);
203 fprintf(ihex,
"%2.2X", page_buffer[ext_addr+addr]);
204 check -= page_buffer[ext_addr+addr];
205 if ((j & 0x1F) == 0x1F)
207 fprintf(ihex,
"%2.2X\r\n", check);
211 if ((i & 0x07) == 0x07) printf(
"\n");
214 fprintf(ihex,
":00000001FF\r\n");
215 printf(
"Write complete.\n");
219 else if(conf_opts.target_type == UNDEFINED)
225 printf(
"\nSensinode hex file converter "CONVERTER_VERSION
"\n");