intel_8254_timer.hh revision 5443:394d180e8c04
1/*
2 * Copyright (c) 2004, 2005
3 * The Regents of The University of Michigan
4 * All Rights Reserved
5 *
6 * This code is part of the M5 simulator.
7 *
8 * Permission is granted to use, copy, create derivative works and
9 * redistribute this software and such derivative works for any
10 * purpose, so long as the copyright notice above, this grant of
11 * permission, and the disclaimer below appear in all copies made; and
12 * so long as the name of The University of Michigan is not used in
13 * any advertising or publicity pertaining to the use or distribution
14 * of this software without specific, written prior authorization.
15 *
16 * THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION FROM THE
17 * UNIVERSITY OF MICHIGAN AS TO ITS FITNESS FOR ANY PURPOSE, AND
18 * WITHOUT WARRANTY BY THE UNIVERSITY OF MICHIGAN OF ANY KIND, EITHER
19 * EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE. THE REGENTS OF THE UNIVERSITY OF MICHIGAN SHALL NOT BE
22 * LIABLE FOR ANY DAMAGES, INCLUDING DIRECT, SPECIAL, INDIRECT,
23 * INCIDENTAL, OR CONSEQUENTIAL DAMAGES, WITH RESPECT TO ANY CLAIM
24 * ARISING OUT OF OR IN CONNECTION WITH THE USE OF THE SOFTWARE, EVEN
25 * IF IT HAS BEEN OR IS HEREAFTER ADVISED OF THE POSSIBILITY OF SUCH
26 * DAMAGES.
27 *
28 * Authors: Ali G. Saidi
29 *          Andrew L. Schultz
30 *          Miguel J. Serrano
31 */
32
33#ifndef __DEV_8254_HH__
34#define __DEV_8254_HH__
35
36#include "base/bitunion.hh"
37#include "sim/eventq.hh"
38#include "sim/host.hh"
39#include "sim/serialize.hh"
40
41#include <string>
42#include <iostream>
43
44/** Programmable Interval Timer (Intel 8254) */
45class Intel8254Timer
46{
47    BitUnion8(CtrlReg)
48        Bitfield<7, 6> sel;
49        Bitfield<5, 4> rw;
50        Bitfield<3, 1> mode;
51        Bitfield<0> bcd;
52    EndBitUnion(CtrlReg)
53
54    enum SelectVal {
55        SelectCounter0,
56        SelectCounter1,
57        SelectCounter2,
58        ReadBackCommand
59    };
60
61    enum ReadWriteVal {
62        LatchCommand,
63        LsbOnly,
64        MsbOnly,
65        TwoPhase
66    };
67
68    enum ModeVal {
69        InitTc,
70        OneShot,
71        RateGen,
72        SquareWave,
73        SoftwareStrobe,
74        HardwareStrobe
75    };
76
77    /** Counter element for PIT */
78    class Counter
79    {
80        /** Event for counter interrupt */
81        class CounterEvent : public Event
82        {
83          private:
84            /** Pointer back to Counter */
85            Counter* counter;
86            Tick interval;
87
88          public:
89            CounterEvent(Counter*);
90
91            /** Event process */
92            virtual void process();
93
94            /** Event description */
95            virtual const char *description() const;
96
97            friend class Counter;
98        };
99
100      private:
101        std::string _name;
102        const std::string &name() const { return _name; }
103
104        CounterEvent event;
105
106        /** Current count value */
107        uint16_t count;
108
109        /** Latched count */
110        uint16_t latched_count;
111
112        /** Interrupt period */
113        uint16_t period;
114
115        /** Current mode of operation */
116        uint8_t mode;
117
118        /** Output goes high when the counter reaches zero */
119        bool output_high;
120
121        /** State of the count latch */
122        bool latch_on;
123
124        /** Set of values for read_byte and write_byte */
125        enum {LSB, MSB};
126
127        /** Determine which byte of a 16-bit count value to read/write */
128        uint8_t read_byte, write_byte;
129
130      public:
131        Counter(const std::string &name);
132
133        /** Latch the current count (if one is not already latched) */
134        void latchCount();
135
136        /** Set the read/write mode */
137        void setRW(int rw_val);
138
139        /** Set operational mode */
140        void setMode(int mode_val);
141
142        /** Set count encoding */
143        void setBCD(int bcd_val);
144
145        /** Read a count byte */
146        uint8_t read();
147
148        /** Write a count byte */
149        void write(const uint8_t data);
150
151        /** Is the output high? */
152        bool outputHigh();
153
154        /**
155         * Serialize this object to the given output stream.
156         * @param base The base name of the counter object.
157         * @param os   The stream to serialize to.
158         */
159        void serialize(const std::string &base, std::ostream &os);
160
161        /**
162         * Reconstruct the state of this object from a checkpoint.
163         * @param base The base name of the counter object.
164         * @param cp The checkpoint use.
165         * @param section The section name of this object
166         */
167        void unserialize(const std::string &base, Checkpoint *cp,
168                         const std::string &section);
169    };
170
171  private:
172    std::string _name;
173    const std::string &name() const { return _name; }
174
175    /** PIT has three seperate counters */
176    Counter *counter[3];
177
178  public:
179    /** Public way to access individual counters (avoid array accesses) */
180    Counter counter0;
181    Counter counter1;
182    Counter counter2;
183
184    Intel8254Timer(const std::string &name);
185
186    /** Write control word */
187    void writeControl(const CtrlReg data);
188
189    /**
190     * Serialize this object to the given output stream.
191     * @param base The base name of the counter object.
192     * @param os The stream to serialize to.
193     */
194    void serialize(const std::string &base, std::ostream &os);
195
196    /**
197     * Reconstruct the state of this object from a checkpoint.
198     * @param base The base name of the counter object.
199     * @param cp The checkpoint use.
200     * @param section The section name of this object
201     */
202    void unserialize(const std::string &base, Checkpoint *cp,
203                     const std::string &section);
204};
205
206#endif // __DEV_8254_HH__
207