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

Subversion Repositories eco32

[/] [eco32/] [trunk/] [tools/] [bin2dat/] [bin2dat.c] - Blame information for rev 320

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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