mc146818.hh revision 9730:f3b53a5a559e
1/*
2 * Copyright (c) 2004-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Ali Saidi
29 *          Andrew Schultz
30 *          Miguel Serrano
31 */
32
33#ifndef __DEV_MC146818_HH__
34#define __DEV_MC146818_HH__
35
36#include "base/bitunion.hh"
37#include "sim/eventq_impl.hh"
38
39/** Real-Time Clock (MC146818) */
40class MC146818 : public EventManager
41{
42  protected:
43    virtual void handleEvent()
44    {
45        warn("No RTC event handler defined.\n");
46    }
47
48  private:
49    /** Event for RTC periodic interrupt */
50    struct RTCEvent : public Event
51    {
52        MC146818 * parent;
53        Tick interval;
54
55        RTCEvent(MC146818 * _parent, Tick i);
56
57        /** Schedule the RTC periodic interrupt */
58        void scheduleIntr();
59
60        /** Event process to occur at interrupt*/
61        virtual void process();
62
63        /** Event description */
64        virtual const char *description() const;
65    };
66
67    /** Event for RTC periodic interrupt */
68    struct RTCTickEvent : public Event
69    {
70        MC146818 * parent;
71
72        RTCTickEvent(MC146818 * _parent) : parent(_parent)
73        {
74            parent->schedule(this, curTick() + SimClock::Int::s);
75        }
76
77        /** Event process to occur at interrupt*/
78        void process();
79
80        /** Event description */
81        const char *description() const;
82    };
83
84  private:
85    std::string _name;
86    const std::string &name() const { return _name; }
87
88    /** RTC periodic interrupt event */
89    RTCEvent event;
90
91    /** RTC tick event */
92    RTCTickEvent tickEvent;
93
94    /** Data for real-time clock function */
95    union {
96        uint8_t clock_data[10];
97
98        struct {
99            uint8_t sec;
100            uint8_t sec_alrm;
101            uint8_t min;
102            uint8_t min_alrm;
103            uint8_t hour;
104            uint8_t hour_alrm;
105            uint8_t wday;
106            uint8_t mday;
107            uint8_t mon;
108            uint8_t year;
109        };
110    };
111
112    struct tm curTime;
113
114    void setTime(const struct tm time);
115
116    BitUnion8(RtcRegA)
117        Bitfield<7> uip;    /// 1 = date and time update in progress
118        Bitfield<6, 4> dv;  /// Divider configuration
119        /** Rate selection
120            0 = Disabled
121            For 32768 Hz time bases:
122              Freq = 32768Hz / 2**(n-1) for n >= 3
123              Freq = 256Hz if n = 1
124              Freq = 128Hz if n = 2
125            Othwerise:
126              Freq = 32768Hz / 2**(n-1)
127        */
128        Bitfield<3, 0> rs;
129    EndBitUnion(RtcRegA)
130
131    /// Is the DV field in regA set to disabled?
132    static inline bool rega_dv_disabled(const RtcRegA &reg);
133
134    BitUnion8(RtcRegB)
135        Bitfield<7> set;       /// stop clock updates
136        Bitfield<6> pie;       /// 1 = enable periodic clock interrupt
137        Bitfield<5> aie;       /// 1 = enable alarm interrupt
138        Bitfield<4> uie;       /// 1 = enable update-ended interrupt
139        Bitfield<3> sqwe;      /// 1 = output sqare wave at SQW pin
140        Bitfield<2> dm;        /// 0 = BCD, 1 = Binary coded time
141        Bitfield<1> format24h; /// 0 = 12 hours, 1 = 24 hours
142        Bitfield<0> dse;       /// USA Daylight Savings Time enable
143    EndBitUnion(RtcRegB)
144
145    /** RTC status register A */
146    RtcRegA stat_regA;
147
148    /** RTC status register B */
149    RtcRegB stat_regB;
150
151  public:
152    MC146818(EventManager *em, const std::string &name, const struct tm time,
153            bool bcd, Tick frequency);
154    virtual ~MC146818();
155
156    /** RTC write data */
157    void writeData(const uint8_t addr, const uint8_t data);
158
159    /** RTC read data */
160    uint8_t readData(const uint8_t addr);
161
162    void tickClock();
163
164    /**
165      * Serialize this object to the given output stream.
166      * @param base The base name of the counter object.
167      * @param os The stream to serialize to.
168      */
169    void serialize(const std::string &base, std::ostream &os);
170
171    /**
172     * Reconstruct the state of this object from a checkpoint.
173     * @param base The base name of the counter object.
174     * @param cp The checkpoint use.
175     * @param section The section name of this object
176     */
177    void unserialize(const std::string &base, Checkpoint *cp,
178                     const std::string &section);
179};
180
181#endif // __DEV_MC146818_HH__
182