BCI-assembler/assembler/opcodes.py

34 lines
671 B
Python
Raw Normal View History

2018-10-09 09:43:42 +00:00
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)}