SoftCPU.cpp 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "SoftCPU.h"
  27. #include "Emulator.h"
  28. #include <AK/Assertions.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31. namespace UserspaceEmulator {
  32. template<typename T>
  33. struct TypeDoubler {
  34. };
  35. template<>
  36. struct TypeDoubler<u8> {
  37. typedef u16 type;
  38. };
  39. template<>
  40. struct TypeDoubler<u16> {
  41. typedef u32 type;
  42. };
  43. template<>
  44. struct TypeDoubler<u32> {
  45. typedef u64 type;
  46. };
  47. template<>
  48. struct TypeDoubler<i8> {
  49. typedef i16 type;
  50. };
  51. template<>
  52. struct TypeDoubler<i16> {
  53. typedef i32 type;
  54. };
  55. template<>
  56. struct TypeDoubler<i32> {
  57. typedef i64 type;
  58. };
  59. SoftCPU::SoftCPU(Emulator& emulator)
  60. : m_emulator(emulator)
  61. {
  62. memset(m_gpr, 0, sizeof(m_gpr));
  63. m_segment[(int)X86::SegmentRegister::CS] = 0x18;
  64. m_segment[(int)X86::SegmentRegister::DS] = 0x20;
  65. m_segment[(int)X86::SegmentRegister::ES] = 0x20;
  66. m_segment[(int)X86::SegmentRegister::SS] = 0x20;
  67. }
  68. void SoftCPU::dump() const
  69. {
  70. printf("eax=%08x ebx=%08x ecx=%08x edx=%08x ", eax(), ebx(), ecx(), edx());
  71. printf("ebp=%08x esp=%08x esi=%08x edi=%08x ", ebp(), esp(), esi(), edi());
  72. printf("o=%u s=%u z=%u a=%u p=%u c=%u\n", of(), sf(), zf(), af(), pf(), cf());
  73. }
  74. u8 SoftCPU::read_memory8(X86::LogicalAddress address)
  75. {
  76. ASSERT(address.selector() == 0x20);
  77. auto value = m_emulator.mmu().read8(address.offset());
  78. printf("\033[36;1mread_memory8: @%08x -> %02x\033[0m\n", address.offset(), value);
  79. return value;
  80. }
  81. u16 SoftCPU::read_memory16(X86::LogicalAddress address)
  82. {
  83. ASSERT(address.selector() == 0x20);
  84. auto value = m_emulator.mmu().read16(address.offset());
  85. printf("\033[36;1mread_memory16: @%08x -> %04x\033[0m\n", address.offset(), value);
  86. return value;
  87. }
  88. u32 SoftCPU::read_memory32(X86::LogicalAddress address)
  89. {
  90. ASSERT(address.selector() == 0x20);
  91. auto value = m_emulator.mmu().read32(address.offset());
  92. printf("\033[36;1mread_memory32: @%08x -> %08x\033[0m\n", address.offset(), value);
  93. return value;
  94. }
  95. void SoftCPU::write_memory8(X86::LogicalAddress address, u8 value)
  96. {
  97. ASSERT(address.selector() == 0x20);
  98. printf("\033[35;1mwrite_memory8: @%08x <- %02x\033[0m\n", address.offset(), value);
  99. m_emulator.mmu().write8(address.offset(), value);
  100. }
  101. void SoftCPU::write_memory16(X86::LogicalAddress address, u16 value)
  102. {
  103. ASSERT(address.selector() == 0x20);
  104. printf("\033[35;1mwrite_memory16: @%08x <- %04x\033[0m\n", address.offset(), value);
  105. m_emulator.mmu().write16(address.offset(), value);
  106. }
  107. void SoftCPU::write_memory32(X86::LogicalAddress address, u32 value)
  108. {
  109. ASSERT(address.selector() == 0x20);
  110. printf("\033[35;1mwrite_memory32: @%08x <- %08x\033[0m\n", address.offset(), value);
  111. m_emulator.mmu().write32(address.offset(), value);
  112. }
  113. void SoftCPU::push32(u32 value)
  114. {
  115. set_esp(esp() - sizeof(value));
  116. write_memory32({ ss(), esp() }, value);
  117. }
  118. u32 SoftCPU::pop32()
  119. {
  120. auto value = read_memory32({ ss(), esp() });
  121. set_esp(esp() + sizeof(value));
  122. return value;
  123. }
  124. template<typename Destination, typename Source>
  125. static typename TypeDoubler<Destination>::type op_xor(SoftCPU& cpu, const Destination& dest, const Source& src)
  126. {
  127. Destination result = 0;
  128. u32 new_flags = 0;
  129. if constexpr (sizeof(Destination) == 4) {
  130. asm volatile("xorl %%ecx, %%eax\n"
  131. : "=a"(result)
  132. : "a"(dest), "c"(src));
  133. } else if constexpr (sizeof(Destination) == 2) {
  134. asm volatile("xor %%cx, %%ax\n"
  135. : "=a"(result)
  136. : "a"(dest), "c"(src));
  137. } else if constexpr (sizeof(Destination) == 1) {
  138. asm volatile("xorb %%cl, %%al\n"
  139. : "=a"(result)
  140. : "a"(dest), "c"(src));
  141. } else {
  142. ASSERT_NOT_REACHED();
  143. }
  144. asm volatile(
  145. "pushf\n"
  146. "pop %%ebx"
  147. : "=b"(new_flags));
  148. cpu.set_flags_oszpc(new_flags);
  149. return result;
  150. }
  151. template<typename Destination, typename Source>
  152. static typename TypeDoubler<Destination>::type op_sub(SoftCPU& cpu, const Destination& dest, const Source& src)
  153. {
  154. Destination result = 0;
  155. u32 new_flags = 0;
  156. if constexpr (sizeof(Destination) == 4) {
  157. asm volatile("subl %%ecx, %%eax\n"
  158. : "=a"(result)
  159. : "a"(dest), "c"(src));
  160. } else if constexpr (sizeof(Destination) == 2) {
  161. asm volatile("subw %%cx, %%ax\n"
  162. : "=a"(result)
  163. : "a"(dest), "c"(src));
  164. } else if constexpr (sizeof(Destination) == 1) {
  165. asm volatile("subb %%cl, %%al\n"
  166. : "=a"(result)
  167. : "a"(dest), "c"(src));
  168. } else {
  169. ASSERT_NOT_REACHED();
  170. }
  171. asm volatile(
  172. "pushf\n"
  173. "pop %%ebx"
  174. : "=b"(new_flags));
  175. cpu.set_flags_oszap(new_flags);
  176. return result;
  177. }
  178. template<typename Destination, typename Source>
  179. static Destination op_add(SoftCPU& cpu, Destination& dest, const Source& src)
  180. {
  181. Destination result = 0;
  182. u32 new_flags = 0;
  183. if constexpr (sizeof(Destination) == 4) {
  184. asm volatile("addl %%ecx, %%eax\n"
  185. : "=a"(result)
  186. : "a"(dest), "c"(src));
  187. } else if constexpr (sizeof(Destination) == 2) {
  188. asm volatile("addw %%cx, %%ax\n"
  189. : "=a"(result)
  190. : "a"(dest), "c"(src));
  191. } else if constexpr (sizeof(Destination) == 1) {
  192. asm volatile("addb %%cl, %%al\n"
  193. : "=a"(result)
  194. : "a"(dest), "c"(src));
  195. } else {
  196. ASSERT_NOT_REACHED();
  197. }
  198. asm volatile(
  199. "pushf\n"
  200. "pop %%ebx"
  201. : "=b"(new_flags));
  202. cpu.set_flags_oszap(new_flags);
  203. return result;
  204. }
  205. template<bool update_dest, typename Op>
  206. void SoftCPU::generic_AL_imm8(Op op, const X86::Instruction& insn)
  207. {
  208. auto dest = al();
  209. auto src = insn.imm8();
  210. auto result = op(*this, dest, src);
  211. if (update_dest)
  212. set_al(result);
  213. }
  214. template<bool update_dest, typename Op>
  215. void SoftCPU::generic_AX_imm16(Op op, const X86::Instruction& insn)
  216. {
  217. auto dest = ax();
  218. auto src = insn.imm16();
  219. auto result = op(*this, dest, src);
  220. if (update_dest)
  221. set_ax(result);
  222. }
  223. template<bool update_dest, typename Op>
  224. void SoftCPU::generic_EAX_imm32(Op op, const X86::Instruction& insn)
  225. {
  226. auto dest = eax();
  227. auto src = insn.imm32();
  228. auto result = op(*this, dest, src);
  229. if (update_dest)
  230. set_eax(result);
  231. }
  232. template<bool update_dest, typename Op>
  233. void SoftCPU::generic_RM16_imm16(Op op, const X86::Instruction& insn)
  234. {
  235. auto dest = insn.modrm().read16(*this, insn);
  236. auto src = insn.imm16();
  237. auto result = op(*this, dest, src);
  238. if (update_dest)
  239. insn.modrm().write16(*this, insn, result);
  240. }
  241. template<bool update_dest, typename Op>
  242. void SoftCPU::generic_RM16_imm8(Op op, const X86::Instruction& insn)
  243. {
  244. auto dest = insn.modrm().read16(*this, insn);
  245. auto src = insn.imm8();
  246. auto result = op(*this, dest, src);
  247. if (update_dest)
  248. insn.modrm().write16(*this, insn, result);
  249. }
  250. template<bool update_dest, typename Op>
  251. void SoftCPU::generic_RM16_reg16(Op op, const X86::Instruction& insn)
  252. {
  253. auto dest = insn.modrm().read16(*this, insn);
  254. auto src = gpr16(insn.reg16());
  255. auto result = op(*this, dest, src);
  256. if (update_dest)
  257. insn.modrm().write16(*this, insn, result);
  258. }
  259. template<bool update_dest, typename Op>
  260. void SoftCPU::generic_RM32_imm32(Op op, const X86::Instruction& insn)
  261. {
  262. auto dest = insn.modrm().read32(*this, insn);
  263. auto src = insn.imm32();
  264. auto result = op(*this, dest, src);
  265. if (update_dest)
  266. insn.modrm().write32(*this, insn, result);
  267. }
  268. template<bool update_dest, typename Op>
  269. void SoftCPU::generic_RM32_imm8(Op op, const X86::Instruction& insn)
  270. {
  271. auto dest = insn.modrm().read32(*this, insn);
  272. auto src = insn.imm8();
  273. auto result = op(*this, dest, src);
  274. if (update_dest)
  275. insn.modrm().write32(*this, insn, result);
  276. }
  277. template<bool update_dest, typename Op>
  278. void SoftCPU::generic_RM32_reg32(Op op, const X86::Instruction& insn)
  279. {
  280. auto dest = insn.modrm().read32(*this, insn);
  281. auto src = gpr32(insn.reg32());
  282. auto result = op(*this, dest, src);
  283. if (update_dest)
  284. insn.modrm().write32(*this, insn, result);
  285. }
  286. template<bool update_dest, typename Op>
  287. void SoftCPU::generic_RM8_imm8(Op op, const X86::Instruction& insn)
  288. {
  289. auto dest = insn.modrm().read8(*this, insn);
  290. auto src = insn.imm8();
  291. auto result = op(*this, dest, src);
  292. if (update_dest)
  293. insn.modrm().write8(*this, insn, result);
  294. }
  295. template<bool update_dest, typename Op>
  296. void SoftCPU::generic_RM8_reg8(Op op, const X86::Instruction& insn)
  297. {
  298. auto dest = insn.modrm().read8(*this, insn);
  299. auto src = gpr8(insn.reg8());
  300. auto result = op(*this, dest, src);
  301. if (update_dest)
  302. insn.modrm().write8(*this, insn, result);
  303. }
  304. template<bool update_dest, typename Op>
  305. void SoftCPU::generic_reg16_RM16(Op op, const X86::Instruction& insn)
  306. {
  307. auto dest = gpr16(insn.reg16());
  308. auto src = insn.modrm().read16(*this, insn);
  309. auto result = op(*this, dest, src);
  310. if (update_dest)
  311. gpr16(insn.reg16()) = result;
  312. }
  313. template<bool update_dest, typename Op>
  314. void SoftCPU::generic_reg32_RM32(Op op, const X86::Instruction& insn)
  315. {
  316. auto dest = gpr32(insn.reg32());
  317. auto src = insn.modrm().read32(*this, insn);
  318. auto result = op(*this, dest, src);
  319. if (update_dest)
  320. gpr32(insn.reg32()) = result;
  321. }
  322. template<bool update_dest, typename Op>
  323. void SoftCPU::generic_reg8_RM8(Op op, const X86::Instruction& insn)
  324. {
  325. auto dest = gpr8(insn.reg8());
  326. auto src = insn.modrm().read8(*this, insn);
  327. auto result = op(*this, dest, src);
  328. if (update_dest)
  329. gpr8(insn.reg8()) = result;
  330. }
  331. void SoftCPU::AAA(const X86::Instruction&) { TODO(); }
  332. void SoftCPU::AAD(const X86::Instruction&) { TODO(); }
  333. void SoftCPU::AAM(const X86::Instruction&) { TODO(); }
  334. void SoftCPU::AAS(const X86::Instruction&) { TODO(); }
  335. void SoftCPU::ADC_AL_imm8(const X86::Instruction&) { TODO(); }
  336. void SoftCPU::ADC_AX_imm16(const X86::Instruction&) { TODO(); }
  337. void SoftCPU::ADC_EAX_imm32(const X86::Instruction&) { TODO(); }
  338. void SoftCPU::ADC_RM16_imm16(const X86::Instruction&) { TODO(); }
  339. void SoftCPU::ADC_RM16_imm8(const X86::Instruction&) { TODO(); }
  340. void SoftCPU::ADC_RM16_reg16(const X86::Instruction&) { TODO(); }
  341. void SoftCPU::ADC_RM32_imm32(const X86::Instruction&) { TODO(); }
  342. void SoftCPU::ADC_RM32_imm8(const X86::Instruction&) { TODO(); }
  343. void SoftCPU::ADC_RM32_reg32(const X86::Instruction&) { TODO(); }
  344. void SoftCPU::ADC_RM8_imm8(const X86::Instruction&) { TODO(); }
  345. void SoftCPU::ADC_RM8_reg8(const X86::Instruction&) { TODO(); }
  346. void SoftCPU::ADC_reg16_RM16(const X86::Instruction&) { TODO(); }
  347. void SoftCPU::ADC_reg32_RM32(const X86::Instruction&) { TODO(); }
  348. void SoftCPU::ADC_reg8_RM8(const X86::Instruction&) { TODO(); }
  349. void SoftCPU::AND_AL_imm8(const X86::Instruction&) { TODO(); }
  350. void SoftCPU::AND_AX_imm16(const X86::Instruction&) { TODO(); }
  351. void SoftCPU::AND_EAX_imm32(const X86::Instruction&) { TODO(); }
  352. void SoftCPU::AND_RM16_imm16(const X86::Instruction&) { TODO(); }
  353. void SoftCPU::AND_RM16_imm8(const X86::Instruction&) { TODO(); }
  354. void SoftCPU::AND_RM16_reg16(const X86::Instruction&) { TODO(); }
  355. void SoftCPU::AND_RM32_imm32(const X86::Instruction&) { TODO(); }
  356. void SoftCPU::AND_RM32_imm8(const X86::Instruction&) { TODO(); }
  357. void SoftCPU::AND_RM32_reg32(const X86::Instruction&) { TODO(); }
  358. void SoftCPU::AND_RM8_imm8(const X86::Instruction&) { TODO(); }
  359. void SoftCPU::AND_RM8_reg8(const X86::Instruction&) { TODO(); }
  360. void SoftCPU::AND_reg16_RM16(const X86::Instruction&) { TODO(); }
  361. void SoftCPU::AND_reg32_RM32(const X86::Instruction&) { TODO(); }
  362. void SoftCPU::AND_reg8_RM8(const X86::Instruction&) { TODO(); }
  363. void SoftCPU::ARPL(const X86::Instruction&) { TODO(); }
  364. void SoftCPU::BOUND(const X86::Instruction&) { TODO(); }
  365. void SoftCPU::BSF_reg16_RM16(const X86::Instruction&) { TODO(); }
  366. void SoftCPU::BSF_reg32_RM32(const X86::Instruction&) { TODO(); }
  367. void SoftCPU::BSR_reg16_RM16(const X86::Instruction&) { TODO(); }
  368. void SoftCPU::BSR_reg32_RM32(const X86::Instruction&) { TODO(); }
  369. void SoftCPU::BSWAP_reg32(const X86::Instruction&) { TODO(); }
  370. void SoftCPU::BTC_RM16_imm8(const X86::Instruction&) { TODO(); }
  371. void SoftCPU::BTC_RM16_reg16(const X86::Instruction&) { TODO(); }
  372. void SoftCPU::BTC_RM32_imm8(const X86::Instruction&) { TODO(); }
  373. void SoftCPU::BTC_RM32_reg32(const X86::Instruction&) { TODO(); }
  374. void SoftCPU::BTR_RM16_imm8(const X86::Instruction&) { TODO(); }
  375. void SoftCPU::BTR_RM16_reg16(const X86::Instruction&) { TODO(); }
  376. void SoftCPU::BTR_RM32_imm8(const X86::Instruction&) { TODO(); }
  377. void SoftCPU::BTR_RM32_reg32(const X86::Instruction&) { TODO(); }
  378. void SoftCPU::BTS_RM16_imm8(const X86::Instruction&) { TODO(); }
  379. void SoftCPU::BTS_RM16_reg16(const X86::Instruction&) { TODO(); }
  380. void SoftCPU::BTS_RM32_imm8(const X86::Instruction&) { TODO(); }
  381. void SoftCPU::BTS_RM32_reg32(const X86::Instruction&) { TODO(); }
  382. void SoftCPU::BT_RM16_imm8(const X86::Instruction&) { TODO(); }
  383. void SoftCPU::BT_RM16_reg16(const X86::Instruction&) { TODO(); }
  384. void SoftCPU::BT_RM32_imm8(const X86::Instruction&) { TODO(); }
  385. void SoftCPU::BT_RM32_reg32(const X86::Instruction&) { TODO(); }
  386. void SoftCPU::CALL_FAR_mem16(const X86::Instruction&) { TODO(); }
  387. void SoftCPU::CALL_FAR_mem32(const X86::Instruction&) { TODO(); }
  388. void SoftCPU::CALL_RM16(const X86::Instruction&) { TODO(); }
  389. void SoftCPU::CALL_RM32(const X86::Instruction&) { TODO(); }
  390. void SoftCPU::CALL_imm16(const X86::Instruction&) { TODO(); }
  391. void SoftCPU::CALL_imm16_imm16(const X86::Instruction&) { TODO(); }
  392. void SoftCPU::CALL_imm16_imm32(const X86::Instruction&) { TODO(); }
  393. void SoftCPU::CALL_imm32(const X86::Instruction&) { TODO(); }
  394. void SoftCPU::CBW(const X86::Instruction&) { TODO(); }
  395. void SoftCPU::CDQ(const X86::Instruction&) { TODO(); }
  396. void SoftCPU::CLC(const X86::Instruction&) { TODO(); }
  397. void SoftCPU::CLD(const X86::Instruction&) { TODO(); }
  398. void SoftCPU::CLI(const X86::Instruction&) { TODO(); }
  399. void SoftCPU::CLTS(const X86::Instruction&) { TODO(); }
  400. void SoftCPU::CMC(const X86::Instruction&) { TODO(); }
  401. void SoftCPU::CMOVcc_reg16_RM16(const X86::Instruction&) { TODO(); }
  402. void SoftCPU::CMOVcc_reg32_RM32(const X86::Instruction&) { TODO(); }
  403. void SoftCPU::CMPSB(const X86::Instruction&) { TODO(); }
  404. void SoftCPU::CMPSD(const X86::Instruction&) { TODO(); }
  405. void SoftCPU::CMPSW(const X86::Instruction&) { TODO(); }
  406. void SoftCPU::CMPXCHG_RM16_reg16(const X86::Instruction&) { TODO(); }
  407. void SoftCPU::CMPXCHG_RM32_reg32(const X86::Instruction&) { TODO(); }
  408. void SoftCPU::CMPXCHG_RM8_reg8(const X86::Instruction&) { TODO(); }
  409. void SoftCPU::CPUID(const X86::Instruction&) { TODO(); }
  410. void SoftCPU::CWD(const X86::Instruction&) { TODO(); }
  411. void SoftCPU::CWDE(const X86::Instruction&) { TODO(); }
  412. void SoftCPU::DAA(const X86::Instruction&) { TODO(); }
  413. void SoftCPU::DAS(const X86::Instruction&) { TODO(); }
  414. void SoftCPU::DEC_RM16(const X86::Instruction&) { TODO(); }
  415. void SoftCPU::DEC_RM32(const X86::Instruction&) { TODO(); }
  416. void SoftCPU::DEC_RM8(const X86::Instruction&) { TODO(); }
  417. void SoftCPU::DEC_reg16(const X86::Instruction&) { TODO(); }
  418. void SoftCPU::DEC_reg32(const X86::Instruction&) { TODO(); }
  419. void SoftCPU::DIV_RM16(const X86::Instruction&) { TODO(); }
  420. void SoftCPU::DIV_RM32(const X86::Instruction&) { TODO(); }
  421. void SoftCPU::DIV_RM8(const X86::Instruction&) { TODO(); }
  422. void SoftCPU::ENTER16(const X86::Instruction&) { TODO(); }
  423. void SoftCPU::ENTER32(const X86::Instruction&) { TODO(); }
  424. void SoftCPU::ESCAPE(const X86::Instruction&) { TODO(); }
  425. void SoftCPU::HLT(const X86::Instruction&) { TODO(); }
  426. void SoftCPU::IDIV_RM16(const X86::Instruction&) { TODO(); }
  427. void SoftCPU::IDIV_RM32(const X86::Instruction&) { TODO(); }
  428. void SoftCPU::IDIV_RM8(const X86::Instruction&) { TODO(); }
  429. void SoftCPU::IMUL_RM16(const X86::Instruction&) { TODO(); }
  430. void SoftCPU::IMUL_RM32(const X86::Instruction&) { TODO(); }
  431. void SoftCPU::IMUL_RM8(const X86::Instruction&) { TODO(); }
  432. void SoftCPU::IMUL_reg16_RM16(const X86::Instruction&) { TODO(); }
  433. void SoftCPU::IMUL_reg16_RM16_imm16(const X86::Instruction&) { TODO(); }
  434. void SoftCPU::IMUL_reg16_RM16_imm8(const X86::Instruction&) { TODO(); }
  435. void SoftCPU::IMUL_reg32_RM32(const X86::Instruction&) { TODO(); }
  436. void SoftCPU::IMUL_reg32_RM32_imm32(const X86::Instruction&) { TODO(); }
  437. void SoftCPU::IMUL_reg32_RM32_imm8(const X86::Instruction&) { TODO(); }
  438. template<typename T>
  439. static T op_inc(SoftCPU& cpu, T data)
  440. {
  441. T result = 0;
  442. u32 new_flags = 0;
  443. if constexpr (sizeof(T) == 4) {
  444. asm volatile("incl %%eax\n"
  445. : "=a"(result)
  446. : "a"(data));
  447. } else if constexpr (sizeof(T) == 2) {
  448. asm volatile("incw %%ax\n"
  449. : "=a"(result)
  450. : "a"(data));
  451. } else if constexpr (sizeof(T) == 1) {
  452. asm volatile("incb %%al\n"
  453. : "=a"(result)
  454. : "a"(data));
  455. }
  456. asm volatile(
  457. "pushf\n"
  458. "pop %%ebx"
  459. : "=b"(new_flags));
  460. cpu.set_flags_oszap(new_flags);
  461. return result;
  462. }
  463. void SoftCPU::INC_RM16(const X86::Instruction& insn)
  464. {
  465. insn.modrm().write16(*this, insn, op_inc(*this, insn.modrm().read16(*this, insn)));
  466. }
  467. void SoftCPU::INC_RM32(const X86::Instruction& insn)
  468. {
  469. insn.modrm().write32(*this, insn, op_inc(*this, insn.modrm().read32(*this, insn)));
  470. }
  471. void SoftCPU::INC_RM8(const X86::Instruction& insn)
  472. {
  473. insn.modrm().write8(*this, insn, op_inc(*this, insn.modrm().read8(*this, insn)));
  474. }
  475. void SoftCPU::INC_reg16(const X86::Instruction& insn)
  476. {
  477. gpr16(insn.reg16()) = op_inc(*this, gpr16(insn.reg16()));
  478. }
  479. void SoftCPU::INC_reg32(const X86::Instruction& insn)
  480. {
  481. gpr32(insn.reg32()) = op_inc(*this, gpr32(insn.reg32()));
  482. }
  483. void SoftCPU::INSB(const X86::Instruction&) { TODO(); }
  484. void SoftCPU::INSD(const X86::Instruction&) { TODO(); }
  485. void SoftCPU::INSW(const X86::Instruction&) { TODO(); }
  486. void SoftCPU::INT3(const X86::Instruction&) { TODO(); }
  487. void SoftCPU::INTO(const X86::Instruction&) { TODO(); }
  488. void SoftCPU::INT_imm8(const X86::Instruction& insn)
  489. {
  490. ASSERT(insn.imm8() == 0x82);
  491. set_eax(m_emulator.virt_syscall(eax(), edx(), ecx(), ebx()));
  492. }
  493. void SoftCPU::INVLPG(const X86::Instruction&) { TODO(); }
  494. void SoftCPU::IN_AL_DX(const X86::Instruction&) { TODO(); }
  495. void SoftCPU::IN_AL_imm8(const X86::Instruction&) { TODO(); }
  496. void SoftCPU::IN_AX_DX(const X86::Instruction&) { TODO(); }
  497. void SoftCPU::IN_AX_imm8(const X86::Instruction&) { TODO(); }
  498. void SoftCPU::IN_EAX_DX(const X86::Instruction&) { TODO(); }
  499. void SoftCPU::IN_EAX_imm8(const X86::Instruction&) { TODO(); }
  500. void SoftCPU::IRET(const X86::Instruction&) { TODO(); }
  501. void SoftCPU::JCXZ_imm8(const X86::Instruction&) { TODO(); }
  502. void SoftCPU::JMP_FAR_mem16(const X86::Instruction&) { TODO(); }
  503. void SoftCPU::JMP_FAR_mem32(const X86::Instruction&) { TODO(); }
  504. void SoftCPU::JMP_RM16(const X86::Instruction&) { TODO(); }
  505. void SoftCPU::JMP_RM32(const X86::Instruction&) { TODO(); }
  506. void SoftCPU::JMP_imm16(const X86::Instruction&) { TODO(); }
  507. void SoftCPU::JMP_imm16_imm16(const X86::Instruction&) { TODO(); }
  508. void SoftCPU::JMP_imm16_imm32(const X86::Instruction&) { TODO(); }
  509. void SoftCPU::JMP_imm32(const X86::Instruction&) { TODO(); }
  510. void SoftCPU::JMP_short_imm8(const X86::Instruction&) { TODO(); }
  511. void SoftCPU::Jcc_NEAR_imm(const X86::Instruction&) { TODO(); }
  512. void SoftCPU::Jcc_imm8(const X86::Instruction&) { TODO(); }
  513. void SoftCPU::LAHF(const X86::Instruction&) { TODO(); }
  514. void SoftCPU::LAR_reg16_RM16(const X86::Instruction&) { TODO(); }
  515. void SoftCPU::LAR_reg32_RM32(const X86::Instruction&) { TODO(); }
  516. void SoftCPU::LDS_reg16_mem16(const X86::Instruction&) { TODO(); }
  517. void SoftCPU::LDS_reg32_mem32(const X86::Instruction&) { TODO(); }
  518. void SoftCPU::LEAVE16(const X86::Instruction&) { TODO(); }
  519. void SoftCPU::LEAVE32(const X86::Instruction&) { TODO(); }
  520. void SoftCPU::LEA_reg16_mem16(const X86::Instruction&) { TODO(); }
  521. void SoftCPU::LEA_reg32_mem32(const X86::Instruction&) { TODO(); }
  522. void SoftCPU::LES_reg16_mem16(const X86::Instruction&) { TODO(); }
  523. void SoftCPU::LES_reg32_mem32(const X86::Instruction&) { TODO(); }
  524. void SoftCPU::LFS_reg16_mem16(const X86::Instruction&) { TODO(); }
  525. void SoftCPU::LFS_reg32_mem32(const X86::Instruction&) { TODO(); }
  526. void SoftCPU::LGDT(const X86::Instruction&) { TODO(); }
  527. void SoftCPU::LGS_reg16_mem16(const X86::Instruction&) { TODO(); }
  528. void SoftCPU::LGS_reg32_mem32(const X86::Instruction&) { TODO(); }
  529. void SoftCPU::LIDT(const X86::Instruction&) { TODO(); }
  530. void SoftCPU::LLDT_RM16(const X86::Instruction&) { TODO(); }
  531. void SoftCPU::LMSW_RM16(const X86::Instruction&) { TODO(); }
  532. void SoftCPU::LODSB(const X86::Instruction&) { TODO(); }
  533. void SoftCPU::LODSD(const X86::Instruction&) { TODO(); }
  534. void SoftCPU::LODSW(const X86::Instruction&) { TODO(); }
  535. void SoftCPU::LOOPNZ_imm8(const X86::Instruction&) { TODO(); }
  536. void SoftCPU::LOOPZ_imm8(const X86::Instruction&) { TODO(); }
  537. void SoftCPU::LOOP_imm8(const X86::Instruction&) { TODO(); }
  538. void SoftCPU::LSL_reg16_RM16(const X86::Instruction&) { TODO(); }
  539. void SoftCPU::LSL_reg32_RM32(const X86::Instruction&) { TODO(); }
  540. void SoftCPU::LSS_reg16_mem16(const X86::Instruction&) { TODO(); }
  541. void SoftCPU::LSS_reg32_mem32(const X86::Instruction&) { TODO(); }
  542. void SoftCPU::LTR_RM16(const X86::Instruction&) { TODO(); }
  543. void SoftCPU::MOVSB(const X86::Instruction&) { TODO(); }
  544. void SoftCPU::MOVSD(const X86::Instruction&) { TODO(); }
  545. void SoftCPU::MOVSW(const X86::Instruction&) { TODO(); }
  546. void SoftCPU::MOVSX_reg16_RM8(const X86::Instruction&) { TODO(); }
  547. void SoftCPU::MOVSX_reg32_RM16(const X86::Instruction&) { TODO(); }
  548. void SoftCPU::MOVSX_reg32_RM8(const X86::Instruction&) { TODO(); }
  549. void SoftCPU::MOVZX_reg16_RM8(const X86::Instruction&) { TODO(); }
  550. void SoftCPU::MOVZX_reg32_RM16(const X86::Instruction&) { TODO(); }
  551. void SoftCPU::MOVZX_reg32_RM8(const X86::Instruction&) { TODO(); }
  552. void SoftCPU::MOV_AL_moff8(const X86::Instruction&) { TODO(); }
  553. void SoftCPU::MOV_AX_moff16(const X86::Instruction&) { TODO(); }
  554. void SoftCPU::MOV_CR_reg32(const X86::Instruction&) { TODO(); }
  555. void SoftCPU::MOV_DR_reg32(const X86::Instruction&) { TODO(); }
  556. void SoftCPU::MOV_EAX_moff32(const X86::Instruction&) { TODO(); }
  557. void SoftCPU::MOV_RM16_imm16(const X86::Instruction& insn)
  558. {
  559. insn.modrm().write16(*this, insn, insn.imm16());
  560. }
  561. void SoftCPU::MOV_RM16_reg16(const X86::Instruction& insn)
  562. {
  563. insn.modrm().write16(*this, insn, gpr16(insn.reg16()));
  564. }
  565. void SoftCPU::MOV_RM16_seg(const X86::Instruction&) { TODO(); }
  566. void SoftCPU::MOV_RM32_imm32(const X86::Instruction& insn)
  567. {
  568. gpr32(insn.reg32()) = insn.imm32();
  569. }
  570. void SoftCPU::MOV_RM32_reg32(const X86::Instruction& insn)
  571. {
  572. insn.modrm().write32(*this, insn, gpr32(insn.reg32()));
  573. }
  574. void SoftCPU::MOV_RM8_imm8(const X86::Instruction& insn)
  575. {
  576. insn.modrm().write8(*this, insn, insn.imm8());
  577. }
  578. void SoftCPU::MOV_RM8_reg8(const X86::Instruction& insn)
  579. {
  580. insn.modrm().write8(*this, insn, insn.modrm().read8(*this, insn));
  581. }
  582. void SoftCPU::MOV_moff16_AX(const X86::Instruction&) { TODO(); }
  583. void SoftCPU::MOV_moff32_EAX(const X86::Instruction&) { TODO(); }
  584. void SoftCPU::MOV_moff8_AL(const X86::Instruction&) { TODO(); }
  585. void SoftCPU::MOV_reg16_RM16(const X86::Instruction& insn)
  586. {
  587. gpr16(insn.reg16()) = insn.modrm().read16(*this, insn);
  588. }
  589. void SoftCPU::MOV_reg16_imm16(const X86::Instruction& insn)
  590. {
  591. gpr16(insn.reg16()) = insn.imm16();
  592. }
  593. void SoftCPU::MOV_reg32_CR(const X86::Instruction&) { TODO(); }
  594. void SoftCPU::MOV_reg32_DR(const X86::Instruction&) { TODO(); }
  595. void SoftCPU::MOV_reg32_RM32(const X86::Instruction& insn)
  596. {
  597. gpr32(insn.reg32()) = insn.modrm().read32(*this, insn);
  598. }
  599. void SoftCPU::MOV_reg32_imm32(const X86::Instruction& insn)
  600. {
  601. gpr32(insn.reg32()) = insn.imm32();
  602. }
  603. void SoftCPU::MOV_reg8_RM8(const X86::Instruction& insn)
  604. {
  605. gpr8(insn.reg8()) = insn.modrm().read8(*this, insn);
  606. }
  607. void SoftCPU::MOV_reg8_imm8(const X86::Instruction& insn)
  608. {
  609. gpr8(insn.reg8()) = insn.imm8();
  610. }
  611. void SoftCPU::MOV_seg_RM16(const X86::Instruction&) { TODO(); }
  612. void SoftCPU::MOV_seg_RM32(const X86::Instruction&) { TODO(); }
  613. void SoftCPU::MUL_RM16(const X86::Instruction&) { TODO(); }
  614. void SoftCPU::MUL_RM32(const X86::Instruction&) { TODO(); }
  615. void SoftCPU::MUL_RM8(const X86::Instruction&) { TODO(); }
  616. void SoftCPU::NEG_RM16(const X86::Instruction&) { TODO(); }
  617. void SoftCPU::NEG_RM32(const X86::Instruction&) { TODO(); }
  618. void SoftCPU::NEG_RM8(const X86::Instruction&) { TODO(); }
  619. void SoftCPU::NOP(const X86::Instruction&) { TODO(); }
  620. void SoftCPU::NOT_RM16(const X86::Instruction&) { TODO(); }
  621. void SoftCPU::NOT_RM32(const X86::Instruction&) { TODO(); }
  622. void SoftCPU::NOT_RM8(const X86::Instruction&) { TODO(); }
  623. void SoftCPU::OR_AL_imm8(const X86::Instruction&) { TODO(); }
  624. void SoftCPU::OR_AX_imm16(const X86::Instruction&) { TODO(); }
  625. void SoftCPU::OR_EAX_imm32(const X86::Instruction&) { TODO(); }
  626. void SoftCPU::OR_RM16_imm16(const X86::Instruction&) { TODO(); }
  627. void SoftCPU::OR_RM16_imm8(const X86::Instruction&) { TODO(); }
  628. void SoftCPU::OR_RM16_reg16(const X86::Instruction&) { TODO(); }
  629. void SoftCPU::OR_RM32_imm32(const X86::Instruction&) { TODO(); }
  630. void SoftCPU::OR_RM32_imm8(const X86::Instruction&) { TODO(); }
  631. void SoftCPU::OR_RM32_reg32(const X86::Instruction&) { TODO(); }
  632. void SoftCPU::OR_RM8_imm8(const X86::Instruction&) { TODO(); }
  633. void SoftCPU::OR_RM8_reg8(const X86::Instruction&) { TODO(); }
  634. void SoftCPU::OR_reg16_RM16(const X86::Instruction&) { TODO(); }
  635. void SoftCPU::OR_reg32_RM32(const X86::Instruction&) { TODO(); }
  636. void SoftCPU::OR_reg8_RM8(const X86::Instruction&) { TODO(); }
  637. void SoftCPU::OUTSB(const X86::Instruction&) { TODO(); }
  638. void SoftCPU::OUTSD(const X86::Instruction&) { TODO(); }
  639. void SoftCPU::OUTSW(const X86::Instruction&) { TODO(); }
  640. void SoftCPU::OUT_DX_AL(const X86::Instruction&) { TODO(); }
  641. void SoftCPU::OUT_DX_AX(const X86::Instruction&) { TODO(); }
  642. void SoftCPU::OUT_DX_EAX(const X86::Instruction&) { TODO(); }
  643. void SoftCPU::OUT_imm8_AL(const X86::Instruction&) { TODO(); }
  644. void SoftCPU::OUT_imm8_AX(const X86::Instruction&) { TODO(); }
  645. void SoftCPU::OUT_imm8_EAX(const X86::Instruction&) { TODO(); }
  646. void SoftCPU::PADDB_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  647. void SoftCPU::PADDW_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  648. void SoftCPU::PADDD_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  649. void SoftCPU::POPA(const X86::Instruction&) { TODO(); }
  650. void SoftCPU::POPAD(const X86::Instruction&) { TODO(); }
  651. void SoftCPU::POPF(const X86::Instruction&) { TODO(); }
  652. void SoftCPU::POPFD(const X86::Instruction&) { TODO(); }
  653. void SoftCPU::POP_DS(const X86::Instruction&) { TODO(); }
  654. void SoftCPU::POP_ES(const X86::Instruction&) { TODO(); }
  655. void SoftCPU::POP_FS(const X86::Instruction&) { TODO(); }
  656. void SoftCPU::POP_GS(const X86::Instruction&) { TODO(); }
  657. void SoftCPU::POP_RM16(const X86::Instruction&) { TODO(); }
  658. void SoftCPU::POP_RM32(const X86::Instruction&) { TODO(); }
  659. void SoftCPU::POP_SS(const X86::Instruction&) { TODO(); }
  660. void SoftCPU::POP_reg16(const X86::Instruction&) { TODO(); }
  661. void SoftCPU::POP_reg32(const X86::Instruction& insn)
  662. {
  663. gpr32(insn.reg32()) = pop32();
  664. }
  665. void SoftCPU::PUSHA(const X86::Instruction&) { TODO(); }
  666. void SoftCPU::PUSHAD(const X86::Instruction&) { TODO(); }
  667. void SoftCPU::PUSHF(const X86::Instruction&) { TODO(); }
  668. void SoftCPU::PUSHFD(const X86::Instruction&) { TODO(); }
  669. void SoftCPU::PUSH_CS(const X86::Instruction&) { TODO(); }
  670. void SoftCPU::PUSH_DS(const X86::Instruction&) { TODO(); }
  671. void SoftCPU::PUSH_ES(const X86::Instruction&) { TODO(); }
  672. void SoftCPU::PUSH_FS(const X86::Instruction&) { TODO(); }
  673. void SoftCPU::PUSH_GS(const X86::Instruction&) { TODO(); }
  674. void SoftCPU::PUSH_RM16(const X86::Instruction&) { TODO(); }
  675. void SoftCPU::PUSH_RM32(const X86::Instruction&) { TODO(); }
  676. void SoftCPU::PUSH_SP_8086_80186(const X86::Instruction&) { TODO(); }
  677. void SoftCPU::PUSH_SS(const X86::Instruction&) { TODO(); }
  678. void SoftCPU::PUSH_imm16(const X86::Instruction&) { TODO(); }
  679. void SoftCPU::PUSH_imm32(const X86::Instruction&) { TODO(); }
  680. void SoftCPU::PUSH_imm8(const X86::Instruction&) { TODO(); }
  681. void SoftCPU::PUSH_reg16(const X86::Instruction&) { TODO(); }
  682. void SoftCPU::PUSH_reg32(const X86::Instruction& insn)
  683. {
  684. push32(gpr32(insn.reg32()));
  685. }
  686. void SoftCPU::RCL_RM16_1(const X86::Instruction&) { TODO(); }
  687. void SoftCPU::RCL_RM16_CL(const X86::Instruction&) { TODO(); }
  688. void SoftCPU::RCL_RM16_imm8(const X86::Instruction&) { TODO(); }
  689. void SoftCPU::RCL_RM32_1(const X86::Instruction&) { TODO(); }
  690. void SoftCPU::RCL_RM32_CL(const X86::Instruction&) { TODO(); }
  691. void SoftCPU::RCL_RM32_imm8(const X86::Instruction&) { TODO(); }
  692. void SoftCPU::RCL_RM8_1(const X86::Instruction&) { TODO(); }
  693. void SoftCPU::RCL_RM8_CL(const X86::Instruction&) { TODO(); }
  694. void SoftCPU::RCL_RM8_imm8(const X86::Instruction&) { TODO(); }
  695. void SoftCPU::RCR_RM16_1(const X86::Instruction&) { TODO(); }
  696. void SoftCPU::RCR_RM16_CL(const X86::Instruction&) { TODO(); }
  697. void SoftCPU::RCR_RM16_imm8(const X86::Instruction&) { TODO(); }
  698. void SoftCPU::RCR_RM32_1(const X86::Instruction&) { TODO(); }
  699. void SoftCPU::RCR_RM32_CL(const X86::Instruction&) { TODO(); }
  700. void SoftCPU::RCR_RM32_imm8(const X86::Instruction&) { TODO(); }
  701. void SoftCPU::RCR_RM8_1(const X86::Instruction&) { TODO(); }
  702. void SoftCPU::RCR_RM8_CL(const X86::Instruction&) { TODO(); }
  703. void SoftCPU::RCR_RM8_imm8(const X86::Instruction&) { TODO(); }
  704. void SoftCPU::RDTSC(const X86::Instruction&) { TODO(); }
  705. void SoftCPU::RET(const X86::Instruction&) { TODO(); }
  706. void SoftCPU::RETF(const X86::Instruction&) { TODO(); }
  707. void SoftCPU::RETF_imm16(const X86::Instruction&) { TODO(); }
  708. void SoftCPU::RET_imm16(const X86::Instruction&) { TODO(); }
  709. void SoftCPU::ROL_RM16_1(const X86::Instruction&) { TODO(); }
  710. void SoftCPU::ROL_RM16_CL(const X86::Instruction&) { TODO(); }
  711. void SoftCPU::ROL_RM16_imm8(const X86::Instruction&) { TODO(); }
  712. void SoftCPU::ROL_RM32_1(const X86::Instruction&) { TODO(); }
  713. void SoftCPU::ROL_RM32_CL(const X86::Instruction&) { TODO(); }
  714. void SoftCPU::ROL_RM32_imm8(const X86::Instruction&) { TODO(); }
  715. void SoftCPU::ROL_RM8_1(const X86::Instruction&) { TODO(); }
  716. void SoftCPU::ROL_RM8_CL(const X86::Instruction&) { TODO(); }
  717. void SoftCPU::ROL_RM8_imm8(const X86::Instruction&) { TODO(); }
  718. void SoftCPU::ROR_RM16_1(const X86::Instruction&) { TODO(); }
  719. void SoftCPU::ROR_RM16_CL(const X86::Instruction&) { TODO(); }
  720. void SoftCPU::ROR_RM16_imm8(const X86::Instruction&) { TODO(); }
  721. void SoftCPU::ROR_RM32_1(const X86::Instruction&) { TODO(); }
  722. void SoftCPU::ROR_RM32_CL(const X86::Instruction&) { TODO(); }
  723. void SoftCPU::ROR_RM32_imm8(const X86::Instruction&) { TODO(); }
  724. void SoftCPU::ROR_RM8_1(const X86::Instruction&) { TODO(); }
  725. void SoftCPU::ROR_RM8_CL(const X86::Instruction&) { TODO(); }
  726. void SoftCPU::ROR_RM8_imm8(const X86::Instruction&) { TODO(); }
  727. void SoftCPU::SAHF(const X86::Instruction&) { TODO(); }
  728. void SoftCPU::SALC(const X86::Instruction&) { TODO(); }
  729. template<typename T>
  730. static T op_sar(SoftCPU& cpu, T data, u8 steps)
  731. {
  732. if (steps == 0)
  733. return data;
  734. u32 result = 0;
  735. u32 new_flags = 0;
  736. if constexpr (sizeof(T) == 4)
  737. asm volatile("sarl %%cl, %%eax\n" ::"a"(data), "c"(steps));
  738. else if constexpr (sizeof(T) == 2)
  739. asm volatile("sarw %%cl, %%ax\n" ::"a"(data), "c"(steps));
  740. else if constexpr (sizeof(T) == 1)
  741. asm volatile("sarb %%cl, %%al\n" ::"a"(data), "c"(steps));
  742. asm volatile(
  743. "mov %%eax, %%ebx\n"
  744. : "=b"(result));
  745. asm volatile(
  746. "pushf\n"
  747. "pop %%eax"
  748. : "=a"(new_flags));
  749. cpu.set_flags_oszapc(new_flags);
  750. return result;
  751. }
  752. void SoftCPU::SAR_RM16_1(const X86::Instruction& insn)
  753. {
  754. auto data = insn.modrm().read16(*this, insn);
  755. insn.modrm().write16(*this, insn, op_sar(*this, data, 1));
  756. }
  757. void SoftCPU::SAR_RM16_CL(const X86::Instruction& insn)
  758. {
  759. auto data = insn.modrm().read16(*this, insn);
  760. insn.modrm().write16(*this, insn, op_sar(*this, data, cl()));
  761. }
  762. void SoftCPU::SAR_RM16_imm8(const X86::Instruction& insn)
  763. {
  764. auto data = insn.modrm().read16(*this, insn);
  765. insn.modrm().write16(*this, insn, op_sar(*this, data, insn.imm8()));
  766. }
  767. void SoftCPU::SAR_RM32_1(const X86::Instruction& insn)
  768. {
  769. auto data = insn.modrm().read32(*this, insn);
  770. insn.modrm().write32(*this, insn, op_sar(*this, data, 1));
  771. }
  772. void SoftCPU::SAR_RM32_CL(const X86::Instruction& insn)
  773. {
  774. auto data = insn.modrm().read32(*this, insn);
  775. insn.modrm().write32(*this, insn, op_sar(*this, data, cl()));
  776. }
  777. void SoftCPU::SAR_RM32_imm8(const X86::Instruction& insn)
  778. {
  779. auto data = insn.modrm().read32(*this, insn);
  780. insn.modrm().write32(*this, insn, op_sar(*this, data, insn.imm8()));
  781. }
  782. void SoftCPU::SAR_RM8_1(const X86::Instruction& insn)
  783. {
  784. auto data = insn.modrm().read8(*this, insn);
  785. insn.modrm().write8(*this, insn, op_sar(*this, data, 1));
  786. }
  787. void SoftCPU::SAR_RM8_CL(const X86::Instruction& insn)
  788. {
  789. auto data = insn.modrm().read8(*this, insn);
  790. insn.modrm().write8(*this, insn, op_sar(*this, data, cl()));
  791. }
  792. void SoftCPU::SAR_RM8_imm8(const X86::Instruction& insn)
  793. {
  794. auto data = insn.modrm().read8(*this, insn);
  795. insn.modrm().write8(*this, insn, op_sar(*this, data, insn.imm8()));
  796. }
  797. void SoftCPU::SBB_AL_imm8(const X86::Instruction&) { TODO(); }
  798. void SoftCPU::SBB_AX_imm16(const X86::Instruction&) { TODO(); }
  799. void SoftCPU::SBB_EAX_imm32(const X86::Instruction&) { TODO(); }
  800. void SoftCPU::SBB_RM16_imm16(const X86::Instruction&) { TODO(); }
  801. void SoftCPU::SBB_RM16_imm8(const X86::Instruction&) { TODO(); }
  802. void SoftCPU::SBB_RM16_reg16(const X86::Instruction&) { TODO(); }
  803. void SoftCPU::SBB_RM32_imm32(const X86::Instruction&) { TODO(); }
  804. void SoftCPU::SBB_RM32_imm8(const X86::Instruction&) { TODO(); }
  805. void SoftCPU::SBB_RM32_reg32(const X86::Instruction&) { TODO(); }
  806. void SoftCPU::SBB_RM8_imm8(const X86::Instruction&) { TODO(); }
  807. void SoftCPU::SBB_RM8_reg8(const X86::Instruction&) { TODO(); }
  808. void SoftCPU::SBB_reg16_RM16(const X86::Instruction&) { TODO(); }
  809. void SoftCPU::SBB_reg32_RM32(const X86::Instruction&) { TODO(); }
  810. void SoftCPU::SBB_reg8_RM8(const X86::Instruction&) { TODO(); }
  811. void SoftCPU::SCASB(const X86::Instruction&) { TODO(); }
  812. void SoftCPU::SCASD(const X86::Instruction&) { TODO(); }
  813. void SoftCPU::SCASW(const X86::Instruction&) { TODO(); }
  814. void SoftCPU::SETcc_RM8(const X86::Instruction&) { TODO(); }
  815. void SoftCPU::SGDT(const X86::Instruction&) { TODO(); }
  816. void SoftCPU::SHLD_RM16_reg16_CL(const X86::Instruction&) { TODO(); }
  817. void SoftCPU::SHLD_RM16_reg16_imm8(const X86::Instruction&) { TODO(); }
  818. void SoftCPU::SHLD_RM32_reg32_CL(const X86::Instruction&) { TODO(); }
  819. void SoftCPU::SHLD_RM32_reg32_imm8(const X86::Instruction&) { TODO(); }
  820. void SoftCPU::SHL_RM16_1(const X86::Instruction&) { TODO(); }
  821. void SoftCPU::SHL_RM16_CL(const X86::Instruction&) { TODO(); }
  822. void SoftCPU::SHL_RM16_imm8(const X86::Instruction&) { TODO(); }
  823. void SoftCPU::SHL_RM32_1(const X86::Instruction&) { TODO(); }
  824. void SoftCPU::SHL_RM32_CL(const X86::Instruction&) { TODO(); }
  825. void SoftCPU::SHL_RM32_imm8(const X86::Instruction&) { TODO(); }
  826. void SoftCPU::SHL_RM8_1(const X86::Instruction&) { TODO(); }
  827. void SoftCPU::SHL_RM8_CL(const X86::Instruction&) { TODO(); }
  828. void SoftCPU::SHL_RM8_imm8(const X86::Instruction&) { TODO(); }
  829. void SoftCPU::SHRD_RM16_reg16_CL(const X86::Instruction&) { TODO(); }
  830. void SoftCPU::SHRD_RM16_reg16_imm8(const X86::Instruction&) { TODO(); }
  831. void SoftCPU::SHRD_RM32_reg32_CL(const X86::Instruction&) { TODO(); }
  832. void SoftCPU::SHRD_RM32_reg32_imm8(const X86::Instruction&) { TODO(); }
  833. void SoftCPU::SHR_RM16_1(const X86::Instruction&) { TODO(); }
  834. void SoftCPU::SHR_RM16_CL(const X86::Instruction&) { TODO(); }
  835. void SoftCPU::SHR_RM16_imm8(const X86::Instruction&) { TODO(); }
  836. void SoftCPU::SHR_RM32_1(const X86::Instruction&) { TODO(); }
  837. void SoftCPU::SHR_RM32_CL(const X86::Instruction&) { TODO(); }
  838. void SoftCPU::SHR_RM32_imm8(const X86::Instruction&) { TODO(); }
  839. void SoftCPU::SHR_RM8_1(const X86::Instruction&) { TODO(); }
  840. void SoftCPU::SHR_RM8_CL(const X86::Instruction&) { TODO(); }
  841. void SoftCPU::SHR_RM8_imm8(const X86::Instruction&) { TODO(); }
  842. void SoftCPU::SIDT(const X86::Instruction&) { TODO(); }
  843. void SoftCPU::SLDT_RM16(const X86::Instruction&) { TODO(); }
  844. void SoftCPU::SMSW_RM16(const X86::Instruction&) { TODO(); }
  845. void SoftCPU::STC(const X86::Instruction&) { TODO(); }
  846. void SoftCPU::STD(const X86::Instruction&) { TODO(); }
  847. void SoftCPU::STI(const X86::Instruction&) { TODO(); }
  848. void SoftCPU::STOSB(const X86::Instruction&) { TODO(); }
  849. void SoftCPU::STOSD(const X86::Instruction&) { TODO(); }
  850. void SoftCPU::STOSW(const X86::Instruction&) { TODO(); }
  851. void SoftCPU::STR_RM16(const X86::Instruction&) { TODO(); }
  852. void SoftCPU::TEST_AL_imm8(const X86::Instruction&) { TODO(); }
  853. void SoftCPU::TEST_AX_imm16(const X86::Instruction&) { TODO(); }
  854. void SoftCPU::TEST_EAX_imm32(const X86::Instruction&) { TODO(); }
  855. void SoftCPU::TEST_RM16_imm16(const X86::Instruction&) { TODO(); }
  856. void SoftCPU::TEST_RM16_reg16(const X86::Instruction&) { TODO(); }
  857. void SoftCPU::TEST_RM32_imm32(const X86::Instruction&) { TODO(); }
  858. void SoftCPU::TEST_RM32_reg32(const X86::Instruction&) { TODO(); }
  859. void SoftCPU::TEST_RM8_imm8(const X86::Instruction&) { TODO(); }
  860. void SoftCPU::TEST_RM8_reg8(const X86::Instruction&) { TODO(); }
  861. void SoftCPU::UD0(const X86::Instruction&) { TODO(); }
  862. void SoftCPU::UD1(const X86::Instruction&) { TODO(); }
  863. void SoftCPU::UD2(const X86::Instruction&) { TODO(); }
  864. void SoftCPU::VERR_RM16(const X86::Instruction&) { TODO(); }
  865. void SoftCPU::VERW_RM16(const X86::Instruction&) { TODO(); }
  866. void SoftCPU::WAIT(const X86::Instruction&) { TODO(); }
  867. void SoftCPU::WBINVD(const X86::Instruction&) { TODO(); }
  868. void SoftCPU::XADD_RM16_reg16(const X86::Instruction&) { TODO(); }
  869. void SoftCPU::XADD_RM32_reg32(const X86::Instruction&) { TODO(); }
  870. void SoftCPU::XADD_RM8_reg8(const X86::Instruction&) { TODO(); }
  871. void SoftCPU::XCHG_AX_reg16(const X86::Instruction&) { TODO(); }
  872. void SoftCPU::XCHG_EAX_reg32(const X86::Instruction&) { TODO(); }
  873. void SoftCPU::XCHG_reg16_RM16(const X86::Instruction&) { TODO(); }
  874. void SoftCPU::XCHG_reg32_RM32(const X86::Instruction&) { TODO(); }
  875. void SoftCPU::XCHG_reg8_RM8(const X86::Instruction&) { TODO(); }
  876. void SoftCPU::XLAT(const X86::Instruction&) { TODO(); }
  877. #define DEFINE_GENERIC_INSN_HANDLERS(mnemonic, op, update_dest) \
  878. void SoftCPU::mnemonic##_AL_imm8(const X86::Instruction& insn) { generic_AL_imm8<update_dest>(op<u8, u8>, insn); } \
  879. void SoftCPU::mnemonic##_AX_imm16(const X86::Instruction& insn) { generic_AX_imm16<update_dest>(op<u16, u16>, insn); } \
  880. void SoftCPU::mnemonic##_EAX_imm32(const X86::Instruction& insn) { generic_EAX_imm32<update_dest>(op<u32, u32>, insn); } \
  881. void SoftCPU::mnemonic##_RM16_imm16(const X86::Instruction& insn) { generic_RM16_imm16<update_dest>(op<u16, u16>, insn); } \
  882. void SoftCPU::mnemonic##_RM16_imm8(const X86::Instruction& insn) { generic_RM16_imm8<update_dest>(op<u16, u8>, insn); } \
  883. void SoftCPU::mnemonic##_RM16_reg16(const X86::Instruction& insn) { generic_RM16_reg16<update_dest>(op<u16, u16>, insn); } \
  884. void SoftCPU::mnemonic##_RM32_imm32(const X86::Instruction& insn) { generic_RM32_imm32<update_dest>(op<u32, u32>, insn); } \
  885. void SoftCPU::mnemonic##_RM32_imm8(const X86::Instruction& insn) { generic_RM32_imm8<update_dest>(op<u32, u8>, insn); } \
  886. void SoftCPU::mnemonic##_RM32_reg32(const X86::Instruction& insn) { generic_RM32_reg32<update_dest>(op<u32, u32>, insn); } \
  887. void SoftCPU::mnemonic##_RM8_imm8(const X86::Instruction& insn) { generic_RM8_imm8<update_dest>(op<u8, u8>, insn); } \
  888. void SoftCPU::mnemonic##_RM8_reg8(const X86::Instruction& insn) { generic_RM8_reg8<update_dest>(op<u8, u8>, insn); } \
  889. void SoftCPU::mnemonic##_reg16_RM16(const X86::Instruction& insn) { generic_reg16_RM16<update_dest>(op<u16, u16>, insn); } \
  890. void SoftCPU::mnemonic##_reg32_RM32(const X86::Instruction& insn) { generic_reg32_RM32<update_dest>(op<u32, u32>, insn); } \
  891. void SoftCPU::mnemonic##_reg8_RM8(const X86::Instruction& insn) { generic_reg8_RM8<update_dest>(op<u8, u8>, insn); }
  892. DEFINE_GENERIC_INSN_HANDLERS(XOR, op_xor, true)
  893. DEFINE_GENERIC_INSN_HANDLERS(ADD, op_add, true)
  894. DEFINE_GENERIC_INSN_HANDLERS(SUB, op_sub, true)
  895. DEFINE_GENERIC_INSN_HANDLERS(CMP, op_sub, false)
  896. void SoftCPU::MOVQ_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  897. void SoftCPU::EMMS(const X86::Instruction&) { TODO(); }
  898. void SoftCPU::MOVQ_mm1_m64_mm2(const X86::Instruction&) { TODO(); }
  899. void SoftCPU::wrap_0xC0(const X86::Instruction&) { TODO(); }
  900. void SoftCPU::wrap_0xC1_16(const X86::Instruction&) { TODO(); }
  901. void SoftCPU::wrap_0xC1_32(const X86::Instruction&) { TODO(); }
  902. void SoftCPU::wrap_0xD0(const X86::Instruction&) { TODO(); }
  903. void SoftCPU::wrap_0xD1_16(const X86::Instruction&) { TODO(); }
  904. void SoftCPU::wrap_0xD1_32(const X86::Instruction&) { TODO(); }
  905. void SoftCPU::wrap_0xD2(const X86::Instruction&) { TODO(); }
  906. void SoftCPU::wrap_0xD3_16(const X86::Instruction&) { TODO(); }
  907. void SoftCPU::wrap_0xD3_32(const X86::Instruction&) { TODO(); }
  908. }