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;/**   Thread                                                              */
18;/**                                                                       */
19;/**************************************************************************/
20;/**************************************************************************/
21;
22;
23;#define TX_SOURCE_CODE
24;
25;
26;/* Include necessary system files.  */
27;
28;#include "tx_api.h"
29;#include "tx_thread.h"
30;
31;
32    .SECTION    P,CODE
33;/**************************************************************************/
34;/*                                                                        */
35;/*  FUNCTION                                               RELEASE        */
36;/*                                                                        */
37;/*    _tx_thread_stack_build                               RXv1/CCRX      */
38;/*                                                           6.1.11       */
39;/*  AUTHOR                                                                */
40;/*                                                                        */
41;/*    William E. Lamie, Microsoft Corporation                             */
42;/*                                                                        */
43;/*  DESCRIPTION                                                           */
44;/*                                                                        */
45;/*    This function builds a stack frame on the supplied thread's stack.  */
46;/*    The stack frame results in a fake interrupt return to the supplied  */
47;/*    function pointer.                                                   */
48;/*                                                                        */
49;/*  INPUT                                                                 */
50;/*                                                                        */
51;/*    thread_ptr                            Pointer to thread control blk */
52;/*    function_ptr                          Pointer to return function    */
53;/*                                                                        */
54;/*  OUTPUT                                                                */
55;/*                                                                        */
56;/*    None                                                                */
57;/*                                                                        */
58;/*  CALLS                                                                 */
59;/*                                                                        */
60;/*    None                                                                */
61;/*                                                                        */
62;/*  CALLED BY                                                             */
63;/*                                                                        */
64;/*    _tx_thread_create                     Create thread service         */
65;/*                                                                        */
66;/*  RELEASE HISTORY                                                       */
67;/*                                                                        */
68;/*    DATE              NAME                      DESCRIPTION             */
69;/*                                                                        */
70;/*  08-02-2021     William E. Lamie         Initial Version 6.1.8         */
71;/*  10-15-2021     William E. Lamie         Modified comment(s), and      */
72;/*                                            removed unnecessary stack   */
73;/*                                            type placement,             */
74;/*                                            resulting in version 6.1.9  */
75;/*  01-31-2022     William E. Lamie         Modified comment(s),          */
76;/*                                            resulting in version 6.1.10 */
77;/*  04-25-2022     William E. Lamie         Modified comment(s),          */
78;/*                                            resulting in version 6.1.11 */
79;/*                                                                        */
80;/**************************************************************************/
81;VOID   _tx_thread_stack_build(TX_THREAD *thread_ptr, VOID (*function_ptr)(VOID))
82;{
83    .GLB __tx_thread_stack_build
84__tx_thread_stack_build:
85;
86;
87;    /* Build an interrupt frame.  The form of the fake interrupt stack
88;       on the Renesas RX should look like the following after it is built:
89;
90;  Stack Top:           ACC0
91;                       R6
92;                       R7
93;                       R8
94;                       R9
95;                       R10
96;                       R11
97;                       R12
98;                       R13
99;                       R14
100;                       R15
101;                       R3
102;                       R4
103;                       R5
104;                       R1
105;                       R2
106;                       PC
107;                       PSW
108
109;
110;    Stack Bottom: (higher memory address)  */
111;
112    MOV.L   16[R1],R3                            ; Pickup end of stack area
113    BCLR    #0, R3                               ; Mask for 4-byte alignment
114    BCLR    #1, R3
115;
116;    /* Build the stack frame.  */
117;
118    MOV.L #30000h, R4
119    MOV.L R4, [-R3]                              ; Initial PSW (SVC mode, U flag set)
120    MOV.L R2, [-R3]                              ; Initial PC
121    MOV.L #0, R4
122    MOV.L R4,[-R3]                               ; Initial R2 ...
123    MOV.L R4,[-R3]                               ; Initial R1 ...
124    MOV.L R4,[-R3]                               ; Initial R5 ...
125    MOV.L R4,[-R3]                               ; Initial R4 ...
126    MOV.L R4,[-R3]                               ; Initial R3 ...
127    MOV.L R4,[-R3]                               ; Initial R15 ...
128    MOV.L R4,[-R3]                               ; Initial R14 ...
129    MOV.L R4,[-R3]                               ; Initial R13 ...
130    MOV.L R4,[-R3]                               ; Initial R12 ...
131    MOV.L R4,[-R3]                               ; Initial R11 ...
132    MOV.L R4,[-R3]                               ; Initial R10 ...
133    MOV.L R4,[-R3]                               ; Initial R9 ...
134    MOV.L R4,[-R3]                               ; Initial R8 ...
135    MOV.L R4,[-R3]                               ; Initial R7 ...
136    MOV.L R4,[-R3]                               ; Initial R6 ...
137
138    MOV.L R4,[-R3]                               ; Accumulator 0
139    MOV.L R4,[-R3]
140
141;    /* Setup stack pointer.  */
142;    thread_ptr -> tx_thread_stack_ptr =  R1;
143    MOV.L R3, 8[R1]
144                                                 ; Store initial SP in thread control block
145    RTS
146
147;}
148    .END
149
150