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

移位运算与乘法

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 [10:0]   din;
reg [1:0]    cnt;
//-----------------计数器----------------//
always @(posedge clk or negedge rst)
if(!rst)
    cnt<=0;
else if(cnt==3)//可以不用给清零条件,计满会溢出清零
    cnt<=0;
else
    cnt<=cnt+1;

always @(posedge clk or negedge rst)
    if(!rst)begin
        input_grant  <=0; 
        out          <=0;
        din          <=0;
    end
    else case(cnt)//对不同的cnt进行分类讨论,编码嫌麻烦可以直接十进制编码
        0:begin
            din          <=d;
            input_grant  <=1;
            out          <=d;//注意此处对out的赋值,此时d还没有完成寄存到din,所以out用d赋值 
            
        end
        1:begin
            input_grant  <=0;
            out          <=(din<<2)-din; 
        end
        2:begin
            out          <=(din<<3)-din; 
        end
        3:begin
            out          <=(din<<3); 
        end
        default:;
    endcase
endmodule

全部评论

相关推荐

2025-12-13 12:38
惠州学院 直播运营
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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