Hard-PNG/SIM/tb_hard_png.v

151 lines
4.6 KiB
Coq
Raw Permalink Normal View History

2022-03-29 19:41:01 +08:00
//--------------------------------------------------------------------------------------------------------
// Module : tb_hard_png
// Type : simulation, top
2023-06-07 20:54:14 +08:00
// Standard: Verilog 2001 (IEEE1364-2001)
2022-03-29 19:41:01 +08:00
// Function: testbench for hard_png
//--------------------------------------------------------------------------------------------------------
`timescale 1ps/1ps
2022-04-02 19:45:58 +08:00
`define START_NO 1 // first png file number to decode
`define FINAL_NO 14 // last png file number to decode
2022-03-29 19:41:01 +08:00
`define IN_PNG_FILE_FOMRAT "test_image/img%02d.png"
`define OUT_TXT_FILE_FORMAT "out%02d.txt"
module tb_hard_png ();
2023-06-07 20:54:14 +08:00
initial $dumpvars(1, tb_hard_png);
2022-03-29 19:41:01 +08:00
reg rstn = 1'b0;
reg clk = 1'b1;
2022-04-02 19:45:58 +08:00
always #10000 clk = ~clk; // 50MHz
initial begin repeat(4) @(posedge clk); rstn<=1'b1; end
2022-03-29 19:41:01 +08:00
2022-04-02 19:45:58 +08:00
2023-06-07 20:54:14 +08:00
reg istart = 1'b0;
2022-03-29 19:41:01 +08:00
reg ivalid = 1'b0;
wire iready;
2023-06-07 20:54:14 +08:00
reg [ 7:0] ibyte = 0;
2022-03-29 19:41:01 +08:00
2022-04-02 19:45:58 +08:00
wire ostart;
2022-03-29 19:41:01 +08:00
wire [ 2:0] colortype;
wire [13:0] width;
wire [31:0] height;
wire ovalid;
wire [ 7:0] opixelr, opixelg, opixelb, opixela;
hard_png hard_png_i (
.rstn ( rstn ),
.clk ( clk ),
// data input
2022-04-02 19:45:58 +08:00
.istart ( istart ),
2022-03-29 19:41:01 +08:00
.ivalid ( ivalid ),
.iready ( iready ),
.ibyte ( ibyte ),
// image size output
2022-04-02 19:45:58 +08:00
.ostart ( ostart ),
2022-03-29 19:41:01 +08:00
.colortype ( colortype ),
.width ( width ),
.height ( height ),
// data output
.ovalid ( ovalid ),
.opixelr ( opixelr ),
.opixelg ( opixelg ),
.opixelb ( opixelb ),
.opixela ( opixela )
);
2023-06-07 20:54:14 +08:00
integer fptxt = 0, fppng = 0;
2022-03-29 19:41:01 +08:00
reg [256*8:1] fname_png;
reg [256*8:1] fname_txt;
2023-06-07 20:54:14 +08:00
integer png_no = 0;
integer txt_no = 0;
integer ii;
integer cyccnt = 0;
integer bytecnt = 0;
2022-03-29 19:41:01 +08:00
initial begin
2022-04-02 19:45:58 +08:00
while(~rstn) @(posedge clk);
2022-03-29 19:41:01 +08:00
fork
// thread: input png file
for(png_no=`START_NO; png_no<=`FINAL_NO; png_no=png_no+1) begin
2022-04-02 19:45:58 +08:00
istart <= 1'b1;
2022-03-29 19:41:01 +08:00
@ (posedge clk);
2022-04-02 19:45:58 +08:00
istart <= 1'b0;
2022-03-29 19:41:01 +08:00
$sformat(fname_png, `IN_PNG_FILE_FOMRAT , png_no);
fppng = $fopen(fname_png, "rb");
if(fppng == 0) begin
$error("input file %s open failed", fname_png);
$finish;
end
cyccnt = 0;
2022-04-02 19:45:58 +08:00
bytecnt = 0;
2022-03-29 19:41:01 +08:00
2022-04-02 19:45:58 +08:00
$display("\nstart to decode %30s", fname_png );
2022-03-29 19:41:01 +08:00
ibyte <= $fgetc(fppng);
while( !$feof(fppng) ) @(posedge clk) begin
if(~ivalid | iready ) begin
2022-04-02 19:45:58 +08:00
ivalid <= 1'b1; // A. use this to always try to input a byte to hard_png (no bubble, will get maximum throughput)
//ivalid <= ($random % 3) == 0; // B. use this to add random bubbles to the input stream of hard_png. (Although the maximum throughput cannot be achieved, it allows input with mismatched rate, which is more common in the actual engineering scenarios)
2022-03-29 19:41:01 +08:00
end
if( ivalid & iready ) begin
ibyte <= $fgetc(fppng);
2023-06-07 20:54:14 +08:00
bytecnt = bytecnt + 1;
2022-03-29 19:41:01 +08:00
end
2023-06-07 20:54:14 +08:00
cyccnt = cyccnt + 1;
2022-03-29 19:41:01 +08:00
end
ivalid <= 1'b0;
$fclose(fppng);
$display("image %30s decode done, input %d bytes in %d cycles, throughput=%f byte/cycle", fname_png, bytecnt, cyccnt, (1.0*bytecnt)/cyccnt );
end
// thread: output txt file
for(txt_no=`START_NO; txt_no<=`FINAL_NO; txt_no=txt_no+1) begin
$sformat(fname_txt, `OUT_TXT_FILE_FORMAT , txt_no);
2022-04-02 19:45:58 +08:00
while(~ostart) @ (posedge clk);
2022-03-29 19:41:01 +08:00
$display("decode result: colortype:%1d width:%1d height:%1d", colortype, width, height);
fptxt = $fopen(fname_txt, "w");
if(fptxt != 0)
$fwrite(fptxt, "decode result: colortype:%1d width:%1d height:%1d\n", colortype, width, height);
else begin
$error("output txt file %30s open failed", fname_txt);
$finish;
end
2023-06-07 20:54:14 +08:00
for(ii=0; ii<width*height; ii=ii+1) begin
2022-03-29 19:41:01 +08:00
@ (posedge clk);
while(~ovalid) @ (posedge clk);
$fwrite(fptxt, "%02x%02x%02x%02x ", opixelr, opixelg, opixelb, opixela);
if( (ii % (width*height/10)) == 0 ) $display("%d/%d", ii, width*height);
end
$fclose(fptxt);
end
join
repeat(100) @ (posedge clk);
$finish;
end
endmodule