ps2sdk  1.1
A collection of Open Source libraries used for developing applications on Sony's PlayStation 2® (PS2).
alarm.c File Reference
#include "tamtypes.h"
#include "timer.h"
#include "ee_regs.h"
#include "kernel.h"
#include "internal.h"
+ Include dependency graph for alarm.c:

Go to the source code of this file.

Data Structures

struct  alarm
 

Macros

#define USER_MODE_DISPATCHER   0x00082000
 
#define INTC_TIM3   12
 
#define T3_COUNT_W   ((vu32*)0xB0001800)
 
#define T3_MODE_W   ((vu32*)0xB0001810)
 
#define T3_COMP_W   ((vu32*)0xB0001820)
 

Functions

static u32 CalculateTimeDiff (u32 t1, u32 t2)
 
static int InsertAlarm (u32 now, u32 target)
 
static s32 SetAlarmInternal (u16 time, void(*callback)(s32 dispatch_id, u16 time, void *common), void *common)
 
static void SetupTIM3 (u16 ticks)
 
s32 ReleaseAlarm (s32 id)
 
s32 SetAlarm (u16 time, void(*callback)(s32 dispatch_id, u16 time, void *common), void *common)
 
void Intc12Handler (void)
 

Variables

static int AlarmCount = 0
 
static u64 AlarmStatus = 0
 
static struct alarm alarms [MAX_ALARMS]
 

Macro Definition Documentation

◆ INTC_TIM3

#define INTC_TIM3   12

Definition at line 19 of file alarm.c.

◆ T3_COMP_W

#define T3_COMP_W   ((vu32*)0xB0001820)

Definition at line 22 of file alarm.c.

◆ T3_COUNT_W

#define T3_COUNT_W   ((vu32*)0xB0001800)

Definition at line 20 of file alarm.c.

◆ T3_MODE_W

#define T3_MODE_W   ((vu32*)0xB0001810)

Definition at line 21 of file alarm.c.

◆ USER_MODE_DISPATCHER

#define USER_MODE_DISPATCHER   0x00082000

Definition at line 17 of file alarm.c.

Function Documentation

◆ CalculateTimeDiff()

static u32 CalculateTimeDiff ( u32  t1,
u32  t2 
)
static

Definition at line 44 of file alarm.c.

45 {
46  return(t2 < t1 ? (0x10000 | t2) : t2);
47 }

Referenced by InsertAlarm(), and ReleaseAlarm().

◆ InsertAlarm()

static int InsertAlarm ( u32  now,
u32  target 
)
static

Definition at line 49 of file alarm.c.

50 {
51  int pos, i;
52 
53  pos = 0;
54  for(pos = 0; pos < AlarmCount; pos++)
55  {
56  if(target < CalculateTimeDiff(now, alarms[pos].time))
57  { //Spot found. If it is not at the end of the list, move back the list.
58  for(i = AlarmCount - 1; i >= pos; i--)
59  alarms[i+1] = alarms[i];
60 
61  break;
62  }
63  }
64 
65  return pos;
66 }
struct @29::@30 pos
u32 time
Definition: libmouse.c:37
static int AlarmCount
Definition: alarm.c:24
static u32 CalculateTimeDiff(u32 t1, u32 t2)
Definition: alarm.c:44
static struct alarm alarms[MAX_ALARMS]
Definition: alarm.c:36

References AlarmCount, alarms, CalculateTimeDiff(), pos, alarm::target, and time.

Referenced by SetAlarmInternal().

◆ Intc12Handler()

void Intc12Handler ( void  )

Definition at line 167 of file alarm.c.

