From 7948288a0b694215167da6d1dce315d4ef44dd7b Mon Sep 17 00:00:00 2001 From: mrb0nk500 Date: Tue, 17 Dec 2019 16:43:39 -0500 Subject: Revamped how the keyboard works. It now does not use an interrupt, although, in the emulator, it does act like an interrupt, when the keypoll macro is set to 0. --- sux.c | 103 ++++++++++++++++++++++++++--------------------------------- test/input.s | 30 ++++++++++++----- 2 files changed, 67 insertions(+), 66 deletions(-) diff --git a/sux.c b/sux.c index 21ac0ca..3f31d4e 100644 --- a/sux.c +++ b/sux.c @@ -6,7 +6,7 @@ #define bench 0 #define debug 0 #define IO 1 -#define en_nc 1 +#define keypoll 0 #if bench #include #endif @@ -26,9 +26,7 @@ uint8_t threads_done = 0; uint8_t kbd_rdy = 0; uint8_t wai = 0; uint8_t irq = 0; -#if en_nc WINDOW *scr; -#endif pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t main_mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond = PTHREAD_COND_INITIALIZER; @@ -63,7 +61,6 @@ void *run(void *args) { gettimeofday(&str[thread], 0); #endif while (!end) { - #if en_nc if (wai) { for (int8_t i = 56; i >= 0; i-=8) { if (i) @@ -87,7 +84,6 @@ void *run(void *args) { wai = 0; kbd_rdy &= (uint8_t)~(1 << thread); } - #endif prefix = addr[cpu->pc[thread]]; if ((prefix & 0x07) == 0x07) cpu->pc[thread]++; @@ -95,19 +91,18 @@ void *run(void *args) { prefix = 0; opcode = addr[cpu->pc[thread]]; - #if debug && !bench - /*sprintf(s, "\033[%uH" - "pc: 0x%08llx, a: 0x%016llx, x: 0x%016llx, y: 0x%016llx" - ", sp: 0x%04lx, ps: 0x%016llx, prefix: 0x%02x, opcode: 0x%02x, thread: %u, inst: %s \r" - , lines - , cpu->pc[thread], cpu->a[thread], cpu->x[thread], cpu->y[thread] - , cpu->sp[thread], cpu->ps, prefix, opcode, thread, opname[opcode]); - fwrite(s, sizeof(char), strlen(s), stdout);*/ + #if debug && !bench && keypoll + #if keypoll + pthread_mutex_lock(&mutex); + #endif mvwprintw(scr, lines, 0, "pc: 0x%08llx, a: 0x%016llx, x: 0x%016llx, y: 0x%016llx" ", sp: 0x%04lx, ps: 0x%016llx, prefix: 0x%02x, opcode: 0x%02x, thread: %u, inst: %s \r" , cpu->pc[thread], cpu->a[thread], cpu->x[thread], cpu->y[thread] , cpu->sp[thread], cpu->ps, prefix, opcode, thread, opname[opcode]); wrefresh(scr); + #if keypoll + pthread_mutex_unlock(&mutex); + #endif lines++; if (lines > 24*(thread+1)) lines = (24*thread)+2; @@ -764,10 +759,9 @@ void *run(void *args) { addr[address] = value & 0xFF; #if IO if (address == TX_ADDR) { - /*if (addr[address] == '\r') { - waddch(context->screen, '\n'); - }*/ - #if en_nc + #if keypoll + pthread_mutex_lock(&mutex); + #endif if (addr[address] == CURSES_BACKSPACE || addr[address] == '\b') { if (x > 0) { x--; @@ -785,13 +779,9 @@ void *run(void *args) { } else { x+=1; } - /*putchar(addr[address]);*/ } - #else - if (addr[address] == '\b') - putchar('\b'); - else - putchar(addr[address]); + #if keypoll + pthread_mutex_unlock(&mutex); #endif } #endif @@ -913,6 +903,16 @@ void *run(void *args) { cpu->pc[thread]+=4; iclk++; } + if (address == CTRL_ADDR) { + 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 + } value = (uint64_t)addr[address]; if (regsize >= 2) value += (uint64_t)addr[address+1] << 8; @@ -1454,20 +1454,18 @@ void *run(void *args) { pthread_mutex_unlock(&mutex); break; default: - if(opcode != BRK) { - /*printf("Cool, you inputed a non existent opcode, which means\n" - "that you have now wasted clock cycles.\n" - "Good job! *clap*\n");*/ - } break; } ins++; - #if debug && !bench - /*sprintf(s, "\033[%uHInstructions executed: %llu, Clock cycles: %llu\n", (6*thread)+1, ins, iclk); - fwrite(s, sizeof(char), strlen(s), stdout); - fflush(stdout);*/ + #if debug && !bench && keypoll + #if keypoll + pthread_mutex_lock(&mutex); + #endif mvwprintw(scr, (24*thread)+1, 0, "Instructions executed: %llu, Clock cycles: %llu\r", ins, iclk); wrefresh(scr); + #if keypoll + pthread_mutex_unlock(&mutex); + #endif #endif #if bench if (ins >= BENCH_INST) { @@ -1503,7 +1501,6 @@ int main(int argc, char **argv) { sprintf(tmp, "\033[2J\033[H"); fwrite(tmp, sizeof(char), strlen(tmp), stdout); fflush(stdout); - #if en_nc if(!scr) scr = initscr(); nodelay(stdscr, 0); @@ -1519,7 +1516,6 @@ int main(int argc, char **argv) { init_pair(1, COLOR_WHITE, -1); attron(COLOR_PAIR(1) | A_BOLD); wmove(scr, 0, 0); - #endif for (int i = 0; i < THREADS; i++) { thr[i].sx.sp[i] = 0xFFFF; thr[i].sx.stk_st[i] = i+1; @@ -1560,6 +1556,7 @@ int main(int argc, char **argv) { assert(!result); } int c = 0; + werase(scr); while (threads_done < THREADS) { int x, y, i = 0; #if !bench @@ -1568,8 +1565,11 @@ int main(int argc, char **argv) { pthread_cond_wait(&main_cond, &main_mutex); pthread_mutex_unlock(&main_mutex); c = 0; + addr[CTRL_ADDR] = 0; } - #if en_nc + #if keypoll + pthread_mutex_lock(&mutex); + #endif getyx(scr, y, x); attroff(A_REVERSE); attron(A_BLINK); @@ -1577,18 +1577,10 @@ int main(int argc, char **argv) { attroff(A_BLINK); wmove(scr, y, x); wrefresh(scr); - c = wgetch(scr); - - /*wmove(scr, 0, getmaxx(scr) - 1); - attron(A_REVERSE); - wprintw(scr, "!"); - attroff(A_REVERSE); - wmove(scr, y, x); - wrefresh(scr);*/ switch (c) { case ERR: - kbd_rdy = 0; + addr[CTRL_ADDR] = 0; wmove(scr, getmaxy(scr)-1, 0); wprintw(scr, "c: %i, x: %i, y: %i, i: %i.", c, x, y, i++); wmove(scr, y, x); @@ -1596,26 +1588,21 @@ int main(int argc, char **argv) { break; default: addr[RX_ADDR] = (uint8_t)c; - kbd_rdy = 1 << 0; + addr[CTRL_ADDR] = 1; + #if !keypoll pthread_mutex_lock(&mutex); - pthread_cond_broadcast(&cond); + pthread_cond_signal(&cond); pthread_mutex_unlock(&mutex); + #endif break; } - #else - c = getchar(); - if (c != EOF) { - sprintf(tmp, "\033[24Hc: %i\r", c); - fwrite(tmp, sizeof(char), strlen(tmp), stdout); - fflush(stdout); - kbd_rdy = 1 << 0; - pthread_mutex_lock(&mutex); - pthread_cond_signal(&cond); - pthread_mutex_unlock(&mutex); - } + #if keypoll + pthread_mutex_unlock(&mutex); #endif #else - + pthread_mutex_lock(&main_mutex); + pthread_cond_wait(&main_cond, &main_mutex); + pthread_mutex_unlock(&main_mutex); #endif } #if bench diff --git a/test/input.s b/test/input.s index 9e5ef69..b699dda 100644 --- a/test/input.s +++ b/test/input.s @@ -18,7 +18,7 @@ end: buffer: ; Main program -.org $0 +.org $8000 reset: cps ldx.w #$FFFF @@ -26,7 +26,7 @@ reset: ldy #$0 clr_buf: lda #$0 - cpy.w #$1000 + cpy.w #$400 beq start sta buffer, y iny @@ -37,20 +37,31 @@ start: ldy #$0 ; Reset y. jmp print +rset_a: + lda #$10 +delay: + beq read + dec + jmp delay sleep: - wai ; Sleep until we get a character. lda end bne spin ; Are we done with getting input? - jmp sleep ; Loop until we get a newline. +read: + nop ; Sleep until we get a character. + lda $C000 ; Get control register. + beq rset_a ; Loop until we get a character. + lda #$0 + sta $C000 + jmp getchar ; We got a key. print: lda string, x ; Get character at offset x. - beq sleep ; Did we find a null terminator? + beq rset_a ; Did we find a null terminator? sta $C001 ; Print character. inx ; Increment offset. jmp print ; Keep printing more characters. -irq_routine: +getchar: lda $C002 ; Get typed character. cmp #$A beq nl ; Did the user type a newline? @@ -60,8 +71,8 @@ echo: sta $C001 ; Echo typed character. sta buffer, y ; Store typed character into the input buffer. iny ; Increment buffer offset. -return: - rti ; End of interrupt routine. + nop + jmp rset_a ; We are not, so add the backspace to the buffer. nl: sta $C001 @@ -98,6 +109,7 @@ fin: sta $C001 ; Print the newline. lda #$1 ; Tell the program that we are done. sta end ; We are done. + jmp sleep spin: @@ -122,6 +134,8 @@ spin: .qword irq_routine ;.org $0 ;viewmem +;.org $100 +;viewmem ;q done -- cgit v1.2.3-13-gbd6f