1 /*---------------------------------------------------------------------------*
2   Project:  NitroSDK - OS - demos - functionCost-2
3   File:     main.c
4 
5   Copyright 2003-2008 Nintendo.  All rights reserved.
6 
7   $NoKeywords: $
8  *---------------------------------------------------------------------------*/
9 #include <nitro.h>
10 
11 void    test1(void);
12 void    test2(void);
13 void    test3(void);
14 void    test_sleep(void);
15 
16 #define MYCOSTBUFFER_SIZE 0x3000
17 #define MYSTATBUFFER_SIZE 0x300
18 
19 u32     myCostBuffer[MYCOSTBUFFER_SIZE / sizeof(u32)];
20 u32     myStatBuffer[MYSTATBUFFER_SIZE / sizeof(u32)];
21 
22 u32     myCostBuffer1[MYCOSTBUFFER_SIZE / sizeof(u32)];
23 u32     myStatBuffer1[MYSTATBUFFER_SIZE / sizeof(u32)];
24 
25 u32     myCostBuffer2[MYCOSTBUFFER_SIZE / sizeof(u32)];
26 u32     myStatBuffer2[MYSTATBUFFER_SIZE / sizeof(u32)];
27 
28 //---- thread
29 #define STACK_SIZE  1024
30 
31 void    proc1(void *arg);
32 void    proc2(void *arg);
33 
34 OSThread thread1;
35 OSThread thread2;
36 
37 u64     stack1[STACK_SIZE / sizeof(u64)];
38 u64     stack2[STACK_SIZE / sizeof(u64)];
39 
40 #define     THREAD1_PRIO    10
41 #define     THREAD2_PRIO    11
42 
43 //================================================================================
44 /*---------------------------------------------------------------------------*
45   Name:         NitroMain
46 
47   Description:  main
48 
49   Arguments:    None
50 
51   Returns:      None
52  *---------------------------------------------------------------------------*/
53 #pragma profile off
NitroMain(void)54 void NitroMain(void)
55 {
56     int     n;
57 
58     OS_Init();
59     OS_InitTick();
60     (void)OS_EnableIrq();
61 
62     //---- init thread
63     //OS_InitThread(); <- maybe done in OS_Init() already.
64     OS_CreateThread(&thread1, proc1, (void *)0x111, stack1 + STACK_SIZE / sizeof(u64), STACK_SIZE,
65                     THREAD1_PRIO);
66     OS_CreateThread(&thread2, proc2, (void *)0x222, stack2 + STACK_SIZE / sizeof(u64), STACK_SIZE,
67                     THREAD2_PRIO);
68     OS_WakeupThreadDirect(&thread1);
69     OS_WakeupThreadDirect(&thread2);
70 
71     //---- init functionCost
72     OS_InitFunctionCost(&myCostBuffer, MYCOSTBUFFER_SIZE);
73     OS_InitStatistics(&myStatBuffer, MYSTATBUFFER_SIZE);
74 
75     for (n = 0; n < 10; n++)
76     {
77         test3();
78         OS_WakeupThreadDirect(&thread2);
79     }
80 
81     //---- calculate cost
82     OS_CalcStatistics(&myStatBuffer);
83 
84 
85     //---- display all thread functionCost
86     OS_DumpStatistics(&myStatBuffer);
87     OS_DumpStatistics(&myStatBuffer1);
88     OS_DumpStatistics(&myStatBuffer2);
89 
90 
91     OS_Printf("==== Finish sample.\n");
92     OS_Terminate();
93 }
94 
95 #pragma profile reset
96 
97 //--------------------------------------------------------------------------------
98 //    proc1
99 //
proc1(void * arg)100 void proc1(void *arg)
101 {
102 #pragma unused( arg )
103 
104     //---- init functionCost
105     OS_InitFunctionCost(&myCostBuffer1, MYCOSTBUFFER_SIZE);
106     OS_InitStatistics(&myStatBuffer1, MYSTATBUFFER_SIZE);
107 
108     while (1)
109     {
110         test1();
111         test_sleep();
112 
113         //---- calculate cost
114         OS_CalcStatistics(&myStatBuffer1);
115     }
116 }
117 
118 //--------------------------------------------------------------------------------
119 //    proc2
120 //
proc2(void * arg)121 void proc2(void *arg)
122 {
123 #pragma unused( arg )
124 
125     //---- init functionCost
126     OS_InitFunctionCost(&myCostBuffer2, MYCOSTBUFFER_SIZE);
127     OS_InitStatistics(&myStatBuffer2, MYSTATBUFFER_SIZE);
128 
129     while (1)
130     {
131         test2();
132         OS_SleepThread(NULL);
133         OS_WakeupThreadDirect(&thread1);
134 
135         //---- calculate cost
136         OS_CalcStatistics(&myStatBuffer2);
137     }
138 }
139 
140 //================================================================
141 //----------------------------------------------------------------
142 // test1
143 //
test1(void)144 void test1(void)
145 {
146     test2();
147 }
148 
149 //----------------------------------------------------------------
150 // test2
151 //
test2(void)152 void test2(void)
153 {
154     test3();
155 }
156 
157 //----------------------------------------------------------------
158 // test3
159 //
test3(void)160 void test3(void)
161 {
162     OS_SpinWait(0x10000);
163     return;
164 }
165 
166 //----------------------------------------------------------------
167 // test_sleep
168 //
test_sleep(void)169 void test_sleep(void)
170 {
171     OS_SpinWait(0x10000);
172     OS_SleepThread(NULL);
173 }
174 
175 /*====== End of main.c ======*/
176