libsidplayfp  2.1.0
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 MOS6526;
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  MOS6526 &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  bool interruptMasked() const { return icr & idr; }
78 
79  bool interruptTriggered() const { return idr & INTERRUPT_REQUEST; }
80 
81  void triggerInterrupt() { idr |= INTERRUPT_REQUEST; }
82 
83 protected:
90  InterruptSource(EventScheduler &scheduler, MOS6526 &parent) :
91  Event("CIA Interrupt"),
92  parent(parent),
93  eventScheduler(scheduler),
94  last_clear(0),
95  icr(0),
96  idr(0),
97  scheduled(false)
98  {}
99 
103  void schedule()
104  {
105  if (!scheduled)
106  {
107  eventScheduler.schedule(*this, 1, EVENT_CLOCK_PHI1);
108  scheduled = true;
109  }
110  }
111 
112  void interrupt(bool state);
113 
114 public:
115  virtual ~InterruptSource() {}
116 
122  virtual void trigger(uint8_t interruptMask) { idr |= interruptMask; }
123 
129  virtual uint8_t clear();
130 
135  virtual void reset()
136  {
137  icr = 0;
138  idr = 0;
139  eventScheduler.cancel(*this);
140  scheduled = false;
141  }
142 
148  void set(uint8_t interruptMask)
149  {
150  if (interruptMask & 0x80)
151  {
152  icr |= interruptMask & ~INTERRUPT_REQUEST;
154  }
155  else
156  {
157  icr &= ~interruptMask;
158  }
159  }
160 
164  void event() override;
165 };
166 
167 }
168 
169 #endif // INTERRUPT_H
Definition: EventScheduler.h:62
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
void set(uint8_t interruptMask)
Definition: interrupt.h:148
void schedule()
Definition: interrupt.h:103
InterruptSource(EventScheduler &scheduler, MOS6526 &parent)
Definition: interrupt.h:90
@ 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
virtual void trigger(uint8_t interruptMask)
Definition: interrupt.h:122
uint8_t icr
Interrupt control register.
Definition: interrupt.h:67
virtual void reset()
Definition: interrupt.h:135
virtual uint8_t clear()
Definition: interrupt.cpp:44
Definition: mos6526.h:153