1 #ifndef INSN_H
2 #define INSN_H
3 /* insn.h: Instruction encoding definition
4  * $Id: insn.h,v 1.1.1.1 2004/02/14 04:14:23 michal 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 {
81     DAT,                        /* must be 0 */
82     SPL,
83     MOV,
84     DJN,
85     ADD,
86     JMZ,
87     SUB,
88     SEQ,
89     SNE,
90     SLT,
91     JMN,
92     JMP,
93     NOP,
94     MUL,
95     MODM,
96     DIV,
97     LDP,
98     STP                         /* 18 */
99 };
100 
101 enum
102 {
103     mF, mA, mB, mAB, mBA, mX, mI        /* 7 */
104 };
105 
106 enum
107 {                               /* must start from 0,
108                                    the ordering is important */
109     DIRECT,
110     IMMEDIATE,
111     BINDIRECT,
112     BPREDEC,
113     BPOSTINC,
114     AINDIRECT,
115     APREDEC,
116     APOSTINC                    /* 8 */
117 };
118 
119 #define INDIRECT BINDIRECT
120 #define PREDEC   BPREDEC
121 #define POSTINC  BPOSTINC
122 
123 #define INDIR_A(mode) ((mode) >= AINDIRECT)
124 
125 /* mode */
126 #define RAW_MODE(mode) ((mode) + (INDIRECT-AINDIRECT))
127 
128 
129 /*
130  * flags
131  */
132 enum
133 {
134     flB_START                   /* start label */
135 };
136 
137 #define fl_START (1<<flB_START)
138 
139 
140 /* Macros to take things mod CORESIZE
141  *
142  * Mod here is the `mathematical' modulo, with non-negative
143  * results even with x negative.
144  *
145  * MOD(x,M):	x mod CORESIZE
146  * MODS(x,M):	x mod CORESIZE
147  */
148 #define MODS(x,M) ( (int)(x)%(int)(M) >= 0 \
149 		  ? (int)(x)%(int)(M) \
150 		  : (M) + ((int)(x)%(int)(M)) )
151 #define MOD(x,M) ( (x) % (M) )
152 
153 #endif /* INSN_H */
154