题解 | #移位运算与乘法#
移位运算与乘法
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_tmp; initial begin input_grant = 1'b0; out = 10'b0; end always@(posedge clk) begin if(!rst) begin cnt <= 1'b0; end else begin cnt <= cnt + 1'b1; end end always@(posedge clk) begin if(!rst) d_tmp <= 8'b0; else begin if(cnt == 2'b0)d_tmp <= d; end end always@(posedge clk) begin if(!rst) input_grant <= 1'b0; else begin if((d != 0)&&(cnt == 2'd0))input_grant <= 1'b1; else input_grant <= 1'b0; end end always@(posedge clk) begin if(!rst) out <= 10'b0; else begin //if(cnt == 2'd3) // out <= d<<cnt; ////elseif(cnt == 2'b0) //// out <= {3'd0,d}; //else out <= (((d << cnt) + (d << cnt_last))&&cnt) + d; case(cnt) 2'd0: out <= d; 2'd1: out <= (d_tmp<<cnt) + d_tmp; 2'd2: out <= (d_tmp<<cnt) + (d_tmp<<(cnt-1)) + d_tmp; 2'd3: out <= d_tmp<<cnt; endcase end end //*************code***********// endmodule