Intel Hex概述 以及 intel2readmemh 和 Intel HEX to BINARY File Converter Utility

什么是Intel Hex文件?

 

Intel HEX文件时遵循Intel HEX文件格式的ASCII文本文件。在Intel HEX文件的每一行都包含了 一个HEX记录。这些记录是由一些代表机器语言代码和常量的16进制数据组成的。Intel HEX文件常用来传输要存储在ROM 或者 EPROM中的程序和数据。大部分的EPROM编程器能使用Intel HEX文件。

 

Intel HEX文件组成:

 

Intel HEX由任意数量的十六进制记录组成。每个记录包含5个域,它们按以下格式排列:

Start Code 每个Intel HEX记录都由冒号开头。

Byte count 是数据长度域,它代表记录当中数据字节的数量。

Address 是地址域,它代表记录当中数据的起始地址。

Record type是代表HEX记录类型的域,它可能是以下数据当中的一个:

00-数据记录

01-文件结束记录

02-扩展段地址记录

03-开始段地址记录

04-扩展线性地址记录

05-开始线性地址记录

Data 是数据域,一个记录可以有许多数据字节。记录当中数据字节的数量必须和数据长度域中指定的数字相符。

Checksum是校验和域,它表示这个记录的校验和。校验和的计算是通过将记录当中所有十六进制编码数字对的值相加,以256为模进行以下补足。

 

完整的hex文件一般有头行,数据行,结束行。

举个例子:

:020000042A00D0
:10000000D1DC4B843410D7730D000000FFFFFFFFDD
:10001000FFFFFFFF500000005000002AD8CB000077

:040000052A000000CD
:00000001FF

 

第一行(头行):

1) 02代表数据域长度为0x02,即2A 00两个字节都为数据域

2) 0000代表地址,对于扩展线性地址而言,这个值一直为0000

3) 04代表扩展线性地址

4) 2A 00代表基址值为0x2A 00

5) D0代表校验值

当扩展线性地址被读取时,扩展线性地址值将会被保存,并且作用于后面从intel hex文件读取的子记录,同时扩展线性地址将一直发挥作用直到下一次扩展性线性地址读取。

 

 

第二行(数据行):

1) 10代表数据域长度为0x10,即D1 DC 4B 84 34 10 D7 73 0D 00 00 00 FF FF FF FF 16个字节都为数据域

2) 0000代表地址偏移为0000

3) 00代表数据域

4) D1 DC 4B 84 34 10 D7 73 0D 00 00 00 FF FF FF FF 代表数据值

5) 77代表校验值

绝对地址记录是扩展线性基址加上地址偏移所决定的,这第二行的数据绝对地址计算如下:

          0000                                                地址偏移

2A00 扩展线性地址

-------------------

2A00 0000 绝对地址

 

 

倒数第二行(最后的数据行):

1) 04代表数据域长度为0x04,即2A 00 00 00 4个字节都为数据域

2) 0000代表地址偏移为0000

3) 05代表开始线性地址并且后面才是真正数据记录。而真正的数据记录个人理解为后面读取的mbn数据。

4) 2A 00 00 00 代表真正数据记录的基址

5) CD代表校验值

 

 

最后行(结束行):

1) 00代表数据域长度为0

2) 0000代表地址将放入到内存的位置

3) 01代表文件结束

4)FF校验值(01h + NOT(00h + 00h + 00h +01h))

 

下面是一些工具:

1、Intel HEX to BINARY File Converter Utility

This utility program creates a BINARY file from an Intel HEX file. You can use BINARY files with most EPROM programmers and you can easily use them for CRC or checksum calculations. Options for this utility program are listed below:

Syntax: HEX2BIN [/option] hexfile [binfile]

hexfile is the Intel HEX input file
binfile is the binary file to create

option  may be any of the following

  /Ln     Binary file length
  /Pn     Pad data for binary file
  /On     Address offset (to add to HEX records)
  /M      Merge data into existing BIN file
  /Q      Quiet mode (no statistics are displayed)
  /X      Don't process ext. segment/linear address records

  /?      This help text

http://www.keil.com/download/docs/7.asp

2、Intel HEX to Verilog converter.

//
// Copyright (c) 1999 Thomas Coonan (tcoonan@mindspring.com)
//
//    This source code is free software; you can redistribute it
//    and/or modify it in source code form under the terms of the GNU
//    General Public License as published by the Free Software
//    Foundation; either version 2 of the License, or (at your option)
//    any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program; if not, write to the Free Software
//    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
//

// Intel HEX to Verilog converter.
//
// Usage:
//    hex2v <file>
//
// You probably want to simply redirect the output into a file.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Input and Output file streams.
FILE *fpi;

// Well.. Let's read stuff in completely before outputting.. Programs
// should be pretty small..
//
#define MAX_MEMORY_SIZE  1024
struct {
   unsigned int  nAddress;
   unsigned int  byData;
} Memory[MAX_MEMORY_SIZE];

