Files
Jungfraujoch/fpga/hls/save_to_hbm.cpp
T

237 lines
9.2 KiB
C++

// Copyright (2019-2022) Paul Scherrer Institute
// SPDX-License-Identifier: CERN-OHL-S-2.0 or GPL-3.0-or-later
#include "hls_jfjoch.h"
#ifndef __SYNTHESIS__
#include <thread>
#endif
#define PACKET_SIZE 8192
inline void write_completion(hls::stream<ap_uint<32> > &m_axis_completion,
const ap_uint<32> &handle,
const ap_uint<8> &module_number,
const ap_uint<64> &frame_num,
const ap_uint<256> &packet_mask,
const ap_uint<16> &packet_count,
const ap_uint<32> &debug,
const ap_uint<64> &timestamp,
const ap_uint<64> &bunchid,
const ap_uint<32> &exptime,
const ap_uint<32> &data_collection_id,
const ap_uint<1> &flushing) {
#pragma HLS INLINE
ap_uint<1> all_packets_ok = packet_mask.and_reduce();
ap_uint<1> any_packets_received = packet_mask.or_reduce();
ap_uint<8> status = 0;
status[0] = all_packets_ok;
status[1] = any_packets_received;
status[2] = flushing;
ap_uint<128> tmp = (handle, packet_count, status, module_number, frame_num);
status[7] = tmp.xor_reduce(); // ensure completion has even parity
if (handle != HANDLE_SKIP_FRAME) {
m_axis_completion << handle;
m_axis_completion << (packet_count, status, module_number);
m_axis_completion << frame_num(63, 32);
m_axis_completion << frame_num(31, 0);
m_axis_completion << timestamp(63,32);
m_axis_completion << timestamp(31,0);
m_axis_completion << bunchid(63,32);
m_axis_completion << bunchid(31,0);
m_axis_completion << exptime;
m_axis_completion << debug;
m_axis_completion << 0;
m_axis_completion << data_collection_id;
m_axis_completion << packet_mask(127,96);
m_axis_completion << packet_mask( 95,64);
m_axis_completion << packet_mask( 63,32);
m_axis_completion << packet_mask( 31, 0);
}
}
void save_to_hbm(STREAM_512 &data_in,
hls::stream<ap_uint<ADDR_STREAM_WIDTH> > &addr_in,
hls::stream<ap_uint<32> > &completion_out,
hls::burst_maxi<hbm256_t> d_hbm_p0, hls::burst_maxi<hbm256_t> d_hbm_p1,
hls::burst_maxi<hbm256_t> d_hbm_p2, hls::burst_maxi<hbm256_t> d_hbm_p3,
volatile uint64_t &packets_processed,
volatile ap_uint<1> &idle,
ap_uint<8> &err_reg,
uint32_t hbm_size) {
#pragma HLS INTERFACE ap_ctrl_none port=return
#pragma HLS INTERFACE register both axis port=data_in
#pragma HLS INTERFACE register both axis port=addr_in
#pragma HLS INTERFACE register both axis port=completion_out
#pragma HLS INTERFACE register ap_vld port=packets_processed
#pragma HLS INTERFACE register ap_vld port=err_reg
#pragma HLS INTERFACE register ap_none port=idle
#pragma HLS INTERFACE register ap_stable port=hbm_size
#pragma HLS INTERFACE m_axi port=d_hbm_p0 bundle=d_hbm_p0 depth=512 offset=direct \
max_read_burst_length=2 max_write_burst_length=16 latency=120 num_write_outstanding=8 num_read_outstanding=2
#pragma HLS INTERFACE m_axi port=d_hbm_p1 bundle=d_hbm_p1 depth=512 offset=direct \
max_read_burst_length=2 max_write_burst_length=16 latency=120 num_write_outstanding=8 num_read_outstanding=2
#pragma HLS INTERFACE m_axi port=d_hbm_p2 bundle=d_hbm_p2 depth=512 offset=direct \
max_read_burst_length=2 max_write_burst_length=16 latency=120 num_write_outstanding=8 num_read_outstanding=2
#pragma HLS INTERFACE m_axi port=d_hbm_p3 bundle=d_hbm_p3 depth=512 offset=direct \
max_read_burst_length=2 max_write_burst_length=16 latency=120 num_write_outstanding=8 num_read_outstanding=2
ap_uint<128> packet_mask[MAX_MODULES_FPGA*2];
#pragma HLS RESOURCE variable=packet_mask core=RAM_1P
ap_uint<16> packet_count[MAX_MODULES_FPGA*2];
#pragma HLS RESOURCE variable=packet_count core=RAM_1P
ap_uint<32> handle[MAX_MODULES_FPGA*2];
#pragma HLS RESOURCE variable=handle core=RAM_1P
ap_uint<64> curr_frame[MAX_MODULES_FPGA*2];
#pragma HLS RESOURCE variable=curr_frame core=RAM_1P
ap_uint<32> debug[MAX_MODULES_FPGA*2];
#pragma HLS RESOURCE variable=debug core=RAM_1P
ap_uint<64> timestamp[MAX_MODULES_FPGA*2];
#pragma HLS RESOURCE variable=timestamp core=RAM_1P
ap_uint<32> exptime[MAX_MODULES_FPGA*2];
#pragma HLS RESOURCE variable=exptime core=RAM_1P
ap_uint<64> jf_bunchid[MAX_MODULES_FPGA*2];
#pragma HLS RESOURCE variable=jf_bunchid core=RAM_1P
idle = 1;
for (int i = 0; i < MAX_MODULES_FPGA*2; i++) {
#pragma HLS UNROLL
curr_frame[i] = UINT64_MAX;
handle[i] = 0;
packet_mask[i] = 0;
packet_count[i] = 0;
debug[i] = 0;
timestamp[i] = 0;
exptime[i] = 0;
jf_bunchid[i] = 0;
}
uint32_t handle_val = 0;
ap_uint<ADDR_STREAM_WIDTH> addr;
addr_in >> addr;
packet_512_t packet_in;
data_in >> packet_in;
ap_uint<5> nmodules = ACT_REG_NMODULES(packet_in.data);
ap_uint<32> data_collection_mode = ACT_REG_MODE(packet_in.data);
ap_uint<32> data_collection_id = data_collection_mode(31, 16); // upper 16-bit of mode
ap_uint<1> mode_nonblocking = (data_collection_mode & MODE_NONBLOCKING_ON_WR) ? 1 : 0;
ap_uint<8> internal_err_reg = 0;
err_reg = internal_err_reg;
idle = 0;
uint64_t total_counter = 0;
packets_processed = 0;
addr_in >> addr;
Loop_good_packet:
while (!addr_last_flag(addr)) {
// Process one UDP packet per iteration
#pragma HLS PIPELINE II=128
ap_uint<64> frame_number = addr_frame_number(addr);
ap_uint<4> module_number = addr_module(addr);
ap_uint<7> eth_packet = addr_eth_packet(addr);
ap_uint<5> id = module_number * 2 + (frame_number % 2);
if (curr_frame[id] != frame_number) {
if (packet_mask[id] != 0) {
ap_uint<32> comp_handle = handle[id];
ap_uint<64> comp_frame = curr_frame[id];
ap_uint<256> comp_packet_mask = packet_mask[id];
ap_uint<16> comp_packet_count = packet_count[id];
ap_uint<32> comp_debug = debug[id];
ap_uint<64> comp_timestamp = timestamp[id];
ap_uint<64> comp_bunchid = jf_bunchid[id];
ap_uint<32> comp_exptime = exptime[id];
write_completion(completion_out, comp_handle, module_number,
comp_frame, comp_packet_mask, comp_packet_count,
comp_debug, comp_timestamp, comp_bunchid,
comp_exptime, data_collection_id, 0);
}
handle[id] = handle_val;
curr_frame[id] = frame_number;
debug[id] = addr_jf_debug(addr);
timestamp[id] = addr_timestamp(addr);
jf_bunchid[id] = addr_bunch_id(addr);
exptime[id] = addr_exptime(addr);
packet_mask[id] = ap_uint<128>(1) << eth_packet;
packet_count[id] = 1;
if (hbm_size == 64)
handle_val = (handle_val + 1) % 64;
else if (hbm_size == 512)
handle_val = (handle_val + 1) % 512;
else if (hbm_size == 1024)
handle_val = (handle_val + 1) % 1024;
else
handle_val = (handle_val + 1) % 32;
} else {
packet_count[id]++;
packet_mask[id] |= ap_uint<128>(1) << eth_packet;
}
size_t out_frame_addr = (handle[id] * 128 + eth_packet) * 64;
for (int i = 0; i < 64; i++) {
if (i % 16 == 0) {
d_hbm_p0.write_request(out_frame_addr + i, 16);
d_hbm_p1.write_request(out_frame_addr + i, 16);
d_hbm_p2.write_request(out_frame_addr + i, 16);
d_hbm_p3.write_request(out_frame_addr + i, 16);
}
data_in >> packet_in;
d_hbm_p0.write(packet_in.data(255, 0));
d_hbm_p1.write(packet_in.data(511, 256));
data_in >> packet_in;
d_hbm_p2.write(packet_in.data(255, 0));
d_hbm_p3.write(packet_in.data(511, 256));
if (i % 16 == 15) {
d_hbm_p0.write_response();
d_hbm_p1.write_response();
d_hbm_p2.write_response();
d_hbm_p3.write_response();
}
}
if (packet_in.last != 1)
internal_err_reg[1] = 1;
total_counter++;
packets_processed = total_counter;
addr_in >> addr;
err_reg = internal_err_reg;
}
for (ap_uint<8> m = 0; m < nmodules * 2; m++) {
#pragma HLS PIPELINE II=16
if (packet_mask[m] != 0)
write_completion(completion_out, handle[m], m / 2, curr_frame[m],
packet_mask[m], packet_count[m],
debug[m], timestamp[m], jf_bunchid[m],
exptime[m], data_collection_id, 1);
}
data_in >> packet_in;
idle = 1;
}