ucsim-0.2.37-pre3 into cvs
[fw/sdcc] / sim / ucsim / s51.src / uc52.cc
1 /*
2  * Simulator of microcontrollers (uc52.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 #include "ddconfig.h"
29
30 #include <stdio.h>
31
32 // local
33 #include "uc52cl.h"
34 #include "regs51.h"
35 #include "timer2cl.h"
36
37
38 /*
39  * Making an 8052 CPU object
40  */
41
42 t_uc52::t_uc52(int Itype, int Itech, class cl_sim *asim):
43   t_uc51(Itype, Itech, asim)
44 {
45   it_sources->add(new cl_it_src(bmET2, T2CON, bmTF2, 0x002b, false,
46                                 "timer #2 TF2"));
47   exf2it= new cl_it_src(bmET2, T2CON, bmEXF2, 0x002b, false,
48                         "timer #2 EXF2");
49   it_sources->add(exf2it);
50 }
51
52
53 void
54 t_uc52::mk_hw_elements(void)
55 {
56   class cl_hw *h;
57
58   t_uc51::mk_hw_elements();
59   hws->add(h= new cl_timer2(this));
60   h->init();
61 }
62
63
64 /*
65  * Calculating address of indirectly addressed IRAM cell
66  *
67  * If CPU is 8051 and addr is over 127, it must be illegal! But in 52
68  * it is legal.
69  *
70  */
71
72 uchar *
73 t_uc52::get_indirect(uchar addr, int *res)
74 {
75   *res= resGO;
76   return(&(MEM(MEM_IRAM)[addr]));
77 }
78
79
80 /*
81  * Simulating timers
82  *
83  * Calling inherited method to simulate timer #0 and #1 and then 
84  * simulating timer #2.
85  *
86  */
87
88 int
89 t_uc52::do_timers(int cycles)
90 {
91   int res;
92
93   if ((res= t_uc51::do_timers(cycles)) != resGO)
94     return(res);
95   return(do_timer2(cycles));
96 }
97
98
99 /*
100  * Simulating timer 2
101  */
102
103 int
104 t_uc52::do_timer2(int cycles)
105 {
106   bool nocount= FALSE;
107   uint t2con= get_mem(MEM_SFR, T2CON);
108
109   exf2it->activate();
110   if (!(t2con & bmTR2))
111     /* Timer OFF */
112     return(resGO);
113
114   if (t2con & (bmRCLK | bmTCLK))
115     return(do_t2_baud(cycles));
116
117   /* Determining nr of input clocks */
118   if (!(t2con & bmTR2))
119     nocount= TRUE; // Timer OFF
120   else
121     if (t2con & bmC_T2)
122       {
123         // Counter mode, falling edge on P1.0 (T2)
124         if ((prev_p1 & bmT2) &&
125             !(get_mem(MEM_SFR, P1) & port_pins[1] & bmT2))
126           cycles= 1;
127         else
128           nocount= TRUE;
129       }
130   /* Counting */
131   while (cycles--)
132     {
133       if (t2con & bmCP_RL2)
134         do_t2_capture(&cycles, nocount);
135       else
136         do_t2_reload(&cycles, nocount);
137     }// while cycles
138   
139   return(resGO);
140 }
141
142
143 /*
144  * Baud rate generator mode of Timer #2
145  */
146
147 int
148 t_uc52::do_t2_baud(int cycles)
149 {
150   uint t2con= get_mem(MEM_SFR, T2CON);
151   uint p1= get_mem(MEM_SFR, P1);
152
153   /* Baud Rate Generator */
154   if ((prev_p1 & bmT2EX) &&
155       !(p1 & port_pins[1] & bmT2EX) &&
156       (t2con & bmEXEN2))
157     mem(MEM_SFR)->set_bit1(T2CON, bmEXF2);
158   if (t2con & bmC_T2)
159     {
160       if ((prev_p1 & bmT2) &&
161           !(p1 & port_pins[1] & bmT2))
162         cycles= 1;
163       else
164         cycles= 0;
165     }
166   else
167     cycles*= 6;
168   if (t2con & bmTR2)
169     while (cycles--)
170       {
171         if (!++(MEM(MEM_SFR)[TL2]))
172           if (!++(MEM(MEM_SFR)[TH2]))
173             {
174               MEM(MEM_SFR)[TH2]= MEM(MEM_SFR)[RCAP2H];
175               MEM(MEM_SFR)[TL2]= MEM(MEM_SFR)[RCAP2L];
176               s_rec_t2++;
177               s_tr_t2++;
178             }
179       }
180   return(resGO);
181 }
182
183
184 /*
185  * Capture function of Timer #2
186  */
187
188 void
189 t_uc52::do_t2_capture(int *cycles, bool nocount)
190 {
191   uint p1= get_mem(MEM_SFR, P1);
192   uint t2con= get_mem(MEM_SFR, T2CON);
193
194   /* Capture mode */
195   if (nocount)
196     *cycles= 0;
197   else
198     {
199       if (!++(MEM(MEM_SFR)[TL2]))
200         {
201           if (!++(MEM(MEM_SFR)[TH2]))
202             mem(MEM_SFR)->set_bit1(T2CON, bmTF2);
203         }
204     }
205   // capture
206   if ((prev_p1 & bmT2EX) &&
207       !(p1 & port_pins[1] & bmT2EX) &&
208       (t2con & bmEXEN2))
209     {
210       MEM(MEM_SFR)[RCAP2H]= MEM(MEM_SFR)[TH2];
211       MEM(MEM_SFR)[RCAP2L]= MEM(MEM_SFR)[TL2];
212       mem(MEM_SFR)->set_bit1(T2CON, bmEXF2);
213       prev_p1&= ~bmT2EX; // Falling edge has been handled
214     }
215 }
216
217
218 /*
219  * Auto Reload mode of Timer #2, counting UP
220  */
221
222 void
223 t_uc52::do_t2_reload(int *cycles, bool nocount)
224 {
225   int overflow;
226   bool ext2= 0;
227   
228   /* Auto-Relode mode */
229   overflow= 0;
230   if (nocount)
231     *cycles= 0;
232   else
233     {
234       if (!++(MEM(MEM_SFR)[TL2]))
235         {
236           if (!++(MEM(MEM_SFR)[TH2]))
237             {
238               mem(MEM_SFR)->set_bit1(T2CON, bmTF2);
239               overflow++;
240             }
241         }
242     }
243   // reload
244   if ((prev_p1 & bmT2EX) &&
245       !(get_mem(MEM_SFR, P1) & port_pins[1] & bmT2EX) &&
246       (get_mem(MEM_SFR, T2CON) & bmEXEN2))
247     {
248       ext2= TRUE;
249       mem(MEM_SFR)->set_bit1(T2CON, bmEXF2);
250       prev_p1&= ~bmT2EX; // Falling edge has been handled
251     }
252   if (overflow ||
253       ext2)
254     {
255       MEM(MEM_SFR)[TH2]= MEM(MEM_SFR)[RCAP2H];
256       MEM(MEM_SFR)[TL2]= MEM(MEM_SFR)[RCAP2L];
257     }
258 }
259
260
261 /*
262  *
263  */
264
265 int
266 t_uc52::serial_bit_cnt(int mode)
267 {
268   int divby= 12;
269   int *tr_src= 0, *rec_src= 0;
270
271   switch (mode)
272     {
273     case 0:
274       divby  = 12;
275       tr_src = &s_tr_tick;
276       rec_src= &s_rec_tick;
277       break;
278     case 1:
279     case 3:
280       divby  = (get_mem(MEM_SFR, PCON)&bmSMOD)?16:32;
281       tr_src = (get_mem(MEM_SFR, T2CON)&bmTCLK)?(&s_tr_t2):(&s_tr_t1);
282       rec_src= (get_mem(MEM_SFR, T2CON)&bmTCLK)?(&s_rec_t2):(&s_rec_t1);
283       break;
284     case 2:
285       divby  = (get_mem(MEM_SFR, PCON)&bmSMOD)?16:32;
286       tr_src = &s_tr_tick;
287       rec_src= &s_rec_tick;
288       break;
289     }
290   if (s_sending)
291     {
292       while (*tr_src >= divby)
293         {
294           (*tr_src)-= divby;
295           s_tr_bit++;
296         }
297     }
298   if (s_receiving)
299     {
300       while (*rec_src >= divby)
301         {
302           (*rec_src)-= divby;
303           s_rec_bit++;
304         }
305     }
306   return(0);
307 }
308
309
310 /* End of s51.src/uc52.cc */