1 /*
2  * libtilemcore - Graphing calculator emulation library
3  *
4  * Copyright (C) 2009 Benjamin Moody
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation; either version 2.1 of
9  * the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24 
25 #include <stdio.h>
26 #include "tilem.h"
27 #include "scancodes.h"
28 
tilem_keypad_reset(TilemCalc * calc)29 void tilem_keypad_reset(TilemCalc* calc)
30 {
31 	int i;
32 
33 	calc->keypad.group = 0xff;
34 	calc->keypad.onkeydown = 0;
35 	calc->keypad.onkeyint = 0;
36 	for (i = 0; i < 8; i++)
37 		calc->keypad.keysdown[i] = 0;
38 }
39 
tilem_keypad_set_group(TilemCalc * calc,byte group)40 void tilem_keypad_set_group(TilemCalc* calc, byte group)
41 {
42 	calc->keypad.group = group;
43 }
44 
tilem_keypad_read_keys(TilemCalc * calc)45 byte tilem_keypad_read_keys(TilemCalc* calc)
46 {
47 	int i;
48 	byte keys, old;
49 
50 	keys = 0;
51 	for (i = 0; i < 8; i++) {
52 		if (!(calc->keypad.group & (1 << i)))
53 			keys |= calc->keypad.keysdown[i];
54 	}
55 
56 	do {
57 		old = keys;
58 		for (i = 0; i < 8; i++) {
59 			if (keys & calc->keypad.keysdown[i])
60 				keys |= calc->keypad.keysdown[i];
61 		}
62 	} while (keys != old);
63 
64 	return ~keys;
65 }
66 
tilem_keypad_press_key(TilemCalc * calc,int scancode)67 void tilem_keypad_press_key(TilemCalc* calc, int scancode)
68 {
69 	if (scancode == TILEM_KEY_ON) {
70 		if (!calc->keypad.onkeydown && calc->keypad.onkeyint)
71 			calc->z80.interrupts |= TILEM_INTERRUPT_ON_KEY;
72 		calc->keypad.onkeydown = 1;
73 	}
74 	else if (scancode > 0 && scancode < 65) {
75 		scancode--;
76 		calc->keypad.keysdown[scancode / 8] |= (1 << (scancode % 8));
77 	}
78 }
79 
tilem_keypad_release_key(TilemCalc * calc,int scancode)80 void tilem_keypad_release_key(TilemCalc* calc, int scancode)
81 {
82 	if (scancode == TILEM_KEY_ON) {
83 		if (calc->keypad.onkeydown && calc->keypad.onkeyint)
84 			calc->z80.interrupts |= TILEM_INTERRUPT_ON_KEY;
85 		calc->keypad.onkeydown = 0;
86 	}
87 	else if (scancode > 0 && scancode < 65) {
88 		scancode--;
89 		calc->keypad.keysdown[scancode / 8] &= ~(1 << (scancode % 8));
90 	}
91 }
92