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 ([email protected])
//
//    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
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章