From e229f9a0fa8b1ff554ac47fddd8ce847e0a5bf57 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Tue, 17 Nov 2026 14:07:16 +0100 Subject: [PATCH 1/6] MockAcquisitionDevice: behavioural mock of the FPGA card, for tests and the broker without hardware The card as a black box, as the host sees it: which module is written to which host buffer and when, the order of completions, packet counts and masks, frames missing or late, summation groups, counters, the end of the data collection, cancel, and the internal frame generator with the refusals of frame_generator.cpp (return codes 1-3, the data collection cancelled, FPGA_EndAction throwing as HLSSimulatedDevice does). It computes nothing: what the card writes for a module is given with SetOutput(); without one, the pixels are the internal generator's frame for it (the frame loaded with SetInternalGeneratorFrame, 16 bit, not reordered) or zeros, every result zero. The card memory is 32 MiB per HBM interface as in the HLS simulation, so the host's generator memory check (64 module frames, the last kept free) applies. Any number of these devices can run at the same time. From 8e3b0f88d (rc186), extended with what the FPGA path gained since: the generator memory check and the generator's refusal path. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01UqXYdHHZ4cogS1mrGUtfN7 --- acquisition_device/CMakeLists.txt | 1 + acquisition_device/MockAcquisitionDevice.cpp | 556 +++++++++++++++++++ acquisition_device/MockAcquisitionDevice.h | 143 +++++ 3 files changed, 700 insertions(+) create mode 100644 acquisition_device/MockAcquisitionDevice.cpp create mode 100644 acquisition_device/MockAcquisitionDevice.h diff --git a/acquisition_device/CMakeLists.txt b/acquisition_device/CMakeLists.txt index f9b4d4241..abb3bd3ef 100644 --- a/acquisition_device/CMakeLists.txt +++ b/acquisition_device/CMakeLists.txt @@ -5,6 +5,7 @@ ADD_LIBRARY(JFJochAcquisitionDevice STATIC AcquisitionDevice.cpp AcquisitionDevice.h AcquisitionCounters.cpp AcquisitionCounters.h HLSSimulatedDevice.cpp HLSSimulatedDevice.h + MockAcquisitionDevice.cpp MockAcquisitionDevice.h Completion.cpp Completion.h ../fpga/pcie_driver/jfjoch_fpga.h PCIExpressDevice.cpp PCIExpressDevice.h FPGAAcquisitionDevice.cpp FPGAAcquisitionDevice.h diff --git a/acquisition_device/MockAcquisitionDevice.cpp b/acquisition_device/MockAcquisitionDevice.cpp new file mode 100644 index 000000000..9c39f20bb --- /dev/null +++ b/acquisition_device/MockAcquisitionDevice.cpp @@ -0,0 +1,556 @@ +// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute +// SPDX-License-Identifier: GPL-3.0-only + +#include + +#include "MockAcquisitionDevice.h" + +namespace { + // Memory of one HBM interface, as the HLS simulation has it: 64 module frames for the internal generator + constexpr uint32_t CardMemoryBytes = 32 * 1024 * 1024; + + bool MaskEmpty(const uint64_t packet_mask[8]) { + for (int i = 0; i < 8; i++) { + if (packet_mask[i] != 0) + return false; + } + return true; + } +} + +MockAcquisitionDevice::MockAcquisitionDevice(uint16_t data_stream, size_t in_frame_buffer_size_modules) + : FPGAAcquisitionDevice(data_stream), + collected_modules(2 * MAX_MODULES_FPGA), + summation_completions(MAX_MODULES_FPGA * MAX_FPGA_SUMMATION), + summation_mask(MAX_MODULES_FPGA, std::vector(MAX_FPGA_SUMMATION)), + summation_count(MAX_MODULES_FPGA), + summation_frame_prefix(MAX_MODULES_FPGA) { + mac_addr = 0xCCAA11223344; + ipv4_addr = 0x0132010A; + + max_modules = MAX_MODULES_FPGA; + internal_generator_module_frames = GeneratorModuleFrames(CardMemoryBytes); + generator_frames.resize(internal_generator_module_frames); + + buffers.reserve(in_frame_buffer_size_modules); + for (size_t i = 0; i < in_frame_buffer_size_modules; i++) { + buffers.push_back(std::make_unique()); + buffer_device.push_back(reinterpret_cast(buffers.back().get())); + } +} + +MockAcquisitionDevice::~MockAcquisitionDevice() { + if (action_thread.joinable()) + action_thread.join(); + stop_frame_generator = true; +} + +void MockAcquisitionDevice::SetOutput(uint64_t image, uint32_t module_number, const DeviceOutput &output) { + if (outputs.size() <= image) + outputs.resize(image + 1); + if (outputs[image].size() <= module_number) + outputs[image].resize(module_number + 1); + outputs[image][module_number] = std::make_unique(output); +} + +void MockAcquisitionDevice::AddPacket(const Packet &packet) { + std::unique_lock ul(packets_mutex); + packets.push_back(packet); +} + +void MockAcquisitionDevice::CreateJFPacket(const DiffractionExperiment &experiment, uint64_t frame_number, + uint32_t eth_packet, uint32_t module_number, const uint16_t *data, + int8_t adjust_axis, uint8_t user) { + uint32_t half_module = ((eth_packet >= 64) ? 1 : 0); + AddPacket(Packet{ + .frame_number = frame_number, + .packet_number = eth_packet % 64, + .udp_port = static_cast(2 * module_number + half_module), + .column = 0, + .detector_type = SLS_DETECTOR_TYPE_JUNGFRAU, + .exptime = 0, + .bunchid = 0x1234567898765431L, + .timestamp = 0xABCDEF0000FEDCBAL, + .debug = 0, + .eth_error = (user % 2 == 1), + .len_error = (adjust_axis != 0), + .generator_slot = NoGeneratorSlot + }); +} + +void MockAcquisitionDevice::CreateJFPackets(const DiffractionExperiment &experiment, uint64_t frame_number_0, + uint64_t frames, uint32_t module_number, const uint16_t *data) { + for (uint64_t i = 0; i < frames; i++) { + for (int j = 0; j < 128; j++) + CreateJFPacket(experiment, frame_number_0 + i, j, module_number, nullptr, 0, 0); + } +} + +void MockAcquisitionDevice::CreateEIGERPacket(const DiffractionExperiment &experiment, uint64_t frame_number, + uint32_t eth_packet, uint32_t module_number, uint32_t col, + uint32_t row, const uint16_t *data) { + AddPacket(Packet{ + .frame_number = frame_number, + .packet_number = eth_packet % 128, + .udp_port = static_cast(2 * module_number + row), + .column = static_cast(col % 2), + .detector_type = SLS_DETECTOR_TYPE_EIGER, + .exptime = 0, + .bunchid = 0x1234567898765431L, + .timestamp = 0xABCDEF0000FEDCBAL, + .debug = 0, + .generator_slot = NoGeneratorSlot + }); +} + +void MockAcquisitionDevice::CreateXfelBunchIDPacket(double pulse_id, uint32_t event_code) { + Packet packet{.bunchid_packet = true, .generator_slot = NoGeneratorSlot}; + memcpy(&packet.bunchid, &pulse_id, sizeof(uint64_t)); + AddPacket(packet); +} + +void MockAcquisitionDevice::CreateFinalPacket(const DiffractionExperiment &experiment) { + CreateJFPacket(experiment, UINT64_MAX, 0, 0, nullptr, false); +} + +void MockAcquisitionDevice::HW_ReadActionRegister(DataCollectionConfig *job) { + memcpy(job, &cfg, sizeof(DataCollectionConfig)); +} + +void MockAcquisitionDevice::HW_WriteActionRegister(const DataCollectionConfig *job) { + memcpy(&cfg, job, sizeof(DataCollectionConfig)); +} + +void MockAcquisitionDevice::FPGA_StartAction(const DiffractionExperiment &experiment) { + if (action_thread.joinable()) + action_thread.join(); + + run_counter += 1; + idle = false; + action_thread = std::thread(&MockAcquisitionDevice::DataCollectionThread, this); +} + +void MockAcquisitionDevice::FPGA_EndAction() { + if (action_thread.joinable()) + action_thread.join(); + if (frame_generator_result != 0) + throw JFJochException(JFJochExceptionCategory::AcquisitionDeviceError, + "Internal frame generator refused its configuration, error code " + + std::to_string(frame_generator_result)); +} + +bool MockAcquisitionDevice::HW_IsIdle() const { + return idle; +} + +bool MockAcquisitionDevice::HW_ReadMailbox(uint32_t *values) { + return completions.Get(*values); +} + +bool MockAcquisitionDevice::HW_SendWorkRequest(uint32_t handle) { + work_requests.Put(handle); + return true; +} + +void MockAcquisitionDevice::Cancel() { + cancel = true; +} + +// The mock does not process data: of what the host loads into the card only the frames of the internal +// generator are kept +void MockAcquisitionDevice::HW_LoadCalibration(const LoadCalibrationConfig &config) { + const DeviceOutput *input = buffer_device[config.handle]; + if (input->module_statistics.load_calibration_destination != LOAD_CALIBRATION_DEST_FRAME_GEN) + return; + auto &frame = generator_frames.at(input->module_statistics.module_number); + frame.resize(RAW_MODULE_SIZE); + memcpy(frame.data(), input->pixels, RAW_MODULE_SIZE * sizeof(uint16_t)); +} + +void MockAcquisitionDevice::HW_SetSpotFinderParameters(const SpotFinderParameters ¶ms) {} + +void MockAcquisitionDevice::HW_RunInternalGenerator(const FrameGeneratorConfig &config) { + frame_generator_result = 0; + frame_generator_future = std::async(std::launch::async, [this, config] { + frame_generator_result = FrameGenerator(config); + // A generator that refused its configuration sends no frame: the data collection ends instead of waiting + if (frame_generator_result != 0) + cancel = true; + }); +} + +int MockAcquisitionDevice::FrameGenerator(FrameGeneratorConfig config) { + // frame_generator.cpp: its checks and return codes, then packet headers only + if ((config.modules > MAX_MODULES_FPGA) || (config.modules == 0)) + return 1; + if ((config.detector_type != SLS_DETECTOR_TYPE_EIGER) && (config.detector_type != SLS_DETECTOR_TYPE_JUNGFRAU)) + return 2; + if ((config.detector_type == SLS_DETECTOR_TYPE_EIGER) + && (config.eiger_bit_depth != 8) && (config.eiger_bit_depth != 16) && (config.eiger_bit_depth != 32)) + return 2; + if (config.frames == 0) + return 0; + // The frames for the generator have to fit in the card memory + uint32_t module_slots = config.modules * (config.images_in_memory + 1); + if ((config.detector_type == SLS_DETECTOR_TYPE_EIGER) && (config.eiger_bit_depth == 32)) + module_slots *= 2; + if (module_slots >= internal_generator_module_frames) + return 3; + + double pulse_id = config.pulse_id; + uint16_t data_stream_port = (config.data_stream & UINT8_MAX) << 8; + + // The packets wait until the data collection takes them - only a limited number of them + auto send = [this] (const Packet &packet) { + while (!cancel && !stop_frame_generator) { + { + std::unique_lock ul(packets_mutex); + if (packets.size() < 16384) { + packets.push_back(packet); + return; + } + } + std::this_thread::sleep_for(std::chrono::microseconds(10)); + } + }; + + // One packet with TUSER = 1, to reset the network cores on the way - it is dropped as an Ethernet error + send(Packet{.frame_number = 1, .udp_port = data_stream_port, .detector_type = SLS_DETECTOR_TYPE_JUNGFRAU, + .eth_error = true, .generator_slot = NoGeneratorSlot}); + + uint32_t frame_cell = 0; + for (uint32_t f = 0; f < config.frames; f++) { + if (cancel || stop_frame_generator) + break; + uint64_t bunchid; + memcpy(&bunchid, &pulse_id, sizeof(uint64_t)); + + for (uint32_t m = 0; m < config.modules; m++) { + if (config.detector_type == SLS_DETECTOR_TYPE_JUNGFRAU) { + for (uint32_t p = 0; p < 128; p++) + send(Packet{ + .frame_number = f + 1, + .packet_number = p, + .udp_port = static_cast(data_stream_port | (2 * m)), + .column = 0, + .detector_type = SLS_DETECTOR_TYPE_JUNGFRAU, + .exptime = config.exptime, + .bunchid = bunchid, + .timestamp = config.exptime * f, + .debug = config.debug, + .generator_slot = frame_cell * config.modules + m + }); + } else { + uint32_t bit_depth = config.eiger_bit_depth; + for (uint32_t p = 0; p < 16 * bit_depth; p++) { + uint32_t half_module = (p >= 8 * bit_depth) ? 1 : 0; + send(Packet{ + .frame_number = f + 1, + .packet_number = (p / 2) % (4 * bit_depth), + .udp_port = static_cast(data_stream_port | (2 * m + half_module)), + .column = static_cast(p % 2), + .detector_type = SLS_DETECTOR_TYPE_EIGER, + .exptime = config.exptime, + .bunchid = 0, + .timestamp = config.exptime * f, + .debug = config.debug, + .generator_slot = (bit_depth == 32 ? 2 : 1) * (frame_cell * config.modules + m) + }); + } + } + } + pulse_id += 1.0; + frame_cell = (frame_cell == config.images_in_memory) ? 0 : frame_cell + 1; + } + return 0; +} + +void MockAcquisitionDevice::DataCollectionThread() { + cancel = false; + stop_frame_generator = false; + + for (auto &c: collected_modules) + c = ModuleCompletion{.frame_number = UINT64_MAX}; + for (int m = 0; m < MAX_MODULES_FPGA; m++) { + summation_mask[m].assign(MAX_FPGA_SUMMATION, false); + summation_count[m] = 0; + summation_frame_prefix[m] = 0; + } + frames_in_group = 0; + handle_to_complete = HANDLE_START; + + uint32_t data_collection_id = (cfg.mode >> 16) & UINT16_MAX; + + // host_writer.cpp: work requests left from the previous data collection are returned unused + uint32_t handle; + while (work_requests.Get(handle)) + completions.Put((DATA_COLLECTION_ID_PURGE << 16) | handle); + completions.Put((data_collection_id << 16) | HANDLE_START); + + // data_collection_fsm.cpp: cancel is checked between packets + while (!cancel) { + Packet packet; + bool received = false; + { + std::unique_lock ul(packets_mutex); + if (!packets.empty()) { + packet = packets.front(); + packets.pop_front(); + received = true; + } + } + if (!received) + std::this_thread::sleep_for(std::chrono::microseconds(10)); + else if (!ReceivePacket(packet)) + break; + } + + EndDataCollection(); + + // Packets after the end of the data collection are dropped + stop_frame_generator = true; + if (frame_generator_future.valid()) + frame_generator_future.get(); + { + std::unique_lock ul(packets_mutex); + packets.clear(); + } + idle = true; +} + +bool MockAcquisitionDevice::ReceivePacket(const Packet &packet) { + // udp.cpp and sls_detector.cpp + udp_packets++; + if (packet.bunchid_packet) { + current_pulse_id = packet.bunchid; + return true; + } + if (packet.eth_error || packet.len_error) + return true; + if ((((packet.udp_port >> 8) & 0xF) != (cfg.data_stream & 0xF)) || (packet.frame_number == 0)) + return true; + sls_packets++; + + // data_collection_fsm.cpp: a frame far enough after the last one ends the data collection + uint64_t frame_number = packet.frame_number - 1; + if (frame_number >= static_cast(cfg.nframes) - 1 + DELAY_FRAMES_STOP_AND_QUIT) + return false; + + // stream512to768.cpp: location of the packet in the module, in units of 32 beats = 1024 pixels + uint32_t module = (packet.udp_port >> 1) & 0x1F; + uint32_t row = packet.udp_port & 1; + uint32_t column = packet.column & 1; + uint32_t eth_packet = packet.packet_number & 0x1FF; + uint32_t packet_parts; + + if (packet.detector_type == SLS_DETECTOR_TYPE_JUNGFRAU) { + packet_parts = 4; + eth_packet = (eth_packet % 128) | (row * 64); + } else if (cfg.mode & MODE_EIGER_8BIT) { + packet_parts = 4; + eth_packet = (row ? (64 + (eth_packet % 32) * 2) : (62 - (eth_packet % 32) * 2)) | column; + } else if (cfg.mode & MODE_EIGER_32BIT) { + packet_parts = 1; + uint32_t pos_of_4_line = (eth_packet % 128) / 2; + uint32_t pos_within_2_lines = eth_packet % 2; + eth_packet = (row ? (256 + pos_of_4_line * 4) : (252 - pos_of_4_line * 4)) | (2 * column) | pos_within_2_lines; + } else { + packet_parts = 2; + eth_packet = (row ? (128 + (eth_packet % 64) * 2) : (126 - (eth_packet % 64) * 2)) | column; + } + + // save_to_hbm.cpp: a module is complete only when the next frame of the same parity arrives + // for that module, or at the end of the data collection + ModuleCompletion &c = collected_modules[module * 2 + frame_number % 2]; + if (c.frame_number != frame_number) { + if (!MaskEmpty(c.packet_mask)) + SendCompletion(c); + c = ModuleCompletion{ + .frame_number = frame_number, + .exptime = packet.exptime, + .timestamp = packet.timestamp, + .bunchid = packet.bunchid, + .debug = packet.debug, + .module = module, + .detector_type = packet.detector_type, + .generator_slot = packet.generator_slot + }; + } + for (uint32_t i = eth_packet * packet_parts; i < (eth_packet + 1) * packet_parts; i++) + c.packet_mask[i / 64] |= (1LU << (i % 64)); + c.packet_count += packet_parts; + return true; +} + +void MockAcquisitionDevice::SendCompletion(const ModuleCompletion &c) { + // frame_summation_reorder_compl.cpp: with summation, frames are grouped per module and only complete + // groups are written + uint32_t sum = cfg.nsummation & UINT8_MAX; + if (sum == 0) { + LoadModule(c); + return; + } + + uint64_t frame_prefix = c.frame_number / (sum + 1); + uint32_t frame_loc = c.frame_number % (sum + 1); + uint32_t m = c.module; + + if (frame_prefix > summation_frame_prefix[m]) { + SendSummationGroup(m); + summation_mask[m].assign(MAX_FPGA_SUMMATION, false); + summation_completions[m * MAX_FPGA_SUMMATION + frame_loc] = c; + summation_mask[m][frame_loc] = true; + summation_count[m] = 1; + summation_frame_prefix[m] = frame_prefix; + } else if (frame_prefix == summation_frame_prefix[m]) { + summation_completions[m * MAX_FPGA_SUMMATION + frame_loc] = c; + summation_mask[m][frame_loc] = true; + summation_count[m] += 1; + } else { + ModuleCompletion late = c; + late.ignore = true; + LoadModule(late); + } +} + +void MockAcquisitionDevice::SendSummationGroup(uint32_t m) { + uint32_t sum = cfg.nsummation & UINT8_MAX; + for (int i = 0; i <= sum; i++) { + if (summation_mask[m][i]) { + ModuleCompletion c = summation_completions[m * MAX_FPGA_SUMMATION + i]; + if (summation_count[m] != sum + 1) + c.ignore = true; + LoadModule(c); + } + } +} + +void MockAcquisitionDevice::LoadModule(const ModuleCompletion &c) { + // load_from_hbm.cpp: an incomplete summation group is dropped + if (c.ignore) + return; + + // frame_summation.cpp: one module per group, with the header of the first frame and the packets + // present in all frames + uint32_t sum = cfg.nsummation & UINT8_MAX; + if (sum == 0) { + WriteToHost(c); + return; + } + if (frames_in_group == 0) { + group_completion = c; + group_completion.frame_number = c.frame_number / (sum + 1); + } else { + for (int i = 0; i < 8; i++) + group_completion.packet_mask[i] &= c.packet_mask[i]; + group_completion.packet_count += c.packet_count; + } + frames_in_group++; + if (frames_in_group == sum + 1) { + frames_in_group = 0; + WriteToHost(group_completion); + } +} + +void MockAcquisitionDevice::WriteToHost(const ModuleCompletion &c) { + // host_writer.cpp: waits for a host buffer; only when the data collection is cancelled the module + // is dropped instead + uint32_t handle; + while (!work_requests.Get(handle)) { + if (cancel) + return; + std::this_thread::sleep_for(std::chrono::microseconds(10)); + } + if (handle >= buffer_device.size()) + return; + DeviceOutput *output = buffer_device[handle]; + + const DeviceOutput *ready_output = nullptr; + if (!outputs.empty()) { + auto &image = outputs[c.frame_number % outputs.size()]; + if (c.module < image.size()) + ready_output = image[c.module].get(); + } + if (ready_output != nullptr) + memcpy(output, ready_output, sizeof(DeviceOutput)); + else { + memset(output, 0, sizeof(DeviceOutput)); + if ((c.generator_slot != NoGeneratorSlot) && !generator_frames[c.generator_slot].empty()) + memcpy(output->pixels, generator_frames[c.generator_slot].data(), RAW_MODULE_SIZE * sizeof(uint16_t)); + } + + ModuleStatistics &stats = output->module_statistics; + stats.frame_number = c.frame_number; + stats.timestamp = c.timestamp; + memcpy(&stats.pulse_id, &c.bunchid, sizeof(uint64_t)); + stats.detector_type = c.detector_type; + stats.exptime = c.exptime; + stats.debug = c.debug; + stats.pedestal = 0; + stats.packet_count = c.packet_count; + stats.module_number = c.module; + stats.load_calibration_destination = 0; + memcpy(stats.packet_mask, c.packet_mask, sizeof(stats.packet_mask)); + + // host_writer.cpp: a module is announced to the host only when the next one is written, or at the end + uint32_t data_collection_id = (cfg.mode >> 16) & UINT16_MAX; + if (handle_to_complete != HANDLE_START) + completions.Put((data_collection_id << 16) | handle_to_complete); + handle_to_complete = handle; +} + +void MockAcquisitionDevice::EndDataCollection() { + // save_to_hbm.cpp: modules still being collected, for each module the older frame first + for (int m = 0; m < MAX_MODULES_FPGA; m++) { + const ModuleCompletion &c0 = collected_modules[2 * m]; + const ModuleCompletion &c1 = collected_modules[2 * m + 1]; + const ModuleCompletion &first = (c0.frame_number < c1.frame_number) ? c0 : c1; + const ModuleCompletion &second = (c0.frame_number < c1.frame_number) ? c1 : c0; + if (!MaskEmpty(first.packet_mask)) + SendCompletion(first); + if (!MaskEmpty(second.packet_mask)) + SendCompletion(second); + } + + // frame_summation_reorder_compl.cpp + if ((cfg.nsummation & UINT8_MAX) > 0) { + for (int m = 0; m < MAX_MODULES_FPGA; m++) + SendSummationGroup(m); + } + + // host_writer.cpp + uint32_t data_collection_id = (cfg.mode >> 16) & UINT16_MAX; + if (handle_to_complete != HANDLE_START) + completions.Put((data_collection_id << 16) | handle_to_complete); + completions.Put((data_collection_id << 16) | HANDLE_END); +} + +DataCollectionStatus MockAcquisitionDevice::GetDataCollectionStatus() const { + DataCollectionStatus status{}; + + uint64_t pulse_id_u64 = current_pulse_id; + double pulse_id; + memcpy(&pulse_id, &pulse_id_u64, sizeof(double)); + + status.ctrl_reg = idle ? (1 << 4) : 0; + status.max_modules = MAX_MODULES_FPGA; + status.hbm_size_bytes = CardMemoryBytes; + status.run_counter = run_counter; + status.current_pulseid = pulse_id; + status.packets_udp = udp_packets; + status.packets_sls = sls_packets; + return status; +} + +DeviceStatus MockAcquisitionDevice::GetDeviceStatus() const { + DeviceStatus status{}; + + strncpy(status.serial_number, "MOCK1", sizeof(status.serial_number)); + strncpy(status.device_number, "Mock device", sizeof(status.device_number)); + status.fpga_default_mac_addr = mac_addr; + status.eth_link_count = 1; + status.eth_link_status = 1; + status.packets_sls = sls_packets; + status.packets_udp = udp_packets; + return status; +} diff --git a/acquisition_device/MockAcquisitionDevice.h b/acquisition_device/MockAcquisitionDevice.h new file mode 100644 index 000000000..a34d50f8e --- /dev/null +++ b/acquisition_device/MockAcquisitionDevice.h @@ -0,0 +1,143 @@ +// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute +// SPDX-License-Identifier: GPL-3.0-only + +#pragma once + +#include +#include +#include + +#include "FPGAAcquisitionDevice.h" + +// Behavioural mock of the FPGA card, for tests and for the broker without hardware. The card is a black +// box: the mock computes nothing (no conversion, summation, spot finding, integration or ROI), it +// reproduces what the host sees of the card - which module is written to which host buffer and when, the +// order of completions, packet counts and masks, frames that are missing or late, summation groups, +// counters, the end of the data collection, cancel and the internal frame generator with its refusals. +// +// The packets are given with CreateJFPacket() etc., or come from the internal frame generator; their +// pixel content is ignored. What the card writes for a module - pixels, spot finder, integration, ADU +// histogram, ROI and pixel statistics - is given ready-made with SetOutput(): the module written for +// frame (after summation) F and module M is the output set for image F % (number of images set) and M. +// Without one, the pixels are the frame the internal generator sent for it (as loaded with +// SetInternalGeneratorFrame(), 16-bit, not reordered), or zeros for a packet made by a test, and every +// result is zero. The header part of ModuleStatistics (frame number, timestamp, pulse ID, exposure time, +// debug, detector type, module number, packet count and mask) is filled by the mock from the packets. +// +// Any number of these devices can run at the same time. +class MockAcquisitionDevice : public FPGAAcquisitionDevice { + struct alignas(4096) FrameBuffer { uint8_t data[FPGA_BUFFER_LOCATION_SIZE]; }; + std::vector> buffers; + + // Header of one UDP packet, as the network cores of the card see it + struct Packet { + uint64_t frame_number; // as in the detector header, i.e. counted from one + uint32_t packet_number; + uint16_t udp_port; + uint16_t column; + uint8_t detector_type; + uint32_t exptime; + uint64_t bunchid; + uint64_t timestamp; + uint32_t debug; + bool eth_error; + bool len_error; + bool bunchid_packet; + uint32_t generator_slot; // frame of the internal generator the packet carries, NoGeneratorSlot if none + }; + static constexpr uint32_t NoGeneratorSlot = UINT32_MAX; + + // axis_completion in fpga/hls/hls_jfjoch.h + struct ModuleCompletion { + uint64_t packet_mask[8]; + uint64_t frame_number; + uint64_t exptime; + uint64_t timestamp; + uint64_t bunchid; + uint32_t debug; + uint16_t packet_count; + uint32_t module; + uint32_t detector_type; + bool ignore; + uint32_t generator_slot; + }; + + std::vector>> outputs; + // The frames of the internal generator, by slot (module frame of the card memory) + std::vector> generator_frames; + + std::mutex packets_mutex; + std::deque packets; + + ThreadSafeFIFO work_requests; + ThreadSafeFIFO completions; + + DataCollectionConfig cfg{}; + + // save_to_hbm.cpp: module being collected, for each module and frame parity + std::vector collected_modules; + + // frame_summation_reorder_compl.cpp and frame_summation.cpp + std::vector summation_completions; + std::vector> summation_mask; + std::vector summation_count; + std::vector summation_frame_prefix; + uint32_t frames_in_group = 0; + ModuleCompletion group_completion{}; + + // host_writer.cpp + uint32_t handle_to_complete = HANDLE_START; + + std::thread action_thread; + std::future frame_generator_future; + std::atomic cancel = false; + std::atomic stop_frame_generator = false; + std::atomic frame_generator_result = 0; + std::atomic idle = true; + + uint32_t run_counter = 0; + std::atomic current_pulse_id = UINT64_MAX; + std::atomic udp_packets = 0; + std::atomic sls_packets = 0; + + void HW_ReadActionRegister(DataCollectionConfig *job) override; + void HW_WriteActionRegister(const DataCollectionConfig *job) override; + void FPGA_StartAction(const DiffractionExperiment &experiment) override; + void FPGA_EndAction() override; + bool HW_IsIdle() const override; + bool HW_ReadMailbox(uint32_t *values) override; + bool HW_SendWorkRequest(uint32_t handle) override; + void HW_LoadCalibration(const LoadCalibrationConfig &config) override; + void HW_SetSpotFinderParameters(const SpotFinderParameters ¶ms) override; + void HW_RunInternalGenerator(const FrameGeneratorConfig &config) override; + + void AddPacket(const Packet &packet); + // frame_generator.cpp: its return code, 0 when it ran + int FrameGenerator(FrameGeneratorConfig config); + + void DataCollectionThread(); + bool ReceivePacket(const Packet &packet); + void SendCompletion(const ModuleCompletion &c); + void SendSummationGroup(uint32_t module); + void LoadModule(const ModuleCompletion &c); + void WriteToHost(const ModuleCompletion &c); + void EndDataCollection(); +public: + MockAcquisitionDevice(uint16_t data_stream, size_t in_frame_buffer_size_modules); + ~MockAcquisitionDevice() override; + + void SetOutput(uint64_t image, uint32_t module_number, const DeviceOutput &output); + + void CreateJFPacket(const DiffractionExperiment& experiment, uint64_t frame_number, uint32_t eth_packet, + uint32_t module_number, const uint16_t *data, int8_t adjust_axis = 0, uint8_t user = 0); + void CreateJFPackets(const DiffractionExperiment& experiment, uint64_t frame_number_0, uint64_t frames, + uint32_t module_number, const uint16_t *data); + void CreateEIGERPacket(const DiffractionExperiment& experiment, uint64_t frame_number, uint32_t eth_packet, + uint32_t module_number, uint32_t col, uint32_t row, + const uint16_t *data); + void CreateXfelBunchIDPacket(double bunchid, uint32_t event_code); + void CreateFinalPacket(const DiffractionExperiment& experiment); + DataCollectionStatus GetDataCollectionStatus() const override; + void Cancel() override; + DeviceStatus GetDeviceStatus() const override; +}; From fc6e3512a4b529174cc41073fda2124208a70493 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Tue, 17 Nov 2026 14:07:17 +0100 Subject: [PATCH 2/6] MockAcquisitionDeviceTest: the mock against the HLS simulation on the host-visible interface The same packets, or the same internal-generator run, on HLSSimulatedDevice and on the mock given what the HLS wrote; the host must see the same on both: bytes received, packet counters, which buffer each module went to (the order of completions), delays, complete/incomplete modules, the module headers, and for the receiver its images, counters and plots. One case per mode: raw and converted JUNGFRAU in every pixel format, summation on the card and on the host, storage cells, pedestal modes, EIGER 8/16/32 bit, two data streams, cancel, out-of-order and erroneous packets. Run once before the HLS simulation is removed: 9 cases, 633 assertions, all pass. From 497073dc9 and b00e2fbe1 (rc186). Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01UqXYdHHZ4cogS1mrGUtfN7 --- tests/CMakeLists.txt | 1 + tests/MockAcquisitionDeviceTest.cpp | 449 ++++++++++++++++++++++++++++ 2 files changed, 450 insertions(+) create mode 100644 tests/MockAcquisitionDeviceTest.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3674d8aa8..26ae59b55 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -70,6 +70,7 @@ ADD_EXECUTABLE(jfjoch_test ../common/DiffractionExperiment.h CheckImageOutput.h FPGAIntegrationTest.cpp + MockAcquisitionDeviceTest.cpp StrongPixelSetTest.cpp AdaptiveThresholdTest.cpp AdaptiveSpotFinderCPUTest.cpp diff --git a/tests/MockAcquisitionDeviceTest.cpp b/tests/MockAcquisitionDeviceTest.cpp new file mode 100644 index 000000000..0c4148ebe --- /dev/null +++ b/tests/MockAcquisitionDeviceTest.cpp @@ -0,0 +1,449 @@ +// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute +// SPDX-License-Identifier: GPL-3.0-only + +// MockAcquisitionDevice against HLSSimulatedDevice: the same scenario runs on the HLS simulation, what the card +// wrote is recorded and given to the mock, and the host must see the same on both - the buffers, which module +// went to which buffer (i.e. the order of completions), counters and, for the receiver, its results. One case +// for each kind of output and mode the tests on the mock give it (tests/MockCardOutput.h); [HLSvsMock] selects them. + +#include +#include +#include + +#include "../acquisition_device/HLSSimulatedDevice.h" +#include "../acquisition_device/MockAcquisitionDevice.h" +#include "../common/AzimuthalIntegrationMapping.h" +#include "../image_pusher/TestImagePusher.h" +#include "../receiver/JFJochReceiverService.h" +#include "../receiver/JFJochReceiverTest.h" + +namespace { + // A device that can give away everything it wrote to the host, by frame and module - as long as no buffer was + // used twice + template + class Recording : public Device { + public: + using Device::Device; + + std::map, const DeviceOutput *> Outputs() const { + std::map, const DeviceOutput *> ret; + for (size_t h = 0; h < this->buffer_device.size(); h++) { + const DeviceOutput *output = this->GetDeviceOutput(h); + if (output->module_statistics.packet_count > 0) + ret[{output->module_statistics.frame_number, output->module_statistics.module_number}] = output; + } + return ret; + } + + void CopyOutputs(MockAcquisitionDevice &mock) const { + for (const auto &[key, output]: Outputs()) + mock.SetOutput(key.first, key.second, *output); + } + }; + + using RecordingHLSDevice = Recording; + + void CompareDevices(const AcquisitionDevice &hls, const AcquisitionDevice &mock, const DiffractionExperiment &x, + uint16_t data_stream) { + CHECK(hls.GetBytesReceived() == mock.GetBytesReceived()); + CHECK(hls.Counters().GetSlowestFrameNumber() == mock.Counters().GetSlowestFrameNumber()); + CHECK(hls.Counters().GetFastestFrameNumber() == mock.Counters().GetFastestFrameNumber()); + + auto stats_hls = hls.GetStatistics(); + auto stats_mock = mock.GetStatistics(); + CHECK(stats_hls.good_packets == stats_mock.good_packets); + CHECK(stats_hls.packets_expected == stats_mock.packets_expected); + CHECK(stats_hls.packets_received_per_module == stats_mock.packets_received_per_module); + + int64_t frames = x.GetFrameNum() / x.GetFPGASummation(); + for (int64_t f = 0; f < frames; f++) { + for (int m = 0; m < x.GetModulesNum(data_stream); m++) { + INFO("frame " << f << " module " << m); + auto handle = hls.Counters().GetBufferHandle(f, m); + // The same buffer means the same order of completions + CHECK(handle == mock.Counters().GetBufferHandle(f, m)); + CHECK(hls.Counters().CalculateDelay(f, m) == mock.Counters().CalculateDelay(f, m)); + CHECK(hls.Counters().IsFullModuleCollected(f, m) == mock.Counters().IsFullModuleCollected(f, m)); + if (handle != AcquisitionCounters::HandleNotFound) + CHECK(memcmp(hls.GetDeviceOutput(f, m), mock.GetDeviceOutput(f, m), sizeof(DeviceOutput)) == 0); + } + } + } + + // Runs the same packets on both devices, the mock with what the HLS simulation wrote + template + void CompareOnPackets(const DiffractionExperiment &x, Packets packets, uint16_t data_stream = 0) { + RecordingHLSDevice hls(data_stream, 64); + packets(hls); + REQUIRE_NOTHROW(hls.StartAction(x)); + REQUIRE_NOTHROW(hls.WaitForActionComplete()); + REQUIRE(hls.GetBytesReceived() > 0); + + MockAcquisitionDevice mock(data_stream, 64); + hls.CopyOutputs(mock); + packets(mock); + REQUIRE_NOTHROW(mock.StartAction(x)); + REQUIRE_NOTHROW(mock.WaitForActionComplete()); + + CompareDevices(hls, mock, x, data_stream); + } + + // Raw JUNGFRAU frame of a module: the G0 pedestal of GeneratePedestalCalibration() with a few photons, spots, + // pixels in G1 and G2, one saturated and one bad pixel + std::vector RawFrame(int module) { + std::vector ret(RAW_MODULE_SIZE); + std::mt19937 g(1900 + module); + std::uniform_int_distribution background(0, 300); + for (int i = 0; i < RAW_MODULE_SIZE; i++) + ret[i] = 3000 + 10 * module + i % 50 + background(g); + std::uniform_int_distribution where(20 * RAW_MODULE_COLS, (RAW_MODULE_LINES - 20) * RAW_MODULE_COLS); + for (int s = 0; s < 40; s++) { + const int i0 = where(g); + for (int y = 0; y < 3; y++) + for (int i = 0; i < 3; i++) + ret[i0 + y * RAW_MODULE_COLS + i] = 12000; + } + for (int i = 0; i < RAW_MODULE_SIZE; i += 1001) + ret[i] = 0x4000 | (14000 + i % 50); + for (int i = 0; i < RAW_MODULE_SIZE; i += 1777) + ret[i] = 0xC000 | 13000; + ret[50 * RAW_MODULE_COLS + 500] = 0xC000; + ret[60 * RAW_MODULE_COLS + 600] = 0xFFFF; + return ret; + } + + // JUNGFRAU, internal packet generator: the card loaded as the receiver loads it - calibration, pixel mask, ROI + // map, integration and resolution maps, spot finder - so all its outputs are there + void CompareOnGenerator(const DiffractionExperiment &x, uint16_t data_stream = 0) { + const JFCalibration calib = GeneratePedestalCalibration(x); + const PixelMask mask(x); + const AzimuthalIntegrationMapping azint(x, mask); + SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); + settings.signal_to_noise_threshold = 3; + settings.photon_count_threshold = 2; + settings.min_pix_per_spot = 1; + + CompareOnPackets(x, [&](AcquisitionDevice &d) { + d.InitializeCalibration(x, calib); + d.InitializePixelMask(x, mask); + d.InitializeROIMap(x, x.ExportROIMap()); + d.InitializeDataProcessing(x, azint); + d.SetSpotFinderParameters(settings); + for (int m = 0; m < x.GetModulesNum(data_stream); m++) + d.SetInternalGeneratorFrame(RawFrame(x.GetFirstModuleOfDataStream(data_stream) + m).data(), m); + }, data_stream); + } + + DiffractionExperiment GeneratorExperiment(const DetectorSetup &detector) { + DiffractionExperiment x(detector); + x.PedestalG0Frames(0).NumTriggers(1).ImagesPerTrigger(2).UseInternalPacketGenerator(true) + .IncidentEnergy_keV(12.4).DetectorDistance_mm(75).BeamX_pxl(500).BeamY_pxl(250); + x.ROI().SetROI(ROIDefinition{.boxes = {ROIBox("box", 380, 450, 200, 240)}, + .circles = {ROICircle("circle", 600, 300, 20)}, .azimuthal = {}}); + return x; + } +} + +TEST_CASE("MockAcquisitionDevice_SameAsHLS_Packets", "[MockAcquisitionDevice][HLSvsMock]") { + const uint16_t nmodules = 4; + DiffractionExperiment x(DetJF(nmodules)); + x.Raw(); + x.PedestalG0Frames(0).ImagesPerTrigger(3).NumTriggers(1); + + std::vector module(RAW_MODULE_SIZE); + for (int i = 0; i < RAW_MODULE_SIZE; i++) + module[i] = (7 * i + 534 + 5 * i * i) % UINT16_MAX; + + CompareOnPackets(x, [&](auto &d) { + d.CreateXfelBunchIDPacket(1234.0, 0); + // Frame 1: module 0 incomplete, both halves; module 1 missing; modules 2 and 3 complete + d.CreateJFPacket(x, 1, 0, 0, module.data()); + d.CreateJFPacket(x, 1, 64, 0, module.data()); + d.CreateJFPackets(x, 1, 1, 2, module.data()); + d.CreateJFPackets(x, 1, 1, 3, module.data()); + // Frame 3 before frame 2 for module 0; one packet with a wrong length, one with an Ethernet error + d.CreateJFPacket(x, 3, 5, 0, module.data()); + d.CreateJFPacket(x, 2, 6, 0, module.data()); + d.CreateJFPacket(x, 2, 7, 0, module.data(), -1); + d.CreateJFPacket(x, 2, 8, 0, module.data(), 0, 1); + d.CreateJFPackets(x, 2, 1, 1, module.data()); + d.CreateJFPackets(x, 3, 1, 3, module.data()); + // A frame after the last one + d.CreateJFPacket(x, 4, 1, 2, module.data()); + d.CreateFinalPacket(x); + }); +} + +TEST_CASE("MockAcquisitionDevice_SameAsHLS_Summation", "[MockAcquisitionDevice][HLSvsMock]") { + const uint16_t nmodules = 2; + DiffractionExperiment x(DetJF(nmodules)); + x.PedestalG0Frames(0).ImagesPerTrigger(3).NumTriggers(1).Summation(2) + .JungfrauConvPhotonCnt(false).PixelSigned(true).BitDepthImage(32); + REQUIRE(x.GetFPGASummation() == 2); + + std::vector module(RAW_MODULE_SIZE); + std::mt19937 g(1387); + std::uniform_int_distribution dist(0, UINT16_MAX); + for (auto &i: module) + i = dist(g); + + CompareOnPackets(x, [&](auto &d) { + for (int f = 1; f <= 6; f++) { + d.CreateJFPackets(x, f, 1, 0, module.data()); + // Second frame of the second image is missing for module 1, so the whole image is dropped + if (f != 4) + d.CreateJFPackets(x, f, 1, 1, module.data()); + } + d.CreateFinalPacket(x); + }); +} + +TEST_CASE("MockAcquisitionDevice_SameAsHLS_EIGER", "[MockAcquisitionDevice][HLSvsMock]") { + DiffractionExperiment x(DetEIGER(1)); + x.Raw(); + x.PedestalG0Frames(0).ImagesPerTrigger(1).NumTriggers(1); + + std::vector module(RAW_MODULE_SIZE); + for (int i = 0; i < RAW_MODULE_SIZE; i++) + module[i] = i % (UINT16_MAX - 11); + + CompareOnPackets(x, [&](auto &d) { + for (int row = 0; row < 2; row++) { + for (int col = 0; col < 2; col++) { + for (int i = 0; i < 64; i++) { + if ((i != 10) || (col != row)) + d.CreateEIGERPacket(x, 1, i, 0, col, row, module.data() + 2048 * (128 * row + col + 2 * i)); + } + } + } + d.CreateFinalPacket(x); + }); +} + +TEST_CASE("MockAcquisitionDevice_SameAsHLS_InternalGenerator", "[MockAcquisitionDevice][HLSvsMock]") { + const uint16_t nmodules = 2; + DiffractionExperiment x(DetJF(nmodules)); + x.Raw(); + x.UseInternalPacketGenerator(true).ImagesPerTrigger(3).PedestalG0Frames(0); + + CompareOnPackets(x, [&](auto &d) {}); +} + +// JUNGFRAU modes, with every output of the card: pixels, spot finder, integration, ADU histogram, ROI, pixel counts +TEST_CASE("MockAcquisitionDevice_SameAsHLS_Modes", "[MockAcquisitionDevice][HLSvsMock]") { + DiffractionExperiment x = GeneratorExperiment(DetJF(1)); + x.Conversion(); + + SECTION("conversion, int16") {} + SECTION("conversion, fixed G1") { + x.FixedGainG1(true); + } + SECTION("conversion, uint16") { + x.BitDepthImage(16).PixelSigned(false); + } + SECTION("conversion, uint8") { + x.BitDepthImage(8).PixelSigned(false); + } + SECTION("conversion, int32") { + x.BitDepthImage(32).PixelSigned(true); + } + SECTION("conversion, Poisson") { + x.LossyCompressionPoisson(1); + } + SECTION("conversion, threshold") { + x.PixelValueLowThreshold(50); + } + SECTION("conversion, applied pixel mask") { + x.ApplyPixelMask(true).MaskChipEdges(true); + } + SECTION("conversion, storage cells") { + x.StorageCells(4); + REQUIRE(x.GetStorageCellNumber() == 4); + } + SECTION("conversion, card summation") { + x.Summation(2); + REQUIRE(x.GetFPGASummation() == 2); + } + SECTION("conversion, host summation") { + x.Summation(3).CPUSummation(true); + REQUIRE(x.GetFPGASummation() == 1); + } + SECTION("raw") { + x.Raw(); + } + SECTION("pedestal G0") { + x.Mode(DetectorMode::PedestalG0).PedestalG0Frames(4); + } + SECTION("pedestal G1, fixed G1, storage cells") { + x.Mode(DetectorMode::PedestalG1).FixedGainG1(true).StorageCells(4).PedestalG1Frames(1); + REQUIRE(x.GetStorageCellNumber() == 4); + } + SECTION("pedestal G2, storage cells") { + x.Mode(DetectorMode::PedestalG2).StorageCells(16).PedestalG2Frames(2); + REQUIRE(x.GetStorageCellNumber() == 2); + } + + CompareOnGenerator(x); +} + +TEST_CASE("MockAcquisitionDevice_SameAsHLS_EIGER_BitDepth", "[MockAcquisitionDevice][HLSvsMock]") { + DiffractionExperiment x = GeneratorExperiment(DetEIGER(1)); + x.Raw(); + + SECTION("8 bit") { + x.EigerBitDepth(8); + } + SECTION("16 bit") {} + SECTION("32 bit") { + x.EigerBitDepth(32); + } + + std::vector frame(RAW_MODULE_SIZE * 2); + std::mt19937 g(1387); + std::uniform_int_distribution dist(0, UINT16_MAX); + for (auto &i: frame) + i = dist(g); + + CompareOnPackets(x, [&](AcquisitionDevice &d) { + d.SetInternalGeneratorFrame(frame.data(), 0); + }); +} + +// Each card ends its own data collection: the second of two data streams, then the first +TEST_CASE("MockAcquisitionDevice_SameAsHLS_TwoDataStreams", "[MockAcquisitionDevice][HLSvsMock]") { + DiffractionExperiment x = GeneratorExperiment(DetJF(2)); + x.Conversion(); + x.DataStreams(2); + REQUIRE(x.GetDataStreamsNum() == 2); + + CompareOnGenerator(x, 1); + CompareOnGenerator(x, 0); +} + +TEST_CASE("MockAcquisitionDevice_SameAsHLS_Cancel", "[MockAcquisitionDevice][HLSvsMock]") { + DiffractionExperiment x(DetJF(4)); + x.Raw(); + x.PedestalG0Frames(0).ImagesPerTrigger(5).NumTriggers(1); + + HLSSimulatedDevice hls(0, 64); + MockAcquisitionDevice mock(0, 64); + for (AcquisitionDevice *d: {static_cast(&hls), static_cast(&mock)}) { + REQUIRE_NOTHROW(d->StartAction(x)); + d->Cancel(); + REQUIRE_NOTHROW(d->WaitForActionComplete()); + CHECK(d->Counters().GetSlowestFrameNumber() == 0); + CHECK(d->GetBytesReceived() == 0); + } +} + +// The receiver on the HLS simulation, and on the mock with what the HLS simulation wrote during that run +TEST_CASE("MockAcquisitionDevice_SameAsHLS_Receiver", "[MockAcquisitionDevice][HLSvsMock]") { + const uint16_t nmodules = 2; + DiffractionExperiment x(DetJF(nmodules)); + x.Conversion(); + x.PedestalG0Frames(0).NumTriggers(1).UseInternalPacketGenerator(true).ImagesPerTrigger(4) + .IncidentEnergy_keV(12.4).DetectorDistance_mm(75).BeamX_pxl(500).BeamY_pxl(600) + .Compression(CompressionAlgorithm::BSHUF_LZ4); + x.ROI().SetROI(ROIDefinition{.boxes = {ROIBox("box", 380, 450, 280, 320)}, .circles = {}, .azimuthal = {}}); + + JFCalibration calib(x); + std::vector pedestal(RAW_MODULE_SIZE); + for (int m = 0; m < nmodules; m++) { + for (int i = 0; i < RAW_MODULE_SIZE; i++) + pedestal[i] = 3000 + 10 * m + i % 50; + calib.Pedestal(m, 0).LoadPedestal(pedestal); + } + + // A few photons of background, some spots, one saturated and one bad pixel + std::vector raw(nmodules * RAW_MODULE_SIZE); + std::mt19937 g(1900); + std::uniform_int_distribution background(0, 300); + std::uniform_int_distribution where(20 * RAW_MODULE_COLS, (RAW_MODULE_LINES - 20) * RAW_MODULE_COLS); + for (int i = 0; i < raw.size(); i++) + raw[i] = 3000 + 10 * (i / RAW_MODULE_SIZE) + i % 50 + background(g); + for (int s = 0; s < 80; s++) { + int i0 = where(g) + (s % nmodules) * RAW_MODULE_SIZE; + for (int y = 0; y < 3; y++) + for (int i = 0; i < 3; i++) + raw[i0 + y * RAW_MODULE_COLS + i] = 12000; + } + raw[50 * RAW_MODULE_COLS + 500] = 0xC000; + raw[60 * RAW_MODULE_COLS + 600] = 0xFFFF; + + SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); + settings.signal_to_noise_threshold = 3; + settings.photon_count_threshold = 2; + settings.min_pix_per_spot = 1; + settings.indexing = false; + + auto run = [&](AcquisitionDeviceGroup &devices, std::vector &image, std::vector> &plots) { + Logger logger("MockAcquisitionDevice_SameAsHLS_Receiver"); + TestImagePusher pusher(x.GetImageNum() - 1); + JFJochReceiverService service(devices, logger, pusher); + service.NumThreads(4); + service.SetSpotFindingSettings(settings); + service.LoadInternalGeneratorImage(x, raw, 0); + PixelMask mask(x); + service.Start(x, mask, &calib); + auto output = service.Stop(); + image = pusher.GetImage(); + for (auto type: {PlotType::SpotCount, PlotType::StrongPixels, PlotType::ErrorPixels, + PlotType::SaturatedPixels, PlotType::PixelSum, PlotType::MaxValue, + PlotType::BkgEstimate, PlotType::PacketsReceived}) { + std::vector v; + service.GetPlotRaw(v, type, ""); + plots.push_back(v); + } + std::vector azint; + service.GetPlotRaw(azint, PlotType::AzInt, ""); + plots.push_back(azint); + std::vector roi; + service.GetPlotRaw(roi, PlotType::ROISum, "box"); + plots.push_back(roi); + return output; + }; + + AcquisitionDeviceGroup hls_devices; + hls_devices.Add(std::make_unique(0, 64)); + std::vector hls_image; + std::vector> hls_plots; + auto hls_output = run(hls_devices, hls_image, hls_plots); + + const auto &hls = dynamic_cast(hls_devices[0]); + auto mock = std::make_unique>(0, 64); + hls.CopyOutputs(*mock); + AcquisitionDeviceGroup mock_devices; + mock_devices.Add(std::move(mock)); + std::vector mock_image; + std::vector> mock_plots; + auto mock_output = run(mock_devices, mock_image, mock_plots); + + REQUIRE(hls_output.status.images_sent == x.GetImageNum()); + CHECK(mock_output.status.images_sent == hls_output.status.images_sent); + CHECK(mock_output.status.max_image_number_sent == hls_output.status.max_image_number_sent); + CHECK(mock_output.status.compressed_size == hls_output.status.compressed_size); + CHECK(mock_output.efficiency == hls_output.efficiency); + CHECK(mock_output.received_packets == hls_output.received_packets); + CHECK(mock_output.expected_packets == hls_output.expected_packets); + CHECK(mock_image == hls_image); + + // The frames of the run; how many of those the generator sends after them to end the data collection are + // written before the receiver cancels the card depends on timing + const auto hls_outputs = hls.Outputs(); + const auto mock_outputs = dynamic_cast &>(mock_devices[0]).Outputs(); + for (uint64_t f = 0; f < x.GetFrameNum(); f++) { + for (uint32_t m = 0; m < nmodules; m++) { + INFO("frame " << f << " module " << m); + REQUIRE(hls_outputs.contains({f, m})); + REQUIRE(mock_outputs.contains({f, m})); + CHECK(memcmp(mock_outputs.at({f, m}), hls_outputs.at({f, m}), sizeof(DeviceOutput)) == 0); + } + } + + REQUIRE(mock_plots.size() == hls_plots.size()); + for (int i = 0; i < hls_plots.size(); i++) { + INFO("plot " << i); + REQUIRE(mock_plots[i].size() == hls_plots[i].size()); + CHECK(memcmp(mock_plots[i].data(), hls_plots[i].data(), hls_plots[i].size() * sizeof(float)) == 0); + } +} From cb95938bc700da5f754c8ac5865b6913b54f2197 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Tue, 17 Nov 2026 14:09:27 +0100 Subject: [PATCH 3/6] Tests: receiver, state machine and writer tests on MockAcquisitionDevice; FPGAIntegrationTest removed The host-side tests run on the mock instead of the HLS simulation: JFJochReceiverIntegrationTest, JFJochReceiverProcessingTest (the GPU-stills cases too, in raw mode the card writes the generator's frame), JFJochStateMachineTest, StreamWriterTest. tests/MockCardOutput.h gives the mock what the card would write for a test's image - its conversion by the host's floating-point reference, pixel statistics, ROI counts and the strong pixels of the CPU spot finder - so the checks of the receiver's images and plots stay as they were. Two cases are new: a receiver on two cards at once (not possible with the HLS simulation) and a cancelled pedestal run. FPGAIntegrationTest.cpp (60 cases) tested the HLS device itself - conversion, summation, spot finder, integration, ROI, ADU histogram, pixel counts, in every mode - and is removed, not ported. Its host-side checks (FPGAAcquisitionDevice and AcquisitionCounters: the internal generator, data streams, missing modules, delays, lost frames, the last frame, bunch ID, the generator's memory limit) are FPGAAcquisitionDeviceTest.cpp on the mock, plus a check that the generator's frames are what the mock writes. From ce8918062 and 710edd61e (rc186), resolved against rc188: JFJochReceiverTest() without quick_integrate, the FPGA receiver that indexes and does not integrate, StillsGPUAnalysisFPGA. JFJochReceiverFPGA_RugnuxOnStaysPerImage is not taken (rc188 keeps live stills on the FPGA receiver). Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01UqXYdHHZ4cogS1mrGUtfN7 --- receiver/JFJochReceiverTest.cpp | 2 +- receiver/JFJochReceiverTest.h | 3 + tests/CMakeLists.txt | 3 +- tests/FPGAAcquisitionDeviceTest.cpp | 216 ++ tests/FPGAIntegrationTest.cpp | 2846 ----------------------- tests/JFJochReceiverIntegrationTest.cpp | 304 +-- tests/JFJochReceiverProcessingTest.cpp | 414 ++-- tests/JFJochStateMachineTest.cpp | 12 +- tests/MockCardOutput.h | 220 ++ tests/StreamWriterTest.cpp | 9 +- 10 files changed, 788 insertions(+), 3241 deletions(-) create mode 100644 tests/FPGAAcquisitionDeviceTest.cpp delete mode 100644 tests/FPGAIntegrationTest.cpp create mode 100644 tests/MockCardOutput.h diff --git a/receiver/JFJochReceiverTest.cpp b/receiver/JFJochReceiverTest.cpp index 6227a38c6..afbe755ba 100644 --- a/receiver/JFJochReceiverTest.cpp +++ b/receiver/JFJochReceiverTest.cpp @@ -8,7 +8,7 @@ #define STORAGE_CELL_FOR_TEST 11 -static JFCalibration GeneratePedestalCalibration(const DiffractionExperiment &x) { +JFCalibration GeneratePedestalCalibration(const DiffractionExperiment &x) { JFCalibration ret(x); std::vector pedestal_g0( RAW_MODULE_SIZE), pedestal_g1(RAW_MODULE_SIZE), pedestal_g2(RAW_MODULE_SIZE); diff --git a/receiver/JFJochReceiverTest.h b/receiver/JFJochReceiverTest.h index ce3dd5554..3606de6df 100644 --- a/receiver/JFJochReceiverTest.h +++ b/receiver/JFJochReceiverTest.h @@ -5,6 +5,9 @@ #pragma once +// The calibration JFJochReceiverTest() converts with +JFCalibration GeneratePedestalCalibration(const DiffractionExperiment &x); + bool JFJochReceiverTest(JFJochReceiverOutput &output, Logger &logger, AcquisitionDeviceGroup &aq_devices, const DiffractionExperiment &x, diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 26ae59b55..55aa3f969 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -69,8 +69,9 @@ ADD_EXECUTABLE(jfjoch_test ../common/RawToConvertedGeometry.h ../common/DiffractionExperiment.h CheckImageOutput.h - FPGAIntegrationTest.cpp + MockCardOutput.h MockAcquisitionDeviceTest.cpp + FPGAAcquisitionDeviceTest.cpp StrongPixelSetTest.cpp AdaptiveThresholdTest.cpp AdaptiveSpotFinderCPUTest.cpp diff --git a/tests/FPGAAcquisitionDeviceTest.cpp b/tests/FPGAAcquisitionDeviceTest.cpp new file mode 100644 index 000000000..d3d8df1bb --- /dev/null +++ b/tests/FPGAAcquisitionDeviceTest.cpp @@ -0,0 +1,216 @@ +// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute +// SPDX-License-Identifier: GPL-3.0-only + +// The host side of the card - FPGAAcquisitionDevice and its AcquisitionCounters - on the mock of the card: what +// arrived, where it went, delays, the end of the data collection. The card itself is compared with the mock in +// MockAcquisitionDeviceTest.cpp. + +#include + +#include "../acquisition_device/MockAcquisitionDevice.h" + +TEST_CASE("FPGAAcquisitionDevice_InternalPacketGenerator", "[FPGAAcquisitionDevice]") { + const uint16_t nmodules = 4; + DiffractionExperiment x(DetJF(nmodules)); + x.Raw(); + x.UseInternalPacketGenerator(true).ImagesPerTrigger(4).PedestalG0Frames(0); + + MockAcquisitionDevice test(0, 64); + REQUIRE_NOTHROW(test.StartAction(x)); + REQUIRE_NOTHROW(test.WaitForActionComplete()); + + REQUIRE(test.GetBytesReceived() == 128 * nmodules * 4 * JUNGFRAU_PACKET_SIZE_BYTES); + for (int image = 0; image < 4; image++) { + for (int m = 0; m < nmodules; m++) { + const auto &stats = test.GetDeviceOutput(image, m)->module_statistics; + CHECK(stats.frame_number == image); + CHECK(stats.module_number == m); + CHECK(stats.timestamp == INT_PKT_GEN_EXPTTIME * image); + CHECK(stats.pulse_id == INT_PKT_GEN_BUNCHID + image); + CHECK(stats.exptime == INT_PKT_GEN_EXPTTIME); + CHECK(stats.detector_type == SLS_DETECTOR_TYPE_JUNGFRAU); + } + } + REQUIRE(test.GetExpectedDescriptorsPerModule() == DMA_DESCRIPTORS_PER_MODULE); +} + +// Of four data streams, the card of the last one generates only its own module +TEST_CASE("FPGAAcquisitionDevice_InternalPacketGenerator_DataStream", "[FPGAAcquisitionDevice]") { + const uint16_t nmodules = 4; + DiffractionExperiment x(DetJF(nmodules)); + x.DataStreams(4); + x.Raw(); + x.UseInternalPacketGenerator(true).ImagesPerTrigger(4).PedestalG0Frames(0); + + MockAcquisitionDevice test(3, 64); + REQUIRE_NOTHROW(test.StartAction(x)); + REQUIRE_NOTHROW(test.WaitForActionComplete()); + + REQUIRE(test.GetBytesReceived() == 128 * 4 * JUNGFRAU_PACKET_SIZE_BYTES); + for (int image = 0; image < 4; image++) + REQUIRE(test.Counters().IsFullModuleCollected(image, 0)); +} + +// Module 0 runs on, module 1 stops after 3 frames, modules 2 and 3 never come: the slowest frame moves on once the +// fastest module is far enough ahead +TEST_CASE("FPGAAcquisitionDevice_MissingModules", "[FPGAAcquisitionDevice]") { + const uint16_t nmodules = 4; + const uint64_t nframes = AcquisitionCounters::ThresholdFramesLost + 5; + + DiffractionExperiment x(DetJF(nmodules)); + x.Raw(); + x.PedestalG0Frames(0).ImagesPerTrigger(nframes).NumTriggers(1); + + MockAcquisitionDevice test(0, nframes + 2); + test.CreateJFPackets(x, 1, nframes, 0, nullptr); + test.CreateJFPackets(x, 1, 3, 1, nullptr); + test.CreateFinalPacket(x); + REQUIRE_NOTHROW(test.StartAction(x)); + REQUIRE_NOTHROW(test.WaitForActionComplete()); + + REQUIRE(test.Counters().GetSlowestFrameNumber() == 5 - 1); + REQUIRE(test.Counters().GetBufferHandle(0, 0) != AcquisitionCounters::HandleNotFound); + REQUIRE(test.Counters().GetBufferHandle(0, 1) == AcquisitionCounters::HandleNotFound); + REQUIRE(test.Counters().GetBufferHandle(0, 2) == AcquisitionCounters::HandleNotFound); + REQUIRE(test.GetBytesReceived() == nframes * 128 * JUNGFRAU_PACKET_SIZE_BYTES); +} + +TEST_CASE("FPGAAcquisitionDevice_Delay", "[FPGAAcquisitionDevice]") { + const uint16_t nmodules = 4; + DiffractionExperiment x(DetJF(nmodules)); + x.Raw(); + x.PedestalG0Frames(0).ImagesPerTrigger(3).NumTriggers(1); + + MockAcquisitionDevice test(0, 64); + for (int f = 1; f <= 3; f++) + for (int m = 0; m < nmodules; m++) + test.CreateJFPacket(x, f, 0, m, nullptr); + test.CreateFinalPacket(x); + + REQUIRE_NOTHROW(test.StartAction(x)); + REQUIRE_NOTHROW(test.WaitForActionComplete()); + + REQUIRE(test.Counters().CalculateDelay(0) == 2); + REQUIRE(test.Counters().CalculateDelay(0, 0) == 2); + REQUIRE(test.Counters().CalculateDelay(1) == 1); + REQUIRE(test.Counters().CalculateDelay(1, 0) == 1); + REQUIRE(test.Counters().CalculateDelay(2) == 0); + REQUIRE(test.Counters().CalculateDelay(2, 0) == 0); +} + +// A single packet of one module: that module of that frame is there, nothing else is +TEST_CASE("FPGAAcquisitionDevice_LostFrame", "[FPGAAcquisitionDevice]") { + const uint16_t nmodules = 4; + DiffractionExperiment x(DetJF(nmodules)); + x.Raw(); + x.PedestalG0Frames(0).ImagesPerTrigger(3).NumTriggers(1); + + MockAcquisitionDevice test(0, 64); + test.CreateJFPacket(x, 1, 0, 0, nullptr); + test.CreateFinalPacket(x); + + REQUIRE_NOTHROW(test.StartAction(x)); + REQUIRE_NOTHROW(test.WaitForActionComplete()); + + REQUIRE(test.GetBytesReceived() == JUNGFRAU_PACKET_SIZE_BYTES); + REQUIRE_NOTHROW(test.GetDeviceOutput(0, 0)); + REQUIRE(!test.Counters().IsFullModuleCollected(0, 0)); + REQUIRE_THROWS(test.GetDeviceOutput(0, 1)); + REQUIRE_THROWS(test.GetDeviceOutput(1, 0)); + REQUIRE_THROWS(test.GetDeviceOutput(2, 1)); +} + +TEST_CASE("FPGAAcquisitionDevice_OneFrame", "[FPGAAcquisitionDevice]") { + const uint16_t nmodules = 4; + DiffractionExperiment x(DetJF(nmodules)); + x.PedestalG0Frames(0).NumTriggers(1).ImagesPerTrigger(1); + + MockAcquisitionDevice test(0, 64); + test.CreateJFPackets(x, 1, 1, 0, nullptr); + test.CreateFinalPacket(x); + + REQUIRE_NOTHROW(test.StartAction(x)); + test.WaitForActionComplete(); + REQUIRE(test.Counters().IsAcquisitionFinished()); + + // address properly aligned + REQUIRE((uint64_t) test.GetDeviceOutput(0, 0)->pixels % 128 == 0); + + REQUIRE(test.Counters().GetSlowestFrameNumber() == 0); + REQUIRE(test.Counters().GetCurrFrameNumber(0) == 0); + REQUIRE(test.GetBytesReceived() == 128 * JUNGFRAU_PACKET_SIZE_BYTES); +} + +// A frame far past the last one ends the data collection +TEST_CASE("FPGAAcquisitionDevice_DetectLastFrame", "[FPGAAcquisitionDevice]") { + const uint16_t nmodules = 4; + DiffractionExperiment x(DetJF(nmodules)); + x.PedestalG0Frames(0).NumTriggers(2).ImagesPerTrigger(5); + + MockAcquisitionDevice test(0, 64); + REQUIRE_NOTHROW(test.StartAction(x)); + REQUIRE(!test.Counters().IsAcquisitionFinished()); + + test.CreateJFPacket(x, 15, 0, 0, nullptr); + test.WaitForActionComplete(); + REQUIRE(test.Counters().IsAcquisitionFinished()); +} + +TEST_CASE("FPGAAcquisitionDevice_BunchID", "[FPGAAcquisitionDevice]") { + const uint64_t bunchid = 0xAAAAAAAA87345600; + DiffractionExperiment x(DetJF(4)); + x.Raw(); + x.PedestalG0Frames(0).ImagesPerTrigger(3).NumTriggers(1); + + MockAcquisitionDevice test(0, 64); + test.CreateXfelBunchIDPacket(bunchid, 0); + test.CreateFinalPacket(x); + + REQUIRE_NOTHROW(test.StartAction(x)); + REQUIRE_NOTHROW(test.WaitForActionComplete()); + + REQUIRE(test.GetBytesReceived() == 0); + REQUIRE(test.GetDataCollectionStatus().current_pulseid == bunchid); +} + +TEST_CASE("FPGAAcquisitionDevice_InternalGeneratorMemory", "[FPGAAcquisitionDevice]") { + // 64 module frames in the card memory; the generator takes 63 at most + DiffractionExperiment x(DetJF(2)); + x.UseInternalPacketGenerator(true).ImagesPerTrigger(1).NumTriggers(1); + MockAcquisitionDevice test(0, 64); + + x.InternalPacketGeneratorImages(31); + REQUIRE_NOTHROW(test.PrepareAction(x)); + + x.InternalPacketGeneratorImages(32); + REQUIRE_THROWS(test.PrepareAction(x)); + + std::vector frame(RAW_MODULE_SIZE); + REQUIRE_NOTHROW(test.SetInternalGeneratorFrame(frame.data(), 62)); + REQUIRE_THROWS(test.SetInternalGeneratorFrame(frame.data(), 63)); +} + +// The generator's frames are what the card writes when no output was set for a module +TEST_CASE("FPGAAcquisitionDevice_InternalGeneratorFrames", "[FPGAAcquisitionDevice]") { + const uint16_t nmodules = 2; + DiffractionExperiment x(DetJF(nmodules)); + x.Raw(); + x.UseInternalPacketGenerator(true).InternalPacketGeneratorImages(2).ImagesPerTrigger(5).PedestalG0Frames(0); + + MockAcquisitionDevice test(0, 64); + std::vector frame(RAW_MODULE_SIZE); + for (int slot = 0; slot < 4; slot++) { + std::fill(frame.begin(), frame.end(), 100 + slot); + test.SetInternalGeneratorFrame(frame.data(), slot); + } + REQUIRE_NOTHROW(test.StartAction(x)); + REQUIRE_NOTHROW(test.WaitForActionComplete()); + + for (int image = 0; image < 5; image++) { + for (int m = 0; m < nmodules; m++) { + const auto pixels = reinterpret_cast(test.GetDeviceOutput(image, m)->pixels); + CHECK(pixels[0] == 100 + (image % 2) * nmodules + m); + CHECK(pixels[RAW_MODULE_SIZE - 1] == 100 + (image % 2) * nmodules + m); + } + } +} diff --git a/tests/FPGAIntegrationTest.cpp b/tests/FPGAIntegrationTest.cpp deleted file mode 100644 index 10f159c59..000000000 --- a/tests/FPGAIntegrationTest.cpp +++ /dev/null @@ -1,2846 +0,0 @@ -// SPDX-FileCopyrightText: 2024 Filip Leonarski, Paul Scherrer Institute -// SPDX-License-Identifier: GPL-3.0-only - -#include -#include - -#include "../jungfrau/JFPedestalCalc.h" -#include "../acquisition_device/HLSSimulatedDevice.h" -#include "../image_analysis/image_preprocessing/ImagePreprocessorCPU.h" -#ifdef JFJOCH_USE_CUDA -#include "../image_analysis/image_preprocessing/ImagePreprocessorBufferGPU.h" -#include "../image_analysis/image_preprocessing/ImagePreprocessorGPU.h" -#endif -#include "CheckImageOutput.h" - -using namespace std::literals::chrono_literals; - -TEST_CASE("HLS_C_Simulation_internal_packet_generator", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - const uint16_t nmodules = 4; - - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(4).PedestalG0Frames(0); - - HLSSimulatedDevice test(0, 64); - - std::vector ref_data(RAW_MODULE_SIZE); - for (int i = 0; i < RAW_MODULE_SIZE; i++) - ref_data[i] = i % 65536; - - for (int m = 0; m < nmodules; m++) - test.SetInternalGeneratorFrame(ref_data.data(), m); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 128 * nmodules * 4 * JUNGFRAU_PACKET_SIZE_BYTES); - - for (int image = 0; image < 4; image++) { - for (int m = 0; m < nmodules; m++) { - auto imageBuf = (uint16_t *) test.GetDeviceOutput(image, m)->pixels; - for (int i = 0; i < RAW_MODULE_SIZE; i++) - REQUIRE(imageBuf[i] == i % 65536); - } - } - REQUIRE(test.GetExpectedDescriptorsPerModule() == DMA_DESCRIPTORS_PER_MODULE); -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_data_stream", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - const uint16_t nmodules = 4; - - DiffractionExperiment x(DetJF(nmodules)); - x.DataStreams(4); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(4).PedestalG0Frames(0); - - HLSSimulatedDevice test(3, 64); - - std::vector ref_data(RAW_MODULE_SIZE); - for (int i = 0; i < RAW_MODULE_SIZE; i++) - ref_data[i] = i % 65536; - - test.SetInternalGeneratorFrame(ref_data.data(), 0); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 128 * nmodules * JUNGFRAU_PACKET_SIZE_BYTES); - - for (int image = 0; image < 4; image++) { - auto imageBuf = (uint16_t *) test.GetDeviceOutput(image, 0)->pixels; - for (int i = 0; i < RAW_MODULE_SIZE; i++) - REQUIRE(imageBuf[i] == i % 65536); - } - REQUIRE(test.GetExpectedDescriptorsPerModule() == DMA_DESCRIPTORS_PER_MODULE); -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_custom_frame", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 32; - const size_t nframes = 2; - DiffractionExperiment x(DetJF(nmodules)); - - std::vector test_frame(nmodules*RAW_MODULE_SIZE); - - std::mt19937 g1(1387); - std::uniform_int_distribution dist(0, 65535); - - for (auto &i: test_frame) - i = dist(g1); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(nframes).PedestalG0Frames(0); - - HLSSimulatedDevice test(0, 128); - for (int m = 0; m < x.GetModulesNum(); m++) - test.SetInternalGeneratorFrame(test_frame.data() + RAW_MODULE_SIZE * m, m); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 128 * nmodules * nframes * JUNGFRAU_PACKET_SIZE_BYTES); - - for (int image = 0; image < nframes; image++) { - for (int m = 0; m < nmodules; m++) { - REQUIRE(test.GetDeviceOutput(image, m)->module_statistics.timestamp == INT_PKT_GEN_EXPTTIME * image); - REQUIRE(test.GetDeviceOutput(image, m)->module_statistics.pulse_id == INT_PKT_GEN_BUNCHID + image); - REQUIRE(test.GetDeviceOutput(image, m)->module_statistics.exptime == INT_PKT_GEN_EXPTTIME); - REQUIRE(test.GetDeviceOutput(image, m)->module_statistics.detector_type == SLS_DETECTOR_TYPE_JUNGFRAU); - - auto imageBuf = (uint16_t *) test.GetDeviceOutput(image, m)->pixels; - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - REQUIRE(imageBuf[i] == test_frame[m * RAW_MODULE_SIZE + i]); - } - } - } -} - -TEST_CASE("HLS_C_Simulation_check_raw", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - std::vector raw_frames(RAW_MODULE_SIZE); - - LoadBinaryFile("../../tests/test_data/mod5_raw0.bin", raw_frames.data(), RAW_MODULE_SIZE); - - const uint16_t nmodules = 4; - DiffractionExperiment x(DetJF(nmodules)); - uint16_t data[4096]; - - x.Raw(); - x.PedestalG0Frames(0).ImagesPerTrigger(1).NumTriggers(1); - - HLSSimulatedDevice test(0, 64); - test.CreateJFPackets(x, 1, 1, 0, raw_frames.data()); - test.CreateFinalPacket(x); - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.Counters().GetSlowestFrameNumber() == 0); - - REQUIRE(test.GetBytesReceived() == 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - uint64_t diffs = 0; - - for (int j = 0; j < RAW_MODULE_SIZE; j++) { - if (raw_frames[j] != ((uint16_t *) test.GetDeviceOutput(0, 0)->pixels)[j]) { - diffs++; - std::cout << raw_frames[j] << " " << ((uint16_t *) test.GetDeviceOutput(0, 0)->pixels)[j] << std::endl; - } - } - - REQUIRE(diffs == 0); -} - -TEST_CASE("HLS_C_Simulation_check_missing_modules", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 4; - const uint64_t nframes = AcquisitionCounters::ThresholdFramesLost + 5; - - std::vector raw_frames(nframes * RAW_MODULE_SIZE, 0); - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw(); - x.PedestalG0Frames(0).ImagesPerTrigger(nframes).NumTriggers(1); - - HLSSimulatedDevice test(0, nframes + 2); - test.CreateJFPackets(x, 1, nframes, 0, raw_frames.data()); - test.CreateJFPackets(x, 1, 3, 1, raw_frames.data()); - test.CreateFinalPacket(x); - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.Counters().GetSlowestFrameNumber() == 5 - 1); - - REQUIRE(test.Counters().GetBufferHandle(0,0) != AcquisitionCounters::HandleNotFound); - REQUIRE(test.Counters().GetBufferHandle(0,1) == AcquisitionCounters::HandleNotFound); - REQUIRE(test.Counters().GetBufferHandle(0,2) == AcquisitionCounters::HandleNotFound); - - REQUIRE(test.GetBytesReceived() == nframes * 128 * JUNGFRAU_PACKET_SIZE_BYTES); -} - - -TEST_CASE("HLS_C_Simulation_check_cancel", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 4; - DiffractionExperiment x(DetJF(nmodules)); - - uint16_t data[4096]; - - x.Raw(); - x.PedestalG0Frames(0).ImagesPerTrigger(5).NumTriggers(1); - - HLSSimulatedDevice test(0, 64); - - REQUIRE_NOTHROW(test.StartAction(x)); - test.Cancel(); - - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.Counters().GetSlowestFrameNumber() == 0); - - - REQUIRE(test.GetBytesReceived() == 0); -} - - -TEST_CASE("HLS_C_Simulation_check_cancel_conversion", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 4; - DiffractionExperiment x(DetJF(nmodules)); - - uint16_t data[4096]; - - x.PedestalG0Frames(0).ImagesPerTrigger(5).NumTriggers(1); - - HLSSimulatedDevice test(0, 64); - - REQUIRE_NOTHROW(test.StartAction(x)); - test.Cancel(); - - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.Counters().GetSlowestFrameNumber() == 0); - - REQUIRE(test.GetBytesReceived() == 0); -} - -TEST_CASE("HLS_C_Simulation_check_delay", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector raw_frames(RAW_MODULE_SIZE*20); - - const uint16_t nmodules = 4; - DiffractionExperiment x(DetJF(nmodules)); - - uint16_t data[4096]; - - x.Raw(); - x.PedestalG0Frames(0).ImagesPerTrigger(3).NumTriggers(1); - - HLSSimulatedDevice test(0, 64); - - test.CreateJFPacket(x, 1, 0, 0, data); - test.CreateJFPacket(x, 1, 0, 1, data); - test.CreateJFPacket(x, 1, 0, 2, data); - test.CreateJFPacket(x, 1, 0, 3, data); - - test.CreateJFPacket(x, 2, 0, 0, data); - test.CreateJFPacket(x, 2, 0, 1, data); - test.CreateJFPacket(x, 2, 0, 2, data); - test.CreateJFPacket(x, 2, 0, 3, data); - - test.CreateJFPacket(x, 3, 0, 0, data); - test.CreateJFPacket(x, 3, 0, 1, data); - test.CreateJFPacket(x, 3, 0, 2, data); - test.CreateJFPacket(x, 3, 0, 3, data); - - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.Counters().CalculateDelay(0) == 2); - REQUIRE(test.Counters().CalculateDelay(0, 0) == 2); - REQUIRE(test.Counters().CalculateDelay(1) == 1); - REQUIRE(test.Counters().CalculateDelay(1, 0) == 1); - REQUIRE(test.Counters().CalculateDelay(2) == 0); - REQUIRE(test.Counters().CalculateDelay(2, 0) == 0); -} - -TEST_CASE("HLS_C_Simulation_check_lost_frame_raw", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector raw_frames(RAW_MODULE_SIZE*20); - - const uint16_t nmodules = 4; - DiffractionExperiment x(DetJF(nmodules)); - - uint16_t data[4096]; - - for (int i = 0; i < 4096; i++) data[i] = i; - x.Raw(); - x.PedestalG0Frames(0).ImagesPerTrigger(3).NumTriggers(1); - - HLSSimulatedDevice test(0, 64); - - test.CreateJFPacket(x, 1, 0, 0, data); - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == JUNGFRAU_PACKET_SIZE_BYTES); - - REQUIRE(test.GetDeviceOutput(0,0)->pixels[0] == 0); - REQUIRE(test.GetDeviceOutput(0,0)->pixels[1] == 1); - REQUIRE(test.GetDeviceOutput(0,0)->pixels[4095] == 4095); - REQUIRE_THROWS(test.GetDeviceOutput(0,1)); - REQUIRE_THROWS(test.GetDeviceOutput(0,1)); -} - -TEST_CASE("HLS_C_Simulation_check_lost_frame_conversion", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector raw_frames(RAW_MODULE_SIZE*20); - - const uint16_t nmodules = 4; - DiffractionExperiment x(DetJF(nmodules)); - - uint16_t data[4096]; - - for (int i = 0; i < 4096; i++) data[i] = i; - x.PedestalG0Frames(0).ImagesPerTrigger(3).NumTriggers(1); - - HLSSimulatedDevice test(0, 64); - - test.CreateJFPacket(x, 1, 0, 0, data); - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - - REQUIRE(test.GetBytesReceived() == JUNGFRAU_PACKET_SIZE_BYTES); - - REQUIRE_THROWS(test.GetDeviceOutput(0,1)); - REQUIRE_THROWS(test.GetDeviceOutput(0,1)); - REQUIRE_THROWS(test.GetDeviceOutput(1,0)); - REQUIRE_THROWS(test.GetDeviceOutput(2,1)); -} - -TEST_CASE("HLS_C_Simulation_check_single_packet_raw", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector raw_frames(RAW_MODULE_SIZE*20); - - const uint16_t nmodules = 4; - DiffractionExperiment x(DetJF(nmodules)); - - uint16_t data[4096]; - uint16_t wrong[4096]; - - for (int i = 0; i < 4096; i++) { - data[i] = (7 * i + 534 + 5 * i * i) % UINT16_MAX; - wrong[i] = UINT16_MAX; - } - - x.Raw(); - x.PedestalG0Frames(0).ImagesPerTrigger(3).NumTriggers(1); - HLSSimulatedDevice test(0, 64); - - test.CreateJFPacket(x, 1, 0, 0, data); - test.CreateJFPacket(x, 1, 64, 0, data); - test.CreateJFPacket(x, 1, 0, 2, data); - - test.CreateJFPacket(x, 1, 2, 3, data); - test.CreateJFPacket(x, 1, 3, 3, data); - - test.CreateJFPacket(x, 1, 1, 3, data); - test.CreateJFPacket(x, 1, 0, 3, data); - test.CreateJFPacket(x, 1, 64, 3, data); - - test.CreateJFPacket(x, 1, 5, 0, data); - test.CreateJFPacket(x, 1, 4, 0, data); - - test.CreateJFPacket(x, 1, 67, 1, data); - test.CreateJFPacket(x, 1, 66, 1, data); - test.CreateJFPacket(x, 1, 68, 1, data); - - test.CreateJFPacket(x, 3, 1, 0, data); - test.CreateJFPacket(x, 2, 1, 0, data); - - test.CreateJFPacket(x, 4, 1, 0, data); - - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 15 * JUNGFRAU_PACKET_SIZE_BYTES); - - REQUIRE(memcmp(test.GetDeviceOutput(0,0)->pixels, data, JUNGFRAU_PACKET_SIZE_BYTES) == 0); - REQUIRE(memcmp(test.GetDeviceOutput(0,0)->pixels + 1 * JUNGFRAU_PACKET_SIZE_BYTES/2, wrong, JUNGFRAU_PACKET_SIZE_BYTES) == 0); - REQUIRE(memcmp(test.GetDeviceOutput(0,0)->pixels + 2 * JUNGFRAU_PACKET_SIZE_BYTES/2, wrong, JUNGFRAU_PACKET_SIZE_BYTES) == 0); - REQUIRE(memcmp(test.GetDeviceOutput(0,0)->pixels + 3 * JUNGFRAU_PACKET_SIZE_BYTES/2, wrong, JUNGFRAU_PACKET_SIZE_BYTES) == 0); - REQUIRE(memcmp(test.GetDeviceOutput(0,0)->pixels + 4 * JUNGFRAU_PACKET_SIZE_BYTES/2, data, JUNGFRAU_PACKET_SIZE_BYTES) == 0); - REQUIRE(memcmp(test.GetDeviceOutput(0,0)->pixels + 5 * JUNGFRAU_PACKET_SIZE_BYTES/2, data, JUNGFRAU_PACKET_SIZE_BYTES) == 0); - REQUIRE(memcmp(test.GetDeviceOutput(0,0)->pixels + 6 * JUNGFRAU_PACKET_SIZE_BYTES/2, wrong, JUNGFRAU_PACKET_SIZE_BYTES) == 0); - REQUIRE(memcmp(test.GetDeviceOutput(0,0)->pixels + 64 * JUNGFRAU_PACKET_SIZE_BYTES/2, data, JUNGFRAU_PACKET_SIZE_BYTES) == 0); - - REQUIRE(memcmp(test.GetDeviceOutput(0,1)->pixels + 67 * JUNGFRAU_PACKET_SIZE_BYTES/2, data, JUNGFRAU_PACKET_SIZE_BYTES) == 0); - REQUIRE(memcmp(test.GetDeviceOutput(0,1)->pixels + 65 * JUNGFRAU_PACKET_SIZE_BYTES/2, wrong, JUNGFRAU_PACKET_SIZE_BYTES) == 0); - REQUIRE(test.GetDeviceOutput(0,0)->module_statistics.detector_type == SLS_DETECTOR_TYPE_JUNGFRAU); -} - -TEST_CASE("HLS_C_Simulation_check_single_packet_conv", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector raw_frames(RAW_MODULE_SIZE*20); - - const uint16_t nmodules = 4; - DiffractionExperiment x(DetJF(nmodules)); - - uint16_t data[4096]; - int16_t wrong[4096]; - - for (int i = 0; i < 4096; i++) { - data[i] = (7 * i + 534 + 5 * i * i) % UINT16_MAX; - wrong[i] = INT16_MIN; - } - - x.PedestalG0Frames(0).ImagesPerTrigger(3).NumTriggers(1); - HLSSimulatedDevice test(0, 64); - - auto gain_from_file = GainCalibrationFromTestFile(); - - JFModulePedestal pedestal_g0(1000), pedestal_g1(14500), pedestal_g2(14500); - JFCalibration c_in(x); - c_in.Pedestal(0, 0) = pedestal_g0; - c_in.Pedestal(0, 1) = pedestal_g1; - c_in.Pedestal(0, 2) = pedestal_g2; - for (int i = 0; i < x.GetModulesNum(); i++) - c_in.GainCalibration(i) = gain_from_file; - - test.InitializeCalibration(x, c_in); - - test.CreateJFPacket(x, 1, 0, 0, data); - test.CreateJFPacket(x, 1, 64, 0, data); - - test.CreateJFPacket(x, 1, 67, 1, data); - test.CreateJFPacket(x, 1, 66, 1, data); - test.CreateJFPacket(x, 1, 68, 1, data); - - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 5 * JUNGFRAU_PACKET_SIZE_BYTES); - CHECK(memcmp(test.GetDeviceOutput(0,0)->pixels + 0 * JUNGFRAU_PACKET_SIZE_BYTES/2, wrong, JUNGFRAU_PACKET_SIZE_BYTES) != 0); - CHECK(memcmp(test.GetDeviceOutput(0,0)->pixels + 1 * JUNGFRAU_PACKET_SIZE_BYTES/2, wrong, JUNGFRAU_PACKET_SIZE_BYTES) == 0); - CHECK(memcmp(test.GetDeviceOutput(0,0)->pixels + 2 * JUNGFRAU_PACKET_SIZE_BYTES/2, wrong, JUNGFRAU_PACKET_SIZE_BYTES) == 0); - CHECK(memcmp(test.GetDeviceOutput(0,0)->pixels + 3 * JUNGFRAU_PACKET_SIZE_BYTES/2, wrong, JUNGFRAU_PACKET_SIZE_BYTES) == 0); - CHECK(memcmp(test.GetDeviceOutput(0,0)->pixels + 6 * JUNGFRAU_PACKET_SIZE_BYTES/2, wrong, JUNGFRAU_PACKET_SIZE_BYTES) == 0); - CHECK(memcmp(test.GetDeviceOutput(0,0)->pixels + 64 * JUNGFRAU_PACKET_SIZE_BYTES/2, wrong, JUNGFRAU_PACKET_SIZE_BYTES) != 0); - - CHECK(memcmp(test.GetDeviceOutput(0,1)->pixels + 65 * JUNGFRAU_PACKET_SIZE_BYTES/2, wrong, JUNGFRAU_PACKET_SIZE_BYTES) == 0); - CHECK(memcmp(test.GetDeviceOutput(0,1)->pixels + 66 * JUNGFRAU_PACKET_SIZE_BYTES/2, wrong, JUNGFRAU_PACKET_SIZE_BYTES) != 0); - CHECK(memcmp(test.GetDeviceOutput(0,1)->pixels + 67 * JUNGFRAU_PACKET_SIZE_BYTES/2, wrong, JUNGFRAU_PACKET_SIZE_BYTES) != 0); - CHECK(memcmp(test.GetDeviceOutput(0,1)->pixels + 68 * JUNGFRAU_PACKET_SIZE_BYTES/2, wrong, JUNGFRAU_PACKET_SIZE_BYTES) != 0); - CHECK(test.GetDeviceOutput(0,0)->module_statistics.detector_type == SLS_DETECTOR_TYPE_JUNGFRAU); -} - -TEST_CASE("HLS_C_Simulation_check_convert_full_range", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - std::vector data(RAW_MODULE_SIZE); - std::vector gain(3 * RAW_MODULE_SIZE); - - JFModulePedestal pedestal_g0(0), pedestal_g1(14500), pedestal_g2(14500); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - data[i] = i % UINT16_MAX; - } - - std::vector energy_values = {6.0, 12.4, 17.7, 5, 4.5, 3.7}; - - const uint16_t nmodules = 1; - DiffractionExperiment x(DetJF(nmodules)); - - HLSSimulatedDevice test(0, 64); - - auto gain_from_file = GainCalibrationFromTestFile(); - - for (const auto energy : energy_values) { - x.PedestalG0Frames(0).NumTriggers(1).ImagesPerTrigger(1).IncidentEnergy_keV(energy); - x.GeometryTransformation(false); - REQUIRE(x.GetIncidentEnergy_keV() == Catch::Approx(energy)); - - JFCalibration c_in(x); - c_in.Pedestal(0,0) = pedestal_g0; - c_in.Pedestal(0,1) = pedestal_g1; - c_in.Pedestal(0,2) = pedestal_g2; - for (int i = 0; i < x.GetModulesNum(); i++) - c_in.GainCalibration(i) = gain_from_file; - - test.InitializeCalibration(x, c_in); - test.CreateJFPackets(x, 1, 1, 0, data.data()); - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - CHECK(test.GetBytesReceived() == 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - double mean_error = CheckImageOutput(x, c_in, data.data(), test.GetDeviceOutput(0, 0)->pixels); - logger.Info("Error of {:.2f} for photon energy {} keV", mean_error, energy); - REQUIRE(mean_error < 0.5); - } -} - -TEST_CASE("HLS_C_Simulation_check_convert_full_range_HG0", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector data(RAW_MODULE_SIZE); - std::vector gain(3 * RAW_MODULE_SIZE); - - JFModulePedestal pedestal_g0(1000), pedestal_g1(14500), pedestal_g2(14500); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - data[i] = i % 0x4000; // only generate gain 0 - } - - std::vector energy_values = {6.0, 12.4, 17.7, 5, 4.5, 3.7, 2.0, 1.0, 0.5, 0.1}; - - const uint16_t nmodules = 1; - DiffractionExperiment x(DetJF(nmodules)); - - x.UsingGainHG0(true); - HLSSimulatedDevice test(0, 64); - - auto gain_from_file = GainCalibrationFromTestFile(); - logger.Info("With HG0"); - for (const auto energy : energy_values) { - - x.PedestalG0Frames(0).NumTriggers(1).ImagesPerTrigger(1).IncidentEnergy_keV(energy); - x.GeometryTransformation(false); - REQUIRE(x.IsUsingGainHG0()); - REQUIRE(x.GetIncidentEnergy_keV() == Catch::Approx(energy)); - - JFCalibration c_in(x); - c_in.Pedestal(0,0) = pedestal_g0; - c_in.Pedestal(0,1) = pedestal_g1; - c_in.Pedestal(0,2) = pedestal_g2; - for (int i = 0; i < x.GetModulesNum(); i++) - c_in.GainCalibration(i) = gain_from_file; - - test.InitializeCalibration(x, c_in); - test.CreateJFPackets(x, 1, 1, 0, data.data()); - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - - CHECK(test.GetBytesReceived() == 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - double mean_error = CheckImageOutput(x, c_in, data.data(), test.GetDeviceOutput(0, 0)->pixels); - logger.Info(" std. dev. of {:.2f} for photon energy {} keV", mean_error, energy); - REQUIRE(mean_error < 0.5); - } - logger.Info("Without HG0"); - x.UsingGainHG0(false); - for (const auto energy : energy_values) { - - x.PedestalG0Frames(0).NumTriggers(1).ImagesPerTrigger(1).IncidentEnergy_keV(energy); - x.GeometryTransformation(false); - REQUIRE(!x.IsUsingGainHG0()); - REQUIRE(x.GetIncidentEnergy_keV() == Catch::Approx(energy)); - - JFCalibration c_in(x); - c_in.Pedestal(0,0) = pedestal_g0; - c_in.Pedestal(0,1) = pedestal_g1; - c_in.Pedestal(0,2) = pedestal_g2; - for (int i = 0; i < x.GetModulesNum(); i++) - c_in.GainCalibration(i) = gain_from_file; - - test.InitializeCalibration(x, c_in); - test.CreateJFPackets(x, 1, 1, 0, data.data()); - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - - CHECK(test.GetBytesReceived() == 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - double mean_error = CheckImageOutput(x, c_in, data.data(), test.GetDeviceOutput(0, 0)->pixels); - logger.Info(" std. dev. of {:.2f} for photon energy {} keV", mean_error, energy); - REQUIRE(mean_error < 0.5); - } -} - - -TEST_CASE("HLS_C_Simulation_check_convert_full_range_fixedG1", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector data(RAW_MODULE_SIZE); - std::vector gain(3 * RAW_MODULE_SIZE); - - JFModulePedestal pedestal_g0(1000), pedestal_g1(1500), pedestal_g2(14500); - - std::vector energy_values = {6.0, 12.4, 17.7, 5, 4.5, 3.7, 2.0, 1.0}; - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - data[i] = i % UINT16_MAX; - } - - const uint16_t nmodules = 1; - DiffractionExperiment x(DetJF(nmodules)); - - x.FixedGainG1(true); - HLSSimulatedDevice test(0, 64); - - auto gain_from_file = GainCalibrationFromTestFile(); - - for (const auto energy : energy_values) { - - x.PedestalG0Frames(0).NumTriggers(1).ImagesPerTrigger(1).IncidentEnergy_keV(energy).GeometryTransformation(false); - REQUIRE(x.IsFixedGainG1()); - REQUIRE(x.GetIncidentEnergy_keV() == Catch::Approx(energy)); - - JFCalibration c_in(x); - c_in.Pedestal(0,0) = pedestal_g0; - c_in.Pedestal(0,1) = pedestal_g1; - c_in.Pedestal(0,2) = pedestal_g2; - for (int i = 0; i < x.GetModulesNum(); i++) - c_in.GainCalibration(i) = gain_from_file; - - test.InitializeCalibration(x, c_in); - test.CreateJFPackets(x, 1, 1, 0, data.data()); - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - - CHECK(test.GetBytesReceived() == 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - double mean_error = CheckImageOutput(x, c_in, data.data(), test.GetDeviceOutput(0, 0)->pixels); - logger.Info(" std. dev. of {:.2f} for photon energy {} keV", mean_error, energy); - REQUIRE(mean_error < 0.5); - } -} - -TEST_CASE("HLS_C_Simulation_check_convert_full_range_I32", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector data(RAW_MODULE_SIZE); - std::vector gain(3 * RAW_MODULE_SIZE); - - JFModulePedestal pedestal_g0(0), pedestal_g1(14500), pedestal_g2(14500); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - data[i] = i % UINT16_MAX; - } - - std::vector energy_values = {6.0, 12.4, 17.7, 5, 4.5, 3.7, 3.0}; - - const uint16_t nmodules = 1; - DiffractionExperiment x(DetJF(nmodules)); - - HLSSimulatedDevice test(0, 64); - - auto gain_from_file = GainCalibrationFromTestFile(); - - for (const auto energy : energy_values) { - x.PedestalG0Frames(0).NumTriggers(1).ImagesPerTrigger(1).IncidentEnergy_keV(energy) - .PixelSigned(true).BitDepthImage(32).GeometryTransformation(false); - - REQUIRE(x.GetIncidentEnergy_keV() == Catch::Approx(energy)); - - JFCalibration c_in(x); - c_in.Pedestal(0,0) = pedestal_g0; - c_in.Pedestal(0,1) = pedestal_g1; - c_in.Pedestal(0,2) = pedestal_g2; - for (int i = 0; i < x.GetModulesNum(); i++) - c_in.GainCalibration(i) = gain_from_file; - - test.InitializeCalibration(x, c_in); - test.CreateJFPackets(x, 1, 1, 0, data.data()); - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - - CHECK(test.GetBytesReceived() == 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - double mean_error = CheckImageOutput(x, c_in, data.data(), (int32_t *) test.GetDeviceOutput(0, 0)->pixels); - logger.Info("Error of {:.2f} for photon energy {} keV", mean_error, energy); - REQUIRE(mean_error < 0.5); - } -} - -TEST_CASE("HLS_C_Simulation_check_convert_full_range_sum4", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector data(RAW_MODULE_SIZE); - std::vector gain(3 * RAW_MODULE_SIZE); - - JFModulePedestal pedestal_g0(0), pedestal_g1(14500), pedestal_g2(14500); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - data[i] = i % UINT16_MAX; - } - - std::vector energy_values = {6.0, 12.4, 17.7, 5, 4.5, 3.7}; - - const uint16_t nmodules = 1; - DiffractionExperiment x(DetJF(nmodules)); - - HLSSimulatedDevice test(0, 64); - - auto gain_from_file = GainCalibrationFromTestFile(); - - size_t nsummation = 4; - - for (const auto energy : energy_values) { - logger.Info("Trying with {} keV", energy); - x.PedestalG0Frames(0).NumTriggers(1).ImagesPerTrigger(1).IncidentEnergy_keV(energy).Summation(nsummation) - .GeometryTransformation(false); - - REQUIRE(x.GetIncidentEnergy_keV() == Catch::Approx(energy)); - - JFCalibration c_in(x); - c_in.Pedestal(0,0) = pedestal_g0; - c_in.Pedestal(0,1) = pedestal_g1; - c_in.Pedestal(0,2) = pedestal_g2; - for (int i = 0; i < x.GetModulesNum(); i++) - c_in.GainCalibration(i) = gain_from_file; - - test.InitializeCalibration(x, c_in); - for (int i = 0; i < nsummation; i++) - test.CreateJFPackets(x, i + 1, 1, 0, data.data()); - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - - CHECK(test.GetBytesReceived() == 128 * nsummation * JUNGFRAU_PACKET_SIZE_BYTES); - - double mean_error = CheckImageOutput(x, c_in, data.data(), (int32_t *) test.GetDeviceOutput(0, 0)->pixels); - - REQUIRE(mean_error < 2.0); - } -} - -TEST_CASE("HLS_C_Simulation_check_convert_full_range_U16", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector data(RAW_MODULE_SIZE); - std::vector gain(3 * RAW_MODULE_SIZE); - - JFModulePedestal pedestal_g0(0), pedestal_g1(14500), pedestal_g2(14500); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - data[i] = i % UINT16_MAX; - } - - std::vector energy_values = {6.0, 12.4, 17.7, 5, 4.5, 3.7}; - - const uint16_t nmodules = 1; - DiffractionExperiment x(DetJF(nmodules)); - - HLSSimulatedDevice test(0, 64); - - auto gain_from_file = GainCalibrationFromTestFile(); - - for (const auto energy : energy_values) { - logger.Info("Trying with {} keV", energy); - x.PedestalG0Frames(0).NumTriggers(1).ImagesPerTrigger(1).IncidentEnergy_keV(energy) - .PixelSigned(false).BitDepthImage(16).GeometryTransformation(false); - - REQUIRE(x.GetIncidentEnergy_keV() == Catch::Approx(energy)); - - JFCalibration c_in(x); - c_in.Pedestal(0,0) = pedestal_g0; - c_in.Pedestal(0,1) = pedestal_g1; - c_in.Pedestal(0,2) = pedestal_g2; - for (int i = 0; i < x.GetModulesNum(); i++) - c_in.GainCalibration(i) = gain_from_file; - - test.InitializeCalibration(x, c_in); - test.CreateJFPackets(x, 1, 1, 0, data.data()); - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - - CHECK(test.GetBytesReceived() == 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - double mean_error = CheckImageOutput(x, c_in, data.data(), (uint16_t *) test.GetDeviceOutput(0, 0)->pixels); - - REQUIRE(mean_error < 0.5); - } -} - -TEST_CASE("HLS_C_Simulation_check_poisson_full_range", "[FPGA][Full]") { - // This test is not 100% representable! - // At the moment it is not possible to include "hls_math.h" in jfjoch_test - // So hls::sqrt is actually implemented as round(sqrt()) - // This can be only handled by HLS test bench - - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector data(RAW_MODULE_SIZE); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - data[i] = i % UINT16_MAX; - } - - const uint16_t nmodules = 1; - - for (int sqrtmult: {1, 2,4,8}) { - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw().PixelSigned(true).BitDepthImage(16); - HLSSimulatedDevice test(0, 64); - - x.NumTriggers(1).ImagesPerTrigger(1).LossyCompressionPoisson(sqrtmult); - - test.CreateJFPackets(x, 1, 1, 0, data.data()); - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - CHECK(test.GetBytesReceived() == 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - auto data_signed = (int16_t *) data.data(); - - uint64_t diff = 0; - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - uint64_t expected_value = 0; - if (data_signed[i] == INT16_MAX) - expected_value = INT16_MAX; - else if (data_signed[i] == INT16_MIN) - expected_value = INT16_MIN; - else if (data_signed[i] >= 0) - expected_value = std::lround(std::sqrt(data_signed[i]) * sqrtmult); - - if (expected_value != test.GetDeviceOutput(0, 0)->pixels[i]) - diff++; - - } - REQUIRE(diff == 0); - } -} - -TEST_CASE("HLS_C_Simulation_check_threshold_full_range", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector data(RAW_MODULE_SIZE); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - data[i] = i % UINT16_MAX; - } - - const uint16_t nmodules = 1; - - for (int threshold: {1, 2, 4, 8}) { - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw().PixelSigned(true).BitDepthImage(16); - HLSSimulatedDevice test(0, 64); - - x.NumTriggers(1).ImagesPerTrigger(1).PixelValueLowThreshold(threshold); - - test.CreateJFPackets(x, 1, 1, 0, data.data()); - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - CHECK(test.GetBytesReceived() == 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - auto data_signed = (int16_t *) data.data(); - - uint64_t diff = 0; - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - uint64_t expected_value = data_signed[i]; - if (data_signed[i] == INT16_MAX) - expected_value = INT16_MAX; - else if (data_signed[i] == INT16_MIN) - expected_value = INT16_MIN; - else if (data_signed[i] < threshold) - expected_value = 0; - - if (expected_value != test.GetDeviceOutput(0, 0)->pixels[i]) { - std::cout << data_signed[i] << " " << expected_value << " " << threshold << std::endl; - diff++; - } - - } - REQUIRE(diff == 0); - } -} - -TEST_CASE("HLS_C_Simulation_check_convert_full_range_poisson", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector data(RAW_MODULE_SIZE); - std::vector gain(3 * RAW_MODULE_SIZE); - - JFModulePedestal pedestal_g0(0), pedestal_g1(14500), pedestal_g2(14500); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - data[i] = i % UINT16_MAX; - } - - std::vector energy_values = {6.0, 12.4, 17.7, 5, 4.5, 3.7}; - - const uint16_t nmodules = 1; - DiffractionExperiment x(DetJF(nmodules)); - x.LossyCompressionPoisson(2); - HLSSimulatedDevice test(0, 64); - - auto gain_from_file = GainCalibrationFromTestFile(); - - for (const auto energy : energy_values) { - logger.Info("Trying with {} keV", energy); - x.PedestalG0Frames(0).NumTriggers(1).ImagesPerTrigger(1).IncidentEnergy_keV(energy) - .PixelSigned(false).BitDepthImage(16).GeometryTransformation(false); - - REQUIRE(x.GetIncidentEnergy_keV() == Catch::Approx(energy)); - - JFCalibration c_in(x); - c_in.Pedestal(0,0) = pedestal_g0; - c_in.Pedestal(0,1) = pedestal_g1; - c_in.Pedestal(0,2) = pedestal_g2; - for (int i = 0; i < x.GetModulesNum(); i++) - c_in.GainCalibration(i) = gain_from_file; - - test.InitializeCalibration(x, c_in); - test.CreateJFPackets(x, 1, 1, 0, data.data()); - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - - CHECK(test.GetBytesReceived() == 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - double mean_error = CheckImageOutput(x, c_in, data.data(), (uint16_t *) test.GetDeviceOutput(0, 0)->pixels); - - REQUIRE(mean_error < 0.5); - } -} - -TEST_CASE("HLS_C_Simulation_no_conversion_U16", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector data(RAW_MODULE_SIZE); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) - data[i] = i % UINT16_MAX; - - data[5456] = UINT16_MAX; - data[211] = UINT16_MAX; - - const uint16_t nmodules = 1; - DiffractionExperiment x(DetJF(nmodules)); - - x.JungfrauConvPhotonCnt(false); - HLSSimulatedDevice test(0, 64); - - x.PedestalG0Frames(0).NumTriggers(1).ImagesPerTrigger(1).PixelSigned(false).BitDepthImage(16); - - test.CreateJFPackets(x, 1, 1, 0, data.data()); - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - CHECK(test.GetBytesReceived() == 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - auto output = (uint16_t *) test.GetDeviceOutput(0, 0)->pixels; - size_t err = 0; - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - CHECK(data[i] == output[i]); - } - REQUIRE(err == 0); -} - -TEST_CASE("HLS_C_Simulation_no_conversion_U32", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector data(RAW_MODULE_SIZE); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) - data[i] = i % UINT16_MAX; - - data[5456] = UINT16_MAX; - data[211] = UINT16_MAX; - - const uint16_t nmodules = 1; - DiffractionExperiment x(DetJF(nmodules)); - - x.JungfrauConvPhotonCnt(false); - HLSSimulatedDevice test(0, 64); - - x.PedestalG0Frames(0).NumTriggers(1).ImagesPerTrigger(1).PixelSigned(false).BitDepthImage(32); - - test.CreateJFPackets(x, 1, 1, 0, data.data()); - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - CHECK(test.GetBytesReceived() == 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - auto output = (uint32_t *) test.GetDeviceOutput(0, 0)->pixels; - size_t err = 0; - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - if (data[i] == UINT16_MAX) { - CHECK(output[i] == UINT32_MAX); - } else if (data[i] != output[i]) err++; - } - REQUIRE(err == 0); -} - -TEST_CASE("HLS_C_Simulation_no_conversion_I32", "[FPGA][Full]") { - Logger logger("HLS_C_Simulation_no_conversion_I32"); - std::vector data(RAW_MODULE_SIZE); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) - data[i] = i % UINT16_MAX; - - data[5456] = UINT16_MAX; - data[211] = UINT16_MAX; - - const uint16_t nmodules = 1; - DiffractionExperiment x(DetJF(nmodules)); - - x.JungfrauConvPhotonCnt(false); - HLSSimulatedDevice test(0, 64); - - x.PedestalG0Frames(0).NumTriggers(1).ImagesPerTrigger(1).PixelSigned(true).BitDepthImage(32); - - test.CreateJFPackets(x, 1, 1, 0, data.data()); - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - CHECK(test.GetBytesReceived() == 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - auto data16_signed = (int16_t *) data.data(); - auto output = (int32_t *) test.GetDeviceOutput(0, 0)->pixels; - size_t err = 0; - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - if (data16_signed[i] == INT16_MAX) { - CHECK(output[i] == INT32_MAX); - } else if (data16_signed[i] == INT16_MIN) { - CHECK(output[i] == INT32_MIN); - } else if (data16_signed[i] != output[i]) err++; - } - REQUIRE(err == 0); -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_convert_full_range", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - double energy = 6.0; - const uint16_t nmodules = 1; - DiffractionExperiment x(DetJF(nmodules)); - - std::vector data(RAW_MODULE_SIZE); - JFModulePedestal pedestal_g0, pedestal_g1, pedestal_g2; - - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - pedestal_g0.GetPedestal()[i] = 0 + (i / 65536) * 1000 + 100 * (i % 5); - pedestal_g1.GetPedestal()[i] = 14500 - (i / 65536) * 1000 + 100 * (i % 3); - pedestal_g2.GetPedestal()[i] = 14500 - + (i / 65536) * 1000; - } - - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - data[i] = i % RAW_MODULE_SIZE; - } - - x.PedestalG0Frames(0).NumTriggers(1).ImagesPerTrigger(1).UseInternalPacketGenerator(true).IncidentEnergy_keV(energy) - .GeometryTransformation(false); - REQUIRE(x.GetIncidentEnergy_keV() == Catch::Approx(energy)); - - auto gain_from_file = GainCalibrationFromTestFile(); - - JFCalibration c(x); - for (int i = 0; i < x.GetModulesNum(); i++) { - c.Pedestal(i, 0) = pedestal_g0; - c.Pedestal(i, 1) = pedestal_g1; - c.Pedestal(i, 2) = pedestal_g2; - c.GainCalibration(i) = gain_from_file; - } - - HLSSimulatedDevice test(0, 64); - - std::vector ref_data(RAW_MODULE_SIZE); - for (int i = 0; i < RAW_MODULE_SIZE; i++) - ref_data[i] = i % 65536; - - for (int m = 0; m < nmodules; m++) - test.SetInternalGeneratorFrame(ref_data.data(), m); - - REQUIRE_NOTHROW(test.InitializeCalibration(x, c)); - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - - REQUIRE(test.GetBytesReceived() == nmodules * 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - double mean_error = CheckImageOutput(x, c, data.data(), test.GetDeviceOutput(0, 0)->pixels); - - REQUIRE(mean_error < 0.5); -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_mask", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - double energy = 6.0; - const uint16_t nmodules = 2; - DiffractionExperiment x(DetJF(nmodules)); - - std::vector data(RAW_MODULE_SIZE); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) - data[i] = i % 7; - - x.Raw(); - x.PedestalG0Frames(0).NumTriggers(1).ImagesPerTrigger(1).UseInternalPacketGenerator(true).IncidentEnergy_keV(energy) - .GeometryTransformation(false).PixelSigned(true).ApplyPixelMask(true); - REQUIRE(x.GetIncidentEnergy_keV() == Catch::Approx(energy)); - - HLSSimulatedDevice test(0, 64); - - std::vector module_pixel_mask(RAW_MODULE_SIZE); - module_pixel_mask[456*49+6] = 1; - module_pixel_mask[0] = 128; - module_pixel_mask[RAW_MODULE_SIZE - 1] = 16384; - - for (int m = 0; m < nmodules; m++) - test.SetInternalGeneratorFrame(data.data(), m); - - REQUIRE_NOTHROW(test.InitializePixelMask(module_pixel_mask.data(), 1)); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == nmodules * 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - CHECK(test.GetDeviceOutput(0, 0)->pixels[0] == 0); - CHECK(test.GetDeviceOutput(0, 0)->pixels[1] == 1); - CHECK(test.GetDeviceOutput(0, 0)->pixels[456*49+5] == 5); - CHECK(test.GetDeviceOutput(0, 0)->pixels[456*49+6] == 6); - CHECK(test.GetDeviceOutput(0, 0)->pixels[456*49+7] == 0); - CHECK(test.GetDeviceOutput(0, 0)->pixels[RAW_MODULE_SIZE - 1] == (RAW_MODULE_SIZE - 1) % 7); - - CHECK(test.GetDeviceOutput(0, 1)->pixels[0] == INT16_MIN); - CHECK(test.GetDeviceOutput(0, 1)->pixels[1] == 1); - CHECK(test.GetDeviceOutput(0, 1)->pixels[456*49+5] == 5); - CHECK(test.GetDeviceOutput(0, 1)->pixels[456*49+6] == INT16_MIN); - CHECK(test.GetDeviceOutput(0, 1)->pixels[456*49+7] == 0); - CHECK(test.GetDeviceOutput(0, 1)->pixels[RAW_MODULE_SIZE - 1] == INT16_MIN); -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_convert_full_range_adu_histo", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - double energy = 6.0; - const uint16_t nmodules = 4; - DiffractionExperiment x(DetJF(nmodules, 2, 8, 36, true)); - - std::vector data(RAW_MODULE_SIZE); - JFModulePedestal pedestal_g0, pedestal_g1, pedestal_g2; - - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - pedestal_g0.GetPedestal()[i] = 0 + (i / 65536) * 1000 + 100 * (i % 5); - pedestal_g1.GetPedestal()[i] = 14500 - (i / 65536) * 1000 + 100 * (i % 3); - pedestal_g2.GetPedestal()[i] = 14500 - + (i / 65536) * 1000; - } - - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - data[i] = i % RAW_MODULE_SIZE; - } - - x.PedestalG0Frames(0).NumTriggers(1).ImagesPerTrigger(1).UseInternalPacketGenerator(true).IncidentEnergy_keV(energy); - REQUIRE(x.GetIncidentEnergy_keV() == Catch::Approx(energy)); - - auto gain_from_file = GainCalibrationFromTestFile(); - - JFCalibration c(x); - for (int i = 0; i < x.GetModulesNum(); i++) { - c.Pedestal(i, 0) = pedestal_g0; - c.Pedestal(i, 1) = pedestal_g1; - c.Pedestal(i, 2) = pedestal_g2; - c.GainCalibration(i) = gain_from_file; - } - - HLSSimulatedDevice test(0, 64); - - std::vector ref_data(RAW_MODULE_SIZE); - for (int i = 0; i < RAW_MODULE_SIZE; i++) - ref_data[i] = i % 65536; - - for (int m = 0; m < nmodules; m++) - test.SetInternalGeneratorFrame(ref_data.data(), m); - - REQUIRE_NOTHROW(test.InitializeCalibration(x, c)); - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - - REQUIRE(test.GetBytesReceived() == nmodules * 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - uint32_t err = 0; - const uint32_t *adu_histogram = test.GetDeviceOutput(0, 0)->adu_histogram; - for (int i = 0; i < ADU_HISTO_BIN_COUNT; i++) { - if(adu_histogram[i] != RAW_MODULE_SIZE / ADU_HISTO_BIN_COUNT) - err++; - } - REQUIRE(err == 0); -} - -TEST_CASE("HLS_C_Simulation_check_2_trigger_convert", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector pedestal_g0(RAW_MODULE_SIZE), pedestal_g1(RAW_MODULE_SIZE), pedestal_g2(RAW_MODULE_SIZE); - - LoadBinaryFile("../../tests/test_data/mod5_pedeG0.bin", pedestal_g0.data(), RAW_MODULE_SIZE); - LoadBinaryFile("../../tests/test_data/mod5_pedeG1.bin", pedestal_g1.data(), RAW_MODULE_SIZE); - LoadBinaryFile("../../tests/test_data/mod5_pedeG2.bin", pedestal_g2.data(), RAW_MODULE_SIZE); - - std::vector raw_frames(RAW_MODULE_SIZE); - std::vector conv_frames(RAW_MODULE_SIZE); - - LoadBinaryFile("../../tests/test_data/mod5_raw0.bin", raw_frames.data(), RAW_MODULE_SIZE); - LoadBinaryFile("../../tests/test_data/mod5_conv0.bin", conv_frames.data(), RAW_MODULE_SIZE); - - const uint16_t nmodules = 4; - DiffractionExperiment x(DetJF(nmodules)); - - uint16_t data[4096]; - - x.PedestalG0Frames(0).NumTriggers(1).ImagesPerTrigger(1); - - HLSSimulatedDevice test(0, 64); - - JFCalibration c(x); - REQUIRE_NOTHROW(c.Pedestal(0, 0).LoadPedestal(pedestal_g0)); - REQUIRE_NOTHROW(c.Pedestal(0, 1).LoadPedestal(pedestal_g1)); - REQUIRE_NOTHROW(c.Pedestal(0, 2).LoadPedestal(pedestal_g2)); - REQUIRE_NOTHROW(c.GainCalibration(0) = GainCalibrationFromTestFile()); - - REQUIRE_NOTHROW(test.InitializeCalibration(x, c)); - - // send some frames without trigger (to be ignored) - test.CreateJFPackets(x, 1, 1, 0, raw_frames.data()); - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - - REQUIRE(!test.Counters().IsAcquisitionFinished()); - - test.WaitForActionComplete(); - - REQUIRE(test.Counters().IsAcquisitionFinished()); - - // address properly aligned - REQUIRE((uint64_t) test.GetDeviceOutput(0,0)->pixels % 128 == 0); - - REQUIRE(test.Counters().GetSlowestFrameNumber() == 0); - REQUIRE(test.Counters().GetCurrFrameNumber(0) == 0); - - - REQUIRE(test.GetBytesReceived()== 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - double mean_error = 0.0; - auto img = test.GetDeviceOutput(0, 0)->pixels; - for (int j = 0; j < RAW_MODULE_SIZE; j++) { - if ((img[j] < 30000) && (img[j] > -30000)) { - float diff = (conv_frames[j] - (float) img[j]); - mean_error += diff * diff; - } - } - - mean_error = sqrt(mean_error/ (RAW_MODULE_SIZE)); - std::cout << "Mean error " << mean_error << std::endl; - REQUIRE(mean_error < 0.30); -} - - -TEST_CASE("HLS_C_Simulation_check_detect_last_frame", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 4; - DiffractionExperiment x(DetJF(nmodules)); - - uint16_t data[4096]; - - x.PedestalG0Frames(0).NumTriggers(2).ImagesPerTrigger(5); - - HLSSimulatedDevice test(0, 64); - - test.CreateJFPacket(x, 15, 0, 0, data); - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE(!test.Counters().IsAcquisitionFinished()); - - test.WaitForActionComplete(); - - REQUIRE(test.Counters().IsAcquisitionFinished()); - -} - -TEST_CASE("HLS_C_Simulation_check_wrong_packet_size", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 1; - DiffractionExperiment x(DetJF(nmodules)); - - uint16_t data[8192]; - - x.PedestalG0Frames(0).NumTriggers(1).ImagesPerTrigger(5); - - HLSSimulatedDevice test(0, 64); - - // send some frames with wrong size or tuser=1 - test.CreateJFPacket(x, 1, 0, 0, data, 0); - test.CreateJFPacket(x, 1, 1, 0, data, -1); - test.CreateJFPacket(x, 1, 2, 0, data, 2); - test.CreateJFPacket(x, 1, 3, 0, data, 0); - test.CreateJFPacket(x, 1, 4, 0, data, -5); - test.CreateJFPacket(x, 1, 5, 0, data, 0, 1); - test.CreateJFPacket(x, 1, 6, 0, data, 0); - test.CreateJFPacket(x, 1, 7, 0, data, 0); - test.CreateJFPacket(x, 1, 8, 0, data, 7); - test.CreateJFPacket(x, 1, 9, 0, data, 100); - test.CreateJFPacket(x, 1, 10, 0, data, 2); - test.CreateJFPacket(x, 1, 11, 0, data, 0); - test.CreateJFPacket(x, 1, 12, 0, data, -80); - test.CreateJFPacket(x, 1, 13, 0, data, 100); - test.CreateJFPacket(x, 1, 118, 0, data, 0); - - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - - REQUIRE(!test.Counters().IsAcquisitionFinished()); - - test.WaitForActionComplete(); - - REQUIRE(test.Counters().IsAcquisitionFinished()); - - REQUIRE(test.GetBytesReceived() == 6 * JUNGFRAU_PACKET_SIZE_BYTES); -} - - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_15_storage_cell_convert_G0", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 2; - const uint16_t ntrigger = 2; - const uint16_t nstoragecells = 15; - - DiffractionExperiment x(DetJF(nmodules)); - - x.PedestalG0Frames(0).NumTriggers(ntrigger).ImagesPerTrigger(nstoragecells).UseInternalPacketGenerator(true) - .IncidentEnergy_keV(10.0).StorageCells(nstoragecells); - - REQUIRE(x.GetImageNum() == ntrigger * nstoragecells); - - HLSSimulatedDevice test(0, ntrigger * nstoragecells * nmodules + 32); - - std::vector tmp(3 * RAW_MODULE_SIZE, 50); - JFModuleGainCalibration gain(tmp); - - std::vector data(RAW_MODULE_SIZE); - for (auto &i: data) - i = 16000; - - for (int m = 0; m < x.GetModulesNum(); m++) - test.SetInternalGeneratorFrame(data.data(), m); - - - JFCalibration c(x); - for (int i = 0; i < nstoragecells; i++) { - for (int j = 0; j < RAW_MODULE_SIZE; j++) { - c.Pedestal(0, 0, i).GetPedestal()[j] = (15 - i) * 500; - c.Pedestal(1, 0, i).GetPedestal()[j] = i * 1000; - } - } - c.GainCalibration(0) = gain; - c.GainCalibration(1) = gain; - - REQUIRE_NOTHROW(test.InitializeCalibration(x, c)); - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - - REQUIRE(test.GetBytesReceived() == ntrigger * nmodules * nstoragecells * 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - for (int i = 0; i < ntrigger * nstoragecells; i++) { - REQUIRE(test.GetDeviceOutput(i, 0)->pixels[511 * 764] == 32 - 15 + (i % nstoragecells)); - REQUIRE(test.GetDeviceOutput(i, 1)->pixels[200 * 145] == 32 - 2 * (i % nstoragecells)); - } -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_8_storage_cell_convert_G0", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - const uint16_t nmodules = 2; - const uint16_t ntrigger = 2; - const uint16_t nstoragecells = 8; - - DiffractionExperiment x(DetJF(nmodules)); - - x.PedestalG0Frames(0).NumTriggers(ntrigger).ImagesPerTrigger(nstoragecells).UseInternalPacketGenerator(true) - .IncidentEnergy_keV(10.0).StorageCells(nstoragecells); - - REQUIRE(x.GetImageNum() == ntrigger * nstoragecells); - - HLSSimulatedDevice test(0, ntrigger * nstoragecells * nmodules + 64); - - std::vector tmp(3 * RAW_MODULE_SIZE, 50); - JFModuleGainCalibration gain(tmp); - - std::vector data(RAW_MODULE_SIZE); - for (auto &i: data) - i = 16000; - - for (int m = 0; m < x.GetModulesNum(); m++) - test.SetInternalGeneratorFrame(data.data(), m); - - - JFCalibration c(x); - for (int i = 0; i < nstoragecells; i++) { - for (int j = 0; j < RAW_MODULE_SIZE; j++) { - c.Pedestal(0, 0, i).GetPedestal()[j] = (15 - i) * 500; - c.Pedestal(1, 0, i).GetPedestal()[j] = i * 1000; - } - } - c.GainCalibration(0) = gain; - c.GainCalibration(1) = gain; - - REQUIRE_NOTHROW(test.InitializeCalibration(x, c)); - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - - REQUIRE(test.GetBytesReceived() == ntrigger * nmodules * nstoragecells * 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - for (int i = 0; i < ntrigger * nstoragecells; i++) { - REQUIRE(test.GetDeviceOutput(i, 0)->pixels[511 * 764] == 32 - 15 + (i % nstoragecells)); - REQUIRE(test.GetDeviceOutput(i, 1)->pixels[200 * 145] == 32 - 2 * (i % nstoragecells)); - } -} - - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_16_storage_cell_convert_G0", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - const uint16_t nmodules = 2; - const uint16_t ntrigger = 4; - const uint16_t nstoragecells = 16; - - DiffractionExperiment x(DetJF(nmodules)); - - x.PedestalG0Frames(0).NumTriggers(ntrigger).ImagesPerTrigger(nstoragecells).UseInternalPacketGenerator(true) - .IncidentEnergy_keV(10.0).StorageCells(nstoragecells); - - REQUIRE(x.GetImageNum() == ntrigger * nstoragecells); - - HLSSimulatedDevice test(0, ntrigger * nstoragecells * nmodules + 128); - std::vector tmp(3 * RAW_MODULE_SIZE, 50); - JFModuleGainCalibration gain(tmp); - - std::vector data(RAW_MODULE_SIZE); - for (auto &i: data) - i = 16000; - - for (int m = 0; m < x.GetModulesNum(); m++) - test.SetInternalGeneratorFrame(data.data(), m); - - JFCalibration c(x); - for (int i = 0; i < nstoragecells; i++) { - for (int j = 0; j < RAW_MODULE_SIZE; j++) { - c.Pedestal(0, 0, i).GetPedestal()[j] = (15 - i) * 500; - c.Pedestal(1, 0, i).GetPedestal()[j] = i * 1000; - } - } - c.GainCalibration(0) = gain; - c.GainCalibration(1) = gain; - - REQUIRE_NOTHROW(test.InitializeCalibration(x, c)); - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - - REQUIRE(test.GetBytesReceived() == ntrigger * nmodules * nstoragecells * 128 * JUNGFRAU_PACKET_SIZE_BYTES); - - for (int i = 0; i < ntrigger * nstoragecells; i++) { - REQUIRE(test.GetDeviceOutput(i, 0)->pixels[511 * 764] == 32 - 15 + (i % nstoragecells)); - REQUIRE(test.GetDeviceOutput(i, 1)->pixels[200 * 145] == 32 - 2 * (i % nstoragecells)); - } -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_storage_cell_convert_G1", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 2; - DiffractionExperiment x(DetJF(nmodules)); - - x.PedestalG0Frames(0).NumTriggers(1).ImagesPerTrigger(16).UseInternalPacketGenerator(true) - .IncidentEnergy_keV(10.0).StorageCells(16); - - HLSSimulatedDevice test(0, 64); - - std::vector data(RAW_MODULE_SIZE); - for (auto &i: data) - i = 16384 | 10; - for (int m = 0; m < x.GetModulesNum(); m++) - test.SetInternalGeneratorFrame(data.data(), m); - - - JFCalibration c(x); - for (int i = 0; i < 16; i++) { - for (int j = 0; j < RAW_MODULE_SIZE; j++) { - c.Pedestal(0, 1, i).GetPedestal()[j] = (17 - i) * 10; - c.Pedestal(1, 1, i).GetPedestal()[j] = i * 20; - } - } - - std::vector tmp(3 * RAW_MODULE_SIZE, -1); - for (int i = 0; i < RAW_MODULE_SIZE; i++) - tmp[i] = DEFAULT_G0_FACTOR; - JFModuleGainCalibration gain(tmp); - c.GainCalibration(0) = gain; - c.GainCalibration(1) = gain; - - REQUIRE_NOTHROW(test.InitializeCalibration(x, c)); - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - - REQUIRE(test.GetBytesReceived() == 32*128*JUNGFRAU_PACKET_SIZE_BYTES); - - for (int i = 0; i < 16; i++) { - REQUIRE(test.GetDeviceOutput(i, 0)->pixels[511 * 764] == 17 - (i % 16) - 1); - REQUIRE(test.GetDeviceOutput(i, 1)->pixels[200 * 145] == 2 * (i % 16) - 1); - } -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_integration", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 4; - - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(1).PedestalG0Frames(0); - - HLSSimulatedDevice test(0, 64); - - std::vector frame(RAW_MODULE_SIZE); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) - frame[i] = 32754; - - for (int m = 0; m < x.GetModulesNum(); m++) - test.SetInternalGeneratorFrame(frame.data(), m); - - - std::vector integration_map(nmodules * RAW_MODULE_SIZE, 54); - for (int i = 0; i < RAW_MODULE_SIZE/2; i++) { - integration_map[2 * i] = 0; - integration_map[2 * i + 1] = FPGA_INTEGRATION_BIN_COUNT - 1; - } - std::vector weights(nmodules * RAW_MODULE_SIZE, 1.0); - integration_map[RAW_MODULE_SIZE - 1] = FPGA_INTEGRATION_BIN_COUNT; - test.InitializeIntegrationMap(x, integration_map, weights); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 128 * nmodules * JUNGFRAU_PACKET_SIZE_BYTES); - - auto imageBuf = test.GetDeviceOutput(0, 0)->pixels; - REQUIRE(memcmp(imageBuf, frame.data(), RAW_MODULE_SIZE * sizeof(uint16_t)) == 0); - - auto integration_result = test.GetDeviceOutput(0, 0)->integration_result; - CHECK(integration_result[0].sum == Catch::Approx(32754LU * (RAW_MODULE_SIZE / 2))); - CHECK(integration_result[0].count == RAW_MODULE_SIZE / 2); - CHECK(integration_result[1].sum == 0); - CHECK(integration_result[1].count == 0); - CHECK(integration_result[FPGA_INTEGRATION_BIN_COUNT - 1].sum == Catch::Approx(32754LU * (RAW_MODULE_SIZE / 2 - 1))); - CHECK(integration_result[FPGA_INTEGRATION_BIN_COUNT - 1].count == RAW_MODULE_SIZE / 2 - 1); - - integration_result = test.GetDeviceOutput(0, 1)->integration_result; - CHECK(integration_result[54].sum == Catch::Approx(32754LU * (RAW_MODULE_SIZE))); - CHECK(integration_result[54].count == RAW_MODULE_SIZE); - - integration_result = test.GetDeviceOutput(0, 2)->integration_result; - CHECK(integration_result[54].sum == Catch::Approx(32754 * (RAW_MODULE_SIZE))); - CHECK(integration_result[54].count == RAW_MODULE_SIZE); - - integration_result = test.GetDeviceOutput(0, 3)->integration_result; - CHECK(integration_result[54].sum == Catch::Approx(32754 * (RAW_MODULE_SIZE))); - CHECK(integration_result[54].count == RAW_MODULE_SIZE); -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_integration_mask", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 4; - - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(1).PedestalG0Frames(0).ApplyPixelMask(false); - - HLSSimulatedDevice test(0, 64); - - std::vector mask(RAW_MODULE_SIZE, 0); - mask[0] = 4; - - std::vector frame(RAW_MODULE_SIZE, 0); - frame[0] = 2; - frame[1] = 3; - - for (int m = 0; m < x.GetModulesNum(); m++) { - test.SetInternalGeneratorFrame(frame.data(), m); - test.InitializePixelMask(mask.data(), m); - } - - std::vector integration_map(nmodules * RAW_MODULE_SIZE, 1); - - std::vector weights(nmodules * RAW_MODULE_SIZE, 1.0); - integration_map[0] = 0; - integration_map[1] = 0; - - test.InitializeIntegrationMap(x, integration_map, weights); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 128 * nmodules * JUNGFRAU_PACKET_SIZE_BYTES); - - auto imageBuf = test.GetDeviceOutput(0, 0)->pixels; - REQUIRE(memcmp(imageBuf, frame.data(), RAW_MODULE_SIZE * sizeof(uint16_t)) == 0); - - auto integration_result = test.GetDeviceOutput(0, 0)->integration_result; - CHECK(integration_result[0].sum == 3); - CHECK(integration_result[0].count == 1); - CHECK(integration_result[1].sum == 0); - CHECK(integration_result[1].count == RAW_MODULE_SIZE - 2); -} - -bool spot_finder_output_contains(const SpotFindingResult& output, uint32_t number) { - size_t byte = number / 8; - size_t bit = number % 8; - return ((output.strong_pixel[byte] & (1 << bit)) != 0); -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_spot_finder_count_threshold", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 4; - - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(1).PedestalG0Frames(0); - - HLSSimulatedDevice test(0, 64); - - std::vector d_map(RAW_MODULE_SIZE, 3.0); - std::vector frame(RAW_MODULE_SIZE, 0); - frame [ 1*1024 + 1] = 11; - frame [123*1024 + 578] = 10; - frame [121*1024 + 800] = 12; - frame [ 89*1024 + 300] = 8; - frame [300*1024 + 0] = 9; - - for (int m = 0; m < x.GetModulesNum(); m++) { - test.SetInternalGeneratorFrame(frame.data(), m); - test.InitializeSpotFinderResolutionMap(d_map.data(), m); - } - - SpotFindingSettings parameters{ - .signal_to_noise_threshold = 0.0, - .photon_count_threshold = 9, - .min_pix_per_spot = 1, - .high_resolution_limit = 2.5, - .low_resolution_limit = 3.1 - }; - test.SetSpotFinderParameters(parameters); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 128 * nmodules * JUNGFRAU_PACKET_SIZE_BYTES); - - auto imageBuf = test.GetDeviceOutput(0, 0)->pixels; - REQUIRE(memcmp(imageBuf, frame.data(), RAW_MODULE_SIZE * sizeof(uint16_t)) == 0); - - auto spot_finder_result = test.GetDeviceOutput(0, 0)->spot_finding_result; - REQUIRE (spot_finder_result.strong_pixel_count == 3); - REQUIRE (spot_finder_result.snr_threshold == 0); - REQUIRE (spot_finder_result.count_threshold == 9); - - CHECK (spot_finder_output_contains(spot_finder_result, 1*1024 + 1)); - CHECK (spot_finder_output_contains(spot_finder_result, 123*1024 + 578)); - CHECK (spot_finder_output_contains(spot_finder_result, 121*1024 + 800)); -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_spot_finder_mask", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 4; - - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(1).PedestalG0Frames(0); - x.ApplyPixelMask(false); - - HLSSimulatedDevice test(0, 64); - - std::vector mask(RAW_MODULE_SIZE, 0); - mask[325*1024 + 463] = 4; - - std::vector frame(RAW_MODULE_SIZE, 0); - // Module boundary - frame [ 0*1024 + 0] = 50; - frame [ 5*1024 + 0] = 50; - frame [ 0*1024 + 5] = 50; - frame [ 511*1024 + 5] = 50; - - // Chip boundary - frame [ 345*1024 + 1023] = 50; - frame [ 346*1024 + 255] = 50; - frame [ 347*1024 + 256] = 50; - frame [ 348*1024 + 511] = 50; - frame [ 349*1024 + 512] = 50; - frame [ 350*1024 + 767] = 50; - frame [ 351*1024 + 768] = 50; - frame [ 255*1024 + 234] = 50; - frame [ 256*1024 + 876] = 50; - - // "Normal" pixel - frame [ 320*1024 + 400] = 50; - - // Masked pixel - frame [ 325*1024 + 463] = 50; - - std::vector d_map(RAW_MODULE_SIZE, 3.0); - - for (int m = 0; m < x.GetModulesNum(); m++) { - test.SetInternalGeneratorFrame(frame.data(), m); - test.InitializeSpotFinderResolutionMap(d_map.data(), m); - test.InitializePixelMask(mask.data(), m); - } - - SpotFindingSettings parameters{ - .signal_to_noise_threshold = 0.0, - .photon_count_threshold = 45, - .min_pix_per_spot = 1, - .high_resolution_limit = 2.0, - .low_resolution_limit = 4.0 - }; - test.SetSpotFinderParameters(parameters); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 128 * nmodules * JUNGFRAU_PACKET_SIZE_BYTES); - - auto imageBuf = test.GetDeviceOutput(0, 0)->pixels; - REQUIRE(memcmp(imageBuf, frame.data(), RAW_MODULE_SIZE * sizeof(uint16_t)) == 0); - - auto spot_finder_result = test.GetDeviceOutput(0, 0)->spot_finding_result; - REQUIRE (spot_finder_result.strong_pixel_count == 1); - REQUIRE (spot_finder_result.snr_threshold == 0); - REQUIRE (spot_finder_result.count_threshold == 45); - - CHECK (spot_finder_output_contains(spot_finder_result, 320*1024 + 400)); -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_spot_finder_overload", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 4; - - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(1).PedestalG0Frames(0).PixelSigned(true); - - HLSSimulatedDevice test(0, 64); - - std::vector frame(RAW_MODULE_SIZE, 0); - frame [ 132*1024 + 133] = INT16_MAX; - frame [ 0*1024 + 500] = INT16_MIN; - frame [ 255*1024 + 234] = INT16_MAX; - frame [ 300*1024 + 234] = INT16_MAX; - - std::vector d_map(RAW_MODULE_SIZE, 3.0); - d_map[ 132*1024 + 133] = 0.0; - for (int m = 0; m < x.GetModulesNum(); m++) { - test.SetInternalGeneratorFrame((uint16_t *) frame.data(), m); - test.InitializeSpotFinderResolutionMap(d_map.data(), m); - } - - SpotFindingSettings parameters{ - .signal_to_noise_threshold = 5.0, - .photon_count_threshold = 45, - .min_pix_per_spot = 1, - .high_resolution_limit = 2.0, - .low_resolution_limit = 4.0 - }; - test.SetSpotFinderParameters(parameters); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 128 * nmodules * JUNGFRAU_PACKET_SIZE_BYTES); - - auto imageBuf = test.GetDeviceOutput(0, 0)->pixels; - REQUIRE(memcmp(imageBuf, frame.data(), RAW_MODULE_SIZE * sizeof(uint16_t)) == 0); - - auto spot_finder_result = test.GetDeviceOutput(0, 0)->spot_finding_result; - REQUIRE (spot_finder_result.strong_pixel_count == 1); - REQUIRE (spot_finder_result.snr_threshold == 5.0); - REQUIRE (spot_finder_result.count_threshold == 45); - - CHECK (spot_finder_output_contains(spot_finder_result, 300*1024 + 234)); -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_spot_finder_min_pix_per_spot", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 4; - - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(1).PedestalG0Frames(0); - - HLSSimulatedDevice test(0, 64); - - std::vector d_map(RAW_MODULE_SIZE, 3.0); - - std::vector frame(RAW_MODULE_SIZE, 0); - frame [1*1024 + 1] = 11; - frame [1*1024 + 2] = 10; - frame [5*1024 + 7] = 12; - frame [6*1024 + 7] = 12; - - frame [ 89*1024+154] = 12; - - frame [ 99*1024+200] = 12; - frame [100*1024+200] = 11; - frame [100*1024+201] = 10; - frame [101*1024+200] = 12; - frame [RAW_MODULE_SIZE - 1 - 1024] = 20; - - for (int m = 0; m < x.GetModulesNum(); m++) { - test.InitializeSpotFinderResolutionMap(d_map.data(), m); - test.SetInternalGeneratorFrame(frame.data(), m); - } - - SpotFindingSettings parameters{ - .signal_to_noise_threshold = 0.0, - .photon_count_threshold = 9, - .min_pix_per_spot = 2, - .high_resolution_limit = 2.0, - .low_resolution_limit = 4.0 - }; - test.SetSpotFinderParameters(parameters); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 128 * nmodules * JUNGFRAU_PACKET_SIZE_BYTES); - - auto imageBuf = test.GetDeviceOutput(0, 0)->pixels; - REQUIRE(memcmp(imageBuf, frame.data(), RAW_MODULE_SIZE * sizeof(uint16_t)) == 0); - - auto spot_finder_result = test.GetDeviceOutput(0, 0)->spot_finding_result; - REQUIRE (spot_finder_result.strong_pixel_count == 8); - REQUIRE (spot_finder_result.snr_threshold == 0); - REQUIRE (spot_finder_result.count_threshold == 9); - - CHECK (spot_finder_output_contains(spot_finder_result, 1*1024 + 1)); - CHECK (spot_finder_output_contains(spot_finder_result, 1*1024 + 2)); - CHECK (spot_finder_output_contains(spot_finder_result, 5*1024 + 7)); - CHECK (!spot_finder_output_contains(spot_finder_result, RAW_MODULE_SIZE-1-1024)); - CHECK (!spot_finder_output_contains(spot_finder_result, 89*1024+154)); -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_spot_finder_d_min_max", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 4; - - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(1).PedestalG0Frames(0); - - HLSSimulatedDevice test(0, 64); - - std::vector d_map(RAW_MODULE_SIZE, 0.0); - d_map[1024+1] = 2.5; - d_map[1024+2] = 4.0; - d_map[1024+3] = 1.0; - d_map[1024+4] = 1.5; - - std::vector frame(RAW_MODULE_SIZE, 0); - frame [1024+1] = 11; - frame [1024+2] = 10; - frame [1024+3] = 12; - frame [1024+4] = 12; - - test.InitializeSpotFinderResolutionMap(d_map.data(), 0); - for (int m = 0; m < x.GetModulesNum(); m++) { - test.SetInternalGeneratorFrame(frame.data(), m); - test.InitializeSpotFinderResolutionMap(d_map.data(), m); - } - - SpotFindingSettings parameters{ - .signal_to_noise_threshold = 0.0, - .photon_count_threshold = 9, - .min_pix_per_spot = 1, - .high_resolution_limit = 1.25, - .low_resolution_limit = 3.0 - }; - test.SetSpotFinderParameters(parameters); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 128 * nmodules * JUNGFRAU_PACKET_SIZE_BYTES); - - auto imageBuf = test.GetDeviceOutput(0, 0)->pixels; - REQUIRE(memcmp(imageBuf, frame.data(), RAW_MODULE_SIZE * sizeof(uint16_t)) == 0); - - auto spot_finder_result = test.GetDeviceOutput(0, 0)->spot_finding_result; - REQUIRE (spot_finder_result.strong_pixel_count == 2); - REQUIRE (spot_finder_result.snr_threshold == 0); - REQUIRE (spot_finder_result.count_threshold == 9); - - CHECK (spot_finder_output_contains(spot_finder_result, 1024+1)); - CHECK (spot_finder_output_contains(spot_finder_result, 1024+4)); -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_spot_finder_snr_threshold", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 4; - - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(1).PedestalG0Frames(0); - - HLSSimulatedDevice test(0, 64); - - std::vector d_map(RAW_MODULE_SIZE, 3.0); - std::vector frame(RAW_MODULE_SIZE); - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - frame[i] = ((i / RAW_MODULE_COLS) + (i % RAW_MODULE_COLS)) % 2; - } - // Mean = 0.5 - // Std. dev. = 0.5 - // Threshold = 10 * std. dev. - 6 is minimum count - - frame [ 1*1024 + 1] = 8; - frame [123*1024 + 578] = 5; - frame [121*1024 + 800] = 4; - frame [ 89*1024 + 300] = 7; - frame [300*1024 + 5] = 3; - - for (int m = 0; m < x.GetModulesNum(); m++) { - test.SetInternalGeneratorFrame(frame.data(), m); - test.InitializeSpotFinderResolutionMap(d_map.data(), m); - } - - SpotFindingSettings parameters{ - .signal_to_noise_threshold = 10.0, - .photon_count_threshold = 0, - .min_pix_per_spot = 1, - .high_resolution_limit = 2.0, - .low_resolution_limit = 3.1 - }; - test.SetSpotFinderParameters(parameters); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 128 * nmodules * JUNGFRAU_PACKET_SIZE_BYTES); - - auto imageBuf = test.GetDeviceOutput(0, 0)->pixels; - REQUIRE(memcmp(imageBuf, frame.data(), RAW_MODULE_SIZE * sizeof(uint16_t)) == 0); - - auto spot_finder_result = test.GetDeviceOutput(0, 0)->spot_finding_result; - REQUIRE (spot_finder_result.strong_pixel_count == 2); - REQUIRE (spot_finder_result.snr_threshold == 10.0); - REQUIRE (spot_finder_result.count_threshold == 0); - - - REQUIRE (spot_finder_output_contains(spot_finder_result, 1*1024+1)); - REQUIRE (spot_finder_output_contains(spot_finder_result, 89*1024 + 300)); -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_pixel_threshold_summation", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 4; - - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(1).PedestalG0Frames(0).Summation(4) - .PixelValueLowThreshold(4).AutoSummation(true).PixelSigned(true) - .PixelValueHighThreshold(500); - - HLSSimulatedDevice test(0, 64); - - std::vector frame(RAW_MODULE_SIZE, 0); - frame [1024+1] = 1; - frame [1024+2] = 3; - frame [1024+3] = 4; - frame [1024+4] = 5; - frame [1024+5] = 100; - frame [1024+6] = 501; - - for (int m = 0; m < x.GetModulesNum(); m++) - test.SetInternalGeneratorFrame(frame.data(), m); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 4 * 128 * nmodules * JUNGFRAU_PACKET_SIZE_BYTES); - - auto imageBuf = test.GetDeviceOutput(0, 0)->pixels; - REQUIRE(imageBuf[1024+1] == 0); - REQUIRE(imageBuf[1024+2] == 0); - REQUIRE(imageBuf[1024+3] == 4*4); - REQUIRE(imageBuf[1024+4] == 4*5); - REQUIRE(imageBuf[1024+5] == 4*100); - REQUIRE(imageBuf[1024+6] == INT16_MAX); - - CHECK(test.GetDeviceOutput(0, 0)->module_statistics.pixel_sum == 4 * (4+5+100)); - CHECK(test.GetDeviceOutput(0,0)->module_statistics.max_value == 4*100); -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_summation_mask", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 4; - - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(1).PedestalG0Frames(0).Summation(4) - .AutoSummation(true).ApplyPixelMask(true).BitDepthImage(16).PixelSigned(true); - - HLSSimulatedDevice test(0, 64); - - std::vector frame(RAW_MODULE_SIZE, 0); - frame [1024+1] = 1; - frame [1024+2] = 3; - frame [1024+3] = 4; - frame [1024+4] = 5; - frame [1024+5] = 100; - frame [1024+6] = -20; - - std::vector pixel_mask(RAW_MODULE_SIZE, 0); - pixel_mask[1024+2] = 1; - - std::vector roi_map(RAW_MODULE_SIZE, 0); - roi_map[1024+1] = 1; - roi_map[1024+2] = 2; - roi_map[1024+5] = 4; - - for (int m = 0; m < x.GetModulesNum(); m++) { - test.InitializePixelMask(pixel_mask.data(), m); - test.SetInternalGeneratorFrame((uint16_t *) frame.data(), m); - test.InitializeROIMap(roi_map.data(), m); - } - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 4 * 128 * nmodules * JUNGFRAU_PACKET_SIZE_BYTES); - - auto imageBuf = test.GetDeviceOutput(0, 0)->pixels; - REQUIRE(imageBuf[1024+1] == 4 * 1); - REQUIRE(imageBuf[1024+2] == INT16_MIN); - REQUIRE(imageBuf[1024+3] == 4*4); - REQUIRE(imageBuf[1024+4] == 4*5); - REQUIRE(imageBuf[1024+5] == 4*100); - REQUIRE(imageBuf[1024+6] == -4*20); - - CHECK(test.GetDeviceOutput(0, 0)->module_statistics.pixel_sum == 4 * (1+4+5+100-20)); - CHECK(test.GetDeviceOutput(0,0)->module_statistics.max_value == 4*100); - CHECK(test.GetDeviceOutput(0, 0)->module_statistics.min_value == -4 * 20); - CHECK(test.GetDeviceOutput(0, 0)->roi_counts[0].good_pixels == 1); - CHECK(test.GetDeviceOutput(0, 0)->roi_counts[1].good_pixels == 0); - CHECK(test.GetDeviceOutput(0, 0)->roi_counts[2].good_pixels == 1); - CHECK(test.GetDeviceOutput(0, 0)->roi_counts[2].sum == 4*100); - CHECK(test.GetDeviceOutput(0, 0)->roi_counts[3].good_pixels == 0); -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_32bit", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 1; - const size_t nframes = 2; - DiffractionExperiment x(DetJF(nmodules)); - - std::vector test_frame(nmodules*RAW_MODULE_SIZE); - - std::mt19937 g1(1387); - std::uniform_int_distribution dist(0, UINT16_MAX - 1); - - for (auto &i: test_frame) - i = dist(g1); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(nframes).PedestalG0Frames(0).PixelSigned(false).BitDepthImage(32); - - HLSSimulatedDevice test(0, 64); - for (int m = 0; m < x.GetModulesNum(); m++) - test.SetInternalGeneratorFrame(test_frame.data() + m * RAW_MODULE_SIZE, m); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 128 * nmodules * nframes * JUNGFRAU_PACKET_SIZE_BYTES); - - auto test_frame_unsigned = (uint16_t *) test_frame.data(); - - for (int image = 0; image < nframes; image++) { - for (int m = 0; m < nmodules; m++) { - CHECK(test.GetDeviceOutput(image, m)->module_statistics.frame_number == image); - CHECK(test.GetDeviceOutput(image, m)->module_statistics.packet_count == 512); - CHECK(test.GetDeviceOutput(image, m)->module_statistics.packet_mask[0] == UINT64_MAX); - CHECK(test.GetDeviceOutput(image, m)->module_statistics.packet_mask[1] == UINT64_MAX); - CHECK(test.GetDeviceOutput(image, m)->module_statistics.packet_mask[2] == UINT64_MAX); - CHECK(test.GetDeviceOutput(image, m)->module_statistics.packet_mask[3] == UINT64_MAX); - CHECK(test.GetDeviceOutput(image, m)->module_statistics.timestamp == INT_PKT_GEN_EXPTTIME * image); - CHECK(test.GetDeviceOutput(image, m)->module_statistics.pulse_id == INT_PKT_GEN_BUNCHID + image); - CHECK(test.GetDeviceOutput(image, m)->module_statistics.exptime == INT_PKT_GEN_EXPTTIME); - - auto imageBuf = (uint32_t *) test.GetDeviceOutput(image, m)->pixels; - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - REQUIRE(imageBuf[i] == test_frame_unsigned[m * RAW_MODULE_SIZE + i]); - } - } - } -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_summation", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 1; - const size_t nframes = 16; - const size_t nsummation = 16; - DiffractionExperiment x(DetJF(nmodules)); - - std::vector test_frame(nmodules*RAW_MODULE_SIZE); - - std::mt19937 g1(1387); - std::uniform_int_distribution dist(0, UINT16_MAX); - - for (auto &i: test_frame) - i = dist(g1); - - x.UseInternalPacketGenerator(true).ImagesPerTrigger(nframes).PedestalG0Frames(0).Summation(nsummation) - .JungfrauConvPhotonCnt(false).PixelSigned(true).BitDepthImage(32); - - HLSSimulatedDevice test(0, 64); - for (int m = 0; m < x.GetModulesNum(); m++) - test.SetInternalGeneratorFrame(test_frame.data() + m * RAW_MODULE_SIZE, m); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 128 * nmodules * nframes * nsummation * JUNGFRAU_PACKET_SIZE_BYTES); - - auto test_frame_signed = (int16_t *) test_frame.data(); - for (int image = 0; image < nframes; image++) { - for (int m = 0; m < nmodules; m++) { - REQUIRE(test.GetDeviceOutput(image, m)->module_statistics.timestamp == INT_PKT_GEN_EXPTTIME * image * nsummation); - REQUIRE(test.GetDeviceOutput(image, m)->module_statistics.pulse_id == INT_PKT_GEN_BUNCHID + image * nsummation); - REQUIRE(test.GetDeviceOutput(image, m)->module_statistics.exptime == INT_PKT_GEN_EXPTTIME); - - auto imageBuf = (int32_t *) test.GetDeviceOutput(image, m)->pixels; - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - if (test_frame_signed[m * RAW_MODULE_SIZE + i] == INT16_MAX) - CHECK(imageBuf[i] == INT32_MAX); - else if (test_frame_signed[m * RAW_MODULE_SIZE + i] == INT16_MIN) - CHECK(imageBuf[i] == INT32_MIN); - else - REQUIRE(imageBuf[i] == test_frame_signed[m * RAW_MODULE_SIZE + i] * nsummation); - } - } - } -} - -TEST_CASE("HLS_C_Simulation_count_sat_and_err_pixels", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 3; - - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(4).PedestalG0Frames(0).PixelSigned(true); - - HLSSimulatedDevice test(0, 64); - - std::vector v(RAW_MODULE_SIZE, 0); - v[567] = INT16_MAX; - v[RAW_MODULE_COLS * 511 + 255] = INT16_MAX; - v[RAW_MODULE_COLS * 250 + 233] = INT16_MIN; - v[0] = INT16_MIN; - v[RAW_MODULE_SIZE-1] = INT16_MIN; - v[23] = 23; - v[24] = -24; - v[25] = 27; - v[1500] = -25; - v[1501] = 30; - v[1502] = INT16_MIN + 25; - for (int m = 0; m < x.GetModulesNum(); m++) - test.SetInternalGeneratorFrame((uint16_t *) v.data(), m); - - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - REQUIRE(test.GetBytesReceived() == 128 * nmodules * 4 * JUNGFRAU_PACKET_SIZE_BYTES); - - for (int image = 0; image < 4; image++) { - for (int m = 0; m < nmodules; m++) { - CHECK(test.GetDeviceOutput(image, m)->module_statistics.saturated_pixels == 2); - CHECK(test.GetDeviceOutput(image, m)->module_statistics.err_pixels == 3); - CHECK(test.GetDeviceOutput(image, m)->module_statistics.max_value == 30); - CHECK(test.GetDeviceOutput(image, m)->module_statistics.min_value == INT16_MIN + 25); - } - } -} - -TEST_CASE("HLS_C_Simulation_pixel_count_mask", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 3; - - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(4).PedestalG0Frames(0).PixelSigned(true); - x.ApplyPixelMask(false); - - HLSSimulatedDevice test(0, 64); - std::vector pixel_mask(RAW_MODULE_SIZE, 0); - pixel_mask[0] = 2; - pixel_mask[1] = 4; - pixel_mask[2] = 1<<30; - test.InitializePixelMask(pixel_mask.data(), 1); - - std::vector v(RAW_MODULE_SIZE, 0); - v[0] = INT16_MAX; - v[1] = INT16_MIN; - v[2] = 35; - v[3] = 234; - - for (int m = 0; m < x.GetModulesNum(); m++) - test.SetInternalGeneratorFrame((uint16_t *) v.data(), m); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - REQUIRE(test.GetBytesReceived() == 128 * nmodules * 4 * JUNGFRAU_PACKET_SIZE_BYTES); - - for (int image = 0; image < 4; image++) { - CHECK(test.GetDeviceOutput(image, 1)->module_statistics.saturated_pixels == 0); - CHECK(test.GetDeviceOutput(image, 1)->module_statistics.err_pixels == 0); - CHECK(test.GetDeviceOutput(image, 1)->module_statistics.max_value == 234); - CHECK(test.GetDeviceOutput(image, 1)->module_statistics.min_value == 0); - CHECK(test.GetDeviceOutput(image, 1)->module_statistics.masked_pixels == 3); - CHECK(test.GetDeviceOutput(image, 1)->module_statistics.pixel_sum == 234); - CHECK(test.GetDeviceOutput(image, 1)->pixels[2] == 35); - - CHECK(test.GetDeviceOutput(image, 0)->module_statistics.saturated_pixels == 1); - CHECK(test.GetDeviceOutput(image, 0)->module_statistics.err_pixels == 1); - CHECK(test.GetDeviceOutput(image, 0)->module_statistics.max_value == 234); - CHECK(test.GetDeviceOutput(image, 0)->module_statistics.min_value == 0); - CHECK(test.GetDeviceOutput(image, 0)->module_statistics.masked_pixels == 0); - CHECK(test.GetDeviceOutput(image, 0)->module_statistics.pixel_sum == 35 + 234); - } -} - -TEST_CASE("HLS_C_Simulation_count_pixel_sum", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 3; - - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(4).PedestalG0Frames(0).PixelSigned(true); - - HLSSimulatedDevice test(0, 64); - - std::vector v_0(RAW_MODULE_SIZE, 123); - std::vector v_1(RAW_MODULE_SIZE, 34); - v_1[0] = INT16_MAX; - v_1[1] = INT16_MIN; - v_1[2] = 0; - v_1[RAW_MODULE_SIZE-1] = -1234; - std::vector v_2(RAW_MODULE_SIZE, -23); - - test.SetInternalGeneratorFrame((uint16_t *) v_0.data(), 0); - test.SetInternalGeneratorFrame((uint16_t *) v_1.data(), 1); - test.SetInternalGeneratorFrame((uint16_t *) v_2.data(), 2); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - REQUIRE(test.GetBytesReceived() == 128 * nmodules * 4 * JUNGFRAU_PACKET_SIZE_BYTES); - - for (int image = 0; image < 4; image++) { - CHECK(test.GetDeviceOutput(image, 0)->module_statistics.pixel_sum == 123 * RAW_MODULE_SIZE); - CHECK(test.GetDeviceOutput(image, 1)->module_statistics.pixel_sum == 34 * (RAW_MODULE_SIZE - 4) - 1234); - CHECK(test.GetDeviceOutput(image, 2)->module_statistics.pixel_sum == -23 * RAW_MODULE_SIZE); - } -} - -TEST_CASE("HLS_C_Simulation_check_bunchid", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint64_t bunchid = 0xAAAAAAAA87345600; - const uint16_t nmodules = 4; - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw(); - x.PedestalG0Frames(0).ImagesPerTrigger(3).NumTriggers(1); - HLSSimulatedDevice test(0, 64); - test.CreateXfelBunchIDPacket(bunchid, 0); - test.CreateFinalPacket(x); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - - REQUIRE(test.GetBytesReceived() == 0); - REQUIRE(test.GetDataCollectionStatus().current_pulseid == bunchid); -} - -TEST_CASE("HLS_C_Simulation_check_raw_eiger", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector raw_frame(RAW_MODULE_SIZE); - std::vector raw_frame_eiger(RAW_MODULE_SIZE); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) - raw_frame[i] = i % (UINT16_MAX - 11); - - RawToEigerInput<4>(raw_frame_eiger.data(), raw_frame.data()); - const uint16_t nmodules = 1; - DetectorSetup detector(DetEIGER(nmodules)); - DiffractionExperiment x(detector); - - x.Raw(); - x.PedestalG0Frames(0).ImagesPerTrigger(1).NumTriggers(1); - - HLSSimulatedDevice test(0, 64); - - // bottom half module is reversed - for (int i = 0; i < 64; i++) - test.CreateEIGERPacket(x, 1, i, 0, 0, 0, raw_frame_eiger.data() + 2048 * (2 * i)); - for (int i = 0; i < 64; i++) - test.CreateEIGERPacket(x, 1, i, 0, 1, 0, raw_frame_eiger.data() + 2048 * (1 + 2 * i)); - - for (int i = 0; i < 64; i++) - test.CreateEIGERPacket(x, 1, i, 0, 0, 1, raw_frame_eiger.data() + 2048 * (128 + 2 * i)); - for (int i = 0; i < 64; i++) - test.CreateEIGERPacket(x, 1, i, 0, 1, 1, raw_frame_eiger.data() + 2048 * (129 + 2 * i)); - - test.CreateFinalPacket(x); - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.Counters().GetSlowestFrameNumber() == 0); - - - REQUIRE(test.GetBytesReceived() == 256 * 4096); - - uint64_t diffs = 0; - for (int j = 0; j < RAW_MODULE_SIZE; j++) { - if (raw_frame[j] != ((uint16_t *) test.GetDeviceOutput(0, 0)->pixels)[j]) - diffs++; - } - - REQUIRE(diffs == 0); -} - -TEST_CASE("HLS_C_Simulation_check_raw_eiger_8bit", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector raw_frame(RAW_MODULE_SIZE); - std::vector raw_frame_eiger(RAW_MODULE_SIZE); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) - raw_frame[i] = i; - - const uint16_t nmodules = 1; - - DetectorSetup detector(DetEIGER(nmodules)); - RawToEigerInput<8>(raw_frame_eiger.data(), raw_frame.data()); - - DiffractionExperiment x(detector); - - x.Raw(); - x.PedestalG0Frames(0).ImagesPerTrigger(1).NumTriggers(1).EigerBitDepth(8).BitDepthImage(8); - - REQUIRE(x.GetByteDepthImage() == 1); - - HLSSimulatedDevice test(0, 64); - - // bottom half module is reversed - for (int i = 0; i < 32; i++) - test.CreateEIGERPacket(x, 1, i, 0, 0, 0, reinterpret_cast(raw_frame_eiger.data() + 4096 * (2 * i))); - for (int i = 0; i < 32; i++) - test.CreateEIGERPacket(x, 1, i, 0, 1, 0, reinterpret_cast(raw_frame_eiger.data() + 4096 * (2 * i + 1))); - - for (int i = 0; i < 32; i++) - test.CreateEIGERPacket(x, 1, i, 0, 0, 1, reinterpret_cast(raw_frame_eiger.data() + 4096 * (2 * i + 64))); - for (int i = 0; i < 32; i++) - test.CreateEIGERPacket(x, 1, i, 0, 1, 1, reinterpret_cast(raw_frame_eiger.data() + 4096 * (2 * i + 65))); - - test.CreateFinalPacket(x); - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.Counters().GetSlowestFrameNumber() == 0); - - REQUIRE(test.GetBytesReceived() == 128 * 4096); - - auto out = (uint8_t *) test.GetDeviceOutput(0, 0)->pixels; - - CHECK(test.GetDeviceOutput(0,0)->module_statistics.packet_mask[0] == UINT64_MAX); - CHECK(test.GetDeviceOutput(0,0)->module_statistics.packet_mask[1] == UINT64_MAX); - CHECK(test.GetDeviceOutput(0,0)->module_statistics.packet_mask[2] == UINT64_MAX); - CHECK(test.GetDeviceOutput(0,0)->module_statistics.packet_mask[3] == UINT64_MAX); - CHECK(test.GetDeviceOutput(0,0)->module_statistics.packet_mask[4] == UINT64_MAX); - CHECK(test.GetDeviceOutput(0,0)->module_statistics.packet_mask[5] == UINT64_MAX); - CHECK(test.GetDeviceOutput(0,0)->module_statistics.packet_mask[6] == UINT64_MAX); - CHECK(test.GetDeviceOutput(0,0)->module_statistics.packet_mask[7] == UINT64_MAX); - - uint64_t diffs = 0; - for (int j = 0; j < RAW_MODULE_SIZE; j++) { - if (raw_frame[j] != out[j]) - diffs++; - } - - REQUIRE(diffs == 0); -} - -TEST_CASE("HLS_C_Simulation_check_raw_eiger_32bit", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector raw_frame(RAW_MODULE_SIZE); - std::vector raw_frame_eiger(RAW_MODULE_SIZE); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) - raw_frame[i] = i; - - const uint16_t nmodules = 1; - - DetectorSetup detector(DetEIGER(nmodules)); - RawToEigerInput<2>(raw_frame_eiger.data(), raw_frame.data()); - - DiffractionExperiment x(detector); - - x.Raw(); - x.PedestalG0Frames(0).ImagesPerTrigger(1).NumTriggers(1).EigerBitDepth(32).BitDepthImage(32); - - REQUIRE(x.GetByteDepthImage() == 4); - - HLSSimulatedDevice test(0, 64); - - // bottom half module is reversed - for (int i = 0; i < 128; i++) - test.CreateEIGERPacket(x, 1, i, 0, 0, 0, reinterpret_cast(raw_frame_eiger.data() + 1024 * (2 * i))); - for (int i = 0; i < 128; i++) - test.CreateEIGERPacket(x, 1, i, 0, 1, 0, reinterpret_cast(raw_frame_eiger.data() + 1024 * (2 * i + 1))); - - for (int i = 0; i < 128; i++) - test.CreateEIGERPacket(x, 1, i, 0, 0, 1, reinterpret_cast(raw_frame_eiger.data() + 1024 * (2 * i + 256))); - for (int i = 0; i < 128; i++) - test.CreateEIGERPacket(x, 1, i, 0, 1, 1, reinterpret_cast(raw_frame_eiger.data() + 1024 * (2 * i + 257))); - - test.CreateFinalPacket(x); - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.Counters().GetSlowestFrameNumber() == 0); - - REQUIRE(test.GetBytesReceived() == 512 * 4096); - - auto out = (uint32_t *) test.GetDeviceOutput(0, 0)->pixels; - - CHECK(test.GetDeviceOutput(0,0)->module_statistics.packet_mask[0] == UINT64_MAX); - CHECK(test.GetDeviceOutput(0,0)->module_statistics.packet_mask[1] == UINT64_MAX); - CHECK(test.GetDeviceOutput(0,0)->module_statistics.packet_mask[2] == UINT64_MAX); - CHECK(test.GetDeviceOutput(0,0)->module_statistics.packet_mask[3] == UINT64_MAX); - CHECK(test.GetDeviceOutput(0,0)->module_statistics.packet_mask[4] == UINT64_MAX); - CHECK(test.GetDeviceOutput(0,0)->module_statistics.packet_mask[5] == UINT64_MAX); - CHECK(test.GetDeviceOutput(0,0)->module_statistics.packet_mask[6] == UINT64_MAX); - CHECK(test.GetDeviceOutput(0,0)->module_statistics.packet_mask[7] == UINT64_MAX); - - uint64_t diffs = 0; - for (int j = 0; j < RAW_MODULE_SIZE; j++) { - if (raw_frame[j] != out[j]) - diffs++; - } - - REQUIRE(diffs == 0); -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_eiger", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector raw_frame(RAW_MODULE_SIZE), raw_frame_eiger(RAW_MODULE_SIZE); - std::mt19937 g1(1387); - std::uniform_int_distribution dist(0, 65535); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) - raw_frame[i] = dist(g1); - - RawToEigerInput<4>(raw_frame_eiger.data(), raw_frame.data()); - - const uint16_t nmodules = 4; - - DiffractionExperiment x(DetEIGER(nmodules)); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(4).PedestalG0Frames(0); - - HLSSimulatedDevice test(0, 64); - for (int m = 0; m < x.GetModulesNum(); m++) - test.SetInternalGeneratorFrame(raw_frame_eiger.data(), m); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - - REQUIRE(test.GetBytesReceived() == 128 * nmodules * 4 * JUNGFRAU_PACKET_SIZE_BYTES); - - for (int image = 0; image < 4; image++) { - for (int m = 0; m < nmodules; m++) { - auto imageBuf = (uint16_t *) test.GetDeviceOutput(image, m)->pixels; - for (int i = 0; i < RAW_MODULE_SIZE; i++) - REQUIRE(imageBuf[i] == raw_frame[i]); - } - } - REQUIRE(test.GetExpectedDescriptorsPerModule() == DMA_DESCRIPTORS_PER_MODULE); -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_eiger_8bit", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - std::vector raw_frame(RAW_MODULE_SIZE*2), raw_frame_eiger(RAW_MODULE_SIZE*2); - std::mt19937 g1(1387); - std::uniform_int_distribution dist(0, 254); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) - raw_frame[i] = dist(g1); - - RawToEigerInput<8>(raw_frame_eiger.data(), raw_frame.data()); - - const uint16_t nmodules = 4; - - DiffractionExperiment x(DetEIGER(nmodules)); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(4).PedestalG0Frames(0); - x.EigerBitDepth(8); - - REQUIRE(x.GetBitDepthReadout() == 8); - REQUIRE(x.GetByteDepthImage() == 2); - - HLSSimulatedDevice test(0, 64); - for (int m = 0; m < x.GetModulesNum(); m++) - test.SetInternalGeneratorFrame(reinterpret_cast(raw_frame_eiger.data()), m); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 128 * nmodules * 2 * JUNGFRAU_PACKET_SIZE_BYTES); - - for (int image = 0; image < 4; image++) { - for (int m = 0; m < nmodules; m++) { - auto imageBuf = test.GetDeviceOutput(image, m)->pixels; - size_t diff = 0; - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - if(imageBuf[i] != raw_frame.data()[i]) diff++; - } - REQUIRE(diff == 0); - } - } - REQUIRE(test.GetExpectedDescriptorsPerModule() == DMA_DESCRIPTORS_PER_MODULE); -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_eiger_32bit", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 4; - std::vector raw_frame(RAW_MODULE_SIZE), raw_frame_eiger(RAW_MODULE_SIZE); - std::mt19937 g1(1387); - std::uniform_int_distribution dist(0, 4*UINT16_MAX); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) - raw_frame[i] = dist(g1); - - RawToEigerInput<2>(raw_frame_eiger.data(), raw_frame.data()); - - DiffractionExperiment x(DetEIGER(nmodules)); - - x.UseInternalPacketGenerator(true).ImagesPerTrigger(4).PedestalG0Frames(0); - x.EigerBitDepth(32); - - HLSSimulatedDevice test(0, 64); - for (int m = 0; m < x.GetModulesNum(); m++) { - test.SetInternalGeneratorFrame(reinterpret_cast(raw_frame_eiger.data()), - 2 * m); - test.SetInternalGeneratorFrame(reinterpret_cast(raw_frame_eiger.data()) + RAW_MODULE_SIZE, - 2 * m + 1); - } - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 512 * nmodules * 2 * JUNGFRAU_PACKET_SIZE_BYTES); - - for (int image = 0; image < 4; image++) { - for (int m = 0; m < nmodules; m++) { - auto imageBuf = reinterpret_cast(test.GetDeviceOutput(image, m)->pixels); - size_t diff = 0; - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - if(imageBuf[i] != raw_frame.data()[i]) diff++; - } - REQUIRE(diff == 0); - } - } - REQUIRE(test.GetExpectedDescriptorsPerModule() == DMA_DESCRIPTORS_PER_MODULE); -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_roi_calc", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 4; - const size_t nimages = 4; - - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(nimages).PedestalG0Frames(0); - - HLSSimulatedDevice test(0, 64); - - std::vector frame(RAW_MODULE_SIZE); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) - frame[i] = 135; - - frame[12+RAW_MODULE_COLS*3] = 83; - frame[13+RAW_MODULE_COLS*3] = 95; - frame[12+RAW_MODULE_COLS*4] = 13; - frame[13+RAW_MODULE_COLS*4] = 125; - - frame[812+RAW_MODULE_COLS*303] = 83; - frame[813+RAW_MODULE_COLS*303] = 95; - frame[812+RAW_MODULE_COLS*304] = 13; - frame[813+RAW_MODULE_COLS*304] = 125; - - std::vector roi_map(RAW_MODULE_SIZE, 0); - roi_map[12+RAW_MODULE_COLS*3] = 1; - roi_map[13+RAW_MODULE_COLS*3] = 1; - roi_map[12+RAW_MODULE_COLS*4] = 1; - roi_map[13+RAW_MODULE_COLS*4] = 1; - - roi_map[812+RAW_MODULE_COLS*303] = (1<<5); - roi_map[813+RAW_MODULE_COLS*303] = (1<<5); - roi_map[812+RAW_MODULE_COLS*304] = (1<<5); - roi_map[813+RAW_MODULE_COLS*304] = (1<<5); - - for (int m = 0; m < x.GetModulesNum(); m++) { - test.SetInternalGeneratorFrame(frame.data(), m); - test.InitializeROIMap(roi_map.data(), m); - } - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 128 * nimages * nmodules * JUNGFRAU_PACKET_SIZE_BYTES); - - auto imageBuf = test.GetDeviceOutput(0, 0)->pixels; - REQUIRE(memcmp(imageBuf, frame.data(), RAW_MODULE_SIZE * sizeof(uint16_t)) == 0); - - for (int i = 0; i < x.GetImageNum(); i++) { - for (int m = 0; m < x.GetModulesNum(); m++) { - CHECK (test.GetDeviceOutput(i, m)->roi_counts[0].sum == 83 + 95 + 13 + 125); - CHECK (test.GetDeviceOutput(i, m)->roi_counts[0].sum2 == 83 * 83 + 95 * 95 + 13 * 13 + 125 * 125); - CHECK (test.GetDeviceOutput(i, m)->roi_counts[0].sum_x_weighted == Catch::Approx(83 * 12 + 95 * 13 + 13 * 12 + 125 * 13)); - CHECK (test.GetDeviceOutput(i, m)->roi_counts[0].sum_y_weighted == Catch::Approx(83 * 3 + 95 * 3 + 13 * 4 + 125 * 4)); - CHECK (test.GetDeviceOutput(i, m)->roi_counts[0].max_value == 125); - CHECK (test.GetDeviceOutput(i, m)->roi_counts[0].good_pixels == 4); - - CHECK (test.GetDeviceOutput(i, m)->roi_counts[5].sum_x_weighted == Catch::Approx(83 * (812+6) + 95 * (813+6) + 13 * (812+6) + 125 * (813+6))); - CHECK (test.GetDeviceOutput(i, m)->roi_counts[5].sum_y_weighted == Catch::Approx(83 * (303+2) + 95 * (303+2) + 13 * (304+2) + 125 * (304+2))); - - } - } -} - -TEST_CASE("HLS_C_Simulation_internal_packet_generator_4_images", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - - const uint16_t nmodules = 4; - const uint16_t nimages = 4; - DiffractionExperiment x(DetJF(nmodules)); - - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(nimages).PedestalG0Frames(0).InternalPacketGeneratorImages(nimages); - - HLSSimulatedDevice test(0, 64); - - std::vector ref_data(RAW_MODULE_SIZE*nimages*nmodules); - for (int i = 0; i < ref_data.size(); i++) - ref_data[i] = (7 * i + 5) % 65521; - - for (int m = 0; m < nmodules*nimages; m++) - test.SetInternalGeneratorFrame(ref_data.data() + RAW_MODULE_SIZE * m, m); - - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - REQUIRE(test.GetBytesReceived() == 128 * nmodules * 4 * JUNGFRAU_PACKET_SIZE_BYTES); - - for (int image = 0; image < 4; image++) { - for (int m = 0; m < nmodules; m++) { - std::cout << image << " " << m << std::endl; - auto imageBuf = (uint16_t *) test.GetDeviceOutput(image, m)->pixels; - for (int i = 0; i < RAW_MODULE_SIZE; i++) - REQUIRE(imageBuf[i] == ref_data[i + (image * nmodules + m) * RAW_MODULE_SIZE]); - } - } -} - -TEST_CASE("HLSSimulatedDevice_InternalGeneratorMemory", "[FPGA]") { - // 64 module frames in the card memory; the generator takes 63 at most - DiffractionExperiment x(DetJF(2)); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(1).NumTriggers(1); - HLSSimulatedDevice hls_device(0, 64); - - x.InternalPacketGeneratorImages(31); - REQUIRE_NOTHROW(hls_device.PrepareAction(x)); - - x.InternalPacketGeneratorImages(32); - REQUIRE_THROWS(hls_device.PrepareAction(x)); - - std::vector frame(RAW_MODULE_SIZE); - REQUIRE_NOTHROW(hls_device.SetInternalGeneratorFrame(frame.data(), 62)); - REQUIRE_THROWS(hls_device.SetInternalGeneratorFrame(frame.data(), 63)); -} - -// The claim behind ImageStatistics::pixel_sum: the preprocessors, reading the frame the card wrote, give the -// card's own sum over the same pixels. -namespace { - template - void CheckPixelSumParity(int64_t nsummation, CompressedImageMode mode) { - const uint16_t nmodules = 2; - - DiffractionExperiment x(DetJF(nmodules)); - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(1).PedestalG0Frames(0).Summation(nsummation) - .AutoSummation(true).ApplyPixelMask(true).BitDepthImage(8 * sizeof(T)).PixelSigned(true); - - std::mt19937 rng(5); - std::vector frame(RAW_MODULE_SIZE); - for (auto &v: frame) - v = static_cast(static_cast(rng() % 401) - 200); - frame[1024 + 1] = 2000; - frame[1024 + 2] = -1500; - frame[1024 + 3] = INT16_MAX; - frame[1024 + 4] = INT16_MIN; - frame[1024 + 5] = 1234; - - std::vector module_mask(RAW_MODULE_SIZE, 0); - module_mask[1024 + 5] = 1; - module_mask[1024 + 6] = 1; - - HLSSimulatedDevice test(0, 64); - std::vector user_mask; - for (int m = 0; m < x.GetModulesNum(); m++) { - test.InitializePixelMask(module_mask.data(), m); - test.SetInternalGeneratorFrame((uint16_t *) frame.data(), m); - user_mask.insert(user_mask.end(), module_mask.begin(), module_mask.end()); - } - REQUIRE_NOTHROW(test.StartAction(x)); - REQUIRE_NOTHROW(test.WaitForActionComplete()); - - std::vector image(nmodules * RAW_MODULE_SIZE); - int64_t card_sum = 0; - for (int m = 0; m < nmodules; m++) { - const auto *output = test.GetDeviceOutput(0, m); - memcpy(image.data() + m * RAW_MODULE_SIZE, output->pixels, RAW_MODULE_SIZE * sizeof(T)); - card_sum += output->module_statistics.pixel_sum; - } - CHECK(card_sum != 0); - - // The modules one under the other, as a detector without a card delivers its image. - DiffractionExperiment x_image(DetDECTRIS(RAW_MODULE_COLS, nmodules * RAW_MODULE_LINES, "Test", {}) - .BitDepthImage(8 * sizeof(T))); - x_image.PixelSigned(true); - PixelMask mask(x_image); - mask.LoadUserMask(x_image, user_mask); - const auto input = reinterpret_cast(image.data()); - - ImagePreprocessorCPU cpu(x_image, mask); - ImagePreprocessorBuffer cpu_buffer(x_image.GetPixelsNum()); - CHECK(cpu.Analyze(cpu_buffer, input, mode).pixel_sum == card_sum); - -#ifdef JFJOCH_USE_CUDA - if (get_gpu_count() > 0) { - ImagePreprocessorGPU gpu(x_image, mask, std::make_shared(), /*copy_image_to_host=*/false); - ImagePreprocessorBufferGPU gpu_buffer(x_image.GetPixelsNum()); - CHECK(gpu.Analyze(gpu_buffer, input, mode).pixel_sum == card_sum); - } -#endif - } -} - -TEST_CASE("HLS_C_Simulation_pixel_sum_matches_preprocessor", "[FPGA][Full]") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - CheckPixelSumParity(1, CompressedImageMode::Int16); - CheckPixelSumParity(4, CompressedImageMode::Int32); -} diff --git a/tests/JFJochReceiverIntegrationTest.cpp b/tests/JFJochReceiverIntegrationTest.cpp index 3b669762a..8aa91430c 100644 --- a/tests/JFJochReceiverIntegrationTest.cpp +++ b/tests/JFJochReceiverIntegrationTest.cpp @@ -4,7 +4,7 @@ #include #include "../receiver/JFJochReceiverTest.h" -#include "../acquisition_device/HLSSimulatedDevice.h" +#include "MockCardOutput.h" #include "../jungfrau/JFPedestalCalc.h" #include "../image_pusher/TestImagePusher.h" #include "../receiver/JFJochReceiverService.h" @@ -19,15 +19,11 @@ TEST_CASE("JFJochReceiverTest_Raw", "[JFJochReceiver]") { x.PedestalG0Frames(0).NumTriggers(1).UseInternalPacketGenerator(true) .ImagesPerTrigger(100).ImagesPerFile(10).IncidentEnergy_keV(12.4).Compression(CompressionAlgorithm::NO_COMPRESSION); - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i,64); - aq_devices.Add(std::move(test)); - } Logger logger("JFJochReceiverTest_Raw"); PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask); JFJochReceiverOutput output; bool ret; @@ -41,6 +37,31 @@ TEST_CASE("JFJochReceiverTest_Raw", "[JFJochReceiver]") { REQUIRE(!output.status.cancelled); } +// Two cards running at the same time - not possible with the HLS simulation +TEST_CASE("JFJochReceiverTest_Raw_TwoDevices", "[JFJochReceiver]") { + DiffractionExperiment x(DetJF(4)); + const uint16_t nthreads = 4; + + x.Raw(); + x.DataStreams(2); + x.PedestalG0Frames(0).NumTriggers(1).UseInternalPacketGenerator(true) + .ImagesPerTrigger(100).ImagesPerFile(10).IncidentEnergy_keV(12.4).Compression(CompressionAlgorithm::NO_COMPRESSION); + REQUIRE(x.GetDataStreamsNum() == 2); + + Logger logger("JFJochReceiverTest_Raw_TwoDevices"); + + PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask); + + JFJochReceiverOutput output; + bool ret; + REQUIRE_NOTHROW(ret = JFJochReceiverTest(output, logger, aq_devices, x, mask, nthreads)); + REQUIRE(ret); + REQUIRE(output.efficiency == 1.0); + REQUIRE(output.status.images_sent == x.GetImageNum()); + REQUIRE(!output.status.cancelled); +} + TEST_CASE("JFJochReceiverTest_Conversion", "[JFJochReceiver]") { Logger logger("JFJochReceiverTest_Conversion"); @@ -51,15 +72,11 @@ TEST_CASE("JFJochReceiverTest_Conversion", "[JFJochReceiver]") { x.PedestalG0Frames(0).NumTriggers(1).UseInternalPacketGenerator(true) .ImagesPerTrigger(32).ImagesPerFile(10).IncidentEnergy_keV(12.4).Compression(CompressionAlgorithm::BSHUF_ZSTD); - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i,64); - aq_devices.Add(std::move(test)); - } REQUIRE(x.IsGeometryTransformed()); PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask); JFJochReceiverOutput output; bool ret; @@ -94,12 +111,12 @@ TEST_CASE("JFJochReceiverTest_ForceCPUAzInt", "[JFJochReceiver]") { for (int i = 0; i < raw_image.size(); i++) raw_image[i] = 1000 + i % 1000; - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) - aq_devices.Add(std::make_unique(i, 64)); - PixelMask mask(x); + std::vector images; + images.push_back(ConvertedImage(x, JFCalibration(x), mask, raw_image)); + AcquisitionDeviceGroup aq_devices = MockDevices(x, images); + TestImagePusher pusher(x.GetImageNum() - 1); JFJochReceiverService service(aq_devices, logger, pusher); service.NumThreads(nthreads); @@ -136,7 +153,7 @@ TEST_CASE("JFJochReceiverTest_AzIntBinsExceedFPGA", "[JFJochReceiver]") { AcquisitionDeviceGroup aq_devices; for (int i = 0; i < x.GetDataStreamsNum(); i++) - aq_devices.Add(std::make_unique(i, 64)); + aq_devices.Add(std::make_unique(i, 64)); PixelMask mask(x); TestImagePusher pusher(0); @@ -157,13 +174,9 @@ TEST_CASE("JFJochReceiverTest_Conversion_NoGeomTransform", "[JFJochReceiver]") { .ImagesPerTrigger(32).ImagesPerFile(10).IncidentEnergy_keV(12.4).Compression(CompressionAlgorithm::BSHUF_ZSTD) .GeometryTransformation(false); - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i,64); - aq_devices.Add(std::move(test)); - } PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask); JFJochReceiverOutput output; bool ret; @@ -187,13 +200,9 @@ TEST_CASE("JFJochReceiverTest_Conversion_Poisson", "[JFJochReceiver]") { CompressionAlgorithm::BSHUF_ZSTD); x.LossyCompressionPoisson(1); - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i, 64); - aq_devices.Add(std::move(test)); - } PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask); JFJochReceiverOutput output; bool ret; @@ -217,13 +226,9 @@ TEST_CASE("JFJochReceiverTest_Conversion_Threshold", "[JFJochReceiver]") { CompressionAlgorithm::BSHUF_ZSTD); x.PixelValueLowThreshold(50); - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i, 64); - aq_devices.Add(std::move(test)); - } PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask); JFJochReceiverOutput output; bool ret; @@ -246,13 +251,9 @@ TEST_CASE("JFJochReceiverTest_Conversion_ApplyMask", "[JFJochReceiver]") { .ImagesPerTrigger(32).ImagesPerFile(10).IncidentEnergy_keV(12.4).Compression(CompressionAlgorithm::BSHUF_ZSTD); x.ApplyPixelMask(true).MaskChipEdges(true).MaskModuleEdges(true); - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i, 64); - aq_devices.Add(std::move(test)); - } PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask); JFJochReceiverOutput output; bool ret; @@ -276,13 +277,9 @@ TEST_CASE("JFJochReceiverTest_Conversion_Threshold_Summation", "[JFJochReceiver] CompressionAlgorithm::BSHUF_ZSTD); x.PixelValueLowThreshold(10); - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i, 64); - aq_devices.Add(std::move(test)); - } PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask); JFJochReceiverOutput output; bool ret; @@ -305,13 +302,9 @@ TEST_CASE("JFJochReceiverTest_Conversion_FixedGainG1", "[JFJochReceiver]") { .ImagesPerTrigger(32).ImagesPerFile(10).IncidentEnergy_keV(12.4).Compression(CompressionAlgorithm::BSHUF_ZSTD) .FixedGainG1(true); - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i,64); - aq_devices.Add(std::move(test)); - } PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask); JFJochReceiverOutput output; bool ret; @@ -336,13 +329,9 @@ TEST_CASE("JFJochReceiverTest_Conversion_FixedGainG1_onlyG1", "[JFJochReceiver]" .FixedGainG1(true); std::vector input(RAW_MODULE_SIZE*2, 0x4000 + 3000); - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i,64); - aq_devices.Add(std::move(test)); - } PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask, input); JFJochReceiverOutput output; bool ret; @@ -366,13 +355,9 @@ TEST_CASE("JFJochReceiverTest_Conversion_U16", "[JFJochReceiver]") { .BitDepthImage(16).PixelSigned(false); REQUIRE(!x.IsPixelSigned()); - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i,64); - aq_devices.Add(std::move(test)); - } PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask); JFJochReceiverOutput output; bool ret; @@ -398,13 +383,9 @@ TEST_CASE("JFJochReceiverTest_Conversion_U8", "[JFJochReceiver]") { REQUIRE(!x.IsPixelSigned()); REQUIRE(x.GetByteDepthImage() == 1); - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i,64); - aq_devices.Add(std::move(test)); - } PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask); JFJochReceiverOutput output; bool ret; @@ -427,13 +408,9 @@ TEST_CASE("JFJochReceiverTest_Conversion_I32", "[JFJochReceiver]") { .ImagesPerTrigger(32).ImagesPerFile(10).IncidentEnergy_keV(12.4).Compression(CompressionAlgorithm::BSHUF_ZSTD) .PixelSigned(true).BitDepthImage(32); - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i,64); - aq_devices.Add(std::move(test)); - } PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask); JFJochReceiverOutput output; bool ret; @@ -455,13 +432,9 @@ TEST_CASE("JFJochReceiverTest_Conversion_Summation2", "[JFJochReceiver]") { x.PedestalG0Frames(0).NumTriggers(1).UseInternalPacketGenerator(true) .ImagesPerTrigger(32).ImagesPerFile(10).IncidentEnergy_keV(12.4).Compression(CompressionAlgorithm::BSHUF_ZSTD).Summation(2); - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i,64); - aq_devices.Add(std::move(test)); - } PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask); JFJochReceiverOutput output; bool ret; @@ -484,13 +457,9 @@ TEST_CASE("JFJochReceiverTest_Conversion_Summation7_CPU", "[JFJochReceiver]") { .ImagesPerTrigger(8).ImagesPerFile(10).IncidentEnergy_keV(12.4).Compression(CompressionAlgorithm::BSHUF_ZSTD) .Summation(7).CPUSummation(true); - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i,64); - aq_devices.Add(std::move(test)); - } PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask); JFJochReceiverOutput output; bool ret; @@ -515,13 +484,9 @@ TEST_CASE("JFJochReceiverTest_Conversion_StorageCell", "[JFJochReceiver]") { REQUIRE(x.GetImageNum() == 16); REQUIRE(x.GetStorageCellNumber() == 16); - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i,64); - aq_devices.Add(std::move(test)); - } PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask); JFJochReceiverOutput output; bool ret; @@ -537,10 +502,10 @@ TEST_CASE("JFJochReceiverTest_PedestalG1", "[JFJochReceiver]") { const uint16_t nthreads = 4; size_t nframes = 256; + // The card output repeats every distinct_frames frames + size_t distinct_frames = 32; - std::vector pedestal_in(RAW_MODULE_SIZE*nframes); - - for (auto &i: pedestal_in) i = 16384 | 14000; + std::vector pedestal_in(RAW_MODULE_SIZE * distinct_frames); // Predictable random number generator std::mt19937 g1(1900); @@ -560,15 +525,17 @@ TEST_CASE("JFJochReceiverTest_PedestalG1", "[JFJochReceiver]") { AcquisitionDeviceGroup aq_devices; for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i,64); - test->CreateJFPackets(x, 1, nframes, 0, pedestal_in.data()); + auto test = std::make_unique(i, 64); + for (int f = 0; f < distinct_frames; f++) + test->SetOutput(f, 0, *OutputWithPixels(pedestal_in.data() + f * RAW_MODULE_SIZE)); + test->CreateJFPackets(x, 1, nframes, 0, nullptr); test->CreateFinalPacket(x); aq_devices.Add(std::move(test)); } JFPedestalCalc pc(x); for (int i = 0; i < nframes; i++) - pc.AnalyzeImage(pedestal_in.data() + i * RAW_MODULE_SIZE); + pc.AnalyzeImage(pedestal_in.data() + (i % distinct_frames) * RAW_MODULE_SIZE); Logger logger("JFJochReceiverTest_PedestalG1"); @@ -594,8 +561,10 @@ TEST_CASE("JFJochReceiverTest_PedestalG2_storage_cell", "[JFJochReceiver]") { const uint16_t nthreads = 4; size_t nframes = 150; + // The card output repeats every 2 * distinct_frames frames + size_t distinct_frames = 25; - std::vector pedestal_in(RAW_MODULE_SIZE*nframes), pedestal_in2(RAW_MODULE_SIZE*nframes);; + std::vector pedestal_in(RAW_MODULE_SIZE * distinct_frames), pedestal_in2(RAW_MODULE_SIZE * distinct_frames); // Predictable random number generator std::mt19937 g2(1900); @@ -625,10 +594,14 @@ TEST_CASE("JFJochReceiverTest_PedestalG2_storage_cell", "[JFJochReceiver]") { AcquisitionDeviceGroup aq_devices; for (int i = 0; i < x.GetDataStreamsNum(); i++) { - auto test = std::make_unique(i, nframes * 2); + auto test = std::make_unique(i, nframes * 2); + for (int j = 0; j < distinct_frames; j++) { + test->SetOutput(2 * j, 0, *OutputWithPixels(pedestal_in2.data() + j * RAW_MODULE_SIZE)); + test->SetOutput(2 * j + 1, 0, *OutputWithPixels(pedestal_in.data() + j * RAW_MODULE_SIZE)); + } for (int j = 0; j < nframes; j++) { - test->CreateJFPackets(x, 2 * j + 1, 1, 0, pedestal_in2.data() + j * RAW_MODULE_SIZE); - test->CreateJFPackets(x, 2 * j + 2, 1, 0, pedestal_in.data() + j * RAW_MODULE_SIZE); + test->CreateJFPackets(x, 2 * j + 1, 1, 0, nullptr); + test->CreateJFPackets(x, 2 * j + 2, 1, 0, nullptr); } test->CreateFinalPacket(x); aq_devices.Add(std::move(test)); @@ -636,7 +609,7 @@ TEST_CASE("JFJochReceiverTest_PedestalG2_storage_cell", "[JFJochReceiver]") { JFPedestalCalc pc(x); for (int i = 0; i < nframes; i++) - pc.AnalyzeImage(pedestal_in.data() + i * RAW_MODULE_SIZE); + pc.AnalyzeImage(pedestal_in.data() + (i % distinct_frames) * RAW_MODULE_SIZE); Logger logger("JFJochReceiverTest_PedestalG2_storage_cell"); @@ -660,10 +633,10 @@ TEST_CASE("JFJochReceiverTest_PedestalG0", "[JFJochReceiver]") { DiffractionExperiment x(DetJF(1)); const uint16_t nthreads = 4; size_t nframes = 500; + // The card output repeats every distinct_frames frames + size_t distinct_frames = 25; - std::vector pedestal_in(RAW_MODULE_SIZE*nframes); - - for (auto &i: pedestal_in) i = 16384 | 14000; + std::vector pedestal_in(RAW_MODULE_SIZE * distinct_frames); // Predictable random number generator std::mt19937 g1(1900); @@ -683,15 +656,17 @@ TEST_CASE("JFJochReceiverTest_PedestalG0", "[JFJochReceiver]") { AcquisitionDeviceGroup aq_devices; for (int i = 0; i < x.GetDataStreamsNum(); i++) { - auto test = std::make_unique(i,64); - test->CreateJFPackets(x, 1, nframes, 0, pedestal_in.data()); + auto test = std::make_unique(i, 64); + for (int f = 0; f < distinct_frames; f++) + test->SetOutput(f, 0, *OutputWithPixels(pedestal_in.data() + f * RAW_MODULE_SIZE)); + test->CreateJFPackets(x, 1, nframes, 0, nullptr); test->CreateFinalPacket(x); aq_devices.Add(std::move(test)); } JFPedestalCalc pc(x); for (int i = 0; i < nframes; i++) - pc.AnalyzeImage(pedestal_in.data() + i * RAW_MODULE_SIZE); + pc.AnalyzeImage(pedestal_in.data() + (i % distinct_frames) * RAW_MODULE_SIZE); PixelMask mask(x); @@ -732,13 +707,12 @@ TEST_CASE("JFJochReceiverTest_PedestalG0_StorageCell", "[JFJochReceiver]") { .ImagesPerTrigger(0).IncidentEnergy_keV(12.4); AcquisitionDeviceGroup aq_devices; - auto test = std::make_unique(0, 64); - for (int i = 0; i < nframes; i++) { - test->CreateJFPackets(x, i * 4 + 1, 1, 0, pedestal_in_0.data()); - test->CreateJFPackets(x, i * 4 + 2, 1, 0, pedestal_in_1.data()); - test->CreateJFPackets(x, i * 4 + 3, 1, 0, pedestal_in_2.data()); - test->CreateJFPackets(x, i * 4 + 4, 1, 0, pedestal_in_3.data()); - } + auto test = std::make_unique(0, 64); + test->SetOutput(0, 0, *OutputWithPixels(pedestal_in_0.data())); + test->SetOutput(1, 0, *OutputWithPixels(pedestal_in_1.data())); + test->SetOutput(2, 0, *OutputWithPixels(pedestal_in_2.data())); + test->SetOutput(3, 0, *OutputWithPixels(pedestal_in_3.data())); + test->CreateJFPackets(x, 1, nframes * 4, 0, nullptr); test->CreateFinalPacket(x); aq_devices.Add(std::move(test)); @@ -790,13 +764,12 @@ TEST_CASE("JFJochReceiverTest_PedestalFixedG1_StorageCell", "[JFJochReceiver]") REQUIRE(x.GetStorageCellNumber() == 4); AcquisitionDeviceGroup aq_devices; - auto test = std::make_unique(0, 64); - for (int i = 0; i < nframes; i++) { - test->CreateJFPackets(x, i * 4 + 1, 1, 0, pedestal_in_0.data()); - test->CreateJFPackets(x, i * 4 + 2, 1, 0, pedestal_in_1.data()); - test->CreateJFPackets(x, i * 4 + 3, 1, 0, pedestal_in_2.data()); - test->CreateJFPackets(x, i * 4 + 4, 1, 0, pedestal_in_3.data()); - } + auto test = std::make_unique(0, 64); + test->SetOutput(0, 0, *OutputWithPixels(pedestal_in_0.data())); + test->SetOutput(1, 0, *OutputWithPixels(pedestal_in_1.data())); + test->SetOutput(2, 0, *OutputWithPixels(pedestal_in_2.data())); + test->SetOutput(3, 0, *OutputWithPixels(pedestal_in_3.data())); + test->CreateJFPackets(x, 1, nframes * 4, 0, nullptr); test->CreateFinalPacket(x); aq_devices.Add(std::move(test)); @@ -840,7 +813,7 @@ TEST_CASE("JFJochReceiverTest_PedestalG1_NoFrames", "[JFJochReceiver]") { AcquisitionDeviceGroup aq_devices; for (int i = 0; i < x.GetDataStreamsNum(); i++) { - auto test = std::make_unique(i, nframes + 8); + auto test = std::make_unique(i, nframes + 8); test->CreateFinalPacket(x); aq_devices.Add(std::move(test)); } @@ -875,19 +848,28 @@ TEST_CASE("JFJochReceiverTest_PacketLost_Raw", "[JFJochReceiver]") { PixelMask pixel_mask(x); + // The card writes the missing packet as UINT16_MAX + std::vector frame_lost = frame_in; + for (int i = 127 * 4096; i < RAW_MODULE_SIZE; i++) + frame_lost[i] = UINT16_MAX; + AcquisitionDeviceGroup aq_devices; for (int i = 0; i < x.GetDataStreamsNum(); i++) { - auto test = std::make_unique(i, 64); - test->CreateJFPackets(x, 1, 1, 0, frame_in.data()); - test->CreateJFPackets(x, 1, 1, 1, frame_in.data()); - test->CreateJFPackets(x, 2, 1, 0, frame_in.data()); - test->CreateJFPackets(x, 2, 1, 1, frame_in.data()); - test->CreateJFPackets(x, 3, 1, 0, frame_in.data()); - test->CreateJFPackets(x, 3, 1, 1, frame_in.data()); + auto test = std::make_unique(i, 64); + for (int image = 0; image < 4; image++) { + test->SetOutput(image, 0, *OutputWithPixels((image == 3) ? frame_lost.data() : frame_in.data())); + test->SetOutput(image, 1, *OutputWithPixels(frame_in.data())); + } + test->CreateJFPackets(x, 1, 1, 0, nullptr); + test->CreateJFPackets(x, 1, 1, 1, nullptr); + test->CreateJFPackets(x, 2, 1, 0, nullptr); + test->CreateJFPackets(x, 2, 1, 1, nullptr); + test->CreateJFPackets(x, 3, 1, 0, nullptr); + test->CreateJFPackets(x, 3, 1, 1, nullptr); for (int j = 0; j < 127; j++) - test->CreateJFPacket(x, 4, j, 0, frame_in.data() + (i * 128 + j) * 4096); - test->CreateJFPackets(x, 4, 1, 1, frame_in.data()); + test->CreateJFPacket(x, 4, j, 0, nullptr); + test->CreateJFPackets(x, 4, 1, 1, nullptr); test->CreateFinalPacket(x); aq_devices.Add(std::move(test)); @@ -939,13 +921,15 @@ TEST_CASE("JFJochReceiverTest_Cancel", "[JFJochReceiver]") { AcquisitionDeviceGroup aq_devices; for (int i = 0; i < x.GetDataStreamsNum(); i++) { - auto test = std::make_unique(i, 64); - test->CreateJFPackets(x, 1, 1, 0, frame_in.data()); - test->CreateJFPackets(x, 1, 1, 1, frame_in.data()); - test->CreateJFPackets(x, 2, 1, 0, frame_in.data()); - test->CreateJFPackets(x, 2, 1, 1, frame_in.data()); - test->CreateJFPackets(x, 3, 1, 0, frame_in.data()); - test->CreateJFPackets(x, 3, 1, 1, frame_in.data()); + auto test = std::make_unique(i, 64); + test->SetOutput(0, 0, *OutputWithPixels(frame_in.data())); + test->SetOutput(0, 1, *OutputWithPixels(frame_in.data())); + test->CreateJFPackets(x, 1, 1, 0, nullptr); + test->CreateJFPackets(x, 1, 1, 1, nullptr); + test->CreateJFPackets(x, 2, 1, 0, nullptr); + test->CreateJFPackets(x, 2, 1, 1, nullptr); + test->CreateJFPackets(x, 3, 1, 0, nullptr); + test->CreateJFPackets(x, 3, 1, 1, nullptr); aq_devices.Add(std::move(test)); } Logger logger("JFJochReceiverTest_Cancel"); @@ -958,13 +942,49 @@ TEST_CASE("JFJochReceiverTest_Cancel", "[JFJochReceiver]") { service.Start(x, pixel_mask, &calib); - std::this_thread::sleep_for(std::chrono::seconds(60)); + std::this_thread::sleep_for(std::chrono::seconds(1)); service.Cancel(false); auto receiver_out = service.Stop(); REQUIRE(receiver_out.status.cancelled); } +TEST_CASE("JFJochReceiverTest_Pedestal_Cancel", "[JFJochReceiver]") { + Logger logger("JFJochReceiverTest_Pedestal_Cancel"); + + DiffractionExperiment x(DetJF(1)); + x.Mode(DetectorMode::PedestalG0); + x.PedestalG0Frames(100).NumTriggers(1).UseInternalPacketGenerator(false).IncidentEnergy_keV(12.4); + + std::vector frame_in(RAW_MODULE_SIZE, 1000); + + // Only the first frames of the 100 arrive, and no final packet: the run waits until it is cancelled. + AcquisitionDeviceGroup aq_devices; + auto test = std::make_unique(0, 64); + test->SetOutput(0, 0, *OutputWithPixels(frame_in.data())); + test->CreateJFPackets(x, 1, 10, 0, nullptr); + aq_devices.Add(std::move(test)); + + PixelMask pixel_mask(x); + TestImagePusher pusher(0); + JFJochReceiverService service(aq_devices, logger, pusher); + service.NumThreads(4); + + service.Start(x, pixel_mask, nullptr); + std::this_thread::sleep_for(std::chrono::seconds(1)); + service.Cancel(false); + + auto output = service.Stop(); + REQUIRE(output.status.cancelled); + REQUIRE(output.efficiency < 1.0); + REQUIRE(output.received_packets.at(0) > 0); + REQUIRE(output.received_packets.at(0) < output.expected_packets.at(0)); + // Too few frames for a pedestal, so every pixel is reported as wrong + REQUIRE(output.pedestal_result.size() == 1); + REQUIRE(output.pedestal_result[0].CountMaskedPixels() == RAW_MODULE_SIZE); + REQUIRE(pusher.GetCounter() == 0); +} + TEST_CASE("JFJochReceiverTest_EIGER", "[JFJochReceiver]") { Logger logger("JFJochReceiverTest_EIGER"); @@ -975,13 +995,9 @@ TEST_CASE("JFJochReceiverTest_EIGER", "[JFJochReceiver]") { x.PedestalG0Frames(0).NumTriggers(1).UseInternalPacketGenerator(true) .ImagesPerTrigger(32).ImagesPerFile(10).IncidentEnergy_keV(12.4).Compression(CompressionAlgorithm::BSHUF_ZSTD); - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i,64); - aq_devices.Add(std::move(test)); - } PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask); JFJochReceiverOutput output; bool ret; @@ -1003,13 +1019,9 @@ TEST_CASE("JFJochReceiverTest_EIGER_8bit", "[JFJochReceiver]") { .ImagesPerTrigger(32).ImagesPerFile(10).IncidentEnergy_keV(12.4).Compression(CompressionAlgorithm::BSHUF_ZSTD); x.EigerBitDepth(8); - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i,64); - aq_devices.Add(std::move(test)); - } PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask); JFJochReceiverOutput output; bool ret; @@ -1031,13 +1043,9 @@ TEST_CASE("JFJochReceiverTest_EIGER_32bit", "[JFJochReceiver]") { .ImagesPerTrigger(32).ImagesPerFile(10).IncidentEnergy_keV(12.4).Compression(CompressionAlgorithm::BSHUF_ZSTD); x.EigerBitDepth(32); - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i,64); - aq_devices.Add(std::move(test)); - } PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask); JFJochReceiverOutput output; bool ret; @@ -1059,13 +1067,9 @@ TEST_CASE("JFJochReceiverTest_EIGER_conversion", "[JFJochReceiver]") { x.PedestalG0Frames(0).NumTriggers(1).UseInternalPacketGenerator(true) .ImagesPerTrigger(32).ImagesPerFile(10).IncidentEnergy_keV(12.4).Compression(CompressionAlgorithm::BSHUF_ZSTD); - AcquisitionDeviceGroup aq_devices; - for (int i = 0; i < x.GetDataStreamsNum(); i++) { - std::unique_ptr test = std::make_unique(i,64); - aq_devices.Add(std::move(test)); - } PixelMask mask(x); + AcquisitionDeviceGroup aq_devices = ConvertingMocks(x, mask); JFJochReceiverOutput output; bool ret; diff --git a/tests/JFJochReceiverProcessingTest.cpp b/tests/JFJochReceiverProcessingTest.cpp index bc873d224..68e4c5a4a 100644 --- a/tests/JFJochReceiverProcessingTest.cpp +++ b/tests/JFJochReceiverProcessingTest.cpp @@ -6,7 +6,7 @@ #include #include "../receiver/JFJochReceiverTest.h" -#include "../acquisition_device/HLSSimulatedDevice.h" +#include "MockCardOutput.h" #include "../writer/HDF5Objects.h" #include "../receiver/JFJochReceiverService.h" #include "../preview/JFJochTIFF.h" @@ -50,13 +50,17 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index", "[JFJochReceiver]" ConvertedToRawGeometry(experiment, image_raw_geom.data(), image_conv.data()); logger.Info("Loaded image"); - // Setup acquisition device - AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); - for (int m = 0; m < experiment.GetModulesNum(); m++) - test->SetInternalGeneratorFrame((uint16_t *) image_raw_geom.data() + m * RAW_MODULE_SIZE, m); - - aq_devices.Add(std::move(test)); + // Setup acquisition device: the card writes the image, with the strong pixels of its spot finder + SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); + settings.signal_to_noise_threshold = 2.5; + settings.photon_count_threshold = 5; + settings.min_pix_per_spot = 1; + settings.max_pix_per_spot = 200; + settings.high_resolution_limit = 2.0; + settings.low_resolution_limit = 50.0; + std::vector images; + images.push_back(Int16Image(experiment, pixel_mask, image_raw_geom, settings)); + AcquisitionDeviceGroup aq_devices = MockDevices(experiment, images); ZMQStream2Pusher pusher({"ipc://*"}); pusher.WriterNotificationSocket("ipc://*"); @@ -72,13 +76,6 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index", "[JFJochReceiver]" // No progress value at the start of measurement REQUIRE(!service.GetProgress().has_value()); - SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); - settings.signal_to_noise_threshold = 2.5; - settings.photon_count_threshold = 5; - settings.min_pix_per_spot = 1; - settings.max_pix_per_spot = 200; - settings.high_resolution_limit = 2.0; - settings.low_resolution_limit = 50.0; service.SetSpotFindingSettings(settings); service.Start(experiment, pixel_mask, nullptr); @@ -142,7 +139,7 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index_GPUStills", "[JFJoch // Setup acquisition device AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); + auto test = std::make_unique(0, 64); for (int m = 0; m < experiment.GetModulesNum(); m++) test->SetInternalGeneratorFrame((uint16_t *) image_raw_geom.data() + m * RAW_MODULE_SIZE, m); @@ -225,7 +222,7 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_GPUStills_writer_down", "[JFJochReceiver]") ConvertedToRawGeometry(experiment, image_raw_geom.data(), image_conv.data()); AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); + auto test = std::make_unique(0, 64); for (int m = 0; m < experiment.GetModulesNum(); m++) test->SetInternalGeneratorFrame((uint16_t *) image_raw_geom.data() + m * RAW_MODULE_SIZE, m); aq_devices.Add(std::move(test)); @@ -257,9 +254,7 @@ TEST_CASE("JFJochIntegrationTest_GPUStills_cancel_during_prepass", "[JFJochRecei // A cancel while the frames are held for the pre-pass ends the collection. Logger logger(Catch::getResultCapture().getCurrentTestName()); - // Few images: after the cancel, the simulated FPGA still processes, at CPU speed, the frames it has already - // taken in, which takes minutes for a long collection on a loaded machine. For the same reason, how long the - // stop takes is not checked. + // How long the stop takes is not checked. DiffractionExperiment experiment(DetJF4M()); experiment.ImagesPerTrigger(30).NumTriggers(1).UseInternalPacketGenerator(true) .FilePrefix("gpu_stills_cancel").JungfrauConvPhotonCnt(false) @@ -269,7 +264,7 @@ TEST_CASE("JFJochIntegrationTest_GPUStills_cancel_during_prepass", "[JFJochRecei PixelMask pixel_mask(experiment); AcquisitionDeviceGroup aq_devices; - aq_devices.Add(std::make_unique(0, 64)); + aq_devices.Add(std::make_unique(0, 64)); NonePusher pusher; JFJochReceiverService service(aq_devices, logger, pusher); @@ -323,13 +318,17 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index_refinement", "[JFJoc ConvertedToRawGeometry(experiment, image_raw_geom.data(), image_conv.data()); logger.Info("Loaded image"); - // Setup acquisition device - AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); - for (int m = 0; m < experiment.GetModulesNum(); m++) - test->SetInternalGeneratorFrame((uint16_t *) image_raw_geom.data() + m * RAW_MODULE_SIZE, m); - - aq_devices.Add(std::move(test)); + // Setup acquisition device: the card writes the image, with the strong pixels of its spot finder + SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); + settings.signal_to_noise_threshold = 2.5; + settings.photon_count_threshold = 5; + settings.min_pix_per_spot = 1; + settings.max_pix_per_spot = 200; + settings.high_resolution_limit = 2.0; + settings.low_resolution_limit = 50.0; + std::vector images; + images.push_back(Int16Image(experiment, pixel_mask, image_raw_geom, settings)); + AcquisitionDeviceGroup aq_devices = MockDevices(experiment, images); ZMQStream2Pusher pusher({"ipc://*"}); pusher.WriterNotificationSocket("ipc://*"); @@ -345,13 +344,6 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index_refinement", "[JFJoc // No progress value at the start of measurement REQUIRE(!service.GetProgress().has_value()); - SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); - settings.signal_to_noise_threshold = 2.5; - settings.photon_count_threshold = 5; - settings.min_pix_per_spot = 1; - settings.max_pix_per_spot = 200; - settings.high_resolution_limit = 2.0; - settings.low_resolution_limit = 50.0; service.SetSpotFindingSettings(settings); service.Start(experiment, pixel_mask, nullptr); @@ -416,13 +408,17 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index_refinement_tetragona ConvertedToRawGeometry(experiment, image_raw_geom.data(), image_conv.data()); logger.Info("Loaded image"); - // Setup acquisition device - AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); - for (int m = 0; m < experiment.GetModulesNum(); m++) - test->SetInternalGeneratorFrame((uint16_t *) image_raw_geom.data() + m * RAW_MODULE_SIZE, m); - - aq_devices.Add(std::move(test)); + // Setup acquisition device: the card writes the image, with the strong pixels of its spot finder + SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); + settings.signal_to_noise_threshold = 2.5; + settings.photon_count_threshold = 5; + settings.min_pix_per_spot = 1; + settings.max_pix_per_spot = 200; + settings.high_resolution_limit = 2.0; + settings.low_resolution_limit = 50.0; + std::vector images; + images.push_back(Int16Image(experiment, pixel_mask, image_raw_geom, settings)); + AcquisitionDeviceGroup aq_devices = MockDevices(experiment, images); ZMQStream2Pusher pusher({"ipc://*"}); pusher.WriterNotificationSocket("ipc://*"); @@ -438,13 +434,6 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index_refinement_tetragona // No progress value at the start of measurement REQUIRE(!service.GetProgress().has_value()); - SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); - settings.signal_to_noise_threshold = 2.5; - settings.photon_count_threshold = 5; - settings.min_pix_per_spot = 1; - settings.max_pix_per_spot = 200; - settings.high_resolution_limit = 2.0; - settings.low_resolution_limit = 50.0; service.SetSpotFindingSettings(settings); service.Start(experiment, pixel_mask, nullptr); @@ -504,13 +493,18 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_index_no_integration", "[JFJoc ConvertedToRawGeometry(experiment, image_raw_geom.data(), image_conv.data()); logger.Info("Loaded image"); - // Setup acquisition device - AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); - for (int m = 0; m < experiment.GetModulesNum(); m++) - test->SetInternalGeneratorFrame((uint16_t *) image_raw_geom.data() + m * RAW_MODULE_SIZE, m); - - aq_devices.Add(std::move(test)); + // Setup acquisition device: the card writes the image, with the strong pixels of its spot finder + SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); + settings.signal_to_noise_threshold = 2.5; + settings.photon_count_threshold = 5; + settings.min_pix_per_spot = 1; + settings.max_pix_per_spot = 200; + settings.high_resolution_limit = 2.0; + settings.low_resolution_limit = 50.0; + settings.quick_integration = true; // asked for, and still not done + std::vector images; + images.push_back(Int16Image(experiment, pixel_mask, image_raw_geom, settings)); + AcquisitionDeviceGroup aq_devices = MockDevices(experiment, images); ZMQStream2Pusher pusher({"ipc://*"}); pusher.WriterNotificationSocket("ipc://*"); @@ -526,14 +520,6 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_index_no_integration", "[JFJoc // No progress value at the start of measurement REQUIRE(!service.GetProgress().has_value()); - SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); - settings.signal_to_noise_threshold = 2.5; - settings.photon_count_threshold = 5; - settings.min_pix_per_spot = 1; - settings.max_pix_per_spot = 200; - settings.high_resolution_limit = 2.0; - settings.low_resolution_limit = 50.0; - settings.quick_integration = true; // asked for, and still not done service.SetSpotFindingSettings(settings); service.Start(experiment, pixel_mask, nullptr); @@ -603,13 +589,17 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_index_no_spots", "[JFJochRecei ConvertedToRawGeometry(experiment, image_raw_geom.data(), image_conv.data()); logger.Info("Loaded image"); - // Setup acquisition device - AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); - for (int m = 0; m < experiment.GetModulesNum(); m++) - test->SetInternalGeneratorFrame((uint16_t *) image_raw_geom.data() + m * RAW_MODULE_SIZE, m); - - aq_devices.Add(std::move(test)); + // Setup acquisition device: the card writes the image, with the strong pixels of its spot finder + SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); + settings.signal_to_noise_threshold = 20; // make sure there are zero spots possible + settings.photon_count_threshold = 10000; + settings.min_pix_per_spot = 5; + settings.max_pix_per_spot = 200; + settings.high_resolution_limit = 2.0; + settings.low_resolution_limit = 50.0; + std::vector images; + images.push_back(Int16Image(experiment, pixel_mask, image_raw_geom, settings)); + AcquisitionDeviceGroup aq_devices = MockDevices(experiment, images); ZMQStream2Pusher pusher({"ipc://*"}); pusher.WriterNotificationSocket("ipc://*"); @@ -625,13 +615,6 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_index_no_spots", "[JFJochRecei // No progress value at the start of measurement REQUIRE(!service.GetProgress().has_value()); - SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); - settings.signal_to_noise_threshold = 20; // make sure there are zero spots possible - settings.photon_count_threshold = 10000; - settings.min_pix_per_spot = 5; - settings.max_pix_per_spot = 200; - settings.high_resolution_limit = 2.0; - settings.low_resolution_limit = 50.0; service.SetSpotFindingSettings(settings); service.Start(experiment, pixel_mask, nullptr); @@ -684,13 +667,11 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_azint_2d", "[JFJochReceiver]") { ConvertedToRawGeometry(experiment, image_raw_geom.data(), image_conv.data()); logger.Info("Loaded image"); - // Setup acquisition device - AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); - for (int m = 0; m < experiment.GetModulesNum(); m++) - test->SetInternalGeneratorFrame((uint16_t *) image_raw_geom.data() + m * RAW_MODULE_SIZE, m); - - aq_devices.Add(std::move(test)); + // Setup acquisition device: the card writes the image, with the strong pixels of its spot finder + std::vector images; + images.push_back(Int16Image(experiment, pixel_mask, image_raw_geom, + DiffractionExperiment::DefaultDataProcessingSettings())); + AcquisitionDeviceGroup aq_devices = MockDevices(experiment, images); ZMQStream2Pusher pusher({"ipc://*"}); pusher.WriterNotificationSocket("ipc://*"); @@ -757,13 +738,17 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index_FFT", "[JFJochReceiv ConvertedToRawGeometry(experiment, image_raw_geom.data(), image_conv.data()); logger.Info("Loaded image"); - // Setup acquisition device - AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); - for (int m = 0; m < experiment.GetModulesNum(); m++) - test->SetInternalGeneratorFrame((uint16_t *) image_raw_geom.data() + m * RAW_MODULE_SIZE, m); - - aq_devices.Add(std::move(test)); + // Setup acquisition device: the card writes the image, with the strong pixels of its spot finder + SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); + settings.signal_to_noise_threshold = 2.5; + settings.photon_count_threshold = 5; + settings.min_pix_per_spot = 1; + settings.max_pix_per_spot = 200; + settings.high_resolution_limit = 2.0; + settings.low_resolution_limit = 50.0; + std::vector images; + images.push_back(Int16Image(experiment, pixel_mask, image_raw_geom, settings)); + AcquisitionDeviceGroup aq_devices = MockDevices(experiment, images); ZMQStream2Pusher pusher({"ipc://*"}); pusher.WriterNotificationSocket("ipc://*"); @@ -779,13 +764,6 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index_FFT", "[JFJochReceiv // No progress value at the start of measurement REQUIRE(!service.GetProgress().has_value()); - SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); - settings.signal_to_noise_threshold = 2.5; - settings.photon_count_threshold = 5; - settings.min_pix_per_spot = 1; - settings.max_pix_per_spot = 200; - settings.high_resolution_limit = 2.0; - settings.low_resolution_limit = 50.0; service.SetSpotFindingSettings(settings); service.Start(experiment, pixel_mask, nullptr); @@ -846,13 +824,17 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index_FFTW", "[JFJochRecei ConvertedToRawGeometry(experiment, image_raw_geom.data(), image_conv.data()); logger.Info("Loaded image"); - // Setup acquisition device - AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); - for (int m = 0; m < experiment.GetModulesNum(); m++) - test->SetInternalGeneratorFrame((uint16_t *) image_raw_geom.data() + m * RAW_MODULE_SIZE, m); - - aq_devices.Add(std::move(test)); + // Setup acquisition device: the card writes the image, with the strong pixels of its spot finder + SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); + settings.signal_to_noise_threshold = 2.5; + settings.photon_count_threshold = 5; + settings.min_pix_per_spot = 1; + settings.max_pix_per_spot = 200; + settings.high_resolution_limit = 2.0; + settings.low_resolution_limit = 50.0; + std::vector images; + images.push_back(Int16Image(experiment, pixel_mask, image_raw_geom, settings)); + AcquisitionDeviceGroup aq_devices = MockDevices(experiment, images); ZMQStream2Pusher pusher({"ipc://*"}); pusher.WriterNotificationSocket("ipc://*"); @@ -868,13 +850,6 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index_FFTW", "[JFJochRecei // No progress value at the start of measurement REQUIRE(!service.GetProgress().has_value()); - SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); - settings.signal_to_noise_threshold = 2.5; - settings.photon_count_threshold = 5; - settings.min_pix_per_spot = 1; - settings.max_pix_per_spot = 200; - settings.high_resolution_limit = 2.0; - settings.low_resolution_limit = 50.0; service.SetSpotFindingSettings(settings); service.Start(experiment, pixel_mask, nullptr); @@ -936,13 +911,17 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index_FFT_reference", "[JF ConvertedToRawGeometry(experiment, image_raw_geom.data(), image_conv.data()); logger.Info("Loaded image"); - // Setup acquisition device - AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); - for (int m = 0; m < experiment.GetModulesNum(); m++) - test->SetInternalGeneratorFrame((uint16_t *) image_raw_geom.data() + m * RAW_MODULE_SIZE, m); - - aq_devices.Add(std::move(test)); + // Setup acquisition device: the card writes the image, with the strong pixels of its spot finder + SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); + settings.signal_to_noise_threshold = 2.5; + settings.photon_count_threshold = 5; + settings.min_pix_per_spot = 1; + settings.max_pix_per_spot = 200; + settings.high_resolution_limit = 2.0; + settings.low_resolution_limit = 50.0; + std::vector images; + images.push_back(Int16Image(experiment, pixel_mask, image_raw_geom, settings)); + AcquisitionDeviceGroup aq_devices = MockDevices(experiment, images); ZMQStream2Pusher pusher({"ipc://*"}); pusher.WriterNotificationSocket("ipc://*"); @@ -958,13 +937,6 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index_FFT_reference", "[JF // No progress value at the start of measurement REQUIRE(!service.GetProgress().has_value()); - SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); - settings.signal_to_noise_threshold = 2.5; - settings.photon_count_threshold = 5; - settings.min_pix_per_spot = 1; - settings.max_pix_per_spot = 200; - settings.high_resolution_limit = 2.0; - settings.low_resolution_limit = 50.0; service.SetSpotFindingSettings(settings); service.Start(experiment, pixel_mask, nullptr); @@ -1026,13 +998,17 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index_FFT_wrong_reference" ConvertedToRawGeometry(experiment, image_raw_geom.data(), image_conv.data()); logger.Info("Loaded image"); - // Setup acquisition device - AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); - for (int m = 0; m < experiment.GetModulesNum(); m++) - test->SetInternalGeneratorFrame((uint16_t *) image_raw_geom.data() + m * RAW_MODULE_SIZE, m); - - aq_devices.Add(std::move(test)); + // Setup acquisition device: the card writes the image, with the strong pixels of its spot finder + SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); + settings.signal_to_noise_threshold = 2.5; + settings.photon_count_threshold = 5; + settings.min_pix_per_spot = 1; + settings.max_pix_per_spot = 200; + settings.high_resolution_limit = 2.0; + settings.low_resolution_limit = 50.0; + std::vector images; + images.push_back(Int16Image(experiment, pixel_mask, image_raw_geom, settings)); + AcquisitionDeviceGroup aq_devices = MockDevices(experiment, images); ZMQStream2Pusher pusher({"ipc://*"}); pusher.WriterNotificationSocket("ipc://*"); @@ -1048,13 +1024,6 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index_FFT_wrong_reference" // No progress value at the start of measurement REQUIRE(!service.GetProgress().has_value()); - SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); - settings.signal_to_noise_threshold = 2.5; - settings.photon_count_threshold = 5; - settings.min_pix_per_spot = 1; - settings.max_pix_per_spot = 200; - settings.high_resolution_limit = 2.0; - settings.low_resolution_limit = 50.0; service.SetSpotFindingSettings(settings); service.Start(experiment, pixel_mask, nullptr); @@ -1120,13 +1089,17 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index_grid", "[JFJochRecei ConvertedToRawGeometry(experiment, image_raw_geom.data(), image_conv.data()); logger.Info("Loaded image"); - // Setup acquisition device - AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); - for (int m = 0; m < experiment.GetModulesNum(); m++) - test->SetInternalGeneratorFrame((uint16_t *) image_raw_geom.data() + m * RAW_MODULE_SIZE, m); - - aq_devices.Add(std::move(test)); + // Setup acquisition device: the card writes the image, with the strong pixels of its spot finder + SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); + settings.signal_to_noise_threshold = 2.5; + settings.photon_count_threshold = 5; + settings.min_pix_per_spot = 1; + settings.max_pix_per_spot = 200; + settings.high_resolution_limit = 2.0; + settings.low_resolution_limit = 50.0; + std::vector images; + images.push_back(Int16Image(experiment, pixel_mask, image_raw_geom, settings)); + AcquisitionDeviceGroup aq_devices = MockDevices(experiment, images); ZMQStream2Pusher pusher({"ipc://*"}); pusher.WriterNotificationSocket("ipc://*"); @@ -1142,13 +1115,6 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index_grid", "[JFJochRecei // No progress value at the start of measurement REQUIRE(!service.GetProgress().has_value()); - SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); - settings.signal_to_noise_threshold = 2.5; - settings.photon_count_threshold = 5; - settings.min_pix_per_spot = 1; - settings.max_pix_per_spot = 200; - settings.high_resolution_limit = 2.0; - settings.low_resolution_limit = 50.0; service.SetSpotFindingSettings(settings); service.Start(experiment, pixel_mask, nullptr); @@ -1224,13 +1190,17 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index_32", "[JFJochReceive ConvertedToRawGeometry(experiment, image_raw_geom.data(), image_conv.data()); logger.Info("Loaded image"); - // Setup acquisition device - AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); - for (int m = 0; m < experiment.GetModulesNum(); m++) - test->SetInternalGeneratorFrame((uint16_t *) image_raw_geom.data() + m * RAW_MODULE_SIZE, m); - - aq_devices.Add(std::move(test)); + // Setup acquisition device: the card writes the image, with the strong pixels of its spot finder + SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); + settings.signal_to_noise_threshold = 2.5; + settings.photon_count_threshold = 5; + settings.min_pix_per_spot = 1; + settings.max_pix_per_spot = 200; + settings.high_resolution_limit = 2.0; + settings.low_resolution_limit = 50.0; + std::vector images; + images.push_back(Int16Image(experiment, pixel_mask, image_raw_geom, settings)); + AcquisitionDeviceGroup aq_devices = MockDevices(experiment, images); ZMQStream2Pusher pusher({"ipc://*"}); pusher.WriterNotificationSocket("ipc://*"); @@ -1246,13 +1216,6 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index_32", "[JFJochReceive // No progress value at the start of measurement REQUIRE(!service.GetProgress().has_value()); - SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); - settings.signal_to_noise_threshold = 2.5; - settings.photon_count_threshold = 5; - settings.min_pix_per_spot = 1; - settings.max_pix_per_spot = 200; - settings.high_resolution_limit = 2.0; - settings.low_resolution_limit = 50.0; service.SetSpotFindingSettings(settings); service.Start(experiment, pixel_mask, nullptr); @@ -1313,12 +1276,15 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index_min_pix_2", "[JFJoch ConvertedToRawGeometry(experiment, image_raw_geom.data(), image_conv.data()); logger.Info("Loaded image"); - // Setup acquisition device - AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); - for (int m = 0; m < experiment.GetModulesNum(); m++) - test->SetInternalGeneratorFrame((uint16_t *) image_raw_geom.data() + m * RAW_MODULE_SIZE, m); - aq_devices.Add(std::move(test)); + // Setup acquisition device: the card writes the image, with the strong pixels of its spot finder + SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); + settings.signal_to_noise_threshold = 2.5; + settings.photon_count_threshold = 3; + settings.min_pix_per_spot = 2; + settings.max_pix_per_spot = 200; + std::vector images; + images.push_back(Int16Image(experiment, pixel_mask, image_raw_geom, settings)); + AcquisitionDeviceGroup aq_devices = MockDevices(experiment, images); ZMQStream2Pusher pusher({"ipc://*"}); ZMQImagePuller puller(pusher.GetAddress()[0]); @@ -1329,11 +1295,6 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_crystal_spot_and_index_min_pix_2", "[JFJoch service.NumThreads(nthreads); service.Indexing(experiment.GetIndexingSettings()); - SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); - settings.signal_to_noise_threshold = 2.5; - settings.photon_count_threshold = 3; - settings.min_pix_per_spot = 2; - settings.max_pix_per_spot = 200; service.SetSpotFindingSettings(settings); service.Start(experiment, pixel_mask, nullptr); @@ -1449,13 +1410,11 @@ TEST_CASE("JFJochIntegrationTest_ZMQ_ROI", "[JFJochReceiver]") { } } - // Setup acquisition device - AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); - for (int m = 0; m < experiment.GetModulesNum(); m++) - test->SetInternalGeneratorFrame((uint16_t *) image_raw_geom.data() + m * RAW_MODULE_SIZE, m); - - aq_devices.Add(std::move(test)); + // Setup acquisition device: the card writes the image, with the strong pixels of its spot finder + std::vector images; + images.push_back(Int16Image(experiment, pixel_mask, image_raw_geom, + DiffractionExperiment::DefaultDataProcessingSettings())); + AcquisitionDeviceGroup aq_devices = MockDevices(experiment, images); ZMQStream2Pusher pusher({"ipc://*"}); ZMQImagePuller puller(pusher.GetAddress()[0]); @@ -1523,7 +1482,7 @@ TEST_CASE("JFJochIntegrationTest_ZMQPreview", "[JFJochReceiver]") { // Setup acquisition device AcquisitionDeviceGroup aq_devices; - aq_devices.Add(std::make_unique(0, 64)); + aq_devices.Add(std::make_unique(0, 64)); NonePusher pusher; @@ -1587,7 +1546,7 @@ TEST_CASE("JFJochIntegrationTest_ZMQMetadata", "[JFJochReceiver]") { // Setup acquisition device AcquisitionDeviceGroup aq_devices; - aq_devices.Add(std::make_unique(0, 64)); + aq_devices.Add(std::make_unique(0, 64)); NonePusher pusher; @@ -1647,7 +1606,7 @@ TEST_CASE("JFJochIntegrationTest_HDF5FilePusher", "[JFJochReceiver]") { // Setup acquisition device AcquisitionDeviceGroup aq_devices; - aq_devices.Add(std::make_unique(0, 64)); + aq_devices.Add(std::make_unique(0, 64)); HDF5FilePusher pusher; @@ -1681,7 +1640,7 @@ TEST_CASE("JFJochIntegrationTest_HDF5FilePusher_republish", "[JFJochReceiver]") PixelMask pixel_mask(experiment); AcquisitionDeviceGroup aq_devices; - aq_devices.Add(std::make_unique(0, 64)); + aq_devices.Add(std::make_unique(0, 64)); HDF5FilePusher pusher(repub_addr); @@ -1743,7 +1702,7 @@ TEST_CASE("JFJochIntegrationTest_HDF5FilePusher_Raw", "[JFJochReceiver]") { // Setup acquisition device AcquisitionDeviceGroup aq_devices; - aq_devices.Add(std::make_unique(0, 64)); + aq_devices.Add(std::make_unique(0, 64)); HDF5FilePusher pusher; @@ -1793,13 +1752,13 @@ TEST_CASE("JFJochIntegrationTest_TCP_crystal_no_processing", "[JFJochReceiver]") ConvertedToRawGeometry(experiment, image_raw_geom.data(), image_conv.data()); logger.Info("Loaded image"); - // Setup acquisition device - AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); - for (int m = 0; m < experiment.GetModulesNum(); m++) - test->SetInternalGeneratorFrame((uint16_t *) image_raw_geom.data() + m * RAW_MODULE_SIZE, m); - - aq_devices.Add(std::move(test)); + // Setup acquisition device: the card writes the image, with the strong pixels of its spot finder + SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); + settings.enable = false; + settings.indexing = true; + std::vector images; + images.push_back(Int16Image(experiment, pixel_mask, image_raw_geom, settings)); + AcquisitionDeviceGroup aq_devices = MockDevices(experiment, images); TCPStreamPusher pusher("tcp://127.0.0.1:*", 1); TCPImagePuller puller(pusher.GetAddress()[0]); @@ -1813,9 +1772,6 @@ TEST_CASE("JFJochIntegrationTest_TCP_crystal_no_processing", "[JFJochReceiver]") // No progress value at the start of measurement REQUIRE(!service.GetProgress().has_value()); - SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); - settings.enable = false; - settings.indexing = true; service.SetSpotFindingSettings(settings); service.Start(experiment, pixel_mask, nullptr); @@ -1870,13 +1826,17 @@ TEST_CASE("JFJochIntegrationTest_TCP_crystal_spot_and_index", "[JFJochReceiver]" ConvertedToRawGeometry(experiment, image_raw_geom.data(), image_conv.data()); logger.Info("Loaded image"); - // Setup acquisition device - AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); - for (int m = 0; m < experiment.GetModulesNum(); m++) - test->SetInternalGeneratorFrame((uint16_t *) image_raw_geom.data() + m * RAW_MODULE_SIZE, m); - - aq_devices.Add(std::move(test)); + // Setup acquisition device: the card writes the image, with the strong pixels of its spot finder + SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); + settings.signal_to_noise_threshold = 2.5; + settings.photon_count_threshold = 5; + settings.min_pix_per_spot = 1; + settings.max_pix_per_spot = 200; + settings.high_resolution_limit = 2.0; + settings.low_resolution_limit = 50.0; + std::vector images; + images.push_back(Int16Image(experiment, pixel_mask, image_raw_geom, settings)); + AcquisitionDeviceGroup aq_devices = MockDevices(experiment, images); TCPStreamPusher pusher("tcp://127.0.0.1:*", 1); @@ -1891,13 +1851,6 @@ TEST_CASE("JFJochIntegrationTest_TCP_crystal_spot_and_index", "[JFJochReceiver]" // No progress value at the start of measurement REQUIRE(!service.GetProgress().has_value()); - SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); - settings.signal_to_noise_threshold = 2.5; - settings.photon_count_threshold = 5; - settings.min_pix_per_spot = 1; - settings.max_pix_per_spot = 200; - settings.high_resolution_limit = 2.0; - settings.low_resolution_limit = 50.0; service.SetSpotFindingSettings(settings); service.Start(experiment, pixel_mask, nullptr); @@ -1952,9 +1905,7 @@ TEST_CASE("JFJochIntegrationTest_TCP_calibration", "[JFJochReceiver]") { // Setup acquisition device AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); - - aq_devices.Add(std::move(test)); + aq_devices.Add(std::make_unique(0, 64)); TCPStreamPusher pusher("tcp://127.0.0.1:*", 1); @@ -2012,13 +1963,17 @@ TEST_CASE("JFJochIntegrationTest_TCP_crystal_spot_and_index_single_file", "[JFJo ConvertedToRawGeometry(experiment, image_raw_geom.data(), image_conv.data()); logger.Info("Loaded image"); - // Setup acquisition device - AcquisitionDeviceGroup aq_devices; - std::unique_ptr test = std::make_unique(0, 64); - for (int m = 0; m < experiment.GetModulesNum(); m++) - test->SetInternalGeneratorFrame((uint16_t *) image_raw_geom.data() + m * RAW_MODULE_SIZE, m); - - aq_devices.Add(std::move(test)); + // Setup acquisition device: the card writes the image, with the strong pixels of its spot finder + SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); + settings.signal_to_noise_threshold = 2.5; + settings.photon_count_threshold = 5; + settings.min_pix_per_spot = 1; + settings.max_pix_per_spot = 200; + settings.high_resolution_limit = 2.0; + settings.low_resolution_limit = 50.0; + std::vector images; + images.push_back(Int16Image(experiment, pixel_mask, image_raw_geom, settings)); + AcquisitionDeviceGroup aq_devices = MockDevices(experiment, images); TCPStreamPusher pusher("tcp://127.0.0.1:*", 1); @@ -2033,13 +1988,6 @@ TEST_CASE("JFJochIntegrationTest_TCP_crystal_spot_and_index_single_file", "[JFJo // No progress value at the start of measurement REQUIRE(!service.GetProgress().has_value()); - SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); - settings.signal_to_noise_threshold = 2.5; - settings.photon_count_threshold = 5; - settings.min_pix_per_spot = 1; - settings.max_pix_per_spot = 200; - settings.high_resolution_limit = 2.0; - settings.low_resolution_limit = 50.0; service.SetSpotFindingSettings(settings); service.Start(experiment, pixel_mask, nullptr); diff --git a/tests/JFJochStateMachineTest.cpp b/tests/JFJochStateMachineTest.cpp index c501586c3..7f5c4d3a4 100644 --- a/tests/JFJochStateMachineTest.cpp +++ b/tests/JFJochStateMachineTest.cpp @@ -3,7 +3,7 @@ #include #include "../broker/JFJochStateMachine.h" -#include "../acquisition_device/HLSSimulatedDevice.h" +#include "../acquisition_device/MockAcquisitionDevice.h" #include "../receiver/JFJochReceiverService.h" #include "../broker/OpenAPIConvert.h" #include "../broker/ModulePlacementConvert.h" @@ -278,7 +278,7 @@ TEST_CASE("JFJochStateMachine_AsyncStartFailure") { AcquisitionDeviceGroup aq_devices; for (int i = 0; i < experiment.GetDataStreamsNum(); i++) - aq_devices.Add(std::make_unique(i, 64)); + aq_devices.Add(std::make_unique(i, 64)); RefusingImagePusher pusher; JFJochReceiverService receiver_service(aq_devices, logger, pusher); @@ -452,7 +452,7 @@ TEST_CASE("JFJochStateMachine_CancelledRunIsNotAnError") { AcquisitionDeviceGroup aq_devices; for (int i = 0; i < experiment.GetDataStreamsNum(); i++) - aq_devices.Add(std::make_unique(i, 64)); + aq_devices.Add(std::make_unique(i, 64)); GatedPusher pusher; JFJochReceiverService receiver_service(aq_devices, logger, pusher); @@ -508,7 +508,7 @@ TEST_CASE("JFJochStateMachine_WriterFailureAtEndIsAnError") { AcquisitionDeviceGroup aq_devices; for (int i = 0; i < experiment.GetDataStreamsNum(); i++) - aq_devices.Add(std::make_unique(i, 64)); + aq_devices.Add(std::make_unique(i, 64)); FinalizeFailingPusher pusher; JFJochReceiverService receiver_service(aq_devices, logger, pusher); @@ -563,7 +563,7 @@ TEST_CASE("JFJochStateMachine_PreflightRefusesBeforeStarting") { AcquisitionDeviceGroup aq_devices; for (int i = 0; i < experiment.GetDataStreamsNum(); i++) - aq_devices.Add(std::make_unique(i, 64)); + aq_devices.Add(std::make_unique(i, 64)); PreflightRefusingPusher pusher; JFJochReceiverService receiver_service(aq_devices, logger, pusher); @@ -682,7 +682,7 @@ TEST_CASE("JFJochStateMachine_StartModulePlacement") { AcquisitionDeviceGroup aq_devices; for (int i = 0; i < experiment.GetDataStreamsNum(); i++) - aq_devices.Add(std::make_unique(i, 64)); + aq_devices.Add(std::make_unique(i, 64)); StartMessageCapture pusher; JFJochReceiverService receiver_service(aq_devices, logger, pusher); JFJochServices services(logger); diff --git a/tests/MockCardOutput.h b/tests/MockCardOutput.h new file mode 100644 index 000000000..217ee3b17 --- /dev/null +++ b/tests/MockCardOutput.h @@ -0,0 +1,220 @@ +// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute +// SPDX-License-Identifier: GPL-3.0-only + +#pragma once + +// What the FPGA card writes, precalculated on the CPU for the tests on MockAcquisitionDevice. It does not matter how +// it is calculated here: that the mock gives the host what the card gives is checked against the HLS simulation in +// MockAcquisitionDeviceTest.cpp. + +#include "CheckImageOutput.h" +#include "../acquisition_device/AcquisitionDeviceGroup.h" +#include "../acquisition_device/MockAcquisitionDevice.h" +#include "../common/PixelMask.h" +#include "../image_analysis/spot_finding/DetModuleSpotFinder_cpu.h" +#include "../receiver/JFJochReceiverTest.h" + +// The card output of one image, one entry per module (absolute module number) +using CardImage = std::vector>; + +inline std::unique_ptr OutputWithPixels(const uint16_t *pixels) { + auto ret = std::make_unique(); + memcpy(ret->pixels, pixels, RAW_MODULE_SIZE * sizeof(uint16_t)); + return ret; +} + +template +void StorePixels(DeviceOutput &output, const std::vector &values) { + auto pixels = reinterpret_cast(output.pixels); + for (int i = 0; i < RAW_MODULE_SIZE; i++) + pixels[i] = static_cast(values[i]); +} + +// The values in the pixel format the card writes for the experiment +inline void StorePixels(DeviceOutput &output, const DiffractionExperiment &x, const std::vector &values) { + switch (x.GetByteDepthFPGA()) { + case 1: + x.IsPixelSigned() ? StorePixels(output, values) : StorePixels(output, values); + break; + case 2: + x.IsPixelSigned() ? StorePixels(output, values) : StorePixels(output, values); + break; + default: + x.IsPixelSigned() ? StorePixels(output, values) : StorePixels(output, values); + break; + } +} + +// Error and saturated pixels counted, the others summed, masked pixels left out; and the same for each ROI of +// the experiment's ROI map (one bit per ROI) +inline void PixelStatistics(DeviceOutput &output, const DiffractionExperiment &x, const PixelMask &mask, + const std::vector &experiment_roi_map, size_t module, + const std::vector &values, int64_t err, int64_t sat) { + std::vector roi_map(RAW_MODULE_SIZE); + ConvertedToRawGeometry(x, module, roi_map.data(), experiment_roi_map.data()); + const uint32_t *module_mask = mask.GetMaskRaw().data() + module * RAW_MODULE_SIZE; + + auto &stats = output.module_statistics; + stats.max_value = INT24_MIN; + stats.min_value = INT24_MAX; + for (int r = 0; r < FPGA_ROI_COUNT; r++) + output.roi_counts[r].max_value = INT24_MIN; + + for (int i = 0; i < RAW_MODULE_SIZE; i++) { + const int64_t v = values[i]; + if (module_mask[i] != 0) { + stats.masked_pixels++; + continue; + } + if (v == err) + stats.err_pixels++; + else if (v == sat) + stats.saturated_pixels++; + else { + stats.pixel_sum += v; + stats.max_value = std::max(stats.max_value, v); + stats.min_value = std::min(stats.min_value, v); + } + + // roi_calc.cpp: the coordinates include the multipixels + const int64_t line = i / RAW_MODULE_COLS + (i / RAW_MODULE_COLS / 256) * 2; + const int64_t col = i % RAW_MODULE_COLS + (i % RAW_MODULE_COLS / 256) * 2; + for (int r = 0; r < FPGA_ROI_COUNT; r++) { + if (((roi_map[i] >> r) & 1) == 0 || v == err) + continue; + ROICount &roi = output.roi_counts[r]; + if (v != sat) { + roi.sum += v; + roi.sum2 += v * v; + roi.good_pixels++; + roi.sum_x_weighted += v * col; + roi.sum_y_weighted += v * line; + } + roi.max_value = std::max(roi.max_value, v); + } + } +} + +// The strong pixels of the card's spot finder, by its CPU version, on the pixels in the output +inline void FindStrongPixels(DeviceOutput &output, const DiffractionExperiment &x, const SpotFindingSettings &settings, + size_t module) { + std::vector d_map(RAW_MODULE_SIZE), mean(RAW_MODULE_SIZE), stddev(RAW_MODULE_SIZE); + std::vector valid_count(RAW_MODULE_SIZE), strong_pixel(RAW_MODULE_SIZE); + x.CalcSpotFinderResolutionMap(d_map.data(), module); + switch (x.GetByteDepthFPGA()) { + case 1: + FindSpots(output, settings, d_map.data(), mean.data(), stddev.data(), valid_count.data(), + strong_pixel.data()); + break; + case 2: + FindSpots(output, settings, d_map.data(), mean.data(), stddev.data(), valid_count.data(), + strong_pixel.data()); + break; + default: + FindSpots(output, settings, d_map.data(), mean.data(), stddev.data(), valid_count.data(), + strong_pixel.data()); + break; + } +} + +// JUNGFRAU conversion of a raw image (all modules) as CheckImageOutput.h computes it; with host summation the card +// converts single frames to 16 bit. With conversion, error and saturated pixels are counted from the conversion into +// signed 32 bit, as the card counts them before the pixel is written in the image's format, where unsigned they have +// one value; without, the card reads UINT16_MAX from the detector as saturated. +inline CardImage ConvertedImage(const DiffractionExperiment &x, const JFCalibration &calib, const PixelMask &mask, + const std::vector &raw, size_t storage_cell = 0) { + DiffractionExperiment card_x(x); + if (x.IsCPUSummation()) + card_x.Summation(1).BitDepthImage(16); + DiffractionExperiment signed_x(card_x); + signed_x.PixelSigned(true).BitDepthImage(32); + + const auto ref = ConversionRef(card_x, calib, raw.data(), x.GetModulesNum(), storage_cell); + const auto ref_signed = ConversionRef(signed_x, calib, raw.data(), x.GetModulesNum(), storage_cell); + const auto roi_map = x.ExportROIMap(); + + CardImage ret; + std::vector values(RAW_MODULE_SIZE), stats_values(RAW_MODULE_SIZE); + for (int m = 0; m < x.GetModulesNum(); m++) { + for (size_t i = 0; i < RAW_MODULE_SIZE; i++) { + const size_t pxl = m * RAW_MODULE_SIZE + i; + values[i] = std::llround(ref[pxl]); + if (x.IsJungfrauConvPhotonCnt()) + stats_values[i] = std::llround(ref_signed[pxl]); + else + stats_values[i] = (raw[pxl] == UINT16_MAX) ? INT32_MAX : values[i]; + } + auto output = std::make_unique(); + StorePixels(*output, card_x, values); + PixelStatistics(*output, x, mask, roi_map, m, stats_values, INT32_MIN, INT32_MAX); + ret.push_back(std::move(output)); + } + return ret; +} + +// The card passing a signed 16-bit image (raw geometry, all modules) through, INT16_MIN marking an error and +// INT16_MAX a saturated pixel, with the strong pixels of its spot finder +inline CardImage Int16Image(const DiffractionExperiment &x, const PixelMask &mask, const std::vector &image, + const SpotFindingSettings &settings) { + const int64_t err = (x.GetByteDepthFPGA() == 4) ? INT32_MIN : INT16_MIN; + const int64_t sat = (x.GetByteDepthFPGA() == 4) ? INT32_MAX : INT16_MAX; + const auto roi_map = x.ExportROIMap(); + + CardImage ret; + std::vector values(RAW_MODULE_SIZE); + for (int m = 0; m < x.GetModulesNum(); m++) { + for (int i = 0; i < RAW_MODULE_SIZE; i++) { + const int16_t v = image[m * RAW_MODULE_SIZE + i]; + values[i] = (v == INT16_MIN) ? err : ((v == INT16_MAX) ? sat : v); + } + auto output = std::make_unique(); + StorePixels(*output, x, values); + PixelStatistics(*output, x, mask, roi_map, m, values, err, sat); + FindStrongPixels(*output, x, settings, m); + ret.push_back(std::move(output)); + } + return ret; +} + +// Frame F of the mock of data stream d writes images[F % images.size()] +inline void SetOutputs(MockAcquisitionDevice &mock, const DiffractionExperiment &x, uint16_t data_stream, + const std::vector &images) { + for (size_t i = 0; i < images.size(); i++) + for (int m = 0; m < x.GetModulesNum(data_stream); m++) + mock.SetOutput(i, m, *images[i][x.GetFirstModuleOfDataStream(data_stream) + m]); +} + +// One mock per data stream +inline AcquisitionDeviceGroup MockDevices(const DiffractionExperiment &x, const std::vector &images) { + AcquisitionDeviceGroup ret; + for (int d = 0; d < x.GetDataStreamsNum(); d++) { + auto mock = std::make_unique(d, 64); + SetOutputs(*mock, x, d, images); + ret.Add(std::move(mock)); + } + return ret; +} + +// The mocks for JFJochReceiverTest(): the conversion of its raw image with its calibration, the pixel mask applied +// if the experiment asks for it, one image per storage cell +inline AcquisitionDeviceGroup ConvertingMocks(const DiffractionExperiment &x, const PixelMask &mask, + std::vector raw) { + if (x.IsApplyPixelMask()) { + for (int i = 0; i < raw.size(); i++) + if (mask.GetMaskRaw().at(i) != 0) + raw[i] = UINT16_MAX; + } + const JFCalibration calib = GeneratePedestalCalibration(x); + std::vector images; + for (int s = 0; s < x.GetStorageCellNumber(); s++) + images.push_back(ConvertedImage(x, calib, mask, raw, s)); + return MockDevices(x, images); +} + +// The default raw image of JFJochReceiverTest() +inline AcquisitionDeviceGroup ConvertingMocks(const DiffractionExperiment &x, const PixelMask &mask) { + std::vector raw(RAW_MODULE_SIZE * x.GetModulesNum()); + for (int i = 0; i < raw.size(); i++) + raw[i] = i % 65536; + return ConvertingMocks(x, mask, raw); +} diff --git a/tests/StreamWriterTest.cpp b/tests/StreamWriterTest.cpp index 1b767fb64..e797b158e 100644 --- a/tests/StreamWriterTest.cpp +++ b/tests/StreamWriterTest.cpp @@ -12,6 +12,7 @@ #include "../receiver/JFJochReceiverService.h" #include "../image_pusher/ZMQWriterNotificationPuller.h" #include "../image_puller/TestImagePuller.h" +#include "../acquisition_device/MockAcquisitionDevice.h" TEST_CASE("StreamWriterTest_ZMQ", "[StreamWriter]") { RegisterHDF5Filter(); @@ -27,7 +28,7 @@ TEST_CASE("StreamWriterTest_ZMQ", "[StreamWriter]") { JFModuleGainCalibration gain; AcquisitionDeviceGroup aq_devices; for (int i = 0; i < x.GetDataStreamsNum(); i++) - aq_devices.AddHLSDevice(64); + aq_devices.Add(std::make_unique(i, 64)); ZMQStream2Pusher pusher({"ipc://*"}); JFJochReceiverService fpga_receiver_service(aq_devices, logger, pusher); @@ -83,7 +84,7 @@ TEST_CASE("StreamWriterTest_ZMQ_Update", "[StreamWriter]") { JFModuleGainCalibration gain; AcquisitionDeviceGroup aq_devices; for (int i = 0; i < x.GetDataStreamsNum(); i++) - aq_devices.AddHLSDevice(64); + aq_devices.Add(std::make_unique(i, 64)); ZMQStream2Pusher pusher({"ipc://*"}); pusher.WriterNotificationSocket("ipc://*"); @@ -144,7 +145,7 @@ TEST_CASE("StreamWriterTest_ZMQ_Update_NoNotification", "[StreamWriter]") { JFModuleGainCalibration gain; AcquisitionDeviceGroup aq_devices; for (int i = 0; i < x.GetDataStreamsNum(); i++) - aq_devices.AddHLSDevice(64); + aq_devices.Add(std::make_unique(i, 64)); ZMQStream2Pusher pusher({"ipc://*"}); pusher.WriterNotificationSocket("ipc://*"); @@ -208,7 +209,7 @@ TEST_CASE("StreamWriterTest_ZMQ_MasterAfterAllDataFiles", "[StreamWriter]") { AcquisitionDeviceGroup aq_devices; for (int i = 0; i < x.GetDataStreamsNum(); i++) - aq_devices.AddHLSDevice(64); + aq_devices.Add(std::make_unique(i, 64)); ZMQStream2Pusher pusher({"ipc://*", "ipc://*"}); pusher.WriterNotificationSocket("ipc://*"); From c40bebca98edb016dac99c1fecec0b0c1b41cf79 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Tue, 17 Nov 2026 14:56:17 +0100 Subject: [PATCH 4/6] Broker without hardware runs on MockAcquisitionDevice A configuration without a pcie entry gets the mock of the card (AcquisitionDeviceGroup::AddMockDevice) instead of the HLS simulation. The local run of docs/JFJOCH_BROKER.md works as before: the images uploaded as the internal generator's images are streamed and written (checked end to end with jfjoch_broker_test.py: 18 images collected, written and equal to the uploaded image outside the pixel mask); the card's spot finding, ROI and azimuthal results are zero in this mode, as the mock computes nothing. The docs and CLAUDE.md say so. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01UqXYdHHZ4cogS1mrGUtfN7 --- CLAUDE.md | 18 +++++++++++------- acquisition_device/AcquisitionDeviceGroup.cpp | 6 +++--- acquisition_device/AcquisitionDeviceGroup.h | 3 ++- broker/JFJochBrokerParser.cpp | 2 +- docs/JFJOCH_BROKER.md | 8 +++++--- 5 files changed, 22 insertions(+), 15 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index d3ecfc9d2..f2a4ff472 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -317,9 +317,12 @@ Check first and do not duplicate a credit an adjacent comment already carries. ## Local end-to-end run (no detector / no FPGA) -The FPGA HLS logic can be simulated on the CPU (`HLSSimulatedDevice`), so the full software -stack runs without hardware (slowly — fixed-point math on CPU). See -`docs/JFJOCH_BROKER.md` for the canonical walkthrough. +Without a `pcie` entry in its config the broker runs on `MockAcquisitionDevice` +(`acquisition_device/`), a black-box mock of the FPGA card: it reproduces the card's interface +towards the host (frames, completions, counters, start/stop/cancel, internal frame generator, the +error and timeout paths) but computes nothing — the images are the uploaded generator images as +given, and the card's spot-finding/ROI/azimuthal results are zero. The same mock carries the +receiver, state-machine and writer tests. See `docs/JFJOCH_BROKER.md` for the canonical walkthrough. ``` cd build/broker @@ -400,10 +403,11 @@ with cuFFT on a GPU and with pocketfft without one (or under `--no-gpu`), while implementation each. Only rugnux's CLI spells it this way: internally (and in the broker's OpenAPI) `FFT` is the GPU implementation, which the CLI's `cufft` maps onto. -**FPGA** (`fpga/`): `hls/` is the Vitis HLS source (image-analysis kernels), `hls_simulation/` -runs that same HLS on CPU for hardware-free testing, `hdl/` is the Verilog RTL, `host_library/` is -the host-side driver, `pcie_driver/` is the kernel module. The HLS algorithms are documented in -`docs/FPGA_DATA_ANALYSIS.md`. +**FPGA** (`fpga/`): `hls/` is the Vitis HLS source (image-analysis kernels; its CPU library +`JFJochHLSCores` and the Xilinx headers in `fpga/include/` are no longer part of the software +build), `hdl/` is the Verilog RTL, `host_library/` is the host-side driver, `pcie_driver/` is the +kernel module. The HLS algorithms are documented in `docs/FPGA_DATA_ANALYSIS.md`. The software is +tested without hardware on `MockAcquisitionDevice`, not on a simulation of the firmware. **Detector control** (`detector_control/`): wrappers for SLS (JUNGFRAU) and DECTRIS SIMPLON (EIGER). **`jungfrau/`**: JUNGFRAU ADU→energy gain/pedestal calibration. diff --git a/acquisition_device/AcquisitionDeviceGroup.cpp b/acquisition_device/AcquisitionDeviceGroup.cpp index 9decc8da4..64c7a3ab2 100644 --- a/acquisition_device/AcquisitionDeviceGroup.cpp +++ b/acquisition_device/AcquisitionDeviceGroup.cpp @@ -3,7 +3,7 @@ #include "AcquisitionDeviceGroup.h" #include "PCIExpressDevice.h" -#include "HLSSimulatedDevice.h" +#include "MockAcquisitionDevice.h" AcquisitionDevice &AcquisitionDeviceGroup::operator[](int idx) { if ((idx < 0) || (idx >= aq_devices.size())) @@ -28,8 +28,8 @@ void AcquisitionDeviceGroup::AddPCIeDevice(const std::string &device_name, std:: aq_devices.emplace_back(std::move(tmp)); } -void AcquisitionDeviceGroup::AddHLSDevice(int64_t buffer_size_modules) { - aq_devices.emplace_back(std::make_unique(aq_devices.size(), buffer_size_modules)); +void AcquisitionDeviceGroup::AddMockDevice(int64_t buffer_size_modules) { + aq_devices.emplace_back(std::make_unique(aq_devices.size(), buffer_size_modules)); } std::vector AcquisitionDeviceGroup::GetNetworkConfig() { diff --git a/acquisition_device/AcquisitionDeviceGroup.h b/acquisition_device/AcquisitionDeviceGroup.h index 7044936a5..318d16917 100644 --- a/acquisition_device/AcquisitionDeviceGroup.h +++ b/acquisition_device/AcquisitionDeviceGroup.h @@ -14,7 +14,8 @@ public: size_t size(); void Add(std::unique_ptr &&device); void AddPCIeDevice(const std::string &device_name, std::optional ipv4_addr = {}); - void AddHLSDevice(int64_t buffer_size_modules); + // The mock of the card, for the broker without hardware + void AddMockDevice(int64_t buffer_size_modules); std::vector GetNetworkConfig(); std::vector GetDeviceStatus() const; void EnableLogging(Logger *logger); diff --git a/broker/JFJochBrokerParser.cpp b/broker/JFJochBrokerParser.cpp index ffbd460fa..d03ba5387 100644 --- a/broker/JFJochBrokerParser.cpp +++ b/broker/JFJochBrokerParser.cpp @@ -252,7 +252,7 @@ std::unique_ptr ParseImagePusher(const org::openapitools::server::m void ParseAcquisitionDeviceGroup(const org::openapitools::server::model::Jfjoch_settings &input, AcquisitionDeviceGroup &aq_devices) { if (!input.pcieIsSet()) - aq_devices.AddHLSDevice(256); + aq_devices.AddMockDevice(256); else for (auto &p: input.getPcie()) { std::optional ipv4_addr = {}; if (p.ipv4IsSet()) diff --git a/docs/JFJOCH_BROKER.md b/docs/JFJOCH_BROKER.md index bdcf241aa..4ff231d9a 100644 --- a/docs/JFJOCH_BROKER.md +++ b/docs/JFJOCH_BROKER.md @@ -429,9 +429,11 @@ For development, it is possible to set up a local installation of Jungfraujoch. This will work without FPGA installed in the computer and allows testing the Jungfraujoch software layer, including ZeroMQ streaming and file writing. -The workflow simulates FPGA behavior, by running high-level synthesis code on the CPU - the performance is therefore -very low, as fixed-point calculations have a large performance penalty on the CPU. In the CPU simulation mode, one can simulate -using only a single FPGA device. +Without a `pcie` entry in the configuration, the broker runs on a mock of the FPGA card (`MockAcquisitionDevice`): +it behaves like the card towards the host - frames, completions, counters, start/stop/cancel, the internal frame +generator - but computes nothing. The images streamed and written are the ones uploaded as the internal generator's +images, unconverted; the card's spot finding, ROI and azimuthal results are zero in this mode (with rugnux live on, +the GPU analysis finds its own spots). To run the test: From cebc677eb98e0b8cee9edfbcf4311d991d120013 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Tue, 17 Nov 2026 14:56:17 +0100 Subject: [PATCH 5/6] JFJochIntegrationTest_GPUStills_cancel_during_prepass: the frames are held for the pre-pass until the cancel With the mock the generator's 30 frames arrive at once and the run was over before the cancel. The case now gives the card fewer frames than the pre-pass waits for and no final packet, so the frames stay held, whatever the card's speed. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01UqXYdHHZ4cogS1mrGUtfN7 --- tests/JFJochReceiverProcessingTest.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/JFJochReceiverProcessingTest.cpp b/tests/JFJochReceiverProcessingTest.cpp index 68e4c5a4a..312a92dd5 100644 --- a/tests/JFJochReceiverProcessingTest.cpp +++ b/tests/JFJochReceiverProcessingTest.cpp @@ -254,9 +254,10 @@ TEST_CASE("JFJochIntegrationTest_GPUStills_cancel_during_prepass", "[JFJochRecei // A cancel while the frames are held for the pre-pass ends the collection. Logger logger(Catch::getResultCapture().getCurrentTestName()); + // Fewer frames arrive than the pre-pass waits for, and no final packet: the frames stay held until the cancel. // How long the stop takes is not checked. DiffractionExperiment experiment(DetJF4M()); - experiment.ImagesPerTrigger(30).NumTriggers(1).UseInternalPacketGenerator(true) + experiment.ImagesPerTrigger(30).NumTriggers(1).UseInternalPacketGenerator(false) .FilePrefix("gpu_stills_cancel").JungfrauConvPhotonCnt(false) .DetectorDistance_mm(75).BeamY_pxl(1136).BeamX_pxl(1090).IncidentEnergy_keV(12.4) .SetUnitCell(UnitCell{.a = 36.9, .b = 78.95, .c = 78.95, .alpha =90, .beta = 90, .gamma = 90}); @@ -264,7 +265,10 @@ TEST_CASE("JFJochIntegrationTest_GPUStills_cancel_during_prepass", "[JFJochRecei PixelMask pixel_mask(experiment); AcquisitionDeviceGroup aq_devices; - aq_devices.Add(std::make_unique(0, 64)); + auto test = std::make_unique(0, 64); + for (int m = 0; m < experiment.GetModulesNum(); m++) + test->CreateJFPackets(experiment, 1, 10, m, nullptr); + aq_devices.Add(std::move(test)); NonePusher pusher; JFJochReceiverService service(aq_devices, logger, pusher); @@ -273,7 +277,7 @@ TEST_CASE("JFJochIntegrationTest_GPUStills_cancel_during_prepass", "[JFJochRecei service.RugnuxLive(true, nullptr, 4, 60); service.Start(experiment, pixel_mask, nullptr); - std::this_thread::sleep_for(std::chrono::seconds(10)); + std::this_thread::sleep_for(std::chrono::seconds(3)); const auto t0 = std::chrono::steady_clock::now(); service.Cancel(false); JFJochReceiverOutput receiver_out; From 1a38a55b05fe9de1091c6d6c5ee022f9126e2c31 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Tue, 17 Nov 2026 14:56:17 +0100 Subject: [PATCH 6/6] HLS simulation removed from the software build HLSSimulatedDevice, HLSDevice (fpga/hls_simulation) and the tests of the HLS kernels themselves go: FPGANetworkTest, FPGAFrameGeneratorTest, FPGAHostWriterTest, FPGASpotFindingUnitTest, FPGAPTPTest, FPGAHLSModulesTest, and MockAcquisitionDeviceTest, which compared the mock with the HLS device. The receiver, the acquisition-device library and jfjoch_test no longer link JFJochHLSSimulation, and jfjoch_fpga_test loses its -H (HLS simulation) option; without hardware the broker and the tests run on MockAcquisitionDevice. Kept for the firmware: fpga/hls (untouched) with its CPU library JFJochHLSCores, now EXCLUDE_FROM_ALL so the software build does not compile the kernels or the Xilinx headers of fpga/include (the pcie_* firmware targets build it on demand); fpga/hls_simulation/hls_cores.h and sls_packet.h, which the HLS testbenches (fpga/hls_tb), sls_detector.cpp and tools/UDPSimulator.cpp include. The licence notices are unchanged: fpga/include and parallel_stream.h stay, datamover_model.h carried no third-party notice. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01UqXYdHHZ4cogS1mrGUtfN7 --- acquisition_device/AcquisitionDevice.h | 2 +- acquisition_device/CMakeLists.txt | 3 +- acquisition_device/HLSSimulatedDevice.cpp | 117 ---- acquisition_device/HLSSimulatedDevice.h | 49 -- fpga/CMakeLists.txt | 4 +- fpga/hls_simulation/CMakeLists.txt | 10 - fpga/hls_simulation/HLSDevice.cpp | 813 ---------------------- fpga/hls_simulation/HLSDevice.h | 78 --- fpga/hls_simulation/datamover_model.h | 95 --- receiver/CMakeLists.txt | 2 +- tests/CMakeLists.txt | 9 +- tests/FPGAFrameGeneratorTest.cpp | 150 ---- tests/FPGAHLSModulesTest.cpp | 231 ------ tests/FPGAHostWriterTest.cpp | 73 -- tests/FPGANetworkTest.cpp | 630 ----------------- tests/FPGAPTPTest.cpp | 221 ------ tests/FPGASpotFindingUnitTest.cpp | 162 ----- tests/MockAcquisitionDeviceTest.cpp | 449 ------------ tools/jfjoch_fpga_test.cpp | 36 +- 19 files changed, 16 insertions(+), 3118 deletions(-) delete mode 100644 acquisition_device/HLSSimulatedDevice.cpp delete mode 100644 acquisition_device/HLSSimulatedDevice.h delete mode 100644 fpga/hls_simulation/CMakeLists.txt delete mode 100644 fpga/hls_simulation/HLSDevice.cpp delete mode 100644 fpga/hls_simulation/HLSDevice.h delete mode 100644 fpga/hls_simulation/datamover_model.h delete mode 100644 tests/FPGAFrameGeneratorTest.cpp delete mode 100644 tests/FPGAHLSModulesTest.cpp delete mode 100644 tests/FPGAHostWriterTest.cpp delete mode 100644 tests/FPGANetworkTest.cpp delete mode 100644 tests/FPGAPTPTest.cpp delete mode 100644 tests/FPGASpotFindingUnitTest.cpp delete mode 100644 tests/MockAcquisitionDeviceTest.cpp diff --git a/acquisition_device/AcquisitionDevice.h b/acquisition_device/AcquisitionDevice.h index 41f2d30ee..9f6ed39d6 100644 --- a/acquisition_device/AcquisitionDevice.h +++ b/acquisition_device/AcquisitionDevice.h @@ -50,7 +50,7 @@ protected: ThreadSafeFIFO work_request_queue; // Non-owning view of the per-buffer addresses. Each device subclass owns the backing memory - // and its lifecycle: PCIExpressDevice mmap's/munmap's kernel DMA buffers, HLSSimulatedDevice + // and its lifecycle: PCIExpressDevice mmap's/munmap's kernel DMA buffers, MockAcquisitionDevice // points these at plain heap buffers it owns. std::vector buffer_device; Logger *logger; diff --git a/acquisition_device/CMakeLists.txt b/acquisition_device/CMakeLists.txt index abb3bd3ef..1b55695e7 100644 --- a/acquisition_device/CMakeLists.txt +++ b/acquisition_device/CMakeLists.txt @@ -4,7 +4,6 @@ ADD_LIBRARY(JFJochAcquisitionDevice STATIC AcquisitionDevice.cpp AcquisitionDevice.h AcquisitionCounters.cpp AcquisitionCounters.h - HLSSimulatedDevice.cpp HLSSimulatedDevice.h MockAcquisitionDevice.cpp MockAcquisitionDevice.h Completion.cpp Completion.h ../fpga/pcie_driver/jfjoch_fpga.h PCIExpressDevice.cpp PCIExpressDevice.h @@ -12,4 +11,4 @@ ADD_LIBRARY(JFJochAcquisitionDevice STATIC AcquisitionDeviceGroup.cpp AcquisitionDeviceGroup.h) -TARGET_LINK_LIBRARIES(JFJochAcquisitionDevice JFJochDevice JFJochCommon JFJochHLSSimulation JFCalibration) \ No newline at end of file +TARGET_LINK_LIBRARIES(JFJochAcquisitionDevice JFJochDevice JFJochCommon JFCalibration) \ No newline at end of file diff --git a/acquisition_device/HLSSimulatedDevice.cpp b/acquisition_device/HLSSimulatedDevice.cpp deleted file mode 100644 index e36a794dd..000000000 --- a/acquisition_device/HLSSimulatedDevice.cpp +++ /dev/null @@ -1,117 +0,0 @@ -// SPDX-FileCopyrightText: 2024 Filip Leonarski, Paul Scherrer Institute -// SPDX-License-Identifier: GPL-3.0-only - -#include "HLSSimulatedDevice.h" - -HLSSimulatedDevice::HLSSimulatedDevice(uint16_t data_stream, size_t in_frame_buffer_size_modules) - : FPGAAcquisitionDevice(data_stream) { - mac_addr = 0xCCAA11223344; - ipv4_addr = 0x0132010A; - - max_modules = MAX_MODULES_FPGA; - - buffers.reserve(in_frame_buffer_size_modules); - for (size_t i = 0; i < in_frame_buffer_size_modules; i++) { - buffers.push_back(std::make_unique()); // zero-initialised, 64-byte aligned - buffer_device.push_back(reinterpret_cast(buffers.back().get())); - } - - device = std::make_unique(buffer_device); - internal_generator_module_frames = GeneratorModuleFrames(device->GetDataCollectionStatus().hbm_size_bytes); -} - -void HLSSimulatedDevice::CreateFinalPacket(const DiffractionExperiment& experiment) { - device->CreateFinalPacket(); -} - -void HLSSimulatedDevice::CreateJFPacket(const DiffractionExperiment& experiment, uint64_t frame_number, uint32_t eth_packet, - uint32_t module_number, const uint16_t *data, int8_t adjust_axis, uint8_t user) { - device->CreateJFPacket(frame_number, eth_packet, module_number, data, adjust_axis, user); -} - -void HLSSimulatedDevice::CreateJFPackets(const DiffractionExperiment& experiment, uint64_t frame_number_0, uint64_t frames, - uint32_t module_number, const uint16_t *data) { - for (uint64_t i = 0; i < frames; i++) { - for (int j = 0; j < 128; j++) - CreateJFPacket(experiment, frame_number_0 + i, j, module_number, data + (i * 128 + j) * 4096, 0, 0); - } -} - -void HLSSimulatedDevice::CreateEIGERPacket(const DiffractionExperiment &experiment, uint64_t frame_number, - uint32_t eth_packet, uint32_t module_number, uint32_t col, uint32_t row, - const uint16_t *data) { - device->CreateEIGERPacket(frame_number, eth_packet, module_number, col, row, data); -} - -void HLSSimulatedDevice::HW_ReadActionRegister(DataCollectionConfig *job) { - device->HW_ReadActionRegister(job); -} - -void HLSSimulatedDevice::HW_WriteActionRegister(const DataCollectionConfig *job) { - device->HW_WriteActionRegister(job); -} - -void HLSSimulatedDevice::FPGA_StartAction(const DiffractionExperiment &experiment) { - device->FPGA_StartAction(); -} - -void HLSSimulatedDevice::HW_RunInternalGenerator(const FrameGeneratorConfig &config) { - device->HW_RunInternalGenerator(config); -} - -void HLSSimulatedDevice::FPGA_EndAction() { - device->FPGA_EndAction(); - if (const int result = device->GetFrameGeneratorResult(); result != 0) - throw JFJochException(JFJochExceptionCategory::AcquisitionDeviceError, - "Internal frame generator refused its configuration, error code " + std::to_string(result)); -} - - -bool HLSSimulatedDevice::HW_ReadMailbox(uint32_t *values) { - return device->HW_ReadMailbox(values); -} - -void HLSSimulatedDevice::Cancel() { - device->Cancel(); -} - -bool HLSSimulatedDevice::HW_IsIdle() const { - return device->HW_IsIdle(); -} - - -bool HLSSimulatedDevice::HW_SendWorkRequest(uint32_t handle) { - return device->HW_SendWorkRequest(handle); -} - -DataCollectionStatus HLSSimulatedDevice::GetDataCollectionStatus() const { - return device->GetDataCollectionStatus(); -} - -void HLSSimulatedDevice::HW_LoadCalibration(const LoadCalibrationConfig &config) { - device->HW_LoadCalibration(config); -} - -void HLSSimulatedDevice::HW_SetSpotFinderParameters(const SpotFinderParameters ¶ms) { - device->HW_SetSpotFinderParameters(params); -} - -void HLSSimulatedDevice::CreateXfelBunchIDPacket(double pulse_id, uint32_t event_code) { - device->CreateXfelBunchIDPacket(pulse_id, event_code); -} - -DeviceStatus HLSSimulatedDevice::GetDeviceStatus() const { - DeviceStatus status{}; - - strncpy(status.serial_number, "HLS1", sizeof(status.device_number)); - strncpy(status.device_number, "HLS device", sizeof(status.device_number)); - status.fpga_default_mac_addr = mac_addr; - status.eth_link_count = 1; - status.eth_link_status = 1; - status.packets_sls = device->GetSLSPackets(); - status.packets_udp = device->GetUDPPackets(); - status.hbm_0_temp_C = 0; - status.hbm_1_temp_C = 0; - status.fpga_temp_C = 0; - return status; -} \ No newline at end of file diff --git a/acquisition_device/HLSSimulatedDevice.h b/acquisition_device/HLSSimulatedDevice.h deleted file mode 100644 index 83e40ef98..000000000 --- a/acquisition_device/HLSSimulatedDevice.h +++ /dev/null @@ -1,49 +0,0 @@ -// SPDX-FileCopyrightText: 2024 Filip Leonarski, Paul Scherrer Institute -// SPDX-License-Identifier: GPL-3.0-only - -#pragma once - -#include - -#include "../common/DiffractionExperiment.h" -#include "../fpga/hls_simulation/HLSDevice.h" - -#include "FPGAAcquisitionDevice.h" - -class HLSSimulatedDevice : public FPGAAcquisitionDevice { - // Owns the simulated frame buffers. Plain heap (this path is not performance-critical, so no - // NUMA placement and no mmap), but page-aligned (4 KiB) to match the real device's kernel DMA - // buffers - more than enough for the data path's alignment needs (AXI datamover 64 B, - // FPGAIntegrationTest 128 B). Declared before `device` so the buffers outlive the HLSDevice - // action thread that writes into them; buffer_device points into these. - struct alignas(4096) FrameBuffer { uint8_t data[FPGA_BUFFER_LOCATION_SIZE]; }; - std::vector> buffers; - std::unique_ptr device; - - void HW_ReadActionRegister(DataCollectionConfig *job) override; - void HW_WriteActionRegister(const DataCollectionConfig *job) override; - - void FPGA_StartAction(const DiffractionExperiment &experiment) override; - void FPGA_EndAction() override; - bool HW_IsIdle() const override; - bool HW_ReadMailbox(uint32_t *values) override; - bool HW_SendWorkRequest(uint32_t handle) override; - void HW_LoadCalibration(const LoadCalibrationConfig &config) override; - void HW_SetSpotFinderParameters(const SpotFinderParameters ¶ms) override; - void HW_RunInternalGenerator(const FrameGeneratorConfig &config) override; -public: - HLSSimulatedDevice(uint16_t data_stream, size_t in_frame_buffer_size_modules); - ~HLSSimulatedDevice() override = default; - void CreateJFPacket(const DiffractionExperiment& experiment, uint64_t frame_number, uint32_t eth_packet, - uint32_t module_number, const uint16_t *data, int8_t adjust_axis = 0, uint8_t user = 0); - void CreateJFPackets(const DiffractionExperiment& experiment, uint64_t frame_number_0, uint64_t frames, - uint32_t module_number, const uint16_t *data); - void CreateEIGERPacket(const DiffractionExperiment& experiment, uint64_t frame_number, uint32_t eth_packet, - uint32_t module_number, uint32_t col, uint32_t row, - const uint16_t *data); - void CreateXfelBunchIDPacket(double bunchid, uint32_t event_code); - void CreateFinalPacket(const DiffractionExperiment& experiment); - DataCollectionStatus GetDataCollectionStatus() const override; - void Cancel() override; - DeviceStatus GetDeviceStatus() const override; -}; diff --git a/fpga/CMakeLists.txt b/fpga/CMakeLists.txt index 1e0e0553b..2a4e781ef 100644 --- a/fpga/CMakeLists.txt +++ b/fpga/CMakeLists.txt @@ -15,8 +15,8 @@ ELSE() MESSAGE(STATUS "Xilinx HLS compiler not found") ENDIF() -ADD_SUBDIRECTORY(hls) -ADD_SUBDIRECTORY(hls_simulation) +# The CPU library of the HLS kernels is not part of the software build; the firmware targets below build it on demand +ADD_SUBDIRECTORY(hls EXCLUDE_FROM_ALL) ADD_SUBDIRECTORY(pcie_driver) ADD_SUBDIRECTORY(host_library) diff --git a/fpga/hls_simulation/CMakeLists.txt b/fpga/hls_simulation/CMakeLists.txt deleted file mode 100644 index 3320f9d00..000000000 --- a/fpga/hls_simulation/CMakeLists.txt +++ /dev/null @@ -1,10 +0,0 @@ -# SPDX-FileCopyrightText: 2024 Filip Leonarski, Paul Scherrer Institute -# SPDX-License-Identifier: GPL-3.0-only - -ADD_LIBRARY(JFJochHLSSimulation STATIC - HLSDevice.cpp - HLSDevice.h - hls_cores.h - sls_packet.h) - -TARGET_LINK_LIBRARIES(JFJochHLSSimulation JFJochHLSCores) diff --git a/fpga/hls_simulation/HLSDevice.cpp b/fpga/hls_simulation/HLSDevice.cpp deleted file mode 100644 index 828c80bcb..000000000 --- a/fpga/hls_simulation/HLSDevice.cpp +++ /dev/null @@ -1,813 +0,0 @@ -// SPDX-FileCopyrightText: 2024 Filip Leonarski, Paul Scherrer Institute -// SPDX-License-Identifier: GPL-3.0-only - -#include -#include -#include - -#include "HLSDevice.h" -#include "hls_cores.h" -#include "datamover_model.h" -#include "sls_packet.h" - -struct HLSDeviceImpl { - AXI_STREAM din_eth; - AXI_STREAM dout_eth; - - constexpr static const size_t hbm_if_count = 32; - constexpr static const size_t hbm_if_size = 32 * 1024 * 1024LU; - - std::vector> hbm; - - hls::stream > work_request_stream; - hls::stream > completion_stream; - - Datamover<512> datamover_in; - Datamover<512> datamover_out; - Datamover<256, 4> datamover_in_hbm_0; - Datamover<256, 4> datamover_in_hbm_1; - Datamover<256, 4> datamover_in_hbm_2; - Datamover<256, 4> datamover_out_hbm_0; - Datamover<256, 4> datamover_out_hbm_1; - Datamover<256, 4> datamover_out_hbm_2; - - ap_uint<1> run_data_collection; - ap_uint<1> cancel_data_collection; - volatile ap_uint<1> host_writer_idle; - std::vector pixel_mask; - - HLSDeviceImpl() - : hbm(hbm_if_size / 32 * hbm_if_count), - pixel_mask(MAX_MODULES_FPGA*RAW_MODULE_SIZE/32), - datamover_in(Direction::Input, nullptr), - datamover_out(Direction::Output, nullptr), - datamover_out_hbm_0(Direction::Output, (char *) hbm.data()), - datamover_out_hbm_1(Direction::Output, (char *) hbm.data()), - datamover_out_hbm_2(Direction::Output, (char *) hbm.data()), - datamover_in_hbm_0(Direction::Input, (char *) hbm.data()), - datamover_in_hbm_1(Direction::Input, (char *) hbm.data()), - datamover_in_hbm_2(Direction::Input, (char *) hbm.data()) {} -}; - -HLSDevice::HLSDevice(std::vector &buffer_device) : idle(true), dma_address_table(65536) { - impl_ = std::make_unique(); - - auto in_mem_location32 = (uint32_t *) dma_address_table.data(); - - for (int i = 0; i < buffer_device.size(); i++) { - in_mem_location32[2 * i ] = ((uint64_t) buffer_device[i]) & UINT32_MAX; - in_mem_location32[2 * i + 1] = ((uint64_t) buffer_device[i]) >> 32; - } -} - -HLSDevice::~HLSDevice() { - if (action_thread.joinable()) - action_thread.join(); - impl_.reset(); -} - -void HLSDevice::CreateFinalPacket() { - CreateJFPacket(UINT64_MAX, 0, 0, nullptr, false); -} - -void HLSDevice::SendPacket(char *buffer, int len, uint8_t user) { - auto obuff = (ap_uint<512> *)buffer; - - for (int i = 0; i < (len + 63) / 64; i++) { - packet_512_t packet_in; - if (i == (len + 63) / 64 - 1) packet_in.last = 1; - else packet_in.last = 0; - packet_in.keep = 0xFFFFFFFFFFFFFFFF; - packet_in.user = user; - packet_in.data = obuff[i]; - impl_->din_eth.write(packet_in); - } -} - -#pragma GCC push_options -#pragma GCC optimize ("O0") - -uint16_t checksum(const uint16_t *addr, size_t count) { -/* Compute Internet Checksum for "count" bytes - * beginning at location "addr". - */ - uint64_t sum = 0; - - for (int i = 0; i < count / 2; i++) - sum += addr[i]; - -/* Add left-over byte, if any */ - if (count % 2 == 1) - sum += ((uint8_t *) addr)[count / 2]; - -/* Fold 32-bit sum to 16 bits */ - while (sum>>16) - sum = (sum & 0xffff) + (sum >> 16); - - return ~sum; -} - -void HLSDevice::CreateJFPacket(uint64_t frame_number, uint32_t eth_packet, - uint32_t module_number, const uint16_t *data, int8_t adjust_axis, uint8_t user) { - std::vector buff(256*64); - - auto packet = (jf_raw_packet *)buff.data(); - - packet->ether_type = htons(0x0800); - packet->sour_mac[0] = 0x00; // module 0 - - uint64_t tmp_mac = mac_addr; - for (int i = 0; i < 6; i++) - packet->dest_mac[i] = (tmp_mac >> (8*i)) % 256; - - uint32_t half_module = ((eth_packet >= 64) ? 1 : 0); - - packet->ipv4_header_h = htons(0x4500); // Big endian in IP header! - packet->ipv4_header_total_length = htons(8268); // Big endian in IP header! - packet->ipv4_header_dest_ip = ipv4_addr; - packet->ipv4_header_sour_ip = 0; - packet->ipv4_header_ttl_protocol = htons(0x0011); - packet->ipv4_header_checksum = checksum( (uint16_t *) (buff.data() + 14), 20); // checksum is already in network order - - packet->udp_dest_port = htons(2 * module_number + half_module); - packet->udp_sour_port = htons(0xDFAC); - packet->udp_length = htons(8248); - - // JF headers are little endian - packet->jf.detectortype = SLS_DETECTOR_TYPE_JUNGFRAU; - packet->jf.timestamp = 0xABCDEF0000FEDCBAL; - packet->jf.bunchid = 0x1234567898765431L; - packet->jf.row = half_module; - packet->jf.column = 0; - packet->jf.framenum = frame_number; - packet->jf.packetnum = eth_packet % 64; - if (data != nullptr) { - for (int i = 0; i < 4096; i++) - packet->jf.data[i] = data[i]; - } - packet->udp_checksum = htons(checksum( (uint16_t *) (buff.data()+42), 8192+48)); - - SendPacket(buff.data(), (130+adjust_axis)*64, user); -} - -void HLSDevice::CreateEIGERPacket(uint64_t frame_number, - uint32_t eth_packet, uint32_t module_number, uint32_t col, uint32_t row, - const uint16_t *data) { - std::vector buff(256*64); - - auto packet = (eiger_raw_packet *)buff.data(); - - packet->ether_type = htons(0x0800); - packet->sour_mac[0] = 0x00; // module 0 - - uint64_t tmp_mac = mac_addr; - for (int i = 0; i < 6; i++) - packet->dest_mac[i] = (tmp_mac >> (8*i)) % 256; - - packet->ipv4_header_h = htons(0x4500); // Big endian in IP header! - packet->ipv4_header_total_length = htons(4172); // Big endian in IP header! - packet->ipv4_header_dest_ip = ipv4_addr; - packet->ipv4_header_sour_ip = 0x0; - - packet->ipv4_header_ttl_protocol = htons(0x0011); - packet->ipv4_header_checksum = checksum( (uint16_t *) (buff.data() + 14), 20); // checksum is already in network order - - packet->udp_dest_port = htons(2 * module_number + row); - packet->udp_sour_port = htons(0xDFAC); - packet->udp_length = htons(4152); - - // JF headers are little endian - packet->eiger.detectortype = SLS_DETECTOR_TYPE_EIGER; - packet->eiger.timestamp = 0xABCDEF0000FEDCBAL; - packet->eiger.bunchid = 0x1234567898765431L; - packet->eiger.row = (row % 2); - packet->eiger.column = col % 2; - packet->eiger.framenum = frame_number; - packet->eiger.packetnum = eth_packet % 128; - if (data != nullptr) { - for (int i = 0; i < 2048; i++) - packet->eiger.data[i] = data[i]; - } - packet->udp_checksum = htons(checksum( (uint16_t *) (buff.data()+42), 4096+48)); - - SendPacket(buff.data(), 66*64, 0); -} - -#pragma GCC pop_options - -void HLSDevice::HW_ReadActionRegister(DataCollectionConfig *job) { - memcpy(job, &cfg, sizeof(DataCollectionConfig)); -} - -void HLSDevice::HW_WriteActionRegister(const DataCollectionConfig *job) { - memcpy(&cfg, job, sizeof(DataCollectionConfig)); -} - -void HLSDevice::FPGA_StartAction() { - if (action_thread.joinable()) - action_thread.join(); - - run_counter += 1; - idle = false; - - impl_->datamover_out.ClearCompletedDescriptors(); - - action_thread = std::thread(&HLSDevice::HLSMainThread, this ); -} - -void HLSDevice::FrameGeneratorFuture(FrameGeneratorConfig config) { - frame_generator_result = frame_generator(impl_->din_eth, - impl_->hbm.data(), - impl_->hbm.data(), - impl_->hbm_if_size, - mac_addr, - ipv4_addr, - impl_->cancel_data_collection, - config); - // A generator that refused its configuration sends no frame: the data collection ends instead of waiting for them - if (frame_generator_result != 0) - impl_->cancel_data_collection = 1; -} - -int HLSDevice::GetFrameGeneratorResult() const { - return frame_generator_result; -} - -void HLSDevice::HW_RunInternalGenerator(const FrameGeneratorConfig &config) { - frame_generator_result = 0; - frame_generator_future = std::async(std::launch::async, &HLSDevice::FrameGeneratorFuture, this, config); -} - -void HLSDevice::FPGA_EndAction() { - if (action_thread.joinable()) - action_thread.join(); -} - -bool HLSDevice::HW_ReadMailbox(uint32_t *values) { - std::unique_lock ul(completion_mutex); - - ap_uint<32> tmp; - bool ret = impl_->completion_stream.read_nb(tmp); - values[0] = tmp; - // equivalent to driver functionality - if (ret) { - uint32_t data_collection_id = (values[0] >> 16) & 0xFFFF; - uint32_t handle = values[0] & 0xFFFF; - if (handle == HANDLE_START) - completion_count = 0; - else if ((handle != HANDLE_END) && (data_collection_id != DATA_COLLECTION_ID_PURGE)) { - completion_count++; - while (completion_count * DMA_DESCRIPTORS_PER_MODULE > impl_->datamover_out.GetCompletedDescriptors()) - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } - } - return ret; -} - -void HLSDevice::Cancel() { - impl_->cancel_data_collection = 1; -} - -bool HLSDevice::HW_IsIdle() const { - return idle && impl_->datamover_out.IsIdle(); -} - - -bool HLSDevice::HW_SendWorkRequest(uint32_t handle) { - impl_->work_request_stream.write(handle); - return true; -} - -inline uint32_t float2uint(float f) { - float_uint32 fu; - fu.f = f; - return fu.u; -} - -void HLSDevice::HLSMainThread() { - ap_uint<1> clear_counters = 0; - - uint64_t packets_processed; - - std::vector hls_cores; - - STREAM_512 ip1("ip1"), udp1("udp1"), udp2("udp2"), icmp1("icmp1"), arp1("arp1"), ptp1("ptp1"); - - STREAM_768 stream_768_00("stream_768_00"); - STREAM_768 stream_768_01("stream_768_01"); - STREAM_768 stream_768_02("stream_768_02"); - STREAM_768 stream_768_03("stream_768_03"); - STREAM_768 stream_768_04("stream_768_04"); - STREAM_768 stream_768_0("stream_768_0"); - STREAM_768 stream_768_1("stream_768_1"); - STREAM_768 stream_768_2("stream_768_2"); - STREAM_768 stream_768_3("stream_768_3"); - STREAM_768 stream_768_4("stream_768_4"); - STREAM_768 stream_768_4a("stream_768_4a"); - STREAM_768 stream_768_5("stream_768_5"); - STREAM_768 stream_768_6("stream_768_6"); - STREAM_768 stream_768_7("stream_768_7"); - STREAM_768 stream_768_8("stream_768_8"); - - STREAM_512 data_0("data_0"); - STREAM_512 data_1("data_1"); - hls::stream, 2> data_2("data_2"); - STREAM_768 data_3("data_3"); - STREAM_768 data_4("data_4"); - STREAM_768 data_5("data_5"); - STREAM_768 data_6("data_6"); - STREAM_512 data_7("data_7"); - STREAM_512 data_8("data_8"); - STREAM_512 data_9("data_9"); - STREAM_512 data_10("data_10"); - STREAM_512 data_12("data_12"); - STREAM_512 data_13("data_13"); - - hls::stream addr0("addr0"); - hls::stream addr1("addr1"); - hls::stream addr2("addr2"); - - hls::stream axi_compl[13]; - - hls::stream> hbm_handles("hbm_handles"); - hls::stream> adu_histo_result("adu_histo_result"); - - hls::stream> integration_result_0("integration_result_0"); - hls::stream> integration_result_1("integration_result_1"); - - hls::stream> spot_finder_result_0("spot_finder_result_0"); - hls::stream> spot_finder_result_1("spot_finder_result_1"); - hls::stream> spot_finder_result_0a("spot_finder_result_0a"); - hls::stream> spot_finder_conn_0("spot_finder_conn_0"); - hls::stream> spot_finder_result_2("spot_finder_result_2"); - hls::stream> spot_finder_result_3("spot_finder_result_3"); - - hls::stream> roi_calc_result_0("roi_calc_result_0"); - hls::stream> pixel_count_result_0("pixel_calc_result_0"); - - hls::stream > udp_metadata("udp_metadata"); - - volatile ap_uint<1> idle_data_collection = 1; - ap_uint<1> load_to_hbm_idle; - ap_uint<1> save_to_hbm_idle; - ap_uint<1> integration_idle; - ap_uint<1> stream512to768_idle; - ap_uint<1> stream768to512_idle; - ap_uint<1> frame_summation_idle; - - volatile bool done = false; - volatile bool udp_done = false; // done AND udp_idle - volatile bool sls_done = false; // done AND sls_idle - - // Sent gratuitous ARP message - arp(arp1, impl_->dout_eth, mac_addr, ipv4_addr, 1); - - volatile rcv_state_t state = RCV_INIT; - impl_->run_data_collection = 0; - - // Read state, check if ready to start - data_collection_fsm(data_0, data_1, - addr0, addr1, - impl_->run_data_collection, - impl_->cancel_data_collection, - idle_data_collection, - cfg.mode, - float2uint(cfg.energy_kev), - cfg.nframes, - cfg.nmodules, - cfg.nstorage_cells, - cfg.nsummation , - cfg.sqrtmult, - cfg.pxlthreshold_min, - cfg.pxlthreshold_max, - state); - - impl_->run_data_collection = 1; - impl_->cancel_data_collection = 0; - - if (state != rcv_state_t::RCV_WAIT_FOR_START) - throw std::runtime_error("HLS device not ready to receive data"); - - // Start data collection - data_collection_fsm(data_0, data_1, - addr0, addr1, - impl_->run_data_collection, - impl_->cancel_data_collection, - idle_data_collection, - cfg.mode, - float2uint(cfg.energy_kev), - cfg.nframes, - cfg.nmodules, - cfg.nstorage_cells, - cfg.nsummation , - cfg.sqrtmult, - cfg.pxlthreshold_min, - cfg.pxlthreshold_max, - state); - - impl_->run_data_collection = 0; - data_collection_fsm(data_0, data_1, - addr0, addr1, - impl_->run_data_collection, - impl_->cancel_data_collection, - idle_data_collection, - cfg.mode, - float2uint(cfg.energy_kev), - cfg.nframes, - cfg.nmodules, - cfg.nstorage_cells, - cfg.nsummation, - cfg.sqrtmult, - cfg.pxlthreshold_min, - cfg.pxlthreshold_max, - state); - - hls_cores.emplace_back([&] { - while (!udp_done) { - if (impl_->din_eth.empty()) - std::this_thread::sleep_for(std::chrono::microseconds(10)); - else - ethernet(impl_->din_eth, ip1, arp1, ptp1, - mac_addr, 1, 0, eth_packets, clear_counters); - } - }); - - hls_cores.emplace_back([&] { - while (!udp_done) { - if (ip1.empty()) - std::this_thread::sleep_for(std::chrono::microseconds(10)); - else - ipv4(ip1, udp1, icmp1, ipv4_addr, 0); - } - }); - - hls_cores.emplace_back([&] { - ap_uint<1> udp_idle = 1; - while (!done || !udp_idle) { - if (udp1.empty()) - std::this_thread::sleep_for(std::chrono::microseconds(10)); - else - udp(udp1, udp2, udp_metadata, udp_packets, clear_counters, udp_idle); - } - udp_done = true; - }); - - hls_cores.emplace_back([&] { - ap_uint<1> sls_idle = 1; - - while (!done || !sls_idle) { - if (udp2.empty()) - std::this_thread::sleep_for(std::chrono::microseconds (10)); - else - sls_detector(udp2, udp_metadata, data_0, addr0, sls_packets, udp_eth_err, udp_len_err, current_pulse_id, - cfg.data_stream, clear_counters, sls_idle); - } - sls_done = true; - }); - - // 1. Parse incoming UDP packets - hls_cores.emplace_back([&] { - while ((state != RCV_WAIT_FOR_START) || (idle_data_collection.read() == 0) || (!data_0.empty())) { - data_collection_fsm(data_0, data_1, - addr0, addr1, - impl_->run_data_collection, - impl_->cancel_data_collection, - idle_data_collection, - cfg.mode, - float2uint(cfg.energy_kev), - cfg.nframes, - cfg.nmodules, - cfg.nstorage_cells, - cfg.nsummation, - cfg.sqrtmult, - cfg.pxlthreshold_min, - cfg.pxlthreshold_max, - state); - } - done = true; - }); - - hls_cores.emplace_back([&] { stream512to768(data_1, data_2, addr1, addr2, stream512to768_idle);}); - - // 2. Cache images in HBM - hls_cores.emplace_back([&] { save_to_hbm(addr2, axi_compl[0], hbm_handles, - impl_->datamover_out_hbm_0.GetCtrlStream(), - impl_->datamover_out_hbm_1.GetCtrlStream(), - impl_->datamover_out_hbm_2.GetCtrlStream(), - save_to_hbm_idle, - impl_->hbm_if_size);}); - - hls_cores.emplace_back([&] { save_to_hbm_data(data_2, - data_3, - impl_->datamover_out_hbm_0.GetDataStream(), - impl_->datamover_out_hbm_1.GetDataStream(), - impl_->datamover_out_hbm_2.GetDataStream());}); - - hls_cores.emplace_back([&] {frame_summation_reorder_compl(data_3, data_4, axi_compl[0], axi_compl[1]);}); - - uint32_t ignore_counter = 0; - hls_cores.emplace_back([&] { load_from_hbm(data_4, data_5, axi_compl[1], axi_compl[3], hbm_handles, - impl_->datamover_in_hbm_0.GetDataStream(), - impl_->datamover_in_hbm_1.GetDataStream(), - impl_->datamover_in_hbm_2.GetDataStream(), - impl_->datamover_in_hbm_0.GetCtrlStream(), - impl_->datamover_in_hbm_1.GetCtrlStream(), - impl_->datamover_in_hbm_2.GetCtrlStream(), - load_to_hbm_idle, ignore_counter, - impl_->hbm_if_size);}); - - // 3. Calculate histogram of ADU values - hls_cores.emplace_back([&] { adu_histo(data_5, stream_768_01, adu_histo_result, axi_compl[3], axi_compl[4]);}); - - // 4. Mask missing pixels - hls_cores.emplace_back([&] { mask_missing(stream_768_01, stream_768_02, axi_compl[4], axi_compl[5]);}); - - // 5. Handle EIGER packets properly - hls_cores.emplace_back([&] { eiger_reorder(stream_768_02, stream_768_03, axi_compl[5], axi_compl[6]);}); - - // 6. Apply pixel mask - hls_cores.emplace_back([&] { - pixel_mask(stream_768_03, - stream_768_04, - impl_->pixel_mask.data(), - axi_compl[6], - axi_compl[7]); - }); - - // 7. Apply pedestal & gain corrections - hls_cores.emplace_back([&] { - jf_conversion(stream_768_04, stream_768_0, - axi_compl[7], axi_compl[8], - impl_->hbm.data(), - impl_->hbm.data(), - impl_->hbm.data(), - impl_->hbm.data(), - impl_->hbm.data(), - impl_->hbm.data(), - impl_->hbm.data(), - impl_->hbm.data(), - impl_->hbm.data(), - impl_->hbm.data(), - impl_->hbm.data(), - impl_->hbm.data(), - impl_->hbm_if_size); - }); - - // 8. Pixel thresholding - hls_cores.emplace_back([&] { pixel_threshold(stream_768_0, stream_768_1);}); - - // 9. Frame summation - hls_cores.emplace_back([&] { frame_summation(stream_768_1, stream_768_2, axi_compl[8], axi_compl[9], frame_summation_idle);}); - - // 10. Integration of pixels - hls_cores.emplace_back([&] { integration(stream_768_2, stream_768_3, integration_result_0, axi_compl[9], axi_compl[10], - impl_->hbm.data(), impl_->hbm.data(), impl_->hbm.data(), impl_->hbm.data(), integration_idle, impl_->hbm_if_size);}); - hls_cores.emplace_back([&] { axis_64_to_512(integration_result_0, integration_result_1);}); - - // 11. Spot finding - ap_uint<32> tmp_snr_threshold = float2uint(spot_finder_parameters.snr_threshold); - ap_uint<32> min_d = float2uint(spot_finder_parameters.min_d); - ap_uint<32> max_d = float2uint(spot_finder_parameters.max_d); - ap_int<32> tmp_count_threshold = spot_finder_parameters.count_threshold; - ap_uint<32> min_pix_per_spot = spot_finder_parameters.min_pix_per_spot; - - hls_cores.emplace_back([&] { - spot_finder_mask(stream_768_3, - stream_768_4, - axi_compl[10], - axi_compl[11], - impl_->hbm.data(), - impl_->hbm.data(), - min_d, - max_d, - impl_->hbm_if_size); - }); - - hls_cores.emplace_back([&] { - spot_finder(stream_768_4, stream_768_4a, spot_finder_result_0, tmp_count_threshold, tmp_snr_threshold); - }); - - hls_cores.emplace_back([&] { - while (spot_finder_result_0.read().user == 0); - }); - - hls_cores.emplace_back([&] { - spot_finder(stream_768_4a, stream_768_5, spot_finder_result_0a, tmp_count_threshold, tmp_snr_threshold); - }); - - hls_cores.emplace_back([&] { - spot_finder_connectivity(spot_finder_result_0a, - spot_finder_result_1, - spot_finder_conn_0); - }); - - hls_cores.emplace_back([&] { - spot_finder_merge(spot_finder_result_1, - spot_finder_conn_0, - spot_finder_result_2, - min_pix_per_spot); - }); - - hls_cores.emplace_back([&] { - axis_32_to_512(spot_finder_result_2, spot_finder_result_3); - }); - - // 12. ROI calculation - hls_cores.emplace_back([&] { - roi_calc(stream_768_5, - stream_768_6, - roi_calc_result_0, - axi_compl[11], - axi_compl[12], - impl_->hbm.data(), - impl_->hbm.data(), - impl_->hbm_if_size); - }); - - // 13. Calc pixel statistics - hls_cores.emplace_back([&] { pixel_calc(stream_768_6, stream_768_7, pixel_count_result_0); }); - - // 14. Pixel sq root - hls_cores.emplace_back([&] { pixel_sqrt(stream_768_7,stream_768_8); }); - - // 15. Reduce/extend 24-bit stream - hls_cores.emplace_back([&] { stream768to512(stream_768_8, data_12, stream768to512_idle);}); - - // 16. Prepare data to write to host memory - hls_cores.emplace_back([&] { - ap_uint<3> state; - host_writer(data_12, adu_histo_result, integration_result_1, spot_finder_result_3, roi_calc_result_0, - pixel_count_result_0, - axi_compl[12], impl_->datamover_out.GetDataStream(), - impl_->datamover_out.GetCtrlStream(), impl_->work_request_stream, impl_->completion_stream, - dma_address_table.data(), packets_processed, impl_->host_writer_idle, impl_->cancel_data_collection, state); - }); - - for (auto &i : hls_cores) - i.join(); - - if (frame_generator_future.valid()) - frame_generator_future.get(); - - // reset static counter - arp(arp1, impl_->dout_eth, mac_addr, ipv4_addr, 0); - try { - while (!impl_->din_eth.empty()) - impl_->din_eth.read(); - - if (!addr1.empty()) - throw std::runtime_error("Addr1 queue not empty"); - - if (!addr2.empty()) - throw std::runtime_error("Addr2 queue not empty"); - - if (!data_1.empty()) - throw std::runtime_error("data_1 queue not empty"); - - if (!data_3.empty()) - throw std::runtime_error("data_3 queue not empty"); - - if (!data_4.empty()) - throw std::runtime_error("data_4 queue not empty"); - - if (!data_5.empty()) - throw std::runtime_error("data_5 queue not empty"); - - if (!data_7.empty()) - throw std::runtime_error("data_7 queue not empty"); - - if (!data_8.empty()) - throw std::runtime_error("data_8 queue not empty"); - - if (!data_9.empty()) - throw std::runtime_error("data_9 queue not empty"); - - if (!data_10.empty()) - throw std::runtime_error("data_10 queue not empty"); - - if (!data_12.empty()) - throw std::runtime_error("data_12 queue not empty"); - - if (!data_13.empty()) - throw std::runtime_error("data_13 queue not empty"); - - for (auto &c: axi_compl) { - if (!c.empty()) - throw std::runtime_error("Compl queue not empty"); - } - - if (!stream_768_0.empty()) - throw std::runtime_error("stream_768_0 queue not empty"); - - if (!stream_768_2.empty()) - throw std::runtime_error("stream_768_2 queue not empty"); - - if (!stream_768_3.empty()) - throw std::runtime_error("stream_768_3 queue not empty"); - - if (!stream_768_4.empty()) - throw std::runtime_error("stream_768_4 queue not empty"); - - if (!stream_768_5.empty()) - throw std::runtime_error("stream_768_5 queue not empty"); - - if (!hbm_handles.empty()) - throw std::runtime_error("Handles queue not empty"); - - if (!integration_result_0.empty()) - throw std::runtime_error("Integration result queue not empty"); - - if (!integration_result_1.empty()) - throw std::runtime_error("Integration result queue not empty"); - - if (!impl_->datamover_in.GetDataStream().empty()) - throw std::runtime_error("Datamover queue is not empty"); - - while (!impl_->datamover_out.IsIdle()) - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - } catch (const std::runtime_error &e) { - throw e; - } - idle = true; -} - -DataCollectionStatus HLSDevice::GetDataCollectionStatus() const { - DataCollectionStatus status{}; - - union { - uint64_t u64; - double d; - } pulse_id_conv; - pulse_id_conv.u64 = current_pulse_id; - - status.ctrl_reg = ap_uint<1>(impl_->host_writer_idle) ? (1 << 4) : 0; - status.max_modules = MAX_MODULES_FPGA; - status.hbm_size_bytes = impl_->hbm_if_size; - status.run_counter = run_counter; - status.current_pulseid = pulse_id_conv.d; - status.packets_udp = udp_packets; - status.packets_sls = sls_packets; - - return status; -} - -void HLSDevice::HW_LoadCalibration(const LoadCalibrationConfig &config) { - int ret = load_calibration(impl_->hbm.data(), impl_->hbm.data(), - impl_->pixel_mask.data(), - config, - impl_->hbm_if_size, - impl_->datamover_in.GetCtrlStream(), - impl_->datamover_in.GetDataStream(), - dma_address_table.data()); - if (ret) - throw std::runtime_error("Error in loading calibration " + std::to_string(ret)); - - if (!impl_->datamover_in.GetDataStream().empty()) - throw std::runtime_error("Datamover queue is not empty"); -} - -void HLSDevice::HW_SetSpotFinderParameters(const SpotFinderParameters ¶ms) { - spot_finder_parameters = params; -} - -void HLSDevice::CreateXfelBunchIDPacket(double pulse_id, uint32_t event_code) { - - union { - uint64_t u64; - double d; - } pulse_id_conv; - pulse_id_conv.d = pulse_id; - - bunchid_raw_packet packet{}; - - packet.ether_type = htons(0x0800); - for (int i = 0; i < 6; i++) - packet.dest_mac[i] = 0xFF; - - packet.ipv4_header_h = htons(0x4500); // Big endian in IP header! - packet.ipv4_header_total_length = htons(sizeof(bunchid_payload) + 8 + 20); // Big endian in IP header! - packet.ipv4_header_dest_ip = UINT32_MAX; - - packet.ipv4_header_ttl_protocol = htons(0x0011); - packet.ipv4_header_checksum = checksum( (uint16_t *) &packet.ipv4_header_h, 20); // checksum is already in network order - - packet.udp_length = htons(sizeof(bunchid_payload) + 8); - packet.payload.magicn[0] = BUNCHID_MAGICN; - packet.payload.magicn[1] = BUNCHID_MAGICN; - packet.payload.magicn[2] = BUNCHID_MAGICN; - packet.payload.bunchid_msb[0] = (pulse_id_conv.u64 >> 32) & UINT32_MAX; - packet.payload.bunchid_msb[1] = (pulse_id_conv.u64 >> 32) & UINT32_MAX; - packet.payload.bunchid_lsb[0] = pulse_id_conv.u64 & UINT32_MAX; - packet.payload.bunchid_lsb[1] = pulse_id_conv.u64 & UINT32_MAX; - packet.payload.montrig = event_code; - - SendPacket((char *) &packet, sizeof(bunchid_raw_packet)); -} - -uint64_t HLSDevice::GetUDPPackets() { - return udp_packets; -} - -uint64_t HLSDevice::GetSLSPackets() { - return sls_packets; -} diff --git a/fpga/hls_simulation/HLSDevice.h b/fpga/hls_simulation/HLSDevice.h deleted file mode 100644 index d5294c8ec..000000000 --- a/fpga/hls_simulation/HLSDevice.h +++ /dev/null @@ -1,78 +0,0 @@ -// SPDX-FileCopyrightText: 2024 Filip Leonarski, Paul Scherrer Institute -// SPDX-License-Identifier: GPL-3.0-only - -#pragma once - -#include -#include -#include - -#include "../pcie_driver/jfjoch_fpga.h" - -struct HLSDeviceImpl; - -class HLSDevice { - // Hide all HLS simulation structures here - std::unique_ptr impl_; - - std::mutex completion_mutex; - uint32_t completion_count; - - std::thread action_thread; - std::future frame_generator_future; - std::atomic frame_generator_result{0}; - std::vector dma_address_table; - uint64_t eth_packets = 0; - uint64_t icmp_packets = 0; - uint64_t udp_packets = 0; - uint64_t sls_packets = 0; - uint32_t udp_len_err = 0; - uint32_t udp_eth_err = 0; - - uint64_t current_pulse_id = 0; - uint32_t run_counter = 0; - - DataCollectionConfig cfg; - SpotFinderParameters spot_finder_parameters; - - const uint64_t mac_addr = 0xCCAA11223344; - const uint32_t ipv4_addr = 0x0132010A; - volatile bool idle; - - void FrameGeneratorFuture(FrameGeneratorConfig config); - void HLSMainThread(); -public: - explicit HLSDevice(std::vector &device_buffer); - ~HLSDevice(); - void SendPacket(char *buffer, int len, uint8_t user = 0); - void CreateJFPacket(uint64_t frame_number, uint32_t eth_packet, - uint32_t module_number, const uint16_t *data, int8_t adjust_axis = 0, uint8_t user = 0); - void CreateEIGERPacket(uint64_t frame_number, uint32_t eth_packet, - uint32_t module_number, uint32_t col, uint32_t row, - const uint16_t *data); - void CreateFinalPacket(); - void CreateXfelBunchIDPacket(double bunchid, uint32_t event_code); - - DataCollectionStatus GetDataCollectionStatus() const ; - // What frame_generator returned in the last run, 0 if it was not run or went fine - int GetFrameGeneratorResult() const; - void Cancel() ; - - void HW_ReadActionRegister(DataCollectionConfig *job); - void HW_WriteActionRegister(const DataCollectionConfig *job); - - void FPGA_StartAction(); - void FPGA_EndAction(); - bool HW_IsIdle() const; - bool HW_ReadMailbox(uint32_t *values); - bool HW_SendWorkRequest(uint32_t handle); - void HW_LoadCalibration(const LoadCalibrationConfig &config); - void HW_SetSpotFinderParameters(const SpotFinderParameters ¶ms); - void HW_RunInternalGenerator(const FrameGeneratorConfig &config) ; - - uint64_t GetSLSPackets(); - uint64_t GetUDPPackets(); -}; - - - diff --git a/fpga/hls_simulation/datamover_model.h b/fpga/hls_simulation/datamover_model.h deleted file mode 100644 index b6389b5e0..000000000 --- a/fpga/hls_simulation/datamover_model.h +++ /dev/null @@ -1,95 +0,0 @@ -// SPDX-FileCopyrightText: 2024 Filip Leonarski, Paul Scherrer Institute -// SPDX-License-Identifier: GPL-3.0-only - -#pragma once - -#include -#include "../hls/hls_jfjoch.h" - -enum class Direction {Input, Output}; - -template class Datamover { - volatile int stop_signal; - Direction dir; - char *offset; - hls::stream control; - hls::stream, DEPTH> data; - std::thread datamover_thread; - - size_t bytes_transferred; - std::atomic descriptors_done = 0; - - void Move() { - axis_datamover_ctrl command = control.read(); - - uint64_t addr = command.data(31+64,32); - uint32_t bytes = command.data(22,0); - if (bytes % (N/8) != 0) - throw std::runtime_error("AXI datamover transfer must be aligned to " + std::to_string(N/8) + " bytes"); - if (addr % (N/8) != 0) - throw std::runtime_error("AXI datamover addr must be aligned to " + std::to_string(N/8) + " bytes"); - if (bytes == 0) - throw std::runtime_error("AXI datamover transfer cannot be 0"); - - uint32_t xfers = bytes / (N/8); - auto host_mem = (ap_uint *) (offset + addr); - ap_axiu data_packet; - if (dir == Direction::Input) { - for (uint64_t i = 0; i < xfers; i++) { - data_packet.data = host_mem[i]; - for (int j = 0; j < N / 8; j++) - data_packet.keep[j] = 1; - data_packet.last = ((i == xfers - 1) ? 1 : 0); - data << data_packet; - bytes_transferred += N/8; - } - } else { - for (uint64_t i = 0; i < xfers; i++) { - data >> data_packet; - host_mem[i] = data_packet.data; - for (int j = 0; j < N / 8; j++) { - if (data_packet.keep[j] != 1) - throw std::runtime_error("TKEEP set low for bit " + std::to_string(i)); - } - if (data_packet.last != ((i == xfers - 1) ? 1 : 0)) - throw std::runtime_error("TLAST packet flag set incorrectly for packet " + std::to_string(i) + " Expected xfers: " + std::to_string(xfers)); - bytes_transferred += N/8; - } - } - descriptors_done++; - } -public: - void Run() { - while (!stop_signal || !control.empty()) { - while (!stop_signal && control.empty()) - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - // All commands that arrive till stop signal is executed, are executed - while (!control.empty()) { - Move(); - } - } - }; - - size_t Stop() noexcept { stop_signal = true; datamover_thread.join(); return bytes_transferred; } - - Datamover(Direction in_dir, char* in_offset) - : dir(in_dir), offset(in_offset), stop_signal(0), bytes_transferred(0) { - datamover_thread = std::thread([this] {this->Run(); }); - }; - - bool IsIdle() const { - return control.empty(); - } - - uint32_t GetCompletedDescriptors() const { - return descriptors_done; - } - - void ClearCompletedDescriptors() { - descriptors_done = 0; - } - - hls::stream& GetCtrlStream() { return control; } - hls::stream >& GetDataStream() { return data; } - ~Datamover() { Stop(); } -}; diff --git a/receiver/CMakeLists.txt b/receiver/CMakeLists.txt index 9285d2bac..dba0aa1cf 100644 --- a/receiver/CMakeLists.txt +++ b/receiver/CMakeLists.txt @@ -26,4 +26,4 @@ ADD_LIBRARY(JFJochReceiver STATIC JFJochReceiver.cpp JFJochReceiver.h) -TARGET_LINK_LIBRARIES(JFJochReceiver ImagePusher JFJochImagePuller JFJochImageAnalysis JFJochAcquisitionDevice JFJochCommon JFJochHLSSimulation JFJochPreview JFJochRugnux JFJochModelWriter) +TARGET_LINK_LIBRARIES(JFJochReceiver ImagePusher JFJochImagePuller JFJochImageAnalysis JFJochAcquisitionDevice JFJochCommon JFJochPreview JFJochRugnux JFJochModelWriter) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 55aa3f969..8684e8e73 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -70,7 +70,6 @@ ADD_EXECUTABLE(jfjoch_test ../common/DiffractionExperiment.h CheckImageOutput.h MockCardOutput.h - MockAcquisitionDeviceTest.cpp FPGAAcquisitionDeviceTest.cpp StrongPixelSetTest.cpp AdaptiveThresholdTest.cpp @@ -86,17 +85,13 @@ ADD_EXECUTABLE(jfjoch_test FFBIDXIndexerCPUTest.cpp ThreadSafeFIFOSetTest.cpp ReceiveRingBufferTest.cpp - FPGANetworkTest.cpp JFPedestalTest.cpp JFCalibrationTest.cpp AzimuthalIntegrationTest.cpp StatusVectorTest.cpp CBORTest.cpp DetectorGeometryTest.cpp DetectorSetupTest.cpp DiffractionGeometryTest.cpp - FPGASpotFindingUnitTest.cpp PreviewCounterTest.cpp - FPGAFrameGeneratorTest.cpp - FPGAHostWriterTest.cpp DiffractionSpotTest.cpp TIFFTest.cpp JFJochReceiverProcessingTest.cpp @@ -118,7 +113,6 @@ ADD_EXECUTABLE(jfjoch_test JFJochImageBufferReaderTest.cpp PixelMaskTest.cpp RegressionTest.cpp - FPGAHLSModulesTest.cpp ZMQPreviewSocketTest.cpp DetectorSettingsTest.cpp AutoIncrVectorTest.cpp @@ -171,7 +165,6 @@ ADD_EXECUTABLE(jfjoch_test XtalRefineTest.cpp XtalRefineCeres.h CrystalLatticeTest.cpp - FPGAPTPTest.cpp ResolutionShellsTest.cpp ImageSpotFinderCPUTest.cpp ImageSpotFinderGPUTest.cpp @@ -251,7 +244,7 @@ ADD_EXECUTABLE(jfjoch_test ) target_link_libraries(jfjoch_test Catch2WithMain JFJochBroker JFJochReceiver JFJochReader JFJochStreamWriter - JFJochRugnuxOutput JFJochRugnux JFJochModelWriter JFJochImageAnalysis JFJochCommon JFJochHLSSimulation JFJochPreview + JFJochRugnuxOutput JFJochRugnux JFJochModelWriter JFJochImageAnalysis JFJochCommon JFJochPreview jfjoch_xds_plugin ZLIB::ZLIB httplib::httplib) target_include_directories(jfjoch_test PRIVATE .) IF (JFJOCH_CUDA_AVAILABLE) diff --git a/tests/FPGAFrameGeneratorTest.cpp b/tests/FPGAFrameGeneratorTest.cpp deleted file mode 100644 index b8d002c94..000000000 --- a/tests/FPGAFrameGeneratorTest.cpp +++ /dev/null @@ -1,150 +0,0 @@ -// SPDX-FileCopyrightText: 2024 Filip Leonarski, Paul Scherrer Institute -// SPDX-License-Identifier: GPL-3.0-only - -#include -#include "../fpga/hls_simulation/hls_cores.h" - -TEST_CASE("FPGA_FrameGenerator_Cancel") { - STREAM_512 data_out; - std::vector > d_hbm_p0(RAW_MODULE_SIZE / (256 / 8) * 22 * 32); - std::vector > d_hbm_p1(RAW_MODULE_SIZE / (256 / 8) * 22 * 32); - ap_uint<1> cancel = 1; - FrameGeneratorConfig config{ - .frames = 256, - .modules = 8, - .detector_type = SLS_DETECTOR_TYPE_JUNGFRAU, - .images_in_memory = 0 - }; - REQUIRE(frame_generator(data_out, d_hbm_p0.data(), d_hbm_p1.data(), 16*1024*1024, 0, 0, cancel, config) == 0); - REQUIRE(data_out.size() == 130); -} - - -TEST_CASE("FPGA_FrameGenerator_WrongDetectorType") { - STREAM_512 data_out; - std::vector > d_hbm_p0(RAW_MODULE_SIZE / (256 / 8) * 22 * 32); - std::vector > d_hbm_p1(RAW_MODULE_SIZE / (256 / 8) * 22 * 32); - ap_uint<1> cancel = 1; - FrameGeneratorConfig config{ - .frames = 256, - .modules = 8, - .detector_type = 0, - .images_in_memory = 0 - }; - REQUIRE(frame_generator(data_out, d_hbm_p0.data(), d_hbm_p1.data(), 16*1024*1024, 0, 0, cancel, config) != 0); - REQUIRE(data_out.size() == 0); -} - -TEST_CASE("FPGA_FrameGenerator_WrongModuleCount") { - STREAM_512 data_out; - std::vector > d_hbm_p0(RAW_MODULE_SIZE / (256 / 8) * 22 * 32); - std::vector > d_hbm_p1(RAW_MODULE_SIZE / (256 / 8) * 22 * 32); - ap_uint<1> cancel = 1; - FrameGeneratorConfig config{ - .frames = 256, - .modules = MAX_MODULES_FPGA + 1, - .detector_type = SLS_DETECTOR_TYPE_JUNGFRAU, - .images_in_memory = 0 - }; - REQUIRE(frame_generator(data_out, d_hbm_p0.data(), d_hbm_p1.data(), 16*1024*1024, 0, 0, cancel, config) != 0); - REQUIRE(data_out.size() == 0); - - config.modules = 0; - REQUIRE(frame_generator(data_out, d_hbm_p0.data(), d_hbm_p1.data(), 16*1024*1024, 0, 0, cancel, config) != 0); - REQUIRE(data_out.size() == 0); -} - -TEST_CASE("FPGA_FrameGenerator_TooManyCells") { - STREAM_512 data_out; - std::vector > d_hbm_p0(RAW_MODULE_SIZE / (256 / 8) * 22 * 32); - std::vector > d_hbm_p1(RAW_MODULE_SIZE / (256 / 8) * 22 * 32); - ap_uint<1> cancel = 1; - FrameGeneratorConfig config{ - .frames = 256, - .modules = 4, - .detector_type = SLS_DETECTOR_TYPE_JUNGFRAU, - .images_in_memory = 8 - }; - REQUIRE(frame_generator(data_out, d_hbm_p0.data(), d_hbm_p1.data(), 16*1024*1024, 0, 0, cancel, config) == 3); - REQUIRE(data_out.size() == 0); -} - -TEST_CASE("FPGA_FrameGenerator_Run_JF") { - STREAM_512 data_out; - std::vector > d_hbm_p0(RAW_MODULE_SIZE / (256 / 8) * 22 * 32); - std::vector > d_hbm_p1(RAW_MODULE_SIZE / (256 / 8) * 22 * 32); - ap_uint<1> cancel = 0; - FrameGeneratorConfig config{ - .frames = 2, - .modules = 2, - .detector_type = SLS_DETECTOR_TYPE_JUNGFRAU, - .images_in_memory = 0 - }; - REQUIRE(frame_generator(data_out, d_hbm_p0.data(), d_hbm_p1.data(), 16*1024*1024, 0, 0, cancel, config) == 0); - REQUIRE(data_out.size() == (config.frames * config.modules * 128 + 1) * 130); -} - -TEST_CASE("FPGA_FrameGenerator_Run_EIGER") { - STREAM_512 data_out; - std::vector > d_hbm_p0(RAW_MODULE_SIZE / (256 / 8) * 22 * 32); - std::vector > d_hbm_p1(RAW_MODULE_SIZE / (256 / 8) * 22 * 32); - ap_uint<1> cancel = 0; - FrameGeneratorConfig config{ - .frames = 2, - .modules = 2, - .detector_type = SLS_DETECTOR_TYPE_EIGER, - .images_in_memory = 0, - .eiger_bit_depth = 16 - }; - REQUIRE(frame_generator(data_out, d_hbm_p0.data(), d_hbm_p1.data(), 16*1024*1024, 0, 0, cancel, config) == 0); - REQUIRE(data_out.size() == (config.frames * config.modules * 256) * 66 + 130); // Trailing packet is JF type! -} - - -TEST_CASE("FPGA_FrameGenerator_Run_EIGER_32bit") { - STREAM_512 data_out; - std::vector > d_hbm_p0(RAW_MODULE_SIZE / (256 / 8) * 22 * 32); - std::vector > d_hbm_p1(RAW_MODULE_SIZE / (256 / 8) * 22 * 32); - ap_uint<1> cancel = 0; - FrameGeneratorConfig config{ - .frames = 2, - .modules = 2, - .detector_type = SLS_DETECTOR_TYPE_EIGER, - .images_in_memory = 0, - .eiger_bit_depth = 32 - }; - REQUIRE(frame_generator(data_out, d_hbm_p0.data(), d_hbm_p1.data(), 16*1024*1024, 0, 0, cancel, config) == 0); - REQUIRE(data_out.size() == (config.frames * config.modules * 512) * 66 + 130); // Trailing packet is JF type! -} - -TEST_CASE("FPGA_FrameGenerator_Run_EIGER_8bit") { - STREAM_512 data_out; - std::vector > d_hbm_p0(RAW_MODULE_SIZE / (256 / 8) * 22 * 32); - std::vector > d_hbm_p1(RAW_MODULE_SIZE / (256 / 8) * 22 * 32); - ap_uint<1> cancel = 0; - FrameGeneratorConfig config{ - .frames = 2, - .modules = 2, - .detector_type = SLS_DETECTOR_TYPE_EIGER, - .images_in_memory = 0, - .eiger_bit_depth = 8 - }; - REQUIRE(frame_generator(data_out, d_hbm_p0.data(), d_hbm_p1.data(), 16*1024*1024, 0, 0, cancel, config) == 0); - REQUIRE(data_out.size() == (config.frames * config.modules * 128) * 66 + 130); // Trailing packet is JF type! -} - -TEST_CASE("FPGA_FrameGenerator_Run_EIGER_wrong_depth") { - STREAM_512 data_out; - std::vector > d_hbm_p0(RAW_MODULE_SIZE / (256 / 8) * 22 * 32); - std::vector > d_hbm_p1(RAW_MODULE_SIZE / (256 / 8) * 22 * 32); - ap_uint<1> cancel = 0; - FrameGeneratorConfig config{ - .frames = 2, - .modules = 2, - .detector_type = SLS_DETECTOR_TYPE_EIGER, - .images_in_memory = 0, - .eiger_bit_depth = 0 - }; - REQUIRE(frame_generator(data_out, d_hbm_p0.data(), d_hbm_p1.data(), 16*1024*1024, 0, 0, cancel, config) != 0); - REQUIRE(data_out.size() == 0); // Trailing packet is JF type! -} \ No newline at end of file diff --git a/tests/FPGAHLSModulesTest.cpp b/tests/FPGAHLSModulesTest.cpp deleted file mode 100644 index c2c50fdb1..000000000 --- a/tests/FPGAHLSModulesTest.cpp +++ /dev/null @@ -1,231 +0,0 @@ -// SPDX-FileCopyrightText: 2024 Filip Leonarski, Paul Scherrer Institute -// SPDX-License-Identifier: GPL-3.0-only - -#include -#include - -#include "../fpga/hls_simulation/hls_cores.h" - - -TEST_CASE("HLS_DataCollectionFSM","[OpenCAPI]") { - DataCollectionConfig act_reg; - - STREAM_512 raw0; - STREAM_512 raw1; - - hls::stream addr0; - hls::stream addr1; - - ap_uint<1> run_data_collection = 0; - ap_uint<1> cancel_data_collection = 0; - ap_uint<1> idle_data_collection; - uint32_t save_data_collection_counter; - act_reg.mode = MODE_CONV; - - // state = WAIT_FOR_START - volatile rcv_state_t state; - data_collection_fsm(raw0, raw1, - addr0, addr1, - run_data_collection, - cancel_data_collection, - idle_data_collection, - act_reg.mode, - act_reg.energy_kev, - act_reg.nframes, - act_reg.nmodules, - act_reg.nstorage_cells, - act_reg.nsummation, - act_reg.sqrtmult, - act_reg.pxlthreshold_min, - act_reg.pxlthreshold_max, - state); - REQUIRE(idle_data_collection == 1); - REQUIRE(addr1.empty()); - REQUIRE(raw1.empty()); - REQUIRE( state == RCV_WAIT_FOR_START); - run_data_collection = 1; - - // state = WAIT_FOR_START - data_collection_fsm(raw0, raw1, - addr0, addr1, - run_data_collection, - cancel_data_collection, - idle_data_collection, - act_reg.mode, - act_reg.energy_kev, - act_reg.nframes, - act_reg.nmodules, - act_reg.nstorage_cells, - act_reg.nsummation, - act_reg.sqrtmult, - act_reg.pxlthreshold_min, - act_reg.pxlthreshold_max, - state); - REQUIRE(idle_data_collection == 0); - REQUIRE(addr1.empty()); - REQUIRE(raw1.empty()); - REQUIRE( state == RCV_WAIT_FOR_START_LOW); - // state = WAIT_FOR_START_LOW - data_collection_fsm(raw0, raw1, - addr0, addr1, - run_data_collection, - cancel_data_collection, - idle_data_collection, - act_reg.mode, - act_reg.energy_kev, - act_reg.nframes, - act_reg.nmodules, - act_reg.nstorage_cells, - act_reg.nsummation, - act_reg.sqrtmult, - act_reg.pxlthreshold_min, - act_reg.pxlthreshold_max, - state); - REQUIRE(idle_data_collection == 0); - REQUIRE(addr1.empty()); - REQUIRE(raw1.empty()); - REQUIRE( state == RCV_WAIT_FOR_START_LOW); - // state = WAIT_FOR_START_LOW - - run_data_collection = 0; - - data_collection_fsm(raw0, raw1, - addr0, addr1, - run_data_collection, - cancel_data_collection, - idle_data_collection, - act_reg.mode, - act_reg.energy_kev, - act_reg.nframes, - act_reg.nmodules, - act_reg.nstorage_cells, - act_reg.nsummation, - act_reg.sqrtmult, - act_reg.pxlthreshold_min, - act_reg.pxlthreshold_max, - state); - REQUIRE(idle_data_collection == 0); - REQUIRE(addr1.empty()); - REQUIRE(raw1.empty()); - REQUIRE( state == RCV_START); - // state = START - data_collection_fsm(raw0, raw1, - addr0, addr1, - run_data_collection, - cancel_data_collection, - idle_data_collection, - act_reg.mode, - act_reg.energy_kev, - act_reg.nframes, - act_reg.nmodules, - act_reg.nstorage_cells, - act_reg.nsummation, - act_reg.sqrtmult, - act_reg.pxlthreshold_min, - act_reg.pxlthreshold_max, - state); - - REQUIRE(idle_data_collection == 0); - REQUIRE(raw1.size() == 1); - - REQUIRE( state == RCV_INIT); - // state = INIT - - data_collection_fsm(raw0, raw1, - addr0, addr1, - run_data_collection, - cancel_data_collection, - idle_data_collection, - act_reg.mode, - act_reg.energy_kev, - act_reg.nframes, - act_reg.nmodules, - act_reg.nstorage_cells, - act_reg.nsummation, - act_reg.sqrtmult, - act_reg.pxlthreshold_min, - act_reg.pxlthreshold_max, - state); - - REQUIRE(idle_data_collection == 0); - REQUIRE(raw1.size() == 1); - REQUIRE( state == RCV_INIT); - // state = INIT - - cancel_data_collection = 1; - - data_collection_fsm(raw0, raw1, - addr0, addr1, - run_data_collection, - cancel_data_collection, - idle_data_collection, - act_reg.mode, - act_reg.energy_kev, - act_reg.nframes, - act_reg.nmodules, - act_reg.nstorage_cells, - act_reg.nsummation, - act_reg.sqrtmult, - act_reg.pxlthreshold_min, - act_reg.pxlthreshold_max, - state); - - REQUIRE(idle_data_collection == 0); - REQUIRE(raw1.size() == 1); - - // state = LAST - REQUIRE( state == RCV_LAST); - data_collection_fsm(raw0, raw1, - addr0, addr1, - run_data_collection, - cancel_data_collection, - idle_data_collection, - act_reg.mode, - act_reg.energy_kev, - act_reg.nframes, - act_reg.nmodules, - act_reg.nstorage_cells, - act_reg.nsummation, - act_reg.sqrtmult, - act_reg.pxlthreshold_min, - act_reg.pxlthreshold_max, - state); - - REQUIRE(idle_data_collection == 0); - REQUIRE(addr1.size() == 1); - REQUIRE(raw1.size() == 2); - REQUIRE( state == RCV_WAIT_FOR_START); - // state = WAIT_FOR_START - - data_collection_fsm(raw0, raw1, - addr0, addr1, - run_data_collection, - cancel_data_collection, - idle_data_collection, - act_reg.mode, - act_reg.energy_kev, - act_reg.nframes, - act_reg.nmodules, - act_reg.nstorage_cells, - act_reg.nsummation, - act_reg.sqrtmult, - act_reg.pxlthreshold_min, - act_reg.pxlthreshold_max, - state); - - REQUIRE( state == RCV_WAIT_FOR_START); - REQUIRE(idle_data_collection == 1); - REQUIRE(addr1.size() == 1); - REQUIRE(raw1.size() == 2); - - auto packet = raw1.read(); - REQUIRE(packet.last == 1); - REQUIRE(packet.dest == 0); - - packet = raw1.read(); - REQUIRE(packet.last); - REQUIRE(packet.dest == 0); - - auto addr = addr1.read(); - REQUIRE(addr.last); -} diff --git a/tests/FPGAHostWriterTest.cpp b/tests/FPGAHostWriterTest.cpp deleted file mode 100644 index 0094eec98..000000000 --- a/tests/FPGAHostWriterTest.cpp +++ /dev/null @@ -1,73 +0,0 @@ -// SPDX-FileCopyrightText: 2024 Filip Leonarski, Paul Scherrer Institute -// SPDX-License-Identifier: GPL-3.0-only - -#include -#include "../fpga/hls_simulation/hls_cores.h" -#include "../common/Logger.h" - -TEST_CASE("FPGA_FrameWriterTestIgnore") { - Logger logger(Catch::getResultCapture().getCurrentTestName()); - STREAM_512 data_in; - hls::stream> adu_histo_in; - hls::stream> integration_in; - hls::stream> spot_finder_in; - hls::stream> roi_calc_in; - hls::stream> pixel_calc_in; - hls::stream s_axis_completion; - hls::stream > host_memory_out; - hls::stream datamover_out_cmd; - hls::stream > s_axis_work_request; - hls::stream > m_axis_completion; - uint64_t dma_address_table[1024]; - uint64_t packets_processed = 0; - ap_uint<1> idle; - ap_uint<1> in_cancel = 0; - - data_in.write(packet_512_t{.data = 0, .user = 0}); - - uint64_t nframes = 128; - for (int i = 0; i < nframes; i++) { - for (int j = 0; j < RAW_MODULE_SIZE * 2 / 64; j++) - data_in.write(packet_512_t{.data = 0, .user = 0, .last = (j == RAW_MODULE_SIZE * 2 / 64 - 1)}); - for (int j = 0; j < RAW_MODULE_SIZE / (64 * 8) + 1; j++) - spot_finder_in.write(0); - for (int j = 0; j < FPGA_INTEGRATION_BIN_COUNT / 8; j++) - integration_in.write(0); - for (int j = 0; j < ADU_HISTO_BIN_COUNT / 16; j++) - adu_histo_in.write(0); - for (int j = 0; j < FPGA_ROI_COUNT; j++) - roi_calc_in.write(0); - pixel_calc_in.write(0); - s_axis_completion.write(axis_completion{.frame_number = i, .last = 0}); - } - - ap_uint<3> state; - - s_axis_completion.write(axis_completion{.last = 1}); - data_in.write(packet_512_t{.data = 0, .user = 1}); - REQUIRE(data_in.size() == nframes * RAW_MODULE_SIZE * 2 / 64 + 2 ); - std::thread thr([&] { std::this_thread::sleep_for(std::chrono::seconds(1)); in_cancel = 1; }); - host_writer(data_in, - adu_histo_in, - integration_in, - spot_finder_in, - roi_calc_in, - pixel_calc_in, - s_axis_completion, - host_memory_out, - datamover_out_cmd, - s_axis_work_request, - m_axis_completion, - dma_address_table, - packets_processed, - idle, - in_cancel, - state); - CHECK(data_in.empty()); - CHECK(host_memory_out.empty()); - CHECK(datamover_out_cmd.empty()); - CHECK(m_axis_completion.size() == 2); - CHECK(packets_processed == 0); - CHECK(idle == 1); - thr.join(); -} \ No newline at end of file diff --git a/tests/FPGANetworkTest.cpp b/tests/FPGANetworkTest.cpp deleted file mode 100644 index 41648b0e3..000000000 --- a/tests/FPGANetworkTest.cpp +++ /dev/null @@ -1,630 +0,0 @@ -// SPDX-FileCopyrightText: 2024 Filip Leonarski, Paul Scherrer Institute -// SPDX-License-Identifier: GPL-3.0-only - -#include -#include -#include -#include - -#include "../fpga/hls_simulation/hls_cores.h" -#include "../fpga/hls_simulation/sls_packet.h" - -// ARP packet - from if_arp.h -#pragma pack(push) -#pragma pack(2) -struct RAW_ARP_Packet -{ - unsigned char dest_mac[6]; - unsigned char sour_mac[6]; - uint16_t ether_type; - unsigned short int ar_hrd; /* Format of hardware address. */ - unsigned short int ar_pro; /* Format of protocol address. */ - unsigned char ar_hln; /* Length of hardware address. */ - unsigned char ar_pln; /* Length of protocol address. */ - unsigned short int ar_op; /* ARP opcode (command). */ - - unsigned char __ar_sha[6]; /* Sender hardware address. */ - unsigned char __ar_sip[4]; /* Sender IP address. */ - unsigned char __ar_tha[6]; /* Target hardware address. */ - unsigned char __ar_tip[4]; /* Target IP address. */ -}; -#pragma pack(pop) - -TEST_CASE("HLS_Network_ARP_Gratuitous") { - - AXI_STREAM arp_in; - AXI_STREAM arp_out; - - arp(arp_in, arp_out, 0x1, 0x1, 1); - - packet_512_t network_packet; - REQUIRE(arp_out.read_nb(network_packet)); - auto arp_packet = (RAW_ARP_Packet *) &network_packet.data; - - for (int i = 0; i < 6; i++) { - REQUIRE(arp_packet->dest_mac[i] == 0xFF); - REQUIRE(arp_packet->__ar_tha[i] == 0xFF); - } - - REQUIRE(arp_packet->ether_type == htons(ETHER_ARP)); - REQUIRE(arp_packet->ar_op == htons(ARPOP_REQUEST)); - REQUIRE(arp_packet->ar_hrd == htons(ARPHRD_ETHER)); - REQUIRE(arp_packet->ar_pro == htons(ETHER_IP)); - REQUIRE(arp_packet->ar_hln == 6); - REQUIRE(arp_packet->ar_pln == 4); -} - -TEST_CASE("HLS_Network_UDP") { - char packet[130*64]; - - auto jf_packet = (jf_raw_packet *) packet; - - jf_packet->ipv4_header_sour_ip = htonl(0x12345640); - jf_packet->udp_length = htons(8248); - jf_packet->udp_dest_port = htons(129); - jf_packet->jf.framenum = 123456; - - STREAM_512 udp_in; - STREAM_512 udp_out; - hls::stream > udp_metadata; - - auto jf_packet_axi = (ap_uint<512> *) packet; - uint64_t packet_counter; - ap_uint<1> clear_counter = 0; - ap_uint<1> udp_idle = 0; - - for (int i = 0; i < 130; i++) { - packet_512_t packet_in; - packet_in.user = 0; - packet_in.last = (i == 129); - packet_in.data = jf_packet_axi[i]; - udp_in.write(packet_in); - udp(udp_in, udp_out, udp_metadata, packet_counter, clear_counter, udp_idle); - } - REQUIRE(udp_in.size() == 0); - REQUIRE(udp_metadata.size() == 1); - REQUIRE(udp_out.size() == 129); - - packet_512_t packet_out; - udp_out.read(packet_out); - REQUIRE(packet_out.data(63,0) == jf_packet->jf.framenum); - - ap_uint udp_metadata_out; - udp_metadata.read(udp_metadata_out); - REQUIRE(udp_metadata_dest_port(udp_metadata_out) == 129); - REQUIRE(udp_metadata_payload_size(udp_metadata_out) == 8240); - REQUIRE(udp_metadata_eth_err(udp_metadata_out) == 0); - REQUIRE(udp_metadata_len_err(udp_metadata_out) == 0); -} - -TEST_CASE("HLS_Network_UDP_EthErr") { - char packet[130*64]; - - auto jf_packet = (jf_raw_packet *) packet; - - jf_packet->udp_length = htons(8248); - jf_packet->udp_dest_port = htons(129); - jf_packet->jf.framenum = 123456; - - STREAM_512 udp_in; - STREAM_512 udp_out; - hls::stream > udp_metadata; - - auto jf_packet_axi = (ap_uint<512> *) packet; - uint64_t packet_counter; - ap_uint<1> clear_counter = 0; - ap_uint<1> udp_idle = 1; - - for (int i = 0; i < 130; i++) { - packet_512_t packet_in; - packet_in.user = (i == 129) ? 1 : 0; - packet_in.last = (i == 129); - packet_in.data = jf_packet_axi[i]; - udp_in.write(packet_in); - udp(udp_in, udp_out, udp_metadata, packet_counter, clear_counter, udp_idle); - } - REQUIRE(udp_idle == 1); - REQUIRE(udp_in.size() == 0); - REQUIRE(udp_metadata.size() == 1); - REQUIRE(udp_out.size() == 129); - - packet_512_t packet_out; - udp_out.read(packet_out); - REQUIRE(packet_out.data(63,0) == jf_packet->jf.framenum); - - ap_uint udp_metadata_out; - udp_metadata.read(udp_metadata_out); - REQUIRE(udp_metadata_dest_port(udp_metadata_out) == 129); - REQUIRE(udp_metadata_payload_size(udp_metadata_out) == 8240); - REQUIRE(udp_metadata_eth_err(udp_metadata_out) == 1); - REQUIRE(udp_metadata_len_err(udp_metadata_out) == 0); -} - -TEST_CASE("HLS_Network_UDP_LenErr") { - char packet[130*64]; - - auto jf_packet = (jf_raw_packet *) packet; - - jf_packet->udp_length = htons(8248); - jf_packet->udp_dest_port = htons(129); - jf_packet->jf.framenum = 123456; - - uint64_t packet_counter; - ap_uint<1> clear_counter = 0; - ap_uint<1> udp_idle = 0; - - STREAM_512 udp_in; - STREAM_512 udp_out; - hls::stream > udp_metadata; - - auto jf_packet_axi = (ap_uint<512> *) packet; - - for (int i = 0; i < 128; i++) { - packet_512_t packet_in; - packet_in.user = 0; - packet_in.last = (i == 127); - packet_in.data = jf_packet_axi[i]; - udp_in.write(packet_in); - udp(udp_in, udp_out, udp_metadata, packet_counter, clear_counter, udp_idle); - } - REQUIRE(udp_in.size() == 0); - REQUIRE(udp_metadata.size() == 1); - REQUIRE(udp_out.size() == 127); - - packet_512_t packet_out; - udp_out.read(packet_out); - REQUIRE(packet_out.data(63,0) == jf_packet->jf.framenum); - - ap_uint udp_metadata_out; - udp_metadata.read(udp_metadata_out); - REQUIRE(udp_metadata_dest_port(udp_metadata_out) == 129); - REQUIRE(udp_metadata_payload_size(udp_metadata_out) == 8240); - REQUIRE(udp_metadata_eth_err(udp_metadata_out) == 0); - REQUIRE(udp_metadata_len_err(udp_metadata_out) == 1); -} - -TEST_CASE("HLS_Network_UDP_SLS_detector_1") { - char packet[130*64]; - - uint8_t data_stream = 3; - - auto jf_packet = (jf_raw_packet *) packet; - jf_packet->ipv4_header_sour_ip = htonl(32); - jf_packet->udp_length = htons(8248); - jf_packet->udp_dest_port = htons((data_stream << 8) + 5); - jf_packet->jf.framenum = 123456; - jf_packet->jf.packetnum = 60; - jf_packet->jf.row = 5; - jf_packet->jf.column = 0; - jf_packet->jf.detectortype = SLS_DETECTOR_TYPE_JUNGFRAU; - for (int i = 0; i < 4096; i++) - jf_packet->jf.data[i] = i; - - STREAM_512 udp_in; - STREAM_512 udp_out; - hls::stream > udp_metadata; - - STREAM_512 raw_out; - hls::stream addr0; - - uint64_t packet_counter; - uint64_t sls_packet_counter; - uint32_t err_counter1, err_counter2; - ap_uint<1> clear_counter = 0; - ap_uint<1> udp_idle = 0; - ap_uint<1> sls_idle = 0; - uint64_t bunchid = 0; - - auto jf_packet_axi = (ap_uint<512> *) packet; - - for (int i = 0; i < 130; i++) { - packet_512_t packet_in; - packet_in.user = 0; - packet_in.last = (i == 129); - packet_in.data = jf_packet_axi[i]; - udp_in.write(packet_in); - udp(udp_in, udp_out, udp_metadata, packet_counter, clear_counter, udp_idle); - } - REQUIRE(udp_in.size() == 0); - REQUIRE(udp_metadata.size() == 1); - REQUIRE(udp_out.size() == 129); - REQUIRE(udp_idle == 1); - while (!udp_out.empty()) { - sls_detector(udp_out, udp_metadata, raw_out, addr0, sls_packet_counter, err_counter1, err_counter2, - bunchid, data_stream, clear_counter, sls_idle); - } - REQUIRE(sls_idle == 1); - REQUIRE(raw_out.size() == 128); - REQUIRE(addr0.size() == 1); - - for (int i = 0; i < 128; i++) { - auto packet_out = raw_out.read(); - REQUIRE(memcmp(&(packet_out.data), jf_packet->jf.data + 32 * i, 64) == 0); - } - - auto addr = addr0.read(); - REQUIRE(addr.frame_number == jf_packet->jf.framenum - 1); - REQUIRE(addr.module == 5 / 2); - REQUIRE(addr.column == 0); - REQUIRE(addr.row == 1); - REQUIRE(addr.eth_packet == jf_packet->jf.packetnum); -} - -TEST_CASE("HLS_Network_UDP_SLS_detector_2") { - char packet[130*64]; - - uint8_t data_stream = 7; - auto jf_packet = (jf_raw_packet *) packet; - - jf_packet->udp_length = htons(8248); - jf_packet->udp_dest_port = htons((data_stream<<8) + 2*2); - jf_packet->ipv4_header_sour_ip = htonl(32+4); - jf_packet->jf.framenum = UINT64_MAX - 300; - jf_packet->jf.packetnum = 35; - jf_packet->jf.row = 4; - jf_packet->jf.detectortype = SLS_DETECTOR_TYPE_JUNGFRAU; - - for (int i = 0; i < 4096; i++) - jf_packet->jf.data[i] = i; - - STREAM_512 udp_in; - STREAM_512 udp_out; - hls::stream > udp_metadata; - - STREAM_512 raw_out; - hls::stream addr0; - - auto jf_packet_axi = (ap_uint<512> *) packet; - - uint64_t packet_counter; - uint64_t sls_packet_counter; - uint32_t err_counter1, err_counter2; - ap_uint<1> clear_counter = 0; - ap_uint<1> udp_idle = 1; - ap_uint<1> sls_idle = 1; - uint64_t bunchid = 0; - - for (int i = 0; i < 130; i++) { - packet_512_t packet_in; - packet_in.user = 0; - packet_in.last = (i == 129); - packet_in.data = jf_packet_axi[i]; - udp_in.write(packet_in); - udp(udp_in, udp_out, udp_metadata, packet_counter, clear_counter, udp_idle); - } - REQUIRE(udp_in.size() == 0); - REQUIRE(udp_metadata.size() == 1); - REQUIRE(udp_out.size() == 129); - REQUIRE(udp_idle == 1); - - while (!udp_out.empty()) { - sls_detector(udp_out, udp_metadata, raw_out, addr0, sls_packet_counter, err_counter1, err_counter2, - bunchid, data_stream, clear_counter, sls_idle); - } - - REQUIRE(raw_out.size() == 128); - REQUIRE(addr0.size() == 1); - REQUIRE(sls_idle == 1); - - for (int i = 0; i < 128; i++) { - auto packet_out = raw_out.read(); - REQUIRE(memcmp(&(packet_out.data), jf_packet->jf.data + 32 * i, 64) == 0); - } - - auto addr = addr0.read(); - REQUIRE(addr.frame_number == jf_packet->jf.framenum - 1); - REQUIRE(addr.module == 2); - REQUIRE(addr.eth_packet == jf_packet->jf.packetnum); -} - -TEST_CASE("HLS_Network_UDP_SLS_detector_2_packets") { - char packet[130*64]; - - auto jf_packet = (jf_raw_packet *) packet; - - jf_packet->ipv4_header_sour_ip = htonl(192); - jf_packet->udp_length = htons(8248); - jf_packet->udp_dest_port = htons(16384 + 2); - jf_packet->jf.framenum = UINT64_MAX - 300; - jf_packet->jf.packetnum = 35; - jf_packet->jf.row = 0; - jf_packet->jf.detectortype = SLS_DETECTOR_TYPE_JUNGFRAU; - - for (int i = 0; i < 4096; i++) - jf_packet->jf.data[i] = i; - - STREAM_512 udp_in; - STREAM_512 udp_out; - hls::stream > udp_metadata; - - STREAM_512 raw_out; - hls::stream addr0; - - auto jf_packet_axi = (ap_uint<512> *) packet; - - uint64_t packet_counter; - uint64_t sls_packet_counter; - uint32_t err_counter1, err_counter2; - ap_uint<1> clear_counter = 0; - ap_uint<1> udp_idle = 1; - ap_uint<1> sls_idle = 1; - uint64_t bunchid = 0; - - for (int i = 0; i < 130; i++) { - packet_512_t packet_in; - packet_in.user = 0; - packet_in.last = (i == 129); - packet_in.data = jf_packet_axi[i]; - udp_in.write(packet_in); - udp(udp_in, udp_out, udp_metadata, packet_counter, clear_counter, udp_idle); - } - - for (int i = 0; i < 130; i++) { - packet_512_t packet_in; - packet_in.user = 0; - packet_in.last = (i == 129); - packet_in.data = jf_packet_axi[i]; - udp_in.write(packet_in); - udp(udp_in, udp_out, udp_metadata, packet_counter, clear_counter, udp_idle); - } - - REQUIRE(udp_in.size() == 0); - REQUIRE(udp_metadata.size() == 2); - REQUIRE(udp_out.size() == 2*129); - - while (!udp_out.empty()) { - sls_detector(udp_out, udp_metadata, raw_out, addr0, sls_packet_counter, err_counter1, err_counter2, - bunchid, 0, clear_counter, sls_idle); - } - - REQUIRE(raw_out.size() == 256); - REQUIRE(addr0.size() == 2); - - for (int j = 0; j < 2; j++) { - for (int i = 0; i < 128; i++) { - auto packet_out = raw_out.read(); - REQUIRE(memcmp(&(packet_out.data), jf_packet->jf.data + 32 * i, 64) == 0); - } - - auto addr = addr0.read(); - REQUIRE(addr.frame_number == jf_packet->jf.framenum - 1); - REQUIRE(addr.module == 1); - REQUIRE(addr.eth_packet == jf_packet->jf.packetnum); - } -} - -TEST_CASE("HLS_Network_UDP_SLS_detector_packets_err") { - char packet[130*64]; - - auto jf_packet = (jf_raw_packet *) packet; - - jf_packet->ipv4_header_sour_ip = htonl(32+4); - jf_packet->udp_length = htons(8248); - jf_packet->udp_dest_port = htons(4); - jf_packet->jf.framenum = UINT64_MAX - 300; - jf_packet->jf.packetnum = 35; - jf_packet->jf.row = 0; - jf_packet->jf.detectortype = SLS_DETECTOR_TYPE_JUNGFRAU; - - for (int i = 0; i < 4096; i++) - jf_packet->jf.data[i] = i; - - STREAM_512 udp_in; - STREAM_512 udp_out; - hls::stream > udp_metadata; - - STREAM_512 raw_out; - hls::stream addr0; - - auto jf_packet_axi = (ap_uint<512> *) packet; - - uint64_t udp_packet_counter; - uint64_t sls_packet_counter; - uint32_t err_counter_eth, err_counter_len; - ap_uint<1> clear_counter = 0; - ap_uint<1> udp_idle = 1; - ap_uint<1> sls_idle = 1; - uint64_t bunchid = 0; - - clear_counter = 1; - for (int i = 0; i < 125; i++) { - packet_512_t packet_in; - packet_in.user = 0; - packet_in.last = (i == 124); - packet_in.data = jf_packet_axi[i]; - udp_in.write(packet_in); - udp(udp_in, udp_out, udp_metadata, udp_packet_counter, clear_counter, udp_idle); - clear_counter = 0; - } - - for (int i = 0; i < 130; i++) { - packet_512_t packet_in; - packet_in.user = 0; - packet_in.last = (i == 129); - packet_in.data = jf_packet_axi[i]; - udp_in.write(packet_in); - udp(udp_in, udp_out, udp_metadata, udp_packet_counter, clear_counter, udp_idle); - } - - for (int i = 0; i < 130; i++) { - packet_512_t packet_in; - packet_in.user = 1; - packet_in.last = (i == 129); - packet_in.data = jf_packet_axi[i]; - udp_in.write(packet_in); - udp(udp_in, udp_out, udp_metadata, udp_packet_counter, clear_counter, udp_idle); - } - - for (int i = 0; i < 130; i++) { - packet_512_t packet_in; - packet_in.user = 1; - packet_in.last = (i == 129); - packet_in.data = jf_packet_axi[i]; - udp_in.write(packet_in); - udp(udp_in, udp_out, udp_metadata, udp_packet_counter, clear_counter, udp_idle); - } - - REQUIRE(udp_in.size() == 0); - REQUIRE(udp_metadata.size() == 4); - REQUIRE(udp_out.size() == 129*3+124); - - clear_counter = 1; - while (!udp_out.empty()) { - sls_detector(udp_out, udp_metadata, raw_out, addr0, sls_packet_counter, err_counter_eth, err_counter_len, - bunchid, 0, clear_counter, sls_idle); - clear_counter = 0; - } - - REQUIRE(raw_out.size() == 128); - REQUIRE(addr0.size() == 1); - - for (int i = 0; i < 128; i++) { - auto packet_out = raw_out.read(); - REQUIRE(memcmp(&(packet_out.data), jf_packet->jf.data + 32 * i, 64) == 0); - } - - auto addr = addr0.read(); - REQUIRE(addr.frame_number == jf_packet->jf.framenum - 1); - REQUIRE(addr.module == 2); - REQUIRE(addr.eth_packet == jf_packet->jf.packetnum); - - REQUIRE(udp_packet_counter == 4); - REQUIRE(sls_packet_counter == 1); - REQUIRE(err_counter_eth == 2); - REQUIRE(err_counter_len == 1); -} - -TEST_CASE("HLS_Network_UDP_SLS_detector_4kB") { - char packet[130*64]; - - auto jf_packet = (eiger_raw_packet *) packet; - - jf_packet->udp_length = htons(4096+48+8); - jf_packet->udp_dest_port = htons(16384 + 5); // bit 14 should not matter - jf_packet->eiger.framenum = 123456; - jf_packet->eiger.packetnum = 60; - jf_packet->eiger.row = 0; - jf_packet->eiger.column = 1; - jf_packet->eiger.detectortype = SLS_DETECTOR_TYPE_EIGER; - - for (int i = 0; i < 2048; i++) - jf_packet->eiger.data[i] = i; - - STREAM_512 udp_in; - STREAM_512 udp_out; - hls::stream > udp_metadata; - - STREAM_512 raw_out; - hls::stream addr0; - ap_uint<1> udp_idle; - - uint64_t packet_counter; - uint64_t sls_packet_counter; - uint32_t err_counter_eth, err_counter_len; - ap_uint<1> sls_idle = 1; - uint64_t bunchid = 0; - - auto jf_packet_axi = (ap_uint<512> *) packet; - ap_uint<1> clear_counter = 0; - - for (int i = 0; i < 66; i++) { - packet_512_t packet_in; - packet_in.user = 0; - packet_in.last = (i == 65); - packet_in.data = jf_packet_axi[i]; - udp_in.write(packet_in); - udp(udp_in, udp_out, udp_metadata, packet_counter, clear_counter, udp_idle); - } - REQUIRE(udp_in.size() == 0); - REQUIRE(udp_metadata.size() == 1); - REQUIRE(udp_out.size() == 65); - - while (!udp_out.empty()) { - sls_detector(udp_out, udp_metadata, raw_out, addr0, sls_packet_counter, err_counter_eth, err_counter_len, - bunchid, 0, clear_counter, sls_idle); - } - - REQUIRE(raw_out.size() == 64); - REQUIRE(addr0.size() == 1); - - for (int i = 0; i < 64; i++) { - auto packet_out = raw_out.read(); - REQUIRE(memcmp(&(packet_out.data), jf_packet->eiger.data + 32 * i, 64) == 0); - } - - auto addr = addr0.read(); - REQUIRE(addr.frame_number == jf_packet->eiger.framenum - 1); - REQUIRE(addr.eth_packet == jf_packet->eiger.packetnum); - REQUIRE(addr.column == 1); - REQUIRE(addr.row == 1); -} - -TEST_CASE("HLS_Network_UDP_SLS_detector_bunchid") { - ap_uint<512> tmp[2]; - - auto packet = (bunchid_raw_packet *) tmp; - - for (int i = 0; i < 6; i++) - packet->dest_mac[i] = 0xFF; - - packet->udp_dest_port = 0; - packet->udp_length = htons(sizeof(bunchid_payload) + 8); - packet->payload.magicn[0] = BUNCHID_MAGICN; - packet->payload.magicn[1] = BUNCHID_MAGICN; - packet->payload.magicn[2] = BUNCHID_MAGICN; - packet->payload.bunchid_msb[0] = 0xAABBCCDD; - packet->payload.bunchid_msb[1] = 0xAABBCCDD; - packet->payload.bunchid_lsb[0] = 0xFFDDCCBB; - packet->payload.bunchid_lsb[1] = 0xFFDDCCBB; - - STREAM_512 udp_in; - STREAM_512 udp_out; - hls::stream > udp_metadata; - - STREAM_512 raw_out; - hls::stream addr0; - - uint64_t udp_packet_counter; - uint64_t sls_packet_counter; - uint32_t err_counter_eth, err_counter_len; - ap_uint<1> clear_counter = 0; - ap_uint<1> udp_idle = 1; - ap_uint<1> sls_idle = 1; - uint64_t bunchid = 0; - - clear_counter = 1; - - packet_512_t packet_in; - packet_in.user = 0; - packet_in.last = 0; - packet_in.data = tmp[0]; - udp_in.write(packet_in); - - packet_in.last = 1; - packet_in.data = tmp[1]; - udp_in.write(packet_in); - - udp(udp_in, udp_out, udp_metadata, udp_packet_counter, clear_counter, udp_idle); - udp(udp_in, udp_out, udp_metadata, udp_packet_counter, clear_counter, udp_idle); - - clear_counter = 0; - REQUIRE(udp_in.size() == 0); - REQUIRE(udp_metadata.size() == 1); - REQUIRE(udp_out.size() == 1); - - clear_counter = 1; - - sls_detector(udp_out, udp_metadata, raw_out, addr0, sls_packet_counter, err_counter_eth, err_counter_len, - bunchid, 0, clear_counter, sls_idle); - - clear_counter = 0; - - REQUIRE(raw_out.size() == 0); - REQUIRE(addr0.size() == 0); - - REQUIRE(bunchid == 0xAABBCCDDFFDDCCBB); - REQUIRE(udp_packet_counter == 1); - REQUIRE(sls_packet_counter == 0); - REQUIRE(err_counter_eth == 0); - REQUIRE(err_counter_len == 0); -} \ No newline at end of file diff --git a/tests/FPGAPTPTest.cpp b/tests/FPGAPTPTest.cpp deleted file mode 100644 index 05d90311f..000000000 --- a/tests/FPGAPTPTest.cpp +++ /dev/null @@ -1,221 +0,0 @@ -// SPDX-FileCopyrightText: 2025 Filip Leonarski, Paul Scherrer Institute -// SPDX-License-Identifier: GPL-3.0-only - -#include -#include -#include -#include // htons, htonl -#include "../fpga/hls_simulation/hls_cores.h" -#include "../fpga/hls/ptp.cpp" - -// Minimal 64-byte frame layout for a single-beat 512-bit AXI packet. -// We only care about: -// - Ethernet header (14 bytes) as padding -// - PTP common header fields: messageType nibble, flagField -// - preciseOriginTimestamp: ns(32) + seconds(48) -// Other fields are left as zero. -#pragma pack(push, 1) -struct EthernetPTP512 { - uint8_t eth_pad[14]; // Ethernet dest/src/type (unused in tests) - - // PTP common header (we only fill what we need) - uint8_t ts_msgType; // transportSpecific (upper nibble) | messageType (lower nibble) - uint8_t versionPTP; // not used, keep zero - uint16_t messageLength; // not used - uint8_t domainNumber; // not used - uint8_t reserved1; // not used - uint16_t flagField; // network order - uint64_t correctionField; // not used - uint32_t reserved2; // not used - uint8_t sourcePortIdentity[10]; // not used - uint16_t sequenceId; // not used - uint8_t controlField; // not used - int8_t logMessageInterval; // not used - - // payload for Sync/Follow_Up we care about: preciseOriginTimestamp - // Offset chosen to match your parser: ns first (32-bit), then seconds (48-bit). - uint32_t preciseOriginTimestamp_ns_be; // network order - uint8_t preciseOriginTimestamp_sec_be[6]; // 48-bit, big-endian -}; -#pragma pack(pop) - -// Helper: host-to-big-endian for 48-bit seconds into a 6-byte array. -static inline void htobe48(uint64_t host_val, uint8_t out[6]) { - // Keep low 48 bits - uint64_t v = host_val & 0x0000FFFFFFFFFFFFULL; - out[0] = static_cast((v >> 40) & 0xFF); - out[1] = static_cast((v >> 32) & 0xFF); - out[2] = static_cast((v >> 24) & 0xFF); - out[3] = static_cast((v >> 16) & 0xFF); - out[4] = static_cast((v >> 8) & 0xFF); - out[5] = static_cast(v & 0xFF); -} - -// Build a 512-bit single-beat AXI PTP packet via a C struct, then cast to ap_uint<512>. -static packet_512_t make_ptp_packet(uint8_t message_type, bool two_step, uint64_t sec48, uint32_t ns32) { - ap_uint<512> word = 0; - - auto frame = reinterpret_cast(&word); - - // Byte 0 of PTP: transportSpecific=0, messageType in low nibble - frame->ts_msgType = (0x0 << 4) | (message_type & 0x0F); - - // twoStepFlag: bit 1 (0x0002) in flagField (big-endian) - uint16_t flags = two_step ? 0x0002 : 0x0000; - frame->flagField = htons(flags); - - // preciseOriginTimestamp: ns (32-bit BE), seconds (48-bit BE) - frame->preciseOriginTimestamp_ns_be = htonl(ns32); - htobe48(sec48, frame->preciseOriginTimestamp_sec_be); - - packet_512_t pkt{}; - pkt.data = word; - pkt.last = 1; - return pkt; -} - -static void run_cycles(AXI_STREAM& s, int cycles, - volatile ap_uint<80>& counter_ns, - volatile ap_uint<32>& counter_error, - volatile ap_uint<1>& synced_to_ptp) { - for (int i = 0; i < cycles; ++i) { - ptp(s, counter_ns, counter_error, synced_to_ptp); - } -} - -TEST_CASE("HLS_PTP_OneStep") { - AXI_STREAM eth_in; - volatile ap_uint<80> counter_ns = 0; - volatile ap_uint<32> counter_error = 0; - volatile ap_uint<1> synced_to_ptp = 0; - - run_cycles(eth_in, 10, counter_ns, counter_error, synced_to_ptp); - - uint64_t sec = 56789; - uint32_t ns = 999999999U; - auto pkt = make_ptp_packet(/*message_type=*/0x0, /*two_step=*/false, sec, ns); - eth_in.write(pkt); - - run_cycles(eth_in, 1, counter_ns, counter_error, synced_to_ptp); - - ap_uint<80> combined = counter_ns; // upper 48 bits = sec, lower 32 bits = ns - uint64_t sec_read = (uint64_t)(combined >> 32); - uint32_t ns_read = (uint32_t)(combined & 0xFFFFFFFFu); - - REQUIRE(synced_to_ptp.read() == 1); - CHECK(sec_read == sec); - CHECK(ns_read == ns); -} - -TEST_CASE("HLS_PTP_OneStep_incr_1sec") { - AXI_STREAM eth_in; - volatile ap_uint<80> counter_ns = 0; - volatile ap_uint<32> counter_error = 0; - volatile ap_uint<1> synced_to_ptp = 0; - - run_cycles(eth_in, 10, counter_ns, counter_error, synced_to_ptp); - - uint64_t sec = 56789; - uint32_t ns = 999999999U; - auto pkt = make_ptp_packet(/*message_type=*/0x0, /*two_step=*/false, sec, ns); - eth_in.write(pkt); - - run_cycles(eth_in, 2, counter_ns, counter_error, synced_to_ptp); - - ap_uint<80> combined = counter_ns; // upper 48 bits = sec, lower 32 bits = ns - uint64_t sec_read = (uint64_t)(combined >> 32); - uint32_t ns_read = (uint32_t)(combined & 0xFFFFFFFFu); - - REQUIRE(synced_to_ptp.read() == 1); - CHECK(sec_read == 56789 + 1); - CHECK(ns_read == 4); -} - -TEST_CASE("HLS_PTP_TwoStep") { - AXI_STREAM eth_in; - volatile ap_uint<80> counter_ns = 0; - volatile ap_uint<32> counter_error = 0; - volatile ap_uint<1> synced_to_ptp = 0; - - run_cycles(eth_in, 5, counter_ns, counter_error, synced_to_ptp); - ap_uint<80> before = counter_ns; - - // Two-step Sync - auto sync_pkt = make_ptp_packet(/*message_type=*/0x0, /*two_step=*/true, /*sec*/5, /*ns*/500); - eth_in.write(sync_pkt); - run_cycles(eth_in, 1, counter_ns, counter_error, synced_to_ptp); - - ap_uint<80> after_sync = counter_ns; - REQUIRE(after_sync > before); // clock advanced by tick, but no snap to sec=5 - uint64_t sec_sync = (uint64_t)(after_sync >> 32); - REQUIRE(sec_sync != 5); - - // Follow_Up updates to its timestamp - uint64_t fu_sec = 123456; - uint32_t fu_ns = 3456; - auto follow_up_pkt = make_ptp_packet(/*message_type=*/0x8, /*two_step=*/true, fu_sec, fu_ns); - eth_in.write(follow_up_pkt); - run_cycles(eth_in, 1, counter_ns, counter_error, synced_to_ptp); - - ap_uint<80> combined = counter_ns; - uint64_t sec_read = (uint64_t)(combined >> 32); - uint32_t ns_read = (uint32_t)(combined & 0xFFFFFFFFu); - - REQUIRE(synced_to_ptp.read() == 1); - CHECK(sec_read == fu_sec); - CHECK(ns_read == fu_ns); -} - -TEST_CASE("HLS_PTP_OneStep_error") { - AXI_STREAM eth_in; - volatile ap_uint<80> counter_ns = 0; - volatile ap_uint<32> counter_error = 0; - volatile ap_uint<1> synced_to_ptp = 0; - - run_cycles(eth_in, 10, counter_ns, counter_error, synced_to_ptp); - - uint64_t sec = 100; - uint32_t ns = 100; - auto pkt = make_ptp_packet(/*message_type=*/0x0, /*two_step=*/false, sec, ns); - eth_in.write(pkt); - - run_cycles(eth_in, 11, counter_ns, counter_error, synced_to_ptp); - - ap_uint<80> combined = counter_ns; // upper 48 bits = sec, lower 32 bits = ns - uint64_t sec_read = (uint64_t)(combined >> 32); - uint32_t ns_read = (uint32_t)(combined & 0xFFFFFFFFu); - - REQUIRE(synced_to_ptp.read() == 1); - CHECK(sec_read == 100); - CHECK(ns_read == 100 + 5 * 10); - - ns = 100 + 5 * 11 - 7; - pkt = make_ptp_packet(/*message_type=*/0x0, /*two_step=*/false, sec, ns); - eth_in.write(pkt); - - run_cycles(eth_in, 1, counter_ns, counter_error, synced_to_ptp); - CHECK(counter_error.read() == 7); - combined = counter_ns; // upper 48 bits = sec, lower 32 bits = ns - sec_read = (uint64_t)(combined >> 32); - ns_read = (uint32_t)(combined & 0xFFFFFFFFu); - - CHECK(sec_read == 100); - CHECK(ns_read == ns); -} - -TEST_CASE("HLS_PTP_2s_out_of_sync") { - AXI_STREAM eth_in; - volatile ap_uint<80> counter_ns = 0; - volatile ap_uint<32> counter_error = 0; - volatile ap_uint<1> synced_to_ptp = 0; - - auto pkt = make_ptp_packet(/*message_type=*/0x0, /*two_step=*/false, /*sec*/100, /*ns*/0); - eth_in.write(pkt); - run_cycles(eth_in, 1, counter_ns, counter_error, synced_to_ptp); - REQUIRE(synced_to_ptp.read() == 1); - - const uint64_t ticks_2s = (2ULL * 1000000000ULL) / 5ULL; - run_cycles(eth_in, (int)(ticks_2s + 2), counter_ns, counter_error, synced_to_ptp); - - REQUIRE(synced_to_ptp.read() == 0); -} diff --git a/tests/FPGASpotFindingUnitTest.cpp b/tests/FPGASpotFindingUnitTest.cpp deleted file mode 100644 index daa7cb91d..000000000 --- a/tests/FPGASpotFindingUnitTest.cpp +++ /dev/null @@ -1,162 +0,0 @@ -// SPDX-FileCopyrightText: 2024 Filip Leonarski, Paul Scherrer Institute -// SPDX-License-Identifier: GPL-3.0-only - -#include -#include - -#include "../fpga/hls_simulation/hls_cores.h" - -TEST_CASE("FPGA_spot_finder_core","[FPGA][SpotFinder]") { - STREAM_768 input; - STREAM_768 output; - hls::stream> strong_pixel; - - ap_int<32> in_photon_count_threshold = 8; - float_uint32 in_strong_pixel_threshold; - in_strong_pixel_threshold.f = 4.0; - - 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] = INT24_MIN; - else - input_frame[i] = i % RAW_MODULE_COLS; - } - - input << packet_768_t{.user = 0}; - for (int i = 0; i < RAW_MODULE_SIZE * sizeof(uint16_t) / 64; i++) { - ap_int<24> tmp[32]; - for (int j = 0; j < 32; j++) - tmp[j] = input_frame[i * 32 + j]; - - input << packet_768_t{.data = pack32(tmp), .keep = UINT32_MAX, .user = 0}; - } - - input << packet_768_t{.user = 1}; - - ap_uint<32> tmp_strong_pixel_threshold = in_strong_pixel_threshold.u; - spot_finder(input, - output, - strong_pixel, - in_photon_count_threshold, - tmp_strong_pixel_threshold); - - REQUIRE(input.size() == 0); - REQUIRE(output.size() == RAW_MODULE_SIZE * sizeof(uint16_t) / 64 + 2); - REQUIRE(strong_pixel.size() == RAW_MODULE_SIZE * sizeof(uint16_t) / 64 + 16 + 1); -} - -void spot_finder_prepare(hls::stream &data_in, - hls::stream &data_out, - hls::stream> &sum_out, - hls::stream> &sum2_out, - hls::stream> &valid_out); - -TEST_CASE("FPGA_spot_finder_prepare", "FPGA") { - hls::stream data_in; - hls::stream data_out; - hls::stream> sum_out; - hls::stream> sum2_out; - hls::stream> valid_out; - - // Empty packet, just forward - data_in << spot_finder_packet{ - .data = 348, - .user = 0 - }; - - std::vector input_frame(RAW_MODULE_SIZE); - std::vector mask(RAW_MODULE_SIZE); - - for (int i = 0; i < RAW_MODULE_SIZE; i++) { - input_frame[i] = i; - mask[i] = 1; - } - - for (int i = 0; i < RAW_MODULE_SIZE * sizeof(uint16_t) / 64; i++) { - ap_int<24> tmp[32]; - ap_uint<32> mask_tmp; - - for (int j = 0; j < 32; j++) { - tmp[j] = input_frame[i * 32 + j]; - mask_tmp[j] = (mask[i * 32 + j] != 0); - } - - data_in << spot_finder_packet{ - .data = pack32(tmp), - .mask = mask_tmp, - .user = 0, - }; - } - - data_in << spot_finder_packet{.user = 1}; - spot_finder_prepare(data_in, - data_out, - sum_out, - sum2_out, - valid_out); - - REQUIRE(data_in.empty()); - REQUIRE(data_out.size() == 16384 + 2); - REQUIRE(sum_out.size() == 16384); - REQUIRE(sum2_out.size() == 16384); - REQUIRE(valid_out.size() == 16384); - - int64_t sum[16384]; - int64_t sum2[16384]; - int64_t valid[16384]; - - for (int i = 0; i < 16384; i++) { - sum[i] = sum_out.read().to_int64(); - sum2[i] = sum2_out.read().to_int64(); - valid[i] = valid_out.read().to_int64(); - } - - REQUIRE(sum_out.empty()); - REQUIRE(sum2_out.empty()); - REQUIRE(valid_out.empty()); - - int sum_err = 0; - int sum2_err = 0; - int valid_err = 0; - - for (int i = 0; i < 16384; i++) { - int64_t sum_ref = 0; - int64_t sum2_ref = 0; - int64_t valid_ref = 0; - - int32_t col = i % 32; - int32_t line = i / 32; - - for (int l = line - 15; l <= line + 15; l++) { - if (l >= 0 && l < 512) { - for (int c = 0; c < 32; c++) { - int64_t val = input_frame[(col * 32 + c) + l * RAW_MODULE_COLS]; - auto mask_val = mask[(col * 32 + c) + l * RAW_MODULE_COLS]; - valid_ref += mask_val; - - if (mask_val == 0) - val = 0; - - sum_ref += val; - sum2_ref += val * val; - - } - } - } - - if (valid_ref != valid[i]) - valid_err++; - - if (sum2_ref != sum2[i]) - sum2_err++; - - if (sum_ref != sum[i]) { - std::cout << i << " " << sum_ref << " " << sum[i] << " " << valid[i] << " " << valid_ref << std::endl; - sum_err++; - } - } - CHECK(sum_err == 0); - CHECK(sum2_err == 0); - CHECK(valid_err == 0); -} diff --git a/tests/MockAcquisitionDeviceTest.cpp b/tests/MockAcquisitionDeviceTest.cpp deleted file mode 100644 index 0c4148ebe..000000000 --- a/tests/MockAcquisitionDeviceTest.cpp +++ /dev/null @@ -1,449 +0,0 @@ -// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute -// SPDX-License-Identifier: GPL-3.0-only - -// MockAcquisitionDevice against HLSSimulatedDevice: the same scenario runs on the HLS simulation, what the card -// wrote is recorded and given to the mock, and the host must see the same on both - the buffers, which module -// went to which buffer (i.e. the order of completions), counters and, for the receiver, its results. One case -// for each kind of output and mode the tests on the mock give it (tests/MockCardOutput.h); [HLSvsMock] selects them. - -#include -#include -#include - -#include "../acquisition_device/HLSSimulatedDevice.h" -#include "../acquisition_device/MockAcquisitionDevice.h" -#include "../common/AzimuthalIntegrationMapping.h" -#include "../image_pusher/TestImagePusher.h" -#include "../receiver/JFJochReceiverService.h" -#include "../receiver/JFJochReceiverTest.h" - -namespace { - // A device that can give away everything it wrote to the host, by frame and module - as long as no buffer was - // used twice - template - class Recording : public Device { - public: - using Device::Device; - - std::map, const DeviceOutput *> Outputs() const { - std::map, const DeviceOutput *> ret; - for (size_t h = 0; h < this->buffer_device.size(); h++) { - const DeviceOutput *output = this->GetDeviceOutput(h); - if (output->module_statistics.packet_count > 0) - ret[{output->module_statistics.frame_number, output->module_statistics.module_number}] = output; - } - return ret; - } - - void CopyOutputs(MockAcquisitionDevice &mock) const { - for (const auto &[key, output]: Outputs()) - mock.SetOutput(key.first, key.second, *output); - } - }; - - using RecordingHLSDevice = Recording; - - void CompareDevices(const AcquisitionDevice &hls, const AcquisitionDevice &mock, const DiffractionExperiment &x, - uint16_t data_stream) { - CHECK(hls.GetBytesReceived() == mock.GetBytesReceived()); - CHECK(hls.Counters().GetSlowestFrameNumber() == mock.Counters().GetSlowestFrameNumber()); - CHECK(hls.Counters().GetFastestFrameNumber() == mock.Counters().GetFastestFrameNumber()); - - auto stats_hls = hls.GetStatistics(); - auto stats_mock = mock.GetStatistics(); - CHECK(stats_hls.good_packets == stats_mock.good_packets); - CHECK(stats_hls.packets_expected == stats_mock.packets_expected); - CHECK(stats_hls.packets_received_per_module == stats_mock.packets_received_per_module); - - int64_t frames = x.GetFrameNum() / x.GetFPGASummation(); - for (int64_t f = 0; f < frames; f++) { - for (int m = 0; m < x.GetModulesNum(data_stream); m++) { - INFO("frame " << f << " module " << m); - auto handle = hls.Counters().GetBufferHandle(f, m); - // The same buffer means the same order of completions - CHECK(handle == mock.Counters().GetBufferHandle(f, m)); - CHECK(hls.Counters().CalculateDelay(f, m) == mock.Counters().CalculateDelay(f, m)); - CHECK(hls.Counters().IsFullModuleCollected(f, m) == mock.Counters().IsFullModuleCollected(f, m)); - if (handle != AcquisitionCounters::HandleNotFound) - CHECK(memcmp(hls.GetDeviceOutput(f, m), mock.GetDeviceOutput(f, m), sizeof(DeviceOutput)) == 0); - } - } - } - - // Runs the same packets on both devices, the mock with what the HLS simulation wrote - template - void CompareOnPackets(const DiffractionExperiment &x, Packets packets, uint16_t data_stream = 0) { - RecordingHLSDevice hls(data_stream, 64); - packets(hls); - REQUIRE_NOTHROW(hls.StartAction(x)); - REQUIRE_NOTHROW(hls.WaitForActionComplete()); - REQUIRE(hls.GetBytesReceived() > 0); - - MockAcquisitionDevice mock(data_stream, 64); - hls.CopyOutputs(mock); - packets(mock); - REQUIRE_NOTHROW(mock.StartAction(x)); - REQUIRE_NOTHROW(mock.WaitForActionComplete()); - - CompareDevices(hls, mock, x, data_stream); - } - - // Raw JUNGFRAU frame of a module: the G0 pedestal of GeneratePedestalCalibration() with a few photons, spots, - // pixels in G1 and G2, one saturated and one bad pixel - std::vector RawFrame(int module) { - std::vector ret(RAW_MODULE_SIZE); - std::mt19937 g(1900 + module); - std::uniform_int_distribution background(0, 300); - for (int i = 0; i < RAW_MODULE_SIZE; i++) - ret[i] = 3000 + 10 * module + i % 50 + background(g); - std::uniform_int_distribution where(20 * RAW_MODULE_COLS, (RAW_MODULE_LINES - 20) * RAW_MODULE_COLS); - for (int s = 0; s < 40; s++) { - const int i0 = where(g); - for (int y = 0; y < 3; y++) - for (int i = 0; i < 3; i++) - ret[i0 + y * RAW_MODULE_COLS + i] = 12000; - } - for (int i = 0; i < RAW_MODULE_SIZE; i += 1001) - ret[i] = 0x4000 | (14000 + i % 50); - for (int i = 0; i < RAW_MODULE_SIZE; i += 1777) - ret[i] = 0xC000 | 13000; - ret[50 * RAW_MODULE_COLS + 500] = 0xC000; - ret[60 * RAW_MODULE_COLS + 600] = 0xFFFF; - return ret; - } - - // JUNGFRAU, internal packet generator: the card loaded as the receiver loads it - calibration, pixel mask, ROI - // map, integration and resolution maps, spot finder - so all its outputs are there - void CompareOnGenerator(const DiffractionExperiment &x, uint16_t data_stream = 0) { - const JFCalibration calib = GeneratePedestalCalibration(x); - const PixelMask mask(x); - const AzimuthalIntegrationMapping azint(x, mask); - SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); - settings.signal_to_noise_threshold = 3; - settings.photon_count_threshold = 2; - settings.min_pix_per_spot = 1; - - CompareOnPackets(x, [&](AcquisitionDevice &d) { - d.InitializeCalibration(x, calib); - d.InitializePixelMask(x, mask); - d.InitializeROIMap(x, x.ExportROIMap()); - d.InitializeDataProcessing(x, azint); - d.SetSpotFinderParameters(settings); - for (int m = 0; m < x.GetModulesNum(data_stream); m++) - d.SetInternalGeneratorFrame(RawFrame(x.GetFirstModuleOfDataStream(data_stream) + m).data(), m); - }, data_stream); - } - - DiffractionExperiment GeneratorExperiment(const DetectorSetup &detector) { - DiffractionExperiment x(detector); - x.PedestalG0Frames(0).NumTriggers(1).ImagesPerTrigger(2).UseInternalPacketGenerator(true) - .IncidentEnergy_keV(12.4).DetectorDistance_mm(75).BeamX_pxl(500).BeamY_pxl(250); - x.ROI().SetROI(ROIDefinition{.boxes = {ROIBox("box", 380, 450, 200, 240)}, - .circles = {ROICircle("circle", 600, 300, 20)}, .azimuthal = {}}); - return x; - } -} - -TEST_CASE("MockAcquisitionDevice_SameAsHLS_Packets", "[MockAcquisitionDevice][HLSvsMock]") { - const uint16_t nmodules = 4; - DiffractionExperiment x(DetJF(nmodules)); - x.Raw(); - x.PedestalG0Frames(0).ImagesPerTrigger(3).NumTriggers(1); - - std::vector module(RAW_MODULE_SIZE); - for (int i = 0; i < RAW_MODULE_SIZE; i++) - module[i] = (7 * i + 534 + 5 * i * i) % UINT16_MAX; - - CompareOnPackets(x, [&](auto &d) { - d.CreateXfelBunchIDPacket(1234.0, 0); - // Frame 1: module 0 incomplete, both halves; module 1 missing; modules 2 and 3 complete - d.CreateJFPacket(x, 1, 0, 0, module.data()); - d.CreateJFPacket(x, 1, 64, 0, module.data()); - d.CreateJFPackets(x, 1, 1, 2, module.data()); - d.CreateJFPackets(x, 1, 1, 3, module.data()); - // Frame 3 before frame 2 for module 0; one packet with a wrong length, one with an Ethernet error - d.CreateJFPacket(x, 3, 5, 0, module.data()); - d.CreateJFPacket(x, 2, 6, 0, module.data()); - d.CreateJFPacket(x, 2, 7, 0, module.data(), -1); - d.CreateJFPacket(x, 2, 8, 0, module.data(), 0, 1); - d.CreateJFPackets(x, 2, 1, 1, module.data()); - d.CreateJFPackets(x, 3, 1, 3, module.data()); - // A frame after the last one - d.CreateJFPacket(x, 4, 1, 2, module.data()); - d.CreateFinalPacket(x); - }); -} - -TEST_CASE("MockAcquisitionDevice_SameAsHLS_Summation", "[MockAcquisitionDevice][HLSvsMock]") { - const uint16_t nmodules = 2; - DiffractionExperiment x(DetJF(nmodules)); - x.PedestalG0Frames(0).ImagesPerTrigger(3).NumTriggers(1).Summation(2) - .JungfrauConvPhotonCnt(false).PixelSigned(true).BitDepthImage(32); - REQUIRE(x.GetFPGASummation() == 2); - - std::vector module(RAW_MODULE_SIZE); - std::mt19937 g(1387); - std::uniform_int_distribution dist(0, UINT16_MAX); - for (auto &i: module) - i = dist(g); - - CompareOnPackets(x, [&](auto &d) { - for (int f = 1; f <= 6; f++) { - d.CreateJFPackets(x, f, 1, 0, module.data()); - // Second frame of the second image is missing for module 1, so the whole image is dropped - if (f != 4) - d.CreateJFPackets(x, f, 1, 1, module.data()); - } - d.CreateFinalPacket(x); - }); -} - -TEST_CASE("MockAcquisitionDevice_SameAsHLS_EIGER", "[MockAcquisitionDevice][HLSvsMock]") { - DiffractionExperiment x(DetEIGER(1)); - x.Raw(); - x.PedestalG0Frames(0).ImagesPerTrigger(1).NumTriggers(1); - - std::vector module(RAW_MODULE_SIZE); - for (int i = 0; i < RAW_MODULE_SIZE; i++) - module[i] = i % (UINT16_MAX - 11); - - CompareOnPackets(x, [&](auto &d) { - for (int row = 0; row < 2; row++) { - for (int col = 0; col < 2; col++) { - for (int i = 0; i < 64; i++) { - if ((i != 10) || (col != row)) - d.CreateEIGERPacket(x, 1, i, 0, col, row, module.data() + 2048 * (128 * row + col + 2 * i)); - } - } - } - d.CreateFinalPacket(x); - }); -} - -TEST_CASE("MockAcquisitionDevice_SameAsHLS_InternalGenerator", "[MockAcquisitionDevice][HLSvsMock]") { - const uint16_t nmodules = 2; - DiffractionExperiment x(DetJF(nmodules)); - x.Raw(); - x.UseInternalPacketGenerator(true).ImagesPerTrigger(3).PedestalG0Frames(0); - - CompareOnPackets(x, [&](auto &d) {}); -} - -// JUNGFRAU modes, with every output of the card: pixels, spot finder, integration, ADU histogram, ROI, pixel counts -TEST_CASE("MockAcquisitionDevice_SameAsHLS_Modes", "[MockAcquisitionDevice][HLSvsMock]") { - DiffractionExperiment x = GeneratorExperiment(DetJF(1)); - x.Conversion(); - - SECTION("conversion, int16") {} - SECTION("conversion, fixed G1") { - x.FixedGainG1(true); - } - SECTION("conversion, uint16") { - x.BitDepthImage(16).PixelSigned(false); - } - SECTION("conversion, uint8") { - x.BitDepthImage(8).PixelSigned(false); - } - SECTION("conversion, int32") { - x.BitDepthImage(32).PixelSigned(true); - } - SECTION("conversion, Poisson") { - x.LossyCompressionPoisson(1); - } - SECTION("conversion, threshold") { - x.PixelValueLowThreshold(50); - } - SECTION("conversion, applied pixel mask") { - x.ApplyPixelMask(true).MaskChipEdges(true); - } - SECTION("conversion, storage cells") { - x.StorageCells(4); - REQUIRE(x.GetStorageCellNumber() == 4); - } - SECTION("conversion, card summation") { - x.Summation(2); - REQUIRE(x.GetFPGASummation() == 2); - } - SECTION("conversion, host summation") { - x.Summation(3).CPUSummation(true); - REQUIRE(x.GetFPGASummation() == 1); - } - SECTION("raw") { - x.Raw(); - } - SECTION("pedestal G0") { - x.Mode(DetectorMode::PedestalG0).PedestalG0Frames(4); - } - SECTION("pedestal G1, fixed G1, storage cells") { - x.Mode(DetectorMode::PedestalG1).FixedGainG1(true).StorageCells(4).PedestalG1Frames(1); - REQUIRE(x.GetStorageCellNumber() == 4); - } - SECTION("pedestal G2, storage cells") { - x.Mode(DetectorMode::PedestalG2).StorageCells(16).PedestalG2Frames(2); - REQUIRE(x.GetStorageCellNumber() == 2); - } - - CompareOnGenerator(x); -} - -TEST_CASE("MockAcquisitionDevice_SameAsHLS_EIGER_BitDepth", "[MockAcquisitionDevice][HLSvsMock]") { - DiffractionExperiment x = GeneratorExperiment(DetEIGER(1)); - x.Raw(); - - SECTION("8 bit") { - x.EigerBitDepth(8); - } - SECTION("16 bit") {} - SECTION("32 bit") { - x.EigerBitDepth(32); - } - - std::vector frame(RAW_MODULE_SIZE * 2); - std::mt19937 g(1387); - std::uniform_int_distribution dist(0, UINT16_MAX); - for (auto &i: frame) - i = dist(g); - - CompareOnPackets(x, [&](AcquisitionDevice &d) { - d.SetInternalGeneratorFrame(frame.data(), 0); - }); -} - -// Each card ends its own data collection: the second of two data streams, then the first -TEST_CASE("MockAcquisitionDevice_SameAsHLS_TwoDataStreams", "[MockAcquisitionDevice][HLSvsMock]") { - DiffractionExperiment x = GeneratorExperiment(DetJF(2)); - x.Conversion(); - x.DataStreams(2); - REQUIRE(x.GetDataStreamsNum() == 2); - - CompareOnGenerator(x, 1); - CompareOnGenerator(x, 0); -} - -TEST_CASE("MockAcquisitionDevice_SameAsHLS_Cancel", "[MockAcquisitionDevice][HLSvsMock]") { - DiffractionExperiment x(DetJF(4)); - x.Raw(); - x.PedestalG0Frames(0).ImagesPerTrigger(5).NumTriggers(1); - - HLSSimulatedDevice hls(0, 64); - MockAcquisitionDevice mock(0, 64); - for (AcquisitionDevice *d: {static_cast(&hls), static_cast(&mock)}) { - REQUIRE_NOTHROW(d->StartAction(x)); - d->Cancel(); - REQUIRE_NOTHROW(d->WaitForActionComplete()); - CHECK(d->Counters().GetSlowestFrameNumber() == 0); - CHECK(d->GetBytesReceived() == 0); - } -} - -// The receiver on the HLS simulation, and on the mock with what the HLS simulation wrote during that run -TEST_CASE("MockAcquisitionDevice_SameAsHLS_Receiver", "[MockAcquisitionDevice][HLSvsMock]") { - const uint16_t nmodules = 2; - DiffractionExperiment x(DetJF(nmodules)); - x.Conversion(); - x.PedestalG0Frames(0).NumTriggers(1).UseInternalPacketGenerator(true).ImagesPerTrigger(4) - .IncidentEnergy_keV(12.4).DetectorDistance_mm(75).BeamX_pxl(500).BeamY_pxl(600) - .Compression(CompressionAlgorithm::BSHUF_LZ4); - x.ROI().SetROI(ROIDefinition{.boxes = {ROIBox("box", 380, 450, 280, 320)}, .circles = {}, .azimuthal = {}}); - - JFCalibration calib(x); - std::vector pedestal(RAW_MODULE_SIZE); - for (int m = 0; m < nmodules; m++) { - for (int i = 0; i < RAW_MODULE_SIZE; i++) - pedestal[i] = 3000 + 10 * m + i % 50; - calib.Pedestal(m, 0).LoadPedestal(pedestal); - } - - // A few photons of background, some spots, one saturated and one bad pixel - std::vector raw(nmodules * RAW_MODULE_SIZE); - std::mt19937 g(1900); - std::uniform_int_distribution background(0, 300); - std::uniform_int_distribution where(20 * RAW_MODULE_COLS, (RAW_MODULE_LINES - 20) * RAW_MODULE_COLS); - for (int i = 0; i < raw.size(); i++) - raw[i] = 3000 + 10 * (i / RAW_MODULE_SIZE) + i % 50 + background(g); - for (int s = 0; s < 80; s++) { - int i0 = where(g) + (s % nmodules) * RAW_MODULE_SIZE; - for (int y = 0; y < 3; y++) - for (int i = 0; i < 3; i++) - raw[i0 + y * RAW_MODULE_COLS + i] = 12000; - } - raw[50 * RAW_MODULE_COLS + 500] = 0xC000; - raw[60 * RAW_MODULE_COLS + 600] = 0xFFFF; - - SpotFindingSettings settings = DiffractionExperiment::DefaultDataProcessingSettings(); - settings.signal_to_noise_threshold = 3; - settings.photon_count_threshold = 2; - settings.min_pix_per_spot = 1; - settings.indexing = false; - - auto run = [&](AcquisitionDeviceGroup &devices, std::vector &image, std::vector> &plots) { - Logger logger("MockAcquisitionDevice_SameAsHLS_Receiver"); - TestImagePusher pusher(x.GetImageNum() - 1); - JFJochReceiverService service(devices, logger, pusher); - service.NumThreads(4); - service.SetSpotFindingSettings(settings); - service.LoadInternalGeneratorImage(x, raw, 0); - PixelMask mask(x); - service.Start(x, mask, &calib); - auto output = service.Stop(); - image = pusher.GetImage(); - for (auto type: {PlotType::SpotCount, PlotType::StrongPixels, PlotType::ErrorPixels, - PlotType::SaturatedPixels, PlotType::PixelSum, PlotType::MaxValue, - PlotType::BkgEstimate, PlotType::PacketsReceived}) { - std::vector v; - service.GetPlotRaw(v, type, ""); - plots.push_back(v); - } - std::vector azint; - service.GetPlotRaw(azint, PlotType::AzInt, ""); - plots.push_back(azint); - std::vector roi; - service.GetPlotRaw(roi, PlotType::ROISum, "box"); - plots.push_back(roi); - return output; - }; - - AcquisitionDeviceGroup hls_devices; - hls_devices.Add(std::make_unique(0, 64)); - std::vector hls_image; - std::vector> hls_plots; - auto hls_output = run(hls_devices, hls_image, hls_plots); - - const auto &hls = dynamic_cast(hls_devices[0]); - auto mock = std::make_unique>(0, 64); - hls.CopyOutputs(*mock); - AcquisitionDeviceGroup mock_devices; - mock_devices.Add(std::move(mock)); - std::vector mock_image; - std::vector> mock_plots; - auto mock_output = run(mock_devices, mock_image, mock_plots); - - REQUIRE(hls_output.status.images_sent == x.GetImageNum()); - CHECK(mock_output.status.images_sent == hls_output.status.images_sent); - CHECK(mock_output.status.max_image_number_sent == hls_output.status.max_image_number_sent); - CHECK(mock_output.status.compressed_size == hls_output.status.compressed_size); - CHECK(mock_output.efficiency == hls_output.efficiency); - CHECK(mock_output.received_packets == hls_output.received_packets); - CHECK(mock_output.expected_packets == hls_output.expected_packets); - CHECK(mock_image == hls_image); - - // The frames of the run; how many of those the generator sends after them to end the data collection are - // written before the receiver cancels the card depends on timing - const auto hls_outputs = hls.Outputs(); - const auto mock_outputs = dynamic_cast &>(mock_devices[0]).Outputs(); - for (uint64_t f = 0; f < x.GetFrameNum(); f++) { - for (uint32_t m = 0; m < nmodules; m++) { - INFO("frame " << f << " module " << m); - REQUIRE(hls_outputs.contains({f, m})); - REQUIRE(mock_outputs.contains({f, m})); - CHECK(memcmp(mock_outputs.at({f, m}), hls_outputs.at({f, m}), sizeof(DeviceOutput)) == 0); - } - } - - REQUIRE(mock_plots.size() == hls_plots.size()); - for (int i = 0; i < hls_plots.size(); i++) { - INFO("plot " << i); - REQUIRE(mock_plots[i].size() == hls_plots[i].size()); - CHECK(memcmp(mock_plots[i].data(), hls_plots[i].data(), hls_plots[i].size() * sizeof(float)) == 0); - } -} diff --git a/tools/jfjoch_fpga_test.cpp b/tools/jfjoch_fpga_test.cpp index 22b5201ec..743e96326 100644 --- a/tools/jfjoch_fpga_test.cpp +++ b/tools/jfjoch_fpga_test.cpp @@ -4,7 +4,6 @@ #include #include "../acquisition_device/PCIExpressDevice.h" -#include "../acquisition_device/HLSSimulatedDevice.h" #include "../receiver/JFJochReceiverTest.h" #include "../tests/CheckImageOutput.h" #include "../common/print_license.h" @@ -13,7 +12,6 @@ void print_usage(Logger &logger) { logger.Info("Usage ./jfjoch_fpga_test {} "); logger.Info("Options:"); logger.Info(" -v Verbose"); - logger.Info(" -H Simulation with C HLS model (doesn't require FPGA device)"); logger.Info(" -E{} EIGER detector mode (with optional bit depth: 8, 16, 32)"); logger.Info(" -R Raw"); logger.Info(" -S Number of summed frames"); @@ -57,7 +55,6 @@ int main(int argc, char **argv) { bool apply_pixel_mask = false; std::optional eiger_bit_depth; DetectorType detector_type = DetectorType::JUNGFRAU; - bool hls_simulation = false; size_t send_buffer_size_MiB = 2048; std::optional lossy_compression_poisson; int64_t thresholding = 0; @@ -69,7 +66,7 @@ int main(int argc, char **argv) { } int opt; - while ((opt = getopt(argc, argv, "s:i:m:N:P:vRIS:E::HB:q:T:cM0ZX:t:f:")) != -1) { + while ((opt = getopt(argc, argv, "s:i:m:N:P:vRIS:E::B:q:T:cM0ZX:t:f:")) != -1) { switch (opt) { case '0': compr = CompressionAlgorithm::NO_COMPRESSION; @@ -116,9 +113,6 @@ int main(int argc, char **argv) { eiger_bit_depth = atol(optarg); } break; - case 'H': - hls_simulation = true; - break; case 'B': send_buffer_size_MiB = atol(optarg); break; @@ -213,26 +207,16 @@ int main(int argc, char **argv) { for (int m = 0; m < x.GetModulesNum(); m++) memcpy(input.data() + RAW_MODULE_SIZE * m, tmp.data(), RAW_MODULE_SIZE * sizeof(uint16_t)); - if (hls_simulation) { - if (nstreams != 1) { - logger.Error("HLS simulation can work with only one device"); - exit(EXIT_FAILURE); - } - auto tmp = std::make_unique(0, 128); - tmp->EnableLogging(&logger); - aq_devices.Add(std::move(tmp)); - } else { - if (nstreams > dev_name.size()) { - logger.Error("Only {} data streams allowed on this platform", dev_name.size()); - exit(EXIT_FAILURE); - } + if (nstreams > dev_name.size()) { + logger.Error("Only {} data streams allowed on this platform", dev_name.size()); + exit(EXIT_FAILURE); + } - for (int i = 0; i < nstreams; i++) { - auto tmp = std::make_unique(i, dev_name[i]); - tmp->EnableLogging(&logger); - tmp->SetIPv4Address((i << 24) + 0x010a0a0a); - aq_devices.Add(std::move(tmp)); - } + for (int i = 0; i < nstreams; i++) { + auto tmp = std::make_unique(i, dev_name[i]); + tmp->EnableLogging(&logger); + tmp->SetIPv4Address((i << 24) + 0x010a0a0a); + aq_devices.Add(std::move(tmp)); } std::atomic done = false;