From a5deb55969b952e284b724fc7b3a6cd6e878644a Mon Sep 17 00:00:00 2001 From: Jasmine Iwanek Date: Tue, 11 Jan 2022 00:53:17 -0500 Subject: [PATCH] Cleanups for the XT Keyboard code --- src/device/keyboard_xt.c | 159 +++++++++++++++++++++++---------------- 1 file changed, 93 insertions(+), 66 deletions(-) diff --git a/src/device/keyboard_xt.c b/src/device/keyboard_xt.c index 78c17ae19..b7782deae 100644 --- a/src/device/keyboard_xt.c +++ b/src/device/keyboard_xt.c @@ -55,6 +55,17 @@ #define STAT_IFULL 0x02 #define STAT_OFULL 0x01 +// Keyboard Types +#define KBD_TYPE_PC81 0 +#define KBD_TYPE_PC82 1 +#define KBD_TYPE_XT82 2 +#define KBD_TYPE_XT86 3 +#define KBD_TYPE_COMPAQ 4 +#define KBD_TYPE_TANDY 5 +#define KBD_TYPE_TOSHIBA 6 +#define KBD_TYPE_VTECH 7 +#define KBD_TYPE_OLIVETTI 8 +#define KBD_TYPE_ZENITH 9 typedef struct { int want_irq; @@ -357,8 +368,8 @@ kbd_log(const char *fmt, ...) #endif static uint8_t -get_fdd_switch_settings(){ - +get_fdd_switch_settings() { + int i, fdd_count = 0; for (i = 0; i < FDD_NUM; i++) { @@ -369,20 +380,18 @@ get_fdd_switch_settings(){ if (!fdd_count) return 0x00; else - return ((fdd_count - 1) << 6) | 0x01; - + return ((fdd_count - 1) << 6) | 0x01; } static uint8_t -get_videomode_switch_settings(){ - +get_videomode_switch_settings() { + if (video_is_mda()) return 0x30; else if (video_is_cga()) return 0x20; /* 0x10 would be 40x25 */ else - return 0x00; - + return 0x00; } static void @@ -392,7 +401,7 @@ kbd_poll(void *priv) timer_advance_u64(&kbd->send_delay_timer, 1000 * TIMER_USEC); - if (!(kbd->pb & 0x40) && (kbd->type != 5)) + if (!(kbd->pb & 0x40) && (kbd->type != KBD_TYPE_TANDY)) return; if (kbd->want_irq) { @@ -504,7 +513,7 @@ kbd_write(uint16_t port, uint8_t val, void *priv) xtkbd_t *kbd = (xtkbd_t *)priv; switch (port) { - case 0x61: + case 0x61: /* Keyboard Control Register (aka Port B) */ if (!(kbd->pb & 0x40) && (val & 0x40)) { key_queue_start = key_queue_end = 0; kbd->want_irq = 0; @@ -514,9 +523,9 @@ kbd_write(uint16_t port, uint8_t val, void *priv) kbd->pb = val; ppi.pb = val; - timer_process(); + timer_process(); - if ((kbd->type <= 1) && (cassette != NULL)) + if (((kbd->type == KBD_TYPE_PC81) || (kbd->type == KBD_TYPE_PC82)) && (cassette != NULL)) pc_cas_set_motor(cassette, (kbd->pb & 0x08) == 0); speaker_update(); @@ -535,13 +544,13 @@ kbd_write(uint16_t port, uint8_t val, void *priv) } #ifdef ENABLE_KEYBOARD_XT_LOG - if (kbd->type <= 1) + if ((kbd->type == KBD_TYPE_PC81) || (kbd->type == KBD_TYPE_PC82)) kbd_log("Cassette motor is %s\n", !(val & 0x08) ? "ON" : "OFF"); #endif break; #ifdef ENABLE_KEYBOARD_XT_LOG - case 0x62: - if (kbd->type <= 1) + case 0x62: /* Switch Register (aka Port C) */ + if ((kbd->type == KBD_TYPE_PC81) || (kbd->type == KBD_TYPE_PC82)) kbd_log("Cassette IN is %i\n", !!(val & 0x10)); break; #endif @@ -554,16 +563,15 @@ kbd_read(uint16_t port, void *priv) { xtkbd_t *kbd = (xtkbd_t *)priv; uint8_t ret = 0xff; - switch (port) { - case 0x60: - if ((kbd->pb & 0x80) && ((kbd->type <= 3) || (kbd->type == 9))) { - if (kbd->type <= 1) + case 0x60: /* Keyboard Data Register (aka Port A) */ + if ((kbd->pb & 0x80) && ((kbd->type <= KBD_TYPE_XT86) || (kbd->type == KBD_TYPE_ZENITH))) { + if (kbd->type <= KBD_TYPE_PC82) ret = (kbd->pd & ~0x02) | (hasfpu ? 0x02 : 0x00); - else if ((kbd->type == 2) || (kbd->type == 3)) + else if ((kbd->type == KBD_TYPE_XT82) || (kbd->type == KBD_TYPE_XT86)) ret = 0xff; /* According to Ruud on the PCem forum, this is supposed to return 0xFF on the XT. */ - else if (kbd->type == 9) { + else if (kbd->type == KBD_TYPE_ZENITH) { /* Zenith Data Systems Z-151 * SW1 switch settings: * bits 6-7: floppy drive number @@ -586,32 +594,32 @@ kbd_read(uint16_t port, void *priv) ret = kbd->pa; break; - case 0x61: + case 0x61: /* Keyboard Control Register (aka Port B) */ ret = kbd->pb; break; - case 0x62: - if (kbd->type == 0) - ret = 0x00; - else if (kbd->type == 1) { - if (kbd->pb & 0x04) + case 0x62: /* Switch Register (aka Port C) */ + if (kbd->type == KBD_TYPE_PC81) + ret = 0x00; + else if (kbd->type == KBD_TYPE_PC82) { + if (kbd->pb & 0x04) /* PB2 */ ret = ((mem_size - 64) / 32) & 0x0f; else ret = ((mem_size - 64) / 32) >> 4; - } else if (kbd->type == 8 || kbd->type == 9) { + } else if (kbd->type == KBD_TYPE_OLIVETTI || kbd->type == KBD_TYPE_ZENITH) { /* Olivetti M19 or Zenith Data Systems Z-151 */ - if (kbd->pb & 0x04) + if (kbd->pb & 0x04) /* PB2 */ ret = kbd->pd & 0xbf; - else + else ret = kbd->pd >> 4; } else { - if (kbd->pb & 0x08) + if (kbd->pb & 0x08) /* PB3 */ ret = kbd->pd >> 4; else { /* LaserXT = Always 512k RAM; LaserXT/3 = Bit 0: set = 512k, clear = 256k. */ #if defined(DEV_BRANCH) && defined(USE_LASERXT) - if (kbd->type == 6) + if (kbd->type == KBD_TYPE_TOSHIBA) ret = ((mem_size == 512) ? 0x0d : 0x0c) | (hasfpu ? 0x02 : 0x00); else #endif @@ -622,19 +630,20 @@ kbd_read(uint16_t port, void *priv) /* This is needed to avoid error 131 (cassette error). This is serial read: bit 5 = clock, bit 4 = data, cassette header is 256 x 0xff. */ - if (kbd->type <= 1) { + if ((kbd->type == KBD_TYPE_PC81) || (kbd->type == KBD_TYPE_PC82)) { if (cassette == NULL) ret |= (ppispeakon ? 0x10 : 0); else ret |= (pc_cas_get_inp(cassette) ? 0x10 : 0); } - if (kbd->type == 5) + if (kbd->type == KBD_TYPE_TANDY) ret |= (tandy1k_eeprom_read() ? 0x10 : 0); break; - case 0x63: - if ((kbd->type == 2) || (kbd->type == 3) || (kbd->type == 4) || (kbd->type == 6)) + case 0x63: /* Keyboard Configuration Register (aka Port D) */ + if ((kbd->type == KBD_TYPE_XT82) || (kbd->type == KBD_TYPE_XT86) + || (kbd->type == KBD_TYPE_COMPAQ) || (kbd->type == KBD_TYPE_TOSHIBA)) ret = kbd->pd; break; } @@ -685,9 +694,10 @@ kbd_init(const device_t *info) video_reset(gfxcard); - if ((kbd->type <= 3) || (kbd->type == 4) || (kbd->type == 6) || (kbd->type == 8)) { + if ((kbd->type <= KBD_TYPE_XT86) || (kbd->type == KBD_TYPE_COMPAQ) + || (kbd->type == KBD_TYPE_TOSHIBA) || (kbd->type == KBD_TYPE_OLIVETTI)) { /* DIP switch readout: bit set = OFF, clear = ON. */ - if (kbd->type == 8) + if (kbd->type == KBD_TYPE_OLIVETTI) /* Olivetti M19 * Jumpers J1, J2 - monitor type. * 01 - mono (high-res) @@ -699,10 +709,12 @@ kbd_init(const device_t *info) /* Switches 7, 8 - floppy drives. */ kbd->pd = get_fdd_switch_settings(); + /* Siitches 5, 6 - video card type */ kbd->pd |= get_videomode_switch_settings(); /* Switches 3, 4 - memory size. */ - if ((kbd->type == 3) || (kbd->type == 4) || (kbd->type == 6)) { + if ((kbd->type == KBD_TYPE_XT86) || (kbd->type == KBD_TYPE_COMPAQ) + || (kbd->type == KBD_TYPE_TOSHIBA)) { switch (mem_size) { case 256: kbd->pd |= 0x00; @@ -718,34 +730,49 @@ kbd_init(const device_t *info) kbd->pd |= 0x0c; break; } - } else if (kbd->type >= 1) { + } else if (kbd->type == KBD_TYPE_XT82) { switch (mem_size) { - case 64: + case 64: /* 1x64k */ kbd->pd |= 0x00; break; - case 128: + case 128: /* 2x64k */ kbd->pd |= 0x04; break; - case 192: + case 192: /* 3x64k */ kbd->pd |= 0x08; break; - case 256: + case 256: /* 4x64k */ default: kbd->pd |= 0x0c; break; } - } else { + } else if (kbd->type == KBD_TYPE_PC82) { switch (mem_size) { - case 16: - kbd->pd |= 0x00; - break; - case 32: - kbd->pd |= 0x04; - break; - case 48: + case 192: /* 3x64k, not supported by stock BIOS due to bugs */ kbd->pd |= 0x08; break; - case 64: + case 64: /* 4x16k */ + case 96: /* 2x32k + 2x16k */ + case 128: /* 4x32k */ + case 160: /* 2x64k + 2x16k */ + case 224: /* 3x64k + 1x32k */ + case 256: /* 4x64k */ + default: + kbd->pd |= 0x0c; + break; + } + } else { /* really just the PC '81 */ + switch (mem_size) { + case 16: /* 1x16k */ + kbd->pd |= 0x00; + break; + case 32: /* 2x16k */ + kbd->pd |= 0x04; + break; + case 48: /* 3x16k */ + kbd->pd |= 0x08; + break; + case 64: /* 4x16k */ default: kbd->pd |= 0x0c; break; @@ -755,7 +782,7 @@ kbd_init(const device_t *info) /* Switch 2 - 8087 FPU. */ if (hasfpu) kbd->pd |= 0x02; - } else if (kbd-> type == 9) { + } else if (kbd-> type == KBD_TYPE_ZENITH) { /* Zenith Data Systems Z-151 * SW2 switch settings: * bit 7: monitor frequency @@ -799,8 +826,8 @@ kbd_init(const device_t *info) keyboard_set_table(scancode_xt); - is_tandy = (kbd->type == 5); - is_t1x00 = (kbd->type == 6); + is_tandy = (kbd->type == KBD_TYPE_TANDY); + is_t1x00 = (kbd->type == KBD_TYPE_TOSHIBA); is_amstrad = 0; @@ -831,7 +858,7 @@ kbd_close(void *priv) const device_t keyboard_pc_device = { "IBM PC Keyboard (1981)", 0, - 0, + KBD_TYPE_PC81, kbd_init, kbd_close, kbd_reset, @@ -841,7 +868,7 @@ const device_t keyboard_pc_device = { const device_t keyboard_pc82_device = { "IBM PC Keyboard (1982)", 0, - 1, + KBD_TYPE_PC82, kbd_init, kbd_close, kbd_reset, @@ -851,7 +878,7 @@ const device_t keyboard_pc82_device = { const device_t keyboard_xt_device = { "XT (1982) Keyboard", 0, - 2, + KBD_TYPE_XT82, kbd_init, kbd_close, kbd_reset, @@ -861,7 +888,7 @@ const device_t keyboard_xt_device = { const device_t keyboard_xt86_device = { "XT (1986) Keyboard", 0, - 3, + KBD_TYPE_XT86, kbd_init, kbd_close, kbd_reset, @@ -871,7 +898,7 @@ const device_t keyboard_xt86_device = { const device_t keyboard_xt_compaq_device = { "Compaq Portable Keyboard", 0, - 4, + KBD_TYPE_COMPAQ, kbd_init, kbd_close, kbd_reset, @@ -881,7 +908,7 @@ const device_t keyboard_xt_compaq_device = { const device_t keyboard_tandy_device = { "Tandy 1000 Keyboard", 0, - 5, + KBD_TYPE_TANDY, kbd_init, kbd_close, kbd_reset, @@ -891,7 +918,7 @@ const device_t keyboard_tandy_device = { const device_t keyboard_xt_t1x00_device = { "Toshiba T1x00 Keyboard", 0, - 6, + KBD_TYPE_TOSHIBA, kbd_init, kbd_close, kbd_reset, @@ -902,7 +929,7 @@ const device_t keyboard_xt_t1x00_device = { const device_t keyboard_xt_lxt3_device = { "VTech Laser XT3 Keyboard", 0, - 7, + KBD_TYPE_VTECH, kbd_init, kbd_close, kbd_reset, @@ -913,7 +940,7 @@ const device_t keyboard_xt_lxt3_device = { const device_t keyboard_xt_olivetti_device = { "Olivetti XT Keyboard", 0, - 8, + KBD_TYPE_OLIVETTI, kbd_init, kbd_close, kbd_reset, @@ -923,7 +950,7 @@ const device_t keyboard_xt_olivetti_device = { const device_t keyboard_xt_zenith_device = { "Zenith XT Keyboard", 0, - 9, + KBD_TYPE_ZENITH, kbd_init, kbd_close, kbd_reset,