Advertisement
beckadam

TI84 Varable File Maker

May 15th, 2019
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.89 KB | None | 0 0
  1. import sys
  2.  
  3. class Var_struct:
  4.     def __init__(self,name,vt):
  5.         self.header = [0x2A, 0x2A, 0x54, 0x49, 0x38, 0x33, 0x46, 0x2A,
  6.                          #**TI83F*
  7.                          0x1A, 0x0A, 0x00,
  8.                          #signature
  9.                          0x41, 0x70, 0x70, 0x56, 0x61, 0x72, 0x69, 0x61,
  10.                          #comment area
  11.                          0x62, 0x6c, 0x65, 0x20, 0x66, 0x69, 0x6c, 0x65,
  12.                          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  13.                          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  14.                          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  15.                          0x00, 0x00,
  16.                          0x00, 0x00]
  17.                              #data size
  18.         self.varheader = [0x0D, 0x00,
  19.                             0x00, 0x00,
  20.                             #length of variable in bytes
  21.                             vt,
  22.                             #variable type ID. 0x15 is for appvar, 0x06 is for locked prgm
  23.                             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  24.                             #variable name (max 8 characters)
  25.                             0x00,
  26.                             #version
  27.                             0x80,
  28.                             #flag. 80h for archived variable. 00h otherwise
  29.                             0x00, 0x00,
  30.                             #length of variable in bytes (copy)
  31.                             0x00, 0x00]
  32.                             #length of variable in bytes (another copy...)
  33.         self.checksum = -1
  34.         if len(name)>8:
  35.             self.name = name[0:7]
  36.         elif len(name)==8:
  37.             self.name = name
  38.         else:
  39.             self.name = name + "\x00"*(8-len(name))
  40.  
  41.         for i in range(8):
  42.             self.varheader[i+5]=ord(self.name[i])
  43.  
  44.     def update(self,data=[]):
  45.         if len(data)>0xFFEC:
  46.             print("Error: Converted data too large!")
  47.             return
  48.         self.header[53]=(len(data)+19)%256
  49.         self.header[54]=(len(data)+19)//256
  50.         dl=len(data)
  51.         self.varheader[2]=(dl+2)%256
  52.         self.varheader[3]=(dl+2)//256
  53.         self.varheader[15]=(dl+2)%256
  54.         self.varheader[16]=(dl+2)//256
  55.         self.varheader[17]=dl%256
  56.         self.varheader[18]=dl//256
  57.         self.data = self.header + self.varheader + data + [0,0]
  58.         self.checksum=sum(self.varheader)+sum(data)
  59.         self.data[-2]=self.checksum%256
  60.         self.data[-1]=self.checksum//256
  61.         for i in range(len(self.data)):
  62.             self.data[i]%=256
  63.  
  64.     def write(self,file):
  65.         try:
  66.             with open(file,"wb") as f:
  67.                 f.write(bytes(self.data))
  68.             return True
  69.         except:
  70.             print("Something went wrong writing!")
  71.             return False
  72.  
  73.  
  74. if __name__=='__main__':
  75.     try:
  76.         file_name = sys.argv[1]
  77.     except:
  78.         file_name = input("File to convert?")
  79.     try:
  80.         with open(file_name,"rb") as f:
  81.             data_in = list(f.read())
  82.     except:
  83.         print("Error: File does not exist!")
  84.         exit()
  85.  
  86.     try:
  87.         calc_name = sys.argv[4]
  88.     except:
  89.         calc_name = input("Name on calc?")
  90.  
  91.     try:
  92.         vartype = str(sys.argv[3]).lower()
  93.     except:
  94.         vartype = input("variable type?")
  95.  
  96.     try:
  97.         vartype = int(vartype)%256
  98.     except:
  99.         if "prot" in vartype:
  100.             vartype=6
  101.         elif "prgm" in vartype:
  102.             vartype=5
  103.         elif "appvar" in vartype or "avar" in vartype:
  104.             vartype=21
  105.         else:
  106.             print("""Error: invalid variable type!
  107. defaulting to locked program...""")
  108.             vartype=6
  109.  
  110.     calc_var = Var_struct(calc_name,vartype)
  111.     calc_var.update(data_in)
  112.     try:
  113.         comp_name = sys.argv[2]
  114.     except:
  115.         comp_name = input("File name?")
  116.  
  117.     calc_var.write(comp_name)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement