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

Subversion Repositories minsoc

[/] [minsoc/] [trunk/] [utils/] [contributions/] [initialized_onchip_ram/] [bin2init.py] - Blame information for rev 86

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

Line No. Rev Author Line
1 40 rfajardo
"""
2
*****************************************************************************
3
                                                                            *
4
                    H E A D E R   I N F O R M A T I O N                     *
5
                                                                            *
6
*****************************************************************************
7
Project Name                   : SysPy (System Python)
8
                                 http://cgi.di.uoa.gr/~evlog/syspy.html
9
 
10
File Name                      : bin2init.py
11
 
12
Created by                     : Evangelos Logaras
13
 
14
 
15
*****************************************************************************
16
                                                                            *
17
                      C O P Y R I G H T   N O T I C E                       *
18
                                                                            *
19
*****************************************************************************
20
 
21
  This library is free software; you can redistribute it and/or
22
  modify it under the terms of the GNU Lesser General Public
23
  License as published by the Free Software Foundation;
24
  version 2.1 of the License, a copy of which is available from
25
  http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt.
26
 
27
  This library is distributed in the hope that it will be useful,
28
  but WITHOUT ANY WARRANTY; without even the implied warranty of
29
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
30
  Lesser General Public License for more details.
31
 
32
  You should have received a copy of the GNU Lesser General Public
33
  License along with this library; if not, write to the Free Software
34
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
35
 
36
 
37
*****************************************************************************
38
                                                                            *
39
                           D E S C R I P T I O N                            *
40
                                                                            *
41
*****************************************************************************
42
 
43
 Generates block_ram.init file from binary images. Binary images are first converted in hex files
44
 using bin2hex.c file, provided with the ORPSoC v2 project. bin2hex executable must exist in the same folder with this script.
45
 
46
 Currently init file is generated for Xilinx's RAMB16_S9 BRAMs
47
 
48
 Usage: python bin2init.py <file.bin> (Python 2.6)
49
"""
50
 
51
import commands
52
import sys
53
 
54
 
55
# Python's variable declarations
56
#----------------------------------------------------------------------------------------------------------------------------------
57
y = '  '
58
mem_arr = []
59
block_ram_num = 4
60
block0 = []
61
block1 = []
62
block2 = []
63
block3 = []
64
block_ram = [block3, block2, block1, block0]
65
init_arr = []
66
mem_size2 = 8192
67
mem_count = 0
68
bram_count = -1
69
init_count = -1
70
hex_count = 0
71
zero_pad = ''
72
filename = ''
73
#----------------------------------------------------------------------------------------------------------------------------------
74
 
75
# Exceptions' class
76
#----------------------------------------------------------------------------------------------------------------------------------
77
class MyExceptions(Exception):
78
       def __init__(self, value):
79
              self.value = value
80
       def __str__(self):
81
              return repr(self.value)
82
#----------------------------------------------------------------------------------------------------------------------------------
83
 
84
# Raising exception if a *.bin file is not provided as an argument
85
#----------------------------------------------------------------------------------------------------------------------------------
86
filename = sys.argv[len(sys.argv) - 1]
87
 
88
if (filename.find(".bin") == -1):
89
       raise MyExceptions("*.bin file required")
90
#----------------------------------------------------------------------------------------------------------------------------------
91
 
92
i = filename.find(".bin")
93
 
94
filename = filename[:i]
95
 
96
# Deleting old *.hex and *.bin files
97
#----------------------------------------------------------------------------------------------------------------------------------
98
commands.getoutput("rm " + filename + ".hex")
99
commands.getoutput("rm " + filename + ".init")
100
#----------------------------------------------------------------------------------------------------------------------------------
101
 
102
## Calling bin2hex executable to convert *.bin file to *.hex
103
commands.getoutput("./bin2hex " + filename + ".bin 4 > "+ filename + ".hex")
104
 
105
# Opening the *.hex and the *.init file
106
#----------------------------------------------------------------------------------------------------------------------------------
107
hexFile = open(filename + ".hex", 'r')
108
initFile = open(filename + ".init", 'w')
109
#----------------------------------------------------------------------------------------------------------------------------------
110
 
111
# Reading the *.hex file and appending its contents to mem_arr[]
112
#----------------------------------------------------------------------------------------------------------------------------------
113
y = ' '
114
hex_count = 0
115
while(y):
116
       hex_count = hex_count + 1
