高层次综合之FIFO和乒乓buffer
案例一#include hls_stream.h#include ap_int.h// 定义常量#define WIDTH 1920#define HEIGHT 1080#define SIZE 1024// 任务1: 生产者 (Producer)void producer(hls::streamap_uint8 input_stream, int buffer[SIZE]) {#pragma HLS INLINE OFF// 从外部AXI4-Stream接口读取数据并写入数组for (int i 0; i SIZE; i) {#pragma HLS PIPELINE II1buffer[i] input_stream.read(); // 示例直接传递}}// 任务2: 消费者 (Consumer)void consumer(int buffer[SIZE], hls::streamap_uint8 output_stream) {#pragma HLS INLINE OFF// 从数组读取数据并输出到AXI4-Stream接口for (int i 0; i SIZE; i) {#pragma HLS PIPELINE II1output_stream.write(buffer[i] 1); // 示例加1后输出}}// 顶层函数void top_function(hls::streamap_uint8 input_stream,hls::streamap_uint8 output_stream) {#pragma HLS INTERFACE axis portinput_stream#pragma HLS INTERFACE axis portoutput_stream#pragma HLS INTERFACE s_axilite portreturn// 定义一个数组作为任务间的数据通道int data_channel[SIZE];// 关键优化将此数组指定为流式FIFO以节省BRAM资源并实现流水#pragma HLS STREAM variabledata_channel depth16// 启用任务级流水线#pragma HLS DATAFLOW// 调用生产者和消费者producer(input_stream, data_channel);consumer(data_channel, output_stream);}案例二#include hls_stream.h#include ap_int.h#define WIDTH 1920#define HEIGHT 1080#define SIZE 1024void producer(hls::streamap_uint8 input_stream, int buffer[SIZE]) {#pragma HLS INLINE OFFfor (int i 0; i SIZE; i) {#pragma HLS PIPELINE II1buffer[i] input_stream.read();}}void consumer(int buffer[SIZE], hls::streamap_uint8 output_stream) {#pragma HLS INLINE OFFfor (int i 0; i SIZE; i) {#pragma HLS PIPELINE II1output_stream.write(buffer[i] 1);}}void top_function(hls::streamap_uint8 input_stream,hls::streamap_uint8 output_stream) {#pragma HLS INTERFACE axis portinput_stream#pragma HLS INTERFACE axis portoutput_stream#pragma HLS INTERFACE s_axilite portreturnint data_channel[SIZE];// 移除 #pragma HLS STREAMHLS将在DATAFLOW中默认使用乒乓缓冲双缓冲#pragma HLS DATAFLOWproducer(input_stream, data_channel);consumer(data_channel, output_stream);}