1 /* sim_defs.h: simulator definitions
2 
3    Copyright (c) 1993-2008 Robert M Supnik
4    Copyright (c) 2021 The DPS8M Development Team
5 
6    Permission is hereby granted, free of charge, to any person obtaining a
7    copy of this software and associated documentation files (the "Software"),
8    to deal in the Software without restriction, including without limitation
9    the rights to use, copy, modify, merge, publish, distribute, sublicense,
10    and/or sell copies of the Software, and to permit persons to whom the
11    Software is furnished to do so, subject to the following conditions:
12 
13    The above copyright notice and this permission notice shall be included in
14    all copies or substantial portions of the Software.
15 
16    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19    ROBERT M SUPNIK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20    IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21    CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 
23    Except as contained in this notice, the name of Robert M Supnik shall not be
24    used in advertising or otherwise to promote the sale, use or other dealings
25    in this Software without prior written authorization from Robert M Supnik.
26 */
27 
28 /*
29    The interface between the simulator control package (SCP) and the
30    simulator consists of the following routines and data structures
31 
32         sim_name                simulator name string
33         sim_devices[]           array of pointers to simulated devices
34         sim_PC                  pointer to saved PC register descriptor
35         sim_interval            simulator interval to next event
36         sim_stop_messages[]     array of pointers to stop messages
37         sim_instr()             instruction execution routine
38         sim_emax                maximum number of words in an instruction
39 
40    In addition, the simulator must supply routines to print and parse
41    architecture specific formats
42 
43         print_sym               print symbolic output
44         parse_sym               parse symbolic input
45 */
46 
47 #ifndef SIM_DEFS_H_
48 # define SIM_DEFS_H_    0
49 
50 # include <stddef.h>
51 # include <stdlib.h>
52 # include <stdio.h>
53 # if defined(_MSC_VER) && (_MSC_VER < 1900)
54 #  define snprintf _snprintf
55 # endif
56 # include <stdarg.h>
57 # include <string.h>
58 # include <errno.h>
59 # include <limits.h>
60 
61 # ifdef _WIN32
62 #  include <winsock2.h>
63 #  undef PACKED                       /* avoid macro name collision */
64 #  undef ERROR                        /* avoid macro name collision */
65 #  undef MEM_MAPPED                   /* avoid macro name collision */
66 #  include <process.h>
67 # endif
68 
69 /* avoid macro names collisions */
70 # ifdef MAX
71 #  undef MAX
72 # endif
73 # ifdef MIN
74 #  undef MIN
75 # endif
76 # ifdef PMASK
77 #  undef PMASK
78 # endif
79 # ifdef RS
80 #  undef RS
81 # endif
82 # ifdef PAGESIZE
83 #  undef PAGESIZE
84 # endif
85 
86 
87 # ifndef TRUE
88 #  define TRUE            1
89 #  define FALSE           0
90 # endif
91 
92 /* SCP API shim.
93 
94    The SCP API for version 4.0 introduces a number of "pointer-to-const"
95    parameter qualifiers that were not present in the 3.x versions.  To maintain
96    compatibility with the earlier versions, the new qualifiers are expressed as
97    "CONST" rather than "const".  This allows macro removal of the qualifiers
98    when compiling for SIMH 3.x.
99 */
100 # ifndef CONST
101 #  define CONST const
102 # endif
103 
104 /* Length specific integer declarations */
105 
106 typedef signed char     int8;
107 typedef signed short    int16;
108 typedef signed int      int32;
109 typedef unsigned char   uint8;
110 typedef unsigned short  uint16;
111 typedef unsigned int    uint32;
112 typedef int             t_stat;                         /* status */
113 typedef int             t_bool;                         /* boolean */
114 
115 /* 64b integers */
116 
117 # if defined (__GNUC__)                                  /* GCC */
118 typedef signed long long        t_int64;
119 typedef unsigned long long      t_uint64;
120 # elif defined (_WIN32)                                  /* Windows */
121 typedef signed __int64          t_int64;
122 typedef unsigned __int64        t_uint64;
123 # else                                                   /* default */
124 #  define t_int64                 signed long long
125 #  define t_uint64                unsigned long long
126 # endif                                                  /* end 64b */
127 # ifndef INT64_C
128 #  define INT64_C(x)      x ## LL
129 # endif
130 
131 typedef t_int64         t_svalue;                       /* signed value */
132 typedef t_uint64        t_value;                        /* value */
133 typedef uint32          t_addr;
134 # define T_ADDR_W        32
135 # define T_ADDR_FMT      ""
136 
137 # if defined (_WIN32)
138 #  define vsnprintf _vsnprintf
139 # endif
140 # define STACKBUFSIZE 2048
141 
142 # if defined (_WIN32) /* Actually, a GCC issue */
143 #  define LL_FMT "I64"
144 # else
145 #  define LL_FMT "ll"
146 # endif
147 
148 # if defined (_WIN32)
149 #  define NULL_DEVICE "NUL:"
150 # else
151 #  define NULL_DEVICE "/dev/null"
152 # endif
153 
154 /* Stubs for inlining */
155 
156 # if defined(_MSC_VER)
157 #  define SIM_INLINE _inline
158 # elif defined(__GNUC__) || defined(__clang_version__) || defined(__xlc__)
159 #  define SIM_INLINE inline
160 # else
161 #  define SIM_INLINE
162 # endif
163 
164 /* System independent definitions */
165 
166 # define FLIP_SIZE       (1 << 16)                       /* flip buf size */
167 # if !defined (PATH_MAX)                                 /* usually in limits */
168 #  define PATH_MAX        512
169 # endif
170 # if (PATH_MAX >= 128)
171 #  define CBUFSIZE        (4096 + PATH_MAX)                /* string buf size */
172 # else
173 #  define CBUFSIZE        4224
174 # endif
175 
176 /* Breakpoint spaces definitions */
177 
178 # define SIM_BKPT_N_SPC  (1 << (32 - SIM_BKPT_V_SPC))    /* max number spaces */
179 # define SIM_BKPT_V_SPC  (BRK_TYP_MAX + 1)               /* location in arg */
180 
181 /* Extended switch definitions (bits >= 26) */
182 
183 # define SIM_SW_HIDE     (1u << 26)                      /* enable hiding */
184 # define SIM_SW_REST     (1u << 27)                      /* attach/restore */
185 # define SIM_SW_REG      (1u << 28)                      /* register value */
186 # define SIM_SW_STOP     (1u << 29)                      /* stop message */
187 # define SIM_SW_SHUT     (1u << 30)                      /* shutdown */
188 
189 /* Simulator status codes
190 
191    0                    ok
192    1 - (SCPE_BASE - 1)  simulator specific
193    SCPE_BASE - n        general
194 */
195 
196 # define SCPE_OK         0                               /* normal return */
197 # define SCPE_BASE       64                              /* base for messages */
198 # define SCPE_NXM        (SCPE_BASE + 0)                 /* nxm */
199 # define SCPE_UNATT      (SCPE_BASE + 1)                 /* no file */
200 # define SCPE_IOERR      (SCPE_BASE + 2)                 /* I/O error */
201 # define SCPE_CSUM       (SCPE_BASE + 3)                 /* loader cksum */
202 # define SCPE_FMT        (SCPE_BASE + 4)                 /* loader format */
203 # define SCPE_NOATT      (SCPE_BASE + 5)                 /* not attachable */
204 # define SCPE_OPENERR    (SCPE_BASE + 6)                 /* open error */
205 # define SCPE_MEM        (SCPE_BASE + 7)                 /* alloc error */
206 # define SCPE_ARG        (SCPE_BASE + 8)                 /* argument error */
207 # define SCPE_STEP       (SCPE_BASE + 9)                 /* step expired */
208 # define SCPE_UNK        (SCPE_BASE + 10)                /* unknown command */
209 # define SCPE_RO         (SCPE_BASE + 11)                /* read only */
210 # define SCPE_INCOMP     (SCPE_BASE + 12)                /* incomplete */
211 # define SCPE_STOP       (SCPE_BASE + 13)                /* sim stopped */
212 # define SCPE_EXIT       (SCPE_BASE + 14)                /* sim exit */
213 # define SCPE_TTIERR     (SCPE_BASE + 15)                /* console tti err */
214 # define SCPE_TTOERR     (SCPE_BASE + 16)                /* console tto err */
215 # define SCPE_EOF        (SCPE_BASE + 17)                /* end of file */
216 # define SCPE_REL        (SCPE_BASE + 18)                /* relocation error */
217 # define SCPE_NOPARAM    (SCPE_BASE + 19)                /* no parameters */
218 # define SCPE_ALATT      (SCPE_BASE + 20)                /* already attached */
219 # define SCPE_TIMER      (SCPE_BASE + 21)                /* hwre timer err */
220 # define SCPE_SIGERR     (SCPE_BASE + 22)                /* signal err */
221 # define SCPE_TTYERR     (SCPE_BASE + 23)                /* tty setup err */
222 # define SCPE_SUB        (SCPE_BASE + 24)                /* subscript err */
223 # define SCPE_NOFNC      (SCPE_BASE + 25)                /* func not imp */
224 # define SCPE_UDIS       (SCPE_BASE + 26)                /* unit disabled */
225 # define SCPE_NORO       (SCPE_BASE + 27)                /* rd only not ok */
226 # define SCPE_INVSW      (SCPE_BASE + 28)                /* invalid switch */
227 # define SCPE_MISVAL     (SCPE_BASE + 29)                /* missing value */
228 # define SCPE_2FARG      (SCPE_BASE + 30)                /* too few arguments */
229 # define SCPE_2MARG      (SCPE_BASE + 31)                /* too many arguments */
230 # define SCPE_NXDEV      (SCPE_BASE + 32)                /* nx device */
231 # define SCPE_NXUN       (SCPE_BASE + 33)                /* nx unit */
232 # define SCPE_NXREG      (SCPE_BASE + 34)                /* nx register */
233 # define SCPE_NXPAR      (SCPE_BASE + 35)                /* nx parameter */
234 # define SCPE_NEST       (SCPE_BASE + 36)                /* nested DO */
235 # define SCPE_IERR       (SCPE_BASE + 37)                /* internal error */
236 # define SCPE_MTRLNT     (SCPE_BASE + 38)                /* tape rec lnt error */
237 # define SCPE_LOST       (SCPE_BASE + 39)                /* Telnet conn lost */
238 # define SCPE_TTMO       (SCPE_BASE + 40)                /* Telnet conn timeout */
239 # define SCPE_STALL      (SCPE_BASE + 41)                /* Telnet conn stall */
240 # define SCPE_AFAIL      (SCPE_BASE + 42)                /* assert failed */
241 # define SCPE_INVREM     (SCPE_BASE + 43)                /* invalid remote console command */
242 # define SCPE_NOTATT     (SCPE_BASE + 44)                /* not attached */
243 # define SCPE_EXPECT     (SCPE_BASE + 45)                /* expect matched */
244 # define SCPE_REMOTE     (SCPE_BASE + 46)                /* remote console command */
245 
246 # define SCPE_MAX_ERR    (SCPE_BASE + 47)                /* Maximum SCPE Error Value */
247 # define SCPE_KFLAG      0x1000                          /* tti data flag */
248 # define SCPE_BREAK      0x2000                          /* tti break flag */
249 # define SCPE_NOMESSAGE  0x10000000                      /* message display supression flag */
250 # define SCPE_BARE_STATUS(stat) ((stat) & ~(SCPE_NOMESSAGE|SCPE_KFLAG|SCPE_BREAK))
251 
252 /* Print value format codes */
253 
254 # define PV_RZRO         0                               /* right, zero fill */
255 # define PV_RSPC         1                               /* right, space fill */
256 # define PV_RCOMMA       2                               /* right, space fill. Comma separate every 3 */
257 # define PV_LEFT         3                               /* left justify */
258 
259 /* Default timing parameters */
260 
261 # define KBD_POLL_WAIT   5000                            /* keyboard poll */
262 # define KBD_MAX_WAIT    500000
263 # define SERIAL_IN_WAIT  100                             /* serial in time */
264 # define SERIAL_OUT_WAIT 100                             /* serial output */
265 # define NOQUEUE_WAIT    1000000                         /* min check time */
266 # define KBD_LIM_WAIT(x) (((x) > KBD_MAX_WAIT)? KBD_MAX_WAIT: (x))
267 # define KBD_WAIT(w,s)   ((w)? w: KBD_LIM_WAIT (s))
268 
269 /* Convert switch letter to bit mask */
270 
271 # define SWMASK(x) (1u << (((int) (x)) - ((int) 'A')))
272 
273 /* String match - at least one character required */
274 
275 # define MATCH_CMD(ptr,cmd) ((NULL == (ptr)) || (!*(ptr)) || strncmp ((ptr), (cmd), strlen (ptr)))
276 
277 /* End of Linked List/Queue value                           */
278 /* Chosen for 2 reasons:                                    */
279 /*     1 - to not be NULL, this allowing the NULL value to  */
280 /*         indicate inclusion on a list                     */
281 /* and                                                      */
282 /*     2 - to not be a valid/possible pointer (alignment)   */
283 # define QUEUE_LIST_END ((UNIT *)1)
284 
285 /* Typedefs for principal structures */
286 
287 typedef struct DEVICE DEVICE;
288 typedef struct UNIT UNIT;
289 typedef struct REG REG;
290 typedef struct CTAB CTAB;
291 typedef struct C1TAB C1TAB;
292 typedef struct SHTAB SHTAB;
293 typedef struct MTAB MTAB;
294 typedef struct SCHTAB SCHTAB;
295 typedef struct BRKTAB BRKTAB;
296 typedef struct BRKTYPTAB BRKTYPTAB;
297 typedef struct EXPTAB EXPTAB;
298 typedef struct EXPECT EXPECT;
299 typedef struct SEND SEND;
300 typedef struct DEBTAB DEBTAB;
301 typedef struct FILEREF FILEREF;
302 typedef struct BITFIELD BITFIELD;
303 
304 typedef t_stat (*ACTIVATE_API)(UNIT *unit, int32 interval);
305 
306 /* Device data structure */
307 
308 struct DEVICE {
309     const char          *name;                          /* name */
310     UNIT                *units;                         /* units */
311     REG                 *registers;                     /* registers */
312     MTAB                *modifiers;                     /* modifiers */
313     uint32              numunits;                       /* #units */
314     uint32              aradix;                         /* address radix */
315     uint32              awidth;                         /* address width */
316     uint32              aincr;                          /* addr increment */
317     uint32              dradix;                         /* data radix */
318     uint32              dwidth;                         /* data width */
319     t_stat              (*examine)(t_value *v, t_addr a, UNIT *up,
320                             int32 sw);                  /* examine routine */
321     t_stat              (*deposit)(t_value v, t_addr a, UNIT *up,
322                             int32 sw);                  /* deposit routine */
323     t_stat              (*reset)(DEVICE *dp);           /* reset routine */
324     t_stat              (*boot)(int32 u, DEVICE *dp);
325                                                         /* boot routine */
326     t_stat              (*attach)(UNIT *up, CONST char *cp);
327                                                         /* attach routine */
328     t_stat              (*detach)(UNIT *up);            /* detach routine */
329     void                *ctxt;                          /* context */
330     uint32              flags;                          /* flags */
331     uint32              dctrl;                          /* debug control */
332     DEBTAB              *debflags;                      /* debug flags */
333     t_stat              (*msize)(UNIT *up, int32 v, CONST char *cp, void *dp);
334                                                         /* mem size routine */
335     char                *lname;                         /* logical name */
336     t_stat              (*help)(FILE *st, DEVICE *dptr,
337                             UNIT *uptr, int32 flag, const char *cptr);
338                                                         /* help */
339     t_stat              (*attach_help)(FILE *st, DEVICE *dptr,
340                             UNIT *uptr, int32 flag, const char *cptr);
341                                                         /* attach help */
342     void *help_ctx;                                     /* Context available to help routines */
343     const char          *(*description)(DEVICE *dptr);  /* Device Description */
344     BRKTYPTAB           *brk_types;                     /* Breakpoint types */
345     };
346 
347 /* Device flags */
348 
349 # define DEV_V_DIS       0                               /* dev disabled */
350 # define DEV_V_DISABLE   1                               /* dev disable-able */
351 # define DEV_V_DYNM      2                               /* mem size dynamic */
352 # define DEV_V_DEBUG     3                               /* debug capability */
353 # define DEV_V_TYPE      4                               /* Attach type */
354 # define DEV_S_TYPE      3                               /* Width of Type Field */
355 # define DEV_V_SECTORS   7                               /* Unit Capacity is in 512byte sectors */
356 # define DEV_V_DONTAUTO  8                               /* Do not auto detach already attached units */
357 # define DEV_V_FLATHELP  9                               /* Use traditional (unstructured) help */
358 # define DEV_V_NOSAVE    10                              /* Don't save device state */
359 # define DEV_V_UF_31     12                              /* user flags, V3.1 */
360 # define DEV_V_UF        16                              /* user flags */
361 # define DEV_V_RSV       31                              /* reserved */
362 
363 # define DEV_DIS         (1 << DEV_V_DIS)                /* device is currently disabled */
364 # define DEV_DISABLE     (1 << DEV_V_DISABLE)            /* device can be set enabled or disabled */
365 # define DEV_DYNM        (1 << DEV_V_DYNM)               /* device requires call on msize routine to change memory size */
366 # define DEV_DEBUG       (1 << DEV_V_DEBUG)              /* device supports SET DEBUG command */
367 # define DEV_SECTORS     (1 << DEV_V_SECTORS)            /* capacity is 512 byte sectors */
368 # define DEV_DONTAUTO    (1 << DEV_V_DONTAUTO)           /* Do not auto detach already attached units */
369 # define DEV_FLATHELP    (1 << DEV_V_FLATHELP)           /* Use traditional (unstructured) help */
370 # define DEV_NOSAVE      (1 << DEV_V_NOSAVE)             /* Don't save device state */
371 # define DEV_NET         0                               /* Deprecated - meaningless */
372 
373 
374 # define DEV_TYPEMASK    (((1 << DEV_S_TYPE) - 1) << DEV_V_TYPE)
375 # define DEV_DISK        (1 << DEV_V_TYPE)               /* sim_disk Attach */
376 # define DEV_TAPE        (2 << DEV_V_TYPE)               /* sim_tape Attach */
377 # define DEV_MUX         (3 << DEV_V_TYPE)               /* sim_tmxr Attach */
378 # define DEV_ETHER       (4 << DEV_V_TYPE)               /* Ethernet Device */
379 # define DEV_DISPLAY     (5 << DEV_V_TYPE)               /* Display Device */
380 # define DEV_TYPE(dptr)  ((dptr)->flags & DEV_TYPEMASK)
381 
382 # define DEV_UFMASK_31   (((1u << DEV_V_RSV) - 1) & ~((1u << DEV_V_UF_31) - 1))
383 # define DEV_UFMASK      (((1u << DEV_V_RSV) - 1) & ~((1u << DEV_V_UF) - 1))
384 # define DEV_RFLAGS      (DEV_UFMASK|DEV_DIS)            /* restored flags */
385 
386 /* Unit data structure
387 
388    Parts of the unit structure are device specific, that is, they are
389    not referenced by the simulator control package and can be freely
390    used by device simulators.  Fields starting with 'buf', and flags
391    starting with 'UF', are device specific.  The definitions given here
392    are for a typical sequential device.
393 */
394 
395 struct UNIT {
396     UNIT                *next;                          /* next active */
397     t_stat              (*action)(UNIT *up);            /* action routine */
398     char                *filename;                      /* open file name */
399     FILE                *fileref;                       /* file reference */
400     void                *filebuf;                       /* memory buffer */
401     uint32              hwmark;                         /* high water mark */
402     int32               time;                           /* time out */
403     uint32              flags;                          /* flags */
404     uint32              dynflags;                       /* dynamic flags */
405     t_addr              capac;                          /* capacity */
406     t_addr              pos;                            /* file position */
407     void                (*io_flush)(UNIT *up);          /* io flush routine */
408     uint32              iostarttime;                    /* I/O start time */
409     int32               buf;                            /* buffer */
410     int32               wait;                           /* wait */
411     int32               u3;                             /* device specific */
412     int32               u4;                             /* device specific */
413     int32               u5;                             /* device specific */
414     int32               u6;                             /* device specific */
415     void                *up7;                           /* device specific */
416     void                *up8;                           /* device specific */
417     void                *tmxr;                          /* TMXR linkage */
418     void                (*cancel)(UNIT *);
419     };
420 
421 /* Unit flags */
422 
423 # define UNIT_V_UF_31    12              /* dev spec, V3.1 */
424 # define UNIT_V_UF       16              /* device specific */
425 # define UNIT_V_RSV      31              /* reserved!! */
426 
427 # define UNIT_ATTABLE    0000001         /* attachable */
428 # define UNIT_RO         0000002         /* read only */
429 # define UNIT_FIX        0000004         /* fixed capacity */
430 # define UNIT_SEQ        0000010         /* sequential */
431 # define UNIT_ATT        0000020         /* attached */
432 # define UNIT_BINK       0000040         /* K = power of 2 */
433 # define UNIT_BUFABLE    0000100         /* bufferable */
434 # define UNIT_MUSTBUF    0000200         /* must buffer */
435 # define UNIT_BUF        0000400         /* buffered */
436 # define UNIT_ROABLE     0001000         /* read only ok */
437 # define UNIT_DISABLE    0002000         /* disable-able */
438 # define UNIT_DIS        0004000         /* disabled */
439 # define UNIT_IDLE       0040000         /* idle eligible */
440 
441 /* Unused/meaningless flags */
442 # define UNIT_TEXT       0000000         /* text mode - no effect */
443 
444 # define UNIT_UFMASK_31  (((1u << UNIT_V_RSV) - 1) & ~((1u << UNIT_V_UF_31) - 1))
445 # define UNIT_UFMASK     (((1u << UNIT_V_RSV) - 1) & ~((1u << UNIT_V_UF) - 1))
446 # define UNIT_RFLAGS     (UNIT_UFMASK|UNIT_DIS)          /* restored flags */
447 
448 /* Unit dynamic flags (dynflags) */
449 
450 /* These flags are only set dynamically */
451 
452 # define UNIT_ATTMULT    0000001         /* Allow multiple attach commands */
453 # define UNIT_TM_POLL    0000002         /* TMXR Polling unit */
454 # define UNIT_NO_FIO     0000004         /* fileref is NOT a FILE * */
455 # define UNIT_DISK_CHK   0000010         /* disk data debug checking (sim_disk) */
456 # define UNIT_TMR_UNIT   0000020         /* Unit registered as a calibrated timer */
457 # define UNIT_V_DF_TAPE  5               /* Bit offset for Tape Density reservation */
458 # define UNIT_S_DF_TAPE  3               /* Bits Reserved for Tape Density */
459 
460 struct BITFIELD {
461     const char      *name;                              /* field name */
462     uint32          offset;                             /* starting bit */
463     uint32          width;                              /* width */
464     const char      **valuenames;                       /* map of values to strings */
465     const char      *format;                            /* value format string */
466     };
467 
468 /* Register data structure */
469 
470 struct REG {
471     CONST char          *name;                          /* name */
472     void                *loc;                           /* location */
473     uint32              radix;                          /* radix */
474     uint32              width;                          /* width */
475     uint32              offset;                         /* starting bit */
476     uint32              depth;                          /* save depth */
477     const char          *desc;                          /* description */
478     BITFIELD            *fields;                        /* bit fields */
479     uint32              flags;                          /* flags */
480     uint32              qptr;                           /* circ q ptr */
481     size_t              str_size;                       /* structure size */
482     };
483 
484 /* Register flags */
485 
486 # define REG_FMT         00003                           /* see PV_x */
487 # define REG_RO          00004                           /* read only */
488 # define REG_HIDDEN      00010                           /* hidden */
489 # define REG_NZ          00020                           /* must be non-zero */
490 # define REG_UNIT        00040                           /* in unit struct */
491 # define REG_STRUCT      00100                           /* in structure array */
492 # define REG_CIRC        00200                           /* circular array */
493 # define REG_VMIO        00400                           /* use VM data print/parse */
494 # define REG_VMAD        01000                           /* use VM addr print/parse */
495 # define REG_FIT         02000                           /* fit access to size */
496 # define REG_HRO         (REG_RO | REG_HIDDEN)           /* hidden, read only */
497 
498 # define REG_V_UF        16                              /* device specific */
499 # define REG_UFMASK      (~((1u << REG_V_UF) - 1))       /* user flags mask */
500 # define REG_VMFLAGS     (REG_VMIO | REG_UFMASK)         /* call VM routine if any of these are set */
501 
502 /* Command tables, base and alternate formats */
503 
504 struct CTAB {
505     const char          *name;                          /* name */
506     t_stat              (*action)(int32 flag, CONST char *cptr);
507                                                         /* action routine */
508     int32               arg;                            /* argument */
509     const char          *help;                          /* help string/structured locator */
510     const char          *help_base;                     /* structured help base*/
511     void                (*message)(const char *unechoed_cmdline, t_stat stat);
512                                                         /* message printing routine */
513     };
514 
515 struct C1TAB {
516     const char          *name;                          /* name */
517     t_stat              (*action)(DEVICE *dptr, UNIT *uptr,
518                             int32 flag, CONST char *cptr);/* action routine */
519     int32               arg;                            /* argument */
520     const char          *help;                          /* help string */
521     };
522 
523 struct SHTAB {
524     const char          *name;                          /* name */
525     t_stat              (*action)(FILE *st, DEVICE *dptr,
526                             UNIT *uptr, int32 flag, CONST char *cptr);
527     int32               arg;                            /* argument */
528     const char          *help;                          /* help string */
529     };
530 
531 /* Modifier table - only extended entries have disp, reg, or flags */
532 
533 struct MTAB {
534     uint32              mask;                           /* mask */
535     uint32              match;                          /* match */
536     const char          *pstring;                       /* print string */
537     const char          *mstring;                       /* match string */
538     t_stat              (*valid)(UNIT *up, int32 v, CONST char *cp, void *dp);
539                                                         /* validation routine */
540     t_stat              (*disp)(FILE *st, UNIT *up, int32 v, CONST void *dp);
541                                                         /* display routine */
542     void                *desc;                          /* value descriptor */
543                                                         /* REG * if MTAB_VAL */
544                                                         /* int * if not */
545     const char          *help;                          /* help string */
546     };
547 
548 
549 /* mtab mask flag bits */
550 /* NOTE: MTAB_VALR and MTAB_VALO are only used to display help */
551 # define MTAB_XTD        (1u << UNIT_V_RSV)              /* ext entry flag */
552 # define MTAB_VDV        (0001 | MTAB_XTD)               /* valid for dev */
553 # define MTAB_VUN        (0002 | MTAB_XTD)               /* valid for unit */
554 # define MTAB_VALR       (0004 | MTAB_XTD)               /* takes a value (required) */
555 # define MTAB_VALO       (0010 | MTAB_XTD)               /* takes a value (optional) */
556 # define MTAB_NMO        (0020 | MTAB_XTD)               /* only if named */
557 # define MTAB_NC         (0040 | MTAB_XTD)               /* no UC conversion */
558 # define MTAB_QUOTE      (0100 | MTAB_XTD)               /* quoted string */
559 # define MTAB_SHP        (0200 | MTAB_XTD)               /* show takes parameter */
560 # define MODMASK(mptr,flag) (((mptr)->mask & (uint32)(flag)) == (uint32)(flag))/* flag mask test */
561 
562 /* Search table */
563 
564 struct SCHTAB {
565     int32               logic;                          /* logical operator */
566     int32               boolop;                         /* boolean operator */
567     uint32              count;                          /* value count in mask and comp arrays */
568     t_value             *mask;                          /* mask for logical */
569     t_value             *comp;                          /* comparison for boolean */
570     };
571 
572 /* Breakpoint table */
573 
574 struct BRKTAB {
575     t_addr              addr;                           /* address */
576     uint32              typ;                            /* mask of types */
577 # define BRK_TYP_USR_TYPES       ((1 << ('Z'-'A'+1)) - 1)/* all types A-Z */
578 # define BRK_TYP_DYN_STEPOVER    (SWMASK ('Z'+1))
579 # define BRK_TYP_DYN_USR         (SWMASK ('Z'+2))
580 # define BRK_TYP_DYN_ALL         (BRK_TYP_DYN_USR|BRK_TYP_DYN_STEPOVER) /* Mask of All Dynamic types */
581 # define BRK_TYP_TEMP            (SWMASK ('Z'+3))        /* Temporary (one-shot) */
582 # define BRK_TYP_MAX             (('Z'-'A')+3)           /* Maximum breakpoint type */
583     int32               cnt;                            /* proceed count */
584     char                *act;                           /* action string */
585     double              time_fired[SIM_BKPT_N_SPC];     /* instruction count when match occurred */
586     BRKTAB *next;                                       /* list with same address value */
587     };
588 
589 /* Breakpoint table */
590 
591 struct BRKTYPTAB {
592     uint32      btyp;                                   /* type mask */
593     const char *desc;                                   /* description */
594     };
595 # define BRKTYPE(typ,descrip) {SWMASK(typ), descrip}
596 
597 /* Expect rule */
598 
599 struct EXPTAB {
600     uint8               *match;                         /* match string */
601     uint32              size;                           /* match string size */
602     char                *match_pattern;                 /* match pattern for format */
603     int32               cnt;                            /* proceed count */
604     int32               switches;                       /* flags */
605 # define EXP_TYP_PERSIST         (SWMASK ('P'))      /* rule persists after match, default is once a rule matches, it is removed */
606 # define EXP_TYP_CLEARALL        (SWMASK ('C'))      /* clear all rules after matching this rule, default is to once a rule matches, it is removed */
607 # define EXP_TYP_REGEX           (SWMASK ('R'))      /* rule pattern is a regular expression */
608 # define EXP_TYP_REGEX_I         (SWMASK ('I'))      /* regular expression pattern matching should be case independent */
609 # define EXP_TYP_TIME            (SWMASK ('T'))      /* halt delay is in microseconds instead of instructions */
610     char                *act;                           /* action string */
611     };
612 
613 /* Expect Context */
614 
615 struct EXPECT {
616     DEVICE              *dptr;                          /* Device (for Debug) */
617     uint32              dbit;                           /* Debugging Bit */
618     EXPTAB              *rules;                         /* match rules */
619     int32               size;                           /* count of match rules */
620     uint32              after;                          /* delay before halting */
621     uint8               *buf;                           /* buffer of output data which has produced */
622     uint32              buf_ins;                        /* buffer insertion point for the next output data */
623     uint32              buf_size;                       /* buffer size */
624     };
625 
626 /* Send Context */
627 
628 struct SEND {
629     uint32              delay;                          /* instruction delay between sent data */
630 # define SEND_DEFAULT_DELAY  1000                        /* default delay instruction count */
631     DEVICE              *dptr;                          /* Device (for Debug) */
632     uint32              dbit;                           /* Debugging Bit */
633     uint32              after;                          /* instruction delay before sending any data */
634     double              next_time;                      /* execution time when next data can be sent */
635     uint8               *buffer;                        /* buffer */
636     size_t              bufsize;                        /* buffer size */
637     int32               insoff;                         /* insert offset */
638     int32               extoff;                         /* extra offset */
639     };
640 
641 /* Debug table */
642 
643 struct DEBTAB {
644     const char          *name;                          /* control name */
645     uint32              mask;                           /* control bit */
646     const char          *desc;                          /* description */
647     };
648 
649 /* Deprecated Debug macros.  Use sim_debug() */
650 
651 # define DEBUG_PRS(d)    (sim_deb && d.dctrl)
652 # define DEBUG_PRD(d)    (sim_deb && d->dctrl)
653 # define DEBUG_PRI(d,m)  (sim_deb && (d.dctrl & (m)))
654 # define DEBUG_PRJ(d,m)  (sim_deb && ((d)->dctrl & (m)))
655 
656 # define SIM_DBG_EVENT       0x10000
657 # define SIM_DBG_ACTIVATE    0x20000
658 # define SIM_DBG_AIO_QUEUE   0x40000
659 
660 /* File Reference */
661 struct FILEREF {
662     char                name[CBUFSIZE];                 /* file name */
663     FILE                *file;                          /* file handle */
664     int32               refcount;                       /* reference count */
665     };
666 
667 /*
668    The following macros exist to help populate structure contents
669 
670    They are dependent on the declaration order of the fields
671    of the structures they exist to populate.
672 
673  */
674 
675 # define UDATA(act,fl,cap) NULL,act,NULL,NULL,NULL,0,0,(fl),0,(cap),0,NULL,0,0
676 
677 # if defined (__STDC__) || defined (_WIN32) /* Variants which depend on how macro arguments are convered to strings */
678 /* Generic Register declaration for all fields.
679    If the register structure is extended, this macro will be retained and a
680    new macro will be provided that populates the new register structure */
681 #  define REGDATA(nm,loc,rdx,wd,off,dep,desc,flds,fl,qptr,siz) \
682     #nm, &(loc), (rdx), (wd), (off), (dep), (desc), (flds), (fl), (qptr), (siz)
683 /* Right Justified Octal Register Data */
684 #  define ORDATA(nm,loc,wd) #nm, &(loc), 8, (wd), 0, 1, NULL, NULL
685 /* Right Justified Decimal Register Data */
686 #  define DRDATA(nm,loc,wd) #nm, &(loc), 10, (wd), 0, 1, NULL, NULL
687 /* Right Justified Hexadecimal Register Data */
688 #  define HRDATA(nm,loc,wd) #nm, &(loc), 16, (wd), 0, 1, NULL, NULL
689 /* Right Justified Binary Register Data */
690 #  define BINRDATA(nm,loc,wd) #nm, &(loc), 2, (wd), 0, 1, NULL, NULL
691 /* One-bit binary flag at an arbitrary offset in a 32-bit word Register */
692 #  define FLDATA(nm,loc,pos) #nm, &(loc), 2, 1, (pos), 1, NULL, NULL
693 /* Arbitrary location and Radix Register */
694 #  define GRDATA(nm,loc,rdx,wd,pos) #nm, &(loc), (rdx), (wd), (pos), 1, NULL, NULL
695 /* Arrayed register whose data is kept in a standard C array Register */
696 #  define BRDATA(nm,loc,rdx,wd,dep) #nm, (loc), (rdx), (wd), 0, (dep), NULL, NULL
697 /* Same as above, but with additional description initializer */
698 #  define ORDATAD(nm,loc,wd,desc) #nm, &(loc), 8, (wd), 0, 1, (desc), NULL
699 #  define DRDATAD(nm,loc,wd,desc) #nm, &(loc), 10, (wd), 0, 1, (desc), NULL
700 #  define HRDATAD(nm,loc,wd,desc) #nm, &(loc), 16, (wd), 0, 1, (desc), NULL
701 #  define BINRDATAD(nm,loc,wd,desc) #nm, &(loc), 2, (wd), 0, 1, (desc), NULL
702 #  define FLDATAD(nm,loc,pos,desc) #nm, &(loc), 2, 1, (pos), 1, (desc), NULL
703 #  define GRDATAD(nm,loc,rdx,wd,pos,desc) #nm, &(loc), (rdx), (wd), (pos), 1, (desc), NULL
704 #  define BRDATAD(nm,loc,rdx,wd,dep,desc) #nm, (loc), (rdx), (wd), 0, (dep), (desc), NULL
705 /* Same as above, but with additional description initializer, and bitfields */
706 #  define ORDATADF(nm,loc,wd,desc,flds) #nm, &(loc), 8, (wd), 0, 1, (desc), (flds)
707 #  define DRDATADF(nm,loc,wd,desc,flds) #nm, &(loc), 10, (wd), 0, 1, (desc), (flds)
708 #  define HRDATADF(nm,loc,wd,desc,flds) #nm, &(loc), 16, (wd), 0, 1, (desc), (flds)
709 #  define BINRDATADF(nm,loc,wd) #nm, &(loc), 2, (wd), 0, 1, NULL, NULL
710 #  define FLDATADF(nm,loc,pos,desc,flds) #nm, &(loc), 2, 1, (pos), 1, (desc), (flds)
711 #  define GRDATADF(nm,loc,rdx,wd,pos,desc,flds) #nm, &(loc), (rdx), (wd), (pos), 1, (desc), (flds)
712 #  define BRDATADF(nm,loc,rdx,wd,dep,desc,flds) #nm, (loc), (rdx), (wd), 0, (dep), (desc), (flds)
713 #  define BIT(nm)              {#nm, 0xffffffff, 1}             /* Single Bit definition */
714 #  define BITNC                {"",  0xffffffff, 1}             /* Don't care Bit definition */
715 #  define BITF(nm,sz)          {#nm, 0xffffffff, sz}            /* Bit Field definition */
716 #  define BITNCF(sz)           {"",  0xffffffff, sz}            /* Don't care Bit Field definition */
717 #  define BITFFMT(nm,sz,fmt)   {#nm, 0xffffffff, sz, NULL, #fmt}/* Bit Field definition with Output format */
718 #  define BITFNAM(nm,sz,names) {#nm, 0xffffffff, sz, names}     /* Bit Field definition with value->name map */
719 # else /* For non-STD-C compiler which can't stringify macro arguments with # */
720 #  define REGDATA(nm,loc,rdx,wd,off,dep,desc,flds,fl,qptr,siz) \
721     "nm", &(loc), (rdx), (wd), (off), (dep), (desc), (flds), (fl), (qptr), (siz)
722 #  define ORDATA(nm,loc,wd) "nm", &(loc), 8, (wd), 0, 1, NULL, NULL
723 #  define DRDATA(nm,loc,wd) "nm", &(loc), 10, (wd), 0, 1, NULL, NULL
724 #  define HRDATA(nm,loc,wd) "nm", &(loc), 16, (wd), 0, 1, NULL, NULL
725 #  define BINRDATA(nm,loc,wd) "nm", &(loc), 2, (wd), 0, 1, NULL, NULL
726 #  define FLDATA(nm,loc,pos) "nm", &(loc), 2, 1, (pos), 1, NULL, NULL
727 #  define GRDATA(nm,loc,rdx,wd,pos) "nm", &(loc), (rdx), (wd), (pos), 1, NULL, NULL
728 #  define BRDATA(nm,loc,rdx,wd,dep) "nm", (loc), (rdx), (wd), 0, (dep), NULL, NULL
729 #  define ORDATAD(nm,loc,wd,desc) "nm", &(loc), 8, (wd), 0, 1, (desc), NULL
730 #  define DRDATAD(nm,loc,wd,desc) "nm", &(loc), 10, (wd), 0, 1, (desc), NULL
731 #  define HRDATAD(nm,loc,wd,desc) "nm", &(loc), 16, (wd), 0, 1, (desc), NULL
732 #  define BINRDATAD(nm,loc,wd,desc) "nm", &(loc), 2, (wd), 0, 1, (desc), NULL
733 #  define FLDATAD(nm,loc,pos,desc) "nm", &(loc), 2, 1, (pos), 1, (desc), NULL
734 #  define GRDATAD(nm,loc,rdx,wd,pos,desc) "nm", &(loc), (rdx), (wd), (pos), 1, (desc), NULL
735 #  define BRDATAD(nm,loc,rdx,wd,dep,desc) "nm", (loc), (rdx), (wd), 0, (dep), (desc), NULL
736 #  define ORDATADF(nm,loc,wd,desc,flds) "nm", &(loc), 8, (wd), 0, 1, (desc), (flds)
737 #  define DRDATADF(nm,loc,wd,desc,flds) "nm", &(loc), 10, (wd), 0, 1, (desc), (flds)
738 #  define HRDATADF(nm,loc,wd,desc,flds) "nm", &(loc), 16, (wd), 0, 1, (desc), (flds)
739 #  define BINRDATADF(nm,loc,wd,desc,flds) "nm", &(loc), 2, (wd), 0, 1, (desc), (flds)
740 #  define FLDATADF(nm,loc,pos,desc,flds) "nm", &(loc), 2, 1, (pos), 1, (desc), (flds)
741 #  define GRDATADF(nm,loc,rdx,wd,pos,desc,flds) "nm", &(loc), (rdx), (wd), (pos), 1, (desc), (flds)
742 #  define BRDATADF(nm,loc,rdx,wd,dep,desc,flds) "nm", (loc), (rdx), (wd), 0, (dep), (desc), (flds)
743 #  define BIT(nm)              {"nm", 0xffffffff, 1}              /* Single Bit definition */
744 #  define BITNC                {"",   0xffffffff, 1}              /* Don't care Bit definition */
745 #  define BITF(nm,sz)          {"nm", 0xffffffff, sz}             /* Bit Field definition */
746 #  define BITNCF(sz)           {"",   0xffffffff, sz}             /* Don't care Bit Field definition */
747 #  define BITFFMT(nm,sz,fmt)   {"nm", 0xffffffff, sz, NULL, "fmt"}/* Bit Field definition with Output format */
748 #  define BITFNAM(nm,sz,names) {"nm", 0xffffffff, sz, names}      /* Bit Field definition with value->name map */
749 # endif
750 # define ENDBITS {NULL}  /* end of bitfield list */
751 
752 /* Arrayed register whose data is part of the UNIT structure */
753 # define URDATA(nm,loc,rdx,wd,off,dep,fl) \
754     REGDATA(nm,(loc),(rdx),(wd),(off),(dep),NULL,NULL,((fl) | REG_UNIT),0,0)
755 /* Arrayed register whose data is part of an arbitrary structure */
756 # define STRDATA(nm,loc,rdx,wd,off,dep,siz,fl) \
757     REGDATA(nm,(loc),(rdx),(wd),(off),(dep),NULL,NULL,((fl) | REG_STRUCT),0,(siz))
758 /* Same as above, but with additional description initializer */
759 # define URDATAD(nm,loc,rdx,wd,off,dep,fl,desc) \
760     REGDATA(nm,(loc),(rdx),(wd),(off),(dep),(desc),NULL,((fl) | REG_UNIT),0,0)
761 # define STRDATAD(nm,loc,rdx,wd,off,dep,siz,fl,desc) \
762     REGDATA(nm,(loc),(rdx),(wd),(off),(dep),(desc),NULL,((fl) | REG_STRUCT),0,(siz))
763 /* Same as above, but with additional description initializer, and bitfields */
764 # define URDATADF(nm,loc,rdx,wd,off,dep,fl,desc,flds) \
765     REGDATA(nm,(loc),(rdx),(wd),(off),(dep),(desc),(flds),((fl) | REG_UNIT),0,0)
766 # define STRDATADF(nm,loc,rdx,wd,off,dep,siz,fl,desc,flds) \
767     REGDATA(nm,(loc),(rdx),(wd),(off),(dep),(desc),(flds),((fl) | REG_STRUCT),0,(siz))
768 
769 /* Function prototypes */
770 
771 # include "scp.h"
772 # include "sim_console.h"
773 # include "sim_timer.h"
774 # include "sim_fio.h"
775 
776 /* Macro to ALWAYS execute the specified expression and fail if it evaluates to false. */
777 /* This replaces any references to "assert()" which should never be invoked */
778 /* with an expression which causes side effects (i.e. must be executed for */
779 /* the program to work correctly) */
780 # define ASSURE(_Expression) while (!(_Expression)) {fprintf(stderr, "%s failed at %s line %d\n", #_Expression, __FILE__, __LINE__);  \
781                                                     sim_printf("%s failed at %s line %d\n", #_Expression, __FILE__, __LINE__);       \
782                                                     abort();}
783 
784 #endif
785