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

移位运算与乘法

https://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 [10:0]out
);
//*************code***********//

reg [1:0]   cnt;
reg [7:0]   d_reg;  //存储输入的d值

always@(posedge clk or negedge rst)
    if(!rst)
        cnt <=  2'd0;
    else    
        cnt <=  cnt + 1'b1;


always@(posedge clk or negedge rst)
    if(!rst)
        input_grant <=  1'b0;
    else    if(cnt==2'd0)
        input_grant <=  1'b1;
    else
        input_grant <=  1'b0;

always@(posedge clk or negedge rst)
    if(!rst)
        d_reg   <= 8'd0;
    else    if(cnt==2'd0)
        d_reg   <=  d;
    else    
        d_reg   <=  d_reg;


always@(posedge clk or negedge rst)
    if(!rst)
        out <=  10'd0;
    else    if(cnt==2'd0)
        out <=  d;
    else    if(cnt==2'd1)
        out <=  (d_reg<<1) + d_reg;
    else    if(cnt==2'd2)
        out <=  (d_reg<<2) + (d_reg<<1) + d_reg;
    else    if(cnt==2'd3)
        out <=  d_reg<<3;


//*************code***********//
endmodule

全部评论

相关推荐

虚闻松声:继续投吧。 简历没啥问题。很优秀。 拙见:自我评价没什么意义;试试转向Agent开发、大模型应用;别死磕传统Java开发。 免费修改简历,就业咨询,欢迎私信交流。
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
3
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务