//**************************************************************************
// *** file name : RGB2YUV.v
// *** version : 1.0
// *** Description : RGB2YUV
// *** Blogs : https://www.cnblogs.com/WenGalois123/
// *** Author : Galois_V
// *** Date : 2022.08.16
// *** Changes : Initial
//**************************************************************************
`timescale 1ns/1ps
module RGB2YUV
(
input i_sys_clk ,
input i_sys_rstn ,
input i_turn_en ,
input [7:0] i_r_data ,
input [7:0] i_g_data ,
input [7:0] i_b_data ,
output reg [7:0] o_y_data ,
output reg [7:0] o_u_data ,
output reg [7:0] o_v_data
);
reg [7:0] r_r_data ;
reg [7:0] r_g_data ;
reg [7:0] r_b_data ;
reg [17:0] r_y_rpart;
reg [17:0] r_y_gpart;
reg [17:0] r_y_bpart;
reg [17:0] r_u_rpart;
reg [17:0] r_u_gpart;
reg [17:0] r_u_bpart;
reg [17:0] r_v_rpart;
reg [17:0] r_v_gpart;
reg [17:0] r_v_bpart;
reg [20:0] r_y_sum ;
reg [20:0] r_u_sum ;
reg [20:0] r_v_sum ;
always@(posedge i_sys_clk)
begin
if(~i_sys_rstn)
begin
r_r_data <= 'd0;
r_g_data <= 'd0;
r_b_data <= 'd0;
end
else if(i_turn_en)
begin
r_r_data <= i_r_data;
r_g_data <= i_g_data;
r_b_data <= i_b_data;
end
end
/******************************************************************************\
calculate Y = 0.299*R + 0.587*G + 0.114*B
\******************************************************************************/
always@(posedge i_sys_clk)
begin
if(~i_sys_rstn)
begin
r_y_rpart <= 'd0;
r_y_gpart <= 'd0;
r_y_bpart <= 'd0;
r_y_sum <= 'd0;
end
else if(i_turn_en)
begin
r_y_rpart <= r_r_data * 306;
r_y_gpart <= r_g_data * 601;
r_y_bpart <= r_b_data * 117;
r_y_sum <= r_y_rpart + r_y_gpart + r_y_bpart;
end
end
/******************************************************************************\
calculate U = -0.147*R - 0.289*G + 0.436*B
\******************************************************************************/
always@(posedge i_sys_clk)
begin
if(~i_sys_rstn)
begin
r_u_rpart <= 'd0;
r_u_gpart <= 'd0;
r_u_bpart <= 'd0;
r_u_sum <= 'd0;
end
else if(i_turn_en)
begin
r_u_rpart <= r_r_data * 150;
r_u_gpart <= r_g_data * 296;
r_u_bpart <= r_b_data * 446;
r_u_sum <= r_u_bpart - r_u_gpart - r_u_rpart;
end
end
/******************************************************************************\
calculate V = 0.615*R - 0.515*G - 0.100*B
\******************************************************************************/
always@(posedge i_sys_clk)
begin
if(~i_sys_rstn)
begin
r_v_rpart <= 'd0;
r_v_gpart <= 'd0;
r_v_bpart <= 'd0;
r_v_sum <= 'd0;
end
else if(i_turn_en)
begin
r_v_rpart <= r_r_data * 630;
r_v_gpart <= r_g_data * 527;
r_v_bpart <= r_b_data * 102;
r_v_sum <= r_v_rpart- r_v_gpart - r_v_bpart;
end
end
/******************************************************************************\
output Y
\******************************************************************************/
always@(posedge i_sys_clk)
begin
if(~i_sys_rstn)
begin
o_y_data <= 'd0;
end
else if(i_turn_en)
begin
if(r_y_sum[20])
begin
o_y_data <= 'd0;
end
else if(r_y_sum[19:10]>255)
begin
o_y_data <= 8'hff;
end
else
begin
o_y_data <= r_y_sum[17:10];
end
end
end
/******************************************************************************\
output U
\******************************************************************************/
always@(posedge i_sys_clk)
begin
if(~i_sys_rstn)
begin
o_u_data <= 'd0;
end
else if(i_turn_en)
begin
if(r_u_sum[20])
begin
o_u_data <= 'd0;
end
else if(r_u_sum[19:10]>255)
begin
o_u_data <= 8'hff;
end
else
begin
o_u_data <= r_u_sum[17:10];
end
end
end
/******************************************************************************\
output v
\******************************************************************************/
always@(posedge i_sys_clk)
begin
if(~i_sys_rstn)
begin
o_v_data <= 'd0;
end
else if(i_turn_en)
begin
if(r_v_sum[20])
begin
o_v_data <= 'd0;
end
else if(r_v_sum[19:10]>255)
begin
o_v_data <= 8'hff;
end
else
begin
o_v_data <= r_v_sum[17:10];
end
end
end
endmodule