Motorola S-record
Motorola S-record is a file format, created by Motorola in the mid-1970s, that conveys binary information as hex values in ASCII text form. This file format may also be known as SRECORD, SREC, S19, S28, S37. It is commonly used for programming flash memory in microcontrollers, EPROMs, EEPROMs, and other types of programmable logic devices. In a typical application, a compiler or assembler converts a program's source code to machine code and outputs it into a HEX file. The HEX file is then imported by a programmer to write the machine code into non-volatile memory, or is transferred to the target system for loading and execution.
Overview
History
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. The basic S-record format was subsequently extended to handle 24- and 32-bit addresses for compatibility with the MC68000 series of microprocessors.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. TekHex is another hex format that can include a symbol table for debugging.Format
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 which stands for "Start-of-Record".
- Record type - single numeric digit "0" to "9" character, defining the type of record. See table below.
- Byte count - two hex digits, indicating the number of bytes that follow in the rest of the record. This field has a minimum value of 3, and a maximum value of 255. "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 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. In the C programming language, the sum is converted into the checksum by:
0xFF -Text line terminators
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 character to terminate lines, whereas Windows programs use a CR character followed by a LF character.
Record types
The following table describes the 10 possible S-record types.| Record field | Record purpose | Address field | Data field | Record description |
| S0 | Header | The data field in this record type may be empty, or may contain the hex equivalent of application-specific ASCII information, often in the form of a null-terminated string. In lieu of more specific information, it is common to see 48, 44, 52, which is the ASCII representation of the letters "HDR". The address field will always contain the hex representation of $0000. | ||
| S1 | Data | 16-bit address | The data field in this record type contains data that is loaded to the 16-bit address in the address field. The number of bytes of data contained in this record is "Byte Count Field" minus 3. This record type is typically used with eight-bit processors, such as the 6502 and 6800 families, the 8051, the Z80, the AVR and PIC. | |
| S2 | Data | 24-bit address | The data field in this record type contains data that is loaded to the 24-bit address in the address field. The number of bytes of data contained in this record is "Byte Count Field" minus 4. The S2 record type was added by Motorola to support the MC68000 microprocessor, which has a 24-bit address bus. This record type is also usable with the Western Design Center 65C816, which emits 24-bit addresses. | |
| S3 | Data | 32-bit address | The data field in this record type contains data that is loaded to the 32-bit address in the address field. The number of bytes of data contained in this record is "Byte Count Field" minus 5. The S3 record type was added to support the 68012 and later 68000-series microprocessors that are equipped with a 32-bit data bus. This record type is also usable with non-Motorola 32-bit processors, such as the ARM architecture family and the RISC-V. | |
| S4 | Undefined | This record type is not defined by the official S-record standard. | ||
| S5 | Count | 16-bit count | This optional record type contains a 16-bit count of S1/S2/S3 records that have been transmitted. The data field is not used. | |
| S6 | Non-standard | 24-bit count | This record type is not defined by the official S-record standard. However, some applications treat this record as an optional 24-bit count of S1/S2/S3 records that have been transmitted. In such an application, the data field is not used. | |
| S7 | Start address | 32-bit address | This record type contains an optional 32-bit address that is the entry point for program execution. An address of $00000000 is used if no specific address is required, such as when S-records are used to burn EPROMs and the like. This record type is used to terminate a series of S3 records. The data field is not used. | |
| S8 | Start address | 24-bit address | This record type contains an optional 24-bit address that is the entry point for program execution. An address of $000000 is used if no specific address is required. This record type is used to terminate a series of S2 records. The data field is not used. | |
| S9 | Start address | 16-bit address | This record type contains an optional 16-bit address that is the entry point for program execution. An address of $0000 is used if no specific address is required. This record type is used to terminate a series of S1 records. The data field is not used. |
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 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
- S5
- S9
- S0
- S2
- S5
- S8
- S0
- S3
- S5
- 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. A record with an 8-hex-character address and 64 data characters would be 78 characters long, 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, it would be 2 for Record Type field + 2 for Byte Count field + for Address / Data / Checksum fields. Additional buffer space may be required to hold up to two control characters, and/or a NUL 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 in this field.
If the 32 byte historical limit is ignored, then the maximum amount of data varies depending on the size of the address field. The maximum number of bytes of data is calculated by 255 minus minus, thus the maximum amount of data for each record type is: 252 data bytes for S0 & S1 records, 251 data bytes for S2 records, 250 data bytes for S3 records.