Make server a struct

This commit is contained in:
Simon Gellis 2024-10-10 18:20:16 -04:00
parent 57403690ff
commit 884b52b397
3 changed files with 38 additions and 26 deletions

View File

@ -6,6 +6,13 @@
#include <stdbool.h> #include <stdbool.h>
#include <vb.h> #include <vb.h>
int handle_command(RdbResponse *res, CommandBuf *cmd, VB *sim, bool *running); typedef struct RdbServer {
VB *sim;
bool running;
} RdbServer;
void rdb_server_init(RdbServer *srv, VB *sim);
int rdb_server_handle_command(RdbServer *srv, CommandBuf *cmd, RdbResponse *res);
int rdb_server_break(RdbServer *srv, RdbResponse *res);
#endif #endif

19
main.c
View File

@ -23,16 +23,15 @@ int sleep_nanos(long int ns) {
int server(int connfd, VB *sim) { int server(int connfd, VB *sim) {
RdbRequest req; RdbRequest req;
RdbResponse res; RdbResponse res;
RdbServer srv;
char reqbuf[BUFLEN]; char reqbuf[BUFLEN];
char resbuf[BUFLEN]; char resbuf[BUFLEN];
bool running;
rdb_read_result_t read_result; rdb_read_result_t read_result;
int result; int result;
rdb_request_init(&req, connfd, reqbuf, BUFLEN); rdb_request_init(&req, connfd, reqbuf, BUFLEN);
rdb_response_init(&res, connfd, resbuf, BUFLEN); rdb_response_init(&res, connfd, resbuf, BUFLEN);
rdb_server_init(&srv, sim);
running = false;
while (1) { while (1) {
CommandBuf cmd; CommandBuf cmd;
@ -45,20 +44,14 @@ int server(int connfd, VB *sim) {
printf("client has disconnected\n"); printf("client has disconnected\n");
return 0; return 0;
} else if (read_result == read_result_pending) { } else if (read_result == read_result_pending) {
if (running) { if (srv.running) {
cycles = MAX_STEP_CYCLES; cycles = MAX_STEP_CYCLES;
brk = vbEmulate(sim, &cycles); brk = vbEmulate(sim, &cycles);
if (brk) { if (brk) {
/* We hit a breakpoint */ /* We hit a breakpoint */
if (brk == -1) { if (brk == -1) {
/* actually, not implemented */ /* actually, not implemented */
running = false; rdb_server_break(&srv, &res);
rdb_response_begin_packet(&res);
rdb_response_write_str(&res, "T05:thread:p1.t1;threads:p1.t1;");
result = rdb_response_send_packet(&res);
if (result != 0) {
return result;
}
} else { } else {
fprintf(stderr, "surprising response %d from vbEmulate\n", brk); fprintf(stderr, "surprising response %d from vbEmulate\n", brk);
return -1; return -1;
@ -71,11 +64,11 @@ int server(int connfd, VB *sim) {
} else { } else {
printf("received command \"%.*s\"\n", (int) cmd.len, cmd.buf); printf("received command \"%.*s\"\n", (int) cmd.len, cmd.buf);
fflush(stdout); fflush(stdout);
result = handle_command(&res, &cmd, sim, &running); result = rdb_server_handle_command(&srv, &cmd, &res);
if (result != 0) { if (result != 0) {
return result; return result;
} }
rdb_request_set_blocking(&req, !running); rdb_request_set_blocking(&req, !srv.running);
rdb_request_reset(&req); rdb_request_reset(&req);
} }

View File

@ -69,7 +69,12 @@ const uint32_t SYSTEM_REGISTERS[] = {
const uint32_t PC_INDEX = 32 + 13; const uint32_t PC_INDEX = 32 + 13;
int handle_command(RdbResponse *res, CommandBuf *cmd, VB *sim, bool *running) { void rdb_server_init(RdbServer *srv, VB *sim) {
srv->sim = sim;
srv->running = false;
}
int rdb_server_handle_command(RdbServer *srv, CommandBuf *cmd, RdbResponse *res) {
rdb_response_begin_packet(res); rdb_response_begin_packet(res);
if (cmd_match_str(cmd, "QStartNoAckMode")) { if (cmd_match_str(cmd, "QStartNoAckMode")) {
@ -149,11 +154,11 @@ int handle_command(RdbResponse *res, CommandBuf *cmd, VB *sim, bool *running) {
} }
if (reg_no == PC_INDEX) { if (reg_no == PC_INDEX) {
reg_value = vbGetProgramCounter(sim); reg_value = vbGetProgramCounter(srv->sim);
} else if (reg_no > 31) { } else if (reg_no > 31) {
reg_value = vbGetSystemRegister(sim, SYSTEM_REGISTERS[reg_no - 32]); reg_value = vbGetSystemRegister(srv->sim, SYSTEM_REGISTERS[reg_no - 32]);
} else { } else {
reg_value = vbGetProgramRegister(sim, reg_no); reg_value = vbGetProgramRegister(srv->sim, reg_no);
} }
rdb_response_write_i32_hex(res, reg_value); rdb_response_write_i32_hex(res, reg_value);
return rdb_response_send_packet(res); return rdb_response_send_packet(res);
@ -176,11 +181,11 @@ int handle_command(RdbResponse *res, CommandBuf *cmd, VB *sim, bool *running) {
return rdb_response_send_packet(res); return rdb_response_send_packet(res);
} }
if (reg_no == PC_INDEX) { if (reg_no == PC_INDEX) {
vbSetProgramCounter(sim, reg_value); vbSetProgramCounter(srv->sim, reg_value);
} else if (reg_no > 31) { } else if (reg_no > 31) {
vbSetSystemRegister(sim, SYSTEM_REGISTERS[reg_no - 32], reg_value); vbSetSystemRegister(srv->sim, SYSTEM_REGISTERS[reg_no - 32], reg_value);
} else { } else {
vbSetProgramRegister(sim, reg_no, reg_value); vbSetProgramRegister(srv->sim, reg_no, reg_value);
} }
rdb_response_write_str(res, "OK"); rdb_response_write_str(res, "OK");
return rdb_response_send_packet(res); return rdb_response_send_packet(res);
@ -193,12 +198,12 @@ int handle_command(RdbResponse *res, CommandBuf *cmd, VB *sim, bool *running) {
if (cmd_match_str(cmd, "c")) { if (cmd_match_str(cmd, "c")) {
/* The debugger has told us to run until we are stopped. */ /* The debugger has told us to run until we are stopped. */
/* Don't send a response to this until we receive an ETX (when the debugger pauses us). */ /* Don't send a response to this until we receive an ETX (when the debugger pauses us). */
*running = true; srv->running = true;
return 0; return 0;
} }
if (cmd_match_str(cmd, "\x03")) { if (cmd_match_str(cmd, "\x03")) {
/* Received an ETX, indicating that the server wants to cancel the "c" command from before. */ /* Received an ETX, indicating that the server wants to cancel the "c" command from before. */
*running = false; srv->running = false;
/* Send the response to the "c" command from before. */ /* Send the response to the "c" command from before. */
rdb_response_write_str(res, "T05thread:p1.t1;threads:p1.t1"); rdb_response_write_str(res, "T05thread:p1.t1;threads:p1.t1");
return rdb_response_send_packet(res); return rdb_response_send_packet(res);
@ -206,7 +211,7 @@ int handle_command(RdbResponse *res, CommandBuf *cmd, VB *sim, bool *running) {
if (cmd_match_str(cmd, "?")) { if (cmd_match_str(cmd, "?")) {
/* The debugger has asked us why we stopped */ /* The debugger has asked us why we stopped */
rdb_response_write_str(res, "T"); rdb_response_write_str(res, "T");
rdb_response_write_str(res, *running ? "00" : "05"); rdb_response_write_str(res, srv->running ? "00" : "05");
rdb_response_write_str(res, "thread:p1.t1;threads:p1.t1;"); rdb_response_write_str(res, "thread:p1.t1;threads:p1.t1;");
return rdb_response_send_packet(res); return rdb_response_send_packet(res);
} }
@ -218,7 +223,7 @@ int handle_command(RdbResponse *res, CommandBuf *cmd, VB *sim, bool *running) {
if (!cmd_match_hex_number(cmd, &len)) return -1; if (!cmd_match_hex_number(cmd, &len)) return -1;
for (i = 0; i < len; ++i) { for (i = 0; i < len; ++i) {
uint8_t byte = vbRead(sim, address + i, VB_U8); uint8_t byte = vbRead(srv->sim, address + i, VB_U8);
rdb_response_write_i8_hex(res, byte); rdb_response_write_i8_hex(res, byte);
} }
return rdb_response_send_packet(res); return rdb_response_send_packet(res);
@ -234,7 +239,7 @@ int handle_command(RdbResponse *res, CommandBuf *cmd, VB *sim, bool *running) {
for (i = 0; i < len; ++i) { for (i = 0; i < len; ++i) {
uint8_t byte; uint8_t byte;
if (!cmd_match_hex_bytes(cmd, 1, &byte)) return -1; if (!cmd_match_hex_bytes(cmd, 1, &byte)) return -1;
vbWrite(sim, address + i, VB_U8, byte); vbWrite(srv->sim, address + i, VB_U8, byte);
} }
rdb_response_write_str(res, "OK"); rdb_response_write_str(res, "OK");
return rdb_response_send_packet(res); return rdb_response_send_packet(res);
@ -243,3 +248,10 @@ int handle_command(RdbResponse *res, CommandBuf *cmd, VB *sim, bool *running) {
return rdb_response_send_packet(res); return rdb_response_send_packet(res);
} }
int rdb_server_break(RdbServer *srv, RdbResponse *res) {
srv->running = false;
rdb_response_begin_packet(res);
rdb_response_write_str(res, "T05:thread:p1.t1;threads:p1.t1;");
return rdb_response_send_packet(res);
}