version 0.5.2
[fw/sdcc] / sim / ucsim / sim.src / memcl.h
1 /*
2  * Simulator of microcontrollers (sim.src/memcl.h)
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 #ifndef SIM_MEMCL_HEADER
29 #define SIM_MEMCL_HEADER
30
31 #include "ddconfig.h"
32
33 // prj
34 #include "stypes.h"
35 #include "pobjcl.h"
36
37 // gui.src
38 #include "guiobjcl.h"
39
40
41 class cl_event_handler;
42
43
44 // Cell types
45 #define CELL_NORMAL     0x00    /* Nothing special */
46 #define CELL_HW_READ    0x01    /* Hw handles read */
47 #define CELL_HW_WRITE   0x02    /* Hw catches write */
48 //#define CELL_INST     0x04    /* Marked as instruction */
49 //#define CELL_FETCH_BRK        0x08    /* Fetch breakpoint */
50 #define CELL_READ_BRK   0x10    /* Read event breakpoint */
51 #define CELL_WRITE_BRK  0x20    /* Write event breakpoint */
52
53 // Cell flags
54 enum cell_flag {
55   CELL_NONE             = 0x00,
56   CELL_INST             = 0x04, /* Marked as instruction */
57   CELL_FETCH_BRK        = 0x08, /* Fetch breakpoint */
58   CELL_NON_DECODED      = 0x40  /* Cell is not decoded (yet) */
59 };
60
61 #define CELL_GENERAL    (CELL_NORMAL|CELL_INST|CELL_FETCH_BRK)
62
63
64 /*
65  * 3rd version of memory system
66  */
67
68 class cl_memory: public cl_base
69 {
70 public:
71   t_addr start_address;
72 protected:
73   class cl_uc *uc;
74   t_addr size;
75 public:
76   char *addr_format, *data_format;
77   int width; // in bits
78   t_mem data_mask;
79
80   //char *class_name; // used by cl_m!!
81 protected:
82   t_addr dump_finished;
83 public:
84   cl_memory(char *id, t_addr asize, int awidth);
85   virtual ~cl_memory(void);
86   virtual int init(void);
87
88   t_addr get_start_address(void) { return(start_address); }
89   t_addr get_size(void) { return(size); }
90   virtual void set_uc(class cl_uc *auc) { uc= auc; }
91   virtual bool valid_address(t_addr addr);
92   virtual t_addr inc_address(t_addr addr, int val);
93   virtual t_addr inc_address(t_addr addr);
94   virtual t_addr validate_address(t_addr addr);
95
96   virtual bool is_chip(void) { return(DD_FALSE); }
97   virtual bool is_address_space(void) { return(DD_FALSE); }
98
99   virtual void err_inv_addr(t_addr addr);
100   virtual void err_non_decoded(t_addr addr);
101
102   virtual t_addr dump(t_addr start, t_addr stop, int bpl,
103                       class cl_console *con);
104   virtual t_addr dump(class cl_console *con);
105   virtual bool search_next(bool case_sensitive,
106                            t_mem *array, int len, t_addr *addr);
107
108
109   virtual t_addr lowest_valid_address(void) { return(start_address); }
110   virtual t_addr highest_valid_address(void) { return(start_address+size-1); }
111
112
113   virtual t_mem read(t_addr addr)=0;
114   virtual t_mem read(t_addr addr, enum hw_cath skip)=0;
115   virtual t_mem get(t_addr addr)=0;
116   virtual t_mem write(t_addr addr, t_mem val)=0;
117   virtual void set(t_addr addr, t_mem val)=0;
118   virtual void set_bit1(t_addr addr, t_mem bits)=0;
119   virtual void set_bit0(t_addr addr, t_mem bits)=0;
120 };
121
122
123 /*
124  * Operators for memory cells
125  */
126
127 class cl_memory_operator: public cl_base
128 {
129 protected:
130   t_addr address;
131   t_mem *data;
132   t_mem mask;
133   class cl_memory_operator *next_operator;
134   class cl_memory_cell *cell;
135 public:
136   cl_memory_operator(class cl_memory_cell *acell, t_addr addr);
137   cl_memory_operator(class cl_memory_cell *acell, t_addr addr,
138                      t_mem *data_place, t_mem the_mask);
139
140   virtual void set_data(t_mem *data_place, t_mem the_mask);
141   virtual class cl_memory_operator *get_next(void) { return(next_operator); }
142   virtual void set_next(class cl_memory_operator *next) { next_operator= next;}
143
144   virtual bool match(class cl_hw *the_hw) { return(DD_FALSE); }
145   virtual bool match(class cl_brk *brk) { return(DD_FALSE); }
146
147   virtual t_mem read(void);
148   virtual t_mem read(enum hw_cath skip) { return(read()); }
149   virtual t_mem write(t_mem val);
150 };
151
152 class cl_hw_operator: public cl_memory_operator
153 {
154 protected:
155   class cl_hw *hw;
156 public:
157   cl_hw_operator(class cl_memory_cell *acell, t_addr addr,
158                  t_mem *data_place, t_mem the_mask, class cl_hw *ahw);
159
160   virtual bool match(class cl_hw *the_hw) { return(hw == the_hw); }
161
162   virtual t_mem read(void);
163   virtual t_mem read(enum hw_cath skip);
164   virtual t_mem write(t_mem val);
165 };
166
167 class cl_event_break_operator: public cl_memory_operator
168 {
169 protected:
170   class cl_uc *uc;
171   class cl_brk *bp;
172 public:
173   cl_event_break_operator(class cl_memory_cell *acell, t_addr addr,
174                           t_mem *data_place, t_mem the_mask,
175                           class cl_uc *auc, class cl_brk *the_bp):
176     cl_memory_operator(acell, addr, data_place, the_mask)
177   {
178     uc= auc;
179     bp= the_bp;
180   }
181
182   virtual bool match(class cl_brk *brk) { return(bp == brk); }
183 };
184
185 class cl_write_operator: public cl_event_break_operator
186 {
187 public:
188   cl_write_operator(class cl_memory_cell *acell, t_addr addr,
189                     t_mem *data_place, t_mem the_mask,
190                     class cl_uc *auc, class cl_brk *the_bp);
191
192   virtual t_mem write(t_mem val);
193 };
194
195 class cl_read_operator: public cl_event_break_operator
196 {
197 public:
198   cl_read_operator(class cl_memory_cell *acell, t_addr addr,
199                    t_mem *data_place, t_mem the_mask,
200                    class cl_uc *auc, class cl_brk *the_bp);
201
202   virtual t_mem read(void);
203 };
204
205
206 /*
207  * version 3 of cell
208  */
209
210 class cl_memory_cell: public cl_base
211 {
212 #ifdef STATISTIC
213 public:
214   unsigned long nuof_writes, nuof_reads;
215 #endif
216 protected:
217   t_mem *data;
218   t_mem mask;
219   uchar width;
220   TYPE_UBYTE flags;
221   class cl_memory_operator *operators;
222 public:
223   cl_memory_cell(void);
224   virtual ~cl_memory_cell(void);
225   virtual int init(void);
226
227   virtual t_mem *get_data(void) { return(data); }
228   virtual t_mem get_mask(void) { return(mask); }
229   virtual TYPE_UBYTE get_flags(void);
230   virtual bool get_flag(enum cell_flag flag);
231   virtual void set_flags(TYPE_UBYTE what);
232   virtual void set_flag(enum cell_flag flag, bool val);
233
234   virtual void un_decode(void);
235   virtual void decode(class cl_memory_chip *chip, t_addr addr);
236
237   virtual t_mem read(void);
238   virtual t_mem read(enum hw_cath skip);
239   virtual t_mem get(void);
240   virtual t_mem write(t_mem val);
241   virtual t_mem set(t_mem val);
242
243   virtual t_mem add(long what);
244   virtual t_mem wadd(long what);
245
246   virtual void set_bit1(t_mem bits);
247   virtual void set_bit0(t_mem bits);
248
249   virtual void append_operator(class cl_memory_operator *op);
250   virtual void prepend_operator(class cl_memory_operator *op);
251   virtual void del_operator(class cl_brk *brk);
252
253   virtual class cl_memory_cell *add_hw(class cl_hw *hw, int *ith, t_addr addr);
254   //virtual class cl_hw *get_hw(int ith);
255   virtual class cl_event_handler *get_event_handler(void);
256 };
257
258 class cl_dummy_cell: public cl_memory_cell
259 {
260 public:
261   cl_dummy_cell(void): cl_memory_cell() {}
262
263   virtual t_mem write(t_mem val);
264   virtual t_mem set(t_mem val);
265 };
266
267
268 /*
269  * Address space
270  */
271
272 class cl_memory_chip;
273
274 class cl_address_space: public cl_memory
275 {
276 protected:
277   class cl_memory_cell **cells, *dummy;
278 public:
279   class cl_decoder_list *decoders;
280 public:
281   cl_address_space(char *id, t_addr astart, t_addr asize, int awidth);
282   virtual ~cl_address_space(void);
283
284   virtual bool is_address_space(void) { return(DD_TRUE); }
285
286   virtual t_mem read(t_addr addr);
287   virtual t_mem read(t_addr addr, enum hw_cath skip);
288   virtual t_mem get(t_addr addr);
289   virtual t_mem write(t_addr addr, t_mem val);
290   virtual void set(t_addr addr, t_mem val);
291   virtual t_mem wadd(t_addr addr, long what);
292   virtual void set_bit1(t_addr addr, t_mem bits);
293   virtual void set_bit0(t_addr addr, t_mem bits);
294
295   virtual class cl_memory_cell *get_cell(t_addr addr);
296   virtual int get_cell_flag(t_addr addr);
297   virtual bool get_cell_flag(t_addr addr, enum cell_flag flag);
298   virtual void set_cell_flag(t_addr addr, bool set_to, enum cell_flag flag);
299
300   virtual bool decode_cell(t_addr addr,
301                            class cl_memory_chip *chip, t_addr chipaddr);
302   virtual void undecode_cell(t_addr addr);
303   virtual void undecode_area(class cl_address_decoder *skip,
304                              t_addr begin, t_addr end, class cl_console *con);
305
306   virtual class cl_memory_cell *register_hw(t_addr addr, class cl_hw *hw,
307                                             int *ith,
308                                             bool announce);
309
310   virtual void set_brk(t_addr addr, class cl_brk *brk);
311   virtual void del_brk(t_addr addr, class cl_brk *brk);
312
313 #ifdef STATISTIC
314   virtual unsigned long get_nuof_reads(void) { return(0); }
315   virtual unsigned long get_nuof_writes(void) { return(0); }
316   virtual void set_nuof_reads(unsigned long value) {}
317   virtual void set_nuof_writes(unsigned long value) {}
318 #endif
319 };
320
321 class cl_address_space_list: public cl_list
322 {
323 protected:
324   class cl_uc *uc;
325 public:
326   cl_address_space_list(class cl_uc *the_uc);
327   virtual t_index add(class cl_address_space *mem);
328 };
329
330
331 /*
332  * Memory chip (storage)
333  */
334
335 class cl_memory_chip: public cl_memory
336 {
337 protected:
338   t_mem *array;
339   int init_value;
340 public:
341   cl_memory_chip(char *id, int asize, int awidth, int initial= -1);
342   virtual ~cl_memory_chip(void);
343   virtual int init(void);
344
345   virtual bool is_chip(void) { return(DD_TRUE); }
346
347   virtual t_mem *get_slot(t_addr addr);
348
349   virtual t_mem read(t_addr addr) { return(get(addr)); }
350   virtual t_mem read(t_addr addr, enum hw_cath skip) { return(get(addr)); }
351   virtual t_mem get(t_addr addr);
352   virtual t_mem write(t_addr addr, t_mem val) { set(addr, val); return(val); }
353   virtual void set(t_addr addr, t_mem val);
354   virtual void set_bit1(t_addr addr, t_mem bits);
355   virtual void set_bit0(t_addr addr, t_mem bits);
356 };
357
358
359 /*
360  * Address decoder
361  */
362
363 class cl_address_decoder: public cl_base
364 {
365 public:
366   class cl_address_space *address_space;
367   class cl_memory_chip *memchip;
368   t_addr as_begin, as_end;
369   t_addr chip_begin;
370   bool activated;
371 public:
372   cl_address_decoder(class cl_memory *as, class cl_memory *chip,
373                      t_addr asb, t_addr ase, t_addr cb);
374   virtual ~cl_address_decoder(void);
375   virtual int init(void);
376
377   virtual bool activate(class cl_console *con);
378
379   virtual bool fully_covered_by(t_addr begin, t_addr end);
380   virtual bool is_in(t_addr begin, t_addr end);
381   virtual bool covers(t_addr begin, t_addr end);
382
383   virtual bool shrink_out_of(t_addr begin, t_addr end);
384   virtual class cl_address_decoder *split(t_addr begin, t_addr end);
385 };
386
387 /* List of address decoders */
388
389 class cl_decoder_list: public cl_sorted_list
390 {
391 protected:
392   bool by_chip;
393 public:
394   cl_decoder_list(t_index alimit, t_index adelta, bool bychip);
395
396   virtual void *key_of(void *item);
397   virtual int compare(void *key1, void *key2);
398 };
399
400
401 /*
402  * Messages
403  */
404
405 #include "eventcl.h"
406
407 class cl_event_address_space_added: public cl_event
408 {
409 public:
410   class cl_address_space *as;
411   cl_event_address_space_added(class cl_address_space *the_as):
412     cl_event(ev_address_space_added)
413   { as= the_as; }
414 };
415
416
417 /*
418  * Errors in memory handling
419  */
420
421 #include "errorcl.h"
422
423 ERROR_CLASS_DECL(mem): public cl_error
424 {
425 protected:
426   class cl_memory *mem;
427   t_addr addr;
428 public:
429   cl_error_mem(class cl_memory *amem, t_addr aaddr);
430 };
431
432 ERROR_CLASS_DECL(mem_invalid_address): public cl_error_mem
433 {
434 public:
435   cl_error_mem_invalid_address(class cl_memory *amem, t_addr aaddr);
436
437   virtual void print(class cl_commander *c);
438 };
439
440 ERROR_CLASS_DECL(mem_non_decoded): public cl_error_mem
441 {
442 public:
443   cl_error_mem_non_decoded(class cl_memory *amem, t_addr aaddr);
444
445   virtual void print(class cl_commander *c);
446 };
447
448
449 #endif
450
451 /* End of memcl.h */