OpenCores
URL https://opencores.org/ocsvn/video_systems/video_systems/trunk

Subversion Repositories video_systems

Compare Revisions

  • This comparison shows the changes necessary to convert path
    /video_systems/trunk/common/color_space converters/rgb2ycrcb
    from Rev 24 to Rev 25
    Reverse comparison

Rev 24 → Rev 25

/bench/verilog/testbench.v
0,0 → 1,191
/////////////////////////////////////////////////////////////////////
//// ////
//// Testbench for Color Space converters ////
//// ////
//// ////
//// Author: Richard Herveille ////
//// richard@asics.ws ////
//// www.asics.ws ////
//// ////
//// ////
/////////////////////////////////////////////////////////////////////
//// ////
//// Copyright (C) 2001 Richard Herveille ////
//// richard@asics.ws ////
//// ////
//// This source file may be used and distributed without ////
//// restriction provided that this copyright statement is not ////
//// removed from the file and that any derivative work contains ////
//// the original copyright notice and the associated disclaimer.////
//// ////
//// THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY ////
//// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED ////
//// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS ////
//// FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL THE AUTHOR ////
//// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, ////
//// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES ////
//// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE ////
//// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR ////
//// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF ////
//// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ////
//// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT ////
//// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE ////
//// POSSIBILITY OF SUCH DAMAGE. ////
//// ////
/////////////////////////////////////////////////////////////////////
 
// CVS Log
//
// $Id: testbench.v,v 1.1.1.1 2002-03-26 07:25:03 rherveille Exp $
//
// $Date: 2002-03-26 07:25:03 $
// $Revision: 1.1.1.1 $
// $Author: rherveille $
// $Locker: $
// $State: Exp $
//
// Change History:
// $Log: not supported by cvs2svn $
 
 
`timescale 1ns/10ps
 
module testbench();
 
parameter emargin = 1; // we allow a small error
parameter debug = 0;
parameter r_runlength = 1023;
parameter g_runlength = 1023;
parameter b_runlength = 1023;
 
// variables
reg clk;
reg ena;
 
reg [9:0] r [7:0];
reg [9:0] g [7:0];
reg [9:0] b [7:0];
 
wire [9:0] y, cr, cb;
 
integer my, mcr, mcb;
integer iy, icr, icb;
 
 
//
// module body
//
 
// hookup modules
rgb2ycrcb dut (
.clk(clk),
.ena(ena),
.r(r[0]),
.g(g[0]),
.b(b[0]),
.y(y),
.cr(cr),
.cb(cb)
);
 
always #5 clk <= ~clk;
 
initial
begin
clk = 0;
ena = 1;
r[0] = 0;
g[0] = 0;
b[0] = 0;
 
$display ("\n *** Color Space Converter testbench started ***\n");
end
 
always
while ( (r[0] <= r_runlength) && (g[0] <= g_runlength) && (b[0] <= b_runlength))
begin
@(posedge clk);
 
b[0] <= #1 b[0] +1;
if (b[0] == b_runlength)
begin
b[0] <= #1 0;
 
g[0] <= #1 g[0] +1;
if (g[0] == g_runlength)
begin
g[0] <= #1 0;
 
r[0] <= #1 r[0] +1;
end
end
 
if (debug)
$display("r[0] = %d, g[0] = %d, b[0] = %d", r[0], g[0], b[0]);
 
if ( (r[0]==r_runlength) && (g[0]==g_runlength) && (b[0]==b_runlength) )
begin
$display ("\n *** Color Space Converter testbench ended ***\n");
$stop;
end
end
 
 
integer n;
always@(posedge clk)
begin
for (n = 0; n < 7; n = n +1)
begin
r[n +1] <= #1 r[n];
g[n +1] <= #1 g[n];
b[n +1] <= #1 b[n];
end
end
 
always@(r[3] or g[3] or b[3])
begin
my = (299 * r[3]) + (587 * g[3]) + (114 * b[3]);
if (my < 0)
my = 0;
 
my = my /1000;
if (my > 1024)
my = 1024;
 
mcr = (500 * r[3]) - (419 * g[3]) - ( 81 * b[3]);
if (mcr < 0)
mcr = 0;
 
mcr = mcr /1000;
if (mcr > 1024)
mcr = 1024;
 
mcb = (500 * b[3]) - (169 * r[3]) - (332 * g[3]);
if (mcb < 0)
mcb = 0;
 
mcb = mcb /1000;
if (mcb > 1024)
mcb = 1024;
end
 
always@(posedge clk)
begin
 
// check results
iy = y;
if ( ( iy < my - emargin) || (iy > my + emargin) )
$display("Y-value error. Received %d, expected %d. R = %d, G = %d, B = %d", y, my, r[3], g[3], b[3]);
 
icr = cr;
if ( ( icr < mcr - emargin) || (icr > mcr + emargin) )
$display("Cr-value error. Received %d, expected %d. R = %d, G = %d, B = %d", cr, mcr, r[3], g[3], b[3]);
 
icb = cb;
if ( ( icb < mcb - emargin) || (icb > mcb + emargin) )
$display("Cb-value error. Received %d, expected %d. R = %d, G = %d, B = %d", cb, mcb, r[3], g[3], b[3]);
end
 
endmodule
 
/rtl/verilog/rgb2ycrcb.v
0,0 → 1,155
/////////////////////////////////////////////////////////////////////
//// ////
//// RGB to YCrCb Color Space converter ////
//// ////
//// Converts RGB values to YCrCB (YUV) values ////
//// Y = 0.299R + 0.587G + 0.114B ////
//// Cr = 0.713(R - Y) ////
//// Cb = 0.565(B - Y) ////
//// ////
//// Author: Richard Herveille ////
//// richard@asics.ws ////
//// www.asics.ws ////
//// ////
//// ////
/////////////////////////////////////////////////////////////////////
//// ////
//// Copyright (C) 2001 Richard Herveille ////
//// richard@asics.ws ////
//// ////
//// This source file may be used and distributed without ////
//// restriction provided that this copyright statement is not ////
//// removed from the file and that any derivative work contains ////
//// the original copyright notice and the associated disclaimer.////
//// ////
//// THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY ////
//// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED ////
//// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS ////
//// FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL THE AUTHOR ////
//// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, ////
//// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES ////
//// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE ////
//// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR ////
//// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF ////
//// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ////
//// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT ////
//// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE ////
//// POSSIBILITY OF SUCH DAMAGE. ////
//// ////
/////////////////////////////////////////////////////////////////////
 
// CVS Log
//
// $Id: rgb2ycrcb.v,v 1.1.1.1 2002-03-26 07:25:01 rherveille Exp $
//
// $Date: 2002-03-26 07:25:01 $
// $Revision: 1.1.1.1 $
// $Author: rherveille $
// $Locker: $
// $State: Exp $
//
// Change History:
// $Log: not supported by cvs2svn $
 
 
`timescale 1ns/10ps
 
