summaryrefslogtreecommitdiff
path: root/sux.c
diff options
context:
space:
mode:
authormrb0nk500 <b0nk@b0nk.xyz>2019-12-17 16:43:39 -0500
committermrb0nk500 <b0nk@b0nk.xyz>2019-12-17 16:43:39 -0500
commit7948288a0b694215167da6d1dce315d4ef44dd7b (patch)
tree01a21ee206d1e36f0313bea80eb1fb2718b3f970 /sux.c
parent0b81224b6ab8cd6da45039525962c6490ed2df56 (diff)
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.
Diffstat (limited to 'sux.c')
-rw-r--r--sux.c103
1 files changed, 45 insertions, 58 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 <sys/time.h>
#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