GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / cfe / cfe / x86emu / decode.c
blob10082ebf35a512b1803de9a3b4cbf91c604403eb
1 /****************************************************************************
3 * Realmode X86 Emulator Library
5 * Copyright (C) 1996-1999 SciTech Software, Inc.
6 * Copyright (C) David Mosberger-Tang
7 * Copyright (C) 1999 Egbert Eich
9 * ========================================================================
11 * Permission to use, copy, modify, distribute, and sell this software and
12 * its documentation for any purpose is hereby granted without fee,
13 * provided that the above copyright notice appear in all copies and that
14 * both that copyright notice and this permission notice appear in
15 * supporting documentation, and that the name of the authors not be used
16 * in advertising or publicity pertaining to distribution of the software
17 * without specific, written prior permission. The authors makes no
18 * representations about the suitability of this software for any purpose.
19 * It is provided "as is" without express or implied warranty.
21 * THE AUTHORS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
22 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
23 * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
24 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
25 * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
26 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
27 * PERFORMANCE OF THIS SOFTWARE.
29 * ========================================================================
31 * Language: ANSI C
32 * Environment: Any
33 * Developer: Kendall Bennett
35 * Description: This file includes subroutines which are related to
36 * instruction decoding and accessess of immediate data via IP. etc.
38 ****************************************************************************/
40 /* $XFree86: xc/extras/x86emu/src/x86emu/decode.c,v 1.8 2000/12/13 03:19:34 tsi Exp $ */
42 #include "x86emu/x86emui.h"
43 #include "cfe_console.h"
45 /*----------------------------- Implementation ----------------------------*/
47 /****************************************************************************
48 REMARKS:
49 Handles any pending asychronous interrupts.
50 ****************************************************************************/
51 static void x86emu_intr_handle(void)
53 u8 intno;
55 if (M.x86.intr & INTR_SYNCH) {
56 intno = M.x86.intno;
57 if (_X86EMU_intrTab[intno]) {
58 (*_X86EMU_intrTab[intno])(intno);
59 } else {
60 push_word((u16)M.x86.R_FLG);
61 CLEAR_FLAG(F_IF);
62 CLEAR_FLAG(F_TF);
63 push_word(M.x86.R_CS);
64 M.x86.R_CS = mem_access_word(intno * 4 + 2);
65 push_word(M.x86.R_IP);
66 M.x86.R_IP = mem_access_word(intno * 4);
67 M.x86.intr = 0;
72 /****************************************************************************
73 PARAMETERS:
74 intrnum - Interrupt number to raise
76 REMARKS:
77 Raise the specified interrupt to be handled before the execution of the
78 next instruction.
79 ****************************************************************************/
80 void x86emu_intr_raise(
81 u8 intrnum)
83 M.x86.intno = intrnum;
84 M.x86.intr |= INTR_SYNCH;
87 #ifdef DEBUG
88 extern u8 inb(u32);
89 #endif
91 /****************************************************************************
92 REMARKS:
93 Main execution loop for the emulator. We return from here when the system
94 halts, which is normally caused by a stack fault when we return from the
95 original real mode call.
96 ****************************************************************************/
97 void X86EMU_exec(void)
99 u8 op1;
101 M.x86.intr = 0;
102 DB(x86emu_end_instr();)
104 for (;;) {
105 DB( if (CHECK_IP_FETCH())
106 x86emu_check_ip_access();)
107 /* If debugging, save the IP and CS values. */
108 SAVE_IP_CS(M.x86.R_CS, M.x86.R_IP);
109 INC_DECODED_INST_LEN(1);
110 if (M.x86.intr) {
111 if (M.x86.intr & INTR_HALTED) {
112 DB( printk("halted\n");
113 X86EMU_trace_regs();)
114 return;
116 if (((M.x86.intr & INTR_SYNCH) && (M.x86.intno == 0 || M.x86.intno == 2)) ||
117 !ACCESS_FLAG(F_IF)) {
118 x86emu_intr_handle();
121 #ifdef DEBUG
122 if (inb(0x3f8 + 5) & 1) {
123 M.x86.debug |= DEBUG_STEP_F | DEBUG_DECODE_F | DEBUG_TRACE_F;
125 #endif
126 op1 = (*sys_rdb)(((u32)M.x86.R_CS << 4) + (M.x86.R_IP++));
127 (*x86emu_optab[op1])(op1);
131 /****************************************************************************
132 REMARKS:
133 Halts the system by setting the halted system flag.
134 ****************************************************************************/
135 void X86EMU_halt_sys(void)
137 M.x86.intr |= INTR_HALTED;
140 /****************************************************************************
141 PARAMETERS:
142 mod - Mod value from decoded byte
143 regh - Reg h value from decoded byte
144 regl - Reg l value from decoded byte
146 REMARKS:
147 Raise the specified interrupt to be handled before the execution of the
148 next instruction.
150 NOTE: Do not inline this function, as (*sys_rdb) is already inline!
151 ****************************************************************************/
152 void fetch_decode_modrm(
153 int *mod,
154 int *regh,
155 int *regl)
157 int fetched;
159 DB( if (CHECK_IP_FETCH())
160 x86emu_check_ip_access();)
161 fetched = (*sys_rdb)(((u32)M.x86.R_CS << 4) + (M.x86.R_IP++));
162 INC_DECODED_INST_LEN(1);
163 *mod = (fetched >> 6) & 0x03;
164 *regh = (fetched >> 3) & 0x07;
165 *regl = (fetched >> 0) & 0x07;
168 /****************************************************************************
169 RETURNS:
170 Immediate byte value read from instruction queue
172 REMARKS:
173 This function returns the immediate byte from the instruction queue, and
174 moves the instruction pointer to the next value.
176 NOTE: Do not inline this function, as (*sys_rdb) is already inline!
177 ****************************************************************************/
178 u8 fetch_byte_imm(void)
180 u8 fetched;
182 DB( if (CHECK_IP_FETCH())
183 x86emu_check_ip_access();)
184 fetched = (*sys_rdb)(((u32)M.x86.R_CS << 4) + (M.x86.R_IP++));
185 INC_DECODED_INST_LEN(1);
186 return fetched;
189 /****************************************************************************
190 RETURNS:
191 Immediate word value read from instruction queue
193 REMARKS:
194 This function returns the immediate byte from the instruction queue, and
195 moves the instruction pointer to the next value.
197 NOTE: Do not inline this function, as (*sys_rdw) is already inline!
198 ****************************************************************************/
199 u16 fetch_word_imm(void)
201 u16 fetched;
203 DB( if (CHECK_IP_FETCH())
204 x86emu_check_ip_access();)
205 fetched = (*sys_rdw)(((u32)M.x86.R_CS << 4) + (M.x86.R_IP));
206 M.x86.R_IP += 2;
207 INC_DECODED_INST_LEN(2);
208 return fetched;
211 /****************************************************************************
212 RETURNS:
213 Immediate lone value read from instruction queue
215 REMARKS:
216 This function returns the immediate byte from the instruction queue, and
217 moves the instruction pointer to the next value.
219 NOTE: Do not inline this function, as (*sys_rdw) is already inline!
220 ****************************************************************************/
221 u32 fetch_long_imm(void)
223 u32 fetched;
225 DB( if (CHECK_IP_FETCH())
226 x86emu_check_ip_access();)
227 fetched = (*sys_rdl)(((u32)M.x86.R_CS << 4) + (M.x86.R_IP));
228 M.x86.R_IP += 4;
229 INC_DECODED_INST_LEN(4);
230 return fetched;
233 /****************************************************************************
234 RETURNS:
235 Value of the default data segment
237 REMARKS:
238 Inline function that returns the default data segment for the current
239 instruction.
241 On the x86 processor, the default segment is not always DS if there is
242 no segment override. Address modes such as -3[BP] or 10[BP+SI] all refer to
243 addresses relative to SS (ie: on the stack). So, at the minimum, all
244 decodings of addressing modes would have to set/clear a bit describing
245 whether the access is relative to DS or SS. That is the function of the
246 cpu-state-varible M.x86.mode. There are several potential states:
248 repe prefix seen (handled elsewhere)
249 repne prefix seen (ditto)
251 cs segment override
252 ds segment override
253 es segment override
254 fs segment override
255 gs segment override
256 ss segment override
258 ds/ss select (in absense of override)
260 Each of the above 7 items are handled with a bit in the mode field.
261 ****************************************************************************/
262 _INLINE u32 get_data_segment(void)
264 #define GET_SEGMENT(segment)
265 switch (M.x86.mode & SYSMODE_SEGMASK) {
266 case 0: /* default case: use ds register */
267 case SYSMODE_SEGOVR_DS:
268 case SYSMODE_SEGOVR_DS | SYSMODE_SEG_DS_SS:
269 return M.x86.R_DS;
270 case SYSMODE_SEG_DS_SS: /* non-overridden, use ss register */
271 return M.x86.R_SS;
272 case SYSMODE_SEGOVR_CS:
273 case SYSMODE_SEGOVR_CS | SYSMODE_SEG_DS_SS:
274 return M.x86.R_CS;
275 case SYSMODE_SEGOVR_ES:
276 case SYSMODE_SEGOVR_ES | SYSMODE_SEG_DS_SS:
277 return M.x86.R_ES;
278 case SYSMODE_SEGOVR_FS:
279 case SYSMODE_SEGOVR_FS | SYSMODE_SEG_DS_SS:
280 return M.x86.R_FS;
281 case SYSMODE_SEGOVR_GS:
282 case SYSMODE_SEGOVR_GS | SYSMODE_SEG_DS_SS:
283 return M.x86.R_GS;
284 case SYSMODE_SEGOVR_SS:
285 case SYSMODE_SEGOVR_SS | SYSMODE_SEG_DS_SS:
286 return M.x86.R_SS;
287 default:
288 #ifdef DEBUG
289 printk("error: should not happen: multiple overrides.\n");
290 #endif
291 HALT_SYS();
292 return 0;
296 /****************************************************************************
297 PARAMETERS:
298 offset - Offset to load data from
300 RETURNS:
301 Byte value read from the absolute memory location.
303 NOTE: Do not inline this function as (*sys_rdX) is already inline!
304 ****************************************************************************/
305 u8 fetch_data_byte(
306 uint offset)
308 #ifdef DEBUG
309 if (CHECK_DATA_ACCESS())
310 x86emu_check_data_access((u16)get_data_segment(), offset);
311 #endif
312 return (*sys_rdb)((get_data_segment() << 4) + offset);
315 /****************************************************************************
316 PARAMETERS:
317 offset - Offset to load data from
319 RETURNS:
320 Word value read from the absolute memory location.
322 NOTE: Do not inline this function as (*sys_rdX) is already inline!
323 ****************************************************************************/
324 u16 fetch_data_word(
325 uint offset)
327 #ifdef DEBUG
328 if (CHECK_DATA_ACCESS())
329 x86emu_check_data_access((u16)get_data_segment(), offset);
330 #endif
331 return (*sys_rdw)((get_data_segment() << 4) + offset);
334 /****************************************************************************
335 PARAMETERS:
336 offset - Offset to load data from
338 RETURNS:
339 Long value read from the absolute memory location.
341 NOTE: Do not inline this function as (*sys_rdX) is already inline!
342 ****************************************************************************/
343 u32 fetch_data_long(
344 uint offset)
346 #ifdef DEBUG
347 if (CHECK_DATA_ACCESS())
348 x86emu_check_data_access((u16)get_data_segment(), offset);
349 #endif
350 return (*sys_rdl)((get_data_segment() << 4) + offset);
353 /****************************************************************************
354 PARAMETERS:
355 segment - Segment to load data from
356 offset - Offset to load data from
358 RETURNS:
359 Byte value read from the absolute memory location.
361 NOTE: Do not inline this function as (*sys_rdX) is already inline!
362 ****************************************************************************/
363 u8 fetch_data_byte_abs(
364 uint segment,
365 uint offset)
367 #ifdef DEBUG
368 if (CHECK_DATA_ACCESS())
369 x86emu_check_data_access(segment, offset);
370 #endif
371 return (*sys_rdb)(((u32)segment << 4) + offset);
374 /****************************************************************************
375 PARAMETERS:
376 segment - Segment to load data from
377 offset - Offset to load data from
379 RETURNS:
380 Word value read from the absolute memory location.
382 NOTE: Do not inline this function as (*sys_rdX) is already inline!
383 ****************************************************************************/
384 u16 fetch_data_word_abs(
385 uint segment,
386 uint offset)
388 #ifdef DEBUG
389 if (CHECK_DATA_ACCESS())
390 x86emu_check_data_access(segment, offset);
391 #endif
392 return (*sys_rdw)(((u32)segment << 4) + offset);
395 /****************************************************************************
396 PARAMETERS:
397 segment - Segment to load data from
398 offset - Offset to load data from
400 RETURNS:
401 Long value read from the absolute memory location.
403 NOTE: Do not inline this function as (*sys_rdX) is already inline!
404 ****************************************************************************/
405 u32 fetch_data_long_abs(
406 uint segment,
407 uint offset)
409 #ifdef DEBUG
410 if (CHECK_DATA_ACCESS())
411 x86emu_check_data_access(segment, offset);
412 #endif
413 return (*sys_rdl)(((u32)segment << 4) + offset);
416 /****************************************************************************
417 PARAMETERS:
418 offset - Offset to store data at
419 val - Value to store
421 REMARKS:
422 Writes a word value to an segmented memory location. The segment used is
423 the current 'default' segment, which may have been overridden.
425 NOTE: Do not inline this function as (*sys_wrX) is already inline!
426 ****************************************************************************/
427 void store_data_byte(
428 uint offset,
429 u8 val)
431 #ifdef DEBUG
432 if (CHECK_DATA_ACCESS())
433 x86emu_check_data_access((u16)get_data_segment(), offset);
434 #endif
435 (*sys_wrb)((get_data_segment() << 4) + offset, val);
438 /****************************************************************************
439 PARAMETERS:
440 offset - Offset to store data at
441 val - Value to store
443 REMARKS:
444 Writes a word value to an segmented memory location. The segment used is
445 the current 'default' segment, which may have been overridden.
447 NOTE: Do not inline this function as (*sys_wrX) is already inline!
448 ****************************************************************************/
449 void store_data_word(
450 uint offset,
451 u16 val)
453 #ifdef DEBUG
454 if (CHECK_DATA_ACCESS())
455 x86emu_check_data_access((u16)get_data_segment(), offset);
456 #endif
457 (*sys_wrw)((get_data_segment() << 4) + offset, val);
460 /****************************************************************************
461 PARAMETERS:
462 offset - Offset to store data at
463 val - Value to store
465 REMARKS:
466 Writes a long value to an segmented memory location. The segment used is
467 the current 'default' segment, which may have been overridden.
469 NOTE: Do not inline this function as (*sys_wrX) is already inline!
470 ****************************************************************************/
471 void store_data_long(
472 uint offset,
473 u32 val)
475 #ifdef DEBUG
476 if (CHECK_DATA_ACCESS())
477 x86emu_check_data_access((u16)get_data_segment(), offset);
478 #endif
479 (*sys_wrl)((get_data_segment() << 4) + offset, val);
482 /****************************************************************************
483 PARAMETERS:
484 segment - Segment to store data at
485 offset - Offset to store data at
486 val - Value to store
488 REMARKS:
489 Writes a byte value to an absolute memory location.
491 NOTE: Do not inline this function as (*sys_wrX) is already inline!
492 ****************************************************************************/
493 void store_data_byte_abs(
494 uint segment,
495 uint offset,
496 u8 val)
498 #ifdef DEBUG
499 if (CHECK_DATA_ACCESS())
500 x86emu_check_data_access(segment, offset);
501 #endif
502 (*sys_wrb)(((u32)segment << 4) + offset, val);
505 /****************************************************************************
506 PARAMETERS:
507 segment - Segment to store data at
508 offset - Offset to store data at
509 val - Value to store
511 REMARKS:
512 Writes a word value to an absolute memory location.
514 NOTE: Do not inline this function as (*sys_wrX) is already inline!
515 ****************************************************************************/
516 void store_data_word_abs(
517 uint segment,
518 uint offset,
519 u16 val)
521 #ifdef DEBUG
522 if (CHECK_DATA_ACCESS())
523 x86emu_check_data_access(segment, offset);
524 #endif
525 (*sys_wrw)(((u32)segment << 4) + offset, val);
528 /****************************************************************************
529 PARAMETERS:
530 segment - Segment to store data at
531 offset - Offset to store data at
532 val - Value to store
534 REMARKS:
535 Writes a long value to an absolute memory location.
537 NOTE: Do not inline this function as (*sys_wrX) is already inline!
538 ****************************************************************************/
539 void store_data_long_abs(
540 uint segment,
541 uint offset,
542 u32 val)
544 #ifdef DEBUG
545 if (CHECK_DATA_ACCESS())
546 x86emu_check_data_access(segment, offset);
547 #endif
548 (*sys_wrl)(((u32)segment << 4) + offset, val);
551 /****************************************************************************
552 PARAMETERS:
553 reg - Register to decode
555 RETURNS:
556 Pointer to the appropriate register
558 REMARKS:
559 Return a pointer to the register given by the R/RM field of the
560 modrm byte, for byte operands. Also enables the decoding of instructions.
561 ****************************************************************************/
562 u8* decode_rm_byte_register(
563 int reg)
565 switch (reg) {
566 case 0:
567 DECODE_PRINTF("AL");
568 return &M.x86.R_AL;
569 case 1:
570 DECODE_PRINTF("CL");
571 return &M.x86.R_CL;
572 case 2:
573 DECODE_PRINTF("DL");
574 return &M.x86.R_DL;
575 case 3:
576 DECODE_PRINTF("BL");
577 return &M.x86.R_BL;
578 case 4:
579 DECODE_PRINTF("AH");
580 return &M.x86.R_AH;
581 case 5:
582 DECODE_PRINTF("CH");
583 return &M.x86.R_CH;
584 case 6:
585 DECODE_PRINTF("DH");
586 return &M.x86.R_DH;
587 case 7:
588 DECODE_PRINTF("BH");
589 return &M.x86.R_BH;
591 HALT_SYS();
592 return NULL; /* NOT REACHED OR REACHED ON ERROR */
595 /****************************************************************************
596 PARAMETERS:
597 reg - Register to decode
599 RETURNS:
600 Pointer to the appropriate register
602 REMARKS:
603 Return a pointer to the register given by the R/RM field of the
604 modrm byte, for word operands. Also enables the decoding of instructions.
605 ****************************************************************************/
606 u16* decode_rm_word_register(
607 int reg)
609 switch (reg) {
610 case 0:
611 DECODE_PRINTF("AX");
612 return &M.x86.R_AX;
613 case 1:
614 DECODE_PRINTF("CX");
615 return &M.x86.R_CX;
616 case 2:
617 DECODE_PRINTF("DX");
618 return &M.x86.R_DX;
619 case 3:
620 DECODE_PRINTF("BX");
621 return &M.x86.R_BX;
622 case 4:
623 DECODE_PRINTF("SP");
624 return &M.x86.R_SP;
625 case 5:
626 DECODE_PRINTF("BP");
627 return &M.x86.R_BP;
628 case 6:
629 DECODE_PRINTF("SI");
630 return &M.x86.R_SI;
631 case 7:
632 DECODE_PRINTF("DI");
633 return &M.x86.R_DI;
635 HALT_SYS();
636 return NULL; /* NOTREACHED OR REACHED ON ERROR */
639 /****************************************************************************
640 PARAMETERS:
641 reg - Register to decode
643 RETURNS:
644 Pointer to the appropriate register
646 REMARKS:
647 Return a pointer to the register given by the R/RM field of the
648 modrm byte, for dword operands. Also enables the decoding of instructions.
649 ****************************************************************************/
650 u32* decode_rm_long_register(
651 int reg)
653 switch (reg) {
654 case 0:
655 DECODE_PRINTF("EAX");
656 return &M.x86.R_EAX;
657 case 1:
658 DECODE_PRINTF("ECX");
659 return &M.x86.R_ECX;
660 case 2:
661 DECODE_PRINTF("EDX");
662 return &M.x86.R_EDX;
663 case 3:
664 DECODE_PRINTF("EBX");
665 return &M.x86.R_EBX;
666 case 4:
667 DECODE_PRINTF("ESP");
668 return &M.x86.R_ESP;
669 case 5:
670 DECODE_PRINTF("EBP");
671 return &M.x86.R_EBP;
672 case 6:
673 DECODE_PRINTF("ESI");
674 return &M.x86.R_ESI;
675 case 7:
676 DECODE_PRINTF("EDI");
677 return &M.x86.R_EDI;
679 HALT_SYS();
680 return NULL; /* NOTREACHED OR REACHED ON ERROR */
683 /****************************************************************************
684 PARAMETERS:
685 reg - Register to decode
687 RETURNS:
688 Pointer to the appropriate register
690 REMARKS:
691 Return a pointer to the register given by the R/RM field of the
692 modrm byte, for word operands, modified from above for the weirdo
693 special case of segreg operands. Also enables the decoding of instructions.
694 ****************************************************************************/
695 u16* decode_rm_seg_register(
696 int reg)
698 switch (reg) {
699 case 0:
700 DECODE_PRINTF("ES");
701 return &M.x86.R_ES;
702 case 1:
703 DECODE_PRINTF("CS");
704 return &M.x86.R_CS;
705 case 2:
706 DECODE_PRINTF("SS");
707 return &M.x86.R_SS;
708 case 3:
709 DECODE_PRINTF("DS");
710 return &M.x86.R_DS;
711 case 4:
712 case 5:
713 case 6:
714 case 7:
715 DECODE_PRINTF("ILLEGAL SEGREG");
716 break;
718 HALT_SYS();
719 return NULL; /* NOT REACHED OR REACHED ON ERROR */
723 /****************************************************************************
724 PARAMETERS:
725 sib - SIB value to decode
727 RETURNS:
728 Offset in memory for the address decoding
730 REMARKS:
731 Return the offset given by the specified SIB byte.
733 NOTE: The code which specifies the corresponding segment (ds vs ss)
734 below in the case of [BP+..]. The assumption here is that at the
735 point that this subroutine is called, the bit corresponding to
736 SYSMODE_SEG_DS_SS will be zero. After every instruction
737 except the segment override instructions, this bit (as well
738 as any bits indicating segment overrides) will be clear. So
739 if a SS access is needed, set this bit. Otherwise, DS access
740 occurs (unless any of the segment override bits are set).
741 ****************************************************************************/
742 static unsigned decode_sib_address(
743 int sib)
745 unsigned ss,index,base;
746 unsigned addr = 0;
748 ss = (sib >> 6) & 3;
749 index = (sib >> 3) & 7;
750 base = (sib & 7);
752 switch (base) {
753 case 0:
754 DECODE_PRINTF("[EAX");
755 addr = M.x86.R_EAX;
756 break;
757 case 1:
758 DECODE_PRINTF("[ECX");
759 addr = M.x86.R_ECX;
760 break;
761 case 2:
762 DECODE_PRINTF("[EDX");
763 addr = M.x86.R_EDX;
764 break;
765 case 3:
766 DECODE_PRINTF("[EBX");
767 addr = M.x86.R_EBX;
768 break;
769 case 4:
770 DECODE_PRINTF("[ESP");
771 addr = M.x86.R_ESP;
772 break;
773 case 5:
774 DECODE_PRINTF("[invalid");
775 addr = M.x86.R_ESP;/* incorrect */
776 break;
777 case 6:
778 DECODE_PRINTF("[invalid");
779 addr = M.x86.R_ESP;/* incorrect */
780 break;
781 case 7:
782 DECODE_PRINTF("[invalid");
783 addr = M.x86.R_ESP; /* incorrect */
784 break;
787 switch (index) {
788 case 0:
789 DECODE_PRINTF("+EAX");
790 addr += M.x86.R_EAX*(1<<ss);
791 break;
792 case 1:
793 DECODE_PRINTF("+ECX");
794 addr += M.x86.R_ECX*(1<<ss);
795 break;
796 case 2:
797 DECODE_PRINTF("+EDX");
798 addr += M.x86.R_EDX*(1<<ss);
799 break;
800 case 3:
801 DECODE_PRINTF("+EBX");
802 addr += M.x86.R_EBX*(1<<ss);
803 break;
804 case 4:
805 break;
806 case 5:
807 DECODE_PRINTF("+EBP");
808 addr += M.x86.R_EBP*(1<<ss);
809 break;
810 case 6:
811 DECODE_PRINTF("+ESI");
812 addr += M.x86.R_ESI*(1<<ss);
813 break;
814 case 7:
815 DECODE_PRINTF("+EDI");
816 addr += M.x86.R_EDI*(1<<ss);
817 break;
820 switch (ss) {
821 case 0:
822 DECODE_PRINTF("]");
823 break;
824 case 1:
825 DECODE_PRINTF("*2]");
826 break;
827 case 2:
828 DECODE_PRINTF("*4]");
829 break;
830 case 3:
831 DECODE_PRINTF("*8]");
832 break;
835 return addr;
839 /****************************************************************************
840 PARAMETERS:
841 rm - RM value to decode
843 RETURNS:
844 Offset in memory for the address decoding
846 REMARKS:
847 Return the offset given by mod=00 addressing. Also enables the
848 decoding of instructions.
850 NOTE: The code which specifies the corresponding segment (ds vs ss)
851 below in the case of [BP+..]. The assumption here is that at the
852 point that this subroutine is called, the bit corresponding to
853 SYSMODE_SEG_DS_SS will be zero. After every instruction
854 except the segment override instructions, this bit (as well
855 as any bits indicating segment overrides) will be clear. So
856 if a SS access is needed, set this bit. Otherwise, DS access
857 occurs (unless any of the segment override bits are set).
858 ****************************************************************************/
859 unsigned decode_rm00_address(
860 int rm)
862 unsigned offset;
864 switch (rm) {
865 case 0:
866 DECODE_PRINTF("[BX+SI]");
867 return M.x86.R_BX + M.x86.R_SI;
868 case 1:
869 DECODE_PRINTF("[BX+DI]");
870 return M.x86.R_BX + M.x86.R_DI;
871 case 2:
872 DECODE_PRINTF("[BP+SI]");
873 M.x86.mode |= SYSMODE_SEG_DS_SS;
874 return M.x86.R_BP + M.x86.R_SI;
875 case 3:
876 DECODE_PRINTF("[BP+DI]");
877 M.x86.mode |= SYSMODE_SEG_DS_SS;
878 return M.x86.R_BP + M.x86.R_DI;
879 case 4:
880 /* If SYSMODE_PREFIX_ADDR is set, get the SIB byte */
881 if (M.x86.mode & SYSMODE_PREFIX_ADDR) {
882 unsigned sib;
883 sib = fetch_byte_imm();
884 return decode_sib_address(sib);
886 else {
887 DECODE_PRINTF("[SI]");
888 return M.x86.R_SI;
890 case 5:
891 DECODE_PRINTF("[DI]");
892 return M.x86.R_DI;
893 case 6:
894 offset = fetch_word_imm();
895 DECODE_PRINTF2("[%04x]", offset);
896 return offset;
897 case 7:
898 DECODE_PRINTF("[BX]");
899 return M.x86.R_BX;
901 HALT_SYS();
902 return 0;
905 /****************************************************************************
906 PARAMETERS:
907 rm - RM value to decode
909 RETURNS:
910 Offset in memory for the address decoding
912 REMARKS:
913 Return the offset given by mod=01 addressing. Also enables the
914 decoding of instructions.
915 ****************************************************************************/
916 unsigned decode_rm01_address(
917 int rm)
919 int displacement = (s8)fetch_byte_imm();
920 switch (rm) {
921 case 0:
922 DECODE_PRINTF2("%d[BX+SI]", displacement);
923 return M.x86.R_BX + M.x86.R_SI + displacement;
924 case 1:
925 DECODE_PRINTF2("%d[BX+DI]", displacement);
926 return M.x86.R_BX + M.x86.R_DI + displacement;
927 case 2:
928 DECODE_PRINTF2("%d[BP+SI]", displacement);
929 M.x86.mode |= SYSMODE_SEG_DS_SS;
930 return M.x86.R_BP + M.x86.R_SI + displacement;
931 case 3:
932 DECODE_PRINTF2("%d[BP+DI]", displacement);
933 M.x86.mode |= SYSMODE_SEG_DS_SS;
934 return M.x86.R_BP + M.x86.R_DI + displacement;
935 case 4:
936 DECODE_PRINTF2("%d[SI]", displacement);
937 return M.x86.R_SI + displacement;
938 case 5:
939 DECODE_PRINTF2("%d[DI]", displacement);
940 return M.x86.R_DI + displacement;
941 case 6:
942 DECODE_PRINTF2("%d[BP]", displacement);
943 M.x86.mode |= SYSMODE_SEG_DS_SS;
944 return M.x86.R_BP + displacement;
945 case 7:
946 DECODE_PRINTF2("%d[BX]", displacement);
947 return M.x86.R_BX + displacement;
949 HALT_SYS();
950 return 0; /* SHOULD NOT HAPPEN */
953 /****************************************************************************
954 PARAMETERS:
955 rm - RM value to decode
957 RETURNS:
958 Offset in memory for the address decoding
960 REMARKS:
961 Return the offset given by mod=10 addressing. Also enables the
962 decoding of instructions.
963 ****************************************************************************/
964 unsigned decode_rm10_address(
965 int rm)
967 unsigned displacement = (u16)fetch_word_imm();
968 switch (rm) {
969 case 0:
970 DECODE_PRINTF2("%d[BX+SI]", displacement);
971 return M.x86.R_BX + M.x86.R_SI + displacement;
972 case 1:
973 DECODE_PRINTF2("%d[BX+DI]", displacement);
974 return M.x86.R_BX + M.x86.R_DI + displacement;
975 case 2:
976 DECODE_PRINTF2("%d[BP+SI]", displacement);
977 M.x86.mode |= SYSMODE_SEG_DS_SS;
978 return M.x86.R_BP + M.x86.R_SI + displacement;
979 case 3:
980 DECODE_PRINTF2("%d[BP+DI]", displacement);
981 M.x86.mode |= SYSMODE_SEG_DS_SS;
982 return M.x86.R_BP + M.x86.R_DI + displacement;
983 case 4:
984 DECODE_PRINTF2("%d[SI]", displacement);
985 return M.x86.R_SI + displacement;
986 case 5:
987 DECODE_PRINTF2("%d[DI]", displacement);
988 return M.x86.R_DI + displacement;
989 case 6:
990 DECODE_PRINTF2("%d[BP]", displacement);
991 M.x86.mode |= SYSMODE_SEG_DS_SS;
992 return M.x86.R_BP + displacement;
993 case 7:
994 DECODE_PRINTF2("%d[BX]", displacement);
995 return M.x86.R_BX + displacement;
997 HALT_SYS();
998 return 0;
999 /*NOTREACHED */