version 0.2.39: fix of arith insts and start of re-structure
[fw/sdcc] / sim / ucsim / sim.src / mem.cc
1 /*
2  * Simulator of microcontrollers (mem.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 <stdio.h>
29 #include <stdlib.h>
30 #include <ctype.h>
31 #include "i_string.h"
32
33 // prj
34 #include "utils.h"
35 #include "globals.h"
36
37 // cmd
38 #include "newcmdcl.h"
39
40 // local
41 #include "memcl.h"
42 #include "hwcl.h"
43
44
45 /*
46  * Memory location handled specially by a hw element
47  */
48
49 cl_memloc::cl_memloc(t_addr addr):
50   cl_base()
51 {
52   address= addr;
53   hws= new cl_list(2, 2);
54   hws->init();
55 }
56
57 cl_memloc::~cl_memloc(void)
58 {
59   hws->disconn_all();
60   delete hws;
61 }
62
63 ulong
64 cl_memloc::read(class cl_mem *mem)
65 {
66   uchar ret= 0;
67   class cl_hw *hw;
68
69   if (!hws ||
70       hws->count == 0)
71     return(ret);
72   if ((hw= (class cl_hw *)(hws->at(0))))
73     ret= hw->read(mem, address);
74   return(ret);
75 }
76
77 void
78 cl_memloc::write(class cl_mem *mem, t_addr addr, t_mem *val)
79 {
80   class cl_hw *hw;
81   int i;
82
83   if (!hws)
84     return;
85   for (i= 0; i < hws->count; i++)
86     {
87       hw= (class cl_hw *)hws->at(0);
88       hw->write(mem, addr, val);
89     }
90 }
91
92
93 /* Sorted collection of memory locations */
94
95 cl_memloc_coll::cl_memloc_coll(void):
96   cl_sorted_list(2, 2)
97 {
98   Duplicates= DD_FALSE;
99 }
100
101 void *
102 cl_memloc_coll::key_of(void *item)
103 {
104   return(&(((class cl_memloc *)item)->address));
105 }
106
107 int
108 cl_memloc_coll::compare(void *key1, void *key2)
109 {
110   if (*(long*)key1 > *(long*)key2)
111     return(1);
112   else
113     if (*(long*)key1 < *(long*)key2)
114       return(-1);
115     else
116       return(0);
117 }
118
119 class cl_memloc *
120 cl_memloc_coll::get_loc(t_addr address)
121 {
122   t_index i;
123
124   if (search(&address, i))
125     return((class cl_memloc*)(at(i)));
126   return(0);
127 }
128
129
130 /*
131  * Memory
132  ******************************************************************************
133  */
134
135 cl_mem::cl_mem(enum mem_class atype, char *aclass_name,
136                t_addr asize, int awidth):
137   cl_guiobj()
138 {
139   int i;
140
141   type= atype;
142   class_name= aclass_name;
143   width= awidth;
144   size= asize;
145   mem= 0;
146   for (i= width, mask= 0; i; i--)
147     mask= (mask<<1) | 1;
148   if (width <= 8)
149     mem= (TYPE_UBYTE *)malloc(size);
150   else if (width <= 16)
151     mem= (TYPE_UWORD *)malloc(size*sizeof(TYPE_WORD));
152   else
153     mem= (TYPE_UDWORD *)malloc(size*sizeof(TYPE_DWORD));
154   read_locs= new cl_memloc_coll();
155   write_locs= new cl_memloc_coll();
156   dump_finished= 0;
157   addr_format= (char *)malloc(10);
158   sprintf(addr_format, "0x%%0%dx",
159           size-1<=0xf?1:
160           (size-1<=0xff?2:
161            (size-1<=0xfff?3:
162             (size-1<=0xffff?4:
163              (size-1<=0xfffff?5:
164               (size-1<=0xffffff?6:12))))));
165   data_format= (char *)malloc(10);
166   sprintf(data_format, "%%0%dx", width/4+((width%4)?1:0));
167 }
168
169 cl_mem::~cl_mem(void)
170 {
171   if (mem)
172     free(mem);
173   if (addr_format)
174     free(addr_format);
175   if (data_format)
176     free(data_format);
177   delete read_locs;
178   delete write_locs;
179 }
180
181 int
182 cl_mem::init(void)
183 {
184   t_addr i;
185
186   for (i= 0; i < size; i++)
187     set(i, (type==MEM_ROM)?(-1):0);
188   return(0);
189 }
190
191 char *
192 cl_mem::id_string(void)
193 {
194   char *s= get_id_string(mem_ids, type);
195
196   return(s?s:(char*)"NONE");
197 }
198
199 t_mem
200 cl_mem::read(t_addr addr)
201 {
202   class cl_memloc *loc;
203
204   if (addr >= size)
205     {
206       //FIXME
207       fprintf(stderr, "Address 0x%06lx is over 0x%06lx\n", addr, size);
208       return(0);
209     }
210   if ((loc= read_locs->get_loc(addr)))
211     return(loc->read(this));
212   if (width <= 8)
213     return((((TYPE_UBYTE*)mem)[addr])&mask);
214   else if (width <= 16)
215     return((((TYPE_UWORD*)mem)[addr])&mask);
216   else
217     return((((TYPE_UDWORD*)mem)[addr])&mask);
218 }
219
220 t_mem
221 cl_mem::get(t_addr addr)
222 {
223   if (addr >= size)
224     return(0);
225   if (width <= 8)
226     return((((TYPE_UBYTE*)mem)[addr])&mask);
227   else if (width <= 16)
228     return((((TYPE_UWORD*)mem)[addr])&mask);
229   else
230     return((((TYPE_UDWORD*)mem)[addr])&mask);
231 }
232
233
234 /*
235  * Modify memory location
236  */
237
238 /* Write calls callbacks of HW elements */
239
240 void
241 cl_mem::write(t_addr addr, t_mem *val)
242 {
243   class cl_memloc *loc;
244
245   if (addr >= size)
246     return;
247   if ((loc= write_locs->get_loc(addr)))
248     loc->write(this, addr, val);
249   if (width <= 8)
250     ((TYPE_UBYTE*)mem)[addr]= (*val)&mask;
251   else if (width <= 16)
252     ((TYPE_UWORD*)mem)[addr]= (*val)&mask;
253   else
254     ((TYPE_UDWORD*)mem)[addr]= (*val)&mask;
255 }
256
257 /* Set doesn't call callbacks */
258
259 void
260 cl_mem::set(t_addr addr, t_mem val)
261 {
262   if (addr >= size)
263     return;
264   if (width <= 8)
265     ((TYPE_UBYTE*)mem)[addr]= val&mask;
266   else if (width <= 16)
267     ((TYPE_UWORD*)mem)[addr]= val&mask;
268   else
269     ((TYPE_UDWORD*)mem)[addr]= val&mask;
270 }
271
272 /* Set or clear bits, without callbacks */
273
274 void
275 cl_mem::set_bit1(t_addr addr, t_mem bits)
276 {
277   if (addr >= size)
278     return;
279   bits&= mask;
280   if (width <= 8)
281     ((TYPE_UBYTE*)mem)[addr]|= bits;
282   else if (width <= 16)
283     ((TYPE_UWORD*)mem)[addr]|= bits;
284   else
285     ((TYPE_UDWORD*)mem)[addr]|= bits;
286 }
287
288 void
289 cl_mem::set_bit0(t_addr addr, t_mem bits)
290 {
291   if (addr >= size)
292     return;
293   bits&= mask;
294   if (width <= 8)
295     ((TYPE_UBYTE*)mem)[addr]&= ~bits;
296   else if (width <= 16)
297     ((TYPE_UWORD*)mem)[addr]&= ~bits;
298   else
299     ((TYPE_UDWORD*)mem)[addr]&= ~bits;
300 }
301
302 t_mem
303 cl_mem::add(t_addr addr, long what)
304 {
305   if (addr >= size)
306     return(0);
307   if (width <= 8)
308     {
309       ((TYPE_UBYTE*)mem)[addr]= ((TYPE_UBYTE*)mem)[addr] + what;
310       return(((TYPE_UBYTE*)mem)[addr]);
311     }
312   else if (width <= 16)
313     {
314       ((TYPE_UWORD*)mem)[addr]= ((TYPE_UWORD*)mem)[addr] + what;
315       return(((TYPE_UWORD*)mem)[addr]);
316     }
317   else
318     {
319       ((TYPE_UDWORD*)mem)[addr]= ((TYPE_UDWORD*)mem)[addr] + what;
320       return(((TYPE_UDWORD*)mem)[addr]);
321     }
322 }
323
324 t_addr
325 cl_mem::dump(t_addr start, t_addr stop, int bpl, class cl_console *con)
326 {
327   int i;
328
329   while ((start <= stop) &&
330          (start < size))
331     {
332       con->printf(addr_format, start); con->printf(" ");
333       for (i= 0;
334            (i < bpl) &&
335              (start+i < size) &&
336              (start+i <= stop);
337            i++)
338         {
339           con->printf(data_format, read(start+i)); con->printf(" ");
340         }
341       while (i < bpl)
342         {
343           int j;
344           j= width/4 + ((width%4)?1:0) + 1;
345           while (j)
346             {
347               con->printf(" ");
348               j--;
349             }
350           i++;
351         }
352       for (i= 0; (i < bpl) &&
353              (start+i < size) &&
354              (start+i <= stop);
355            i++)
356         {
357           long c= get(start+i);
358           con->printf("%c", isprint(255&c)?(255&c):'.');
359           if (width > 8)
360             con->printf("%c", isprint(255&(c>>8))?(255&(c>>8)):'.');
361           if (width > 16)
362             con->printf("%c", isprint(255&(c>>16))?(255&(c>>16)):'.');
363           if (width > 24)
364             con->printf("%c", isprint(255&(c>>24))?(255&(c>>24)):'.');
365         }
366       con->printf("\n");
367       dump_finished= start+i;
368       start+= bpl;
369     }
370   return(dump_finished);
371 }
372
373 t_addr
374 cl_mem::dump(class cl_console *con)
375 {
376   return(dump(dump_finished, dump_finished+10*8-1, 8, con));
377 }
378
379 bool
380 cl_mem::search_next(bool case_sensitive, t_mem *array, int len, t_addr *addr)
381 {
382   t_addr a;
383   int i;
384   bool found;
385
386   if (addr == NULL)
387     a= 0;
388   else
389     a= *addr;
390   
391   if (a+len > size)
392     return(DD_FALSE);
393
394   found= DD_FALSE;
395   while (!found &&
396          a+len <= size)
397     {
398       bool match= DD_TRUE;
399       for (i= 0; i < len && match; i++)
400         {
401           t_mem d1, d2;
402           d1= get(a+i);
403           d2= array[i];
404           if (!case_sensitive)
405             {
406               if (/*d1 < 128*/isalpha(d1))
407                 d1= toupper(d1);
408               if (/*d2 < 128*/isalpha(d2))
409                 d2= toupper(d2);
410             }
411           match= d1 == d2;
412         }
413       found= match;
414       if (!found)
415         a++;
416     }
417
418   if (addr)
419     *addr= a;
420   return(found);
421 }
422
423
424 /*
425  * Bitmap
426  */
427
428 cl_bitmap::cl_bitmap(t_addr asize):
429   cl_base()
430 {
431   map= (uchar*)malloc(size= asize/(8*SIZEOF_CHAR));
432   memset(map, 0, size);
433 }
434
435 cl_bitmap::~cl_bitmap(void)
436 {
437   free(map);
438 }
439
440 void
441 cl_bitmap::set(t_addr pos)
442 {
443   int i;
444
445   if ((i= pos/(8*SIZEOF_CHAR)) < size)
446     map[i]|= (1 << (pos & ((8*SIZEOF_CHAR)-1)));
447 }
448
449 void
450 cl_bitmap::clear(t_addr pos)
451 {
452   int i;
453
454   if ((i= pos/(8*SIZEOF_CHAR)) < size)
455     map[i]&= ~(1 << (pos & ((8*SIZEOF_CHAR)-1)));
456 }
457
458 bool
459 cl_bitmap::get(t_addr pos)
460 {
461   return(map[pos/(8*SIZEOF_CHAR)] & (1 << (pos & ((8*SIZEOF_CHAR)-1))));
462 }
463
464 bool
465 cl_bitmap::empty(void)
466 {
467   int i;
468
469   for (i= 0; i < size && map[i] == 0; i++) ;
470   return(i == size);
471 }
472
473 /*
474  * Special memory for code (ROM)
475  */
476
477 cl_rom::cl_rom(t_addr asize, int awidth):
478   cl_mem(MEM_ROM, get_id_string(mem_classes, MEM_ROM), asize, awidth)
479 {
480   bp_map= new cl_bitmap(asize);
481   inst_map= new cl_bitmap(asize);
482 }
483
484 cl_rom::~cl_rom(void)
485 {
486   delete bp_map;
487   delete inst_map;
488 }
489
490
491 /* End of mem.cc */