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

Subversion Repositories risc5x

[/] [risc5x/] [trunk/] [hex_conv/] [hex_conv.cpp] - Blame information for rev 3

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 mikej
//
2
// Risc5x
3
// www.OpenCores.Org - November 2001
4
//
5
//
6
// This library is free software; you can distribute it and/or modify it
7
// under the terms of the GNU Lesser General Public License as published
8
// by the Free Software Foundation; either version 2.1 of the License, or
9
// (at your option) any later version.
10
//
11
// This library is distributed in the hope that it will be useful, but
12
// WITHOUT ANY WARRANTY; without even the implied warranty of
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
// See the GNU Lesser General Public License for more details.
15
//
16
// A RISC CPU core.
17
//
18
// (c) Mike Johnson 2001. All Rights Reserved.
19
// mikej@opencores.org for support or any other issues.
20
//
21
// Revision list
22
//
23
// version 1.0 initial opencores release
24
//
25
 
26
#include <stdio.h>
27
#include <stdlib.h>
28
 
29
#include <iostream>
30
#include <fstream>
31
#include <list>
32
#include <vector>
33
#include <string>
34
using namespace std;
35
 
36
#define ROM_SIZE 0x800
37
 
38
class cLine
39
{
40
        string line;
41
public:
42
 
43
        typedef std::vector<cLine> List;
44
        cLine(string l) : line(l) {}
45
        string get() {return line;}
46
};
47
 
48
int hex_to_int(string hex, int start, int len)
49
{
50
        int result = 0;
51
 
52
        for (int i = start; i < (start + len); i++) {
53
          result <<= 4;
54
          if      (hex[i] >= '0' && hex[i] <= '9')
55
                  result += (hex[i] - '0');
56
          else if (hex[i] >= 'A' && hex[i] <= 'F')
57
                  result += (hex[i] - 'A') +10;
58
          else if (hex[i] >= 'a' && hex[i] <= 'f')
59
                  result += (hex[i] - 'a') +10;
60
          else
61
          {
62
                  printf("hex to int error \n");
63
                  exit (1);
64
          }
65
        }
66
        return result;
67
}
68
 
69
int main(int argc, char* argv[])
70
 
71
{
72
        // read file
73
        cLine::List l;
74
        string buffer;
75
        ifstream infile;
76
 
77
        if(argc != 2)
78
        {
79
                printf("no input file \n");
80
                return -1;
81
        }
82
 
83
        infile.open(argv[1]);
84
        if(infile.fail()) {
85
           printf("Could not open input file \n");
86
           return -1;
87
        }
88
        do
89
        {
90
                std::getline(infile,buffer,infile.widen('\n'));
91
                if(!buffer.empty())
92
                {
93
                   string::size_type sz1 = buffer.find(":");
94
                   if(sz1 != string::npos)
95
                  {
96
                                        sz1+=1;
97
                                        string::size_type sz2 = buffer.find(";");
98
                                        string real(&buffer[sz1]);
99
                                        l.push_back(cLine(real));
100
                   }
101
 
102
                }
103
        } while(!infile.fail());
104
        infile.close();
105
 
106
        // process
107
        int mem[ROM_SIZE];
108
        string line;
109
        int len =  l.size();
110
        int i,j,k,sum;
111
        int wc,type,data;
112
        int file_addr = 0;
113
        int addr = 0;
114
        int offset = 0;
115
        int mask = 0;
116
 
117
        // clear mem
118
        for (i = 0; i < ROM_SIZE; i++) mem[i] = 0;
119
 
120
        // process file
121
        for (j = 0; j < len-1; j++) {
122
 
123
          line = l[j].get();
124
          wc = hex_to_int(line,0,2);
125
          file_addr = hex_to_int(line,2,4);
126
          type = hex_to_int(line,6,2);
127
 
128
          sum = 0;
129
          for (i = 0; i < wc*2 + 9; i +=2) {
130
                sum += hex_to_int(line,i,2);
131
          }
132
          if ((sum & 0xff) != 0)
133
                  printf("incorrect checksum line %d \n", j+1);
134
          int value = 0;
135
 
136
          if (type == 0) {
137
                for (i = 0; i < wc*2; i +=4) {
138
                  value = hex_to_int(line,i + 8,2) + hex_to_int(line,i+10,2) * 256;
139
                  mem[addr] = value;
140
                  addr ++;
141
                }
142
          }
143
        }
144
        // print attribute statements
145
        /*
146
        for (k = 0; k < 6; k ++){
147
          mask = 0x3 << (k*2);
148
 
149
          printf("\n\n");
150
          for (j = 0; j < (ROM_SIZE/128); j++) {
151
                printf("attribute INIT_%02X of inst%d : label is \042",j,k);
152
                for (i = 0; i < 128; i+=4) {
153
 
154
                  data  = ((mem[(j*128) + (127 - i)] & mask) >> k*2);
155
                  data <<= 2;
156
                  data += ((mem[(j*128) + (126 - i)] & mask) >> k*2);
157
                  data <<= 2;
158
                  data += ((mem[(j*128) + (125 - i)] & mask) >> k*2);
159
                  data <<= 2;
160
                  data += ((mem[(j*128) + (124 - i)] & mask) >> k*2);
161
                  printf("%02X",data);
162
                }
163
                printf("\042;\n");
164
          }
165
        }
166
        */
167
 
168
        // print ucf statements
169
        for (k = 0; k < 6; k ++){
170
          mask = 0x3 << (k*2);
171
 
172
          printf("\n\n");
173
          for (j = 0; j < (ROM_SIZE/128); j++) {
174
                printf("INST PRAMS_%d_INST INIT_%02X = ",k,j);
175
                for (i = 0; i < 128; i+=4) {
176
 
177
                  data  = ((mem[(j*128) + (127 - i)] & mask) >> k*2);
178
                  data <<= 2;
179
                  data += ((mem[(j*128) + (126 - i)] & mask) >> k*2);
180
                  data <<= 2;
181
                  data += ((mem[(j*128) + (125 - i)] & mask) >> k*2);
182
                  data <<= 2;
183
                  data += ((mem[(j*128) + (124 - i)] & mask) >> k*2);
184
                  printf("%02X",data);
185
                }
186
                printf(";\n");
187
          }
188
        }
189
 
190
        return 0;
191
}

powered by: WebSVN 2.1.0

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