Merge branch ucsim-034-pre3 to main trunk; new version 0.4
[fw/sdcc] / sim / ucsim / s51.src / uc89c51r.cc
1 /*
2  * Simulator of microcontrollers (uc89c51r.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 "uc89c51rcl.h"
34 #include "regs51.h"
35 #include "pcacl.h"
36
37
38 t_uc89c51r::t_uc89c51r(int Itype, int Itech, class cl_sim *asim):
39   t_uc51r(Itype, Itech, asim)
40 {
41   /*it_sources->add_at(4, new cl_it_src(bmEC, CCON, bmCCF4, 0x0033, false,
42                                       "PCA module #4"));
43   it_sources->add_at(4, new cl_it_src(bmEC, CCON, bmCCF3, 0x0033, false,
44                                       "PCA module #3"));
45   it_sources->add_at(4, new cl_it_src(bmEC, CCON, bmCCF2, 0x0033, false,
46                                       "PCA module #2"));
47   it_sources->add_at(4, new cl_it_src(bmEC, CCON, bmCCF1, 0x0033, false,
48                                       "PCA module #1"));
49   it_sources->add_at(4, new cl_it_src(bmEC, CCON, bmCCF0, 0x0033, false,
50                                       "PCA module #0"));
51   it_sources->add_at(4, new cl_it_src(bmEC, CCON, bmCF, 0x0033, false,
52   "PCA counter"));*/
53 }
54
55
56 void
57 <<<<<<< uc89c51r.cc
58 t_uc89c51r::reset(void)
59 {
60   t_uc51r::reset();
61   mem(MEM_SFR)->set_bit1(CCAPM0, bmECOM);
62   mem(MEM_SFR)->set_bit1(CCAPM1, bmECOM);
63   mem(MEM_SFR)->set_bit1(CCAPM2, bmECOM);
64   mem(MEM_SFR)->set_bit1(CCAPM3, bmECOM);
65   mem(MEM_SFR)->set_bit1(CCAPM4, bmECOM);
66   t0_overflows= 0;
67   dpl0= dph0= dpl1= dph1= 0;
68   set_mem(MEM_SFR, IPH, 0);
69 }
70
71 void
72 t_uc89c51r::proc_write(uchar *addr)
73 =======
74 t_uc89c51r::mk_hw_elements(void)
75 >>>>>>> 1.2.2.3
76 {
77 <<<<<<< uc89c51r.cc
78   t_uc51r::proc_write(addr);
79
80   if (addr == &(/*MEM(MEM_SFR)*/sfr->umem8[CCAP0L]))
81     mem(MEM_SFR)->set_bit0(CCAPM0, bmECOM);
82   if (addr == &(/*MEM(MEM_SFR)*/sfr->umem8[CCAP0H]))
83     mem(MEM_SFR)->set_bit1(CCAPM0, bmECOM);
84
85   if (addr == &(/*MEM(MEM_SFR)*/sfr->umem8[CCAP1L]))
86     mem(MEM_SFR)->set_bit0(CCAPM1, bmECOM);
87   if (addr == &(/*MEM(MEM_SFR)*/sfr->umem8[CCAP1H]))
88     mem(MEM_SFR)->set_bit1(CCAPM1, bmECOM);
89
90   if (addr == &(/*MEM(MEM_SFR)*/sfr->umem8[CCAP2L]))
91     mem(MEM_SFR)->set_bit0(CCAPM2, bmECOM);
92   if (addr == &(/*MEM(MEM_SFR)*/sfr->umem8[CCAP2H]))
93     mem(MEM_SFR)->set_bit1(CCAPM2, bmECOM);
94
95   if (addr == &(/*MEM(MEM_SFR)*/sfr->umem8[CCAP3L]))
96     mem(MEM_SFR)->set_bit0(CCAPM3, bmECOM);
97   if (addr == &(/*MEM(MEM_SFR)*/sfr->umem8[CCAP3H]))
98     mem(MEM_SFR)->set_bit1(CCAPM3, bmECOM);
99
100   if (addr == &(/*MEM(MEM_SFR)*/sfr->umem8[CCAP4L]))
101     mem(MEM_SFR)->set_bit0(CCAPM4, bmECOM);
102   if (addr == &(/*MEM(MEM_SFR)*/sfr->umem8[CCAP4H]))
103     mem(MEM_SFR)->set_bit1(CCAPM4, bmECOM);
104 =======
105   class cl_hw *h;
106 >>>>>>> 1.2.2.3
107
108 <<<<<<< uc89c51r.cc
109   if (addr == &(/*MEM(MEM_SFR)*/sfr->umem8[AUXR]))
110     mem(MEM_SFR)->set_bit0(AUXR, 0x04);
111 =======
112   t_uc51r::mk_hw_elements();
113   hws->add(h= new cl_pca(this, 0));
114   h->init();
115   /*hws->add(h= new cl_pca(this, 1));
116   h->init();
117   hws->add(h= new cl_pca(this, 2));
118   h->init();
119   hws->add(h= new cl_pca(this, 3));
120   h->init();
121   hws->add(h= new cl_pca(this, 4));
122   h->init();*/
123   hws->add(h= new cl_89c51r_dummy_hw(this));
124   h->init();
125 >>>>>>> 1.2.2.3
126 }
127
128
129 void
130 t_uc89c51r::reset(void)
131 {
132   t_uc51r::reset();
133   sfr->set_bit1(CCAPM0, bmECOM);
134   sfr->set_bit1(CCAPM1, bmECOM);
135   sfr->set_bit1(CCAPM2, bmECOM);
136   sfr->set_bit1(CCAPM3, bmECOM);
137   sfr->set_bit1(CCAPM4, bmECOM);
138   //t0_overflows= 0;
139   dpl0= dph0= dpl1= dph1= 0;
140   sfr->set(IPH, 0);
141 }
142
143 int
144 t_uc89c51r::it_priority(uchar ie_mask)
145 {
146   uchar l, h;
147
148   l= sfr->get(IP) & ie_mask;
149   h= sfr->get(IPH) & ie_mask;
150   if (!h && !l)
151     return(0);
152   if (!h && l)
153     return(1);
154   if (h && !l)
155     return(2);
156   if (h && l)
157     return(3);
158   return(0);
159 }
160
161 void
162 t_uc89c51r::pre_inst(void)
163 {
164   if (sfr->get(AUXR1) & bmDPS)
165     {
166       sfr->set(DPL, dpl1);
167       sfr->set(DPH, dph1);
168     }
169   else
170     {
171       sfr->set(DPL, dpl0);
172       sfr->set(DPH, dph0);
173     }
174   t_uc51r::pre_inst();
175 }
176
177 void
178 t_uc89c51r::post_inst(void)
179 {
180   if (sfr->get(AUXR1) & bmDPS)
181     {
182       dpl1= sfr->get(DPL);
183       dph1= sfr->get(DPH);
184     }
185   else
186     {
187       dpl0= sfr->get(DPL);
188       dph0= sfr->get(DPH);
189     }
190   t_uc51r::post_inst();
191 }
192
193
194 /*
195 <<<<<<< uc89c51r.cc
196  * Simulating timers
197  *
198  * Calling inherited method to simulate timer #0, #1, #2 and then 
199  * simulating Programmable Counter Array
200 =======
201 >>>>>>> 1.2.2.3
202  */
203
204 int
205 t_uc89c51r::do_timers(int cycles)
206 {
207   int res;
208
209 <<<<<<< uc89c51r.cc
210   if ((res= t_uc51r::do_timers(cycles)) != resGO)
211     return(res);
212   return(do_pca(cycles));
213 }
214 =======
215 cl_89c51r_dummy_hw::cl_89c51r_dummy_hw(class cl_uc *auc):
216   cl_hw(auc, HW_DUMMY, 0, "_89c51r_dummy")
217 {}
218 >>>>>>> 1.2.2.3
219
220 int
221 <<<<<<< uc89c51r.cc
222 t_uc89c51r::t0_overflow(void)
223 =======
224 cl_89c51r_dummy_hw::init(void)
225 >>>>>>> 1.2.2.3
226 {
227 <<<<<<< uc89c51r.cc
228   uchar cmod= get_mem(MEM_SFR, CMOD) & (bmCPS0|bmCPS1);
229
230   if (cmod == bmCPS1)
231     t0_overflows++;
232   return(0);
233 }
234
235
236 /*
237  * Simulating Programmable Counter Array
238  */
239
240 int
241 t_uc89c51r::do_pca(int cycles)
242 {
243   int ret= resGO;
244   uint ccon= get_mem(MEM_SFR, CCON);
245
246   if (!(ccon & bmCR))
247     return(resGO);
248   if (state == stIDLE &&
249       (ccon & bmCIDL))
250     return(resGO);
251
252   switch (get_mem(MEM_SFR, CMOD) & (bmCPS1|bmCPS0))
253     {
254     case 0:
255       ret= do_pca_counter(cycles);
256       break;
257     case bmCPS0:
258       ret= do_pca_counter(cycles*3);
259       break;
260     case bmCPS1:
261       ret= do_pca_counter(t0_overflows);
262       t0_overflows= 0;
263       break;
264     case (bmCPS0|bmCPS1):
265       if ((prev_p1 & bmECI) != 0 &
266           (get_mem(MEM_SFR, P1) & bmECI) == 0)
267         do_pca_counter(1);
268       break;
269     }
270   return(ret);
271 }
272
273 int
274 t_uc89c51r::do_pca_counter(int cycles)
275 {
276   while (cycles--)
277 =======
278   class cl_mem *sfr= uc->mem(MEM_SFR);
279   if (!sfr)
280 >>>>>>> 1.2.2.3
281     {
282       fprintf(stderr, "No SFR to register %s[%d] into\n", id_string, id);
283     }
284   //auxr= sfr->register_hw(AUXR, this, 0);
285   register_cell(sfr, AUXR, &auxr, wtd_restore);
286   return(0);
287 }
288
289 void
290 cl_89c51r_dummy_hw::write(class cl_cell *cell, t_mem *val)
291 {
292   if (cell == auxr)
293     auxr->set_bit0(0x04);
294 }
295
296
297 /* End of s51.src/uc89c51r.cc */