dev: Delete the authors list from files in src/dev.
[gem5.git] / src / dev / intel_8254_timer.hh
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
29 #ifndef __DEV_8254_HH__
30 #define __DEV_8254_HH__
31
32 #include <iostream>
33 #include <string>
34
35 #include "base/bitunion.hh"
36 #include "base/types.hh"
37 #include "base/trace.hh"
38 #include "debug/Intel8254Timer.hh"
39 #include "sim/eventq_impl.hh"
40 #include "sim/serialize.hh"
41
42 /** Programmable Interval Timer (Intel 8254) */
43 class Intel8254Timer : public EventManager
44 {
45 protected:
46 BitUnion8(CtrlReg)
47 Bitfield<7, 6> sel;
48 Bitfield<5, 4> rw;
49 Bitfield<3, 1> mode;
50 Bitfield<0> bcd;
51 EndBitUnion(CtrlReg)
52
53 enum SelectVal {
54 SelectCounter0,
55 SelectCounter1,
56 SelectCounter2,
57 ReadBackCommand
58 };
59
60 enum ReadWriteVal {
61 LatchCommand,
62 LsbOnly,
63 MsbOnly,
64 TwoPhase
65 };
66
67 enum ModeVal {
68 InitTc,
69 OneShot,
70 RateGen,
71 SquareWave,
72 SoftwareStrobe,
73 HardwareStrobe
74 };
75
76 /** Counter element for PIT */
77 class Counter
78 {
79 /** Event for counter interrupt */
80 class CounterEvent : public Event
81 {
82 private:
83 /** Pointer back to Counter */
84 Counter* counter;
85 Tick interval;
86
87 public:
88 CounterEvent(Counter*);
89
90 /** Event process */
91 void process();
92
93 /** Event description */
94 virtual const char *description() const;
95
96 friend class Counter;
97
98 void setTo(int clocks);
99
100 int clocksLeft();
101
102 Tick getInterval();
103 };
104
105 private:
106 std::string _name;
107 const std::string &name() const { return _name; }
108
109 unsigned int num;
110
111 CounterEvent event;
112
113 /** True after startup is called. */
114 bool running;
115
116 /** Initial count value */
117 uint16_t initial_count;
118
119 /** Latched count */
120 uint16_t latched_count;
121
122 /** Interrupt period */
123 uint16_t period;
124
125 /** When to start ticking */
126 Tick offset;
127
128 /** Current mode of operation */
129 uint8_t mode;
130
131 /** Output goes high when the counter reaches zero */
132 bool output_high;
133
134 /** State of the count latch */
135 bool latch_on;
136
137 /** Set of values for read_byte and write_byte */
138 enum {LSB, MSB};
139
140 /** Determine which byte of a 16-bit count value to read/write */
141 uint8_t read_byte, write_byte;
142
143 /** Pointer to container */
144 Intel8254Timer *parent;
145
146 public:
147 Counter(Intel8254Timer *p, const std::string &name, unsigned int num);
148
149 /** Latch the current count (if one is not already latched) */
150 void latchCount();
151
152 /** Get the current count for this counter */
153 int currentCount();
154
155 /** Set the read/write mode */
156 void setRW(int rw_val);
157
158 /** Set operational mode */
159 void setMode(int mode_val);
160
161 /** Set count encoding */
162 void setBCD(int bcd_val);
163
164 /** Read a count byte */
165 uint8_t read();
166
167 /** Write a count byte */
168 void write(const uint8_t data);
169
170 /** Is the output high? */
171 bool outputHigh();
172
173 /**
174 * Serialize this object to the given output stream.
175 * @param base The base name of the counter object.
176 * @param os The stream to serialize to.
177 */
178 void serialize(const std::string &base, CheckpointOut &cp) const;
179
180 /**
181 * Reconstruct the state of this object from a checkpoint.
182 * @param base The base name of the counter object.
183 * @param cp The checkpoint use.
184 * @param section The section name of this object
185 */
186 void unserialize(const std::string &base, CheckpointIn &cp);
187
188 /** Start ticking */
189 void startup();
190 };
191
192 protected:
193 std::string _name;
194 const std::string &name() const { return _name; }
195
196 /** PIT has three seperate counters */
197 Counter *counter[3];
198
199 virtual void
200 counterInterrupt(unsigned int num)
201 {
202 DPRINTF(Intel8254Timer, "Timer interrupt from counter %d.\n", num);
203 }
204
205 public:
206
207 virtual
208 ~Intel8254Timer()
209 {}
210
211 Intel8254Timer(EventManager *em, const std::string &name,
212 Counter *counter0, Counter *counter1, Counter *counter2);
213
214 Intel8254Timer(EventManager *em, const std::string &name);
215
216 /** Write control word */
217 void writeControl(const CtrlReg data);
218
219 uint8_t
220 readCounter(unsigned int num)
221 {
222 assert(num < 3);
223 return counter[num]->read();
224 }
225
226 void
227 writeCounter(unsigned int num, const uint8_t data)
228 {
229 assert(num < 3);
230 counter[num]->write(data);
231 }
232
233 bool
234 outputHigh(unsigned int num)
235 {
236 assert(num < 3);
237 return counter[num]->outputHigh();
238 }
239
240 /**
241 * Serialize this object to the given output stream.
242 * @param base The base name of the counter object.
243 * @param os The stream to serialize to.
244 */
245 void serialize(const std::string &base, CheckpointOut &cp) const;
246
247 /**
248 * Reconstruct the state of this object from a checkpoint.
249 * @param base The base name of the counter object.
250 * @param cp The checkpoint use.
251 * @param section The section name of this object
252 */
253 void unserialize(const std::string &base, CheckpointIn &cp);
254
255 /** Start ticking */
256 void startup();
257 };
258
259 #endif // __DEV_8254_HH__