1 /* Declarations to support the VMIPS clock device.
2    Copyright 2003 Paul Twohey.
3 
4 This file is part of VMIPS.
5 
6 VMIPS is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2 of the License, or (at your
9 option) any later version.
10 
11 VMIPS is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15 
16 You should have received a copy of the GNU General Public License along
17 with VMIPS; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19 
20 #ifndef _CLOCKDEV_H_
21 #define _CLOCKDEV_H_
22 
23 #include "clock.h"
24 #include "deviceint.h"
25 #include "devicemap.h"
26 #include "devreg.h"
27 #include "task.h"
28 #include <new>
29 
30 /* High resolution clock device. */
31 class ClockDevice : public DeviceMap, public DeviceInt
32 {
33 public:
34 	/* Create a new clock device that uses CLOCK as its time source and
35 	   which reports interrupts on irq IRQ at the regular interval
36 	   FREQUENCY_NS nanoseconds. */
37 	ClockDevice( Clock *clock, uint32 irq, long frequency_ns );
38 
39 	/* Destroy the clock device and cancel any tasks it may have
40 	   waiting to execute on CLOCK. */
41 	virtual ~ClockDevice();
42 
43 	/* Utility function called to trigger a clock interrupt, transitions
44 	   the clock to the READY state and asserts an interrupt if they
45 	   are enabled. Used in conjunction with class ClockTrigger to make
46 	   clock interrupts. */
47 	virtual void ready_clock();
48 
49 	virtual uint32 fetch_word(uint32 offset, int mode, DeviceExc *client);
50 	virtual void store_word(uint32 offset, uint32 data, DeviceExc *client);
51 
52 	/* Return a description of this device. */
53 	virtual const char *descriptor_str() const;
54 
55 protected:
56 	/* Transition the clock into the UNREADY state and deassert the
57 	   clock interrupt. */
58 	virtual void unready_clock();
59 
60 protected:
61 	/* Utility class to trigger interrupts to the clock device. */
62 	class ClockTrigger : public CancelableTask
63 	{
64 	public:
65 		ClockTrigger( ClockDevice *clock_device );
66 		virtual ~ClockTrigger();
67 
68 	protected:
69 		virtual void real_task();
70 
71 	protected:
72 		ClockDevice	*clock_device;
73 	};
74 
75 protected:
76 	const uint32	irq;
77 	const long	frequency_ns;
78 
79 	enum State {
80 		UNREADY	= 0,
81 		READY	= CTL_RDY
82 	};
83 
84 	Clock		*clock;
85 	ClockTrigger	*clock_trigger;
86 	State		clock_state;
87 	bool		interrupt_enabled;
88 };
89 
90 #endif /* _CLOCKDEV_H_ */
91