168 {
169  struct alarm alarm;
170  void *gp;
171  int i;
172 
173  for(i = 0; i < AlarmCount; i++)
174  { //Attempt to find another alarm request that has a different target. Update TIM3's comparator.
175  if(alarms[i].target != alarms[0].target)
176  {
177  SetupTIM3(alarms[i].target);
178  break;
179  }
180  }
181 
182  do{
183  alarm = alarms[0];
184  AlarmCount--;
185  for(i = 0; i < AlarmCount; i++)
186  alarms[i] = alarms[i+1];
187 
188  gp = ChangeGP(alarm.gp);
189  AlarmStatus &= ~(1 << alarm.id);
191  SetGP(gp);
192 
193  if(AlarmCount <= 0)
194  break;
195  }while(alarms[0].target == alarm.target);
196 
197  if(AlarmCount <= 0) //If there are no further alarms, disable CMPE.
198  *T3_MODE_W = Tn_MODE(3,0,0,0,0,1,0,0,1,0);
199  else
200  SetupTIM3(alarms[0].target);
201 
202  ExitHandler();
203 }
#define gp
Definition: as_reg_compat.h:84
void InvokeUserModeCallback(void *dispatcher, void *callback, int id, u32 target, void *common)
#define ExitHandler()
Definition: kernel.h:28
void * ChangeGP(void *gp)
void SetGP(void *gp)
#define T3_MODE_W
Definition: alarm.c:21
#define USER_MODE_DISPATCHER
Definition: alarm.c:17
static void SetupTIM3(u16 ticks)
Definition: alarm.c:159
static u64 AlarmStatus
Definition: alarm.c:25
Definition: alarm.c:27
void * callback
Definition: alarm.c:31
int id
Definition: alarm.c:30
u16 target
Definition: alarm.c:28
void * common
Definition: alarm.c:32
void * gp
Definition: alarm.c:33
#define Tn_MODE(CLKS, GATE, GATS, GATM, ZRET, CUE, CMPE, OVFE, EQUF, OVFF)
Definition: timer.h:42

References AlarmCount, alarms, AlarmStatus, alarm::callback, ChangeGP(), alarm::common, ExitHandler, gp, alarm::gp, alarm::id, InvokeUserModeCallback(), SetGP(), SetupTIM3(), T3_MODE_W, alarm::target, Tn_MODE, and USER_MODE_DISPATCHER.

◆ ReleaseAlarm()

s32 ReleaseAlarm ( s32  id)

Definition at line 110 of file alarm.c.

111 {
112  u16 time;
113  s32 result;
114  int i, j;
115 
116  result = -1;
117  for(i = 0; i < AlarmCount; i++)
118  {
119  if(alarms[i].id == id)
120  {
121  if(alarms[i].target == *T3_COMP_W)
122  {
123  if(*R_EE_I_STAT & (1 << INTC_TIM3)) //Cannot release alarm that has already triggered.
124  return -1;
125  }
126 
127  time = alarms[i].time;
128  //Move forward list.
129  for(j = i; j < AlarmCount - 1; j++)
130  alarms[j] = alarms[j+1];
131 
132  --AlarmCount;
133  AlarmStatus &= ~(1 << id);
134 
135  if(i == 0) //If the first alarm was released, update timer comparator.
136  SetupTIM3(alarms[0].target);
137 
138  if(AlarmCount == 0) //Stop timer if there are no alarms left.
139  *T3_MODE_W = Tn_MODE(3,0,0,0,0,1,0,0,0,0);
140 
142  }
143  }
144 
145  EE_SYNC();
146  return result;
147 }
#define R_EE_I_STAT
Definition: ee_regs.h:461
#define EE_SYNC()
Definition: kernel.h:31
s32 result
Definition: rpc_client.c:23
#define INTC_TIM3
Definition: alarm.c:19
#define T3_COMP_W
Definition: alarm.c:22
#define T3_COUNT_W
Definition: alarm.c:20
u16 time
Definition: alarm.c:29
signed int s32
Definition: tamtypes.h:58
unsigned short u16
Definition: tamtypes.h:24

