Record structure An SREC format file consists of a series of
ASCII text records. The records have the following structure from left to right: •
Record start - each record begins with an uppercase letter "S" character (ASCII 0x53) which stands for "Start-of-Record". •
Record type - single numeric digit "0" to "9" character (ASCII 0x30 to 0x39), defining the type of record. See table below. •
Byte count - two
hex digits ("00" to "FF"), 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 (2 for 16-bit address field plus 1 checksum byte), and a maximum value of 255 (0xFF). "00" / "01" / "02" are illegal values. •
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 2
n 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. In the
C programming language, the sum is converted into the checksum by: 0xFF - (sum & 0xFF)
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 character (
line feed, 0x0A as ASCII character value) character to terminate lines, whereas Windows programs use a CR character (
carriage return, 0x0D as ASCII character value) followed by a LF character.
Record types The following table describes the 10 possible S-record types.
Record order Although some Unix documentation states "the order of S-records within a file is of no significance and no particular order may be assumed", in practice most software has ordered the SREC records. The typical record order starts with a (sometimes optional) S0 header record, continues with a sequence of one or more S1/S2/S3 data records, may have one optional S5/S6 count record, and ends with one appropriate S7/S8/S9 termination record. ; S19-style 16-bit address records • S0 • S1 (one or more records) • S5 (optional record) • S9 ; S28-style 24-bit address records • S0 • S2 (one or more records) • S5 (optional record) • S8 ; S37-style 32-bit address records • S0 • S3 (one or more records) • S5 (optional record) • S7
Limitations Record length A
manual page from historic
Unix O/S documentation states: "An S-record file consists of a sequence of specially formatted
ASCII character strings. An S-record will be less than or equal to 78 bytes in length". The manual page further limits the number of characters in the Data field to 64 (or 32 data bytes). A record with an 8-hex-character address and 64 data characters would be 78 (2 + 2 + 8 + 64 + 2) characters long (this count ignores possible end-of-line or string termination characters), and fits on an 80-character wide
teleprinter. A note at the bottom of the manual page states, "This manual page is the only place that a 78-byte limit on total record length or 64-byte limit on data length is documented. These values shouldn't be trusted for the general case". If the 78 byte historical limit is ignored, the maximum length of an S-record would be 514 characters. Assuming a Byte Count of 0xFF (255), it would be 2 for Record Type field + 2 for Byte Count field + (2 * 255) for Address / Data / Checksum fields. Additional buffer space may be required to hold up to two control characters (
carriage return and/or
line feed), and/or a NUL (0x00) string terminator for C/C++ programming languages. Using long line lengths has problems: "The Motorola S-record format definition permits up to 255 bytes of payload, or lines of 514 characters, plus the line termination. All
EPROM programmers should have sufficiently large line buffers to cope with records this big. Few do."
Data field The minimum amount of data for S0/S1/S2/S3 records is zero. Some historical documentation recommends a maximum of 32 bytes of data (64 hex characters) in this field (maybe because 32 is the largest
power of 2 of data that would fit on an
80 column wide
teleprinter /
computer terminal /
punched card). If the 32 byte historical limit is ignored, then the maximum amount of data varies depending on the size of the address field (4 / 6 / 8). The maximum number of bytes of data is calculated by 255 (maximum for Byte Count field) minus (1 byte for Checksum field) minus (number of bytes in the Address field), thus the maximum amount of data for each record type is: 252 data bytes (504 hex characters) for S0 & S1 records, 251 data bytes (502 hex characters) for S2 records, 250 data bytes (500 hex characters) for S3 records.
Comments Other than ASCII-to-hex converted comments in S0 header records, the SREC file format doesn't officially support human-readable
ASCII comments, though some software ignores all lines that don't start with "S" and/or ignores all text after the Checksum field (thus trailing text is sometimes used (incompatibly) for comments). For example, the CCS PIC compiler supports placing a ";" comment line at the top or bottom of an
Intel HEX file, and its manuals states "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. ==Examples==