1 |
221 |
olivier.gi |
#!/usr/bin/tclsh
|
2 |
|
|
#------------------------------------------------------------------------------
|
3 |
|
|
# Copyright (C) 2001 Authors
|
4 |
|
|
#
|
5 |
|
|
# This source file may be used and distributed without restriction provided
|
6 |
|
|
# that this copyright statement is not removed from the file and that any
|
7 |
|
|
# derivative work contains the original copyright notice and the associated
|
8 |
|
|
# disclaimer.
|
9 |
|
|
#
|
10 |
|
|
# This source file is free software; you can redistribute it and/or modify
|
11 |
|
|
# it under the terms of the GNU Lesser General Public License as published
|
12 |
|
|
# by the Free Software Foundation; either version 2.1 of the License, or
|
13 |
|
|
# (at your option) any later version.
|
14 |
|
|
#
|
15 |
|
|
# This source is distributed in the hope that it will be useful, but WITHOUT
|
16 |
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
17 |
|
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
18 |
|
|
# License for more details.
|
19 |
|
|
#
|
20 |
|
|
# You should have received a copy of the GNU Lesser General Public License
|
21 |
|
|
# along with this source; if not, write to the Free Software Foundation,
|
22 |
|
|
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
23 |
|
|
#
|
24 |
|
|
#------------------------------------------------------------------------------
|
25 |
|
|
#
|
26 |
|
|
# File Name: ihex2mif.tcl
|
27 |
|
|
#
|
28 |
|
|
# Author(s):
|
29 |
|
|
# - Olivier Girard, olgirard@gmail.com
|
30 |
|
|
#
|
31 |
|
|
#------------------------------------------------------------------------------
|
32 |
|
|
# $Rev: 136 $
|
33 |
|
|
# $LastChangedBy: olivier.girard $
|
34 |
|
|
# $LastChangedDate: 2012-03-22 22:14:16 +0100 (Thu, 22 Mar 2012) $
|
35 |
|
|
#------------------------------------------------------------------------------
|
36 |
|
|
|
37 |
|
|
###############################################################################
|
38 |
|
|
# PARAMETER CHECK #
|
39 |
|
|
###############################################################################
|
40 |
|
|
|
41 |
|
|
if {$argc != 6} {
|
42 |
|
|
puts "ERROR : wrong number of arguments"
|
43 |
|
|
puts "USAGE : ihex2mif.tcl -ihex <input file> -out <output file> -mem_size <memory size>"
|
44 |
|
|
puts "Example : ihex2mif.tcl -ihex rom.ihex -out rom.mif -mem_size 2048"
|
45 |
|
|
exit 1
|
46 |
|
|
}
|
47 |
|
|
|
48 |
|
|
# default values
|
49 |
|
|
set ihex empty.in
|
50 |
|
|
set out empty.out
|
51 |
|
|
set mem_size -1
|
52 |
|
|
|
53 |
|
|
# parse arguments
|
54 |
|
|
for {set i 0} {$i < $argc} {incr i} {
|
55 |
|
|
switch -exact -- [lindex $argv $i] {
|
56 |
|
|
-ihex {set ihex [lindex $argv [expr $i+1]]; incr i}
|
57 |
|
|
-out {set out [lindex $argv [expr $i+1]]; incr i}
|
58 |
|
|
-mem_size {set mem_size [lindex $argv [expr $i+1]]; incr i}
|
59 |
|
|
}
|
60 |
|
|
}
|
61 |
|
|
|
62 |
|
|
# Make sure arugments were specified
|
63 |
|
|
if {[string eq $ihex empty.in]} {
|
64 |
|
|
puts "IHEX input file isn't specified"
|
65 |
|
|
exit 1
|
66 |
|
|
}
|
67 |
|
|
if {[string eq $out empty.out]} {
|
68 |
|
|
puts "MIF output file isn't specified"
|
69 |
|
|
exit 1
|
70 |
|
|
}
|
71 |
|
|
if {[string eq $mem_size -1]} {
|
72 |
|
|
puts "Memory size isn't specified"
|
73 |
|
|
exit 1
|
74 |
|
|
}
|
75 |
|
|
|
76 |
|
|
|
77 |
|
|
###############################################################################
|
78 |
|
|
# CONVERSION PROCEDURE #
|
79 |
|
|
###############################################################################
|
80 |
|
|
|
81 |
|
|
#-----------------------------------------------------------------------------#
|
82 |
|
|
# OPEN FILES #
|
83 |
|
|
#-----------------------------------------------------------------------------#
|
84 |
|
|
|
85 |
|
|
# IHEX Input
|
86 |
|
|
if [catch {open $ihex r} f_ihex] {
|
87 |
|
|
puts "ERROR Cannot open input file $ihex"
|
88 |
|
|
exit 1
|
89 |
|
|
}
|
90 |
|
|
|
91 |
|
|
# MIF Output
|
92 |
|
|
if { "$out"=="-"} {
|
93 |
|
|
set f_out stdout
|
94 |
|
|
} else {
|
95 |
|
|
if [catch {open $out w } f_out] {
|
96 |
|
|
puts "ERROR Cannot create output file $out"
|
97 |
|
|
exit 1
|
98 |
|
|
}
|
99 |
|
|
}
|
100 |
|
|
|
101 |
|
|
|
102 |
|
|
#-----------------------------------------------------------------------------#
|
103 |
|
|
# CONVERSION #
|
104 |
|
|
#-----------------------------------------------------------------------------#
|
105 |
|
|
|
106 |
|
|
|
107 |
|
|
# Conversions procedure
|
108 |
|
|
proc hex2dec { val } {
|
109 |
|
|
set val [format "%u" 0x[string trimleft $val]]
|
110 |
|
|
return $val
|
111 |
|
|
}
|
112 |
|
|
|
113 |
|
|
|
114 |
|
|
# Initialize memory array (16 bit words)
|
115 |
|
|
set num_word [expr ($mem_size/2)]
|
116 |
|
|
for {set i 0} {$i < $num_word} {incr i} {
|
117 |
|
|
set mem_arr($i) 0000
|
118 |
|
|
}
|
119 |
|
|
|
120 |
|
|
|
121 |
|
|
# Calculate Address offset (offset=(0x10000-memory_size))
|
122 |
|
|
set mem_offset [expr 65536-$mem_size]
|
123 |
|
|
|
124 |
|
|
|
125 |
|
|
# Process input file
|
126 |
|
|
while {[gets $f_ihex line] >= 0} {
|
127 |
|
|
|
128 |
|
|
# Process line
|
129 |
|
|
set byte_count [hex2dec [string range $line 1 2]]
|
130 |
|
|
set start_addr [expr ([hex2dec [string range $line 3 6]] - $mem_offset)]
|
131 |
|
|
set rec_type [string range $line 7 8]
|
132 |
|
|
|
133 |
|
|
if {$rec_type == 00} {
|
134 |
|
|
for {set i 0} {$i < $byte_count*2} {set i [expr $i+4]} {
|
135 |
|
|
set mem_msb [string range $line [expr $i+11] [expr $i+12]]
|
136 |
|
|
set mem_lsb [string range $line [expr $i+9] [expr $i+10]]
|
137 |
|
|
set addr [expr ($start_addr+$i/2)/2]
|
138 |
|
|
set mem_arr($addr) "$mem_msb$mem_lsb"
|
139 |
|
|
}
|
140 |
|
|
}
|
141 |
|
|
}
|
142 |
|
|
close $f_ihex
|
143 |
|
|
|
144 |
|
|
# MIF file header
|
145 |
|
|
puts $f_out [format "DEPTH = %5d; -- The size of memory in words" $num_word]
|
146 |
|
|
puts $f_out "WIDTH = 16; -- The size of data in bits"
|
147 |
|
|
puts $f_out "ADDRESS_RADIX = HEX; -- The radix for address values"
|
148 |
|
|
puts $f_out "DATA_RADIX = HEX; -- The radix for data values"
|
149 |
|
|
puts $f_out "CONTENT -- start of (address : data pairs)"
|
150 |
|
|
puts $f_out "BEGIN"
|
151 |
|
|
puts $f_out ""
|
152 |
|
|
|
153 |
|
|
# MIF file data
|
154 |
|
|
for {set i 0} {$i < $num_word} {incr i} {
|
155 |
|
|
puts $f_out [format "%04x : %02s;" $i $mem_arr($i)]
|
156 |
|
|
}
|
157 |
|
|
|
158 |
|
|
# MIF file footer
|
159 |
|
|
puts $f_out ""
|
160 |
|
|
puts $f_out "END;"
|
161 |
|
|
puts $f_out ""
|
162 |
|
|
|
163 |
|
|
if { "$out"!="-"} {
|
164 |
|
|
close $f_out
|
165 |
|
|
}
|
166 |
|
|
|
167 |
|
|
exit 0
|