module rgb2hsv(//from SDRAM
iRed,
iGreen,
iBlue,
//output
oHue,
oSat,
oVal,
//control signal
iCLK,
iRST_N
);
//from SDRAM
input [7:0] iRed;
input [7:0] iGreen;
input [7:0] iBlue;
//output
output reg [7:0] oHue;
output reg [7:0] oSat;
output reg [7:0] oVal;
// Control Signal
input iCLK;
input iRST_N;
reg [7:0] maxim;
reg [7:0] minim;
wire [7:0] dif;
wire [7:0] a;
//calculate max
always @ (posedge iCLK or negedge iRST_N)
begin
if(!iRST_N)
begin
maxim <= 0;
end
else
begin
if (iRed >= iGreen && iRed >= iBlue)
begin
maxim <= iRed;
end
else if (iGreen >= iRed && iGreen >= iBlue)
begin
maxim <= iGreen;
end
else if (iBlue >= iGreen && iBlue >= iRed)
begin
maxim <= iBlue;
end
end
end
//calculate min
always @ (posedge iCLK or negedge iRST_N)
begin
if(!iRST_N)
begin
minim <= 0;
end
else
begin
if (iRed <= iGreen && iRed <= iBlue)
begin
minim <= iRed;
end
else if (iGreen <= iRed && iGreen <= iBlue)
begin
minim <= iGreen;
end
else if (iBlue <= iGreen && iBlue <= iRed)
begin
minim <= iBlue;
end
end
end
//calculate max-min
assign dif = maxim - minim;
assign a = ((iGreen - iBlue) * 60)/dif;
//compute for hue
always @ (posedge iCLK or negedge iRST_N)
begin
if(!iRST_N)
begin
oHue <= 0;
end
else
begin
if (iRed >= iGreen && iRed >= iBlue)
begin
oHue <= a % 360;
end
else if (iGreen >= iRed && iGreen >= iBlue)
begin
oHue <= (((iBlue - iRed) * 60)/dif) + 120;
end
else if (iBlue >= iRed && iBlue >= iGreen)
begin
oHue <=(((iRed-iGreen) * 60)/dif) + 240;
end
else
begin
oHue <= 0;
end
end
end
//compute for value
always @ (posedge iCLK or negedge iRST_N)
begin
if(!iRST_N)
begin
oVal <= 0;
end
else
begin
oVal <= maxim;
end
end
//compute for saturation
always @ (posedge iCLK or negedge iRST_N)
begin
if(!iRST_N)
begin
oSat <= 0;
end
else
begin
if (maxim == 0)
begin
oSat <= 0;
end
else
begin
oSat <= dif/maxim;
end
end
end
endmodule