Abstract:
An instruction processor suitable for use in a reduced instruction-set computer employs an instruction pipeline which performs conditional branching in a single processor cycle. The processor treats a branch condition as a normal instruction operand rather than a special case within a separate condition code register. The condition bit and the branch target address determine which instruction is to be fetched, the branch not taking effect until the next-following instruction is executed. In this manner, no replacement of the instruction which physically follows the branch instruction in the pipeline need be made, and the branch occurs within the single cycle of the pipeline allocated to it. A simple circuit implements this delayed-branch method. A computer incorporating the processor readily executes special-handling techniques for calls on subroutine, interrupts and traps.

Description:
This invention relates to method and apparatus for processing instructions for a digital computer, and more particularly, for processing branch instructions in a pipeline using only the single cycle allocated in the pipeline to the instruction without need for branch prediction or complex circuitry. 
     BACKGROUND OF THE INVENTION 
     Reduced instruction set computers (RISC) recognize the advantages of using simple decoding and the pipelined execution of instructions. Branch instructions are required in a computer to control the flow of instructions. A branch instruction in a pipelined computer will normally delay the pipeline until the instruction at the location to which the branch instruction transferred control, the &#34;branch address&#34;, is fetched. As such, these instructions impede the normal pipelined flow of instructions. Known in the prior art are elaborate techniques which delay the effect of branches, &#34;delayed branching&#34;, or predicting branches ahead of time and correcting for wrong predictions, or fetching multiple instructions until the direction of the branch is known. 
     Since most of these techniques are too complex for a RISC architecture, the delayed branch is chosen for it; the delayed branch allows RISC to always fetch the (physically) next instruction during the execution of the current instruction. As most RISCs employ pipelining of instructions, in the prior art delayed branching requires two instruction processor clock cycles to execute a branch instruction. This disrupts the instruction pipeline. Complex circuitry was introduced into the prior art to eliminate such disruption. Since branch instructions occur frequently within the instruction stream, prior art computers were slower and more complex than desired. 
     Since calls on subroutines and interrupt and trap routines similarly involve branching, the time penalties incurred in the prior art RISCs are also present for these commonly-occurring procedures. Accordingly, there is a need for an instruction processor suitable for use in a RISC which performs branches in a single cycle, and thus does not disrupt the instruction pipeline, while providing completely accurate branch prediction without requiring complex circuitry. 
     SUMMARY OF THE INVENTION 
     The instruction processor of the instant invention provides a program counter for use in a pipelined RISC in which branch instructions include a bit stored in a general-purpose register, instead of a condition code register, which allows the branch condition to be treated as a normal instruction operand, instead of as a special case within the condition code register. During the decode cycle of the branch instruction, the condition bit is fetched and the branch &#34;target&#34; address is computed by a separate relative address adder, or fetched from a register, depending on the type of branch instruction being executed. 
     At the beginning of the execution cycle of the branch instruction, the condition bit and target address control which instruction is to be fetched, the branch not taking effect until the next-following instruction is executed. A multiplexer implements this control. In this manner, no replacement of the instruction which physically follows the branch instruction in the pipeline need be made, and thus the pipeline can execute at the maximum rate without interruption. Accordingly, the branch occurs within the single cycle of the pipeline allocated to it. 
     A computer incorporating the processor of the present invention also readily executes special-handling techniques for calls on subroutines, interrupts and trap routines. 
    
    
     BRIEF DESCRIPTION OF THE DRAWINGS 
     FIG. 1 is a block diagram of an instruction processor employing a program counter unit of the present invention providing single-cycle branching; 
     FIG. 2 is a timing diagram of the pipeline stages during processing of a branch instruction by the instruction processor of the present invention; and 
     FIGS. 3a and 3b are a composite timing diagram of the pipeline stages during the processing of an interrupt routine by the instruction processor, and during return from the interrupt routine. 
    
    
     DETAILED DESCRIPTION OF THE PREFERRED EMBODIMENT 
     A program counter portion 10 of a digital processor control providing the single-cycle branch capability of the present invention is illustrated in block diagram form in FIG. 1. An instruction register, not shown in FIG. 1, contains the instruction which is currently being executed by processor 10. If this instruction calls for a branch to be executed, a branch condition will be stored in a predetermined bit position within a general-purpose register, not shown in FIG. 1, where it will be treated by the processor 10 as an instruction operand. During the instruction decode cycle of the processor 10, when a branch instruction is being decoded, the branch condition will be retrieved from the general-purpose register and used during the instruction execution cycle of processor 10. The branch condition and a target address, described below, are used to determine the location of the instruction to be executed next by processor 10. The simple logic illustrated in FIG. 1 permits an instruction pipeline to operate without disruption even in the case of an instruction calling for conditional branching. 
     The processor 10 executes branch instructions which can specify one of three branch destinations; a &#34;relative or absolute branch address&#34;, an &#34;indirect branch address&#34;, or a &#34;trap address&#34;. A fourth &#34;continue address&#34; is simply that of the next-following instruction which will be executed if a branch is not undertaken. The paths illustrated in FIG. 1 show only the data flow between the indicated elements and are capable of conducting several signals in parallel. Control signal paths are also required, as will be appreciated by those skilled in the art, but are not shown in FIG. 1 because it is well known by those skilled in the art how to effect control of the various illustrated elements. 
     With reference to FIG. 1, processor 10 includes a branch target address (BRN TGT) multiplexer/register 12 which receives from a general-purpose register file, not shown in FIG. 1, via a data path 14 an address which contains the location to which a branch is to be made by processor 10, the so-called &#34;indirect branch address&#34;. A second type of branch address, the so-called &#34;relative or absolute branch address&#34; is determined by an adder 16 which at a first input receives from the instruction register a branch displacement value via a data path 18. This value can be added to the address of the presently-executed instruction received at a second input to adder 16 via a data path 20, resulting in the relative branch address. Should an addition not be performed, only the branch displacement address will be generated by the adder 16, resulting in the absolute branch address. The address generated by adder 16 is conducted via a data path 22 to a second input of BRN TGT register/multiplexer 12. 
     The branch target address selected by BRN TGT register/multiplexer 12 as determined by a control signal generated by processor 10 in accordance with the branch instruction executed by processor 10 is generated at an output thereof and conducted via data path 24 to a first input of a multiplexer (MUX) 26 which has an output terminal connected via a data path 28 to an input terminal of an instruction cache 30. The instruction cache 30 contains a set of storage locations, 512 in the preferred embodiment, for storing sets of contiguous instructions which constitute a portion of the program being currently executed by processor 10. Application of an address at the input terminal of cache 30 causes the instruction stored at that address to be conducted to the instruction register of processor 10 to become the next instruction to be executed thereby. 
     Also receiving the address generated by MUX 26 is a program counter (PC) stack 32 comprising a decode PC register 34 to which is conducted via data path 28 the address generated by MUX 26, an execute PC register 36 to which is conducted via a data path 38 the contents of the decode PC register 34, and a store PC register 40 to which is conducted via a data path 42 the contents of the execute PC register 36. The PC stack 32 implements a four-stage instruction address pipeline, as will be described below in connection with FIG. 2. 
     Also receiving the address generated by MUX 26 is an address incrementer (+1) 44 which generates at an output the address applied to it via data path 28 incremented by 1, i.e., the continue address. A program counter (PROG CNT) register 46 receives the continue address generated by increment 44 via a data path 48 and stores this address. The continue address is generated at an output of the PROG CNT register 46 and conducted via data path 20 to the second input of adder 16 and a second input of MUX 26. 
     The MUX 26 receives a control signal based on the branch condition, described above, to determine whether the branch target address applied on data path 24 or the continue address applied on data path 20 will be applied to the instruction cache 30 to fetch the next instruction to be executed by processor 10. An instruction calling for a branch will be processed by processor 10 so that the branch does not occur until the instruction following the branch instruction is executed. In this manner, the instruction pipeline, implemented by the PC stack pipeline 32, operates without interruption even when a branch instruction enters the pipeline, since no replacement of the instruction which normally follows the branch instruction need be made in the pipeline. Accordingly, the branch occurs within the single cycle of the pipeline allocated to it, as will be described in connection with FIG. 2. 
     A four-stage pipeline is used by the processor 10 of the instant invention; an instruction fetch stage, an instruction decode stage, an instruction execution stage, and a data storage stage. The various stages of the instruction pipeline employed by processor 10 are shown in FIG. 2, illustrating the execution of a branch instruction. 
     By employing a so-called &#34;delayed branching&#34; technique, the processor 10 can effect execution of a branch instruction in a single processor cycle without requiring complex logic circuitry. The space between the vertical dashed lines in FIG. 2 corresponds to a single processor cycle, each cycle having an equal duration. Shown extending from t 0  to t 1  during the first cycle of the processor 10 is &#34;BRANCH 1&#34; in which a branch instruction is fetched from instruction cache 30 and stored in the instruction register of processor 10. Shown extending from t 1  to t 2  during the second cycle of the processor 10 &#34;BRANCH 2&#34; is the decoding of the branch instruction stored in the instruction register. The branch condition needed by the instruction is retrieved from the general-purpose register described above and the branch target address specified by the instruction is determined, as described above, and conducted on data bus 24 to the first input of MUX 26. 
     During the execution cycle of the branch instruction extending from t 2  to t 3  &#34;BRANCH 3&#34;, the condition causes processor 10 to generate a control signal which, in turn, causes MUX 26 to select either the branch target address or the continue address to be conducted to the instruction cache 30 for use in fetching the next (logical) instruction. During the storeback cycle, extending from t 3  to t 4 , the instruction (physically) following the branch instruction is in the execute stage of processor 10. 
     As shown in FIG. 2, a branch delay instruction &#34;DELAY&#34; physically follows the branch instruction and is always executed; the branch itself not occurring until after the branch delay instruction, whereupon the instruction to which the branch instruction passes control &#34;TARGET&#34; executes. In this manner, the processor 10 can always fetch the next instruction during the execution of the current instruction, i.e., operate in a pipeline mode without the need to interrupt the pipeline nor retract the fetching of an instruction. Accordingly, FIG. 2 indicates that during the second cycle &#34;DELAY 1&#34; processor 10 fetches from cache 30 the instruction physically following the branch instruction whose fetching, decoding, execution and storeback cycles were just described. This instruction thus occupies the stage in the pipeline immediately following the branch instruction which preceded it. Hence, the decoding, execution and storeback cycles for the branch delay instruction &#34;DELAY 2&#34;, &#34;DELAY 3&#34; and &#34;DELAY 4&#34; will occur during the third, fourth and fifth cycles of processor 10 as shown in FIG. 2. 
     The instruction to which control passes by virtue of the branch instruction, &#34;TARGET&#34;, will occupy the stage in the pipeline immediately following the delay instruction, as shown in FIG. 2. Thus the fetching, decoding, execution and storeback cycles for the TARGET instruction &#34;TARGET 1&#34;, &#34;TARGET 2&#34;, &#34;TARGET 3&#34;, and &#34;TARGET 4&#34;, will occur during the fourth, fifth and sixth cycles of processor 10 as shown in FIG. 2. The serial connection of the decode PC register 34, the execute PC register 36 and the store PC register 40, clocked at the intervals t 0 , t 1 , ..., t 6  implement the pipeline described above by storing the addresses of instructions associated with the corresponding pipeline stages. 
     The processor 10 of the instant invention can execute a branch which is called for by a call subroutine instruction by a modification of the delayed branching technique described above in connection with FIG. 2. The call subroutine instruction is fetched from the cache 30 and stored in the instruction register during the first cycle of processor 10 extending from t 0  to t 1  ; denoted as &#34;BRANCH 1&#34; in FIG. 2. During the second cycle of the processor 10, the call subroutine instruction, &#34;BRANCH 2&#34;, is decoded and the contents of the PROG CNT register 46 is generated on data path 20, and via MUX 26, onto data path 28 for entry into a data path pipeline, not shown in FIG. 1. During the third cycle of the processor 10 &#34;BRANCH 3&#34;, the contents of the PROG CNT register are increased by four in an arithmetic logic unit of the processor 10, not shown in FIG. 2, to establish a return address from the subroutine. During the fourth cycle of the processor 10 &#34;BRANCH 4&#34;, the return address is saved in a general-purpose register. In all other respects, the technique for executing a call subroutine instruction is as described above in connection with FIG. 2 for executing a branch instruction, where the instruction to which control passes by virtue of the call subroutine instruction, &#34;TARGET&#34;, wil be the first instruction of the subroutine. 
     As the processor 10 of the present invention is capable of servicing interrupts and traps, special consideration must be given to the occurrence of an interrupt or trap between a branch or a call subroutine instruction and the delayed-branch instruction which follows it. In this case, the processor 10 must cause the delayed-branch instruction to be executed after return from the interrupt or trap routine, in addition to the target instruction to which control passes by virtue of the branch or call instruction. To assure this result when returning from an interrupt or trap routine, processor 10 must execute two branches: a first branch causes execution of the branch delay instruction which was pre-empted by the occurrence of the interrupt or trap, and a second branch which causes execution of the target instruction which followed the delayed-branch instruction. 
     The various stages of the instruction pipeline employed by processor 10 to effect execution of an interrupt or trap routine occurring between a branch or subroutine call instruction and the delayed-branch instruction which follows it are illustrated in FIG. 3A. The pipeline stages employed by processor 10 to effect return from the interrupt or trap routine are illustrated in FIG. 3B. With reference to FIG. 3A, the operation of processor 10 is illustrated by an interrupt occurring at time t 1 . Modifications to the latter procedure will not be described herein as they can be provided by those skilled in the art. For purposes of illustration, a shift instruction is shown as fetched from instruction cache 30 in the preceding cycle extending from t 0  to t 1 . The pipeline also contains, for purposes of illustration, a jump instruction, followed by an add instruction, followed by the shift instruction. Since the jump instruction was executed just before occurrence of the interrupt and the add and shift instructions had yet to be executed, it will be necessary for processor 10 to return from the interrupt routine and then execute the add and shift instructions. The addresses of these instructions must be saved before transfer to the interrupt routine. Accordingly, a &#34;SAVE --  PC --  JUMP&#34; instruction is indicated in FIG. 3A as fetched during the cycle extending from t 1  to t 2 , following occurrence of the interrupt. This will cause processor 10 to save the address of the branch delay instruction, namely the add instruction which was to execute during the cycle extending from t1 to t 2  following occurrence of the interrupt. The contents of the execute PC register 36 (FIG. 1) portion of the PC stack 32 will accordingly be saved. Also, the &#34;SAVE --  PC JUMP&#34; instruction will cause processor 10 to generate the contents of the BRN TGT multiplexer/register 12 onto data path 24, and via MUX 26, onto data path 28 and therefrom to instruction cache 30. These contents being the address of the first instruction of the interrupt routine. As shown in FIG. 3A, processor 10 will fetch during the cycle extending from t 2  to t 3  a &#34;SAVE --  PC&#34; instruction, which will cause processor 10 to save the addresss of the target instruction, namely, the shift instruction, which would have normally followed the add instruction. The first instruction of the interrupt routine, designated the &#34;INTERRUPT HANDLER&#34; in FIG. 3A, will then be fetched by processor 10 during the cycle extending from t 3  to t 4 . 
     The decoding and execution of the shift and add instructions, respectively, are accordingly aborted as indicated in FIG. 3A by the designations &#34;(ADD)&#34; and (SHIFT)&#34; during the decoding and execution stages. During subsequent store back stages, the &#34;SAVE --  PC --  JUMP&#34; and &#34;SAVE --  PC&#34; instructions cause processor 10 to save the addresses of the add instruction and the shift instruction, as indicated in FIG. 3A. 
     With reference to FIG. 3B, the interrupt routine will complete by causing the processor 10 to fetch two jump indirect instructions, which are shown in FIG. 3B as being decoded during the cycles extending from t 0  &#39; to t 1  &#39; and t 1  &#39; to t 2  &#39;. To return from the interrupt routine then, processor 10 will perform an indirect jump via the value saved by the &#34;SAVE --  PC&#34; instruction described in connection with FIG. 3A and will accordingly fetch the add instruction from cache 30 during the cycle extending from t 1  &#39; to t 2  &#39; and will perform an indirect jump via the value saved by the &#34;SAVE --  PC --  JUMP&#34; instruction and will accordingly fetch the shift instruction from cache 30 during the cycle extending from t 2  &#39; to t 3  &#39; as shown in FIG. 3B. Thus, the processor 10 will execute these instructions in their order of occurrence in the pipeline just prior to the occurrence of the interrupt.