Skip to content

Fix broken 6502 test cases #213

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

Merged
merged 5 commits into from
Jul 9, 2025
Merged
Show file tree
Hide file tree
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
40 changes: 35 additions & 5 deletions libb/6502.b
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,30 @@ realloc(ptr, size) {
with the `divmod` test
*/
_div(a, b) {
auto d;
auto d, sign;
sign = 0;
if (a < 0) {
sign = !sign;
a = -a;
}
if (b < 0) {
sign = !sign;
b = -a;
}

d = 0; while(a >= b) {
a = a - b;
d++;
}
if (sign) d = -d;
return (d);
}
_udiv(a, b) {
auto d;
d = 0; while(a >= b | a < 0) {
a = a - b;
d++;
}
return (d);
}

Expand All @@ -58,18 +77,29 @@ _rem (a, b) {
}
return (a);
}
_urem(a, b) {
auto d;
while(a >= b | a < 0) {
a = a - b;
}
return (a);
}

printn(n, b, sign) {
auto a, c, d;
auto a, c, d, __div, __rem;

/* use correct div/rem based on sign */
__div = sign ? &_div : &_udiv;
__rem = sign ? &_rem : &_urem;

if (sign & n < 0) {
putchar('-');
n = -n;
}

if(a=_div(n, b)) /* assignment, not test for equality */
if(a=__div(n, b)) /* assignment, not test for equality */
printn(a, b, 0); /* recursive */
c = _rem(n,b) + '0';
c = __rem(n,b) + '0';
if (c > '9') c += 7;
putchar(c);
}
Expand Down Expand Up @@ -106,7 +136,7 @@ printf(str, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15) {
while (c = char(*arg, j++)) {
putchar(c);
}
} else if (c == 'z') { /* hack for %zu */
} else if ((c == 'z') | (c == 'l')) { /* hack for %zu %lu, % */
c = '%';
goto while_end;
} else {
Expand Down
8 changes: 4 additions & 4 deletions tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -830,14 +830,14 @@
"comment": ""
},
"uxn": {
"expected_stdout": "69\n1000000\n123456789987654321\n",
"expected_stdout": "69\n16960\n64177\n",
"state": "Enabled",
"comment": ""
"comment": "Since uxn word size is 16 we expect such big literals to get truncated accordingly. 1000000 == 0xf4240 -> 16960 == 0x4240. 123456789987654321 == 0x1b69b4be052fab1 -> 64177 == 0xfab1"
},
"6502": {
"expected_stdout": "69\r\n1000000\r\n123456789987654321\r\n",
"expected_stdout": "69\r\n16960\r\n64177\r\n",
"state": "Enabled",
"comment": ""
"comment": "Since 6502 word size is 16 we expect such big literals to get truncated accordingly. 1000000 == 0xf4240 -> 16960 == 0x4240. 123456789987654321 == 0x1b69b4be052fab1 -> 64177 == 0xfab1"
},
"gas-x86_64-darwin": {
"expected_stdout": "69\n1000000\n123456789987654321\n",
Expand Down