CO-P4-设计文档
2024计算机组成原理P4设计文档
P4_L0_document
信号解释
- RegDst(寄存器写入地址)
- 2’b00: RT
- 2’b01: RD
- 2’b11: 5’h1f
- default: 5’b0
- Mem2Reg(寄存器写入数据)
- 2’b00: alu_c
- 2’b01: dm_data
- 2’b10: pc + 32’d4
- default: 32’b0
- ALUSrc(alu的b操作数选择)
- 1’b0: grf_RD2
- 1’b1: ext_ext32
- default: 32’b0
- ALU_F(ALU计算方式)
- 4’b0000: add
- 4’b0001: sub
- 4’b0010: or
- 4’b0011: SL16 (lui)
- 4’b0100: SLL (补0逻辑左移)
- 4’b0101: SRL (补0逻辑右移)
- 4‘b0110: SRA (算数右移)
- ExtOp (扩展操作)
- 2’b01: 0扩展
- 2‘b10: 算术扩展(符号位)
- 2’b11: 1扩展
设计草稿
本次CPU支持add, sub, or, ori, lw, sw, lb, lbu, lh, sb, sh, beq, lui, nop,begz,bgtz,blez,bltz,bne, j, jal, jr
共23条指令。
mips.v (Top)
Ports
Port name | Direction | Type | Description |
---|---|---|---|
clk | input | ||
reset | input |
Signals
Name | Type | Description |
---|---|---|
pc | wire [31:0] | |
if_jump | wire | |
if_branch | wire | |
instr_out | wire [31:0] | |
ifu_offset | wire [31:0] | |
OPCODE | wire [5:0] | |
FUNCT | wire [5:0] | |
RS | wire [4:0] | |
RT | wire [4:0] | |
RD | wire [4:0] | |
SHAMT | wire [4:0] | |
IMM | wire [15:0] | |
INDEX | wire [25:0] | |
bOp_out | wire [5:0] | |
RegDst_out | wire [1:0] | |
ALUSrc_out | wire | |
Mem2Reg_out | wire [1:0] | |
RegWrite_out | wire | |
MemWrite_out | wire | |
branch_out | wire | |
ExtOp_out | wire [1:0] | |
ALUOP_out | wire [1:0] | |
jumpSrc_out | wire [1:0] | |
dm_data | wire [31:0] | |
ext_ext32 | wire [31:0] | |
grf_RD1 | wire [31:0] | |
grf_RD2 | wire [31:0] | |
alu_B | wire [31:0] | |
alu_C | wire [31:0] | |
grf_WA | wire [4:0] | |
grf_WD | wire [31:0] | |
jumpext_ext32 | wire [31:0] |
Instantiations
ifu: IFU
controller: Controller
grf: GRF
alu: ALU
dm: DM
ext: EXT
jumpext: jumpEXT
bcheck: bCheck
IFU.v
Ports
Port name | Direction | Type | Description |
---|---|---|---|
clk | input | ||
reset | input | ||
jump | input | ||
branch | input | ||
offset | input | [31:0] | |
PC | output | [31:0] | |
Instr | output | [31:0] |
Controller.v
Ports
Port name | Direction | Type | Description |
---|---|---|---|
OpCode | input | [5:0] | |
Funct | input | [5:0] | |
rt | input | [4:0] | |
jump | output | ||
jumpSrc | output | [1:0] | |
bOp | output | [5:0] | |
RegDst | output | [1:0] | |
ALUSrc | output | ||
Mem2Reg | output | [1:0] | |
RegWrite | output | ||
MemWrite | output | ||
branch | output | ||
ExtOp | output | [1:0] | |
ALUOP | output | [1:0] |
信号映射表
OpCode | Funct | RT | |
---|---|---|---|
add | 000000 | 100000 | |
sub | 000000 | 100010 | |
or | 000000 | 100101 | |
SLL | 000000 | 000000 | |
SRL | 000000 | 000010 | |
SRA | 000000 | 000011 | |
ori | 001101 | ||
lw | 100011 | ||
sw | 101011 | ||
lb | 100000 | ||
lbu | 100100 | ||
lh | 100001 | ||
sb | 101000 | ||
sh | 101001 | ||
beq | 000100 | ||
lui | 001111 | ||
bgez | 000001 | 00001 | |
bgtz | 000111 | ||
blez | 000110 | ||
bltz | 000001 | ||
bne | 000101 | 00000 | |
jump | 000010 | ||
jal | 000011 | ||
jr | 000000 | 001000 |
add | sub | ori | SLL | SRL | SRA | or | lw | sw | lb | lbu | lh | sb | sh | branch | lui | jump | jal | jr | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
RegDst | 01 | 01 | 00 | 01 | 01 | 01 | 01 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 11(const31) | 00 |
ALUSrc | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 0 |
Mem2Reg | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 01 | 00 | 01 | 01 | 01 | 00 | 00 | 00 | 00 | 00 | 10(PC + 4) | 00 |
RegWrite | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 1 | 1 | 1 | 0 | 0 | 0 | 1 | 0 | 1 | 0 |
MemWrite | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | 0 | 0 | 0 |
branch | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 |
ExtOp | 00 | 00 | 01(zero) | 0 | 0 | 0 | 00 | ]10(signed) | 10(signed) | 10 | 10 | 10 | 10 | 10 | 10(signed) | 10(signed) | 00 | 0 | 0 |
ALUOP<3,0> | 0000 | 0001 | 0010 | 0100 | 0101 | 0110 | 0010 | 0000(+) | 0000(+) | 0000 | 0000 | 0000 | 0000 | 0000 | 0000(+) | 0011 | 0000 | 0000 | 0000 |
jumpSrc | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 01 | 01 | 10 |
jump | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 |
ls_type | 00 | 00 | 01 | 10 | 11 | 01 | 11 |
GRF.v
Ports
Port name | Direction | Type | Description |
---|---|---|---|
A1 | input | [4:0] | |
A2 | input | [4:0] | |
A3 | input | [4:0] | |
WD | input | [31:0] | |
clk | input | ||
reset | input | ||
WE | input | ||
pc | input | [31:0] | |
RD1 | output | [31:0] | |
RD2 | output | [31:0] |
DM.v
Ports
Port name | Direction | Type | Description |
---|---|---|---|
A | input | [31:0] | |
D_input | input | [31:0] | |
str | input | ||
clk | input | ||
pc | input | [31:0] | |
D_output | output | [31:0] |
ALU.v
Ports
Port name | Direction | Type | Description |
---|---|---|---|
A | input | [31:0] | |
B | input | [31:0] | |
shamt | input | [4:0] | |
F | input | [3:0] | |
C | output | [31:0] |
bCheck.v
Ports
Port name | Direction | Type | Description |
---|---|---|---|
Grs | input | [31:0] | |
Grt | input | [31:0] | |
bOp | input | [5:0] | |
branch | input | ||
check | output |
EXT.v
Ports
Port name | Direction | Type | Description |
---|---|---|---|
imm16 | input | [15:0] | |
ExtOp | input | [1:0] | |
ext32 | output | [31:0] |
jumpEXT.v
Ports
Port name | Direction | Type | Description |
---|---|---|---|
index | input | [25:0] | |
pc | input | [31:0] | |
ext32 | output | [31:0] |
测试方案
利用魔改Mars: 直接比较ISE得输出和Mars的输出
课上环境:在Isim中将32个Reg和内存添加到波形图中,对比最终结果
简单的测试代码:
1 | #GRF REG_WRITE TEST |
1 | //STDOUT |
思考题
阅读下面给出的 DM 的输入示例中(示例 DM 容量为 4KB,即 32bit × 1024字),根据你的理解回答,这个 addr 信号又是从哪里来的?地址信号 addr 位数为什么是 [11:2] 而不是 [9:0] ?
addr信号来源于ALU的结果输出端。
输入信号为按字节索引的地址,而DM的存储方式为按字索引,取addr[11:2]即等于addr[9:0] << 2
思考上述两种控制器设计的译码方式,给出代码示例,并尝试对比各方式的优劣。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16always @(*) begin
if (OpCode == 6'b000000) begin
if (Funct == 6'b100000) begin
//add
RegDst <= 2'b01;
ALUSrc <= 1'b0;
Mem2Reg <= 2'b00;
RegWrite <= 1'b1;
MemWrite <= 1'b0;
branch <= 1'b0;
ExtOp <= 2'b00;
ALUOP <= 2'b00;
jumpSrc <= 2'b00;
jump <= 1'b0;
end
end1
2
3
4
5
6
7
8
9
10
11
12
13
14always @(*) begin
if (OpCode == 6'b100011 ||
OpCode == 6'b101011 ||
OpCode == 6'b000100 ||
OpCode == 6'b001111) begin
ExtOp <= 2'b10;
end
else if (OpCode == 6'b001101) begin
ExtOp <= 2'b01;
end
else begin
ExtOp <= 2'b00;
end
end- 记录指令对应的控制信号如何取值:方便添加新指令。
- 记录控制信号每种取值所对应的指令:方便添加控制信号。
在相应的部件中,复位信号的设计都是同步复位,这与 P3 中的设计要求不同。请对比同步复位与异步复位这两种方式的 reset 信号与 clk 信号优先级的关系。
异步复位reset信号优先,同步复位clk信号优先。
C 语言是一种弱类型程序设计语言。C 语言中不对计算结果溢出进行处理,这意味着 C 语言要求程序员必须很清楚计算结果是否会导致溢出。因此,如果仅仅支持 C 语言,MIPS 指令的所有计算指令均可以忽略溢出。 请说明为什么在忽略溢出的前提下,addi 与 addiu 是等价的,add 与 addu 是等价的。提示:阅读《MIPS32® Architecture For Programmers Volume II: The MIPS32® Instruction Set》中相关指令的 Operation 部分。
The term “unsigned” in the instruction name is a misnomer; this operation is 32-bit modulo arithmetic that does not trap on overflow. This instruction is appropriate for unsigned arithmetic, such as address arithmetic, or integer arithmetic environments that ignore overflow, such as C language arithmetic.
addu和add的区别、addi和addiu的区别均仅在与是否有溢出判断,故忽略溢出时效果是一样的。
CO-P4-设计文档