2024-06-27 21:52:46 +08:00
|
|
|
|
`timescale 1ns / 1ps
|
|
|
|
|
|
|
|
|
|
// 三通道图像合成一个RGB图像
|
|
|
|
|
module ColorBlender #(
|
2024-06-28 19:38:36 +08:00
|
|
|
|
parameter reg [ 4:0] IN_DEPTH = 12, // 输入图像的色深
|
|
|
|
|
parameter reg [ 4:0] OUT_DEPTH = 8, // 输出图像的色深
|
|
|
|
|
parameter reg [ 8:0] BUFF_SIZE = 32
|
2024-06-27 21:52:46 +08:00
|
|
|
|
) (
|
2024-06-28 19:38:36 +08:00
|
|
|
|
input wire clk,
|
|
|
|
|
input wire reset,
|
2024-06-27 21:52:46 +08:00
|
|
|
|
|
2024-06-28 19:38:36 +08:00
|
|
|
|
input wire in_en,
|
|
|
|
|
input wire [15:0] in_data[3], // 0:R 1:G 2:B
|
|
|
|
|
output wire out_ready,
|
|
|
|
|
output wire out_receive,
|
2024-06-27 21:52:46 +08:00
|
|
|
|
|
|
|
|
|
// 输出相关
|
2024-06-28 19:38:36 +08:00
|
|
|
|
input wire in_ready,
|
|
|
|
|
input wire in_receive,
|
|
|
|
|
output reg out_en,
|
|
|
|
|
output reg [3 * OUT_DEPTH - 1:0] out_data,
|
2024-06-27 21:52:46 +08:00
|
|
|
|
|
|
|
|
|
// 颜色校正
|
2024-06-28 19:38:36 +08:00
|
|
|
|
input wire [15:0] gain_red,
|
|
|
|
|
input wire [15:0] gain_green,
|
|
|
|
|
input wire [15:0] gain_blue,
|
2024-06-27 21:52:46 +08:00
|
|
|
|
input wire color_correction
|
|
|
|
|
);
|
2024-06-28 19:38:36 +08:00
|
|
|
|
localparam reg [2:0] READ_DATA = 0;
|
|
|
|
|
localparam reg [2:0] CALC_DATA = 1;
|
|
|
|
|
localparam reg [2:0] SATI_DATA = 2;
|
|
|
|
|
localparam reg [2:0] SEND_DATA = 3;
|
2024-06-27 21:52:46 +08:00
|
|
|
|
|
|
|
|
|
reg [2:0] state, nextState;
|
2024-06-28 19:38:36 +08:00
|
|
|
|
reg [BUFF_SIZE - 1:0] data_cal[3]; // 用于保存运算结果,防止溢出
|
2024-06-27 21:52:46 +08:00
|
|
|
|
|
|
|
|
|
always @(posedge clk or posedge reset) begin
|
|
|
|
|
if (reset) begin
|
|
|
|
|
state <= READ_DATA;
|
|
|
|
|
end else begin
|
|
|
|
|
state <= nextState;
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
always @(*) begin
|
|
|
|
|
case (state)
|
2024-06-28 21:20:29 +08:00
|
|
|
|
READ_DATA: nextState = (in_en) ? CALC_DATA : READ_DATA;
|
2024-06-28 19:38:36 +08:00
|
|
|
|
CALC_DATA: nextState = SATI_DATA;
|
|
|
|
|
SATI_DATA: nextState = SEND_DATA;
|
2024-06-28 11:01:27 +08:00
|
|
|
|
SEND_DATA: nextState = (in_receive) ? READ_DATA : SEND_DATA;
|
2024-06-28 19:38:36 +08:00
|
|
|
|
default: nextState = READ_DATA;
|
2024-06-27 21:52:46 +08:00
|
|
|
|
endcase
|
|
|
|
|
end
|
|
|
|
|
|
2024-06-28 19:38:36 +08:00
|
|
|
|
assign out_ready = (!in_en && state == READ_DATA) ? 1 : 0;
|
|
|
|
|
assign out_receive = (in_en && state == READ_DATA) ? 1 : 0;
|
2024-06-27 21:52:46 +08:00
|
|
|
|
|
|
|
|
|
always @(posedge clk or posedge reset) begin
|
|
|
|
|
if (reset) begin
|
|
|
|
|
// 初始化
|
|
|
|
|
data_cal[0] <= 0;
|
|
|
|
|
data_cal[1] <= 0;
|
|
|
|
|
data_cal[2] <= 0;
|
2024-06-28 19:38:36 +08:00
|
|
|
|
|
|
|
|
|
out_data <= 0;
|
|
|
|
|
out_en <= 0;
|
2024-06-27 21:52:46 +08:00
|
|
|
|
end else begin
|
|
|
|
|
case (state)
|
|
|
|
|
READ_DATA: begin
|
|
|
|
|
if (in_en) begin
|
2024-06-28 19:38:36 +08:00
|
|
|
|
data_cal[0] <= ({{(BUFF_SIZE - 16){1'b0}}, in_data[0]}) << (8 - (IN_DEPTH - OUT_DEPTH));
|
|
|
|
|
data_cal[1] <= ({{(BUFF_SIZE - 16){1'b0}}, in_data[1]}) << (8 - (IN_DEPTH - OUT_DEPTH));
|
|
|
|
|
data_cal[2] <= ({{(BUFF_SIZE - 16){1'b0}}, in_data[2]}) << (8 - (IN_DEPTH - OUT_DEPTH));
|
2024-06-27 21:52:46 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
CALC_DATA: begin
|
2024-06-28 21:20:29 +08:00
|
|
|
|
if (color_correction) begin
|
|
|
|
|
data_cal[0] <= (data_cal[0] * {{(BUFF_SIZE - 16){1'b0}}, gain_red}) >> 16;
|
|
|
|
|
data_cal[1] <= (data_cal[1] * {{(BUFF_SIZE - 16){1'b0}}, gain_green}) >> 16;
|
|
|
|
|
data_cal[2] <= (data_cal[2] * {{(BUFF_SIZE - 16){1'b0}}, gain_blue}) >> 16;
|
|
|
|
|
end
|
|
|
|
|
else begin
|
|
|
|
|
data_cal[0] <= data_cal[0] >> 8;
|
|
|
|
|
data_cal[1] <= data_cal[1] >> 8;
|
|
|
|
|
data_cal[2] <= data_cal[2] >> 8;
|
|
|
|
|
end
|
2024-06-28 19:38:36 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
SATI_DATA: begin
|
|
|
|
|
data_cal[0] <= |data_cal[0][BUFF_SIZE -1 :OUT_DEPTH] ? {BUFF_SIZE{1'b1}} : data_cal[0];
|
|
|
|
|
data_cal[1] <= |data_cal[0][BUFF_SIZE -1 :OUT_DEPTH] ? {BUFF_SIZE{1'b1}} : data_cal[1];
|
|
|
|
|
data_cal[2] <= |data_cal[0][BUFF_SIZE -1 :OUT_DEPTH] ? {BUFF_SIZE{1'b1}} : data_cal[2];
|
2024-06-27 21:52:46 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
SEND_DATA: begin
|
2024-06-28 19:38:36 +08:00
|
|
|
|
if (in_ready && !in_receive) begin
|
|
|
|
|
out_en <= 1;
|
|
|
|
|
out_data <= {
|
|
|
|
|
data_cal[0][OUT_DEPTH-1:0], data_cal[1][OUT_DEPTH-1:0], data_cal[2][OUT_DEPTH-1:0]
|
|
|
|
|
};
|
|
|
|
|
end else out_en <= 0;
|
2024-06-27 21:52:46 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
default: ;
|
|
|
|
|
endcase
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
endmodule
|