From 672b0eb7de769a72ed7c4f8cd471d433843ef690 Mon Sep 17 00:00:00 2001 From: Andrew Waterman Date: Mon, 19 Mar 2012 17:15:38 -0700 Subject: [PATCH] abstract regfile behind object --- riscv/decode.h | 40 ++++++++++++++++++++-------------------- riscv/htif.cc | 4 ++-- riscv/processor.cc | 4 ++-- riscv/processor.h | 4 ++-- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/riscv/decode.h b/riscv/decode.h index 6fa0a4b..eb13b53 100644 --- a/riscv/decode.h +++ b/riscv/decode.h @@ -137,26 +137,26 @@ union insn_t }; #include -class do_writeback +template +class regfile_t { public: - do_writeback(reg_t* _rf, int _rd) : rf(_rf), rd(_rd) {} - - const do_writeback& operator = (reg_t rhs) + void reset() { -#if 0 - printf("R[%x] <= %llx\n",rd,(long long)rhs); -#endif - rf[rd] = rhs; - rf[0] = 0; - return *this; + memset(data, 0, sizeof(data)); + } + T& operator [] (size_t i) + { + if (zero_reg) + data[0] = 0; + return data[i]; + } + const T& operator [] (size_t i) const + { + return const_cast&>(*this)[i]; } - - operator reg_t() { return rf[rd]; } - private: - reg_t* rf; - int rd; + T data[N]; }; #define throw_illegal_instruction \ @@ -166,8 +166,8 @@ private: // helpful macros, etc #define RS1 XPR[insn.rtype.rs1] #define RS2 XPR[insn.rtype.rs2] -#define RD do_writeback(XPR,insn.rtype.rd) -#define RA do_writeback(XPR,1) +#define RD XPR[insn.rtype.rd] +#define RA XPR[1] #define FRS1 FPR[insn.ftype.rs1] #define FRS2 FPR[insn.ftype.rs2] #define FRS3 FPR[insn.ftype.rs3] @@ -211,7 +211,7 @@ private: #define require_rvc if(!(sr & SR_EC)) throw_illegal_instruction #define CRD_REGNUM ((insn.bits >> 5) & 0x1f) -#define CRD do_writeback(XPR, CRD_REGNUM) +#define CRD XPR[CRD_REGNUM] #define CRS1 XPR[(insn.bits >> 10) & 0x1f] #define CRS2 XPR[(insn.bits >> 5) & 0x1f] #define CIMM6 ((int32_t)((insn.bits >> 10) & 0x3f) << 26 >> 26) @@ -237,8 +237,8 @@ static const int rvc_rs2_regmap[8] = { 20, 21, 2, 3, 4, 5, 6, 0 }; #define UT_RS1(idx) uts[idx]->XPR[insn.rtype.rs1] #define UT_RS2(idx) uts[idx]->XPR[insn.rtype.rs2] -#define UT_RD(idx) do_writeback(uts[idx]->XPR,insn.rtype.rd) -#define UT_RA(idx) do_writeback(uts[idx]->XPR,1) +#define UT_RD(idx) uts[idx]->XPR[insn.rtype.rd] +#define UT_RA(idx) uts[idx]->XPR[1] #define UT_FRS1(idx) uts[idx]->FPR[insn.ftype.rs1] #define UT_FRS2(idx) uts[idx]->FPR[insn.ftype.rs2] #define UT_FRS3(idx) uts[idx]->FPR[insn.ftype.rs3] diff --git a/riscv/htif.cc b/riscv/htif.cc index 0439236..9aa9a7c 100644 --- a/riscv/htif.cc +++ b/riscv/htif.cc @@ -32,8 +32,8 @@ struct packet }; htif_t::htif_t(int _tohost_fd, int _fromhost_fd) - : sim(NULL), tohost_fd(_tohost_fd), fromhost_fd(_fromhost_fd), seqno(1), - reset(true) + : sim(NULL), tohost_fd(_tohost_fd), fromhost_fd(_fromhost_fd), + reset(true), seqno(1) { } diff --git a/riscv/processor.cc b/riscv/processor.cc index 6df910c..9785799 100644 --- a/riscv/processor.cc +++ b/riscv/processor.cc @@ -48,8 +48,8 @@ void processor_t::reset() // the following state is undefined upon boot-up, // but we zero it for determinism - memset(XPR,0,sizeof(XPR)); - memset(FPR,0,sizeof(FPR)); + XPR.reset(); + FPR.reset(); pc = 0; epc = 0; diff --git a/riscv/processor.h b/riscv/processor.h index 1e007a8..09fac00 100644 --- a/riscv/processor.h +++ b/riscv/processor.h @@ -28,8 +28,8 @@ private: mmu_t& mmu; // main memory is always accessed via the mmu // user-visible architected state - reg_t XPR[NXPR]; - freg_t FPR[NFPR]; + regfile_t XPR; + regfile_t FPR; reg_t pc; // counters -- 2.30.2