SREC (file format)

Last updated

S-record
Motorola SREC Chart.png
A quick reference chart for the Motorola SREC format. (Note that in the record example image the word "bytes" is alternatively used to specify characters.)
Filename extension
.s19, .s28, .s37, .s, .s1, .s2, .s3, .sx, .srec, .exo, [1] .mot, .mxt
Developed byMotorola

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 (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 non-volatile memory, or is transferred to the target system for loading and execution.

Contents

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.

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

STypeByte CountAddressDataChecksum

An SREC format file consists of a series of ASCII text records. The records have the following structure from left to right:

  1. Record start - each record begins with an uppercase letter "S" character (ASCII 0x53) which stands for "Start-of-Record". [2]
  2. Record type - single numeric digit "0" to "9" character (ASCII 0x30 to 0x39), defining the type of record. See table below.
  3. 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.
  4. Address - four / six / eight hex digits as determined by the record type. The address bytes are arranged in big-endian format.
  5. 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.
  6. 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 10 possible S-records. S4 is reserved and not currently defined. S6 was originally reserved but was later redefined.

Record
field
Record
purpose
Address
field
Data
field
Record
description
S0Header16-bit
"0000"
Dark Red x.svgThis record contains vendor specific ASCII text comment 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, sign on. [3] It is common to see: 48, 44, 52 which is the ASCII representation of the letters "H", "D", "R". [4]
S1Data16-bit
Address
Yes check.svgThis record contains data that starts at a 16-bit address. [4] [3] The number of bytes of data contained in this record is "Byte Count Field" minus 3 (2 bytes for "16-bit Address Field" plus 1 byte for "Checksum Field"). This record is typically used for 8-bit processors, such as 6502, 6800, 8051, Z80, AVR, PIC.
S2Data24-bit
Address
Yes check.svgThis record contains data that starts at a 24-bit address. [4] The number of bytes of data contained in this record is "Byte Count Field" minus 4 (3 bytes for "24-bit Address Field" plus 1 byte for "Checksum Field").
S3Data32-bit
Address
Yes check.svgThis record contains data that starts at a 32-bit address. [4] The number of bytes of data contained in this record is "Byte Count Field" minus 5 (4 bytes for "32-bit Address Field" plus 1 byte for "Checksum Field"). This record is typically used for 32-bit processors, such as 68000, ARM, RISC-V.
S4ReservedThis record is reserved.
S5Count16-bit
Count
Dark Red x.svgThis optional record contains a 16-bit count of S1/S2/S3 records. [4] This record is used if the record count is less than or equal to 65,535 (0xFFFF), otherwise S6 record would be used.
S6Count24-bit
Count
Dark Red x.svgThis 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 (0x10000), then S5 record would be used. NOTE: This newer record is the most recent change (it may not be official). [4]
S7Start Address
(Termination)
32-bit
Address
Dark Red x.svgThis record contains the starting execution location at a 32-bit address. [4] [5] 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.
S8Start Address
(Termination)
24-bit
Address
Dark Red x.svgThis record contains the starting execution location at a 24-bit address. [4] [5] 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.
S9Start Address
(Termination)
16-bit
Address
Dark Red x.svgThis record contains the starting execution location at a 16-bit address. [4] [5] This is used to terminate a series of S1 records. [3] 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.

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", [4] 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
  1. S0
  2. S1 (one or more records)
  3. S5 (optional record)
  4. S9
S28-style 24-bit address records
  1. S0
  2. S2 (one or more records)
  3. S5 (optional record)
  4. S8
S37-style 32-bit address records
  1. S0
  2. S3 (one or more records)
  3. S5 (optional record)
  4. 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). [4] 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". [4]

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." [6]

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 [4] (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. [7]

Examples

Color legend

  Record type  Byte count  Address  Data  Checksum

Checksum calculation

The following example record:

S1137AF00A0A0D0000000000000000000000000061

is decoded to show how the checksum value is calculated. The following example uses a dollar sign ($) to indicate a hexadecimal value (a Motorola convention):

  1. Add: Add each byte $13 + $7A + $F0 + $0A + $0A + $0D + $00 + ... + $00 = $019E sum.
  2. Mask: Discard the most significant byte ($01) of the sum and retain the least significant byte (LSB), which is $9E.
  3. Complement: Compute the ones' complement of the LSB, which is $61.

In the C programming language, the sum is converted into the checksum by: 0xFF - (sum & 0xFF)

16-bit memory address

S00F000068656C6C6F202020202000003CS11F00007C0802A6900100049421FFF07C6C1B787C8C23783C6000003863000026S11F001C4BFFFFE5398000007D83637880010014382100107C0803A64E800020E9S111003848656C6C6F20776F726C642E0A0042S5030003F9S9030000FC

See also

Related Research Articles

In mathematics and computing, the hexadecimal numeral system is a positional numeral system that represents numbers using a radix (base) of sixteen. Unlike the decimal system representing numbers using ten symbols, hexadecimal uses sixteen distinct symbols, most often the symbols "0"–"9" to represent values 0 to 9, and "A"–"F" to represent values from ten to fifteen.

<span class="mw-page-title-main">Nibble</span> Four-bit unit of digital information

In computing, a nibble (occasionally nybble, nyble, or nybl to match the spelling of byte) is a four-bit aggregation, or half an octet. It is also known as half-byte or tetrade. In a networking or telecommunication context, the nibble is often called a semi-octet, quadbit, or quartet. A nibble has sixteen (24) possible values. A nibble can be represented by a single hexadecimal digit (0F) and called a hex digit.

UTF-8 is a variable-length character encoding standard used for electronic communication. Defined by the Unicode Standard, the name is derived from Unicode Transformation Format – 8-bit.

<span class="mw-page-title-main">UTF-16</span> Variable-width encoding of Unicode, using one or two 16-bit code units

UTF-16 (16-bit Unicode Transformation Format) is a character encoding capable of encoding all 1,112,064 valid code points of Unicode (in fact this number of code points is dictated by the design of UTF-16). The encoding is variable-length, as code points are encoded with one or two 16-bit code units. UTF-16 arose from an earlier obsolete fixed-width 16-bit encoding, now known as UCS-2 (for 2-byte Universal Character Set), once it became clear that more than 216 (65,536) code points were needed.

In computing, endianness is the order or sequence of bytes of a word of digital data in computer memory or data communication which is identified by describing the impact of the "first" bytes, meaning at the smallest address or sent first. Endianness is primarily expressed as big-endian (BE) or little-endian (LE). A big-endian system stores the most significant byte of a word at the smallest memory address and the least significant byte at the largest. A little-endian system, in contrast, stores the least-significant byte at the smallest address. Bi-endianness is a feature supported by numerous computer architectures that feature switchable endianness in data fetches and stores or for instruction fetches. Other orderings are generically called middle-endian or mixed-endian.

<span class="mw-page-title-main">KIM-1</span> Single-board computer produced by MOS Technology

The KIM-1, short for Keyboard Input Monitor, is a small 6502-based single-board computer developed and produced by MOS Technology, Inc. and launched in 1976. It was very successful in that period, due to its low price and easy-access expandability.

In computing, tar is a computer software utility for collecting many files into one archive file, often referred to as a tarball, for distribution or backup purposes. The name is derived from "tape archive", as it was originally developed to write data to sequential I/O devices with no file system of their own. The archive data sets created by tar contain various file system parameters, such as name, timestamps, ownership, file-access permissions, and directory organization. POSIX abandoned tar in favor of pax, yet tar sees continued widespread use.

In computer programming, Base64 is a group of tetrasexagesimal binary-to-text encoding schemes that represent binary data in sequences of 24 bits that can be represented by four 6-bit Base64 digits.

In computer programming, a magic number is any of the following:

Modbus or MODBUS is a client/server data communications protocol in the application layer of the OSI model. It was originally published by Modicon in 1979 for use with its programmable logic controllers (PLCs). Modbus has become a de facto standard communication protocol for communication between industrial electronic devices in a wide range of buses and network.

BinHex, originally short for "binary-to-hexadecimal", is a binary-to-text encoding system that was used on the classic Mac OS for sending binary files through e-mail. Originally a hexadecimal encoding, subsequent versions of BinHex are more similar to uuencode, but combined both "forks" of the Mac file system together along with extended file information. BinHexed files take up more space than the original files, but will not be corrupted by non-"8-bit clean" software.

A hex editor is a computer program that allows for manipulation of the fundamental binary data that constitutes a computer file. The name 'hex' comes from 'hexadecimal', a standard numerical format for representing binary data. A typical computer file occupies multiple areas on the storage medium, whose contents are combined to form the file. Hex editors that are designed to parse and edit sector data from the physical segments of floppy or hard disks are sometimes called sector editors or disk editors.

Netpbm is an open-source package of graphics programs and a programming library. It is used mainly in the Unix world, where one can find it included in all major open-source operating system distributions, but also works on Microsoft Windows, macOS, and other operating systems.

Intel hexadecimal object file format, Intel hex format or Intellec Hex is a file format that conveys binary information in ASCII text form, making it possible to store on non-binary media such as paper tape, punch cards, etc., to display on text terminals or be printed on line-oriented printers. The format is commonly used for programming microcontrollers, EPROMs, and other types of programmable logic devices and hardware emulators. In a typical application, a compiler or assembler converts a program's source code to machine code and outputs it into a HEX file. Some also use it as a container format holding packets of stream data. Common file extensions used for the resulting files are .HEX or .H86. The HEX file is then read by a programmer to write the machine code into a PROM or is transferred to the target system for loading and execution.

<span class="mw-page-title-main">Decimal computer</span> Computer operating on base-10 numbers

Decimal computers are computers which can represent numbers and addresses in decimal as well as providing instructions to operate on those numbers and addresses directly in decimal, without conversion to a pure binary representation. Some also had a variable wordlength, which enabled operations on numbers with a large number of digits.

Tektronix hex format and Extended Tektronix hex format / Extended Tektronix Object Format are ASCII-based hexadecimal file formats, created by Tektronix, for conveying binary information for applications like programming microcontrollers, EPROMs, and other kinds of chips.

<span class="mw-page-title-main">ZX Spectrum character set</span>

The ZX Spectrum character set is the variant of ASCII used in the ZX Spectrum family computers. It is based on ASCII-1967 but the characters ^, ` and DEL are replaced with ↑, £ and ©. It also differs in its use of the C0 control codes other than the common BS and CR, and it makes use of the 128 high-bit characters beyond the ASCII range. The ZX Spectrum's main set of printable characters and system font are also used by the Jupiter Ace computer.

Escape sequences are used in the programming languages C and C++, and their design was copied in many other languages such as Java, PHP, C#, etc. An escape sequence is a sequence of characters that does not represent itself when used inside a character or string literal, but is translated into another character or a sequence of characters that may be difficult or impossible to represent directly.

The FAT file system is a file system used on MS-DOS and Windows 9x family of operating systems. It continues to be used on mobile devices and embedded systems, and thus is a well suited file system for data exchange between computers and devices of almost any type and age from 1981 through the present.

The MOS Technology file format is a file format that conveys binary information in ASCII text form.

References

  1. "AR#476 PROMGen - Description of PROM/EEPROM file formats: MCS, EXO, HEX, and others". Xilinx. 2010-03-08. Motorola EXORmacs - File Format Code 87. Archived from the original on 2020-03-03. Retrieved 2020-03-03.
  2. Wiles, Mike; Felix, Andre (2000-10-21) [1975]. Holley, Michael (ed.). MCM6830L7 MIKBUG / MINIBUG ROM (PDF) (Engineering note). Motorola Semiconductor Products, Inc. Note 100. Archived from the original (PDF) on 2019-06-16. Retrieved 2019-06-16. (23 pages)
  3. 1 2 3 Hennig-Roleff, Werner (1993-02-01) [1988]. "HEX.DOC: Motorola - HEX Format". SIM51. 1.04 (in German). Archived from the original on 2017-08-11. Retrieved 2021-12-08. (NB. This is an older version of SIM51, the software and documentation was maintained up to 1996.)
  4. 1 2 3 4 5 6 7 8 9 10 11 12 13 "Motorola S-records (UNIX man page and comments)". Uisp AVR In-System Programmer. Archived from the original on 2002-07-03.
  5. 1 2 3 "Appendix C". M68000 Family Programmer's Reference Manual. Revision 1. Motorola. 1992. pp. C-1–C-5. ISBN   978-0-13723289-5.
  6. "srec_examples and srec_cat". SourceForge . Archived from the original on 2013-01-27.
  7. CCS Compiler Reference Manual PCB/PCM/PCH (PDF), Custom Computer Services, Inc., May 2014, p. 142, retrieved 2015-02-08

Further reading