Skip to content

Commit

Permalink
Add new program subtypes for ASM
Browse files Browse the repository at this point in the history
  • Loading branch information
kg583 committed Sep 23, 2023
1 parent 2000548 commit 96e440f
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 2 deletions.
1 change: 1 addition & 0 deletions tests/tivars.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ def test_all_tokens(self):

self.assertEqual(test_program.bytes(), file.read()[:-2])
self.assertEqual(test_program.is_protected, False)
self.assertEqual(test_program.is_tokenized, True)

def test_protection(self):
test_program = TIProtectedProgram(name="SETDATE")
Expand Down
3 changes: 2 additions & 1 deletion tivars/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
__all__ = ["TIEntry",
"TIReal",
"TIRealList", "TIMatrix",
"TIEquation", "TIString", "TIProgram", "TIProtectedProgram",
"TIEquation", "TIString",
"TIProgram", "TIAsmProgram", "TIProtectedProgram", "TIProtectedAsmProgram",
"TIPicture", "TIMonoPicture",
"TIMonoGDB", "TIGDB", "TIGraphedEquation",
"TIMonoFuncGDB", "TIMonoParamGDB", "TIMonoPolarGDB", "TIMonoSeqGDB",
Expand Down
87 changes: 86 additions & 1 deletion tivars/types/tokenized.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,18 @@ class TIProgram(TokenizedEntry, register=True):
Whether this program type is protected
"""

is_tokenized = True
"""
Whether this program is tokenized
"""

asm_tokens = {b'\xBB\x6D': TI_83P,
b'\xEF\x69': TI_84PCSE,
b'\xEF\x7B': TI_84PCE}
"""
Tokens which identify the program as containing assembly code
"""

_type_id = 0x05

@Section(8, TokenizedString)
Expand Down Expand Up @@ -331,6 +343,65 @@ def unprotect(self):
self.type_id = 0x05
self.coerce()

@Loader[ByteString, BytesIO]
def load_bytes(self, data: bytes | BytesIO):
super(TokenizedEntry, self).load_bytes(data)

try:
if self.version != (version := self.get_version()):
warn(f"The version is incorrect (expected 0x{version:02x}, got 0x{self.version:02x}).",
BytesWarning)

except ValueError as e:
if self.is_tokenized:
warn(f"The file contains an invalid token {' '.join(str(e).split()[2:])}.",
BytesWarning)

def coerce(self):
match self.type_id, any(token in self.data for token in self.asm_tokens):
case 0x05, False:
self.__class__ = TIProgram
case 0x05, True:
self.__class__ = TIAsmProgram
case 0x06, False:
self.__class__ = TIProtectedProgram
case 0x06, True:
self.__class__ = TIProtectedAsmProgram


class TIAsmProgram(TIProgram):
"""
Parser for assembly programs
A `TIAsmProgram` is a stream of raw bytes that is run as assembly code.
A single valid token at the start of the data section identifies the program as using assembly.
"""

is_tokenized = False

def __format__(self, format_spec: str) -> str:
try:
match [*format_spec]:
case sep, *width if width:
return self.data[2:].hex(sep, int(''.join(width)))

case sep, *_:
return self.data[2:].hex(sep)

case _:
return self.data[2:].hex()

except TypeError:
return super().__format__(format_spec)

def get_min_os(self, data: bytes = None) -> OsVersion:
return max([model.OS() for token, model in self.asm_tokens.items() if token in (data or self.data)],
default=super().get_min_os(data))

@Loader[str]
def load_string(self, string: str, *, model: TIModel = None, lang: str = None):
raise NotImplementedError


class TIProtectedProgram(TIProgram, register=True):
"""
Expand All @@ -344,4 +415,18 @@ class TIProtectedProgram(TIProgram, register=True):
_type_id = 0x06


__all__ = ["TIEquation", "TINewEquation", "TIString", "TIProgram", "TIProtectedProgram"]
class TIProtectedAsmProgram(TIAsmProgram, TIProtectedProgram):
"""
Parser for protected programs
A `TIProtectedAsmProgram` is a stream of raw bytes that is run as assembly code with protection against editing.
A single valid token at the start of the data section identifies the program as using assembly.
"""

is_protected = True

_type_id = 0x06


__all__ = ["TIEquation", "TINewEquation", "TIString",
"TIProgram", "TIAsmProgram", "TIProtectedProgram", "TIProtectedAsmProgram"]

0 comments on commit 96e440f

Please sign in to comment.