version 0.5.2
[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   class cl_cmd_arg *params[4]= { cmdline->param(0),
51                                  cmdline->param(1),
52                                  cmdline->param(2),
53                                  cmdline->param(3) };
54   
55   if (!params[0])
56     {
57       con->dd_printf("Timer id is missing.");
58       return(DD_FALSE);
59     }
60   if (params[0]->as_number())
61     {
62       as_nr= DD_TRUE;
63       id_nr= params[0]->value.number;
64       if (id_nr <= 0)
65         {
66           con->dd_printf("Error: "
67                          "Timer id must be greater than zero or a string\n");
68           return(DD_TRUE);
69         }
70       ticker= uc->get_counter(id_nr);
71     }
72   else
73     {
74       as_nr= DD_FALSE;
75       id_str= params[0]->s_value;
76       ticker= uc->get_counter(id_str);
77     }
78   cmdline->shift();
79   return(DD_FALSE);
80 }
81
82
83 /*
84  * Command: timer add
85  *-----------------------------------------------------------------------------
86  * Add a new timer to the list
87  */
88
89 COMMAND_DO_WORK_UC(cl_timer_add_cmd)
90   //add(class cl_uc *uc, class cl_cmdline *cmdline, class cl_console *con)
91 {
92   class cl_cmd_arg *params[4]= { cmdline->param(0),
93                                  cmdline->param(1),
94                                  cmdline->param(2),
95                                  cmdline->param(3) };
96   long dir= +1, in_isr= 0;
97
98   if (cl_timer_cmd::do_work(uc, cmdline, con))
99     return(DD_FALSE);
100   if (ticker)
101     {
102       if (!as_nr)
103         con->dd_printf("Error: Timer \"%s\" already exists\n", id_str);
104       else
105         con->dd_printf("Error: Timer %d already exists\n", id_nr);
106       return(DD_FALSE);
107     }
108
109   if (cmdline->nuof_params() > 0)
110     {
111       if (cmdline->syntax_match(uc, NUMBER))
112         dir= params[0]->value.number;
113       else if (cmdline->syntax_match(uc, NUMBER NUMBER))
114         {
115           dir= params[0]->value.number;
116           in_isr= params[1]->value.number;
117         }
118     }
119
120   if (!as_nr)
121     {
122       ticker= new cl_ticker(dir, in_isr, id_str);
123       uc->add_counter(ticker, id_str);
124     }
125   else
126     {
127       ticker= new cl_ticker(dir, in_isr, 0);
128       uc->add_counter(ticker, id_nr);
129     }
130
131   return(DD_FALSE);
132 }
133
134 /*
135  * Command: timer delete
136  *-----------------------------------------------------------------------------
137  * Delete a timer from the list
138  */
139
140 COMMAND_DO_WORK_UC(cl_timer_delete_cmd)
141   //del(class cl_uc *uc, class cl_cmdline *cmdline, class cl_console *con)
142 {
143   if (cl_timer_cmd::do_work(uc, cmdline, con))
144     return(DD_FALSE);
145   if (!ticker)
146     {
147       if (!as_nr)
148         con->dd_printf("Timer \"%s\" does not exist\n", id_str);
149       else
150         con->dd_printf("Timer %d does not exist\n", id_nr);
151       return(DD_FALSE);
152     }
153   if (!as_nr)
154     uc->del_counter(id_str);
155   else
156     uc->del_counter(id_nr);
157
158   return(DD_FALSE);
159 }
160
161 /*
162  * Command: timer get
163  *-----------------------------------------------------------------------------
164  * Get the value of just one timer or all of them
165  */
166
167 COMMAND_DO_WORK_UC(cl_timer_get_cmd)
168   //get(class cl_uc *uc, class cl_cmdline *cmdline, class cl_console *con)
169 {
170   if (cmdline->nuof_params())
171     {
172       if (cl_timer_cmd::do_work(uc, cmdline, con))
173         return(DD_FALSE);
174     }
175   else
176     ticker= 0;
177   if (ticker)
178     ticker->dump(id_nr, uc->xtal, con);
179   else
180     {
181       uc->ticks->dump(0, uc->xtal, con);
182       uc->isr_ticks->dump(0, uc->xtal, con);
183       uc->idle_ticks->dump(0, uc->xtal, con);
184       for (id_nr= 0; id_nr < uc->counters->count; id_nr++)
185         {
186           ticker= uc->get_counter(id_nr);
187           if (ticker)
188             ticker->dump(id_nr, uc->xtal, con);
189         }
190     }
191
192   return(DD_FALSE);
193 }
194
195 /*
196  * Command: timer run
197  *-----------------------------------------------------------------------------
198  * Allow a timer to run
199  */
200
201 COMMAND_DO_WORK_UC(cl_timer_run_cmd)
202   //run(class cl_uc *uc, class cl_cmdline *cmdline, class cl_console *con)
203 {
204   if (cl_timer_cmd::do_work(uc, cmdline, con))
205     return(DD_FALSE);
206   if (!ticker)
207     {
208       if (!as_nr)
209         con->dd_printf("Timer %d does not exist\n", id_str);
210       else
211         con->dd_printf("Timer %d does not exist\n", id_nr);
212       return(0);
213     }
214   ticker->options|= TICK_RUN;
215
216   return(DD_FALSE);
217 }
218
219 /*
220  * Command: timer stop
221  *-----------------------------------------------------------------------------
222  * Stop a timer
223  */
224
225 COMMAND_DO_WORK_UC(cl_timer_stop_cmd)
226   //stop(class cl_uc *uc, class cl_cmdline *cmdline, class cl_console *con)
227 {
228   if (cl_timer_cmd::do_work(uc, cmdline, con))
229     return(DD_FALSE);
230
231   if (!ticker)
232     {
233       if (!as_nr)
234         con->dd_printf("Timer %d does not exist\n", id_str);
235       else
236         con->dd_printf("Timer %d does not exist\n", id_nr);
237       return(DD_FALSE);
238     }
239   ticker->options&= ~TICK_RUN;
240
241   return(DD_FALSE);
242 }
243
244
245 /*
246  * Command: timer value
247  *-----------------------------------------------------------------------------
248  * Set a timer to a specified value
249  */
250
251 COMMAND_DO_WORK_UC(cl_timer_value_cmd)
252   //val(class cl_uc *uc, class cl_cmdline *cmdline, class cl_console *con)
253 {
254   class cl_cmd_arg *params[4]= { cmdline->param(0),
255                                  cmdline->param(1),
256                                  cmdline->param(2),
257                                  cmdline->param(3) };
258   
259   if (cl_timer_cmd::do_work(uc, cmdline, con))
260     return(DD_FALSE);
261   if (!ticker)
262     {
263       if (!as_nr)
264         con->dd_printf("Error: Timer %d does not exist\n", id_str);
265       else
266         con->dd_printf("Error: Timer %d does not exist\n", id_nr);
267       return(DD_FALSE);
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 */