parent
677a4fa3f5
commit
468beb8bf8
@ -0,0 +1,23 @@
|
|||||||
|
def Imm_gen(inst_code):
|
||||||
|
test=inst_code[25:32]
|
||||||
|
if test=='0010011':
|
||||||
|
imm_out=20*inst_code[0]+inst_code[0:12]
|
||||||
|
elif test=='0000011':
|
||||||
|
pass
|
||||||
|
elif test=='0100011':
|
||||||
|
pass
|
||||||
|
elif test=='1100011':
|
||||||
|
pass
|
||||||
|
elif test=='1101111':
|
||||||
|
pass
|
||||||
|
elif test=='1100111':
|
||||||
|
pass
|
||||||
|
elif test=='0110111':
|
||||||
|
pass
|
||||||
|
def imm_gen(inst_code,imm_out):
|
||||||
|
test=inst_code[25:32]
|
||||||
|
if test=="0010011":
|
||||||
|
imm_out=inst_code[0]*20+inst_code[0:12]
|
||||||
|
else:
|
||||||
|
imm_out='0'*32
|
||||||
|
return imm_out
|
@ -0,0 +1,2 @@
|
|||||||
|
def adder(a,b): #返回加法运算结果y
|
||||||
|
return a+b
|
@ -0,0 +1,19 @@
|
|||||||
|
def BintoInt(x):
|
||||||
|
i = int(x, base=2)
|
||||||
|
if x[0] == '1':
|
||||||
|
i = i - 2**len(x)
|
||||||
|
return i
|
||||||
|
|
||||||
|
|
||||||
|
def BintoUInt(x):
|
||||||
|
return int(x, base=2)
|
||||||
|
|
||||||
|
|
||||||
|
def UInttoBin(x, n):
|
||||||
|
return '0' * (n - len(bin(x)[2:])) + bin(x)[2:]
|
||||||
|
|
||||||
|
|
||||||
|
def InttoBin(x, n):
|
||||||
|
if x < 0:
|
||||||
|
x += 2**n
|
||||||
|
return UInttoBin(x, n)
|
@ -0,0 +1,14 @@
|
|||||||
|
def fowardingunit(rs1,rs2,ex_mem_rd,mem_wb_rd,ex_mem_regwrite,mem_wb_regwrite):
|
||||||
|
if rs1!=0 and rs1==ex_mem_rd and ex_mem_regwrite:
|
||||||
|
forward_a='01'
|
||||||
|
elif rs1!=0 and rs1==mem_wb_rd and mem_wb_regwrite:
|
||||||
|
forward_a='10'
|
||||||
|
else:
|
||||||
|
forward_a='00'
|
||||||
|
if rs2!=0 and rs2==ex_mem_rd and ex_mem_regwrite:
|
||||||
|
forward_b='01'
|
||||||
|
elif rs2!=0 and rs2==mem_wb_rd and mem_wb_regwrite:
|
||||||
|
forward_b='10'
|
||||||
|
else:
|
||||||
|
forward_b='00'
|
||||||
|
return forward_a,forward_b
|
@ -0,0 +1,6 @@
|
|||||||
|
def hazard_detector(if_id_rs1,if_id_rs2,id_ex_rd,id_ex_memread):
|
||||||
|
if id_ex_memread and (id_ex_rd==if_id_rs1 or id_ex_rd==if_id_rs2):
|
||||||
|
stall=1
|
||||||
|
else:
|
||||||
|
stall=0
|
||||||
|
return stall
|
@ -0,0 +1,5 @@
|
|||||||
|
class insn_mem:
|
||||||
|
memory=[8*'0']*4*1024*16
|
||||||
|
def fetch(self,address):
|
||||||
|
insn=self.memory[address]
|
||||||
|
return insn
|
@ -0,0 +1,5 @@
|
|||||||
|
def mux(d0,d1,s): #返回选择结果
|
||||||
|
if s=='0':
|
||||||
|
return d0
|
||||||
|
else:
|
||||||
|
return d1
|
@ -0,0 +1,9 @@
|
|||||||
|
def mux4(d00,d01,d10,d11,s): #返回选择结果
|
||||||
|
if s=='00':
|
||||||
|
return d00
|
||||||
|
elif s=='01':
|
||||||
|
return d01
|
||||||
|
elif s=='10':
|
||||||
|
return d10
|
||||||
|
else:
|
||||||
|
return d11
|
@ -0,0 +1,25 @@
|
|||||||
|
def proc_controller(opcode):
|
||||||
|
if opcode=='0110011': #(add,and,or,sll,slt,sltu,sra,srl,sub,xor)
|
||||||
|
ALUSrc, MemtoReg, RegWrite, MemRead, MemWrite, ALUOp, Branch, JalrSel, RWSel='0','0','1','0','0','10','0','0','00'
|
||||||
|
elif opcode=='0110111': #(lui)20位立即数
|
||||||
|
ALUSrc, MemtoReg, RegWrite, MemRead, MemWrite, ALUOp, Branch, JalrSel, RWSel='0','0','1','0','0','00','0','0','10'
|
||||||
|
elif opcode=='1101111': #(jal)
|
||||||
|
ALUSrc, MemtoReg, RegWrite, MemRead, MemWrite, ALUOp, Branch, JalrSel, RWSel='1','0','1','0','0','11','1','0','01'
|
||||||
|
elif opcode=='0010011': #(addi,andi,ori,slli,slti,sltiu,srai,srli,xori)
|
||||||
|
ALUSrc, MemtoReg, RegWrite, MemRead, MemWrite, ALUOp, Branch, JalrSel, RWSel='1','0','1','0','0','10','0','0','00'
|
||||||
|
elif opcode=='0000011': #(lb,lbu,lh,lhu,lw)
|
||||||
|
ALUSrc, MemtoReg, RegWrite, MemRead, MemWrite, ALUOp, Branch, JalrSel, RWSel='1','1','1','1','0','00','0','0','00'
|
||||||
|
elif opcode=='1100111': #(jalr)
|
||||||
|
ALUSrc, MemtoReg, RegWrite, MemRead, MemWrite, ALUOp, Branch, JalrSel, RWSel='1','0','1','0','0','10','1','1','01'
|
||||||
|
elif opcode=='0100011': #(sb, sh, sw)
|
||||||
|
ALUSrc, MemtoReg, RegWrite, MemRead, MemWrite, ALUOp, Branch, JalrSel, RWSel='1','0','0','0','1','00','0','0','00'
|
||||||
|
elif opcode=='1100011': #(beq,bge,bgeu,blt,bne,bltu)
|
||||||
|
ALUSrc, MemtoReg, RegWrite, MemRead, MemWrite, ALUOp, Branch, JalrSel, RWSel='0','0','0','0','0','01','1','0','00'
|
||||||
|
elif opcode=='1101111': #(jal)
|
||||||
|
ALUSrc, MemtoReg, RegWrite, MemRead, MemWrite, ALUOp, Branch, JalrSel, RWSel='1','0','1','0','0','11','1','0','01'
|
||||||
|
#################################
|
||||||
|
elif opcode=='0010111': #(auipc)20位立即数
|
||||||
|
ALUSrc, MemtoReg, RegWrite, MemRead, MemWrite, ALUOp, Branch, JalrSel, RWSel='1','0','1','0','0','00','1','0','11'
|
||||||
|
else:
|
||||||
|
ALUSrc, MemtoReg, RegWrite, MemRead, MemWrite, ALUOp, Branch, JalrSel, RWSel='0','0','0','0','0','00','0','0','00'
|
||||||
|
return ALUSrc, MemtoReg, RegWrite, MemRead, MemWrite, ALUOp, Branch, JalrSel, RWSel
|
Loading…
Reference in new issue