Patent Publication Number: US-7596681-B2

Title: Processor and processing method for reusing arbitrary sections of program code

Description:
BACKGROUND OF THE INVENTION 
   1. Field of the Invention 
   The present invention relates generally to processors, and more specifically, to a processor and processing method that direct execution to and from an arbitrary section of program code. 
   2. Background of the Invention 
   Processing systems are ubiquitous in both computing systems and embedded applications in consumer and industrial devices. Update of so-called read-only program code historically involved replacement of a read-only memory (ROM device) within the computer or other device containing the ROM code. Generally such devices are not field replaceable and return to a service center or factory is required. Masked ROMs are ROMs that are fabricated with a permanent program code and typically provide the lowest-cost and silicon area requirement for providing read-only memory within an integrated circuit. 
   More recently, electrically alterable read-only memories have been developed and techniques associated with those technologies implemented in software that permit a field modification to ROM devices, thus alleviating the need for factory service to upgrade the ROM program code. 
   However, several drawbacks are present with the use of electrically alterable memories, including, a limited number of write cycles to failure and limited fail-safe storage lifetimes. Most significantly, larger die areas and higher power requirements compared to masked ROM and the need to program the memories in conjunction with integration make the use of masked ROM preferable to electrically alterable ROM. Also, the technology requirements for electrically alterable ROM technologies is incompatible with some fabrication technologies or otherwise complicates the fabrication process, thus raising the cost when the electrically alterable ROM is included on a die with other circuits. 
   When program code in ROM is updated, generally only small portions of the full program code set are updated. Therefore, where the behavior of the ROM code is well-bounded, it is possible to use portions of the ROM code from an updated program stored in random access memory (RAM). However, the bounding requirement is that no path through the ROM code can possibly cause execution of any program code instruction that must be replaced in the updated version, severely limiting the flexibility of such ROM code re-use. 
   Existing processor architectures provide for directing execution of a processor to another memory location not consecutive with the last instruction, in the form of a call or jump (branch) instruction. The distinction between a call or jump instruction is that the call instruction saves the next program counter value, and upon encountering a return instruction, re-directs execution to the stored program counter value. A jump instruction is absolute in that there is no corresponding return instruction. In either case, unless a return instruction is already available at the end of a particular code section in ROM and unless the code in that section is bounded so as not to call or jump to other code that might execute a program instruction that is replaced in an update, existing processor architecture will not support the re-use of that particular code section. 
   Other known program instruction types provide for looping by executing a portion of program code repeatedly from a loop program instruction to an ending program instruction, with a count that may be specified within the instruction, via an operand or via a register. However, such program instructions do not provide for re-use of ROM program code, because such an instruction located in a particular memory will only loop program instructions immediately following the loop instruction in sequence. 
   Therefore, it is desirable to provide a processor and processing method for re-using arbitrary sections of program code. It is also desirable in general to extend a processor instruction set for more programming flexibility and program compactness. 
   SUMMARY OF THE INVENTION 
   The above stated objectives of re-using arbitrary sections of program code in ROM and providing a more general instruction set for programming flexibility and program compactness is provided in a processor and processing method. The method is a method of operation of the processor. 
   The processor includes an instruction decoder for decoding an instruction set that includes a specific program instruction that directs program execution to a start address specified in conjunction with the specific instruction. The processor also includes a program control circuit that receives an end address in conjunction with the specific program instruction and directs program execution to another location out of sequence with the current program instruction when the end address is reached. 
   The specific program instruction thus implements a DO &lt;start&gt; &lt;end&gt; form of program control instruction and may further include a loop counter that causes a repeat of the code between the start and end address for a repetition count. The program instruction may be of the form of a CALL &lt;start&gt; &lt;end&gt; instruction that returns program control to the instruction following the specific program instruction, or the program instruction may implement a JUMP &lt;start&gt; &lt;end&gt; &lt;next PC address&gt; program instruction that directs execution to the start address and then to the “next PC address” when the end address is reached. 
   The start, end and optional next PC address can be specified in fields of the program instruction, in registers, or as operands, and a repeat counter may be implemented in any of the forms of the instruction. 
   The foregoing and other objectives, features, and advantages of the invention will be apparent from the following, more particular, description of the preferred embodiment of the invention, as illustrated in the accompanying drawings. 

   
     BRIEF DESCRIPTION OF THE DRAWINGS 
       FIG. 1  is a block diagram depicting a processing system including a processor in accordance with an embodiment of the present invention. 
       FIG. 2  is a block diagram depicting details of program control  16  within processor  10  of  FIG. 1 . 
       FIG. 3  is an illustration depicting program flow in a processing method in accordance with an embodiment of the present invention. 
       FIG. 4  is a flowchart illustrating a processing method in accordance with an embodiment of the present invention. 
   

   DESCRIPTION OF ILLUSTRATIVE EMBODIMENT 
   The present invention encompasses a processor and processing method that provide program control for the re-use of arbitrary code sections. Arbitrary, in the context of the present invention, means program code with an arbitrary start and end address in memory. However, it is still necessary to ensure that the program code sections being re-used do not transfer control to those portions memory that are “blacked-out” by the application of the techniques described herein and enabled by the processor of the present invention. 
   The techniques of the present invention can be applied for “code patching” a read-only memory (ROM) via another code space in RAM that includes instructions as disclosed below, or in general to provide for the flexible re-use of code in any memory. What would ordinarily be a fixed subroutine bounded by one or more entry points and a return instruction can be used with multiple exit points specified at entry. Further, in size-critical code applications, the techniques of the present invention can be used to eliminate the need for the placement of return instructions in the program code. 
   Referring now to  FIG. 1 , a processor  10  in accordance with an embodiment of the present invention is shown. The processor is of the form of a microprocessor or microcontroller suitable for use in embedded applications, such as digital signal processing cores included in larger “system on a chip” type integrated circuits that include other input/output and signal processing functions. In particular, the depicted processor  10  includes a ROM  12  and a random access memory (RAM)/register bank  14  coupled via an internal bus to processing units  18 , a program control circuit  16  and external bus interfaces  19 . Processor  10  is an exemplary device that embodies one or more of the techniques of the present invention. However, the techniques of the present invention can be used in any application with any type of processor that includes one or more of the improvement described herein. 
   Program control  16  directs the execution of program instructions that cause processing units  18  to perform various tasks, such as floating-point and fixed-point operations, the loading of values to and from external bus interfaces  19 , the loading and storing of program instructions and data to and from RAM  14  and the loading of program instructions from ROM  12 . In particular, program control  16  is responsible for the implementation of branching and looping instructions that are loaded from RAM  14  and ROM  12  when encountered in the sequence of execution of program code that is generally sequential in memory until a branch or looping instruction is encountered in the program instruction sequence. 
   Referring now to  FIG. 2 , details of program control  16  pertinent to the invention are illustrated in a block diagram. A program counter  21  provides the address for loading the next program instruction for execution, and the value loaded is provided by an output of a multiplexer (MUX)  26 . The particular value loaded into the program counter is determined in part by control logic that determines when a code section specified by a program instruction in accordance with the processing method of the present invention has completed execution. The default selected input of the multiplexer is the output of an increment  29  of the value stored in program counter  21  and may logically be provided by means other than multiplexer  26 , such as via a microcode state machine or a self-adder register used to implement program counter  21 . However, no matter what form of logic is used for a default increment, the increment is overridden when at least one of the following events occur: an instruction that redirects program execution is received; or the end address of a program sequence having a specified end address is reached as determined by an address comparator  25 . 
   Each of the above events occur in existing processors and program code; however, the present invention is unique in that the redirect (start) and end address events both are set up to occur in response to the decode of a single program instruction. The end address can be specified as an offset from the start address, or as an absolute value. Instruction decoder  20  receives program instructions from an instruction fetch  22  via an address specified by program counter  21 . In the present invention, when a specific program instruction is received for which a start and end address is specified, either by fields within the instruction, operands, or register values within RAM/register bank  14 , program execution is redirected to the start address, and the end address is loaded into end address latch  24 . End address latch  24  is optional if the end address is held in an accessible stack location that can provide a value for comparison with the current program counter value during program execution. 
   The start address is loaded from instruction decoder (or from RAM  14  as indicated by the dashed line) into a start address latch  27  and then loaded into program counter  21  via multiplexer  26  under control provided by instruction decoder  20 . Start address latch  27  is optional if the start address only has to be loaded once, or if the start address is retained in an accessible stack location, but is otherwise required for looping instructions that also specify a repetition count that is loaded into a rep counter  28 . Rep counter  28  may be alternatively provided from an accessible stack location, wherein the value of the field that specifies the repetition count is altered directly by control logic or microcode in order to effect the counting required for the Do instruction. 
   Rep counter  28  further controls whether the next value of program counter  21  is provided from start address latch  27  causing a repetition or from either a stored program counter that is pushed on a stack  14 A when the specific program instruction redirects execution, or from another address value specified in conjunction with the specific program instruction. In the first case, the implementation is that of a Call &lt;start address&gt; &lt;end address&gt; or Do &lt;start address&gt; &lt;end address&gt;, N type instruction that pushes the incremented program counter value, but rather than responding to a return instruction corresponding to the call instruction, address comparator  25  determines when to pop the stored incremented program counter value. In the second case, an instruction of the form JUMP &lt;start&gt; &lt;end&gt; &lt;next address&gt; provides for loading an arbitrary value into program counter after a single pass through or multiple repetitions of the program code extending from the start address to the end address is performed. Table I below illustrates the next program counter value selected by multiplexer  26 . 
   
     
       
         
             
             
             
             
           
             
                 
               TABLE I 
             
             
                 
                 
             
             
                 
                 
                 
               Override Behavior 
             
             
                 
               Condition 
               Next PC value 
               at addr = end 
             
             
                 
                 
             
           
          
             
                 
               Received non- 
               PC + 1 
               if within Called 
             
             
                 
               program control 
                 
               section PC = stored 
             
             
                 
               instruction 
                 
               PC stack value 
             
             
                 
               Received Call 
               Start Address 
             
             
                 
               &lt;start&gt;&lt;end&gt; 
             
             
                 
               Received Do 
               Start Address 
               if within Do section, 
             
             
                 
               &lt;start&gt;&lt;end&gt;, N 
                 
               PC = Start Address 
             
             
                 
                 
                 
               count &lt; N 
             
             
                 
               Received Jump 
               Start Address 
               if within Jump section, 
             
             
                 
               &lt;start&gt;&lt;end&gt;&lt;next&gt; 
                 
               next address or Start 
             
             
                 
                 
                 
               address if count &lt; reps 
             
             
                 
                 
             
          
         
       
     
   
   Stack  14 A, in order to provide for the nesting of the program instructions, as well as providing for nesting of the above program instructions with ordinary call and do instructions, needs to not only preserve the program counter value for the next instruction after the Call, Do or Jump instructions described above, but must also preserve the end address and current repetition count/start address if repetition control is present. Also, for the Jump &lt;start&gt;&lt;end&gt;&lt;next&gt; instruction, the &lt;next&gt; value must be retained. 
   As mentioned above, the start address may be provided directly to address comparator  25  from a current location in stack  14 A, which is generally a dedicated stack for handling the values needed to save the execution states associated with the execution of program instructions of the present invention and with an additional field for identifying a program instruction type, may also store the values needed for saving the states associated with ordinary call and/or do instructions. A single stack location wide enough to hold all the fields required for any individual one of the instructions plus the type field is sufficient. 
   As an alternative to a stack having an instruction type field, stack  14 A may be composed of multiple stacks, as many as one for each type of instruction. In a multi-stack implementation, separate address comparators are used to determine when the program counter reaches, for example, the end of an ordinary Do instruction or the end of a Jump instruction having a specified next address in accordance with an embodiment of the present invention. The stack used to control the nesting of an ordinary Do instruction can also be used to store the values associated with the present invention&#39;s Do &lt;start&gt;&lt;end&gt;, N instruction, as an ordinary Do stack needs to contain the next address after the Do instruction in order to direct repetition. 
   Referring now to  FIG. 3 , program control in a processor and according to a processing method in accordance with an embodiment of the invention is shown. Within RAM  14 , updated code that re-uses code within ROM  12  is depicted. A Do &lt;Start 1 &gt; &lt;End 1 &gt;, N instruction causes the incremented program counter value and the other associated instruction values such as the repetition count, start address to be pushed onto stack  14 A and program execution to transfer from RAM  14  to ROM  12  address Start  1 . When program execution reaches and completes the instruction at address End 1 , program control is re-cycled back to address Start 1  until N repetitions have occurred, at which point the pushed program counter value is popped off of stack  14 A, returning control to the program call immediately following the Do &lt;Start 1 &gt; &lt;End 1 &gt;, N in RAM  14 . 
   When program execution reaches the JMP &lt;start 2 &gt; &lt;End 2 &gt; &lt;next&gt; instruction, program control transfers to the instruction at address Start 2  in ROM  12  and execution proceeds sequentially until the instruction at address End 2  is executed, at which time program control transfers to the address specified by Next, which is illustrated as being in RAM  14 , but in some circumstances may be useful to transfer to ROM  12  when proper behavior is ensured. In order to use the techniques of the invention as illustrated, the program code between addresses Start 1  and End 1  and between addresses Start 2  and End 2  must be well-behaved, meaning that program control will not be transferred to any now invalid code (blacked-out code) in ROM  12 . Also, the code section that is entered by one of the above-described instructions should assuredly reach the end address, otherwise the instruction would operate in a manner similar to an ordinary call instruction without an appropriate corresponding return, causing possible stack corruption and possibly mis-directed program execution. 
   As illustrated, the invention provides a new and useful type of program control that makes it possible to specify a section of code in ROM for re-use, while maintaining total control over where the re-used code will cause execution to transfer, by specifying both a start and an end address and automatically re-directing program execution when the end address is reached. As explained above, current looping instructions, even when provided with a repetition count of 1 cannot perform such a function, as looping instructions do not provide for specifying an arbitrary start address, but rather commence looping at the next program counter location. Current call type instructions would require the presence of a return instruction at the proper location in the ROM code section being re-used, a circumstance that may be possible under specific conditions, but does not provide for re-use of arbitrary code sections in ROM. 
   The present invention provides for one or more of the following instruction types:
         Call &lt;start&gt;&lt;end&gt; provides for a single execution of an arbitrary code section.   Do &lt;start&gt;&lt;end&gt;, N as illustrated above, provides for repetitive execution of a code section.   Jump &lt;start&gt;&lt;end&gt;&lt;next&gt; as illustrated above provides for a single execution of an arbitrary code section with subsequent execution directed to another arbitrary address.   Jump &lt;start&gt;&lt;end&gt; N &lt;next&gt; as provides for multiple repetitions of an arbitrary code section with subsequent execution directed to another arbitrary address.
 
