SoftCPU.cpp 42 KB

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