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

Subversion Repositories openmsp430

[/] [openmsp430/] [trunk/] [fpga/] [altera_de0_nano_soc/] [sim/] [rtl_sim/] [bin/] [ihex2mem.tcl] - Blame information for rev 221

Details | Compare with Previous | View Log

Line No. Rev Author Line
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: ihex2mem.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   : 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 { "$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)-1]
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
 
145
# Writing memory array to file
146
for {set i 0} {$i <= $num_word} {incr i} {
147
 
148
    if {![expr $i%16]} {
149
        puts -nonewline $f_out "\n@[format "%04x" $i] "
150
    }
151
    puts -nonewline $f_out " [format "%02s" $mem_arr($i) ]"
152
}
153
 
154
puts  $f_out "\n"
155
 
156
if { "$out"!="-"} {
157
  close $f_out
158
}
159
 
160
exit 0

powered by: WebSVN 2.1.0

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