From b93aa5abb5664666be5fbf3a3408b1eab22a559e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E5=87=A0=E6=96=B9?= Date: Mon, 22 Nov 2021 17:40:18 +0800 Subject: [PATCH] imm_gen --- code/Imm_gen.py | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/code/Imm_gen.py b/code/Imm_gen.py index e211483..f289227 100644 --- a/code/Imm_gen.py +++ b/code/Imm_gen.py @@ -1,23 +1,15 @@ -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 \ No newline at end of file +def Imm_gen(inst_code: str) -> str: + test = inst_code[25:32] + if test in ['0010011', '0000011', '1100111']: # i-Type, l, jalr + imm_out = inst_code[0:12].rjust(32, inst_code[0]) + elif test == '0100011': # s + imm_out = (inst_code[0:7] + inst_code[20:25]).rjust(32, inst_code[0]) + elif test == '1100011': # branch + imm_out = (inst_code[0] + inst_code[24] + inst_code[1:7] + + inst_code[20:24]).rjust(32, inst_code[0]) + elif test == '1101111': # jal + imm_out = (inst_code[0] + inst_code[12:20] + inst_code[11] + + inst_code[1:11]).rjust(32, inst_code[0]) + elif test in ['0110111', '0010111']: # lui, auipc + imm_out = inst_code[0:20].rjust(32, inst_code[0]) + return imm_out