题解 | #整数倍数据位宽转换8to16#
整数倍数据位宽转换8to16
http://www.nowcoder.com/practice/f1fb03cb0baf46ada2969806114bce5e
由于本题整数倍刚好是两倍,所以用一个存储器buff来保存上一个数据,在下个数据到来时,把两个数据拼起来一起给data_out。cnt计数周期为0-1。
完整代码
`timescale 1ns/1ns
module width_8to16(
input clk ,
input rst_n ,
input valid_in ,
input [7:0] data_in ,
output reg valid_out,
output reg [15:0] data_out
);
reg[7:0] buff;
reg[1:0] cnt;
always@(negedge rst_n or posedge clk)begin
if(~rst_n)
cnt <= 0;
else if(valid_in)
if(cnt == 1)
cnt <= 0;
else
cnt <= cnt + 1;
end
always@(negedge rst_n or posedge clk)begin
if(~rst_n)begin
buff <= 0;
data_out <= 0;
end
else if(valid_in)
if(cnt == 1)begin
data_out <= {buff, data_in};
end
else
buff <= data_in;
end
always@(negedge rst_n or posedge clk)begin
if(~rst_n)begin
valid_out <= 0;
end
else if(valid_in && cnt == 1)
valid_out <= 1;
else
valid_out <= 0;
end
endmodule
如果整数倍>2,则定义更大的存储器,在计数周期内保存之前的输入数据
if(cnt != t)
buff <= {buff[xx:xx], data_in};