题解 | 移位运算与乘法

移位运算与乘法

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] din;
reg [10:0] din;

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

always@(posedge clk or negedge rst)begin
    if(!rst)begin
        din<=0;
        input_grant<=0;
        out<=0;
    end
    else case(cnt)
        2'b00:begin
            din<=d;
            input_grant<=1;
            out<=d;
        end
        2'b01:begin
            input_grant<=0;
            out<=(din<<2)-din;
        end
        2'b10:begin
            input_grant<=0;
            out<=(din<<3)-din;
        end
        2'b11:begin
            input_grant<=0;
            out<=(din<<3);
        end
    endcase
end

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

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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