Freescale Semiconductor
Order this document by AN1218/D Rev. 2
AN1218
Freescale Semiconductor, Inc...
HC05 to HC08 Optimization
By Mark Glenewinkel CSIC Applications Austin, Texas
Introduction
Freescale's HC05 Family of microcontrollers contains the world's most popular 8-bit microcontroller units (MCUs). In keeping pace with technology and the changing needs of the customer, Freescale has designed the HC08 Family of MCUs. The HC08 Family CPU is a performance extension to the HC05 Family of low cost MCUs. This application note will describe the differences and advantages of the HC08 Family CPU: the CPU08. CPU08 is fully opcode and object code compatible with the HC05 CPU. Any HC05 code will execute directly on the HC08 without instruction set differences. As this application note will show, there are many improvements to the speed and capability in the CPU08. CPU08 is a faster processor. The basic execution speed of the CPU08 has been increased with advanced high performance CMOS technology. Execution cycles of most instructions have been improved with an advanced computer architecture. CPU08 has more programming capability. It has more addressing modes, better math support, and much improved data manipulation, accessing, and moving capabilities. Looping and branching instructions have also been optimized.
© Freescale Semiconductor, Inc., 2004. All rights reserved.
AN1218 Rev. 2
For More Information On This Product, Go to: www.freescale.com
Freescale Semiconductor, Inc. Application Note
This application note will help inform and educate the reader concerning the differences between the HC05 and HC08 CPUs. Detailed examples illustrating the added features found with the CPU08 are given to help optimize software design with the CPU08.
Scope of this Application Note
This note assumes the reader has a background in MCU software and hardware design and is also familiar with the HC05. It was written for the engineering manager and the design engineer. As a reference, the application note overviews the basic differences between the two CPUs so that one can fit the right CPU for a specific application. As a tutorial, the application note gives the designer the means to understand and utilize the HC08 enhancements. Software is given to illustrate and compare the performance of the CPUs.
Freescale Semiconductor, Inc...
HC08 Features
The following is a list of major features of the HC08 CPU (CPU08) that differentiate it from the HC05 CPU (CPU05). • • • • • • • • • Fully upward object code compatible with the MC6805, MC146805, and the MC68HC05 Family 64 KByte program/data memory space Enhanced HC05 programming model 8 MHz CPU bus frequency 16 addressing modes, 5 more than the HC05 Expandable internal bus definition for addressing range extension beyond 64 KBytes 16-bit index register with manipulation instructions 16-bit stack pointer with manipulation instructions Memory to memory data moves without using the accumulator
AN1218 Rev. 2 2 For More Information On This Product, Go to: www.freescale.com
Freescale Semiconductor, Inc.
Application Note CPU05/CPU08 Programmer's Model Comparison
• • • •
Fast 8-bit multiply and integer/fractional divide instructions Binary coded decimal (BCD) instruction enhancements Internal bus flexibility to accommodate CPU enhancing peripherals such as a DMA controller Fully static low voltage/low power design
CPU05/CPU08 Programmer's Model Comparison
Freescale Semiconductor, Inc...
The CPU05 and the CPU08 programmer's model differences are illustrated in Figure 1. The index register of the CPU08 has been extended to 16 bits, allowing the user to index or address a 64 KByte memory space without any offset. The upper byte of the index register is called the H index register. The concatenated 16-bit register is called the H:X register. Source code written for CPU05 will not affect the H register and it will remain in its reset state of $00. There are seven new instructions that allow the user to manipulate the H:X index register. These instructions are covered in detail later. The stack pointer (SP) has been extended from its 6-bit CPU05 version to a full 16-bit SP on the CPU08. SPH:SPL refers to the 16-bit stack pointer by naming the high byte, SPH, and the low byte, SPL. To maintain HC05 compatibility, the reset state is $00FF. New instructions and new addressing modes greatly increase the utility of the CPU08 stack pointer over the CPU05 stack pointer. Nine new CPU08 instructions allow the user to easily manipulate the SP and the stack. CPU08 also has relative addressing modes that allow the SP to be used as an index register to access temporary variables on the stack. These addressing modes and new instructions are discussed later in this application note.
H Index Register
Stack Pointer
AN1218 Rev. 2 For More Information On This Product, Go to: www.freescale.com 3
Freescale Semiconductor, Inc. Application Note
7 7
A
0 0
ACCUMULATOR
X 12 PC 12 0 0 0 0 0 7 1 1 SP CCR 0 0
INDEX REGISTER
PROGRAM COUNTER
STACK POINTER
Freescale Semiconductor, Inc...
H
I
N
Z
C
CONDITION CODE REGISTER
(a) CPU05
7 15 H 15 15 X
0 ACCUMULATOR (A) 0 INDEX REGISTER (H:X) 0 STACK POINTER (SP) 0 PROGRAM COUNTER (PC)
7 0 V11HINZC
CONDITION CODE REGISTER (CCR)
(b) CPU08
Figure 1. CPU05 and CPU08 Comparison
Program Counter Expanded
The CPU08 program counter (PC) has been expanded to 16 bits which allows the CPU08 to address 64 KBytes of memory. Not all HC05 devices have a 16-bit program counter. CPU08 has 16 addressing modes, 8 more than the HC05. Table 1 lists these addressing modes and the CPUs that use them. A brief discussion of these modes is given below.
New Addressing Modes, Comparison
AN1218 Rev. 2 4 For More Information On This Product, Go to: www.freescale.com
Freescale Semiconductor, Inc.
Application Note CPU05/CPU08 Programmer's Model Comparison
Table 1. Addressing Mode Comparison Table
Addressing Mode Inherent Immediate Direct Extended Indexed, no offset HC05 X X X X X X X X HC08 X X X X X X X X X X X X X
Freescale Semiconductor, Inc...
Indexed, 8-bit offset Indexed, 16-bit offset Relative Stack Pointer, 8-bit offset Stack Pointer, 16-bit offset Memory to memory (4 modes) Indexed w/post increment Indexed, 8-bit offset, w/post increment
HC05 and HC08 Addressing Modes
Inherent instructions such as reset stack pointer (RSP) and multiply (MUL) have no operand. Inherent instructions require no memory address and are one byte long. Immediate instructions contain a value that is used in an operation with the index register or accumulator. Immediate instructions require no memory address and are two bytes long. The operand is found in the byte immediately following the opcode. Direct instructions can access any of the first 256 memory addresses with only two bytes. The first byte contains the opcode followed by the low byte of the operand address. The CPU automatically uses $00 for the high byte of the operand address. Most direct instructions are two bytes long.
AN1218 Rev. 2 For More Information On This Product, Go to: www.freescale.com 5
Freescale Semiconductor, Inc. Application Note
Extended instructions can access any address in the memory map. Extended instructions are three bytes long and contain the opcode and the two-byte operand address. Indexed instructions with no offset are one-byte instructions that utilize the index register of the CPU. CPU08 also uses the H:X register containing the high byte of the address operand. Indexed, 8-bit offset instructions are two-byte instructions that utilize the index register of the CPU to access data at any location in memory. The 8-bit unsigned offset following the opcode is added to the 16-bit unsigned index register (H:X). The sum is the address used to access data. Indexed, 16-bit offset instructions are like the 8-bit offset instructions except that they are three bytes long and add a 16-bit unsigned number to the 16-bit index register (H:X). Relative addressing is only used for branch instructions. If the branching condition is true, the CPU finds the branch destination by adding the offset operand to the PC counter. The offset is a two's complement byte that gives a branching range of –128 to +127 bytes. This instruction is two bytes long. Stack pointer, 8-bit offset instructions operate like indexed, 8-bit offset instructions except that they add the offset to the 16-bit SP. This mode is available only on the CPU08. If interrupts are disabled, this addressing mode allows the SP to be used as a second index register. This instruction is three bytes long. Stack pointer, 16-bit offset instructions are only available on the CPU08. They are like the stack pointer, 8-bit offset instructions except that they add a 16-bit value to the SP. This instruction is four bytes long.
Freescale Semiconductor, Inc...
New HC08 Addressing Modes
AN1218 Rev. 2 6 For More Information On This Product, Go to: www.freescale.com
Freescale Semiconductor, Inc.
Application Note CPU05/CPU08 Programmer's Model Comparison
Memory to memory instructions utilize four different modes available only to the CPU08. 1. The move, immediate to direct, is a three-byte mode generally used to initialize RAM and register values in page 0 of the memory map. The operand in the second byte is immediately stored to the direct page location found in the third byte. 2. The move, direct to direct, is a three-byte instruction. The operand following the opcode is the direct page location that is stored to the second operand direct page location. 3. The move, indexed to direct, post increment, is a two-byte instruction. The operand addressed by the 16-bit index register (H:X) is stored to direct page location address by the byte following the opcode. The index register is then incremented. 4. The move, direct to indexed, post increment, is a two-byte instruction. The operand in the direct page location addressed by the byte following the opcode is stored in the location addressed by the 16-bit index register (H:X). The index register is then incremented. In the CPU08, four instructions address operands with the index register and then increment the index register afterwards. This is called indexed with post increment mode. These instructions include CBEQ indexed, CBEQ indexed with offset, MOV IX+Dir, and MOV DirIx+. Table 2 gives examples to illustrate these different addressing modes.
Freescale Semiconductor, Inc...
AN1218 Rev. 2
For More Information On This Product, Go to: www.freescale.com
7
Freescale Semiconductor, Inc. Application Note
Table 2. Addressing Mode Examples
Addressing Mode Inherent Immediate Direct Extended Indexed, no offset RSP LDA LDA LDA LDA LDA LDA BRA LDA LDA MOV MOV MOV MOV CBEQ CBEQ #$FF $50 $1000 ,X $50,X $0150,X $20 $50,SP $0150,SP #$30,$80 $80,$90 X+,$90 $80,X+ X+,LOOP $20,X+,LOOP Example
Freescale Semiconductor, Inc...
Indexed, 8-bit offset Indexed, 16-bit offset Relative Stack Pointer, 8-bit offset* Stack Pointer, 16-bit offset* Memory to memory ImmDir* DirDir* Ix+Dir* DirIx+* Indexed w/post increment* Indexed, 8-bit offset, w/post increment* * New CPU08 addressing modes
Condition Code Register with Overflow Bit V
A summary of the condition code register (CCR) is given below. Unless otherwise stated, all bits correspond to both CPUs. Overflow Bit V This bit is set when a two's-complement overflow has occurred as the result of an operation. The V bit has been added to the CPU08 condition code register to support two's-complement arithmetic. Half-Carry Bit H The half-carry bit is set when a carry has occurred between bits 3 and 4 of the accumulator because of the last ADD or ADC operation. This bit is required for BCD operations.
AN1218 Rev. 2 8 For More Information On This Product, Go to: www.freescale.com
Freescale Semiconductor, Inc.
Application Note Description of the Clock
Interrupt Mask Bit I All timer and external interrupts are disabled when this bit is set. Interrupts are enabled when the bit is cleared. This bit is automatically set after any CPU reset. Negative Bit N This bit is set after any arithmetic, logical, or data manipulation operation was negative. In other words, bit 7 of the result of the operation was a logical one.
Freescale Semiconductor, Inc...
Zero Bit Z The zero bit is set after any arithmetic, logical, or data manipulation operation was zero. Carry/Borrow Bit C The carry/borrow bit is set when a carry out of bit 7 of the accumulator occurred during the last arithmetic, logical, or data manipulation operation. The bit is also set or cleared during bit test and branch instructions and shifts and rotates.
Description of the Clock
In the CPU08, the CPU clock rate is twice that of the address/data bus rates. The internal CPU08 clock rate is 16 MHz for an 8 MHz HC08. To maintain a 50% duty cycle CPU clock, the oscillator clock, OSC CLK, must run twice the rate of the CPU clock. Therefore a 32 MHz OSC clock is needed to drive an 8 MHz HC08. The flagship member of the CPU08 family has a phase locked loop (PLL) synthesizer to generate the 32 MHz signal. It is derived from a suggested crystal frequency of 4.9152 MHz. Address/Data Rate = CPU Clock Rate OSC Clock Rate Z = 8 MHz
= 2Z = 16 MHz = 4Z = 32 MHz
AN1218 Rev. 2 For More Information On This Product, Go to: www.freescale.com 9
Freescale Semiconductor, Inc. Application Note Index Registers
CPU08 has the additional H index register which is the high byte extension to the X index register. Together, the two index registers formulate the concatenated 16-bit H:X index register. Five new instructions are introduced on the CPU08 to allow manipulation of the H:X index register. Source code written for the HC05 will not effect the H register and it will remain in its reset state of $00.
Freescale Semiconductor, Inc...
The TSX and the TXS instructions also utilize the H:X index register. These instructions are covered in more detail in the stack pointer section. The new CPU08 instructions that affect the index registers are listed below. Examples for these instructions are given in Appendix A — New CPU08 Indexing Instruction Examples. AIX Add Immediate to Index Register Operation: X ← (H:X) + (M) Description: AIX adds an immediate value to the 16-bit index register formed by the concatenation of the H and X registers. The immediate operand is an 8-bit two's complement signed offset. Prior to addition to H:X, the offset is sign extended to 16 bits. CLRH Clear Index High Operation: H ← $00 Description: The contents of H are replaced with zeros. CPHX Compare 16-bit Index Register Operation: (H:X) – (M:M+1) Description: CPHX compares the 16-bit index register H:X with the 16-bit value in memory and sets the condition code register accordingly.
Five New Indexing Instructions, Detail
AN1218 Rev. 2 10 For More Information On This Product, Go to: www.freescale.com
Freescale Semiconductor, Inc.
Application Note Index Registers
LDHX
Load 16-bit Index Register Operation: H:X ← (M:M+1) Description: Loads the contents of the speci ed memory location into the 16-bit index register H:X. The condition codes are set according to the data.
STHX
Store 16-bit Index Register Operation: (M:M+1) ← (H:X) Description: Stores the 16-bit index register H:X to the speci ed memor y location. The condition codes are set according to the data.
Freescale Semiconductor, Inc...
Software Techniques Using Indexed Addressing, Tables
The CPU08 index register has some distinct advantages over the CPU05 index register. Even though the CPU05 has 16-bit index offset, the 8-bit index register restricts indexing to a maximum of 256 bytes. CPU08 with its H register extension allows full 16-bit index addressing equaling 65,536 bytes of memory access. Proper 16-bit pointers allow efficient compiling of C code and other higher level languages. Maximum table lengths in the CPU08 which can be accessed in a single instruction are therefore 64 KByte. An optional address extension module can extend the data space beyond 64 KBytes, but the maximum offset remains 64 KBytes. Index addressing modes include 8- and 16bit offsets. Many programmers like to use calculated addressing. CPU08 has a new instruction, AIX, that allows the addition of a two's complement number. Table access is easier and more flexible. The H:X index register can also be used as an auxiliary 16-bit accumulator. Sixteen-bit data comparisons are easier with the CPHX instruction. The following section illustrates the advantage of using a 16-bit index register.
AN1218 Rev. 2 For More Information On This Product, Go to: www.freescale.com 11
Freescale Semiconductor, Inc. Application Note
Code Example We will now illustrate the added benefit of the CPU08 16-bit index register. The index will be used to address a 512 byte table. In the CPU05, the table must be broken up into sections of memory consisting of 256 bytes per section. Our table has 512 bytes, so we will be using two sections, section 0 and section 1, for the CPU05. The address to look up on the table will be found in RAM. Notice that the CPU05 code is longer. If your table was larger, you would require more sections of memory to handle your table. A subroutine might be written to make the job more modular. In the HC08 example, the 512 byte table can be handled directly. A comparison between CPU05 and CPU08 code is shown in Appendix B — CPU05 and CPU08 512-Byte Table Indexing Code.
Freescale Semiconductor, Inc...
Stack Pointer
CPU08 has a full 16-bit stack pointer. To maintain compatibility with the CPU05, it is initialized to $00FF out of reset. Stack manipulation is from high to low memory. The SP is decremented each time data is pushed on the stack and incremented each time data is pulled from the stack. The SP points to the next available stack address rather than the latest stack entry address. Nine new instructions have been added for the user to manipulate the stack. These instructions allow the direct push and pull of any register to the stack. The SP can be changed with a transfer of the H:X register to the SP or the SP can be augmented by the add immediate instruction. Stack manipulation can be a very powerful programming technique. With the CPU08, the assembly programmer can pass parameters and store local or temporary variables when using subroutines and/or interrupts. New addressing modes were added to address these variables on the stack. Using the stack pointer as an index register with 8- or 16-bit offsets, the user may access variables on the stack. These instructions greatly cut cycle count by not having to load/store the variable. RAM
AN1218 Rev. 2 12 For More Information On This Product, Go to: www.freescale.com
Freescale Semiconductor, Inc.
Application Note Stack Pointer
requirements are also reduced. Significant C code efficiency can be gained when utilizing these new stack pointer addressing modes. If interrupts are disabled, the stack pointer can be used as a second 16-bit index register with 8- or 16-bit offsets. All the new CPU08 instructions that affect the stack pointer are listed below. Examples for these instructions are given in Appendix C — New CPU08 Stack Pointer Instructions. AIS Add Immediate to Stack Pointer Operation: SP ← (SP) + (M) Description: Adds the immediate operand to the stack pointer SP. The immediate value is an 8-bit two's complement signed operand. Prior to addition to the SP, the operand is sign extended to 16 bits. This instruction can be used to create and remove a stack frame buffer which is used to store temporary variables. PSHA Push Accumulator onto Stack Operation: ⇓ (A); SP ← (SP-$01) Description: The contents of the accumulator are pushed onto the stack at the address contained in the stack pointer. The stack pointer is then decremented to point at the next available location in the stack. The contents of the accumulator remain unchanged. PSHH Push Index Register H onto Stack Operation: ⇓ (H); SP ← (SP-$01) Description: The contents of the 8-bit high order index register H are pushed onto the stack at the address contained in the stack pointer. The stack pointer is then decremented to point at the next available location in the stack. The contents of the H register remain unchanged.
AN1218 Rev. 2 For More Information On This Product, Go to: www.freescale.com 13
Nine New Stack Manipulation Instructions, Detail
Freescale Semiconductor, Inc...
Freescale Semiconductor, Inc. Application Note
PSHX Push Index Register X onto Stack Operation: ⇓ (X); SP ← (SP-$01) Description: The contents of the 8-bit low order index register X are pushed onto the stack at the address contained in the stack pointer. The stack pointer is then decremented to point at the next available location in the stack. The contents of the X register remain unchanged.
Freescale Semiconductor, Inc...
PULA
Pull Accumulator from Stack Operation: SP ← (SP+$01); ⇑ (A) Description: The stack pointer is incremented to address the last operand on the stack. The accumulator is then loaded with the contents of the address pointed to by SP.
PULH
Pull Index Register H from Stack Operation: SP ← (SP+$01); ⇑ (H) Description: The stack pointer is incremented to address the last operand on the stack. The 8-bit index register H is then loaded with the contents of the address pointed to by SP.
PULX
Pull Index Register X from Stack Operation: SP ← (SP+$01); ⇑ (X) Description: The stack pointer is incremented to address the last operand on the stack. The 8-bit index register X is then loaded with the contents of the address pointed to by SP.
TSX
Transfer Stack Pointer to Index Register Operation: H:X ← (SP) + $0001 Description: Loads the index register H:X with one plus the contents of the 16-bit stack pointer SP. The contents of the stack pointer remain unchanged. After a TSX instruction, the
AN1218 Rev. 2
14
For More Information On This Product, Go to: www.freescale.com
Freescale Semiconductor, Inc.
Application Note Stack Pointer
index register H:X points to the last value that was stored on the stack. TXS Transfer Index Register to Stack Pointer Operation: SP ← (H:X) - $0001 Description: Loads the stack pointer SP with the contents of the index register H:X minus one. The contents of the index register H:X remain unchanged.
Freescale Semiconductor, Inc...
Software Techniques Using the SP
The CPU05 and the CPU08 use the stack for two primary purposes. First, every time the CPU executes an interrupt service routine, the register contents are saved on the stack. After the execution of a return from interrupt (RTI) instruction, the register contents on the stack are restored to the CPU. Second, every time a jump to subroutine (JSR) or a branch to subroutine (BSR) occurs, the return address is saved on the stack. The address is restored to the program counter after a return from subroutine (RTS) instruction is executed. The CPU08 with its new stack manipulation instructions allows the user to pass parameters to the subroutine and store local or temporary values within the subroutine. Two major benefits are derived from using the stack for parameters and temporary values: 1. A subroutine will allocate RAM storage for its variables and release this memory when the subroutine is finished. Therefore, global variables are not needed for these routines. This saves RAM memory space. 2. The allocation of new local variables for each subroutine makes the subroutine recursive and reentrant. This allows the programmer to easily modularize his code. Let's look at the stacking operation of the CPU05 and the CPU08. The stack is located in RAM. Since stacking occurs from high memory to low memory, the SP usually points to the highest RAM memory address. Both the CPU05 and the CPU08 reset the SP at $00FF. The CPU08 instruction set allows the programmer to move the stack out of Page 0 memory if needed.
AN1218 Rev. 2 For More Information On This Product, Go to: www.freescale.com 15
Freescale Semiconductor, Inc. Application Note
When an interrupt occurs, the contents of all the CPU registers are pushed onto the stack, the interrupt vector is fetched, and the program begins execution at the start of the interrupt routine. The stack contents before and after an interrupt are shown in Figure 2. For the CPU08 to remain upward compatible with the CPU05, the H index register is not pushed onto the stack.
NOTE:
If the H register is used in the interrupt service routine or if indexed addressing modes are used, the H register must be pushed onto the stack.
This is accomplished by using the PSHH instruction. Before returning from the interrupt, the PULH instruction must be used to extract the H index register off the stack.
STACK BEFORE AN INTERRUPT CALL
Freescale Semiconductor, Inc...
STACK POINTER →
?? ??
$00FE $00FF
STACK AFTER AN INTERRUPT CALL STACK POINTER → CONDITION CODE REGISTER ACCUMULATOR A INDEX REGISTER X PROGRAM COUNTER HIGH PROGRAM COUNTER LOW $00FA $00FB $00FC $00FD $00FE $00FF
Figure 2. Stack Before and After an Interrupt Call
AN1218 Rev. 2 16 For More Information On This Product, Go to: www.freescale.com
Freescale Semiconductor, Inc.
Application Note Stack Pointer
Figure 3 illustrates the stack before and after a subroutine is called when the stack pointer is at $00FF. When a subroutine is called, the 16bit program counter is pushed onto the stack and the execution of code begins at the start of the subroutine. The program counter is split into its 8-bit high and low bytes.
STACK BEFORE A SUBROUTINE CALL
Freescale Semiconductor, Inc...
?? STACK POINTER → ??
$00FE $00FF
STACK AFTER A SUBROUTINE CALL STACK POINTER → INDEX REGISTER X PROGRAM COUNTER HIGH PROGRAM COUNTER LOW $00FD $00FE $00FF
Figure 3. Stack Before and After a Subroutine Call
If the values in the X register and the accumulator are needed within a subroutine, they will need to be saved somehow before the subroutine uses them. If using the CPU05, you would have to allocate global RAM space for saving these CPU registers. Your code would look something like that in Figure 4.
AN1218 Rev. 2 For More Information On This Product, Go to: www.freescale.com 17
Freescale Semiconductor, Inc. Application Note
* START Subroutine XX for CPU05 STX STA XX XX LDX LDA $50 $51 XX XX $50 $51 *
;4 store X away to RAM ;4 store A away to RAM ;actual subroutine code
;3 load X from RAM ;3 load A from RAM
Figure 4. CPU05 Subroutine Code
Freescale Semiconductor, Inc...
The CPU05 code will use 14 cycles to store and load registers. Also, two bytes of global RAM space are allocated for this subroutine. If we were to use the CPU08, the code could utilize the stack. Global RAM space and six cycles would be saved. Refer to Figure 5.
* START Subroutine XX for CPU08 PSHX PSHH PSHA XX XX PULA PULH PULX XX XX *
;2 push X onto stack ;2 push H onto stack ;2 push A onto stack ;actual subroutine code
;2 pull A off of stack ;2 pull H off of stack ;2 pull X off of stack
Figure 5. CPU08 Subroutine Code The stack helps in efficiently utilizing parameters, local variables, and subroutine return values. Parameters are variables that are passed to the subroutine. Local variables are variables that are only used within the scope of the subroutine. A subroutine return value is the output of the subroutine. An example of a subroutine and its variables are given below in equation form: Y = (X)3 If we were to write a subroutine that calculates the cube of the value X, X would be the parameter passed to the subroutine. Y would be the subroutine return value, and any variable used to calculate Y would be
AN1218 Rev. 2 18 For More Information On This Product, Go to: www.freescale.com
Freescale Semiconductor, Inc.
Application Note Stack Pointer
a local variable. The stack of these complex subroutines follow the generalized structure shown in Figure 6. Figure 6 shows the stack before the subroutine initialization, before entering the subroutine, and during the subroutine. The actual cube subroutine is written in the following section of code. A diagram of the stack during its execution is given within the code listing.
STACK
Freescale Semiconductor, Inc...
SP DURING SUBROUTINE
→ LOCAL VARIABLE 1 LOCAL VARIABLE 2 ACCUMULATOR A INDEX REGISTER X PROGRAM COUNTER HIGH
$00f7 $00F8 $00F9 $00FA $00FB $00FC $00FD $00FE $00FF
SP DURING SUBROUTINE SP BEFORE SUBROUTINE INITIALIZATION
→ →
PROGRAM COUNTER LOW RETURN VALUE PASSED PARAMETER
Figure 6. Stack Structure of a Complex Subroutine Refer to Appendix D — Using the Stack in a Subroutine to Compute a Cube for an example of modular subroutine code that efficiently computes the cube of an 8-bit positive number.
Code Example
AN1218 Rev. 2 For More Information On This Product, Go to: www.freescale.com 19
Freescale Semiconductor, Inc. Application Note Data Movement
Why Improve the Movement of Data in the CPU05?
The most common CPU function is the transfer of data. Most microcontroller-based systems spend the majority of their time moving data from one location to the other. Many different addressing modes are used to access and transfer bytes of data. If there was a way to decrease the time it takes to transfer data, then the overall performance of the system would be improved. CPU05 moves data from one location to the next by first loading the accumulator with the byte from the transfer source. Next, CPU05 stores the byte from the accumulator to the transfer's destination. In this manner all data must pass through the accumulator, thus making the accumulator a bottleneck in data movement. The movement of the contents of location $40 to location $60 with the CPU05 is illustrated in Figure 7.
LDA $40 # $40 →→ # ACCA
Freescale Semiconductor, Inc...
STA $60
#
$60
←←
Figure 7. Accumulator as a Bottleneck CPU08 provides the new MOV instruction which bypasses the accumulator. Using the MOV instruction, the CPU is instructed to take the contents of the source location and directly place the data in the destination. This is illustrated in Figure 8. There are four different addressing modes special to the MOV instruction. Details of this instruction are given below. .
MOV $40,$60 # ↓ ↓ # $60 $40
Figure 8. No Accumulator Bottleneck
AN1218 Rev. 2 20 For More Information On This Product, Go to: www.freescale.com
Freescale Semiconductor, Inc.
Application Note Data Movement
New MOVE Instruction, Detail
The new CPU08 Move instruction is detailed below. Examples for this instruction and all four of its addressing modes are given in Appendix E — New CPU08 MOV Instruction Examples. The examples in Appendix E — New CPU08 MOV Instruction Examples also compare the CPU05 and the CPU08 bus cycles and memory requirements for the algorithm to execute the movement of data. MOV Move Operation: (M)destination ← (M)source Description: Moves a byte of data from a source address to a destination address. Data is examined as it is moved, and condition codes are set. Source data is not changed. Internal registers (other than CCR) are not affected. There are four addressing modes for the MOV instruction. A discussion of these modes was given in an earlier section. The MOV command will cut cycle time and code space when moving data. The most obvious advantage of the MOV instruction is when the configuration registers are being initialized along with other RAM variables at the start of the program. A user wants to start his application one of two different ways. The user initializes the application on the MCU based on the logic level of port D bit 4. Once the part is out of reset, it reads port D and moves data from ROM into the RAM configuration registers according to the logic level of bit 4. Refer to Appendix F — CPU05 and CPU08 Data Movement Code for code comparing the CPU05 and the CPU08.
Freescale Semiconductor, Inc...
Software Techniques
Code Example
AN1218 Rev. 2 For More Information On This Product, Go to: www.freescale.com 21
Freescale Semiconductor, Inc. Application Note New Branch Instructions
Description Six new unsigned branch instructions were added to the instruction set of the CPU08 to improve looping and table searching capabilities. These instructions are CBEQ, CBEQA, CBEQX, DBNZ, DBNZA, and DBNZX. The CBEQ instructions combine the compare (CMP and CPX) instructions and the branch if equal (BEQ) instruction . The DBNZ instructions combine the decrement (DEC, DECA, and DECX) instructions and the branch if not equal (BNE) instruction. These new instructions improve cycle time and decrease code space. More detail is given below on each instruction. All the new CPU08 instructions that affect branching are listed below. Examples for these instructions are given in Appendix G — New Branch Instruction Examples. The examples in Appendix G — New Branch Instruction Examples also compare the CPU05 and the CPU08 bus cycles and memory requirements for the algorithm to execute the branch. CBEQ Compare and Branch if Equal Operation: A) - (M); PC ← (PC) + $0003 + Rel if result is $00
Freescale Semiconductor, Inc...
Six New Branch Instructions, Detail
For IX+ mode: (A) - (M); PC ← (PC) + $0002 + Rel, if result is $00 Description: CBEQ compares the operand from memory with the accumulator and causes a branch if the result is zero. This function combines CMP and BEQ for faster table look-up routines. The addressing mode CBEQ_IX+ compares the operand addressed by the 16-bit index register H:X to the accumulator and causes a branch if the result is zero. The 16-bit index register is then incremented regardless of whether a branch is taken. CBEQ_IX1+ operates the same way except
AN1218 Rev. 2 22 For More Information On This Product, Go to: www.freescale.com
Freescale Semiconductor, Inc.
Application Note New Branch Instructions
an 8-bit offset is added to the effective address of the operand. CBEQA Compare and Branch if Equal Operation: (A) - (M); PC ← (PC) + $0003 + Rel if result is $00
Freescale Semiconductor, Inc...
Description: CBEQA compares an immediate operand in memory with the accumulator and causes a branch if the result is zero. This instruction combines CMP and BEQ for faster table look-up routines. CBEQX Compare and Branch if Equal Operation: (IX) - (M); PC ← (PC) + $0003 + Rel if result is $00
Description: CBEQX compares an immediate operand in memory with the lower order index register X and causes a branch if the result is zero. This instruction combines CPX and BEQ for faster loop counter control. DBNZ Decrement and Branch if Not Zero Operation: M ← (M) - $01; PC ← (PC) + $0003 + Rel, if result _ $00 for Direct, IX1, and SP1 PC ← (PC) + $0002 + Rel, if result _ $00 for IX Description: DBNZ subtracts one from the operand M in memory and causes a branch if the result is not zero. This instruction combines DEC and BNE for faster loop counter control. DBNZA Decrement and Branch if Not Zero Operation: A ← (A) - $01; PC ← (PC) + $0002 + Rel, if result _ $00
Description: DBNZA subtracts one from the accumulator and causes a branch if the result is not zero. This instruction combines DECA and BNE for faster loop counter control.
AN1218 Rev. 2 For More Information On This Product, Go to: www.freescale.com 23
Freescale Semiconductor, Inc. Application Note
DBNZX Decrement and Branch if Not Zero Operation: X ← (X) - $01; PC ← (PC) + $0002 + Rel, if result _ $00
Description: DBNZX subtracts one from the lower index register and causes a branch if the result is not zero. This instruction combines DECX and BNE for faster loop counter control. The use of these new instructions can cut cycle time in looping or counting routines. Compare and branch routines can be used to search for specific values in tables or variable locations. Decrement and branch routines can be used for keeping count in loops. The following piece of code shows how the compare and branch instruction searches a table for a match. As an example, let's say that you recently read in a table of 80 A/D data bytes. You would like to know if the signal was saturated above the rails of the A/D converter. You would then search the table for the value $FF. If found, your code would branch out and execute some control algorithm to attenuate the analog signal. Refer to Appendix H — CPU05 and CPU08 Search Code for a comparison of CPU05 and CPU08 code.
Freescale Semiconductor, Inc...
Code Example
Mathematical Operations
V Bit, DIV, DAA, and the NSA Instruction
New features and instructions added to the CPU08 have made some mathematical computations easier. The V bit is added to the CCR to support signed arithmetic. CPU08 has the capability of 16-bit division. The DIV instruction will divide a 16-bit dividend by an 8-bit divisor. For binary coded decimal operations, the CPU08 has a decimal adjust accumulator, DAA, instruction and a nibble swap accumulator, NSA, instruction. The V bit in the CCR adds greater programming flexibility to the user. The addition of two's complement comparisons can aid in the branching
AN1218 Rev. 2
Signed Math and Signed Branches
24
For More Information On This Product, Go to: www.freescale.com
Freescale Semiconductor, Inc.
Application Note Four New Signed Branch Instructions, Detail
operations of high level languages such as C. Also, the representation of signed numbers and their operations can easily be computed. This can be especially helpful with digital signal processing algorithms and the proper storage of signed analog to digital readings.
Four New Signed Branch Instructions, Detail
All the new CPU08 instructions that affect signed branching are listed below. Examples for these instructions are given in Appendix I — New CPU08 Signed Branch Instruction Examples. BGE Branch if Greater Than or Equal (signed operands) Operation: PC ← (PC) + $0002 + Rel, if (N⊕V)=0, i.e., if (A) _ (M), ("signed" numbers)
Freescale Semiconductor, Inc...
Description: If the BGE instruction is executed immediately after execution of any of the compare or subtract instructions, the branch will occur if and only if the two's complement number represented by the appropriate internal register (A, X, or H:X) was greater than or equal to the two's complement number represented by M. BGT Branch if Greater Than (signed operands) Operation: PC ← (PC) + $0002 + Rel, if Z+(N⊕V)=0, i.e., if (A) > (M), ("signed" numbers)
Description: If the BGT instruction is executed immediately after execution of any of the compare or subtract instructions, the branch will occur if and only if the two's complement number represented by the appropriate internal register (A, X, or H:X) was greater than the two's complement number represented by M.
AN1218 Rev. 2 For More Information On This Product, Go to: www.freescale.com 25
Freescale Semiconductor, Inc. Application Note
BLE Branch if Less Than or Equal (signed operands) Operation: PC ← (PC) + $0002 + Rel, if Z+(N⊕V)=1 i.e., if (A) _ (M), ("signed" numbers)
Freescale Semiconductor, Inc...
Description: If the BLE instruction is executed immediately after execution of any of the compare or subtract instructions, the branch will occur if and only if the two's complement number represented by the appropriate internal register (A, X, or H:X) was less than or equal to the two's complement number represented by M. BLT Branch if Less Than (signed operands) Operation: PC ← (PC) + $0002 + Rel, if (N⊕V)=1 i.e., if (A) < (M), ("signed" numbers)
Description: If the BLT instruction is executed immediately after execution of any of the compare or subtract instructions, the branch will occur if and only if the two's complement number represented by the appropriate internal register (A, X, or H:X) was less than the two's complement number represented by M. The Divide instruction on the CPU08 does not require the lengthy code needed to divide numbers on the CPU05. A description of the Divide instruction is given below. Appendix J — Five Miscellaneous CPU08 Instructions Including BCD, Divide, and CCR Operations shows a short example of using the new Divide instruction. Appendix K — CPU08 Averaging Code illustrates an averaging routine implementing the Divide instruction. DIV Divide Operation: (H:A) / X → A; Remainder → H Description: Divides a 16-bit unsigned dividend contained in the concatenated registers H and A by an 8-bit divisor contained in index
AN1218 Rev. 2 26 For More Information On This Product, Go to: www.freescale.com
New DIV Instruction
Freescale Semiconductor, Inc.
Application Note Four New Signed Branch Instructions, Detail
register X. The quotient is placed in the accumulator A, and the remainder is placed in the high order index register H. The divisor is left unchanged. The decimal adjust accumulator, DAA, and the nibble swap accumulator, NSA, are new instructions to help with binary coded decimal (BCD) operations. The DAA instruction allows the user to adjust the accumulator so that the number represents a BCD number. Swapping nibbles is needed for packing BCD numbers into memory. One use of BCD is data instrumentation. It is easier to store and manipulate these numbers in BCD rather than convert or decode numbers from hexadecimal. Packing is used to store decimal numbers into memory. Instead of one byte storing one decimal, the NSA instruction easily swaps nibbles in the accumulator so that two decimal numbers can be stored in one byte. Appendix J — Five Miscellaneous CPU08 Instructions Including BCD, Divide, and CCR Operations gives examples using the DAA instruction and the NSA instruction. Refer to Appendix L — CPU08 BCD Example Code for an example of BCD code. DAA Decimal Adjust Accumulator Operation: (A)10 Description: Adjusts the contents of the accumulator and the state of the CCR carry bit after binary coded decimal operations so that there is a correct BCD sum and an accurate carry indication. The state of the CCR half carry bit affects operation. NSA Nibble Swap Accumulator Operation: A ← (A[3:0]:A[7:4]) Description: Swaps upper and lower nibbles (4 bits) of the accumulator. This is used for more ef cient stor age and use of binary coded operands.
New DAA and the NSA instruction
Freescale Semiconductor, Inc...
AN1218 Rev. 2
For More Information On This Product, Go to: www.freescale.com
27
Freescale Semiconductor, Inc. Application Note
New TAP and TPA instructions The transfer accumulator to the condition code register, TAP, and the transfer condition code register to accumulator, TPA, are new instructions to modify or manipulate the condition code register, CCR. These instructions are detailed below. Code examples can be found in Appendix J — Five Miscellaneous CPU08 Instructions Including BCD, Divide, and CCR Operations. TAP Transfer Accumulator to Condition Code Register Operation: CCR ← (A) Description: Transfers the contents of the Accumulator to the Condition Code Register. TPA Transfer Condition Code Register to Accumulator Operation: A ← (CCR) Description: Transfers the contents of the Condition Code Register to the Accumulator.
Freescale Semiconductor, Inc...
Instruction Cycle Improvements
The CPU08 instruction set not only has new instructions but many of the old instructions are faster. The CPU08 gathers data in a pipeline fashion. Instead of waiting for the instruction to be finished to gather the next opcode or operand, the CPU will fetch the next address byte during the execution of the current instruction. This pipelining overlaps execution of most instructions and thus increases the performance of the CPU08. A list of instructions that were improved is given in Table 3. Please refer to the CPU08 opcode map for further details.
AN1218 Rev. 2 28 For More Information On This Product, Go to: www.freescale.com
Freescale Semiconductor, Inc.
Application Note Instruction Cycle Improvements
Table 3. Instruction List (Sheet 1 of 4)
Opcode Mnemonic
ADC ADC ADC ADD ADD ADD AND
Address Mode
IX IX1 IX2 IX IX1 IX2 IX IX1 IX2 DIR IX IX1 INH INH DIR DIR DIR DIR DIR DIR DIR DIR IX IX1 IX2 DIR DIR DIR DIR DIR DIR DIR DIR REL INH
HC05 Cycles
3 4 5 3 4 5 3 4 5 5 5 6 3 3 5 5 5 5 5 5 5 5 3 4 5 5 5 5 5 5 5 5 5 6 2
HC08 Cycles
2 3 4 2 3 4 2 3 4 4 3 4 1 1 4 4 4 4 4 4 4 4 2 3 4 4 4 4 4 4 4 4 4 4 1
Freescale Semiconductor, Inc...
AND AND ASR ASR ASR ASRA ASRX BCLR0 BCLR1 BCLR2 BCLR3 BCLR4 BCLR5 BCLR6 BCLR7 BIT BIT BIT BSET0 BSET1 BSET2 BSET3 BSET4 BSET5 BSET6 BSET7 BSR CLC
AN1218 Rev. 2 For More Information On This Product, Go to: www.freescale.com 29
Freescale Semiconductor, Inc. Application Note
Table 3. Instruction List (Sheet 2 of 4)
Opcode Mnemonic
CLR CLR CLR CLRA CLRX CMP CMP
Address Mode
DIR IX IX1 INH INH IX IX1 IX2 DIR IX IX1 INH INH IX IX1 IX2 DIR IX IX1 INH INH IX IX1 IX2 DIR IX IX1 INH INH DIR EXT IX IX1 IX2 IX
HC05 Cycles
5 5 6 3 3 3 4 5 5 5 6 3 3 3 4 5 5 5 6 3 3 3 4 5 5 5 6 3 3 5 6 5 6 7 3
HC08 Cycles
3 2 3 1 1 2 3 4 4 3 4 1 1 2 3 4 4 3 4 1 1 2 3 4 4 3 4 1 1 4 5 4 5 6 2
Freescale Semiconductor, Inc...
CMP COM COM COM COMA COMX CPX CPX CPX DEC DEC DEC DECA DECX EOR EOR EOR INC INC INC INCA INCX JSR JSR JSR JSR JSR LDA
AN1218 Rev. 2 30 For More Information On This Product, Go to: www.freescale.com
Freescale Semiconductor, Inc.
Application Note Instruction Cycle Improvements
Table 3. Instruction List (Sheet 3 of 4)
Opcode Mnemonic
LDA LDA LDX LDX LDX LSL LSL
Address Mode
IX1 IX2 IX IX1 IX2 DIR IX IX1 INH INH DIR IX IX1 INH INH INH DIR IX IX1 INH INH INH IX IX1 IX2 DIR IX IX1 INH INH DIR IX IX1 INH INH
HC05 Cycles
4 5 3 4 5 5 5 6 3 3 5 5 6 3 3 11 5 5 6 3 3 2 3 4 5 5 5 6 3 3 5 5 6 3 3
HC08 Cycles
3 4 2 3 4 4 3 4 1 1 4 3 4 1 1 5 4 3 4 1 1 1 2 3 4 4 3 4 1 1 4 3 4 1 1
Freescale Semiconductor, Inc...
LSL LSLA LSLX LSR LSR LSR LSRA LSRX MUL NEG NEG NEG NEGA NEGX NOP ORA ORA ORA ROL ROL ROL ROLA ROLX ROR ROR ROR RORA RORX
AN1218 Rev. 2 For More Information On This Product, Go to: www.freescale.com 31
Freescale Semiconductor, Inc. Application Note
Table 3. Instruction List (Sheet 4 of 4)
Opcode Mnemonic
RSP RTI RTS SBC SBC SBC SEC
Address Mode
INH INH INH IX IX1 IX2 INH DIR EXT IX IX1 IX2 INH DIR EXT IX IX1 IX2 IX IX1 IX2 INH INH DIR IX IX1 INH INH INH INH
HC05 Cycles
2 9 6 3 4 5 2 4 5 4 5 6 2 4 5 4 5 6 3 4 5 10 2 4 4 5 3 3 2 2
HC08 Cycles
1 7 4 2 3 4 1 3 4 2 3 4 1 3 4 2 3 4 2 3 4 9 1 3 2 3 1 1 1 1
Freescale Semiconductor, Inc...
STA STA STA STA STA STOP STX STX STX STX STX SUB SUB SUB SWI TAX TST TST TST TSTA TSTX TXA WAIT
AN1218 Rev. 2 32 For More Information On This Product, Go to: www.freescale.com
Freescale Semiconductor, Inc.
Application Note Conclusion
Conclusion
This application note has covered the differences between the HC05 and the HC08 CPU architecture. Please refer to the M68HC05 Applications Guide for further study of the CPU05. The CPU08 Reference Manual is a valuable resource for studying the CPU08 in more detail. Please consult your local Freescale sales office or your authorized Freescale distributor for applications support, literature, and specific part information. The MCU BBS is also available with free software for use with HC05 and HC08 MCUs. The BBS number is (512) 891-3733. The code examples used in this application note can be found on the BBS. The file name is HC08OPT.ARC.
Freescale Semiconductor, Inc...
AN1218 Rev. 2
For More Information On This Product, Go to: www.freescale.com
33
Freescale Semiconductor, Inc. Application Note Appendix A — New CPU08 Indexing Instruction Examples
************************************************************ * * File : INDEX.ASM * Description : * Shows examples for new CPU08 indexing * instructions - AIX, CLRH, CPHX, LDHX, STHX * Not all addressing modes are shown. * Note : Please consult the CPU08 Reference Manual * for further details on these instructions * Code is written for educational * purposes only * ************************************************************** ORG ***** START $200
Freescale Semiconductor, Inc...
AIX - add immediate to index register LDHX AIX #$1010 #-10 ; H:X ← $1010 ; H:X = $1010 + (-$10) ; = $1000
*****
CLRH - clear index high LDHX CLRH #$1290 ; H:X ← $1290 ; H:X ← $0090
*****
CPHX - compare 16-bit index register LDHX CPHX #$1290 #$1290 ; ; ; ; ; ; H:X CCR CCR H:X CCR CCR ← $1290 = %0110,1000 before CPHX, Z=0 ← $1290 = %0110,1010 after CPHX, Z=1
*****
LDHX - load 16-bit index register LDHX #$1290 ; H:X ← $1290
*****
STHX - store 16-bit index register LDHX STHX #$1290 $50 ; ; ; ; H:X ← $1290 ($50) ← (H:X) ($50) ← $12 ($51) ← $90
DONE *****
NOP BRA
DONE
Initialize the reset vector ORG $FFFE DW START
AN1218 Rev. 2 34 For More Information On This Product, Go to: www.freescale.com
Freescale Semiconductor, Inc.
Application Note Appendix B — CPU05 and CPU08 512-Byte Table Indexing Code
Appendix B — CPU05 and CPU08 512-Byte Table Indexing Code
************************************************************ * * File : INDEXX.ASM * Description : * The following code illustrates the * different instructions used to address * a 512 byte table in memory. HC05 and HC08 * code is compared. * Notes: Comments to the right of some instructions * give numbers. * CPU05 - 1st # is CPU05 cycle count * 2nd # is instruction byte count * CPU08 - 1st # is CPU08 cycle count * 2nd # is instruction byte count * Please consult the CPU08 Reference Manual * for further details on these instructions * Code is written for educational * purposes only * ************************************************************
Freescale Semiconductor, Inc...
* * *
For the purpose of this example, the table address will be predefined in RAM. TBL_A = $120
TBL_ST0 EQU TBL_ST1 EQU ORG RMB
$400 TBL_ST0+256T $50 2
; ; ; ; ;
start of table, section 0 start of table, section 1 start of RAM variables address for table to be accessed by the code
TBL_A
ORG ***** *****
$200
Address a 512 byte table with the index register The table starts at $400 and ends at $5FF
AN1218 Rev. 2 For More Information On This Product, Go to: www.freescale.com 35
Freescale Semiconductor, Inc. Application Note
********************************* * HC05 code * * CPU05 has to address the table in a section-like * fashion. Section 0 is between $400 and $4FF. * Section 1 is between $500 and $5FF. * The 16-bit address is stored in RAM location TBL_A. * This is the offset to the table starting * at $400, TBL_ST0. * Example: Address is $520 = $400 + $120 * TBL_A = $01 * TBL_A+1 = $20
Freescale Semiconductor, Inc...
START
LDX LDA BEQ LDA BRA
TBL_A+1 TBL_A TBL0 TBL_ST1,X NEXT
;3,2 ;3,2 ;3,2 ;5,3 ;3,2 ;
X ← (TBL_A+1) A ← (TBL_A) branch to section 0 if 0 A ← (X+TBL_ST1) branch when done to the CPU08 example
TBL0
LDA
TBL_ST0,X
;5,3 A ← (X+TBL_ST0)
* Total # CPU05 cycles = 17 (max) * Total # bytes = 11 (max) ********************************* ********************************* * HC08 code * * CPU08 has full 16-bit indexed addressing so the * table address is loaded from TBL_A in RAM. No * memory table sectioning is needed. NEXT LDHX LDA TBL_A TBL_ST0,X ;4,2 H:X ← (TBL_A) ;4,3 A ← (X+TBL_ST0)
* Total # CPU08 cycles =8 * Total # bytes =5 *********************************
DONE
NOP BRA
DONE
*****
Initialize the reset vector ORG $FFFE DW START
AN1218 Rev. 2 36 For More Information On This Product, Go to: www.freescale.com
Freescale Semiconductor, Inc.
Application Note Appendix C — New CPU08 Stack Pointer Instructions
Appendix C — New CPU08 Stack Pointer Instructions
************************************************************ * * File : SP.ASM * Description : * Shows examples for new CPU08 stack pointer * instructions - AIS, PSHA, PSHH, PSHX * PULA, PULH, PULX, TSX, TXS * Not all addressing modes are shown. * Note : Please consult the CPU08 Reference Manual * for further details on these instructions * Code is written for educational * purposes only * ************************************************************
Freescale Semiconductor, Inc...
ORG ***** * START
$200
AIS - add immediate to stack pointer SP is predefined at $0FE0 AIS #$1F ; SP ← $0FE0 + $1F ; SP = $0FFF
***** * *
PSHA - push accumulator onto stack SP is predefined at $0FFF A = $80 PSHA ; ($0FFF) ← $80 ; SP ← SP-$01 ; SP = $0FFE
***** * *
PSHH - push index register H onto stack SP is predefined at $0FFE H:X = $2050 PSHH ; ($0FFE) ← $20 ; SP ← SP-$01 ; SP = $0FFD
***** * *
PSHX - push index register X onto stack SP is predefined at $0FFD H:X = $2050 PSHX ; ($0FFD) = $50 ; SP ← SP-$01 ; SP = $0FFC
AN1218 Rev. 2 For More Information On This Product, Go to: www.freescale.com 37
Freescale Semiconductor, Inc. Application Note
***** * * * PULX - pull index register X from stack SP is predefined at $0FFC $0FFD = $50 H:X = $0000 PULX ; ; ; ; SP ← SP+$01 SP = $0FFD X ← ($0FFD) H:X = $0050
Freescale Semiconductor, Inc...
***** * * *
PULH - pull index register H from stack SP is predefined at $0FFD $0FFE = $20 H:X = $0050 PULH ; ; ; ; SP ← SP+$01 SP = $0FFE H ← ($0FFE) H:X = $2050
***** * * *
PULA - pull accumulator from stack SP is predefined at $0FFE $0FFF = $80 A = $00 PULA ; ; ; ; SP ← SP+$01 SP = $0FFF A ← ($0FFF) A = $80
***** * *
TSX - transfer stack pointer to index register SP is predefined at $0FF5 H:X = $1290 TSX ; H:X ← SP+$01 ; H:X = $0FF6
***** * *
TXS - transfer index register to stack pointer SP is predefined at $0FF5 H:X = $1290 TXS ; SP ← H:X-$01 ; SP = $128F
DONE
NOP BRA
DONE
*****
Initialize the reset vector ORG $FFFE DW START
AN1218 Rev. 2 38 For More Information On This Product, Go to: www.freescale.com
Freescale Semiconductor, Inc.
Application Note Appendix D — Using the Stack in a Subroutine to Compute a Cube
Appendix D — Using the Stack in a Subroutine to Compute a Cube
************************************************************ * * File : CUBE.ASM * Description : * This program takes an 8-bit positive * number, X_IN, and cubes it. The answer, * Y_IN, is in a 24-bit format. * This program also illustrates the * value of using the stack for complex * subroutines that use parameter passing, * local variables, and return values. * Stack Description: * Given below is a diagram of the stack * during the subroutine * The numbers on the right specify the * number of bytes above the stack pointer * * ------------------------* SP → ?? * VAR1 1 * VAR2 2 * A 3 * H 4 * X 5 * PC_HIGH 6 * PC_LOW 7 * Y_HIGH 8 * Y_MED 9 * Y_LOW 10 * X_IN 11 * ------------------------* * Note : Please consult the CPU08 Reference Manual * for further HC08 instruction details * Code is written for educational * purposes only * ************************************************************
Freescale Semiconductor, Inc...
ORG X_IN RMB ORG
$80 1 $200 ;8-bit number to be cubed
AN1218 Rev. 2 For More Information On This Product, Go to: www.freescale.com 39
Freescale Semiconductor, Inc. Application Note
***** * * * * START Load up stack before entering the subroutine Stack is given the 8-bit number to be cubed, X_IN Next, 3 bytes must be made available to the stack for the 24 bit output of the routine 3 pushes are made to illustrate this point LDA PSHA CLRA PSHA PSHA PSHA ***** X_IN ;A ← (X_IN) ;push parameter X_IN onto stack ;zero must be pushed on stack ; allocation for return answer ;push Y_Low byte onto stack ;push Y_Med byte onto stack ;push Y_High byte onto stack
Freescale Semiconductor, Inc...
Jump to the cube subroutine JSR CUBE ;jump sub to CUBE, Y = X_IN^3 When subroutine is over, reset stack pointer to original location. Pull the answers off the stack when needed. AIS BRA #$04 DONE ;SP ← (SP) + $04 ;branch to the end of this ;example
***** *
***** ***** * * CUBE
CUBE subroutine Given X_IN, find Y = X^3 Save X,H, and A on stack Decrement stack for 2 bytes PSHX ;push X onto stack PSHH ;push H onto stack PSHA ;push A onto stack AIS #-2 ;decrement stack for local var Run the math routine Square X_IN, answer is X:A LDA 11T,SP ;A = X_IN LDX 11T,SP ;X = X_IN MUL ;X:A = (X)*(A) Store away the high byte answer, X, to var1 STX 1,SP ;store high answ to var1 Multiply 16 bit result by X_IN Multiply X_IN by low byte of 16-bit square LDX 11T,SP ;X = X_IN MUL ;X:A = (X)*(A)
* *
*
* *
AN1218 Rev. 2 40 For More Information On This Product, Go to: www.freescale.com
Freescale Semiconductor, Inc.
Application Note Appendix D — Using the Stack in a Subroutine to Compute a Cube
* * *
Store away low byte of 16-bit result to Y_LOW Store high byte of 16-bit result to var2 STA 10T,SP ;store low answ to Y_LOW STX 2,SP ;store high answ to var2 Multiply high byte of 16-bit result by X_IN LDA 11T,SP ;A ← X_IN LDX 1,SP ;load X with var1 MUL ;X:A = X_IN * var1 Store high byte of answer to Y_HIGH STX 8T,SP ;store high byte to Y_HIGH ADD var2 to the low byte answer to get Y_MED If there is a carry, add one bit to Y_HIGH ADD 2T,SP ;A = var2 + A BCS CS ;branch if C bit set in CCR BRA FIN ;C bit is 0, branch to FIN INC STA 8T,SP 9T,SP ;add 1 to Y_HIGH ;store A to Y_MED
*
*
Freescale Semiconductor, Inc...
* *
CS FIN
* * * *
Save X,H, and A on stack Increment stack for 2 bytes Restore X,H, and A Return from the subroutine AIS #$02 PULA PULH PULX RTS
DONE
NOP BRA
DONE
*****
Initialize the reset vector ORG $FFFE DW START
AN1218 Rev. 2 For More Information On This Product, Go to: www.freescale.com 41
Freescale Semiconductor, Inc. Application Note Appendix E — New CPU08 MOV Instruction Examples
************************************************************ * * File : MOVE.ASM * Description : * Shows examples for the MOV instruction * All four addressing modes are illustrated * 05 and 08 code is compared * Notes: Comments to the right of some instructions * give numbers. * CPU05 - 1st # is CPU05 cycle count * 2nd # is instruction byte count * CPU08 - 1st # is CPU08 cycle count * 2nd # is instruction byte count * Please consult the CPU08 Reference Manual * for further details on these instructions * Code is written for educational * purposes only * ************************************************************
Freescale Semiconductor, Inc...
ORG ***** * START
$200
Move Immediate value to Direct memory location HC05 LDA STA HC08 MOV * ;2,2 A ← $30 ;4,2 ($80) ← (A) * ;4,3 ($80) ← $30 = 6,4 = 4,3
#$30 $80
*
#$30,$80
* * ***** *
Total CPU05 cycles, bytes Total CPU08 cycles, bytes
Move Direct mem value to Direct mem location HC05 LDA STA * ;3,2 A ← ($80) ;4,2 ($90) ← (A)
$80 $90
*
HC08 MOV
$80,$90
* ;5,3 ($90) ← ($80) = 7,4 = 5,3 AN1218 Rev. 2
* *
Total CPU05 cycles, bytes Total CPU08 cycles, bytes
42
For More Information On This Product, Go to: www.freescale.com
Freescale Semiconductor, Inc.
Application Note Appendix E — New CPU08 MOV Instruction Examples
*****
Move contents of Indexed to Direct mem location, post inc Xreg HC05 LDX LDA STA INCX HC08 LDX MOV * ; ;3,1 ;4,2 ;3,1 X ← $80 A ← (X) ($90) ← (A) X←X+1
*
#$80 ,X $90
*
#$80 X+,$90
Freescale Semiconductor, Inc...
* ; X ← $80 ;4,2 ($90) ← (X) ; X←X+1 = 10,4 = 4,2
* * *****
Total CPU05 cycles, bytes Total CPU08 cycles, bytes
Move Direct mem contents to Indexed location, post inc Xreg HC05 LDX LDA STA INCX HC08 LDX MOV * ; ;3,2 ;4,1 ;3,1 X ← $90 A ← ($80) (X) ← (A) X←X+1
*
#$90 $80 ,X
*
#$90 $80,X+
* ; X ← $90 ;4,2 (X) ← ($80) ; X←X+1 = 10,4 = 4,2
* *
Total CPU05 cycles, bytes Total CPU08 cycles, bytes
*****
Initialize the reset vector ORG $FFFE DW START
AN1218 Rev. 2 For More Information On This Product, Go to: www.freescale.com 43
Freescale Semiconductor, Inc. Application Note Appendix F — CPU05 and CPU08 Data Movement Code
************************************************************ * * File : MOVEX.ASM * Description : * A user wants to start an application one of * two different ways. The user sets the * application on the MCU by the logic level * of Port D, bit 3. Once out of reset, the * MCU reads Port D and moves data from ROM * into the RAM configuration registers * according to the logic level of bit 3. * Notes: Comments to the right of some instructions * give numbers. * CPU05 - 1st # is CPU05 cycle count * 2nd # is instruction byte count * CPU08 - 1st # is CPU08 cycle count * 2nd # is instruction byte count * Please consult the CPU08 Reference Manual * for further details on these instructions * Code is written for educational * purposes only * ************************************************************ * * * TBL PORTD PORTADR PORTBDR SPICTRL SCICTRL TIMCTRL For the purpose of this example, we will be using random ctrl registers for the code. They are listed below in an equate table EQU EQU EQU EQU EQU EQU EQU ORG ***** * * * If bit starts If bit starts $1000 $03 $04 $05 $0A $0E $12 $200 3 = 0 when read, then the table at $1000 3 = 1 when read, then the table at $1008 ; start of table ; port D data register ; port A data direction register ; port B data direction register ; SPI control register ; SCI control register ; Timer control register
Freescale Semiconductor, Inc...
********************************* * HC05 code * START05 LDA AND TAX PORTD #$08 ;3,2 ;2,2 ; ;2,1 ; A ← (PORTD) clear A except bit 3 A = 0 or 8 X ← (A) set the offset of X
AN1218 Rev. 2 44 For More Information On This Product, Go to: www.freescale.com
Freescale Semiconductor, Inc.
Application Note Appendix F — CPU05 and CPU08 Data Movement Code A ← (TBL+X) (PORTADR) ← X←X+1 A ← (TBL+X) (PORTBDR) ← X←X+1 A ← (TBL+X) (SPICTRL) ← X←X+1 A ← (TBL+X) (SCICTRL) ← X←X+1 A ← (TBL+X) (TIMCTRL) ←
LDA STA INCX LDA STA INCX LDA STA INCX LDA STA INCX LDA STA
TBL,X PORTADR TBL,X PORTBDR TBL,X SPICTRL TBL,X SCICTRL TBL,X TIMCTRL
;5,3 ;4,2 ;3,1 ;5,3 ;4,2 ;3,1 ;5,3 ;4,2 ;3,1 ;5,3 ;4,2 ;3,1 ;5,3 ;4,2
(A)
(A)
(A)
(A)
(A)
Freescale Semiconductor, Inc...
* Total # CPU05 cycles = 64 * Total # bytes = 34 ********************************* ********************************* * HC08 code * START08 LDHX LDA AND TAX #TBL PORTD #$08 ;3,3 ;3,2 ;2,2 ; ;1,1 ; ;4,2 ; ;4,2 ; ;4,2 ; ;4,2 ; ;4,2 ; H:X ← TBL A ← (PORTD) clear A except bit 3 A = 0 or 8 X ← (A) set the offset of X (PORTADR) ← X←X+1 (PORTBDR) ← X←X+1 (SPICTRL) ← X←X+1 (SCICTRL) ← X←X+1 (TIMCTRL) ← X←X+1 (H:X) (H:X) (H:X) (H:X) (H:X)
MOV MOV MOV MOV MOV
X+,PORTADR X+,PORTBDR X+,SPICTRL X+,SCICTRL X+,TIMCTRL
* Total # CPU08 cycles = 29 * Total # bytes = 18 ********************************* DONE NOP BRA
DONE
*****
Initialize the reset vector ORG $FFFE DW START05
AN1218 Rev. 2 For More Information On This Product, Go to: www.freescale.com 45
Freescale Semiconductor, Inc. Application Note Appendix G — New Branch Instruction Examples
************************************************************ * * File : BRANCH.ASM * Description : * Shows examples for new CPU08 branch * instructions - CBEQ, CBEQA, CBEQX * DBNZ, DBNZA, DBNZX * Notes: Comments to the right of some instructions * give numbers. * CPU05 - 1st # is CPU05 cycle count * 2nd # is instruction byte count * CPU08 - 1st # is CPU08 cycle count * 2nd # is instruction byte count * Please consult the CPU08 Reference Manual * for further details on these instructions * Code is written for educational * purposes only * ************************************************************ ORG ***** * * * LPA $200
Freescale Semiconductor, Inc...
CBEQ - compare and branch if equal, direct A is predefined at $40 Memory location $80 contains $40 HC05 code CMP $80 BEQ LP1 BRA LPA * ;3,2 ;3,2 ; ;
(A) - ($80) if (A) = ($80) then branch to LP1 go to LPA, try again!
* LP1
HC08 code CBEQ $80,LPB BRA LP1
* ;5,3 if (A)-($80)=0, ; then branch to LPB ; go to LP1 = 6,4 = 5,3
* * ***** * * LPB
Total CPU05 cycles, bytes Total CPU08 cycles, bytes
CBEQA - compare and branch if equal, immediate A is predefined at $50 HC05 code CMP #$50 BEQ LP2 BRA LPB * ;2,2 (A) - $50 ;3,2 if (A) = $50, then LP2 ; go to LPB AN1218 Rev. 2
46
For More Information On This Product, Go to: www.freescale.com
Freescale Semiconductor, Inc.
Application Note Appendix G — New Branch Instruction Examples
* LP2
HC08 code CBEQA #$50,LPC BRA LP2
* ;4,3 if #$50 = (A), then LPC ; go to LP2 = 5,4 = 4,3
* *
Total CPU05 cycles, bytes Total CPU08 cycles, bytes
***** * * LPC
CBEQX - compare and branch if equal, index Index register X is predefined at $60 HC05 code CPX #$60 BEQ LP3 BRA LPC HC08 code CBEQX #$60,LPD BRA LP3 * ;2,2 X - $60 ;3,2 if X = $60, then LP3 ; go to LPC * ;4,3 if X = $60, then LPD ; go to LP3 = 5,4 = 4,3
Freescale Semiconductor, Inc...
* LP3
* *
Total CPU05 cycles, bytes Total CPU08 cycles, bytes
***** * * LPD
DBNZ - decrement and branch if not zero HC05 code * Memory location $A0 is predefined at $08 NOP ; used here to represent any ; number of instructions DEC $A0 ;5,2 decrement ($A0) BNE LPD ;3,2 if ($A0) not zero, then LPD HC08 code * Memory location $A0 is predefined at $08 NOP ; used here to represent any ; number of instructions DBNZ $A0,LP4 ;5,3 ($A0) = ($A0) - 1 ; if ($A0) not zero, then LP4 Total CPU05 cycles, bytes Total CPU08 cycles, bytes = 8,4 = 5,3
* * LP4
* *
AN1218 Rev. 2 For More Information On This Product, Go to: www.freescale.com 47
Freescale Semiconductor, Inc. Application Note
***** * * LPE DBNZA - decrement acca and branch if not zero HC05 code A is predefined at $06 NOP DECA BNE * ; ; ;3,1 ;3,2 used here to represent any number of instructions (A) = (A) - 1 if (A) not zero, then LPE
LPE
Freescale Semiconductor, Inc...
* * LP5
HC08 code A is predefined at $06 NOP DBNZA LP5
* ; ; ;3,2 ; used here to represent any number of instructions (A) = (A) - 1 if (A) not zero, then LP5 = 6,3 = 3,2
* *
Total CPU05 cycles, bytes Total CPU08 cycles, bytes
***** * * LPF
DBNZX - decrement x and branch if not zero HC05 code * Index register X is predefined at $04 NOP ; used here to represent any ; number of instructions DECX ;3,1 (X) = (X) - 1 BNE LPF ;3,2 if (X) not zero, then LPF HC08 code * Index register X is predefined at $04 NOP ; used here to represent any ; number of instructions DBNZX LP6 ;3,2 (X) = (X) - 1 ; if (X) not zero, then LP6 Total CPU05 cycles, bytes Total CPU08 cycles, bytes = 6,3 = 3,2
* * LP6
* *
DONE
NOP BRA
DONE
*****
Initialize the reset vector ORG $FFFE DW LPA
AN1218 Rev. 2 48 For More Information On This Product, Go to: www.freescale.com
Freescale Semiconductor, Inc.
Application Note Appendix H — CPU05 and CPU08 Search Code
Appendix H — CPU05 and CPU08 Search Code
************************************************************ * * File : BRANCHX.ASM * Description : * This code shows an example of using branch * algorithms to search for a number in a * table. The code will search for $FF in * a table. This would signify that in a * table of A/D values, an A/D reading * was saturated. * Notes: Comments to the right of some instructions * give numbers. * CPU05 - 1st # is CPU05 cycle count * 2nd # is instruction byte count * CPU08 - 1st # is CPU08 cycle count * 2nd # is instruction byte count * Please consult the CPU08 Reference Manual * for further details on these instructions * Code is written for educational * purposes only * ************************************************************
Freescale Semiconductor, Inc...
TABLE
EQU
$400
; starting address of the ; A/D table
ORG TBL_LEN RMB
$50 1
; memory value containing ; the number of values in ; a the A/D table
ORG ***** * * * * * * *
$200
Search for $FF (saturation) in a table of A/D values TBL_LEN is predefined at $08 for this example Therefore the table is defined from $400 to $407 The values given for the total # of cycles and bytes reflect an absolute count with no looping involved An accurate account of the cycle count would involve the table length and whether or not a comparison was made.
AN1218 Rev. 2 For More Information On This Product, Go to: www.freescale.com 49
Freescale Semiconductor, Inc. Application Note
* SRCH LOOP3 HC05 code LDX TBL_LEN LDA TABLE-1,X CMP #$FF BEQ NEXT * ;3,2 ;5,3 ;2,2 ;3,2 ; ; ; ;3,1 ;3,2 ; ; = 19 = 12
DECX BNE
LOOP3
X ← (TBL_LEN) A ← (TABLE-1+X) (A) - $FF if Z=1, then goto NEXT this signifies that a saturation value has been found in the table X ← X -1 if Z=0, then goto LOOP3 go look at another value in the table
Freescale Semiconductor, Inc...
* *
Total # CPU05 cycles Total # bytes
* NEXT LOOP4
HC08 code LDX TBL_LEN LDA TABLE-1,X CBEQA #$FF,DONE
DBNZX
LOOP4
* ;3,2 ;4,3 ;4,3 ; ; ; ; ;3,2 ; ; ; = 14 = 10
X ← (TBL_LEN) A ← (TABLE-1+X) (A) - $FF if Z=1, then goto DONE this signifies that a saturation value has been found in the table X ← X -1 if Z=0, then goto LOOP4 go look at another value in the table
* *
Total # CPU08 cycles Total # bytes
DONE
NOP BRA
DONE
*****
Initialize the reset vector ORG $FFFE DW SRCH
AN1218 Rev. 2 50 For More Information On This Product, Go to: www.freescale.com
Freescale Semiconductor, Inc.
Application Note Appendix I — New CPU08 Signed Branch Instruction Examples
Appendix I — New CPU08 Signed Branch Instruction Examples
************************************************************ * * File : SIGNBRA.ASM * Description : * Shows examples for new CPU08 signed branch * instructions - BGE, BGT, BLE, BLT * The examples demonstrate two's complement * math with branching. * Note : Please consult the CPU08 Reference Manual * for further details on these instructions * Code is written for educational * purposes only * ************************************************************ ORG ***** * LP_BGE $200
Freescale Semiconductor, Inc...
BGE - branch if greater than or equal A is predefined at $FF CMP BGE BRA #$FF LP_BGT LP_BGE ; ; ; ; (A) - $FF, [ -1 - (-1) ] if A >= $FF, then branch to LP_BGT go to LP_BGE
***** * LP_BGT
BGT - branch if greater than A is predefined at $07 CMP BGT BRA #$FF LP_BLE LP_BGT ; ; ; ; (A) - $FF, [ 7 - (-1) ] if A > $FF, then branch to LP_BLE go to LP_BGT
***** * LP_BLE
BLE - branch if less than or equal A is predefined at $FF CMP BLE BRA #$FF LP_BLT LP_BLE ; ; ; ; (A) - $FF, [ -1 - (-1) ] if A