1 |
2 |
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: ihex2mem.tcl
|
27 |
|
|
#
|
28 |
|
|
#------------------------------------------------------------------------------
|
29 |
|
|
|
30 |
|
|
###############################################################################
|
31 |
|
|
# PARAMETER CHECK #
|
32 |
|
|
###############################################################################
|
33 |
|
|
|
34 |
|
|
if {$argc != 6} {
|
35 |
|
|
puts "ERROR : wrong number of arguments"
|
36 |
|
|
puts "USAGE : ihex2mem.tcl -ihex <input file> -out <output file> -mem_size <memory size>"
|
37 |
|
|
puts "Example : ihex2mem.tcl -ihex rom.ihex -out rom.mem -mem_size 2048"
|
38 |
|
|
exit 1
|
39 |
|
|
}
|
40 |
|
|
|
41 |
|
|
# default values
|
42 |
|
|
set ihex empty.in
|
43 |
|
|
set out empty.out
|
44 |
|
|
set mem_size -1
|
45 |
|
|
|
46 |
|
|
# parse arguments
|
47 |
|
|
for {set i 0} {$i < $argc} {incr i} {
|
48 |
|
|
switch -exact -- [lindex $argv $i] {
|
49 |
|
|
-ihex {set ihex [lindex $argv [expr $i+1]]; incr i}
|
50 |
|
|
-out {set out [lindex $argv [expr $i+1]]; incr i}
|
51 |
|
|
-mem_size {set mem_size [lindex $argv [expr $i+1]]; incr i}
|
52 |
|
|
}
|
53 |
|
|
}
|
54 |
|
|
|
55 |
|
|
# Make sure arugments were specified
|
56 |
|
|
if {[string eq $ihex empty.in]} {
|
57 |
|
|
puts "IHEX input file isn't specified"
|
58 |
|
|
exit 1
|
59 |
|
|
}
|
60 |
|
|
if {[string eq $out empty.out]} {
|
61 |
|
|
puts "MEMH output file isn't specified"
|
62 |
|
|
exit 1
|
63 |
|
|
}
|
64 |
|
|
if {[string eq $mem_size -1]} {
|
65 |
|
|
puts "Memory size isn't specified"
|
66 |
|
|
exit 1
|
67 |
|
|
}
|
68 |
|
|
|
69 |
|
|
|
70 |
|
|
###############################################################################
|
71 |
|
|
# CONVERSION PROCEDURE #
|
72 |
|
|
###############################################################################
|
73 |
|
|
|
74 |
|
|
#-----------------------------------------------------------------------------#
|
75 |
|
|
# OPEN FILES #
|
76 |
|
|
#-----------------------------------------------------------------------------#
|
77 |
|
|
|
78 |
|
|
# IHEX Input
|
79 |
|
|
if [catch {open $ihex r} f_ihex] {
|
80 |
|
|
puts "ERROR Cannot open input file $ihex"
|
81 |
|
|
exit 1
|
82 |
|
|
}
|
83 |
|
|
|
84 |
|
|
# MEMH Output
|
85 |
|
|
if [catch {open $out w } f_out] {
|
86 |
|
|
puts "ERROR Cannot create output file $out"
|
87 |
|
|
exit 1
|
88 |
|
|
}
|
89 |
|
|
|
90 |
|
|
|
91 |
|
|
#-----------------------------------------------------------------------------#
|
92 |
|
|
# CONVERSION #
|
93 |
|
|
#-----------------------------------------------------------------------------#
|
94 |
|
|
|
95 |
|
|
|
96 |
|
|
# Conversions procedure
|
97 |
|
|
proc hex2dec { val } {
|
98 |
|
|
set val [format "%u" 0x[string trimleft $val]]
|
99 |
|
|
return $val
|
100 |
|
|
}
|
101 |
|
|
|
102 |
|
|
|
103 |
|
|
# Initialize memory array (16 bit words)
|
104 |
|
|
set num_word [expr ($mem_size/2)-1]
|
105 |
|
|
for {set i 0} {$i <= $num_word} {incr i} {
|
106 |
|
|
set mem_arr($i) 0000
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
|
110 |
|
|
# Calculate Address offset (offset=(0x10000-memory_size))
|
111 |
|
|
set mem_offset [expr 65536-$mem_size]
|
112 |
|
|
|
113 |
|
|
|
114 |
|
|
# Process input file
|
115 |
|
|
while {[gets $f_ihex line] >= 0} {
|
116 |
|
|
|
117 |
|
|
# Process line
|
118 |
|
|
set byte_count [hex2dec [string range $line 1 2]]
|
119 |
|
|
set start_addr [expr ([hex2dec [string range $line 3 6]] - $mem_offset)]
|
120 |
|
|
set rec_type [string range $line 7 8]
|
121 |
|
|
|
122 |
|
|
if {$rec_type == 00} {
|
123 |
|
|
for {set i 0} {$i < $byte_count*2} {set i [expr $i+4]} {
|
124 |
|
|
set mem_msb [string range $line [expr $i+11] [expr $i+12]]
|
125 |
|
|
set mem_lsb [string range $line [expr $i+9] [expr $i+10]]
|
126 |
|
|
set addr [expr ($start_addr+$i/2)/2]
|
127 |
|
|
set mem_arr($addr) "$mem_msb$mem_lsb"
|
128 |
|
|
}
|
129 |
|
|
}
|
130 |
|
|
}
|
131 |
|
|
close $f_ihex
|
132 |
|
|
|
133 |
|
|
|
134 |
|
|
# Writing memory array to file
|
135 |
|
|
for {set i 0} {$i <= $num_word} {incr i} {
|
136 |
|
|
|
137 |
|
|
if {![expr $i%16]} {
|
138 |
|
|
puts -nonewline $f_out "\n@[format "%04x" $i] "
|
139 |
|
|
}
|
140 |
|
|
puts -nonewline $f_out " [format "%02s" $mem_arr($i) ]"
|
141 |
|
|
}
|
142 |
|
|
|
143 |
|
|
puts $f_out "\n"
|
144 |
|
|
close $f_out
|
145 |
|
|
|
146 |
|
|
exit 0
|