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

移位运算与乘法

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;//din是7位
//first
always@(posedge clk or negedge rst)begin
    if(!rst)
        cnt <= 1'b0;
    /*    
    else if ( cnt == 2'd3)
        cnt <= 1'b0;
    */
    else
        cnt <= cnt + 1'b1;//服了服了,你为啥不加1
end
/*//third
always@(posedge clk or negedge rst)begin
    if (!rst)
        input_grant <= 1'b0;
    else begin
        case(cnt)
            2'd0:
                input_grant <= 1;
            default:input_grant <=0;
        endcase
    end
end
*/
//third
always@(posedge clk or negedge rst) begin
    if(!rst) begin
        din <= 1'b0;
        out <= 1'b0;
        input_grant <= 1'b0;
    end
    else begin
        case(cnt)
            2'd0:begin
                din <= d;
                out <= d;
                input_grant <= 1'b1;
            end
            2'd1:begin
                out <= (din<<2)-din;
                input_grant <= 1'b0;
            end   
            2'd2:begin
                out <= (din<<3)-din;
                input_grant <= 1'b0;
            end 
            2'd3:begin//这里是3不是1,以后别复制了
                out <= (din<<3);
                input_grant <= 1'b0;
            end 
        endcase
    end
end


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

全部评论
计数器+状态机完成所有操作,因为最后的输出需要寄存,所以状态机的第三段使用时序,由于使用计数器完成了状态转换,所以第二段状态转移规律可以省略。因为cnt两位,最多计数到3所以不需要else if来判断是否计满。
点赞 回复 分享
发布于 2023-09-12 10:44 北京

相关推荐

评论
点赞
收藏
分享

创作者周榜

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