commit 8a22fbd1a47a2c00a00741a511dd90a07adfcea0 Author: WangXuan95 <629708558@qq.com> Date: Mon Nov 1 15:44:10 2021 +0800 release diff --git a/README.md b/README.md new file mode 100644 index 0000000..71ec2c7 --- /dev/null +++ b/README.md @@ -0,0 +1,126 @@ +![test](https://img.shields.io/badge/test-passing-green.svg) +![docs](https://img.shields.io/badge/docs-passing-green.svg) +![platform](https://img.shields.io/badge/platform-Quartus|Vivado-blue.svg) + + +Hard-PNG +=========================== +基于**FPGA**的流式的**png**图象解码器 + + + +# 特点 +* 支持宽度不大于**4000像素**的png图片,对图片高度没有限制。 +* **支持所有颜色类型**: 灰度、灰度透明、RGB、索引RGB、RGBA。 +* 仅支持**8bit深度**,大多数png图片都是**8bit深度**。 +* 完全使用**SystemVerilog**实现,方便移植和仿真。 + +| ![框图](https://github.com/WangXuan95/Hard-PNG/blob/master/images/blockdiagram.png) | +| :----: | +| **图1** : Hard-PNG 原理框图 | + +# 背景知识 + +**png**是仅次于**jpg**的第二常见的图象压缩格式,相比于**jpg**,**png**支持透明通道,支持无损压缩。在色彩丰富的数码照片中,无损压缩的**png**只能获得**1~4倍**的压缩比,低失真有损压缩的**png**能获得**4~20倍**的压缩比。在色彩较少的人工合成图(例如框图、平面设计)中,无损压缩的**png**就能获得**10倍**以上的压缩比。因此,**png**更适合压缩人工合成图,**jpg**更适合压缩数码照片。 + +**png** 图片的文件扩展名为 **.png** 。以我们提供的文件 [**test1.png**](https://github.com/WangXuan95/Hard-PNG/blob/master/images/test1.png) 为例,它包含**98字节**,称为**原始码流**。我们可以使用[**WinHex软件**](http://www.x-ways.net/winhex/)查看它: +``` +0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, ...... , 0xAE, 0x42, 0x60, 0x82 +``` +该图象文件解压后只有**4列2行**,共**8个像素**,16进制表示如下表。其中R, G, B, A分别代表像素的**红**、**绿**、**蓝**、**透明**通道。 + +| | 列 1 | 列 2 | 列 3 | 列 4 | +| :---: | :---: | :---: | :---: | :---: | +| **行 1** | R:**FF** G:**F2** B:**00** A:**FF** | R:**ED** G:**1C** B:**24** A:**FF** | R:**00** G:**00** B:**00** A:**FF** | R:**3F** G:**48** B:**CC** A:**FF** | +| **行 2** | R:**7F** G:**7F** B:**7F** A:**FF** | R:**ED** G:**1C** B:**24** A:**FF** | R:**FF** G:**FF** B:**FF** A:**FF** | R:**FF** G:**AE** B:**CC** A:**FF** | + +# Hard-PNG 的使用 + +**Hard-PNG**是一个能够输入**原始码流**,输出**解压后的像素**的硬件模块,它的代码在 [**hard_png.sv**](https://github.com/WangXuan95/Hard-PNG/blob/master/hard_png.sv) 中。其中 **hard_png** 是顶层模块,它的接口如**图2**所示 + +| ![接口图](https://github.com/WangXuan95/Hard-PNG/blob/master/images/interface.png) | +| :----: | +| **图2** : **hard_png** 接口图 | + +它的使用方法很简单,首先需要给 **clk** 信号提供时钟(频率不限),并将 **rst** 信号置低,解除模块复位。 +然后将**原始码流**从**原始码流输入接口** 输入,就可以从**图象基本信息输出接口**和**像素输出接口**中得到解压结果。 + +以[**test1.png**](https://github.com/WangXuan95/Hard-PNG/blob/master/images/test1.png)为例,我们应该以**图3**的时序把**原始码流**(98个字节)输入**hard_png**中。 +该输入接口类似 **AXI-stream** ,其中 **ivalid=1** 时说明外部想发送一个字节给 **hard_png**。**iready=1** 时说明 **hard_png** 已经准备好接收一个字节。只有 **ivalid** 和 **iready** 同时 **=1** 时,**ibyte** 才被成功的输入 **hard_png** 中。 + +| ![输入时序图](https://github.com/WangXuan95/Hard-PNG/blob/master/images/wave1.png) | +| :----: | +| **图3** : **hard_png** 输入时序图,以 **test1.png** 为例 | + +在输入的同时,解压结果从模块中输出,如**图4**。在新的一帧图象输出前,**newframe** 信号会出现一个时钟周期的高电平脉冲,同时 **colortype, width, height** 保持有效直到该图象的所有像素输出完为止。其中 **width, height** 分别为图象的宽度和高度, **colortype** 的含义如下表。另外, **ovalid=1** 代表该时钟周期有一个像素输出,该像素的R,G,B,A通道分别出现在 **opixelr,opixelg,opixelb,opixela** 信号上。 + +| colortype | 2'd0 | 2'd1 | 2'd2 | 2'd3 | +| :-------: | :--: | :--: | :--: | :--: | +| **颜色类型** | 灰度图 | 灰度+透明 | RGB / 索引RGB | RGBA | +| **含义** | RGB通道相等, A通道=0xFF | RGB通道相等 | RGB通道不等, A通道=0xFF | RGBA通道均不等 | + +| ![输出时序图](https://github.com/WangXuan95/Hard-PNG/blob/master/images/wave2.png) | +| :----: | +| **图4** : **hard_png** 输出时序图,以 **test1.png** 为例 | + +当一个图象完全输入结束后,我们可以紧接着输入下一个图象进行解压。如果一个图象输入了一半,我们想打断当前解压进程并输入下一个图象,则需要将 **rst** 信号拉高至少一个时钟周期进行复位。 + + +# 仿真 + +[**tb_hard_png.sv**](https://github.com/WangXuan95/Hard-PNG/blob/master/tb_hard_png.sv) 是仿真的顶层,它从指定的 **.png** 文件中读取**原始码流**输入[**hard_png**](https://github.com/WangXuan95/Hard-PNG/blob/master/hard_png.sv)中,再接收**解压后的像素**并写入一个 **.txt** 文件。 + +仿真前,请将 [**tb_hard_png.sv**](https://github.com/WangXuan95/Hard-PNG/blob/master/tb_hard_png.sv) 中的**PNG_FILE宏名**改为 **.png** 文件的路径,将**OUT_FILE宏名**改为 **.txt** 文件的路径。然后运行仿真。 **.png** 文件越大,仿真的时间越长。当**ivalid**信号出现下降沿时,仿真完成。然后你可以从 **.txt** 文件中查看解压结果。 + +我们在 [**images文件夹**](https://github.com/WangXuan95/Hard-PNG/blob/master/images) 下提供了多个 **.png** 文件,它们尺寸各异,且有不同的颜色类型,你可以用它们进行仿真。以 [**test3.png**](https://github.com/WangXuan95/Hard-PNG/blob/master/images/test3.png) 为例,仿真得到的 **.txt** 文件如下: +``` +frame type:2 width:83 height:74 +f4d8c3ff f4d8c3ff f4d8c3ff f4d8c3ff f4d8c3ff f4d9c3ff ...... +``` +这代表图片的尺寸是**83x74**, **colortype** 是2(RGB),第1行第1列的像素是RGBA=(0xf4, 0xd8, 0xc3, 0xff),第1行第2列的像素是RGBA=(0xf4, 0xd8, 0xc3, 0xff),...... + +# 正确性验证 + +为了验证解压结果是否正确,我们提供了**Python**程序 [**validation.py**](https://github.com/WangXuan95/Hard-PNG/blob/master/validation.py) ,它对 **.png** 文件进行软件解压,并与仿真得到的 **.txt** 文件进行比较,若比较结果相同则验证通过。为了准备必要的运行环境,请安装**Python3**以及其配套的 [**numpy**](https://pypi.org/project/numpy/) 和 [**PIL**](https://pypi.org/project/Pillow/) 库。运行环境准备好后,打开 [**validation.py**](https://github.com/WangXuan95/Hard-PNG/blob/master/validation.py) ,将变量 **PNG_FILE** 改为要验证的 **.png** 文件的路径,将 **TXT_FILE** 改为仿真输出的 **.txt** 文件的路径,然后用命令运行它: +``` +python validation.py +``` +若验证通过,则打印 **"validation successful!!"** 。目前我们测试了几十张不同的 **.png** 图片,均验证通过。 + +# 性能测试 + +* **测试平台**: 在 Altera Cyclone IV EP4CE40F23C6 上运行 **Hard-PNG** 进行**png**解压,时钟频率= **50MHz** (正好时序收敛)。 +* **对比平台**: 使用**MSVC++编译器**以**O3优化级别**编译[**upng库**](https://github.com/elanthis/upng),在笔记本电脑(**Intel Core I7 8750H**)上运行**png**解压。 + +测试结果如下表,**Hard-PNG**的性能接近对比平台。由此可以推断,**Hard-PNG**的性能好于大部分**ARM嵌入式处理器**。 + +| **png文件名** | **颜色类型** | **图象尺寸** | **对比平台耗时** | **Hard-PNG 耗时** | +| :-----------: | :----------: | :----------: | :--------------: | :---------------: | +| test9.png | RGB | 631x742 | 83 ms | 204 ms | +| test10.png | 索引RGB | 631x742 | 不支持 | 48 ms | +| test11.png | RGBA | 1920x1080 | 402 ms | 993 ms | +| test12.png | 索引RGB | 1920x1080 | 不支持 | 204 ms | +| test13.png | RGB | 1819x1011 | 321 ms | 655 ms | +| test14.png | 黑白 | 1819x1011 | 135 ms | 227 ms | +| wave2.png | 索引RGB | 1427x691 | 不支持 | 27 ms | + + +# FPGA 资源消耗 + +下表是**hard_png模块**综合后占用的FPGA资源量。 + +| **FPGA 型号** | LUT | LUT(%) | FF | FF(%) | Logic | Logic(%) | BRAM | BRAM(%) | +| :--------------------------------: | :--: | :----: | :--: | :---: | :---: | :------: | :-----: | :-----: | +| **Xilinx Artix-7 XC7A35T** | 2581 | 13% | 2253 | 5% | - | - | 792kbit | 44% | +| **Altera Cyclone IV EP4CE40F23C6** | - | - | - | - | 4551 | 11% | 427kbit | 37% | + + + + +# 参考链接 + +感谢以下链接为我们提供参考。 + +* [**upng**](https://github.com/elanthis/upng): 一个轻量化的 C 语言 **png** 解码库 +* [**TinyPNG**](https://tinypng.com/): 一个利用索引 RGB 对 **png** 图片进行有损压缩的工具 +* [**PNG Specification**](https://www.w3.org/TR/REC-png.pdf): **png** 标准手册 diff --git a/hard_png.sv b/hard_png.sv new file mode 100644 index 0000000..8f60860 --- /dev/null +++ b/hard_png.sv @@ -0,0 +1,1954 @@ +`timescale 1 ns/1 ns + + + +module hard_png( + input wire rst, + input wire clk, + // png data input stream + input wire ivalid, + output wire iready, + input wire [ 7:0] ibyte, + // image frame configuration output + output wire newframe, + output wire [ 1:0] colortype, // 0:gray 1:gray+A 2:RGB 3:RGBA + output wire [13:0] width, // horizontal size / frame width / pixel per row + output wire [31:0] height, // vertical size / frame height / rows per frame + // pixel output + output wire ovalid, + output wire [ 7:0] opixelr, opixelg, opixelb, opixela +); + +wire reset; +wire [13:0] bpr; // bytes per row +wire [ 1:0] bpp; // bytes per pixel + +wire pvalid; +wire pready; +wire [ 7:0] pbyte; + +wire mvalid; +wire [ 7:0] mbyte; + +wire bvalid; +wire [ 7:0] bbyte; + +wire isplte; +wire plte_wen; +wire [ 7:0] plte_waddr; +wire [23:0] plte_wdata; +wire [ 7:0] plte_raddr; +wire [23:0] plte_rdata; + +assign colortype = isplte ? 2'd2 : bpp; + +png_parser png_parser_i( + .rst ( rst ), + .orst ( reset ), + .clk ( clk ), + .oframe ( newframe ), + .isplte ( isplte ), + .bpp ( bpp ), + .ppr ( width ), + .bpr ( bpr ), + .rpf ( height ), + .ivalid ( ivalid ), + .iready ( iready ), + .ibyte ( ibyte ), + .ovalid ( pvalid ), + .oready ( pready ), + .obyte ( pbyte ), + .plte_wen ( plte_wen ), + .plte_waddr ( plte_waddr ), + .plte_wdata ( plte_wdata ) +); + +uz_inflate uz_inflate_i( + .rst ( reset ), + .clk ( clk ), + .ivalid ( pvalid ), + .iready ( pready ), + .ibyte ( pbyte ), + .ovalid ( mvalid ), + .obyte ( mbyte ), + .end_stream ( ) +); + +unfilter unfilter_i( + .rst ( reset ), + .clk ( clk ), + .bpp ( bpp ), + .bpr ( bpr ), + .ivalid ( mvalid ), + .idata ( mbyte ), + .ovalid ( bvalid ), + .odata ( bbyte ) +); + +build_pixel build_pixel_i( + .clk ( clk ), + .newframe ( newframe ), + .bpp ( bpp ), + .isplte ( isplte ), + .plte_raddr ( plte_raddr ), + .plte_rdata ( plte_rdata ), + .ivalid ( bvalid ), + .ibyte ( bbyte ), + .ovalid ( ovalid ), + .opixelr ( opixelr ), + .opixelg ( opixelg ), + .opixelb ( opixelb ), + .opixela ( opixela ) +); + +RamSinglePort #( + .SIZE ( 256 ), + .WIDTH ( 24 ) +) ram_for_plte ( + .clk ( clk ), + .wen ( plte_wen ), + .waddr ( 8'(plte_waddr) ), + .wdata ( plte_wdata ), + .raddr ( 8'(plte_raddr) ), + .rdata ( plte_rdata ) +); + +endmodule + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +module build_pixel( + input wire clk, + input wire newframe, + input wire [ 1:0] bpp, + input wire isplte, + output wire [ 7:0] plte_raddr, + input wire [23:0] plte_rdata, + input wire ivalid, + input wire [ 7:0] ibyte, + output reg ovalid, + output wire [ 7:0] opixelr, opixelg, opixelb, opixela +); +initial ovalid = 1'b0; +reg [1:0] pixcnt = '0; +reg [7:0] pr='0, pg='0, pb='0, pa='0; + +assign plte_raddr = ibyte; + +assign opixelr = ovalid ? (isplte ? plte_rdata[23:16] : pr) : 8'h0; +assign opixelg = ovalid ? (isplte ? plte_rdata[15: 8] : pg) : 8'h0; +assign opixelb = ovalid ? (isplte ? plte_rdata[ 7: 0] : pb) : 8'h0; +assign opixela = ovalid ? (isplte ? 8'hff : pa) : 8'h0; + +always @ (posedge clk) + if(newframe) begin + pixcnt <= '0; + ovalid <= 1'b0; + {pr, pg, pb, pa} <= 0; + end else if(ivalid) begin + case(pixcnt) + 2'd0 : {pr, pg, pb, pa} <= {ibyte, ibyte, ibyte, 8'hff}; + 2'd1 : { pa} <= { ibyte}; + 2'd2 : { pg, pb, pa} <= { pa, ibyte, 8'hff}; + 2'd3 : { pa} <= { ibyte}; + endcase + if(pixcnt0 && curr_name==IDAT && gapcnt==2'd0) begin + ovalid <= 1'b1; + iready <= oready; + obyte <= ibyte; + end else begin + ovalid <= 1'b0; + iready <= 1'b1; + obyte <= '0; + end + +always @ (posedge clk or posedge rst) + if(rst) begin + bcnt <= '0; + cnt <= '0; + crccnt <= '0; + gapcnt <= '0; + busy <= 1'b0; + sizevalid <= 1'b0; + imagevalid <= 1'b0; + curr_name <= NONE; + ispltetmp <= 1'b0; + bpptmp <= '0; + pprtmp <= '0; + bprtmp <= '0; + rpftmp <= '0; + isplte <= 1'b0; + bpp <= '0; + ppr <= '0; + bpr <= '0; + rpf <= '0; + oframe <= 1'b0; + plte_wen <= 1'b0; + plte_waddr <= '0; + plte_wdata <= '0; + plte_bytecnt <= '0; + plte_pixcnt <= '0; + end else begin + oframe <= 1'b0; + plte_wen <= 1'b0; + plte_waddr <= '0; + plte_wdata <= '0; + if(ivalid) begin + plte_bytecnt <= '0; + plte_pixcnt <= '0; + if(~busy) begin + bcnt <= '0; + cnt <= '0; + crccnt <= '0; + busy <= (lastlbytes==png_precode); + end else begin + if(cnt>0) begin + bcnt <= '0; + if(curr_name==IHDR) begin + cnt <= cnt - 1; + gapcnt <= 2'd2; + if(cnt==6) begin + imagevalid <= 1'b0; + rpftmp <= l32bit; + if(h32bit[31:14]=='0) begin + sizevalid <= 1'b1; + pprtmp <= h32bit[13:0]; + end else begin + sizevalid <= 1'b0; + pprtmp <= '1; + end + end else if(cnt==3) begin + ispltetmp <= ispltes[lastlbytes[10:8]]; + bpptmp <= bpps[lastlbytes[10:8]]; + end else if(cnt==2) begin + case(bpptmp) + 2'd0 : bprtmp <= {2'b00, pprtmp}; + 2'd1 : bprtmp <= {1'b0, pprtmp, 1'b0}; + 2'd2 : bprtmp <= {1'b0, pprtmp, 1'b0} + {2'b00, pprtmp}; + 2'd3 : bprtmp <= {pprtmp, 2'b00}; + endcase + end else if(cnt==1) begin + if(sizevalid && parametervalid && (bprtmp[15:14]==2'd0)) begin + oframe <= 1'b1; + imagevalid <= 1'b1; + isplte <= ispltetmp; + bpp <= bpptmp; + ppr <= pprtmp; + bpr <= bprtmp[13:0]; + rpf <= rpftmp; + end else begin + imagevalid <= 1'b0; + isplte <= 1'b0; + bpp <= '0; + ppr <= '0; + bpr <= '0; + rpf <= '0; + end + end + end else if(curr_name==IDAT) begin + if(gapcnt>2'd0) + gapcnt <= gapcnt - 2'd1; + if(imagevalid && gapcnt==2'd0) begin + if(oready) + cnt <= cnt - 1; + end else begin + cnt <= cnt - 1; + end + end else if(curr_name==PLTE) begin + plte_pixcnt <= plte_pixcnt; + case(plte_bytecnt) + 2'd0 :plte_bytecnt <= 2'd1; + 2'd1 :plte_bytecnt <= 2'd2; + default:begin + plte_bytecnt <= 2'd0; + plte_pixcnt <= plte_pixcnt + 8'd1; + plte_wen <= 1'b1; + plte_waddr <= plte_pixcnt; + plte_wdata <= lastlbytes[23:0]; + end + endcase + cnt <= cnt - 1; + end else begin + cnt <= cnt - 1; + end + end else if(crccnt>3'd0) begin + bcnt <= '0; + cnt <= '0; + crccnt <= crccnt - 3'd1; + if(crccnt==3'd1) begin + if(curr_name==IEND) begin + busy <= 1'b0; + end + curr_name <= NONE; + end + end else begin + if(bcnt==3'd7) begin + cnt <= h32bit; + crccnt <= 3'd4; + if (l32bit==ihdr_name) + curr_name <= IHDR; + else if(l32bit==plte_name) + curr_name <= PLTE; + else if(l32bit==idat_name) + curr_name <= IDAT; + else if(l32bit==iend_name) + curr_name <= IEND; + else + curr_name <= NONE; + end + bcnt <= bcnt + 3'd1; + end + end + end + end + +endmodule + + + + + + + + + + + + + + + + + +module uz_inflate( + input wire rst, + input wire clk, + input wire ivalid, + output reg iready, + input wire [7:0] ibyte, + output reg ovalid, + output reg [7:0] obyte, + output wire end_stream +); + +initial ovalid = 1'b0; +initial obyte = '0; + +wire huffman_ovalid; +wire [7:0] huffman_obyte; +reg raw_ovalid; +reg [7:0] raw_obyte; + +reg raw_mode = 1'b0; +wire raw_format; + +reg [ 2:0] status = '0; +reg [15:0] rcnt = '0; +reg [ 2:0] cnt = '0; +reg [ 7:0] rbyte = '0; + +reg tvalid; +wire tready; +reg tbit; + +always @ (posedge clk or posedge rst) + if(rst) begin + ovalid <= 1'b0; + obyte <= '0; + end else begin + if(raw_mode) begin + ovalid <= raw_ovalid; + obyte <= raw_obyte; + end else begin + ovalid <= huffman_ovalid; + obyte <= huffman_obyte; + end + end + +always_comb + if(rst) begin + raw_ovalid <= 1'b0; + raw_obyte <= '0; + iready <= 1'b0; + tvalid <= 1'b0; + tbit <= 1'b0; + end else begin + raw_ovalid <= 1'b0; + raw_obyte <= '0; + if(raw_mode) begin + iready <= 1'b1; + tvalid <= 1'b0; + tbit <= 1'b0; + if(status>=3) begin + raw_ovalid <= ivalid; + raw_obyte <= ibyte; + end + end else begin + if(raw_format) begin + iready <= 1'b1; + tvalid <= 1'b0; + tbit <= 1'b0; + end else if(cnt==3'h0) begin + iready <= tready; + tvalid <= ivalid; + tbit <= ibyte[0]; + end else begin + iready <= 1'b0; + tvalid <= 1'b1; + tbit <= rbyte[cnt]; + end + end + end + +always @ (posedge clk or posedge rst) + if(rst) begin + raw_mode <= 1'b0; + cnt <= '0; + rbyte <= '0; + rcnt <= '0; + status <= '0; + end else begin + if(raw_mode) begin + cnt <= '0; + rbyte <= '0; + if(ivalid) begin + if (status==0) begin + rcnt[15:8] <= ibyte; + status <= status + 3'h1; + end else if(status==1) begin + status <= status + 3'h1; + end else if(status==2) begin + if(rcnt>0) begin + rcnt <= rcnt - 16'd1; + status <= status + 3'h1; + end else begin + raw_mode <= 1'b0; + status <= '0; + end + end else begin + if(rcnt>0) begin + rcnt <= rcnt - 16'd1; + end else begin + raw_mode <= 1'b0; + status <= '0; + end + end + end + end else begin + rcnt <= '0; + status <= '0; + if(raw_format) begin + if(ivalid) begin + raw_mode <= 1'b1; + rcnt[ 7:0] <= ibyte; + end + cnt <= '0; + rbyte <= '0; + end else begin + if(cnt==3'h0) begin + if(ivalid & tready) begin + cnt <= cnt + 3'h1; + rbyte <= ibyte; + end + end else begin + if(tready) + cnt <= cnt + 3'h1; + end + end + end + end + + +huffman_inflate huffman_inflate_i( + .rst ( raw_mode | rst ), + .clk ( clk ), + .ivalid ( tvalid ), + .iready ( tready ), + .ibit ( tbit ), + .ovalid ( huffman_ovalid ), + .obyte ( huffman_obyte ), + .raw_format ( raw_format ), + .end_stream ( end_stream ) +); + +endmodule + + + + + + + + + + + +module huffman_inflate( + input wire rst, + input wire clk, + input wire ivalid, + output wire iready, + input wire ibit, + output wire ovalid, + output wire [7:0] obyte, + output reg raw_format, + output reg end_stream +); + +initial {raw_format, end_stream} = '0; + +wire [ 4:0] CLCL [19]; assign CLCL[0]=5'd16; assign CLCL[1]=5'd17; assign CLCL[2]=5'd18; assign CLCL[3]=5'd0; assign CLCL[4]=5'd8; assign CLCL[5]=5'd7; assign CLCL[6]=5'd9; assign CLCL[7]=5'd6; assign CLCL[8]=5'd10; assign CLCL[9]=5'd5; assign CLCL[10]=5'd11; assign CLCL[11]=5'd4; assign CLCL[12]=5'd12; assign CLCL[13]=5'd3; assign CLCL[14]=5'd13; assign CLCL[15]=5'd2; assign CLCL[16]=5'd14; assign CLCL[17]=5'd1; assign CLCL[18]=5'd15; +wire [ 8:0] LENGTH_BASE [30]; assign LENGTH_BASE[0]=9'd0; assign LENGTH_BASE[1]=9'd3; assign LENGTH_BASE[2]=9'd4; assign LENGTH_BASE[3]=9'd5; assign LENGTH_BASE[4]=9'd6; assign LENGTH_BASE[5]=9'd7; assign LENGTH_BASE[6]=9'd8; assign LENGTH_BASE[7]=9'd9; assign LENGTH_BASE[8]=9'd10; assign LENGTH_BASE[9]=9'd11; assign LENGTH_BASE[10]=9'd13; assign LENGTH_BASE[11]=9'd15; assign LENGTH_BASE[12]=9'd17; assign LENGTH_BASE[13]=9'd19; assign LENGTH_BASE[14]=9'd23; assign LENGTH_BASE[15]=9'd27; assign LENGTH_BASE[16]=9'd31; assign LENGTH_BASE[17]=9'd35; assign LENGTH_BASE[18]=9'd43; assign LENGTH_BASE[19]=9'd51; assign LENGTH_BASE[20]=9'd59; assign LENGTH_BASE[21]=9'd67; assign LENGTH_BASE[22]=9'd83; assign LENGTH_BASE[23]=9'd99; assign LENGTH_BASE[24]=9'd115; assign LENGTH_BASE[25]=9'd131; assign LENGTH_BASE[26]=9'd163; assign LENGTH_BASE[27]=9'd195; assign LENGTH_BASE[28]=9'd227; assign LENGTH_BASE[29]=9'd258; +wire [ 2:0] LENGTH_EXTRA [30]; assign LENGTH_EXTRA[0]=3'd0; assign LENGTH_EXTRA[1]=3'd0; assign LENGTH_EXTRA[2]=3'd0; assign LENGTH_EXTRA[3]=3'd0; assign LENGTH_EXTRA[4]=3'd0; assign LENGTH_EXTRA[5]=3'd0; assign LENGTH_EXTRA[6]=3'd0; assign LENGTH_EXTRA[7]=3'd0; assign LENGTH_EXTRA[8]=3'd0; assign LENGTH_EXTRA[9]=3'd1; assign LENGTH_EXTRA[10]=3'd1; assign LENGTH_EXTRA[11]=3'd1; assign LENGTH_EXTRA[12]=3'd1; assign LENGTH_EXTRA[13]=3'd2; assign LENGTH_EXTRA[14]=3'd2; assign LENGTH_EXTRA[15]=3'd2; assign LENGTH_EXTRA[16]=3'd2; assign LENGTH_EXTRA[17]=3'd3; assign LENGTH_EXTRA[18]=3'd3; assign LENGTH_EXTRA[19]=3'd3; assign LENGTH_EXTRA[20]=3'd3; assign LENGTH_EXTRA[21]=3'd4; assign LENGTH_EXTRA[22]=3'd4; assign LENGTH_EXTRA[23]=3'd4; assign LENGTH_EXTRA[24]=3'd4; assign LENGTH_EXTRA[25]=3'd5; assign LENGTH_EXTRA[26]=3'd5; assign LENGTH_EXTRA[27]=3'd5; assign LENGTH_EXTRA[28]=3'd5; assign LENGTH_EXTRA[29]=3'd0; +wire [14:0] DISTANCE_BASE [30]; assign DISTANCE_BASE[0]=15'd1; assign DISTANCE_BASE[1]=15'd2; assign DISTANCE_BASE[2]=15'd3; assign DISTANCE_BASE[3]=15'd4; assign DISTANCE_BASE[4]=15'd5; assign DISTANCE_BASE[5]=15'd7; assign DISTANCE_BASE[6]=15'd9; assign DISTANCE_BASE[7]=15'd13; assign DISTANCE_BASE[8]=15'd17; assign DISTANCE_BASE[9]=15'd25; assign DISTANCE_BASE[10]=15'd33; assign DISTANCE_BASE[11]=15'd49; assign DISTANCE_BASE[12]=15'd65; assign DISTANCE_BASE[13]=15'd97; assign DISTANCE_BASE[14]=15'd129; assign DISTANCE_BASE[15]=15'd193; assign DISTANCE_BASE[16]=15'd257; assign DISTANCE_BASE[17]=15'd385; assign DISTANCE_BASE[18]=15'd513; assign DISTANCE_BASE[19]=15'd769; assign DISTANCE_BASE[20]=15'd1025; assign DISTANCE_BASE[21]=15'd1537; assign DISTANCE_BASE[22]=15'd2049; assign DISTANCE_BASE[23]=15'd3073; assign DISTANCE_BASE[24]=15'd4097; assign DISTANCE_BASE[25]=15'd6145; assign DISTANCE_BASE[26]=15'd8193; assign DISTANCE_BASE[27]=15'd12289; assign DISTANCE_BASE[28]=15'd16385; assign DISTANCE_BASE[29]=15'd24577; +wire [ 3:0] DISTANCE_EXTRA [30]; assign DISTANCE_EXTRA[0]=4'd0; assign DISTANCE_EXTRA[1]=4'd0; assign DISTANCE_EXTRA[2]=4'd0; assign DISTANCE_EXTRA[3]=4'd0; assign DISTANCE_EXTRA[4]=4'd1; assign DISTANCE_EXTRA[5]=4'd1; assign DISTANCE_EXTRA[6]=4'd2; assign DISTANCE_EXTRA[7]=4'd2; assign DISTANCE_EXTRA[8]=4'd3; assign DISTANCE_EXTRA[9]=4'd3; assign DISTANCE_EXTRA[10]=4'd4; assign DISTANCE_EXTRA[11]=4'd4; assign DISTANCE_EXTRA[12]=4'd5; assign DISTANCE_EXTRA[13]=4'd5; assign DISTANCE_EXTRA[14]=4'd6; assign DISTANCE_EXTRA[15]=4'd6; assign DISTANCE_EXTRA[16]=4'd7; assign DISTANCE_EXTRA[17]=4'd7; assign DISTANCE_EXTRA[18]=4'd8; assign DISTANCE_EXTRA[19]=4'd8; assign DISTANCE_EXTRA[20]=4'd9; assign DISTANCE_EXTRA[21]=4'd9; assign DISTANCE_EXTRA[22]=4'd10; assign DISTANCE_EXTRA[23]=4'd10; assign DISTANCE_EXTRA[24]=4'd11; assign DISTANCE_EXTRA[25]=4'd11; assign DISTANCE_EXTRA[26]=4'd12; assign DISTANCE_EXTRA[27]=4'd12; assign DISTANCE_EXTRA[28]=4'd13; assign DISTANCE_EXTRA[29]=4'd13; + +reg irepeat = 1'b0; +reg srepeat = 1'b0; + +reg symbol_valid = 1'b0; +reg [7:0] symbol = '0; + +reg decoder_nreset = 1'b0; + +reg [ 1:0] iword = '0; +reg [ 1:0] ibcnt = '0; +reg [ 4:0] precode_wpt = '0; + +reg bfin = 1'b0; +reg bfix = 1'b0; +reg fixed_tree = 1'b0; +reg [13:0] precode_reg = '0; +wire [ 4:0] hclen = 5'd4 + {1'b0, precode_reg[13:10]}; +wire [ 8:0] hlit = 9'd257 + precode_reg[ 4: 0]; +wire [ 8:0] hdist = 9'd1 + {4'h0, precode_reg[ 9: 5]}; +wire [ 8:0] hmax = hlit + hdist; +wire [ 8:0] hend = (hlit+9'd32>9'd288) ? hlit+9'd32 : 9'd288; + +reg [ 4:0] lentree_wpt = '0; +reg [ 8:0] tree_wpt = '0; + +wire lentree_codeen; +wire [ 5:0] lentree_code; +wire codetree_codeen; +wire [ 9:0] codetree_code; +wire distree_codeen; +wire [ 9:0] distree_code; + +reg [ 2:0] repeat_code_pt = '0; +enum {REPEAT_NONE, REPEAT_PREVIOUS, REPEAT_ZERO_FEW, REPEAT_ZERO_MANY} repeat_mode = REPEAT_NONE; +reg [ 6:0] repeat_code='0; +reg [ 7:0] repeat_len ='0; +reg [ 5:0] repeat_val = '0; + +reg lentree_run = 1'b0; +wire lentree_done; +reg tree_run = 1'b0; +wire codetree_done; +wire distree_done; +wire tree_done = (codetree_done & distree_done) | fixed_tree; + +reg [ 2:0] tcnt =3'h0, tmax =3'h0; +reg [ 3:0] dscnt=4'h0, dsmax=4'h0; + +enum {T, D, R, S} status = T; + +wire lentree_ien = ~end_stream & ~raw_format & ivalid & lentree_done & ~lentree_codeen & (repeat_mode==REPEAT_NONE && repeat_len==8'd0) & (tree_wpt3'd0) & (tree_wpt4'd0)) ) ) ); + +reg [ 8:0] lengthb= '0; +reg [ 5:0] lengthe= '0; +wire [ 8:0] length = lengthb + lengthe; +reg [ 8:0] len_last = '0; + +reg [15:0] distanceb='0; +reg [15:0] distancee='0; +wire [15:0] distance = distanceb + distancee; + +reg lentree_wen = 1'b0; +reg [ 4:0] lentree_waddr = '0; +reg [ 2:0] lentree_wdata = '0; +reg codetree_wen = 1'b0; +reg [ 8:0] codetree_waddr = '0; +reg [ 5:0] codetree_wdata = '0; +reg distree_wen = 1'b0; +reg [ 4:0] distree_waddr = '0; +reg [ 5:0] distree_wdata = '0; + +wire [ 5:0] lentree_raddr; +wire [ 5:0] lentree_rdata; +wire [ 9:0] codetree_raddr; +wire [ 9:0] codetree_rdata, codetree_rdata_fixed; +wire [ 5:0] distree_raddr; +wire [ 9:0] distree_rdata, distree_rdata_fixed; + +task automatic lentree_write(input wen=1'b0, input [4:0] waddr='0, input [2:0] wdata='0); + lentree_wen <= wen; + lentree_waddr <= waddr; + lentree_wdata <= wdata; +endtask + +task automatic codetree_write(input wen=1'b0, input [8:0] waddr='0, input [5:0] wdata='0); + codetree_wen <= wen; + codetree_waddr <= waddr; + codetree_wdata <= wdata; +endtask + +task automatic distree_write(input wen=1'b0, input [4:0] waddr='0, input [5:0] wdata='0); + distree_wen <= wen; + distree_waddr <= waddr; + distree_wdata <= wdata; +endtask + +task automatic reset_all_regs(); + decoder_nreset <= 1'b0; + {bfin, bfix, fixed_tree} <= '0; + iword <= '0; + ibcnt <= '0; + precode_wpt <= '0; + precode_reg <= '0; + lentree_wpt <= '0; + lentree_run <= 1'b0; + tree_run <= 1'b0; + lentree_write(); + codetree_write(); + distree_write(); + repeat_code_pt <= '0; + repeat_mode <= REPEAT_NONE; + repeat_code <= '0; + repeat_len <= '0; + repeat_val <= '0; + tree_wpt <= '0; + tcnt <= '0; + tmax <= '0; + lengthb <= '0; + lengthe <= '0; + distanceb<= '0; + distancee<= '0; + dscnt <= '0; + dsmax <= '0; + status <= T; + symbol_valid <= 1'b0; + symbol <= '0; + irepeat <= 1'b0; + srepeat <= 1'b0; + len_last <= '0; +endtask + +always @ (posedge clk or posedge rst) + if(rst) begin + {raw_format, end_stream} <= '0; + reset_all_regs(); + end else begin + symbol_valid <= 1'b0; + symbol <= '0; + irepeat <= 1'b0; + srepeat <= 1'b0; + decoder_nreset <= 1'b1; + lentree_write(); + codetree_write(); + distree_write(); + if(precode_wpt<=2) begin + lentree_run <= 1'b0; + tree_run <= 1'b0; + if(ivalid) begin + precode_wpt <= precode_wpt + 5'd1; + if(precode_wpt==0) begin + bfin <= ibit; + end else if(precode_wpt==1) begin + bfix <= ibit; + end else begin + case({ibit,bfix}) + 2'b00 : + raw_format <= 1'b1; + 2'b01 : begin + precode_wpt <= '1; + lentree_wpt <= '1; + tree_wpt <= '1; + fixed_tree <= 1'b1; + end + endcase + end + end + end else if(precode_wpt<17) begin + lentree_run <= 1'b0; + tree_run <= 1'b0; + if(ivalid) begin + precode_reg <= {ibit, precode_reg[13:1]}; + precode_wpt <= precode_wpt + 5'd1; + end + end else if(lentree_wpt3'd0) begin + if(ivalid) begin + repeat_code_pt <= repeat_code_pt - 3'd1; + repeat_code[3'd7-repeat_code_pt] <= ibit; + end + end else if(repeat_mode>0) begin + case(repeat_mode) + REPEAT_PREVIOUS: begin + repeat_len <= repeat_code[6:5] + 8'd3; + end + REPEAT_ZERO_FEW: begin + repeat_len <= repeat_code[6:4] + 8'd3; + end + REPEAT_ZERO_MANY: begin + repeat_len <= repeat_code[6:0] + 8'd11; + end + default: begin + repeat_len <= 0; + end + endcase + repeat_mode <= REPEAT_NONE; + end else if(repeat_len>8'd0) begin + repeat_len <= repeat_len - 8'd1; + tree_wpt <= tree_wpt + 9'd1; + if(tree_wpt<288) + codetree_write(1'b1, tree_wpt, (tree_wpt=hlit && tree_wpt<(hlit+9'd32)) + distree_write(1'b1, tree_wpt - hlit, (tree_wpt=hlit && tree_wpt<(hlit+9'd32)) + distree_write(1'b1, tree_wpt - hlit, (tree_wpt=hlit && tree_wpt<(hlit+9'd32)) + distree_write(1'b1, tree_wpt - hlit, '0); + tree_wpt <= tree_wpt + 9'd1; + end else if(tree_wpt4'd0) begin + if(ivalid) begin + dscnt <= dscnt - 4'd1; + distancee[dsmax-dscnt] <= ibit; + end + end else if(tcnt>3'd0) begin + if(ivalid) begin + tcnt <= tcnt - 3'd1; + lengthe[tmax-tcnt] <= ibit; + end + end else if(status==R) begin + status <= S; + len_last <= length; + srepeat <= 1'b1; + end else if(status==S) begin + if(len_last>0) begin + irepeat <= 1'b1; + len_last <= len_last - 9'd1; + end else + status <= T; + end else if(codetree_codeen) begin + if(codetree_code<10'd256) begin // normal symbol + symbol_valid <= 1'b1; + symbol <= codetree_code[7:0]; + end else if(codetree_code==10'd256) begin // end symbol + end_stream <= bfin; + reset_all_regs(); + end else begin // special symbol + lengthb<= LENGTH_BASE[codetree_code-10'd256]; + lengthe<= '0; + tcnt <= LENGTH_EXTRA[codetree_code-10'd256]; + tmax <= LENGTH_EXTRA[codetree_code-10'd256]; + status <= D; + end + end else if(distree_codeen) begin + distanceb<= DISTANCE_BASE[distree_code]; + distancee<= '0; + dscnt <= DISTANCE_EXTRA[distree_code]; + dsmax <= DISTANCE_EXTRA[distree_code]; + status <= R; + end + end + end + +huffman_build #( + .NUMCODES ( 19 ), + .CODEBITS ( 3 ), + .BITLENGTH ( 7 ), + .OUTWIDTH ( 6 ) +) lentree_builder ( + .clk ( clk ), + .wren ( lentree_wen ), + .wraddr ( lentree_waddr ), + .wrdata ( lentree_wdata ), + .run ( lentree_run ), + .done ( lentree_done ), + .rdaddr ( lentree_raddr ), + .rddata ( lentree_rdata ) +); + +huffman_decode_symbol #( + .NUMCODES ( 19 ), + .OUTWIDTH ( 6 ) +) lentree_decoder ( + .rst ( ~decoder_nreset), + .clk ( clk ), + .ien ( lentree_ien ), + .ibit ( ibit ), + .oen ( lentree_codeen ), + .ocode ( lentree_code ), + .rdaddr ( lentree_raddr ), + .rddata ( lentree_rdata ) +); + +huffman_build #( + .NUMCODES ( 288 ), + .CODEBITS ( 5 ), + .BITLENGTH ( 15 ), + .OUTWIDTH ( 10 ) +) codetree_builder ( + .clk ( clk ), + .wren ( codetree_wen ), + .wraddr ( codetree_waddr ), + .wrdata ( (5)'(codetree_wdata) ), + .run ( tree_run ), + .done ( codetree_done ), + .rdaddr ( codetree_raddr ), + .rddata ( codetree_rdata ) +); + +fixed_codetree codetree_fixed( + .clk ( clk ), + .rdaddr ( codetree_raddr ), + .rddata ( codetree_rdata_fixed ) +); + +huffman_decode_symbol #( + .NUMCODES ( 288 ), + .OUTWIDTH ( 10 ) +) codetree_decoder ( + .rst ( ~decoder_nreset), + .clk ( clk ), + .ien ( codetree_ien ), + .ibit ( ibit ), + .oen ( codetree_codeen), + .ocode ( codetree_code ), + .rdaddr ( codetree_raddr ), + .rddata ( fixed_tree ? codetree_rdata_fixed : codetree_rdata ) +); + +huffman_build #( + .NUMCODES ( 32 ), + .CODEBITS ( 5 ), + .BITLENGTH ( 15 ), + .OUTWIDTH ( 10 ) +) distree_builder ( + .clk ( clk ), + .wren ( distree_wen ), + .wraddr ( distree_waddr ), + .wrdata ( (5)'(distree_wdata) ), + .run ( tree_run ), + .done ( distree_done ), + .rdaddr ( distree_raddr ), + .rddata ( distree_rdata ) +); + +fixed_distree distree_fixed( + .clk ( clk ), + .rdaddr ( distree_raddr ), + .rddata ( distree_rdata_fixed ) +); + +huffman_decode_symbol #( + .NUMCODES ( 32 ), + .OUTWIDTH ( 10 ) +) distree_decoder ( + .rst ( ~decoder_nreset), + .clk ( clk ), + .ien ( distree_ien ), + .ibit ( ibit ), + .oen ( distree_codeen ), + .ocode ( distree_code ), + .rdaddr ( distree_raddr ), + .rddata ( fixed_tree ? distree_rdata_fixed : distree_rdata ) +); + +repeat_buffer repeat_buffer_i( + .clk ( clk ), + + .ivalid ( symbol_valid ), + .idata ( symbol ), + + .repeat_en ( irepeat ), + .repeat_start ( srepeat ), + .repeat_dist ( distance ), + + .ovalid ( ovalid ), + .odata ( obyte ) +); + +endmodule + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +module huffman_decode_symbol #( + parameter NUMCODES = 288, + parameter OUTWIDTH = 10 +)( + rst, clk, + ien, ibit, + oen, ocode, + rdaddr, rddata +); + +function automatic integer clogb2(input integer val); + integer valtmp; + valtmp = val; + for(clogb2=0; valtmp>0; clogb2=clogb2+1) valtmp = valtmp>>1; +endfunction + +input rst, clk; +input ien, ibit; +output oen = 1'b0; +output [ OUTWIDTH-1:0] ocode = '0; +output [clogb2(2*NUMCODES-1)-1:0] rdaddr; +input [ OUTWIDTH-1:0] rddata; + +wire rst, clk; +wire ien, ibit; +reg oen = 1'b0; +reg [ OUTWIDTH-1:0] ocode = '0; +wire [clogb2(2*NUMCODES-1)-1:0] rdaddr; +wire [ OUTWIDTH-1:0] rddata; + +reg [clogb2(2*NUMCODES-1)-2:0] tpos = '0; +wire [clogb2(2*NUMCODES-1)-2:0] ntpos; +reg ienl = 1'b0; + +assign rdaddr = {ntpos, ibit}; + +assign ntpos = ienl ? (clogb2(2*NUMCODES-1)-1)'(rddata<(OUTWIDTH)'(NUMCODES) ? '0 : rddata-(OUTWIDTH)'(NUMCODES)) : tpos; + +always @ (posedge clk or posedge rst) + if(rst) + ienl <= 1'b0; + else + ienl <= ien; + +always @ (posedge clk or posedge rst) + if(rst) + tpos <= '0; + else + tpos <= ntpos; + +always_comb + if(ienl && rddata0; clogb2=clogb2+1) valtmp = valtmp>>1; +endfunction + +input clk; +input wren; +input [ clogb2(NUMCODES-1)-1:0] wraddr; +input [ CODEBITS -1:0] wrdata; +input run; +output done; +input [clogb2(2*NUMCODES-1)-1:0] rdaddr; +output [ OUTWIDTH-1:0] rddata; + +wire clk; +wire wren; +wire [ clogb2(NUMCODES-1)-1:0] wraddr; +wire [ CODEBITS -1:0] wrdata; +wire run; +wire done; +wire [clogb2(2*NUMCODES-1)-1:0] rdaddr; +wire [ OUTWIDTH-1:0] rddata; + +reg [clogb2(NUMCODES)-1:0] blcount [BITLENGTH]; +reg [ 31:0] nextcode [BITLENGTH+1]; + +reg clear_tree2d = 1'b0; +reg build_tree2d = 1'b0; +reg [clogb2(BITLENGTH)-1:0] idx = '0; +reg [clogb2(2*NUMCODES+1)-1:0] clearidx = '0; +reg [ clogb2(NUMCODES)-1:0] nn='0, nnn, lnn='0; +reg [CODEBITS-1:0] ii='0, lii='0; +reg [CODEBITS-1:0] blenn, blen = '0; +wire [31:0] tree1d = nextcode[blen]; +wire islast = (blen==0 || ii==0); +reg [clogb2(2*NUMCODES-1)-1:0] nodefilled = '0; +reg [clogb2(2*NUMCODES-1)-1:0] ntreepos, treepos='0; +wire [clogb2(2*NUMCODES-1) :0] ntpos= {ntreepos, tree1d[ii]}; +reg [clogb2(2*NUMCODES-1) :0] tpos = '0; +wire rdfilled; +reg valid = 1'b0; +wire [OUTWIDTH-1:0] wrtree2d = (lii==0) ? lnn : nodefilled + (clogb2(2*NUMCODES-1))'(NUMCODES); +reg alldone = 1'b0; + +assign done = alldone & run; + +initial for(int i=0; i< BITLENGTH; i++) blcount[i] = '0; +initial for(int i=0; i<=BITLENGTH; i++) nextcode[i] = '0; + +always @ (posedge clk) begin + valid <= build_tree2d & nn0; + treepos <= ntreepos; + tpos <= ntpos; + lii <= ii; + lnn <= nn; +end + +always @ (posedge clk) + if(islast) + blen <= blenn; + +always @ (posedge clk) + if(done) begin + for(int i=0; i0) + nextcode[blen] <= tree1d + 1; + end else + ii <= ii - (CODEBITS)'(1); + end else + alldone <= 1'b1; + end else begin + if(idx0) + nodefilled <= nodefilled + (clogb2(2*NUMCODES-1))'(1); + +RamSinglePort #( + .SIZE ( NUMCODES ), + .WIDTH ( CODEBITS ) +) ram_for_bitlens ( + .clk ( clk ), + .wen ( wren ), + .waddr ( wraddr ), + .wdata ( wrdata ), + .raddr ( (clogb2(NUMCODES-1))'(nnn) + (clogb2(NUMCODES-1))'(1) ), + .rdata ( blenn ) +); + +RamSinglePort #( + .SIZE ( NUMCODES * 2 ), + .WIDTH ( OUTWIDTH + 1 ) +) ram_for_tree2d ( + .clk ( clk ), + .wen ( clearidx<(2*NUMCODES) | (valid & rdfilled) ), + .waddr ( (clogb2(2*NUMCODES-1))'(clearidx<(2*NUMCODES) ? clearidx : tpos ) ), + .wdata ( clearidx<(2*NUMCODES) ? {1'b1,{OUTWIDTH{1'b0}}} : {1'b0, wrtree2d} ), + .raddr ( (clogb2(2*NUMCODES-1))'(alldone ? rdaddr : ntpos ) ), + .rdata ( {rdfilled, rddata} ) +); + +endmodule + + + + + + + + + + + + + + + + + +module repeat_buffer #( + parameter DWIDTH = 8 +) ( + input clk, + + input ivalid, + input [DWIDTH-1:0] idata, + + input repeat_en, + input repeat_start, + input [ 15:0] repeat_dist, + + output ovalid, + output [DWIDTH-1:0] odata +); + +wire [15:0] MAXLEN = 16'd33792; + +reg [15:0] wptr = '0; +reg [15:0] rptr = '0; +reg [15:0] sptr = '0; +reg [15:0] eptr = '0; +wire [15:0] sptrw = (wptr sa ? p - sa : sa - p; + automatic logic signed [10:0] pb = p > sb ? p - sb : sb - p; + automatic logic signed [10:0] pc = p > sc ? p - sc : sc - p; + if (pa <= pb && pa <= pc) + return a; + else if (pb <= pc) + return b; + else + return c; +endfunction + +reg nfirstrow = 1'b0; +reg [13:0] col = '0; +reg [ 2:0] mode = '0; +reg [ 7:0] fdata; +wire [ 7:0] LLdata, UUdata, ULdata; +wire nfirstcol = col > (14'h1+bpp); +wire [ 8:0] SSdata = (nfirstcol ? {1'b0,LLdata} : 9'h0) + (nfirstrow ? {1'b0,UUdata} : 9'h0); + + +always @ (posedge clk or posedge rst) + if(rst) begin + nfirstrow <= 1'b0; + col <= '0; + end else begin + if(ivalid) begin + if(col0; clogb2=clogb2+1) valtmp = valtmp>>1; +endfunction + +input clk; +input wen; +input [clogb2(SIZE-1)-1:0] waddr; +input [WIDTH-1:0] wdata; +input [clogb2(SIZE-1)-1:0] raddr; +output [WIDTH-1:0] rdata; + +wire clk; +wire wen; +wire [clogb2(SIZE-1)-1:0] waddr; +wire [WIDTH-1:0] wdata; +wire [clogb2(SIZE-1)-1:0] raddr; +reg [WIDTH-1:0] rdata; + +reg [WIDTH-1:0] mem [SIZE]; + +always @ (posedge clk) + if(wen) + mem[waddr] <= wdata; + +initial rdata = '0; +always @ (posedge clk) + rdata <= mem[raddr]; + +endmodule diff --git a/images/blockdiagram.png b/images/blockdiagram.png new file mode 100644 index 0000000..203b055 Binary files /dev/null and b/images/blockdiagram.png differ diff --git a/images/interface.png b/images/interface.png new file mode 100644 index 0000000..fc31c25 Binary files /dev/null and b/images/interface.png differ diff --git a/images/test0.png b/images/test0.png new file mode 100644 index 0000000..e186692 Binary files /dev/null and b/images/test0.png differ diff --git a/images/test1.png b/images/test1.png new file mode 100644 index 0000000..a5dfb3e Binary files /dev/null and b/images/test1.png differ diff --git a/images/test10.png b/images/test10.png new file mode 100644 index 0000000..9895b57 Binary files /dev/null and b/images/test10.png differ diff --git a/images/test12.png b/images/test12.png new file mode 100644 index 0000000..16bc8cb Binary files /dev/null and b/images/test12.png differ diff --git a/images/test2.png b/images/test2.png new file mode 100644 index 0000000..f3cb81f Binary files /dev/null and b/images/test2.png differ diff --git a/images/test3.png b/images/test3.png new file mode 100644 index 0000000..328a2c0 Binary files /dev/null and b/images/test3.png differ diff --git a/images/test4.png b/images/test4.png new file mode 100644 index 0000000..49871d5 Binary files /dev/null and b/images/test4.png differ diff --git a/images/test5.png b/images/test5.png new file mode 100644 index 0000000..3305523 Binary files /dev/null and b/images/test5.png differ diff --git a/images/test6.png b/images/test6.png new file mode 100644 index 0000000..571fb92 Binary files /dev/null and b/images/test6.png differ diff --git a/images/test7.png b/images/test7.png new file mode 100644 index 0000000..890b277 Binary files /dev/null and b/images/test7.png differ diff --git a/images/test8.png b/images/test8.png new file mode 100644 index 0000000..5cea019 Binary files /dev/null and b/images/test8.png differ diff --git a/images/wave1.png b/images/wave1.png new file mode 100644 index 0000000..3681f81 Binary files /dev/null and b/images/wave1.png differ diff --git a/images/wave2.png b/images/wave2.png new file mode 100644 index 0000000..7a8a1eb Binary files /dev/null and b/images/wave2.png differ diff --git a/result/test0.txt b/result/test0.txt new file mode 100644 index 0000000..28404d1 --- /dev/null +++ b/result/test0.txt @@ -0,0 +1,3 @@ + +frame type:2 width:1 height:154 +ebe6afff f5f0b9ff f1ebb7ff dbd5a3ff d1c392ff c6a673ff 8b622eff 78501dff a37d4eff a68155ff 6a441dff 482200ff 583110ff 663f20ff 673e20ff 66391cff 5e2f11ff 5b280bff 642f10ff 774022ff 864f30ff 633110ff 401200ff 5b2f14ff 552c16ff 2e0900ff 391404ff 4f2515ff 5a2f1eff 582917ff 5e2f1bff 5c3120ff 452113ff 240d07ff 180c0cff 0c0b11ff 000107ff 000104ff 04080bff 090e12ff 000107ff 1e292fff 2a373fff 000811ff 202d36ff 07141dff 010e17ff 000a12ff 010c12ff 030a10ff 000509ff 000205ff 010204ff 06050aff 030002ff 060000ff 20110eff 281006ff 59351fff a36e4cff 9e5c2cff b36328ff a44b09ff bb5e1bff b35a18ff 934107ff 8f4613ff 8f5024ff a16c42ff 9a6e3fff a58151ff ddbb8dff efd0a4ff e9cda6ff e9d1adff ddc9a8ff e5d4b6ff d4c5a8ff f1e3c8ff e3d5baff e5d7bdff c8ba9fff bbad92ff ded0b3ff baaa90ff c8b6a2ff d8c5b4ff e0cfbdff dbccb7ff d9cdb7ff dfd4beff d9d1baff cdc5aeff ded6bfff ddd3baff dad0b7ff d2c6b0ff cebda9ff d8c6b2ff dec9b6ff d2bda8ff c3b199ff d0c0a7ff dacab1ff d5c5acff cdbca2ff ccb89fff cfba9fff d1bca1ff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff \ No newline at end of file diff --git a/tb_hard_png.sv b/tb_hard_png.sv new file mode 100644 index 0000000..c2b24f3 --- /dev/null +++ b/tb_hard_png.sv @@ -0,0 +1,95 @@ +`timescale 1 ns/1 ns + +`define PNG_FILE "E:/FPGAcommon/Hard-PNG/images/test14.png" // the png file to decode +`define OUT_FILE "E:/FPGAcommon/Hard-PNG/result/test14.txt" // decode result txt file +`define OUT_ENABLE 1 // whether to write result to the decode result txt file + +module tb_hard_png(); + +integer fppng, fptxt; +reg [7:0] rbyte; + +reg rst = 1'b1; +reg clk = 1'b1; +always #5 clk = ~clk; + +reg ivalid = 1'b0; +wire iready; +reg [ 7:0] ibyte = '0; + +wire newframe; +wire [ 1:0] colortype; +wire [13:0] width; +wire [31:0] height; + +wire ovalid; +wire [ 7:0] opixelr, opixelg, opixelb, opixela; + + +initial begin + fppng = $fopen(`PNG_FILE, "rb"); + if(`OUT_ENABLE) fptxt = $fopen(`OUT_FILE, "w"); + rbyte = $fgetc(fppng); + + @(posedge clk) rst = 1'b1; + @(posedge clk) rst = 1'b0; + + @(posedge clk) #1 + ivalid <= 1'b0; + ibyte <= 1'b0; + + while(!$feof(fppng)) begin + @(posedge clk) #1 + ivalid <= 1'b1; + ibyte <= rbyte; + #1 if(iready) begin + rbyte = $fgetc(fppng); + //@(posedge clk) #1 + //ivalid <= 1'b0; + //ibyte <= '0; + end + end + + @(posedge clk) #1 + ivalid <= 1'b0; + ibyte <= 1'b0; + + $fclose(fppng); + if(`OUT_ENABLE) $fclose(fptxt); +end + +hard_png hard_png_i( + .rst ( rst ), + .clk ( clk ), + // data input + .ivalid ( ivalid ), + .iready ( iready ), + .ibyte ( ibyte ), + // image size output + .newframe ( newframe ), + .colortype ( colortype ), + .width ( width ), + .height ( height ), + // data output + .ovalid ( ovalid ), + .opixelr ( opixelr ), + .opixelg ( opixelg ), + .opixelb ( opixelb ), + .opixela ( opixela ) +); + +reg [31:0] pixcnt = 0; + +always @ (posedge clk) + if(newframe) begin + pixcnt <= 0; + if(`OUT_ENABLE) + $fwrite(fptxt, "\nframe type:%1d width:%1d height:%1d\n", colortype, width, height); + else + $write("\nframe type:%1d width:%1d height:%1d\n", colortype, width, height); + end else if(ovalid) begin + pixcnt <= pixcnt + 1; + if(`OUT_ENABLE) $fwrite(fptxt, "%02x%02x%02x%02x ", opixelr, opixelg, opixelb, opixela); + end + +endmodule diff --git a/validation.py b/validation.py new file mode 100644 index 0000000..16e8210 --- /dev/null +++ b/validation.py @@ -0,0 +1,58 @@ +PNG_FILE = "E:/FPGAcommon/Hard-PNG/images/test15.png" +TXT_FILE = "E:/FPGAcommon/Hard-PNG/result/test15.txt" + +import numpy as np +from PIL import Image + +def read_txt(fname): + with open(fname, "rt") as txt: + height, width = 0, 0 + for line in txt.readlines(): + if height>0 and width>0: + arr = np.zeros([height*width,4], dtype=np.uint8) + for idx, value in enumerate(line.split()): + rgba = [int(value[0:2],16), int(value[2:4],16), int(value[4:6],16), int(value[6:8],16)] + arr[idx] = rgba + return height, width, arr + if line.startswith("frame"): + height, width = 0, 0 + for item in line.split(): + pair = item.split(':') + try: + name, value = pair[0].strip(), int(pair[1].strip()) + if name == "height": + height = value + elif name == "width": + width = value + except: + pass + return 0, 0, np.zeros([0], dtype=np.uint8) + +def read_png(fname): + img = Image.open(fname) + width, height = img.size + if img.mode=="RGB" or img.mode=="RGBA" or img.mode=="P": + arr = np.asarray(img.convert("RGBA")).reshape([height*width,-1]) + img.close() + return height, width, arr + elif img.mode=="L": + arrl= np.asarray(img).reshape([height*width,-1]) + img.close() + arr = np.zeros([height*width,4], dtype=np.uint8) + for i in range(height*width): + arr[i][0], arr[i][1], arr[i][2], arr[i][3] = arrl[i][0], arrl[i][0], arrl[i][0], 0xff + return height, width, arr + else: + return 0, 0, np.zeros([0], dtype=np.uint8) + + + +h_hw, w_hw, arr_hw = read_txt(TXT_FILE) +h_sw, w_sw, arr_sw = read_png(PNG_FILE) + +for idx, (pix_hw, pix_sw) in enumerate(zip(arr_hw, arr_sw)): + if pix_hw[0]!=pix_sw[0] or pix_hw[1]!=pix_sw[1] or pix_hw[2]!=pix_sw[2]: + print(" ** mismatch at %d " % (idx,), pix_hw, pix_sw) + break +else: + print(" validation successful!!")