题解 | #移位运算与乘法#

移位运算与乘法

http://www.nowcoder.com/practice/1dd22852bcac42ce8f781737f84a3272

没有用移位运算,直接用乘法了。

`timescale 1ns/1ns
module multi_sel(
input [7:0]d ,
input clk,
input rst,
output reg input_grant,
output reg [11:0]out
);
//*************code***********//
    //计数器代码
    reg [1:0] cnt;
    always@(posedge clk or negedge rst)begin
        if(!rst)
            cnt<=0;
        else if(end_cnt)
            cnt<=0;
        else
            cnt<=cnt+1;
    end
    assign add_cnt=rst!=0;
    assign end_cnt=add_cnt&&cnt==4-1;
    
    //输入有效标志代码
    always@(posedge clk or negedge rst)begin
        if(!rst)
            input_grant<=0;
        else if(cnt==0) begin
            input_grant<=1;
        end
        else
            input_grant<=0;
    end
 
    //计算结果代码
    reg [7:0] d_temp;
    always @(posedge clk or negedge rst)begin
        if(!rst)begin
            out<=0;
        end
        else if(cnt==0)begin
            out<=d*x;
            d_temp<=d;
        end
        else begin
            out<=d_temp*x;
        end
    end
    
    //乘数因子代码
    reg [3:0] x;
    always@(*)begin
        if(cnt==0)
            x=1;
        else if(cnt==1)
            x=3;
        else if(cnt==2)
            x=7;
        else 
            x=8;
    end
            

//*************code***********//
endmodule
全部评论

相关推荐

小浪_Coding:找硬件测试,也可兼顾软测欧, 简历还可以的 ,注意排版,项目写的有条理一点, 然后个人技能多加点, 润色好简历之后就开始沟通海投了,深圳,东莞这边做硬件相关的公司还不少, 医疗类,仪器类的都可以尝试
点赞 评论 收藏
分享
06-28 22:48
已编辑
广东金融学院 Java
小浪_Coding:学院本+这俩项目不是buff叠满了嘛
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务