FreeCalypso > hg > fc-selenite
comparison src/gpf/osl/os_tim_fl.c @ 6:8b2a9a374324
src/gpf: addition of Magnetite src/gpf2
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sun, 15 Jul 2018 08:15:49 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
5:1ea54a97e831 | 6:8b2a9a374324 |
---|---|
1 /* | |
2 * This C module is a reconstruction based on the disassembly of | |
3 * os_tim.obj in frame_na7_db_fl.lib from the Leonardo package, | |
4 * subsequently reworked by Space Falcon. | |
5 */ | |
6 | |
7 /* set of included headers from COFF symtab: */ | |
8 #include <stdio.h> | |
9 #include "nucleus.h" | |
10 #include "typedefs.h" | |
11 #include "os.h" | |
12 #include "gdi.h" | |
13 #include "os_types.h" | |
14 #include "os_glob.h" | |
15 | |
16 extern UNSIGNED TMD_Timer; | |
17 extern INT TMD_Timer_State; | |
18 | |
19 extern T_OS_TIMER_ENTRY TimerTable[]; | |
20 extern T_OS_TIMER_TABLE_ENTRY *p_list[]; | |
21 | |
22 extern void os_Timeout(UNSIGNED t_handle); | |
23 extern void timer_error(int err); | |
24 | |
25 unsigned os_time_to_tick_multiplier = TIME_TO_TICK_TDMA_FRAME_MULTIPLIER; | |
26 unsigned os_tick_to_time_multiplier = TICK_TO_TIME_TDMA_FRAME_MULTIPLIER; | |
27 | |
28 unsigned volatile t_start_ticks; | |
29 T_OS_TIMER_TABLE_ENTRY *t_running; | |
30 int used_timers; | |
31 int next_t_handle; | |
32 int volatile t_list_access; | |
33 int max_used_timers; | |
34 NU_SEMAPHORE TimSemCB; | |
35 NU_TIMER os_timer_cb; | |
36 | |
37 #ifdef __GNUC__ | |
38 #define BARRIER asm volatile ("": : :"memory") | |
39 #else | |
40 #define BARRIER /* prayer */ | |
41 #endif | |
42 | |
43 GLOBAL LONG | |
44 os_set_tick(int os_system_tick) | |
45 { | |
46 switch (os_system_tick) { | |
47 case SYSTEM_TICK_TDMA_FRAME: | |
48 os_time_to_tick_multiplier = TIME_TO_TICK_TDMA_FRAME_MULTIPLIER; | |
49 os_tick_to_time_multiplier = TICK_TO_TIME_TDMA_FRAME_MULTIPLIER; | |
50 return(OS_OK); | |
51 case SYSTEM_TICK_10_MS: | |
52 os_time_to_tick_multiplier = TIME_TO_TICK_10MS_MULTIPLIER; | |
53 os_tick_to_time_multiplier = TICK_TO_TIME_10MS_MULTIPLIER; | |
54 return(OS_OK); | |
55 default: | |
56 return(OS_ERROR); | |
57 } | |
58 } | |
59 | |
60 GLOBAL LONG | |
61 os_TimerInformation(USHORT Index, char *Buffer) | |
62 { | |
63 static int t_info_read; | |
64 | |
65 if (t_info_read) { | |
66 t_info_read = 0; | |
67 return(OS_ERROR); | |
68 } | |
69 sprintf(Buffer, "Maximum %d of %d available timers running", | |
70 max_used_timers, MaxSimultaneousTimer); | |
71 t_info_read = 1; | |
72 return(OS_OK); | |
73 } | |
74 | |
75 GLOBAL LONG | |
76 os_TimInit(void) | |
77 { | |
78 int i; | |
79 | |
80 if (NU_Create_Semaphore(&TimSemCB, "TIMSEM", 1, NU_PRIORITY) | |
81 != NU_SUCCESS) | |
82 return(OS_ERROR); | |
83 if (NU_Create_Timer(&os_timer_cb, "OS_TIMER", os_Timeout, 0, 1, 0, | |
84 NU_DISABLE_TIMER) != NU_SUCCESS) | |
85 return(OS_ERROR); | |
86 used_timers = 0; | |
87 max_used_timers = 0; | |
88 next_t_handle = 1; | |
89 t_list_access = 0; | |
90 t_start_ticks = 0; | |
91 p_list[0] = 0; | |
92 for (i = 1; i < MaxSimultaneousTimer; i++) { | |
93 TimerTable[i].entry.status = TMR_FREE; | |
94 TimerTable[i].entry.next = 0; | |
95 TimerTable[i].entry.prev = 0; | |
96 TimerTable[i].next_t_handle = i + 1; | |
97 p_list[i] = 0; | |
98 } | |
99 TimerTable[MaxSimultaneousTimer].entry.status = TMR_FREE; | |
100 TimerTable[MaxSimultaneousTimer].next_t_handle = 0; | |
101 t_running = 0; | |
102 return(OS_OK); | |
103 } | |
104 | |
105 GLOBAL LONG | |
106 os_RecoverTick(OS_TICK ticks) | |
107 { | |
108 UNSIGNED current_system_clock; | |
109 | |
110 current_system_clock = NU_Retrieve_Clock(); | |
111 NU_Set_Clock(current_system_clock + ticks); | |
112 if (TMD_Timer_State == TM_ACTIVE) { | |
113 if (TMD_Timer <= ticks) { | |
114 TMD_Timer_State = TM_EXPIRED; | |
115 TMD_Timer = 0; | |
116 } else | |
117 TMD_Timer -= ticks; | |
118 } | |
119 return(OS_OK); | |
120 } | |
121 | |
122 GLOBAL LONG | |
123 os_QueryTimer(OS_HANDLE TaskHandle, OS_HANDLE TimerHandle, | |
124 OS_TIME *RemainingTime) | |
125 { | |
126 T_OS_TIMER_TABLE_ENTRY *timer, *t_iter; | |
127 OS_TICK c_ticks, r_ticks, e_ticks; | |
128 STATUS sts; | |
129 | |
130 if (TimerHandle > MaxSimultaneousTimer) | |
131 return(OS_ERROR); | |
132 sts = NU_Obtain_Semaphore(&TimSemCB, NU_SUSPEND); | |
133 timer = &TimerTable[TimerHandle].entry; | |
134 if (timer->status == TMR_FREE) { | |
135 if (sts == NU_SUCCESS) | |
136 NU_Release_Semaphore(&TimSemCB); | |
137 return(OS_ERROR); | |
138 } | |
139 t_list_access = 1; | |
140 BARRIER; | |
141 if (!t_running) { | |
142 r_ticks = 0; | |
143 goto out; | |
144 } | |
145 c_ticks = NU_Retrieve_Clock(); | |
146 e_ticks = c_ticks - t_start_ticks; | |
147 t_iter = t_running; | |
148 if (t_iter->r_ticks >= e_ticks) | |
149 r_ticks = t_iter->r_ticks - e_ticks; | |
150 else | |
151 r_ticks = 0; | |
152 while (t_iter != timer) { | |
153 t_iter = t_iter->next; | |
154 if (t_iter == t_running) { | |
155 r_ticks = 0; | |
156 goto out; | |
157 } | |
158 r_ticks += t_iter->r_ticks; | |
159 } | |
160 out: BARRIER; | |
161 t_list_access = 0; | |
162 if (sts == NU_SUCCESS) | |
163 NU_Release_Semaphore(&TimSemCB); | |
164 *RemainingTime = SYSTEM_TICKS_TO_TIME(r_ticks); | |
165 return(OS_OK); | |
166 } | |
167 | |
168 GLOBAL LONG | |
169 os_InactivityTicks(int *next_event, OS_TICK *next_event_ticks) | |
170 { | |
171 *next_event = 1; | |
172 switch (TMD_Timer_State) { | |
173 case TM_ACTIVE: | |
174 *next_event_ticks = TMD_Timer; | |
175 return(OS_OK); | |
176 case TM_NOT_ACTIVE: | |
177 *next_event_ticks = 0; | |
178 *next_event = 0; | |
179 return(OS_OK); | |
180 default: | |
181 *next_event_ticks = 0; | |
182 return(OS_OK); | |
183 } | |
184 } |