cd7f23525acd9639c8f52ab2c89a6d4f14e42201
[fw/sdcc] / sim / ucsim / xa.src / inst_gen.cc
1 /*
2  * Simulator of microcontrollers (inst_gen.cc)
3  * this code pulled into various parts
4    of inst.cc with FUNC1 and FUNC2 defined as
5    various operations to implement ADD, ADDC, ...
6  *
7  * Copyright (C) 1999,2002 Drotos Daniel, Talker Bt.
8  * 
9  * To contact author send email to drdani@mazsola.iit.uni-miskolc.hu
10  * Other contributors include:
11  *   Karl Bongers karl@turbobit.com,
12  *   Johan Knol 
13  *
14  */
15
16 /* This file is part of microcontroller simulator: ucsim.
17
18 UCSIM is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 2 of the License, or
21 (at your option) any later version.
22
23 UCSIM is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26 GNU General Public License for more details.
27
28 You should have received a copy of the GNU General Public License
29 along with UCSIM; see the file COPYING.  If not, write to the Free
30 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
31 02111-1307, USA. */
32 /*@1@*/
33
34   switch (operands) {
35     case REG_REG:
36       if (code & 0x0800) {  /* word op */
37         set_reg2( RI_F0,
38                   FUNC2( reg2(RI_F0), reg2(RI_0F) )
39                 );
40       } else {
41         set_reg1( RI_F0,
42                   FUNC1( reg1(RI_F0), reg1(RI_0F) )
43                 );
44       }
45     break;
46     case REG_IREGINC :
47     case REG_IREG:
48     {
49       short srcreg = reg2(RI_07);
50       if (code & 0x0800) {  /* word op */
51         set_reg2( RI_F0,
52                   FUNC2( reg2(RI_F0),
53                         get2(srcreg)
54                       )
55                 );
56       } else {
57         set_reg1( RI_F0,
58                   FUNC1( reg1(RI_F0),
59                         get1(srcreg)
60                       )
61                 );
62       }
63       if (operands == REG_IREGINC) {
64         set_reg2(RI_07,  srcreg+1);
65       }
66     }
67     break;
68     case IREGINC_REG :
69     case IREG_REG :
70     {
71       short addr = reg2(RI_07);
72       if (code & 0x0800) {  /* word op */
73         unsigned short wtmp, wtotal;
74         wtmp = get2(addr);
75         wtotal = FUNC2( wtmp, reg2(RI_F0) );
76         store2(addr, wtotal);
77       } else {
78         unsigned char total;
79         total = FUNC1( get1(addr), reg1(RI_F0) );
80         store1(addr, total);
81       }
82       if (operands == IREGINC_REG) {
83         set_reg2(RI_07, addr+1);
84       }
85     }
86     break;
87
88     case IREGOFF8_REG :
89     case IREGOFF16_REG :
90     {
91       int offset;
92       if (operands == REG_IREGOFF8) {
93         offset = (int)((char) fetch());
94       } else {
95         offset = (int)((short)fetch2());
96       }
97       if (code & 0x0800) {  /* word op */
98         t_mem addr = reg2(RI_70) + offset;
99         unsigned short wtmp, wtotal;
100         wtmp = get2(addr);
101         wtotal = FUNC2( wtmp, reg2(RI_F0) );
102         store2(addr, wtotal);
103       } else {
104         t_mem addr = reg2(RI_70) + ((short) fetch2());
105         unsigned char total;
106         total = FUNC1( get1(addr), reg1(RI_F0) );
107         store1(addr, total);
108       }
109     }
110     break;
111
112     case REG_IREGOFF8 :
113     case REG_IREGOFF16 :
114     {
115       int offset;
116       if (operands == REG_IREGOFF8) {
117         offset = (int)((char) fetch());
118       } else {
119         offset = (int)((short)fetch2());
120       }
121
122       if (code & 0x0800) {  /* word op */
123         set_reg2( RI_F0,
124                   FUNC2( reg2(RI_F0),
125                         get2(reg2(RI_07)+offset)
126                       )
127                 );
128       } else {
129         int offset = (int)((short)fetch2());
130         set_reg1( RI_F0,
131                   FUNC1( reg1(RI_F0),
132                         get1(reg2(RI_07)+offset)
133                       )
134                 );
135       }
136     }
137     break;
138
139     case DIRECT_REG :
140     {
141       int addr = ((code & 0x7) << 8) | fetch();
142       if (code & 0x0800) {  /* word op */
143         unsigned short wtmp = get_word_direct(addr);
144         set_word_direct( addr,
145                   FUNC2( wtmp, reg2(RI_F0) )
146                 );
147       } else {
148         unsigned char tmp = get_byte_direct(addr);
149         set_byte_direct( addr,
150                   FUNC1( tmp, reg1(RI_F0) )
151                 );
152       }
153     }
154     break;
155
156     case REG_DIRECT :
157     {
158       int addr = ((code & 0x7) << 8) | fetch();
159       if (code & 0x0800) {  /* word op */
160         set_reg2( RI_F0,
161                   FUNC2( reg2(RI_F0),
162                         get_word_direct(addr)
163                       )
164                 );
165       } else {
166         set_reg1( RI_F0,
167                   FUNC1( reg1(RI_F0),
168                         get_byte_direct(addr)
169                       )
170                 );
171       }
172     }
173     break;
174
175     case REG_DATA8 :
176       set_reg1( RI_F0, FUNC1( reg1(RI_F0), fetch()) );
177     break;
178
179     case REG_DATA16 :
180       set_reg2( RI_F0, FUNC2( reg2(RI_F0), fetch2()) );
181     break;
182
183     case IREGINC_DATA8 :
184     case IREG_DATA8 :
185     {
186       unsigned char total;
187       unsigned char tmp;
188       t_mem addr = reg2(RI_70);
189       tmp = get1(addr);
190       total = FUNC1(tmp, fetch() );
191       store1(addr, total);
192       if (operands == IREGINC_DATA8) {
193         set_reg2(RI_70, addr+1);
194       }
195     }
196     break;
197
198     case IREGINC_DATA16 :
199     case IREG_DATA16 :
200     {
201       unsigned short total;
202       unsigned short tmp;
203       t_mem addr = reg2(RI_70);
204       tmp = get2(addr);
205       total = FUNC2(tmp, fetch2() );
206       store2(addr, total);
207       if (operands == IREGINC_DATA16) {
208         set_reg2(RI_70, addr+1);
209       }
210     }
211     break;
212
213     case IREGOFF8_DATA8 :
214     case IREGOFF16_DATA8 :
215     {
216       unsigned short addr;
217       int offset;
218       unsigned char tmp;
219       if (operands == IREGOFF8_DATA8) {
220         offset = (int)((char) fetch());
221       } else {
222         offset = (int)((short)fetch2());
223       }
224       tmp = fetch();
225       addr = reg2(RI_70);
226
227       store1( addr+offset,
228                   FUNC1( tmp,
229                         get1(addr+offset)
230                       )
231                 );
232     }
233     break;
234
235     case IREGOFF8_DATA16 :
236     case IREGOFF16_DATA16 :
237     {
238       unsigned short addr;
239       int offset;
240       unsigned short tmp;
241       if (operands == IREGOFF8_DATA16) {
242         offset = (int)((char) fetch());
243       } else {
244         offset = (int)((short)fetch2());
245       }
246       tmp = fetch2();
247       addr = reg2(RI_70);
248
249       store2( addr+offset,
250                   FUNC2( tmp,
251                         get2(addr+offset)
252                       )
253                 );
254     }
255     break;
256
257     case DIRECT_DATA8 :
258     {
259       int addr = ((code & 0x70) << 4) | fetch();
260       unsigned char bdir = get_byte_direct(addr);
261       unsigned char bdat = fetch();
262       set_byte_direct( addr,  FUNC1( bdir, bdat) );
263     }
264     break;
265
266     case DIRECT_DATA16 :
267     {
268       int addr = ((code & 0x70) << 4) | fetch();
269       unsigned short wdir = get_word_direct(addr);
270       unsigned short wdat = fetch2();
271       set_word_direct( addr,  FUNC2( wdir, wdat) );
272     }
273     break;
274   }
275