summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/cpu/i86/instr286.c
blob: 23d14edd5a2c2892cd73acfb43d3bcaef1defd16 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/* descriptor table format in memory
   UINT16 limit
   UINT24 addr
   0..3 type
    system segment
     3 execute:
      0 data segment: readable,
       1: write
       2: expand down
      1 code segment
       1: readable
       2: conforming (can be used with higher privilege level)
     0: access (set when processor accesses segment)

   4 dt 0 system segment, 1 application segment (code, data)
   5,6 dpl descriptor privileg level
   7 p present 0 gives trap when accessed
   UINT16 reserved (should be zero)
*/
#define WRITEABLE(a) ((a&0xa)==2)
#define READABLE(a) ( ((a&0xa)==0xa)|| ((a&8)==0) )

static void i80286_trap2(i80286_state *cpustate,int number)
{
	i80286_interrupt(cpustate,number);
}

static int i80286_selector_okay(i80286_state *cpustate,UINT16 selector)
{
	if (selector&4) {
		return (selector&~7)<cpustate->ldtr.limit;
	} else {
		return (selector&~7)<cpustate->gdtr.limit;
	}
}

static offs_t i80286_selector_to_address(i80286_state *cpustate,UINT16 selector)
{
	if (selector&4) {
		return cpustate->ldtr.base+(selector&~7);
	} else {
		return cpustate->gdtr.base+(selector&~7);
	}
}

static void i80286_data_descriptor(i80286_state *cpustate,int reg, UINT16 selector)
{
	if (PM) {
		UINT16 help;
		/* selector format
           15..3 number/address in descriptor table
           2: 0 global, 1 local descriptor table
           1,0: requested privileg level
           must be higher or same as current privileg level in code selector */
		if (selector&4) { /* local descriptor table */
			if (selector>cpustate->ldtr.limit) i80286_trap2(cpustate,GENERAL_PROTECTION_FAULT);
			cpustate->sregs[reg]=selector;
			cpustate->limit[reg]=ReadWord(cpustate->ldtr.base+(selector&~7));
			cpustate->base[reg]=ReadWord(cpustate->ldtr.base+(selector&~7)+2)
				|(ReadWord(cpustate->ldtr.base+(selector&~7)+4)<<16);
			cpustate->rights[reg]=cpustate->base[reg]>>24;
			cpustate->base[reg]&=0xffffff;
		} else { /* global descriptor table */
			if (!(selector&~7)||(selector>cpustate->gdtr.limit)) i80286_trap2(cpustate,GENERAL_PROTECTION_FAULT);
			cpustate->sregs[reg]=selector;
			cpustate->limit[reg]=ReadWord(cpustate->gdtr.base+(selector&~7));
			cpustate->base[reg]=ReadWord(cpustate->gdtr.base+(selector&~7)+2);
			help=ReadWord(cpustate->gdtr.base+(selector&~7)+4);
			cpustate->rights[reg]=help>>8;
			cpustate->base[reg]|=(help&0xff)<<16;
		}
	} else {
		cpustate->sregs[reg]=selector;
		cpustate->base[reg]=selector<<4;
	}
}

static void i80286_code_descriptor(i80286_state *cpustate,UINT16 selector, UINT16 offset)
{
	UINT16 word1, word2, word3;
	if (PM) {
		/* selector format
           15..3 number/address in descriptor table
           2: 0 global, 1 local descriptor table
           1,0: requested privileg level
           must be higher or same as current privileg level in code selector */
		if (selector&4) { /* local descriptor table */
			if (selector>cpustate->ldtr.limit) i80286_trap2(cpustate,GENERAL_PROTECTION_FAULT);
			word1=ReadWord(cpustate->ldtr.base+(selector&~7));
			word2=ReadWord(cpustate->ldtr.base+(selector&~7)+2);
			word3=ReadWord(cpustate->ldtr.base+(selector&~7)+4);
		} else { /* global descriptor table */
			if (!(selector&~7)||(selector>cpustate->gdtr.limit)) i80286_trap2(cpustate,GENERAL_PROTECTION_FAULT);
			word1=ReadWord(cpustate->gdtr.base+(selector&~7));
			word2=ReadWord(cpustate->gdtr.base+(selector&~7)+2);
			word3=ReadWord(cpustate->gdtr.base+(selector&~7)+4);
		}
		if (word3&0x1000) {
			cpustate->sregs[CS]=selector;
			cpustate->limit[CS]=word1;
			cpustate->base[CS]=word2|((word3&0xff)<<16);
			cpustate->rights[CS]=word3>>8;
			cpustate->pc=cpustate->base[CS]+offset;
		} else { // systemdescriptor
			switch (word3&0xf00) {
			case 0x400: // call gate
				// word3&0x1f words to be copied from stack to stack
				i80286_data_descriptor(cpustate, CS, word2);
				cpustate->pc=cpustate->base[CS]+word1;
				break;
			case 0x500: // task gate
				i80286_data_descriptor(cpustate, CS, word2);
				cpustate->pc=cpustate->base[CS]+word1;
				break;
			case 0x600: // interrupt gate
				cpustate->TF = cpustate->IF = 0;
				i80286_data_descriptor(cpustate, CS, word2);
				cpustate->pc=cpustate->base[CS]+word1;
				break;
			case 0x700: // trap gate
				i80286_data_descriptor(cpustate, CS, word2);
				cpustate->pc=cpustate->base[CS]+word1;
				break;
			}
		}
	} else {
		cpustate->sregs[CS]=selector;
		cpustate->base[CS]=selector<<4;
		cpustate->pc=cpustate->base[CS]+offset;
	}
}