char szLine[80];
unsigned int  start_address, address, ndata_bytes, ndata_words;
unsigned int  data;
unsigned int  nMemoryCount;

int main (int argc, char *argv[])
{
   int  i;

   if (argc != 2) {
      printf ("\nThe Synthetic PIC --- Intel HEX File to Verilog memory file");
      printf ("\nUsage: hex2verilog <infile>");
      printf ("\n");
      return 0;
   }


   // Open input HEX file
   fpi=fopen(argv[1], "r");
   if (!fpi) {
      printf("\nCan't open input file %s.\n", argv[1]);
      return 1;
   }

   // Read in the HEX file
   //
   // !! Note, that things are a little strange for us, because the PIC is
   //    a 12-bit instruction, addresses are 16-bit, and the hex format is
   //    8-bit oriented!!
   //
   nMemoryCount = 0;
   while (!feof(fpi)) {
      // Get one Intel HEX line
      fgets (szLine, 80, fpi);
      if (strlen(szLine) >= 10) {
         // This is the PIC, with its 12-bit "words".  We're interested in these
         // words and not the bytes.  Read 4 hex digits at a time for each
         // address.
         //
         sscanf (&szLine[1], "%2x%4x", &ndata_bytes, &start_address);
         if (start_address >= 0 && start_address <= 20000 && ndata_bytes > 0) {
            // Suck up data bytes starting at 9th byte.
            i = 9;

            // Words.. not bytes..
            ndata_words   = ndata_bytes/2;
            start_address = start_address/2;

            // Spit out all the data that is supposed to be on this line.
            for (address = start_address; address < start_address + ndata_words; address++) {
               // Scan out 4 hex digits for a word.  This will be one address.
               sscanf (&szLine[i], "%04x", &data);

               // Need to swap bytes...
               data = ((data >> 8) & 0x00ff) | ((data << 8) & 0xff00);
               i += 4;

               // Store in our memory buffer
               Memory[nMemoryCount].nAddress = address;
               Memory[nMemoryCount].byData   = data;
               nMemoryCount++;
            }
         }
      }
   }
   fclose (fpi);

   // Now output the Verilog $readmemh format!
   //
   for (i = 0; i < nMemoryCount; i++) {
      printf ("\n@%03X %03X", Memory[i].nAddress, Memory[i].byData);
   }
   printf ("\n");

}

Hex数据:

:020000040000FA
:100000000000022812309A0001309B00803000006E
:100010000000DE2000000000831200009D001B088D
:100020009C0014309E0001309F00831200001C08C9
:10003000A0001D08A100FF309C07031C9D032008A1
:10004000210403199A2804301E07A000A101A10D64
:100050001F08A10720089A0021089B00803000009B
:100060000000DE200000000083120000A3001B0837
:10007000A2001E089A001F089B00803000000000AC
:10008000DE200000000083120000A500A1001B0874
:10009000A400A00002301E07A000A101A10D1F08AE
:1000A000A10720089A0021089B0080300000000072
:1000B000DE200000000083120000A100A7001B0842
:1000C000A000A600831200002208A6002308A700B3
:1000D000FF30A207031CA303260827040319932853
:1000E00024089A0025089B00803000000000FF20B3
:1000F0000000000083120000A600200883120313F2
:1001000084008313831200002118831726088312AA
:100110000313800083120000A40A0319A50AA00A91
:100120000319A10A62280630831200009E070318F3
:100130009F0A1528000000009E280800AC01AD01B0
:100140002C08A800831285002D08A900AA012908FF
:100150008600280888004030AC070318AD0A00303C
:100160002D02031DB528FF302C02031CA028AC0172
:10017000AD012C08831285002D08A800A9012808CC
:1001800086002C08A800A9010808AA00AB012808CD
:100190002A06031DCF2829082B060319D028202161
:1001A0004030AC070318AD0A00302D02031DDA28D9
:1001B000FF302C02031CB92820210800003A031943
:1001C000E628803A0319F0289B0100341A088400BD
:1001D00083131B18831700089B00840A000808007B
:1001E000000000000D2199009A0F9B039B0A00005C
:1001F00000000D21980019089B0018080800003A1B
:1002000003190629803A03190D2900341A088400BD
:1002100083131B188317000808001B088A001A089C
:100220008200080002340034233401342C340034BA
:1002300002340034253401342E34003401340034C7
:0C024000000020290800003400340034C5
:00000001FF
 

转换结果:

