1;/***************************************************************************
2; * Copyright (c) 2024 Microsoft Corporation
3; *
4; * This program and the accompanying materials are made available under the
5; * terms of the MIT License which is available at
6; * https://opensource.org/licenses/MIT.
7; *
8; * SPDX-License-Identifier: MIT
9; **************************************************************************/
10;
11;
12;/**************************************************************************/
13;/**************************************************************************/
14;/**                                                                       */
15;/** ThreadX Component                                                     */
16;/**                                                                       */
17;/**   Timer                                                               */
18;/**                                                                       */
19;/**************************************************************************/
20;/**************************************************************************/
21
22    .equ        SWI0, 872E0H
23    .global     __tx_timer_time_slice
24    .global     __tx_timer_system_clock
25    .global     __tx_timer_current_ptr
26    .global     __tx_timer_list_start
27    .global     __tx_timer_list_end
28    .global     __tx_timer_expired_time_slice
29    .global     __tx_timer_expired
30    .global     __tx_timer_expiration_process
31    .global     __tx_thread_context_save
32    .global     __tx_thread_time_slice
33    .global     __tx_thread_context_restore
34    .global     __tx_thread_preempt_disable
35    .global     __tx_thread_execute_ptr
36    .global     __tx_thread_current_ptr
37
38    .SECTION    P,CODE
39;/**************************************************************************/
40;/*                                                                        */
41;/*  FUNCTION                                               RELEASE        */
42;/*                                                                        */
43;/*    _tx_timer_interrupt                                  RXv1/GNURX     */
44;/*                                                           6.1.11       */
45;/*  AUTHOR                                                                */
46;/*                                                                        */
47;/*    William E. Lamie, Microsoft Corporation                             */
48;/*                                                                        */
49;/*  DESCRIPTION                                                           */
50;/*                                                                        */
51;/*    This function processes the hardware timer interrupt.  This         */
52;/*    processing includes incrementing the system clock and checking for  */
53;/*    time slice and/or timer expiration.  If either is found, the        */
54;/*    interrupt context save/restore functions are called along with the  */
55;/*    expiration functions.                                               */
56;/*                                                                        */
57;/*  INPUT                                                                 */
58;/*                                                                        */
59;/*    None                                                                */
60;/*                                                                        */
61;/*  OUTPUT                                                                */
62;/*                                                                        */
63;/*    None                                                                */
64;/*                                                                        */
65;/*  CALLS                                                                 */
66;/*                                                                        */
67;/*    _tx_thread_context_save               Save interrupted context      */
68;/*    _tx_timer_expiration_process          Timer expiration processing   */
69;/*    _tx_thread_time_slice                 Time slice interrupted thread */
70;/*    _tx_thread_context_restore            Restore interrupted context   */
71;/*                                                                        */
72;/*  CALLED BY                                                             */
73;/*                                                                        */
74;/*    interrupt vector                                                    */
75;/*                                                                        */
76;/*  RELEASE HISTORY                                                       */
77;/*                                                                        */
78;/*    DATE              NAME                      DESCRIPTION             */
79;/*                                                                        */
80;/*  08-02-2021     William E. Lamie         Initial Version 6.1.8         */
81;/*  10-15-2021     William E. Lamie         Modified comment(s),          */
82;/*                                            resulting in version 6.1.9  */
83;/*  01-31-2022     William E. Lamie         Modified comment(s), and      */
84;/*                                            added missing thread        */
85;/*                                            preemption logic,           */
86;/*                                            resulting in version 6.1.10 */
87;/*  04-25-2022     William E. Lamie         Modified comment(s),          */
88;/*                                            resulting in version 6.1.11 */
89;/*                                                                        */
90;/**************************************************************************/
91;VOID   _tx_timer_interrupt(VOID)
92;{
93    .global __tx_timer_interrupt
94__tx_timer_interrupt:
95;
96;    /* Upon entry to this routine, it is assumed that all interrupts are locked
97;       out and the stack looks like the following:
98;                            SP+4 ->   Interrupted PC
99;                            SP+8->    Interrupted SR
100;   */
101;
102;    /* Increment the system clock.  */
103;    _tx_timer_system_clock++;
104;
105    PUSHM    R14-R15
106    PUSHM    R1-R5
107
108    MOV.L    #__tx_timer_system_clock, R1        ; Pickup address of system clock
109    MOV.L    [R1], R2                            ; Pickup system clock
110    ADD      #1, R2                              ; Increment system clock
111    MOV.L    R2,[R1]                             ; Store new system clock
112;
113;    /* Test for time-slice expiration.  */
114;    if (_tx_timer_time_slice)
115;    {
116;
117    MOV.L   #__tx_timer_time_slice, R1           ; Pickup address of time slice
118    MOV.L   [R1], R2                             ; Pickup the current time slice
119    CMP     #0, R2                               ; Is a time slice active?
120    BEQ     __tx_timer_no_time_slice             ; No, skip timer slice processing
121;
122;       /* Decrement the time_slice.  */
123;       _tx_timer_time_slice--;
124;
125    SUB     #1, R2                               ; Decrement the time-slice
126    MOV.L   R2, [R1]                             ; Store time-slice
127;
128;       /* Check for expiration.  */
129;       if (__tx_timer_time_slice == 0)
130;
131    CMP     #0, R2                               ; Has it expired?
132    BNE     __tx_timer_no_time_slice             ; No, time-slice has not expired
133;
134;       /* Set the time-slice expired flag.  */
135;       _tx_timer_expired_time_slice =  TX_TRUE;
136;
137    MOV.L   #__tx_timer_expired_time_slice, R1   ; Pickup address of expired time-slice
138    MOV.L   #1, R2                               ; Build expired value
139    MOV.L   R2, [R1]                             ; Set expired time slice variable
140;    }
141;
142__tx_timer_no_time_slice:
143;
144;    /* Test for timer expiration.  */
145;    if (*_tx_timer_current_ptr)
146;    {
147;
148    MOV.L   #__tx_timer_current_ptr, R1          ; Pickup address of current timer ptr
149    MOV.L   [R1], R2                             ; Pickup current pointer
150    MOV.L   [R2+], R1                            ; pickup timer list entry, _tx_timer_current_ptr++
151    CMP     #0, R1                               ; Is timer pointer NULL?
152    BEQ     __tx_timer_no_timer                  ; Yes, no timer has expired
153
154;
155;        /* Set expiration flag.  */
156;        _tx_timer_expired =  TX_TRUE;
157;
158    MOV.L   #__tx_timer_expired,R2               ; Build address of expired flag
159    MOV.L   #1, R1                               ; Build expired value
160    MOV.L   R1, [R2]
161    BRA     __tx_timer_done                      ; Finished with timer processing
162;
163;    }
164;    else
165;    {
166__tx_timer_no_timer:
167;
168;        /* No timer expired, increment the timer pointer.  */
169;        _tx_timer_current_ptr++;
170;
171;       /* R2 already contains __tx_timer_current_ptr++ */
172;
173;        /* Check for wrap-around.  */
174;        if (_tx_timer_current_ptr == _tx_timer_list_end)
175;
176    MOV.L   #__tx_timer_list_end, R1             ; Pickup the timer list end ptr
177    MOV.L   [R1], R1                             ; Pickup actual timer list end
178    CMP     R1, R2                               ; Are we at list end?
179    BNE     __tx_timer_skip_wrap                 ; No, don't move pointer to the
180                                                 ;   top of the list
181;
182;            /* Wrap to beginning of list.  */
183;            _tx_timer_current_ptr =  _tx_timer_list_start;
184;
185    MOV.L   #__tx_timer_list_start, R2           ; Pickup the timer list start ptr
186    MOV.L   [R2], R2                             ; Pickup the start of the list
187;    }
188;
189__tx_timer_skip_wrap:
190    MOV.L   #__tx_timer_current_ptr,R1
191    MOV.L   R2, [R1]                             ; Store in updated pointer in  _tx_timer_current_ptr
192
193__tx_timer_done:
194;
195;    /* See if anything has expired.  */
196;    if ((_tx_timer_expired_time_slice) || (_tx_timer_expired))
197;    {
198;
199    MOV.L  #__tx_timer_expired_time_slice, R1    ; Pickup expired time slice addr
200    MOV.L  [R1], R1                              ; Pickup expired time slice
201    MOV.L   #__tx_timer_expired, R2              ; Pickup expired timer flag address
202    MOV.L   [R2], R2                             ; Pickup actual flag
203    OR      R1, R2                               ; Or flags together
204    BEQ     __tx_timer_nothing_expired           ; If Z set, nothing has expired
205
206__tx_something_expired:
207;    /* Did a timer expire?  */
208;    if (_tx_timer_expired)
209;    {
210    MOV.L   #__tx_timer_expired,R1               ; Pickup expired flag address
211    MOV.L   [R1], R1                             ; Pickup expired flag
212    CMP     #0,R1                                ; Is the expired timer flag set?
213    BEQ     __tx_timer_dont_activate             ; No, skip timer activation
214;
215;        /* Process timer expiration.  */
216;        _tx_timer_expiration_process();
217;
218    BSR    __tx_timer_expiration_process         ; Call the timer expiration handling routine
219;
220;    }
221__tx_timer_dont_activate:
222;
223;    /* Did time slice expire?  */
224;    if (_tx_timer_expired_time_slice)
225;    {
226;
227    MOV.L   #__tx_timer_expired_time_slice, R1   ; Pickup time-slice expired flag addr
228    MOV.L   [R1], R1                             ; Pickup actual flag
229    CMP     #0,R1                                ; Has time-slice expired?
230    BEQ      __tx_timer_not_ts_expiration        ; No, skip time-slice expiration
231;
232;        /* Time slice interrupted thread.  */
233;        _tx_thread_time_slice();
234
235    BSR     __tx_thread_time_slice               ; Call time-slice processing
236
237;   /* Check if we must trigger a context switch. */
238    MOV.L   #__tx_thread_preempt_disable, R1     ; Load prempt disable flag.
239    MOV.L  [R1], R1
240    CMP    #0, R1
241	BNE    __tx_timer_not_ts_expiration          ; Skip if prempt disabled.
242
243    MOV.L   #__tx_thread_execute_ptr, R1
244    MOV.L  [R1], R1
245    MOV.L   #__tx_thread_current_ptr, R2
246    MOV.L  [R2], R2
247    CMP    R1, R2
248    BEQ    __tx_timer_not_ts_expiration
249
250    MOV.L   #SWI0, R1
251    MOV.L   #1, [R1]
252
253
254;    }
255;
256__tx_timer_not_ts_expiration:
257
258__tx_timer_nothing_expired:
259
260    POPM R1-R5
261    POPM R14-R15
262;
263    RTS                                          ; Return to point of interrupt
264;
265;}
266
267    .end
268