The STM8 is an 8-bitmicrocontroller family by STMicroelectronics. The STM8 microcontrollers use an extended variant of the ST7 microcontroller architecture. STM8 microcontrollers are particularly low cost for a full-featured 8-bit microcontroller.
Architecture
The STM8 is very similar to the earlier ST7, but is better suited as a target for C due to its 16-bit index registers and stack pointer-relative addressing mode. Although internally a Harvard architecture it has a unified 24-bit address space, allowing code to execute out of RAM, and data to be accessed out of ROM. Access to data above 64K is limited to special "load far" instructions; most operations' memory operands can access at most 128K. Very few STM8 microcontrollers have more than 64K of data memory. It has the same six registers as the ST7, but the index registers X and Y have been expanded to 16 bits, and the program counter has been expanded to 24 bits. The accumulator A and the stack pointer remain 8 and 16 bits, respectively. The condition code register has two more defined bits, for a total of seven. There is an overflow flag, and a second interrupt enable bit, allowing four interrupt priority levels.
The STM8 instruction set is mostly a superset of the ST7's, but it is not completely binary compatible. Operations on the X and Y registers are extended to 16 bits. Thus, loads and stores access two bytes of memory rather than one. Interrupts push nine bytes of state instead of five on the ST7. The multiply instruction stores the 16-bit product in the specified index register, rather than dividing it between X and A. Indirect addressing modes which fetch an 8-bit address from memory have been deleted; all indirect addressing modes fetch 16-bit addresses. A new prefix byte 72 has been added, and used to encode indirect starting with a 16-bit address. The bit manipulation instructions have been changed to take a 16-bit address and to require the 72 prefix byte. The unprefixed opcodes 0x and 1x they formerly occupied are instead used for stack-pointer relative addressing. Some rarely used branch instructions have had their opcodes changed to require a 90 prefix, and the unprefixed opcodes reassigned to signed branches which depend on the V flag. Load and compare instructions targeting the X register are of little use on the ST7 with addressing modes indexed by the X register. On the STM8, when such operations specify a memory operand indexed by the X register, the register operand is changed to Y. With a 90 prefix, the registers are reversed so the index register is Y and the operand register is X. One major performance difference is that the STM8 fetches 32 bits from ROM per cycle, and typical instructions take one cycle to execute plus one cycle per RAM access. The ST7, in contrast, fetches 8 bits per cycle and takes one cycle per instruction byte.
Instruction set
STM8 instructions consist of an optional prefix byte, an opcode byte, and a few bytes of operands. Prefix bytes mostly modify the addressing mode used to specify the memory operand, but in some cases, prefixes 72 and 90 perform a different operation entirely. Prefix 90 exchanges X and Y in the following instruction. In the table below, these variants are combined on one line by writing "X/Y", which means either "X" or "Y". Prefix 90 is also used in two places to introduce new opcodes: the BCPL and BCCM instructions, and some branch conditions. Prefix 92 converts instructions with an offset operand to indirect addressing. The offset is replaced by the 8-bit address of a 16-bit offset value in memory. It is used only for this function. Prefix 91 has both of the preceding effects, converting addressing modes to. Prefix 72 is used in a number of places, in a much less regular pattern. In some cases, it introduces new addressing modes, but it also introduces many completely new operations. For CPW and LDW instructions where the operand addressing mode is indexed by X, the STM8 uses the Y register by default instead of X. Applying a 90 prefix exchanges X and Y so the register is X and the addressing mode is indexed by Y.