Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add semicolon and hex notation support to the MOS VDU command #118

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
64 changes: 54 additions & 10 deletions src/mos.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Choose a reason for hiding this comment

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

I think these 2 lines should be removed and let strtol() call below to deal with this prefix. Otherwise, the parsing code will allow input like 0x0xFE to be parsed as a valid number.

Also, the independent checks for prefix/suffix should probably be converted to sequence of if / elseif. The idea is to accept as valid only numbers with at most one prefix or suffix, not any combination of all possible prefixes/suffixes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed on those two lines, since strtol can handle 0x prefixes there's no need.

We discussed the second point and it costs us nothing to deal with situations where a user may double up on notation.

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