b4:第二个源操作数的寄存器地址,R型的16-20位,I型指令的目的寄存器地址,16-20位
b5:R型指令的目的寄存器地址,11-15位 b6:I型指令的立即数,0-15位 b7:R型指令的低六位,0-5位 b8:b4和b5经二路选择器二选一 b20:从IM中取出来的指令内容 c1:I型指令将16位立即数扩展成32位 c2:存入目的寄存器的内容 c3:从源寄存器1中取出的内容 c4:从源寄存器2中取出的内容 c5:c1和c4二选一
c6:ALU计算出的结果,也是读DM的地址 c7:从DM中取出的内容
c8、c9:针对不同类型的指令对进行pc值的修正的选择 d1:功能选择信号
e类信号:主操作控制信号,主要是各部件的读写控制信号
核心模块说明:
IM:按序号存放指令(共17条,其中lw指令执行了两次),在CPU启动时从<0>开始执行。
Rf:主寄存器,存放32个32位字,存储情况见下表(十进制表示,单数行是地址序号,双数行是对应的存储值):
<0> 0 <8> 7 <16>
<1> 14 <9> 6 <17>
<2> 13 <10> 5 <18>
<3> 12 <11> 4 <19>
<4> 11 <12> 3 <20>
<5> 10 <13> 2 <21>
<6> 9 <14> 1 <22>
<7> 8 <15> 0 <23>
15 <24> 7 14 <25> 6 13 <26> 5 12 <27> 4 11 <28> 3 10 <29> 2 9 <30> 1 8 <31> 0 DM:数据存储器,存储情况见下表(十进制表示,单数行是地址序号,双数行是对应的存储值):
<0> 8 <8> 8 <16> 8 <24> 8
tRf:测试模块,用于输出指令执行结果以检验是否正确; IRf:测试模块,用于输出当前执行的指令,与tRf搭配使用。
<1> 7 <9> 7 <17> 7 <25> 7 <2> 6 <10> 6 <18> 6 <26> 6 <3> 5 <11> 5 <19> 5 <27> 5 <4> 4 <12> 4 <20> 4 <28> 4 <5> 3 <13> 3 <21> 3 <29> 3 <6> 2 <14> 2 <22> 2 <30> 2 <7> 1 <15> 1 <23> 1 <31> 1 四.代码分析
1. Add //完成分支指令的目标地址计算// library ieee;
use ieee.std_logic_1164.all; use ieee.std_logic_arith.all; use ieee.std_logic_unsigned.all; entity add is
Port ( a: in STD_LOGIC_VECTOR (31 downto 0);
b : in STD_LOGIC_VECTOR (31 downto 0);
y : out STD_LOGIC_VECTOR (31 downto 0)); end add;
architecture Behavioral of add is begin
y<=a + b;-------将a和b相加赋给y
end Behavioral;
2. Add4 //完成PC+1(采用字寻址)的计算// library ieee;
use ieee.std_logic_1164.all; use ieee.std_logic_arith.all; use ieee.std_logic_unsigned.all;
entity Add4 is
port(pcin:in std_logic_vector(31 downto 0);
pcout:out std_logic_vector(31 downto 0));
end Add4;
architecture behave of Add4 is begin
process(pcin) begin
pcout <= pcin + 1;---------pc值的修改
end process;
end behave;
3. ALU //主运算器// library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all; use ieee.std_logic_unsigned.all;
entity alu is Port (
a1, b1 : in STD_LOGIC_vector(31 downto 0); alucontr: in STD_LOGIC_VECTOR (3 downto 0); result : buffer STD_LOGIC_VECTOR (31 downto 0); zero : out STD_LOGIC ); end alu;
architecture behave of alu is --signal d1: integer;
--signal c1,f1: bit_vector(31 downto 0); begin
process(a1,b1,alucontr) begin
case alucontr is
when \与操作 when \或操作 when \相加 when \异或 when \或非操作
when \conv_integer(b1)); ------Sll将a1向左移动b1数值位 when \减法 when \自增 when \自减
when \比较大小 if(a1 when \conv_integer(b1)); ------srl算术右移 when others=> result<=x\end case; if(a1=b1)then --beq 判断分支指令是否转移当a1=b1时进行转移 zero<='1'; else zero<='0'; end if; end process; end behave; 4. and_gate //与门,完成分支指令的判定// library ieee; use ieee.std_logic_1164.all; entity and_gate is port( a,b:in std_logic; c:out std_logic ); end and_gate; architecture dataflow of and_gate is begin c <= a and b;