一、實(shí)驗(yàn)?zāi)康?/h3>
1.掌握寄存器堆的工作原理和接口。
2.掌握寄存器堆的實(shí)現(xiàn)方法。
3.掌握寄存器堆在微處理器中承擔(dān)的功能。
二.實(shí)驗(yàn)內(nèi)容
- 設(shè)計(jì)一32*32bit 的寄存器文件,即32 個(gè) 32 位的寄存器文件(寄存器組)
–具備兩組讀端口及一組寫端口;
–通過讀端口可從0~31 號(hào)的任意地址讀取 數(shù)據(jù);
–通過寫端口可向1~31 號(hào)的任意地址寫入 數(shù)據(jù)(0號(hào)寄存器的值固定為32’b0); - 利用寄存器堆和ALU 完成平方數(shù)f(n),其 中 1<n<16。
- 可選:
1)用fsm三段式實(shí)現(xiàn)
2)用7段數(shù)碼管輸出
–數(shù)碼管的輸出不能有明顯的閃爍
三.實(shí)驗(yàn)程序
alu.v(加法器)
module alu(
input [31:0] a,
input [31:0] b,
input [3:0] op,
output reg[31:0] f,
output c
);
always @(*)
case(op)
4'b0000: f = 32'b0;
4'b0001: f = a + b;
4'b0010: f = a - b;
4'b0011: f = a & b;
4'b0100: f = a | b;
4'b0101: f = a ^ b;
default: f = 32'b0;
endcase
assign c = ~(|f);
endmodule
RegFiles.v(寄存器堆)
module RegFiles(
input clk,
input [4:0] raddr1,
output [31:0] rdata1,
input [4:0] raddr2,
output [31:0] rdata2,
input we,
input [31:0] wdata,
input [4:0] waddr
);
reg[31:0] regs[1:31]; // 寄存器堆,每個(gè)寄存器32bit
assign rdata1=(raddr1==5'b00000)?32'b0:regs[raddr1]; // 讀端口1
assign rdata2=(raddr2==5'b00000)?32'b0:regs[raddr2]; // 讀端口2
always @(posedge clk) // 寫端口
if(we)
if(waddr!=5'b00000)
regs[waddr]<=wdata;
endmodule
square.v(求平方數(shù))
module square(
input clk,
input rst,
input [3:0] n,
output [7:0] result
);
wire[31:0] a_w,b_w,f_w; // 兩個(gè)加數(shù)(讀端口數(shù)據(jù))和結(jié)果
wire[31:0] wd; //寫端口數(shù)據(jù)
reg[4:0] wa; // 寫端口地址(reg)
reg[4:0] ra1,ra2; // 讀端口地址
reg we=1'b0; // 讀寫控制
reg isf; // 判斷加數(shù)
reg[3:0] count; // 計(jì)數(shù)
reg[1:0] cur_state,nex_state; // 現(xiàn)態(tài)和次態(tài)
assign wd=isf?f_w:n; // 第一個(gè)加數(shù)恒為n,ifs為0,第二個(gè)加數(shù)為n,ifs為1,第二個(gè)加數(shù)為f_w
RegFiles myreg(.clk(clk),.raddr1(ra1),.raddr2(ra2),.rdata1(a_w),.rdata2(b_w),.waddr(wa),.wdata(wd),.we(we));
alu myalu(.a(a_w),.b(b_w),.op(4'b0001),.f(f_w));
// 狀態(tài)轉(zhuǎn)移
always @(posedge clk)
begin
if(rst)
begin
cur_state<=2'b00;
end
else
cur_state<=nex_state;
end
// 狀態(tài)轉(zhuǎn)移條件
always @(*)
begin
case(cur_state)
2'b00:
nex_state<=2'b01;
2'b01:
nex_state<=2'b10;
2'b10:
nex_state<=2'b10;
endcase
end
// 狀態(tài)輸出
always @(posedge clk)
begin
case(cur_state)
2'b00:
begin
count<=4'b0010;
isf<=1'b0;
wa<=5'b00001;
we<=1'b1;
end
2'b01:
begin
isf<=1'b0;
ra1<=5'b00001;
ra2<=5'b00010;
wa<=5'b00010;
we<=1'b1;
end
2'b10:
begin
if(count<n)
begin
isf<=1'b1;
count<=count+1;
we<=1'b1;
end
else
begin
we<=1'b0;
end
end
endcase
end
assign result=f_w[7:0];
endmodule
div.v(分頻器)
module div(
input clk,
output clk_new
);
reg[17:0] q = 18'b0;
always @(posedge clk)
begin
q=q+1'b1;
end
assign clk_new=q[17];
endmodule
seven.v(七段數(shù)碼管)
module seven(
input [3:0] data,
output reg[6:0] out
);
always @(*)
case (data)
4'b0000:out = 7'b1111110; // 7e
4'b0001:out = 7'b0110000; // 30
4'b0010:out = 7'b1101101; // 6d
4'b0011:out = 7'b1111001; // 79
4'b0100:out = 7'b0110011; // 33
4'b0101:out = 7'b1011011; // 5b
4'b0110:out = 7'b1011111; // 5f
4'b0111:out = 7'b1110000; // 70
4'b1000:out = 7'b1111111; // 7f
4'b1001:out = 7'b1111011; // 7b
4'b1010:out = 7'b1110111; // 77
4'b1011:out = 7'b0011111; // 1f
4'b1100:out = 7'b1001110; // 4e
4'b1101:out = 7'b0111101; // 3d
4'b1110:out = 7'b1001111; // 4f
4'b1111:out = 7'b1000111; // 47
default:out = 7'b1111110; //7e
endcase
endmodule
show.v(七段數(shù)碼管顯示)
module show(
input clk,
input rst,
input [7:0] result,
output reg[1:0] an,
output [6:0] out
);
wire clk_new;
div mydiv(.clk(clk),.clk_new(clk_new));
reg[3:0] data;
reg[1:0] cur_state,nex_state;
// 狀態(tài)轉(zhuǎn)移
// always @(posedge clk)
always @(posedge clk_new)
begin
if (rst)
cur_state<=2'b00;
else
cur_state<=nex_state;
end
// 狀態(tài)轉(zhuǎn)移條件
always @(*)
begin
case(cur_state)
2'b00:
nex_state<=2'b01;
2'b01:
nex_state<=2'b00;
endcase
end
// 狀態(tài)輸出
// always @(posedge clk)
always @(posedge clk_new)
begin
case(cur_state)
2'b00:
begin
an<=2'b01;
data<=result[3:0];
end
2'b01:
begin
an<=2'b10;
data<=result[7:4];
end
endcase
end
seven myseven(.data(data),.out(out));
endmodule
top.v(不用數(shù)碼管顯示)
module top(
input clk,
input rst,
input [3:0] n,
output [7:0] result
);
wire[7:0] result;
square mysquare(.clk(clk),.rst(rst),.n(n),.result(result));
endmodule
top.v(用數(shù)碼管顯示)
module top(
input clk,
input rst,
input [3:0] n,
output [2:0] an,
output [6:0] out
);
wire[31:0] temp;
fib myfib(.clk(clk),.rst(rst),.n(n),.result(temp));
show myshow(.clk(clk),.rst(rst),.result(temp[11:0]),.an(an),.out(out));
endmodule
四、仿真程序
mysim.v(不用數(shù)碼管顯示)
module mysim(
);
reg clk=1'b0;
reg rst=1'b1;
reg[3:0] n=4'b1110;
wire [7:0] result;
always
#10 clk=~clk;
initial
#11 rst=1'b0;
top mytop(.clk(clk),.rst(rst),.n(n),.result(result));
endmodule
mysim.v(用數(shù)碼管顯示)
module mysim(
);
reg clk=1'b0;
reg rst=1'b1;
reg[3:0] n=4'b1110;
wire[2:0] an;
wire[6:0] out;
always
#10 clk=~clk;
initial
#11 rst=1'b0;
top mytop(.clk(clk),.rst(rst),.n(n),.an(an),.out(out));
endmodule
五、仿真結(jié)果
不用數(shù)碼管顯示:
用數(shù)碼管顯示:文章來源:http://www.zghlxwxcb.cn/news/detail-455972.html
六、實(shí)驗(yàn)結(jié)果
用數(shù)碼管顯示文章來源地址http://www.zghlxwxcb.cn/news/detail-455972.html
到了這里,關(guān)于計(jì)算機(jī)組成原理實(shí)驗(yàn)——二、寄存器實(shí)驗(yàn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!