搜索
bottom↓
回复: 3

状态机学习

[复制链接]

出0入0汤圆

发表于 2012-11-11 11:17:42 | 显示全部楼层 |阅读模式
本帖最后由 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, 杜汶泽)

出0入0汤圆

 楼主| 发表于 2012-11-11 11:18:42 | 显示全部楼层
希望能够找到二段式实现不了的原因。
自己先顶一个!!!!!!!

出0入0汤圆

发表于 2012-11-11 14:10:55 | 显示全部楼层
这个要顶,mark

出0入0汤圆

 楼主| 发表于 2012-11-11 19:41:53 | 显示全部楼层
自己再来顶一下······································
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-7-24 07:19

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表