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/keys.h"
27 #include "kc/keyb9.h"
28 #include "kc/keyb9k.h"
29 
30 #include "cmd/cmd.h"
31 
Keyboard9(void)32 Keyboard9::Keyboard9(void)
33 {
34   init();
35   z80->register_ic(this);
36 }
37 
~Keyboard9(void)38 Keyboard9::~Keyboard9(void)
39 {
40   z80->unregister_ic(this);
41 }
42 
43 void
init(void)44 Keyboard9::init(void)
45 {
46   int a;
47 
48   for (a = 0;a < MAX_KEYS;a++)
49     {
50       _keybuf[a].sym = -1;
51       _keybuf[a].code = -1;
52     }
53 }
54 
55 int
decode_key(int keysym,bool press)56 Keyboard9::decode_key(int keysym, bool press)
57 {
58   return keysym;
59 }
60 
61 void
keyPressed(int keysym,int keycode)62 Keyboard9::keyPressed(int keysym, int keycode)
63 {
64   int a;
65   int k;
66   byte_t c1, c2, r1, r2;
67 
68   k = decode_key(keysym, true);
69   if (k == -1)
70     return;
71 
72   k = __keys[k];
73 
74   c1 = r1 = 0;
75   for (a = 0;a < MAX_KEYS;a++)
76     {
77       if (_keybuf[a].code != -1)
78         {
79           r1 |= 1 << (((__keys[_keybuf[a].sym] >> 4) & 0x0f) - 1);
80           c1 |= 1 << (((__keys[_keybuf[a].sym]     ) & 0x0f) - 1);
81         }
82       if (k != 0)
83         if (_keybuf[a].code == -1)
84           {
85             _keybuf[a].sym = k & 0xff;
86             _keybuf[a].code = keycode;
87             r2 = 1 << (((k >> 4) & 0x0f) - 1);
88             c2 = 1 << (((k     ) & 0x0f) - 1);
89             k >>= 8;
90           }
91     }
92 
93 #if 0
94   cout.form("##### Keyboard: keyPressed  [%02x/%02x] "
95             "r/c: [%02x/%02x]=>[%02x/%02x] -",
96             keysym, keycode,
97             r1, c1, r1 | r2, c1 | c2);
98   for (a = 0;a < MAX_KEYS;a++)
99     if (_keybuf[a].code != -1)
100       cout.form(" {%02d:%02x}", a, _keybuf[a].code);
101   cout.form("\n");
102 #endif
103 }
104 
105 void
keyReleased(int keysym,int keycode)106 Keyboard9::keyReleased(int keysym, int keycode)
107 {
108   int a;
109 
110   if (keysym == -1)
111     {
112       for (a = 0;a < MAX_KEYS;a++)
113         _keybuf[a].code = -1;
114       return;
115     }
116 
117   for (a = 0;a < MAX_KEYS;a++)
118     if (_keybuf[a].code == keycode)
119       _keybuf[a].code = -1;
120 
121 #if 0
122   cout.form("##### Keyboard: keyReleased [%02x/%02x] -",
123             keysym,
124             keycode);
125   for (a = 0;a < MAX_KEYS;a++)
126     if (_keybuf[a].code != -1)
127       cout.form(" {%02d:%02x}", a, _keybuf[a].code);
128   cout.form("\n");
129 #endif
130 }
131 
132 void
replayString(const char * text)133 Keyboard9::replayString(const char *text)
134 {
135 }
136 
137 void
callback(void * data)138 Keyboard9::callback(void *data)
139 {
140 }
141 
142 void
reset(bool power_on)143 Keyboard9::reset(bool power_on)
144 {
145   init();
146 }
147 
148 void
reti(void)149 Keyboard9::reti(void)
150 {
151 }
152 
153 void
set_val(byte_t val)154 Keyboard9::set_val(byte_t val)
155 {
156   _line = val;
157 }
158 
159 byte_t
get_val()160 Keyboard9::get_val()
161 {
162   int a, c;
163 
164   c = 0;
165   for (a = 0;a < MAX_KEYS;a++)
166     {
167       if (_keybuf[a].code == -1)
168 	continue;
169 
170       if (_line == (((_keybuf[a].sym >> 4) & 0x0f) - 1))
171 	c |= 1 << ((_keybuf[a].sym & 0x0f) - 1);
172     }
173 
174   return ~c;
175 }
176