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

Subversion Repositories eco32

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

Go to most recent revision | 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
  fprintf(outfile, ":02000004");
52
  fprintf(outfile, "%04X", loadAddr >> 16);
53
  chksum = 0x02 + 0x04 +
54
           ((loadAddr >> 24) & 0xFF) +
55
           ((loadAddr >> 16) & 0xFF);
56
  fprintf(outfile, "%02X\n", (-chksum) & 0xFF);
57
  while (1) {
58
    chksum = 0;
59
    for (numBytes = 0; numBytes < 16; numBytes++) {
60
      c = fgetc(infile);
61
      if (c == EOF) {
62
        break;
63
      }
64
      lineData[numBytes] = c;
65
      chksum += c;
66
    }
67
    if (numBytes == 0) {
68
      break;
69
    }
70
    fprintf(outfile, ":%02X%04X00", numBytes, loadAddr & 0xFFFF);
71
    for (i = 0; i < numBytes; i++) {
72
      fprintf(outfile, "%02X", lineData[i]);
73
    }
74
    chksum += numBytes;
75
    chksum += ((loadAddr >> 8) & 0xFF) +
76
              ((loadAddr >> 0) & 0xFF);
77
    fprintf(outfile, "%02X\n", (-chksum) & 0xFF);
78
    loadAddr += numBytes;
79
    if (c == EOF) {
80
      break;
81
    }
82
  }
83
  fprintf(outfile, ":00000001FF\n");
84
  fclose(infile);
85
  fclose(outfile);
86
  return 0;
87
}

powered by: WebSVN 2.1.0

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