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

Subversion Repositories tv80

[/] [tv80/] [trunk/] [sc_env/] [app_localcfg/] [load_ihex.cpp] - Blame information for rev 104

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

Line No. Rev Author Line
1 104 ghutchis
/*
2
 * load_ihex.cpp
3
 *
4
 *  Created on: Feb 15, 2011
5
 *      Author: hutch
6
 */
7
 
8
#include "load_ihex.h"
9
#include <stdio.h>
10
#include <string.h>
11
#include <assert.h>
12
 
13
int inline readline(FILE *fh, char *buf)
14
{
15
  int c = 1, cnt = 0;
16
 
17
  if (feof(fh)) {
18
    *buf = (char) 0;
19
    return 0;
20
  }
21
  while (c) {
22
    c = fread (buf, 1, 1, fh);
23
    cnt++;
24
    if (c && (*buf == '\n')) {
25
      buf++;
26
      *buf = (char) 0;
27
      c = 0;
28
    }
29
    else buf++;
30
  }
31
  return cnt;
32
}
33
 
34
 
35
int load_ihex(char *filename, uint8_t *buffer, int max)
36
{
37
  FILE *fh;
38
  char line[80];
39
  char *lp;
40
  int rlen, addr, rtyp, databyte;
41
  int rv;
42
  int dcount = 0;
43
  int highest = 0;
44
 
45
  fh = fopen (filename, "r");
46
 
47
  rv = readline (fh, line);
48
    while (strlen(line) > 0) {
49
      sscanf (line, ":%02x%04x%02x", &rlen, &addr, &rtyp);
50
      lp = line + 9;
51
      for (int c=0; c<rlen; c++) {
52
        sscanf (lp, "%02x", &databyte);
53
        lp += 2;
54
        buffer[addr+c] = databyte; dcount++;
55
        assert (dcount < max);
56
        if ((addr+c) > highest) highest = addr+c;
57
      }
58
      rv = readline (fh, line);
59
    }
60
 
61
  fclose (fh);
62
  printf ("ENVMEM  : Read %d bytes from %s\n", dcount, filename);
63
  return (highest);
64
}

powered by: WebSVN 2.1.0

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