移位运算和乘法
已知d为一个8位数,请在每个时钟周期分别输出该数乘1/3/7/8,并输出一个信号通知此时刻输入的d有效(d给出的信号的上升沿表示写入有效)
`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;
always@(posedge clk or negedge rst)begin
if(!rst)
cnt<=0;
else
cnt<=cnt+1'b1;
end
reg [7:0] d_reg;
always@(posedge clk or negedge rst)begin
if(!rst)begin
d_reg<=0;
input_grant<=0;
end
else if(cnt==0)begin
d_reg<=d;
input_grant<=1;
end
else begin
d_reg<=d_reg;
input_grant<=0;
end
end
always@(posedge clk or negedge rst)begin
if(!rst)begin
input_grant<=0;
out<=0;
end
else begin
case(cnt)
2'd0:out<=d;
2'd1:out<=(d_reg<<2)-d_reg;
2'd2:out<=(d_reg<<3)-d_reg;
2'd3:out<=d_reg<<3;
endcase
end
end
//*************code***********//
endmodule
1、移位运算要加括号()
2、cnt=0,输入d寄存一拍,但是由于cnt=0的下一拍也要输出,寄存的无法使用
所以cnt=0是时的输出要用d