libsidplayfp  2.2.2
interrupt.h
1 /*
2  * This file is part of libsidplayfp, a SID player engine.
3  *
4  * Copyright 2011-2020 Leandro Nini <drfiemost@users.sourceforge.net>
5  * Copyright 2007-2010 Antti Lankila
6  * Copyright 2000 Simon White
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  */
22 
23 #ifndef INTERRUPT_H
24 #define INTERRUPT_H
25 
26 #include "Event.h"
27 #include "EventScheduler.h"
28 
29 #include <stdint.h>
30 
31 #include "sidcxx11.h"
32 
33 namespace libsidplayfp
34 {
35 
36 class MOS652X;
37 
41 class InterruptSource : protected Event
42 {
43 public:
44  enum
45  {
49  INTERRUPT_ALARM = 1 << 2,
50  INTERRUPT_SP = 1 << 3,
51  INTERRUPT_FLAG = 1 << 4,
52  INTERRUPT_REQUEST = 1 << 7
53  };
54 
55 private:
57  MOS652X &parent;
58 
59 protected:
62 
64  event_clock_t last_clear;
65 
67  uint8_t icr;
68 
70  uint8_t idr;
71 
72 private:
74  bool scheduled;
75 
76 protected:
77  inline bool interruptMasked() const { return icr & idr; }
78 
79  inline bool interruptTriggered() const { return idr & INTERRUPT_REQUEST; }
80 
81  inline void triggerInterrupt() { idr |= INTERRUPT_REQUEST; }
82 
86  inline bool ack0() const { return eventScheduler.getTime(EVENT_CLOCK_PHI2) == (last_clear+1); }
87 
88 protected:
95  InterruptSource(EventScheduler &scheduler, MOS652X &parent) :
96  Event("CIA Interrupt"),
97  parent(parent),
98  eventScheduler(scheduler),
99  last_clear(0),
100  icr(0),
101  idr(0),
102  scheduled(false)
103  {}
104 
108  void schedule()
109  {
110  if (!scheduled)
111  {
112  eventScheduler.schedule(*this, 1, EVENT_CLOCK_PHI1);
113  scheduled = true;
114  }
115  }
116 
117  void interrupt(bool state);
118 
119 public:
120  virtual ~InterruptSource() {}
121 
127  virtual void trigger(uint8_t interruptMask) { idr |= interruptMask; }
128 
134  virtual uint8_t clear();
135 
140  virtual void reset()
141  {
142  icr = 0;
143  idr = 0;
144  eventScheduler.cancel(*this);
145  scheduled = false;
146  }
147 
153  void set(uint8_t interruptMask)
154  {
155  if (interruptMask & 0x80)
156  {
157  icr |= interruptMask & ~INTERRUPT_REQUEST;
159  }
160  else
161  {
162  icr &= ~interruptMask;
163  }
164  }
165 
169  void event() override;
170 };
171 
172 }
173 
174 #endif // INTERRUPT_H
Definition: EventScheduler.h:62
event_clock_t getTime(event_phase_t phase) const
Definition: EventScheduler.h:162
void cancel(Event &event)
Definition: EventScheduler.cpp:35
Definition: Event.h:39
Definition: interrupt.h:42
uint8_t idr
Interrupt data register.
Definition: interrupt.h:70
EventScheduler & eventScheduler
Event scheduler.
Definition: interrupt.h:61
event_clock_t last_clear
Clock when clear was called last.
Definition: interrupt.h:64
void event() override
Definition: interrupt.cpp:31
InterruptSource(EventScheduler &scheduler, MOS652X &parent)
Definition: interrupt.h:95
void set(uint8_t interruptMask)
Definition: interrupt.h:153
void schedule()
Definition: interrupt.h:108
@ INTERRUPT_NONE
no interrupt
Definition: interrupt.h:46
@ INTERRUPT_REQUEST
control bit
Definition: interrupt.h:52
@ INTERRUPT_SP
serial port
Definition: interrupt.h:50
@ INTERRUPT_UNDERFLOW_B
underflow Timer B
Definition: interrupt.h:48
@ INTERRUPT_FLAG
external flag
Definition: interrupt.h:51
@ INTERRUPT_ALARM
alarm clock
Definition: interrupt.h:49
@ INTERRUPT_UNDERFLOW_A
underflow Timer A
Definition: interrupt.h:47
bool ack0() const
Definition: interrupt.h:86
virtual void trigger(uint8_t interruptMask)
Definition: interrupt.h:127
uint8_t icr
Interrupt control register.
Definition: interrupt.h:67
virtual void reset()
Definition: interrupt.h:140
virtual uint8_t clear()
Definition: interrupt.cpp:44
Definition: mos652x.h:151