ucsim-0.2.37-pre3 into cvs
[fw/sdcc] / sim / ucsim / cmd.src / timer.cc
1 /*
2  * Simulator of microcontrollers (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 "cmdsetcl.h"
38 #include "newcmdcl.h"
39
40
41 /*
42  * Command: timer
43  *----------------------------------------------------------------------------
44  */
45
46 int
47 cl_timer_cmd::do_work(class cl_cmdline *cmdline, class cl_console *con)
48 {
49   char *s;
50
51   if (cmdline->param(0) == 0)
52     {
53       if (long_help)
54         con->printf("%s\n", long_help);
55       else
56         con->printf("What to do?\n");
57       return(0);
58     }
59   if ((s= cmdline->param(0)->get_svalue()))
60     {
61       if (cmdline->param(1) == 0)
62         {
63           con->printf("Timer number is missing\n");
64           return(0);
65         }
66       set_ticker(cmdline->param(1));
67       if (strstr(s, "c") == s ||
68           strstr(s, "m") == s ||
69           strstr(s, "a") == s)
70         return(add(cmdline, con));
71       else if (strstr(s, "d") == s)
72         return(del(cmdline, con));
73       else if (strstr(s, "g") == s)
74         return(get(cmdline, con));
75       else if (strstr(s, "r") == s)
76         return(run(cmdline, con));
77       else if (strstr(s, "s") == s)
78         return(stop(cmdline, con));
79       else if (strstr(s, "v") == s)
80         return(val(cmdline, con));
81       else
82         con->printf("Undefined timer command: \"%s\". Try \"help timer\"\n",
83                     s);
84     }
85   return(0);
86 }
87
88 void
89 cl_timer_cmd::set_ticker(class cl_cmd_arg *param)
90 {
91   if ((name= param->get_svalue()) == 0)
92     what= param->get_ivalue();
93   if (name)
94     ticker= sim->uc->get_counter(name);
95   else
96     ticker= sim->uc->get_counter(what);
97 }
98
99 /*
100  * Add a new timer to the list
101  */
102
103 int
104 cl_timer_cmd::add(class cl_cmdline *cmdline, class cl_console *con)
105 {
106   class cl_cmd_arg *params[4]= { cmdline->param(0),
107                                  cmdline->param(1),
108                                  cmdline->param(2),
109                                  cmdline->param(3) };
110   int dir= +1, in_isr= 0;
111   
112   if (!name &&
113       what < 1)
114     {
115       con->printf("Timer id must be greater then zero or a string\n");
116       return(0);
117     }
118   if (ticker)
119     {
120       if (name)
121         con->printf("Timer \"%s\" already exists\n", name);
122       else
123         con->printf("Timer %d already exists\n", what);
124       return(0);
125     }
126   if (params[2] != 0)
127     dir= (params[2])->get_ivalue();
128   if (params[3] != 0)
129     in_isr= (params[3])->get_ivalue();
130
131   if (name)
132     {
133       ticker= new cl_ticker(dir, in_isr, name);
134       sim->uc->add_counter(ticker, name);
135     }
136   else
137     {
138       ticker= new cl_ticker(dir, in_isr, 0);
139       sim->uc->add_counter(ticker, what);
140     }
141
142   return(0);
143 }
144
145 /*
146  * Delete a timer from the list
147  */
148
149 int
150 cl_timer_cmd::del(class cl_cmdline *cmdline, class cl_console *con)
151 {
152   if (!ticker)
153     {
154       if (name)
155         con->printf("Timer \"%s\" does not exist\n", name);
156       else
157         con->printf("Timer %d does not exist\n", what);
158       return(0);
159     }
160   if (name)
161     sim->uc->del_counter(name);
162   else
163     sim->uc->del_counter(what);
164
165   return(0);
166 }
167
168 /*
169  * Get the value of just one timer or all of them
170  */
171
172 int
173 cl_timer_cmd::get(class cl_cmdline *cmdline, class cl_console *con)
174 {
175   if (ticker)
176     ticker->dump(what, sim->uc->xtal, con);
177   else
178     {
179       sim->uc->ticks->dump(0, sim->uc->xtal, con);
180       sim->uc->isr_ticks->dump(0, sim->uc->xtal, con);
181       sim->uc->idle_ticks->dump(0, sim->uc->xtal, con);
182       for (what= 0; what < sim->uc->counters->count; what++)
183         {
184           ticker= sim->uc->get_counter(what);
185           if (ticker)
186             ticker->dump(what, sim->uc->xtal, con);
187         }
188     }
189
190   return(0);
191 }
192
193 /*
194  * Allow a timer to run
195  */
196
197 int
198 cl_timer_cmd::run(class cl_cmdline *cmdline, class cl_console *con)
199 {
200   if (!ticker)
201     {
202       if (name)
203         con->printf("Timer %d does not exist\n", name);
204       else
205         con->printf("Timer %d does not exist\n", what);
206       return(0);
207     }
208   ticker->options|= TICK_RUN;
209
210   return(0);
211 }
212
213 /*
214  * Stop a timer
215  */
216
217 int
218 cl_timer_cmd::stop(class cl_cmdline *cmdline, class cl_console *con)
219 {
220   if (!ticker)
221     {
222       if (name)
223         con->printf("Timer %d does not exist\n", name);
224       else
225         con->printf("Timer %d does not exist\n", what);
226       return(0);
227     }
228   ticker->options&= ~TICK_RUN;
229
230   return(0);
231 }
232
233
234 /*
235  * Set a timer to a specified value
236  */
237
238 int
239 cl_timer_cmd::val(class cl_cmdline *cmdline, class cl_console *con)
240 {
241   class cl_cmd_arg *params[4]= { cmdline->param(0),
242                                  cmdline->param(1),
243                                  cmdline->param(2),
244                                  cmdline->param(3) };
245   
246   if (!ticker)
247     {
248       if (name)
249         con->printf("Timer %d does not exist\n", name);
250       else
251         con->printf("Timer %d does not exist\n", what);
252       return(0);
253     }
254   if (params[2] == 0)
255     {
256       con->printf("Value is missing\n");
257       return(0);
258     }
259   long val= (params[2])->get_ivalue();
260   ticker->ticks= val;
261
262   return(0);
263 }
264
265
266 /* End of timer.cc */