基于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为高时,输出解压缩数据。

相关推荐
鸽芷咕15 分钟前
【Python报错已解决】ModuleNotFoundError: No module named ‘paddle‘
开发语言·python·机器学习·bug·paddle
子午26 分钟前
动物识别系统Python+卷积神经网络算法+TensorFlow+人工智能+图像识别+计算机毕业设计项目
人工智能·python·cnn
风等雨归期35 分钟前
【python】【绘制小程序】动态爱心绘制
开发语言·python·小程序
千穹凌帝38 分钟前
SpinalHDL之结构(二)
开发语言·前端·fpga开发
Adolf_199341 分钟前
Flask-JWT-Extended登录验证, 不用自定义
后端·python·flask
冯宝宝^41 分钟前
基于mongodb+flask(Python)+vue的实验室器材管理系统
vue.js·python·flask
叫我:松哥1 小时前
基于Python flask的医院管理学院,医生能够增加/删除/修改/删除病人的数据信息,有可视化分析
javascript·后端·python·mysql·信息可视化·flask·bootstrap
Eiceblue1 小时前
Python 复制Excel 中的行、列、单元格
开发语言·python·excel
NLP工程化2 小时前
对 Python 中 GIL 的理解
python·gil
极客代码2 小时前
OpenCV Python 深度指南
开发语言·人工智能·python·opencv·计算机视觉