Skip to content

Instantly share code, notes, and snippets.

@grodtron
Created February 8, 2012 03:27
Show Gist options
  • Save grodtron/1765054 to your computer and use it in GitHub Desktop.
Save grodtron/1765054 to your computer and use it in GitHub Desktop.
Dissasembler for simplified version of motorolla 68000
#!/usr/bin/python
import optparse
def add(opcode,memory=None):
dest = ((opcode >> 9) & int( "111",2))
src = ((opcode >> 0) & int( "111",2))
mode = ((opcode >> 3) & int("111111",2))
destType = 'd' if mode == int("001000",2) else 'a'
srcType = 'd'
print "add\t%s%d,%s%d"%(srcType, src, destType, dest)
return True
def sub(opcode,memory=None):
dest = ((opcode >> 9) & int( "111",2))
src = ((opcode >> 0) & int( "111",2))
destType = 'd'
srcType = 'd'
print "sub\t%s%d,%s%d"%(srcType, src, destType, dest)
return True
def div(opcode,memory=None):
dest = ((opcode >> 9) & int( "111",2))
src = ((opcode >> 0) & int( "111",2))
destType = 'd'
srcType = 'd'
print "div\t%s%d,%s%d"%(srcType, src, destType, dest)
return True
def mul(opcode,memory=None):
dest = ((opcode >> 9) & int( "111",2))
src = ((opcode >> 0) & int( "111",2))
destType = 'd'
srcType = 'd'
print "mul\t%s%d,%s%d"%(srcType, src, destType, dest)
return True
def move(opcode, memory=None):
def makeOperand(mode, number):
if mode == 0: return "d%d" % number
if mode == 1: return "a%d" % number
if mode == 5: return "$%s(a%d)" % (hex(memory.next())[2:].upper().zfill(4), number)
return "ERR"
dest = ((opcode >> 9) & int( "111",2))
src = ((opcode >> 0) & int( "111",2))
destMode = ((opcode >> 6) & int( "111",2))
srcMode = ((opcode >> 3) & int( "111",2))
dest = makeOperand(destMode, dest)
src = makeOperand(srcMode, src)
operand = "movea" if destMode == 1 or srcMode == 1 else "move"
print "%s\t%s,%s"%(operand,src, dest)
return True
def branch(opcode, memory=None):
mode = (opcode >> 8) & int("1111",2)
mode = {0:"bra", 7:"beq", 14:"bgt", 13:"blt"}[mode]
displacement = hex(opcode & int("11111111",2))[2:].upper().zfill(2)
print "%s\t$%s" % (mode, displacement)
return True
def stop(opcode, memory=None):
mode = (opcode >> 9) & int("111",2)
if mode == 7:
print "stop"
return False
elif mode == 4:
print "swap\td%d" % (opcode & 7)
return True
def declareData(opcode, memory=None):
print "dc\t$%s" % hex(opcode)[2:].upper().zfill(4)
return False
opcodes = {
int("1101",2) << 12:add,
int("1001",2) << 12:sub,
int("1100",2) << 12:mul,
int("1000",2) << 12:div,
int("1011",2) << 12:"!comp",
int("0100",2) << 12:stop,
int("0011",2) << 12:move,
int("0110",2) << 12:branch
}
def main(memory):
# keep track of whether or not we've reached a stop instruction
inProgram = True
while 1:
try:
instruction = memory.next()
except StopIteration:# end of input
break
if inProgram:
try:
try:
inProgram = opcodes[instruction & (15 << 12)](instruction, memory)
except TypeError: # 'str' object is not callable
print opcodes[instruction & (15 << 12)]
except KeyError: # operation doesn't exist/is not known/is not implemented
print hex(instruction)
else:
declareData(instruction)
if __name__ == '__main__':
parser = optparse.OptionParser("usage: %prog [-b|--base<n>] [-a|--addresses] filename")
parser.add_option("-b", "--base",type="int", default=2)
parser.add_option("-a", "--addresses", action="store_true", default=False)
(options, args) = parser.parse_args()
base = options.base
addresses = options.addresses
if len(args) == 1:
filename = args[0]
memory = reduce(\
lambda x, y: x + y, \
(\
list(int(d,base) for d in c.split()[int(addresses):])\
for c in open(filename).read().splitlines() if len(c) > 0)\
)
main(iter(memory))
else:
print parser.get_usage()
; test output from the disassembler
sub d0,d0
movea d0,a1
movea a1,a0
move $4052(a1),d1
swap d0
movea $4050(a1),a0
move d1,d3
add d3,d3
move d3,d2
mul d0,d2
sub d3,d2
add d2,a0
sub d2,d2
sub d3,d2
div d3,d0
movea $403E(a0),a3
movea $403E(a1),a2
movea a3,$403E(a1)
movea a2,$403E(a0)
add d2,a0
add d3,a1
sub d1,d0
bgt $EC
move $4054(a0),d0
stop
dc $000A
dc $000B
dc $000C
dc $000D
dc $000E
dc $0000
dc $0001
dc $0002
dc $0003
dc $0009
dc $0001
dc $0003
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment