Created
March 9, 2016 17:19
-
-
Save AnthonyLam/38f95132b325f70ae5dc to your computer and use it in GitHub Desktop.
Tool for creating instructions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
# Format of Input Text | |
# OPCODE RS RD RT/#IMM; | |
class Asm: | |
def __init__(self,filename): | |
self.functions = {"nop":"0000", | |
"add":"0001", "sub":"0010", | |
"cmp":"0011", "slt":"0100", | |
"and":"0101", "or":"0110","not":"0111","xor":"1000", | |
"sll":"1001", "srl":"1010","sla":"1011", "sra":"1100", | |
"mov":"1101"} | |
self.operands = list() | |
self.actions = list() | |
op_regex = "^([a-zA-Z]{2,3})(?:\s*([0-9]{1,2})\s*([0-9]{1,2})\s*((?:#-?)?[0-9]{1,2}))?;$" | |
with open(filename) as fi: | |
reg = re.compile(op_regex,re.M | re.DOTALL) | |
for item in fi: | |
data = reg.search(item) | |
self.operands.append(data.groups()[1:]) | |
self.actions.append(data.groups()[0]) | |
def three_op(self,key,val): | |
op,rt = self.rt_as_binary(key) | |
rd = self.rd_as_binary(key) | |
rs = self.rs_as_binary(key) | |
return op + rs + rd + self.functions[val] + rt | |
@staticmethod | |
def rd_as_binary(key): | |
return format(int(key[1]),'06b') | |
@staticmethod | |
def rs_as_binary(key): | |
return format(int(key[0]),'06b') | |
# Darshan Parajuli | |
@staticmethod | |
def to_twos_comp(bit_string: str) -> str: | |
def _full_adder(x, y, c): | |
sum = (~x & ~y & c) | (~x & y & ~c) | (x & ~y & ~c) | (x & y & c) | |
carry = (~x & y & c) | (x & ~y & c) | (x & y & ~c) | (x & y & c) | |
return sum, carry | |
flipped = ''.join('1' if c == '0' else '0' for c in bit_string) | |
s, c = _full_adder(int(flipped[-1]), 1, 0) | |
out = str(s) | |
carry = c | |
for i in reversed(flipped[:-1]): | |
s, c = _full_adder(int(i), 0, carry) | |
carry = c | |
out = str(s) + out | |
return out | |
# --- | |
def rt_as_binary(self,key): | |
if self._check_imm(key[2]): | |
op = "1" | |
if key[2][1] == '-': | |
rt_imm = format(int(key[2][2:]),'015b') | |
rt_imm = self.to_twos_comp(rt_imm) | |
else: | |
rt_imm = format(int(key[2][1:]),'015b') | |
else: | |
op="0" | |
rt_imm = format(int(key[2]),'06b') + "000000000" | |
return op,rt_imm | |
def _check_imm(self,inp): | |
return inp[0] == '#' | |
def __iter__(self): | |
for key,val in zip(self.operands,self.actions): | |
if val == "nop": | |
yield "0"*32 | |
else: | |
yield self.three_op(key,val) | |
if __name__ == "__main__": | |
asm = Asm("ops.txt") | |
for m in asm: | |
print("ins <= \"{}\";".format(m)) | |
print("wait for 100 ns;") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nop; | |
mov 0 10 #10; | |
mov 0 30 #20; | |
add 10 20 30; | |
mov 0 11 #23; | |
sll 11 12 #5; | |
sll 11 12 10; | |
nop; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome you guyman!