0.2.38-pre1 implements AVR instructions
[fw/sdcc] / sim / ucsim / cmd.src / cmdset.cc
1 /*
2  * Simulator of microcontrollers (cmd.src/cmdset.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 "i_string.h"
31
32 #include "globals.h"
33 #include "utils.h"
34
35 // sim
36 #include "simcl.h"
37
38 // local
39 #include "cmdsetcl.h"
40
41
42 /*
43  * Command: conf
44  *----------------------------------------------------------------------------
45  */
46
47 int
48 cl_conf_cmd::do_work(class cl_cmdline *cmdline, class cl_console *con)
49 {
50   int i;
51
52   con->printf("ucsim version %s\n", VERSIONSTR);
53   con->printf("Type of microcontroller: %s\n", sim->uc->id_string());
54   con->printf("Controller has %d hardware element(s).\n",
55               sim->uc->hws->count);
56   for (i= 0; i < sim->uc->hws->count; i++)
57     {
58       class cl_hw *hw= (class cl_hw *)(sim->uc->hws->at(i));
59       con->printf("  %s[%d]\n", hw->id_string, hw->id);
60     }
61   con->printf("Memories:\n");
62   for (i= MEM_ROM; i < MEM_TYPES; i++)
63     {
64       class cl_mem *mem= (class cl_mem *)(sim->uc->mems->at(i));
65       if (mem)
66         con->printf("  %s size= 0x%06x %d\n",
67                     mem->id_string(), mem->size, mem->size);
68     }
69   return(0);
70 }
71
72
73 /*
74  * Command: state
75  *----------------------------------------------------------------------------
76  */
77
78 int
79 cl_state_cmd::do_work(class cl_cmdline *cmdline, class cl_console *con)
80 {
81   //con->printf("sim state= %d\n", sim->state);
82   con->printf("CPU state= %s PC= 0x%06x XTAL= %g\n",
83               get_id_string(cpu_states, sim->uc->state),
84               sim->uc->PC, 
85               sim->uc->xtal);
86   con->printf("Total time since last reset= %g sec (%lu clks)\n",
87               sim->uc->get_rtime(), sim->uc->ticks->ticks);
88   con->printf("Time in isr = %g sec (%lu clks) %3.2g%%\n",
89               sim->uc->isr_ticks->get_rtime(sim->uc->xtal),
90               sim->uc->isr_ticks->ticks,
91               (sim->uc->ticks->ticks == 0)?0.0:
92               (100.0*((double)(sim->uc->isr_ticks->ticks)/
93                       (double)(sim->uc->ticks->ticks))));
94   con->printf("Time in idle= %g sec (%lu clks) %3.2g%%\n",
95               sim->uc->idle_ticks->get_rtime(sim->uc->xtal),
96               sim->uc->idle_ticks->ticks,
97               (sim->uc->ticks->ticks == 0)?0.0:
98               (100.0*((double)(sim->uc->idle_ticks->ticks)/
99                       (double)(sim->uc->ticks->ticks))));
100   con->printf("Max value of stack pointer= 0x%06x, avg= 0x%06x\n",
101               sim->uc->sp_max, sim->uc->sp_avg);
102   return(0);
103 }
104
105
106 /*
107  * Command: file
108  *----------------------------------------------------------------------------
109  */
110
111 int
112 cl_file_cmd::do_work(class cl_cmdline *cmdline, class cl_console *con)
113 {
114   char *fname= 0;
115   long l;
116   
117   if ((cmdline->param(0) == 0) ||
118       ((fname= cmdline->param(0)->get_svalue()) == NULL))
119     {
120       con->printf("File name is missing.\n");
121       return(0);
122     }
123   if ((l= sim->uc->read_hex_file(fname)) >= 0)
124     con->printf("%ld words read from %s\n", l, fname);
125
126   return(0);
127 }
128
129
130 /*
131  * Command: download
132  *----------------------------------------------------------------------------
133  */
134
135 int
136 cl_dl_cmd::do_work(class cl_cmdline *cmdline, class cl_console *con)
137 {
138   long l;
139   
140   if ((l= sim->uc->read_hex_file(NULL)) >= 0)
141     con->printf("%ld words loaded\n", l);
142
143   return(0);
144 }
145
146
147 /*
148  * Command: get
149  *----------------------------------------------------------------------------
150  */
151
152 int
153 cl_get_cmd::do_work(class cl_cmdline *cmdline, class cl_console *con)
154 {
155   char *s;
156
157   if (cmdline->param(0) == 0)
158     {
159       con->printf("Get what?\n");
160       return(0);
161     }
162   if ((s= cmdline->param(0)->get_svalue()))
163     {
164       if (strstr(s, "t") == s)
165         return(timer(cmdline, con));
166       else
167         con->printf("Unknow keyword of get command\n");
168     }
169   return(0);
170 }
171
172
173 /*
174  * Command: set
175  *----------------------------------------------------------------------------
176  */
177
178
179 int
180 cl_set_cmd::do_work(class cl_cmdline *cmdline, class cl_console *con)
181 {
182   char *s;
183
184   if (cmdline->param(0) == 0)
185     {
186       con->printf("Set what?\n");
187       return(0);
188     }
189   if ((s= cmdline->param(0)->get_svalue()))
190     {
191       if (strstr(s, "t") == s)
192         return(timer(cmdline, con));
193       else
194         con->printf("Unknow keyword of set command\n");
195     }
196   return(0);
197 }
198
199
200 /*
201  * Command: run
202  *----------------------------------------------------------------------------
203  */
204
205
206 int
207 cl_run_cmd::do_work(class cl_cmdline *cmdline, class cl_console *con)
208 {
209   sim->start(con);
210   return(0);
211 }
212
213
214 /*
215  * Command: step
216  *----------------------------------------------------------------------------
217  */
218
219
220 int
221 cl_step_cmd::do_work(class cl_cmdline *cmdline, class cl_console *con)
222 {
223   sim->uc->do_inst(1);
224   sim->uc->print_regs(con);
225   return(0);
226 }
227
228
229 /*
230  * Command: reset
231  *----------------------------------------------------------------------------
232  */
233
234
235 int
236 cl_reset_cmd::do_work(class cl_cmdline *cmdline, class cl_console *con)
237 {
238   sim->uc->reset();
239   return(0);
240 }
241
242
243 /*
244  * Command: dump
245  *----------------------------------------------------------------------------
246  */
247
248 int
249 cl_dump_cmd::do_work(class cl_cmdline *cmdline, class cl_console *con)
250 {
251   class cl_mem *mem;
252   char *s;
253   long l, start= -1, end= 10*8-1, bpl= 8;
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 (params[0] == 0)
260     {
261       con->printf("Memory type missing\n");
262       return(0);
263     }
264   if ((s= params[0]->get_svalue()))
265     {
266       if (strstr(s, "i") == s)
267         mem= sim->uc->mem(MEM_IRAM);
268       else if (strstr(s, "x") == s)
269         mem= sim->uc->mem(MEM_XRAM);
270       else if (strstr(s, "r") == s)
271         mem= sim->uc->mem(MEM_ROM);
272       else if (strstr(s, "s") == s)
273         mem= sim->uc->mem(MEM_SFR);
274       else
275         {
276           con->printf("Unknown memory type\n");
277           return(0);
278         }
279       if (!mem)
280         {
281           con->printf("No such memory\n");
282           return(0);
283         }
284     }
285   else
286     {
287       con->printf("Wrong memory type\n");
288       return(0);
289     }
290
291   if (params[1])
292     {
293       if ((start= params[1]->get_address()) >= 0)
294         {
295           end+= start;
296           if (params[2])
297             {
298               if ((l= params[2]->get_address()) >= 0)
299                 end= l;
300               else
301                 {
302                   con->printf("End address is wrong\n");
303                   return(0);
304                 }
305             }
306         }
307       else
308         {
309           con->printf("Start address is wrong\n");
310           return(0);
311         }
312     }
313   if (params[3] &&
314       (l= params[3]->get_address()) >= 0)
315     bpl= l;
316   mem->dump(start, end, bpl, con);
317   return(0);
318 }
319
320
321 /*
322  * Command: di
323  *----------------------------------------------------------------------------
324  */
325
326 int
327 cl_di_cmd::do_work(class cl_cmdline *cmdline, class cl_console *con)
328 {
329   cmdline->insert_param(0, new cl_cmd_sym_arg("i"));
330   cl_dump_cmd::do_work(cmdline, con);
331   return(0);
332 }
333
334
335 /*
336  * Command: dx
337  *----------------------------------------------------------------------------
338  */
339
340 int
341 cl_dx_cmd::do_work(class cl_cmdline *cmdline, class cl_console *con)
342 {
343   cmdline->insert_param(0, new cl_cmd_sym_arg("x"));
344   cl_dump_cmd::do_work(cmdline, con);
345   return(0);
346 }
347
348
349 /*
350  * Command: dch
351  *----------------------------------------------------------------------------
352  */
353
354 int
355 cl_dch_cmd::do_work(class cl_cmdline *cmdline, class cl_console *con)
356 {
357   cmdline->insert_param(0, new cl_cmd_sym_arg("r"));
358   cl_dump_cmd::do_work(cmdline, con);
359   return(0);
360 }
361
362
363 /*
364  * Command: ds
365  *----------------------------------------------------------------------------
366  */
367
368 int
369 cl_ds_cmd::do_work(class cl_cmdline *cmdline, class cl_console *con)
370 {
371   cmdline->insert_param(0, new cl_cmd_sym_arg("s"));
372   cl_dump_cmd::do_work(cmdline, con);
373   return(0);
374 }
375
376
377 /*
378  * Command: dc
379  *----------------------------------------------------------------------------
380  */
381
382 int
383 cl_dc_cmd::do_work(class cl_cmdline *cmdline, class cl_console *con)
384 {
385   long i, l, start= last, end= -1;
386   class cl_cmd_arg *params[2]= { cmdline->param(0),
387                                  cmdline->param(1) };
388
389   if (params[0])
390     {
391       if ((start= params[0]->get_address()) >= 0)
392         {
393           if (params[1])
394             {
395               if ((l= params[1]->get_address()) >= 0)
396                 end= l;
397               else
398                 {
399                   con->printf("End address is wrong\n");
400                   return(0);
401                 }
402             }
403         }
404       else
405         {
406           con->printf("Start address is wrong\n");
407           return(0);
408         }
409     }
410   i= 0;
411   for (l= start;
412        (end < 0 && i < 20) || l <= end;
413        l+= sim->uc->inst_length(sim->uc->get_mem(MEM_ROM, l)), i++)
414     sim->uc->print_disass(l, con);
415   last= l;
416   return(0);
417 }
418
419
420 /*
421  * Command: help
422  *----------------------------------------------------------------------------
423  */
424
425 int
426 cl_help_cmd::do_work(class cl_cmdline */*cmdline*/, class cl_console *con)
427 {
428   class cl_cmd *c;
429   int i;
430
431   for (i= 0; i < sim->cmdset->count; i++)
432     {
433       c= (class cl_cmd *)(sim->cmdset->at(i));
434       if (c->short_help)
435         con->printf("%s\n", c->short_help);
436       else
437         con->printf("%s\n", (char*)(c->names->at(0)));
438     }
439   return(0);
440 }
441
442
443 /*
444  * Command: quit
445  *----------------------------------------------------------------------------
446  */
447
448 int
449 cl_quit_cmd::do_work(class cl_cmdline */*cmdline*/, class cl_console */*con*/)
450 {
451   return(1);
452 }
453
454 /*
455  * Command: kill
456  *----------------------------------------------------------------------------
457  */
458
459 int
460 cl_kill_cmd::do_work(class cl_cmdline */*cmdline*/, class cl_console */*con*/)
461 {
462   sim->state|= SIM_QUIT;
463   return(1);
464 }
465
466
467 /* End of cmd.src/cmdset.cc */