2024-06-27 21:52:46 +08:00
|
|
|
|
`timescale 1ns / 1ps
|
|
|
|
|
|
|
|
|
|
// 三通道图像合成一个RGB图像
|
|
|
|
|
module ColorBlender #(
|
|
|
|
|
parameter reg [ 4:0] IN_DEPTH = 12, // 输入图像的色深
|
|
|
|
|
parameter reg [ 4:0] OUT_DEPTH = 8, // 输出图像的色深
|
2024-06-28 11:01:27 +08:00
|
|
|
|
parameter reg [16:0] GAIN_RED = 120, // 红色增益系数
|
|
|
|
|
parameter reg [16:0] GAIN_GREEN = 50, // 绿色增益系数
|
|
|
|
|
parameter reg [16:0] GAIN_BLUE = 95, // 蓝色增益系数
|
2024-06-27 21:52:46 +08:00
|
|
|
|
parameter reg [ 4:0] GAIN_OFFSET = 7
|
|
|
|
|
) (
|
|
|
|
|
input clk,
|
|
|
|
|
input reset,
|
|
|
|
|
|
|
|
|
|
input in_en,
|
|
|
|
|
input [15:0] data_in[2], // 0:R 1:G 2:B
|
|
|
|
|
output out_ready,
|
|
|
|
|
output out_receive,
|
|
|
|
|
|
|
|
|
|
// 输出相关
|
|
|
|
|
input in_ready,
|
|
|
|
|
input in_receive,
|
|
|
|
|
output out_en,
|
2024-06-28 11:01:27 +08:00
|
|
|
|
output [3 * OUT_DEPTH - 1:0] out_data,
|
2024-06-27 21:52:46 +08:00
|
|
|
|
|
|
|
|
|
// 颜色校正
|
|
|
|
|
input wire color_correction
|
|
|
|
|
);
|
|
|
|
|
localparam READ_DATA = 0;
|
|
|
|
|
localparam CALC_DATA = 1;
|
|
|
|
|
localparam SEND_DATA = 2;
|
|
|
|
|
|
|
|
|
|
reg [2:0] state, nextState;
|
|
|
|
|
reg [31:0] data_cal[2]; // 用于保存运算结果,防止溢出
|
|
|
|
|
|
|
|
|
|
always @(posedge clk or posedge reset) begin
|
|
|
|
|
if (reset) begin
|
|
|
|
|
state <= READ_DATA;
|
|
|
|
|
end else begin
|
|
|
|
|
state <= nextState;
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
always @(*) begin
|
|
|
|
|
case (state)
|
|
|
|
|
READ_DATA: nextState = (in_en) ? (color_correction ? CALC_DATA : SEND_DATA) : READ_DATA;
|
|
|
|
|
CALC_DATA: nextState = SEND_DATA;
|
2024-06-28 11:01:27 +08:00
|
|
|
|
SEND_DATA: nextState = (in_receive) ? READ_DATA : SEND_DATA;
|
2024-06-27 21:52:46 +08:00
|
|
|
|
default: nextState = READ_DATA;
|
|
|
|
|
endcase
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
assign out_ready = (state == READ_DATA) ? 1 : 0;
|
|
|
|
|
assign out_receive = in_en ? 1 : 0;
|
|
|
|
|
assign out_en = (in_ready && state == SEND_DATA) ? 1 : 0;
|
|
|
|
|
|
|
|
|
|
always @(posedge clk or posedge reset) begin
|
|
|
|
|
if (reset) begin
|
|
|
|
|
// 初始化
|
|
|
|
|
data_cal[0] <= 0;
|
|
|
|
|
data_cal[1] <= 0;
|
|
|
|
|
data_cal[2] <= 0;
|
|
|
|
|
end else begin
|
|
|
|
|
case (state)
|
|
|
|
|
READ_DATA: begin
|
|
|
|
|
if (in_en) begin
|
2024-06-28 11:01:27 +08:00
|
|
|
|
data_cal[0] <= ({16'b0, data_in[0]} << 8;
|
|
|
|
|
data_cal[1] <= ({16'b0, data_in[1]} << 8;
|
|
|
|
|
data_cal[2] <= ({16'b0, data_in[2]} << 8;
|
2024-06-27 21:52:46 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
CALC_DATA: begin
|
2024-06-28 11:01:27 +08:00
|
|
|
|
data_cal[0] <= data_cal[0] * (GAIN_RED << 8)
|
|
|
|
|
data_cal[1] <= data_cal[1] * (GAIN_GREEN << 8)
|
|
|
|
|
data_cal[2] <= data_cal[2] * (GAIN_BLUE << 8)
|
2024-06-27 21:52:46 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
SEND_DATA: begin
|
2024-06-28 11:01:27 +08:00
|
|
|
|
if (in_ready) begin
|
|
|
|
|
out_data[0] <= data_cal[0] >> (8 + IN_DEPTH - OUT_DEPTH)
|
|
|
|
|
out_data[1] <= data_cal[1] >> (8 + IN_DEPTH - OUT_DEPTH)
|
|
|
|
|
out_data[2] <= data_cal[2] >> (8 + IN_DEPTH - OUT_DEPTH)
|
|
|
|
|
end
|
2024-06-27 21:52:46 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
default: ;
|
|
|
|
|
endcase
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
endmodule
|