Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 53 additions & 10 deletions src/mos.c
Original file line number Diff line number Diff line change
Expand Up @@ -640,16 +640,59 @@ 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;
}

// 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;
}
Comment on lines +660 to +675
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is cool - belt & braces style

I had a wild thought on hex handling... it might be nice for hex values to automatically be variable/arbitrary length. so 0x1 or 0x12 would send a single byte, 0x123 or 0x1234 two bytes, 0x12345 or 0x123456 three bytes, and so on

you could just interpret the string one byte at a time. the only tricky part really is dealing with odd length strings, where the first character is a stand-alone nibble rather than a full byte character pair.


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
Expand Down