2 * Simulator of microcontrollers (mem.cc)
4 * Copyright (C) 1999,99 Drotos Daniel, Talker Bt.
6 * To contact author send email to drdani@mazsola.iit.uni-miskolc.hu
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
46 * Memory location handled specially by a hw element
49 cl_memloc::cl_memloc(t_addr addr):
53 hws= new cl_list(2, 2);
57 cl_memloc::~cl_memloc(void)
64 cl_memloc::read(class cl_mem *mem)
72 if ((hw= (class cl_hw *)(hws->at(0))))
73 ret= hw->read(mem, address);
78 cl_memloc::write(class cl_mem *mem, t_addr addr, t_mem *val)
85 for (i= 0; i < hws->count; i++)
87 hw= (class cl_hw *)hws->at(0);
88 hw->write(mem, addr, val);
93 /* Sorted collection of memory locations */
95 cl_memloc_coll::cl_memloc_coll(void):
102 cl_memloc_coll::key_of(void *item)
104 return(&(((class cl_memloc *)item)->address));
108 cl_memloc_coll::compare(void *key1, void *key2)
110 if (*(long*)key1 > *(long*)key2)
113 if (*(long*)key1 < *(long*)key2)
120 cl_memloc_coll::get_loc(t_addr address)
124 if (search(&address, i))
125 return((class cl_memloc*)(at(i)));
132 ******************************************************************************
135 cl_mem::cl_mem(enum mem_class atype, char *aclass_name,
136 t_addr asize, int awidth):
142 class_name= aclass_name;
146 for (i= width, mask= 0; i; i--)
149 mem= (TYPE_UBYTE *)malloc(size);
150 else if (width <= 16)
151 mem= (TYPE_UWORD *)malloc(size*sizeof(TYPE_WORD));
153 mem= (TYPE_UDWORD *)malloc(size*sizeof(TYPE_DWORD));
154 read_locs= new cl_memloc_coll();
155 write_locs= new cl_memloc_coll();
157 addr_format= (char *)malloc(10);
158 sprintf(addr_format, "0x%%0%dx",
164 (size-1<=0xffffff?6:12))))));
165 data_format= (char *)malloc(10);
166 sprintf(data_format, "%%0%dx", width/4+((width%4)?1:0));
169 cl_mem::~cl_mem(void)
186 for (i= 0; i < size; i++)
187 set(i, (type==MEM_ROM)?(-1):0);
192 cl_mem::id_string(void)
194 char *s= get_id_string(mem_ids, type);
196 return(s?s:(char*)"NONE");
200 cl_mem::read(t_addr addr)
202 class cl_memloc *loc;
207 fprintf(stderr, "Address 0x%06lx is over 0x%06lx\n", addr, size);
210 if ((loc= read_locs->get_loc(addr)))
211 return(loc->read(this));
213 return((((TYPE_UBYTE*)mem)[addr])&mask);
214 else if (width <= 16)
215 return((((TYPE_UWORD*)mem)[addr])&mask);
217 return((((TYPE_UDWORD*)mem)[addr])&mask);
221 cl_mem::get(t_addr addr)
226 return((((TYPE_UBYTE*)mem)[addr])&mask);
227 else if (width <= 16)
228 return((((TYPE_UWORD*)mem)[addr])&mask);
230 return((((TYPE_UDWORD*)mem)[addr])&mask);
235 * Modify memory location
238 /* Write calls callbacks of HW elements */
241 cl_mem::write(t_addr addr, t_mem *val)
243 class cl_memloc *loc;
247 if ((loc= write_locs->get_loc(addr)))
248 loc->write(this, addr, val);
250 ((TYPE_UBYTE*)mem)[addr]= (*val)&mask;
251 else if (width <= 16)
252 ((TYPE_UWORD*)mem)[addr]= (*val)&mask;
254 ((TYPE_UDWORD*)mem)[addr]= (*val)&mask;
257 /* Set doesn't call callbacks */
260 cl_mem::set(t_addr addr, t_mem val)
265 ((TYPE_UBYTE*)mem)[addr]= val&mask;
266 else if (width <= 16)
267 ((TYPE_UWORD*)mem)[addr]= val&mask;
269 ((TYPE_UDWORD*)mem)[addr]= val&mask;
272 /* Set or clear bits, without callbacks */
275 cl_mem::set_bit1(t_addr addr, t_mem bits)
281 ((TYPE_UBYTE*)mem)[addr]|= bits;
282 else if (width <= 16)
283 ((TYPE_UWORD*)mem)[addr]|= bits;
285 ((TYPE_UDWORD*)mem)[addr]|= bits;
289 cl_mem::set_bit0(t_addr addr, t_mem bits)
295 ((TYPE_UBYTE*)mem)[addr]&= ~bits;
296 else if (width <= 16)
297 ((TYPE_UWORD*)mem)[addr]&= ~bits;
299 ((TYPE_UDWORD*)mem)[addr]&= ~bits;
303 cl_mem::add(t_addr addr, long what)
309 ((TYPE_UBYTE*)mem)[addr]= ((TYPE_UBYTE*)mem)[addr] + what;
310 return(((TYPE_UBYTE*)mem)[addr]);
312 else if (width <= 16)
314 ((TYPE_UWORD*)mem)[addr]= ((TYPE_UWORD*)mem)[addr] + what;
315 return(((TYPE_UWORD*)mem)[addr]);
319 ((TYPE_UDWORD*)mem)[addr]= ((TYPE_UDWORD*)mem)[addr] + what;
320 return(((TYPE_UDWORD*)mem)[addr]);
325 cl_mem::dump(t_addr start, t_addr stop, int bpl, class cl_console *con)
329 while ((start <= stop) &&
332 con->printf(addr_format, start); con->printf(" ");
339 con->printf(data_format, read(start+i)); con->printf(" ");
344 j= width/4 + ((width%4)?1:0) + 1;
352 for (i= 0; (i < bpl) &&
357 long c= get(start+i);
358 con->printf("%c", isprint(255&c)?(255&c):'.');
360 con->printf("%c", isprint(255&(c>>8))?(255&(c>>8)):'.');
362 con->printf("%c", isprint(255&(c>>16))?(255&(c>>16)):'.');
364 con->printf("%c", isprint(255&(c>>24))?(255&(c>>24)):'.');
367 dump_finished= start+i;
370 return(dump_finished);
374 cl_mem::dump(class cl_console *con)
376 return(dump(dump_finished, dump_finished+10*8-1, 8, con));
380 cl_mem::search_next(bool case_sensitive, t_mem *array, int len, t_addr *addr)
399 for (i= 0; i < len && match; i++)
406 if (/*d1 < 128*/isalpha(d1))
408 if (/*d2 < 128*/isalpha(d2))
428 cl_bitmap::cl_bitmap(t_addr asize):
431 map= (uchar*)malloc(size= asize/(8*SIZEOF_CHAR));
432 memset(map, 0, size);
435 cl_bitmap::~cl_bitmap(void)
441 cl_bitmap::set(t_addr pos)
445 if ((i= pos/(8*SIZEOF_CHAR)) < size)
446 map[i]|= (1 << (pos & ((8*SIZEOF_CHAR)-1)));
450 cl_bitmap::clear(t_addr pos)
454 if ((i= pos/(8*SIZEOF_CHAR)) < size)
455 map[i]&= ~(1 << (pos & ((8*SIZEOF_CHAR)-1)));
459 cl_bitmap::get(t_addr pos)
461 return(map[pos/(8*SIZEOF_CHAR)] & (1 << (pos & ((8*SIZEOF_CHAR)-1))));
465 cl_bitmap::empty(void)
469 for (i= 0; i < size && map[i] == 0; i++) ;
474 * Special memory for code (ROM)
477 cl_rom::cl_rom(t_addr asize, int awidth):
478 cl_mem(MEM_ROM, get_id_string(mem_classes, MEM_ROM), asize, awidth)
480 bp_map= new cl_bitmap(asize);
481 inst_map= new cl_bitmap(asize);
484 cl_rom::~cl_rom(void)