From f15d836004e9ed00a0d6b4e8ad6eef5a64f36c5e Mon Sep 17 00:00:00 2001 From: HeathenUK Date: Tue, 21 Nov 2023 10:05:55 +0000 Subject: [PATCH 1/2] Add semicolon and hex notation support to VDU command. Conscious decision to not support comma (rather than whitespace) delimited values, as this would increase code complexity and depth exponentially given the potential for whitespace, comma and semicolon to all act as a delimiter. --- src/mos.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 54 insertions(+), 10 deletions(-) diff --git a/src/mos.c b/src/mos.c index ddc74b9..990d806 100644 --- a/src/mos.c +++ b/src/mos.c @@ -640,16 +640,60 @@ int mos_cmdSET(char * ptr) { // Returns: // - MOS error code // -int mos_cmdVDU(char *ptr) { - UINT24 value; - - while(mos_parseNumber(NULL, &value)) { - if(value > 255) { - return 19; // Bad Parameter - } - putch(value); - } - return 0; +int mos_cmdVDU(char *ptr) { + char *value_str; + UINT24 value = 0; + + while (mos_parseString(NULL, &value_str)) { + UINT8 isLong = 0; + UINT8 base = 10; + char *endPtr; + size_t len = strlen(value_str); + + //Strip semicolon notation and set as Long + if (len > 0 && value_str[len - 1] == ';') { + value_str[len - 1] = '\0'; + len--; + isLong = 1; + } + + // Check for '0x' or '0X' prefix + if (len > 2 && (value_str[0] == '0' && tolower(value_str[1] == 'x'))) { + base = 16; + value_str += 2; + len -= 2; + } + // Check for '&' prefix + if (value_str[0] == '&') { + base = 16; + value_str++; + len--; + } + // Check for 'h' suffix + if (len > 0 && tolower(value_str[len - 1]) == 'h') { + value_str[len - 1] = '\0'; + base = 16; + } + + value = strtol(value_str, &endPtr, base); + + if (*endPtr != '\0' || value > 65535) { + return 19; + } + + if (value > 255) { + isLong = 1; + } + + if (isLong) { + putch(value & 0xFF); // write LSB + putch(value >> 8); // write MSB + } else { + putch(value); + } + } + + return 0; } // TIME From 5e73f2afe905d2ba71e51bb6b8d34989d29f2c1a Mon Sep 17 00:00:00 2001 From: HeathenUK Date: Tue, 21 Nov 2023 17:01:08 +0000 Subject: [PATCH 2/2] Don't try to strip 0x notation since strtol is designed to cope with it. --- src/mos.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/mos.c b/src/mos.c index 990d806..751be60 100644 --- a/src/mos.c +++ b/src/mos.c @@ -660,9 +660,8 @@ int mos_cmdVDU(char *ptr) { // Check for '0x' or '0X' prefix if (len > 2 && (value_str[0] == '0' && tolower(value_str[1] == 'x'))) { base = 16; - value_str += 2; - len -= 2; } + // Check for '&' prefix if (value_str[0] == '&') { base = 16;