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/kramermc/keydef.h"
28 #include "kc/kramermc/keyboard.h"
29 
30 #include "libdbg/dbg.h"
31 
KeyboardKramerMC(void)32 KeyboardKramerMC::KeyboardKramerMC(void)
33 {
34   init();
35   z80->register_ic(this);
36 }
37 
38 
~KeyboardKramerMC(void)39 KeyboardKramerMC::~KeyboardKramerMC(void)
40 {
41   z80->unregister_ic(this);
42 }
43 
44 void
init(void)45 KeyboardKramerMC::init(void)
46 {
47   _row = 0;
48   _keybuf.code = -1;
49 }
50 
51 int
decode_key(int keysym,bool press)52 KeyboardKramerMC::decode_key(int keysym, bool press)
53 {
54   return __keys[keysym];
55 }
56 
57 void
keyPressed(int keysym,int keycode)58 KeyboardKramerMC::keyPressed(int keysym, int keycode)
59 {
60   int k = decode_key(keysym, true);
61   if (k == -1)
62     return;
63 
64   int mod = (k & 0xff00) >> 8;
65   int key = k & 0xff;
66 
67   if (_keybuf.code != -1)
68     if ((_keybuf.sym1 != mod) || (_keybuf.sym2 != 0))
69       return;
70 
71   if (mod == 0)
72     {
73       _keybuf.sym1 = key;
74       _keybuf.sym2 = 0;
75     }
76   else
77     {
78       _keybuf.sym1 = mod;
79       _keybuf.sym2 = key;
80       z80->addCallback(MODIFIER_PRESS_DELAY, this, (void *)0);
81     }
82   _keybuf.code = keycode;
83 
84   DBG(2, form("KCemu/keyboard/kramermc/key_press",
85 	      "##### Keyboard: keyPressed  [%03x/%02x] {%02x:%02x:%02x}\n",
86 	      keysym,
87 	      k,
88 	      _keybuf.sym1,
89 	      _keybuf.sym2,
90 	      _keybuf.code));
91 
92 }
93 
94 void
keyReleased(int keysym,int keycode)95 KeyboardKramerMC::keyReleased(int keysym, int keycode)
96 {
97   _keybuf.code = -1;
98   DBG(2, form("KCemu/keyboard/kramermc/key_press",
99 	      "##### Keyboard: keyPressed  [%03x/%02x] {%02x:%02x:%02x}\n",
100 	      keysym,
101 	      keycode,
102 	      _keybuf.sym1,
103 	      _keybuf.sym2,
104 	      _keybuf.code));
105 }
106 
107 void
replayString(const char * text)108 KeyboardKramerMC::replayString(const char *text)
109 {
110 }
111 
112 void
callback(void * data)113 KeyboardKramerMC::callback(void *data)
114 {
115   long val = (long)data;
116 
117   if (val == 0)
118     {
119       /*
120        *  remove possible shift or control state
121        */
122       _keybuf.sym1 = 0;
123       z80->addCallback(MODIFIER_RELEASE_DELAY, this, (void *)1);
124       //printf("{remove shift}\n");
125     }
126   else
127     {
128       _keybuf.sym1 = _keybuf.sym2;
129       _keybuf.sym2 = 0;
130       //printf("{2nd key}\n");
131     }
132 }
133 
134 int
callback_A_in(void)135 KeyboardKramerMC::callback_A_in(void)
136 {
137   return -1;
138 }
139 
140 void
callback_A_out(byte_t val)141 KeyboardKramerMC::callback_A_out(byte_t val)
142 {
143   _row = ((val >> 1) & 0x07) + 1;
144 }
145 
146 int
callback_B_in(void)147 KeyboardKramerMC::callback_B_in(void)
148 {
149   int col = 0;
150   static int old_val = -1;
151 
152   int v = _keybuf.sym1;
153 
154   //if (v != old_val)
155     //printf("[%02x - %3d]\n", v, v);
156   old_val = v;
157 
158   if (_keybuf.code != -1)
159     if (_keybuf.sym1 > 0)
160       {
161 	if (_row == (v & 0x0f))
162 	  col |= 1 << (((v >> 4) & 0x0f) - 1);
163       }
164 
165   return col ^ 0xff;
166 }
167 
168 void
callback_B_out(byte_t val)169 KeyboardKramerMC::callback_B_out(byte_t val)
170 {
171 }
172 
173 void
reset(bool power_on)174 KeyboardKramerMC::reset(bool power_on)
175 {
176   init();
177 }
178 
179 void
reti(void)180 KeyboardKramerMC::reti(void)
181 {
182 }
183