def returnMode(line,variables): if(line['length'] > 0): # take the labels out of lines if (line['data'][0].endswith(':')): line['length'] -= 1 line['data'] = line['data'][1:] # only process lines that aren't just labels or variables if(line['length'] > 0 and line['data'][0].lower() != 'define'): opcode = line['data'][0].upper() address = 'null' if (line['length']) == 2: address = line['data'][1] # replace variables with their values for variable in variables.keys(): if variable in address: address = address.replace(variable, variables[variable]) if (opcode in opcodes.keys()): if (line['length'] == 1): return 'SNGL' elif (line['length'] == 2): # immediate if(address.startswith('#')): return 'IMM' elif (address.startswith('(')): # indexed indirect if address[4] == ',': return 'INDX' # indirect index elif address[5] == ',': return 'INDY' # indirect else: return 'IND' elif (address.startswith('$')): operandLength = len(address) # absolute X and Y if (operandLength == 7): mode = 'ABS' + address[6].upper() return mode # zero page elif (operandLength == 3): return 'ZP' # zero page X and Y elif (address[3] == ','): mode = 'ZP' + address[4].upper() return mode # absolute else: return 'ABS' elif (opcode[0] == "J"): return 'ABS' elif (opcode in ['BCC','BCS','BEQ','BMI','BNE','BPL','BVC','BVS']): return 'BRA' else: raise ValueError('Problem with address in this line: ',line) else: raise ValueError('Invalid line length') else: raise ValueError('No opcode found')
def returnMode(line, variables): if (line['length'] > 0): # take the labels out of lines if (line['data'][0].endswith(':')): line['length'] -= 1 line['data'] = line['data'][1:] # only process lines that aren't just labels or variables if (line['length'] > 0 and line['data'][0].lower() != 'define'): opcode = line['data'][0].upper() address = 'null' if (line['length']) == 2: address = line['data'][1] # replace variables with their values for variable in variables.keys(): if variable in address: address = address.replace(variable, variables[variable]) if (opcode in opcodes.keys()): if (line['length'] == 1): return 'SNGL' elif (line['length'] == 2): # immediate if (address.startswith('#')): return 'IMM' elif (address.startswith('(')): # indexed indirect if address[4] == ',': return 'INDX' # indirect index elif address[5] == ',': return 'INDY' # indirect else: return 'IND' elif (address.startswith('$')): operandLength = len(address) # absolute X and Y if (operandLength == 7): mode = 'ABS' + address[6].upper() return mode # zero page elif (operandLength == 3): return 'ZP' # zero page X and Y elif (address[3] == ','): mode = 'ZP' + address[4].upper() return mode # absolute else: return 'ABS' elif (opcode[0] == "J"): return 'ABS' elif (opcode in [ 'BCC', 'BCS', 'BEQ', 'BMI', 'BNE', 'BPL', 'BVC', 'BVS' ]): return 'BRA' else: raise ValueError('Problem with address in this line: ', line) else: raise ValueError('Invalid line length') else: raise ValueError('No opcode found')
def getHexFromLines(parsedLines,labels,variables): print("Labels:", labels) print("Variables:", variables) hexOutput = "" for line in parsedLines: if(line['length'] > 0): # take the labels out of lines if (line['data'][0].endswith(':')): line['length'] -= 1 line['data'] = line['data'][1:] # only process lines that aren't just labels or variables if(line['length'] > 0 and line['data'][0].lower() != 'define'): opcode = line['data'][0].upper() address = 'null' if (line['length']) == 2: address = line['data'][1] # replace variables with their values for variable in variables.keys(): if variable in address: address = address.replace(variable, variables[variable]) print("Opcode", opcode, "Address", address) if (opcode in opcodes.keys()): # implicit if (line['length'] == 1): print((opcodes[opcode]['SNGL']) + ' ') hexOutput += (opcodes[opcode]['SNGL']) + ' ' elif (line['length'] == 2): # immediate if(address.startswith('#')): if(address[1] == '$'): print(opcodes[opcode]['IMM'] + ' ' + padHex(address[2:])) hexOutput += opcodes[opcode]['IMM'] + ' ' + padHex(address[2:]) else: print(opcodes[opcode]['IMM'] + ' ' + padHex(address[1:])) hexOutput += opcodes[opcode]['IMM'] + ' ' + padHex(address[1:]) elif (address.startswith('(')): # indexed indirect if address[4] == ',': print(opcodes[opcode]['INDX'] + ' ' + address[2:4] + ' ') hexOutput += opcodes[opcode]['INDX'] + ' ' + address[2:4] + ' ' # indirect index elif address[5] == ',': print(opcodes[opcode]['INDY'] + ' ' + address[2:4] + ' ') hexOutput += opcodes[opcode]['INDY'] + ' ' + address[2:4] + ' ' # indirect else: print(opcodes[opcode]['IND'] + ' ' + address[4:6] + ' ' + address[2:4] + ' ') hexOutput += opcodes[opcode]['IND'] + ' ' + address[4:6] + ' ' + address[2:4] + ' ' elif (address.startswith('$')): operandLength = len(address) # absolute X and Y if (operandLength == 7): mode = 'ABS' + address[6].upper() print(opcodes[opcode][mode] + ' ' + address[3:5] + ' ' + address[1:3] + ' ') hexOutput += opcodes[opcode][mode] + ' ' + address[3:5] + ' ' + address[1:3] + ' ' # zero page elif (operandLength == 3): print(opcodes[opcode]['ZP'] + ' ' + padHex(address[1:])) hexOutput += opcodes[opcode]['ZP'] + ' ' + padHex(address[1:]) # zero page X and Y elif (address[3] == ','): mode = 'ZP' + address[4].upper() print(opcodes[opcode][mode] + ' ' + address[1:3] + ' ') hexOutput += opcodes[opcode][mode] + ' ' + address[1:3] + ' ' # absolute else: print(opcodes[opcode]['ABS'] + ' ' + padHex(address[3:]) + address[1:3] + ' ') hexOutput += opcodes[opcode]['ABS'] + ' ' + padHex(address[3:]) + address[1:3] + ' ' elif (address in labels.keys()): # print ("we\'ve got a label") if (opcode[0] == "J"): # Jump to an absolute point (this doesn't account for indirect jumps) byteDiff = labels[address] hexByteDiff = str(toHex(byteDiff, 16)[2:]) # take starting position 0x0600 into account if (len(hexByteDiff) < 3): print(opcodes[opcode]['ABS'] + ' ' + padHex(hexByteDiff) + '06 ') hexOutput += opcodes[opcode]['ABS'] + ' ' + padHex(hexByteDiff) + '06 ' else: print(opcodes[opcode]['ABS'] + ' ' + padHex(hexByteDiff)) hexOutput += opcodes[opcode]['ABS'] + ' ' + padHex(hexByteDiff) else: # relative addressing with label byteDiff = labels[address] - line['bytePosition'] - 2 print(opcodes[opcode]['BRA'] + ' ' + padHex(str(toHex(byteDiff, 8)[2:]))) hexOutput += opcodes[opcode]['BRA'] + ' ' + padHex(str(toHex(byteDiff, 8)[2:])) else: raise ValueError('Problem with address in this line: ',line) else: raise ValueError('Invalid line length') else: raise ValueError('No opcode found') return (hexOutput.strip())
def getHexFromLines(parsedLines, labels, variables): print("Labels:", labels) print("Variables:", variables) hexOutput = "" for line in parsedLines: if (line['length'] > 0): # take the labels out of lines if (line['data'][0].endswith(':')): line['length'] -= 1 line['data'] = line['data'][1:] # only process lines that aren't just labels or variables if (line['length'] > 0 and line['data'][0].lower() != 'define'): opcode = line['data'][0].upper() address = 'null' if (line['length']) == 2: address = line['data'][1] # replace variables with their values for variable in variables.keys(): if variable in address: address = address.replace(variable, variables[variable]) print("Opcode", opcode, "Address", address) if (opcode in opcodes.keys()): # implicit if (line['length'] == 1): print((opcodes[opcode]['SNGL']) + ' ') hexOutput += (opcodes[opcode]['SNGL']) + ' ' elif (line['length'] == 2): # immediate if (address.startswith('#')): if (address[1] == '$'): print(opcodes[opcode]['IMM'] + ' ' + padHex(address[2:])) hexOutput += opcodes[opcode][ 'IMM'] + ' ' + padHex(address[2:]) else: print(opcodes[opcode]['IMM'] + ' ' + padHex(address[1:])) hexOutput += opcodes[opcode][ 'IMM'] + ' ' + padHex(address[1:]) elif (address.startswith('(')): # indexed indirect if address[4] == ',': print(opcodes[opcode]['INDX'] + ' ' + address[2:4] + ' ') hexOutput += opcodes[opcode][ 'INDX'] + ' ' + address[2:4] + ' ' # indirect index elif address[5] == ',': print(opcodes[opcode]['INDY'] + ' ' + address[2:4] + ' ') hexOutput += opcodes[opcode][ 'INDY'] + ' ' + address[2:4] + ' ' # indirect else: print(opcodes[opcode]['IND'] + ' ' + address[4:6] + ' ' + address[2:4] + ' ') hexOutput += opcodes[opcode][ 'IND'] + ' ' + address[ 4:6] + ' ' + address[2:4] + ' ' elif (address.startswith('$')): operandLength = len(address) # absolute X and Y if (operandLength == 7): mode = 'ABS' + address[6].upper() print(opcodes[opcode][mode] + ' ' + address[3:5] + ' ' + address[1:3] + ' ') hexOutput += opcodes[opcode][ mode] + ' ' + address[3:5] + ' ' + address[ 1:3] + ' ' # zero page elif (operandLength == 3): print(opcodes[opcode]['ZP'] + ' ' + padHex(address[1:])) hexOutput += opcodes[opcode][ 'ZP'] + ' ' + padHex(address[1:]) # zero page X and Y elif (address[3] == ','): mode = 'ZP' + address[4].upper() print(opcodes[opcode][mode] + ' ' + address[1:3] + ' ') hexOutput += opcodes[opcode][ mode] + ' ' + address[1:3] + ' ' # absolute else: print(opcodes[opcode]['ABS'] + ' ' + padHex(address[3:]) + address[1:3] + ' ') hexOutput += opcodes[opcode][ 'ABS'] + ' ' + padHex( address[3:]) + address[1:3] + ' ' elif (address in labels.keys()): # print ("we\'ve got a label") if (opcode[0] == "J"): # Jump to an absolute point (this doesn't account for indirect jumps) byteDiff = labels[address] hexByteDiff = str(toHex(byteDiff, 16)[2:]) # take starting position 0x0600 into account if (len(hexByteDiff) < 3): print(opcodes[opcode]['ABS'] + ' ' + padHex(hexByteDiff) + '06 ') hexOutput += opcodes[opcode][ 'ABS'] + ' ' + padHex( hexByteDiff) + '06 ' else: print(opcodes[opcode]['ABS'] + ' ' + padHex(hexByteDiff)) hexOutput += opcodes[opcode][ 'ABS'] + ' ' + padHex(hexByteDiff) else: # relative addressing with label byteDiff = labels[address] - line[ 'bytePosition'] - 2 print(opcodes[opcode]['BRA'] + ' ' + padHex(str(toHex(byteDiff, 8)[2:]))) hexOutput += opcodes[opcode][ 'BRA'] + ' ' + padHex( str(toHex(byteDiff, 8)[2:])) else: raise ValueError( 'Problem with address in this line: ', line) else: raise ValueError('Invalid line length') else: raise ValueError('No opcode found') return (hexOutput.strip())