[ucsim] Update email and file info, fix stm8 flash controller
[sdcc.git] / sdcc / sim / ucsim / src / core / sim.src / arg.cc
blob944c253d42d3541156c1a0591ba4de51a6b73a4d
1 /*
2 * Simulator of microcontrollers (arg.cc)
4 * Copyright (C) 1999 Drotos Daniel
5 *
6 * To contact author send email to dr.dkdb@gmail.com
8 */
10 /* This file is part of microcontroller simulator: ucsim.
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.
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.
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@*/
28 //#include "ddconfig.h"
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 //#include "i_string.h"
35 // prj
36 #include "globals.h"
38 // sim
39 //#include "simcl.h"
41 // cmd
42 //#include "cmdutil.h"
44 // local
45 #include "argcl.h"
49 * Making the argument
52 cl_arg::cl_arg(long lv):
53 cl_base()
55 i_value= lv;
56 s_value= 0;
59 cl_arg::cl_arg(const char *sv):
60 cl_base()
62 s_value= sv?strdup(sv):0;
65 cl_arg::cl_arg(double fv):
66 cl_base()
68 f_value= fv;
69 s_value= 0;
72 cl_arg::cl_arg(void *pv):
73 cl_base()
75 p_value= pv;
76 s_value= 0;
79 cl_arg::~cl_arg(void)
81 if (s_value)
82 free((void*)s_value);
87 * Getting value of the argument
90 bool
91 cl_arg::get_ivalue(long *value)
93 if (value)
94 *value= i_value;
95 return(true);
98 char *
99 cl_arg::get_svalue(void)
101 return(s_value);
104 double
105 cl_arg::get_fvalue(void)
107 return(f_value);
110 void *
111 cl_arg::get_pvalue(void)
113 return(p_value);
118 * Command parameters
119 *----------------------------------------------------------------------------
122 cl_cmd_arg::~cl_cmd_arg(void)
124 if (interpreted_as_string)
126 if (value.string.string)
127 free(value.string.string);
132 cl_cmd_arg::init(void)
134 return 0;
137 bool
138 cl_cmd_arg::as_address(class cl_uc *uc)
140 return(get_address(uc, &(value.address)));
143 bool
144 cl_cmd_arg::as_number(void)
146 return(get_ivalue(&(value.number)));
149 bool
150 cl_cmd_arg::as_data(void)
152 long l;
153 bool ret= get_ivalue(&l);
154 value.data= l;
155 return(ret);
158 bool
159 cl_cmd_arg::as_memory(class cl_uc *uc)
161 value.memory.memory= uc->memory(get_svalue());
162 value.memory.address_space= 0;
163 value.memory.memchip= 0;
164 if (value.memory.memory)
166 if (value.memory.memory->is_chip())
167 value.memory.memchip=
168 (class cl_memory_chip *)(value.memory.memory);
169 if (value.memory.memory->is_address_space())
170 value.memory.address_space=
171 (class cl_address_space *)(value.memory.memory);
173 return(value.memory.memory != 0);
176 bool
177 cl_cmd_arg::as_hw(class cl_uc *uc)
179 return(false);
182 bool
183 cl_cmd_arg::as_cell(class cl_uc *uc)
185 return(false);
188 bool
189 cl_cmd_arg::as_string(void)
191 char *s= get_svalue();
192 if (!s)
193 return(false);
194 if (is_string())
195 value.string.string= proc_escape(s, &value.string.len);
196 else
198 value.string.string= strdup(s);
199 value.string.len= strlen(s);
201 return(interpreted_as_string= value.string.string != NULL);
204 bool
205 cl_cmd_arg::as_bit(class cl_uc *uc)
207 if (interpreted_as_string)
209 interpreted_as_string= false;
210 if (value.string.string)
211 free(value.string.string);
214 return(get_bit_address(uc,
215 &(value.bit.mem),
216 &(value.bit.mem_address),
217 &(value.bit.bitnr_high),
218 &(value.bit.bitnr_low)));
222 /* Integer number */
224 cl_cmd_int_arg::cl_cmd_int_arg(long addr):
225 cl_cmd_arg(addr)
228 bool
229 cl_cmd_int_arg::get_address(class cl_uc *uc, t_addr *addr)
231 long iv;
233 bool b= get_ivalue(&iv);
234 if (addr)
235 *addr= iv;
236 return(b);
239 bool
240 cl_cmd_int_arg::get_bit_address(class cl_uc *uc, // input
241 class cl_memory **mem, // outputs
242 t_addr *mem_addr,
243 int *bitnr_high,
244 int *bitnr_low)
246 t_addr bit_addr;
248 if (!get_address(uc, &bit_addr))
249 return(false);
251 if (bitnr_high)
252 *bitnr_high = -1;
253 if (bitnr_low)
254 *bitnr_low = -1;
256 if (mem)
257 if (!(*mem= uc->bit2mem(bit_addr, mem_addr, bitnr_high, bitnr_low)))
258 *mem = uc->rom;
260 return(true);
263 bool
264 cl_cmd_int_arg::as_string(void)
266 value.string.string= (char*)malloc(100);
267 sprintf(value.string.string, "%ld", i_value);
268 value.string.len= strlen(value.string.string);
269 return(interpreted_as_string= value.string.string != NULL);
273 /* Symbol */
275 cl_cmd_sym_arg::cl_cmd_sym_arg(const char *sym):
276 cl_cmd_arg(sym)
279 bool
280 cl_cmd_sym_arg::as_string(void)
282 char *s= get_svalue();
283 if (!s)
284 return(false);
285 value.string.string= strdup(s);
286 value.string.len= strlen(s);
287 return(interpreted_as_string= value.string.string != NULL);
290 bool
291 cl_cmd_sym_arg::get_address(class cl_uc *uc, t_addr *addr)
293 t_addr a;
295 if (uc->symbol2address(get_svalue(), NULL, &a))
297 if (addr)
298 *addr= a;
299 return 1;
301 return(0);
304 bool
305 cl_cmd_sym_arg::get_bit_address(class cl_uc *uc, // input
306 class cl_memory **mem, // outputs
307 t_addr *mem_addr,
308 int *bitnr_high,
309 int *bitnr_low)
311 t_index i;
312 if (uc->vars->by_name.search(get_svalue(), i))
314 class cl_var *v= (cl_var*)uc->vars->by_name.at(i);
315 if (mem)
316 *mem= v->get_mem();
317 if (mem_addr)
318 *mem_addr= v->get_addr();
319 if (bitnr_high)
320 *bitnr_high= v->bitnr_high;
321 if (bitnr_low)
322 *bitnr_low= v->bitnr_low;
323 return true;
325 return false;
328 bool
329 cl_cmd_sym_arg::as_address(class cl_uc *uc)
331 t_addr a;
332 if (uc->symbol2address(get_svalue(), NULL, &a))
334 value.address= a;
335 return true;
337 return(false);
340 bool
341 cl_cmd_sym_arg::as_hw(class cl_uc *uc)
343 cl_hw *hw, *found;
344 int i= 0;
346 hw= found= uc->get_hw(get_svalue(), &i);
347 if (!hw)
348 return(false);
349 if (hw && (strcmp(get_svalue(), "cpu")==0))
350 return value.hw= hw, true;
351 i++;
352 found= uc->get_hw(get_svalue(), &i);
353 if (found)
354 return(false);
355 value.hw= hw;
356 return(true);
359 bool
360 cl_cmd_sym_arg::as_cell(class cl_uc *uc)
362 class cl_memory *mem;
363 class cl_memory_cell *cell;
364 t_addr addr;
366 if (uc->symbol2address(get_svalue(), &mem, &addr))
368 if (mem->is_address_space())
370 value.cell= ((cl_address_space *)mem)->get_cell(addr);
371 return value.cell != NULL;
373 return false;
375 else if (uc->symbol2cell(get_svalue(), &cell))
377 value.cell= cell;
378 return true;
380 return false;
384 /* String */
386 cl_cmd_str_arg::cl_cmd_str_arg(const char *str):
387 cl_cmd_arg(str)
392 /* Bit */
394 cl_cmd_bit_arg::cl_cmd_bit_arg(class cl_cmd_arg *asfr, class cl_cmd_arg *abit_low, class cl_cmd_arg *abit_high):
395 cl_cmd_arg((long)0)
397 sfr= asfr;
398 bit_low= abit_low;
399 bit_high= abit_high;
402 cl_cmd_bit_arg::~cl_cmd_bit_arg(void)
404 if (sfr)
405 delete sfr;
406 if (bit_low)
407 delete bit_low;
408 if (bit_high)
409 delete bit_high;
412 bool
413 cl_cmd_bit_arg::get_address(class cl_uc *uc, t_addr *addr)
415 if (sfr)
416 return(sfr->get_address(uc, addr));
417 return(0);
420 bool
421 cl_cmd_bit_arg::get_bit_address(class cl_uc *uc, // input
422 class cl_memory **mem, // outputs
423 t_addr *mem_addr,
424 int *bitnr_high,
425 int *bitnr_low)
427 if (!sfr || !bitnr_high || !bitnr_low)
428 return(false);
430 if ((mem || mem_addr) && !sfr->get_bit_address(uc, mem, mem_addr, NULL, NULL))
431 return(false);
433 long l;
435 if (!bit_low ||
436 !bit_low->get_ivalue(&l) ||
437 l < 0 ||
438 l >= (long)sizeof(t_mem)*8)
439 return(false);
440 *bitnr_low = *bitnr_high = l;
442 if (bit_high)
444 if (!bit_high->get_ivalue(&l) ||
445 l < 0 ||
446 l >= (long)sizeof(t_mem)*8)
447 return(false);
448 *bitnr_high = l;
451 if (*bitnr_low > *bitnr_high)
453 int t = *bitnr_low;
454 *bitnr_low = *bitnr_high;
455 *bitnr_high = t;
458 return(true);
462 /* Array */
464 cl_cmd_array_arg::cl_cmd_array_arg(class cl_cmd_arg *aname,
465 class cl_cmd_arg *aindex):
466 cl_cmd_arg((long)0)
468 name_arg= aname;
469 index= aindex;
472 cl_cmd_array_arg::~cl_cmd_array_arg(void)
474 if (name_arg)
475 delete name_arg;
476 if (index)
477 delete index;
480 bool
481 cl_cmd_array_arg::get_bit_address(class cl_uc *uc, // input
482 class cl_memory **mem, // outputs
483 t_addr *mem_addr,
484 int *bitnr_high,
485 int *bitnr_low)
487 // address_space[address]
488 char *n= 0;
489 t_addr a;
490 if (name_arg == 0 ||
491 index == 0 ||
492 (n= name_arg->get_svalue()) == NULL ||
493 !index->get_address(uc, &a))
494 return false;
495 class cl_memory *m= uc->memory(n);
496 if (!m ||
497 !m->valid_address(a))
498 return false;
499 if (mem)
500 *mem = m;
501 if (mem_addr)
502 *mem_addr = a;
503 if (bitnr_high)
504 *bitnr_high = -1;
505 if (bitnr_low)
506 *bitnr_low = -1;
507 return true;
510 bool
511 cl_cmd_array_arg::as_hw(class cl_uc *uc)
513 char *n= 0;
514 t_addr a;
516 if (name_arg == 0 ||
517 index == 0 ||
518 (n= name_arg->get_svalue()) == NULL ||
519 !index->get_address(uc, &a))
520 return(false);
522 value.hw= uc->get_hw(n, a, NULL);
523 return(value.hw != NULL);
526 bool
527 cl_cmd_array_arg::as_cell(class cl_uc *uc)
529 class cl_memory *mem;
530 t_addr addr;
532 value.cell = NULL;
534 if (get_bit_address(uc, &mem, &addr, NULL, NULL) &&
535 mem->is_address_space())
536 value.cell= ((cl_address_space *)mem)->get_cell(addr);
538 return value.cell != NULL;
542 /* End of sim.src/arg.cc */