1 |
80 |
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 |
|
|
# Author(s):
|
29 |
|
|
# - Olivier Girard, olgirard@gmail.com
|
30 |
|
|
#
|
31 |
|
|
#------------------------------------------------------------------------------
|
32 |
|
|
# $Rev: 16 $
|
33 |
|
|
# $LastChangedBy: olivier.girard $
|
34 |
|
|
# $LastChangedDate: 2009-08-04 23:03:47 +0200 (Tue, 04 Aug 2009) $
|
35 |
|
|
#------------------------------------------------------------------------------
|
36 |
|
|
|
37 |
|
|
###############################################################################
|
38 |
|
|
# PARAMETER CHECK #
|
39 |
|
|
###############################################################################
|
40 |
|
|
|
41 |
|
|
if {$argc != 6} {
|
42 |
|
|
puts "ERROR : wrong number of arguments"
|
43 |
|
|
puts "USAGE : ihex2mem.tcl -ihex <input file> -out <output file> -mem_size <memory size>"
|
44 |
|
|
puts "Example : ihex2mem.tcl -ihex rom.ihex -out rom.mem -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 "MEMH 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 |
|
|
# MEMH Output
|
92 |
|
|
if [catch {open $out w } f_out] {
|
93 |
|
|
puts "ERROR Cannot create output file $out"
|
94 |
|
|
exit 1
|
95 |
|
|
}
|
96 |
|
|
|
97 |
|
|
|
98 |
|
|
#-----------------------------------------------------------------------------#
|
99 |
|
|
# CONVERSION #
|
100 |
|
|
#-----------------------------------------------------------------------------#
|
101 |
|
|
|
102 |
|
|
|
103 |
|
|
# Conversions procedure
|
104 |
|
|
proc hex2dec { val } {
|
105 |
|
|
set val [format "%u" 0x[string trimleft $val]]
|
106 |
|
|
return $val
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
|
110 |
|
|
# Initialize memory array (16 bit words)
|
111 |
|
|
set num_word [expr ($mem_size/2)-1]
|
112 |
|
|
for {set i 0} {$i <= $num_word} {incr i} {
|
113 |
|
|
set mem_arr($i) 0000
|
114 |
|
|
}
|
115 |
|
|
|
116 |
|
|
|
117 |
|
|
# Calculate Address offset (offset=(0x10000-memory_size))
|
118 |
|
|
set mem_offset [expr 65536-$mem_size]
|
119 |
|
|
|
120 |
|
|
|
121 |
|
|
# Process input file
|
122 |
|
|
while {[gets $f_ihex line] >= 0} {
|
123 |
|
|
|
124 |
|
|
# Process line
|
125 |
|
|
set byte_count [hex2dec [string range $line 1 2]]
|
126 |
|
|
set start_addr [expr ([hex2dec [string range $line 3 6]] - $mem_offset)]
|
127 |
|
|
set rec_type [string range $line 7 8]
|
128 |
|
|
|
129 |
|
|
if {$rec_type == 00} {
|
130 |
|
|
for {set i 0} {$i < $byte_count*2} {set i [expr $i+4]} {
|
131 |
|
|
set mem_msb [string range $line [expr $i+11] [expr $i+12]]
|
132 |
|
|
set mem_lsb [string range $line [expr $i+9] [expr $i+10]]
|
133 |
|
|
set addr [expr ($start_addr+$i/2)/2]
|
134 |
|
|
set mem_arr($addr) "$mem_msb$mem_lsb"
|
135 |
|
|
}
|
136 |
|
|
}
|
137 |
|
|
}
|
138 |
|
|
close $f_ihex
|
139 |
|
|
|
140 |
|
|
|
141 |
|
|
# Writing memory array to file
|
142 |
|
|
for {set i 0} {$i <= $num_word} {incr i} {
|
143 |
|
|
|
144 |
|
|
if {![expr $i%16]} {
|
145 |
|
|
puts -nonewline $f_out "\n@[format "%04x" $i] "
|
146 |
|
|
}
|
147 |
|
|
puts -nonewline $f_out " [format "%02s" $mem_arr($i) ]"
|
148 |
|
|
}
|
149 |
|
|
|
150 |
|
|
puts $f_out "\n"
|
151 |
|
|
close $f_out
|
152 |
|
|
|
153 |
|
|
exit 0
|