1 //
2 //   Copyright (C) 2010 by sinamas <sinamas at users.sourceforge.net>
3 //
4 //   This program is free software; you can redistribute it and/or modify
5 //   it under the terms of the GNU General Public License version 2 as
6 //   published by the Free Software Foundation.
7 //
8 //   This program is distributed in the hope that it will be useful,
9 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 //   GNU General Public License version 2 for more details.
12 //
13 //   You should have received a copy of the GNU General Public License
14 //   version 2 along with this program; if not, write to the
15 //   Free Software Foundation, Inc.,
16 //   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17 //
18 
19 #ifndef INTERRUPT_REQUESTER_H
20 #define INTERRUPT_REQUESTER_H
21 
22 #include "counterdef.h"
23 #include "minkeeper.h"
24 
25 namespace gambatte {
26 
27 struct SaveState;
28 
29 enum IntEventId { intevent_unhalt,
30                   intevent_end,
31                   intevent_blit,
32                   intevent_serial,
33                   intevent_oam,
34                   intevent_dma,
35                   intevent_tima,
36                   intevent_video,
37                   intevent_interrupts, intevent_last = intevent_interrupts };
38 
39 class InterruptRequester {
40 public:
41 	InterruptRequester();
42 	void saveState(SaveState &) const;
43 	void loadState(SaveState const &);
44 	void resetCc(unsigned long oldCc, unsigned long newCc);
ifreg()45 	unsigned ifreg() const { return ifreg_; }
pendingIrqs()46 	unsigned pendingIrqs() const { return ifreg_ & iereg_; }
ime()47 	bool ime() const { return intFlags_.ime(); }
halted()48 	bool halted() const { return intFlags_.halted(); }
49 	void ei(unsigned long cc);
50 	void di();
51 	void halt();
52 	void unhalt();
53 	void flagIrq(unsigned bit);
54 	void ackIrq(unsigned bit);
55 	void setIereg(unsigned iereg);
56 	void setIfreg(unsigned ifreg);
57 
minEventId()58 	IntEventId minEventId() const { return static_cast<IntEventId>(eventTimes_.min()); }
minEventTime()59 	unsigned long minEventTime() const { return eventTimes_.minValue(); }
setEventTime(unsigned long value)60 	template<IntEventId id> void setEventTime(unsigned long value) { eventTimes_.setValue<id>(value); }
setEventTime(IntEventId id,unsigned long value)61 	void setEventTime(IntEventId id, unsigned long value) { eventTimes_.setValue(id, value); }
eventTime(IntEventId id)62 	unsigned long eventTime(IntEventId id) const { return eventTimes_.value(id); }
63 
64 private:
65 	class IntFlags {
66 	public:
IntFlags()67 		IntFlags() : flags_(0) {}
ime()68 		bool ime() const { return flags_ & flag_ime; }
halted()69 		bool halted() const { return flags_ & flag_halted; }
imeOrHalted()70 		bool imeOrHalted() const { return flags_; }
setIme()71 		void setIme() { flags_ |= flag_ime; }
unsetIme()72 		void unsetIme() { flags_ &= ~flag_ime; }
setHalted()73 		void setHalted() { flags_ |= flag_halted; }
unsetHalted()74 		void unsetHalted() { flags_ &= ~flag_halted; }
set(bool ime,bool halted)75 		void set(bool ime, bool halted) { flags_ = halted * flag_halted + ime * flag_ime; }
76 
77 	private:
78 		unsigned char flags_;
79 		enum { flag_ime = 1, flag_halted = 2 };
80 	};
81 
82 	MinKeeper<intevent_last + 1> eventTimes_;
83 	unsigned long minIntTime_;
84 	unsigned ifreg_;
85 	unsigned iereg_;
86 	IntFlags intFlags_;
87 };
88 
flagHdmaReq(InterruptRequester & intreq)89 inline void flagHdmaReq(InterruptRequester &intreq) { intreq.setEventTime<intevent_dma>(0); }
flagGdmaReq(InterruptRequester & intreq)90 inline void flagGdmaReq(InterruptRequester &intreq) { intreq.setEventTime<intevent_dma>(1); }
ackDmaReq(InterruptRequester & intreq)91 inline void ackDmaReq(InterruptRequester &intreq) { intreq.setEventTime<intevent_dma>(disabled_time); }
hdmaReqFlagged(InterruptRequester const & intreq)92 inline bool hdmaReqFlagged(InterruptRequester const &intreq) { return intreq.eventTime(intevent_dma) == 0; }
gdmaReqFlagged(InterruptRequester const & intreq)93 inline bool gdmaReqFlagged(InterruptRequester const &intreq) { return intreq.eventTime(intevent_dma) == 1; }
94 
95 }
96 
97 #endif
98