-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim_scp_private.h
427 lines (396 loc) · 21.3 KB
/
sim_scp_private.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/* sim_scp_private.h: scp library private definitions
Copyright (c) 2023, Mark Pizzolato
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
MARK PIZZOLATO BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of Mark Pizzolato shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from Mark Pizzolato.
This include file may only be included by code in SCP libraries and should
never be included directly in any simulator source code modules.
*/
#ifndef SIM_SCP_PRIVATE_H_
#define SIM_SCP_PRIVATE_H_ 0
#ifdef __cplusplus
extern "C" {
#endif
#include "sim_sock.h"
#if defined(_WIN32)
#define dlopen(X,Y) LoadLibraryA((X))
#define dlsym(X,Y) GetProcAddress((HINSTANCE)(X),(Y))
#define dlclose(X) FreeLibrary((X))
#define SIM_DLOPEN_EXTENSION DLL
#else /* !defined(_WIN32) */
#if defined(SIM_HAVE_DLOPEN)
#include <dlfcn.h>
#define SIM_DLOPEN_EXTENSION SIM_HAVE_DLOPEN
#endif
#endif /* defined(_WIN32) */
#if defined(HAVE_PCRE_H)
#include <pcre.h>
#else /* !defined(HAVE_PCRE_H) */
/* Dynamically loaded PCRE support */
#if !defined(PCRE_DYNAMIC_SETUP)
#define PCRE_DYNAMIC_SETUP
typedef void pcre;
typedef void pcre_extra;
#ifndef PCRE_INFO_CAPTURECOUNT
#define PCRE_INFO_CAPTURECOUNT 2
#define PCRE_ERROR_NOMATCH (-1)
#endif
#ifndef PCRE_NOTBOL
#define PCRE_NOTBOL 0x00000080 /* E D J */
#endif
#ifndef PCRE_CASELESS
#define PCRE_CASELESS 0x00000001 /* C1 */
#endif
/* Pointers to useful PCRE functions */
extern pcre *(*pcre_compile) (const char *, int, const char **, int *, const unsigned char *);
extern const char *(*pcre_version) (void);
extern void (*pcre_free) (void *);
extern int (*pcre_fullinfo) (const pcre *, const pcre_extra *, int, void *);
extern int (*pcre_exec) (const pcre *, const pcre_extra *, const char *, int, int, int, int *, int);
#endif /* PCRE_DYNAMIC_SETUP */
#endif /* HAVE_PCRE_H */
/* Dynamically loaded PNG support */
#if defined(PNG_H) /* This symbol has been defined by png.h since png 1.0.7 in 2000 */
#if !defined(PNG_ROUTINE)
#if PNG_LIBPNG_VER < 10600
#define png_const_structrp png_structp
#define png_structrp png_structp
#define png_const_inforp png_infop
#define png_inforp png_infop
#define png_const_colorp png_colorp
#define png_const_bytep png_bytep
#undef PNG_SETJMP_SUPPORTED
#endif
/* Pointers to useful PNG functions */
#if defined(_WIN32) || defined(ALL_DEPENDENCIES) /* This would be appropriate anytime libpng is specifically listed at link time */
#define PNG_ROUTINE(_type, _name, _args) _type (*p_##_name) _args = &_name;
#else
#define PNG_ROUTINE(_type, _name, _args) _type (*p_##_name) _args;
#endif
#else /* !defined(PNG_ROUTINE) */
#undef PNG_ROUTINE
static struct PNG_Entry {
const char *entry_name;
void **entry_pointer;
} libpng_entries[] = {
#define DEFINING_PNG_ARRAY 1
#define PNG_ROUTINE(_type, _name, _args) {#_name, (void **)&p_##_name},
#endif
/* Return the user pointer associated with the I/O functions */
PNG_ROUTINE(png_voidp, png_get_io_ptr, (png_const_structrp png_ptr))
/* Allocate and initialize png_ptr struct for reading, and any other memory. */
PNG_ROUTINE(png_structp, png_create_read_struct,
(png_const_charp user_png_ver, png_voidp error_ptr,
png_error_ptr error_fn, png_error_ptr warn_fn))
/* Allocate and initialize png_ptr struct for writing, and any other memory */
PNG_ROUTINE(png_structp, png_create_write_struct,
(png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warn_fn))
/* Allocate and initialize the info structure */
PNG_ROUTINE(png_infop, png_create_info_struct, (png_const_structrp png_ptr))
/* Free any memory associated with the png_struct and the png_info_structs */
PNG_ROUTINE(void, png_destroy_read_struct, (png_structpp png_ptr_ptr,
png_infopp info_ptr_ptr, png_infopp end_info_ptr_ptr))
/* Free any memory associated with the png_struct and the png_info_structs */
PNG_ROUTINE(void, png_destroy_write_struct, (png_structpp png_ptr_ptr, png_infopp info_ptr_ptr))
/* Replace the default data output functions with a user supplied one(s).
* If buffered output is not used, then output_flush_fn can be set to NULL.
* If PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile time
* output_flush_fn will be ignored (and thus can be NULL).
* It is probably a mistake to use NULL for output_flush_fn if
* write_data_fn is not also NULL unless you have built libpng with
* PNG_WRITE_FLUSH_SUPPORTED undefined, because in this case libpng's
* default flush function, which uses the standard *FILE structure, will
* be used.
*/
PNG_ROUTINE(void, png_set_write_fn, (png_structrp png_ptr, png_voidp io_ptr,
png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn))
PNG_ROUTINE(void, png_set_PLTE, (png_structrp png_ptr,
png_inforp info_ptr, png_const_colorp palette, int num_palette))
PNG_ROUTINE(void, png_set_IHDR, (png_const_structrp png_ptr,
png_inforp info_ptr, png_uint_32 width, png_uint_32 height, int bit_depth,
int color_type, int interlace_method, int compression_method,
int filter_method))
/* Use blue, green, red order for pixels. */
PNG_ROUTINE(void, png_set_bgr, (png_structrp png_ptr))
PNG_ROUTINE(void, png_write_info,
(png_structrp png_ptr, png_const_inforp info_ptr))
/* Write a row of image data */
PNG_ROUTINE(void, png_write_row, (png_structrp png_ptr,
png_const_bytep row))
/* Write the image data */
PNG_ROUTINE(void, png_write_image, (png_structrp png_ptr, png_bytepp image))
/* Write the end of the PNG file. */
PNG_ROUTINE(void, png_write_end, (png_structrp png_ptr,
png_inforp info_ptr))
#if defined(PNG_SETJMP_SUPPORTED)
/* This function returns the jmp_buf built in to *png_ptr. It must be
* supplied with an appropriate 'longjmp' function to use on that jmp_buf
* unless the default error function is overridden in which case NULL is
* acceptable. The size of the jmp_buf is checked against the actual size
* allocated by the library - the call will return NULL on a mismatch
* indicating an ABI mismatch.
*/
PNG_ROUTINE(jmp_buf*, png_set_longjmp_fn, (png_structrp png_ptr,
png_longjmp_ptr longjmp_fn, size_t jmp_buf_size))
#endif /* PNG_SETJMP_SUPPORTED */
PNG_ROUTINE(png_const_charp, png_get_libpng_ver,
(png_const_structrp png_ptr))
#if defined(ZLIB_VERSION)
PNG_ROUTINE(png_const_charp, zlibVersion,
(void))
#endif
#if defined(DEFINING_PNG_ARRAY)
{0},
};
#undef DEFINING_PNG_ARRAY
#else /* !defined(DEFINING_PNG_ARRAY) */
#undef SIM_SCP_PRIVATE_H_
#include "sim_scp_private.h" /* recurse to generate libpng_entries array */
#endif /* !defined(DEFINING_PNG_ARRAY) */
#endif /* PNG_H */
/* Asynch/Threaded I/O support */
#if defined (SIM_ASYNCH_IO)
#include <pthread.h>
#define SIM_ASYNCH_CLOCKS 1
extern pthread_mutex_t sim_asynch_lock;
extern pthread_cond_t sim_asynch_wake;
extern pthread_mutex_t sim_timer_lock;
extern pthread_cond_t sim_timer_wake;
extern t_bool sim_timer_event_canceled;
extern pthread_t sim_asynch_main_threadid;
extern UNIT * volatile sim_asynch_queue;
extern volatile t_bool sim_idle_wait;
extern int32 sim_asynch_check;
extern int32 sim_asynch_latency;
extern int32 sim_asynch_inst_latency;
/* Thread local storage */
#if defined(thread_local)
#define AIO_TLS thread_local
#elif (__STDC_VERSION__ >= 201112) && !(defined(__STDC_NO_THREADS__))
#define AIO_TLS _Thread_local
#elif defined(__GNUC__) && !defined(__APPLE__) && !defined(__hpux) && !defined(__OpenBSD__) && !defined(_AIX)
#define AIO_TLS __thread
#elif defined(_MSC_VER)
#define AIO_TLS __declspec(thread)
#else
/* Other compiler environment, then don't worry about thread local storage. */
/* It is primarily used only used in debugging messages */
#define AIO_TLS
#endif
#define AIO_QUEUE_CHECK(que, lock) \
do { \
UNIT *_cptr; \
if (lock) \
pthread_mutex_lock (lock); \
for (_cptr = que; \
(_cptr != QUEUE_LIST_END); \
_cptr = _cptr->next) \
if (!_cptr->next) \
SIM_SCP_ABORT ("Queue Corruption detected"); \
if (lock) \
pthread_mutex_unlock (lock); \
} while (0)
#define AIO_MAIN_THREAD (pthread_equal ( pthread_self(), sim_asynch_main_threadid ))
#define AIO_LOCK \
pthread_mutex_lock(&sim_asynch_lock)
#define AIO_UNLOCK \
pthread_mutex_unlock(&sim_asynch_lock)
#define AIO_IS_ACTIVE(uptr) (((uptr)->a_is_active ? (uptr)->a_is_active (uptr) : FALSE) || ((uptr)->a_next))
#if defined(__DECC_VER)
#include <builtins>
#if defined(__IA64) || defined(__ia64)
#define USE_AIO_INTRINSICS 1
#endif
#endif
#if defined(_WIN32) || defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) || defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8)
#define USE_AIO_INTRINSICS 1
#endif
/* Provide a way to test both Intrinsic and Lock based queue manipulations */
/* when both are available on a particular platform */
#if defined(DONT_USE_AIO_INTRINSICS) && defined(USE_AIO_INTRINSICS)
#undef USE_AIO_INTRINSICS
#endif
#ifdef USE_AIO_INTRINSICS
/* This approach uses intrinsics to manage access to the link list head */
/* sim_asynch_queue. This implementation is a completely lock free design */
/* which avoids the potential ABA issues. */
#define AIO_QUEUE_MODE "Lock free asynchronous event queue"
#define AIO_INIT \
do { \
sim_asynch_main_threadid = pthread_self(); \
/* Empty list/list end uses the point value (void *)1. \
This allows NULL in an entry's a_next pointer to \
indicate that the entry is not currently in any list */ \
sim_asynch_queue = QUEUE_LIST_END; \
} while (0)
#define AIO_CLEANUP \
do { \
pthread_mutex_destroy(&sim_asynch_lock); \
pthread_cond_destroy(&sim_asynch_wake); \
pthread_mutex_destroy(&sim_timer_lock); \
pthread_cond_destroy(&sim_timer_wake); \
} while (0)
#ifdef _WIN32
#elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) || defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8)
#define InterlockedCompareExchangePointerAcquire(Destination, Exchange, Comparand) __sync_val_compare_and_swap(Destination, Comparand, Exchange)
#define InterlockedCompareExchangePointerRelease(Destination, Exchange, Comparand) __sync_val_compare_and_swap(Destination, Comparand, Exchange)
#elif defined(__DECC_VER)
#define InterlockedCompareExchangePointerAcquire(Destination, Exchange, Comparand) _InterlockedCompareExchange64_acq(Destination, Exchange, Comparand)
#define InterlockedCompareExchangePointerRelease(Destination, Exchange, Comparand) _InterlockedCompareExchange64_rel(Destination, Exchange, Comparand)
#else
#error "Implementation of function InterlockedCompareExchangePointer() is needed to build with USE_AIO_INTRINSICS"
#endif
#define AIO_ILOCK AIO_LOCK
#define AIO_IUNLOCK AIO_UNLOCK
#if defined(_M_IX86) || defined(_M_X64)
#define AIO_QUEUE_VAL ((UNIT *)sim_asynch_queue)
#else /* !defined(_M_IX86) || defined(_M_X64) */
#define AIO_QUEUE_VAL (UNIT *)(InterlockedCompareExchangePointerAcquire((void * volatile *)&sim_asynch_queue, (void *)sim_asynch_queue, NULL))
#endif /* defined(_M_IX86) || defined(_M_X64) */
#define AIO_QUEUE_SET(newval, oldval) ((UNIT *)(InterlockedCompareExchangePointerRelease((void * volatile *)&sim_asynch_queue, (void *)newval, oldval)))
#define AIO_UPDATE_QUEUE sim_aio_update_queue ()
#define AIO_ACTIVATE(caller, uptr, event_time) \
if (!pthread_equal ( pthread_self(), sim_asynch_main_threadid )) { \
sim_aio_activate ((ACTIVATE_API)caller, uptr, event_time); \
return SCPE_OK; \
} else (void)0
#else /* !USE_AIO_INTRINSICS */
/* This approach uses a pthread mutex to manage access to the link list */
/* head sim_asynch_queue. It will always work, but may be slower than the */
/* lock free approach when using USE_AIO_INTRINSICS */
#define AIO_QUEUE_MODE "Lock based asynchronous event queue"
#define AIO_INIT \
do { \
pthread_mutexattr_t attr; \
\
pthread_mutexattr_init (&attr); \
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); \
pthread_mutex_init (&sim_asynch_lock, &attr); \
pthread_mutexattr_destroy (&attr); \
sim_asynch_main_threadid = pthread_self(); \
/* Empty list/list end uses the point value (void *)1. \
This allows NULL in an entry's a_next pointer to \
indicate that the entry is not currently in any list */ \
sim_asynch_queue = QUEUE_LIST_END; \
} while (0)
#define AIO_CLEANUP \
do { \
pthread_mutex_destroy(&sim_asynch_lock); \
pthread_cond_destroy(&sim_asynch_wake); \
pthread_mutex_destroy(&sim_timer_lock); \
pthread_cond_destroy(&sim_timer_wake); \
} while (0)
#define AIO_ILOCK AIO_LOCK
#define AIO_IUNLOCK AIO_UNLOCK
#define AIO_QUEUE_VAL sim_asynch_queue
#define AIO_QUEUE_SET(newval, oldval) ((sim_asynch_queue = newval),oldval)
#define AIO_UPDATE_QUEUE sim_aio_update_queue ()
#define AIO_ACTIVATE(caller, uptr, event_time) \
if (!pthread_equal ( pthread_self(), sim_asynch_main_threadid )) { \
sim_debug (SIM_DBG_AIO_QUEUE, sim_dflt_dev, "Lock Based Queueing Asynch event for %s after %d %s\n", sim_uname(uptr), event_time, sim_vm_interval_units);\
AIO_LOCK; \
if (uptr->a_next) { /* already queued? */ \
uptr->a_activate_call = sim_activate_abs; \
uptr->a_event_time = MIN (uptr->a_event_time, event_time); \
} else { \
uptr->a_next = sim_asynch_queue; \
uptr->a_event_time = event_time; \
uptr->a_activate_call = (ACTIVATE_API)&caller; \
sim_asynch_queue = uptr; \
} \
if (sim_idle_wait) { \
if (sim_deb) { /* only while debug do lock/unlock overhead */ \
AIO_UNLOCK; \
sim_debug (TIMER_DBG_IDLE, &sim_timer_dev, "wakeup from idle due to async event on %s after %d %s\n", sim_uname(uptr), event_time, sim_vm_interval_units);\
AIO_LOCK; \
} \
pthread_cond_signal (&sim_asynch_wake); \
} \
AIO_UNLOCK; \
sim_asynch_check = 0; /* try to force check */ \
return SCPE_OK; \
} else (void)0
#endif /* USE_AIO_INTRINSICS */
#define AIO_VALIDATE(uptr) \
if (!pthread_equal ( pthread_self(), sim_asynch_main_threadid )) { \
SIM_SCP_ABORT ("Improper thread context for operation detected");\
} else (void)0
#else /* !SIM_ASYNCH_IO */
#define AIO_QUEUE_MODE "Asynchronous I/O is not available"
#define AIO_UPDATE_QUEUE
#define AIO_ACTIVATE(caller, uptr, event_time)
#define AIO_VALIDATE(uptr)
#define AIO_INIT
#define AIO_MAIN_THREAD TRUE
#define AIO_LOCK
#define AIO_UNLOCK
#define AIO_CLEANUP
#define AIO_IS_ACTIVE(uptr) FALSE
#define AIO_TLS
#endif /* SIM_ASYNCH_IO */
/* Private SCP only structures */
#if !defined(SIM_SCP_PRIVATE_DONT_REPEAT)
#define SIM_SCP_PRIVATE_DONT_REPEAT
/* Internal SCP declarations */
extern DEVICE sim_scp_dev;
#define SIM_DBG_INIT 0x00200000 /* initialization activities */
/* Expect rule */
struct EXPTAB {
uint8 *match; /* match string */
uint32 size; /* match string size */
char *match_pattern; /* match pattern for format */
int32 cnt; /* proceed count */
uint32 after; /* delay before halting */
int32 switches; /* flags */
#define EXP_TYP_PERSIST (SWMASK ('P')) /* rule persists after match, default is once a rule matches, it is removed */
#define EXP_TYP_CLEARALL (SWMASK ('C')) /* clear all rules after matching this rule, default is to once a rule matches, it is removed */
#define EXP_TYP_REGEX (SWMASK ('R')) /* rule pattern is a regular expression */
#define EXP_TYP_REGEX_I (SWMASK ('I')) /* regular expression pattern matching should be case independent */
#define EXP_TYP_TIME (SWMASK ('T')) /* halt delay is in microseconds instead of instructions */
pcre *regex; /* compiled regular expression */
int re_nsub; /* regular expression sub expression count */
char *act; /* action string */
};
/* Expect Context */
struct EXPECT {
DEVICE *dptr; /* Device (for Debug) */
uint32 dbit; /* Debugging Bit */
EXPTAB *rules; /* match rules */
int32 size; /* count of match rules */
uint8 *buf; /* buffer of output data which has produced */
uint32 buf_ins; /* buffer insertion point for the next output data */
uint32 buf_size; /* buffer size */
uint32 buf_data; /* count of data in buffer */
};
/* Send Context */
struct SEND {
uint32 delay; /* instruction delay between sent data */
#define SEND_DEFAULT_DELAY 1000 /* default delay instruction count */
DEVICE *dptr; /* Device (for Debug) */
uint32 dbit; /* Debugging Bit */
uint32 after; /* instruction delay before sending any data */
double next_time; /* execution time when next data can be sent */
uint8 *buffer; /* buffer */
size_t bufsize; /* buffer size */
int32 insoff; /* insert offset */
int32 extoff; /* extra offset */
};
#endif /* defined(SIM_SCP_PRIVATE_DONT_REPEAT) */
#ifdef __cplusplus
}
#endif
#endif