From a6f3f87b97dacffaf3f6fd5dee37cf4531f10999 Mon Sep 17 00:00:00 2001 From: "Satoshi N. M" Date: Sat, 13 Jan 2018 13:41:29 -0800 Subject: [PATCH] Change serial commands to control characters, send everything else to ACIA (6850) input buffer --- src/acia6850.c | 31 +++++++++++++++++++++++++++---- src/cdcacm.c | 20 ++++++++------------ src/main.c | 18 ++++++++---------- 3 files changed, 43 insertions(+), 26 deletions(-) diff --git a/src/acia6850.c b/src/acia6850.c index f5b7851..84e4da4 100644 --- a/src/acia6850.c +++ b/src/acia6850.c @@ -1,3 +1,4 @@ +#include #include #include @@ -20,14 +21,36 @@ extern usbd_device *usbd_dev; #define RDRF (1 << 0) #define TDRE (1 << 1) + +static char input[256]; +static int input_index = 0; +static int input_processed_index = 0; + +void process_serial_input_byte(char b) { + input[input_index++] = b; + input_index %= sizeof(input); +} + + uint8_t read6850(uint16_t address) { switch(address & 1) { - case ACIAStatus: - return TDRE; // writable + case ACIAStatus: { + // Always writable + uint8_t flags = TDRE; + + // Readable if there is pending user input data which wasn't read + if (input_processed_index < input_index) flags |= RDRF; + + return flags; break; - case ACIAData: - // TODO: read buffer from serial port + } + case ACIAData: { + char data = input[input_processed_index++]; + input_processed_index %= sizeof(input); + + return data; break; + } default: break; } diff --git a/src/cdcacm.c b/src/cdcacm.c index b5491ee..8f6458e 100644 --- a/src/cdcacm.c +++ b/src/cdcacm.c @@ -189,7 +189,8 @@ void cdcacm_send_chunked_blocking(char *buf, int len, usbd_device *dev) { } while (bytes_remaining > 0); } -extern char *process_serial_command(char *buf, int len); +extern char *process_serial_command(char b); +extern void *process_serial_input_byte(char b); static void usbuart_usb_out_cb(usbd_device *dev, uint8_t ep) { @@ -198,9 +199,6 @@ static void usbuart_usb_out_cb(usbd_device *dev, uint8_t ep) char buf[CDCACM_PACKET_SIZE]; char reply_buf[256]; - static char typing_buf[2048] = {0}; - static int typing_index = 0; - int len = usbd_ep_read_packet(dev, CDCACM_UART_ENDPOINT, buf, CDCACM_PACKET_SIZE); @@ -214,21 +212,19 @@ static void usbuart_usb_out_cb(usbd_device *dev, uint8_t ep) if (buf[i] == '\r') reply_buf[j++] = '\n'; reply_buf[j++] = buf[i]; - typing_buf[typing_index++] = buf[i]; - - if (buf[i] == '\r' || buf[i] == '\n') { - char *response = process_serial_command(typing_buf, typing_index); - typing_index = 0; + char *response = process_serial_command(buf[i]); + if (response) { + reply_buf[j++] = '\r'; + reply_buf[j++] = '\n'; for (size_t k = 0; k < strlen(response); ++k) { reply_buf[j++] = response[k]; } - // prompt reply_buf[j++] = '\r'; reply_buf[j++] = '\n'; - reply_buf[j++] = '>'; - reply_buf[j++] = ' '; + } else { + process_serial_input_byte(buf[i]); } } diff --git a/src/main.c b/src/main.c index 2f30042..8ef8316 100644 --- a/src/main.c +++ b/src/main.c @@ -103,27 +103,25 @@ static void usb_reset() paused = false; } -char *process_serial_command(char *buf, int len) { - (void) len; - - if (buf[0] == 'v') { +char *process_serial_command(char b) { + if (b == '\x16') { // ^V return "Pill 6502 version " FIRMWARE_VERSION; - } else if (buf[0] == 'p') { + } else if (b == '\x10') { // ^P paused = !paused; return paused ? "paused" : "resumed"; - } else if (buf[0] == 'r') { + } else if (b == '\x12') { // ^R reset6502(); paused = false; return "reset"; - } else if (buf[0] == 't') { + } else if (b == '\x14') { // T static char buf[64]; snprintf(buf, sizeof(buf), "%ld ticks\r\n%ld instructions", clockticks6502, instructions); return buf; - } else { - return "invalid command, try ? for help"; + } else if (b == '\x07') { // G + return "^V=version ^R=reset ^P=pause ^T=ticks ^G=help"; } - return ""; + return NULL; } static void setup_clock(void) {