diff options
author | mrb0nk500 <b0nk@b0nk.xyz> | 2020-08-11 18:03:48 -0400 |
---|---|---|
committer | mrb0nk500 <b0nk@b0nk.xyz> | 2020-08-11 18:03:48 -0400 |
commit | d49e39c672e78d536d658785adffb9149715832b (patch) | |
tree | 74ce383bbf76e88b4e4ac8d2822b51325500b686 | |
parent | d17eb092f620d217f08ae7fb27122bb30799eaf4 (diff) |
Refactored the keyboard I/O emulation, again.
I decided to use keypad now, and create a function to
return the actual key sequences.
The keyboard I/O is also now being handled by the io
emulation function, and not by the main function.
This should get rid of any possible deadlocks.
-rw-r--r-- | io.c | 188 | ||||
-rw-r--r-- | sux.c | 111 | ||||
-rw-r--r-- | sux.h | 7 |
3 files changed, 119 insertions, 187 deletions
@@ -1,103 +1,121 @@ #include "sux.h" +char *key = NULL; uint8_t key_idx = 0; uint8_t iscol; uint8_t idx = 3; uint8_t bcd[4]; -int get_keycode(char *str) { - size_t size = strlen(str); +static inline char *get_keyseq(int keycode) { + switch (keycode) { + case KEY_BACKSPACE: return "\b"; + case KEY_UP : return "\x1B[A"; + case KEY_DOWN : return "\x1B[B"; + case KEY_RIGHT : return "\x1B[C"; + case KEY_LEFT : return "\x1B[D"; + case KEY_F( 1) : return "\x1BOP"; + case KEY_F( 2) : return "\x1BOQ"; + case KEY_F( 3) : return "\x1BOR"; + case KEY_F( 4) : return "\x1BOS"; + case KEY_F( 5) : return "\x1B[15~"; + case KEY_F( 6) : return "\x1B[17~"; + case KEY_F( 7) : return "\x1B[18~"; + case KEY_F( 8) : return "\x1B[19~"; + case KEY_F( 9) : return "\x1B[20~"; + case KEY_F(10) : return "\x1B[21~"; + case KEY_F(11) : return "\x1B[23~"; + case KEY_F(12) : return "\x1B[24~"; + default : return NULL; + } +} + +int get_key(WINDOW *scr) { + int x, y; + int c; int keycode = 0; - if (size == 1) { - keycode = str[0]; - } else if (size > 1) { - int isesc = 0; - for (int i = 0; str[i] != '\0'; i++) { - switch (str[i]) { - case '\x1B': isesc = 1; break; - case '[': - if (isesc) { - i++; - char tmp[3]; - switch (str[i]) { - case 'A': keycode = KEY_UP ; break; - case 'B': keycode = KEY_DOWN ; break; - case 'C': keycode = KEY_LEFT ; break; - case 'D': keycode = KEY_RIGHT; break; - default : - if (isdigit(str[i])) { - memcpy(tmp, str+i, 2); - i += 2; - tmp[2] = '\0'; - int num = strtol(tmp, NULL, 10); - if (str[i] == '~') { - switch (num) { - case 15: keycode = KEY_F( 5); break; - case 17: keycode = KEY_F( 6); break; - case 18: keycode = KEY_F( 7); break; - case 19: keycode = KEY_F( 8); break; - case 20: keycode = KEY_F( 9); break; - case 21: keycode = KEY_F(10); break; - case 23: keycode = KEY_F(11); break; - case 24: keycode = KEY_F(12); break; - } - } - } + curs_set(1); + if ((key == NULL) || (key && key[key_idx] == '\0') || !kbd_rdy) { + c = wgetch(scr); + if (c == 19) { + if (kbd_rdy) { + c = wgetch(scr); + } + step = 1; + } else if (c == 0x11) { + end = 1; + } + if (kbd_rdy) { + key_idx = 0; + key = get_keyseq(c); + } + keycode = c; + } + if (kbd_rdy) { + c = (key != NULL) ? key[key_idx++] : c; + } - } - } else { - keycode = '['; - } - break; - case 'O': - if (isesc) { - i++; - switch (str[i]) { - case 'P': keycode = KEY_F(1); break; - case 'Q': keycode = KEY_F(2); break; - case 'R': keycode = KEY_F(3); break; - case 'S': keycode = KEY_F(4); break; - } - } else { - keycode = 'O'; - } + if (step) { + if (keycode != 19 && keycode != 18 && keycode != 0x11 && !isalnum(keycode)) { + /*WINDOW *w; + int maxcol = getmaxx(scr)/2; + int maxrow = getmaxy(scr)/2;*/ + switch (keycode) { + case KEY_F(1): + /*w = newwin(maxrow, maxcol, maxrow, maxcol); + emumon(w); + delwin(w);*/ + endwin(); + puts("Starting asmmon()"); + asmmon("stdin"); + puts("Reinitializing screen."); + init_scr(); + wrefresh(scr); break; } + #if debug && !bench + getyx(scr, y, x); + wmove(scr, getmaxy(scr)-1, 0); + wclrtoeol(scr); + wprintw(scr, "keycode: %i", keycode); + wmove(scr, y, x); + #endif } } - return keycode; -} - -int get_key(WINDOW *scr) { - int c = 0; - uint8_t i = 0; - uint8_t isesc = 0; - size_t size = strlen(key); - if (key[key_idx] == '\0' && size) { - memset(key, 0, size+1); - key_idx = 0; - } - if (!size || !key_idx) { - for (;;) { - c = wgetch(scr); - if (c != ERR) { - key[i++] = c; - isesc = (c == '\x1B'); - } else if (i && !isesc) { - key[i] = 0; - key_idx = 0; - size = strlen(key); + if (kbd_rdy) { + switch (c) { + case ERR: + addr[CTRL_ADDR] = 0; + break; + case '\0': break; + default: + if (kbd_rdy && c < 0x100) { + addr[CTRL_ADDR] = 1; + #if debug && !bench + wmove(scr, getmaxy(scr)-1, 0); + wclrtoeol(scr); + wprintw(scr, "c: %i ", c); + wprintw(scr, "key: "); + for (int i = 0; key && key[i] != '\0'; i++) { + wprintw(scr, "$%02X%s", key[i], (key[i+1] != '\0') ? ", " : ""); + } + wmove(scr, y, x); + #endif + } break; - } + } + } else { + if (step) { + step = !(c == 18); } } - if (key[key_idx] != '\0') { - return key[key_idx++]; - } + addr[STEP_ADDR] = step; + curs_set(0); + return c; } + void io(uint64_t address, uint8_t rw) { int x, y; uint16_t scr_col = 0; @@ -109,14 +127,8 @@ void io(uint64_t address, uint8_t rw) { break; } kbd_rdy = 1; - pthread_mutex_lock(&main_mutex); - pthread_cond_signal(&main_cond); - pthread_mutex_unlock(&main_mutex); - #if !keypoll - pthread_mutex_lock(&mutex); - pthread_cond_wait(&cond, &mutex); - pthread_mutex_unlock(&mutex); - #endif + wrefresh(scr); + addr[RX_ADDR] = get_key(scr); kbd_rdy = 0; break; case TX_ADDR: @@ -33,6 +33,8 @@ uint8_t *addr; uint8_t kbd_rdy; +uint8_t end = 0; + WINDOW *scr; struct suxthr { @@ -489,13 +491,15 @@ void *run(void *args) { ins++; #endif #if !bench - if (step) { + if (end) { pthread_mutex_lock(&main_mutex); pthread_cond_signal(&main_cond); pthread_mutex_unlock(&main_mutex); - pthread_mutex_lock(&mutex); - pthread_cond_wait(&cond, &mutex); - pthread_mutex_unlock(&mutex); + return NULL; + } + if (step) { + int c = 0;; + for (; step && c != 19 && !end; c = get_key(scr)); #if debug wrefresh(scr); #endif @@ -539,7 +543,7 @@ void init_scr() { scr = initscr(); } nodelay(scr, 0); - wtimeout(scr, 8); + keypad(scr, 1); crmode(); noecho(); nl(); @@ -551,11 +555,13 @@ void init_scr() { attron(COLOR_PAIR(1) | A_BOLD); } + + + int main(int argc, char **argv) { struct suxthr thr[THREADS]; char *tmp = malloc(2048); addr = malloc(mem_size); - memset(key, 0, sizeof(key)); #if bench inss = 0; #endif @@ -609,102 +615,15 @@ int main(int argc, char **argv) { result = pthread_create(&therads[i], NULL, run, &thr[i]); assert(!result); } - int c = 0; - uint8_t step_key = 0; - uint8_t end = 0; werase(scr); while (threads_done < THREADS && !end) { #if !bench - int x, y; - if ((step_key && step && !kbd_rdy) || !step || kbd_rdy) { - if ((c != EOF && c !=-1)) { - #if !keypoll - pthread_mutex_lock(&mutex); - pthread_cond_signal(&cond); - pthread_mutex_unlock(&mutex); - #endif - pthread_mutex_lock(&main_mutex); - curs_set(0); - pthread_cond_wait(&main_cond, &main_mutex); - pthread_mutex_unlock(&main_mutex); - curs_set(1); - c = 0; - step_key = 0; - addr[CTRL_ADDR] = 0; - wrefresh(scr); - } - } + pthread_mutex_lock(&main_mutex); + pthread_cond_wait(&main_cond, &main_mutex); + pthread_mutex_unlock(&main_mutex); #if keypoll pthread_mutex_lock(&mutex); #endif - c = get_key(scr); - if (c == 19) { - if (kbd_rdy) { - - c = get_key(scr); - } - step = 1; - } else if (c == 0x11) { - end = 1; - continue; - } - if (step) { - if (c != 19 && c != 18 && c != 0x11 && !isalnum(c)) { - /*WINDOW *w; - int maxcol = getmaxx(scr)/2; - int maxrow = getmaxy(scr)/2;*/ - int keycode = get_keycode(key); - switch (keycode) { - case KEY_F(1): - /*w = newwin(maxrow, maxcol, maxrow, maxcol); - emumon(w); - delwin(w);*/ - endwin(); - puts("Starting asmmon()"); - asmmon("stdin"); - puts("Reinitializing screen."); - init_scr(); - wrefresh(scr); - break; - } - #if debug && !bench - wmove(scr, getmaxy(scr)-1, 0); - wclrtoeol(scr); - wprintw(scr, "c: %i", c); - wmove(scr, y, x); - #endif - } - } - if (kbd_rdy) { - switch (c) { - case ERR: - addr[CTRL_ADDR] = 0; - break; - case '\0': break; - default: - if (kbd_rdy && c < 0x100) { - addr[RX_ADDR] = (uint8_t)c; - addr[CTRL_ADDR] = 1; - #if debug && !bench - wmove(scr, getmaxy(scr)-1, 0); - wclrtoeol(scr); - wprintw(scr, "c: %i ", c); - wprintw(scr, "key: "); - for (int i = 0; key[i] != '\0'; i++) { - wprintw(scr, "$%02X%s", key[i], (key[i+1] != '\0') ? ", " : ""); - } - wmove(scr, y, x); - #endif - } - break; - } - } else { - if (step) { - step = !(c == 18); - step_key = (c == 19); - } - } - addr[STEP_ADDR] = step; #if keypoll pthread_mutex_unlock(&mutex); #endif @@ -28,6 +28,7 @@ static const uint64_t mem_size = 0x04000000; /* Size of address space. */ extern uint8_t step; extern uint8_t esc; +extern uint8_t end; #define setflag(flag, bit) ((flag)) ? (cpu->ps.u8[thread] |= bit) : (cpu->ps.u8[thread] &= ~bit) #define getflag(bit) (cpu->ps.u8[thread] & bit) @@ -41,12 +42,12 @@ extern pthread_cond_t main_cond; extern void disasm(struct sux *cpu, uint64_t *operands, uint8_t lines, uint8_t opcode, uint8_t prefix, uint8_t thread); #endif -#define KEYBUF_SIZE 0x40 -char key[KEYBUF_SIZE]; +/*#define KEYBUF_SIZE 0x40 +char key[KEYBUF_SIZE];*/ -extern int get_keycode(char *str); extern int get_key(WINDOW *scr); extern void io(uint64_t address, uint8_t rw); +extern void init_scr(); static inline uint64_t get_addr(struct sux *cpu, uint64_t *tmpaddr, uint8_t opcode, uint8_t prefix, uint8_t thread) { union reg address; |