关于intel 32 hex文件格式以及hex2rom.sed

用ADS做出的memory,一般都弄成intel 32 hex的格式的,但是和用的mem的ram或者是rom格式都是有差别的。所以就要从i32格式转化成为满足需要的readmemh文件。

首先,先了解一下i32是一种什么格式的文件,举例

 1 :020000040000FA
 2 :10000000060000EA140000EA140000EA140000EA06
 3 :10001000140000EA0000A0E1130000EA230000EA57
 4 :100020008C009FE58C009FE5D1F021E300D040E2F9
 5 :10003000D2F021E340D040E2D3F021E390DF40E270
 6 :10004000D7F021E3D0DF40E2DBF021E3E0DF40E264
 7 :1000500050F021E3F0DF40E2960000EAFEFFFFEA05
 8 :10006000FEFFFFEAFEFFFFEAFEFFFFEA04E04EE2CA
 9 :100070000F502DE940109FE5000091E53C309FE5D1
10 :04008000A000000011
11 :00000001FF

Intel HEX文件是记录文本行的ASCII文本文件,在Intel HEX文件中,每一行是一个HEX记录由十六进制数组成的机器码或者静态数据,Intel HEX文件经常被用于将程序或数据传输存储到ROM.EPROM,大多数编程器和模拟器使用Intel HEX文件。

一个Intel HEX文件可以包含任意多的十六进制记录,每条记录有五个域,下面是一个记录的格式.

:llaaaattddCC
  :       --> Start code
  ll       --> Byte count
  aaaa --> Address
  tt      --> Record type
  dd     --> Data (位数不确定)
  CC    --> Checksum
 
1. :是每行开始的起始符号
2. byte count 表示的是数据域的长度内有多少字节的内容
3. Address表示的就是地址,有个限定就是64kbit
4. Record type (00-05) 表示的是类型分为以下几类
    00, 数据记录 (data record). contains data and 16-bit address.
    01, 文件结尾 (end of file record). Must occur exactly once per file in the last line of the file. The byte count is 00 and the data field is empty. Usually the address field is also 0000, in which case the complete line is ':00000001FF'. Originally the End Of File record could contain a start address for the program being loaded, e.g. :00AB2F0125 would cause a jump to address AB2F. This was convenient when programs were loaded from punched paper tape.
    02, 拓展段地址记录 (Extended Segment Address Record). segment-base address (two hex digit pairs in big endian order). Used when 16 bits are not enough, identical to 80x86 real mode addressing. The address specified by the data field of the most recent 02 record is multiplied by 16 (shifted 4 bits left) and added to the subsequent 00 record addresses. This allows addressing of up to a megabyte of address space. The address field of this record has to be 0000, the byte count is 02 (the segment is 16-bit). The least significant hex digit of the segment address is always 0.
    03, 开始段地址记录 (Start Segment Address Record). For 80x86 processors, it specifies the initial content of the CS:IP registers. The address field is 0000, the byte count is 04, the first two bytes are the CS value, the latter two are the IP value.
    04, 拓展线性地址记录 (Extended Linear Address Record). allowing for fully 32 bit addressing (up to 4GiB). The address field is 0000, the byte count is 02. The two data bytes (two hex digit pairs in big endian order) represent the upper 16 bits of the 32 bit address for all subsequent 00 type records until the next 04 type record comes. If there is not a 04 type record, the upper 16 bits default to 0000. To get the absolute address for subsequent 00 type records, the address specified by the data field of the most recent 04 record is added to the 00 record addresses.
    05, 开始线性地址记录 (Start Linear Address Record). The address field is 0000, the byte count is 04. The 4 data bytes represent the 32-bit value loaded into the EIP register of the 80386 and higher CPU.
5. data
6. checksum一种算法得来的校验和(略,笑)
two hex digits - the least significant byte of the two's complement of the sum of the values 
of all fields except fields 1 and 6 (Start code ":" byte and two hex digits of the Checksum). 
It is calculated by adding together the hex-encoded bytes (hex digit pairs), then leaving only 
the least significant byte of the result, and making a 2's complement (either by subtracting 
the byte from 0x100, or inverting it by XOR-ing with 0xFF and adding 0x01). If you are not working 
with 8-bit variables, you must suppress the overflow by AND-ing the result with 0xFF. The overflow 
may occur since both 0x100-0 and (0x00 XOR 0xFF)+1 equal 0x100. If the checksum is correctly 
calculated, adding all the bytes (the Byte count, both bytes in Address, the Record type, 
each Data byte and the Checksum) together will always result in a value wherein the least significant 
byte is zero (0x00).

For example, on :0300300002337A1E 03 + 00 + 30 + 00 + 02 + 33 + 7A = E2, 2's complement is 1E

好了,看明白了吧。那么下面就要进行处理了,主体的思想先说:

首先去掉第一行,最后一行,再把每行的最前面的address去掉和最后1个byte的checksum去掉,之后开始真正的处理过程开始substitute

待处理数据

1 :020000040000FA
2 :10000000060000EA140000EA140000EA140000EA06
3 :10001000140000EA0000A0E1130000EA230000EA57
4 :10002000FEFFFFEAFEFFFFEAFEFFFFEA04E04EE2CA
5 :100030000F502DE940109FE5000091E53C309FE5D1
6 :04004000A000000011
7 :00000001FF

处理完数据

 1 EA000006
 2 EA000014
 3 EA000014
 4 EA000014
 5 EA000014
 6 E1A00000
 7 EA000013
 8 EA000023
 9 EAFFFFFE
10 EAFFFFFE
11 EAFFFFFE
12 E24EE004
13 E92D500F
14 E59F1040
15 E5910000
16 E59F303C
17 000000A0

可以看到,需要处理一下lilttle endian和big endian的转化问题,由于arm是32bit的处理器,每行的instruction需要弄成32bit的,基本思想如上,祭出hex2rom.sed

 1 #! /bin/sed -f
 2 
 3 1d;$d;
 4 # remove the ^M mark, necessary for later processing
 5 s=[[:cntrl:]]==
 6 
 7 :pre
 8 # delete the :00 zero data line
 9 /^\(:00\)[0-9A-F]\{6\}[0-9A-F]\{2\}/d
10 # remove the leading address, record type and trailing checksum sector
11 s=^\(:..\)[0-9A-F]\{6\}\([0-9A-F]*\)[0-9A-F]\{2\}=\1\2=
12 
13 :proc
14 s=:10\([0-9A-F]\{2\}\)\([0-9A-F]\{2\}\)\([0-9A-F]\{2\}\)\([0-9A-F]\{2\}\)\(.*\)=:0C\5\n\4\3\2\1=
15 s=:0C\([0-9A-F]\{2\}\)\([0-9A-F]\{2\}\)\([0-9A-F]\{2\}\)\([0-9A-F]\{2\}\)\(.*\)=:08\5\n\4\3\2\1=
16 s=:08\([0-9A-F]\{2\}\)\([0-9A-F]\{2\}\)\([0-9A-F]\{2\}\)\([0-9A-F]\{2\}\)\(.*\)=:04\5\n\4\3\2\1=
17 s=:04\([0-9A-F]\{2\}\)\([0-9A-F]\{2\}\)\([0-9A-F]\{2\}\)\([0-9A-F]\{2\}\)\(.*\)=:00\5\n\4\3\2\1=
18 s=:00\n==
19 ################################################################################
20 #       Tue Oct 16 09:51:06 CST 2012    CREATED by poiu_elab@1207
21 ################################################################################

就不多解释了,完毕

posted @ 2012-10-16 21:49  poiu_elab  阅读(2326)  评论(0编辑  收藏  举报