simulavr  1.1.0
hwwado.cpp
Go to the documentation of this file.
1  /*
2  ****************************************************************************
3  *
4  * simulavr - A simulator for the Atmel AVR family of microcontrollers.
5  * Copyright (C) 2001, 2002, 2003 Klaus Rudolph
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  ****************************************************************************
22  *
23  * $Id$
24  */
25 
26 #include "hwwado.h"
27 #include "avrdevice.h"
28 #include "systemclock.h"
29 
30 #define WDTOE 0x10
31 #define WDE 0x08
32 
33 
34 void HWWado::SetWdtcr(unsigned char val) {
35  unsigned char oldWDTOE= wdtcr & WDTOE;
36  unsigned char newWDE= val & WDE;
37 
38  if ( newWDE != 0) { //enable the wado allways allowed
39  wdtcr=val;
40  } else { //unset the wado
41  if (oldWDTOE !=0) { //WDTOE was set,
42  wdtcr=val;
43  }
44  }
45 
46  if ( (val & WDTOE ) != 0) {
47  cntWde=4;
48  }
49 
50 }
51 
52 unsigned int HWWado::CpuCycle() {
53  if ( cntWde > 0) {
54  cntWde--;
55 
56  }
57 
58  if (cntWde==0) wdtcr&=(0xff-WDTOE); //clear WDTOE after 4 cpu cycles
59 
60  if ((( wdtcr& WDE )!= 0 ) && (timeOutAt < SystemClock::Instance().GetCurrentTime() )) {
61  core->Reset();
62  }
63 
64 
65  return 0;
66 }
67 
69  Hardware(c),
70  TraceValueRegister(c, "WADO"),
71  core(c),
72  wdtcr_reg(this, "WDTCR",
73  this, &HWWado::GetWdtcr, &HWWado::SetWdtcr) {
74  core->AddToCycleList(this);
75  Reset();
76 }
77 
78 void HWWado::Reset() {
79  timeOutAt=0;
80  wdtcr=0;
81  cntWde = 0;
82 }
83 
84 
85 void HWWado::Wdr() {
87  switch ( wdtcr& 0x7) {
88  case 0:
89  timeOutAt= currentTime+ 47000000; //47ms
90  break;
91 
92  case 1:
93  timeOutAt= currentTime+ 94000000; //94ms
94  break;
95 
96  case 2:
97  timeOutAt= currentTime+ 190000000; //190 ms
98  break;
99 
100  case 3:
101  timeOutAt= currentTime+ 380000000; //380 ms
102  break;
103 
104  case 4:
105  timeOutAt= currentTime+ 750000000; //750 ms
106  break;
107 
108  case 5:
109  timeOutAt= currentTime+ 1500000000; //1.5 s
110  break;
111 
112  case 6:
113  timeOutAt= currentTime+ 3000000000ULL; //3 s
114  break;
115 
116  case 7:
117  timeOutAt= currentTime+ 6000000000ULL; //6 s
118  break;
119 
120  }
121 }
unsigned char cntWde
Definition: hwwado.h:41
Basic AVR device, contains the core functionality.
Definition: avrdevice.h:66
void AddToCycleList(Hardware *hw)
Definition: avrdevice.cpp:51
AvrDevice * core
Definition: hwwado.h:43
unsigned char GetWdtcr()
Definition: hwwado.h:50
SystemClockOffset GetCurrentTime() const
Returns the current simulation time.
Definition: systemclock.h:95
void Reset()
Definition: hwwado.cpp:78
Definition: hwwado.h:38
#define WDTOE
Definition: hwwado.cpp:30
HWWado(AvrDevice *)
Definition: hwwado.cpp:68
void Reset()
Definition: avrdevice.cpp:390
Build a register for TraceValue&#39;s.
Definition: traceval.h:442
static SystemClock & Instance()
Returns the central SystemClock instance for the application.
long long SystemClockOffset
unsigned char wdtcr
Definition: hwwado.h:40
void Wdr()
Definition: hwwado.cpp:85
void SetWdtcr(unsigned char val)
Definition: hwwado.cpp:34
SystemClockOffset timeOutAt
Definition: hwwado.h:42
virtual unsigned int CpuCycle()
Definition: hwwado.cpp:52
IOReg< HWWado > wdtcr_reg
Definition: hwwado.h:54
#define WDE
Definition: hwwado.cpp:31