Merge branch ucsim-034-pre3 to main trunk; new version 0.4
[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->mem(MEM_ROM),
90                                   sim->uc->make_new_brknr(), end,
91                                   brkDYNAMIC, 1);
92               sim->uc->fbrk->add_bp(b);
93             }
94         }
95     }
96   con->dd_printf("Simulation started, PC=0x%06x\n", sim->uc->PC);
97   if (sim->uc->fbrk_at(sim->uc->PC))
98     sim->uc->do_inst(1);
99
100   sim->start(con);
101   return(DD_FALSE);
102 }
103
104
105 /*
106  * Command: stop
107  *----------------------------------------------------------------------------
108  */
109
110 //int
111 //cl_stop_cmd::do_work(class cl_sim *sim,
112 //                   class cl_cmdline *cmdline, class cl_console *con)
113 COMMAND_DO_WORK_SIM(cl_stop_cmd)
114 {
115   sim->stop(resUSER);
116   sim->uc->print_disass(sim->uc->PC, con);
117   return(DD_FALSE);
118 }
119
120
121 /*
122  * Command: step
123  *----------------------------------------------------------------------------
124  */
125
126 //int
127 //cl_step_cmd::do_work(class cl_sim *sim,
128 //                   class cl_cmdline *cmdline, class cl_console *con)
129 COMMAND_DO_WORK_UC(cl_step_cmd)
130 {
131   printf("step %x\n",uc->PC);
132   uc->do_inst(1);
133   printf("step done %x\n",uc->PC);
134   uc->print_regs(con);
135   return(0);
136 }
137
138
139 /*
140  * Command: next
141  *----------------------------------------------------------------------------
142  */
143
144 //int
145 //cl_next_cmd::do_work(class cl_sim *sim,
146 //                   class cl_cmdline *cmdline, class cl_console *con)
147 COMMAND_DO_WORK_SIM(cl_next_cmd)
148 {
149   class cl_brk *b;
150   t_addr next;
151   int branch;
152   int inst_len;
153
154 #if 0
155   struct dis_entry *de;
156   t_mem code= sim->uc->get_mem(MEM_ROM, sim->uc->PC);
157   int i= 0;
158   de= &(sim->uc->dis_tbl()[i]);
159   while ((code & de->mask) != de->code &&
160          de->mnemonic)
161     {
162       i++;
163       de= &(sim->uc->dis_tbl()[i]);
164     }
165 #endif
166
167   branch = sim->uc->inst_branch(sim->uc->PC);
168   inst_len = sim->uc->inst_length(sim->uc->PC);
169
170   if ((branch == 'a') || (branch == 'l'))
171     {
172       next= sim->uc->PC + inst_len;
173       if (!sim->uc->fbrk_at(next))
174         {
175           b= new cl_fetch_brk(sim->uc->mem(MEM_ROM),
176                               sim->uc->make_new_brknr(),
177                               next, brkDYNAMIC, 1);
178
179           b->init();
180 //        sim->uc->fbrk->add_bp(b);
181
182           sim->uc->fbrk->add(b);
183           b->activate();
184         }
185       if (sim->uc->fbrk_at(sim->uc->PC))
186         sim->uc->do_inst(1);
187       sim->start(con);
188       //sim->uc->do_inst(-1);
189     }
190   else
191     sim->uc->do_inst(1);
192   sim->uc->print_regs(con);
193   return(DD_FALSE);
194 }
195
196
197 /*
198  * Command: help
199  *----------------------------------------------------------------------------
200  */
201
202 //int
203 //cl_help_cmd::do_work(class cl_sim *sim,
204 //                   class cl_cmdline *cmdline, class cl_console *con)
205 COMMAND_DO_WORK_APP(cl_help_cmd)
206 {
207   class cl_sim *sim;
208   class cl_commander *cmd;
209   class cl_cmdset *cmdset= 0;
210   class cl_cmd *c;
211   int i;
212   class cl_cmd_arg *parm= cmdline->param(0);
213
214   sim= app->get_sim();
215   if ((cmd= app->get_commander()) != 0)
216     cmdset= cmd->cmdset;
217   if (!cmdset)
218     return(DD_FALSE);
219   if (!parm) {
220     for (i= 0; i < cmdset->count; i++)
221       {
222         c= (class cl_cmd *)(cmdset->at(i));
223         if (c->short_help)
224           con->dd_printf("%s\n", c->short_help);
225         else
226           con->dd_printf("%s\n", (char*)(c->names->at(0)));
227       }
228   }
229   else if (cmdline->syntax_match(/*sim*/0, STRING)) {
230     int matches= 0;
231     for (i= 0; i < cmdset->count; i++)
232       {
233         c= (class cl_cmd *)(cmdset->at(i));
234         if (c->name_match(parm->value.string.string, DD_FALSE))
235           matches++;
236       }
237     if (!matches)
238       con->dd_printf("No such command\n");
239     else if (matches > 1)
240       for (i= 0; i < cmdset->count; i++)
241         {
242           c= (class cl_cmd *)(cmdset->at(i));
243           if (!c->name_match(parm->value.string.string, DD_FALSE))
244             continue;
245           if (c->short_help)
246             con->dd_printf("%s\n", c->short_help);
247           else
248             con->dd_printf("%s\n", (char*)(c->names->at(0)));
249         }
250     else
251       for (i= 0; i < cmdset->count; i++)
252         {
253           c= (class cl_cmd *)(cmdset->at(i));
254           if (!c->name_match(parm->value.string.string, DD_FALSE))
255             continue;
256           if (c->short_help)
257             con->dd_printf("%s\n", c->short_help);
258           else
259             con->dd_printf("%s\n", (char*)(c->names->at(0)));
260           int names;
261           con->dd_printf("Names of command:");
262           for (names= 0; names < c->names->count; names++)
263             con->dd_printf(" %s", (char*)(c->names->at(names)));
264           con->dd_printf("\n");
265           if (c->long_help)
266             con->dd_printf("%s\n", c->long_help);
267           else
268             con->dd_printf("%s\n", (char*)(c->names->at(0)));
269         }
270   }
271   else
272     con->dd_printf("%s\n", short_help?short_help:"Error: wrong syntax");
273
274   return(0);
275 }
276
277
278 /*
279  * Command: quit
280  *----------------------------------------------------------------------------
281  */
282
283 //int
284 //cl_quit_cmd::do_work(class cl_sim *sim,
285 //                   class cl_cmdline */*cmdline*/, class cl_console */*con*/)
286 COMMAND_DO_WORK(cl_quit_cmd)
287 {
288   return(1);
289 }
290
291
292 /*
293  * Command: kill
294  *----------------------------------------------------------------------------
295  */
296
297 //int
298 //cl_kill_cmd::do_work(class cl_sim *sim,
299 //                   class cl_cmdline */*cmdline*/, class cl_console */*con*/)
300 COMMAND_DO_WORK_APP(cl_kill_cmd)
301 {
302   app->going= 0;
303   if (app->sim)
304     app->sim->state|= SIM_QUIT;
305   return(1);
306 }
307
308
309 /* End of cmd.src/cmdset.cc */