* sim/ucsim/error.cc, sim/ucsim/errorcl.h,
[fw/sdcc] / sim / ucsim / cmd.src / set.cc
1 /*
2  * Simulator of microcontrollers (cmd.src/set.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 /*
11   This file is part of microcontroller simulator: ucsim.
12
13   UCSIM is free software; you can redistribute it and/or modify
14   it under the terms of the GNU General Public License as published by
15   the Free Software Foundation; either version 2 of the License, or
16   (at your option) any later version.
17
18   UCSIM is distributed in the hope that it will be useful,
19   but WITHOUT ANY WARRANTY; without even the implied warranty of
20   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21   GNU General Public License for more details.
22
23   You should have received a copy of the GNU General Public License
24   along with UCSIM; see the file COPYING.  If not, write to the Free
25   Software Foundation, 59 Temple Place - Suite 330, Boston, MA
26   02111-1307, USA.
27 */
28 /*@1@*/
29
30 #include "ddconfig.h"
31
32 #include <ctype.h>
33 #include "i_string.h"
34
35 // prj
36 #include "errorcl.h"
37
38 // sim
39 #include "simcl.h"
40 #include "optioncl.h"
41
42 // local
43 #include "setcl.h"
44 #include "cmdutil.h"
45
46
47 /*
48  * Command: set memory
49  *----------------------------------------------------------------------------
50  */
51
52 //int
53 //cl_set_mem_cmd::do_work(class cl_sim *sim,
54 //                      class cl_cmdline *cmdline, class cl_console *con)
55 COMMAND_DO_WORK_UC(cl_set_mem_cmd)
56 {
57   class cl_memory *mem= 0;
58   class cl_cmd_arg *params[4]= { cmdline->param(0),
59                                  cmdline->param(1),
60                                  cmdline->param(2),
61                                  cmdline->param(3) };
62
63   if (cmdline->syntax_match(uc, MEMORY ADDRESS DATALIST)) {
64     mem= params[0]->value.memory.memory;
65     t_addr start= params[1]->value.address;
66     t_mem *array= params[2]->value.data_list.array;
67     int len= params[2]->value.data_list.len;
68     
69     if (len == 0)
70       con->dd_printf("Error: no data\n");
71     else if (start < mem->get_start_address())
72       con->dd_printf("Start address less then 0x%"_A_"x\n",
73                      mem->get_start_address());
74     else
75       {
76         int i;
77         t_addr addr;
78         for (i= 0, addr= start;
79              i < len && mem->valid_address(addr);
80              i++, addr++)
81           mem->write(addr, array[i]);
82         uc->check_errors();
83         mem->dump(start, start+len-1, 8, con);
84       }
85   }
86   else
87     con->dd_printf("%s\n", short_help?short_help:"Error: wrong syntax\n");
88   
89   return(DD_FALSE);;
90 }
91
92
93 /*
94  * Command: set bit
95  *----------------------------------------------------------------------------
96  */
97
98 //int
99 //cl_set_bit_cmd::do_work(class cl_sim *sim,
100 //                      class cl_cmdline *cmdline, class cl_console *con)
101 COMMAND_DO_WORK_UC(cl_set_bit_cmd)
102 {
103   class cl_memory *mem;
104   t_addr mem_addr= 0;
105   t_mem bit_mask= 0;
106   class cl_cmd_arg *params[4]= { cmdline->param(0),
107                                  cmdline->param(1),
108                                  cmdline->param(2),
109                                  cmdline->param(3) };
110   
111   if (cmdline->syntax_match(uc, BIT NUMBER)) {
112     mem= params[0]->value.bit.mem;
113     mem_addr= params[0]->value.bit.mem_address;
114     bit_mask= params[0]->value.bit.mask;
115     if (params[1]->value.number)
116       mem->set_bit1(mem_addr, bit_mask);
117     else
118       mem->set_bit0(mem_addr, bit_mask);
119     mem->dump(mem_addr, mem_addr, 1, con);
120   }
121   else
122     con->dd_printf("%s\n", short_help?short_help:"Error: wrong syntax\n");
123
124   return(DD_FALSE);;
125 }
126
127
128 /*
129  * Command: set hw
130  *----------------------------------------------------------------------------
131  */
132
133 COMMAND_DO_WORK_UC(cl_set_hw_cmd)
134 {
135   class cl_hw *hw= 0;
136   class cl_cmd_arg *params[1]= { cmdline->param(0)/*,
137                                  cmdline->param(1),
138                                  cmdline->param(2),
139                                  cmdline->param(3)*/ };
140   
141   if (params[0] && /*cmdline->syntax_match(uc, HW)*/params[0]->as_hw(uc)) {
142     hw= params[0]->value.hw;
143     //pn= hw->id;
144     //l= params[1]->value.number;
145   }
146   /*else if (cmdline->syntax_match(uc, NUMBER NUMBER)) {
147     pn= params[0]->value.number;
148     l= params[1]->value.number;
149     hw= uc->get_hw(HW_PORT, pn, 0);
150     }*/
151   else
152     con->dd_printf("%s\n", short_help?short_help:"Error: wrong syntax\n");
153   /*if (pn < 0 ||
154       pn > 3)
155     con->dd_printf("Error: wrong port\n");
156     else*/
157     {
158       if (hw)
159         {
160           cmdline->shift();
161           hw->set_cmd(cmdline, con);
162         }
163       else
164         con->dd_printf("Error: no hw\n");
165     }
166   return(DD_FALSE);;
167 }
168
169
170 /*
171  * Command: set option
172  *----------------------------------------------------------------------------
173  */
174
175 //int
176 //cl_set_option_cmd::do_work(class cl_sim *sim,
177 //                         class cl_cmdline *cmdline, class cl_console *con)
178 COMMAND_DO_WORK_APP(cl_set_option_cmd)
179 {
180   char *id= 0, *s= 0;
181   int idx;
182   class cl_cmd_arg *params[4]= { cmdline->param(0),
183                                  cmdline->param(1),
184                                  cmdline->param(2),
185                                  cmdline->param(3) };
186   class cl_option *option= 0;
187
188   if (cmdline->syntax_match(0/*app->get_uc()*/, NUMBER STRING)) {
189     idx= params[0]->value.number;
190     s= params[1]->value.string.string;
191     option= app->options->get_option(idx);
192   }
193   else if (cmdline->syntax_match(0, STRING STRING STRING)) {
194     id= params[0]->value.string.string;
195     char *cr= params[1]->value.string.string;
196     s= params[2]->value.string.string;
197     int n= app->options->nuof_options(id, cr);
198     if (n > 1)
199       {
200         con->dd_printf("Ambiguous option name, use number instead\n");
201         return(DD_FALSE);
202       }
203     else if (n == 0)
204       ;//con->dd_printf("Named option does not exist\n");
205     else
206       {
207         if ((option= app->options->get_option(id, cr)) == 0)
208           option= app->options->get_option(cr, id);
209       }
210   }
211   else if (cmdline->syntax_match(0/*app->get_uc()*/, STRING STRING)) {
212     id= params[0]->value.string.string;
213     s= params[1]->value.string.string;
214     int n= app->options->nuof_options(id);
215     if (n > 1)
216       {
217         con->dd_printf("Ambiguous option name, use number instead\n");
218         return(DD_FALSE);
219       }
220     else if (n == 0)
221       ;//con->dd_printf("Named option does not exist\n");
222     else
223       option= app->options->get_option(id);
224   }
225   else
226     con->dd_printf("%s\n", short_help?short_help:"Error: wrong syntax\n");
227   if (!option)
228     {
229       con->dd_printf("Option does not exist\n");
230       return(DD_FALSE);
231     }
232
233   option->set_value(s);
234
235   return(DD_FALSE);
236 }
237
238
239 /*
240  * Command: set error
241  *----------------------------------------------------------------------------
242  */
243
244 //int
245 //cl_set_option_cmd::do_work(class cl_sim *sim,
246 //                         class cl_cmdline *cmdline, class cl_console *con)
247 COMMAND_DO_WORK_APP(cl_set_error_cmd)
248 {
249   class cl_cmd_arg *params[4]= { cmdline->param(0),
250                                  cmdline->param(1),
251                                  cmdline->param(2),
252                                  cmdline->param(3) };
253   char *error_name= NIL, *value= NIL;
254
255   if (cmdline->syntax_match(0/*app->get_uc()*/, STRING STRING)) {
256     error_name= params[0]->value.string.string;
257     value= params[1]->value.string.string;
258   }
259   else
260     con->dd_printf("%s\n", short_help?short_help:"Error: wrong syntax\n");
261
262   class cl_list *registered_errors = cl_error_registry::get_list();
263   if (error_name &&
264       value &&
265       registered_errors)
266     {
267       int i;
268       for (i= 0; i < registered_errors->count; i++)
269         {
270           class cl_error_class *e=
271             dynamic_cast<class cl_error_class *>(registered_errors->object_at(i));
272           if (e->is_inamed(error_name))
273             {
274               if (strchr("uU-?", *value) != NULL)
275                 e->set_on(ERROR_PARENT);
276               else if (strchr("1tTyY", *value) != NULL ||
277                        (strlen(value) > 1 &&
278                         strchr("nN", value[2]) != NULL))
279                 e->set_on(ERROR_ON);
280               else if (strchr("0fFnN", *value) != NULL ||
281                        (strlen(value) > 1 &&
282                         strchr("fF", value[2]) != NULL))
283                 e->set_on(ERROR_OFF);
284               else
285                 con->dd_printf("Bad value (%s)\n", value);
286               return(DD_FALSE);
287             }
288         }
289     }
290   con->dd_printf("Error %s not found\n", error_name);
291
292   return(DD_FALSE);
293 }
294
295
296 /* End of cmd.src/set.cc */