Previous 15994 Revisions Next

r11569 Tuesday 17th May, 2011 at 13:23:42 UTC by Miodrag Milanović
Credited in MAME, but for log :

i286 - Added 286 LOADALL and triple fault resets. [Carl]
i286 - Added support for 16-bit protected mode far calls and segment privilege checks. [Carl]
i86 - Fixed shift instructions, helps detecting proper CPU [Miodrag Milanovic]
i86 - Fixed handling of invalid instructions in 0x60-0x6f section [Miodrag Milanovic]
[src/emu/cpu/i86]i286.c i286.h i86.c i86priv.h instr186.c instr186.h instr286.c instr286.h instr86.c instr86.h table186.h table286.h table86.h

src/emu/cpu/i86/instr186.c
r11568r11569
131131   unsigned ModRM = FETCH;
132132   unsigned count = FETCH;
133133
134   PREFIX86(_rotate_shift_Byte)(cpustate,ModRM,count);
134   PREFIX86(_rotate_shift_Byte)(cpustate,ModRM,count & 0x1f);
135135}
136136
137137static void PREFIX186(_rotshft_wd8)(i8086_state *cpustate)    /* Opcode 0xc1 */
r11568r11569
139139   unsigned ModRM = FETCH;
140140   unsigned count = FETCH;
141141
142   PREFIX86(_rotate_shift_Word)(cpustate,ModRM,count);
142   PREFIX86(_rotate_shift_Word)(cpustate,ModRM,count & 0x1f);
143143}
144144
145145static void PREFIX186(_enter)(i8086_state *cpustate)    /* Opcode 0xc8 */
r11568r11569
163163   cpustate->regs.w[SP]=cpustate->regs.w[BP];
164164   POP(cpustate->regs.w[BP]);
165165}
166
167static void PREFIX186(_rotshft_bcl)(i8086_state *cpustate)    /* Opcode 0xd2 */
168{
169   PREFIX86(_rotate_shift_Byte)(cpustate,FETCHOP,cpustate->regs.b[CL] & 0x1f);
170}
171
172static void PREFIX186(_rotshft_wcl)(i8086_state *cpustate)    /* Opcode 0xd3 */
173{
174   PREFIX86(_rotate_shift_Word)(cpustate,FETCHOP,cpustate->regs.b[CL] & 0x1f);
175}
src/emu/cpu/i86/instr286.c
r11568r11569
2121#define IS_WRITEABLE(a)      ( ( (a) & 0xa ) == 2 )
2222#define IS_READABLE(a)      ( ( ( (a) & 0xa ) == 0xa ) || ( ( (a) & 8 ) == 0 ) )
2323
24static void i80286_trap2(i80286_state *cpustate,int number)
24#define LIMIT(desc)  (desc[0]&0xffff)
25#define BASE(desc)   ((desc[1]&0xffff)|((desc[2]&0xff)<<16))
26#define RIGHTS(desc) ((desc[2]>>8)&0xff)
27
28#define ACCESS(r)  (r&1)
29#define SET_ACC(desc) (desc[2]|=0x100)
30#define RW(r)      ((r>>1)&1)
31#define READ(r)    ((r>>1)&1)
32#define CONF(r)    ((r>>2)&1)
33#define EXPDOWN(r) ((r>>2)&1)
34#define CODE(r)    ((r>>3)&1)
35#define SEGDESC(r) ((r>>4)&1)
36#define DPL(r)     ((r>>5)&3)
37#define PRES(r)    ((r>>7)&1)
38#define GATE(r)    (r&31)
39#define GATESEL(desc) (desc[1])
40#define GATEOFF(desc) (desc[0])
41#define GATECNT(desc) (desc[2]&31)
42
43#define RPL(s)     (s&3)
44#define IDX(s)     (s&(~7))
45#define IDXTBL(s)  (s&(~3))
46#define TBL(s)     (s&4)
47
48#define TSSDESCIDLE 1
49#define LDTDESC    2
50#define TSSDESCBUSY 3
51#define CALLGATE   4
52#define TASKGATE   5
53#define INTGATE    6
54#define TRAPGATE   7
55
56#define TSS_BACK 0
57#define TSS_SP0  2
58#define TSS_SS0  4
59#define TSS_SP1  6
60#define TSS_SS1  8
61#define TSS_SP2  10
62#define TSS_SS2  12
63#define TSS_IP   14
64#define TSS_FLAG 16
65#define TSS_AX   18
66#define TSS_CX   20
67#define TSS_DX   22
68#define TSS_BX   24
69#define TSS_SP   26
70#define TSS_BP   28
71#define TSS_SI   30
72#define TSS_DI   32
73#define TSS_ES   34
74#define TSS_CS   36
75#define TSS_SS   38
76#define TSS_DS   40
77#define TSS_LDT  42
78
79#define PMAX(a,b) ((a<b)?b:a)
80
81// when a cpu reset happens on a AT the bios checks for 9 in byte 0xf
82// of the nvram.  if yes, after init, it sets the stack pointer to the value in 0040:0067
83// in the bios data segment then pops es and ds off that stack, does popa then a far ret.
84
85static void i80286_trap2(i80286_state *cpustate,UINT32 error)
2586{
26   i80286_interrupt(cpustate,number);
87   int error_code = error & 0xffff;
88   UINT16 number = error >> 16;
89   if(error_code == 0xffff) error_code = -1;
90   cpustate->pc = cpustate->prevpc;
91   try {
92      switch(number) {
93      case DIVIDE_BY_ZERO:
94      case INVALID_TSS:
95      case SEG_NOT_PRESENT:
96      case STACK_FAULT:
97      case GENERAL_PROTECTION_FAULT:
98         cpustate->trap_level++;
99         if(cpustate->trap_level == 2) throw TRAP(DOUBLE_FAULT,0);
100         if(cpustate->trap_level == 3) break;
101         i80286_interrupt_descriptor(cpustate,number,1,error_code);
102         break;
103      case DOUBLE_FAULT:
104         i80286_interrupt_descriptor(cpustate,number,1,0);
105         break;
106      default:
107         i80286_interrupt_descriptor(cpustate,number,1,-1);
108      }
109   } catch(UINT32 e) { i80286_trap2(cpustate, e); }
110   if(cpustate->trap_level == 3)
111      // this is supposed to triggered by support hardware
112      // create a shutdown output line that causes a reset
113      // NMI can wake processor without reset
114      device_set_input_line(cpustate->device, INPUT_LINE_RESET, PULSE_LINE);
115   cpustate->trap_level = 0;
27116}
28117
29118static int i80286_selector_okay(i80286_state *cpustate,UINT16 selector)
r11568r11569
44133   }
45134}
46135
47static void i80286_data_descriptor(i80286_state *cpustate,int reg, UINT16 selector)
136static void i80286_pop_seg(i80286_state *cpustate, int reg) {
137   UINT16 sel;
138   sel = ReadWord(cpustate->base[SS]+cpustate->regs.w[SP]);
139   i80286_data_descriptor(cpustate, reg, sel, CPL);
140   cpustate->regs.w[SP] += 2;
141}
142
143static void i80286_data_descriptor(i80286_state *cpustate,int reg, UINT16 selector, int cpl)
48144{
49145   if (PM) {
50      UINT16 help;
146      UINT16 desc[3];
147      UINT8 r;
148      int addr;
51149      /* selector format
52150           15..3 number/address in descriptor table
53151           2: 0 global, 1 local descriptor table
54152           1,0: requested privileg level
55153           must be higher or same as current privileg level in code selector */
56      if (selector&4) { /* local descriptor table */
57         if (selector>cpustate->ldtr.limit) i80286_trap2(cpustate,GENERAL_PROTECTION_FAULT);
58         cpustate->sregs[reg]=selector;
59         cpustate->limit[reg]=ReadWord(cpustate->ldtr.base+(selector&~7));
60         cpustate->base[reg]=ReadWord(cpustate->ldtr.base+(selector&~7)+2)
61            |(ReadWord(cpustate->ldtr.base+(selector&~7)+4)<<16);
62         cpustate->rights[reg]=cpustate->base[reg]>>24;
63         cpustate->base[reg]&=0xffffff;
154      if ((reg != SS) && !IDXTBL(selector)) {
155         cpustate->sregs[reg]=0;
156         cpustate->limit[reg]=0;
157         cpustate->base[reg]=0;
158         cpustate->rights[reg]=0;
159         return;
160      }
161
162      if (TBL(selector)) { /* local descriptor table */
163         if (selector>cpustate->ldtr.limit) throw TRAP(GENERAL_PROTECTION_FAULT,IDXTBL(selector));
164         addr = cpustate->ldtr.base+IDX(selector);
64165      } else { /* global descriptor table */
65         if (!(selector&~7)||(selector>cpustate->gdtr.limit)) i80286_trap2(cpustate,GENERAL_PROTECTION_FAULT);
66         cpustate->sregs[reg]=selector;
67         cpustate->limit[reg]=ReadWord(cpustate->gdtr.base+(selector&~7));
68         cpustate->base[reg]=ReadWord(cpustate->gdtr.base+(selector&~7)+2);
69         help=ReadWord(cpustate->gdtr.base+(selector&~7)+4);
70         cpustate->rights[reg]=help>>8;
71         cpustate->base[reg]|=(help&0xff)<<16;
166         if (selector>cpustate->gdtr.limit) throw TRAP(GENERAL_PROTECTION_FAULT,IDXTBL(selector));
167         addr = cpustate->gdtr.base+IDX(selector);
72168      }
169
170      desc[0] = ReadWord(addr);
171      desc[1] = ReadWord(addr+2);
172      desc[2] = ReadWord(addr+4);
173      r = RIGHTS(desc);
174      if (!SEGDESC(r)) throw TRAP(GENERAL_PROTECTION_FAULT,IDXTBL(selector));
175      if (reg == SS) {
176         if (!IDXTBL(selector)) throw TRAP(GENERAL_PROTECTION_FAULT,0);
177         if (DPL(r)!=cpl) throw TRAP(GENERAL_PROTECTION_FAULT,IDXTBL(selector));
178         if (RPL(selector)!=cpl) throw TRAP(GENERAL_PROTECTION_FAULT,IDXTBL(selector));
179         if (!RW(r) || CODE(r)) throw TRAP(GENERAL_PROTECTION_FAULT,IDXTBL(selector));
180         if (!PRES(r)) throw TRAP(STACK_FAULT,IDXTBL(selector));
181      } else {
182         if (DPL(r) < PMAX(cpl,RPL(selector))) throw TRAP(GENERAL_PROTECTION_FAULT,IDXTBL(selector));
183         if (CODE(r) && !READ(r)) throw TRAP(GENERAL_PROTECTION_FAULT,IDXTBL(selector));
184      }
185
186      SET_ACC(desc);
187      WriteWord(addr+4, desc[2]);
188      cpustate->sregs[reg]=selector;
189      cpustate->limit[reg]=LIMIT(desc);
190      cpustate->base[reg]=BASE(desc);
191      cpustate->rights[reg]=RIGHTS(desc);
73192   } else {
74193      cpustate->sregs[reg]=selector;
75194      cpustate->base[reg]=selector<<4;
76195   }
77196}
78197
79static void i80286_code_descriptor(i80286_state *cpustate,UINT16 selector, UINT16 offset)
198static void i80286_code_descriptor(i80286_state *cpustate,UINT16 selector, UINT16 offset, int gate)
80199{
81   UINT16 word1, word2, word3;
82200   if (PM) {
201      UINT16 desc[3];
202      UINT8 r;
203      int addr;
83204      /* selector format
84205           15..3 number/address in descriptor table
85206           2: 0 global, 1 local descriptor table
86207           1,0: requested privileg level
87208           must be higher or same as current privileg level in code selector */
88      if (selector&4) { /* local descriptor table */
89         if (selector>cpustate->ldtr.limit) i80286_trap2(cpustate,GENERAL_PROTECTION_FAULT);
90         word1=ReadWord(cpustate->ldtr.base+(selector&~7));
91         word2=ReadWord(cpustate->ldtr.base+(selector&~7)+2);
92         word3=ReadWord(cpustate->ldtr.base+(selector&~7)+4);
209      if (TBL(selector)) { /* local descriptor table */
210         if (selector>cpustate->ldtr.limit) throw TRAP(GENERAL_PROTECTION_FAULT,IDXTBL(selector));
211         addr = cpustate->ldtr.base+IDX(selector);
93212      } else { /* global descriptor table */
94         if (!(selector&~7)||(selector>cpustate->gdtr.limit)) i80286_trap2(cpustate,GENERAL_PROTECTION_FAULT);
95         word1=ReadWord(cpustate->gdtr.base+(selector&~7));
96         word2=ReadWord(cpustate->gdtr.base+(selector&~7)+2);
97         word3=ReadWord(cpustate->gdtr.base+(selector&~7)+4);
213         if (!IDX(selector)||(selector>cpustate->gdtr.limit)) throw TRAP(GENERAL_PROTECTION_FAULT,IDXTBL(selector));
214         addr = cpustate->gdtr.base+IDX(selector);
98215      }
99      if (word3&0x1000) {
100         cpustate->sregs[CS]=selector;
101         cpustate->limit[CS]=word1;
102         cpustate->base[CS]=word2|((word3&0xff)<<16);
103         cpustate->rights[CS]=word3>>8;
216      desc[0] = ReadWord(addr);
217      desc[1] = ReadWord(addr+2);
218      desc[2] = ReadWord(addr+4);
219      r = RIGHTS(desc);
220
221      if (SEGDESC(r)) {
222         if (!CODE(r)) throw TRAP(GENERAL_PROTECTION_FAULT,IDXTBL(selector));
223         if (CONF(r)) { if(DPL(r)>CPL) throw TRAP(GENERAL_PROTECTION_FAULT,IDXTBL(selector)); }
224         else if ((RPL(selector)>CPL) || (DPL(r)!=CPL)) throw TRAP(GENERAL_PROTECTION_FAULT,IDXTBL(selector));
225         
226         SET_ACC(desc);
227         WriteWord(addr+4, desc[2]);
228         if (!PRES(r)) throw TRAP(SEG_NOT_PRESENT,IDXTBL(selector));  // this order is important
229         if (offset > LIMIT(desc)) throw TRAP(GENERAL_PROTECTION_FAULT, 0);
230         cpustate->sregs[CS]=IDXTBL(selector) | CPL;
231         cpustate->limit[CS]=LIMIT(desc);
232         cpustate->base[CS]=BASE(desc);
233         cpustate->rights[CS]=RIGHTS(desc);
104234         cpustate->pc=cpustate->base[CS]+offset;
105235      } else { // systemdescriptor
106         switch (word3&0xf00) {
107         case 0x400: // call gate
108            // word3&0x1f words to be copied from stack to stack
109            i80286_data_descriptor(cpustate, CS, word2);
110            cpustate->pc=cpustate->base[CS]+word1;
236         UINT16 gatedesc[3];
237         UINT16 gatesel = GATESEL(desc);
238         if (!gate) throw TRAP(GENERAL_PROTECTION_FAULT,IDXTBL(selector)); // can't ret through gate
239         if (DPL(r) < PMAX(CPL,RPL(selector))) throw TRAP(GENERAL_PROTECTION_FAULT,IDXTBL(selector));
240         if (!PRES(r)) throw TRAP(SEG_NOT_PRESENT, IDXTBL(selector));
241
242         switch (GATE(r)) {
243         case CALLGATE:
244            if (TBL(gatesel)) { /* local descriptor table */
245               if (gatesel>cpustate->ldtr.limit) throw TRAP(GENERAL_PROTECTION_FAULT,IDXTBL(gatesel));
246               addr = cpustate->ldtr.base+IDX(gatesel);
247            } else { /* global descriptor table */
248               if (!IDX(gatesel)||(gatesel>cpustate->gdtr.limit)) throw TRAP(GENERAL_PROTECTION_FAULT,IDXTBL(gatesel));
249               addr = cpustate->gdtr.base+IDX(gatesel);
250            }
251            addr = cpustate->gdtr.base+IDX(gatesel);
252            gatedesc[0] = ReadWord(addr);
253            gatedesc[1] = ReadWord(addr+2);
254            gatedesc[2] = ReadWord(addr+4);
255            r = RIGHTS(gatedesc);
256            if (!CODE(r)) throw TRAP(GENERAL_PROTECTION_FAULT,IDXTBL(gatesel));
257            if (DPL(r)>CPL) throw TRAP(GENERAL_PROTECTION_FAULT,IDXTBL(gatesel));
258            if (!PRES(r)) throw TRAP(SEG_NOT_PRESENT,IDXTBL(gatesel));
259            if (GATEOFF(desc) > LIMIT(gatedesc)) throw TRAP(GENERAL_PROTECTION_FAULT,IDXTBL(gatesel));
260
261            if (!CONF(r)&&(DPL(r)<CPL)) {  // inner call
262               UINT16 ssdesc[3];
263               UINT16 tss_ss,tss_sp, oldss, oldsp;
264               int oldsp_phy, ssr, i;
265               if(gate == 1) throw TRAP(GENERAL_PROTECTION_FAULT,IDXTBL(gatesel)); // can't jmp to inner
266               tss_ss = ReadWord(cpustate->tr.base+TSS_SS0+(DPL(r)*4));
267               tss_sp = ReadWord(cpustate->tr.base+TSS_SP0+(DPL(r)*4));
268
269               if (!IDXTBL(tss_ss)) throw TRAP(INVALID_TSS, 0);
270               if (TBL(tss_ss)) { /* local descriptor table */
271                  if (tss_ss>cpustate->ldtr.limit) throw TRAP(INVALID_TSS,IDXTBL(tss_ss));
272                  addr = cpustate->ldtr.base+IDX(tss_ss);
273               } else { /* global descriptor table */
274                  if (tss_ss>cpustate->gdtr.limit) throw TRAP(INVALID_TSS,IDXTBL(tss_ss));
275                  addr = cpustate->gdtr.base+IDX(tss_ss);
276               }
277               ssdesc[0] = ReadWord(addr);
278               ssdesc[1] = ReadWord(addr+2);
279               ssdesc[2] = ReadWord(addr+4);
280               ssr = RIGHTS(ssdesc);
281               if ((RPL(tss_ss) != DPL(r)) || DPL(ssr) != DPL(r)) TRAP(INVALID_TSS,IDXTBL(tss_ss));
282               if (!RW(ssr) || !PRES(ssr)) throw TRAP(INVALID_TSS,IDXTBL(tss_ss));
283               oldss = cpustate->sregs[SS];
284               oldsp = cpustate->regs.w[SP];
285               oldsp_phy = cpustate->base[SS]+oldsp+(GATECNT(desc)*2);
286               cpustate->sregs[SS]=tss_ss;
287               cpustate->limit[SS]=LIMIT(ssdesc);
288               cpustate->base[SS]=BASE(ssdesc);
289               cpustate->rights[SS]=RIGHTS(ssdesc);
290               PUSH(oldss);
291               PUSH(oldsp);
292               for (i = 0; i < GATECNT(desc); i++)
293                  PUSH(ReadWord(oldsp_phy-(i*2)));
294            }
295            cpustate->sregs[CS]=IDXTBL(gatesel) | DPL(r);
296            cpustate->limit[CS]=LIMIT(gatedesc);
297            cpustate->base[CS]=BASE(gatedesc);
298            cpustate->rights[CS]=RIGHTS(gatedesc);
299            cpustate->pc=(cpustate->base[CS]+GATEOFF(desc))&AMASK;
111300            break;
112         case 0x500: // task gate
113            i80286_data_descriptor(cpustate, CS, word2);
114            cpustate->pc=cpustate->base[CS]+word1;
301         case TASKGATE:
302            i80286_data_descriptor(cpustate, CS, desc[1], CPL);
303            cpustate->pc=cpustate->base[CS]+desc[0];
115304            break;
116         case 0x600: // interrupt gate
117            cpustate->TF = cpustate->IF = 0;
118            i80286_data_descriptor(cpustate, CS, word2);
119            cpustate->pc=cpustate->base[CS]+word1;
120            break;
121         case 0x700: // trap gate
122            i80286_data_descriptor(cpustate, CS, word2);
123            cpustate->pc=cpustate->base[CS]+word1;
124            break;
305         default:
306            throw TRAP(GENERAL_PROTECTION_FAULT,IDXTBL(selector));
125307         }
126308      }
127309   } else {
128310      cpustate->sregs[CS]=selector;
129311      cpustate->base[CS]=selector<<4;
130      cpustate->pc=cpustate->base[CS]+offset;
312      cpustate->rights[CS]=0x9a;
313      cpustate->limit[CS]=0xffff;
314      cpustate->pc=(cpustate->base[CS]+offset)&AMASK;
131315   }
132316}
133317
134static void i80286_interrupt_descriptor(i80286_state *cpustate,UINT16 number)
318static void i80286_interrupt_descriptor(i80286_state *cpustate,UINT16 number, int hwint, int error)
135319{
136   UINT16 word1,word2,word3;
137   if ((number<<3)>=cpustate->idtr.limit) {
138      ;// go into shutdown mode
139      return;
140   }
141   PREFIX(_pushf(cpustate));
142   PUSH(cpustate->sregs[CS]);
143   PUSH(cpustate->pc - cpustate->base[CS]);
144   word1=ReadWord(cpustate->idtr.base+(number<<3));
145   word2=ReadWord(cpustate->idtr.base+(number<<3)+2);
146   word3=ReadWord(cpustate->idtr.base+(number<<3)+4);
147   switch (word3&0xf00) {
148   case 0x500: // task gate
149      i80286_data_descriptor(cpustate, CS, word2);
150      cpustate->pc=cpustate->base[CS]+word1;
320   UINT16 desc[3], gatedesc[3], gatesel;
321   int r, addr;
322
323   if(!PM) return PREFIX86(_interrupt)(cpustate, number);
324
325   if ((number<<3)>=cpustate->idtr.limit)
326      throw TRAP(GENERAL_PROTECTION_FAULT,(number*8+2+(hwint&&1)));
327
328   desc[0] = ReadWord(cpustate->idtr.base+(number<<3));
329   desc[1] = ReadWord(cpustate->idtr.base+(number<<3)+2);
330   desc[2] = ReadWord(cpustate->idtr.base+(number<<3)+4);
331   r = RIGHTS(desc);
332   if (!hwint && (DPL(r)<CPL)) throw TRAP(GENERAL_PROTECTION_FAULT,(number*8+2+(hwint&&1)));
333   if (!PRES(r)) throw TRAP(SEG_NOT_PRESENT,(number*8+2+(hwint&&1)));
334
335   switch (GATE(r)) {
336   case TASKGATE:
337      i80286_data_descriptor(cpustate, CS, desc[1], CPL);
338      cpustate->pc=cpustate->base[CS]+desc[0];
151339      break;
152   case 0x600: // interrupt gate
153      cpustate->TF = cpustate->IF = 0;
154      i80286_data_descriptor(cpustate, CS, word2);
155      cpustate->pc=cpustate->base[CS]+word1;
340   case INTGATE:
341   case TRAPGATE:
342      gatesel = GATESEL(desc);
343      if (TBL(gatesel)) { /* local descriptor table */
344         if (gatesel>cpustate->ldtr.limit) throw TRAP(GENERAL_PROTECTION_FAULT,(IDXTBL(gatesel)+(hwint&&1)));
345         addr = cpustate->ldtr.base+IDX(gatesel);
346      } else { /* global descriptor table */
347         if (!IDX(gatesel)||(gatesel>cpustate->gdtr.limit)) throw TRAP(GENERAL_PROTECTION_FAULT,(IDXTBL(gatesel)+(hwint&&1)));
348         addr = cpustate->gdtr.base+IDX(gatesel);
349      }
350      gatedesc[0] = ReadWord(addr);
351      gatedesc[1] = ReadWord(addr+2);
352      gatedesc[2] = ReadWord(addr+4);
353      r = RIGHTS(gatedesc);
354      if (!CODE(r)) throw TRAP(GENERAL_PROTECTION_FAULT,(IDXTBL(gatesel)+(hwint&&1)));
355      if (DPL(r)>CPL) throw TRAP(GENERAL_PROTECTION_FAULT,(IDXTBL(gatesel)+(hwint&&1)));
356      if (!PRES(r)) throw TRAP(SEG_NOT_PRESENT,(IDXTBL(gatesel)+(hwint&&1)));
357      if (GATEOFF(desc) > LIMIT(gatedesc)) throw TRAP(GENERAL_PROTECTION_FAULT,(IDXTBL(gatesel)+(hwint&&1)));
358
359      if (!CONF(r)&&(DPL(r)<CPL)) {  // inner call
360         UINT16 ssdesc[3];
361         UINT16 tss_ss,tss_sp, oldss, oldsp;
362         int ssr;
363         tss_ss = ReadWord(cpustate->tr.base+TSS_SS0+(DPL(r)*4));
364         tss_sp = ReadWord(cpustate->tr.base+TSS_SP0+(DPL(r)*4));
365
366         if (!IDXTBL(tss_ss)) throw TRAP(INVALID_TSS, (hwint&&1));
367         if (TBL(tss_ss)) { /* local descriptor table */
368            if (tss_ss>cpustate->ldtr.limit) throw TRAP(INVALID_TSS,(IDXTBL(tss_ss)+(hwint&&1)));
369            addr = cpustate->ldtr.base+IDX(tss_ss);
370         } else { /* global descriptor table */
371            if (tss_ss>cpustate->gdtr.limit) throw TRAP(INVALID_TSS,(IDXTBL(tss_ss)+(hwint&&1)));
372            addr = cpustate->gdtr.base+IDX(tss_ss);
373         }
374         ssdesc[0] = ReadWord(addr);
375         ssdesc[1] = ReadWord(addr+2);
376         ssdesc[2] = ReadWord(addr+4);
377         ssr = RIGHTS(ssdesc);
378         if ((RPL(tss_ss) != DPL(r)) || (DPL(ssr) != DPL(r))) TRAP(INVALID_TSS,(IDXTBL(tss_ss)+(hwint&&1)));
379         if (!SEGDESC(ssr) || CODE(ssr) || !RW(ssr)) throw TRAP(INVALID_TSS,(IDXTBL(tss_ss)+(hwint&&1)));
380         if (!PRES(ssr)) throw TRAP(STACK_FAULT,(IDXTBL(tss_ss)+(hwint&&1)));
381         oldss = cpustate->sregs[SS];
382         oldsp = cpustate->regs.w[SP];
383         cpustate->sregs[SS]=tss_ss;
384         cpustate->limit[SS]=LIMIT(ssdesc);
385         cpustate->base[SS]=BASE(ssdesc);
386         cpustate->rights[SS]=RIGHTS(ssdesc);
387         PUSH(oldss);
388         PUSH(oldsp);
389      }
390      PREFIX(_pushf(cpustate));
391      PUSH(cpustate->sregs[CS]);
392      PUSH(cpustate->pc-cpustate->base[CS]);
393      if((hwint == 1) && (error != -1)) PUSH(error);
394      cpustate->sregs[CS]=IDXTBL(gatesel) | DPL(r);
395      cpustate->limit[CS]=LIMIT(gatedesc);
396      cpustate->base[CS]=BASE(gatedesc);
397      cpustate->rights[CS]=RIGHTS(gatedesc);
398      cpustate->pc=(cpustate->base[CS]+GATEOFF(desc))&AMASK;
399      cpustate->TF = 0;
400      if (GATE(RIGHTS(desc)) == INTGATE) cpustate->IF = 0;
401      CHANGE_PC(cpustate->pc);
156402      break;
157   case 0x700: // trap gate
158      i80286_data_descriptor(cpustate, CS, word2);
159      cpustate->pc=cpustate->base[CS]+word1;
160      break;
403   default:
404      throw TRAP(GENERAL_PROTECTION_FAULT,(number*8+2+(hwint&&1)));
161405   }
162406}
163407
r11568r11569
167411   UINT16 ModRM;
168412   UINT16 tmp;
169413   offs_t addr;
414   int msw, sel, addr2, r;
415   UINT16 desc[2];
170416
171417   switch (next) {
172418   case 0:
419      if (!PM) throw TRAP(ILLEGAL_INSTRUCTION,-1);
173420      ModRM=FETCHOP;
174421      switch (ModRM&0x38) {
175422      case 0: /* sldt */
176         if (!PM) i80286_trap2(cpustate,ILLEGAL_INSTRUCTION);
177423         PutRMWord(ModRM, cpustate->ldtr.sel);
178424         break;
179425      case 8: /* str */
180         if (!PM) i80286_trap2(cpustate,ILLEGAL_INSTRUCTION);
181426         PutRMWord(ModRM, cpustate->tr.sel);
182427         break;
183428      case 0x10: /* lldt */
184         if (!PM) i80286_trap2(cpustate,ILLEGAL_INSTRUCTION);
185         if (PM&&(CPL!=0)) i80286_trap2(cpustate,GENERAL_PROTECTION_FAULT);
186         cpustate->ldtr.sel=GetRMWord(ModRM);
187         if ((cpustate->ldtr.sel&~7)>=cpustate->gdtr.limit) i80286_trap2(cpustate,GENERAL_PROTECTION_FAULT);
188         cpustate->ldtr.limit=ReadWord(cpustate->gdtr.base+(cpustate->ldtr.sel&~7));
189         cpustate->ldtr.base=ReadWord(cpustate->gdtr.base+(cpustate->ldtr.sel&~7)+2)
190            |(ReadWord(cpustate->gdtr.base+(cpustate->ldtr.sel&~7)+4)<<16);
191         cpustate->ldtr.rights=cpustate->ldtr.base>>24;
192         cpustate->ldtr.base&=0xffffff;
429         if (CPL!=0) throw TRAP(GENERAL_PROTECTION_FAULT,0);
430         sel=GetRMWord(ModRM);
431         if (IDXTBL(sel)) {
432            if (IDX(sel)>=cpustate->gdtr.limit) throw TRAP(GENERAL_PROTECTION_FAULT,IDXTBL(sel));
433            addr2 = cpustate->gdtr.base + IDX(sel);
434            desc[0] = ReadWord(addr2);
435            desc[1] = ReadWord(addr2+2);
436            desc[2] = ReadWord(addr2+4);
437            r = RIGHTS(desc);
438            if (GATE(r) != LDTDESC) throw TRAP(GENERAL_PROTECTION_FAULT,IDXTBL(sel));
439            if (!PRES(r)) TRAP(SEG_NOT_PRESENT,IDXTBL(sel));
440         } else {
441            desc[0] = 0;
442            desc[1] = 0;
443            desc[2] = 0;
444         }
445         cpustate->ldtr.sel=sel;
446         cpustate->ldtr.limit=LIMIT(desc);
447         cpustate->ldtr.base=BASE(desc);
448         cpustate->ldtr.rights=RIGHTS(desc);
193449         break;
194450      case 0x18: /* ltr */
195         if (!PM) i80286_trap2(cpustate,ILLEGAL_INSTRUCTION);
196         if (CPL!=0) i80286_trap2(cpustate,GENERAL_PROTECTION_FAULT);
197         cpustate->tr.sel=GetRMWord(ModRM);
198         if ((cpustate->tr.sel&~7)>=cpustate->gdtr.limit) i80286_trap2(cpustate,GENERAL_PROTECTION_FAULT);
199         cpustate->tr.limit=ReadWord(cpustate->gdtr.base+(cpustate->tr.sel&~7));
200         cpustate->tr.base=ReadWord(cpustate->gdtr.base+(cpustate->tr.sel&~7)+2)
201            |(ReadWord(cpustate->gdtr.base+(cpustate->tr.sel&~7)+4)<<16);
202         cpustate->tr.rights=cpustate->tr.base>>24;
203         cpustate->tr.base&=0xffffff;
451         if (CPL!=0) throw TRAP(GENERAL_PROTECTION_FAULT,0);
452         sel=GetRMWord(ModRM);
453         if (IDX(sel)>=cpustate->gdtr.limit) throw TRAP(GENERAL_PROTECTION_FAULT,IDXTBL(sel));
454         addr2 = cpustate->gdtr.base + IDX(sel);
455         desc[0] = ReadWord(addr2);
456         desc[1] = ReadWord(addr2+2);
457         desc[2] = ReadWord(addr2+4);
458         r = RIGHTS(desc);
459         if (GATE(r) != TSSDESCIDLE) throw TRAP(GENERAL_PROTECTION_FAULT,IDXTBL(sel));
460         if (!PRES(r)) TRAP(SEG_NOT_PRESENT,IDXTBL(sel));
461         cpustate->tr.sel=sel;
462         cpustate->tr.limit=LIMIT(desc);
463         cpustate->tr.base=BASE(desc);
464         cpustate->tr.rights=RIGHTS(desc);
204465         break;
205466      case 0x20: /* verr */
206         if (!PM) i80286_trap2(cpustate,ILLEGAL_INSTRUCTION);
207467         tmp=GetRMWord(ModRM);
208         if (tmp&4) {
209            cpustate->ZeroVal=! ( ((tmp&~7)<cpustate->ldtr.limit)
210                     && IS_READABLE( ReadByte(cpustate->ldtr.base+(tmp&~7)+5)) );
211         } else {
212            cpustate->ZeroVal=! ( ((tmp&~7)<cpustate->gdtr.limit)
213                     && IS_READABLE( ReadByte(cpustate->gdtr.base+(tmp&~7)+5)) );
468         if (!tmp)
469            cpustate->ZeroVal=0;
470         else if (tmp>((TBL(tmp)?cpustate->ldtr.limit:cpustate->gdtr.limit)))
471            cpustate->ZeroVal=0;
472         else {
473            addr2 = (TBL(tmp)?cpustate->ldtr.base:cpustate->gdtr.base);
474            desc[0] = ReadWord(addr2);
475            desc[1] = ReadWord(addr2+2);
476            desc[2] = ReadWord(addr2+4);
477            r = RIGHTS(desc);
478            if (!SEGDESC(r))
479               cpustate->ZeroVal=0;
480            else if (CODE(r) && CONF(r))
481               cpustate->ZeroVal=1;
482            else if ((DPL(r)<CPL) || (DPL(r)<RPL(tmp)))
483               cpustate->ZeroVal=0;
484            else    cpustate->ZeroVal=1;
214485         }
215486         break;
216487      case 0x28: /* verw */
217         if (!PM) i80286_trap2(cpustate,ILLEGAL_INSTRUCTION);
218488         tmp=GetRMWord(ModRM);
219         if (tmp&4) {
220            cpustate->ZeroVal=! ( ((tmp&~7)<cpustate->ldtr.limit)
221                     && IS_WRITEABLE( ReadByte(cpustate->ldtr.base+(tmp&~7)+5)) );
222         } else {
223            cpustate->ZeroVal=! ( ((tmp&~7)<cpustate->gdtr.limit)
224                     && IS_WRITEABLE( ReadByte(cpustate->gdtr.base+(tmp&~7)+5)) );
489         if (!tmp)
490            cpustate->ZeroVal=0;
491         else if (tmp>((TBL(tmp)?cpustate->ldtr.limit:cpustate->gdtr.limit)))
492            cpustate->ZeroVal=0;
493         else {
494            addr2 = (TBL(tmp)?cpustate->ldtr.base:cpustate->gdtr.base);
495            desc[0] = ReadWord(addr2);
496            desc[1] = ReadWord(addr2+2);
497            desc[2] = ReadWord(addr2+4);
498            r = RIGHTS(desc);
499            if (!SEGDESC(r) || CODE(r))
500               cpustate->ZeroVal=0;
501            else if ((DPL(r)<CPL) || (DPL(r)<RPL(tmp)))
502               cpustate->ZeroVal=0;
503            else    cpustate->ZeroVal=1;
225504         }
226505         break;
227506      default:
228         i80286_trap2(cpustate,ILLEGAL_INSTRUCTION);
507         throw TRAP(ILLEGAL_INSTRUCTION,-1);
229508         break;
230509      }
231510      break;
r11568r11569
237516      case 0: /* sgdt */
238517         PutRMWord(ModRM,cpustate->gdtr.limit);
239518         PutRMWordOffset(2,cpustate->gdtr.base&0xffff);
240         PutRMByteOffset(4,cpustate->gdtr.base>>16);
519         PutRMWordOffset(4,0xff00|cpustate->gdtr.base>>16);
241520         break;
242521      case 8: /* sidt */
243522         PutRMWord(ModRM,cpustate->idtr.limit);
244523         PutRMWordOffset(2,cpustate->idtr.base&0xffff);
245         PutRMByteOffset(4,cpustate->idtr.base>>16);
524         PutRMWordOffset(4,0xff00|cpustate->idtr.base>>16);
246525         break;
247526      case 0x10: /* lgdt */
248         if (PM&&(CPL!=0)) i80286_trap2(cpustate,GENERAL_PROTECTION_FAULT);
527         if (PM&&(CPL!=0)) throw TRAP(GENERAL_PROTECTION_FAULT,0);
249528         cpustate->gdtr.limit=GetRMWord(ModRM);
250529         cpustate->gdtr.base=GetRMWordOffset(2)|(GetRMByteOffset(4)<<16);
251530         break;
252531      case 0x18: /* lidt */
253         if (PM&&(CPL!=0)) i80286_trap2(cpustate,GENERAL_PROTECTION_FAULT);
532         if (PM&&(CPL!=0)) throw TRAP(GENERAL_PROTECTION_FAULT,0);
254533         cpustate->idtr.limit=GetRMWord(ModRM);
255534         cpustate->idtr.base=GetRMWordOffset(2)|(GetRMByteOffset(4)<<16);
256535         break;
r11568r11569
258537         PutRMWord(ModRM, cpustate->msw);
259538         break;
260539      case 0x30: /* lmsw */
261         if (PM&&(CPL!=0)) i80286_trap2(cpustate,GENERAL_PROTECTION_FAULT);
262         cpustate->msw=(cpustate->msw&1)|GetRMWord(ModRM);
540         if (PM&&(CPL!=0)) throw TRAP(GENERAL_PROTECTION_FAULT,0);
541         msw = GetRMWord(ModRM);
542         if (!PM&(msw&1)) cpustate->sregs[CS] = IDX(cpustate->sregs[CS]); // cheat and set cpl to 0
543         cpustate->msw=(cpustate->msw&1)|msw;
263544         break;
264545      default:
265         i80286_trap2(cpustate,ILLEGAL_INSTRUCTION);
546         throw TRAP(ILLEGAL_INSTRUCTION,-1);
266547         break;
267548      }
268549      break;
r11568r11569
280561      }
281562      break;
282563   case 3: /* LSL */
283      if (!PM) i80286_trap2(cpustate,ILLEGAL_INSTRUCTION);
564      if (!PM) throw TRAP(ILLEGAL_INSTRUCTION,-1);
284565      ModRM = FETCHOP;
285566      tmp=GetRMWord(ModRM);
286567      if ( i80286_selector_okay(cpustate,tmp) )
r11568r11569
294575         cpustate->ZeroVal = 1;
295576      }
296577      break;
578   case 5: /* loadall */
579      if (PM&&(CPL!=0)) throw TRAP(GENERAL_PROTECTION_FAULT,0);
580      cpustate->msw =        (cpustate->msw&1)|ReadWord(0x806);
581      cpustate->tr.sel =     ReadWord(0x816);
582      cpustate->flags =      ReadWord(0x818);
583      ExpandFlags(cpustate->flags);
584      cpustate->pc =         ReadWord(0x81a);
585      cpustate->ldtr.sel =   ReadWord(0x81c);
586      cpustate->sregs[DS] =  ReadWord(0x81e);
587      cpustate->sregs[SS] =  ReadWord(0x820);
588      cpustate->sregs[CS] =  ReadWord(0x822);
589      cpustate->sregs[ES] =  ReadWord(0x824);
590      cpustate->regs.w[DI] = ReadWord(0x826);
591      cpustate->regs.w[SI] = ReadWord(0x828);
592      cpustate->regs.w[BP] = ReadWord(0x82a);
593      cpustate->regs.w[SP] = ReadWord(0x82c);
594      cpustate->regs.w[BX] = ReadWord(0x82e);
595      cpustate->regs.w[DX] = ReadWord(0x830);
596      cpustate->regs.w[CX] = ReadWord(0x832);
597      cpustate->regs.w[AX] = ReadWord(0x834);
598      UINT16 desc[3];
599// loadall uses base-rights-limit order
600#define LOADDESC(addr, sreg) {  desc[1] = ReadWord(addr); desc[2] = ReadWord(addr+2); desc[0] = ReadWord(addr+4); \
601            cpustate->base[sreg] = BASE(desc); cpustate->rights[sreg] = RIGHTS(desc); \
602            cpustate->limit[sreg] = LIMIT(desc); }
603      LOADDESC(0x836, ES);
604      LOADDESC(0x83C, CS);
605      LOADDESC(0x842, SS);
606      LOADDESC(0x848, DS);
607#undef LOADDESC
608// void cast supresses warning
609#define LOADDESC(addr, reg, r) { desc[1] = ReadWord(addr); desc[2] = ReadWord(addr+2); desc[0] = ReadWord(addr+4); \
610            cpustate->reg.base = BASE(desc); (void)(r); cpustate->reg.limit = LIMIT(desc); }
611      LOADDESC(0x84e, gdtr, 1);
612      LOADDESC(0x854, ldtr, cpustate->ldtr.rights = RIGHTS(desc));
613      LOADDESC(0x85a, idtr, 1);
614      LOADDESC(0x860, tr,  cpustate->tr.rights = RIGHTS(desc));
615#undef LOADDESC
616      cpustate->pc = (cpustate->pc + cpustate->base[CS]) & AMASK;
617      CHANGE_PC(cpustate->pc);
618      break;
619
297620   case 6: /* clts */
298      if (PM&&(CPL!=0)) i80286_trap2(cpustate,GENERAL_PROTECTION_FAULT);
621      if (PM&&(CPL!=0)) throw TRAP(GENERAL_PROTECTION_FAULT,0);
299622      cpustate->msw=~8;
300623      break;
301624   default:
302      i80286_trap2(cpustate,ILLEGAL_INSTRUCTION);
625      throw TRAP(ILLEGAL_INSTRUCTION,-1);
303626      break;
304627   }
305628}
r11568r11569
322645   }
323646   else
324647   {
325      i80286_trap2(cpustate,ILLEGAL_INSTRUCTION);
648      throw TRAP(ILLEGAL_INSTRUCTION,-1);
326649   }
327650}
328651
652static void i80286_load_flags(i8086_state *cpustate, int flags, int cpl)
653{
654   if(PM && cpl) {
655      int mask = 0xf000;
656      if(cpl>IOPL) mask |= 0x200;
657      flags &= ~mask;
658      flags |= (cpustate->flags & mask);
659   }
660   else if(!PM) (flags &= ~0xf000);
661   ExpandFlags(flags);
662   cpustate->flags = flags;
663   cpustate->flags = CompressFlags();
664   
665   if (cpustate->TF) PREFIX(_trap)(cpustate);
666   /* if the IF is set, and an interrupt is pending, signal an interrupt */
667   if (cpustate->IF && cpustate->irq_state)
668      i80286_interrupt_descriptor(cpustate, (*cpustate->irq_callback)(cpustate->device, 0), 2, -1);
669}
670
671static void PREFIX286(_popf)(i8086_state *cpustate)
672{
673   UINT16 flags;
674   POP(flags);
675   ICOUNT -= timing.popf;
676   i80286_load_flags(cpustate, flags, CPL);
677}
678
679static UINT16 i80286_far_return(i8086_state *cpustate, int iret, int bytes)
680{
681   UINT16 sel, off, flags = 0;
682   int spaddr;
683   // must be restartable
684   spaddr = (cpustate->base[SS] + cpustate->regs.w[SP]) & AMASK;
685   off = ReadWord(spaddr);
686   sel = ReadWord(spaddr+2);
687   if(iret) flags = ReadWord(spaddr+4);
688
689   if(PM) {
690      UINT16 desc[3], newsp, newss;
691      int addr, r;
692
693      if (TBL(sel)) { /* local descriptor table */
694         if (sel>cpustate->ldtr.limit) throw TRAP(GENERAL_PROTECTION_FAULT,IDXTBL(sel));
695         addr = cpustate->ldtr.base+IDX(sel);
696      } else { /* global descriptor table */
697         if (!IDX(sel)||(sel>cpustate->gdtr.limit)) throw TRAP(GENERAL_PROTECTION_FAULT,IDXTBL(sel));
698         addr = cpustate->gdtr.base+IDX(sel);
699      }
700      if (RPL(sel)<CPL) throw TRAP(GENERAL_PROTECTION_FAULT,IDXTBL(sel));
701      desc[0] = ReadWord(addr);
702      desc[1] = ReadWord(addr+2);
703      desc[2] = ReadWord(addr+4);
704      r = RIGHTS(desc);
705      if (!CODE(r) || !SEGDESC(r)) throw TRAP(GENERAL_PROTECTION_FAULT,IDXTBL(sel));
706      if (CONF(r)) { if(DPL(r)>RPL(sel)) throw TRAP(GENERAL_PROTECTION_FAULT,IDXTBL(sel)); }
707      else if (DPL(r)!=RPL(sel)) throw TRAP(GENERAL_PROTECTION_FAULT,IDXTBL(sel));
708
709      SET_ACC(desc);
710      WriteWord(addr+4, desc[2]);
711      if (!PRES(r)) throw TRAP(SEG_NOT_PRESENT,IDXTBL(sel));
712      if (off > LIMIT(desc)) throw TRAP(GENERAL_PROTECTION_FAULT, IDXTBL(sel));
713      if (CPL<RPL(sel)) {
714         newsp = ReadWord(spaddr+((iret?6:4)+bytes));
715         newss = ReadWord(spaddr+((iret?8:6)+bytes));
716         i80286_data_descriptor(cpustate, SS, newss, RPL(sel));
717         cpustate->regs.w[SP] = newsp + bytes;
718      } else cpustate->regs.w[SP] += (iret?6:4) + bytes;
719      cpustate->sregs[CS]=sel;
720      cpustate->limit[CS]=LIMIT(desc);
721      cpustate->base[CS]=BASE(desc);
722      cpustate->rights[CS]=RIGHTS(desc);
723      cpustate->pc=(cpustate->base[CS]+off)&AMASK ;
724
725      // docs say check rpl but windows doesn't like it
726      if (((CODE(cpustate->rights[DS])&&!CONF(cpustate->rights[DS])) || !CODE(cpustate->rights[DS]))
727         && (DPL(cpustate->rights[DS]) < CPL))
728         i80286_data_descriptor(cpustate, DS, 0, CPL);
729
730      if (((CODE(cpustate->rights[ES])&&!CONF(cpustate->rights[ES])) || !CODE(cpustate->rights[ES]))
731         && (DPL(cpustate->rights[ES]) < CPL))
732         i80286_data_descriptor(cpustate, ES, 0, CPL);
733   } else {
734      cpustate->regs.w[SP] += (iret?6:4) + bytes;
735      cpustate->sregs[CS]=sel;
736      cpustate->base[CS]=sel<<4;
737      cpustate->rights[CS]=0x93;
738      cpustate->limit[CS]=0xffff;
739      cpustate->pc=(cpustate->base[CS]+off)&AMASK;
740   }
741   CHANGE_PC(cpustate->pc);
742
743   return flags;
744}
745
746static void PREFIX286(_iret)(i8086_state *cpustate)
747{
748   int oldcpl = (PM)?CPL:0;
749   UINT16 flags = i80286_far_return(cpustate, 1, 0);
750   ICOUNT -= timing.iret;
751   i80286_load_flags(cpustate, flags, oldcpl);
752}
753
754static void PREFIX286(_retf_d16)(i8086_state *cpustate)
755{
756   unsigned count = FETCH;
757   count += FETCH << 8;
758   i80286_far_return(cpustate, 0, count);
759   ICOUNT -= timing.ret_far_imm;
760}
761
762static void PREFIX286(_retf)(i8086_state *cpustate)
763{
764   i80286_far_return(cpustate, 0, 0);
765   ICOUNT -= timing.ret_far;
766}
767
329768static void i80286_check_permission(i8086_state *cpustate, UINT8 check_seg, UINT16 offset, i80286_size size, i80286_operation operation)
330769{
331770   if (PM)
332771   {
772      // is the selector null?
773      if (!IDXTBL(cpustate->sregs[check_seg])) throw TRAP(GENERAL_PROTECTION_FAULT, 0);
774     
333775      /* Is the segment physically present? */
334      if ( ! IS_PRESENT( cpustate->rights[check_seg] ) )
335         throw GENERAL_PROTECTION_FAULT;
776      if (!PRES(cpustate->rights[check_seg]))
777         throw TRAP((check_seg==SS)?STACK_FAULT:SEG_NOT_PRESENT, IDXTBL(cpustate->sregs[check_seg]));
336778
337779      /* Would we go past the segment boundary? */
338780      if ( (offset + (size-1)) > cpustate->limit[check_seg] )
339      {
340         throw GENERAL_PROTECTION_FAULT;
341      }
781         throw TRAP((check_seg==SS)?STACK_FAULT:GENERAL_PROTECTION_FAULT, 0);
342782
343783      switch(operation)
344784      {
345785      case I80286_READ:
346786         /* Is the segment readable? */
347787         if ( ! IS_READABLE( cpustate->rights[check_seg] ) )
348            throw GENERAL_PROTECTION_FAULT;
788            throw TRAP(GENERAL_PROTECTION_FAULT, IDXTBL(cpustate->sregs[check_seg]));
349789         break;
350790
351791      case I80286_WRITE:
352792         /* Is the segment writeable?  */
353793         if ( ! IS_WRITEABLE( cpustate->rights[check_seg] ) )
354            throw GENERAL_PROTECTION_FAULT;
794            throw TRAP(GENERAL_PROTECTION_FAULT, IDXTBL(cpustate->sregs[check_seg]));
355795         break;
356796
357797      case I80286_EXECUTE:
src/emu/cpu/i86/instr186.h
r11568r11569
3232static void PREFIX186(_repne)(i8086_state *cpustate);
3333static void PREFIX186(_repe)(i8086_state *cpustate);
3434static void PREFIX186(_sti)(i8086_state *cpustate);
35static void PREFIX186(_rotshft_bcl)(i8086_state *cpustate);
36static void PREFIX186(_rotshft_wcl)(i8086_state *cpustate);
src/emu/cpu/i86/instr286.h
r11568r11569
1#define DIVIDE_BY_ZERO 0
2#define SINGLE_STEP 1
3#define NMI 2
4#define BREAK 3
5#define INTO_OVERFLOW 4
6#define BOUND_OVERRUN 5
17#define ILLEGAL_INSTRUCTION 6
2#define GENERAL_PROTECTION_FAULT 0xd
8#define CPU_EXT_UNAVAILABLE 7
9#define DOUBLE_FAULT 8
10#define CPU_EXT_SEG_OVERRUN 9
11#define INVALID_TSS 10
12#define SEG_NOT_PRESENT 11
13#define STACK_FAULT 12
14#define GENERAL_PROTECTION_FAULT 13
315
416#define PM (cpustate->msw&1)
517#define CPL (cpustate->sregs[CS]&3)
6#define IOPL ((cpustate->flags&0x3000)>>12)
718
8static void i80286_trap2(i80286_state *cpustate,int number);
9static void i80286_interrupt_descriptor(i80286_state *cpustate,UINT16 number);
10static void i80286_code_descriptor(i80286_state *cpustate,UINT16 selector, UINT16 offset);
11static void i80286_data_descriptor(i80286_state *cpustate,int reg, UINT16 selector);
19static void i80286_trap2(i80286_state *cpustate,UINT32 error);
20static void i80286_interrupt_descriptor(i80286_state *cpustate,UINT16 number, int trap, int error);
21static void i80286_code_descriptor(i80286_state *cpustate,UINT16 selector, UINT16 offset, int gate);
22static void i80286_data_descriptor(i80286_state *cpustate,int reg, UINT16 selector, int cpl);
1223static void PREFIX286(_0fpre)(i80286_state *cpustate);
1324static void PREFIX286(_arpl)(i80286_state *cpustate);
25static void i80286_pop_seg(i80286_state *cpustate,int reg);
1426
1527enum i80286_size
1628{
src/emu/cpu/i86/i86.c
r11568r11569
495495      case STATE_GENFLAGS:
496496         cpustate->flags = CompressFlags();
497497         string.printf("%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",
498               cpustate->flags & 0x8000 ? '?' : '.',
499               cpustate->flags & 0x4000 ? '?' : '.',
500               cpustate->flags & 0x2000 ? '?' : '.',
501               cpustate->flags & 0x1000 ? '?' : '.',
498               cpustate->flags & 0x8000 ? '1' : '.',
499               cpustate->flags & 0x4000 ? '1' : '.',
500               cpustate->flags & 0x2000 ? '1' : '.',
501               cpustate->flags & 0x1000 ? '1' : '.',
502502               cpustate->flags & 0x0800 ? 'O' : '.',
503503               cpustate->flags & 0x0400 ? 'D' : '.',
504504               cpustate->flags & 0x0200 ? 'I' : '.',
505505               cpustate->flags & 0x0100 ? 'T' : '.',
506506               cpustate->flags & 0x0080 ? 'S' : '.',
507507               cpustate->flags & 0x0040 ? 'Z' : '.',
508               cpustate->flags & 0x0020 ? '?' : '.',
508               cpustate->flags & 0x0020 ? '0' : '.',
509509               cpustate->flags & 0x0010 ? 'A' : '.',
510               cpustate->flags & 0x0008 ? '?' : '.',
510               cpustate->flags & 0x0008 ? '0' : '.',
511511               cpustate->flags & 0x0004 ? 'P' : '.',
512               cpustate->flags & 0x0002 ? 'N' : '.',
512               cpustate->flags & 0x0002 ? '1' : '.',
513513               cpustate->flags & 0x0001 ? 'C' : '.');
514514         break;
515515
src/emu/cpu/i86/table86.h
r11568r11569
9696    PREFIX86(_pop_bp),           /* 0x5d */
9797    PREFIX86(_pop_si),           /* 0x5e */
9898    PREFIX86(_pop_di),           /* 0x5f */
99    PREFIX86(_invalid), // PREFIX86(_pusha),            /* 0x60 */
100    PREFIX86(_invalid), // PREFIX86(_popa),             /* 0x61 */
101    PREFIX86(_invalid), // PREFIX86(_bound),            /* 0x62 */
102    PREFIX86(_invalid),
103    PREFIX86(_invalid),
104    PREFIX86(_invalid),
105    PREFIX86(_invalid),
106    PREFIX86(_invalid),
107    PREFIX86(_invalid), //i_push_d16),         /* 0x68 */
108    PREFIX86(_invalid), //i_imul_d16),         /* 0x69 */
109    PREFIX86(_invalid), //i_push_d8),          /* 0x6a */
110    PREFIX86(_invalid), //i_imul_d8),          /* 0x6b */
111    PREFIX86(_invalid), //i_insb),             /* 0x6c */
112    PREFIX86(_invalid), //i_insw),             /* 0x6d */
113    PREFIX86(_invalid), //i_outsb),            /* 0x6e */
114    PREFIX86(_invalid), //i_outsw),            /* 0x6f */
99    PREFIX86(_invalid_2b), // PREFIX86(_pusha),            /* 0x60 */
100    PREFIX86(_invalid_2b), // PREFIX86(_popa),             /* 0x61 */
101    PREFIX86(_invalid_2b), // PREFIX86(_bound),            /* 0x62 */
102    PREFIX86(_invalid_2b),
103    PREFIX86(_invalid_2b),
104    PREFIX86(_invalid_2b),
105    PREFIX86(_invalid_2b),
106    PREFIX86(_invalid_2b),
107    PREFIX86(_invalid_2b), //i_push_d16),         /* 0x68 */
108    PREFIX86(_invalid_2b), //i_imul_d16),         /* 0x69 */
109    PREFIX86(_invalid_2b), //i_push_d8),          /* 0x6a */
110    PREFIX86(_invalid_2b), //i_imul_d8),          /* 0x6b */
111    PREFIX86(_invalid_2b), //i_insb),             /* 0x6c */
112    PREFIX86(_invalid_2b), //i_insw),             /* 0x6d */
113    PREFIX86(_invalid_2b), //i_outsb),            /* 0x6e */
114    PREFIX86(_invalid_2b), //i_outsw),            /* 0x6f */
115115    PREFIX86(_jo),               /* 0x70 */
116116    PREFIX86(_jno),              /* 0x71 */
117117    PREFIX86(_jb),               /* 0x72 */
r11568r11569
359359   case 0x5d:    PREFIX86(_pop_bp)(cpustate); break;\
360360   case 0x5e:    PREFIX86(_pop_si)(cpustate); break;\
361361   case 0x5f:    PREFIX86(_pop_di)(cpustate); break;\
362        case 0x60:    PREFIX86(_invalid)(cpustate); break;\
363        case 0x61:    PREFIX86(_invalid)(cpustate); break;\
364        case 0x62:    PREFIX86(_invalid)(cpustate); break;\
365   case 0x63:    PREFIX86(_invalid)(cpustate); break;\
366   case 0x64:    PREFIX86(_invalid)(cpustate); break;\
367   case 0x65:     PREFIX86(_invalid)(cpustate); break;\
368   case 0x66:    PREFIX86(_invalid)(cpustate); break;\
369   case 0x67:    PREFIX86(_invalid)(cpustate); break;\
370        case 0x68:    PREFIX86(_invalid)(cpustate); break;\
371        case 0x69:    PREFIX86(_invalid)(cpustate); break;\
372        case 0x6a:    PREFIX86(_invalid)(cpustate); break;\
373        case 0x6b:    PREFIX86(_invalid)(cpustate); break;\
374        case 0x6c:    PREFIX86(_invalid)(cpustate); break;\
375        case 0x6d:    PREFIX86(_invalid)(cpustate); break;\
376        case 0x6e:    PREFIX86(_invalid)(cpustate); break;\
377        case 0x6f:    PREFIX86(_invalid)(cpustate); break;\
362        case 0x60:    PREFIX86(_invalid_2b)(cpustate); break;\
363        case 0x61:    PREFIX86(_invalid_2b)(cpustate); break;\
364        case 0x62:    PREFIX86(_invalid_2b)(cpustate); break;\
365   case 0x63:    PREFIX86(_invalid_2b)(cpustate); break;\
366   case 0x64:    PREFIX86(_invalid_2b)(cpustate); break;\
367   case 0x65:     PREFIX86(_invalid_2b)(cpustate); break;\
368   case 0x66:    PREFIX86(_invalid_2b)(cpustate); break;\
369   case 0x67:    PREFIX86(_invalid_2b)(cpustate); break;\
370        case 0x68:    PREFIX86(_invalid_2b)(cpustate); break;\
371        case 0x69:    PREFIX86(_invalid_2b)(cpustate); break;\
372        case 0x6a:    PREFIX86(_invalid_2b)(cpustate); break;\
373        case 0x6b:    PREFIX86(_invalid_2b)(cpustate); break;\
374        case 0x6c:    PREFIX86(_invalid_2b)(cpustate); break;\
375        case 0x6d:    PREFIX86(_invalid_2b)(cpustate); break;\
376        case 0x6e:    PREFIX86(_invalid_2b)(cpustate); break;\
377        case 0x6f:    PREFIX86(_invalid_2b)(cpustate); break;\
378378   case 0x70:    PREFIX86(_jo)(cpustate); break;\
379379   case 0x71:    PREFIX86(_jno)(cpustate); break;\
380380   case 0x72:    PREFIX86(_jb)(cpustate); break;\
src/emu/cpu/i86/i86priv.h
r11568r11569
121121#define POP(var)            { cpustate->regs.w[SP] += 2; var = ReadWord(((cpustate->base[SS] + ((cpustate->regs.w[SP]-2) & 0xffff)) & AMASK)); }
122122
123123/************************************************************************/
124#ifdef I80286
125#define IOPL ((cpustate->flags&0x3000)>>12)
126#define NT ((cpustate->flags&0x4000)>>14)
127#define xF (0)
128#else
129#define IOPL (3)
130#define NT (1)
131#define xF (1)
132#endif
124133
125#define CompressFlags() (WORD)(CF | (PF << 2) | (AF << 4) | (ZF << 6) \
134#define CompressFlags() (WORD)(CF | 2 |(PF << 2) | (AF << 4) | (ZF << 6) \
126135            | (SF << 7) | (cpustate->TF << 8) | (cpustate->IF << 9) \
127            | (DF << 10) | (OF << 11))
136            | (DF << 10) | (OF << 11) | (IOPL << 12) | (NT << 14) | (xF << 15))
128137
129138#define ExpandFlags(f) \
130139{ \
r11568r11569
138147     cpustate->DirVal = ((f) & 1024) ? -1 : 1; \
139148     cpustate->OverVal = (f) & 2048; \
140149}
141
142150#endif /* __I86_H__ */
src/emu/cpu/i86/instr86.c
r11568r11569
4242
4343#ifdef I80286
4444   if (PM) {
45      i80286_interrupt_descriptor(cpustate, int_num);
45      i80286_interrupt_descriptor(cpustate, int_num, 0, 0);
4646   } else {
4747#endif
4848      dest_off = ReadWord(int_num*4);
r11568r11569
5555      cpustate->sregs[CS] = (WORD)dest_seg;
5656      cpustate->base[CS] = SegBase(CS);
5757      cpustate->pc = (cpustate->base[CS] + dest_off) & AMASK;
58      CHANGE_PC(cpustate->pc);
5859#ifdef I80286
5960   }
6061#endif
61   CHANGE_PC(cpustate->pc);
62
6362   cpustate->extra_cycles += timing.exception;
6463}
6564
r11568r11569
178177         break;
179178      case 0x20:
180179      case 0x30:   /* SHL eb,count */
181         dst <<= count;
180         for(int i=0;i<count;i++) dst<<= 1;
182181         SetCFB(dst);
183182         cpustate->AuxVal = 1;
184183         SetSZPF_Byte(dst);
185184         PutbackRMByte(ModRM,(BYTE)dst);
186185         break;
187186      case 0x28:   /* SHR eb,count */
188         dst >>= count-1;
187         for(int i=0;i<count-1;i++) dst>>= 1;
189188         cpustate->CarryVal = dst & 0x1;
190189         dst >>= 1;
191190         SetSZPF_Byte(dst);
r11568r11569
193192         PutbackRMByte(ModRM,(BYTE)dst);
194193         break;
195194      case 0x38:   /* SAR eb,count */
196         dst = ((INT8)dst) >> (count-1);
195         for(int i=0;i<count-1;i++) dst = ((INT8)dst) >> 1;
197196         cpustate->CarryVal = dst & 0x1;
198197         dst = ((INT8)((BYTE)dst)) >> 1;
199198         SetSZPF_Byte(dst);
r11568r11569
369368         break;
370369      case 0x20:
371370      case 0x30:   /* SHL ew,count */
372         dst <<= count;
371         for(int i=0;i<count;i++) dst<<= 1;
373372         SetCFW(dst);
374373         cpustate->AuxVal = 1;
375374         SetSZPF_Word(dst);
376375         PutbackRMWord(ModRM,dst);
377376         break;
378377      case 0x28:   /* SHR ew,count */
379         dst >>= count-1;
378         for(int i=0;i<count-1;i++) dst>>= 1;
380379         cpustate->CarryVal = dst & 0x1;
381380         dst >>= 1;
382381         SetSZPF_Word(dst);
383382         cpustate->AuxVal = 1;
384383         PutbackRMWord(ModRM,dst);
385384         break;
386      case 0x38:   /* SAR ew,count */
387         dst = ((INT16)dst) >> (count-1);
385      case 0x38:   /* SAR ew,count */         
386         for(int i=0;i<count-1;i++) dst = ((INT16)dst) >> 1;
388387         cpustate->CarryVal = dst & 0x01;
389388         dst = ((INT16)((WORD)dst)) >> 1;
390389         SetSZPF_Word(dst);
r11568r11569
711710static void PREFIX86(_pop_es)(i8086_state *cpustate)    /* Opcode 0x07 */
712711{
713712#ifdef I80286
714   UINT16 tmp;
715   POP(tmp);
716   i80286_data_descriptor(cpustate,ES,tmp);
713   i80286_pop_seg(cpustate,ES);
717714#else
718715   POP(cpustate->sregs[ES]);
719716   cpustate->base[ES] = SegBase(ES);
r11568r11569
900897static void PREFIX86(_pop_ds)(i8086_state *cpustate)    /* Opcode 0x1f */
901898{
902899#ifdef I80286
903   UINT16 tmp;
904   POP(tmp);
905   i80286_data_descriptor(cpustate,DS,tmp);
900   i80286_pop_seg(cpustate,DS);
906901#else
907902   POP(cpustate->sregs[DS]);
908903   cpustate->base[DS] = SegBase(DS);
r11568r11569
13081303static void PREFIX86(_push_sp)(i8086_state *cpustate)    /* Opcode 0x54 */
13091304{
13101305   ICOUNT -= timing.push_r16;
1306#ifdef I80286
1307   PUSH(cpustate->regs.w[SP]+2);
1308#else
13111309   PUSH(cpustate->regs.w[SP]);
1310#endif
13121311}
13131312
13141313static void PREFIX86(_push_bp)(i8086_state *cpustate)    /* Opcode 0x55 */
r11568r11569
18311830{
18321831   unsigned ModRM = FETCH;
18331832   ICOUNT -= (ModRM >= 0xc0) ? timing.mov_rs : timing.mov_ms;
1834#ifdef I80286
18351833   if (ModRM & 0x20) {   /* HJB 12/13/98 1xx is invalid */
1836      i80286_trap2(cpustate,ILLEGAL_INSTRUCTION);
1837      return;
1834      cpustate->pc = cpustate->prevpc;
1835      return PREFIX86(_invalid)(cpustate);
18381836   }
1839#else
1840   if (ModRM & 0x20) return;   /* HJB 12/13/98 1xx is invalid */
1841#endif
1837     
18421838   PutRMWord(ModRM,cpustate->sregs[(ModRM & 0x38) >> 3]);
18431839}
18441840
r11568r11569
18531849static void PREFIX86(_popw)(i8086_state *cpustate)    /* Opcode 0x8f */
18541850{
18551851   unsigned ModRM = FETCH;
1856    WORD tmp;
1857   POP(tmp);
1852       WORD tmp;
1853   tmp = ReadWord(cpustate->base[SS] + cpustate->regs.w[SP]);
18581854   ICOUNT -= (ModRM >= 0xc0) ? timing.pop_r16 : timing.pop_m16;
18591855   PutRMWord(ModRM,tmp);
1856   cpustate->regs.w[SP] += 2;
18601857}
18611858
18621859
r11568r11569
19251922
19261923static void PREFIX86(_call_far)(i8086_state *cpustate)
19271924{
1928    unsigned tmp, tmp2;
1929   WORD ip;
1925   unsigned int tmp, tmp2;
1926   WORD cs, ip;
19301927
19311928   tmp = FETCH;
19321929   tmp += FETCH << 8;
r11568r11569
19351932   tmp2 += FETCH << 8;
19361933
19371934   ip = cpustate->pc - cpustate->base[CS];
1938   PUSH(cpustate->sregs[CS]);
1939   PUSH(ip);
1935   cs = cpustate->sregs[CS];
19401936
19411937#ifdef I80286
1942   i80286_code_descriptor(cpustate, tmp2, tmp);
1938   i80286_code_descriptor(cpustate, tmp2, tmp, 2);
19431939#else
19441940   cpustate->sregs[CS] = (WORD)tmp2;
19451941   cpustate->base[CS] = SegBase(CS);
19461942   cpustate->pc = (cpustate->base[CS] + (WORD)tmp) & AMASK;
19471943#endif
1944   PUSH(cs);
1945   PUSH(ip);
19481946   ICOUNT -= timing.call_far;
19491947   CHANGE_PC(cpustate->pc);
19501948}
r11568r11569
19671965
19681966   tmp = CompressFlags();
19691967#ifdef I80286
1970    PUSH( tmp &= ~0xf000 );
1971#else
1972    PUSH( tmp | 0xf000 );
1968    if(!PM) ( tmp &= ~0xf000 );
19731969#endif
1970    PUSH( tmp );
19741971}
19751972
1973#ifndef I80286
19761974static void PREFIX86(_popf)(i8086_state *cpustate)    /* Opcode 0x9d */
19771975{
19781976   unsigned tmp;
1979    POP(tmp);
1977   POP(tmp);
19801978   ICOUNT -= timing.popf;
1981    ExpandFlags(tmp);
19821979
1980   ExpandFlags(tmp);
1981   cpustate->flags = tmp;
1982   cpustate->flags = CompressFlags();
1983
19831984   if (cpustate->TF) PREFIX(_trap)(cpustate);
19841985
19851986   /* if the IF is set, and an interrupt is pending, signal an interrupt */
19861987   if (cpustate->IF && cpustate->irq_state)
19871988      PREFIX(_interrupt)(cpustate, (UINT32)-1);
19881989}
1990#endif
19891991
19901992static void PREFIX86(_sahf)(i8086_state *cpustate)    /* Opcode 0x9e */
19911993{
r11568r11569
22732275
22742276    RegWord(ModRM)= tmp;
22752277#ifdef I80286
2276   i80286_data_descriptor(cpustate,ES,GetnextRMWord);
2278   i80286_data_descriptor(cpustate,ES,GetnextRMWord,CPL);
22772279#else
22782280   cpustate->sregs[ES] = GetnextRMWord;
22792281   cpustate->base[ES] = SegBase(ES);
r11568r11569
22882290
22892291    RegWord(ModRM)=tmp;
22902292#ifdef I80286
2291   i80286_data_descriptor(cpustate,DS,GetnextRMWord);
2293   i80286_data_descriptor(cpustate,DS,GetnextRMWord,CPL);
22922294#else
22932295   cpustate->sregs[DS] = GetnextRMWord;
22942296   cpustate->base[DS] = SegBase(DS);
r11568r11569
23102312   PutImmRMWord(ModRM);
23112313}
23122314
2315#ifndef I80286
23132316static void PREFIX86(_retf_d16)(i8086_state *cpustate)    /* Opcode 0xca */
23142317{
23152318   unsigned count = FETCH;
23162319   count += FETCH << 8;
23172320
2318#ifdef I80286
2319   {
2320      int tmp, tmp2;
2321      POP(tmp2);
2322      POP(tmp);
2323      i80286_code_descriptor(cpustate, tmp, tmp2);
2324   }
2325#else
23262321   POP(cpustate->pc);
23272322   POP(cpustate->sregs[CS]);
23282323   cpustate->base[CS] = SegBase(CS);
23292324   cpustate->pc = (cpustate->pc + cpustate->base[CS]) & AMASK;
2330#endif
23312325   cpustate->regs.w[SP]+=count;
23322326   ICOUNT -= timing.ret_far_imm;
23332327   CHANGE_PC(cpustate->pc);
r11568r11569
23352329
23362330static void PREFIX86(_retf)(i8086_state *cpustate)    /* Opcode 0xcb */
23372331{
2338#ifdef I80286
2339   {
2340      int tmp, tmp2;
2341      POP(tmp2);
2342      POP(tmp);
2343      i80286_code_descriptor(cpustate, tmp, tmp2);
2344   }
2345#else
23462332   POP(cpustate->pc);
23472333   POP(cpustate->sregs[CS]);
23482334   cpustate->base[CS] = SegBase(CS);
23492335   cpustate->pc = (cpustate->pc + cpustate->base[CS]) & AMASK;
2350#endif
23512336   ICOUNT -= timing.ret_far;
23522337   CHANGE_PC(cpustate->pc);
23532338}
2339#endif
23542340
23552341static void PREFIX86(_int3)(i8086_state *cpustate)    /* Opcode 0xcc */
23562342{
r11568r11569
23732359   } else ICOUNT -= timing.into_nt;
23742360}
23752361
2362#ifndef I80286
23762363static void PREFIX86(_iret)(i8086_state *cpustate)    /* Opcode 0xcf */
23772364{
23782365   ICOUNT -= timing.iret;
2379#ifdef I80286
2380   {
2381      int tmp, tmp2;
2382      POP(tmp2);
2383      POP(tmp);
2384      i80286_code_descriptor(cpustate, tmp, tmp2);
2385   }
2386#else
23872366   POP(cpustate->pc);
23882367   POP(cpustate->sregs[CS]);
23892368   cpustate->base[CS] = SegBase(CS);
23902369   cpustate->pc = (cpustate->pc + cpustate->base[CS]) & AMASK;
2391#endif
2392    PREFIX(_popf)(cpustate);
2370       PREFIX(_popf)(cpustate);
23932371   CHANGE_PC(cpustate->pc);
23942372
23952373   /* if the IF is set, and an interrupt is pending, signal an interrupt */
23962374   if (cpustate->IF && cpustate->irq_state)
23972375      PREFIX(_interrupt)(cpustate, (UINT32)-1);
23982376}
2377#endif
23992378
24002379static void PREFIX86(_rotshft_b)(i8086_state *cpustate)    /* Opcode 0xd0 */
24012380{
r11568r11569
24092388}
24102389
24112390
2391#ifdef I8086
24122392static void PREFIX86(_rotshft_bcl)(i8086_state *cpustate)    /* Opcode 0xd2 */
24132393{
24142394   PREFIX(_rotate_shift_Byte)(cpustate,FETCHOP,cpustate->regs.b[CL]);
r11568r11569
24182398{
24192399   PREFIX(_rotate_shift_Word)(cpustate,FETCHOP,cpustate->regs.b[CL]);
24202400}
2401#endif
24212402
24222403/* OB: Opcode works on NEC V-Series but not the Variants              */
24232404/*     one could specify any byte value as operand but the NECs */
r11568r11569
25932574   tmp1 += FETCH << 8;
25942575
25952576#ifdef I80286
2596   i80286_code_descriptor(cpustate, tmp1,tmp);
2577   i80286_code_descriptor(cpustate, tmp1,tmp, 1);
25972578#else
25982579   cpustate->sregs[CS] = (WORD)tmp1;
25992580   cpustate->base[CS] = SegBase(CS);
r11568r11569
26512632static void PREFIX(_pop_ss)(i8086_state *cpustate)    /* Opcode 0x17 */
26522633{
26532634#ifdef I80286
2654   UINT16 tmp;
2655   POP(tmp);
2656   i80286_data_descriptor(cpustate, SS, tmp);
2635   i80286_pop_seg(cpustate, SS);
26572636#else
26582637   POP(cpustate->sregs[SS]);
26592638   cpustate->base[SS] = SegBase(SS);
r11568r11569
27042683    switch (ModRM & 0x38)
27052684    {
27062685    case 0x00:  /* mov es,ew */
2707      i80286_data_descriptor(cpustate,ES,src);
2686      i80286_data_descriptor(cpustate,ES,src,CPL);
27082687      break;
27092688    case 0x18:  /* mov ds,ew */
2710      i80286_data_descriptor(cpustate,DS,src);
2689      i80286_data_descriptor(cpustate,DS,src,CPL);
27112690      break;
27122691    case 0x10:  /* mov ss,ew */
2713      i80286_data_descriptor(cpustate,SS,src);
2692      i80286_data_descriptor(cpustate,SS,src,CPL);
2693      cpustate->seg_prefix = FALSE;
27142694      PREFIX(_instruction)[FETCHOP](cpustate);
27152695      break;
27162696    case 0x08:  /* mov cs,ew */
r11568r11569
27302710    case 0x10:  /* mov ss,ew */
27312711      cpustate->sregs[SS] = src;
27322712      cpustate->base[SS] = SegBase(SS); /* no interrupt allowed before next instr */
2713      cpustate->seg_prefix = FALSE;
27332714      PREFIX(_instruction)[FETCHOP](cpustate);
27342715      break;
27352716    case 0x08:  /* mov cs,ew */
r11568r11569
27502731
27512732static void PREFIX(_sti)(i8086_state *cpustate)    /* Opcode 0xfb */
27522733{
2734#ifdef I80286
2735   if(PM && (CPL>IOPL)) throw TRAP(GENERAL_PROTECTION_FAULT,0);
2736#endif
27532737   ICOUNT -= timing.flag_ops;
27542738   SetIF(1);
27552739   PREFIX(_instruction)[FETCHOP](cpustate); /* no interrupt before next instruction */
27562740
27572741   /* if an interrupt is pending, signal an interrupt */
27582742   if (cpustate->irq_state)
2743#ifdef I80286
2744      i80286_interrupt_descriptor(cpustate, (*cpustate->irq_callback)(cpustate->device, 0), 2, -1);
2745#else     
27592746      PREFIX86(_interrupt)(cpustate, (UINT32)-1);
2747#endif
27602748}
27612749
27622750#ifndef I80186
r11568r11569
30453033
30463034static void PREFIX86(_cli)(i8086_state *cpustate)    /* Opcode 0xfa */
30473035{
3036#ifdef I80286
3037   if(PM && (CPL>IOPL)) throw TRAP(GENERAL_PROTECTION_FAULT,0);
3038#endif
30483039   ICOUNT -= timing.flag_ops;
30493040   SetIF(0);
30503041}
r11568r11569
31333124      tmp = cpustate->sregs[CS];   /* HJB 12/13/98 need to skip displacements of cpustate->ea */
31343125      tmp1 = GetRMWord(ModRM);
31353126      ip = cpustate->pc - cpustate->base[CS];
3136      PUSH(tmp);
3137      PUSH(ip);
31383127#ifdef I80286
3139      i80286_code_descriptor(cpustate, GetnextRMWord, tmp1);
3128      i80286_code_descriptor(cpustate, GetnextRMWord, tmp1, 2);
31403129#else
31413130      cpustate->sregs[CS] = GetnextRMWord;
31423131      cpustate->base[CS] = SegBase(CS);
31433132      cpustate->pc = (cpustate->base[CS] + tmp1) & AMASK;
31443133#endif
3134      PUSH(tmp);
3135      PUSH(ip);
31453136      CHANGE_PC(cpustate->pc);
31463137      break;
31473138
r11568r11569
31573148
31583149#ifdef I80286
31593150      tmp = GetRMWord(ModRM);
3160      i80286_code_descriptor(cpustate, GetnextRMWord, tmp);
3151      i80286_code_descriptor(cpustate, GetnextRMWord, tmp, 1);
31613152#else
31623153      cpustate->pc = GetRMWord(ModRM);
31633154      cpustate->sregs[CS] = GetnextRMWord;
r11568r11569
31793170static void PREFIX86(_invalid)(i8086_state *cpustate)
31803171{
31813172#ifdef I80286
3182   i80286_trap2(cpustate,ILLEGAL_INSTRUCTION);
3173   throw TRAP(ILLEGAL_INSTRUCTION,-1);
31833174#else
31843175   /* i8086/i8088 ignore an invalid opcode. */
31853176   /* i80186/i80188 probably also ignore an invalid opcode. */
r11568r11569
31873178   ICOUNT -= 10;
31883179#endif
31893180}
3181
3182#ifndef I80286
3183static void PREFIX86(_invalid_2b)(i8086_state *cpustate)
3184{
3185   unsigned ModRM = FETCH;
3186   GetRMByte(ModRM);
3187   logerror("illegal 2 byte instruction %.2x at %.5x\n",PEEKBYTE(cpustate->pc-2), cpustate->pc-2);
3188   ICOUNT -= 10;
3189}
31903190#endif
3191#endif
3192
src/emu/cpu/i86/instr86.h
r11568r11569
140140static void PREFIX86(_lea)(i8086_state *cpustate);
141141static void PREFIX86(_mov_sregw)(i8086_state *cpustate);
142142static void PREFIX86(_invalid)(i8086_state *cpustate);
143#ifndef I80286
144static void PREFIX86(_invalid_2b)(i8086_state *cpustate);
145#endif
143146static void PREFIX86(_popw)(i8086_state *cpustate);
144147static void PREFIX86(_nop)(i8086_state *cpustate);
145148static void PREFIX86(_xchg_axcx)(i8086_state *cpustate);
src/emu/cpu/i86/i286.c
r11568r11569
1717
1818#define INPUT_LINE_A20      1
1919
20#include "i86priv.h"
2120#include "i286.h"
2221
2322
r11568r11569
7372   INT32   extra_cycles;       /* extra cycles for interrupts */
7473
7574   int halted;         /* Is the CPU halted ? */
75   int trap_level;
7676
7777   int icount;
7878   char seg_prefix;
r11568r11569
9999/***************************************************************************/
100100
101101#define I80286
102#include "i86priv.h"
102103#define PREFIX(fname) i80286##fname
103104#define PREFIX86(fname) i80286##fname
104105#define PREFIX186(fname) i80286##fname
r11568r11569
150151{
151152   i80286_state *cpustate = get_safe_token(device);
152153
154   memset(&cpustate->regs, 0, sizeof(i80286basicregs));
153155   cpustate->sregs[CS] = 0xf000;
154156   cpustate->base[CS] = 0xff0000;
155157   /* temporary, until I have the right reset vector working */
r11568r11569
158160   cpustate->limit[CS]=cpustate->limit[SS]=cpustate->limit[DS]=cpustate->limit[ES]=0xffff;
159161   cpustate->sregs[DS]=cpustate->sregs[SS]=cpustate->sregs[ES]=0;
160162   cpustate->base[DS]=cpustate->base[SS]=cpustate->base[ES]=0;
163   cpustate->rights[CS]=cpustate->rights[DS]=cpustate->rights[SS]=cpustate->rights[ES]=0x93;
161164   cpustate->msw=0xfff0;
162165   cpustate->flags=2;
163166   ExpandFlags(cpustate->flags);
164167   cpustate->idtr.base=0;cpustate->idtr.limit=0x3ff;
168   cpustate->gdtr.base=cpustate->ldtr.base=cpustate->tr.base=0;
169   cpustate->gdtr.limit=cpustate->ldtr.limit=cpustate->tr.limit=0;
170   cpustate->ldtr.rights=cpustate->tr.rights=0;
171   cpustate->ldtr.sel=cpustate->tr.sel=0;
165172   cpustate->rep_in_progress = FALSE;
166173
167174   CHANGE_PC(cpustate->pc);
r11568r11569
179186   {
180187      cpustate->halted = 0;
181188   }
182
183   if (irqline == INPUT_LINE_NMI)
189   try
184190   {
185      if (cpustate->nmi_state == state)
186         return;
187      cpustate->nmi_state = state;
191      if (irqline == INPUT_LINE_NMI)
192      {
193         if (cpustate->nmi_state == state)
194            return;
195         cpustate->nmi_state = state;
188196
189      /* on a rising edge, signal the NMI */
190      if (state != CLEAR_LINE)
191         PREFIX(_interrupt)(cpustate, I8086_NMI_INT_VECTOR);
197         /* on a rising edge, signal the NMI */
198         if (state != CLEAR_LINE)
199            i80286_interrupt_descriptor(cpustate, I8086_NMI_INT_VECTOR, 2, -1);
200      }
201      else
202      {
203         cpustate->irq_state = state;
204
205         /* if the IF is set, signal an interrupt */
206         if (state != CLEAR_LINE && cpustate->IF)
207            i80286_interrupt_descriptor(cpustate, (*cpustate->irq_callback)(cpustate->device, 0), 2, -1);
208
209      }
192210   }
193   else
211   catch (UINT32 e)
194212   {
195      cpustate->irq_state = state;
196
197      /* if the IF is set, signal an interrupt */
198      if (state != CLEAR_LINE && cpustate->IF)
199         PREFIX(_interrupt)(cpustate, (UINT32)-1);
213      i80286_trap2(cpustate, e);
200214   }
201215}
202216
r11568r11569
231245      {
232246         TABLE286 // call instruction
233247      }
234      catch (int e)
248      catch (UINT32 e)
235249      {
236250         i80286_trap2(cpustate,e);
237251      }
r11568r11569
468482      case CPUINFO_STR_FLAGS:
469483         cpustate->flags = CompressFlags();
470484         sprintf(info->s, "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",
471               cpustate->flags & 0x8000 ? '?' : '.',
472               cpustate->flags & 0x4000 ? '?' : '.',
473               cpustate->flags & 0x2000 ? '?' : '.',
474               cpustate->flags & 0x1000 ? '?' : '.',
485               cpustate->flags & 0x8000 ? '0' : '.',
486               cpustate->flags & 0x4000 ? 'N' : '.',
487               cpustate->flags & 0x2000 ? 'I' : '.',
488               cpustate->flags & 0x1000 ? 'I' : '.',
475489               cpustate->flags & 0x0800 ? 'O' : '.',
476490               cpustate->flags & 0x0400 ? 'D' : '.',
477491               cpustate->flags & 0x0200 ? 'I' : '.',
478492               cpustate->flags & 0x0100 ? 'T' : '.',
479493               cpustate->flags & 0x0080 ? 'S' : '.',
480494               cpustate->flags & 0x0040 ? 'Z' : '.',
481               cpustate->flags & 0x0020 ? '?' : '.',
495               cpustate->flags & 0x0020 ? '0' : '.',
482496               cpustate->flags & 0x0010 ? 'A' : '.',
483               cpustate->flags & 0x0008 ? '?' : '.',
497               cpustate->flags & 0x0008 ? '0' : '.',
484498               cpustate->flags & 0x0004 ? 'P' : '.',
485               cpustate->flags & 0x0002 ? 'N' : '.',
499               cpustate->flags & 0x0002 ? '1' : '.',
486500               cpustate->flags & 0x0001 ? 'C' : '.');
487501         break;
488502
r11568r11569
498512      case CPUINFO_STR_REGISTER + I80286_SI:         sprintf(info->s, "SI: %04X", cpustate->regs.w[SI]); break;
499513      case CPUINFO_STR_REGISTER + I80286_DI:         sprintf(info->s, "DI: %04X", cpustate->regs.w[DI]); break;
500514      case CPUINFO_STR_REGISTER + I80286_CS:         sprintf(info->s, "CS:  %04X %02X", cpustate->sregs[CS], cpustate->rights[CS]); break;
501      case CPUINFO_STR_REGISTER + I80286_CS_2:      sprintf(info->s, "%06X %04X", cpustate->base[CS], cpustate->limit[CS]); break;
515      case CPUINFO_STR_REGISTER + I80286_CS_2:      sprintf(info->s, "CSDESC: %06X %04X", cpustate->base[CS], cpustate->limit[CS]); break;
502516      case CPUINFO_STR_REGISTER + I80286_SS:         sprintf(info->s, "SS:  %04X %02X", cpustate->sregs[SS], cpustate->rights[SS]); break;
503      case CPUINFO_STR_REGISTER + I80286_SS_2:      sprintf(info->s, "%06X %04X", cpustate->base[SS], cpustate->limit[SS]); break;
517      case CPUINFO_STR_REGISTER + I80286_SS_2:      sprintf(info->s, "SSDESC: %06X %04X", cpustate->base[SS], cpustate->limit[SS]); break;
504518      case CPUINFO_STR_REGISTER + I80286_DS:         sprintf(info->s, "DS:  %04X %02X", cpustate->sregs[DS], cpustate->rights[DS]); break;
505      case CPUINFO_STR_REGISTER + I80286_DS_2:      sprintf(info->s, "%06X %04X", cpustate->base[DS], cpustate->limit[DS]); break;
519      case CPUINFO_STR_REGISTER + I80286_DS_2:      sprintf(info->s, "DSDESC: %06X %04X", cpustate->base[DS], cpustate->limit[DS]); break;
506520      case CPUINFO_STR_REGISTER + I80286_ES:         sprintf(info->s, "ES:  %04X %02X", cpustate->sregs[ES], cpustate->rights[ES]); break;
507      case CPUINFO_STR_REGISTER + I80286_ES_2:      sprintf(info->s, "%06X %04X", cpustate->base[ES], cpustate->limit[ES]); break;
521      case CPUINFO_STR_REGISTER + I80286_ES_2:      sprintf(info->s, "ESDESC: %06X %04X", cpustate->base[ES], cpustate->limit[ES]); break;
508522      case CPUINFO_STR_REGISTER + I80286_VECTOR:      sprintf(info->s, "V:%02X", cpustate->int_vector); break;
509523      case CPUINFO_STR_REGISTER + I80286_MSW:         sprintf(info->s, "MSW:%04X", cpustate->msw); break;
510      case CPUINFO_STR_REGISTER + I80286_TR_BASE:      sprintf(info->s, "GDTR: %06X", cpustate->gdtr.base); break;
511      case CPUINFO_STR_REGISTER + I80286_TR_LIMIT:   sprintf(info->s, "%04X", cpustate->gdtr.limit); break;
512      case CPUINFO_STR_REGISTER + I80286_GDTR_BASE:   sprintf(info->s, "IDTR: %06X", cpustate->idtr.base); break;
513      case CPUINFO_STR_REGISTER + I80286_GDTR_LIMIT:   sprintf(info->s, "%04X", cpustate->idtr.limit); break;
514      case CPUINFO_STR_REGISTER + I80286_LDTR_BASE:   sprintf(info->s, "LDTR:%04X %02X", cpustate->ldtr.sel, cpustate->ldtr.rights); break;
515      case CPUINFO_STR_REGISTER + I80286_LDTR_LIMIT:   sprintf(info->s, "%06X %04X", cpustate->ldtr.base, cpustate->ldtr.limit); break;
516      case CPUINFO_STR_REGISTER + I80286_IDTR_BASE:   sprintf(info->s, "IDTR: %06X", cpustate->idtr.base); break;
517      case CPUINFO_STR_REGISTER + I80286_IDTR_LIMIT:   sprintf(info->s, "%04X", cpustate->idtr.limit); break;
524      case CPUINFO_STR_REGISTER + I80286_TR_BASE:      sprintf(info->s, "TRBASE: %06X", cpustate->tr.base); break;
525      case CPUINFO_STR_REGISTER + I80286_TR_LIMIT:   sprintf(info->s, "TRLIM: %04X", cpustate->tr.limit); break;
526      case CPUINFO_STR_REGISTER + I80286_GDTR_BASE:   sprintf(info->s, "GDTRBASE: %06X", cpustate->gdtr.base); break;
527      case CPUINFO_STR_REGISTER + I80286_GDTR_LIMIT:   sprintf(info->s, "GDTRLIM: %04X", cpustate->gdtr.limit); break;
528      case CPUINFO_STR_REGISTER + I80286_LDTR_BASE:   sprintf(info->s, "LDTR: %04X %02X", cpustate->ldtr.sel, cpustate->ldtr.rights); break;
529      case CPUINFO_STR_REGISTER + I80286_LDTR_LIMIT:   sprintf(info->s, "LDTRDESC: %06X %04X", cpustate->ldtr.base, cpustate->ldtr.limit); break;
530      case CPUINFO_STR_REGISTER + I80286_IDTR_BASE:   sprintf(info->s, "IDTRBASE: %06X", cpustate->idtr.base); break;
531      case CPUINFO_STR_REGISTER + I80286_IDTR_LIMIT:   sprintf(info->s, "IDTRLIM: %04X", cpustate->idtr.limit); break;
518532   }
519533}
520534
src/emu/cpu/i86/table186.h
r11568r11569
210210    PREFIX86(_iret),              /* 0xcf */
211211    PREFIX86(_rotshft_b),         /* 0xd0 */
212212    PREFIX86(_rotshft_w),         /* 0xd1 */
213    PREFIX86(_rotshft_bcl),       /* 0xd2 */
214    PREFIX86(_rotshft_wcl),       /* 0xd3 */
213    PREFIX186(_rotshft_bcl),       /* 0xd2 */
214    PREFIX186(_rotshft_wcl),       /* 0xd3 */
215215    PREFIX86(_aam),               /* 0xd4 */
216216    PREFIX86(_aad),               /* 0xd5 */
217217    PREFIX86(_invalid),
r11568r11569
473473   case 0xcf:    PREFIX86(_iret)(cpustate); break;\
474474        case 0xd0:    PREFIX86(_rotshft_b)(cpustate); break;\
475475        case 0xd1:    PREFIX86(_rotshft_w)(cpustate); break;\
476        case 0xd2:    PREFIX86(_rotshft_bcl)(cpustate); break;\
477        case 0xd3:    PREFIX86(_rotshft_wcl)(cpustate); break;\
476        case 0xd2:    PREFIX186(_rotshft_bcl)(cpustate); break;\
477        case 0xd3:    PREFIX186(_rotshft_wcl)(cpustate); break;\
478478   case 0xd4:    PREFIX86(_aam)(cpustate); break;\
479479   case 0xd5:    PREFIX86(_aad)(cpustate); break;\
480480   case 0xd6:    PREFIX86(_invalid)(cpustate); break;\
src/emu/cpu/i86/table286.h
r11568r11569
162162    PREFIX86(_call_far),         /* 0x9a */
163163    PREFIX86(_wait),            /* 0x9b */
164164    PREFIX86(_pushf),            /* 0x9c */
165    PREFIX86(_popf),            /* 0x9d */
165    PREFIX286(_popf),            /* 0x9d */
166166    PREFIX86(_sahf),            /* 0x9e */
167167    PREFIX86(_lahf),            /* 0x9f */
168168    PREFIX86(_mov_aldisp),       /* 0xa0 */
r11568r11569
207207    PREFIX86(_mov_wd16),         /* 0xc7 */
208208    PREFIX186(_enter),          /* 0xc8 */
209209    PREFIX186(_leave),          /* 0xc9 */
210    PREFIX86(_retf_d16),         /* 0xca */
211    PREFIX86(_retf),            /* 0xcb */
210    PREFIX286(_retf_d16),         /* 0xca */
211    PREFIX286(_retf),            /* 0xcb */
212212    PREFIX86(_int3),            /* 0xcc */
213213    PREFIX86(_int),            /* 0xcd */
214214    PREFIX86(_into),            /* 0xce */
215    PREFIX86(_iret),            /* 0xcf */
215    PREFIX286(_iret),            /* 0xcf */
216216    PREFIX86(_rotshft_b),         /* 0xd0 */
217217    PREFIX86(_rotshft_w),         /* 0xd1 */
218    PREFIX86(_rotshft_bcl),      /* 0xd2 */
219    PREFIX86(_rotshft_wcl),      /* 0xd3 */
218    PREFIX186(_rotshft_bcl),      /* 0xd2 */
219    PREFIX186(_rotshft_wcl),      /* 0xd3 */
220220    PREFIX86(_aam),            /* 0xd4 */
221221    PREFIX86(_aad),            /* 0xd5 */
222222    PREFIX286(_invalid),
r11568r11569
425425   case 0x9a: PREFIX86(_call_far)(cpustate); break;\
426426   case 0x9b: PREFIX86(_wait)(cpustate); break;\
427427   case 0x9c: PREFIX86(_pushf)(cpustate); break;\
428   case 0x9d: PREFIX86(_popf)(cpustate); break;\
428   case 0x9d: PREFIX286(_popf)(cpustate); break;\
429429   case 0x9e: PREFIX86(_sahf)(cpustate); break;\
430430   case 0x9f: PREFIX86(_lahf)(cpustate); break;\
431431   case 0xa0: PREFIX86(_mov_aldisp)(cpustate); break;\
r11568r11569
470470   case 0xc7: PREFIX86(_mov_wd16)(cpustate); break;\
471471   case 0xc8: PREFIX186(_enter)(cpustate); break;\
472472   case 0xc9: PREFIX186(_leave)(cpustate); break;\
473   case 0xca: PREFIX86(_retf_d16)(cpustate); break;\
474   case 0xcb: PREFIX86(_retf)(cpustate); break;\
473   case 0xca: PREFIX286(_retf_d16)(cpustate); break;\
474   case 0xcb: PREFIX286(_retf)(cpustate); break;\
475475   case 0xcc: PREFIX86(_int3)(cpustate); break;\
476476   case 0xcd: PREFIX86(_int)(cpustate); break;\
477477   case 0xce: PREFIX86(_into)(cpustate); break;\
478   case 0xcf: PREFIX86(_iret)(cpustate); break;\
478   case 0xcf: PREFIX286(_iret)(cpustate); break;\
479479   case 0xd0: PREFIX86(_rotshft_b)(cpustate); break;\
480480   case 0xd1: PREFIX86(_rotshft_w)(cpustate); break;\
481   case 0xd2: PREFIX86(_rotshft_bcl)(cpustate); break;\
482   case 0xd3: PREFIX86(_rotshft_wcl)(cpustate); break;\
481   case 0xd2: PREFIX186(_rotshft_bcl)(cpustate); break;\
482   case 0xd3: PREFIX186(_rotshft_wcl)(cpustate); break;\
483483   case 0xd4: PREFIX86(_aam)(cpustate); break;\
484484   case 0xd5: PREFIX86(_aad)(cpustate); break;\
485485   case 0xd6: PREFIX286(_invalid)(cpustate); break;\
src/emu/cpu/i86/i286.h
r11568r11569
4242   I80286_IRQ_STATE
4343};
4444
45#define TRAP(fault, code)  (UINT32)(((fault&0xffff)<<16)|(code&0xffff))
46
4547/* Public functions */
4648DECLARE_LEGACY_CPU_DEVICE(I80286, i80286);
4749

Previous 15994 Revisions Next


© 1998-2018 The MESS Team