OpenCores
URL https://opencores.org/ocsvn/eco32/eco32/trunk

Subversion Repositories eco32

[/] [eco32/] [trunk/] [tools/] [bin2mcs/] [bin2mcs.c] - Blame information for rev 234

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 40 hellwig
/*
2
 * bin2mcs.c -- convert binary data to Intel hex records
3
 */
4
 
5
 
6
#include <stdio.h>
7
#include <stdlib.h>
8
#include <string.h>
9
#include <stdarg.h>
10
 
11
 
12
void error(char *fmt, ...) {
13
  va_list ap;
14
 
15
  va_start(ap, fmt);
16
  printf("Error: ");
17
  vprintf(fmt, ap);
18
  printf("\n");
19
  va_end(ap);
20
  exit(1);
21
}
22
 
23
 
24
int main(int argc, char *argv[]) {
25
  char *endptr;
26
  unsigned int loadAddr;
27
  FILE *infile;
28
  FILE *outfile;
29
  int numBytes, i;
30
  int c;
31
  unsigned char lineData[16];
32
  unsigned int chksum;
33
 
34
  if (argc != 4) {
35
    printf("Usage: %s <load addr, hex> <input file> <output file>\n",
36
           argv[0]);
37
    exit(1);
38
  }
39
  loadAddr = strtoul(argv[1], &endptr, 16);
40
  if (*endptr != '\0') {
41
    error("illegal load address %s", argv[1]);
42
  }
43
  infile = fopen(argv[2], "rb");
44
  if (infile == NULL) {
45
    error("cannot open input file %s", argv[2]);
46
  }
47
  outfile = fopen(argv[3], "wt");
48
  if (outfile == NULL) {
49
    error("cannot open output file %s", argv[3]);
50
  }
51
  while (1) {
52 234 hellwig
    if ((loadAddr & 0xFFFF) == 0) {
53
      fprintf(outfile, ":02000004");
54
      fprintf(outfile, "%04X", loadAddr >> 16);
55
      chksum = 0x02 + 0x04 +
56
               ((loadAddr >> 24) & 0xFF) +
57
               ((loadAddr >> 16) & 0xFF);
58
      fprintf(outfile, "%02X\n", (-chksum) & 0xFF);
59
    }
60 40 hellwig
    chksum = 0;
61
    for (numBytes = 0; numBytes < 16; numBytes++) {
62
      c = fgetc(infile);
63
      if (c == EOF) {
64
        break;
65
      }
66
      lineData[numBytes] = c;
67
      chksum += c;
68
    }
69
    if (numBytes == 0) {
70
      break;
71
    }
72
    fprintf(outfile, ":%02X%04X00", numBytes, loadAddr & 0xFFFF);
73
    for (i = 0; i < numBytes; i++) {
74
      fprintf(outfile, "%02X", lineData[i]);
75
    }
76
    chksum += numBytes;
77
    chksum += ((loadAddr >> 8) & 0xFF) +
78
              ((loadAddr >> 0) & 0xFF);
79
    fprintf(outfile, "%02X\n", (-chksum) & 0xFF);
80
    loadAddr += numBytes;
81
    if (c == EOF) {
82
      break;
83
    }
84
  }
85
  fprintf(outfile, ":00000001FF\n");
86
  fclose(infile);
87
  fclose(outfile);
88
  return 0;
89
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.