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

相关推荐
孟健6 小时前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
码路飞7 小时前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python
曲幽10 小时前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers
敏编程14 小时前
一天一个Python库:jsonschema - JSON 数据验证利器
python
前端付豪14 小时前
LangChain记忆:通过Memory记住上次的对话细节
人工智能·python·langchain
databook15 小时前
ManimCE v0.20.1 发布:LaTeX 渲染修复与动画稳定性提升
python·动效
花酒锄作田1 天前
使用 pkgutil 实现动态插件系统
python
前端付豪1 天前
LangChain链 写一篇完美推文?用SequencialChain链接不同的组件
人工智能·python·langchain
曲幽1 天前
FastAPI实战:打造本地文生图接口,ollama+diffusers让AI绘画更听话
python·fastapi·web·cors·diffusers·lcm·ollama·dreamshaper8·txt2img
老赵全栈实战1 天前
Pydantic配置管理最佳实践(一)
python