题解 | #移位运算与乘法#
移位运算与乘法
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] out_reg;
always@(posedge clk or negedge rst) begin
if(~rst) begin
out_reg <= 8'b0;
input_grant <= 1'b0;
end
else if(cnt == 2'b00) begin
out_reg <= d;
input_grant <= 1'b1;
end
else begin
out_reg <= out_reg; //这一句可以没有
input_grant <= 1'b0;
end
end
always@(posedge clk or negedge rst) begin
if(~rst) begin
out <= 0;
end
else begin
case(cnt) //这里没有begin 因为有endcase
2'b00: out <= d; //这里要直接输出,不能再输出寄存器的值
2'b01: out <= (out_reg<<2) - out_reg;
2'b10: out <=(out_reg<<3) - out_reg;
2'b11: out <= (out_reg<<3);
endcase
end
end
always@(posedge clk or negedge rst) begin
if (~rst) begin
cnt <= 2'b00;
end
else if (cnt != 2'b11) begin
cnt <= cnt + 2'b01;
end
else begin
cnt <= 2'b00;
end
end
//*************code***********//
endmodule
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] out_reg;
always@(posedge clk or negedge rst) begin
if(~rst) begin
out_reg <= 8'b0;
input_grant <= 1'b0;
end
else if(cnt == 2'b00) begin
out_reg <= d;
input_grant <= 1'b1;
end
else begin
out_reg <= out_reg; //这一句可以没有
input_grant <= 1'b0;
end
end
always@(posedge clk or negedge rst) begin
if(~rst) begin
out <= 0;
end
else begin
case(cnt) //这里没有begin 因为有endcase
2'b00: out <= d; //这里要直接输出,不能再输出寄存器的值
2'b01: out <= (out_reg<<2) - out_reg;
2'b10: out <=(out_reg<<3) - out_reg;
2'b11: out <= (out_reg<<3);
endcase
end
end
always@(posedge clk or negedge rst) begin
if (~rst) begin
cnt <= 2'b00;
end
else if (cnt != 2'b11) begin
cnt <= cnt + 2'b01;
end
else begin
cnt <= 2'b00;
end
end
//*************code***********//
endmodule