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

Subversion Repositories z80soc

[/] [z80soc/] [trunk/] [V0.7.3/] [Software/] [C/] [bin/] [hex2mif.sh] - Blame information for rev 46

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 46 rrred
#!/bin/sh
2
# hex2mif.sh
3
# By: Ronivon Candido Costa - ronivon.costa@gmail.com
4
# 2010 - 12 - 05
5
# ----------------------------------------------------------------------------------------------
6
# This tool runs on Cygwin, Linux and Mac OS X
7
#
8
# hex2mif.sh will take an input file in Hex format and convert to a .mif format,
9
# which can be used to initialize rom/rams for Alrera and Xilinx devices.
10
# hex2mif.sh understand the formats:
11
# - Motorola HEX format
12
# - Z80ASM format (after assembled, use the View in Hex format, and copy the contents to rom.hex
13
#
14
# To use, pass the input file as parameter::
15
# " hex2mif.sh file.ihx  >rom.mif
16
# ----------------------------------------------------------------------------------------------
17
 
18
sortHexFile() {
19
 in=$1
20
 >$in.tmp
21
 while read line
22
 do
23
    if [[ "$line" != ":00000001FF" ]];then
24
       len=`echo ${line:1:2} | bc`*2
25
       addr=${line:3:4}
26
       echo "$addr:${line:9:$len}" >>$in.tmp
27
    fi
28
 done<$in
29
 cat $in.tmp | sort >$in
30
 echo ":END" >>$in
31
 rm $in.tmp
32
}
33
 
34
 
35
convMotorolaHexToMifXilinx() {
36
 
37
  in=$1
38
 
39
  let address=0
40
  while read line
41
  do
42
    if [[ "$line" != ":END" ]];then
43
       let len=${#line}-5
44
       fileaddress=`echo "obase=10;ibase=16;${line:0:4}" | bc`
45
 
46
          while [[ $address -lt $fileaddress ]]
47
          do
48
              echo "00000000"
49
              let address=address+1
50
          done
51
 
52
       let bytepos=5
53
       while [[ $len -gt 0 ]]
54
       do
55
          binvalue="00000000"`echo "obase=2;ibase=16;${line:$bytepos:2}" | bc`
56
          echo ${binvalue:(-8)}
57
          let bytepos=bytepos+2
58
          let len=len-2
59
          let address=address+1
60
       done
61
    fi
62
  done<$in
63
}
64
 
65
convMotorolaHexToMifAltera() {
66
 
67
  in=$1
68
 
69
  let address=0
70
 
71
echo "DEPTH = 16384;
72
WIDTH = 8;
73
ADDRESS_RADIX = HEX;
74
DATA_RADIX = BIN;
75
CONTENT
76
BEGIN"
77
 
78
  while read line
79
  do
80
    if [[ "$line" != ":END" ]];then
81
       let len=${#line}-5
82
       fileaddress=`echo "obase=10;ibase=16;${line:0:4}" | bc`
83
          while [[ $address -lt $fileaddress ]]
84
          do
85
              hexadd="0000"`echo "obase=16;ibase=10;${address:0:5}" | bc`
86
              echo "${hexadd:(-4)} : 00000000;"
87
              let address=address+1
88
          done
89
 
90
       let bytepos=5
91
       while [[ $len -gt 0 ]]
92
       do
93
          binvalue="00000000"`echo "obase=2;ibase=16;${line:$bytepos:2}" | bc`
94
          hexadd="0000"`echo "obase=16;ibase=10;${address:0:5}" | bc`
95
          echo "${hexadd:(-4)} : ${binvalue:(-8)};"
96
          let bytepos=bytepos+2
97
          let len=len-2
98
          let address=address+1
99
       done
100
    fi
101
  done<$in
102
echo "END;"
103
 
104
}
105
 
106
infile=rom.hex
107
cp $1 $infile
108
file=$infile.1
109
cp $infile $file
110
 
111
# Vefify if file is in Motorola Hex format, and convert to HEX Ascii codes
112
 
113
read line < $file
114
 
115
if [[ ${line:0:1} = ":" ]];then
116
    sortHexFile $file
117
    convMotorolaHexToMifAltera $file
118
else if [[ ${line:4:1} = ":" ]];then
119
        convMotorolaHexToMifAltera $file
120
     else echo "File in unknown format... not doing anything."
121
     fi
122
fi
123
 
124
rm $file
125
 

powered by: WebSVN 2.1.0

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