Added IOREQ_MILLIS and IOREQ_GETC to get current time and poll keyboard.

IOREQ_GETC implemented inefficiently, but is fine for demo purposes.
This commit is contained in:
Toby Jaffey 2025-12-07 20:29:10 +00:00
parent af3b48cb01
commit 9e6f6c5a67
3 changed files with 86 additions and 7 deletions

View file

@ -7,4 +7,5 @@
#define IOREQ_PRINTX 0x13D #define IOREQ_PRINTX 0x13D
#define IOREQ_MILLIS 0x13F #define IOREQ_MILLIS 0x13F
#define IOREQ_PRINTC 0x140 #define IOREQ_PRINTC 0x140
#define IOREQ_GETC 0x141

View file

@ -1,5 +1,5 @@
all: all:
gcc -Wall -Werror -pedantic -std=c99 -DUVM32_MEMORY_SIZE=16384 -I../uvm32 -o host ../uvm32/uvm32.c host.c gcc -Wall -Werror -pedantic -std=c99 -O2 -DUVM32_MEMORY_SIZE=65535 -I../uvm32 -o host ../uvm32/uvm32.c host.c
clean: clean:
rm -f host rm -f host

View file

@ -1,11 +1,16 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <termios.h>
#include "uvm32.h" #include "uvm32.h"
#include "../common/uvm32_common_custom.h" #include "../common/uvm32_common_custom.h"
// stash terminal settings on startup
static struct termios orig_termios;
// ioreqs exposed to vm environement // ioreqs exposed to vm environement
typedef enum { typedef enum {
F_PRINT, F_PRINT,
@ -14,6 +19,7 @@ typedef enum {
F_PRINTC, F_PRINTC,
F_PRINTLN, F_PRINTLN,
F_MILLIS, F_MILLIS,
F_GETC,
} f_code_t; } f_code_t;
// Map exposed ioreqs to CSRs // Map exposed ioreqs to CSRs
@ -24,8 +30,24 @@ const uvm32_mapping_t env[] = {
{ .csr = IOREQ_PRINTX, .typ = IOREQ_TYP_U32_WR, .code = F_PRINTX }, { .csr = IOREQ_PRINTX, .typ = IOREQ_TYP_U32_WR, .code = F_PRINTX },
{ .csr = IOREQ_PRINTC, .typ = IOREQ_TYP_U32_WR, .code = F_PRINTC }, { .csr = IOREQ_PRINTC, .typ = IOREQ_TYP_U32_WR, .code = F_PRINTC },
{ .csr = IOREQ_MILLIS, .typ = IOREQ_TYP_U32_RD, .code = F_MILLIS }, { .csr = IOREQ_MILLIS, .typ = IOREQ_TYP_U32_RD, .code = F_MILLIS },
{ .csr = IOREQ_GETC, .typ = IOREQ_TYP_U32_RD, .code = F_GETC },
}; };
void disableRawMode(void) {
tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios);
}
void enableRawMode(void) {
tcgetattr(STDIN_FILENO, &orig_termios);
atexit(disableRawMode);
struct termios raw = orig_termios;
raw.c_lflag &= ~(ECHO | ICANON | IEXTEN);
raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
raw.c_cflag |= (CS8);
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
raw.c_cc[VMIN] = 0;
raw.c_cc[VTIME] = 1;
}
static uint8_t *read_file(const char* filename, int *len) { static uint8_t *read_file(const char* filename, int *len) {
FILE* f = fopen(filename, "rb"); FILE* f = fopen(filename, "rb");
uint8_t *buf = NULL; uint8_t *buf = NULL;
@ -56,15 +78,60 @@ static uint8_t *read_file(const char* filename, int *len) {
return buf; return buf;
} }
// Poll for input, inelegant, but works and doesn't confuse the main
// flow of the program with select/poll
bool poll_getch(uint8_t* c) {
struct timeval tv;
fd_set readfds;
int ret;
FD_ZERO(&readfds);
FD_SET(STDIN_FILENO, &readfds);
tv.tv_sec = 0;
tv.tv_usec = 200;
ret = select(STDIN_FILENO + 1, &readfds, NULL, NULL, &tv);
if (ret == -1) {
printf("console read failed\r\n");
exit(1);
} else if (!ret) {
// timeout
return false;
}
if (FD_ISSET(STDIN_FILENO, &readfds)) {
int len;
len = read(STDIN_FILENO, c, 1);
if (len == -1) {
printf("console read failed\r\n");
exit(1);
}
if (len) {
if (*c == 0x03 || *c == 0x04) { // ctrl-c ctrl-d
exit(0);
}
if (*c == 0x0d) {
*c = '\n';
}
return true;
}
}
return false;
}
void hexdump(const uint8_t *p, int len) { void hexdump(const uint8_t *p, int len) {
while(len--) { while(len--) {
printf("%02x", *p++); printf("%02x", *p++);
} }
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
uvm32_state_t vmst; uvm32_state_t vmst;
clock_t start_time = clock() / (CLOCKS_PER_SEC / 1000);
argc--; argc--;
argv++; argv++;
@ -92,8 +159,10 @@ int main(int argc, char *argv[]) {
uint32_t total_instrs = 0; uint32_t total_instrs = 0;
uint32_t num_ioreqs = 0; uint32_t num_ioreqs = 0;
enableRawMode();
while(isrunning) { while(isrunning) {
total_instrs += uvm32_run(&vmst, &evt, 100); // num instructions before vm considered hung total_instrs += uvm32_run(&vmst, &evt, 1000000); // num instructions before vm considered hung
num_ioreqs++; num_ioreqs++;
switch(evt.typ) { switch(evt.typ) {
@ -126,10 +195,17 @@ int main(int argc, char *argv[]) {
case F_PRINTX: case F_PRINTX:
printf("%08x", evt.data.ioreq.val.u32); printf("%08x", evt.data.ioreq.val.u32);
break; break;
case F_GETC: {
uint8_t ch;
if (poll_getch(&ch)) {
*evt.data.ioreq.val.u32p = ch;
} else {
*evt.data.ioreq.val.u32p = 0xFFFFFFFF; // nothing
}
} break;
case F_MILLIS: { case F_MILLIS: {
static uint32_t t = 0; clock_t now = clock() / (CLOCKS_PER_SEC / 1000);
*evt.data.ioreq.val.u32p = t; *evt.data.ioreq.val.u32p = now - start_time;
t++;
} break; } break;
default: // catch any others default: // catch any others
switch(evt.data.ioreq.typ) { switch(evt.data.ioreq.typ) {
@ -162,5 +238,7 @@ int main(int argc, char *argv[]) {
printf("Executed total of %d instructions and %d ioreqs\n", (int)total_instrs, (int)num_ioreqs); printf("Executed total of %d instructions and %d ioreqs\n", (int)total_instrs, (int)num_ioreqs);
free(rom); free(rom);
disableRawMode();
return 0; return 0;
} }