1753e9d0613866147bda1064c0b62d0b407b9b2f
[fw/sdcc] / sim / ucsim / avr.src / bit_inst.cc
1 /*
2  * Simulator of microcontrollers (bit_inst.cc)
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 // local
29 #include "avrcl.h"
30 #include "regsavr.h"
31
32
33 /*
34  * Set Carry Flag
35  * SEC
36  * 1001 0100 0000 1000
37  *----------------------------------------------------------------------------
38  */
39
40 int
41 cl_avr::sec(t_mem code)
42 {
43   t_mem d= BIT_C | ram->read(SREG);
44   ram->write(SREG, &d);
45   return(resGO);
46 }
47
48
49 /*
50  * Set Negative Flag
51  * SEN
52  * 1001 0100 0010 1000
53  *----------------------------------------------------------------------------
54  */
55
56 int
57 cl_avr::sen(t_mem code)
58 {
59   t_mem d= BIT_N | ram->read(SREG);
60   ram->write(SREG, &d);
61   return(resGO);
62 }
63
64
65 /*
66  * Set Zero Flag
67  * SEZ
68  * 1001 0100 0001 1000
69  *----------------------------------------------------------------------------
70  */
71
72 int
73 cl_avr::sez(t_mem code)
74 {
75   t_mem d= BIT_Z | ram->read(SREG);
76   ram->write(SREG, &d);
77   return(resGO);
78 }
79
80
81 /*
82  * Set Global Interrupt Flag
83  * SEI
84  * 1001 0100 0111 1000
85  *----------------------------------------------------------------------------
86  */
87
88 int
89 cl_avr::sei(t_mem code)
90 {
91   t_mem d= BIT_I | ram->read(SREG);
92   ram->write(SREG, &d);
93   return(resGO);
94 }
95
96
97 /*
98  * Set Signed Flag
99  * SES
100  * 1001 0100 0100 1000
101  *----------------------------------------------------------------------------
102  */
103
104 int
105 cl_avr::ses(t_mem code)
106 {
107   t_mem d= BIT_S | ram->read(SREG);
108   ram->write(SREG, &d);
109   return(resGO);
110 }
111
112
113 /*
114  * Set Overflow Flag
115  * SEV
116  * 1001 0100 0011 1000
117  *----------------------------------------------------------------------------
118  */
119
120 int
121 cl_avr::sev(t_mem code)
122 {
123   t_mem d= BIT_V | ram->read(SREG);
124   ram->write(SREG, &d);
125   return(resGO);
126 }
127
128
129 /*
130  * Set T Flag
131  * SET
132  * 1001 0100 0110 1000
133  *----------------------------------------------------------------------------
134  */
135
136 int
137 cl_avr::set(t_mem code)
138 {
139   t_mem d= BIT_T | ram->read(SREG);
140   ram->write(SREG, &d);
141   return(resGO);
142 }
143
144
145 /*
146  * Set Half Carry Flag
147  * SEH
148  * 1001 0100 0101 1000
149  *----------------------------------------------------------------------------
150  */
151
152 int
153 cl_avr::seh(t_mem code)
154 {
155   t_mem d= BIT_H | ram->read(SREG);
156   ram->write(SREG, &d);
157   return(resGO);
158 }
159
160
161 /*
162  * Clear Carry Flag
163  * CLC
164  * 1001 0100 1000 1000
165  *----------------------------------------------------------------------------
166  */
167
168 int
169 cl_avr::clc(t_mem code)
170 {
171   t_mem d= ~BIT_C & ram->read(SREG);
172   ram->write(SREG, &d);
173   return(resGO);
174 }
175
176
177 /*
178  * Clear Negative Flag
179  * CLN
180  * 1001 0100 1010 1000
181  *----------------------------------------------------------------------------
182  */
183
184 int
185 cl_avr::cln(t_mem code)
186 {
187   t_mem d= ~BIT_N & ram->read(SREG);
188   ram->write(SREG, &d);
189   return(resGO);
190 }
191
192
193 /*
194  * Clear Zero Flag
195  * CLZ
196  * 1001 0100 1001 1000
197  *----------------------------------------------------------------------------
198  */
199
200 int
201 cl_avr::clz(t_mem code)
202 {
203   t_mem d= ~BIT_Z & ram->read(SREG);
204   ram->write(SREG, &d);
205   return(resGO);
206 }
207
208
209 /*
210  * Global Interrupt Flag
211  * CLI
212  * 1001 0100 1111 1000
213  *----------------------------------------------------------------------------
214  */
215
216 int
217 cl_avr::cli(t_mem code)
218 {
219   t_mem d= ~BIT_I & ram->read(SREG);
220   ram->write(SREG, &d);
221   return(resGO);
222 }
223
224
225 /*
226  * Clear Signed Flag
227  * CLS
228  * 1001 0100 1100 1000
229  *----------------------------------------------------------------------------
230  */
231
232 int
233 cl_avr::cls(t_mem code)
234 {
235   t_mem d= ~BIT_S & ram->read(SREG);
236   ram->write(SREG, &d);
237   return(resGO);
238 }
239
240
241 /*
242  * Clear Overflow Flag
243  * CLV
244  * 1001 0100 1011 1000
245  *----------------------------------------------------------------------------
246  */
247
248 int
249 cl_avr::clv(t_mem code)
250 {
251   t_mem d= ~BIT_V & ram->read(SREG);
252   ram->write(SREG, &d);
253   return(resGO);
254 }
255
256
257 /*
258  * Clear T Flag
259  * CLT
260  * 1001 0100 1110 1000
261  *----------------------------------------------------------------------------
262  */
263
264 int
265 cl_avr::clt(t_mem code)
266 {
267   t_mem d= ~BIT_T & ram->read(SREG);
268   ram->write(SREG, &d);
269   return(resGO);
270 }
271
272
273 /*
274  * Clear Half Carry Flag
275  * CLH
276  * 1001 0100 1101 1000
277  *----------------------------------------------------------------------------
278  */
279
280 int
281 cl_avr::clh(t_mem code)
282 {
283   t_mem d= ~BIT_H & ram->read(SREG);
284   ram->write(SREG, &d);
285   return(resGO);
286 }
287
288
289 /*
290  * Clear Bit in I/O Register
291  * CBI P,b 0<=P<=31 0<=b<=7
292  * 1001 1000 pppp pbbb
293  *____________________________________________________________________________
294  */
295
296 int
297 cl_avr::cbi_A_b(t_mem code)
298 {
299   uint addr, mask;
300   t_mem d;
301
302   addr= ((code&0xf8)>>3)+0x20;
303   mask= 1 << (code&7);
304   d= ~mask & ram->read(addr);
305   ram->write(addr, &d);
306   tick(1);
307   return(resGO);
308 }
309
310
311 /*
312  * Set Bit in I/O Register
313  * SBI P,b 0<=P<=31 0<=b<=7
314  * 1001 1010 pppp pbbb
315  *____________________________________________________________________________
316  */
317
318 int
319 cl_avr::sbi_A_b(t_mem code)
320 {
321   uint addr, mask;
322   
323   addr= ((code&0xf8)>>3)+0x20;
324   mask= 1 << (code&7);
325   t_mem d= mask | ram->read(addr);
326   ram->write(addr, &d);
327   tick(1);
328   return(resGO);
329 }
330
331
332 /*
333  * Bit Load from the T Flag in SREG to a Bit in Register
334  * BLD Rd,b 0<=d<=31, 0<=b<=7
335  * 1111 100d dddd 0bbb
336  *____________________________________________________________________________
337  */
338
339 int
340 cl_avr::bld_Rd_b(t_mem code)
341 {
342   t_addr d;
343   int b, mask;
344   t_mem data;
345
346   d= (code&0x1f0)>>4;
347   b= code&7;
348   mask= 1<<b;
349   if (ram->read(SREG) & BIT_T)
350     data= ram->read(d) | mask;
351   else
352     data= ram->read(d) & ~mask;
353   ram->write(d, &data);
354   return(resGO);
355 }
356
357
358 /*
359  * Bit Store from Bit in Register to T Flag in SREG
360  * BST Rd,b 0<=d<=31, 0<=b<=7
361  * 1111 101d dddd Xbbb
362  *____________________________________________________________________________
363  */
364
365 int
366 cl_avr::bst_Rd_b(t_mem code)
367 {
368   t_addr d;
369   int b, mask;
370
371   d= (code&0x1f0)>>4;
372   b= code&7;
373   mask= 1<<b;
374   t_mem data= ram->read(d);
375   if (data & mask)
376     ram->set_bit1(SREG, BIT_T);
377   else
378     ram->set_bit0(SREG, BIT_T);
379   return(resGO);
380 }
381
382
383 /* End of avr.src/bit_inst.cc */