基于FPGA的lz4解压缩仿真调试

1、简介

对于任意长度顺序呈现的输入数据流,通过对冗余byte的数据编码,完成数据压缩的问题。数据包格式

从数据包长度可知,最少需要5个字节才能压缩,否则压缩无意义,对于lz其他的介绍可以百度,本文只介绍实现。

通常我们使用fpga解压缩来解决数据过大无法存储,或者加载时间过长的问题,下面开始介绍实现过程

2 使用python处理

文本数据来生成二进制文件,这个数据要对fpga仿真输入源,不是普通的文本数据,否则要转化成asic码。

a.txt

复制代码
00000000
00001234
00000000
00005678
00000000
0000abcd
00000000
00002345

文本转化成二进制文件b.bin

压缩后的文件c.bin

解压后的文件d.bin

python脚本

复制代码
import lz4.frame

file_a = "a.txt"
file_b = "b.bin"
file_c = "c.bin"
file_d = "d.bin"

with open(file_a, 'r') as hex_file:  
    with open(file_b, 'wb') as bin_file:  
        for line in hex_file:  
            byte_data = bytes.fromhex(line.strip())  
            bin_file.write(byte_data)  

with open(file_b, 'rb') as file:  
    binary_data = file.read()  

compressed = lz4.frame.compress(binary_data)

with open(file_c, 'wb') as file:  
    file.write(compressed)

decompressed = lz4.frame.decompress(compressed)

with open(file_d, 'wb') as file:  
    file.write(decompressed)

if binary_data == decompressed:
    print("ok")
else:
    print("not ok")

从c.bin中数据可以,压缩数据长度0x0000001a,共26个字节。从1100到2345。

3 fpga仿真

tb仿真文件

复制代码
reg [7:0] mem_byte[0:127];

integer handle,bytes_read;
integer i;

initial begin  
	handle = $fopen("../tb/c.bin","rb"); 
	bytes_read = $fread(mem_byte, handle);
end  

initial begin
	clk = 0 ; forever #10 clk = ~clk ;
end

initial begin
	reset = 1;
	#1000;
	reset = 0;
	#1000;
	write_en = 0;
	compressed_word = 0;
	#1000;

	for(i=19;i<bytes_read;i=i+1)begin
		@(posedge clk) begin
			write_en = 1;
			compressed_word = mem_byte[i];
		end
	end
	@(posedge clk) begin
		write_en = 0;
		compressed_word = mem_byte[i];
	end

	#100000;
	$finish;
	
end

仿真脚本

使用make all进行仿真,仿真波形如下。

可以看到data_valid为高时,输出解压缩数据。

相关推荐
众创岛4 小时前
python中enumerate的用法
开发语言·python
布史4 小时前
Prometheus Python Client 实操指南:从零实现自定义 Exporter
网络·python·prometheus
GateWorld4 小时前
FPGA内部模块详解之四 算力引擎——数字信号处理单元(DSP Slice)深度解析
fpga开发·dsp
纤纡.4 小时前
矿物识别分类:8 种机器学习算法对比与实战(平均值填充数据集)
python·深度学习·算法·机器学习
2301_818419015 小时前
使用PyTorch构建你的第一个神经网络
jvm·数据库·python
代码探秘者5 小时前
【算法篇】3.位运算
java·数据结构·后端·python·算法·spring
`Jay5 小时前
Python Redis连接池&账号管理池
redis·分布式·爬虫·python·学习
2301_793804695 小时前
Python异步编程入门:Asyncio库的使用
jvm·数据库·python
2301_810160955 小时前
NumPy入门:高性能科学计算的基础
jvm·数据库·python
add45a5 小时前
超越Python:下一步该学什么编程语言?
jvm·数据库·python