diff --git a/idasm/README b/idasm/README index ca9c08b..5fbe635 100755 --- a/idasm/README +++ b/idasm/README @@ -1 +1,8 @@ -This program is a smart, interactive disassembler for IMLAC binary files. +This program is a smart, interactive disassembler for IMLAC binary files, +papertape or TTY. + +It is designed to allow the user to read in a *.ptp or *.tty file, which +may then be interacted with: +. change MAIN to DISPLAY instruction set and vice versa +. add labels where appropriate +. change DATA to ASCII pseudo-ops and vice versa diff --git a/idasm/binimport.py b/idasm/binimport.py index 9477886..3c1fa04 100755 --- a/idasm/binimport.py +++ b/idasm/binimport.py @@ -1,7 +1,8 @@ #!/usr/bin/env python +# -*- coding: utf-8 -*- """ -Import an imlac binary file +Import an imlac binary file. """ import sys @@ -11,89 +12,125 @@ import mem import disasmdata -offset = 0 +# address where next word will be loaded into memory +Dot = 0 -def doblockloader(f, word): - ldaddr = 0177700 - numwords = 0100 - 1 # have already read one word in +# dictionary of memory 'chunks' +# {: [word1, word2, ...], ...} +Memory = {} + +# size of memory configured +MemSize = 04000 + +# size of block loader +LoaderSize = 0100 + + +def doblockloader(f, word, mymem): + """Read block loader into high memory. + + f file handle to read from + word the word we have already read + mymem Mem() object to store loader in + """ + + ldaddr = MemSize - LoaderSize + numwords = LoaderSize - 1 # have already read one word in + mymem.add(ldaddr, word) while numwords > 0: word = readword(f) + print('doblockloader: top of loop, numwords=%d, word=%06o' % (numwords, word)) + mymem.add(ldaddr, word) + ldaddr += 1 numwords = numwords - 1 - ldaddr = ldaddr + 1 -def dobody(f, count): - mymem = mem.Mem() + print('doblockloader: mymem.memory=%s' % str(mymem.memory)) - numwords = count +def dobody(f, mymem): + """Read all of file after block loader. + + f input file handle + mymem the mem.Mem() dict to store data in + + Returns an updated mem.Mem() object containing the input code. + """ + + numwords = skipzeros(f) while True: + # negative load address is end-of-file ldaddr = readword(f) if ldaddr & 0x8000: break + + # read data block, calculating checksum csum = 0 + #csum = ldaddr while numwords > 0: word = readword(f) csum += word -# if csum > 0xffff: -# csum += 1 -# csum = csum & 0xffff + csum &= 0xffff mymem.add(ldaddr, word) (op, fld) = disasmdata.disasmdata(word) mymem.putOp(ldaddr, op) mymem.putFld(ldaddr, fld) ldaddr += 1 numwords -= 1 - csum &= 0xffff + #csum &= 0xffff checksum = readword(f) - if csum != checksum: - print "Checksum error" - #sys.exit(20) +# if csum != checksum: +# print "Checksum error" +# return None numwords = skipzeros(f) + return mymem - def ptpimport(file): - global offset + global Dot try: f = open(file, "rb") - except IOError, e: + except IOError as e: print e return 3 - offset = 0 + # create Mem() object to store data + mymem = mem.Mem() + print('str(dir(mymem))=%s' % str(dir(mymem))) + + Dot = 0 + + # find and read the block loader byte = skipzeros(f) word = (byte << 8) + readbyte(f) - doblockloader(f, word) + doblockloader(f, word, mymem) - count = skipzeros(f) - mymem = dobody(f, count) + # now read all the data blocks + mymem = dobody(f, mymem) return mymem - def readbyte(f): - global offset + global Dot + try: byte = f.read(1) - except IOError, e: + except IOError as e: print e sys.exit(10) - except EOFError, e: + except EOFError as e: print e sys.exit(11) - offset += 1 + Dot += 1 if len(byte) > 0: val = struct.unpack("B", byte) return val[0] else: return 0 - def readword(f): return (readbyte(f) << 8) + readbyte(f) - def skipzeros(f): while True: val = readbyte(f) @@ -102,7 +139,11 @@ def skipzeros(f): if __name__ == "__main__": - themem = ptpimport("keybrd.ptp") + themem = ptpimport('40tp_simpleDisplay.ptp') + if themem is None: + print('Error reading input file.') + sys.exit(10) + print('str(dir(themem))=%s' % str(dir(themem))) addrlist = themem.keys() addrlist.sort() diff --git a/idasm/disasmd.py b/idasm/disasmd.py index 32436d0..5c39b8d 100755 --- a/idasm/disasmd.py +++ b/idasm/disasmd.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +# -*- coding: utf-8 -*- """ This module disassembles one imlac machine word into a display processor diff --git a/idasm/disasmdata.py b/idasm/disasmdata.py index 47686e5..ab464a9 100755 --- a/idasm/disasmdata.py +++ b/idasm/disasmdata.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +# -*- coding: utf-8 -*- """ This module disassembles one imlac machine word into a data instruction diff --git a/idasm/disasmm.py b/idasm/disasmm.py index 651600d..0606497 100755 --- a/idasm/disasmm.py +++ b/idasm/disasmm.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +# -*- coding: utf-8 -*- """ This module disassembles one imlac machine word into a main processor diff --git a/idasm/mem.py b/idasm/mem.py index b715651..e2025b1 100755 --- a/idasm/mem.py +++ b/idasm/mem.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +# -*- coding: utf-8 -*- """ This module implements the 'mem' object and access routines. @@ -31,10 +32,10 @@ class Mem(object): def add(self, address, code): self.putMem(address, (code, "", "", 0, False, None, 0)) - + def len(self): return len(self.memory) - + def getMem(self, address): addrstr = "%05o" % address result = self.memory.get(addrstr, None) @@ -42,96 +43,96 @@ class Mem(object): result = (0, "", "", 0, False, None, 0) self.putMem(address, result) return result - + def getCode(self, address): (code, _, _, _, _, _, _) = self.getMem(address) return code - + def getOp(self, address): (_, op, _, _, _, _, _) = self.getMem(address) return op - + def getFld(self, address): (_, _, fld, _, _, _, _) = self.getMem(address) return fld - + def getLabcount(self, address): (_, _, _, labcount, _, _, _) = self.getMem(address) return labcount - + def getRef(self, address): (_, _, _, _, ref, _, _) = self.getMem(address) return ref - + def getType(self, address): (_, _, _, _, _, type, _) = self.getMem(address) return type - + def getCycle(self, address): (_, _, _, _, _, _, cycle) = self.getMem(address) return cycle - + def putMem(self, address, entry): addrstr = "%05o" % address self.memory[addrstr] = entry - + def putCode(self, address, code): (_, op, fld, labcount, ref, type, cycle) = self.getMem(address) self.putMem(address, (code, op, fld, labcount, ref, type, cycle)) - + def putOp(self, address, op): (code, _, fld, labcount, ref, type, cycle) = self.getMem(address) self.putMem(address, (code, op, fld, labcount, ref, type, cycle)) - + def putFld(self, address, fld): (code, op, _, labcount, ref, type, cycle) = self.getMem(address) self.putMem(address, (code, op, fld, labcount, ref, type, cycle)) - + def putLabcount(self, address, labcount): (code, op, fld, _, ref, type, cycle) = self.getMem(address) self.putMem(address, (code, op, fld, labcount, ref, type, cycle)) - + def putRef(self, address, ref): (code, op, fld, labcount, _, type, cycle) = self.getMem(address) self.putMem(address, (code, op, fld, labcount, ref, type, cycle)) - + def putType(self, address, type): (code, op, fld, labcount, ref, _, cycle) = self.getMem(address) self.putMem(address, (code, op, fld, labcount, ref, type, cycle)) - + def putCycle(self, address, cycle): (code, op, fld, labcount, ref, type, _) = self.getMem(address) self.putMem(address, (code, op, fld, labcount, ref, type, cycle)) - + def decLab(self, address): (code, op, fld, labcount, ref, type, cycle) = self.getMem(address) labcount -= 1 self.putMem(address, (code, op, fld, labcount, ref, type, cycle)) - + def incLab(self, address): (code, op, fld, labcount, ref, type, cycle) = self.getMem(address) labcount += 1 self.putMem(address, (code, op, fld, labcount, ref, type, cycle)) - + def clearRef(self, address): (code, op, fld, labcount, _, type, cycle) = self.getMem(address) self.putMem(address, (code, op, fld, labcount, False, type, cycle)) - + def setRef(self, address): (code, op, fld, labcount, _, type, cycle) = self.getMem(address) self.putMem(address, (code, op, fld, labcount, True, type, cycle)) - + def keys(self): return self.memory.keys() def clearUndo(self): self.undo = None - + def setUndo(self): self.undo = self.memory.copy() - + def undoX(self): tmp = self.memory self.memory = self.undo self.undo = tmp - + diff --git a/idasm/processdata.py b/idasm/processdata.py index 66cabd4..5cc75b0 100755 --- a/idasm/processdata.py +++ b/idasm/processdata.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +# -*- coding: utf-8 -*- """ This module takes one word value and processes it as a DATA word. diff --git a/idasm/processdisplay.py b/idasm/processdisplay.py index bd95d16..3655728 100755 --- a/idasm/processdisplay.py +++ b/idasm/processdisplay.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +# -*- coding: utf-8 -*- """ This module takes a memory dictionary of the form: diff --git a/idasm/processmain.py b/idasm/processmain.py index 41b20ae..c64a314 100755 --- a/idasm/processmain.py +++ b/idasm/processmain.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +# -*- coding: utf-8 -*- """ Process main instructions from a start address.