References AlarmCount, alarms, AlarmStatus, CalculateTimeDiff(), EE_SYNC, alarm::id, INTC_TIM3, R_EE_I_STAT, result, SetupTIM3(), T3_COMP_W, T3_COUNT_W, T3_MODE_W, alarm::target, alarm::time, time, and Tn_MODE.

Referenced by WaitSemaTimeout().

◆ SetAlarm()

s32 SetAlarm ( u16  time,
void(*)(s32 dispatch_id, u16 time, void *common)  callback,
void *  common 
)

Definition at line 149 of file alarm.c.

150 {
151  s32 result;
152 
153  result = SetAlarmInternal(time, callback, common);
154  EE_SYNC();
155 
156  return result;
157 }
static s32 SetAlarmInternal(u16 time, void(*callback)(s32 dispatch_id, u16 time, void *common), void *common)
Definition: alarm.c:68

References alarm::callback, alarm::common, EE_SYNC, result, SetAlarmInternal(), and time.

◆ SetAlarmInternal()

static s32 SetAlarmInternal ( u16  time,
void(*)(s32 dispatch_id, u16 time, void *common)  callback,
void *  common 
)
static

Definition at line 68 of file alarm.c.

69 {
70  void *gp;
71  u32 target, now;
72  struct alarm *alarm;
73  int i, alarmID, pos;
74 
75  now = *T3_COUNT_W;
76  target = now + time;
77 
78  if(AlarmCount >= MAX_ALARMS)
79  return -1;
80 
81  alarmID = -1;
82  for(i = 0; i < MAX_ALARMS; i++)
83  {
84  if(((AlarmStatus >> i) & 1) == 0)
85  {
86  AlarmStatus |= (1 << i);
87  alarmID = i;
88  break;
89  }
90  }
91 
92  if(alarmID < 0)
93  return alarmID;
94 
95  gp = GetGP();
96  pos = InsertAlarm(now, target);
97  alarm = &alarms[pos];
98  alarm->time = time;
99  alarm->target = (u16)target;
100  alarm->id = alarmID;
101  alarm->gp = gp;
103  alarm->common = common;
104  AlarmCount++;
105  SetupTIM3(alarms[0].target);
106 
107  return alarm->id;
108 }
#define MAX_ALARMS
Definition: kernel.h:72
void * GetGP(void)
static int InsertAlarm(u32 now, u32 target)
Definition: alarm.c:49
unsigned int u32
Definition: tamtypes.h:30

References AlarmCount, alarms, AlarmStatus, alarm::callback, alarm::common, GetGP(), gp, alarm::gp, alarm::id, InsertAlarm(), MAX_ALARMS, pos, SetupTIM3(), T3_COUNT_W, alarm::target, alarm::time, and time.

Referenced by SetAlarm().

◆ SetupTIM3()

static void SetupTIM3 ( u16  ticks)
static

Definition at line 159 of file alarm.c.

160 {
161  *T3_COMP_W = ticks;
162  EE_SYNC();
163  *T3_MODE_W = Tn_MODE(3,0,0,0,0,1,1,0,1,0);
164 }

References EE_SYNC, T3_COMP_W, T3_MODE_W, and Tn_MODE.

Referenced by Intc12Handler(), ReleaseAlarm(), and SetAlarmInternal().

Variable Documentation

◆ AlarmCount

int AlarmCount = 0
static

Definition at line 24 of file alarm.c.

Referenced by InsertAlarm(), Intc12Handler(), ReleaseAlarm(), and SetAlarmInternal().

◆ alarms

struct alarm alarms[MAX_ALARMS]
static

Definition at line 25 of file alarm.c.

Referenced by InsertAlarm(), Intc12Handler(), ReleaseAlarm(), and SetAlarmInternal().

◆ AlarmStatus

u64 AlarmStatus = 0
static

Definition at line 25 of file alarm.c.

Referenced by Intc12Handler(), ReleaseAlarm(), and SetAlarmInternal().