1 /*
2  *  KCemu -- The emulator for the KC85 homecomputer series and much more.
3  *  Copyright (C) 1997-2010 Torsten Paul
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License along
16  *  with this program; if not, write to the Free Software Foundation, Inc.,
17  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #include <iostream>
21 #include <iomanip>
22 
23 #include "kc/system.h"
24 
25 #include "kc/z80.h"
26 #include "kc/ctc_base.h"
27 
28 #include "libdbg/dbg.h"
29 
CTC_Base(void)30 CTC_Base::CTC_Base(void) : CTC("CTC")
31 {
32   z80->register_ic(this);
33   _z80_irq_mask = z80->get_irq_mask();
34 }
35 
~CTC_Base(void)36 CTC_Base::~CTC_Base(void)
37 {
38   z80->unregister_ic(this);
39 }
40 
41 long long
get_counter()42 CTC_Base::get_counter()
43 {
44   return z80->getCounter();
45 }
46 
47 void
irqreq(void)48 CTC_Base::irqreq(void)
49 {
50   DBG(2, form("KCemu/CTC/reti",
51 	      "CTC_Base::irqreq()\n"));
52   z80->set_irq_line(_z80_irq_mask);
53 }
54 
55 word_t
irqack(void)56 CTC_Base::irqack(void)
57 {
58   int a, b;
59   int vector = IRQ_NOT_ACK;
60 
61   DBG(2, form("KCemu/CTC/reti",
62 	      "CTC_Base::irqack(): active: %d %d %d %d - pending: %d %d %d %d\n",
63 	      _irq_active[0],
64 	      _irq_active[1],
65 	      _irq_active[2],
66 	      _irq_active[3],
67 	      _irq_pending[0],
68 	      _irq_pending[1],
69 	      _irq_pending[2],
70 	      _irq_pending[3]));
71 
72   for (a = 0;a < 4;a++)
73     {
74       if (_irq_pending[a])
75 	{
76 	  _irq_active[a] = 1;
77 	  _irq_pending[a] = 0;
78 	  vector = getIRQVector(a);
79 	  DBG(2, form("KCemu/CTC/reti",
80 		      "CTC_Base::irqack(): channel = %d, irq_vector = %02xh\n",
81 		      a, vector));
82 	  break;
83 	}
84     }
85 
86   b = 0;
87   for (a = 0;a < 4;a++)
88     if (_irq_pending[a])
89       b++;
90 
91   if (b == 0)
92     z80->reset_irq_line(_z80_irq_mask);
93 
94   return vector;
95 }
96 
97 void
trigger_irq(int channel)98 CTC_Base::trigger_irq(int channel)
99 {
100   int a;
101 
102   for (a = 0;a < channel;a++)
103     if (_irq_active[a])
104       return;
105 
106   irq();
107 }
108 
109 void
add_callback(unsigned long long offset,Callback * cb,void * data)110 CTC_Base::add_callback(unsigned long long offset, Callback *cb, void *data)
111 {
112   z80->addCallback(offset, cb, data);
113 }
114