40ab449545dfe50c7fdba1395d58ac57d3b45ae4
[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   }
194   return(DD_FALSE);
195 }
196
197
198 /*
199  * Command: help
200  *----------------------------------------------------------------------------
201  */
202
203 //int
204 //cl_help_cmd::do_work(class cl_sim *sim,
205 //                   class cl_cmdline *cmdline, class cl_console *con)
206 COMMAND_DO_WORK_APP(cl_help_cmd)
207 {
208   class cl_sim *sim;
209   class cl_commander *cmd;
210   class cl_cmdset *cmdset= 0;
211   class cl_cmd *c;
212   int i;
213   class cl_cmd_arg *parm= cmdline->param(0);
214
215   sim= app->get_sim();
216   if ((cmd= app->get_commander()) != 0)
217     cmdset= cmd->cmdset;
218   if (!cmdset)
219     return(DD_FALSE);
220   if (!parm) {
221     for (i= 0; i < cmdset->count; i++)
222       {
223         c= (class cl_cmd *)(cmdset->at(i));
224         if (c->short_help)
225           con->dd_printf("%s\n", c->short_help);
226         else
227           con->dd_printf("%s\n", (char*)(c->names->at(0)));
228       }
229   }
230   else if (cmdline->syntax_match(/*sim*/0, STRING)) {
231     int matches= 0;
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           matches++;
237       }
238     if (!matches)
239       con->dd_printf("No such command\n");
240     else if (matches > 1)
241       for (i= 0; i < cmdset->count; i++)
242         {
243           c= (class cl_cmd *)(cmdset->at(i));
244           if (!c->name_match(parm->value.string.string, DD_FALSE))
245             continue;
246           if (c->short_help)
247             con->dd_printf("%s\n", c->short_help);
248           else
249             con->dd_printf("%s\n", (char*)(c->names->at(0)));
250         }
251     else
252       for (i= 0; i < cmdset->count; i++)
253         {
254           c= (class cl_cmd *)(cmdset->at(i));
255           if (!c->name_match(parm->value.string.string, DD_FALSE))
256             continue;
257           if (c->short_help)
258             con->dd_printf("%s\n", c->short_help);
259           else
260             con->dd_printf("%s\n", (char*)(c->names->at(0)));
261           int names;
262           con->dd_printf("Names of command:");
263           for (names= 0; names < c->names->count; names++)
264             con->dd_printf(" %s", (char*)(c->names->at(names)));
265           con->dd_printf("\n");
266           if (c->long_help)
267             con->dd_printf("%s\n", c->long_help);
268           else
269             con->dd_printf("%s\n", (char*)(c->names->at(0)));
270         }
271   }
272   else
273     con->dd_printf("%s\n", short_help?short_help:"Error: wrong syntax");
274
275   return(0);
276 }
277
278
279 /*
280  * Command: quit
281  *----------------------------------------------------------------------------
282  */
283
284 //int
285 //cl_quit_cmd::do_work(class cl_sim *sim,
286 //                   class cl_cmdline */*cmdline*/, class cl_console */*con*/)
287 COMMAND_DO_WORK(cl_quit_cmd)
288 {
289   return(1);
290 }
291
292
293 /*
294  * Command: kill
295  *----------------------------------------------------------------------------
296  */
297
298 //int
299 //cl_kill_cmd::do_work(class cl_sim *sim,
300 //                   class cl_cmdline */*cmdline*/, class cl_console */*con*/)
301 COMMAND_DO_WORK_APP(cl_kill_cmd)
302 {
303   app->going= 0;
304   if (app->sim)
305     app->sim->state|= SIM_QUIT;
306   return(1);
307 }
308
309
310 /*
311  * EXEC file
312  */
313
314 COMMAND_DO_WORK_APP(cl_exec_cmd)
315 {
316   class cl_cmd_arg *parm= cmdline->param(0);
317   char *fn= 0;
318
319   if (cmdline->syntax_match(0, STRING)) {
320     fn= parm->value.string.string; 
321   }
322   else
323     con->dd_printf("%s\n", short_help?short_help:"Error: wrong syntax\n");
324
325   class cl_commander *c= app->get_commander();
326   class cl_console *cons= con->clone_for_exec(fn);
327   if (cons)
328     {
329       cons->flags|= CONS_NOWELCOME;
330       c->add_console(cons);
331     }
332
333   return(DD_FALSE);
334 }
335
336
337 /* End of cmd.src/cmdset.cc */