You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.2 KiB

3 years ago
from binandint import *
3 years ago
def alu(a, b, alu_ctrl): # a,b为两个运算数alu_ctrl为控制信号返回运算结果result与是否为0 zero
signed = a-b
if alu_ctrl == '00000': # add,jalr
alu_result = a+b
elif alu_ctrl == '00001': # sub
alu_result = a-b
elif alu_ctrl == '00010': # or
alu_result = a | b
elif alu_ctrl == '00011': # and
alu_result = a & b
elif alu_ctrl == '00100': # slt
if(signed >= 0):
alu_result = 0
else:
alu_result = 1
elif alu_ctrl == '00101': # beq
if a == b:
alu_result = 1
else:
alu_result = 0
elif alu_ctrl == '00110': # bne
if a != b:
alu_result = 1
else:
alu_result = 0
elif alu_ctrl == '00111': # blt
if a < b:
alu_result = 1
else:
alu_result = 0
elif alu_ctrl == '01000': # bge
if a >= b:
alu_result = 1
else:
alu_result = 0
elif alu_ctrl == '01001':
if a < 0:
ua = a+2**32
else:
ua = a
if b < 0:
ub = b+2**32
else:
ub = b
if ua < ub:
alu_result = 1
else:
alu_result = 0
elif alu_ctrl == '01010':
if a < 0:
ua = a+2**32
else:
ua = a
if b < 0:
ub = b+2**32
else:
ub = b
if ua >= ub:
alu_result = 1
else:
alu_result = 0
elif alu_ctrl == '01011':
alu_result = 1
elif alu_ctrl == '01100':
alu_result = a ^ b
elif alu_ctrl == '01101':
alu_result = a << b
elif alu_ctrl == '01110':
if a < 0:
ua = a+2**32
else:
ua = a
if b < 0:
ub = b+2**32
else:
ub = b
if ua <= ub:
alu_result = 1
else:
alu_result = 0
elif alu_ctrl == '01111':
if a < 0:
ua = a+2**32
else:
ua = a
alu_result = a >> b
elif alu_ctrl == '10000':
alu_result = a >> b
else:
alu_result = 0
return alu_result