Also, without loss of generality, any of the above instructions can be implemented in a conditional form that tests a logical value or other condition before redirecting execution to the start address.
       

   Referring now to  FIG. 4 , a processing method in accordance with an embodiment of the present invention is depicted in a flow chart that describes the behavior of program control  16  of  FIG. 2 . First, a specific program instruction is received directing execution between a start and end address (step  40 ) and the necessary values (start/end/count/next) are pushed onto the stack. If there is an implied return address (decision  41 ), such as in the Call and Do instructions illustrated above, then the incremented program counter value is pushed onto the stack (step  42 ). Next instructions between the start and end address are executed (step  43 ) and if a repetition count was specified (decision  44 ), the count is tested to determine whether the number of repetitions have been met (decision  45 ). If the number of repetitions has not been met (decision  45 ), then flow returns to step  43 . 
   If no repetition was specified (decision  44 ) or once the number of repetitions is complete (decision  45 ), then the flow proceeds to step  46 . If a return point was implied (step  46 ), the stack is decremented past the stored instruction values (start/end/count/next) and then the previously stored program counter value is popped off the stack (step  47 ) and execution returns to the instruction following the instruction received in step  40 . Otherwise the stack is decremented and program execution jumps to the “next address” specified in conjunction with the program instruction (step  48 ). 
   In the implementation of an embodiment of the invention, the address comparisons can be implemented at adjacent addresses, rather than the addresses specified in the description above, in order to perform equivalent program control functions. Such design variations are within the scope of the present invention. For example, the end address could specify the address after the code section and program execution redirected upon program counter increment before execution of the program instruction at the incremented address. Similarly, addresses can be specified in a relative manner with respect to fixed locations in the ROM and RAM memory space or specified in conjunction with an absolute segment register, for example. 
   While the invention has been particularly shown and described with reference to the preferred embodiments thereof, it will be understood by those skilled in the art that the foregoing and other changes in form, and details may be made therein without departing from the spirit and scope of the invention.