sim86_instruction_table.inl (9157B)
1 /* ======================================================================== 2 3 (C) Copyright 2023 by Molly Rocket, Inc., All Rights Reserved. 4 5 This software is provided 'as-is', without any express or implied 6 warranty. In no event will the authors be held liable for any damages 7 arising from the use of this software. 8 9 Please see https://computerenhance.com for more information 10 11 ======================================================================== */ 12 13 /* 14 NOTE(casey): This instruction table is a direct translation of table 4-12 in the Intel 8086 manual. 15 The macros are designed to allow direct transcription, without changing the order or manner 16 of specification in the table in any way. Additional "implicit" versions of the macros are provided 17 so that hard-coded fields can be supplied uniformly. 18 19 The table is also designed to allow you to include it multiple times to "pull out" other things 20 from the table, such as opcode mnemonics as strings or enums, etc. 21 */ 22 23 #ifndef INST 24 #define INST(Mnemonic, Encoding, ...) {Op_##Mnemonic, Encoding, __VA_ARGS__}, 25 #endif 26 27 #ifndef INSTALT 28 #define INSTALT INST 29 #endif 30 31 #define B(Bits) {Bits_Literal, sizeof(#Bits)-1, 0, 0b##Bits} 32 #define D {Bits_D, 1} 33 #define S {Bits_S, 1} 34 #define W {Bits_W, 1} 35 #define V {Bits_V, 1} 36 #define Z {Bits_Z, 1} 37 38 #define XXX {Bits_Data, 3, 0} 39 #define YYY {Bits_Data, 3, 3} 40 #define RM {Bits_RM, 3} 41 #define MOD {Bits_MOD, 2} 42 #define REG {Bits_REG, 3} 43 #define SR {Bits_SR, 2} 44 45 #define ImpW(Value) {Bits_W, 0, 0, Value} 46 #define ImpREG(Value) {Bits_REG, 0, 0, Value} 47 #define ImpMOD(Value) {Bits_MOD, 0, 0, Value} 48 #define ImpRM(Value) {Bits_RM, 0, 0, Value} 49 #define ImpD(Value) {Bits_D, 0, 0, Value} 50 #define ImpS(Value) {Bits_S, 0, 0, Value} 51 52 #define DISP {Bits_HasDisp, 0, 0, 1} 53 #define ADDR {Bits_HasDisp, 0, 0, 1}, {Bits_DispAlwaysW, 0, 0, 1} 54 #define DATA {Bits_HasData, 0, 0, 1} 55 #define DATA_IF_W {Bits_WMakesDataW, 0, 0, 1} 56 #define Flags(F) {F, 0, 0, 1} 57 58 INST(mov, {B(100010), D, W, MOD, REG, RM}) 59 INSTALT(mov, {B(1100011), W, MOD, B(000), RM, DATA, DATA_IF_W, ImpD(0)}) 60 INSTALT(mov, {B(1011), W, REG, DATA, DATA_IF_W, ImpD(1)}) 61 INSTALT(mov, {B(1010000), W, ADDR, ImpREG(0), ImpMOD(0), ImpRM(0b110), ImpD(1)}) 62 INSTALT(mov, {B(1010001), W, ADDR, ImpREG(0), ImpMOD(0), ImpRM(0b110), ImpD(0)}) 63 INSTALT(mov, {B(100011), D, B(0), MOD, B(0), SR, RM}) // NOTE(casey): This collapses 2 entries in the 8086 table by adding an explicit D bit 64 65 INST(push, {B(11111111), MOD, B(110), RM, ImpW(1)}) 66 INSTALT(push, {B(01010), REG, ImpW(1)}) 67 INSTALT(push, {B(000), SR, B(110), ImpW(1)}) 68 69 INST(pop, {B(10001111), MOD, B(000), RM, ImpW(1)}) 70 INSTALT(pop, {B(01011), REG, ImpW(1)}) 71 INSTALT(pop, {B(000), SR, B(111), ImpW(1)}) 72 73 INST(xchg, {B(1000011), W, MOD, REG, RM, ImpD(1)}) 74 INSTALT(xchg, {B(10010), REG, ImpMOD(0b11), ImpW(1), ImpRM(0)}) 75 76 INST(in, {B(1110010), W, DATA, ImpREG(0), ImpD(1)}) 77 INSTALT(in, {B(1110110), W, ImpREG(0), ImpD(1), ImpMOD(0b11), ImpRM(2), Flags(Bits_RMRegAlwaysW)}) 78 INST(out, {B(1110011), W, DATA, ImpREG(0), ImpD(0)}) 79 INSTALT(out, {B(1110111), W, ImpREG(0), ImpD(0), ImpMOD(0b11), ImpRM(2), Flags(Bits_RMRegAlwaysW)}) 80 81 INST(xlat, {B(11010111)}) 82 INST(lea, {B(10001101), MOD, REG, RM, ImpD(1), ImpW(1)}) 83 INST(lds, {B(11000101), MOD, REG, RM, ImpD(1), ImpW(1)}) 84 INST(les, {B(11000100), MOD, REG, RM, ImpD(1), ImpW(1)}) 85 INST(lahf, {B(10011111)}) 86 INST(sahf, {B(10011110)}) 87 INST(pushf, {B(10011100)}) 88 INST(popf, {B(10011101)}) 89 90 INST(add, {B(000000), D, W, MOD, REG, RM}) 91 INSTALT(add, {B(100000), S, W, MOD, B(000), RM, DATA, DATA_IF_W}) 92 INSTALT(add, {B(0000010), W, DATA, DATA_IF_W, ImpREG(0), ImpD(1)}) 93 94 INST(adc, {B(000100), D, W, MOD, REG, RM}) 95 INSTALT(adc, {B(100000), S, W, MOD, B(010), RM, DATA, DATA_IF_W}) 96 INSTALT(adc, {B(0001010), W, DATA, DATA_IF_W, ImpREG(0), ImpD(1)}) 97 98 INST(inc, {B(1111111), W, MOD, B(000), RM}) 99 INSTALT(inc, {B(01000), REG, ImpW(1)}) 100 101 INST(aaa, {B(00110111)}) 102 INST(daa, {B(00100111)}) 103 104 INST(sub, {B(001010), D, W, MOD, REG, RM}) 105 INSTALT(sub, {B(100000), S, W, MOD, B(101), RM, DATA, DATA_IF_W}) 106 INSTALT(sub, {B(0010110), W, DATA, DATA_IF_W, ImpREG(0), ImpD(1)}) 107 108 INST(sbb, {B(000110), D, W, MOD, REG, RM}) 109 INSTALT(sbb, {B(100000), S, W, MOD, B(011), RM, DATA, DATA_IF_W}) 110 INSTALT(sbb, {B(0001110), W, DATA, DATA_IF_W, ImpREG(0), ImpD(1)}) 111 112 INST(dec, {B(1111111), W, MOD, B(001), RM}) 113 INSTALT(dec, {B(01001), REG, ImpW(1)}) 114 115 INST(neg, {B(1111011), W, MOD, B(011), RM}) 116 117 INST(cmp, {B(001110), D, W, MOD, REG, RM}) 118 INSTALT(cmp, {B(100000), S, W, MOD, B(111), RM, DATA, DATA_IF_W}) 119 INSTALT(cmp, {B(0011110), W, DATA, DATA_IF_W, ImpREG(0), ImpD(1)}) // TODO(casey): The manual table suggests this data is only 8-bit, but wouldn't it be 16 as well? 120 121 INST(aas, {B(00111111)}) 122 INST(das, {B(00101111)}) 123 INST(mul, {B(1111011), W, MOD, B(100), RM, ImpS(0)}) 124 INST(imul, {B(1111011), W, MOD, B(101), RM, ImpS(1)}) 125 INST(aam, {B(11010100), B(00001010)}) // TODO(casey): The manual says this has a DISP... but how could it? What for?? 126 INST(div, {B(1111011), W, MOD, B(110), RM, ImpS(0)}) 127 INST(idiv, {B(1111011), W, MOD, B(111), RM, ImpS(1)}) 128 INST(aad, {B(11010101), B(00001010)}) 129 INST(cbw, {B(10011000)}) 130 INST(cwd, {B(10011001)}) 131 132 INST(not, {B(1111011), W, MOD, B(010), RM}) 133 INST(shl, {B(110100), V, W, MOD, B(100), RM}) 134 INST(shr, {B(110100), V, W, MOD, B(101), RM}) 135 INST(sar, {B(110100), V, W, MOD, B(111), RM}) 136 INST(rol, {B(110100), V, W, MOD, B(000), RM}) 137 INST(ror, {B(110100), V, W, MOD, B(001), RM}) 138 INST(rcl, {B(110100), V, W, MOD, B(010), RM}) 139 INST(rcr, {B(110100), V, W, MOD, B(011), RM}) 140 141 INST(and, {B(001000), D, W, MOD, REG, RM}) 142 INSTALT(and, {B(1000000), W, MOD, B(100), RM, DATA, DATA_IF_W}) 143 INSTALT(and, {B(0010010), W, DATA, DATA_IF_W, ImpREG(0), ImpD(1)}) 144 145 INST(test, {B(100001), D, W, MOD, REG, RM}) 146 INSTALT(test, {B(1111011), W, MOD, B(000), RM, DATA, DATA_IF_W}) 147 INSTALT(test, {B(1010100), W, DATA, DATA_IF_W, ImpREG(0), ImpD(1)}) // TODO(casey): The manual table suggests this data is only 8-bit, but it seems like it could be 16 too? 148 149 INST(or, {B(000010), D, W, MOD, REG, RM}) 150 INSTALT(or, {B(1000000), W, MOD, B(001), RM, DATA, DATA_IF_W}) 151 INSTALT(or, {B(0000110), W, DATA, DATA_IF_W, ImpREG(0), ImpD(1)}) 152 153 INST(xor, {B(001100), D, W, MOD, REG, RM}) 154 INSTALT(xor, {B(1000000), W, MOD, B(110), RM, DATA, DATA_IF_W}) // TODO(casey): The manual has conflicting information about this encoding, but I believe this is the correct binary pattern. 155 INSTALT(xor, {B(0011010), W, DATA, DATA_IF_W, ImpREG(0), ImpD(1)}) 156 157 INST(rep, {B(1111001), Z}) 158 INST(movs, {B(1010010), W}) 159 INST(cmps, {B(1010011), W}) 160 INST(scas, {B(1010111), W}) 161 INST(lods, {B(1010110), W}) 162 INST(stos, {B(1010101), W}) 163 164 INST(call, {B(11101000), ADDR}) 165 INSTALT(call, {B(11111111), MOD, B(010), RM, ImpW(1)}) 166 INSTALT(call, {B(10011010), ADDR, DATA, ImpW(1)}) 167 INSTALT(call, {B(11111111), MOD, B(011), RM, ImpW(1)}) 168 169 INST(jmp, {B(11101001), ADDR}) 170 INSTALT(jmp, {B(11101011), DISP}) 171 INSTALT(jmp, {B(11111111), MOD, B(100), RM, ImpW(1)}) 172 INSTALT(jmp, {B(11101010), ADDR, DATA, ImpW(1)}) 173 INSTALT(jmp, {B(11111111), MOD, B(101), RM, ImpW(1)}) 174 175 INST(ret, {B(11000011)}) 176 INSTALT(ret, {B(11000010), DATA, DATA_IF_W, ImpW(1)}) 177 INSTALT(ret, {B(11001011)}) 178 INSTALT(ret, {B(11001010), DATA, DATA_IF_W, ImpW(1)}) 179 180 INST(je, {B(01110100), DISP, Flags(Bits_RelJMPDisp)}) 181 INST(jl, {B(01111100), DISP, Flags(Bits_RelJMPDisp)}) 182 INST(jle, {B(01111110), DISP, Flags(Bits_RelJMPDisp)}) 183 INST(jb, {B(01110010), DISP, Flags(Bits_RelJMPDisp)}) 184 INST(jbe, {B(01110110), DISP, Flags(Bits_RelJMPDisp)}) 185 INST(jp, {B(01111010), DISP, Flags(Bits_RelJMPDisp)}) 186 INST(jo, {B(01110000), DISP, Flags(Bits_RelJMPDisp)}) 187 INST(js, {B(01111000), DISP, Flags(Bits_RelJMPDisp)}) 188 INST(jne, {B(01110101), DISP, Flags(Bits_RelJMPDisp)}) 189 INST(jnl, {B(01111101), DISP, Flags(Bits_RelJMPDisp)}) 190 INST(jg, {B(01111111), DISP, Flags(Bits_RelJMPDisp)}) 191 INST(jnb, {B(01110011), DISP, Flags(Bits_RelJMPDisp)}) 192 INST(ja, {B(01110111), DISP, Flags(Bits_RelJMPDisp)}) 193 INST(jnp, {B(01111011), DISP, Flags(Bits_RelJMPDisp)}) 194 INST(jno, {B(01110001), DISP, Flags(Bits_RelJMPDisp)}) 195 INST(jns, {B(01111001), DISP, Flags(Bits_RelJMPDisp)}) 196 INST(loop, {B(11100010), DISP, Flags(Bits_RelJMPDisp)}) 197 INST(loopz, {B(11100001), DISP, Flags(Bits_RelJMPDisp)}) 198 INST(loopnz, {B(11100000), DISP, Flags(Bits_RelJMPDisp)}) 199 INST(jcxz, {B(11100011), DISP, Flags(Bits_RelJMPDisp)}) 200 201 INST(int, {B(11001101), DATA}) 202 INST(int3, {B(11001100)}) // TODO(casey): The manual does not suggest that this intrinsic has an "int3" mnemonic, but NASM thinks so 203 204 INST(into, {B(11001110)}) 205 INST(iret, {B(11001111)}) 206 207 INST(clc, {B(11111000)}) 208 INST(cmc, {B(11110101)}) 209 INST(stc, {B(11111001)}) 210 INST(cld, {B(11111100)}) 211 INST(std, {B(11111101)}) 212 INST(cli, {B(11111010)}) 213 INST(sti, {B(11111011)}) 214 INST(hlt, {B(11110100)}) 215 INST(wait, {B(10011011)}) 216 INST(esc, {B(11011), XXX, MOD, YYY, RM}) 217 INST(lock, {B(11110000)}) 218 INST(segment, {B(001), SR, B(110)}) 219 220 #undef INST 221 #undef INSTALT 222 223 #undef B 224 #undef D 225 #undef S 226 #undef W 227 #undef V 228 #undef Z 229 230 #undef XXX 231 #undef YYY 232 #undef RM 233 #undef MOD 234 #undef REG 235 #undef SR 236 237 #undef ImpW 238 #undef ImpREG 239 #undef ImpMOD 240 #undef ImpRM 241 #undef ImpD 242 #undef ImpS 243 244 #undef DISP 245 #undef ADDR 246 #undef DATA 247 #undef DATA_IF_W 248 #undef Flags