|
本帖最后由 zikongxiaozi 于 2012-11-11 11:17 编辑
本人想实现一个用状态机控制的16bit乘法器,但是试验下来发现使用二段式状态机无法实现,而使用一段式则可以实现,且经仿真后得到所需的效果,现在贴下本人所写代码,望高手解疑答惑,谢谢!
二段式:
module test_mult16(clk,rst_n,num,pnum,dataout,temp,done,count);
input clk;
input rst_n;
input [15:0] num;
input [15:0] pnum;
output [31:0]temp,dataout;
output done;
output [4:0]count;
//---------------------------两段式状态机,控制乘法器的三个实现步骤------------------------//
reg [1:0] pstate,nstate;
parameter idle = 2'b00, add = 2'b01, shift = 2'b10;
always @ (posedge clk) begin
if (!rst_n) begin
pstate <= idle;
end
else begin
pstate <= nstate;
end
end
reg [4:0]count;
reg [31:0]temp_r,dataout_r;
reg done_r;
always @ (pstate) begin
case (pstate)
idle : begin
count = 5'd0;
done_r = 1'b0;
dataout_r = 32'b0;
temp_r[15:0] = num;
temp_r[31:16] = 16'b0;
nstate = add;
end
add : begin
if (pnum[count])begin
dataout_r = dataout_r + temp;
nstate = shift;
end
end
shift : begin
count = count + 1'b1;
temp_r = {temp_r[30:0],1'b0};
if (count == 5'd16) begin
nstate = idle;
done_r = 1'b1;
end
else
nstate = add;
end
default : nstate = idle;
endcase
end
wire done;
wire [31:0]dataout;
wire [31:0]temp;
assign done = done_r; //输出标志位表示一次乘法已经结束
assign dataout = dataout_r;
assign temp = temp_r;
endmodule
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
一段式:
module test_mult16(clk,rst_n,num,pnum,dataout,temp,done,count);
input clk;
input rst_n;
input [15:0] num;
input [15:0] pnum;
output [31:0]temp,dataout;
output done;
output [4:0]count;
//-------------------------------一段式状态机-------------------------------------//
reg [1:0]pstate;
reg [31:0]temp_r,dataout_r;
reg [4:0]count;
reg done_r;
parameter idle = 2'b00,
add = 2'b01,
shift = 2'b11;
always @ (posedge clk or negedge rst_n)begin
if(!rst_n)begin
pstate <= idle;
end
else begin
case (pstate)
idle : begin
count <= 5'b0;
dataout_r <= 32'b0;
temp_r[15:0] <= num;
temp_r[31:16] <= 16'b0;
done_r <= 1'b0;
pstate <= add;
end
add : begin
if (pnum[count])begin
dataout_r <= dataout_r + temp;
pstate <= shift;
end
end
shift : begin
count <= count + 1'b1;
temp_r <= {temp_r[30:0],1'b0};
if (count == 5'd16) begin
done_r <= 1'b1;
count <= 5'd0;
pstate <= idle;
end
else begin
pstate <= add;
end
end
default : pstate <= idle;
endcase
end
end
wire done;
wire [31:0]dataout;
wire [31:0]temp;
assign done = done_r; //输出标志位表示一次乘法已经结束
assign dataout = dataout_r;
assign temp = temp_r;
endmodule
由一段式实现的状态机图和仿真:
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
阿莫论坛20周年了!感谢大家的支持与爱护!!
知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)
|