1 #ifndef INSN_H
2 #define INSN_H
3 /* insn.h: Instruction encoding definition
4  * $Id: insn.h,v 1.3 2003/06/06 21:31:15 martinus 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 /*
13  * Instruction encoding:
14  *
15  * Instructions are held in a insn_t typed struct with three fields:
16  *   in:	instruction flags, opcode, modifier, a-mode, b-mode
17  *   a:		a-value
18  *   b: 	b-value
19  *
20  * The layout of the `in' field is as follows:
21  *
22  * bit         15 14 13 12 11 10  9  8  7  6  5  4  3  2  1  0
23  * field   | flags | |- op-code  -| |-.mod-| |b-mode| |a-mode|
24  *
25  * Currently there is only one flag, the fl_START flag, which
26  * the assembler uses to figure out the starting instruction
27  * of a warrior (i.e. the one given by the START label).
28  */
29 
30 #define mBITS 3			/* number of bits for mode */
31 #define opBITS 5		/* bits for opcode */
32 #define moBITS 3		/* bits for modifier */
33 #define flBITS 2		/* bits for flags */
34 
35 /* Positions of various fields
36  */
37 #define maPOS 0
38 #define mbPOS (maPOS + mBITS)
39 #define moPOS (mbPOS + mBITS)
40 #define opPOS (moPOS + moBITS)
41 #define flPOS (opPOS + opBITS)
42 
43 /* Various masks.  These extract a field once it has been
44  * shifted to the least significant bits of the word.
45  */
46 #define moMASK ((1<<moBITS)-1)
47 #define opMASK ((1<<opBITS)-1)
48 #define mMASK ((1<<mBITS)-1)
49 #define flMASK ((1<<flBITS)-1)
50 #define iMASK ( (1<<flPOS)-1 )
51 
52 /*
53  * Extract the flags of an instruction `in' field
54  */
55 #define get_flags(in) ( ((in)>>flPOS) & flMASK )
56 
57 
58 /*
59  * OP(o,m,ma,mb): This macro encodes an instruction `in' field
60  *		  from its various components (not flags).
61  *
62  * 	o: opcode
63  *	m: modifier
64  *	ma: a-mode
65  *	mb: b-mode
66  *
67  *  e.g. OP(SPL, mF, DIRECT, BPREDEC )
68  *  is a
69  *       spl.f $  , <
70  */
71 #define _OP(o,m) ( ((o)<<moBITS) | (m) )
72 #define OP( o, m, ma, mb ) ((_OP((o),(m))<<moPOS) | ((mb) << mbPOS) | (ma))
73 
74 
75 /*
76  * Encodings for various fields of the `in' field.
77  *
78  */
79 enum {
80   DAT,				/* must be 0 */
81   SPL,
82   MOV,
83   DJN,
84   ADD,
85   JMZ,
86   SUB,
87   SEQ,
88   SNE,
89   SLT,
90   JMN,
91   JMP,
92   NOP,
93   MUL,
94   MODM,
95   DIV,
96   LDP,
97   STP				/* 18 */
98 };
99 
100 enum {
101   mF, mA, mB, mAB, mBA, mX, mI	/* 7 */
102 };
103 
104 enum {				/* must start from 0,
105 				   the ordering is important */
106   DIRECT,
107   IMMEDIATE,
108   BINDIRECT,
109   BPREDEC,
110   BPOSTINC,
111   AINDIRECT,
112   APREDEC,
113   APOSTINC		/* 8 */
114 };
115 
116 #define INDIRECT BINDIRECT
117 #define PREDEC   BPREDEC
118 #define POSTINC  BPOSTINC
119 
120 #define INDIR_A(mode) ((mode) >= AINDIRECT)
121 
122 // mode
123 #define RAW_MODE(mode) ((mode) + (INDIRECT-AINDIRECT))
124 
125 
126 /*
127  * flags
128  */
129 enum {
130   flB_START			/* start label */
131 };
132 
133 #define fl_START (1<<flB_START)
134 
135 
136 /* Macros to take things mod CORESIZE
137  *
138  * Mod here is the `mathematical' modulo, with non-negative
139  * results even with x negative.
140  *
141  * MOD(x,M):	x mod CORESIZE
142  * MODS(x,M):	x mod CORESIZE
143  */
144 #define MODS(x,M) ( (int)(x)%(int)(M) >= 0 \
145 		  ? (int)(x)%(int)(M) \
146 		  : (M) + ((int)(x)%(int)(M)) )
147 #define MOD(x,M) ( (x) % (M) )
148 
149 #endif /* INSN_H */
150