OSDN Git Service

debug mode supported
[motonesemu/motonesemu.git] / emulator / clock.c
1 #include <stdlib.h>
2 #include <unistd.h>
3
4 #include "tools.h"
5 #include "clock.h"
6
7 #define SIG_CPU_CLOCK SIGRTMIN
8
9 static timer_t cpu_clock_timer;
10
11 struct clock_handler {
12     struct slist l;
13     clock_func_t *handler;
14 };
15
16 static struct clock_handler *handler_list;
17
18 static void cpu_clock_loop(int arg) {
19     struct clock_handler *ch;
20
21     dprint("clock...\n");
22     ch = handler_list;
23     while (ch != NULL) {
24         if (!ch->handler())
25             return;
26         ch = (struct clock_handler*)ch->l.next;
27     }
28
29     return;
30 }
31
32
33 int start_clock(void) {
34     int ret;
35     struct sigaction    sigact;
36
37
38     //register handler
39     sigact.sa_handler = cpu_clock_loop;
40     sigact.sa_flags = 0;
41     sigemptyset(&sigact.sa_mask);
42
43     if(sigaction(SIG_CPU_CLOCK, &sigact, NULL) == -1)
44     {
45         return FALSE;
46     }
47     ret = start_cpu_clock();
48
49     return ret;
50 }
51
52 int pause_cpu_clock(void) {
53     return 0 == timer_delete(cpu_clock_timer);
54 }
55
56 int start_cpu_clock(void) {
57     struct sigevent sev;
58     struct itimerspec   itval;
59     int int_sec, int_nanosec;
60
61     sev.sigev_notify = SIGEV_SIGNAL;
62     sev.sigev_signo = SIG_CPU_CLOCK;
63     sev.sigev_value.sival_ptr = &cpu_clock_timer;
64
65     int_sec = CPU_CLOCK_SEC;
66     int_nanosec = CPU_CLOCK_NSEC;
67     itval.it_interval.tv_sec = int_sec;
68     itval.it_interval.tv_nsec = int_nanosec;
69     itval.it_value.tv_sec = int_sec;
70     itval.it_value.tv_nsec = int_nanosec;
71
72     if(timer_create(CLOCK_REALTIME, &sev, &cpu_clock_timer) == -1)
73     {
74         return FALSE;
75     }
76     if(timer_settime(cpu_clock_timer, 0, &itval, NULL) == -1)
77     {
78         return FALSE;
79     }
80     return TRUE;
81 }
82
83 int register_clock_hander(clock_func_t *handler) {
84     struct clock_handler *ch;
85
86     ch = malloc(sizeof(struct clock_handler));
87     ch->l.next = NULL;
88     ch->handler = handler;
89
90     if (handler_list == NULL) {
91         handler_list = ch;
92     }
93     else {
94         slist_add_tail(&handler_list->l, &ch->l);
95     }
96     return TRUE;
97 }
98
99 #if 0
100 int register_timer(unsigned long int_sec, unsigned long int_nanosec, __sighandler_t func, 
101         int signum, timer_t *timerId) {
102     struct sigaction    sigact;
103     struct itimerspec   itval;
104     struct sigevent sev;
105
106
107     //register handler
108     sigact.sa_handler = func;
109     sigact.sa_flags = 0;
110     sigemptyset(&sigact.sa_mask);
111
112     if(sigaction(signum,&sigact,NULL) == -1)
113     {
114         return FALSE;
115     }
116
117     //create timer
118     itval.it_interval.tv_sec = int_sec;
119     itval.it_interval.tv_nsec = int_nanosec;
120     itval.it_value.tv_sec = int_sec;
121     itval.it_value.tv_nsec = int_nanosec;
122
123     sev.sigev_notify = SIGEV_SIGNAL;
124     sev.sigev_signo = signum;
125     sev.sigev_value.sival_ptr = timerId;
126
127     if(timer_create(CLOCK_REALTIME, &sev, timerId) == -1)
128     {
129         return FALSE;
130     }
131     if(timer_settime(*timerId,0,&itval,NULL) == -1)
132     {
133         return FALSE;
134     }
135     return TRUE;
136 }
137 #endif
138
139 int init_clock(void) {
140     handler_list = NULL;
141     cpu_clock_timer = 0;
142     return TRUE;
143 }
144
145 void clean_clock(void) {
146     struct clock_handler *ch = handler_list;
147
148     timer_delete(cpu_clock_timer);
149
150     while (ch != NULL) {
151         struct clock_handler *pp = ch;
152         ch = (struct clock_handler*)ch->l.next;
153         free(pp);
154     }
155     dprint("clean_clock done.\n");
156     
157
158 }
159