def parse_instructions(f): instr_lst = [] yaml_obj = yaml.load(open(f, 'r').read(), Loader=Loader) for instr in yaml_obj['instructions']: name = instr['name'] opcode = instr['opcode'] if opcode > 63: raise Exception("Invalid Opcode: %i" % opcode) instr_desc = instr['description'] operand = bool(instr['requires_operand']) instruction = Instruction(name, opcode, instr_desc, operand) for key in instr['flags'].keys(): if key.lower() not in ['other', 'cf', 'zf', 'pf']: raise Exception("Invalid Flag: %s" % key) for step in instr['flags'][key]: sigs = step['signals'] if sigs is None: sigs = [] else: new_sigs = [] for sig in sigs: new_sigs.append(CtrlSigs[sig]) sigs = new_sigs step_desc = step['description'] microstep = MicroStep(sigs, step_desc) instruction.insert_step(microstep, key) instr_lst.append(instruction) return instr_lst
def compile_line_8(programline, mapping): assembly = [] temp_register = mapping['__temp__'] operation_to_opcode = {'+': 'ADD', '-': 'SUB'} # c = a <op> b # first move the temp register assembly.append( Instruction(opcode='MOV', rd=temp_register, rr=mapping[programline.a])) # apply the operation to the temp register assembly.append( Instruction(opcode=operation_to_opcode[programline.op], rd=temp_register, rr=mapping[programline.b])) # move the result to the destination register assembly.append( Instruction(opcode='MOV', rd=mapping[programline.c], rr=temp_register)) return assembly
def getInstructions(self, vehicle): self.instructions = [] slice_start = 0 slice_end = 0 for pt in self[0:-2]: slice_end += 1 if pt.roadname != pt.next.roadname or pt.next.isStoplight: self.instructions.append( Instruction(self[:slice_end], self[0].pt.bearing(self[1].pt), 'start', vehicle)) slice_start = slice_end break for pt in self[slice_start:-2]: slice_end += 1 if pt.roadname != pt.next.roadname or pt.next.isStoplight: feature = 'turn' if self[slice_start].isStopsign: feature = 'stopsign' if self[slice_start].isStoplight: feature = 'stoplight' self.instructions.append( Instruction( self[slice_start:slice_end], self[slice_start].pt.directionAngle( self[slice_start - 1].pt, self[slice_start + 1].pt), feature, vehicle)) slice_start = slice_end feature = 'turn' if self[slice_start].isStopsign: feature = 'stopsign' if self[slice_start].isStoplight: feature = 'stoplight' self.instructions.append( Instruction( self[slice_start:], self[slice_start].pt.directionAngle(self[slice_start - 1].pt, self[slice_start + 1].pt), feature, vehicle))
def compile_line_16(programline, mapping): # assembly = [] # vars low, high = mapping['low'], mapping['high'] temp_low, temp_high = low['__temp__'], high['__temp__'] if programline.op == '+': # move the low bits into the assembly register return [ Instruction(opcode='MOV', rd=temp_low, rr=low[programline.a]), Instruction(opcode='ADD', rd=temp_low, rr=low[programline.b]), Instruction(opcode='MOV', rd=low[programline.c], rr=temp_low), # carry is currently in the CARRY flag, # MOV does not affect the flags Instruction(opcode='MOV', rd=temp_high, rr=high[programline.a]), Instruction(opcode='ADC', rd=temp_high, rr=high[programline.b]), Instruction(opcode='MOV', rd=high[programline.c], rr=temp_high) ] else: raise NotImplementedError return [None]
def write_outputs(): total_plants = 0 original_stdout = sys.stdout # Writing the instructions output f = open("output.txt", "w") sys.stdout = f instructions = [[] for i in range(inputs.HORIZON + 2)] # Three types of instructions (See the Instruction() class in the classes.py file) for e in gc.g.edges: if e[0].type == 'module' and e[1].type == 'module' and e[0].where != e[1].where: for p in plants.plants: if (e[0], e[1], p) in opti.flow_vars and opti.flow_vars[e[0], e[1], p].varValue > 0: instructions[e[1].when].append(Instruction(p[0].name, opti.flow_vars[e[0], e[1], p].varValue,'module_transfer', e[0].module_number, e[0].module_type, e[1].module_number, e[1].module_type)) if e[0].type == 'source' and e[1].type == 'module': for p in plants.plants: if (e[0], e[1], p) in opti.flow_vars and opti.flow_vars[e[0], e[1], p].varValue > 0: instructions[e[1].when].append(Instruction( p[0].name, opti.flow_vars[e[0], e[1], p].varValue, 'source_transfer', e[1].module_number, e[1].module_type)) total_plants += opti.flow_vars[e[0], e[1], p].varValue if e[0].type == 'module' and e[1].type == 'sink': for p in plants.plants: if (e[0], e[1], p) in opti.flow_vars and opti.flow_vars[e[0], e[1], p].varValue > 0: instructions[e[0].when + 1].append(Instruction( p[0].name, opti.flow_vars[e[0], e[1], p].varValue, 'sink_transfer', e[0].module_number, e[0].module_type)) for i in range(len(instructions)): print("DAY " + str(i)) for s in instructions[i]: print(s.toString()) sys.stdout = original_stdout f.close() # Writing the growth modules states output f = open("output2.txt", "w") sys.stdout = f states = [[[] for i in range(inputs.HORIZON + 1)] for k in range(len(inputs.MODULES))] for e in gc.g.edges: if e[1].type == 'module': for p in plants.plants: if (e[0].type != 'source' or e[1].when == 0) and (e[0], e[1], p) in opti.flow_vars and opti.flow_vars[e[0], e[1], p].varValue > 0: states[e[1].module_type][e[1].when].append( "Module : " + str(e[1].module_number) + " | Plant : " + p[0].name) for i in range(len(states)): print("GROWTH MODULE " + str(i)) for j in range(len(states[i])): if(len(states[i][j]) != 0): print("DAY " + str(j)) for k in range(len(states[i][j])): print(states[i][j][k]) sys.stdout = original_stdout f.close() #Day by day what plants we are harvesting + For each type of plants the day it is being harvested f = open("output_harvested.txt", "w") sys.stdout = f harvested = [[] for p in range(edges.n_plants)] frame_plant =[[0]*len(instructions) for p in range(edges.n_plants)] for i in range(len(instructions)): print("DAY " + str(i)) for s in instructions[i]: if s.type == 'sink': print("Day " + str(i) + ": " +s.toString()) for j in range(edges.n_plants): if s.name == inputs.plant_data[j].name: harvested[j].append((s, i)) frame_plant[j][i] = int(s.number) print("_____________________________________________________________________________________") for i in range(len(harvested)): if harvested[i]: print(harvested[i][0][0].name + ":") for j in range(len(harvested[i])): s, day = harvested[i][j] print("Day " + str(day) + ": " + s.toString()) print("\n") print("_____________________________________________________________________________________") df = pd.DataFrame(frame_plant, index = [p.name for p in inputs.plant_data]) pd.set_option('display.max_rows', None) pd.set_option('display.max_columns', None) print(df.transpose()) sys.stdout = original_stdout f.close() print("total number of plants is:", total_plants)
def write_outputs(): original_stdout = sys.stdout # Writing the instructions output f = open("output.txt", "w") sys.stdout = f instructions = [[] for i in range(inputs.HORIZON + 2)] # Three types of instructions (See the Instruction() class in the classes.py file) for e in gc.g.edges: if e[0].type == 'hole' and e[ 1].type == 'hole' and e[0].where != e[1].where: for p in plants.plants: if (e[0], e[1], p) in opti.flow_vars and opti.flow_vars[ e[0], e[1], p].varValue == 1: instructions[e[1].when].append( Instruction(p[0].name, 'hole_transfer', e[0].hole, e[0].tray, e[1].hole, e[1].tray)) if e[0].type == 'source' and e[1].type == 'hole': for p in plants.plants: if (e[0], e[1], p) in opti.flow_vars and opti.flow_vars[ e[0], e[1], p].varValue == 1: instructions[e[1].when].append( Instruction(p[0].name, 'source_transfer', e[1].hole, e[1].tray)) if e[0].type == 'hole' and e[1].type == 'sink': for p in plants.plants: if (e[0], e[1], p) in opti.flow_vars and opti.flow_vars[ e[0], e[1], p].varValue == 1: instructions[e[0].when + 1].append( Instruction(p[0].name, 'sink_transfer', e[0].hole, e[0].tray)) for i in range(len(instructions)): print("DAY " + str(i)) for s in instructions[i]: print(s.toString()) sys.stdout = original_stdout f.close() # Writing the growth modules states output f = open("output2.txt", "w") states = [[[] for i in range(inputs.HORIZON + 1)] for k in range(inputs.TRAYS)] for e in gc.g.edges: if e[1].type == 'hole': for p in plants.plants: if (e[0].type != 'source' or e[1].when == 0) and ( e[0], e[1], p) in opti.flow_vars and opti.flow_vars[ e[0], e[1], p].varValue == 1: states[e[1].tray][e[1].when].append("Hole : " + str(e[1].hole) + " | Plant : " + p[0].name) sys.stdout = f for i in range(len(states)): print("GROWTH MODULE " + str(i)) for j in range(len(states[i])): if (len(states[i][j]) != 0): print("DAY " + str(j)) for k in range(len(states[i][j])): print(states[i][j][k]) sys.stdout = original_stdout f.close()
from classes import Instruction, CtrlSigs, MicroStep instr_lst = [] # No Operation NOP = Instruction('NOP', 0b000000, "Do absolutely nothing") NOP.insert_step(MicroStep([], "Do absolutely nothing")) instr_lst.append(NOP) # A Load instructions LDA = Instruction('LDA', 0b000001, "Load RAM contents to Register A", True) LDA.insert_step(MicroStep([CtrlSigs.IO, CtrlSigs.MI], "Move IR Address to MAR")) LDA.insert_step(MicroStep([CtrlSigs.RO, CtrlSigs.AI], "Move RAM contents to reg. A")) instr_lst.append(LDA) LAI = Instruction('LAI', 0b000010, "Load a value to Register A immediately", True) LAI.insert_step(MicroStep([CtrlSigs.IO, CtrlSigs.AI], "Move IR value to reg. A")) instr_lst.append(LAI) # B Load Instructions LDB = Instruction('LDB', 0b000011, "Load RAM contents to Register B", True) LDB.insert_step(MicroStep([CtrlSigs.IO, CtrlSigs.MI], "Move IR contetns to MAR")) LDB.insert_step(MicroStep([CtrlSigs.RO, CtrlSigs.BI], "Move RAM contents to reg. B")) instr_lst.append(LDB) LBI = Instruction('LBI', 0b000100, "Load a value to Register B immediately, True) LBI.insert_step(MicroStep([CtrlSigs.IO, CtrlSigs.BI], "Move IR value to reg. B")) instr_lst.append(LBI) # C Load Instructions LDC = Instruction('LDC', 0b000101, "Load RAM contents to Register C", True)