Imported Upstream version 2.9.0
[debian/cc1111] / sim / ucsim / sim.src / arg.cc
1 /*
2  * Simulator of microcontrollers (arg.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 #include <stdio.h>
31 #include <stdlib.h>
32 #include "i_string.h"
33
34 // prj
35 #include "globals.h"
36
37 // sim
38 #include "simcl.h"
39
40 // cmd
41 #include "cmdutil.h"
42
43 // local
44 #include "argcl.h"
45
46
47 /*
48  * Making the argument
49  */
50
51 cl_arg::cl_arg(long lv):
52   cl_base()
53 {
54   i_value= lv;
55   s_value= 0;
56 }
57
58 cl_arg::cl_arg(const char *sv):
59   cl_base()
60 {
61   s_value= sv?strdup(sv):0;
62 }
63
64 cl_arg::cl_arg(double fv):
65   cl_base()
66 {
67   f_value= fv;
68   s_value= 0;
69 }
70
71 cl_arg::cl_arg(void *pv):
72   cl_base()
73 {
74   p_value= pv;
75   s_value= 0;
76 }
77
78 cl_arg::~cl_arg(void)
79 {
80   if (s_value)
81     free(s_value);
82 }
83
84
85 /*
86  * Getting value of the argument
87  */
88
89 bool
90 cl_arg::get_ivalue(long *value)
91 {
92   if (value)
93     *value= i_value;
94   return(DD_TRUE);
95 }
96
97 char *
98 cl_arg::get_svalue(void)
99 {
100   return(s_value);
101 }
102
103 double
104 cl_arg::get_fvalue(void)
105 {
106   return(f_value);
107 }
108
109 void *
110 cl_arg::get_pvalue(void)
111 {
112   return(p_value);
113 }
114
115
116 /*
117  * Command parameters
118  *----------------------------------------------------------------------------
119  */
120
121 cl_cmd_arg::~cl_cmd_arg(void)
122 {
123   if (interpreted_as_string)
124     {
125       if (value.string.string)
126         free(value.string.string);
127     }
128 }
129
130 bool
131 cl_cmd_arg::as_address(class cl_uc *uc)
132 {
133   return(get_address(uc, &(value.address)));    
134 }
135
136 bool
137 cl_cmd_arg::as_number(void)
138 {
139   return(get_ivalue(&(value.number)));
140 }
141
142 bool
143 cl_cmd_arg::as_data(void)
144 {
145   long l;
146   bool ret= get_ivalue(&l);
147   value.data= l;
148   return(ret);
149 }
150
151 bool
152 cl_cmd_arg::as_memory(class cl_uc *uc)
153 {
154   value.memory.memory= uc->memory(s_value);
155   value.memory.address_space= 0;
156   value.memory.memchip= 0;
157   if (value.memory.memory)
158     {
159       if (value.memory.memory->is_chip())
160         value.memory.memchip=
161           dynamic_cast<class cl_memory_chip *>(value.memory.memory);
162       if (value.memory.memory->is_address_space())
163         value.memory.address_space=
164           dynamic_cast<class cl_address_space *>(value.memory.memory);
165     }
166   return(value.memory.memory != 0);
167 }
168
169 bool
170 cl_cmd_arg::as_hw(class cl_uc *uc)
171 {
172   return(DD_FALSE);
173 }
174
175 bool
176 cl_cmd_arg::as_string(void)
177 {
178   char *s= get_svalue();
179   if (!s)
180     return(DD_FALSE);
181   if (is_string())
182     value.string.string= proc_escape(s, &value.string.len);
183   else
184     {
185       value.string.string= strdup(s);
186       value.string.len= strlen(s);
187     }
188   return(interpreted_as_string= value.string.string != NULL);
189 }
190
191 bool
192 cl_cmd_arg::as_bit(class cl_uc *uc)
193 {
194   return(get_bit_address(uc,
195                          &(value.bit.mem),
196                          &(value.bit.mem_address),
197                          &(value.bit.mask)));
198 }
199
200
201 /* Interger number */
202
203 cl_cmd_int_arg::cl_cmd_int_arg(/*class cl_uc *iuc,*/ long addr):
204   cl_cmd_arg(/*iuc,*/ addr)
205 {}
206
207 bool
208 cl_cmd_int_arg::get_address(class cl_uc *uc, t_addr *addr)
209 {
210   long iv;
211
212   bool b= get_ivalue(&iv);
213   if (addr)
214     *addr= iv;
215   return(b);
216 }
217
218 bool
219 cl_cmd_int_arg::get_bit_address(class cl_uc *uc, // input
220                                 class cl_address_space **mem, // outputs
221                                 t_addr *mem_addr,
222                                 t_mem *bit_mask)
223 {
224   t_addr bit_addr;
225
226   if (!get_address(uc, &bit_addr))
227     return(DD_FALSE);
228   
229   if (mem)
230     *mem= uc->bit2mem(bit_addr, mem_addr, bit_mask);
231   return(mem && *mem);
232 }
233
234 bool
235 cl_cmd_int_arg::as_string(void)
236 {
237   value.string.string= (char*)malloc(100);
238   sprintf(value.string.string, "%ld", i_value);
239   value.string.len= strlen(value.string.string);
240   return(interpreted_as_string= value.string.string != NULL);
241 }
242
243
244 /* Symbol */
245
246 cl_cmd_sym_arg::cl_cmd_sym_arg(/*class cl_uc *iuc,*/ const char *sym):
247   cl_cmd_arg(/*iuc,*/ sym)
248 {}
249
250 bool
251 cl_cmd_sym_arg::as_string(void)
252 {
253   char *s= get_svalue();
254   if (!s)
255     return(DD_FALSE);
256   value.string.string= strdup(s);
257   value.string.len= strlen(s);
258   return(interpreted_as_string= value.string.string != NULL);
259 }
260
261 bool
262 cl_cmd_sym_arg::get_address(class cl_uc *uc, t_addr *addr)
263 {
264   struct name_entry *ne;
265
266   if ((ne= get_name_entry(uc->sfr_tbl(),
267                           get_svalue(),
268                           uc)) != NULL)
269     {
270       if (addr)
271         *addr= ne->addr;
272       return(1);
273     }
274   return(0);
275 }
276
277 bool
278 cl_cmd_sym_arg::get_bit_address(class cl_uc *uc, // input
279                                 class cl_address_space **mem, // outputs
280                                 t_addr *mem_addr,
281                                 t_mem *bit_mask)
282 {
283   struct name_entry *ne;
284
285   ne= get_name_entry(uc->bit_tbl(), get_svalue(), uc);
286   if (ne == NULL)
287     return(DD_FALSE);
288   if (mem)
289     *mem= uc->bit2mem(ne->addr, mem_addr, bit_mask);
290   return(mem && *mem);
291 }
292
293 bool
294 cl_cmd_sym_arg::as_address(class cl_uc *uc)
295 {
296   struct name_entry *ne;
297   //printf("SYM %s as addr?\n",get_svalue());
298   if ((ne= get_name_entry(uc->sfr_tbl(), get_svalue(), uc)) != NULL)
299     {
300       value.address= ne->addr;
301       return(DD_TRUE);
302     }
303   return(DD_FALSE);
304 }
305
306 bool
307 cl_cmd_sym_arg::as_hw(class cl_uc *uc)
308 {
309   cl_hw *hw, *found;
310   int i= 0;
311
312   hw= found= uc->get_hw(get_svalue(), &i);
313   if (!hw)
314     return(DD_FALSE);
315   i++;
316   found= uc->get_hw(get_svalue(), &i);
317   if (found)
318     return(DD_FALSE);
319   value.hw= hw;
320   return(DD_TRUE);
321 }
322
323
324 /* String */
325
326 cl_cmd_str_arg::cl_cmd_str_arg(/*class cl_uc *iuc,*/ const char *str):
327   cl_cmd_arg(/*iuc,*/ str)
328 {
329 }
330
331
332 /* Bit */
333
334 cl_cmd_bit_arg::cl_cmd_bit_arg(/*class cl_uc *iuc,*/
335                                class cl_cmd_arg *asfr, class cl_cmd_arg *abit):
336   cl_cmd_arg(/*iuc,*/ (long)0)
337 {
338   sfr= asfr;
339   bit= abit;
340 }
341
342 cl_cmd_bit_arg::~cl_cmd_bit_arg(void)
343 {
344   if (sfr)
345     delete sfr;
346   if (bit)
347     delete bit;
348 }
349
350 bool
351 cl_cmd_bit_arg::get_address(class cl_uc *uc, t_addr *addr)
352 {
353   if (sfr)
354     return(sfr->get_address(uc, addr));
355   return(0);
356 }
357
358 bool
359 cl_cmd_bit_arg::get_bit_address(class cl_uc *uc, // input
360                                 class cl_address_space **mem, // outputs
361                                 t_addr *mem_addr,
362                                 t_mem *bit_mask)
363 {
364   if (mem)
365     {
366       *mem= uc->address_space(MEM_SFR_ID);
367       if (!*mem)
368         return(DD_FALSE);
369     }
370   if (mem_addr)
371     {
372       if (!sfr ||
373           !sfr->get_address(uc, mem_addr))
374         return(DD_FALSE);
375     }
376   if (bit_mask)
377     {
378       if (!bit)
379         return(DD_FALSE);
380       long l;
381       if (!bit->get_ivalue(&l) ||
382           l > 7)
383         return(DD_FALSE);
384       *bit_mask= 1 << l;
385     }
386   return(DD_TRUE);
387 }
388
389
390 /* Array */
391
392 cl_cmd_array_arg::cl_cmd_array_arg(/*class cl_uc *iuc,*/
393                                    class cl_cmd_arg *aname,
394                                    class cl_cmd_arg *aindex):
395   cl_cmd_arg(/*iuc,*/ (long)0)
396 {
397   name_arg= aname;
398   index= aindex;
399 }
400
401 cl_cmd_array_arg::~cl_cmd_array_arg(void)
402 {
403   if (name_arg)
404     delete name_arg;
405   if (index)
406     delete index;
407 }
408
409 bool
410 cl_cmd_array_arg::as_hw(class cl_uc *uc)
411 {
412   char *n;
413   t_addr a;
414
415   if (name_arg == 0 ||
416       index == 0 ||
417       (n= name_arg->get_svalue()) == NULL ||
418       !index->get_address(uc, &a))
419     return(DD_FALSE);
420   
421   value.hw= uc->get_hw(n, a, NULL);
422   return(value.hw != NULL);
423 }
424
425
426 /*
427  * Program arguments
428  *----------------------------------------------------------------------------
429  */
430 /*
431 cl_prg_arg::cl_prg_arg(char sn, char *ln, long lv):
432   cl_arg(lv)
433 {
434   short_name= sn;
435   long_name = ln?strdup(ln):0;
436 }
437
438 cl_prg_arg::cl_prg_arg(char sn, char *ln, char *sv):
439   cl_arg(sv)
440 {
441   short_name= sn;
442   long_name = ln?strdup(ln):0;
443 }
444
445 cl_prg_arg::cl_prg_arg(char sn, char *ln, double fv):
446   cl_arg(fv)
447 {
448   short_name= sn;
449   long_name = ln?strdup(ln):0;
450 }
451
452 cl_prg_arg::cl_prg_arg(char sn, char *ln, void *pv):
453   cl_arg(pv)
454 {
455   short_name= sn;
456   long_name = ln?strdup(ln):0;
457 }
458
459 cl_prg_arg::~cl_prg_arg(void)
460 {
461   if (long_name)
462     free(long_name);
463 }
464 */
465
466 /*
467  * List of arguments
468  *----------------------------------------------------------------------------
469  */
470 /*
471 int
472 cl_arguments::arg_avail(char nam)
473 {
474   class cl_prg_arg *a;
475   int i;
476
477   for (i= 0; i < count; i++)
478     {
479       a= (class cl_prg_arg *)(at(i));
480       if (a->short_name == nam)
481         return(1);
482     }
483   return(0);
484 }
485
486 int
487 cl_arguments::arg_avail(char *nam)
488 {
489   class cl_prg_arg *a;
490   int i;
491
492   for (i= 0; i < count; i++)
493     {
494       a= (class cl_prg_arg *)(at(i));
495       if (a->long_name &&
496           strcmp(a->long_name, nam) == 0)
497         return(1);
498     }
499   return(0);
500 }
501
502 long
503 cl_arguments::get_iarg(char sname, char *lname)
504 {
505   class cl_prg_arg *a;
506   int i;
507
508   for (i= 0; i < count; i++)
509     {
510       a= (class cl_prg_arg *)(at(i));
511       if ((sname && a->short_name == sname) ||
512           (lname && a->long_name && strcmp(a->long_name, lname) == 0))
513         {
514           long iv;
515           if (a->get_ivalue(&iv))
516             return(iv);
517           else
518             //FIXME
519             return(0);
520         }
521     }
522   return(0);
523 }
524
525 char *
526 cl_arguments::get_sarg(char sname, char *lname)
527 {
528   class cl_prg_arg *a;
529   int i;
530
531   for (i= 0; i < count; i++)
532     {
533       a= (class cl_prg_arg *)(at(i));
534       if ((sname && a->short_name == sname) ||
535           (lname && a->long_name && strcmp(a->long_name, lname) == 0))
536         return(a->get_svalue());
537     }
538   return(0);
539 }
540
541
542 double
543 cl_arguments::get_farg(char sname, char *lname)
544 {
545   class cl_prg_arg *a;
546   int i;
547
548   for (i= 0; i < count; i++)
549     {
550       a= (class cl_prg_arg *)(at(i));
551       if ((sname && a->short_name == sname) ||
552           (lname && a->long_name && strcmp(a->long_name, lname) == 0))
553         return(a->get_fvalue());
554     }
555   return(0);
556 }
557
558 void *
559 cl_arguments::get_parg(char sname, char *lname)
560 {
561   class cl_prg_arg *a;
562   int i;
563
564   for (i= 0; i < count; i++)
565     {
566       a= (class cl_prg_arg *)(at(i));
567       if ((sname && a->short_name == sname) ||
568           (lname && a->long_name && strcmp(a->long_name, lname) == 0))
569         return(a->get_pvalue());
570     }
571   return(0);
572 }
573 */
574
575 /* End of arg.cc */