123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121(**************************************************************************)(* This file is part of BINSEC. *)(* *)(* Copyright (C) 2016-2026 *)(* CEA (Commissariat à l'énergie atomique et aux énergies *)(* alternatives) *)(* *)(* you can redistribute it and/or modify it under the terms of the GNU *)(* Lesser General Public License as published by the Free Software *)(* Foundation, version 2.1. *)(* *)(* It is distributed in the hope that it will be useful, *)(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *)(* GNU Lesser General Public License for more details. *)(* *)(* See the GNU Lesser General Public License version 2.1 *)(* for more details (enclosed in the file licenses/LGPLv2.1). *)(* *)(**************************************************************************)openBasic_types.IntegersopenLoader_typestypeheader={name:string;comment:string}moduleSection=structtypet={offset:int;size:int}typeheader=unitletname_="RAM"letpos{offset;_}={virt=Virtual_address.create0x9d93;raw=offset}letsize{size;_}={virt=Z.of_intsize;raw=size}letheader_=()lethas_flag__=trueendmoduleSymbol=structtypet=unittypeheader=unitletname_=assertfalseletvalue_=assertfalseletheader_=()endmoduleImg=structtypet={header:header;ram:Section.t;buf:buffer}typenonrecheader=headerletarch_=Machine.z80letentry_=Virtual_address.create0x9d95letsections{ram;_}=[|ram|]letsymbols_=[||]letheader{header;_}=headerletcursor?at{buf;_}=Reader.of_bigarray?pos:atbufletcontent{buf;_}Section.{offset;size;_}=Bigarray.Array1.subbufoffsetsizeletbuffer{buf;_}=bufletppppf{header={name;comment};_}=Format.fprintfppf"@[<v>TI-83 Plus compiled assembly program %s@ %S@]"namecommentendletcheck_magicbuf=(not(Bigarray.Array1.dimbuf<11))&&buf.{0}=Char.code'*'&&buf.{1}=Char.code'*'&&buf.{2}=Char.code'T'&&buf.{3}=Char.code'I'&&buf.{4}=Char.code'8'&&buf.{5}=Char.code'3'&&buf.{6}=Char.code'F'&&buf.{7}=Char.code'*'&&buf.{8}=0x1a&&buf.{9}=0x0a&&buf.{10}=0x00letloadbuf=letcursor=Reader.of_bigarray~pos:0x0bbufinletcomment=Reader.Peek.zero_string""cursor~maxlen:42()inReader.advancecursor0x2a;ignore(Reader.Read.u16cursor);ifUint16.to_int(Reader.Read.u16cursor)<>0x0dtheninvalid_arg"Invalid format";ignore(Reader.Read.u16cursor);ifUint8.to_int(Reader.Read.u8cursor)<>0x06theninvalid_arg"Invalid format";letname=Reader.Peek.zero_string""cursor~maxlen:10()inReader.advancecursor0x0a;ignore(Reader.Read.u16cursor);letsize=Uint16.to_int(Reader.Read.u16cursor)inletoffset=Reader.get_poscursorinifUint8.to_int(Reader.Peek.u8cursor)<>0xbbtheninvalid_arg"Invalid format";Img.{header={name;comment};ram={offset;size};buf}letload_file_descrfile_descr=letbuffer=Bigarray.(array1_of_genarray(Unix.map_filefile_descrInt8_unsignedC_layoutfalse[|-1|]))inloadbufferletload_filepath=letfile_descr=Unix.openfilepath[Unix.O_RDONLY]0inletimg=load_file_descrfile_descrinUnix.closefile_descr;imgletread_offsetImg.{buf;_}offset=Int.unsafe_to_uint8buf.{offset}letread_addressImg.{ram={offset;size;_};buf;_}addr=letaddr=Virtual_address.to_intaddrinifaddr<0x9d93||0x9d93+size<=addrtheninvalid_arg(Format.sprintf"Unreachable virtual address %04x"addr)elseInt.unsafe_to_uint8buf.{addr-0x9d93+offset}