1 /* pMARS -- a portable Memory Array Redcode Simulator
2  * Copyright (C) 1993-1996 Albert Ma, Na'ndor Sieben, Stefan Strack and Mintardjo Wangsawidjaja
3  * Copyright (C) 2000 Ilmari Karonen
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19 
20 /*
21  * global.h: global header
22  * $Id: global.h,v 1.5 2000/12/25 00:56:41 iltzu Exp $
23  *
24  * 10-23-98 Pentium optimized version 30% faster than the original
25  *          Ken Espiritu   (kespirit@cesun1.ce.vt.edu)
26  */
27 
28 #include "config.h"
29 #include <stdio.h>
30 #ifndef INT_MAX
31 #include <limits.h>
32 #endif
33 #if defined(__STDC__) || defined(__MSDOS__)
34 #include <stdlib.h>
35 #endif
36 
37 /* *********************************************************************
38    System dependent definitions or declarations
39    ********************************************************************* */
40 
41 /* Generic Pointer type */
42 #if defined(NEW_STYLE)
43 typedef void *pointer_t;
44 #else
45 typedef char *pointer_t;
46 #endif
47 
48 #if defined(DOS16)
49 
50 /* ***** Turbo C FAR model ***** */
51 #if defined(__TURBOC__)
52 
53 #include <alloc.h>
54 #define MALLOC(x)     malloc((size_t)(x))
55 #define REALLOC(x, y) realloc((pointer_t)(x), (size_t)(y))
56 #define FREE(x)       free((pointer_t)(x))
57 
58 #endif                                /* __TURBOC__ */
59 
60 
61 /* ***** Microsoft/Quick C FAR model ***** */
62 #if defined(_MSC_VER)
63 
64 /* Need to test these predefined macros */
65 #include <malloc.h>
66 
67 #define MALLOC(x)     _fmalloc((size_t)(x))
68 #define REALLOC(x, y) _frealloc((pointer_t)(x), (size_t)(y))
69 #define FREE(x)       _ffree((pointer_t)(x))
70 
71 #define DOSFARMODEL
72 
73 #endif                                /* _MSC_VER */
74 
75 #endif                                /* DOS16 */
76 
77 
78 /***** Others which don't have MALLOC() defined. *****/
79 
80 #if !defined(MALLOC)
81 
82 #define MALLOC(x)     malloc((size_t)(x))
83 #define REALLOC(x, y) realloc((pointer_t)(x), (size_t)(y))
84 #define FREE(x)       free((pointer_t)(x))
85 
86 #endif                                /* ! MALLOC */
87 
88 /***** Miscellaneous *****/
89 
90 /* Some old compilers do not have NULL defined */
91 #ifndef NULL
92 
93 #ifdef DOSFARMODEL
94 #define NULL 0L
95 #else
96 #define NULL 0
97 #endif                                /* DOSFARMODEL */
98 
99 #endif                                /* NULL */
100 
101 /* unsigned types (renamed to avoid conflict with possibly predefined types) */
102 typedef unsigned char uChar;
103 typedef unsigned short uShrt;
104 typedef unsigned long uLong;
105 
106 /* FALSE, TRUE */
107 #ifndef TRUE
108 enum {
109   FALSE, TRUE
110 };
111 #endif
112 
113 /* global error output macro */
114 #ifdef MACGRAPHX
115 #define errout(s) macputs(s)
116 #else
117 #define errout(s) fputs(s,stderr)
118 #endif
119 
120 /* ************************************************************************
121    pmars global structures and definitions
122    ************************************************************************ */
123 
124 #if defined(__STDC__)  || defined(DOS16)
125 #define NEW_STYLE
126 #endif
127 
128 /* Version and date */
129 
130 #define PMARSVER  92
131 #define PMARSDATE "25/12/00"
132 
133 #ifdef VMS                        /* Must change codes to work with VMS error
134                                  * handling */
135 extern  PMARS_FATAL, PMARS_BADCOMLIN, PMARS_PARSEERR;
136 #define GRAPHERR   1                /* no grphx yet */
137 #define NOT386     1                /* if this error occurs, I don't wanna hear
138                                  * about it. */
139 #define MEMERR   292                /* %SYS-E-INSFMEM */
140 #define SERIOUS  &PMARS_FATAL        /* %PMARS-F-FATAL */
141 #define FNOFOUND 98962                /* %RMS-E-FNF */
142 #define CLP_NOGOOD &PMARS_BADCOMLIN        /* %PMARS-E-BADCOMLIN */
143 #define PARSEERR &PMARS_PARSEERR/* ENDABORT */
144 #define USERABORT 44                /* %SYS-E-ABORT */
145 #else                                /* everyone else */
146 /* return code:
147    0: success
148    Negative number: System error such as insufficient space, etc
149    Positive number: User error such as number too big, file not found, etc */
150 #define GRAPHERR  -4                /* graphic error */
151 #define NOT386    -3                /* trying to execute 386 code on lesser
152                                  * machine */
153 #define MEMERR    -2                /* insufficient memory, cannot free, etc. */
154 #define SERIOUS   -1                /* program logic error */
155 #define FNOFOUND   1                /* File not found */
156 #define CLP_NOGOOD 2                /* command line argument error */
157 #define PARSEERR   3                /* File doesn't assemble correctly */
158 #define USERABORT  4                /* user stopped program from cdb, etc. */
159 #endif
160 
161 /* these are used as return codes of internal functions */
162 #define SUCCESS    0
163 #define WARNING    0
164 
165 /* used by eval.c */
166 #define OVERFLOW  1
167 #define OK_EXPR   0
168 #define BAD_EXPR -1
169 #define DIV_ZERO -2
170 
171 /* used by cdb.c */
172 #define NOBREAK 0
173 #define BREAK   1
174 #define STEP    2
175 
176 #define SKIP    1                /* cmdMod settings for cdb: skip next command */
177 #define RESET   2                /* clear command queue */
178 
179 #define UNSHARED -1                /* P-space is private */
180 #define PIN_APPEARED -2
181 
182 /* used by sim.c and asm.c */
183 #ifdef NEW_MODES
184 #define INDIR_A(x) (0x80 & (x))
185 #define RAW_MODE(x) (0x7F & (x))
186 #define SYM_TO_INDIR_A(x) ( ((x) - 3) | 0x80)        /* turns index into
187                                                  * addr_sym[] to INDIR_A code */
188 #define INDIR_A_TO_SYM(x) ( RAW_MODE(x) + 3 )        /* vice versa */
189 #endif
190 
191 /* used by many */
192 #define STDOUT stdout
193 
194 #ifdef DOS16
195 #define MAXCORESIZE   8192
196 #else
197 #ifdef SMALLMEM
198 #define MAXCORESIZE   65535
199 #else
200 #define MAXCORESIZE   ((INT_MAX>>1)+1)
201 #endif
202 #endif
203 
204 #define MAXTASKNUM    INT_MAX
205 #define MAXROUND      INT_MAX
206 #define MAXCYCLE      LONG_MAX
207 #ifdef DOS16
208 #define MAXWARRIOR         8
209 #else
210 #define MAXWARRIOR        36
211 #endif
212 #define MAXINSTR         1000
213 
214 #define MAXSEPARATION MAXCORESIZE/MAXWARRIOR
215 
216 #define MAXALLCHAR 256
217 
218 /* The following holds the order in which opcodes, modifiers, and addr_modes
219    are represented as in parser. The enumerated field should start from zero */
220 enum addr_mode {
221   IMMEDIATE,                        /* # */
222   DIRECT,                        /* $ */
223   INDIRECT,                        /* @ */
224   PREDECR,                        /* < */
225   POSTINC                        /* > */
226 };
227 
228 enum op {
229   MOV, ADD, SUB, MUL, DIV, MOD, JMZ,
230   JMN, DJN, CMP, SLT, SPL, DAT, JMP,
231   SEQ,  SNE, NOP, LDP, STP
232 };                                /* has to match asm.c:opname[] */
233 
234 enum modifier {
235   mA,                                /* .A */
236   mB,                                /* .B */
237   mAB,                                /* .AB */
238   mBA,                                /* .BA */
239   mF,                                /* .F */
240   mX,                                /* .X */
241   mI                                /* .I */
242 };
243 
244 
245 #ifdef SMALLMEM
246 typedef unsigned short ADDR_T;
247 #define ISNEG(x) ((x)==0xFFFF)        /* use for unsigned ADDR_T */
248 #else
249 typedef int ADDR_T;
250 #define ISNEG(x) ((x)<0)
251 #endif
252 
253 typedef unsigned char FIELD_T;
254 typedef unsigned long U32_T;        /* unsigned long (32 bits) */
255 typedef long S32_T;
256 
257 
258 /* Memory structure */
259 typedef struct mem_struct {
260   ADDR_T  A_value, B_value;
261   FIELD_T opcode;
262   FIELD_T A_mode, B_mode;
263   FIELD_T debuginfo;
264 
265 }       mem_struct;
266 
267 /* Warrior structure */
268 typedef struct warrior_struct {
269   long    pSpaceIDNumber;
270 #ifdef DOS16
271   ADDR_T far *taskHead, far * taskTail;
272 #else
273   ADDR_T *taskHead, *taskTail;
274 #endif
275   int     tasks;
276   ADDR_T  lastResult;
277   int     pSpaceIndex;
278   ADDR_T  position;                /* load position in core */
279   int     instLen;                /* Length of instBank */
280   int     offset;                /* Offset value specified by 'ORG' or 'END'.
281                                  * 0 is default */
282   short   score[MAXWARRIOR * 2 - 1];
283 
284   char   *name;                        /* warrior name */
285   char   *version;
286   char   *date;
287   char   *fileName;                /* file name */
288   char   *authorName;                /* author name */
289   mem_struct *instBank;
290 
291   struct warrior_struct *nextWarrior;
292 
293 }       warrior_struct;
294 
295 /* ***********************************************************************
296    pmars global variable declarations
297    *********************************************************************** */
298 
299 extern int errorcode;
300 extern int errorlevel;
301 extern char errmsg[MAXALLCHAR];
302 
303 /* Some parameters */
304 extern int warriors;
305 extern ADDR_T coreSize;
306 extern int taskNum;
307 extern ADDR_T instrLim;
308 extern ADDR_T separation;
309 extern int rounds;
310 extern long cycles;
311 
312 extern int cmdMod;
313 extern S32_T seed;
314 
315 extern int SWITCH_b;
316 extern int SWITCH_e;
317 extern int SWITCH_k;
318 extern int SWITCH_8;
319 extern int SWITCH_f;
320 extern ADDR_T SWITCH_F;
321 extern int SWITCH_V;
322 extern int SWITCH_o;
323 extern int SWITCH_Q;
324 extern char *SWITCH_eq;
325 #ifdef VMS
326 extern int SWITCH_D;
327 #endif
328 #ifdef PERMUTATE
329 extern int SWITCH_P;
330 #endif
331 
332 extern int inCdb;
333 extern int debugState;
334 extern int copyDebugInfo;
335 #if defined(DOSTXTGRAPHX) || defined(DOSGRXGRAPHX) || defined(LINUXGRAPHX) \
336     || defined(XWINGRAPHX)
337 extern int inputRedirection;
338 #endif
339 #if defined(XWINGRAPHX)
340 extern int xWinArgc;
341 extern char **xWinArgv;
342 #endif
343 extern mem_struct INITIALINST;        /* initialize to DAT.F $0,$0 */
344 
345 extern warrior_struct warrior[MAXWARRIOR];
346 #ifdef DOS16
347 extern ADDR_T far *pSpace[MAXWARRIOR];
348 #else
349 extern ADDR_T *pSpace[MAXWARRIOR];
350 #endif
351 extern ADDR_T pSpaceSize;
352 
353 /* ***********************************************************************
354    display define's, declarations and typedefs
355    *********************************************************************** */
356 
357 #if defined(DOSTXTGRAPHX) || defined(DOSGRXGRAPHX) || defined(LINUXGRAPHX) \
358     || defined(XWINGRAPHX)
359 
360 #if !defined(LINUXGRAPHX)        /* vga.h already defines TEXT to be 0 */
361 #define         TEXT 0
362 #endif
363 #define         GRX 1
364 #define         SPEEDLEVELS 9
365 #define         NORMAL_ATTR 0x0700
366 extern int displayLevel;
367 extern int displayMode;
368 extern int displaySpeed;
369 extern int SWITCH_v;
370 
371 #if defined(LINUXGRAPHX)        /* needed for correct keyboard handling */
372 extern struct termios tio_orig;
373 extern int console_fd;
374 #endif
375 
376 #if defined(CURSESGRAPHX)
377 #include <curses.h>
378 #if defined(A_NORMAL) || defined(A_BOLD) || defined(A_REVERSE)
379 #define ATTRIBUTE
380 #endif                                /* A_* */
381 
382 extern int refreshInterval;
383 extern int refIvalAr[SPEEDLEVELS];
384 
385 #else
386 
387 extern int keyDelay;
388 extern int keyDelayAr[SPEEDLEVELS];
389 extern unsigned long loopDelay;
390 extern unsigned long loopDelayAr[SPEEDLEVELS];
391 
392 #endif                                /* CURSESGRAPHX */
393 #endif                                /* DOSTXTGRAPHX and DOSGRXGRAPHX and
394                                  * LINUXGRAPHX */
395 
396 
397 /* ***********************************************************************
398    function prototypes
399    *********************************************************************** */
400 
401 #ifdef NEW_STYLE
402 
403 extern int
404         parse_param(int argc, char *argv[]);
405 extern int eval_expr(char *expr, long *result);
406 extern int assemble(char *fName, int aWarrior);
407 extern void disasm(mem_struct * cells, ADDR_T n, ADDR_T offset);
408 extern void simulator1(void);
409 extern char *locview(ADDR_T loc, char *outp);
410 extern int cdb(char *msg);
411 extern int score(int warnum);
412 extern void sort_by_score(int *idxV, int *scrV);
413 extern int deaths(int warnum);
414 extern void results(FILE * outp);
415 extern void sort_by_score();
416 extern void Exit(int code);
417 extern void reset_regs(void);
418 extern void set_reg(char regChr, long val);
419 
420 #if defined(DOSTXTGRAPHX) || defined(DOSGRXGRAPHX) || defined(LINUXGRAPHX) \
421     || defined(XWINGRAPHX)
422 extern void decode_vopt(int option);
423 #ifndef LINUXGRAPHX
424 extern void grputs(char *str);
425 extern void aputs5(char *str, int attribute);
426 #endif
427 
428 #if defined(LINUXGRAPHX)
429 extern char *svga_gets(char *s, int maxstr);
430 extern void svga_puts(char *s);
431 extern void svga_display_close(int wait);
432 extern void svga_write_menu(void);
433 extern void svga_open_graphics(void);
434 extern void svga_clear(void);
435 extern void svga_update(int curPanel);
436 extern void svga_clear_arena(void);
437 extern int svga_getch(void);
438 #endif
439 
440 #if defined(XWINGRAPHX)
441 extern void xWin_puts(char *s);
442 extern void xWin_write_menu(void);
443 extern void xWin_clear(void);
444 extern void xWin_update(int curPanel);
445 extern void xWin_display_close(int wait);
446 extern char *xWin_gets(char *s, int maxstr);
447 extern void xWin_resize(void);
448 #endif
449 #endif
450 
451 #ifdef DOS16
452 extern char *cellview(mem_struct far * cell, char *outp, int emptyDisp);
453 #else
454 extern char *cellview(mem_struct * cell, char *outp, int emptyDisp);
455 #endif
456 
457 #else
458 
459 extern int
460         parse_param();
461 extern int
462         eval_expr();
463 extern int assemble();
464 extern void disasm();
465 extern void simulator1();
466 extern char *locview();
467 extern char *cellview();
468 extern int cdb();
469 extern int score();
470 extern int deaths();
471 extern void results();
472 extern void Exit();
473 extern void reset_regs();
474 extern void set_reg();
475 
476 #if defined(CURSESGRAPHX)
477 extern void decode_vopt();
478 extern void aputs5();
479 #endif
480 
481 #if defined (LINUXGRAPHX)
482 extern char *svga_gets();
483 extern void svga_puts();
484 extern void svga_display_close();
485 extern void svga_write_menu();
486 extern void svga_open_graphics();
487 extern void svga_clear();
488 extern void svga_update();
489 extern void svga_clear_arena();
490 extern int svga_getch();
491 #endif
492 
493 #if defined(XWINGRAPHX)
494 extern void xWin_puts();
495 extern void xWin_write_menu();
496 extern void xWin_clear();
497 extern void xWin_update();
498 extern void xWin_display_close();
499 extern char *xWin_gets();
500 extern void xWin_resize();
501 #endif
502 
503 #endif
504