SREC (file format)
A quick reference chart for the Motorola SREC format.
Motorola S-record is a file format, created by Motorola, that conveys binary information in ASCII hex text form. This file format may also be known as SRECORD, SREC, S19, S28, S37. It is commonly used for programming microcontrollers, EPROMs, and other types of programmable logic devices. In a typical application, a compiler or assembler converts a program's source code (such as C or assembly language) to machine code and outputs it into a HEX file. The HEX file is then imported by a programmer to "burn" the machine code into a ROM, or is transferred to the target system for loading and execution.
The S-record format was created in the mid-1970s for the Motorola 6800 processor. Software development tools for that and other embedded processors would make executable code and data in the S-record format. PROM programmers would then read the S-record format and "burn" the data into the PROMs or EPROMs used in the embedded system.
Other hex formats
There are other ASCII encoding with a similar purpose. BPNF, BHLF, and B10F were early binary formats, but they are neither compact nor flexible. Hexadecimal formats are more compact because they represent 4 bits rather than 1 bit per character. Many, such as S-record, are more flexible because they include address information so they can specify just a portion of a PROM. Intel HEX format was often used with Intel processors. Tek Hex is another hex format that can include a symbol table for debugging.
An SREC format file consists of a series of ASCII text records. The records have the following structure from left to right:
- Record type, two characters, an uppercase "S" (0x53) then a numeric digit 0 to 9, defining the type of record.
- Byte count, two hex digits, indicating the number of bytes (hex digit pairs) that follow in the rest of the record (address + data + checksum). This field has a minimum value of 3 for 16-bit address field plus 1 checksum byte, and a maximum value of 255 (0xFF).
- Address, four / six / eight hex digits as determined by the record type. The address bytes are arranged in big endian format.
- Data, a sequence of 2n hex digits, for n bytes of the data. For S1/S2/S3 records, a maximum of 32 bytes per record is typical since it will fit on an 80 character wide terminal screen, though 16 bytes would be easier to visually decode each byte at a specific address.
- Checksum, two hex digits, the least significant byte of ones' complement of the sum of the values represented by the two hex digit pairs for the byte count, address and data fields. See example section for a detailed checksum example.
Text line terminators
SREC records are separated by one or more ASCII line termination characters so that each record appears alone on a text line. This enhances legibility by visually delimiting the records and it also provides padding between records that can be used to improve machine parsing efficiency.
Programs that create HEX records typically use line termination characters that conform to the conventions of their operating systems. For example, Linux programs use a single LF (line feed, hex value
0A) character to terminate lines, whereas Windows programs use a CR (carriage return, hex value
0D) followed by a LF.
The following table describes 10 possible S-records. S4 is reserved and not currently defined. S6 was originally reserved but was later redefined at some point.
|This record contains vendor specific ASCII text represented as a series of hex digit pairs. It is common to see the data for this record in the format of a null-terminated string. The text data can be anything including a mixture of the following information: file/module name, version/revision number, date/time, product name, vendor name, memory designator on PCB, copyright notice.|
|This record contains data that starts at the 16-bit address field. This record is typically used for 8-bit microcontrollers, such as AVR, PIC, 8051, 68xx, 6502, 80xx, Z80. The number of bytes of data contained in this record is "Byte Count Field" minus 3, which is 2 bytes for "16-bit Address Field" and 1 byte for "Checksum Field".|
|This record contains data that starts at a 24-bit address. The number of bytes of data contained in this record is "Byte Count Field" minus 4, which is 3 bytes for "24-bit Address Field" and 1 byte for "Checksum Field".|
|This record contains data that starts at a 32-bit address. This record is typically used for 32-bit microcontrollers, such as ARM and 680x0. The number of bytes of data contained in this record is "Byte Count Field" minus 5, which is 4 bytes for "32-bit Address Field" and 1 byte for "Checksum Field".|
|S4||Reserved||N/A||N/A||This record is reserved.|
|This optional record contains a 16-bit count of S1 / S2 / S3 records. This record is used if the record count is less than or equal to 65,535 (0xFFFF), otherwise S6 record would be used.|
|This optional record contains a 24-bit count of S1 / S2 / S3 records. This record is used if the record count is less than or equal to 16,777,215 (0xFFFFFF). If less than 65,536 (0x010000), then S5 record would be used. NOTE: This newer record is the most recent change (not sure if official).|
|This record contains the starting execution location at a 32-bit address. This is used to terminate a series of S3 records. If a SREC file is only used to program a memory device and the execution location is ignored, then an address of zero could be used.|
|This record contains the starting execution location at a 24-bit address. This is used to terminate a series of S2 records. If a SREC file is only used to program a memory device and the execution location is ignored, then an address of zero could be used.|
|This record contains the starting execution location at a 16-bit address. This is used to terminate a series of S1 records. If a SREC file is only used to program a memory device and the execution location is ignored, then an address of zero could be used.|
Quoted from old Unix documentation "the order of S-records within a file is of no significance and no particular order may be assumed", though in practice most software has ordered the SREC records. The typical record order starts with a S0 record, followed by one or more S1/S2/S3 data records, then one optional S5/S6 count record, and ending with one S7/S8/S9 record.
- S19-style 16-bit address records
- S1 (one or more records)
- S5 (optional record)
- S28-style 24-bit address records
- S2 (one or more records)
- S5 (optional record)
- S37-style 32-bit address records
- S3 (one or more records)
- S5 (optional record)
Record length - Quoted from old Unix documentation "an S-record will be less than or equal to 78 characters in length". This is the only place that a 78-character limit on total record length is documented, though in modern practice this should be ignored. Modern software functions should allocate a buffer size that is large enough to parse a maximum-length S-record. The maximum length of an S-record is 514 characters (2 for Record Type field + 2 for Byte Count field + 2*254 for Address and Data fields + 2 for Checksum field). Additional buffer space should be allocated as required for the line terminator(s), and a possible NUL string terminator (not part of an SREC file).
Data field - Though older documentation recommends a maximum of 32 bytes of data (64 hex characters), it should be ignored for modern parsing functions. The minimum amount of data for S0/S1/S2/S3 records is zero. The maximum amount of data varies depending on the size of the address field. Since the Byte Count field can't be higher than 255 (0xFF), then the maximum number of bytes of data is calculated by 255 minus (1 byte for checksum field) minus (number of bytes in the address field). S0/S1 records support up to 252 bytes of data. S2 record supports up to 251 bytes of data. S3 record supports up to 250 bytes of data.
Comments - The SREC file format doesn't officially support comments. Unofficially, software that ignores all text lines that doesn't start with "S" and ignores all text after the checksum field could treat extra text as comments. The CCS PIC compiler supports placing a ";" comment line at the top or bottom of an Intel HEX file, and its manuals stats "some programmers (MPLAB in particular) do not like comments at the top of the hex file", which is why the compiler has the option of placing the comment at the bottom of the hex file.
Software - Some poorly designed software have limitations reading various types of SREC files, such as: (1) not able to handle S1/S2/S3 data records with more than 16 or 32 data bytes, (2) not able to handle every SREC record type, (3) not able to handle missing SREC record types.
- Color legend
Record type Byte count Address Data Checksum
The following example record:
is decoded to show how the checksum value is calculated as follows:
- Add: add each byte 13 + 7A+F0 + 0A+0A+0D+00+00+00+00+00+00+00+00+00+00+00+00+00 = 19E (hex) total.
- Mask: keep the least significant byte of the total = 9E (hex).
- Complement: compute ones' complement of least significant byte = 61 (hex).
16-bit memory address
S00F000068656C6C6F202020202000003C S11F00007C0802A6900100049421FFF07C6C1B787C8C23783C6000003863000026 S11F001C4BFFFFE5398000007D83637880010014382100107C0803A64E800020E9 S111003848656C6C6F20776F726C642E0A0042 S5030003F9 S9030000FC
- Template:SREC HEX, wiki template to easily display an SREC Hex record in any Wikipedia article.
- Intel HEX, hex file format by Intel
- TekHex, hex file format by Tektronix
- Binary-to-text encoding, comparison of various encoding algorithms
- GNU Binutils objdump and objcopy programs can produce and display S-records.
- "Motorola S-records from Unix man pages.". Retrieved 2014-06-30.
- Motorola (1992, Appendix C)
- CCS Compiler Reference Manual; Custom Computer Services.
- srec_examples; sourceforge.
- M68000 Family Programmer's Reference Manual (rev. 1 ed.), Motorola (Freescale), 1991, ISBN 978-0137232895
- M68HC05EVM Evaluation Module User's Manual (4th ed.), Motorola (Freescale), 1990
- M146805EVM Evaluation Module Users Manual (1st ed.), Motorola (Freescale), 1983
- MCM6830L7 MIKBUG / MINIBUG ROM, Engineering Note 100, Motorola (Freescale), 1975