7e7141e0a6db0701d267914a6f4335484c97b90f
[fw/sdcc] / sim / ucsim / s51.src / wdt.cc
1 /*
2  * Simulator of microcontrollers (wdt.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 <ctype.h>
29
30 // local
31 #include "wdtcl.h"
32 #include "regs51.h"
33
34
35 cl_wdt::cl_wdt(class cl_uc *auc, long resetvalue):
36   cl_hw(auc, HW_WDT, 0, "wdt")
37 {
38   reset_value= resetvalue;
39   wdt= -1;
40   written_since_reset= DD_FALSE;
41 }
42
43 int
44 cl_wdt::init(void)
45 {
46   class cl_mem *sfr= uc->mem(MEM_SFR);
47
48   if (!sfr)
49     {
50       fprintf(stderr, "No SFR to register WDT into\n");
51     }
52   //wdtrst= sfr->register_hw(WDTRST, this, (int*)0);
53   register_cell(sfr, WDTRST, &wdtrst, wtd_restore);
54   return(0);
55 }
56
57 void
58 cl_wdt::write(class cl_cell *cell, t_mem *val)
59 {
60   if (cell == wdtrst &&
61       (((*val)&0xff) == 0xe1) &&
62       (wdtrst->get() == 0x1e) &&
63       written_since_reset)
64     {
65       wdt= 0;
66       /*uc->sim->app->get_commander()->
67         debug("%g sec (%d tick): Watchdog timer enabled/reset PC= 0x%06x"
68         "\n", uc->get_rtime(), uc->ticks->ticks, uc51r->PC);*/
69     }
70   written_since_reset= DD_TRUE;
71 }
72
73 int
74 cl_wdt::tick(int cycles)
75 {
76   if (wdt >= 0)
77     {
78       wdt+= cycles;
79       if (wdt > reset_value)
80         {
81           /*sim->app->get_commander()->
82             debug("%g sec (%d ticks): Watchdog timer resets the CPU, "
83             "PC= 0x%06x\n", get_rtime(), ticks->ticks, PC);*/
84           uc->reset();
85           //return(resWDTRESET);
86         }
87     }
88   return(0);
89 }
90
91 void
92 cl_wdt::reset(void)
93 {
94   written_since_reset= DD_FALSE;
95   wdt= -1;
96 }
97
98 void
99 cl_wdt::print_info(class cl_console *con)
100 {
101   con->dd_printf("%s[%d] %s counter=%d (remains=%d)\n", id_string, id,
102                  (wdt>=0)?"ON":"OFF", wdt, (wdt>=0)?(reset_value-wdt):0);
103 }
104
105
106 /* End of s51.src/wdt.cc */