输入位宽8bit,输出位宽12bit ,也就是说每三个输入数据可以生成两个完整输出。
注意给出的波形是data_lock而不是data_in,这是陷阱。data_lock是data_in打了一拍的结果。
用一个三进制计数器,按拍拼凑数据输出即可。
`timescale 1ns/1ns
module width_8to12(
input clk ,
input rst_n ,
input valid_in ,
input [7:0] data_in ,
output reg valid_out,
output reg [11:0] data_out
);
reg[1:0] cyc_cnt ;
reg[7:0] data_lock ;
always @(posedge clk or negedge rst_n) begin
if(!rst_n)
cyc_cnt <= 'd0 ;
else if(valid_in) begin
if(cyc_cnt == 2)
cyc_cnt <= 0;
else
cyc_cnt <= cyc_cnt + 1 ;
end
else
cyc_cnt <= cyc_cnt ;
end
always @(posedge clk or negedge rst_n) begin
if(!rst_n)
data_lock <= 'd0 ;
else if(valid_in)
data_lock <= data_in ;
else
data_lock <= data_lock ;
end
always @(posedge clk or negedge rst_n) begin
if(!rst_n)
data_out <= 'd0 ;
else if(cyc_cnt == 1 && valid_in)
data_out <= {data_lock,data_in[7:4]} ;
else if(cyc_cnt == 2 && valid_in)
data_out <= {data_lock[3:0],data_in} ;
else
data_out <= data_out ;
end
always @(posedge clk or negedge rst_n) begin
if(!rst_n)
valid_out <= 1'b0 ;
else if((cyc_cnt == 1 || cyc_cnt == 2) && valid_in)
valid_out <= 1'b1 ;
else
valid_out <= 1'b0 ;
end
endmodule
标签:进阶,lock,牛客,数据位,8to12,rst,input,data,reg
From: https://www.cnblogs.com/icwangpu/p/17033653.html