17abb4c88ee4e8bb87dcaeca5a1168274756b57e
[fw/sdcc] / sim / ucsim / cmd.src / timer.cc
1 /*
2  * Simulator of microcontrollers (cmd.src/timer.cc)
3  *
4  * Copyright (C) 1999,99 Drotos Daniel, Talker Bt.
5  * 
6  * To contact author send email to drdani@mazsola.iit.uni-miskolc.hu
7  *
8  */
9
10 /* This file is part of microcontroller simulator: ucsim.
11
12 UCSIM is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 UCSIM is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with UCSIM; see the file COPYING.  If not, write to the Free
24 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 02111-1307, USA. */
26 /*@1@*/
27
28 #include "ddconfig.h"
29
30 #include "stdio.h"
31 #include "i_string.h"
32
33 // sim
34 #include "simcl.h"
35
36 // local
37 #include "timercl.h"
38
39
40 /*
41  * Command: timer
42  *----------------------------------------------------------------------------
43  */
44
45 //int
46 //cl_timer_cmd::do_work(class cl_sim *sim,
47 //                    class cl_cmdline *cmdline, class cl_console *con)
48 COMMAND_DO_WORK_UC(cl_timer_cmd)
49 {
50   char *s;
51
52   if (cmdline->param(0) == 0)
53     {
54       if (long_help)
55         con->dd_printf("%s\n", long_help);
56       else
57         con->dd_printf("What to do?\n");
58       return(0);
59     }
60   if ((s= cmdline->param(0)->get_svalue()))
61     {
62       if (cmdline->param(1) == 0)
63         {
64           con->dd_printf("Timer number is missing\n");
65           return(0);
66         }
67       set_ticker(uc, cmdline->param(1));
68       if (strstr(s, "c") == s ||
69           strstr(s, "m") == s ||
70           strstr(s, "a") == s)
71         return(add(uc, cmdline, con));
72       else if (strstr(s, "d") == s)
73         return(del(uc, cmdline, con));
74       else if (strstr(s, "g") == s)
75         return(get(uc, cmdline, con));
76       else if (strstr(s, "r") == s)
77         return(run(uc, cmdline, con));
78       else if (strstr(s, "s") == s)
79         return(stop(uc, cmdline, con));
80       else if (strstr(s, "v") == s)
81         return(val(uc, cmdline, con));
82       else
83         con->dd_printf("Undefined timer command: \"%s\". Try \"help timer\"\n",
84                        s);
85     }
86   return(0);
87 }
88
89 void
90 cl_timer_cmd::set_ticker(class cl_uc *uc,
91                          class cl_cmd_arg *param)
92 {
93   if ((name= param->get_svalue()))
94     ticker= uc->get_counter(name);
95   else
96     if (param->get_ivalue(&what))
97       ticker= uc->get_counter(what);
98 }
99
100 /*
101  * Add a new timer to the list
102  */
103
104 int
105 cl_timer_cmd::add(class cl_uc *uc,
106                   class cl_cmdline *cmdline, class cl_console *con)
107 {
108   class cl_cmd_arg *params[4]= { cmdline->param(0),
109                                  cmdline->param(1),
110                                  cmdline->param(2),
111                                  cmdline->param(3) };
112   long dir= +1, in_isr= 0;
113   
114   if (!name &&
115       what < 1)
116     {
117       con->dd_printf("Error: "
118                      "Timer id must be greater then zero or a string\n");
119       return(DD_FALSE);
120     }
121   if (ticker)
122     {
123       if (name)
124         con->dd_printf("Error: Timer \"%s\" already exists\n", name);
125       else
126         con->dd_printf("Error: Timer %d already exists\n", what);
127       return(0);
128     }
129   if (params[2])
130     if (!params[2]->get_ivalue(&dir))
131       {
132         con->dd_printf("Error: Wrong direction\n");
133         return(DD_FALSE);
134       }
135   if (params[3])
136     if (!params[3]->get_ivalue(&in_isr))
137       {
138         con->dd_printf("Error: Wrong parameter\n");
139         return(DD_FALSE);
140       }
141
142   if (name)
143     {
144       ticker= new cl_ticker(dir, in_isr, name);
145       uc->add_counter(ticker, name);
146     }
147   else
148     {
149       ticker= new cl_ticker(dir, in_isr, 0);
150       uc->add_counter(ticker, what);
151     }
152
153   return(DD_FALSE);
154 }
155
156 /*
157  * Delete a timer from the list
158  */
159
160 int
161 cl_timer_cmd::del(class cl_uc *uc,
162                   class cl_cmdline *cmdline, class cl_console *con)
163 {
164   if (!ticker)
165     {
166       if (name)
167         con->dd_printf("Timer \"%s\" does not exist\n", name);
168       else
169         con->dd_printf("Timer %d does not exist\n", what);
170       return(0);
171     }
172   if (name)
173     uc->del_counter(name);
174   else
175     uc->del_counter(what);
176
177   return(0);
178 }
179
180 /*
181  * Get the value of just one timer or all of them
182  */
183
184 int
185 cl_timer_cmd::get(class cl_uc *uc,
186                   class cl_cmdline *cmdline, class cl_console *con)
187 {
188   if (ticker)
189     ticker->dump(what, uc->xtal, con);
190   else
191     {
192       uc->ticks->dump(0, uc->xtal, con);
193       uc->isr_ticks->dump(0, uc->xtal, con);
194       uc->idle_ticks->dump(0, uc->xtal, con);
195       for (what= 0; what < uc->counters->count; what++)
196         {
197           ticker= uc->get_counter(what);
198           if (ticker)
199             ticker->dump(what, uc->xtal, con);
200         }
201     }
202
203   return(0);
204 }
205
206 /*
207  * Allow a timer to run
208  */
209
210 int
211 cl_timer_cmd::run(class cl_uc *uc,
212                   class cl_cmdline *cmdline, class cl_console *con)
213 {
214   if (!ticker)
215     {
216       if (name)
217         con->dd_printf("Timer %d does not exist\n", name);
218       else
219         con->dd_printf("Timer %d does not exist\n", what);
220       return(0);
221     }
222   ticker->options|= TICK_RUN;
223
224   return(0);
225 }
226
227 /*
228  * Stop a timer
229  */
230
231 int
232 cl_timer_cmd::stop(class cl_uc *uc,
233                    class cl_cmdline *cmdline, class cl_console *con)
234 {
235   if (!ticker)
236     {
237       if (name)
238         con->dd_printf("Timer %d does not exist\n", name);
239       else
240         con->dd_printf("Timer %d does not exist\n", what);
241       return(0);
242     }
243   ticker->options&= ~TICK_RUN;
244
245   return(0);
246 }
247
248
249 /*
250  * Set a timer to a specified value
251  */
252
253 int
254 cl_timer_cmd::val(class cl_uc *uc,
255                   class cl_cmdline *cmdline, class cl_console *con)
256 {
257   class cl_cmd_arg *params[4]= { cmdline->param(0),
258                                  cmdline->param(1),
259                                  cmdline->param(2),
260                                  cmdline->param(3) };
261   
262   if (!ticker)
263     {
264       if (name)
265         con->dd_printf("Error: Timer %d does not exist\n", name);
266       else
267         con->dd_printf("Error: Timer %d does not exist\n", what);
268       return(0);
269     }
270   if (params[2])
271     {
272       con->dd_printf("Error: Value is missing\n");
273       return(DD_FALSE);
274     }
275   long val;
276   if (!params[2]->get_ivalue(&val))
277     {
278       con->dd_printf("Error: Wrong parameter\n");
279       return(DD_FALSE);
280     }
281   ticker->ticks= val;
282
283   return(DD_FALSE);
284 }
285
286
287 /* End of cmd.src/timer.cc */