1. Field of the Invention
The present invention relates to a stack controller for use in a processor system such as a microprocessor when executing a program sequence, and to a method therefor.
2. Description of the Background Art
Conventionally, a microprocessor that executes the same program multiple times defines the processing part of that program as a subroutine for efficient program execution. When an instruction that calls such a subroutine is executed, a calling program pushes the return address, which points to the routine to be processed immediately after the subroutine, onto a stack and then passes control to the start address of the subroutine. When the processing of the subroutine is terminated, the program executes the return-from-subroutine instruction, pops the return address from the stack, and passes control to the address.
To speedily push onto and pop from the stack during program execution, such a microprocessor has a stack register provided in its hardware and, as the program progresses, automatically pushes or pops a program counter, a loop counter, and a loop end address onto and from the stack under hardware control.
In a microprocessor, when the stack capacity is exceeded during program execution, a program sequence halts unexpectedly or a program runaway problem is generated due to a missing return-from-subroutine address. Since an attempt to simply increase the capacity of the stack to solve those programs would result in an endless increase in the hardware size, other solutions are known.
For example, Japanese patent laid-open publication No. 77004/1996 discloses a memory management method in which, when the bus management feature detects an interrupt causing a stack area to overflow, the address of the stack area, pointed to by a stack pointer, is saved and the address of a free area in a random-access memory (RAM) is set in the stack pointer to continue the process.
European patent application publication No. EP0720087 A1 discloses a memory extension stack circuit that performs the following operation in a data processing system. When the hardware stack is almost full, the move instruction is inserted into a program sequence in execution to move the last entry in the hardware stack to a memory extension stack. When the hardware stack is almost empty, the move instruction is inserted into the program sequence to move an entry from the memory extension stack to the end of the hardware stack. In this way, the program keeps the hardware running.
The problem with the memory management method disclosed in the Japanese patent publication is that the stack is usually prepared in the main memory and, in addition, allocated to the extension stack are frequently-used, most-recent data. Therefore, this memory management method is not suitable for high-speed processing.
The problem with the memory extension stack circuit disclosed in the European patent publication is that the control logics manage and process all processing for the hardware stack and the memory extension stack. Therefore, the control logics tend to become large and complicated.
It is an object of the present invention to provide a stack controller for and a method of executing a program sequence continuously without halting or runaway and without increase in hardware size or circuit complexity even when the stack register capacity is exceeded during program execution.
In accordance with the present invention, a stack controller comprises a main controller for executing a program sequence including a plurality of instructions such as an operation instruction and a subroutine call instruction; a stack register having a first stack where first data are stored in a Last-In Fast-Out (LIFO) fashion, the first data including a program counter, a loop counter and a loop end address of the predetermined program; and a storage circuit for temporarily storing second data such as an intermediate result processed by the operation instruction. The storage circuit includes a second stack where the first data are stored in a LIFO fashion. The stack controller further comprises an interrupt controller that monitors a capacity of the first stack. When a number of free entries of the first stack has reached a predetermined number of push-trigger entries, the interrupt controller generates a push interrupt and sends the generated push interrupt to the main controller. When the number of free entries has reached a predetermined number of pop-trigger entries, the interrupt controller generates a pop interrupt and sends the generated pop interrupt to the main controller. The main controller comprises a push interrupt processor operative in response to the push interrupt for repeatedly retrieving data from a bottom of the first stack and then pushing the retrieved data onto the second stack a number of times equal to a predetermined number of restoring entries; and a pop interrupt processor operative in response to the pop interrupt for repeatedly popping data from the second stack and then storing the popped data at the bottom of the first stack a number of times equal to a predetermined number of restoring entries.
Also in accordance with: the present invention, a stack control method comprises the steps of: executing by a main controller a program sequence including a plurality of instructions such as an operation instruction and a subroutine call instruction; storing first data in a first stack in a stack register in a LIFO fashion, the first data including a program counter, a loop counter and a loop end address of the predetermined program; and temporarily storing second data, such as an intermediate result processed by the operation instruction, into a memory. The method further comprises the steps of: storing the first data into a second stack in the memory in a LIFO fashion; monitoring a capacity of the first stack and generating, when a number of free entries of the first stack has reached a predetermined number of push-trigger entries, a push interrupt and sending the generated push interrupt to the main controller, and, when the number of free entries has reached a predetermined number of pop-trigger entries, a pop interrupt to send the generated pop interrupt to the main controller; repeatedly retrieving data in response to the push interrupt, by the main controller, from a bottom of the first stack and then pushing the retrieved data onto the second stack a number of times equal to a predetermined number of restoring entries; and repeatedly popping data in response to the pop interrupt, by the main controller, from the second stack and then storing the popped data at the bottom of the first stack a number of times equal to a predetermined number of restoring entries.
In accordance with the stack controller according to the present invention, the interrupt controller generates a push interrupt before the hardware stack is filled to allow the main controller to retrieve data from the bottom of the hardware stack and push the retrieved data onto the software stack to temporarily save it. After free entries are created in the hardware stack, the interrupt controller generates a pop interrupt to allow the main controller to pop data from the software stack and restore the popped data at the bottom of the hardware stack. In this way, the stack controller according to the present invention allows a program to be executed continuously without program halting or runaway and without increase in the size or scale and complexity of the main controller even when the hardware stack capacity is exceeded during program execution.
In accordance with the stack controller according to the present invention, multiple data entries can be saved from or restored to the hardware stack at a time. This feature reduces the frequency at which push or pop interrupts are generated and prevents performance degradation that would be otherwise be caused by push and pop interrupts.
The objects and features of the present invention will become more apparent from consideration of the following detailed description taken in conjunction with the accompanying drawings in which:
With reference to the accompanying drawings, a preferred embodiment of a stack controller according to the present invention will be described in detail. As shown in
In the stack controller 10 in accordance with the illustrative embodiment, the main controller 12, the ROM 14, the stack register 16, the interrupt controller 18 and the RAM 20 are interconnected as illustrated with a control bus 22, over which control signals are transferred. The main controller 12, the ROM 14, the stack register 16, the interrupt controller 18, and the RAM 20 are also interconnected with a data bus 24, over which data signals are transferred.
The main controller 12, generally comprised of a CPU (Central Processing Unit), is adapted for controlling and managing the overall operation of the stack controller 10, and specifically for sequentially reading out instructions of program sequences from the ROM 14 for execution. The main controller 12 pushes and pops data for a program counter, a loop counter and loop end addresses used during program execution, onto and from the hardware stack 26.
For example, when a subroutine call instruction is to be executed, the main controller 12 pushes a subroutine return address therefor onto the hardware stack 26, processes the subroutine, pops a return address therefor from the hardware stack 26 after processing the subroutine, and continues the processing of the instruction associated with the return address. Also, when the main controller 12 receives an external interrupt instruction from the interrupt controller 18, the main controller 12 pushes a return address pointing to an instruction immediately following the current instruction onto the hardware stack 26, thereafter processes the external interrupt, and pops the return address from the hardware stack 26 after the processing of the external interrupt to thereby continue the processing of the instruction indicated by the return address.
The main controller 12 in this embodiment is in response to push and pop interrupt instructions supplied from the interrupt controller 18 over the control bus 22 to execute push and pop interrupt processing, respectively. During push interrupt processing, the main controller 12 sequentially retrieves data from the bottom of hardware stack 26 and pushes the retrieved data onto the software stack 28, the operation being repeated for a predetermined number of saving entries. During pop interrupt processing, the main controller 12 sequentially pops data from the software stack 28 and stores the popped data at the bottom of the hardware stack 26, the processing being also repeated for a predetermined number of restoring entries. The predetermined number of saving entries equals the predetermined number of restoring entries. In the description below, this number is referred to as the number of software stack storing entries.
In the instant embodiment, the main controller 12 can reference an internal address access register 30 provided in the hardware to access the entry at the bottom of the hardware stack 26. As shown in
The ROM 14, preferably a non-volatile memory, is adapted to store program sequences each composed of a plurality of instructions such as operation instructions and subroutine call instructions. The ROM 14 in this embodiment also stores program sequences for push and pop interrupt processing to be executed by the main controller 12.
The stack register 16 is a type of data storage structure capable of storing a plurality of data entries. In this embodiment, the stack register 16 may contain, in the form of hardware stack 26 to or from which data required for program instruction execution are written in or read out at high speed, a program counter stack for storing a program counter for containing the return address of a processing routine such as a program subroutine or an interrupt processing routine, a loop counter stack for containing the number of multiply nested loops, and a loop end address stack for containing the loop end address of a loop.
The hardware stack 26 usually operates in the Last-In First-Out (LIFO) fashion in which the last written data are read out first. In the description, writing data onto the stack is referred to as pushing, and reading out data from the stack as popping.
In the illustrative embodiment, where the data entries stored in the hardware stack 26 have an equal number of bytes, dividing the total size or storage capacity of the hardware stack 26 by this number of bytes gives the maximum number for data entries that can be stored in the hardware stack 26. The hardware stack 26 overflows if the number of pushed data entries exceeds this maximum number for data entries.
As shown in
The stack register 16 can automatically decrement the pointer pointing to the read internal address in response to a read instruction, and increment the pointer pointing to the write internal address in response to a write instruction. If a read instruction is received when data are not stored in the hardware stack 26 or if a write instruction is received when data entries corresponding to the maximum number are stored in the hardware stack 26, the stack register 16 can then use the status register 32 to notify an error.
The interrupt controller 18 controls the stack pointer pointing to an entry in the stack register 16 over the control bus 22. The interrupt controller 18 also notifies an external interrupt, generated in response to an interrupt request from an external device such as a peripheral device, to the main controller 12 over the control bus 22. The interrupt controller 18 may have an external interrupt permission register that works as a flag register, not shown. In that case, the flag register contains a logical “1” or unity to permit an external interrupt and a logical “0” or null to inhibit an external interrupt. When an interrupt request is received from an external device, the interrupt controller 18 references this register. Only when the interrupt is permitted, notifies the external interrupt. The initial value of the external interrupt permission register is set to “1”.
The interrupt controller 18 in this embodiment primarily functions as monitoring the usage capacity of the hardware stack 26 to generate an interrupt, depending upon its usage monitored. For example, if the interrupt controller 18 determines that the number of free entries of the hardware stack 26 has reached the number of push-trigger entries, it generates a push interrupt. If the controller 18 determines that the number of free entries of the hardware stack 26 has reached the number of pop-trigger entries, it generates a pop interrupt. Such a generated interrupt is supplied to the main controller 12.
The interrupt controller 18 may be structured so that, immediately before sending a push or a pop interrupt notification, the interrupt controller 18 sets the external interrupt permission register to “0”, and when control is returned from the push or pop interrupt processing, appropriately, of the main controller 12, the interrupt controller 18 sets the external interrupt permission register to “1”.
The number of push- or pop-trigger entries determined by the interrupt controller 18 may be specified by the number of data entries that can be pushed onto the hardware stack 26. The number of push-trigger entries is represented in data entry number more than zero, and preferably equal to one entry of data. The number of pop-trigger entries ranges from the number of software stack storing entries plus one to the maximum number of data entries that can be stored in the hardware stack 26. The larger the number of software stack storing entries is, the larger the number of pop-trigger entries may be.
The interrupt controller 18 has a pop interrupt permission register that works as a flag register. This flag register contains “1” to permit a pop interrupt and “0” to inhibit a pop interrupt. The interrupt controller 18 may be adapted to reference this register when it determines that the number of free entries of the hardware stack 26 has reached the number of pop-trigger entries and to send the pop interrupt notification only when the interrupt is permitted. The initial value of the pop interrupt permission register is “0”.
This pop interrupt permission register is also controlled by the main controller 12. The main controller 12 may be adapted to check the number of data entries stored in the software stack 28 immediately before pop interrupt processing is terminated and to set the pop interrupt permission register to “0” only when the number of data entries is “0”.
The RAM 20 is used primarily as an operation or work memory for temporarily storing intermediate operational results or data. In particular, the RAM 20 in this embodiment has a saving data accumulation area, or software stack 28, for use by the main controller 12 during the push and pop interrupt processing. The software stack 28 works in the LIFO fashion.
Next, the operation of the stack controller 10 in this embodiment will be described with reference to the flowchart,
The hardware stack 26 in the stack register 16 of the stack controller 10 in the illustrative embodiment can store many data entries in practice. However, for convenience of description, the hardware stack 26 shown in
The software stack 28 in the RAM 20 in this embodiment is configured in the same way as the hardware stack 26. As shown in
In this embodiment, the main controller 12 executes the push instruction that pushes a data entry onto the hardware stack 26 (S102). This data entry is pushed onto the hardware stack 26 over the data bus 24.
The interrupt controller 18 monitors the number of free entries of the hardware stack 26 to check if the number of free entries has reached the number of push-trigger entries, step S104. If it is found as a result of this checking that the number of free entries of the hardware stack 26 has reached the number of push-trigger entries, that is, one entry of data, control is then passed to step S106. Otherwise, control is passed to step S114 to terminate the push instruction.
In the illustrative embodiment, if data entries are stored in the areas H1, H2, H3 and H4 of the hardware stack 26 and no data are stored in the area H5 in step S104 as shown in
Next, in step S106, the interrupt controller 18 generates a push interrupt, inhibits an external interrupt, and sets the external interrupt permission register to “0”. After that, the push interrupt is sent to the main controller 12 over the control bus 22.
In response to the push interrupt, the main controller 12 starts push interrupt processing. When control transfers to a function of executing the push interrupt processing, the return address is pushed onto the hardware stack 26 and, as shown in
Well, when the push interrupt processing is executed, the data entry stored at the bottom of the hardware stack 26 is saved into the software stack 28 (S108). Specifically, as shown in
When a data entry is pushed onto the software stack 28 as described above, it is necessary to prepare for a pop interrupt that will be generated later when the number of free entries of the hardware stack 26 reaches the number of pop-trigger entries as the processing progresses. Therefore, the main controller 12 causes the interrupt controller 18 to set “1” in the pop interrupt register (S110).
After that, when the push interrupt processing executed by the main controller 12 is terminated, the return address is popped from the hardware stack 26 and, as shown in
In the following, another operation of the stack controller 10 in this embodiment will be described with reference to the control flow shown in
In this example, the hardware stack 26 and the software stack 28 shown in
At this time, the interrupt controller 18 checks if the number of free entries of the hardware stack 26, which is being monitored, has reached the number of pop-trigger entries (S204). If it is found, as the result of this checking, that the number of free entries of the hardware stack 26 has reached the number of pop-trigger entries, that is, three entries of data are free, control is then passed to step S206. Otherwise, control is passed to S218 to terminate the pop instruction.
In step S204 in this embodiment, if the areas H1 and H2 of the hardware stack 26 store data entries and the areas H3, H4 and H5 store no data entry as shown in
Next, in step S206, the interrupt controller 18 checks if a pop interrupt is permitted, namely, if the pop interrupt permission register contains “1” or “0”. If the pop interrupt permission register contains “1”, control is passed to step S208, and otherwise control is passed to step S218 to terminate the pop instruction.
In step S208, the interrupt controller 18 generates a pop interrupt, inhibits an external interrupt, and sets the external interrupt permission register to “0”. After that, the pop interrupt is sent to the main controller 12 over the control bus 22.
In response to the pop interrupt, the main controller 12 starts pop interrupt processing. When control transfers to a function of executing the pop interrupt processing, the return address is pushed onto the hardware stack 26 and, as shown in
Next, during the pop interrupt processing, the data entry stored at the top of the software stack 28 is restored to the bottom of the hardware stack 26 (S210). Specifically, the data entry stored in the area S1 is popped and is stored in the area H4 as shown in
In addition, the number of free entries of the software stack 28 is checked during the pop interrupt processing (S212). If the number of free entries of the software stack 28 is five, that is, if no data entry is stored, control is passed to step S214. If one or more entries of data items are stored in the software stack 28, control is passed to step S216.
In step S214, the main controller 12 causes the interrupt controller 18 to inhibit the pop interrupt and to set the pop interrupt register to “0”, and then passes control to step S216. The reason for inhibiting the pop interrupt as described above is that the pop interrupt becomes useless in this case. This is because, even if the main controller 12 executes pop interrupt processing when the number of free entries of the hardware stack 26 has reached the number of pop-trigger entries, no data can be retrieved from the software stack 28 when no data entry is stored in the software stack 28.
The main controller 12 terminates the pop interrupt processing and pops the return address from the hardware stack 26 as described above. As a result, the number of free entries of the hardware stack 26 becomes two, that is, the areas H4 and H5, as shown in
The operation of the stack controller 10 in this embodiment under another condition will be described by referring also to the flowchart,
In this case, in steps S102 and S104 in
In step S106,
Data stored at the bottom of the hardware stack 26 are saved into the software stack 28 in step S108,
In this case, data are sequentially retrieved from the bottom of the hardware stack 26 and are pushed onto the software stack 28. The data with index number “1”, which were stored in the area H5 of the hardware stack 26 before the push interrupt processing, are stored in the area S2 of the software stack 28, and the data with index number 2, which were stored in the area H4, are stored in the area S1. In this way, the two entries of data are saved in the areas S1 and S2 of the software stack 28.
The main controller 12 terminates the push interrupt processing after S110,
In step S112 and S114,
Now, the operation of the stack controller 10 in this embodiment under another condition will be described with reference also to the flowchart,
In this case, in steps S202, S204 and S206,
In step S208,
Data stored at the top of the software stack 28 are restored to the bottom of the hardware stack 26 in step S210,
In this case, data are sequentially popped from the software stack 28 and are stored at the bottom of the hardware stack 26. The data with index number 2, which were stored in the area S1 of the software stack 28 before the pop interrupt processing, are stored in the area H2 of the hardware stack 26, and the data with index number “1”, which were stored in the area S2, will be stored in the area H3. In this way, the two entries of data are restored from the software stack 28 to the areas H2 and H3.
In steps S212 and S214,
In steps S216 and S218,
As described above, the stack controller according to the present invention can process data in the stacks according to various numbers of saving and restoring entries. Preferably, the minimum of the number of saving entries and the number of restoring entries required is one, and the maximum is an optimum value calculated by the expression
(maximum number of data entries of hardware stack−1)/2
with its decimal places rounded off. Although the number of saving entries and the number of restoring entries may exceed this optimum value, it is desirable that the number of saving or restoring entries be equal to or smaller than the calculated value in order to prolong the period to the next push interrupt or the next pop interrupt. If the number of restoring entries is larger than this optimum value, then the number of pop-trigger entries may be the maximum number of data entries of the hardware stack. Specifically, when data are popped from the hardware stack and the number of stored data entries has decreased to “0”, a pop interrupt may be generated.
If data are not saved from or restored to the stacks very frequently, a smaller number of saving entries and a smaller number of restoring entries may be used for the stack controller to save the cycles required for data movement. On the other hand, if a smaller number of saving entries and a smaller number of restoring entries are used for the stack controller when subroutines are called repeatedly and the hardware stack is almost used up, for example, when control is passed to an inner level of nested subroutines, a push interrupt and a pop interrupt would occur frequently with the result that the performance is decreased. In such a case, the number of saving entries and the number of restoring entries may preferably be increased.
For example, assume that a program sequence such as the one 40 shown in
It is also possible for the stack controller according to the present invention to have a function of determining the optimum number of saving entries for the push interrupt processing and the optimum number of restoring entries for the pop interrupt processing. A program for determining the number of saving/restoring entries can be stored in the ROM 14 for execution by the main controller 12. In this case, when executing the push interrupt processing and the pop interrupt processing, the main controller 12 reads out the number of saving/restoring entries stored in a memory such as the RAM 20.
The main controller 12 also keeps running the program sequence for determining the number of saving/restoring entries, or executes the program sequence at a predetermined interval, to determine the optimum number of saving/restoring entries for the current processing and stores the number thus determined into the memory. This allows the main controller 12 to execute the push interrupt processing and the pop interrupt processing always using the optimum number of saving/restoring entries.
For example, the program sequence for determining the number of saving/restoring entries may be designed to determine the optimum number of saving/restoring entries according to the frequency at which data are saved to and restored from the stack. When data are saved to and restored from the stack frequently, the numbers of saving entries and restoring entries are increased to improve the performance. Conversely, when data are saved to or restored from the stack not very frequently, the numbers of saving entries and restoring entries are decreased to save the cycles required for data movement.
It will now be described how the stack controller 10, which is controlled by the program sequence for determining the number of saving/restoring entries, executes the push interrupt processing and the pop interrupt processing according to the frequency of saving, with reference to the processing of data in the hardware stack 26 and the software stack 28 shown in
First, as shown in
Next, as shown in
At this time, because the number of free entries of the hardware stack 26 has reached the number of push-trigger entries, a push interrupt is generated. The data at the bottom of the hardware stack 26 are saved for the number of saving entries, that is, one entry of data. As shown in
If the program sequence for determining the number of saving/restoring entries detects that a push interrupt is generated for one entry of data continuously for a predetermined number of times, the program increases the number of saving entries and the number of restoring entries, for example, to two and sets the number of pop-trigger entries to five.
Later, as the operation progresses, the data are popped from the hardware stack 26 as shown in
In this case, because the number of free entries of the hardware stack 26 is the number of pop-trigger entries, a pop interrupt is generated to restore data from the software stack 28 for the number of restoring entries, i.e. two. As shown in
Later, as the operation progresses, new data are pushed onto the hardware stack 26 as shown in
In this case, since the number of free entries of the hardware stack 26 is the number of push-trigger entries, a push interrupt is generated to save data from bottom of the hardware stack 26 for the number of saving entries, i.e. two. As shown in
As described above, the program sequence for determining the number of saving/restoring entries, which is used by the stack controller, can adjust the number of saving/restoring entries to the optimum number of saving/restoring entries according to the frequency of saving and restoring, namely, according to the frequency of a push interrupt and a pop interrupt. In this case, the number of push-trigger entries and the number of pop-trigger entries may also be adjusted.
Although the program sequence for determining the number of saving/restoring entries uses the predetermined number of continuous push interrupts as the frequency condition in the instant embodiment, the frequency condition may also be an interval of time to the next push interrupt or the number of program sequences processed during the interval of time. The program sequence for determining the number of saving/restoring entries may also be designed to set the frequency condition, not for the push interrupt but the pop interrupt. The program sequence for determining the number of saving/restoring entries may also determine the optimum number of saving/restoring entries according to the location where the program is executed.
The illustrative embodiment has primarily been described with the stack controller according to the present invention applied to a microprocessor. Alternatively to a microprocessor, the stack controller and its control method according to the present invention may effectively be used in a data processing device, such as a micro-controller and a digital signal processor (DSP), where program sequences are stored and instructions are serially executed.
The entire disclosure of Japanese patent application No. 2005-131343 filed on Apr. 28, 2005, including the specification, claims, accompanying drawings and abstract of the disclosure is incorporated herein by reference in its entirety.
While the present invention has been described with reference to the particular illustrative embodiment, it is not to be restricted by the embodiment. It is to be appreciated that those skilled in the art can change or modify the embodiment without departing from the scope and spirit of the present invention.
Number | Date | Country | Kind |
---|---|---|---|
2005-131343 | Apr 2005 | JP | national |