一、實(shí)驗(yàn)?zāi)康?/h3>
1.掌握ALU模塊的組成和接口,理解ALU的功 能。
2.通過編程調(diào)用ALU模塊計(jì)算斐波那契數(shù)。
3.掌握Verilog中多模塊編程方法和實(shí)現(xiàn)。
二、實(shí)驗(yàn)內(nèi)容
用 Verilog 設(shè)計(jì)一個(gè)算術(shù)運(yùn)算單元 ALU,采 用純組合邏輯設(shè)計(jì),32bit 寬。
- 利用該 ALU 完成斐波那契數(shù) f(n),其中 2<n<16。
- 可選
–改成3段式實(shí)現(xiàn)(已實(shí)現(xiàn))
–用七段數(shù)碼管輸出(已實(shí)現(xiàn))
三、實(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
fib.v(三段式斐波那契數(shù)列)
module fib(
input clk,
input rst,
input [3:0] n,
output [31:0] result
);
reg[31:0] ra, rb;
wire[31:0] wf;
reg[3:0] count;
alu myalu(.a(ra),.b(rb),.op(4'b0001),.f(wf));
reg[1:0] cur_state, nex_state; // 現(xiàn)態(tài)和次態(tài)
// 狀態(tài)轉(zhuǎn)移
always @(posedge clk)
begin
if(rst==1)
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'b01;
endcase
end
// 狀態(tài)輸出
always @(posedge clk)
begin
case(cur_state)
2'b00:
begin
ra<=32'b1;
rb<=32'b1;
count<=4'b0011;
end
2'b01:
if(count<n)
begin
ra<=rb;
rb<=wf;
count<=count+1'b1;
end
endcase
end
assign result=wf;
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 [11:0] result,
output reg[2: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'b10;
2'b10:
nex_state<=2'b00;
endcase
end
// 狀態(tài)輸出
// always @(posedge clk)
always @(posedge clk_new)
begin
case(cur_state)
2'b00:
begin
an<=3'b01;
data<=result[3:0];
end
2'b01:
begin
an<=3'b010;
data<=result[7:4];
end
2'b10:
begin
an<=3'b100;
data<=result[11:8];
end
endcase
end
seven myseven(.data(data),.out(out));
endmodule
top.v(不用數(shù)碼管顯示)
module top(
input clk,
input rst,
input [3:0] n,
output [11:0] result
);
wire[31:0] temp;
fib myfib(.clk(clk),.rst(rst),.n(n),.result(temp));
assign result=temp[11:0];
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 [11: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-421666.html
六、實(shí)驗(yàn)結(jié)果
用數(shù)碼管顯示文章來源地址http://www.zghlxwxcb.cn/news/detail-421666.html
到了這里,關(guān)于計(jì)算機(jī)組成原理實(shí)驗(yàn)——一、ALU實(shí)驗(yàn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!