Skip to content

Commit

Permalink
core, generalise keyboard handling
Browse files Browse the repository at this point in the history
  • Loading branch information
irixxxx committed Jan 13, 2025
1 parent 563e147 commit c1029b9
Show file tree
Hide file tree
Showing 11 changed files with 315 additions and 334 deletions.
2 changes: 1 addition & 1 deletion pico/pico.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ typedef struct PicoInterface
void (*mcdTrayOpen)(void);
void (*mcdTrayClose)(void);

unsigned int ps2; // PS/2 peripherals, e.g. Pico Keyboard
unsigned int kbd; // PS/2 peripherals, e.g. Pico Keyboard
} PicoInterface;

extern PicoInterface PicoIn;
Expand Down
14 changes: 7 additions & 7 deletions pico/pico/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,23 +139,23 @@ static u32 PicoRead8_pico_kb(u32 a)

PicoPicohw.kb.has_read = 1;

u32 key_shift = (PicoIn.ps2 & 0xff00) >> 8;
u32 key = (PicoIn.ps2 & 0x00ff);
u32 key_shift = (PicoIn.kbd & 0xff00) >> 8;
u32 key = (PicoIn.kbd & 0x00ff);

// The Shift key requires 2 key up events to be registered:
// SHIFT_UP_HELD_DOWN to allow the game to register the key down event
// for the next held down key(s), and SHIFT_UP when the Shift key
// is no longer held down.
//
// For the second key up event, we need to
// override the parsed key code with PEVB_PICO_PS2_LSHIFT,
// override the parsed key code with PEVB_KBD_SHIFT,
// otherwise it will be zero and the game won't clear its Shift key state.
u32 key_code = (key_shift
&& !key
&& PicoPicohw.kb.key_state != KEY_UP
&& PicoPicohw.kb.shift_state != SHIFT_UP_HELD_DOWN)
? key_shift
: PicoPicohw.kb.shift_state == SHIFT_UP ? PEVB_PICO_PS2_LSHIFT : key;
: PicoPicohw.kb.shift_state == SHIFT_UP ? PEVB_KBD_SHIFT : key;
u32 key_code_7654 = (key_code & 0xf0) >> 4;
u32 key_code_3210 = (key_code & 0x0f);
switch(PicoPicohw.kb.i) {
Expand All @@ -175,7 +175,7 @@ static u32 PicoRead8_pico_kb(u32 a)
break;
case 0x7: // cap/num/scr
if (PicoPicohw.kb.active) {
if (key == PEVB_PICO_PS2_CAPSLOCK && PicoPicohw.kb.has_caps_lock == 1) {
if (key == PEVB_KBD_CAPSLOCK && PicoPicohw.kb.has_caps_lock == 1) {
PicoPicohw.kb.caps_lock = PicoPicohw.kb.caps_lock == 4 ? 0 : 4;
PicoPicohw.kb.has_caps_lock = 0;
}
Expand Down Expand Up @@ -216,8 +216,8 @@ static u32 PicoRead8_pico_kb(u32 a)
PicoPicohw.kb.time_keydown = get_ticks() - PicoPicohw.kb.start_time_keydown;
if (PicoPicohw.kb.time_keydown > 350
// Modifier keys don't have typematic
&& key_code != PEVB_PICO_PS2_CAPSLOCK
&& key_code != PEVB_PICO_PS2_LSHIFT) {
&& key_code != PEVB_KBD_CAPSLOCK
&& key_code != PEVB_KBD_SHIFT) {
d |= 8; // Send key down a.k.a. make
if (PicoPicohw.kb.key_state == KEY_DOWN)
elprintf(EL_PICOHW, "PicoPicohw.kb.key_state: KEY DOWN\n");
Expand Down
152 changes: 76 additions & 76 deletions pico/sms.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,92 +140,92 @@ static u8 vdp_hcounter(int cycles)
// row 1: q w e r t y u
// row 2: a s d f g h j
// row 3: z x c v b n m
// row 4: ED SP CL DL
// row 5: , . / PI v < >
// row 6: k l ; : ] CR ^
// row 4: ED SP CL DL kana space clear/home del/ins
// row 5: , . / PI v < > pi
// row 6: k l ; : ] CR ^ enter
// row 7: i o p @ [
// port B @0xdd:
// row 8: 8 9 0 - ^ YN BR
// row 9: GR
// row 10: CTL
// row 11: FN SFT
// row 8: 8 9 0 - ^ YN BR yen break
// row 9: GR graph
// row 10: CTL control
// row 11: FN SFT func shift
static unsigned char kbd_matrix[12];

// row | col
static unsigned char kbd_map[] = {
[PEVB_PICO_PS2_1] = 0x00,
[PEVB_PICO_PS2_2] = 0x01,
[PEVB_PICO_PS2_3] = 0x02,
[PEVB_PICO_PS2_4] = 0x03,
[PEVB_PICO_PS2_5] = 0x04,
[PEVB_PICO_PS2_6] = 0x05,
[PEVB_PICO_PS2_7] = 0x06,
[PEVB_PICO_PS2_8] = 0x80,
[PEVB_PICO_PS2_9] = 0x81,
[PEVB_PICO_PS2_0] = 0x82,
[PEVB_PICO_PS2_MINUS] = 0x83,
[PEVB_PICO_PS2_CARET] = 0x84,
[PEVB_PICO_PS2_YEN] = 0x85,
[PEVB_PICO_PS2_ESCAPE] = 0x86, // break

[PEVB_PICO_PS2_q] = 0x10,
[PEVB_PICO_PS2_w] = 0x11,
[PEVB_PICO_PS2_e] = 0x12,
[PEVB_PICO_PS2_r] = 0x13,
[PEVB_PICO_PS2_t] = 0x14,
[PEVB_PICO_PS2_y] = 0x15,
[PEVB_PICO_PS2_u] = 0x16,
[PEVB_PICO_PS2_i] = 0x70,
[PEVB_PICO_PS2_o] = 0x71,
[PEVB_PICO_PS2_p] = 0x72,
[PEVB_PICO_PS2_AT] = 0x73,
[PEVB_PICO_PS2_LEFTBRACKET] = 0x74,

[PEVB_PICO_PS2_a] = 0x20,
[PEVB_PICO_PS2_s] = 0x21,
[PEVB_PICO_PS2_d] = 0x22,
[PEVB_PICO_PS2_f] = 0x23,
[PEVB_PICO_PS2_g] = 0x24,
[PEVB_PICO_PS2_h] = 0x25,
[PEVB_PICO_PS2_j] = 0x26,
[PEVB_PICO_PS2_k] = 0x60,
[PEVB_PICO_PS2_l] = 0x61,
[PEVB_PICO_PS2_SEMICOLON] = 0x62,
[PEVB_PICO_PS2_COLON] = 0x63,
[PEVB_PICO_PS2_RIGHTBRACKET] = 0x64,
[PEVB_PICO_PS2_RETURN] = 0x65,
[PEVB_PICO_PS2_UP] = 0x66, // up

[PEVB_PICO_PS2_z] = 0x30,
[PEVB_PICO_PS2_x] = 0x31,
[PEVB_PICO_PS2_c] = 0x32,
[PEVB_PICO_PS2_v] = 0x33,
[PEVB_PICO_PS2_b] = 0x34,
[PEVB_PICO_PS2_n] = 0x35,
[PEVB_PICO_PS2_m] = 0x36,
[PEVB_PICO_PS2_COMMA] = 0x50,
[PEVB_PICO_PS2_PERIOD] = 0x51,
[PEVB_PICO_PS2_SLASH] = 0x52,
[PEVB_PICO_PS2_RO] = 0x53, // pi
[PEVB_PICO_PS2_DOWN] = 0x54, // down
[PEVB_PICO_PS2_LEFT] = 0x55, // left
[PEVB_PICO_PS2_RIGHT] = 0x56, // right

[PEVB_PICO_PS2_CJK] = 0x40, // kana
[PEVB_PICO_PS2_SPACE] = 0x41, // space
[PEVB_PICO_PS2_HOME] = 0x42, // clear/home
[PEVB_PICO_PS2_BACKSPACE] = 0x43, // del/ins

[PEVB_PICO_PS2_SOUND] = 0x96, // graph
[PEVB_PICO_PS2_CTRL] = 0xa6, // ctrl
[PEVB_PICO_PS2_CAPSLOCK] = 0xb5, // func
[PEVB_PICO_PS2_LSHIFT] = 0xb6, // shift
[PEVB_KBD_1] = 0x00,
[PEVB_KBD_2] = 0x01,
[PEVB_KBD_3] = 0x02,
[PEVB_KBD_4] = 0x03,
[PEVB_KBD_5] = 0x04,
[PEVB_KBD_6] = 0x05,
[PEVB_KBD_7] = 0x06,
[PEVB_KBD_8] = 0x80,
[PEVB_KBD_9] = 0x81,
[PEVB_KBD_0] = 0x82,
[PEVB_KBD_MINUS] = 0x83,
[PEVB_KBD_CARET] = 0x84,
[PEVB_KBD_YEN] = 0x85,
[PEVB_KBD_ESCAPE] = 0x86, // break

[PEVB_KBD_q] = 0x10,
[PEVB_KBD_w] = 0x11,
[PEVB_KBD_e] = 0x12,
[PEVB_KBD_r] = 0x13,
[PEVB_KBD_t] = 0x14,
[PEVB_KBD_y] = 0x15,
[PEVB_KBD_u] = 0x16,
[PEVB_KBD_i] = 0x70,
[PEVB_KBD_o] = 0x71,
[PEVB_KBD_p] = 0x72,
[PEVB_KBD_AT] = 0x73,
[PEVB_KBD_LEFTBRACKET] = 0x74,

[PEVB_KBD_a] = 0x20,
[PEVB_KBD_s] = 0x21,
[PEVB_KBD_d] = 0x22,
[PEVB_KBD_f] = 0x23,
[PEVB_KBD_g] = 0x24,
[PEVB_KBD_h] = 0x25,
[PEVB_KBD_j] = 0x26,
[PEVB_KBD_k] = 0x60,
[PEVB_KBD_l] = 0x61,
[PEVB_KBD_SEMICOLON] = 0x62,
[PEVB_KBD_COLON] = 0x63,
[PEVB_KBD_RIGHTBRACKET] = 0x64,
[PEVB_KBD_RETURN] = 0x65,
[PEVB_KBD_UP] = 0x66, // up

[PEVB_KBD_z] = 0x30,
[PEVB_KBD_x] = 0x31,
[PEVB_KBD_c] = 0x32,
[PEVB_KBD_v] = 0x33,
[PEVB_KBD_b] = 0x34,
[PEVB_KBD_n] = 0x35,
[PEVB_KBD_m] = 0x36,
[PEVB_KBD_COMMA] = 0x50,
[PEVB_KBD_PERIOD] = 0x51,
[PEVB_KBD_SLASH] = 0x52,
[PEVB_KBD_RO] = 0x53, // pi
[PEVB_KBD_DOWN] = 0x54, // down
[PEVB_KBD_LEFT] = 0x55, // left
[PEVB_KBD_RIGHT] = 0x56, // right

[PEVB_KBD_CJK] = 0x40, // kana
[PEVB_KBD_SPACE] = 0x41, // space
[PEVB_KBD_HOME] = 0x42, // clear/home
[PEVB_KBD_BACKSPACE] = 0x43, // del/ins

[PEVB_KBD_SOUND] = 0x96, // graph
[PEVB_KBD_CAPSLOCK] = 0xa6, // ctrl
[PEVB_KBD_FUNC] = 0xb5, // func
[PEVB_KBD_SHIFT] = 0xb6, // shift
};

static void kbd_update(void)
{
u32 key = (PicoIn.ps2 & 0x00ff);
u32 sft = (PicoIn.ps2 & 0xff00) >> 8;
u32 key = (PicoIn.kbd & 0x00ff);
u32 sft = (PicoIn.kbd & 0xff00) >> 8;

memset(kbd_matrix, 0, sizeof(kbd_matrix));
if (/*PicoPicohw.kb.active &&*/ sft) {
Expand Down
14 changes: 7 additions & 7 deletions platform/common/config_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static int seek_sect(FILE *f, const char *section)
return 0;
}

static void keys_write(FILE *fn, int dev_id, const int *binds, const int *ps2_binds)
static void keys_write(FILE *fn, int dev_id, const int *binds, const int *kbd_binds)
{
char act[48];
int key_count = 0, k, i;
Expand Down Expand Up @@ -107,8 +107,8 @@ static void keys_write(FILE *fn, int dev_id, const int *binds, const int *ps2_bi
for (k = 0; k < key_count; k++) {
const char *name = in_get_key_name(dev_id, k);
if (strcmp(name, "#") == 0) name = "\\x23"; // replace comment sign
if (ps2_binds[k])
fprintf(fn, "bind %s = key%02x" NL, name, ps2_binds[k]);
if (kbd_binds[k])
fprintf(fn, "bind %s = key%02x" NL, name, kbd_binds[k]);
}
}

Expand Down Expand Up @@ -178,7 +178,7 @@ int config_write(const char *fname)
fprintf(fn, "binddev = %s" NL, name);

in_get_config(t, IN_CFG_BIND_COUNT, &count);
keys_write(fn, t, binds, in_get_dev_ps2_binds(t));
keys_write(fn, t, binds, in_get_dev_kbd_binds(t));
}

fprintf(fn, "Sound Volume = %i" NL, currentConfig.volume);
Expand Down Expand Up @@ -393,7 +393,7 @@ static int parse_bind_val(const char *val, int *type)

if (strncasecmp(val, "key", 3) == 0)
{
*type = IN_BINDTYPE_PICO_PS2;
*type = IN_BINDTYPE_KEYBOARD;
return strtol(val + 3, NULL, 16);
}

Expand Down Expand Up @@ -455,8 +455,8 @@ static void keys_parse_all(FILE *f)
}

mystrip(var + 5);
if (type == IN_BINDTYPE_PICO_PS2)
in_config_bind_ps2_key(dev_id, var + 5, acts);
if (type == IN_BINDTYPE_KEYBOARD)
in_config_bind_kbd_key(dev_id, var + 5, acts);
else
in_config_bind_key(dev_id, var + 5, acts, type);
}
Expand Down
17 changes: 9 additions & 8 deletions platform/common/emu.c
Original file line number Diff line number Diff line change
Expand Up @@ -1295,20 +1295,21 @@ void emu_update_input(void)
{
static int prev_events = 0;
int actions[IN_BINDTYPE_COUNT] = { 0, };
int actions_pico_ps2[IN_BIND_LAST] = { 0, };
int actions_kbd[IN_BIND_LAST] = { 0, };
int pl_actions[4];
int events;

in_update(actions);
in_update_pico_ps2(actions_pico_ps2);
PicoIn.ps2 = 0;
in_update_kbd(actions_kbd);
PicoIn.kbd = 0;
for (int i = 0; i < IN_BIND_LAST; i++) {
if (actions_pico_ps2[i]) {
unsigned int action = actions_pico_ps2[i];
if ((action & 0xff) == PEVB_PICO_PS2_LSHIFT) {
PicoIn.ps2 = (PicoIn.ps2 & 0x00ff) | (action << 8);
if (actions_kbd[i]) {
unsigned int action = actions_kbd[i];
unsigned int key = (action & 0xff);
if (key == PEVB_KBD_SHIFT || key == PEVB_KBD_CTRL || key == PEVB_KBD_FUNC) {
PicoIn.kbd = (PicoIn.kbd & 0x00ff) | (action << 8);
} else {
PicoIn.ps2 = (PicoIn.ps2 & 0xff00) | action;
PicoIn.kbd = (PicoIn.kbd & 0xff00) | action;
}
}
}
Expand Down
Loading

0 comments on commit c1029b9

Please sign in to comment.