ngscopeclient v0.1.1
scopehal.h
Go to the documentation of this file.
1/***********************************************************************************************************************
2* *
3* libscopehal *
4* *
5* Copyright (c) 2012-2025 Andrew D. Zonenberg and contributors *
6* All rights reserved. *
7* *
8* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the *
9* following conditions are met: *
10* *
11* * Redistributions of source code must retain the above copyright notice, this list of conditions, and the *
12* following disclaimer. *
13* *
14* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the *
15* following disclaimer in the documentation and/or other materials provided with the distribution. *
16* *
17* * Neither the name of the author nor the names of any contributors may be used to endorse or promote products *
18* derived from this software without specific prior written permission. *
19* *
20* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
21* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
22* THE AUTHORS BE HELD LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
23* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR *
24* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
25* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
26* POSSIBILITY OF SUCH DAMAGE. *
27* *
28***********************************************************************************************************************/
29
36#ifndef scopehal_h
37#define scopehal_h
38
39#ifdef _WIN32
40
41// These must be included first, as ws2tcpip.h pulls in winsock2.h, which overrides WinSock1 features in windows.h
42#include <ws2tcpip.h>
43#include <windows.h>
44#endif
45
46#include <deque>
47#include <vector>
48#include <string>
49#include <map>
50#include <stdint.h>
51#include <chrono>
52#include <thread>
53#include <memory>
54#include <climits>
55#include <cinttypes>
56#include <set>
57#include <float.h>
58#include <shared_mutex>
59
60#include <sigc++/sigc++.h>
61
62#include <yaml-cpp/yaml.h>
63
64#include "../log/log.h"
65#include "../xptools/TimeUtil.h"
66
67#include "config.h"
68
69//Vulkan is now a mandatory dependency, so no compile time enable flag
70//(disable some warnings in Vulkan headers that we can't do anything about)
71#pragma GCC diagnostic push
72#pragma GCC diagnostic ignored "-Wshadow"
73#include <vulkan/vulkan_raii.hpp>
74#pragma GCC diagnostic pop
75
76//must be early because a lot of inline methods in headers rely on these
77#ifdef __x86_64__
78extern bool g_hasFMA;
79extern bool g_hasAvx512F;
80extern bool g_hasAvx512VL;
81extern bool g_hasAvx512DQ;
82extern bool g_hasAvx2;
83#endif
84
85//Helper for absolute value of an int64_t
86#ifdef _WIN32
87#define i64abs(x) llabs(x)
88#else
89#define i64abs(x) labs(x)
90#endif
91
92//Enable flags for various features
93extern bool g_gpuFilterEnabled;
94extern bool g_hasShaderFloat64;
95extern bool g_hasShaderInt64;
96extern bool g_hasShaderInt16;
97extern bool g_hasShaderInt8;
98extern bool g_hasShaderAtomicFloat;
99extern bool g_hasDebugUtils;
100extern bool g_hasMemoryBudget;
101extern bool g_hasPushDescriptor;
102
103extern size_t g_maxComputeGroupCount[3];
104
106{
107 uint32_t end;
108 uint32_t filterlen;
109};
110
111uint32_t GetComputeBlockCount(size_t numGlobal, size_t blockSize);
112
113#include "Unit.h"
114#include "Bijection.h"
115#include "IDTable.h"
116
117#include "AcceleratorBuffer.h"
118#include "ComputePipeline.h"
119
120#include "SCPITransport.h"
121#include "SCPISocketTransport.h"
122#include "SCPITwinLanTransport.h"
123#include "SCPILxiTransport.h"
124#include "SCPINullTransport.h"
125#include "SCPIUARTTransport.h"
126#include "SCPIHIDTransport.h"
127#include "VICPSocketTransport.h"
128#include "SCPIDevice.h"
129#ifdef __linux
131#endif
132
133#if !defined(_WIN32) && !defined(__APPLE__)
134// TMC is only supported on Linux for now
135// https://github.com/glscopeclient/scopehal/issues/519
136#include "SCPITMCTransport.h"
137#endif
138
139#include "FlowGraphNode.h"
140#include "SinkNode.h"
141#include "Instrument.h"
142#include "StreamDescriptor.h"
143
144#include "OscilloscopeChannel.h"
147#include "Trigger.h"
148
149#include "BERT.h"
150#include "BinaryDriver.h"
151#include "CommandLineDriver.h"
152#include "DigitalIOChannel.h"
153#include "DigitalInputChannel.h"
154#include "DigitalOutputChannel.h"
155#include "FunctionGenerator.h"
157#include "Load.h"
158#include "CANChannel.h"
159#include "Multimeter.h"
160#include "MultimeterChannel.h"
161#include "Oscilloscope.h"
162#include "SParameterChannel.h"
163#include "PowerSupply.h"
164#include "PowerSupplyChannel.h"
165#include "RFSignalGenerator.h"
166#include "RFSignalGeneratorChannel.h"
167#include "SCPIInstrument.h"
168#include "HIDInstrument.h"
169#include "ModbusInstrument.h"
170#include "SCPIBERT.h"
171#include "SCPIFunctionGenerator.h"
172#include "SCPILoad.h"
173#include "SCPIMiscInstrument.h"
174#include "SCPIMultimeter.h"
175#include "SCPIOscilloscope.h"
176#include "SCPIPowerSupply.h"
177#include "SCPIRFSignalGenerator.h"
178#include "SpectrometerDarkFrameChannel.h"
179#include "SCPISA.h"
180#include "SCPISDR.h"
181#include "SCPISpectrometer.h"
182#include "SCPIVNA.h"
183#include "SwitchMatrix.h"
184
185#include "SParameters.h"
186#include "TouchstoneParser.h"
187#include "IBISParser.h"
188
189#include "FilterParameter.h"
190#include "Filter.h"
191#include "ImportFilter.h"
192#include "PeakDetectionFilter.h"
193#include "SpectrumChannel.h"
195#include "SParameterFilter.h"
196
197#include "FilterGraphExecutor.h"
198
199#include "QueueManager.h"
200
201uint64_t ConvertVectorSignalToScalar(const std::vector<bool>& bits);
202
203std::string GetDefaultChannelColor(int i);
204
205std::string Trim(const std::string& str);
206std::string TrimQuotes(const std::string& str);
207std::string BaseName(const std::string& path);
208
209std::string ReadFile(const std::string& path);
210std::string ReadDataFile(const std::string& relpath);
211std::vector<uint32_t> ReadDataFileUint32(const std::string& relpath);
212std::string FindDataFile(const std::string& relpath);
213void GetTimestampOfFile(std::string path, time_t& timestamp, int64_t& fs);
214
215std::string to_string_sci(double d);
216std::string to_string_hex(uint64_t n, bool zeropad = false, int len = 0);
217
219void DriverStaticInit();
220
221bool VulkanInit(bool skipGLFW = false);
222void InitializeSearchPaths();
223void InitializePlugins();
224void DetectCPUFeatures();
225std::string GetDirOfCurrentExecutable();
226
227void ScopehalStaticCleanup();
228
229float FreqToPhase(float hz);
230
231uint64_t next_pow2(uint64_t v);
232uint64_t prev_pow2(uint64_t v);
233
234std::vector<std::string> explode(const std::string& str, char separator);
235std::string str_replace(const std::string& search, const std::string& replace, const std::string& subject);
236std::string strtolower(const std::string& s);
237
238#define FS_PER_PICOSECOND 1e3
239#define FS_PER_NANOSECOND 1e6
240#define FS_PER_MICROSECOND 1e9
241#define FS_PER_SECOND 1e15
242#define SECONDS_PER_FS 1e-15
243
244//string to size_t conversion
245#ifdef _WIN32
246#define stos(str) static_cast<size_t>(stoll(str))
247#else
248#define stos(str) static_cast<size_t>(stol(str))
249#endif
250
251extern std::vector<std::string> g_searchPaths;
252
253//Shader args for frequently used kernels
255{
256 uint32_t size;
257 float gain;
258 float offset;
259};
260
261//Vulkan global stuff
262extern vk::raii::Context g_vkContext;
263extern std::unique_ptr<vk::raii::Instance> g_vkInstance;
264extern uint8_t g_vkComputeDeviceUuid[16];
265extern uint32_t g_vkComputeDeviceDriverVer;
266extern vk::raii::PhysicalDevice* g_vkComputePhysicalDevice;
267extern std::unique_ptr<QueueManager> g_vkQueueManager;
268extern bool g_vulkanDeviceIsIntelMesa;
269extern bool g_vulkanDeviceIsAnyMesa;
270extern bool g_vulkanDeviceIsMoltenVK;
271extern uint32_t g_vkPinnedMemoryHeap;
272extern uint32_t g_vkLocalMemoryHeap;
274extern std::shared_mutex g_vulkanActivityMutex;;
275
276//Validation helper for templates
277//Throws compile-time error if specialized for false since there's no implementation
278template<bool> class CompileTimeAssert;
279template<> class CompileTimeAssert<true>{};
280
281#ifdef _WIN32
282std::string NarrowPath(wchar_t* wide);
283#else
284std::string ExpandPath(const std::string& in);
285void CreateDirectory(const std::string& path);
286#endif
287
288//Checksum helpers
289uint32_t CRC32(const uint8_t* bytes, size_t start, size_t end);
290uint32_t CRC32(const std::vector<uint8_t>& bytes);
291
292uint32_t ColorFromString(const std::string& str, unsigned int alpha = 255);
293
294const char* ScopehalGetVersion();
295
296#endif
Declaration of AcceleratorBuffer.
Declaration of BERT.
Declaration of Bijection.
Helper class for binary driver: provides methods for handling binary data.
Declaration of CANChannel, CANSymbol, and CANWaveform.
Helper class for command line drivers: provides helper methods for command line based communication w...
Declaration of ComputePipeline.
Declaration of DigitalIOChannel.
Declaration of DigitalInputChannel.
Declaration of DigitalOutputChannel.
Declaration of FilterGraphExecutor.
Declaration of FilterParameter.
Declaration of Filter.
Declaration of FlowGraphNode.
Inline functions for FlowGraphNode which reference OscilloscopeChannel class.
Declaration of FunctionGeneratorChannel.
Declaration of FunctionGenerator.
Declaration of HIDInstrument.
Declaration of IBISParser, IVCurve, IBISCorner, and VTCurves.
Declaration of IDTable class.
Declaration of ImportFilter.
Declaration of Instrument.
Declaration of Load.
Declaration of ModbusInstrument.
Declaration of MultimeterChannel.
Declaration of Multimeter.
Declaration of OscilloscopeChannel.
Declaration of Oscilloscope.
Declaration of PeakDetectionFilter.
Declaration of QueueManager and QueueHandle.
Declaration of SCPIHIDTransport.
Declaration of SCPILxiTransport.
Declaration of SCPINullTransport.
Declaration of SA.
Declaration of SCPISDR.
Declaration of SCPISocketCANTransport.
Declaration of SCPISocketTransport.
Declaration of Spectrometer.
Declaration of SCPITMCTransport.
Declaration of SCPITransport.
Declaration of SCPITwinLanTransport.
Declaration of SCPIUARTTransport.
Declaration of VNA.
Declaration of SParameterChannel.
Declaration of SParameterFilter.
Declaration of SParameterSourceFilter.
Declaration of SParameters and related classes.
Declaration of SinkNode.
Declaration of StreamDescriptor.
Inline functions for StreamDescriptor.
Declaration of SwitchMatrix.
Declaration of TouchstoneParser.
Declaration of Trigger.
Declaration of Unit.
Declaration of VICPSocketTransport.
uint32_t ColorFromString(const string &str, unsigned int alpha)
Converts a hex color code plus externally supplied default alpha value into a packed RGBA color.
Definition: Waveform.cpp:272
Definition: scopehal.h:278
bool g_hasShaderAtomicFloat
Indicates whether atomic operations on the float type are available in shaders.
Definition: VulkanInit.cpp:189
uint8_t g_vkComputeDeviceUuid[16]
UUID of g_vkComputeDevice.
Definition: VulkanInit.cpp:130
bool VulkanInit(bool skipGLFW=false)
Initialize a Vulkan context for compute.
Definition: VulkanInit.cpp:258
bool g_vulkanDeviceIsIntelMesa
Indicates that the Vulkan driver is Mesa on an Intel GPU.
Definition: VulkanInit.cpp:235
bool g_hasShaderInt64
Indicates whether the int64 type is available for use in shaders and SSBOs.
Definition: VulkanInit.cpp:171
std::unique_ptr< vk::raii::Instance > g_vkInstance
Global Vulkan instance.
Definition: VulkanInit.cpp:65
bool g_hasPushDescriptor
Indicates whether the VK_KHR_push_descriptor extension is available.
Definition: VulkanInit.cpp:207
bool g_hasMemoryBudget
Indicates whether the VK_EXT_memory_budget extension is available.
Definition: VulkanInit.cpp:201
std::unique_ptr< QueueManager > g_vkQueueManager
Allocates QueueHandle objects.
Definition: VulkanInit.cpp:106
bool g_hasShaderInt16
Indicates whether the int16 type is available for use in shaders and SSBOs.
Definition: VulkanInit.cpp:177
bool g_hasDebugUtils
Indicates whether the VK_EXT_debug_utils extension is available.
Definition: VulkanInit.cpp:195
bool g_vulkanDeviceIsMoltenVK
Indicates that the Vulkan driver is MoltenVK.
Definition: VulkanInit.cpp:247
vk::raii::Context g_vkContext
Global Vulkan context.
Definition: VulkanInit.cpp:59
bool g_hasShaderFloat64
Indicates whether the float64 type is available for use in shaders and SSBOs.
Definition: VulkanInit.cpp:165
uint32_t g_vkLocalMemoryHeap
Heap from which g_vkLocalMemoryType is allocated.
Definition: VulkanInit.cpp:154
uint32_t g_vkPinnedMemoryHeap
Heap from which g_vkPinnedMemoryType is allocated.
Definition: VulkanInit.cpp:148
size_t g_maxComputeGroupCount[3]
Maximum size of a Vulkan dispatch group for compute shaders, in each axis.
Definition: VulkanInit.cpp:226
bool g_vulkanDeviceIsAnyMesa
Indicates that the Vulkan driver is Mesa on any GPU.
Definition: VulkanInit.cpp:241
vk::raii::PhysicalDevice * g_vkComputePhysicalDevice
Physical device for g_vkComputeDevice.
Definition: VulkanInit.cpp:142
bool g_hasShaderInt8
Indicates whether the int8 type is available for use in shaders and SSBOs.
Definition: VulkanInit.cpp:183
bool g_vulkanDeviceHasUnifiedMemory
Indicates whether the Vulkan device is unified memory.
Definition: VulkanInit.cpp:220
uint32_t g_vkComputeDeviceDriverVer
Driver version of g_vkComputeDevice.
Definition: VulkanInit.cpp:136
string str_replace(const string &search, const string &replace, const string &subject)
Replaces all occurrences of the search string with "replace" in the given string.
Definition: scopehal.cpp:910
string TrimQuotes(const string &str)
Removes quotes from the start and end of a string.
Definition: scopehal.cpp:543
vector< uint32_t > ReadDataFileUint32(const string &relpath)
Locates and returns the contents of a data file as a std::vector<uint32_t>
Definition: scopehal.cpp:804
uint64_t ConvertVectorSignalToScalar(const vector< bool > &bits)
Converts a vector bus signal into a scalar (up to 64 bits wide)
Definition: scopehal.cpp:335
vector< string > explode(const string &str, char separator)
Splits a string up into an array separated by delimiters.
Definition: scopehal.cpp:876
string strtolower(const string &s)
Converts a string to lower case.
Definition: scopehal.cpp:899
string Trim(const string &str)
Removes whitespace from the start and end of a string.
Definition: scopehal.cpp:511
string ReadFile(const string &path)
Returns the contents of a file.
Definition: scopehal.cpp:653
string FindDataFile(const string &relpath)
Locates a data file.
Definition: scopehal.cpp:836
string ReadDataFile(const string &relpath)
Locates and returns the contents of a data file as a std::string.
Definition: scopehal.cpp:771
void TransportStaticInit()
Static initialization for SCPI transports.
Definition: scopehal.cpp:161
void InitializePlugins()
Initialize all plugins.
Definition: scopehal.cpp:346
const char * ScopehalGetVersion()
Returns the library version string (Semantic Version formatted)
Definition: scopehal.cpp:1027
uint32_t CRC32(const uint8_t *bytes, size_t start, size_t end)
Calculates a CRC32 checksum using the standard Ethernet polynomial.
Definition: scopehal.cpp:996
uint64_t prev_pow2(uint64_t v)
Rounds a 64-bit integer down to the next power of 2.
Definition: scopehal.cpp:640
std::string GetDirOfCurrentExecutable()
Gets the path to the directory containing the current executable.
Definition: scopehal.cpp:685
std::string to_string_sci(double d)
Like std::to_string, but output in scientific notation.
Definition: scopehal.cpp:588
uint64_t next_pow2(uint64_t v)
Rounds a 64-bit integer up to the next power of 2.
Definition: scopehal.cpp:616
std::string to_string_hex(uint64_t n, bool zeropad=false, int len=0)
Like std::to_string, but output in hex.
Definition: scopehal.cpp:598
void DetectCPUFeatures()
Static initialization for CPU feature flags.
Definition: scopehal.cpp:188
void DriverStaticInit()
Static initialization for instrument drivers.
Definition: scopehal.cpp:230
bool g_gpuFilterEnabled
True if filters can use GPU acceleration.
Definition: scopehal.cpp:142
std::shared_mutex g_vulkanActivityMutex
Mutex for controlling access to background Vulkan activity.
Definition: scopehal.cpp:156
float FreqToPhase(float hz)
Converts a frequency in Hz to a phase velocity in rad/sec.
Definition: scopehal.cpp:580
Definition: scopehal.h:255
Definition: scopehal.h:106