e72931916991983976700ef4ee387e6d41935706
[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 (set_name(param->get_svalue()))
94     ticker= uc->get_counter(get_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 (!get_name() &&
115       what < 1)
116     {
117       con->dd_printf("Error: Timer id must be greater then zero or a string\n");
118       return(DD_FALSE);
119     }
120   if (ticker)
121     {
122       if (get_name())
123         con->dd_printf("Error: Timer \"%s\" already exists\n", get_name());
124       else
125         con->dd_printf("Error: Timer %d already exists\n", what);
126       return(0);
127     }
128   if (params[2])
129     if (!params[2]->get_ivalue(&dir))
130       {
131         con->dd_printf("Error: Wrong direction\n");
132         return(DD_FALSE);
133       }
134   if (params[3])
135     if (!params[3]->get_ivalue(&in_isr))
136       {
137         con->dd_printf("Error: Wrong parameter\n");
138         return(DD_FALSE);
139       }
140
141   if (get_name())
142     {
143       ticker= new cl_ticker(dir, in_isr, get_name());
144       uc->add_counter(ticker, get_name());
145     }
146   else
147     {
148       ticker= new cl_ticker(dir, in_isr, 0);
149       uc->add_counter(ticker, what);
150     }
151
152   return(DD_FALSE);
153 }
154
155 /*
156  * Delete a timer from the list
157  */
158
159 int
160 cl_timer_cmd::del(class cl_uc *uc,
161                   class cl_cmdline *cmdline, class cl_console *con)
162 {
163   if (!ticker)
164     {
165       if (get_name())
166         con->dd_printf("Timer \"%s\" does not exist\n", get_name());
167       else
168         con->dd_printf("Timer %d does not exist\n", what);
169       return(0);
170     }
171   if (get_name())
172     uc->del_counter(get_name());
173   else
174     uc->del_counter(what);
175
176   return(0);
177 }
178
179 /*
180  * Get the value of just one timer or all of them
181  */
182
183 int
184 cl_timer_cmd::get(class cl_uc *uc,
185                   class cl_cmdline *cmdline, class cl_console *con)
186 {
187   if (ticker)
188     ticker->dump(what, uc->xtal, con);
189   else
190     {
191       uc->ticks->dump(0, uc->xtal, con);
192       uc->isr_ticks->dump(0, uc->xtal, con);
193       uc->idle_ticks->dump(0, uc->xtal, con);
194       for (what= 0; what < uc->counters->count; what++)
195         {
196           ticker= uc->get_counter(what);
197           if (ticker)
198             ticker->dump(what, uc->xtal, con);
199         }
200     }
201
202   return(0);
203 }
204
205 /*
206  * Allow a timer to run
207  */
208
209 int
210 cl_timer_cmd::run(class cl_uc *uc,
211                   class cl_cmdline *cmdline, class cl_console *con)
212 {
213   if (!ticker)
214     {
215       if (get_name())
216         con->dd_printf("Timer %d does not exist\n", get_name());
217       else
218         con->dd_printf("Timer %d does not exist\n", what);
219       return(0);
220     }
221   ticker->options|= TICK_RUN;
222
223   return(0);
224 }
225
226 /*
227  * Stop a timer
228  */
229
230 int
231 cl_timer_cmd::stop(class cl_uc *uc,
232                    class cl_cmdline *cmdline, class cl_console *con)
233 {
234   if (!ticker)
235     {
236       if (get_name())
237         con->dd_printf("Timer %d does not exist\n", get_name());
238       else
239         con->dd_printf("Timer %d does not exist\n", what);
240       return(0);
241     }
242   ticker->options&= ~TICK_RUN;
243
244   return(0);
245 }
246
247
248 /*
249  * Set a timer to a specified value
250  */
251
252 int
253 cl_timer_cmd::val(class cl_uc *uc,
254                   class cl_cmdline *cmdline, class cl_console *con)
255 {
256   class cl_cmd_arg *params[4]= { cmdline->param(0),
257                                  cmdline->param(1),
258                                  cmdline->param(2),
259                                  cmdline->param(3) };
260   
261   if (!ticker)
262     {
263       if (get_name())
264         con->dd_printf("Error: Timer %d does not exist\n", get_name());
265       else
266         con->dd_printf("Error: Timer %d does not exist\n", what);
267       return(0);
268     }
269   if (params[2])
270     {
271       con->dd_printf("Error: Value is missing\n");
272       return(DD_FALSE);
273     }
274   long val;
275   if (!params[2]->get_ivalue(&val))
276     {
277       con->dd_printf("Error: Wrong parameter\n");
278       return(DD_FALSE);
279     }
280   ticker->ticks= val;
281
282   return(DD_FALSE);
283 }
284
285
286 /* End of cmd.src/timer.cc */