Implementation of Soft Stack in MAXQ2000 Data Memory

Abstract: This application note describes a simple method for implementing a soft stack in data memory in assembly applications. This method uses MAXQ2000 and other MAXQ20 based microcontrollers. Using the macro pre-processing feature of MAX-IDE, sample code was written in Maxim's MAXQ® series engineering application development and debugging environment.

Introduction The MAXQ2000 microcontroller, like Maxim's RISC microcontroller series MAXQ devices, is based on the MAXQ20 core. Microcontrollers based on MAXQ20 can usually implement a 16-bit wide hard stack with a fixed depth (16 for MAXQ2000) and stored in a dedicated internal memory separate from data and program space. Through subroutine calls and interrupts, this hard stack can be used to save and restore the operating state of the microcontroller.

Although it is very suitable for compact applications, in large-scale assembly applications, using deeply nested subroutines (or using subroutines that save and restore multiple working registers on the stack), the hard stack will quickly run out space. Using the "soft stack" in the data memory to write applications in the C programming language (using compilers such as IAR's Embedded Workbench®) or Rowley Associates' Crossworks for MAXQ can avoid this problem. (For detailed information about these development tools, please refer to: Development Tools). This soft stack stores the subroutine call / return address and local working variables. However, the MAXQ20 core has no built-in mechanism to locate the stack in the data memory, which is only used in assembly applications.

This application note describes a simple method for implementing a soft stack in data memory in assembly applications. The code in the application note can be used for MAXQ2000 and other MAXQ20 based microcontrollers. Using the macro pre-processing feature of MAX-IDE, the example code is written in Maxim's MAXQ series engineering application development and debugging environment.

You can download the latest installation package and documentation for the MAX-IDE environment for free: MAX-IDE installation (ZIP) MAXQ core assembly guide (PDF) development tool guide (PDF) MAXQ20 core hard stack work MAXQ20 core uses two different types Stack working mode: PUSH work (it includes operation codes PUSH, LCALL and SCALL) is used to store data in the stack. These operations increment the stack pointer SP by one, and then store the data at the stack location pointed to by the SP pointer (@SP). POP work (which includes the operation codes POP, POPI, RET, and RETI) is used to recover data from the stack. These operations restore data from the stack location pointed to by SP, and then decrement the stack pointer by one. A main function of the hard stack is to save and restore the address when calling the subroutine. Therefore, the stack contains 16 bits (words). This bit wide supports saving or restoring the 16-bit instruction pointer (IP) register in a push or pop operation. Although PUSH or POP can be used to save or restore 8-bit registers (for example, AP) on the stack, each stack operation always uses the entire 16-bit word.

In addition to the various operating codes that use the stack, there are two other situations in which the microcontroller automatically uses the hard stack: When an interrupt is processed, the interrupt service routine starts (referred to by the interrupt vector register IV) Previously, the current program execution point was pushed onto the stack. When calling certain debugging commands (for example, reading registers and writing data memory), the code in the program ROM needs to be executed. Before the control is transferred to the program ROM, the current execution point is pushed onto the stack. Once the debug program in the program ROM is executed, the execution point is ejected from the stack, restoring the previous state of the processor. Finding the interrupt service routine or executing the debugger command always requires using the hard stack. Since this behavior is embedded in the hardware, this problem cannot be avoided. However, for the more commonly used pairs of PUSH / POP and CALL / RET instructions, a soft stack can be used.

Note that when any of the following stack error conditions occur, the MAXQ2000 (like other MAXQ20 core-based devices) does not support any error detection or alarm: Stack Overflow: Occurs when pushing a value onto a stack that is already full. This error caused the oldest value in the stack to be overwritten. Stack underflow: Occurs when a value is ejected from an empty stack. This error caused an invalid data value to be returned. For example, if RET is executed when the stack is empty, an incorrect (possibly random) address will be returned after execution. Building a soft stack in data memory The first step in building a soft stack in data memory is to define which part of data memory to use. Then, a data memory pointer (DP [0], DP [1], or BP [Offs]) that tracks the current position at the top of the stack must be defined. Note: The application software does not use stack-specific data memory (for example, variables or buffers) for other purposes.

A simple way to define and initialize such a soft stack involves the BP [Offs] register pair and a formula. SS_BASE equ 0100h ss_init: move DPC, # 1Ch; Set all pointers to word mode move BP, #SS_BASE; Set base pointer to stack base locaTIon move Offs, # 0; Set stack to start ret if the base pointer (BP) register is set At the SS_BASE position, the Offs register can be used to point to the current top of the stack. Since the Offs register is only 8 bits wide, the hardware will automatically limit the stack to the data memory (BP .. (BP + 255)). If a push-in occurs when Offs is equal to 255 (overflow), or a pop-up occurs when Offs is equal to 0 (underflow), then the Offs register is limited to the other end of the stack. This behavior mimics the way the hard stack works and supports a simple soft stack implementation; it does not detect underflow and overflow states.

Before using the soft stack, the main program must call the ss_init routine. It sets the BP [Offs] pointer to word mode. Since the soft stack is implemented as a 16-bit wide stack, this step must be completed. It also points the BP [Offs] register pair to the beginning of the stack.

Soft stack work cannot redefine the built-in operation codes (PUSH, POP, CALL, RET, etc.) that use the stack to use the soft stack, because these codes mean hard-wired connection of the MAXQ assembler. Moreover, it may be necessary to use a standard hard stack in some parts of the application, for example, interrupt service routines. For these reasons, macros that copy standard operating codes access the soft stack. mpush MACRO Reg move @BP [++ Offs], Reg; Push value to soft stack endm mpop MACRO Reg move Reg, @BP [Offs--]; Pop value from soft stack endm mcall MACRO Addr LOCAL return move @BP [+ + Offs], #return; Push return desTInaTIon to soft stack jump Addr return: endm mret MACRO jump @BP [Offs--]; Jump to popped destination from soft stack endm The following will discuss how these macros work.

mpush This macro is used in the same way as the PUSH operation code. It supports pushing 8-bit or 16-bit registers, or pushing immediate values ​​onto the stack. mpush A [0]; Save the value of the A [0] register mpush A [1]; Save A [1] mpush A [2]; Save A [2] ...; code which destroys A [0]- A [2] mpop A [2]; Restore the value of A [2] (pop in reverse order) mpop A [1]; Restore A [1] mpop A [0]; Restore A [0] mpop This macro is used in the same way as the POP operation code. It supports loading 8-bit or 16-bit registers from the stack, as shown above. Note that if a 16-bit value is pushed and the value pops into an 8-bit register, then only the low-order byte is stored in the register. The high byte is lost. This is consistent with the built-in hard stack. subroutine: mpush A [0]; Save the current value of A [0] ...; Code which destroys A [0] mpop A [1]; Restore A [0] mret mcall

The mretmcall macro is used in the same way as the CALL operation code execution subroutine. Once the execution is complete, the subroutine must use the mret macro (instead of the standard RET operation code) to return. mcall mySub ... mySub: mpush A [0]; Save A [0] mpush A [1]; Save A [1] ...; Perform calculations, etc. mpop A [1] mpop A [0] mret expansion The size of the soft stack Some applications require a soft stack larger than 256 levels. Using one of the other data pointers (DP [0] or DP [1]), a stack of any size can be achieved (up to the upper limit of the data memory). SS_BASE equ 0000h SS_TOP equ 01FFh ss_init: move DPC, # 1Ch; Set all data pointers to word mode move DP [0], #SS_BASE; Set pointer to stack base location ret The above code is stored in the data memory from 0000h to 01FFh, so , The resulting stack can maintain 511 levels. (A location in the stack memory space is not used; this can be more efficient and implements the mpush / mpop / mcall / mret macro with shorter code.)

Only the data pointer is changed, and the rest of the code remains unchanged to expand the stack. Nonetheless, since DP [0] is not limited to a certain range of data memory, there are no measures to prevent a push or pop operation that increases or decreases DP [0] from exceeding the specified soft stack boundary. To avoid this, a simple underflow / overflow check can be added.

Add underflow and overflow checks to keep the macro (expand into code each time it is used) as short as possible, complete the underflow and overflow checks in the subroutine, use the hard stack, and call the subroutine by the macro. mpush MACRO Reg call ss_check_over; Check for possible overflow move @ ++ DP [0], Reg; Push value to soft stack endm mpop MACRO Reg call ss_check_under; Check for possible underflow move Reg, @DP [0]-; Pop value from soft stack endm mcall MACRO Addr LOCAL return call ss_check_over; Check for possible overflow move @ ++ DP [0], #return; Push return destination to soft stack jump Addr return: endm mret MACRO call ss_check_under; Check for possible underflow jump @ DP [0]-; Jump to popped destination from soft stack endm ss_check_under: push A [0] push AP push APC push PSF move APC, # 80h; Set Acc to A [0], standard mode, no auto inc / dec move Acc, DP [0]; Get current value of stack pointer cmp #SS_BASE jump NE, ss_check_under_ok nop; <Error handler should be implemented here> ss_check_under_ok: pop PSF pop APC pop AP pop A [0] ret ss_check_over: push A [ 0] push AP push APC push PSF move APC, # 80h; Set Acc to A [0], standard mode, no auto inc / dec move Acc, DP [0]; Get current value of stac k pointer cmp #SS_TOP jump NE, ss_check_over_ok nop; <Error handler should be implemented here> ss_check_over_ok: pop PSF pop APC pop AP pop A [0] ret The above code causes a push or pop (or call or ret) operation Check the current stack position before. If an underflow or overflow error is detected, different applications have different responses. Under normal circumstances, this type of error will only appear in the application development stage; if the code is written correctly, this type of error will not occur. If an error does occur, it is generally considered as a major error, like an underflow / overflow error when using the hard stack. Possible responses to such errors include suspending, sending error messages, or flashing LEDs. In the development phase, a good way is to set a breakpoint in MAX-IDE for each of the two programs (for example, in the "Error handler should be implemented here" line). If an underflow or overflow occurs, immediately Send feedback.

However, applications can sometimes recover from stack underflow or overflow (for example, reloading and restarting application subtasks from scratch). In this case, you need to simply set a flag to indicate that a stack error has occurred. Optional such flags include two general flags (GPF0 and GPF1) located in the PSF register. Since there are two bit flags, one of them can be used to indicate overflow and the other to indicate underflow.

Conclusion The powerful macro preprocessing function of MAX-IDE enables the direct replacement of the MAXQ2000 and other MAXQ20 microcontroller data memory soft stacks. This soft stack makes subroutines more modular and reusable, which helps the development of large-scale assembly applications. The stack also supports detection of stack errors.

Water Global Lantern

Battery Operated Led,LED Lighted Lantern,Led Swirling Lighting

XINGYONG XMAS OPTICAL (DONGGUAN ) CO., LTD , https://www.xingyongxmas.com

Posted on