Pretend to run the emulator

This commit is contained in:
Simon Gellis 2024-10-07 21:20:01 -04:00
parent e5932da1e5
commit 6ed78636a5
3 changed files with 22 additions and 5 deletions

View File

@ -22,6 +22,7 @@ typedef enum rdb_read_state_t {
typedef struct RdbRequest { typedef struct RdbRequest {
int connfd; int connfd;
bool blocking;
struct Buffer { struct Buffer {
char buf[INBUF_LEN]; char buf[INBUF_LEN];
size_t len; size_t len;
@ -36,6 +37,7 @@ typedef struct RdbRequest {
void rdb_request_init(RdbRequest *req, int connfd, char *cmd, size_t cmdlen); void rdb_request_init(RdbRequest *req, int connfd, char *cmd, size_t cmdlen);
void rdb_request_reset(RdbRequest *req); void rdb_request_reset(RdbRequest *req);
void rdb_request_set_blocking(RdbRequest *req, bool blocking);
rdb_read_result_t rdb_request_read(RdbRequest *req, size_t *len); rdb_read_result_t rdb_request_read(RdbRequest *req, size_t *len);
#endif #endif

14
main.c
View File

@ -78,10 +78,11 @@ const uint32_t SYSTEM_REGISTERS[] = {
const uint32_t PC_INDEX = 32 + 13; const uint32_t PC_INDEX = 32 + 13;
int handle_command(RdbClient *client, CommandBuf *cmd, VB *sim) { int handle_command(RdbClient *client, CommandBuf *cmd, VB *sim, bool *running) {
rdb_client_begin_packet(client); rdb_client_begin_packet(client);
if (cmd_match_only_str(cmd, "\x03")) { if (cmd_match_only_str(cmd, "\x03")) {
*running = false;
rdb_client_write_str(client, "T05thread:p1.t1;threads:p1.t1"); rdb_client_write_str(client, "T05thread:p1.t1;threads:p1.t1");
return rdb_client_send_packet(client); return rdb_client_send_packet(client);
} }
@ -183,6 +184,7 @@ int handle_command(RdbClient *client, CommandBuf *cmd, VB *sim) {
} }
if (cmd_match_only_str(cmd, "c")) { if (cmd_match_only_str(cmd, "c")) {
printf("running until we hit a breakpoint or the server stops us\n"); printf("running until we hit a breakpoint or the server stops us\n");
*running = true;
return 0; return 0;
} }
if (cmd_match_only_str(cmd, "?")) { if (cmd_match_only_str(cmd, "?")) {
@ -224,6 +226,8 @@ int server(int connfd, VB *sim) {
rdb_request_init(&req, connfd, buf, BUFLEN); rdb_request_init(&req, connfd, buf, BUFLEN);
rdb_client_init(&client, connfd); rdb_client_init(&client, connfd);
bool running = false;
while (1) { while (1) {
rdb_read_result_t result = rdb_request_read(&req, &len); rdb_read_result_t result = rdb_request_read(&req, &len);
if (result == read_result_error) { if (result == read_result_error) {
@ -232,7 +236,10 @@ int server(int connfd, VB *sim) {
printf("client has disconnected\n"); printf("client has disconnected\n");
return 0; return 0;
} else if (result == read_result_pending) { } else if (result == read_result_pending) {
// TODO: should run the emulator while we wait if (running) {
printf("pretend the emulator is running now\n");
sleep(1);
}
continue; continue;
} else { } else {
printf("received command \"%.*s\"\n", (int) len, buf); printf("received command \"%.*s\"\n", (int) len, buf);
@ -240,10 +247,11 @@ int server(int connfd, VB *sim) {
CommandBuf cmd; CommandBuf cmd;
cmd.buf = buf; cmd.buf = buf;
cmd.len = len; cmd.len = len;
int res = handle_command(&client, &cmd, sim); int res = handle_command(&client, &cmd, sim, &running);
if (res != 0) { if (res != 0) {
return res; return res;
} }
rdb_request_set_blocking(&req, !running);
rdb_request_reset(&req); rdb_request_reset(&req);
} }

View File

@ -2,12 +2,14 @@
#include <hex.h> #include <hex.h>
#include <request.h> #include <request.h>
#include <stdio.h> #include <stdio.h>
#include <sys/socket.h>
#include <unistd.h> #include <unistd.h>
void rdb_request_init(RdbRequest *req, int connfd, char *cmd, size_t cmdlen) { void rdb_request_init(RdbRequest *req, int connfd, char *cmd, size_t cmdlen) {
req->connfd = connfd; req->connfd = connfd;
req->cmd = cmd; req->cmd = cmd;
req->cmdlen = cmdlen; req->cmdlen = cmdlen;
req->blocking = true;
rdb_request_reset(req); rdb_request_reset(req);
} }
@ -19,14 +21,19 @@ void rdb_request_reset(RdbRequest *req) {
req->chk = 0; req->chk = 0;
} }
void rdb_request_set_blocking(RdbRequest *req, bool blocking) {
req->blocking = blocking;
}
static rdb_read_result_t read_char(RdbRequest *req, char *in) { static rdb_read_result_t read_char(RdbRequest *req, char *in) {
if (req->inbuf.index >= req->inbuf.len) { if (req->inbuf.index >= req->inbuf.len) {
ssize_t inlen = read(req->connfd, req->inbuf.buf, INBUF_LEN); int flags = req->blocking ? 0 : MSG_DONTWAIT;
ssize_t inlen = recv(req->connfd, req->inbuf.buf, INBUF_LEN, flags);
if (inlen == 0) { if (inlen == 0) {
return read_result_disconnected; return read_result_disconnected;
} }
if (inlen < 0) { if (inlen < 0) {
if (errno == EAGAIN) { if (errno == EAGAIN || errno == EWOULDBLOCK) {
return read_result_pending; return read_result_pending;
} else { } else {
perror("could not read incoming packet"); perror("could not read incoming packet");