next-kbd.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * QEMU NeXT Keyboard/Mouse emulation
  3. *
  4. * Copyright (c) 2011 Bryce Lanham
  5. *
  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:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  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.
  23. */
  24. /*
  25. * This is admittedly hackish, but works well enough for basic input. Mouse
  26. * support will be added once we can boot something that needs the mouse.
  27. */
  28. #include "qemu/osdep.h"
  29. #include "qemu/log.h"
  30. #include "exec/address-spaces.h"
  31. #include "hw/hw.h"
  32. #include "hw/sysbus.h"
  33. #include "hw/m68k/next-cube.h"
  34. #include "ui/console.h"
  35. #include "sysemu/sysemu.h"
  36. #include "migration/vmstate.h"
  37. #define NEXTKBD(obj) OBJECT_CHECK(NextKBDState, (obj), TYPE_NEXTKBD)
  38. /* following defintions from next68k netbsd */
  39. #define CSR_INT 0x00800000
  40. #define CSR_DATA 0x00400000
  41. #define KD_KEYMASK 0x007f
  42. #define KD_DIRECTION 0x0080 /* pressed or released */
  43. #define KD_CNTL 0x0100
  44. #define KD_LSHIFT 0x0200
  45. #define KD_RSHIFT 0x0400
  46. #define KD_LCOMM 0x0800
  47. #define KD_RCOMM 0x1000
  48. #define KD_LALT 0x2000
  49. #define KD_RALT 0x4000
  50. #define KD_VALID 0x8000 /* only set for scancode keys ? */
  51. #define KD_MODS 0x4f00
  52. #define KBD_QUEUE_SIZE 256
  53. typedef struct {
  54. uint8_t data[KBD_QUEUE_SIZE];
  55. int rptr, wptr, count;
  56. } KBDQueue;
  57. typedef struct NextKBDState {
  58. SysBusDevice sbd;
  59. MemoryRegion mr;
  60. KBDQueue queue;
  61. uint16_t shift;
  62. } NextKBDState;
  63. static void queue_code(void *opaque, int code);
  64. /* lots of magic numbers here */
  65. static uint32_t kbd_read_byte(void *opaque, hwaddr addr)
  66. {
  67. switch (addr & 0x3) {
  68. case 0x0: /* 0xe000 */
  69. return 0x80 | 0x20;
  70. case 0x1: /* 0xe001 */
  71. return 0x80 | 0x40 | 0x20 | 0x10;
  72. case 0x2: /* 0xe002 */
  73. /* returning 0x40 caused mach to hang */
  74. return 0x10 | 0x2 | 0x1;
  75. default:
  76. qemu_log_mask(LOG_UNIMP, "NeXT kbd read byte %"HWADDR_PRIx"\n", addr);
  77. }
  78. return 0;
  79. }
  80. static uint32_t kbd_read_word(void *opaque, hwaddr addr)
  81. {
  82. qemu_log_mask(LOG_UNIMP, "NeXT kbd read word %"HWADDR_PRIx"\n", addr);
  83. return 0;
  84. }
  85. /* even more magic numbers */
  86. static uint32_t kbd_read_long(void *opaque, hwaddr addr)
  87. {
  88. int key = 0;
  89. NextKBDState *s = NEXTKBD(opaque);
  90. KBDQueue *q = &s->queue;
  91. switch (addr & 0xf) {
  92. case 0x0: /* 0xe000 */
  93. return 0xA0F09300;
  94. case 0x8: /* 0xe008 */
  95. /* get keycode from buffer */
  96. if (q->count > 0) {
  97. key = q->data[q->rptr];
  98. if (++q->rptr == KBD_QUEUE_SIZE) {
  99. q->rptr = 0;
  100. }
  101. q->count--;
  102. if (s->shift) {
  103. key |= s->shift;
  104. }
  105. if (key & 0x80) {
  106. return 0;
  107. } else {
  108. return 0x10000000 | KD_VALID | key;
  109. }
  110. } else {
  111. return 0;
  112. }
  113. default:
  114. qemu_log_mask(LOG_UNIMP, "NeXT kbd read long %"HWADDR_PRIx"\n", addr);
  115. return 0;
  116. }
  117. }
  118. static uint64_t kbd_readfn(void *opaque, hwaddr addr, unsigned size)
  119. {
  120. switch (size) {
  121. case 1:
  122. return kbd_read_byte(opaque, addr);
  123. case 2:
  124. return kbd_read_word(opaque, addr);
  125. case 4:
  126. return kbd_read_long(opaque, addr);
  127. default:
  128. g_assert_not_reached();
  129. }
  130. }
  131. static void kbd_writefn(void *opaque, hwaddr addr, uint64_t value,
  132. unsigned size)
  133. {
  134. qemu_log_mask(LOG_UNIMP, "NeXT kbd write: size=%u addr=0x%"HWADDR_PRIx
  135. "val=0x%"PRIx64"\n", size, addr, value);
  136. }
  137. static const MemoryRegionOps kbd_ops = {
  138. .read = kbd_readfn,
  139. .write = kbd_writefn,
  140. .valid.min_access_size = 1,
  141. .valid.max_access_size = 4,
  142. .endianness = DEVICE_NATIVE_ENDIAN,
  143. };
  144. static void nextkbd_event(void *opaque, int ch)
  145. {
  146. /*
  147. * Will want to set vars for caps/num lock
  148. * if (ch & 0x80) -> key release
  149. * there's also e0 escaped scancodes that might need to be handled
  150. */
  151. queue_code(opaque, ch);
  152. }
  153. static const unsigned char next_keycodes[128] = {
  154. 0x00, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x50, 0x4F,
  155. 0x4E, 0x1E, 0x1F, 0x20, 0x1D, 0x1C, 0x1B, 0x00,
  156. 0x42, 0x43, 0x44, 0x45, 0x48, 0x47, 0x46, 0x06,
  157. 0x07, 0x08, 0x00, 0x00, 0x2A, 0x00, 0x39, 0x3A,
  158. 0x3B, 0x3C, 0x3D, 0x40, 0x3F, 0x3E, 0x2D, 0x2C,
  159. 0x2B, 0x26, 0x00, 0x00, 0x31, 0x32, 0x33, 0x34,
  160. 0x35, 0x37, 0x36, 0x2e, 0x2f, 0x30, 0x00, 0x00,
  161. 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  162. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  163. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  164. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  165. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  166. };
  167. static void queue_code(void *opaque, int code)
  168. {
  169. NextKBDState *s = NEXTKBD(opaque);
  170. KBDQueue *q = &s->queue;
  171. int key = code & KD_KEYMASK;
  172. int release = code & 0x80;
  173. static int ext;
  174. if (code == 0xE0) {
  175. ext = 1;
  176. }
  177. if (code == 0x2A || code == 0x1D || code == 0x36) {
  178. if (code == 0x2A) {
  179. s->shift = KD_LSHIFT;
  180. } else if (code == 0x36) {
  181. s->shift = KD_RSHIFT;
  182. ext = 0;
  183. } else if (code == 0x1D && !ext) {
  184. s->shift = KD_LCOMM;
  185. } else if (code == 0x1D && ext) {
  186. ext = 0;
  187. s->shift = KD_RCOMM;
  188. }
  189. return;
  190. } else if (code == (0x2A | 0x80) || code == (0x1D | 0x80) ||
  191. code == (0x36 | 0x80)) {
  192. s->shift = 0;
  193. return;
  194. }
  195. if (q->count >= KBD_QUEUE_SIZE) {
  196. return;
  197. }
  198. q->data[q->wptr] = next_keycodes[key] | release;
  199. if (++q->wptr == KBD_QUEUE_SIZE) {
  200. q->wptr = 0;
  201. }
  202. q->count++;
  203. /*
  204. * might need to actually trigger the NeXT irq, but as the keyboard works
  205. * at the moment, I'll worry about it later
  206. */
  207. /* s->update_irq(s->update_arg, 1); */
  208. }
  209. static void nextkbd_reset(DeviceState *dev)
  210. {
  211. NextKBDState *nks = NEXTKBD(dev);
  212. memset(&nks->queue, 0, sizeof(KBDQueue));
  213. nks->shift = 0;
  214. }
  215. static void nextkbd_realize(DeviceState *dev, Error **errp)
  216. {
  217. NextKBDState *s = NEXTKBD(dev);
  218. memory_region_init_io(&s->mr, OBJECT(dev), &kbd_ops, s, "next.kbd", 0x1000);
  219. sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->mr);
  220. qemu_add_kbd_event_handler(nextkbd_event, s);
  221. }
  222. static const VMStateDescription nextkbd_vmstate = {
  223. .name = TYPE_NEXTKBD,
  224. .unmigratable = 1, /* TODO: Implement this when m68k CPU is migratable */
  225. };
  226. static void nextkbd_class_init(ObjectClass *oc, void *data)
  227. {
  228. DeviceClass *dc = DEVICE_CLASS(oc);
  229. set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
  230. dc->vmsd = &nextkbd_vmstate;
  231. dc->realize = nextkbd_realize;
  232. dc->reset = nextkbd_reset;
  233. }
  234. static const TypeInfo nextkbd_info = {
  235. .name = TYPE_NEXTKBD,
  236. .parent = TYPE_SYS_BUS_DEVICE,
  237. .instance_size = sizeof(NextKBDState),
  238. .class_init = nextkbd_class_init,
  239. };
  240. static void nextkbd_register_types(void)
  241. {
  242. type_register_static(&nextkbd_info);
  243. }
  244. type_init(nextkbd_register_types)