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

Subversion Repositories minirisc

[/] [minirisc/] [trunk/] [scode/] [hex2v.c] - Blame information for rev 7

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 lampret
/* ***********************************************************************
2
  The Free IP Project
3
  Free-RISC8 -- Verilog 8-bit Microcontroller
4
  (c) 1999, The Free IP Project and Thomas Coonan
5
 
6
 
7
  FREE IP GENERAL PUBLIC LICENSE
8
  TERMS AND CONDITIONS FOR USE, COPYING, DISTRIBUTION, AND MODIFICATION
9
 
10
  1.  You may copy and distribute verbatim copies of this core, as long
11
      as this file, and the other associated files, remain intact and
12
      unmodified.  Modifications are outlined below.
13
  2.  You may use this core in any way, be it academic, commercial, or
14
      military.  Modified or not.
15
  3.  Distribution of this core must be free of charge.  Charging is
16
      allowed only for value added services.  Value added services
17
      would include copying fees, modifications, customizations, and
18
      inclusion in other products.
19
  4.  If a modified source code is distributed, the original unmodified
20
      source code must also be included (or a link to the Free IP web
21
      site).  In the modified source code there must be clear
22
      identification of the modified version.
23
  5.  Visit the Free IP web site for additional information.
24
      http://www.free-ip.com
25
 
26
*********************************************************************** */
27
 
28
// Intel HEX to Verilog converter.
29
//
30
// Usage:
31
//    hex2v <file>
32
//
33
// You probably want to simply redirect the output into a file.
34
//
35
#include <stdio.h>
36
#include <stdlib.h>
37
#include <string.h>
38
 
39
// Input and Output file streams.
40
FILE *fpi;
41
 
42
// Well.. Let's read stuff in completely before outputting.. Programs
43
// should be pretty small..
44
//
45
#define MAX_MEMORY_SIZE  2048
46
struct {
47
   unsigned int  nAddress;
48
   unsigned int  byData;
49
} Memory[MAX_MEMORY_SIZE];
50
 
51
char szLine[80];
52
unsigned int  start_address, address, ndata_bytes, ndata_words;
53
unsigned int  data;
54
unsigned int  nMemoryCount;
55
 
56
int main (int argc, char *argv[])
57
{
58
   int  i;
59
 
60
   if (argc != 2) {
61
      printf ("\nThe Synthetic PIC --- Intel HEX File to Verilog memory file");
62
      printf ("\nUsage: hex2verilog <infile>");
63
      printf ("\n");
64
      return 0;
65
   }
66
 
67
 
68
   // Open input HEX file
69
   fpi=fopen(argv[1], "r");
70
   if (!fpi) {
71
      printf("\nCan't open input file %s.\n", argv[1]);
72
      return 1;
73
   }
74
 
75
   // Read in the HEX file
76
   //
77
   // !! Note, that things are a little strange for us, because the PIC is
78
   //    a 12-bit instruction, addresses are 16-bit, and the hex format is
79
   //    8-bit oriented!!
80
   //
81
   nMemoryCount = 0;
82
   while (!feof(fpi)) {
83
      // Get one Intel HEX line
84
      fgets (szLine, 80, fpi);
85
      if (strlen(szLine) >= 10) {
86
         // This is the PIC, with its 12-bit "words".  We're interested in these
87
         // words and not the bytes.  Read 4 hex digits at a time for each
88
         // address.
89
         //
90
         sscanf (&szLine[1], "%2x%4x", &ndata_bytes, &start_address);
91
         if (start_address >= 0 && start_address <= 20000 && ndata_bytes > 0) {
92
            // Suck up data bytes starting at 9th byte.
93
            i = 9;
94
 
95
            // Words.. not bytes..
96
            ndata_words   = ndata_bytes/2;
97
            start_address = start_address/2;
98
 
99
            // Spit out all the data that is supposed to be on this line.
100
            for (address = start_address; address < start_address + ndata_words; address++) {
101
               // Scan out 4 hex digits for a word.  This will be one address.
102
               sscanf (&szLine[i], "%04x", &data);
103
 
104
               // Need to swap bytes...
105
               data = ((data >> 8) & 0x00ff) | ((data << 8) & 0xff00);
106
               i += 4;
107
 
108
               // Store in our memory buffer
109
               Memory[nMemoryCount].nAddress = address;
110
               Memory[nMemoryCount].byData   = data;
111
               nMemoryCount++;
112
            }
113
         }
114
      }
115
   }
116
   fclose (fpi);
117
 
118
   // Now output the Verilog $readmemh format!
119
   //
120
   for (i = 0; i < nMemoryCount; i++) {
121
      printf ("\n@%03X %03X", Memory[i].nAddress, Memory[i].byData);
122
   }
123
   printf ("\n");
124
 
125
}

powered by: WebSVN 2.1.0

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