class Autoinserter(object): def __init__(self): self.mayor = 2 self.minor = 1 def __next__(self): """Generate the next opcode""" # 10bit opcode opcode = 0b1111111111 # Calculate this opcode. opcode *= self.minor opcode //= self.mayor # The lower 6 bits are reserved. opcode <<= 6 # 16 bits in total. Should not matter. opcode &= 0xffff # Update the state. self.minor = (self.minor + 2) % self.mayor if(self.minor == 1): self.mayor *= 2 return opcode def __iter__(self): return self def make_opcodes(mnemonics_in_order): ai = Autoinserter() return {mnemonic: opcode for (mnemonic, opcode) in zip(mnemonics_in_order, ai)}