From patchwork Tue Dec 22 17:40:25 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?5q2m55SwID0/SVNPLTIwMjItSlA/Qj9JQnNrUWoxVFRHa2JLRUk9Pz0=?= X-Patchwork-Id: 41624 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 3872EB7B6A for ; Wed, 23 Dec 2009 04:57:09 +1100 (EST) Received: from localhost ([127.0.0.1]:48125 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NN8yn-0004ul-C6 for incoming@patchwork.ozlabs.org; Tue, 22 Dec 2009 12:57:05 -0500 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NN8kv-0007nf-MU for qemu-devel@nongnu.org; Tue, 22 Dec 2009 12:42:46 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1NN8ko-0007jn-Oj for qemu-devel@nongnu.org; Tue, 22 Dec 2009 12:42:43 -0500 Received: from [199.232.76.173] (port=56252 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NN8kn-0007jc-PG for qemu-devel@nongnu.org; Tue, 22 Dec 2009 12:42:37 -0500 Received: from smtp-vip.mem.interq.net ([210.157.1.50]:22943 helo=smtp01.mem.internal-gmo) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1NN8km-0001za-PE for qemu-devel@nongnu.org; Tue, 22 Dec 2009 12:42:37 -0500 Received: (from root@localhost) by smtp01.mem.internal-gmo (8.13.8/8.12.6) id nBMHgHaA021776 for qemu-devel@nongnu.org; Wed, 23 Dec 2009 02:42:17 +0900 (JST) Received: from YOUR-BD18D6DD63.m1.interq.or.jp (ntymns039132.ymns.nt.ftth.ppp.infoweb.ne.jp [121.92.167.132]) by smtp01.mem.internal-gmo with ESMTP id nBMHgGgF021741 for ; (me101664 for with PLAIN) Wed, 23 Dec 2009 02:42:17 +0900 (JST) Message-Id: <200912221740.AA00208@YOUR-BD18D6DD63.m1.interq.or.jp> From: "TAKEDA, toshiya" Date: Wed, 23 Dec 2009 02:40:25 +0900 To: qemu-devel MIME-Version: 1.0 X-Mailer: AL-Mail32 Version 1.13 X-detected-operating-system: by monty-python.gnu.org: Solaris 10 (beta) Subject: [Qemu-devel] [PATCH V4 05/18] support NEC PC-9821 keyboard X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Signed-off-by: TAKEDA, toshiya --- hw/pc98kbd.c | 395 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 395 insertions(+), 0 deletions(-) create mode 100644 hw/pc98kbd.c diff --git a/hw/pc98kbd.c b/hw/pc98kbd.c new file mode 100644 index 0000000..04e1fa5 --- /dev/null +++ b/hw/pc98kbd.c @@ -0,0 +1,395 @@ +/* + * QEMU NEC PC-9821 keyboard + * + * Copyright (c) 2009 TAKEDA, toshiya + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#include "hw.h" +#include "pc.h" +#include "isa.h" +#include "qdev.h" +#include "console.h" +#include "qemu-timer.h" + +#define SIO_BUFFER_SIZE 256 +#define SIO_RECV_DELAY 8 + +enum { + SIO_STAT_TXRDY = 0x01, + SIO_STAT_RXRDY = 0x02, + SIO_STAT_TXE = 0x04, + SIO_STAT_PE = 0x08, + SIO_STAT_OE = 0x10, + SIO_STAT_FE = 0x20, + SIO_STAT_SYNDET = 0x40, + SIO_STAT_DSR = 0x80, +}; + +enum { + SIO_MODE_CLEAR, + SIO_MODE_SYNC, + SIO_MODE_ASYNC, + SIO_MODE_SYNC1, + SIO_MODE_SYNC2, +}; + +enum { + KBD_LOCK_NUMLK = 1, + KBD_LOCK_CAPS = 4, + KBD_LOCK_KANA = 8, + KBD_LOCK_MASK = (1 | 4 | 8), +}; + +/* + END -> HELP + F11 -> STOP + F12 -> COPY +*/ + +static const uint8_t kbd_table[128] = { + 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, + 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x74, 0x1d, 0x1e, + 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, + 0x27, 0xff, 0x70, 0x28, 0x29, 0x2a, 0x2b, 0x2c, + 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x70, 0xff, + 0x70, 0x34, 0xff, 0x62, 0x63, 0x64, 0x65, 0x66, + 0x67, 0x68, 0x69, 0x6a, 0x6b, 0xff, 0xff, 0x3e, + 0x3a, 0x36, 0xff, 0x3b, 0xff, 0x3c, 0xff, 0x3f, + 0x3d, 0x37, 0x38, 0x39, 0xff, 0xff, 0xff, 0x60, + 0x61, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0x33, 0xff, 0xff, 0xff, 0xff, + 0xff, 0x35, 0xff, 0x51, 0xff, 0x0d, 0xff, 0xff, +}; + +struct KeyBoardState { + /* keyboard */ + uint8_t lock; + uint8_t pressed[128]; + + /* sio */ + uint8_t mode; + uint8_t status; + uint8_t rxen; + uint8_t txen; + uint8_t recv_data; + uint8_t recv_buf[SIO_BUFFER_SIZE]; + int recv_count; + int recv_read; + int recv_write; + uint8_t send_buf[2]; + int send_count; + + QEMUTimer *sio_timer; + qemu_irq irq; +}; + +typedef struct ISAKeyBoardState { + ISADevice dev; + uint32_t isairq; + struct KeyBoardState state; +} ISAKeyBoardState; + +typedef struct KeyBoardState KeyBoardState; + +/* keyboard */ + +static void kbd_recv(void *opaque, uint8_t value) +{ + KeyBoardState *s = opaque; + + if (s->recv_count < SIO_BUFFER_SIZE) { + s->recv_buf[(s->recv_write++) & (SIO_BUFFER_SIZE - 1)] = value; + s->recv_count++; + if (s->recv_count == 1) { + qemu_mod_timer(s->sio_timer, qemu_get_clock(rt_clock) + + SIO_RECV_DELAY); + } + } +} + +static void kbd_send(void *opaque, uint8_t value) +{ + KeyBoardState *s = opaque; + + s->send_buf[(s->send_count++) & 1] = value; + + switch (s->send_buf[0]) { + case 0x95: + case 0x9c: + kbd_recv(s, 0xfa); + if (s->send_count == 2) { + s->send_count = 0; + } + break; + case 0x96: + kbd_recv(s, 0xfa); + kbd_recv(s, 0xa0); + kbd_recv(s, 0x85);/*0x86*/ + s->send_count = 0; + break; + case 0x99: + kbd_recv(s, 0xfa); + kbd_recv(s, 0xfb); + s->send_count = 0; + break; + case 0x9d: + kbd_recv(s, 0xfa); + if (s->send_count == 2) { + if (s->send_buf[1] == 0x60) { + kbd_recv(s, 0x70 | (s->lock & KBD_LOCK_MASK)); + } else { + s->lock = value; + } + s->send_count = 0; + } + break; + case 0x9f: + kbd_recv(s, 0xfa); + kbd_recv(s, 0xa0); + kbd_recv(s, 0x80); + s->send_count = 0; + break; + default: + kbd_recv(s, 0xfc); + s->send_count = 0; + break; + } +} + +static void kbd_event_handler(void *opaque, int keycode) +{ + KeyBoardState *s = opaque; + + if (keycode & 0x80) { + /* key released */ + if ((keycode = kbd_table[keycode & 0x7f]) != 0xff) { + if(s->pressed[keycode]) { + kbd_recv(s, keycode | 0x80); + } + s->pressed[keycode] = 0; + } + } else { + /* key pressed */ + if (keycode == 0x3a) { + s->lock ^= KBD_LOCK_CAPS; + if (s->lock & KBD_LOCK_CAPS) { + kbd_recv(s, 0x71); + } else { + kbd_recv(s, 0x71 | 0x80); + } + } else if (keycode == 0x70) { + s->lock ^= KBD_LOCK_KANA; + if (s->lock & KBD_LOCK_KANA) { + kbd_recv(s, 0x72); + } else { + kbd_recv(s, 0x72 | 0x80); + } + } else if ((keycode = kbd_table[keycode]) != 0xff) { + if(s->pressed[keycode]) { + kbd_recv(s, keycode | 0x80); + } + kbd_recv(s, keycode); + s->pressed[keycode] = 1; + } + } +} + +/* sio */ + +static void sio_data_write(void *opaque, uint32_t addr, uint32_t value) +{ + KeyBoardState *s = opaque; + + if (s->status & SIO_STAT_TXRDY) { + s->status &= ~SIO_STAT_TXE; + kbd_send(s, value); + } +} + +static uint32_t sio_data_read(void *opaque, uint32_t addr) +{ + KeyBoardState *s = opaque; + + s->status &= ~SIO_STAT_RXRDY; + qemu_set_irq(s->irq, 0); + return s->recv_data; +} + +static void sio_cmd_write(void *opaque, uint32_t addr, uint32_t value) +{ + KeyBoardState *s = opaque; + + switch(s->mode) { + case SIO_MODE_CLEAR: + if(value & 3) { + s->mode = SIO_MODE_ASYNC; + } else if(value & 0x80) { + s->mode = SIO_MODE_SYNC2; /* 1char */ + } else { + s->mode = SIO_MODE_SYNC1; /* 2chars */ + } + break; + case SIO_MODE_SYNC1: + s->mode = SIO_MODE_SYNC2; + break; + case SIO_MODE_SYNC2: + s->mode = SIO_MODE_SYNC; + break; + case SIO_MODE_ASYNC: + case SIO_MODE_SYNC: + if(value & 0x40) { + s->mode = SIO_MODE_CLEAR; + break; + } + if(value & 0x10) { + s->status &= ~(SIO_STAT_PE | SIO_STAT_OE | SIO_STAT_FE); + } + s->rxen = value & 4; + s->txen = value & 1; + break; + } +} + +static uint32_t sio_status_read(void *opaque, uint32_t addr) +{ + KeyBoardState *s = opaque; + uint8_t value; + + value = s->status; + if (s->txen) { + s->status |= SIO_STAT_TXE; + } + return value; +} + +static void sio_timer_handler(void *opaque) +{ + KeyBoardState *s = opaque; + + if (s->recv_count > 0) { + uint8_t value = s->recv_buf[(s->recv_read++) & 0xff]; + s->recv_count--; + if (s->rxen) { + if (!(s->status & SIO_STAT_RXRDY)) { + s->status |= SIO_STAT_RXRDY; + s->recv_data = value; + qemu_set_irq(s->irq, 1); + } else { + /* time out */ + s->status |= SIO_STAT_OE; + } + } + if (s->recv_count > 0) { + qemu_mod_timer(s->sio_timer, qemu_get_clock(rt_clock) + + SIO_RECV_DELAY); + } + } +} + +/* interface */ + +static void pc98_kbd_reset(void *opaque) +{ + KeyBoardState *s = opaque; + + s->lock = 0; + memset(s->pressed, 0, 128); + + s->mode = SIO_MODE_CLEAR; + s->status = (SIO_STAT_TXRDY | SIO_STAT_TXE); + s->txen = s->rxen = 0; + s->recv_data = 0xff; + s->recv_count = s->recv_read = s->recv_write = 0; + s->send_count = 0; +} + +static int pc98_kbd_pre_load(void *opaque) +{ + pc98_kbd_reset(opaque); + return 0; +} + +static const VMStateDescription vmstate_kbd = { + .name = "pc98-kbd", + .version_id = 1, + .minimum_version_id = 1, + .minimum_version_id_old = 1, + .pre_load = pc98_kbd_pre_load, + .fields = (VMStateField []) { + VMSTATE_UINT8(lock, KeyBoardState), + VMSTATE_UINT8(mode, KeyBoardState), + VMSTATE_UINT8(status, KeyBoardState), + VMSTATE_UINT8(rxen, KeyBoardState), + VMSTATE_UINT8(txen, KeyBoardState), + VMSTATE_UINT8(recv_data, KeyBoardState), + VMSTATE_END_OF_LIST() + } +}; + +static int pc98_kbd_initfn(ISADevice *dev) +{ + ISAKeyBoardState *isa = DO_UPCAST(ISAKeyBoardState, dev, dev); + KeyBoardState *s = &isa->state; + + register_ioport_write(0x41, 1, 1, sio_data_write, s); + register_ioport_read(0x41, 1, 1, sio_data_read, s); + register_ioport_write(0x43, 1, 1, sio_cmd_write, s); + register_ioport_read(0x43, 1, 1, sio_status_read, s); + + isa_init_irq(dev, &s->irq, isa->isairq); + + s->sio_timer = qemu_new_timer(rt_clock, sio_timer_handler, s); + qemu_add_kbd_event_handler(kbd_event_handler, s); + + vmstate_register(-1, &vmstate_kbd, s); + pc98_kbd_reset(s); + qemu_register_reset(pc98_kbd_reset, s); + + return 0; +} + +void pc98_kbd_init(void) +{ + ISADevice *dev; + + dev = isa_create("pc98-kbd"); + qdev_init_nofail(&dev->qdev); +} + +static ISADeviceInfo pc98_kbd_info = { + .qdev.name = "pc98-kbd", + .qdev.size = sizeof(ISAKeyBoardState), + .init = pc98_kbd_initfn, + .qdev.props = (Property[]) { + DEFINE_PROP_UINT32("irq", ISAKeyBoardState, isairq, 1), + DEFINE_PROP_END_OF_LIST(), + }, +}; + +static void pc98_kbd_register_devices(void) +{ + isa_qdev_register(&pc98_kbd_info); +} + +device_init(pc98_kbd_register_devices)