Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- TMS320C2x DSP - context switching, commonly required when processing a subroutine http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
      Context switching, commonly required when processing a subroutine
      call or interrupt, may be quite extensive or simple, depending on
      the system requirements. On the TMS320C2x, the program counter is
      stored automatically on the hardware stack. If there is any other
      important information in any of the other TMS320C2x registers, such
      as the status or auxiliary registers, these must be saved by a
      software command. A stack in data memory, identified by an auxiliary
      register, is useful for storing the machine state when processing
      interrupts.

      Examples of saving and restoring the state of the TMS320C25 follow.
      Auxiliary register 7 (AR7) is used in both examples as the stack
      pointer. As the stack grows, it expands into lower memory addresses.
      The registers saved are the status registers (ST0 and ST1),
      accumulator (ACCH and ACCL), product register (PR), temporary
      register (TR), all eight levels of the hardware stack, and the
      auxiliary registers (AR0 through AR6). The routines are protected
      against interrupts, allowing context switches to be nested. This is
      accomplished by the use of the MAR *- and the MAR *+ instructions
      at the beginning of the context save and the context restore
      routines, respectively. Note that the last instruction of the
      context save decrements AR7 while the context restore is completed
      with an additional increment of AR7. This prevents loss of data if
      a context save or restore routine is interrupted.


      .title        'CONTEXT SAVE'
      .def   SAVE
*
* CONTEXT SAVE ON SUBROUTINE CALL OR INTERRUPT
*   ASSUME AR7 IS THE STACK POINTER AND AR7=128
*   ASSUME CURRENT ARP=2
*
SAVE  LARP  AR7          ; 7 --> ARP, 2 --> ARB, AR7 = 128
      MAR   *-           ;                       AR7 = 127
*   SAVE THE STATUS REGISTERS
      SST1  *-           ; ST1    --> (127),     AR7 = 126
      SST   *-           ; ST0    --> (126),     AR7 = 125
*   SAVE THE ACCUMULATOR
      SACH  *-           ; ACCH     --> (125),   AR7 = 124
      SACL  *-           ; ACCL     --> (124),   AR7 = 123
*   SAVE THE P-REGISTER
      SPM   0            ; NO SHIFT ON PR OUTPUT
      SPH   *-           ; PRH    --> (123),     AR7 = 122
      SPL   *-           ; PRL    --> (122),     AR7 = 121
*   SAVE THE T-REGISTER
      MPYK  1            ; PREG = TREG
      SPL   *-           ; TR     --> (121),     AR7 = 120
*   SAVE ALL EIGHT LEVELS OF THE HARDWARE STACK
      RPTK  7
      POPD  *-           ; TOS  (8) --> (120),   AR7 = 119
*                        ; STACK(7) --> (119),   AR7 = 118
*                        ; STACK(6) --> (118),   AR7 = 117
*                        ; STACK(5) --> (117),   AR7 = 116
*                        ; STACK(4) --> (116),   AR7 = 115
*                        ; STACK(3) --> (115),   AR7 = 114
*                        ; STACK(2) --> (114),   AR7 = 113
*                        ; BOS  (1) --> (113),   AR7 = 112
*   SAVE AUXILIARY REGISTERS AR0 THROUGH AR6
      SAR   AR0,*-       ; AR0          --> (112),     AR7 = 111
      SAR   AR1,*-       ; AR0          --> (111),     AR7 = 110
      SAR   AR2,*-       ; AR0          --> (110),     AR7 = 109
      SAR   AR3,*-       ; AR0          --> (109),     AR7 = 108
      SAR   AR4,*-       ; AR0          --> (108),     AR7 = 107
      SAR   AR5,*-       ; AR0          --> (107),     AR7 = 106
      SAR   AR6,*-       ; AR0          --> (106),     AR7 = 105
*   SAVE IS COMPLETE



      .title        'CONTEXT RESTORE'
      .def   RESTOR
*
* CONTEXT RESTORE AT THE END OF A SUBROUTINE OR INTERRUPT
*   ASSUME AR7 IS THE STACK POINTER AND AR7=105
*
RESTOR      LARP  AR7    ; 7 --> ARP, ? --> ARB, AR7 = 105
      MAR   *+     ;                 AR7 = 106
*   RESTORE AUXILIARY REGISTERS AR0 THROUGH AR6
      LAR   AR6,*+       ; (106) -->  AR6,       AR7 = 107
      LAR   AR5,*+       ; (107) -->  AR5,       AR7 = 108
      LAR   AR4,*+       ; (108) -->  AR4,       AR7 = 109
      LAR   AR3,*+       ; (109) -->  AR3,       AR7 = 110
      LAR   AR2,*+       ; (110) -->  AR2,       AR7 = 111
      LAR   AR1,*+       ; (111) -->  AR1,       AR7 = 112
      LAR   AR0,*+       ; (112) -->  AR0,       AR7 = 113
*   RESTORE ALL EIGHT LEVELS OF THE HARDWARE STACK
      RPTK  7
      PSHD  *+           ; (113) -->   BOS(1),   AR7 = 114
*                        ; (114) --> STACK(2),   AR7 = 115
*                        ; (115) --> STACK(3),   AR7 = 116
*                        ; (116) --> STACK(4),   AR7 = 117
*                        ; (117) --> STACK(5),   AR7 = 118
*                        ; (118) --> STACK(6),   AR7 = 119
*                        ; (119) --> STACK(7),   AR7 = 120
*                        ; (120) -->   TOS(8),   AR7 = 121
*   NOTE THAT THE RETURN PC IS NOW ON THE TOP OF STACK FOR
*   THE RET INSTRUCTION.
*   NOTE THAT THE LOWER 16 BITS OF THE P-REGISTER MUST BE
*   LOADED VIA THE T-REGISTER AND THAT THE STACK POINTER IS
*   POINTING AT THE VALUE TO BE LOADED IN THE T-REGISTER.
*   RESTORE THE LOW P-REGISTER
      MAR   *+           ; SKIP T-REGISTER,      AR7 = 122
      LT    *-           ; (122) -->   TR,       AR7 = 121
      MPYK  1            ; (TR)  -->  PRL
*   RESTORE THE T-REGISTER
      LT    *+           ; (121) -->   TR,       AR7 = 122
      MAR   *+           ; SKIP P-REGISTER LO,   AR7 = 123
*   RESTORE THE HIGH P-REGISTER
      LPH   *+           ; (123) -->  PRH,       AR7 = 124
*   RESTORE THE ACCUMULATOR
      ZALS  *+           ; (124) --> ACCL,       AR7 = 125
      ADDH  *+           ; (125) --> ACCH,       AR7 = 126
*   RESTORE THE STATUS REGISTERS
      LST   *+           ; (126)   -->   ST0,    AR7 = 127
      LST1  *+           ; (127)   -->   ST1,    AR7 = 128
*                        ; 2 --> ARB, 2 --> ARP
*   RESTORE IS COMPLETE
      EINT               ; ENABLE INTERRUPTS
      RET                ; RETURN TO INTERRUPTED OR CALLING
*                        ; ROUTINE.

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson