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