exec/memory: Use struct Object typedef
[qemu/ar7.git] / hw / m68k / next-kbd.c
blobc11b5281f19967215c6754e3035356fd399e80d2
1 /*
2 * QEMU NeXT Keyboard/Mouse emulation
4 * Copyright (c) 2011 Bryce Lanham
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
26 * This is admittedly hackish, but works well enough for basic input. Mouse
27 * support will be added once we can boot something that needs the mouse.
30 #include "qemu/osdep.h"
31 #include "qemu/log.h"
32 #include "exec/address-spaces.h"
33 #include "hw/hw.h"
34 #include "hw/sysbus.h"
35 #include "hw/m68k/next-cube.h"
36 #include "ui/console.h"
37 #include "sysemu/sysemu.h"
38 #include "migration/vmstate.h"
39 #include "qom/object.h"
41 OBJECT_DECLARE_SIMPLE_TYPE(NextKBDState, NEXTKBD)
43 /* following defintions from next68k netbsd */
44 #define CSR_INT 0x00800000
45 #define CSR_DATA 0x00400000
47 #define KD_KEYMASK 0x007f
48 #define KD_DIRECTION 0x0080 /* pressed or released */
49 #define KD_CNTL 0x0100
50 #define KD_LSHIFT 0x0200
51 #define KD_RSHIFT 0x0400
52 #define KD_LCOMM 0x0800
53 #define KD_RCOMM 0x1000
54 #define KD_LALT 0x2000
55 #define KD_RALT 0x4000
56 #define KD_VALID 0x8000 /* only set for scancode keys ? */
57 #define KD_MODS 0x4f00
59 #define KBD_QUEUE_SIZE 256
61 typedef struct {
62 uint8_t data[KBD_QUEUE_SIZE];
63 int rptr, wptr, count;
64 } KBDQueue;
67 struct NextKBDState {
68 SysBusDevice sbd;
69 MemoryRegion mr;
70 KBDQueue queue;
71 uint16_t shift;
74 static void queue_code(void *opaque, int code);
76 /* lots of magic numbers here */
77 static uint32_t kbd_read_byte(void *opaque, hwaddr addr)
79 switch (addr & 0x3) {
80 case 0x0: /* 0xe000 */
81 return 0x80 | 0x20;
83 case 0x1: /* 0xe001 */
84 return 0x80 | 0x40 | 0x20 | 0x10;
86 case 0x2: /* 0xe002 */
87 /* returning 0x40 caused mach to hang */
88 return 0x10 | 0x2 | 0x1;
90 default:
91 qemu_log_mask(LOG_UNIMP, "NeXT kbd read byte %"HWADDR_PRIx"\n", addr);
94 return 0;
97 static uint32_t kbd_read_word(void *opaque, hwaddr addr)
99 qemu_log_mask(LOG_UNIMP, "NeXT kbd read word %"HWADDR_PRIx"\n", addr);
100 return 0;
103 /* even more magic numbers */
104 static uint32_t kbd_read_long(void *opaque, hwaddr addr)
106 int key = 0;
107 NextKBDState *s = NEXTKBD(opaque);
108 KBDQueue *q = &s->queue;
110 switch (addr & 0xf) {
111 case 0x0: /* 0xe000 */
112 return 0xA0F09300;
114 case 0x8: /* 0xe008 */
115 /* get keycode from buffer */
116 if (q->count > 0) {
117 key = q->data[q->rptr];
118 if (++q->rptr == KBD_QUEUE_SIZE) {
119 q->rptr = 0;
122 q->count--;
124 if (s->shift) {
125 key |= s->shift;
128 if (key & 0x80) {
129 return 0;
130 } else {
131 return 0x10000000 | KD_VALID | key;
133 } else {
134 return 0;
137 default:
138 qemu_log_mask(LOG_UNIMP, "NeXT kbd read long %"HWADDR_PRIx"\n", addr);
139 return 0;
143 static uint64_t kbd_readfn(void *opaque, hwaddr addr, unsigned size)
145 switch (size) {
146 case 1:
147 return kbd_read_byte(opaque, addr);
148 case 2:
149 return kbd_read_word(opaque, addr);
150 case 4:
151 return kbd_read_long(opaque, addr);
152 default:
153 g_assert_not_reached();
157 static void kbd_writefn(void *opaque, hwaddr addr, uint64_t value,
158 unsigned size)
160 qemu_log_mask(LOG_UNIMP, "NeXT kbd write: size=%u addr=0x%"HWADDR_PRIx
161 "val=0x%"PRIx64"\n", size, addr, value);
164 static const MemoryRegionOps kbd_ops = {
165 .read = kbd_readfn,
166 .write = kbd_writefn,
167 .valid.min_access_size = 1,
168 .valid.max_access_size = 4,
169 .endianness = DEVICE_NATIVE_ENDIAN,
172 static void nextkbd_event(void *opaque, int ch)
175 * Will want to set vars for caps/num lock
176 * if (ch & 0x80) -> key release
177 * there's also e0 escaped scancodes that might need to be handled
179 queue_code(opaque, ch);
182 static const unsigned char next_keycodes[128] = {
183 0x00, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x50, 0x4F,
184 0x4E, 0x1E, 0x1F, 0x20, 0x1D, 0x1C, 0x1B, 0x00,
185 0x42, 0x43, 0x44, 0x45, 0x48, 0x47, 0x46, 0x06,
186 0x07, 0x08, 0x00, 0x00, 0x2A, 0x00, 0x39, 0x3A,
187 0x3B, 0x3C, 0x3D, 0x40, 0x3F, 0x3E, 0x2D, 0x2C,
188 0x2B, 0x26, 0x00, 0x00, 0x31, 0x32, 0x33, 0x34,
189 0x35, 0x37, 0x36, 0x2e, 0x2f, 0x30, 0x00, 0x00,
190 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
191 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
192 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
193 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
194 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
197 static void queue_code(void *opaque, int code)
199 NextKBDState *s = NEXTKBD(opaque);
200 KBDQueue *q = &s->queue;
201 int key = code & KD_KEYMASK;
202 int release = code & 0x80;
203 static int ext;
205 if (code == 0xE0) {
206 ext = 1;
209 if (code == 0x2A || code == 0x1D || code == 0x36) {
210 if (code == 0x2A) {
211 s->shift = KD_LSHIFT;
212 } else if (code == 0x36) {
213 s->shift = KD_RSHIFT;
214 ext = 0;
215 } else if (code == 0x1D && !ext) {
216 s->shift = KD_LCOMM;
217 } else if (code == 0x1D && ext) {
218 ext = 0;
219 s->shift = KD_RCOMM;
221 return;
222 } else if (code == (0x2A | 0x80) || code == (0x1D | 0x80) ||
223 code == (0x36 | 0x80)) {
224 s->shift = 0;
225 return;
228 if (q->count >= KBD_QUEUE_SIZE) {
229 return;
232 q->data[q->wptr] = next_keycodes[key] | release;
234 if (++q->wptr == KBD_QUEUE_SIZE) {
235 q->wptr = 0;
238 q->count++;
241 * might need to actually trigger the NeXT irq, but as the keyboard works
242 * at the moment, I'll worry about it later
244 /* s->update_irq(s->update_arg, 1); */
247 static void nextkbd_reset(DeviceState *dev)
249 NextKBDState *nks = NEXTKBD(dev);
251 memset(&nks->queue, 0, sizeof(KBDQueue));
252 nks->shift = 0;
255 static void nextkbd_realize(DeviceState *dev, Error **errp)
257 NextKBDState *s = NEXTKBD(dev);
259 memory_region_init_io(&s->mr, OBJECT(dev), &kbd_ops, s, "next.kbd", 0x1000);
260 sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->mr);
262 qemu_add_kbd_event_handler(nextkbd_event, s);
265 static const VMStateDescription nextkbd_vmstate = {
266 .name = TYPE_NEXTKBD,
267 .unmigratable = 1, /* TODO: Implement this when m68k CPU is migratable */
270 static void nextkbd_class_init(ObjectClass *oc, void *data)
272 DeviceClass *dc = DEVICE_CLASS(oc);
274 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
275 dc->vmsd = &nextkbd_vmstate;
276 dc->realize = nextkbd_realize;
277 dc->reset = nextkbd_reset;
280 static const TypeInfo nextkbd_info = {
281 .name = TYPE_NEXTKBD,
282 .parent = TYPE_SYS_BUS_DEVICE,
283 .instance_size = sizeof(NextKBDState),
284 .class_init = nextkbd_class_init,
287 static void nextkbd_register_types(void)
289 type_register_static(&nextkbd_info);
292 type_init(nextkbd_register_types)