117
       if (hex_count == 127):
118
              mem_arr.append("00000000")
119
 
120
       y = hexFile.readline()
121
       mem_arr.append(y)
122
#----------------------------------------------------------------------------------------------------------------------------------
123
 
124
# Reading mem_arr[] and creating the contents of BRAMs
125
#----------------------------------------------------------------------------------------------------------------------------------
126
for i in range(len(mem_arr)):
127
       bram_count = bram_count + 1
128
       if (bram_count < 32):
129
              block_ram[0].append(mem_arr[i][6:8])
130
              block_ram[1].append(mem_arr[i][4:6])
131
              block_ram[2].append(mem_arr[i][2:4])
132
              block_ram[3].append(mem_arr[i][0:2])
133
 
134
       elif (bram_count >= 32):
135
              bram_count = 0
136
 
137
              init_count = init_count + 1
138
 
139
              if (init_count >= 64):
140
                     init_count = 0
141
                     mem_count = mem_count + 1
142
 
143
              hex_init_count = str(hex(init_count))
144
              hex_init_count = hex_init_count[2:]
145
              hex_init_count = hex_init_count.upper()
146
              if (init_count < 16):
147
                     hex_init_count = '0' + hex_init_count
148
 
149
 
150
              for j in range((block_ram_num - 1), -1, -1):
151
                     if (j == (block_ram_num - 1)):
152
                            init_arr.append(";\ndefparam MEM[" + str(mem_count) + "].block_ram_" + str(j) + ".INIT_" + hex_init_count + " = 256'h")
153
                            block_ram[j].reverse()
154
                            for k in range(len(block_ram[j])):
155
                                   init_arr.append(block_ram[j][k].replace("\n", ''))
156
                     else:
157
                            init_arr.append(";\ndefparam MEM[" + str(mem_count) + "].block_ram_" + str(j) + ".INIT_" + hex_init_count + " = 256'h")
158
                            block_ram[j].reverse()
159
                            for k in range(len(block_ram[j])):
160
                                   init_arr.append(block_ram[j][k].replace("\n", ''))
161
 
162
              block_ram[0] = []
163
              block_ram[1] = []
164
              block_ram[2] = []
165
              block_ram[3] = []
166
 
167
              block_ram[0].append(mem_arr[i][6:8])
168
              block_ram[1].append(mem_arr[i][4:6])
169
              block_ram[2].append(mem_arr[i][2:4])
170
              block_ram[3].append(mem_arr[i][0:2])
171
 
172
 
173
if (bram_count != -1):
174
       init_count = init_count + 1
175
       hex_init_count = str(hex(init_count))
176
       hex_init_count = hex_init_count[2:]
177
       hex_init_count = hex_init_count.upper()
178
       if (init_count < 16):
179
              hex_init_count = '0' + hex_init_count
180
 
181
       if (init_count == 0):
182
              for j in range(64 - 2 * bram_count):
183
                     zero_pad = zero_pad + '0'
184
       else:
185
              for j in range(64 - 2 * bram_count):
186
                     zero_pad = zero_pad + '0'
187
 
188
       for j in range((block_ram_num - 1), -1, -1):
189
              init_arr.append(";\ndefparam MEM[" + str(mem_count) + "].block_ram_" + str(j) + ".INIT_" + hex_init_count + " = 256'h")
190
              block_ram[j].reverse()
191
              init_arr.append(zero_pad)
192
              for k in range(len(block_ram[j])):
193
                     init_arr.append(block_ram[j][k].replace("\n", ''))
194
 
195
init_arr.append(';')
196
#----------------------------------------------------------------------------------------------------------------------------------
197
 
198
# Writing BRAMs contetns to *.init file
199
#----------------------------------------------------------------------------------------------------------------------------------
200
i = init_arr[0].find(";/n")
201
 
202
init_arr[0] = init_arr[0][i + 2:]
203
 
204
for i in range(len(init_arr)):
205
       initFile.write(init_arr[i])
206
#----------------------------------------------------------------------------------------------------------------------------------
207
 
208
# Closing the *.hex and the *.init file
209
#----------------------------------------------------------------------------------------------------------------------------------       
210
hexFile.close()
211
initFile.close()
212
#----------------------------------------------------------------------------------------------------------------------------------

powered by: WebSVN 2.1.0

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