fix printf is a macro in gcc 3
[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 // prj
31 #include "i_string.h"
32 #include "utils.h"
33 #include "globals.h"
34
35 // sim.src
36 #include "simcl.h"
37
38 // local, cmd.src
39 #include "cmdsetcl.h"
40 #include "cmdutil.h"
41
42
43 /*
44  * Command: run
45  *----------------------------------------------------------------------------
46  */
47
48 //int
49 //cl_run_cmd::do_work(class cl_sim *sim,
50 //                  class cl_cmdline *cmdline, class cl_console *con)
51 COMMAND_DO_WORK_SIM(cl_run_cmd)
52 {
53   class cl_brk *b;
54   t_addr start, end;
55   class cl_cmd_arg *params[4]= { cmdline->param(0),
56                                  cmdline->param(1),
57                                  cmdline->param(2),
58                                  cmdline->param(3) };
59
60   if (params[0])
61     if (!(params[0]->get_address(sim->uc, &start)))
62       {
63         con->dd_printf("Error: wrong start address\n");
64         return(DD_FALSE);
65       }
66   if (params[1])
67     if (!(params[1]->get_address(sim->uc, &end)))
68       {
69         con->dd_printf("Error: wromg end address\n");
70         return(DD_FALSE);
71       }
72   if (params[0])
73     {
74       if (!sim->uc->inst_at(start))
75         con->dd_printf("Warning: maybe not instruction at 0x%06lx\n", start);
76       sim->uc->PC= start;
77       if (params[1])
78         {
79           if (start == end)
80             {
81               con->dd_printf("Addresses must be different.\n");
82               return(DD_FALSE);
83             }
84           if ((b= sim->uc->fbrk_at(end)))
85             {
86             }
87           else
88             {
89               b= new cl_fetch_brk(sim->uc->make_new_brknr(), end,
90                                   brkDYNAMIC, 1);
91               sim->uc->fbrk->add_bp(b);
92             }
93         }
94     }
95   con->dd_printf("Simulation started, PC=0x%06x\n", sim->uc->PC);
96   if (sim->uc->fbrk_at(start))
97     sim->uc->do_inst(1);
98   sim->start(con);
99   return(DD_FALSE);
100 }
101
102
103 /*
104  * Command: stop
105  *----------------------------------------------------------------------------
106  */
107
108 //int
109 //cl_stop_cmd::do_work(class cl_sim *sim,
110 //                   class cl_cmdline *cmdline, class cl_console *con)
111 COMMAND_DO_WORK_SIM(cl_stop_cmd)
112 {
113   sim->stop(resUSER);
114   sim->uc->print_disass(sim->uc->PC, con);
115   return(DD_FALSE);
116 }
117
118
119 /*
120  * Command: step
121  *----------------------------------------------------------------------------
122  */
123
124 //int
125 //cl_step_cmd::do_work(class cl_sim *sim,
126 //                   class cl_cmdline *cmdline, class cl_console *con)
127 COMMAND_DO_WORK_UC(cl_step_cmd)
128 {
129   uc->do_inst(1);
130   uc->print_regs(con);
131   return(0);
132 }
133
134
135 /*
136  * Command: next
137  *----------------------------------------------------------------------------
138  */
139
140 //int
141 //cl_next_cmd::do_work(class cl_sim *sim,
142 //                   class cl_cmdline *cmdline, class cl_console *con)
143 COMMAND_DO_WORK_SIM(cl_next_cmd)
144 {
145   class cl_brk *b;
146   t_addr next;
147   struct dis_entry *de;
148
149   t_mem code= sim->uc->get_mem(MEM_ROM, sim->uc->PC);
150   int i= 0;
151   de= &(sim->uc->dis_tbl()[i]);
152   while ((code & de->mask) != de->code &&
153          de->mnemonic)
154     {
155       i++;
156       de= &(sim->uc->dis_tbl()[i]);
157     }
158   if ((de->branch == 'a') ||
159       (de->branch == 'l'))
160     {
161       next= sim->uc->PC + de->length;
162       if (!sim->uc->fbrk_at(next))
163         {
164           b= new cl_fetch_brk(sim->uc->make_new_brknr(),
165                               next, brkDYNAMIC, 1);
166           sim->uc->fbrk->add(b);
167         }
168       sim->start(con);
169       //sim->uc->do_inst(-1);
170     }
171   else
172     sim->uc->do_inst(1);
173   sim->uc->print_regs(con);
174   return(DD_FALSE);
175 }
176
177
178 /*
179  * Command: help
180  *----------------------------------------------------------------------------
181  */
182
183 //int
184 //cl_help_cmd::do_work(class cl_sim *sim,
185 //                   class cl_cmdline *cmdline, class cl_console *con)
186 COMMAND_DO_WORK_APP(cl_help_cmd)
187 {
188   class cl_sim *sim;
189   class cl_commander *cmd;
190   class cl_cmdset *cmdset= 0;
191   class cl_cmd *c;
192   int i;
193   class cl_cmd_arg *parm= cmdline->param(0);
194
195   sim= app->get_sim();
196   if ((cmd= app->get_commander()) != 0)
197     cmdset= cmd->cmdset;
198   if (!cmdset)
199     return(DD_FALSE);
200   if (!parm) {
201     for (i= 0; i < cmdset->count; i++)
202       {
203         c= (class cl_cmd *)(cmdset->at(i));
204         if (c->short_help)
205           con->dd_printf("%s\n", c->short_help);
206         else
207           con->dd_printf("%s\n", (char*)(c->names->at(0)));
208       }
209   }
210   else if (cmdline->syntax_match(/*sim*/0, STRING)) {
211     int matches= 0;
212     for (i= 0; i < cmdset->count; i++)
213       {
214         c= (class cl_cmd *)(cmdset->at(i));
215         if (c->name_match(parm->value.string.string, DD_FALSE))
216           matches++;
217       }
218     if (!matches)
219       con->dd_printf("No such command\n");
220     else if (matches > 1)
221       for (i= 0; i < cmdset->count; i++)
222         {
223           c= (class cl_cmd *)(cmdset->at(i));
224           if (!c->name_match(parm->value.string.string, DD_FALSE))
225             continue;
226           if (c->short_help)
227             con->dd_printf("%s\n", c->short_help);
228           else
229             con->dd_printf("%s\n", (char*)(c->names->at(0)));
230         }
231     else
232       for (i= 0; i < cmdset->count; i++)
233         {
234           c= (class cl_cmd *)(cmdset->at(i));
235           if (!c->name_match(parm->value.string.string, DD_FALSE))
236             continue;
237           if (c->short_help)
238             con->dd_printf("%s\n", c->short_help);
239           else
240             con->dd_printf("%s\n", (char*)(c->names->at(0)));
241           int names;
242           con->dd_printf("Names of command:");
243           for (names= 0; names < c->names->count; names++)
244             con->dd_printf(" %s", (char*)(c->names->at(names)));
245           con->dd_printf("\n");
246           if (c->long_help)
247             con->dd_printf("%s\n", c->long_help);
248           else
249             con->dd_printf("%s\n", (char*)(c->names->at(0)));
250         }
251   }
252   else
253     con->dd_printf("%s\n", short_help?short_help:"Error: wrong syntax");
254
255   return(0);
256 }
257
258
259 /*
260  * Command: quit
261  *----------------------------------------------------------------------------
262  */
263
264 //int
265 //cl_quit_cmd::do_work(class cl_sim *sim,
266 //                   class cl_cmdline */*cmdline*/, class cl_console */*con*/)
267 COMMAND_DO_WORK(cl_quit_cmd)
268 {
269   return(1);
270 }
271
272
273 /*
274  * Command: kill
275  *----------------------------------------------------------------------------
276  */
277
278 //int
279 //cl_kill_cmd::do_work(class cl_sim *sim,
280 //                   class cl_cmdline */*cmdline*/, class cl_console */*con*/)
281 COMMAND_DO_WORK_APP(cl_kill_cmd)
282 {
283   app->going= 0;
284   if (app->sim)
285     app->sim->state|= SIM_QUIT;
286   return(1);
287 }
288
289
290 /* End of cmd.src/cmdset.cc */