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

相关推荐
泡海椒2 小时前
内置SPI函数库详解:JQuick-Java Builtin工具类实战用法
java·开发语言·python
hqyjzsb2 小时前
零 AI 项目经验,学 Python 转型 AI 的正确顺序是什么?
开发语言·人工智能·python·算法·职场和发展·数据挖掘·数据分析
sanjiaomao3333 小时前
从论文公式到Python实现:用单元测试校验滑动平均算法
python·算法·单元测试
linweidong5 小时前
中科闻歌Java面试题及参考答案
java·python·大模型·提示词·ai agent·mcp·rag原理
小玮看世界5 小时前
[Python]OD算法在OD实际运用转化参考清单
开发语言·python·算法
萧鼎5 小时前
2026实战:用 tomlkit 优雅读写 TOML 配置,告别手写解析器
python·开源·教程·python库
szxinmai主板定制专家6 小时前
基于 RK3588+RK1820/28 算力卡的国产工控机,适用于机器视觉,机器人等边缘算力场景
人工智能·fpga开发·zynq·控制器·mpsoc·半导体设备
小刘在重生~6 小时前
Java 常用类|包装类 装箱拆箱、Integer 缓存面试题
java·python·缓存
2601_962099466 小时前
Python xlwt设置excel单元格字体及格式
python·excel·xlwt·样式设置·单元格格式
奇牙coding1236 小时前
GPT-6-Astra API 接入教程:OpenRouter 路由配置 + Python/curl 示例 + 静默降级踩坑
开发语言·python·gpt·ai