6c6780d1990059e9f3c92b4b21e9db802271d553
[fw/sdcc] / sim / ucsim / xa.src / xacl.h
1 /*
2  * Simulator of microcontrollers (xacl.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  * Other contributors include:
8  *   Karl Bongers karl@turbobit.com,
9  *   Johan Knol johan.knol@iduna.nl
10  *
11  */
12
13 /* This file is part of microcontroller simulator: ucsim.
14
15 UCSIM is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
19
20 UCSIM is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 GNU General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with UCSIM; see the file COPYING.  If not, write to the Free
27 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
28 02111-1307, USA. */
29 /*@1@*/
30
31 #ifndef XACL_HEADER
32 #define XACL_HEADER
33
34 #include "uccl.h"
35
36 #include "regsxa.h"
37
38 /*
39  * Base type of XA microcontrollers
40  */
41
42 class cl_xa: public cl_uc
43 {
44 public:
45   cl_mem *ram;
46   cl_mem *rom;
47   struct t_regs regs;
48
49   class cl_mem *sfr, *iram;
50
51   // for now make it as simple as possible
52 //  TYPE_UBYTE mem_direct[1024*2];
53 //#ifndef WORDS_BIGENDIAN
54 //  TYPE_UWORD *wmem_direct;  /* word pointer at mem_direct */
55 //#endif
56
57 public:
58   cl_xa(class cl_sim *asim);
59   virtual int init(void);
60   virtual char *id_string(void);
61
62   virtual class cl_mem *mk_mem(enum mem_class type, char *class_name);
63   virtual t_addr get_mem_size(enum mem_class type);
64   virtual void mk_hw_elements(void);
65
66   virtual struct dis_entry *dis_tbl(void);
67
68   virtual struct name_entry *sfr_tbl(void);
69   virtual struct name_entry *bit_tbl(void);
70   virtual char *get_dir_name(short);
71   virtual char *get_bit_name(short);
72
73   virtual int inst_length(t_addr addr);
74   virtual int inst_branch(t_addr addr);
75   virtual int longest_inst(void);
76
77   virtual int get_disasm_info(t_addr addr,
78                        int *ret_len,
79                        int *ret_branch,
80                        int *immed_offset,
81                        int *parms,
82                        int *mnemonic);
83
84   virtual char *disass(t_addr addr, char *sep);
85   virtual void print_regs(class cl_console *con);
86
87   virtual int exec_inst(void);
88   virtual int get_reg(int word_flag, unsigned int index);
89
90   virtual void store1(t_addr addr, unsigned char val);
91   virtual void store2(t_addr addr, unsigned short val);
92   virtual unsigned char get1(t_addr addr);
93   virtual unsigned short get2(t_addr addr);
94
95   virtual bool get_bit(int bit);
96   virtual void set_bit(int bit, int value);
97
98 #include "instcl.h"
99
100   private :
101
102    /* following are macros which get substituted for FUNC1() and FUNC2()
103       in the inst.cc to form the body of ADD,ADDC,SUB,XOR,... */
104   /* can I put these in the .cc file and still have them do the inline thing? */
105   /*-------------------------------------
106     add - flags changed:C,AC,V,N,Z.
107   |---------------------------------------*/
108   inline unsigned char add1(unsigned char dst, unsigned char src)
109   {
110     unsigned int result;
111     unsigned char flags;
112     flags = get_psw();
113     flags &= ~BIT_ALL; /* clear these bits */
114     result = dst + src;
115     if (result == 0) flags |= BIT_Z;
116     if (result > 0xff) flags |= BIT_C;
117     if (result & 0x80) flags |= BIT_N;
118     /* fixme: do AC, V */
119     set_psw(flags);
120     return (unsigned char) result;
121   }
122
123   inline unsigned short add2(unsigned short dst, unsigned short src)
124   {
125     unsigned int result;
126     unsigned char flags;
127     flags = get_psw();
128     flags &= ~BIT_ALL; /* clear these bits */
129     result = dst + src;
130     if (result == 0) flags |= BIT_Z;
131     if (result > 0xff) flags |= BIT_C;
132     if (result & 0x80) flags |= BIT_N;
133     /* fixme: do AC, V */
134     set_psw(flags);
135     return (unsigned short) result;
136   }
137
138   /*-------------------------------------
139     addc - flags changed:C,AC,V,N,Z.
140   |---------------------------------------*/
141   inline unsigned char addc1(unsigned char dst, unsigned char src)
142   {
143     unsigned int result;
144     unsigned char flags;
145     flags = get_psw();
146     if (flags & BIT_C) {
147       flags &= ~BIT_ALL; /* clear these bits */
148       result = dst + src + 1;
149     } else {
150       flags &= ~BIT_ALL; /* clear these bits */
151       result = dst + src;
152     }
153     if (result == 0) flags |= BIT_Z;
154     if (result > 0xff) flags |= BIT_C;
155     if (result & 0x80) flags |= BIT_N;
156     /* fixme: do AC, V */
157     set_psw(flags);
158     return (unsigned char) result;
159   }
160
161   inline unsigned short addc2(unsigned short dst, unsigned short src)
162   {
163     unsigned int result;
164     unsigned char flags;
165     flags = get_psw();
166     flags &= ~BIT_ALL; /* clear these bits */
167     if (flags & BIT_C) {
168       flags &= ~BIT_ALL; /* clear these bits */
169       result = dst + src + 1;
170     } else {
171       flags &= ~BIT_ALL; /* clear these bits */
172       result = dst + src;
173     }
174     if (result == 0) flags |= BIT_Z;
175     if (result > 0xff) flags |= BIT_C;
176     if (result & 0x80) flags |= BIT_N;
177     /* fixme: do AC, V */
178     set_psw(flags);
179     return (unsigned short) result;
180   }
181
182   /*-------------------------------------
183     sub - flags changed:C,AC,V,N,Z.
184   |---------------------------------------*/
185   inline unsigned char sub1(unsigned char dst, unsigned char src)
186   {
187     unsigned int result;
188     unsigned char flags;
189     flags = get_psw();
190     flags &= ~BIT_ALL; /* clear these bits */
191     result = dst - src;
192     if (result == 0) flags |= BIT_Z;
193     if (result > 0xff) flags |= BIT_C;
194     if (dst < src) flags |= BIT_N;
195     /* fixme: do AC, V */
196     set_psw(flags);
197     return (unsigned char) result;
198   }
199
200   inline unsigned short sub2(unsigned short dst, unsigned short src)
201   {
202     unsigned int result;
203     unsigned char flags;
204     flags = get_psw();
205     flags &= ~BIT_ALL; /* clear these bits */
206     result = dst - src;
207     if (result == 0) flags |= BIT_Z;
208     if (result > 0xff) flags |= BIT_C;
209     if (dst < src) flags |= BIT_N;
210     /* fixme: do AC, V */
211     set_psw(flags);
212     return (unsigned short) result;
213   }
214
215   /*-------------------------------------
216     subb - flags changed:C,AC,V,N,Z.
217   |---------------------------------------*/
218   inline unsigned char subb1(unsigned char dst, unsigned char src)
219   {
220     unsigned int result;
221     unsigned char flags;
222     flags = get_psw();
223     if (flags & BIT_C) {
224       flags &= ~BIT_ALL; /* clear these bits */
225       result = dst - src - 1;
226     } else {
227       flags &= ~BIT_ALL; /* clear these bits */
228       result = dst - src;
229     }
230     if (result == 0) flags |= BIT_Z;
231     if (result > 0xff) flags |= BIT_C;
232     if (dst < src) flags |= BIT_N;
233     /* fixme: do AC, V */
234     set_psw(flags);
235     return (unsigned char) result;
236   }
237
238   inline unsigned short subb2(unsigned short dst, unsigned short src)
239   {
240     unsigned int result;
241     unsigned char flags;
242     flags = get_psw();
243     flags &= ~BIT_ALL; /* clear these bits */
244     if (flags & BIT_C) {
245       flags &= ~BIT_ALL; /* clear these bits */
246       result = dst - src - 1;
247     } else {
248       flags &= ~BIT_ALL; /* clear these bits */
249       result = dst - src;
250     }
251     if (result == 0) flags |= BIT_Z;
252     if (result > 0xff) flags |= BIT_C;
253     if (dst < src) flags |= BIT_N;
254     /* fixme: do AC, V */
255     set_psw(flags);
256     return (unsigned short) result;
257   }
258
259   /*-------------------------------------
260     cmp - flags changed:C,AC,V,N,Z.
261   |---------------------------------------*/
262   inline unsigned char cmp1(unsigned char dst, unsigned char src)
263   {
264     unsigned int result;
265     unsigned char flags;
266     flags = get_psw();
267     flags &= ~BIT_ALL; /* clear these bits */
268     result = dst - src;
269     if (result == 0) flags |= BIT_Z;
270     if (result > 0xff) flags |= BIT_C;
271     if (dst < src) flags |= BIT_N;
272     /* fixme: do AC, V */
273     set_psw(flags);
274     return (unsigned char) dst;
275   }
276
277   inline unsigned short cmp2(unsigned short dst, unsigned short src)
278   {
279     unsigned int result;
280     unsigned char flags;
281     flags = get_psw();
282     flags &= ~BIT_ALL; /* clear these bits */
283     result = dst - src;
284     if (result == 0) flags |= BIT_Z;
285     if (result > 0xff) flags |= BIT_C;
286     if (dst < src) flags |= BIT_N;
287     /* fixme: do AC, V */
288     set_psw(flags);
289     return (unsigned short) dst;
290   }
291
292   /*-------------------------------------
293     and - flags changed:N,Z.
294   |---------------------------------------*/
295   inline unsigned char and1(unsigned char dst, unsigned char src)
296   {
297     unsigned int result;
298     unsigned char flags;
299     flags = get_psw() & ~(BIT_N | BIT_Z); /* clear these bits */
300     result = dst & src;
301     if (result == 0) flags |= BIT_Z;
302     if (result & 0x80) flags |= BIT_N;
303     set_psw(flags);
304     return (unsigned char) result;
305   }
306
307   inline unsigned short and2(unsigned short dst, unsigned short src)
308   {
309     unsigned int result;
310     unsigned char flags;
311     flags = get_psw() & ~(BIT_N | BIT_Z); /* clear these bits */
312     result = dst & src;
313     if (result == 0) flags |= BIT_Z;
314     if (result & 0x80) flags |= BIT_N;
315     set_psw(flags);
316     return (unsigned short) result;
317   }
318
319   /*-------------------------------------
320     or - flags changed:N,Z.
321   |---------------------------------------*/
322   inline unsigned char or1(unsigned char dst, unsigned char src)
323   {
324     unsigned int result;
325     unsigned char flags;
326     flags = get_psw() & ~(BIT_N | BIT_Z); /* clear these bits */
327     result = dst | src;
328     if (result == 0) flags |= BIT_Z;
329     if (result & 0x80) flags |= BIT_N;
330     set_psw(flags);
331     return (unsigned char) result;
332   }
333
334   inline unsigned short or2(unsigned short dst, unsigned short src)
335   {
336     unsigned int result;
337     unsigned char flags;
338     flags = get_psw() & ~(BIT_N | BIT_Z); /* clear these bits */
339     result = dst | src;
340     if (result == 0) flags |= BIT_Z;
341     if (result & 0x80) flags |= BIT_N;
342     set_psw(flags);
343     return (unsigned short) result;
344   }
345
346   /*-------------------------------------
347     xor - flags changed:N,Z.
348   |---------------------------------------*/
349   inline unsigned char xor1(unsigned char dst, unsigned char src)
350   {
351     unsigned char flags;
352     flags = get_psw() & ~(BIT_N | BIT_Z); /* clear these bits */
353     dst ^= src;
354     if (dst == 0) flags |= BIT_Z;
355     if (dst & 0x80) flags |= BIT_N;
356     set_psw(flags);
357     return (unsigned char) dst;
358   }
359
360   inline unsigned short xor2(unsigned short dst, unsigned short src)
361   {
362     unsigned char flags;
363     flags = get_psw() & ~(BIT_N | BIT_Z); /* clear these bits */
364     dst ^= src;
365     if (dst == 0) flags |= BIT_Z;
366     if (dst & 0x8000) flags |= BIT_N;
367     set_psw(flags);
368     return (unsigned short) dst;
369   }
370
371   /*-------------------------------------
372     mov - flags changed:N,Z.
373   |---------------------------------------*/
374   inline unsigned char mov1(unsigned char dst, unsigned char src)
375   {
376     unsigned char flags;
377     flags = get_psw() & ~(BIT_N | BIT_Z); /* clear these bits */
378     dst = src;
379     if (dst == 0) flags |= BIT_Z;
380     if (dst & 0x80) flags |= BIT_N;
381     set_psw(flags);
382     return (unsigned char) dst;
383   }
384
385   inline unsigned short mov2(unsigned short dst, unsigned short src)
386   {
387     unsigned char flags;
388     flags = get_psw() & ~(BIT_N | BIT_Z); /* clear these bits */
389     dst = src;
390     if (dst == 0) flags |= BIT_Z;
391     if (dst & 0x8000) flags |= BIT_N;
392     set_psw(flags);
393     return (unsigned short) dst;
394   }
395 };
396
397 #endif
398
399 /* End of xa.src/xacl.h */