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

相关推荐
nenchoumi31191 小时前
AirSim/Cosys-AirSim 游戏开发(一)XBox 手柄 Windows + python 连接与读取
windows·python·xbox
GoodStudyAndDayDayUp1 小时前
初入 python Django 框架总结
数据库·python·django
星辰大海的精灵1 小时前
基于Dify+MCP实现通过微信发送天气信息给好友
人工智能·后端·python
精灵vector1 小时前
Agent短期记忆的几种持久化存储方式
人工智能·python
北京_宏哥1 小时前
🔥Python零基础从入门到精通详细教程4-数据类型的转换- 上篇
前端·python·面试
乾巫宇宙国监察特使1 小时前
Python的设计模式
python·测试
Hockor2 小时前
写给前端的 Python 教程四(列表/元组)
前端·后端·python
这里有鱼汤2 小时前
熟练掌握MACD这8种形态,让你少走三年弯路(附Python量化代码)| 建议收藏
后端·python
404.Not Found2 小时前
Day46 Python打卡训练营
开发语言·python
love530love2 小时前
【PyCharm必会基础】正确移除解释器及虚拟环境(以 Poetry 为例 )
开发语言·ide·windows·笔记·python·pycharm