ngscopeclient v0.2.1
Loading...
Searching...
No Matches
scopehal.h
Go to the documentation of this file.
1/***********************************************************************************************************************
2* *
3* libscopehal *
4* *
5* Copyright (c) 2012-2026 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#ifdef HAVE_NVTX
70#include <nvtx3/nvtx3.hpp>
71#endif
72
73//Vulkan is now a mandatory dependency, so no compile time enable flag
74//(disable some warnings in Vulkan headers that we can't do anything about)
75#pragma GCC diagnostic push
76#pragma GCC diagnostic ignored "-Wshadow"
77#include <vulkan/vulkan_raii.hpp>
78#pragma GCC diagnostic pop
79
80//must be early because a lot of inline methods in headers rely on these
81#ifdef __x86_64__
82extern bool g_hasFMA;
83extern bool g_hasAvx512F;
84extern bool g_hasAvx512VL;
85extern bool g_hasAvx512DQ;
86extern bool g_hasAvx2;
87#endif
88
89//Helper for absolute value of an int64_t
90#ifdef _WIN32
91#define i64abs(x) llabs(x)
92#else
93#define i64abs(x) labs(x)
94#endif
95
96//Enable flags for various Vulkan features
97extern bool g_hasShaderFloat64;
98extern bool g_hasShaderInt64;
99extern bool g_hasShaderAtomicInt64;
100extern bool g_hasShaderInt16;
101extern bool g_hasShaderInt8;
102extern bool g_hasShaderAtomicFloat;
103extern bool g_hasDebugUtils;
104extern bool g_hasMemoryBudget;
105extern bool g_hasPushDescriptor;
106
107extern size_t g_maxComputeGroupCount[3];
108
110{
111 uint32_t end;
112 uint32_t filterlen;
113};
114
115uint32_t GetComputeBlockCount(size_t numGlobal, size_t blockSize);
116
117#include "Unit.h"
118#include "Bijection.h"
119#include "IDTable.h"
120
121#include "AcceleratorBuffer.h"
122#include "ScratchBufferManager.h"
123#include "ComputePipeline.h"
124
125#include "SCPITransport.h"
126#include "SCPISocketTransport.h"
127#include "SCPITwinLanTransport.h"
128#include "SCPILxiTransport.h"
129#include "SCPINullTransport.h"
130#include "SCPIUARTTransport.h"
131#include "SCPIHIDTransport.h"
132#include "VICPSocketTransport.h"
133#include "SCPIDevice.h"
134#ifdef __linux
136#endif
137
138#if !defined(_WIN32) && !defined(__APPLE__)
139// TMC is only supported on Linux for now
140// https://github.com/glscopeclient/scopehal/issues/519
141#include "SCPITMCTransport.h"
142#endif
143
144#include "FlowGraphNode.h"
145#include "SinkNode.h"
146#include "Instrument.h"
147#include "StreamDescriptor.h"
149#include "InputConstraint.h"
150
151#include "OscilloscopeChannel.h"
155#include "Trigger.h"
156
157#include "BERT.h"
158#include "BinaryDriver.h"
159#include "CommandLineDriver.h"
160#include "DigitalIOChannel.h"
161#include "DigitalInputChannel.h"
162#include "DigitalOutputChannel.h"
163#include "VectorGPIOChannel.h"
164#include "VIOInputChannel.h"
165#include "VIOOutputChannel.h"
166#include "FunctionGenerator.h"
168#include "Load.h"
169#include "CANChannel.h"
170#include "Multimeter.h"
171#include "MultimeterChannel.h"
172#include "Oscilloscope.h"
173#include "SParameterChannel.h"
174#include "PowerSupply.h"
175#include "PowerSupplyChannel.h"
176#include "RFSignalGenerator.h"
177#include "RFSignalGeneratorChannel.h"
178#include "SCPIInstrument.h"
179#include "MockInstrument.h"
180#include "HIDInstrument.h"
181#include "ModbusInstrument.h"
182#include "SCPIBERT.h"
183#include "SCPIFunctionGenerator.h"
184#include "SCPILoad.h"
185#include "SCPIMiscInstrument.h"
186#include "SCPIMultimeter.h"
187#include "SCPIOscilloscope.h"
188#include "SCPIPowerSupply.h"
189#include "SCPIRFSignalGenerator.h"
190#include "SpectrometerDarkFrameChannel.h"
191#include "SCPISA.h"
192#include "SCPISDR.h"
193#include "SCPISpectrometer.h"
194#include "SCPIVNA.h"
195#include "SwitchMatrix.h"
196
197#include "SParameters.h"
198#include "TouchstoneParser.h"
199#include "IBISParser.h"
200
201#include "FilterParameter.h"
202#include "Filter.h"
203#include "ImportFilter.h"
204#include "PeakDetectionFilter.h"
205#include "SpectrumChannel.h"
207#include "SParameterFilter.h"
208
209#include "FilterGraphExecutor.h"
210
211#include "QueueManager.h"
212#include "NamedDebugRange.h"
213
214uint64_t ConvertVectorSignalToScalar(const std::vector<bool>& bits);
215
216std::string GetDefaultChannelColor(int i);
217
218std::string Trim(const std::string& str);
219std::string TrimQuotes(const std::string& str);
220std::string BaseName(const std::string& path);
221
222std::string ReadFile(const std::string& path);
223std::string ReadDataFile(const std::string& relpath);
224std::vector<uint32_t> ReadDataFileUint32(const std::string& relpath);
225std::string FindDataFile(const std::string& relpath);
226void GetTimestampOfFile(const std::string& path, time_t& timestamp, int64_t& fs);
227
228std::string to_string_sci(double d);
229std::string to_string_hex(uint64_t n, bool zeropad = false, int len = 0);
230
232void DriverStaticInit();
233
234bool VulkanInit(bool skipGLFW = false);
235void InitializeSearchPaths();
236void InitializePlugins();
237void DetectCPUFeatures();
238std::string GetDirOfCurrentExecutable();
239
240void ScopehalStaticCleanup();
241
242float FreqToPhase(float hz);
243
246
247std::vector<std::string> explode(const std::string& str, char separator);
248std::string str_replace(const std::string& search, const std::string& replace, const std::string& subject);
249std::string strtolower(const std::string& s);
250
251#define FS_PER_PICOSECOND 1e3
252#define FS_PER_NANOSECOND 1e6
253#define FS_PER_MICROSECOND 1e9
254#define FS_PER_SECOND 1e15
255#define SECONDS_PER_FS 1e-15
256
257//string to size_t conversion
258#ifdef _WIN32
259#define stos(str) static_cast<size_t>(stoll(str))
260#else
261#define stos(str) static_cast<size_t>(stol(str))
262#endif
263
264extern std::vector<std::string> g_searchPaths;
265
266//Shader args for frequently used kernels
268{
269 uint32_t size;
270 float gain;
271 float offset;
272};
273
275{
276 uint32_t size;
277 float gain;
278 float offset;
279 uint32_t inputBufferOffset;
280};
281
282//Vulkan global stuff
283extern vk::raii::Context g_vkContext;
284extern std::unique_ptr<vk::raii::Instance> g_vkInstance;
287extern vk::raii::PhysicalDevice* g_vkComputePhysicalDevice;
288extern std::unique_ptr<QueueManager> g_vkQueueManager;
289extern bool g_vulkanDeviceIsIntelMesa;
290extern bool g_vulkanDeviceIsAnyMesa;
291extern bool g_vulkanDeviceIsMoltenVK;
292extern bool g_vulkanDeviceIsApplePV;
296extern std::shared_mutex g_vulkanActivityMutex;
297
298//Validation helper for templates
299//Throws compile-time error if specialized for false since there's no implementation
300template<bool> class CompileTimeAssert;
301template<> class CompileTimeAssert<true>{};
302
303#ifdef _WIN32
304std::string NarrowPath(wchar_t* wide);
305#else
306std::string ExpandPath(const std::string& in);
307void CreateDirectory(const std::string& path);
308#endif
309
310//Checksum helpers
311uint32_t CRC32(const uint8_t* bytes, size_t start, size_t end);
312uint32_t CRC32(const std::vector<uint8_t>& bytes);
313
314uint32_t ColorFromString(const std::string& str, unsigned int alpha = 255);
315
316const char* ScopehalGetVersion();
317
318#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 InputConstraint.
Declaration of Instrument.
Declaration of Load.
Declaration of MockInstrument.
Declaration of ModbusInstrument.
Declaration of MultimeterChannel.
Declaration of Multimeter.
Declaration of NamedDebugRange.
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 ScratchBufferManager.
Declaration of SinkNode.
Declaration of StreamDescriptor.
Inline functions for StreamDescriptor.
Declaration of StreamGroupDescriptor.
Inline functions for StreamGroupDescriptor.
Declaration of SwitchMatrix.
Declaration of TouchstoneParser.
Declaration of Trigger.
Declaration of Unit.
Declaration of VICPSocketTransport.
Declaration of VIOInputChannel.
Declaration of VIOOutputChannel.
Declaration of VectorGPIOChannel.
Definition AcceleratorBuffer.h:204
Definition scopehal.h:300
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:1064
bool g_vulkanDeviceIsIntelMesa
Indicates that the Vulkan driver is Mesa on an Intel GPU.
Definition VulkanInit.cpp:241
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:213
bool g_hasMemoryBudget
Indicates whether the VK_EXT_memory_budget extension is available.
Definition VulkanInit.cpp:207
bool g_vulkanDeviceIsApplePV
Indicates that the Vulkan driver is an Apple paravirtualization device.
Definition VulkanInit.cpp:259
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:201
bool g_hasShaderAtomicInt64
Indicates whether atomic operations on the int64 type are available in shaders.
Definition VulkanInit.cpp:195
bool g_vulkanDeviceIsMoltenVK
Indicates that the Vulkan driver is MoltenVK.
Definition VulkanInit.cpp:253
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:232
bool g_vulkanDeviceIsAnyMesa
Indicates that the Vulkan driver is Mesa on any GPU.
Definition VulkanInit.cpp:247
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:226
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:1013
string TrimQuotes(const string &str)
Removes quotes from the start and end of a string.
Definition scopehal.cpp:617
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:907
uint64_t ConvertVectorSignalToScalar(const vector< bool > &bits)
Converts a vector bus signal into a scalar (up to 64 bits wide)
Definition scopehal.cpp:409
vector< string > explode(const string &str, char separator)
Splits a string up into an array separated by delimiters.
Definition scopehal.cpp:979
string strtolower(const string &s)
Converts a string to lower case.
Definition scopehal.cpp:1002
string Trim(const string &str)
Removes whitespace from the start and end of a string.
Definition scopehal.cpp:585
string ReadFile(const string &path)
Returns the contents of a file.
Definition scopehal.cpp:727
string FindDataFile(const string &relpath)
Locates a data file.
Definition scopehal.cpp:939
string ReadDataFile(const string &relpath)
Locates and returns the contents of a data file as a std::string.
Definition scopehal.cpp:863
void TransportStaticInit()
Static initialization for SCPI transports.
Definition scopehal.cpp:224
void InitializePlugins()
Initialize all plugins.
Definition scopehal.cpp:420
const char * ScopehalGetVersion()
Returns the library version string (Semantic Version formatted)
Definition scopehal.cpp:1114
uint32_t CRC32(const uint8_t *bytes, size_t start, size_t end)
Calculates a CRC32 checksum in network byte order using the standard Ethernet polynomial.
Definition scopehal.cpp:1098
uint64_t prev_pow2(uint64_t v)
Rounds a 64-bit integer down to the next power of 2.
Definition scopehal.cpp:714
std::string GetDirOfCurrentExecutable()
Gets the path to the directory containing the current executable.
Definition scopehal.cpp:776
std::string to_string_sci(double d)
Like std::to_string, but output in scientific notation.
Definition scopehal.cpp:662
uint64_t next_pow2(uint64_t v)
Rounds a 64-bit integer up to the next power of 2.
Definition scopehal.cpp:690
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:672
void DetectCPUFeatures()
Static initialization for CPU feature flags.
Definition scopehal.cpp:251
void DriverStaticInit()
Static initialization for instrument drivers.
Definition scopehal.cpp:293
std::shared_mutex g_vulkanActivityMutex
Mutex for controlling access to background Vulkan activity.
Definition scopehal.cpp:219
float FreqToPhase(float hz)
Converts a frequency in Hz to a phase velocity in rad/sec.
Definition scopehal.cpp:654
Definition scopehal.h:275
Definition scopehal.h:268
Definition scopehal.h:110