Files
IPA/rtl/data_cache/data_assemble.v
Core_kingdom 79dee10db1 cache module
2025-08-26 16:53:22 +08:00

109 lines
3.7 KiB
Verilog
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

module data_assemble #(
parameter PIXEL_WIDTH = 8, // 单通道像素位宽
parameter GRAY_PIXEL_CNT = 32, // Gray模式32个8bit→256bit
parameter RGB_PIXEL_CNT = 8 // RGB模式8个32bit24bit数据+8bit补零→256bit
) (
input wire clk,
input wire rst_n,
input wire en, // 拼接使能
input wire input_pixel_type, // 0=Gray1=RGB
input wire [PIXEL_WIDTH-1:0] ir_ch0, // CH0数据
input wire [PIXEL_WIDTH-1:0] ir_ch1, // CH1数据
input wire [PIXEL_WIDTH-1:0] ir_ch2, // CH2数据
input wire pixel_valid, // 像素有效
output reg done, // 拼接完成256bit就绪
output reg [255:0] assembled_data // 拼接后256bit数据
);
// 内部信号
reg [4:0] gray_cnt; // Gray模式计数器0~31
reg [2:0] rgb_cnt; // RGB模式计数器0~7
reg [255:0] data_reg; // 拼接数据寄存器
reg sync_out,sync_out_r;
// 拼接计数器复位逻辑
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
gray_cnt <= 5'd0;
rgb_cnt <= 3'd0;
end else if (!en) begin
gray_cnt <= 5'd0;
rgb_cnt <= 3'd0;
end else if (pixel_valid) begin
case (input_pixel_type)
1'b0: begin // Gray模式
gray_cnt <= (gray_cnt == GRAY_PIXEL_CNT ) ? 5'd0 : gray_cnt + 5'd1;
end
1'b1: begin // RGB模式
rgb_cnt <= (rgb_cnt == RGB_PIXEL_CNT ) ? 3'd0 : rgb_cnt + 3'd1;
end
endcase
end
end
// 数据拼接逻辑
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
data_reg <= 256'd0;
assembled_data <= 256'd0;
sync_out <= 'd0;
sync_out_r <= 'd0;
end else if (!en) begin
sync_out <= 'd0;
data_reg <= 256'd0;
assembled_data <= 256'd0;
done <= 'd0;
end else if (pixel_valid) begin
case (input_pixel_type)
1'b0: begin // Gray模式32x8bit→256bit高位到低位拼接
data_reg[(GRAY_PIXEL_CNT -1- gray_cnt) * PIXEL_WIDTH +: PIXEL_WIDTH] <= ir_ch0;
// 计数器满:拼接完成,锁存数据
if (gray_cnt == GRAY_PIXEL_CNT -1 ) begin
sync_out = 1'd1;
//data_reg <= 256'd0; // 复位寄存器,准备下一轮
end
end
1'b1: begin // RGB模式8x32bit→256bit32bit=8bit补零+CH2+CH1+CH0
reg [31:0] rgb_pixel;
rgb_pixel = {8'd0, ir_ch2, ir_ch1, ir_ch0}; // 补零+三通道数据
data_reg[(RGB_PIXEL_CNT - 1 - rgb_cnt) * 32 +: 32] <= rgb_pixel;
// 计数器满:拼接完成,锁存数据
if (gray_cnt == GRAY_PIXEL_CNT -1 ) begin
sync_out = 1'd1;
//data_reg <= 256'd0; // 复位寄存器,准备下一轮
end
end
endcase
end
end
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
sync_out_r <= 'd0;
end else begin
sync_out_r <= sync_out;
end
end
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
assembled_data <= 'd0;
done <= 'd0;
end else if (sync_out_r) begin
assembled_data <= data_reg;
done <= 1'b1;
end
end
always @(posedge clk or negedge rst_n) begin
if (done == 1'b1) begin
done <= 'd0;
end
end
endmodule