Imported Upstream version 2.9.0
[debian/cc1111] / sim / ucsim / s51.src / uc390hw.cc
1 /*
2  * Simulator of microcontrollers (serial.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 #include <stdlib.h>
32
33 // local
34 #include "uc390hwcl.h"
35 #include "regs51.h"
36 #include "uc51cl.h"
37
38
39 cl_uc390_hw::cl_uc390_hw (class cl_uc *auc):
40   cl_hw (auc, HW_DUMMY, 0, "ds390hw")
41 {
42   uc390 = (class cl_uc390 *) uc;
43 }
44
45 int
46 cl_uc390_hw::init(void)
47 {
48   sfr = uc->address_space(MEM_SFR_ID);
49   if (sfr)
50     {
51       /*cell_dps   = sfr->register_hw (DPS  , this, 0);
52       cell_p4cnt = sfr->register_hw (P4CNT, this, 0);
53       cell_exif  = sfr->register_hw (EXIF , this, 0);
54       cell_acon  = sfr->register_hw (ACON , this, 0);
55       cell_p5cnt = sfr->register_hw (P5CNT, this, 0);
56       cell_c0c   = sfr->register_hw (C0C  , this, 0);
57       cell_pmr   = sfr->register_hw (PMR  , this, 0);
58       cell_mcon  = sfr->register_hw (MCON , this, 0);
59       cell_ta    = sfr->register_hw (TA   , this, 0);
60       cell_cor   = sfr->register_hw (COR  , this, 0);
61       cell_mcnt0 = sfr->register_hw (MCNT0, this, 0);
62       cell_mcnt1 = sfr->register_hw (MCNT1, this, 0);
63       cell_ma    = sfr->register_hw (MA   , this, 0);
64       cell_mb    = sfr->register_hw (MB   , this, 0);
65       cell_mc    = sfr->register_hw (MC   , this, 0);
66       cell_wdcon = sfr->register_hw (WDCON, this, 0);
67       cell_c1c   = sfr->register_hw (C1C  , this, 0);*/
68       register_cell (sfr, DPS  , &cell_dps  , wtd_restore);
69       register_cell (sfr, P4CNT, &cell_p4cnt, wtd_restore);
70       register_cell (sfr, EXIF , &cell_exif , wtd_restore);
71       register_cell (sfr, ACON , &cell_acon , wtd_restore);
72       register_cell (sfr, P5CNT, &cell_p5cnt, wtd_restore);
73       register_cell (sfr, C0C  , &cell_c0c  , wtd_restore);
74       register_cell (sfr, PMR  , &cell_pmr  , wtd_restore);
75       register_cell (sfr, MCON , &cell_mcon , wtd_restore);
76       register_cell (sfr, TA   , &cell_ta   , wtd_restore);
77       register_cell (sfr, COR  , &cell_cor  , wtd_restore);
78       register_cell (sfr, MCNT0, &cell_mcnt0, wtd_restore);
79       register_cell (sfr, MCNT1, &cell_mcnt1, wtd_restore);
80       register_cell (sfr, MA   , &cell_ma   , wtd_restore);
81       register_cell (sfr, MB   , &cell_mb   , wtd_restore);
82       register_cell (sfr, MC   , &cell_mc   , wtd_restore);
83       register_cell (sfr, WDCON, &cell_wdcon, wtd_restore);
84       register_cell (sfr, C1C  , &cell_c1c  , wtd_restore);
85     }
86   return 0;
87 }
88
89 t_mem
90 cl_uc390_hw::read (class cl_memory_cell *cell)
91 {
92   if (cell == cell_exif)
93     {
94       if (ctm_ticks &&
95           uc390->ticks->ticks >= ctm_ticks + 50 /*65535*/)
96         {
97           ctm_ticks = 0;
98           cell->set (cell->get() | 0x08); /* set CKRDY */
99         }
100     }
101   return cell->get();
102 }
103
104 void
105 cl_uc390_hw::write (class cl_memory_cell *cell, t_mem *val)
106 {
107   if (cell == cell_dps)
108     *val = (*val & 0xe5) | 0x04;
109   else if (cell == cell_exif)
110     {
111       /* Bit 0 (BGS) is TA-protected */
112       if (timed_access_state != 2 ||
113           timed_access_ticks + 2*12 < uc390->ticks->ticks) // fixme: 3 cycles
114         *val = (*val & ~0x01) | (cell_exif->get() & 0x01);
115
116       /* CKRDY and RGMD are read-only */
117       *val = (*val & 0x0c) | (*val & ~0x0c);
118     }
119   else if (cell == cell_p4cnt)
120     {
121       /* P4CNT is TA-protected */
122       if (timed_access_state != 2 ||
123           timed_access_ticks + 2*12 < uc390->ticks->ticks) // fixme: 3 cycles
124         *val = cell_p4cnt->get();
125       *val |= 0x80; /* always 1 */
126     }
127   else if (cell == cell_acon)
128     {
129       /* ACON is TA-protected */
130       if (timed_access_state != 2 ||
131           timed_access_ticks + 2*12 < uc390->ticks->ticks) // fixme: 3 cycles
132         *val = cell_acon->get();
133       else
134         {
135
136           /* lockout: IDM1:IDM0 and SA can't be set at the same time */
137             if ((cell_mcon->get() & 0xc0) == 0xc0) /* IDM1 and IDM0 set? */
138               *val &= ~0x04; /* lockout SA */
139         }
140       *val |= 0xf8; /* always 1 */
141     }
142   else if (cell == cell_p5cnt)
143     {
144       /* Bits 0...2 are TA-protected */
145       if (timed_access_state != 2 ||
146           timed_access_ticks + 2*12 < uc390->ticks->ticks) // fixme: 3 cycles
147         *val = (*val & ~0x07) | (cell_p5cnt->get() & 0x07);
148     }
149   else if (cell == cell_c0c)
150     {
151       /* Bit 3 (CRST) is TA-protected */
152       if (timed_access_state != 2 ||
153           timed_access_ticks + 2*12 < uc390->ticks->ticks) // fixme: 3 cycles
154         *val = (*val & ~0x08) | (cell_c0c->get() & 0x08);
155     }
156   else if (cell == cell_pmr)
157     {
158       /* fixme: check previous state */
159       if ((*val & 0xd0) == 0x90) /* CD1:CD0 set to 10, CTM set */
160         {
161           ctm_ticks = uc390->ticks->ticks;
162           cell_exif->set (cell_exif->get() & ~0x08); /* clear CKRDY */
163         }
164       else
165         ctm_ticks = 0;
166       *val |= 0x03; /* always 1 */
167     }
168   else if (cell == cell_mcon)
169     {
170       /* MCON is TA-protected */
171       if (timed_access_state != 2 ||
172           timed_access_ticks + 2*12 < uc390->ticks->ticks) // fixme: 3 cycles
173         *val = cell_mcon->get();
174       else
175         /* lockout: IDM1:IDM0 and SA can't be set at the same time */
176         if ((cell_acon->get() & 0x04) == 0x04) /* SA set? */
177           *val &= ~0xc0; /* lockout IDM1:IDM0 */
178       *val |= 0x10; /* always 1 */
179     }
180   else if (cell == cell_ta)
181     {
182       if (*val == 0xAA)
183         {
184           timed_access_state = 1;
185           timed_access_ticks = uc390->ticks->ticks;
186         }
187       else if (*val == 0x55 &&
188                timed_access_state == 1 &&
189                timed_access_ticks + 2*12 >= uc390->ticks->ticks) // fixme: 3 cycles
190         {
191           timed_access_state = 2;
192           timed_access_ticks = uc390->ticks->ticks;
193         }
194       else
195         timed_access_state = 0;
196     }
197   else if (cell == cell_cor)
198     {
199       /* COR is TA-protected */
200       if (timed_access_state != 2 ||
201           timed_access_ticks + 2*12 < uc390->ticks->ticks) // fixme: 3 cycles
202         *val = cell_cor->get();
203     }
204   else if (cell == cell_mcnt0)
205     {
206       ;
207     }
208   else if (cell == cell_mcnt1)
209     {
210       *val |= 0x0f; /* always 1 */
211     }
212   else if (cell == cell_ma)
213     {
214       ;
215     }
216   else if (cell == cell_mb)
217     {
218       ;
219     }
220   else if (cell == cell_mc)
221     {
222       ;
223     }
224   else if (cell == cell_wdcon)
225     {
226       /* Bits 0, 1, 3 and 6 are TA-protected */
227       if (timed_access_state != 2 ||
228           timed_access_ticks + 2*12 < uc390->ticks->ticks) // fixme: 3 cycles
229         *val = (*val & ~0x4b) | (cell_wdcon->get() & 0x4b);
230     }
231   else if (cell == cell_c1c)
232     {
233       /* Bit 3 (CRST) is TA-protected */
234       if (timed_access_state != 2 ||
235           timed_access_ticks + 2*12 < uc390->ticks->ticks) // fixme: 3 cycles
236         *val = (*val & ~0x08) | (cell_c1c->get() & 0x08);
237     }
238 }
239
240 /*void
241 cl_uc390_hw::mem_cell_changed (class cl_m *mem, t_addr addr)
242 {
243   class cl_m *sfr = uc->mem (MEM_SFR);
244
245   if (mem && sfr && mem == sfr)
246     switch (addr)
247       {
248         case DPS:   cell_dps   = sfr->get_cell (DPS);   break;
249         case P4CNT: cell_p4cnt = sfr->get_cell (P4CNT); break;
250         case EXIF:  cell_exif  = sfr->get_cell (EXIF);  break;
251         case ACON:  cell_acon  = sfr->get_cell (ACON);  break;
252         case P5CNT: cell_p5cnt = sfr->get_cell (P5CNT); break;
253         case C0C:   cell_c0c   = sfr->get_cell (C0C);   break;
254         case PMR:   cell_pmr   = sfr->get_cell (PMR);   break;
255         case MCON:  cell_mcon  = sfr->get_cell (MCON);  break;
256         case TA:    cell_ta    = sfr->get_cell (TA);    break;
257         case COR:   cell_cor   = sfr->get_cell (COR);   break;
258         case MCNT0: cell_mcnt0 = sfr->get_cell (MCNT0); break;
259         case MCNT1: cell_mcnt1 = sfr->get_cell (MCNT1); break;
260         case MA:    cell_ma    = sfr->get_cell (MA);    break;
261         case MB:    cell_mb    = sfr->get_cell (MB);    break;
262         case MC:    cell_mc    = sfr->get_cell (MC);    break;
263         case WDCON: cell_wdcon = sfr->get_cell (WDCON); break;
264         case C1C:   cell_c1c   = sfr->get_cell (C1C);   break;
265       }
266 }*/
267
268 void
269 cl_uc390_hw::reset(void)
270 {
271   ctm_ticks = 0;
272   timed_access_state = 0;
273 }
274
275 void
276 cl_uc390_hw::print_info(class cl_console_base *con)
277 {
278   int i;
279   long l;
280
281   i = sfr->get (EXIF);
282   con->dd_printf ("%s"
283                   " EXIF 0x%02x: IE5 %c IE4 %c IE3 %c IE2 %c CKRDY %c RGMD %c RGSL %c BGS %c\n",
284                   id_string,
285                   i,
286                   (i & 0x80) ? '1' : '0',
287                   (i & 0x40) ? '1' : '0',
288                   (i & 0x20) ? '1' : '0',
289                   (i & 0x10) ? '1' : '0',
290                   (i & 0x08) ? '1' : '0',
291                   (i & 0x04) ? '1' : '0',
292                   (i & 0x02) ? '1' : '0',
293                   (i & 0x01) ? '1' : '0');
294   i = sfr->get (DPS);
295   con->dd_printf ("\tDPS  0x%02x: ID1 %c ID0 %c TSL %c SEL %c\n",
296                   i,
297                   (i & 0x80) ? '1' : '0',
298                   (i & 0x40) ? '1' : '0',
299                   (i & 0x20) ? '1' : '0',
300                   (i & 0x01) ? '1' : '0');
301   l = sfr->get (DPX) * 256*256 +
302       sfr->get (DPH) * 256 +
303       sfr->get (DPL);
304   con->dd_printf ("\tDPTR  0x%06x\n", l);
305   l = sfr->get (DPX1) * 256*256 +
306       sfr->get (DPH1) * 256 +
307       sfr->get (DPL1);
308   con->dd_printf ("\tDPTR1 0x%06x\n", l);
309 }
310
311 /* End of s51.src/uc390hw.cc */