module rgb2ycrcb(clk, ena, r, g, b, y, cr, cb);
//
// inputs & outputs
//
input clk;
input ena;
input [9:0] r, g, b;
 
output [9:0] y, cr, cb;
reg [9:0] y, cr, cb;
 
 
//
// variables
//
reg [21:0] y1, cr1, cb1;
 
//
// module body
//
 
 
// step 1: Calculate Y, Cr, Cb
//
// Use N.M format for multiplication:
// Y = 0.299 * R.000 + 0.587 * G.000 + 0.114 * B.000
// Y = 0x132 * R + 0x259 * G + 0x074 * B
//
// Cr = 0.713(R - Y)
// Cr = 0.500 * R.000 + -0.419 * G.000 - 0.0813 * B.000
// Cr = (R >> 1) - 0x1AD * G - 0x053 * B
//
// Cb = 0.565(B - Y)
// Cb = -0.169 * R.000 + -0.332 * G.000 + 0.500 * B.000
// Cb = (B >> 1) - 0x0AD * R - 0x153 * G
 
 
// calculate Y
reg [19:0] yr, yg, yb;
 
always@(posedge clk)
if (ena)
begin
yr <= #1 10'h132 * r;
yg <= #1 10'h259 * g;
yb <= #1 10'h074 * b;
 
y1 <= #1 yr + yg + yb;
end
 
// calculate Cr
reg [19:0] crr, crg, crb;
 
always@(posedge clk)
if (ena)
begin
crr <= #1 r << 9;
crg <= #1 10'h1ad * g;
crb <= #1 10'h053 * b;
 
cr1 <= #1 crr - crg - crb;
end
 
// calculate Cb
reg [19:0] cbr, cbg, cbb;
 
always@(posedge clk)
if (ena)
begin
cbr <= #1 10'h0ad * r;
cbg <= #1 10'h153 * g;
cbb <= #1 b << 9;
 
cb1 <= #1 cbb - cbr - cbg;
end
 
//
// step2: check boundaries
//
always@(posedge clk)
if (ena)
begin
// check Y
y <= #1 (y1[19:10] & {10{!y1[21]}}) | {10{(!y1[21] && y1[20])}};
 
// check Cr
cr <= #1 (cr1[19:10] & {10{!cr1[21]}}) | {10{(!cr1[21] && cr1[20])}};
 
// check Cb
cb <= #1 (cb1[19:10] & {10{!cb1[21]}}) | {10{(!cb1[21] && cb1[20])}};
end
endmodule
 
 
 
 
 
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.