Casio CAS format conversion scripts for legacy CFX
Posté le 24/10/2022 02:14
I've written Python 3 scripts to convert Casio Basic programs to/from the Casio CAS format. This is meant to be used on extremely old CFX calculators from the 1990's such as the CFX-9800G. CAS files are expected/generated when transferring programs to/from the calculator using the Casio FA-121 software.
Why create these scripts when we can already view and edit CAS files in the FA-121 software? Because that editor is from 1994 and does not support newer CFX opcodes such as color commands. Additionally, writing code in Windows with a white background is not my preference (although in all honesty I probably would have put up with it). I'm glad I wrote these as I can now develop my programs in Linux using a text editor of my choice, and I can even build the CAS files for my project using a simple Makefile.
For these scripts to be applicable, your calculator's
ALPHA key must be to the
right of the
SHIFT key:
⚠ If your
ALPHA key is below the
SHIFT key, your calculator is
too new for these scripts. Behavior is undefined.
⚠ There are other types of CAS files (Editor, Function Mem, Matrix, etc), but these scripts only handle the Program type. Behavior when used on non-Program CAS files is undefined.
Convert Program from CAS to TXT
cas2txt FILENAME.cas >FILENAME.txt
A UTF-8 text file will be created containing the program's contents.
⚠ If an opcode is not understood it will be output inside curly braces. Example:
{0xB0}
Please let me know if you encounter one of these and also what you see on your calculator.
Convert Program from TXT to CAS
txt2cas FILENAME.txt >FILENAME.cas
A Casio CAS file will be created containing the program's contents.
Newline Handling
In a Casio Basic program the ↲ character visually depicts a new line. This might make sense on the small calculator screen, but on a PC, it just adds visual clutter. Therefore, the ↲ character is not included in files generated by cas2txt. The ↲ character is also ignored, if present, when running txt2cas. Instead, each line is assumed to have a newline after it by txt2cas, unless the line ends with ◢ in which case the newline is ignored.
Text Interpretation in txt2cas
There isn't a 1:1 mapping between Casio opcode textual representation and Unicode characters, and some are even visually ambiguous. Pop quiz: which of these is Micro, and which is Mu: µ or μ? And, some combining-mark characters such as x̄ and x̂ are not supported by all editors or display poorly. Plus, it's annoying to have to constantly copy/paste Unicode characters when trying to write a program. Therefore, a plain text representation is provided for almost all opcodes.
When possible, the plain text representations from
https://bible.planet-casio.com/simlo/chm/v20/fxCG20_OPCODES.htm have been implemented. However, that page documents opcodes for much newer calculators than are targeted by these scripts. As a result, some of the legacy CFX opcodes are not defined in that document, some have changed meaning, and a great number of additional opcodes are present there. That said, that page is a good reference for pretty much every opcode you'll encounter when writing a program.
Unicode representations that match those generated by cas2txt are provided. Some alternate plain text representations are also provided, as well as lowercase versions of those whenever possible (notable exceptions: Deg, Gra, and Rad, as their lowercase forms needed to be preserved for plain text alternates to °, ᵍ, and ʳ.
To find a text representation for a certain opcode, use your calculator to create a program containing that opcode and use cas2txt. If it provided a Unicode representation, search txt2cas to see if there is a plain text representation.
Other interpretation features and notes:
• Some special representations are available: ^2 for ², ^-1 for ⁻¹, and 10^ for ⏨. These are aware of the presence of surrounding digits, so for example ^20 will remain as ^20 and won't be corrupted into ²0.
• Whitespace (spaces, tabs, etc) will be stripped from the beginning and end of each line. This means you can indent your code (loops, etc) without it affecting the CAS file. Adding some simple indentation improves readability dramatically.
• Spaces outside of comments and strings are removed. Thus, you can write lines such as:
I < 136 => Goto 3
• Blank lines are removed, so you can break your code up into logical sections.
• Doubling the comment character produces an internal-only comment that won't be written to the CAS file. This is useful for documenting your program without bloating the CAS size. These internal comments can also be placed at the end of a line. Examples:
'' Shuffle deck
Z[I] -> Z[13I-540] '' 45+13*(I-45)
• The character "-" always produces a subtraction opcode. However, I've found that's usually adequate as the calculator tolerates using subtraction in nearly all situations. These definitely work: -5, -(2+3), 2--3. However, if necessary, you can use (-) or the Unicode ⁻ to force a negative sign.
• Any final newline in the file is omitted.
Download here:
cas-1.01.tar.gz