I\'ve ported my library x86Lib to mbed. It fully emulates the 8086 processor, but a few things I\'m still working on. Notable missing things are interrupts. Previously I used exceptions for interrupts, but exceptions aren\'t supported with the mbed compiler. It is also quite slow

Dependents:   x86Lib_Tester

Committer:
earlz
Date:
Sun Mar 04 08:15:47 2012 +0000
Revision:
0:217a7931b41f

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
earlz 0:217a7931b41f 1 /**
earlz 0:217a7931b41f 2 Copyright (c) 2007 - 2009 Jordan "Earlz/hckr83" Earls <http://www.Earlz.biz.tm>
earlz 0:217a7931b41f 3 All rights reserved.
earlz 0:217a7931b41f 4
earlz 0:217a7931b41f 5 Redistribution and use in source and binary forms, with or without
earlz 0:217a7931b41f 6 modification, are permitted provided that the following conditions
earlz 0:217a7931b41f 7 are met:
earlz 0:217a7931b41f 8
earlz 0:217a7931b41f 9 1. Redistributions of source code must retain the above copyright
earlz 0:217a7931b41f 10 notice, this list of conditions and the following disclaimer.
earlz 0:217a7931b41f 11 2. Redistributions in binary form must reproduce the above copyright
earlz 0:217a7931b41f 12 notice, this list of conditions and the following disclaimer in the
earlz 0:217a7931b41f 13 documentation and/or other materials provided with the distribution.
earlz 0:217a7931b41f 14 3. The name of the author may not be used to endorse or promote products
earlz 0:217a7931b41f 15 derived from this software without specific prior written permission.
earlz 0:217a7931b41f 16
earlz 0:217a7931b41f 17 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
earlz 0:217a7931b41f 18 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
earlz 0:217a7931b41f 19 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
earlz 0:217a7931b41f 20 THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
earlz 0:217a7931b41f 21 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
earlz 0:217a7931b41f 22 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
earlz 0:217a7931b41f 23 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
earlz 0:217a7931b41f 24 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
earlz 0:217a7931b41f 25 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
earlz 0:217a7931b41f 26 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
earlz 0:217a7931b41f 27
earlz 0:217a7931b41f 28 This file is part of the x86Lib project.
earlz 0:217a7931b41f 29 **/
earlz 0:217a7931b41f 30 #define X86LIB_BUILD
earlz 0:217a7931b41f 31 #include <x86Lib.h>
earlz 0:217a7931b41f 32 namespace x86Lib{
earlz 0:217a7931b41f 33 using namespace std;
earlz 0:217a7931b41f 34
earlz 0:217a7931b41f 35
earlz 0:217a7931b41f 36
earlz 0:217a7931b41f 37
earlz 0:217a7931b41f 38
earlz 0:217a7931b41f 39
earlz 0:217a7931b41f 40
earlz 0:217a7931b41f 41
earlz 0:217a7931b41f 42
earlz 0:217a7931b41f 43
earlz 0:217a7931b41f 44 void x86CPU::op16_jmp_rel8(){
earlz 0:217a7931b41f 45 eip++;
earlz 0:217a7931b41f 46 Jmp16_near8(op_cache[1]);
earlz 0:217a7931b41f 47 }
earlz 0:217a7931b41f 48
earlz 0:217a7931b41f 49 void x86CPU::op16_jmp_rel16(){
earlz 0:217a7931b41f 50 eip+=2; //get to last byte of address so jmp works right
earlz 0:217a7931b41f 51 Jmp16_near16(*(uint16_t*)&op_cache[1]);
earlz 0:217a7931b41f 52 }
earlz 0:217a7931b41f 53
earlz 0:217a7931b41f 54 void x86CPU::op16_jmp_imm16_imm16(){ //far jmp
earlz 0:217a7931b41f 55 *(uint32_t*)&op_cache=ReadDword(cCS,eip+1);
earlz 0:217a7931b41f 56 seg[cCS]=*(uint16_t*)&op_cache[2]; //I always forget that they are reversed...
earlz 0:217a7931b41f 57 eip=*(uint16_t*)&op_cache[0];
earlz 0:217a7931b41f 58 eip--; //eip will be incremented in Cycle
earlz 0:217a7931b41f 59 }
earlz 0:217a7931b41f 60
earlz 0:217a7931b41f 61 void x86CPU::op16_jmp_rm16(ModRM16 &rm){
earlz 0:217a7931b41f 62 eip=rm.ReadWordr(); //absolute address...
earlz 0:217a7931b41f 63 eip--;
earlz 0:217a7931b41f 64 }
earlz 0:217a7931b41f 65
earlz 0:217a7931b41f 66 void x86CPU::op16_jmp_m16_m16(ModRM16 &rm){
earlz 0:217a7931b41f 67 *(uint32_t*)&op_cache=rm.ReadDword(); //quicker to use op_cache, than dynamic variables...
earlz 0:217a7931b41f 68 seg[cCS]=*(uint16_t*)&op_cache[2];
earlz 0:217a7931b41f 69 eip=*(uint16_t*)&op_cache[0];
earlz 0:217a7931b41f 70 eip--;
earlz 0:217a7931b41f 71 }
earlz 0:217a7931b41f 72
earlz 0:217a7931b41f 73
earlz 0:217a7931b41f 74
earlz 0:217a7931b41f 75 void x86CPU::op16_jcxz_rel8(){
earlz 0:217a7931b41f 76 if(*regs16[CX]==0){
earlz 0:217a7931b41f 77 Jmp16_near8(op_cache[1]);
earlz 0:217a7931b41f 78 }
earlz 0:217a7931b41f 79 }
earlz 0:217a7931b41f 80
earlz 0:217a7931b41f 81
earlz 0:217a7931b41f 82
earlz 0:217a7931b41f 83
earlz 0:217a7931b41f 84
earlz 0:217a7931b41f 85
earlz 0:217a7931b41f 86 void x86CPU::op16_call_rel16(){
earlz 0:217a7931b41f 87
earlz 0:217a7931b41f 88 Push16(eip+2);
earlz 0:217a7931b41f 89 if(*(uint16_t*)&op_cache[1]<0x8000){
earlz 0:217a7931b41f 90 //eip++;
earlz 0:217a7931b41f 91 }
earlz 0:217a7931b41f 92 eip+=2;
earlz 0:217a7931b41f 93 Jmp16_near16(*(uint16_t*)&op_cache[1]);
earlz 0:217a7931b41f 94 }
earlz 0:217a7931b41f 95 void x86CPU::op16_retn(){
earlz 0:217a7931b41f 96 eip=Pop16();
earlz 0:217a7931b41f 97 //eip--; //because in Cycle, we increment..
earlz 0:217a7931b41f 98 }
earlz 0:217a7931b41f 99
earlz 0:217a7931b41f 100 void x86CPU::op16_loop_rel8(){
earlz 0:217a7931b41f 101 (*regs16[CX])--;
earlz 0:217a7931b41f 102 eip++;
earlz 0:217a7931b41f 103 if(*regs16[CX]!=0){
earlz 0:217a7931b41f 104 Jmp16_near8(op_cache[1]);
earlz 0:217a7931b41f 105 }
earlz 0:217a7931b41f 106 }
earlz 0:217a7931b41f 107
earlz 0:217a7931b41f 108 void x86CPU::op16_loope_rel8(){
earlz 0:217a7931b41f 109 (*regs16[CX])--;
earlz 0:217a7931b41f 110 eip++;
earlz 0:217a7931b41f 111 if((*regs16[CX]!=0) && (freg.zf==1)){
earlz 0:217a7931b41f 112 Jmp16_near8(op_cache[1]);
earlz 0:217a7931b41f 113 }
earlz 0:217a7931b41f 114 }
earlz 0:217a7931b41f 115
earlz 0:217a7931b41f 116 void x86CPU::op16_loopne_rel8(){
earlz 0:217a7931b41f 117 (*regs16[CX])--;
earlz 0:217a7931b41f 118 eip++;
earlz 0:217a7931b41f 119 if((*regs16[CX]!=0) && (freg.zf==0)){
earlz 0:217a7931b41f 120 Jmp16_near8(op_cache[1]);
earlz 0:217a7931b41f 121 }
earlz 0:217a7931b41f 122 }
earlz 0:217a7931b41f 123
earlz 0:217a7931b41f 124
earlz 0:217a7931b41f 125 void x86CPU::op16_call_imm16_imm16(){ //far call
earlz 0:217a7931b41f 126 Push16(seg[cCS]);
earlz 0:217a7931b41f 127 Push16(eip+4);
earlz 0:217a7931b41f 128 *(uint32_t*)&op_cache=ReadDword(cCS,eip+1);
earlz 0:217a7931b41f 129 seg[cCS]=*(uint16_t*)&op_cache[2]; //I always forget that they are reversed...
earlz 0:217a7931b41f 130 eip=*(uint16_t*)&op_cache[0];
earlz 0:217a7931b41f 131 eip--; //eip will be incremented in Cycle
earlz 0:217a7931b41f 132 }
earlz 0:217a7931b41f 133
earlz 0:217a7931b41f 134 void x86CPU::op16_retf(){
earlz 0:217a7931b41f 135 eip=Pop16();
earlz 0:217a7931b41f 136 seg[cCS]=Pop16();
earlz 0:217a7931b41f 137 }
earlz 0:217a7931b41f 138
earlz 0:217a7931b41f 139 void x86CPU::op16_int_imm8(){
earlz 0:217a7931b41f 140 eip++;
earlz 0:217a7931b41f 141 Int16(op_cache[1]);
earlz 0:217a7931b41f 142 }
earlz 0:217a7931b41f 143
earlz 0:217a7931b41f 144 void x86CPU::op16_iret(){
earlz 0:217a7931b41f 145 eip=Pop16();
earlz 0:217a7931b41f 146 seg[cCS]=Pop16();
earlz 0:217a7931b41f 147 *(uint16_t*)&freg=Pop16();
earlz 0:217a7931b41f 148 }
earlz 0:217a7931b41f 149
earlz 0:217a7931b41f 150 void x86CPU::op16_int3(){
earlz 0:217a7931b41f 151 Int16(3);
earlz 0:217a7931b41f 152 }
earlz 0:217a7931b41f 153
earlz 0:217a7931b41f 154 void x86CPU::op16_into(){
earlz 0:217a7931b41f 155 if(freg.of==1){
earlz 0:217a7931b41f 156 Int16(4);
earlz 0:217a7931b41f 157 }
earlz 0:217a7931b41f 158 }
earlz 0:217a7931b41f 159 void x86CPU::op16_call_rm16(ModRM16 &rm){ //far call
earlz 0:217a7931b41f 160 Push16(eip+rm.GetLength()+1);
earlz 0:217a7931b41f 161 eip=rm.ReadWordr();
earlz 0:217a7931b41f 162 eip--; //eip will be incremented in Cycle
earlz 0:217a7931b41f 163 }
earlz 0:217a7931b41f 164
earlz 0:217a7931b41f 165 void x86CPU::op16_call_rm16_rm16(ModRM16 &rm){ //far call
earlz 0:217a7931b41f 166 Push16(seg[cCS]);
earlz 0:217a7931b41f 167 Push16(eip+rm.GetLength()+1);
earlz 0:217a7931b41f 168 *(uint32_t*)&op_cache=ReadDword(DS,rm.ReadDword());
earlz 0:217a7931b41f 169 seg[cCS]=*(uint16_t*)&op_cache[2]; //I always forget that they are reversed...
earlz 0:217a7931b41f 170 eip=*(uint16_t*)&op_cache[0];
earlz 0:217a7931b41f 171 eip--; //eip will be incremented in Cycle
earlz 0:217a7931b41f 172 }
earlz 0:217a7931b41f 173
earlz 0:217a7931b41f 174
earlz 0:217a7931b41f 175
earlz 0:217a7931b41f 176 };
earlz 0:217a7931b41f 177
earlz 0:217a7931b41f 178