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

Subversion Repositories openmsp430

[/] [openmsp430/] [trunk/] [core/] [sim/] [rtl_sim/] [bin/] [ihex2mem.tcl] - Blame information for rev 200

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

Line No. Rev Author Line
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 17 olivier.gi
# Author(s):
29
#             - Olivier Girard,    olgirard@gmail.com
30
#
31 2 olivier.gi
#------------------------------------------------------------------------------
32 17 olivier.gi
# $Rev: 106 $
33
# $LastChangedBy: olivier.girard $
34
# $LastChangedDate: 2011-03-25 23:01:03 +0100 (Fri, 25 Mar 2011) $
35
#------------------------------------------------------------------------------
36 2 olivier.gi
 
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 106 olivier.gi
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 2 olivier.gi
}
100
 
101
#-----------------------------------------------------------------------------#
102
#                                 CONVERSION                                  #
103
#-----------------------------------------------------------------------------#
104
 
105
 
106
# Conversions procedure
107
proc hex2dec { val  } {
108
  set val [format "%u" 0x[string trimleft $val]]
109
  return $val
110
}
111
 
112
 
113
# Initialize memory array (16 bit words)
114
set num_word [expr ($mem_size/2)-1]
115
for {set i 0} {$i <= $num_word} {incr i} {
116
    set mem_arr($i) 0000
117
}
118
 
119
 
120
# Calculate Address offset (offset=(0x10000-memory_size))
121
set mem_offset [expr 65536-$mem_size]
122
 
123
 
124
# Process input file 
125
while {[gets $f_ihex line] >= 0} {
126
 
127
    # Process line
128
    set byte_count [hex2dec [string range $line 1 2]]
129
    set start_addr [expr ([hex2dec [string range $line 3 6]] - $mem_offset)]
130
    set rec_type   [string range $line 7 8]
131
 
132
    if {$rec_type == 00} {
133
        for {set i 0} {$i < $byte_count*2} {set i [expr $i+4]} {
134
            set mem_msb  [string range $line [expr $i+11] [expr $i+12]]
135
            set mem_lsb  [string range $line [expr $i+9]  [expr $i+10]]
136
            set addr     [expr ($start_addr+$i/2)/2]
137
            set mem_arr($addr) "$mem_msb$mem_lsb"
138
        }
139
    }
140
}
141
close $f_ihex
142
 
143
 
144
# Writing memory array to file
145
for {set i 0} {$i <= $num_word} {incr i} {
146
 
147
    if {![expr $i%16]} {
148
        puts -nonewline $f_out "\n@[format "%04x" $i] "
149
    }
150
    puts -nonewline $f_out " [format "%02s" $mem_arr($i) ]"
151
}
152
 
153
puts  $f_out "\n"
154
 
155 106 olivier.gi
if { "$out"!="-"} {
156
  close $f_out
157
}
158
 
159 2 olivier.gi
exit 0

powered by: WebSVN 2.1.0

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