*** empty log message ***
[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 == IREGOFF8_REG) {
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_07) + 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_07) + ((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         set_reg1( RI_F0,
130                   FUNC1( reg1(RI_F0),
131                         get1(reg2(RI_07)+offset)
132                       )
133                 );
134       }
135     }
136     break;
137
138     case DIRECT_REG :
139     {
140       int addr = ((code & 0x7) << 8) | fetch();
141       if (code & 0x0800) {  /* word op */
142         unsigned short wtmp = get_word_direct(addr);
143         set_word_direct( addr,
144                   FUNC2( wtmp, reg2(RI_F0) )
145                 );
146       } else {
147         unsigned char tmp = get_byte_direct(addr);
148         set_byte_direct( addr,
149                   FUNC1( tmp, reg1(RI_F0) )
150                 );
151       }
152     }
153     break;
154
155     case REG_DIRECT :
156     {
157       int addr = ((code & 0x7) << 8) | fetch();
158       if (code & 0x0800) {  /* word op */
159         set_reg2( RI_F0,
160                   FUNC2( reg2(RI_F0),
161                         get_word_direct(addr)
162                       )
163                 );
164       } else {
165         set_reg1( RI_F0,
166                   FUNC1( reg1(RI_F0),
167                         get_byte_direct(addr)
168                       )
169                 );
170       }
171     }
172     break;
173
174     case REG_DATA8 :
175 #if 0
176       {
177         unsigned char dat = fetch();
178         unsigned char res;
179         res = FUNC1( reg1(RI_F0), dat);
180         set_reg1( RI_F0, res );
181    printf("reg_data8 code=%x dat=%x, res=%x r=%x\n", code, dat, res, reg1( RI_F0) );
182       }
183 #endif
184       set_reg1( RI_F0, FUNC1( reg1(RI_F0), fetch()) );
185     break;
186
187     case REG_DATA16 :
188       {
189         unsigned short dat = fetch2();
190         set_reg2( RI_F0, FUNC2( reg2(RI_F0), dat) );
191       }
192     break;
193
194     case IREGINC_DATA8 :
195     case IREG_DATA8 :
196     {
197       unsigned char total;
198       unsigned char tmp;
199       t_mem addr = reg2(RI_70);
200       tmp = get1(addr);
201       total = FUNC1(tmp, fetch() );
202       store1(addr, total);
203       if (operands == IREGINC_DATA8) {
204         set_reg2(RI_70, addr+1);
205       }
206     }
207     break;
208
209     case IREGINC_DATA16 :
210     case IREG_DATA16 :
211     {
212       unsigned short total;
213       unsigned short tmp;
214       t_mem addr = reg2(RI_70);
215       tmp = get2(addr);
216       total = FUNC2(tmp, fetch2() );
217       store2(addr, total);
218       if (operands == IREGINC_DATA16) {
219         set_reg2(RI_70, addr+1);
220       }
221     }
222     break;
223
224     case IREGOFF8_DATA8 :
225     case IREGOFF16_DATA8 :
226     {
227       unsigned short addr;
228       int offset;
229       unsigned char tmp;
230       if (operands == IREGOFF8_DATA8) {
231         offset = (int)((char) fetch());
232       } else {
233         offset = (int)((short)fetch2());
234       }
235       tmp = fetch();
236       addr = reg2(RI_70);
237
238       store1( addr+offset,
239                   FUNC1( tmp,
240                         get1(addr+offset)
241                       )
242                 );
243     }
244     break;
245
246     case IREGOFF8_DATA16 :
247     case IREGOFF16_DATA16 :
248     {
249       unsigned short addr;
250       int offset;
251       unsigned short tmp;
252       if (operands == IREGOFF8_DATA16) {
253         offset = (int)((char) fetch());
254       } else {
255         offset = (int)((short)fetch2());
256       }
257       tmp = fetch2();
258       addr = reg2(RI_70);
259
260       store2( addr+offset,
261                   FUNC2( tmp,
262                         get2(addr+offset)
263                       )
264                 );
265     }
266     break;
267
268     case DIRECT_DATA8 :
269     {
270       int addr = ((code & 0x70) << 4) | fetch();
271       unsigned char bdir = get_byte_direct(addr);
272       unsigned char bdat = fetch();
273       set_byte_direct( addr,  FUNC1( bdir, bdat) );
274     }
275     break;
276
277     case DIRECT_DATA16 :
278     {
279       int addr = ((code & 0x70) << 4) | fetch();
280       unsigned short wdir = get_word_direct(addr);
281       unsigned short wdat = fetch2();
282       set_word_direct( addr,  FUNC2( wdir, wdat) );
283     }
284     break;
285   }
286