地址    数据
@000 000
@000 000
@001 2802
@002 3012
@003 09A
@004 3001
@005 09B
@006 3080
@007 000
@008 000
@009 20DE
@00A 000
@00B 000
@00C 1283
@00D 000
@00E 09D
@00F 81B
@010 09C
@011 3014
@012 09E
@013 3001
@014 09F
@015 1283
@016 000
@017 81C
@018 0A0
@019 81D
@01A 0A1
@01B 30FF
@01C 79C
@01D 1C03
@01E 39D
@01F 820
@020 421
@021 1903
@022 289A
@023 3004
@024 71E
@025 0A0
@026 1A1
@027 DA1
@028 81F
@029 7A1
@02A 820
@02B 09A
@02C 821
@02D 09B
@02E 3080
@02F 000
@030 000
@031 20DE
@032 000
@033 000
@034 1283
@035 000
@036 0A3
@037 81B
@038 0A2
@039 81E
@03A 09A
@03B 81F
@03C 09B
@03D 3080
@03E 000
@03F 000
@040 20DE
@041 000
@042 000
@043 1283
@044 000
@045 0A5
@046 0A1
@047 81B
@048 0A4
@049 0A0
@04A 3002
@04B 71E
@04C 0A0
@04D 1A1
@04E DA1
@04F 81F
@050 7A1
@051 820
@052 09A
@053 821
@054 09B
@055 3080
@056 000
@057 000
@058 20DE
@059 000
@05A 000
@05B 1283
@05C 000
@05D 0A1
@05E 0A7
@05F 81B
@060 0A0
@061 0A6
@062 1283
@063 000
@064 822
@065 0A6
@066 823
@067 0A7
@068 30FF
@069 7A2
@06A 1C03
@06B 3A3
@06C 826
@06D 427
@06E 1903
@06F 2893
@070 824
@071 09A
@072 825
@073 09B
@074 3080
@075 000
@076 000
@077 20FF
@078 000
@079 000
@07A 1283
@07B 000
@07C 0A6
@07D 820
@07E 1283
@07F 1303
@080 084
@081 1383
@082 1283
@083 000
@084 1821
@085 1783
@086 826
@087 1283
@088 1303
@089 080
@08A 1283
@08B 000
@08C AA4
@08D 1903
@08E AA5
@08F AA0
@090 1903
@091 AA1
@092 2862
@093 3006
@094 1283
@095 000
@096 79E
@097 1803
@098 A9F
@099 2815
@09A 000
@09B 000
@09C 289E
@09D 008
@09E 1AC
@09F 1AD
@0A0 82C
@0A1 0A8
@0A2 1283
@0A3 085
@0A4 82D
@0A5 0A9
@0A6 1AA
@0A7 829
@0A8 086
@0A9 828
@0AA 088
@0AB 3040
@0AC 7AC
@0AD 1803
@0AE AAD
@0AF 3000
@0B0 22D
@0B1 1D03
@0B2 28B5
@0B3 30FF
@0B4 22C
@0B5 1C03
@0B6 28A0
@0B7 1AC
@0B8 1AD
@0B9 82C
@0BA 1283
@0BB 085
@0BC 82D
@0BD 0A8
@0BE 1A9
@0BF 828
@0C0 086
@0C1 82C
@0C2 0A8
@0C3 1A9
@0C4 808
@0C5 0AA
@0C6 1AB
@0C7 828
@0C8 62A
@0C9 1D03
@0CA 28CF
@0CB 829
@0CC 62B
@0CD 1903
@0CE 28D0
@0CF 2120
@0D0 3040
@0D1 7AC
@0D2 1803
@0D3 AAD
@0D4 3000
@0D5 22D
@0D6 1D03
@0D7 28DA
@0D8 30FF
@0D9 22C
@0DA 1C03
@0DB 28B9
@0DC 2120
@0DD 008
@0DE 3A00
@0DF 1903
@0E0 28E6
@0E1 3A80
@0E2 1903
@0E3 28F0
@0E4 19B
@0E5 3400
@0E6 81A
@0E7 084
@0E8 1383
@0E9 181B
@0EA 1783
@0EB 800
@0EC 09B
@0ED A84
@0EE 800
@0EF 008
@0F0 000
@0F1 000
@0F2 210D
@0F3 099
@0F4 F9A
@0F5 39B
@0F6 A9B
@0F7 000
@0F8 000
@0F9 210D
@0FA 098
@0FB 819
@0FC 09B
@0FD 818
@0FE 008
@0FF 3A00
@100 1903
@101 2906
@102 3A80
@103 1903
@104 290D
@105 3400
@106 81A
@107 084
@108 1383
@109 181B
@10A 1783
@10B 800
@10C 008
@10D 81B
@10E 08A
@10F 81A
@110 082
@111 008
@112 3402
@113 3400
@114 3423
@115 3401
@116 342C
@117 3400
@118 3402
@119 3400
@11A 3425
@11B 3401
@11C 342E
@11D 3400
@11E 3401
@11F 3400
@120 000
@121 2920
@122 008
@123 3400
@124 3400
@125 3400
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值