diff --git a/fpga/hls/hls_jfjoch.h b/fpga/hls/hls_jfjoch.h index 5332e2a8..e56f4699 100644 --- a/fpga/hls/hls_jfjoch.h +++ b/fpga/hls/hls_jfjoch.h @@ -202,6 +202,12 @@ void spot_finder(STREAM_512 &data_in, volatile ap_int<16> &in_photon_count_threshold, volatile strong_pixel_threshold_t &in_strong_pixel_threshold); +void spot_finder_2(STREAM_512 &data_in, + STREAM_512 &data_out, + hls::stream> &strong_pixel_out, + volatile ap_int<16> &in_photon_count_threshold, + volatile strong_pixel_threshold_t &in_strong_pixel_threshold); + void adu_histo(STREAM_512 &data_in, STREAM_512 &data_out, hls::stream> &result_out, diff --git a/fpga/hls/spot_finder.cpp b/fpga/hls/spot_finder.cpp index 4994ec27..b69c994d 100644 --- a/fpga/hls/spot_finder.cpp +++ b/fpga/hls/spot_finder.cpp @@ -114,11 +114,11 @@ ap_uint<32> check_threshold(const ap_uint<512> data_packed, return tmp_output; } -void spot_finder_update_sum(STREAM_512 &data_in, - hls::stream &data_out, - hls::stream> &sum_out, - hls::stream> &sum2_out, - hls::stream> &valid_out) { +void spot_finder_col_sum(STREAM_512 &data_in, + hls::stream &data_out, + hls::stream> &sum_out, + hls::stream> &sum2_out, + hls::stream> &valid_out) { ap_uint<512> data_cache[(2 * FPGA_NBX + 1) * 32]; ap_uint<32> mask_cache[(2 * FPGA_NBX + 1) * 32]; @@ -154,6 +154,7 @@ void spot_finder_update_sum(STREAM_512 &data_in, if (i < RAW_MODULE_SIZE * sizeof(uint16_t) / 64) { data_out << spot_finder_packet{.data=packet_in.data, .user=0}; calc_mask(packet_in.data, packet_in_data, packet_in_mask); + data_in >> packet_in; } else { packet_in_data = 0; packet_in_mask = 0; @@ -192,9 +193,6 @@ void spot_finder_update_sum(STREAM_512 &data_in, data_cache[cell_number_top] = packet_in_data; mask_cache[cell_number_top] = packet_in_mask; - - if (i < RAW_MODULE_SIZE * sizeof(uint16_t) / 64) - data_in >> packet_in; } } data_out << spot_finder_packet{.data=0, .user=1}; @@ -310,7 +308,8 @@ void spot_finder_line_sum_align(hls::stream &data_in, data_out << packet_in; } -void spot_finder_apply_threshold(hls::stream &data_in, +void spot_finder_check_threshold(hls::stream &data_in, + STREAM_512 &data_out, hls::stream> &sum_in, hls::stream> &sum2_in, hls::stream> &valid_in, @@ -319,14 +318,14 @@ void spot_finder_apply_threshold(hls::stream &data_in, volatile strong_pixel_threshold_t &in_strong_pixel_threshold) { spot_finder_packet packet_in; data_in >> packet_in; - + data_out << packet_512_t{.data=packet_in.data, .user=0, .last=0}; ap_uint line_sum = 0; ap_uint line_sum2 = 0; ap_uint line_valid = 0; data_in >> packet_in; while (!packet_in.user) { - data_in >> packet_in; + data_out << packet_512_t{.data=packet_in.data, .user=0, .last=0}; sum_in >> line_sum; sum2_in >> line_sum2; valid_in >> line_valid; @@ -335,36 +334,55 @@ void spot_finder_apply_threshold(hls::stream &data_in, strong_pixel.data = check_threshold(packet_in.data, line_sum, line_sum2, line_valid, in_strong_pixel_threshold, in_photon_count_threshold); strong_pixel_out << strong_pixel; + data_in >> packet_in; } strong_pixel_out << ap_axiu<32,1,1,1>{.data = 0, .user = 1}; + data_out << packet_512_t{.data=0, .user=1, .last=1}; } void spot_finder_2(STREAM_512 &data_in, - hls::stream> &strong_pixel_out, - volatile ap_int<16> &in_photon_count_threshold, - volatile strong_pixel_threshold_t &in_strong_pixel_threshold) { + STREAM_512 &data_out, + hls::stream> &strong_pixel_out, + volatile ap_int<16> &in_photon_count_threshold, + volatile strong_pixel_threshold_t &in_strong_pixel_threshold) { #pragma HLS INTERFACE ap_ctrl_none port=return #pragma HLS DATAFLOW #pragma HLS INTERFACE axis port=data_in +#pragma HLS INTERFACE axis port=data_out #pragma HLS INTERFACE axis port=strong_pixel_out #pragma HLS INTERFACE ap_none register port=in_photon_count_threshold #pragma HLS INTERFACE ap_none register port=in_strong_pixel_threshold - hls::stream data_stream_0, data_stream_1, data_stream_2, data_stream_3; - hls::stream> sum_stream_0, sum_stream_1, sum_stream_2, sum_stream_3; - hls::stream> sum2_stream_0, sum2_stream_1, sum2_stream_2, sum2_stream_3; - hls::stream> valid_stream_0, valid_stream_1, valid_stream_2, valid_stream_3; + hls::stream data_stream_0, data_stream_1, data_stream_2; + hls::stream> sum_stream_0, sum_stream_1, sum_stream_2; + hls::stream> sum2_stream_0, sum2_stream_1, sum2_stream_2; + hls::stream> valid_stream_0, valid_stream_1, valid_stream_2; - spot_finder_update_sum(data_in, data_stream_0, sum_stream_0, sum2_stream_0, valid_stream_0); +#ifdef __SYNTHESIS__ + spot_finder_col_sum(data_in, data_stream_0, sum_stream_0, sum2_stream_0, valid_stream_0); spot_finder_line_sum(data_stream_0, data_stream_1, sum_stream_0, sum2_stream_0, valid_stream_0, sum_stream_1, sum2_stream_1, valid_stream_1); spot_finder_line_sum_align(data_stream_1, data_stream_2, sum_stream_1, sum2_stream_1, valid_stream_1, sum_stream_2, sum2_stream_2, valid_stream_2); - spot_finder_apply_threshold(data_stream_2, sum_stream_2, sum2_stream_2, valid_stream_2, + spot_finder_check_threshold(data_stream_2, data_out, sum_stream_2, sum2_stream_2, valid_stream_2, strong_pixel_out, in_photon_count_threshold, in_strong_pixel_threshold); +#else + std::vector spot_finder_cores; + spot_finder_cores.emplace_back([&] {spot_finder_col_sum(data_in, data_stream_0, sum_stream_0, sum2_stream_0, valid_stream_0);}); + spot_finder_cores.emplace_back([&] {spot_finder_line_sum(data_stream_0, data_stream_1, + sum_stream_0, sum2_stream_0, valid_stream_0, + sum_stream_1, sum2_stream_1, valid_stream_1);}); + spot_finder_cores.emplace_back([&] {spot_finder_line_sum_align(data_stream_1, data_stream_2, + sum_stream_1, sum2_stream_1, valid_stream_1, + sum_stream_2, sum2_stream_2, valid_stream_2);}); + spot_finder_cores.emplace_back([&] {spot_finder_check_threshold(data_stream_2, data_out, sum_stream_2, sum2_stream_2, valid_stream_2, + strong_pixel_out, in_photon_count_threshold, in_strong_pixel_threshold);}); + for (auto &i : spot_finder_cores) + i.join(); +#endif } void spot_finder(STREAM_512 &data_in, diff --git a/fpga/hls/spot_finder.h b/fpga/hls/spot_finder.h index 3b31d1d3..d5b001f8 100644 --- a/fpga/hls/spot_finder.h +++ b/fpga/hls/spot_finder.h @@ -64,6 +64,7 @@ ap_uint prefix_sum(const ap_uint ext_column_sum, ap_int column_sum[32 + 2 * FPGA_NBX]; ap_uint retval; ap_int line_sum[32]; + ap_int tmp_sum = 0; for (int i = 0; i < 2 * FPGA_NBX; i++) column_sum[i] = ext_column_sum_old(i*N+(N-1), i*N); @@ -71,19 +72,18 @@ ap_uint prefix_sum(const ap_uint ext_column_sum, for (int i = 0; i < 32; i++) column_sum[i+ 2 * FPGA_NBX] = ext_column_sum(i * N + (N - 1), i * N); - for (int i = 0; i < 32; i++) { - line_sum[i] = 0; - for (int j = 0; j < 2 * FPGA_NBX + 1; j++) - line_sum[i] += column_sum[i + j]; - } + //for (int i = 0; i < 32; i++) { + // line_sum[i] = 0; + // for (int j = 0; j < 2 * FPGA_NBX + 1; j++) + // line_sum[i] += column_sum[i + j]; + //} -/* for (int i = 0; i < 32; i++) { tmp_sum += column_sum[i + 2 * FPGA_NBX]; line_sum[i] = tmp_sum; tmp_sum -= column_sum[i]; } -*/ + return pack32(line_sum); } @@ -101,11 +101,11 @@ void calc_sum(ap_uint &ext_column_sum, void calc_mask(const ap_uint<512> &input, ap_uint<512> &output, ap_uint<32> &mask); -void spot_finder_update_sum(STREAM_512 &data_in, - hls::stream &data_out, - hls::stream> &sum_out, - hls::stream> &sum2_out, - hls::stream> &valid_out); +void spot_finder_col_sum(STREAM_512 &data_in, + hls::stream &data_out, + hls::stream> &sum_out, + hls::stream> &sum2_out, + hls::stream> &valid_out); void spot_finder_line_sum(hls::stream &data_in, hls::stream &data_out, @@ -125,7 +125,7 @@ void spot_finder_line_sum_align(hls::stream &data_in, hls::stream> &sum2_out, hls::stream> &valid_out); -void spot_finder_apply_threshold(hls::stream &data_in, +void spot_finder_check_threshold(hls::stream &data_in, hls::stream> &sum_in, hls::stream> &sum2_in, hls::stream> &valid_in, diff --git a/fpga/scripts/jfjoch.tcl b/fpga/scripts/jfjoch.tcl index ed2b97d8..7e2bfd99 100644 --- a/fpga/scripts/jfjoch.tcl +++ b/fpga/scripts/jfjoch.tcl @@ -530,7 +530,7 @@ proc create_hier_cell_jungfraujoch { parentCell nameHier } { set smartconnect_2 [ create_bd_cell -type ip -vlnv xilinx.com:ip:smartconnect:1.0 smartconnect_2 ] # Create instance: spot_finder_0, and set properties - set spot_finder_0 [ create_bd_cell -type ip -vlnv psi.ch:hls:spot_finder:1.0 spot_finder_0 ] + set spot_finder_0 [ create_bd_cell -type ip -vlnv psi.ch:hls:spot_finder_2:1.0 spot_finder_0 ] # Create instance: stream_merge_0, and set properties set stream_merge_0 [ create_bd_cell -type ip -vlnv psi.ch:hls:stream_merge:1.0 stream_merge_0 ] diff --git a/receiver/HLSSimulatedDevice.cpp b/receiver/HLSSimulatedDevice.cpp index 2ab3ed02..2e81a85d 100644 --- a/receiver/HLSSimulatedDevice.cpp +++ b/receiver/HLSSimulatedDevice.cpp @@ -352,7 +352,7 @@ void HLSSimulatedDevice::HLSMainThread() { strong_pixel_threshold_t strong_pixel_threshold = 7; // 6. Spot finding - hls_cores.emplace_back([&] { spot_finder(converted_5, converted_6, + hls_cores.emplace_back([&] { spot_finder_2(converted_5, converted_6, spot_finder_result_0, photon_count_threshold, strong_pixel_threshold);}); diff --git a/tests/FPGASpotFindingUnitTest.cpp b/tests/FPGASpotFindingUnitTest.cpp index c8476cb0..56d3bccc 100644 --- a/tests/FPGASpotFindingUnitTest.cpp +++ b/tests/FPGASpotFindingUnitTest.cpp @@ -190,7 +190,7 @@ TEST_CASE("FPGA_spot_finder_update_sum","[FPGA][SpotFinder]") { hls::stream> sum2_out; hls::stream> valid_out; - std::vector input_frame(RAW_MODULE_SIZE); + std::vector input_frame(RAW_MODULE_SIZE), output_frame(RAW_MODULE_SIZE); for (int i = 0; i < RAW_MODULE_SIZE; i++) { if (i % RAW_MODULE_COLS == 1023) input_frame[i] = INT16_MIN; @@ -198,6 +198,7 @@ TEST_CASE("FPGA_spot_finder_update_sum","[FPGA][SpotFinder]") { input_frame[i] = i % RAW_MODULE_COLS; } auto input_frame_512 = (ap_uint<512> *) input_frame.data(); + auto output_frame_512 = (ap_uint<512> *) output_frame.data(); input << packet_512_t{.user = 0}; for (int i = 0; i < RAW_MODULE_SIZE * sizeof(uint16_t) / 64; i++) @@ -205,7 +206,7 @@ TEST_CASE("FPGA_spot_finder_update_sum","[FPGA][SpotFinder]") { input << packet_512_t{.user = 1}; - spot_finder_update_sum(input, output, sum_out, sum2_out, valid_out); + spot_finder_col_sum(input, output, sum_out, sum2_out, valid_out); REQUIRE(input.size() == 0); @@ -262,6 +263,14 @@ TEST_CASE("FPGA_spot_finder_update_sum","[FPGA][SpotFinder]") { CHECK(valid[1+509*1024] == FPGA_NBX + 1 + 2); CHECK(valid[1+510*1024] == FPGA_NBX + 1 + 1); CHECK(valid[1+511*1024] == FPGA_NBX + 1); + + spot_finder_packet packet_out; + output >> packet_out; + for (int i = 0; i < RAW_MODULE_SIZE * sizeof(uint16_t) / 64; i++) { + output >> packet_out; + output_frame_512[i] = packet_out.data; + } + REQUIRE(output_frame == input_frame); } TEST_CASE("FPGA_spot_finder_line_sum","[FPGA][SpotFinder]") { @@ -285,9 +294,19 @@ TEST_CASE("FPGA_spot_finder_line_sum","[FPGA][SpotFinder]") { ap_int sum2_unpacked[32]; ap_int valid_unpacked[32]; + std::vector input_frame(RAW_MODULE_COLS), output_frame(RAW_MODULE_COLS); + for (int i = 0; i < RAW_MODULE_COLS; i++) { + if (i % RAW_MODULE_COLS == 1023) + input_frame[i] = INT16_MIN; + else + input_frame[i] = i % RAW_MODULE_COLS; + } + auto input_frame_512 = (ap_uint<512> *) input_frame.data(); + auto output_frame_512 = (ap_uint<512> *) output_frame.data(); + input << spot_finder_packet{.user = 0}; for (int i = 0; i < 32; i++) - input << spot_finder_packet{.data = 0, .user = 0}; + input << spot_finder_packet{.data = input_frame_512[i], .user = 0}; input << spot_finder_packet{.user = 1}; @@ -358,4 +377,11 @@ TEST_CASE("FPGA_spot_finder_line_sum","[FPGA][SpotFinder]") { CHECK(valid_output[1022] == FPGA_NBX+2); CHECK(valid_output[1023] == FPGA_NBX+1); + + spot_finder_packet packet_out; + output >> packet_out; + for (int i = 0; i < 32; i++) { + output >> packet_out; + output_frame_512[i] = packet_out.data; + } }