1 #ifndef EXHAUST_H
2 #define EXHAUST_H
3 /*  exhaust.h:  Global constants, structures, and types
4  * $Id: exhaust.h,v 1.7 2004/02/22 06:54:24 varfar Exp $
5  */
6 
7 /* This file is part of `exhaust', a memory array redcode simulator.
8  * Copyright (C) 2002 M Joonas Pihlaja
9  * Public Domain.
10  */
11 
12 #include "insn.h"
13 
14 
15 /* global debug level */
16 #ifndef DEBUG
17 #define DEBUG 0
18 #endif
19 
20 
21 /*
22  * Global constants
23  *
24  */
25 
26 #define MAXLENGTH 100
27   /* max length of warrior */
28 
29 
30 /*
31  * Global types
32  *
33  */
34 
35 /* misc. integral types */
36 typedef unsigned char  u8_t;
37 typedef unsigned short u16_t;
38 typedef unsigned long  u32_t;
39 typedef long           s32_t;
40 
41   /* Choose an appropriate field_t width.  In a field_t variable
42    * we hold unsigned integers in 0..CORESIZE-1
43    */
44 #define FIELD_T_WIDTH 16
45 
46 #if FIELD_T_WIDTH <= 8
47   typedef u8_t field_t;
48 #elif FIELD_T_WIDTH <= 16
49   typedef u16_t field_t;
50 #else
51   typedef u32_t field_t;
52 #endif
53 
54   /*
55    * Instructions in core:
56    */
57   typedef struct insn_st {
58     field_t in;                   /* flags, opcode, modifier, a- and b-modes */
59     field_t a, b;               /* a-value, b-value */
60     #ifdef __cplusplus
opcodeinsn_st61     OPCODE opcode() const { return (OPCODE)((in >> opPOS) & opMASK); }
modifierinsn_st62     MODIFIER modifier() const { return (MODIFIER)((in >> moPOS) & moMASK); }
opcode_modifierinsn_st63 	 unsigned opcode_modifier() const { return (in >> moPOS); }
addrmode_ainsn_st64     ADDRMODE addrmode_a() const { return (ADDRMODE)((in >> maPOS) & maMASK); }
addrmode_binsn_st65     ADDRMODE addrmode_b() const { return (ADDRMODE)((in >> mbPOS) & mbMASK); }
set_opcodeinsn_st66     void set_opcode(const OPCODE op) { in = OP(op,modifier(),addrmode_a(),addrmode_b()); }
set_modifierinsn_st67     void set_modifier(const MODIFIER mod) { in = OP(opcode(),mod,addrmode_a(),addrmode_b()); }
set_opcode_modifierinsn_st68 	 void set_opcode_modifier(const unsigned opmod) { in = (opmod << moPOS) | (addrmode_b() << mbPOS) | addrmode_b(); }
set_addrmode_ainsn_st69     void set_addrmode_a(const ADDRMODE a) { in = OP(opcode(),modifier(),a,addrmode_b()); }
set_addrmode_binsn_st70     void set_addrmode_b(const ADDRMODE b) { in = OP(opcode(),modifier(),addrmode_a(),b); }
flagsinsn_st71     unsigned flags() const { return (in >> flPOS); }
has_flagsinsn_st72     bool has_flags() const { return (in > iMASK); }
has_no_flagsinsn_st73     bool has_no_flags() const { return !has_flags(); }
invalidateinsn_st74     void invalidate() {
75     	in = ~0;
76     	a = ~0;
77     	b = ~0;
78     }
79     bool valid(const field_t coresize = 0,const bool allow_flags=false) const { // if coresize is 0, bounds of operands will not be checked
80 			if(	(opcode() >= OPCODE_LAST) ||
81 				(modifier() >= MODIFIER_LAST) ||
82 				(addrmode_a() >= ADDRMODE_LAST) ||
83 				(addrmode_b() >= ADDRMODE_LAST))
84 				return false;
85 			if(0 < coresize) { // set?
86 				if(	(coresize <= a) ||
87 					(coresize <= b))
88 					return false;
89 			}
90 			if(!allow_flags) {
91 				if(has_flags())
92 					return false;
93 			}
94 			return true; // if here, then ok
95     }
set_clearinsn_st96 		void set_clear() { in = OP(DAT,mF,DIRECT,DIRECT); a = 0; b = 0; }
is_clearinsn_st97 		bool is_clear() const { // is this default core?
98 			return
99 				(	(DAT == opcode()) &&
100 					(mF == modifier()) &&
101 					(0 == a) &&
102 					(0 == b));
103 		}
104     #endif
105   } insn_t;
106 
107 
108   /*
109    * Warrior struct
110    */
111   typedef struct warrior_st {
112     insn_t code[ MAXLENGTH ];   /* code of warrior */
113     unsigned int len;		/* length of -"- */
114     unsigned int start;		/* start relative to first insn */
115 
116     int have_pin;		/* does warrior have pin? */
117     u32_t pin;			/* pin of warrior or garbage. */
118 
119     /* info fields -- these aren't automatically set or used */
120     char *name;
121     int no;                     /* warrior no. */
122   } warrior_t;
123 
124 
125 #endif /* EXHAUST_H */
126