MEGA65 / mega65-rom-public

MEGA65 ROM public issue reporting
4 stars 0 forks source link

Support byte variables B& in FOR loops #145

Closed dansanderson closed 4 months ago

dansanderson commented 4 months ago

For completeness, support byte variables in FOR statements, and limit the value range with Illegal Quantity Error similar to integers.

dansanderson commented 4 months ago

More polish: We should type-check the STEP parameter and only allow integer steps in these cases. Right now you can provide a fractional STEP to an integer variable and it'll never terminate.

dansanderson commented 4 months ago

FOR currently disallows byte-type index variables. Clearing bit 5 of subflg at the top of for: disables the Type Mismatch Error, but more is needed for the variable to behave correctly within the body of the loop.

FOR has separate set-up behavior for integer index variables. I enhanced the test of intflg to branch to forint: for both integer and byte types. This improves (but does not completely fix) the index variable behavior in the loop, but attempts to add the STEP value and use its memory for comparison purposes treat it as a 16-bit integer, which corrupts memory and misbehaves.

The stack record uses the STEP exponent = 0 to indicate an integer type. There is currently no distinction between an integer and a byte in this record, and one is needed to prevent next:50$ from writing two bytes to byte variable memory. Integer values (variable, TO, STEP) appear to be treated as the low 16 bits of the FAC throughout this routine, so extending this routine to simply truncate at 8 bits for byte indexes should be a complete solution.