static void i80286_interrupt_descriptor(i80286_state *cpustate,UINT16 number)
{
	UINT16 word1,word2,word3;
	if ((number<<3)>=cpustate->idtr.limit) {
		;// go into shutdown mode
		return;
	}
	PREFIX(_pushf(cpustate));
	PUSH(cpustate->sregs[CS]);
	PUSH(cpustate->pc - cpustate->base[CS]);
	word1=ReadWord(cpustate->idtr.base+(number<<3));
	word2=ReadWord(cpustate->idtr.base+(number<<3)+2);
	word3=ReadWord(cpustate->idtr.base+(number<<3)+4);
	switch (word3&0xf00) {
	case 0x500: // task gate
		i80286_data_descriptor(cpustate, CS, word2);
		cpustate->pc=cpustate->base[CS]+word1;
		break;
	case 0x600: // interrupt gate
		cpustate->TF = cpustate->IF = 0;
		i80286_data_descriptor(cpustate, CS, word2);
		cpustate->pc=cpustate->base[CS]+word1;
		break;
	case 0x700: // trap gate
		i80286_data_descriptor(cpustate, CS, word2);
		cpustate->pc=cpustate->base[CS]+word1;
		break;
	}
}

static void PREFIX286(_0fpre)(i8086_state *cpustate)
{
	unsigned next = FETCHOP;
	UINT16 ModRM;
	UINT16 tmp;
	offs_t addr;

	switch (next) {
	case 0:
		ModRM=FETCHOP;
		switch (ModRM&0x38) {
		case 0: /* sldt */
			if (!PM) i80286_trap2(cpustate,ILLEGAL_INSTRUCTION);
			PutRMWord(ModRM, cpustate->ldtr.sel);
			break;
		case 8: /* str */
			if (!PM) i80286_trap2(cpustate,ILLEGAL_INSTRUCTION);
			PutRMWord(ModRM, cpustate->tr.sel);
			break;
		case 0x10: /* lldt */
			if (!PM) i80286_trap2(cpustate,ILLEGAL_INSTRUCTION);
			if (PM&&(CPL!=0)) i80286_trap2(cpustate,GENERAL_PROTECTION_FAULT);
			cpustate->ldtr.sel=GetRMWord(ModRM);
			if ((cpustate->ldtr.sel&~7)>=cpustate->gdtr.limit) i80286_trap2(cpustate,GENERAL_PROTECTION_FAULT);
			cpustate->ldtr.limit=ReadWord(cpustate->gdtr.base+(cpustate->ldtr.sel&~7));
			cpustate->ldtr.base=ReadWord(cpustate->gdtr.base+(cpustate->ldtr.sel&~7)+2)
				|(ReadWord(cpustate->gdtr.base+(cpustate->ldtr.sel&~7)+4)<<16);
			cpustate->ldtr.rights=cpustate->ldtr.base>>24;
			cpustate->ldtr.base&=0xffffff;
			break;
		case 0x18: /* ltr */
			if (!PM) i80286_trap2(cpustate,ILLEGAL_INSTRUCTION);
			if (CPL!=0) i80286_trap2(cpustate,GENERAL_PROTECTION_FAULT);
			cpustate->tr.sel=GetRMWord(ModRM);
			if ((cpustate->tr.sel&~7)>=cpustate->gdtr.limit) i80286_trap2(cpustate,GENERAL_PROTECTION_FAULT);
			cpustate->tr.limit=ReadWord(cpustate->gdtr.base+(cpustate->tr.sel&~7));
			cpustate->tr.base=ReadWord(cpustate->gdtr.base+(cpustate->tr.sel&~7)+2)
				|(ReadWord(cpustate->gdtr.base+(cpustate->tr.sel&~7)+4)<<16);
			cpustate->tr.rights=cpustate->tr.base>>24;
			cpustate->tr.base&=0xffffff;
			break;
		case 0x20: /* verr */
			if (!PM) i80286_trap2(cpustate,ILLEGAL_INSTRUCTION);
			tmp=GetRMWord(ModRM);
			if (tmp&4) {
				cpustate->ZeroVal=( ((tmp&~7)<cpustate->ldtr.limit)
							&& READABLE( ReadByte(cpustate->ldtr.base+(tmp&~7)+5)) );
			} else {
				cpustate->ZeroVal=( ((tmp&~7)<cpustate->gdtr.limit)
							&& READABLE( ReadByte(cpustate->gdtr.base+(tmp&~7)+5)) );
			}
			break;
		case 0x28: /* verw */
			if (!PM) i80286_trap2(cpustate,ILLEGAL_INSTRUCTION);
			tmp=GetRMWord(ModRM);
			if (tmp&4) {
				cpustate->ZeroVal=( ((tmp&~7)<cpustate->ldtr.limit)
							&& WRITEABLE( ReadByte(cpustate->ldtr.base+(tmp&~7)+5)) );
			} else {
				cpustate->ZeroVal=( ((tmp&~7)<cpustate->gdtr.limit)
							&& WRITEABLE( ReadByte(cpustate->gdtr.base+(tmp&~7)+5)) );
			}
			break;
		default:
			i80286_trap2(cpustate,ILLEGAL_INSTRUCTION);
			break;
		}
		break;
	case 1:
		/* lgdt, lldt in protected mode privilege level 0 required else common protection
           failure 0xd */
		ModRM = FETCHOP;
		switch (ModRM&0x38) {
		case 0: /* sgdt */
			PutRMWord(ModRM,cpustate->gdtr.limit);
			PutRMWordOffset(2,cpustate->gdtr.base&0xffff);
			PutRMByteOffset(4,cpustate->gdtr.base>>16);
			break;
		case 8: /* sidt */
			PutRMWord(ModRM,cpustate->idtr.limit);
			PutRMWordOffset(2,cpustate->idtr.base&0xffff);
			PutRMByteOffset(4,cpustate->idtr.base>>16);
			break;
		case 0x10: /* lgdt */
			if (PM&&(CPL!=0)) i80286_trap2(cpustate,GENERAL_PROTECTION_FAULT);
			cpustate->gdtr.limit=GetRMWord(ModRM);
			cpustate->gdtr.base=GetRMWordOffset(2)|(GetRMByteOffset(4)<<16);
			break;
		case 0x18: /* lidt */
			if (PM&&(CPL!=0)) i80286_trap2(cpustate,GENERAL_PROTECTION_FAULT);
			cpustate->idtr.limit=GetRMWord(ModRM);
			cpustate->idtr.base=GetRMWordOffset(2)|(GetRMByteOffset(4)<<16);
			break;
		case 0x20: /* smsw */
			PutRMWord(ModRM, cpustate->msw);
			break;
		case 0x30: /* lmsw */
			if (PM&&(CPL!=0)) i80286_trap2(cpustate,GENERAL_PROTECTION_FAULT);
			cpustate->msw=(cpustate->msw&1)|GetRMWord(ModRM);
			break;
		default:
			i80286_trap2(cpustate,ILLEGAL_INSTRUCTION);
			break;
		}
		break;
	case 2: /* LAR */
		ModRM = FETCHOP;
		tmp=GetRMWord(ModRM);
		cpustate->ZeroVal=i80286_selector_okay(cpustate,tmp);
		if (cpustate->ZeroVal) {
			RegWord(ModRM)=tmp;
		}
		break;
	case 3: /* LSL */
		if (!PM) i80286_trap2(cpustate,ILLEGAL_INSTRUCTION);
		ModRM = FETCHOP;
		tmp=GetRMWord(ModRM);
		cpustate->ZeroVal=i80286_selector_okay(cpustate,tmp);
		if (cpustate->ZeroVal) {
			addr=i80286_selector_to_address(cpustate,tmp);
			RegWord(ModRM)=ReadWord(addr);
		}
		break;
	case 6: /* clts */
		if (PM&&(CPL!=0)) i80286_trap2(cpustate,GENERAL_PROTECTION_FAULT);
		cpustate->msw=~8;
		break;
	default:
		i80286_trap2(cpustate,ILLEGAL_INSTRUCTION);
		break;
	}
}

static void PREFIX286(_arpl)(i8086_state *cpustate) /* 0x63 */
{
	if (PM) {
		UINT16 ModRM=FETCHOP, tmp=GetRMWord(ModRM);

		cpustate->ZeroVal=i80286_selector_okay(cpustate,RegWord(ModRM))
			  &&i80286_selector_okay(cpustate,RegWord(ModRM))
			  &&((tmp&3)<(RegWord(ModRM)&3));
		if (cpustate->ZeroVal) PutbackRMWord(ModRM, (tmp&~3)|(RegWord(ModRM)&3));
	} else {
		i80286_trap2(cpustate,ILLEGAL_INSTRUCTION);
	}
}