1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
/* pu_lib.c */
/* Routines to read and write pic structures to disk */
/* Ian King 1994 */
#include "pu_defs.h"
#include <stdio.h>
#include <ctype.h>
#define PARAMS(x) x
#include <bfd.h>
void PU_Clear(PICDEFN *pic)
{
int i;
for(i=0; i<MAXPICSIZE; i++)
pic->picmemmap[i] = 0x0;
pic->pictype = 84;
pic->picid[0] = 0x00;
pic->picid[1] = 0x00;
pic->picid[2] = 0x00;
pic->picid[3] = 0x00;
pic->osctype = 3;
pic->clock = 1.0e6;
pic->cp_fuse = 1;
pic->wd_fuse = 1;
pic->pu_fuse = 1;
}
int PU_Read (const char *filename, PICDEFN *pic, int *top)
{
bfd *file = NULL;
asection *section = NULL;
PU_Clear (pic);
file = bfd_openr (filename, "ihex");
if (file == NULL) {
fprintf (stderr, "unable to open file\n");
return PU_FAIL;
}
{
char **matching;
bfd_check_format_matches (file, bfd_object, &matching);
}
for (section = file->sections; section != NULL; section = section->next) {
bfd_size_type sz = bfd_section_size (section);
if ((section->vma % 2) != 0) {
fprintf (stderr, "section not on 16-bit boundary: ignoring");
break;
}
bfd_get_section_contents (file, section, pic->picmemmap + (section->vma / 2), 0, sz);
}
bfd_close (file);
return PU_OK;
}
int PU_Read_Orig(const char *filename, PICDEFN *pic, int *top)
{
FILE *filehandle;
int ch;
unsigned short checksum;
int finalcheck;
int address;
int numberofwords;
int i;
int currentword;
int check_provided;
check_provided = 0;
*top = 0;
filehandle = fopen(filename, "r");
if (filehandle == NULL)
{
printf("Could not open file %s for reading\n",filename);
return PU_FAIL;
}
PU_Clear(pic);
while(!feof(filehandle))
{
while(isspace(ch = fgetc(filehandle)))
;
switch(ch)
{
case '#': /* comment */
while(fgetc(filehandle)!='\n')
;
break;
case 'T': fscanf(filehandle,"%d", &pic->pictype);
break;
case 'W': fscanf(filehandle,"%d", &pic->wd_fuse);
break;
case 'P': fscanf(filehandle,"%d", &pic->cp_fuse);
break;
case 'C': fscanf(filehandle,"%d %e", &pic->osctype, &pic->clock);
break;
case 'U': fscanf(filehandle,"%d", &pic->pu_fuse);
break;
case 'I': fscanf(filehandle,"%x %x %x %x", &pic->picid[0],&pic->picid[1],&pic->picid[2],&pic->picid[3]);
break;
case 'A': fscanf(filehandle,"%x", &address);
break;
case 'D': fscanf(filehandle,"%d", &numberofwords);
for(i=0; i<numberofwords; i++)
{
fscanf(filehandle,"%x", ¤tword);
pic->picmemmap[address] = currentword;
if (address > *top)
*top = address;
address++;
if (address > MAXPICSIZE)
{
printf("Illegal address %04x\n", address);
return PU_FAIL;
}
}
break;
case 'S': fscanf(filehandle,"%x", &finalcheck);
check_provided = 1;
break;
case EOF: break;
default: printf("Illegal character in %s (%c)\n", filename, ch);
return PU_FAIL;
}
}
fclose(filehandle);
if (!check_provided)
{
printf("No checksum found, probably not a valid pic file\n");
return PU_OK;
}
checksum = 0;
for(i=0; i<MAXPICSIZE; i++)
checksum ^= pic->picmemmap[i];
if ((unsigned short)finalcheck != checksum)
{
printf("Fails checksum (want %04x got %04x)\n",finalcheck,checksum);
/*
* My version of PICASM doesn't give proper checksums (because
* of an unrelated hack). But the file is really OK. -dhm
*/
return PU_OK;
}
return PU_OK;
}
int PU_WriteHeader(char *filename, PICDEFN *pic, char *comment)
{
FILE *filehandle;
filehandle = fopen(filename, "w");
if (filehandle == NULL)
{
printf("Could not open file %s for writing\n",filename);
return PU_FAIL;
}
fprintf(filehandle,"# File %s\n", filename);
fprintf(filehandle,"# %s\n", comment);
fprintf(filehandle,"T%d\n", pic->pictype);
fprintf(filehandle,"W%d\n", pic->wd_fuse);
fprintf(filehandle,"P%d\n", pic->cp_fuse);
fprintf(filehandle,"C%d %e\n", pic->osctype, pic->clock);
fprintf(filehandle,"U%d\n", pic->pu_fuse);
fprintf(filehandle,"I%04x %04x %04x %04x\n",pic->picid[0],pic->picid[1],
pic->picid[02],pic->picid[3]);
fclose(filehandle);
return PU_OK;
}
int PU_WriteBlock(char *filename, PICDEFN *pic, int from, int to)
{
FILE *filehandle;
int length;
int i;
int numberleft;
int current;
int mask;
length = (to-from) + 1;
if (length<=0)
{
printf("Error stupid block size! from %04x to %04x (size %d)\n",
from, to, length);
return PU_FAIL;
}
if ((from>MAXPICSIZE) || (to>MAXPICSIZE))
{
printf("Error from or to Out of Range from=%04x to=%04x\n",from, to);
return PU_FAIL;
}
if ((from < 0) || (to < 0))
{
printf("Error Negative address specified!\n");
return PU_FAIL;
}
filehandle = fopen(filename, "a");
if (filehandle == NULL)
{
printf("Could not open file %s for writing\n",filename);
return PU_FAIL;
}
fprintf(filehandle,"A%04x\n", from);
current = from;
switch (pic->pictype) {
case 54:
mask = 0x0fff;
break;
case 84:
mask = 0x3fff;
break;
default:
mask = ~0;
break;
}
while(current < to)
{
numberleft = to - current;
if (numberleft > 8)
numberleft = 8;
fprintf(filehandle,"D%d", numberleft);
for(i=0;i<numberleft;i++)
fprintf(filehandle," %04x", pic->picmemmap[current+i] & mask);
fprintf(filehandle,"\n");
current += numberleft;
}
fclose(filehandle);
return PU_OK;
}
int PU_WriteTrailer(char *filename, PICDEFN *pic, char *comment)
{
FILE *filehandle;
int i;
unsigned short checksum;
filehandle = fopen(filename, "a");
if (filehandle == NULL)
{
printf("Could not open file %s for writing\n",filename);
return PU_FAIL;
}
checksum = 0;
for(i=0; i<MAXPICSIZE; i++)
checksum ^= pic->picmemmap[i]; /* simple checksum */
/* easily fooled! */
fprintf(filehandle, "S%04x\n", checksum);
fprintf(filehandle, "# %s\n", comment);
fprintf(filehandle, "# ... The End ...\n");
fclose(filehandle);
return PU_OK;
}
/* ... The End ... */
|