用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 ################################################################################

就不多解释了,完毕

转载于:https://www.cnblogs.com/poiu-elab/archive/2012/10/16/2726825.html

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

  1. Intel Hex 文件格式

    简介 这种文件格式一般是以hex为后缀名,在嵌入式MCU程序开发中,经常编译链接后生成一个这样文件,然后将这个文件烧写到MCU的ROM中. 所以,这个是开发工程的输出文件类型的一种. 发明这种文件格式 ...

  2. 很多人都不清楚HEX文件格式

    Intel HEX文件是由一行行符合Intel HEX文件格式的文本所构成的ASCII文本文件.在Intel HEX文件中,每一行包含一个HEX记录.这些记录由对应机器语言码和/或常量数据的十六进制编 ...

  3. 基础知识 | hex文件格式详解

    hex文件格式总结 什么是hex文件? 文件格式 指令类型(Record type) 校验和 :04 02B0 00 92020008 AE :04 0000 05 08000135 B9 :00 0 ...

  4. HEX文件格式解析(转)

    Hex格式文件有两种,一种是Intel的Intel HEX,另一种是Motorola(摩托罗拉)的SREC(又称MOT). Intel HEX 文件是由一行行符合Intel HEX 文件格式的文本所 ...

  5. c语言解析hex文件格式,HEX文件格式,ihex,hex解析

    来自:http://blog.csdn.net/GZFStudy/archive/2008/09/03/2873814.aspx Intel HEX 文件是由一行行符合Intel HEX 文件格式的文 ...

  6. hex文件格式学习记录

    .hex文件 .hex文件是什么 .hex文件的数据结构 按照记录类型具体分析 Mermaid Flowchart .hex文件是什么 它是由一行行符合Intel HEX 文件格式的文本所构成的ASC ...

  7. hex文件格式解析_玩转Hex文件

    00 关于Hex Hex文件格式(本文讲的是Intel Hex)是好多好多年以前定义的格式,解析这文件的工具网上搜出来的也是一大摞一大摞的.记住,我们就别瞎折腾自己写一个了哦.我们要学会站在巨人的肩膀 ...

  8. hex文件格式剖析,以及hex与bin文件互相转换

    大家好,我是学电子的小白白. 熟悉单片机开发的朋友,应该经常见到*.hex后缀的文件,它是单片机和嵌入式工程编译输出的一种常见的目标文件格式(比如keil就能编译输出hex文件),通过烧写工具把它下载 ...

  9. 基本:HEX文件格式定义

    0x01 概述 此处简单的对于HEX文件进行一些说明介绍,主要介绍了关于Hex文件基本的架构与数据定义 此章过于简单以至于没多少字,主要是hex文件本就是使用一种可视化显示不在Ascii编码内真实数据 ...

最新文章

  1. web前端开发培训有哪些学习阶段
  2. idea 使用maven构建项目时,target bytecode version经常自动变化
  3. jzoj4616-[NOI2016模拟7.12]二进制的世界【平衡规划,dp】
  4. 饿了么想解决外卖小哥马路杀手的问题,但用无人机?
  5. 解决cxgrid主从表数据显示不全的问题
  6. 中英文对照 —— 软件与病毒、电子与硬件
  7. docker基础容器中bash: vi: command not found问题解决
  8. 7-7 mmh学长的大数模板 (20分)
  9. android qq 登陆 简书,第三方登录 — QQ登录
  10. android qq 功能,Android 调用QQ相关的功能
  11. raspberry pi系统安装
  12. [JAVA冷知识]什么是逆变(contravariant)与协变(covariant)?数组是否支持协变逆变?泛型呢?
  13. 此计算机上的操作系统或service pack,win10系统安装补丁失败提示缺少service pack系统组件的设置办法...
  14. 如何随心意改变桌面快捷方式的图标
  15. 别瞎扯,元宇宙就是没有切实发展?
  16. RK3399平台开发系列讲解(其他篇)1.31、 什么是虚拟化
  17. SOA、RMI、RPC、Rest、RestFul、Soap、WebService 详解
  18. 默小伟网站开发帮助文档UI模板
  19. 复制黏贴直接上传图片
  20. Netlogo笔记07:蚁群算法实现TSP问题可视化

热门文章

  1. 最近整理关于SQL Server2005性能优化技巧
  2. linux 433发送驱动
  3. ubuntu下vim + ctags + taglist配置和使
  4. gj6 深入python的set和dict
  5. python 源码保护_Python代码保护
  6. 低代码这么火,它的人才认证你考了吗?
  7. 浅谈阿里云混合云的探索与实践
  8. 一站式快速自助建站-超低价0代码建站套餐助你轻松拥有自己的网站
  9. Seata 1.4.0 重磅发布
  10. 搜索引擎新架构:与SQL不得不说的故事