[a06aa8b] | 1 | /*
|
---|
| 2 | * Copyright (C) 2017 The Contributors
|
---|
| 3 | *
|
---|
| 4 | * This program is free software: you can redistribute it and/or modify
|
---|
| 5 | * it under the terms of the GNU General Public License as published by
|
---|
| 6 | * the Free Software Foundation, either version 3 of the License, or (at
|
---|
| 7 | * your option) any later version.
|
---|
| 8 | *
|
---|
| 9 | * This program is distributed in the hope that it will be useful, but
|
---|
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 12 | * General Public License for more details.
|
---|
| 13 | *
|
---|
| 14 | * A copy of the GNU General Public License can be found in the file
|
---|
[2147e53] | 15 | * "gpl.txt" in the top directory of this repository.
|
---|
[a06aa8b] | 16 | */
|
---|
| 17 |
|
---|
| 18 | #include <all.h>
|
---|
| 19 |
|
---|
[4c71d39] | 20 | #define ver(...) _ver(kbd_verbose, 0, __VA_ARGS__)
|
---|
| 21 | #define ver2(...) _ver(kbd_verbose, 1, __VA_ARGS__)
|
---|
| 22 | #define ver3(...) _ver(kbd_verbose, 2, __VA_ARGS__)
|
---|
[a06aa8b] | 23 |
|
---|
[4c71d39] | 24 | int32_t kbd_verbose = 0;
|
---|
[a06aa8b] | 25 |
|
---|
[4ed9bfe] | 26 | #define BUF_SZ 16
|
---|
| 27 |
|
---|
| 28 | static int32_t buf_hd = 0;
|
---|
| 29 | static int32_t buf_tl = 0;
|
---|
| 30 | static uint8_t buf[BUF_SZ];
|
---|
| 31 |
|
---|
| 32 | static uint8_t reg = 0;
|
---|
| 33 | static bool irq = false;
|
---|
| 34 |
|
---|
| 35 | static void xmit(void)
|
---|
| 36 | {
|
---|
| 37 | ver2("kbd xmit %d %d", buf_tl, buf_hd);
|
---|
| 38 |
|
---|
| 39 | if (buf_tl >= buf_hd) {
|
---|
| 40 | return;
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | reg = buf[buf_tl % BUF_SZ];
|
---|
| 44 | irq = true;
|
---|
| 45 | ver2("kbd xmit 0x%02x", reg);
|
---|
| 46 |
|
---|
| 47 | ++buf_tl;
|
---|
| 48 |
|
---|
| 49 | if (buf_tl >= BUF_SZ) {
|
---|
| 50 | buf_hd -= BUF_SZ;
|
---|
| 51 | buf_tl -= BUF_SZ;
|
---|
| 52 | ver2("kbd adj %d %d", buf_tl, buf_hd);
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | static void out(uint8_t c)
|
---|
| 57 | {
|
---|
| 58 | ver2("kbd out %d %d 0x%02x", buf_tl, buf_hd, c);
|
---|
| 59 |
|
---|
| 60 | if (SDL_LockMutex(cpu_mutex) < 0) {
|
---|
| 61 | fail("SDL_LockMutex() failed: %s", SDL_GetError());
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | if (buf_hd >= buf_tl + BUF_SZ) {
|
---|
| 65 | err("keyboard port losing data");
|
---|
| 66 | }
|
---|
| 67 | else {
|
---|
| 68 | buf[buf_hd % BUF_SZ] = c;
|
---|
| 69 | ++buf_hd;
|
---|
| 70 |
|
---|
| 71 | if (!irq) {
|
---|
| 72 | xmit();
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | if (SDL_UnlockMutex(cpu_mutex) < 0) {
|
---|
| 77 | fail("SDL_UnlockMutex() failed: %s", SDL_GetError());
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | static void but_on(int32_t sig)
|
---|
| 82 | {
|
---|
| 83 | out((uint8_t)(0x80 | sig));
|
---|
| 84 | out(0x01);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | static void but_off(int32_t sig)
|
---|
| 88 | {
|
---|
| 89 | out((uint8_t)(0x80 | sig));
|
---|
| 90 | out(0x00);
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | #if defined NOT_YET
|
---|
| 94 | static void key_touch(int32_t sig, int32_t val)
|
---|
| 95 | {
|
---|
| 96 | out((uint8_t)(0x80 | sig));
|
---|
| 97 | out(0x01);
|
---|
| 98 | out((uint8_t)val);
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | static void key_off(int32_t sig)
|
---|
| 102 | {
|
---|
| 103 | out((uint8_t)(0x80 | sig));
|
---|
| 104 | out(0x00);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | static void pot_128(int32_t sig, int32_t val)
|
---|
| 108 | {
|
---|
| 109 | out((uint8_t)(0x80 | sig));
|
---|
| 110 | out((uint8_t)val);
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | static void pot_256(int32_t sig, int32_t val)
|
---|
| 114 | {
|
---|
| 115 | out((uint8_t)(0x80 | sig));
|
---|
| 116 | out((uint8_t)((val >> 7) & 0x01));
|
---|
| 117 | out((uint8_t)(val & 0x7f));
|
---|
| 118 | }
|
---|
| 119 | #endif
|
---|
| 120 |
|
---|
| 121 | void kbd_key(SDL_KeyboardEvent *ev)
|
---|
| 122 | {
|
---|
| 123 | (void)ev;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | void kbd_text(SDL_TextInputEvent *ev)
|
---|
| 127 | {
|
---|
| 128 | for (int32_t i = 0; ev->text[i] != 0; ++i) {
|
---|
| 129 | if (ev->text[i] >= '0' && ev->text[i] <= '9') {
|
---|
| 130 | int32_t sig = 60 + ev->text[i] - '0';
|
---|
| 131 | but_on(sig);
|
---|
| 132 | but_off(sig);
|
---|
| 133 | continue;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | switch (ev->text[i]) {
|
---|
| 137 | case 'x':
|
---|
| 138 | but_on(70);
|
---|
| 139 | but_off(70);
|
---|
| 140 | break;
|
---|
| 141 |
|
---|
| 142 | case 'e':
|
---|
| 143 | but_on(71);
|
---|
| 144 | but_off(71);
|
---|
| 145 | break;
|
---|
| 146 |
|
---|
| 147 | case 'm':
|
---|
| 148 | but_on(72);
|
---|
| 149 | but_off(72);
|
---|
| 150 | break;
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 |
|
---|
[a06aa8b] | 155 | void kbd_init(void)
|
---|
| 156 | {
|
---|
| 157 | ver("kbd init");
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | void kbd_quit(void)
|
---|
| 161 | {
|
---|
| 162 | ver("kbd quit");
|
---|
| 163 | }
|
---|
| 164 |
|
---|
[3c30832] | 165 | bool kbd_exec(void)
|
---|
[a06aa8b] | 166 | {
|
---|
[4c71d39] | 167 | ver3("kbd exec");
|
---|
[4ed9bfe] | 168 | return irq;
|
---|
[a06aa8b] | 169 | }
|
---|
| 170 |
|
---|
| 171 | uint32_t kbd_read(uint32_t off, int32_t sz)
|
---|
| 172 | {
|
---|
[4c71d39] | 173 | ver2("kbd rd %u:%d", off, sz * 8);
|
---|
[4ed9bfe] | 174 |
|
---|
| 175 | if (sz != 1 || off > 0) {
|
---|
| 176 | fail("invalid kbd rd %u:%d", off, sz * 8);
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | irq = false;
|
---|
| 180 | uint32_t res = reg;
|
---|
| 181 |
|
---|
| 182 | xmit();
|
---|
| 183 |
|
---|
| 184 | return res;
|
---|
[a06aa8b] | 185 | }
|
---|
| 186 |
|
---|
| 187 | void kbd_write(uint32_t off, int32_t sz, uint32_t val)
|
---|
| 188 | {
|
---|
[4c71d39] | 189 | ver2("kbd wr %u:%d 0x%0*x", off, sz * 8, sz * 2, val);
|
---|
[4ed9bfe] | 190 | fail("invalid kbd wr %u:%d", off, sz * 8);
|
---|
[a06aa8b] | 191 | }
|
---|