SoftCPU.cpp 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813
  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. //#define MEMORY_DEBUG
  32. namespace UserspaceEmulator {
  33. template<typename T, typename U>
  34. inline constexpr T sign_extended_to(U value)
  35. {
  36. if (!(value & X86::TypeTrivia<U>::sign_bit))
  37. return value;
  38. return (X86::TypeTrivia<T>::mask & ~X86::TypeTrivia<U>::mask) | value;
  39. }
  40. SoftCPU::SoftCPU(Emulator& emulator)
  41. : m_emulator(emulator)
  42. {
  43. memset(m_gpr, 0, sizeof(m_gpr));
  44. m_segment[(int)X86::SegmentRegister::CS] = 0x18;
  45. m_segment[(int)X86::SegmentRegister::DS] = 0x20;
  46. m_segment[(int)X86::SegmentRegister::ES] = 0x20;
  47. m_segment[(int)X86::SegmentRegister::SS] = 0x20;
  48. m_segment[(int)X86::SegmentRegister::GS] = 0x28;
  49. }
  50. void SoftCPU::dump() const
  51. {
  52. printf("eax=%08x ebx=%08x ecx=%08x edx=%08x ", eax(), ebx(), ecx(), edx());
  53. printf("ebp=%08x esp=%08x esi=%08x edi=%08x ", ebp(), esp(), esi(), edi());
  54. printf("o=%u s=%u z=%u a=%u p=%u c=%u\n", of(), sf(), zf(), af(), pf(), cf());
  55. }
  56. u8 SoftCPU::read8()
  57. {
  58. auto value = read_memory8({ cs(), eip() });
  59. m_eip += 1;
  60. return value;
  61. }
  62. u16 SoftCPU::read16()
  63. {
  64. auto value = read_memory16({ cs(), eip() });
  65. m_eip += 2;
  66. return value;
  67. }
  68. u32 SoftCPU::read32()
  69. {
  70. auto value = read_memory32({ cs(), eip() });
  71. m_eip += 4;
  72. return value;
  73. }
  74. u8 SoftCPU::read_memory8(X86::LogicalAddress address)
  75. {
  76. ASSERT(address.selector() == 0x18 || address.selector() == 0x20 || address.selector() == 0x28);
  77. auto value = m_emulator.mmu().read8(address);
  78. #ifdef MEMORY_DEBUG
  79. printf("\033[36;1mread_memory8: @%08x:%08x -> %02x\033[0m\n", address.selector(), address.offset(), value);
  80. #endif
  81. return value;
  82. }
  83. u16 SoftCPU::read_memory16(X86::LogicalAddress address)
  84. {
  85. ASSERT(address.selector() == 0x18 || address.selector() == 0x20 || address.selector() == 0x28);
  86. auto value = m_emulator.mmu().read16(address);
  87. #ifdef MEMORY_DEBUG
  88. printf("\033[36;1mread_memory16: @%04x:%08x -> %04x\033[0m\n", address.selector(), address.offset(), value);
  89. #endif
  90. return value;
  91. }
  92. u32 SoftCPU::read_memory32(X86::LogicalAddress address)
  93. {
  94. ASSERT(address.selector() == 0x18 || address.selector() == 0x20 || address.selector() == 0x28);
  95. auto value = m_emulator.mmu().read32(address);
  96. #ifdef MEMORY_DEBUG
  97. printf("\033[36;1mread_memory32: @%04x:%08x -> %08x\033[0m\n", address.selector(), address.offset(), value);
  98. #endif
  99. return value;
  100. }
  101. void SoftCPU::write_memory8(X86::LogicalAddress address, u8 value)
  102. {
  103. ASSERT(address.selector() == 0x20 || address.selector() == 0x28);
  104. #ifdef MEMORY_DEBUG
  105. printf("\033[35;1mwrite_memory8: @%04x:%08x <- %02x\033[0m\n", address.selector(), address.offset(), value);
  106. #endif
  107. m_emulator.mmu().write8(address, value);
  108. }
  109. void SoftCPU::write_memory16(X86::LogicalAddress address, u16 value)
  110. {
  111. ASSERT(address.selector() == 0x20 || address.selector() == 0x28);
  112. #ifdef MEMORY_DEBUG
  113. printf("\033[35;1mwrite_memory16: @%04x:%08x <- %04x\033[0m\n", address.selector(), address.offset(), value);
  114. #endif
  115. m_emulator.mmu().write16(address, value);
  116. }
  117. void SoftCPU::write_memory32(X86::LogicalAddress address, u32 value)
  118. {
  119. ASSERT(address.selector() == 0x20 || address.selector() == 0x28);
  120. #ifdef MEMORY_DEBUG
  121. printf("\033[35;1mwrite_memory32: @%04x:%08x <- %08x\033[0m\n", address.selector(), address.offset(), value);
  122. #endif
  123. m_emulator.mmu().write32(address, value);
  124. }
  125. void SoftCPU::push_string(const StringView& string)
  126. {
  127. size_t space_to_allocate = round_up_to_power_of_two(string.length() + 1, 16);
  128. set_esp(esp() - space_to_allocate);
  129. m_emulator.mmu().copy_to_vm(esp(), string.characters_without_null_termination(), string.length());
  130. m_emulator.mmu().write8({ 0x20, esp() + string.length() }, '\0');
  131. }
  132. void SoftCPU::push32(u32 value)
  133. {
  134. set_esp(esp() - sizeof(value));
  135. write_memory32({ ss(), esp() }, value);
  136. }
  137. u32 SoftCPU::pop32()
  138. {
  139. auto value = read_memory32({ ss(), esp() });
  140. set_esp(esp() + sizeof(value));
  141. return value;
  142. }
  143. template<bool check_zf, typename Callback>
  144. void SoftCPU::do_once_or_repeat(const X86::Instruction& insn, Callback callback)
  145. {
  146. if (!insn.has_rep_prefix())
  147. return callback();
  148. if (insn.has_address_size_override_prefix()) {
  149. while (cx()) {
  150. callback();
  151. set_cx(cx() - 1);
  152. if constexpr (check_zf) {
  153. if (insn.rep_prefix() == X86::Prefix::REPZ && !zf())
  154. break;
  155. if (insn.rep_prefix() == X86::Prefix::REPNZ && zf())
  156. break;
  157. }
  158. }
  159. return;
  160. }
  161. while (ecx()) {
  162. callback();
  163. set_ecx(ecx() - 1);
  164. if constexpr (check_zf) {
  165. if (insn.rep_prefix() == X86::Prefix::REPZ && !zf())
  166. break;
  167. if (insn.rep_prefix() == X86::Prefix::REPNZ && zf())
  168. break;
  169. }
  170. }
  171. }
  172. template<typename T>
  173. static T op_inc(SoftCPU& cpu, T data)
  174. {
  175. T result = 0;
  176. u32 new_flags = 0;
  177. if constexpr (sizeof(T) == 4) {
  178. asm volatile("incl %%eax\n"
  179. : "=a"(result)
  180. : "a"(data));
  181. } else if constexpr (sizeof(T) == 2) {
  182. asm volatile("incw %%ax\n"
  183. : "=a"(result)
  184. : "a"(data));
  185. } else if constexpr (sizeof(T) == 1) {
  186. asm volatile("incb %%al\n"
  187. : "=a"(result)
  188. : "a"(data));
  189. }
  190. asm volatile(
  191. "pushf\n"
  192. "pop %%ebx"
  193. : "=b"(new_flags));
  194. cpu.set_flags_oszap(new_flags);
  195. return result;
  196. }
  197. template<typename T>
  198. static T op_dec(SoftCPU& cpu, T data)
  199. {
  200. T result = 0;
  201. u32 new_flags = 0;
  202. if constexpr (sizeof(T) == 4) {
  203. asm volatile("decl %%eax\n"
  204. : "=a"(result)
  205. : "a"(data));
  206. } else if constexpr (sizeof(T) == 2) {
  207. asm volatile("decw %%ax\n"
  208. : "=a"(result)
  209. : "a"(data));
  210. } else if constexpr (sizeof(T) == 1) {
  211. asm volatile("decb %%al\n"
  212. : "=a"(result)
  213. : "a"(data));
  214. }
  215. asm volatile(
  216. "pushf\n"
  217. "pop %%ebx"
  218. : "=b"(new_flags));
  219. cpu.set_flags_oszap(new_flags);
  220. return result;
  221. }
  222. template<typename T>
  223. static T op_xor(SoftCPU& cpu, const T& dest, const T& src)
  224. {
  225. T result = 0;
  226. u32 new_flags = 0;
  227. if constexpr (sizeof(T) == 4) {
  228. asm volatile("xorl %%ecx, %%eax\n"
  229. : "=a"(result)
  230. : "a"(dest), "c"((u32)src));
  231. } else if constexpr (sizeof(T) == 2) {
  232. asm volatile("xor %%cx, %%ax\n"
  233. : "=a"(result)
  234. : "a"(dest), "c"((u16)src));
  235. } else if constexpr (sizeof(T) == 1) {
  236. asm volatile("xorb %%cl, %%al\n"
  237. : "=a"(result)
  238. : "a"(dest), "c"((u8)src));
  239. } else {
  240. ASSERT_NOT_REACHED();
  241. }
  242. asm volatile(
  243. "pushf\n"
  244. "pop %%ebx"
  245. : "=b"(new_flags));
  246. cpu.set_flags_oszpc(new_flags);
  247. return result;
  248. }
  249. template<typename T>
  250. static T op_or(SoftCPU& cpu, const T& dest, const T& src)
  251. {
  252. T result = 0;
  253. u32 new_flags = 0;
  254. if constexpr (sizeof(T) == 4) {
  255. asm volatile("orl %%ecx, %%eax\n"
  256. : "=a"(result)
  257. : "a"(dest), "c"((u32)src));
  258. } else if constexpr (sizeof(T) == 2) {
  259. asm volatile("or %%cx, %%ax\n"
  260. : "=a"(result)
  261. : "a"(dest), "c"((u16)src));
  262. } else if constexpr (sizeof(T) == 1) {
  263. asm volatile("orb %%cl, %%al\n"
  264. : "=a"(result)
  265. : "a"(dest), "c"((u8)src));
  266. } else {
  267. ASSERT_NOT_REACHED();
  268. }
  269. asm volatile(
  270. "pushf\n"
  271. "pop %%ebx"
  272. : "=b"(new_flags));
  273. cpu.set_flags_oszpc(new_flags);
  274. return result;
  275. }
  276. template<typename T>
  277. static T op_sub(SoftCPU& cpu, const T& dest, const T& src)
  278. {
  279. T result = 0;
  280. u32 new_flags = 0;
  281. if constexpr (sizeof(T) == 4) {
  282. asm volatile("subl %%ecx, %%eax\n"
  283. : "=a"(result)
  284. : "a"(dest), "c"((u32)src));
  285. } else if constexpr (sizeof(T) == 2) {
  286. asm volatile("subw %%cx, %%ax\n"
  287. : "=a"(result)
  288. : "a"(dest), "c"((u16)src));
  289. } else if constexpr (sizeof(T) == 1) {
  290. asm volatile("subb %%cl, %%al\n"
  291. : "=a"(result)
  292. : "a"(dest), "c"((u8)src));
  293. } else {
  294. ASSERT_NOT_REACHED();
  295. }
  296. asm volatile(
  297. "pushf\n"
  298. "pop %%ebx"
  299. : "=b"(new_flags));
  300. cpu.set_flags_oszapc(new_flags);
  301. return result;
  302. }
  303. template<typename T, bool cf>
  304. static T op_sbb_impl(SoftCPU& cpu, const T& dest, const T& src)
  305. {
  306. T result = 0;
  307. u32 new_flags = 0;
  308. if constexpr (cf)
  309. asm volatile("stc");
  310. else
  311. asm volatile("clc");
  312. if constexpr (sizeof(T) == 4) {
  313. asm volatile("sbbl %%ecx, %%eax\n"
  314. : "=a"(result)
  315. : "a"(dest), "c"((u32)src));
  316. } else if constexpr (sizeof(T) == 2) {
  317. asm volatile("sbbw %%cx, %%ax\n"
  318. : "=a"(result)
  319. : "a"(dest), "c"((u16)src));
  320. } else if constexpr (sizeof(T) == 1) {
  321. asm volatile("sbbb %%cl, %%al\n"
  322. : "=a"(result)
  323. : "a"(dest), "c"((u8)src));
  324. } else {
  325. ASSERT_NOT_REACHED();
  326. }
  327. asm volatile(
  328. "pushf\n"
  329. "pop %%ebx"
  330. : "=b"(new_flags));
  331. cpu.set_flags_oszapc(new_flags);
  332. return result;
  333. }
  334. template<typename T>
  335. static T op_sbb(SoftCPU& cpu, T& dest, const T& src)
  336. {
  337. if (cpu.cf())
  338. return op_sbb_impl<T, true>(cpu, dest, src);
  339. return op_sbb_impl<T, false>(cpu, dest, src);
  340. }
  341. template<typename T>
  342. static T op_add(SoftCPU& cpu, T& dest, const T& src)
  343. {
  344. T result = 0;
  345. u32 new_flags = 0;
  346. if constexpr (sizeof(T) == 4) {
  347. asm volatile("addl %%ecx, %%eax\n"
  348. : "=a"(result)
  349. : "a"(dest), "c"((u32)src));
  350. } else if constexpr (sizeof(T) == 2) {
  351. asm volatile("addw %%cx, %%ax\n"
  352. : "=a"(result)
  353. : "a"(dest), "c"((u16)src));
  354. } else if constexpr (sizeof(T) == 1) {
  355. asm volatile("addb %%cl, %%al\n"
  356. : "=a"(result)
  357. : "a"(dest), "c"((u8)src));
  358. } else {
  359. ASSERT_NOT_REACHED();
  360. }
  361. asm volatile(
  362. "pushf\n"
  363. "pop %%ebx"
  364. : "=b"(new_flags));
  365. cpu.set_flags_oszapc(new_flags);
  366. return result;
  367. }
  368. template<typename T, bool cf>
  369. static T op_adc_impl(SoftCPU& cpu, T& dest, const T& src)
  370. {
  371. T result = 0;
  372. u32 new_flags = 0;
  373. if constexpr (cf)
  374. asm volatile("stc");
  375. else
  376. asm volatile("clc");
  377. if constexpr (sizeof(T) == 4) {
  378. asm volatile("adcl %%ecx, %%eax\n"
  379. : "=a"(result)
  380. : "a"(dest), "c"((u32)src));
  381. } else if constexpr (sizeof(T) == 2) {
  382. asm volatile("adcw %%cx, %%ax\n"
  383. : "=a"(result)
  384. : "a"(dest), "c"((u16)src));
  385. } else if constexpr (sizeof(T) == 1) {
  386. asm volatile("adcb %%cl, %%al\n"
  387. : "=a"(result)
  388. : "a"(dest), "c"((u8)src));
  389. } else {
  390. ASSERT_NOT_REACHED();
  391. }
  392. asm volatile(
  393. "pushf\n"
  394. "pop %%ebx"
  395. : "=b"(new_flags));
  396. cpu.set_flags_oszapc(new_flags);
  397. return result;
  398. }
  399. template<typename T>
  400. static T op_adc(SoftCPU& cpu, T& dest, const T& src)
  401. {
  402. if (cpu.cf())
  403. return op_adc_impl<T, true>(cpu, dest, src);
  404. return op_adc_impl<T, false>(cpu, dest, src);
  405. }
  406. template<typename T>
  407. static T op_and(SoftCPU& cpu, const T& dest, const T& src)
  408. {
  409. T result = 0;
  410. u32 new_flags = 0;
  411. if constexpr (sizeof(T) == 4) {
  412. asm volatile("andl %%ecx, %%eax\n"
  413. : "=a"(result)
  414. : "a"(dest), "c"((u32)src));
  415. } else if constexpr (sizeof(T) == 2) {
  416. asm volatile("andw %%cx, %%ax\n"
  417. : "=a"(result)
  418. : "a"(dest), "c"((u16)src));
  419. } else if constexpr (sizeof(T) == 1) {
  420. asm volatile("andb %%cl, %%al\n"
  421. : "=a"(result)
  422. : "a"(dest), "c"((u8)src));
  423. } else {
  424. ASSERT_NOT_REACHED();
  425. }
  426. asm volatile(
  427. "pushf\n"
  428. "pop %%ebx"
  429. : "=b"(new_flags));
  430. cpu.set_flags_oszpc(new_flags);
  431. return result;
  432. }
  433. template<typename T>
  434. static T op_imul(SoftCPU& cpu, const T& dest, const T& src)
  435. {
  436. T result = 0;
  437. u32 new_flags = 0;
  438. if constexpr (sizeof(T) == 4) {
  439. asm volatile("imull %%ecx, %%eax\n"
  440. : "=a"(result)
  441. : "a"(dest), "c"((i32)src));
  442. } else if constexpr (sizeof(T) == 2) {
  443. asm volatile("imulw %%cx, %%ax\n"
  444. : "=a"(result)
  445. : "a"(dest), "c"((i16)src));
  446. } else {
  447. ASSERT_NOT_REACHED();
  448. }
  449. asm volatile(
  450. "pushf\n"
  451. "pop %%ebx"
  452. : "=b"(new_flags));
  453. cpu.set_flags_oszapc(new_flags);
  454. return result;
  455. }
  456. template<typename T>
  457. static T op_shr(SoftCPU& cpu, T data, u8 steps)
  458. {
  459. if (steps == 0)
  460. return data;
  461. u32 result = 0;
  462. u32 new_flags = 0;
  463. if constexpr (sizeof(T) == 4) {
  464. asm volatile("shrl %%cl, %%eax\n"
  465. : "=a"(result)
  466. : "a"(data), "c"(steps));
  467. } else if constexpr (sizeof(T) == 2) {
  468. asm volatile("shrw %%cl, %%ax\n"
  469. : "=a"(result)
  470. : "a"(data), "c"(steps));
  471. } else if constexpr (sizeof(T) == 1) {
  472. asm volatile("shrb %%cl, %%al\n"
  473. : "=a"(result)
  474. : "a"(data), "c"(steps));
  475. }
  476. asm volatile(
  477. "pushf\n"
  478. "pop %%ebx"
  479. : "=b"(new_flags));
  480. cpu.set_flags_oszapc(new_flags);
  481. return result;
  482. }
  483. template<typename T>
  484. static T op_shl(SoftCPU& cpu, T data, u8 steps)
  485. {
  486. if (steps == 0)
  487. return data;
  488. u32 result = 0;
  489. u32 new_flags = 0;
  490. if constexpr (sizeof(T) == 4) {
  491. asm volatile("shll %%cl, %%eax\n"
  492. : "=a"(result)
  493. : "a"(data), "c"(steps));
  494. } else if constexpr (sizeof(T) == 2) {
  495. asm volatile("shlw %%cl, %%ax\n"
  496. : "=a"(result)
  497. : "a"(data), "c"(steps));
  498. } else if constexpr (sizeof(T) == 1) {
  499. asm volatile("shlb %%cl, %%al\n"
  500. : "=a"(result)
  501. : "a"(data), "c"(steps));
  502. }
  503. asm volatile(
  504. "pushf\n"
  505. "pop %%ebx"
  506. : "=b"(new_flags));
  507. cpu.set_flags_oszapc(new_flags);
  508. return result;
  509. }
  510. template<bool update_dest, typename Op>
  511. void SoftCPU::generic_AL_imm8(Op op, const X86::Instruction& insn)
  512. {
  513. auto dest = al();
  514. auto src = insn.imm8();
  515. auto result = op(*this, dest, src);
  516. if (update_dest)
  517. set_al(result);
  518. }
  519. template<bool update_dest, typename Op>
  520. void SoftCPU::generic_AX_imm16(Op op, const X86::Instruction& insn)
  521. {
  522. auto dest = ax();
  523. auto src = insn.imm16();
  524. auto result = op(*this, dest, src);
  525. if (update_dest)
  526. set_ax(result);
  527. }
  528. template<bool update_dest, typename Op>
  529. void SoftCPU::generic_EAX_imm32(Op op, const X86::Instruction& insn)
  530. {
  531. auto dest = eax();
  532. auto src = insn.imm32();
  533. auto result = op(*this, dest, src);
  534. if (update_dest)
  535. set_eax(result);
  536. }
  537. template<bool update_dest, typename Op>
  538. void SoftCPU::generic_RM16_imm16(Op op, const X86::Instruction& insn)
  539. {
  540. auto dest = insn.modrm().read16(*this, insn);
  541. auto src = insn.imm16();
  542. auto result = op(*this, dest, src);
  543. if (update_dest)
  544. insn.modrm().write16(*this, insn, result);
  545. }
  546. template<bool update_dest, typename Op>
  547. void SoftCPU::generic_RM16_imm8(Op op, const X86::Instruction& insn)
  548. {
  549. auto dest = insn.modrm().read16(*this, insn);
  550. auto src = sign_extended_to<u16>(insn.imm8());
  551. auto result = op(*this, dest, src);
  552. if (update_dest)
  553. insn.modrm().write16(*this, insn, result);
  554. }
  555. template<bool update_dest, typename Op>
  556. void SoftCPU::generic_RM16_reg16(Op op, const X86::Instruction& insn)
  557. {
  558. auto dest = insn.modrm().read16(*this, insn);
  559. auto src = gpr16(insn.reg16());
  560. auto result = op(*this, dest, src);
  561. if (update_dest)
  562. insn.modrm().write16(*this, insn, result);
  563. }
  564. template<bool update_dest, typename Op>
  565. void SoftCPU::generic_RM32_imm32(Op op, const X86::Instruction& insn)
  566. {
  567. auto dest = insn.modrm().read32(*this, insn);
  568. auto src = insn.imm32();
  569. auto result = op(*this, dest, src);
  570. if (update_dest)
  571. insn.modrm().write32(*this, insn, result);
  572. }
  573. template<bool update_dest, typename Op>
  574. void SoftCPU::generic_RM32_imm8(Op op, const X86::Instruction& insn)
  575. {
  576. auto dest = insn.modrm().read32(*this, insn);
  577. auto src = sign_extended_to<u32>(insn.imm8());
  578. auto result = op(*this, dest, src);
  579. if (update_dest)
  580. insn.modrm().write32(*this, insn, result);
  581. }
  582. template<bool update_dest, typename Op>
  583. void SoftCPU::generic_RM32_reg32(Op op, const X86::Instruction& insn)
  584. {
  585. auto dest = insn.modrm().read32(*this, insn);
  586. auto src = gpr32(insn.reg32());
  587. auto result = op(*this, dest, src);
  588. if (update_dest)
  589. insn.modrm().write32(*this, insn, result);
  590. }
  591. template<bool update_dest, typename Op>
  592. void SoftCPU::generic_RM8_imm8(Op op, const X86::Instruction& insn)
  593. {
  594. auto dest = insn.modrm().read8(*this, insn);
  595. auto src = insn.imm8();
  596. auto result = op(*this, dest, src);
  597. if (update_dest)
  598. insn.modrm().write8(*this, insn, result);
  599. }
  600. template<bool update_dest, typename Op>
  601. void SoftCPU::generic_RM8_reg8(Op op, const X86::Instruction& insn)
  602. {
  603. auto dest = insn.modrm().read8(*this, insn);
  604. auto src = gpr8(insn.reg8());
  605. auto result = op(*this, dest, src);
  606. if (update_dest)
  607. insn.modrm().write8(*this, insn, result);
  608. }
  609. template<bool update_dest, typename Op>
  610. void SoftCPU::generic_reg16_RM16(Op op, const X86::Instruction& insn)
  611. {
  612. auto dest = gpr16(insn.reg16());
  613. auto src = insn.modrm().read16(*this, insn);
  614. auto result = op(*this, dest, src);
  615. if (update_dest)
  616. gpr16(insn.reg16()) = result;
  617. }
  618. template<bool update_dest, typename Op>
  619. void SoftCPU::generic_reg32_RM32(Op op, const X86::Instruction& insn)
  620. {
  621. auto dest = gpr32(insn.reg32());
  622. auto src = insn.modrm().read32(*this, insn);
  623. auto result = op(*this, dest, src);
  624. if (update_dest)
  625. gpr32(insn.reg32()) = result;
  626. }
  627. template<bool update_dest, typename Op>
  628. void SoftCPU::generic_reg8_RM8(Op op, const X86::Instruction& insn)
  629. {
  630. auto dest = gpr8(insn.reg8());
  631. auto src = insn.modrm().read8(*this, insn);
  632. auto result = op(*this, dest, src);
  633. if (update_dest)
  634. gpr8(insn.reg8()) = result;
  635. }
  636. void SoftCPU::AAA(const X86::Instruction&) { TODO(); }
  637. void SoftCPU::AAD(const X86::Instruction&) { TODO(); }
  638. void SoftCPU::AAM(const X86::Instruction&) { TODO(); }
  639. void SoftCPU::AAS(const X86::Instruction&) { TODO(); }
  640. void SoftCPU::ARPL(const X86::Instruction&) { TODO(); }
  641. void SoftCPU::BOUND(const X86::Instruction&) { TODO(); }
  642. void SoftCPU::BSF_reg16_RM16(const X86::Instruction&) { TODO(); }
  643. void SoftCPU::BSF_reg32_RM32(const X86::Instruction&) { TODO(); }
  644. void SoftCPU::BSR_reg16_RM16(const X86::Instruction&) { TODO(); }
  645. void SoftCPU::BSR_reg32_RM32(const X86::Instruction&) { TODO(); }
  646. void SoftCPU::BSWAP_reg32(const X86::Instruction&) { TODO(); }
  647. void SoftCPU::BTC_RM16_imm8(const X86::Instruction&) { TODO(); }
  648. void SoftCPU::BTC_RM16_reg16(const X86::Instruction&) { TODO(); }
  649. void SoftCPU::BTC_RM32_imm8(const X86::Instruction&) { TODO(); }
  650. void SoftCPU::BTC_RM32_reg32(const X86::Instruction&) { TODO(); }
  651. void SoftCPU::BTR_RM16_imm8(const X86::Instruction&) { TODO(); }
  652. void SoftCPU::BTR_RM16_reg16(const X86::Instruction&) { TODO(); }
  653. void SoftCPU::BTR_RM32_imm8(const X86::Instruction&) { TODO(); }
  654. void SoftCPU::BTR_RM32_reg32(const X86::Instruction&) { TODO(); }
  655. void SoftCPU::BTS_RM16_imm8(const X86::Instruction&) { TODO(); }
  656. void SoftCPU::BTS_RM16_reg16(const X86::Instruction&) { TODO(); }
  657. void SoftCPU::BTS_RM32_imm8(const X86::Instruction&) { TODO(); }
  658. void SoftCPU::BTS_RM32_reg32(const X86::Instruction&) { TODO(); }
  659. void SoftCPU::BT_RM16_imm8(const X86::Instruction&) { TODO(); }
  660. void SoftCPU::BT_RM16_reg16(const X86::Instruction&) { TODO(); }
  661. void SoftCPU::BT_RM32_imm8(const X86::Instruction&) { TODO(); }
  662. void SoftCPU::BT_RM32_reg32(const X86::Instruction&) { TODO(); }
  663. void SoftCPU::CALL_FAR_mem16(const X86::Instruction&) { TODO(); }
  664. void SoftCPU::CALL_FAR_mem32(const X86::Instruction&) { TODO(); }
  665. void SoftCPU::CALL_RM16(const X86::Instruction&) { TODO(); }
  666. void SoftCPU::CALL_RM32(const X86::Instruction& insn)
  667. {
  668. push32(eip());
  669. set_eip(insn.modrm().read32(*this, insn));
  670. }
  671. void SoftCPU::CALL_imm16(const X86::Instruction&) { TODO(); }
  672. void SoftCPU::CALL_imm16_imm16(const X86::Instruction&) { TODO(); }
  673. void SoftCPU::CALL_imm16_imm32(const X86::Instruction&) { TODO(); }
  674. void SoftCPU::CALL_imm32(const X86::Instruction& insn)
  675. {
  676. push32(eip());
  677. set_eip(eip() + (i32)insn.imm32());
  678. }
  679. void SoftCPU::CBW(const X86::Instruction&) { TODO(); }
  680. void SoftCPU::CDQ(const X86::Instruction&) { TODO(); }
  681. void SoftCPU::CLC(const X86::Instruction&)
  682. {
  683. set_cf(false);
  684. }
  685. void SoftCPU::CLD(const X86::Instruction&)
  686. {
  687. set_df(false);
  688. }
  689. void SoftCPU::CLI(const X86::Instruction&) { TODO(); }
  690. void SoftCPU::CLTS(const X86::Instruction&) { TODO(); }
  691. void SoftCPU::CMC(const X86::Instruction&) { TODO(); }
  692. void SoftCPU::CMOVcc_reg16_RM16(const X86::Instruction& insn)
  693. {
  694. if (evaluate_condition(insn.cc()))
  695. gpr16(insn.reg16()) = insn.modrm().read16(*this, insn);
  696. }
  697. void SoftCPU::CMOVcc_reg32_RM32(const X86::Instruction& insn)
  698. {
  699. if (evaluate_condition(insn.cc()))
  700. gpr32(insn.reg32()) = insn.modrm().read32(*this, insn);
  701. }
  702. void SoftCPU::CMPSB(const X86::Instruction&) { TODO(); }
  703. void SoftCPU::CMPSD(const X86::Instruction&) { TODO(); }
  704. void SoftCPU::CMPSW(const X86::Instruction&) { TODO(); }
  705. void SoftCPU::CMPXCHG_RM16_reg16(const X86::Instruction& insn)
  706. {
  707. auto current = insn.modrm().read16(*this, insn);
  708. if (current == eax()) {
  709. set_zf(true);
  710. insn.modrm().write16(*this, insn, gpr16(insn.reg16()));
  711. } else {
  712. set_zf(false);
  713. set_eax(current);
  714. }
  715. }
  716. void SoftCPU::CMPXCHG_RM32_reg32(const X86::Instruction& insn)
  717. {
  718. auto current = insn.modrm().read32(*this, insn);
  719. if (current == eax()) {
  720. set_zf(true);
  721. insn.modrm().write32(*this, insn, gpr32(insn.reg32()));
  722. } else {
  723. set_zf(false);
  724. set_eax(current);
  725. }
  726. }
  727. void SoftCPU::CMPXCHG_RM8_reg8(const X86::Instruction& insn)
  728. {
  729. auto current = insn.modrm().read8(*this, insn);
  730. if (current == eax()) {
  731. set_zf(true);
  732. insn.modrm().write8(*this, insn, gpr8(insn.reg8()));
  733. } else {
  734. set_zf(false);
  735. set_eax(current);
  736. }
  737. }
  738. void SoftCPU::CPUID(const X86::Instruction&) { TODO(); }
  739. void SoftCPU::CWD(const X86::Instruction&) { TODO(); }
  740. void SoftCPU::CWDE(const X86::Instruction&) { TODO(); }
  741. void SoftCPU::DAA(const X86::Instruction&) { TODO(); }
  742. void SoftCPU::DAS(const X86::Instruction&) { TODO(); }
  743. void SoftCPU::DEC_RM16(const X86::Instruction& insn)
  744. {
  745. insn.modrm().write16(*this, insn, op_dec(*this, insn.modrm().read16(*this, insn)));
  746. }
  747. void SoftCPU::DEC_RM32(const X86::Instruction& insn)
  748. {
  749. insn.modrm().write32(*this, insn, op_dec(*this, insn.modrm().read32(*this, insn)));
  750. }
  751. void SoftCPU::DEC_RM8(const X86::Instruction& insn)
  752. {
  753. insn.modrm().write8(*this, insn, op_dec(*this, insn.modrm().read8(*this, insn)));
  754. }
  755. void SoftCPU::DEC_reg16(const X86::Instruction& insn)
  756. {
  757. gpr16(insn.reg16()) = op_dec(*this, gpr16(insn.reg16()));
  758. }
  759. void SoftCPU::DEC_reg32(const X86::Instruction& insn)
  760. {
  761. gpr32(insn.reg32()) = op_dec(*this, gpr32(insn.reg32()));
  762. }
  763. void SoftCPU::DIV_RM16(const X86::Instruction&) { TODO(); }
  764. void SoftCPU::DIV_RM32(const X86::Instruction& insn)
  765. {
  766. auto divisor = insn.modrm().read32(*this, insn);
  767. if (divisor == 0) {
  768. warn() << "Divide by zero";
  769. TODO();
  770. }
  771. u64 dividend = ((u64)edx() << 32) | eax();
  772. auto result = dividend / divisor;
  773. if (result > NumericLimits<u32>::max()) {
  774. warn() << "Divide overflow";
  775. TODO();
  776. }
  777. set_eax(result);
  778. set_edx(dividend % divisor);
  779. }
  780. void SoftCPU::DIV_RM8(const X86::Instruction&) { TODO(); }
  781. void SoftCPU::ENTER16(const X86::Instruction&) { TODO(); }
  782. void SoftCPU::ENTER32(const X86::Instruction&) { TODO(); }
  783. void SoftCPU::ESCAPE(const X86::Instruction&) { TODO(); }
  784. void SoftCPU::HLT(const X86::Instruction&) { TODO(); }
  785. void SoftCPU::IDIV_RM16(const X86::Instruction&) { TODO(); }
  786. void SoftCPU::IDIV_RM32(const X86::Instruction&) { TODO(); }
  787. void SoftCPU::IDIV_RM8(const X86::Instruction&) { TODO(); }
  788. void SoftCPU::IMUL_RM16(const X86::Instruction&) { TODO(); }
  789. void SoftCPU::IMUL_RM32(const X86::Instruction&) { TODO(); }
  790. void SoftCPU::IMUL_RM8(const X86::Instruction&) { TODO(); }
  791. void SoftCPU::IMUL_reg16_RM16(const X86::Instruction& insn)
  792. {
  793. gpr16(insn.reg16()) = op_imul<i16>(*this, gpr16(insn.reg16()), insn.modrm().read16(*this, insn));
  794. }
  795. void SoftCPU::IMUL_reg16_RM16_imm16(const X86::Instruction& insn)
  796. {
  797. gpr16(insn.reg16()) = op_imul<i16>(*this, insn.modrm().read16(*this, insn), insn.imm16());
  798. }
  799. void SoftCPU::IMUL_reg16_RM16_imm8(const X86::Instruction& insn)
  800. {
  801. gpr16(insn.reg16()) = op_imul<i16>(*this, insn.modrm().read16(*this, insn), sign_extended_to<i16>(insn.imm8()));
  802. }
  803. void SoftCPU::IMUL_reg32_RM32(const X86::Instruction& insn)
  804. {
  805. gpr32(insn.reg32()) = op_imul<i32>(*this, gpr32(insn.reg32()), insn.modrm().read32(*this, insn));
  806. }
  807. void SoftCPU::IMUL_reg32_RM32_imm32(const X86::Instruction& insn)
  808. {
  809. gpr32(insn.reg32()) = op_imul<i32>(*this, insn.modrm().read32(*this, insn), insn.imm32());
  810. }
  811. void SoftCPU::IMUL_reg32_RM32_imm8(const X86::Instruction& insn)
  812. {
  813. gpr32(insn.reg32()) = op_imul<i32>(*this, insn.modrm().read32(*this, insn), sign_extended_to<i32>(insn.imm8()));
  814. }
  815. void SoftCPU::INC_RM16(const X86::Instruction& insn)
  816. {
  817. insn.modrm().write16(*this, insn, op_inc(*this, insn.modrm().read16(*this, insn)));
  818. }
  819. void SoftCPU::INC_RM32(const X86::Instruction& insn)
  820. {
  821. insn.modrm().write32(*this, insn, op_inc(*this, insn.modrm().read32(*this, insn)));
  822. }
  823. void SoftCPU::INC_RM8(const X86::Instruction& insn)
  824. {
  825. insn.modrm().write8(*this, insn, op_inc(*this, insn.modrm().read8(*this, insn)));
  826. }
  827. void SoftCPU::INC_reg16(const X86::Instruction& insn)
  828. {
  829. gpr16(insn.reg16()) = op_inc(*this, gpr16(insn.reg16()));
  830. }
  831. void SoftCPU::INC_reg32(const X86::Instruction& insn)
  832. {
  833. gpr32(insn.reg32()) = op_inc(*this, gpr32(insn.reg32()));
  834. }
  835. void SoftCPU::INSB(const X86::Instruction&) { TODO(); }
  836. void SoftCPU::INSD(const X86::Instruction&) { TODO(); }
  837. void SoftCPU::INSW(const X86::Instruction&) { TODO(); }
  838. void SoftCPU::INT3(const X86::Instruction&) { TODO(); }
  839. void SoftCPU::INTO(const X86::Instruction&) { TODO(); }
  840. void SoftCPU::INT_imm8(const X86::Instruction& insn)
  841. {
  842. ASSERT(insn.imm8() == 0x82);
  843. set_eax(m_emulator.virt_syscall(eax(), edx(), ecx(), ebx()));
  844. }
  845. void SoftCPU::INVLPG(const X86::Instruction&) { TODO(); }
  846. void SoftCPU::IN_AL_DX(const X86::Instruction&) { TODO(); }
  847. void SoftCPU::IN_AL_imm8(const X86::Instruction&) { TODO(); }
  848. void SoftCPU::IN_AX_DX(const X86::Instruction&) { TODO(); }
  849. void SoftCPU::IN_AX_imm8(const X86::Instruction&) { TODO(); }
  850. void SoftCPU::IN_EAX_DX(const X86::Instruction&) { TODO(); }
  851. void SoftCPU::IN_EAX_imm8(const X86::Instruction&) { TODO(); }
  852. void SoftCPU::IRET(const X86::Instruction&) { TODO(); }
  853. void SoftCPU::JCXZ_imm8(const X86::Instruction&) { TODO(); }
  854. void SoftCPU::JMP_FAR_mem16(const X86::Instruction&) { TODO(); }
  855. void SoftCPU::JMP_FAR_mem32(const X86::Instruction&) { TODO(); }
  856. void SoftCPU::JMP_RM16(const X86::Instruction&) { TODO(); }
  857. void SoftCPU::JMP_RM32(const X86::Instruction& insn)
  858. {
  859. set_eip(insn.modrm().read32(*this, insn));
  860. }
  861. void SoftCPU::JMP_imm16(const X86::Instruction& insn)
  862. {
  863. set_eip(eip() + (i16)insn.imm16());
  864. }
  865. void SoftCPU::JMP_imm16_imm16(const X86::Instruction&) { TODO(); }
  866. void SoftCPU::JMP_imm16_imm32(const X86::Instruction&) { TODO(); }
  867. void SoftCPU::JMP_imm32(const X86::Instruction& insn)
  868. {
  869. set_eip(eip() + (i32)insn.imm32());
  870. }
  871. void SoftCPU::JMP_short_imm8(const X86::Instruction& insn)
  872. {
  873. set_eip(eip() + (i8)insn.imm8());
  874. }
  875. void SoftCPU::Jcc_NEAR_imm(const X86::Instruction& insn)
  876. {
  877. if (evaluate_condition(insn.cc()))
  878. set_eip(eip() + (i32)insn.imm32());
  879. }
  880. void SoftCPU::Jcc_imm8(const X86::Instruction& insn)
  881. {
  882. if (evaluate_condition(insn.cc()))
  883. set_eip(eip() + (i8)insn.imm8());
  884. }
  885. void SoftCPU::LAHF(const X86::Instruction&) { TODO(); }
  886. void SoftCPU::LAR_reg16_RM16(const X86::Instruction&) { TODO(); }
  887. void SoftCPU::LAR_reg32_RM32(const X86::Instruction&) { TODO(); }
  888. void SoftCPU::LDS_reg16_mem16(const X86::Instruction&) { TODO(); }
  889. void SoftCPU::LDS_reg32_mem32(const X86::Instruction&) { TODO(); }
  890. void SoftCPU::LEAVE16(const X86::Instruction&) { TODO(); }
  891. void SoftCPU::LEAVE32(const X86::Instruction&)
  892. {
  893. u32 new_ebp = read_memory32({ ss(), ebp() });
  894. set_esp(ebp() + 4);
  895. set_ebp(new_ebp);
  896. }
  897. void SoftCPU::LEA_reg16_mem16(const X86::Instruction& insn)
  898. {
  899. gpr16(insn.reg16()) = insn.modrm().resolve(*this, insn.segment_prefix()).offset();
  900. }
  901. void SoftCPU::LEA_reg32_mem32(const X86::Instruction& insn)
  902. {
  903. gpr32(insn.reg32()) = insn.modrm().resolve(*this, insn.segment_prefix()).offset();
  904. }
  905. void SoftCPU::LES_reg16_mem16(const X86::Instruction&) { TODO(); }
  906. void SoftCPU::LES_reg32_mem32(const X86::Instruction&) { TODO(); }
  907. void SoftCPU::LFS_reg16_mem16(const X86::Instruction&) { TODO(); }
  908. void SoftCPU::LFS_reg32_mem32(const X86::Instruction&) { TODO(); }
  909. void SoftCPU::LGDT(const X86::Instruction&) { TODO(); }
  910. void SoftCPU::LGS_reg16_mem16(const X86::Instruction&) { TODO(); }
  911. void SoftCPU::LGS_reg32_mem32(const X86::Instruction&) { TODO(); }
  912. void SoftCPU::LIDT(const X86::Instruction&) { TODO(); }
  913. void SoftCPU::LLDT_RM16(const X86::Instruction&) { TODO(); }
  914. void SoftCPU::LMSW_RM16(const X86::Instruction&) { TODO(); }
  915. void SoftCPU::LODSB(const X86::Instruction&) { TODO(); }
  916. void SoftCPU::LODSD(const X86::Instruction&) { TODO(); }
  917. void SoftCPU::LODSW(const X86::Instruction&) { TODO(); }
  918. void SoftCPU::LOOPNZ_imm8(const X86::Instruction&) { TODO(); }
  919. void SoftCPU::LOOPZ_imm8(const X86::Instruction&) { TODO(); }
  920. void SoftCPU::LOOP_imm8(const X86::Instruction&) { TODO(); }
  921. void SoftCPU::LSL_reg16_RM16(const X86::Instruction&) { TODO(); }
  922. void SoftCPU::LSL_reg32_RM32(const X86::Instruction&) { TODO(); }
  923. void SoftCPU::LSS_reg16_mem16(const X86::Instruction&) { TODO(); }
  924. void SoftCPU::LSS_reg32_mem32(const X86::Instruction&) { TODO(); }
  925. void SoftCPU::LTR_RM16(const X86::Instruction&) { TODO(); }
  926. void SoftCPU::MOVSB(const X86::Instruction& insn)
  927. {
  928. auto src_segment = segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS));
  929. if (insn.has_address_size_override_prefix()) {
  930. do_once_or_repeat<false>(insn, [&] {
  931. auto src = read_memory8({ src_segment, si() });
  932. write_memory8({ es(), di() }, src);
  933. set_di(di() + (df() ? -1 : 1));
  934. set_si(si() + (df() ? -1 : 1));
  935. });
  936. } else {
  937. do_once_or_repeat<false>(insn, [&] {
  938. auto src = read_memory8({ src_segment, esi() });
  939. write_memory8({ es(), edi() }, src);
  940. set_edi(edi() + (df() ? -1 : 1));
  941. set_esi(esi() + (df() ? -1 : 1));
  942. });
  943. }
  944. }
  945. void SoftCPU::MOVSD(const X86::Instruction& insn)
  946. {
  947. auto src_segment = segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS));
  948. if (insn.has_address_size_override_prefix()) {
  949. do_once_or_repeat<false>(insn, [&] {
  950. auto src = read_memory32({ src_segment, si() });
  951. write_memory32({ es(), di() }, src);
  952. set_di(di() + (df() ? -4 : 4));
  953. set_si(si() + (df() ? -4 : 4));
  954. });
  955. } else {
  956. do_once_or_repeat<false>(insn, [&] {
  957. auto src = read_memory32({ src_segment, esi() });
  958. write_memory32({ es(), edi() }, src);
  959. set_edi(edi() + (df() ? -4 : 4));
  960. set_esi(esi() + (df() ? -4 : 4));
  961. });
  962. }
  963. }
  964. void SoftCPU::MOVSW(const X86::Instruction& insn)
  965. {
  966. auto src_segment = segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS));
  967. if (insn.has_address_size_override_prefix()) {
  968. do_once_or_repeat<false>(insn, [&] {
  969. auto src = read_memory16({ src_segment, si() });
  970. write_memory16({ es(), di() }, src);
  971. set_di(di() + (df() ? -2 : 2));
  972. set_si(si() + (df() ? -2 : 2));
  973. });
  974. } else {
  975. do_once_or_repeat<false>(insn, [&] {
  976. auto src = read_memory16({ src_segment, esi() });
  977. write_memory16({ es(), edi() }, src);
  978. set_edi(edi() + (df() ? -2 : 2));
  979. set_esi(esi() + (df() ? -2 : 2));
  980. });
  981. }
  982. }
  983. void SoftCPU::MOVSX_reg16_RM8(const X86::Instruction& insn)
  984. {
  985. gpr16(insn.reg16()) = sign_extended_to<u16>(insn.modrm().read8(*this, insn));
  986. }
  987. void SoftCPU::MOVSX_reg32_RM16(const X86::Instruction& insn)
  988. {
  989. gpr32(insn.reg32()) = sign_extended_to<u32>(insn.modrm().read16(*this, insn));
  990. }
  991. void SoftCPU::MOVSX_reg32_RM8(const X86::Instruction& insn)
  992. {
  993. gpr32(insn.reg32()) = sign_extended_to<u32>(insn.modrm().read8(*this, insn));
  994. }
  995. void SoftCPU::MOVZX_reg16_RM8(const X86::Instruction& insn)
  996. {
  997. gpr16(insn.reg16()) = insn.modrm().read8(*this, insn);
  998. }
  999. void SoftCPU::MOVZX_reg32_RM16(const X86::Instruction& insn)
  1000. {
  1001. gpr32(insn.reg32()) = insn.modrm().read16(*this, insn);
  1002. }
  1003. void SoftCPU::MOVZX_reg32_RM8(const X86::Instruction& insn)
  1004. {
  1005. gpr32(insn.reg32()) = insn.modrm().read8(*this, insn);
  1006. }
  1007. void SoftCPU::MOV_AL_moff8(const X86::Instruction& insn)
  1008. {
  1009. set_al(read_memory8({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }));
  1010. }
  1011. void SoftCPU::MOV_AX_moff16(const X86::Instruction& insn)
  1012. {
  1013. set_ax(read_memory16({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }));
  1014. }
  1015. void SoftCPU::MOV_CR_reg32(const X86::Instruction&) { TODO(); }
  1016. void SoftCPU::MOV_DR_reg32(const X86::Instruction&) { TODO(); }
  1017. void SoftCPU::MOV_EAX_moff32(const X86::Instruction& insn)
  1018. {
  1019. set_eax(read_memory32({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }));
  1020. }
  1021. void SoftCPU::MOV_RM16_imm16(const X86::Instruction& insn)
  1022. {
  1023. insn.modrm().write16(*this, insn, insn.imm16());
  1024. }
  1025. void SoftCPU::MOV_RM16_reg16(const X86::Instruction& insn)
  1026. {
  1027. insn.modrm().write16(*this, insn, gpr16(insn.reg16()));
  1028. }
  1029. void SoftCPU::MOV_RM16_seg(const X86::Instruction&) { TODO(); }
  1030. void SoftCPU::MOV_RM32_imm32(const X86::Instruction& insn)
  1031. {
  1032. insn.modrm().write32(*this, insn, insn.imm32());
  1033. }
  1034. void SoftCPU::MOV_RM32_reg32(const X86::Instruction& insn)
  1035. {
  1036. insn.modrm().write32(*this, insn, gpr32(insn.reg32()));
  1037. }
  1038. void SoftCPU::MOV_RM8_imm8(const X86::Instruction& insn)
  1039. {
  1040. insn.modrm().write8(*this, insn, insn.imm8());
  1041. }
  1042. void SoftCPU::MOV_RM8_reg8(const X86::Instruction& insn)
  1043. {
  1044. insn.modrm().write8(*this, insn, gpr8(insn.reg8()));
  1045. }
  1046. void SoftCPU::MOV_moff16_AX(const X86::Instruction& insn)
  1047. {
  1048. write_memory16({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }, ax());
  1049. }
  1050. void SoftCPU::MOV_moff32_EAX(const X86::Instruction& insn)
  1051. {
  1052. write_memory32({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }, eax());
  1053. }
  1054. void SoftCPU::MOV_moff8_AL(const X86::Instruction& insn)
  1055. {
  1056. write_memory8({ segment(insn.segment_prefix().value_or(X86::SegmentRegister::DS)), insn.imm_address() }, al());
  1057. }
  1058. void SoftCPU::MOV_reg16_RM16(const X86::Instruction& insn)
  1059. {
  1060. gpr16(insn.reg16()) = insn.modrm().read16(*this, insn);
  1061. }
  1062. void SoftCPU::MOV_reg16_imm16(const X86::Instruction& insn)
  1063. {
  1064. gpr16(insn.reg16()) = insn.imm16();
  1065. }
  1066. void SoftCPU::MOV_reg32_CR(const X86::Instruction&) { TODO(); }
  1067. void SoftCPU::MOV_reg32_DR(const X86::Instruction&) { TODO(); }
  1068. void SoftCPU::MOV_reg32_RM32(const X86::Instruction& insn)
  1069. {
  1070. gpr32(insn.reg32()) = insn.modrm().read32(*this, insn);
  1071. }
  1072. void SoftCPU::MOV_reg32_imm32(const X86::Instruction& insn)
  1073. {
  1074. gpr32(insn.reg32()) = insn.imm32();
  1075. }
  1076. void SoftCPU::MOV_reg8_RM8(const X86::Instruction& insn)
  1077. {
  1078. gpr8(insn.reg8()) = insn.modrm().read8(*this, insn);
  1079. }
  1080. void SoftCPU::MOV_reg8_imm8(const X86::Instruction& insn)
  1081. {
  1082. gpr8(insn.reg8()) = insn.imm8();
  1083. }
  1084. void SoftCPU::MOV_seg_RM16(const X86::Instruction&) { TODO(); }
  1085. void SoftCPU::MOV_seg_RM32(const X86::Instruction&) { TODO(); }
  1086. void SoftCPU::MUL_RM16(const X86::Instruction&) { TODO(); }
  1087. void SoftCPU::MUL_RM32(const X86::Instruction&) { TODO(); }
  1088. void SoftCPU::MUL_RM8(const X86::Instruction&) { TODO(); }
  1089. void SoftCPU::NEG_RM16(const X86::Instruction& insn)
  1090. {
  1091. insn.modrm().write16(*this, insn, op_sub<u16>(*this, 0, insn.modrm().read16(*this, insn)));
  1092. }
  1093. void SoftCPU::NEG_RM32(const X86::Instruction& insn)
  1094. {
  1095. insn.modrm().write32(*this, insn, op_sub<u32>(*this, 0, insn.modrm().read32(*this, insn)));
  1096. }
  1097. void SoftCPU::NEG_RM8(const X86::Instruction& insn)
  1098. {
  1099. insn.modrm().write8(*this, insn, op_sub<u8>(*this, 0, insn.modrm().read8(*this, insn)));
  1100. }
  1101. void SoftCPU::NOP(const X86::Instruction&)
  1102. {
  1103. }
  1104. void SoftCPU::NOT_RM16(const X86::Instruction& insn)
  1105. {
  1106. insn.modrm().write16(*this, insn, ~insn.modrm().read16(*this, insn));
  1107. }
  1108. void SoftCPU::NOT_RM32(const X86::Instruction& insn)
  1109. {
  1110. insn.modrm().write32(*this, insn, ~insn.modrm().read32(*this, insn));
  1111. }
  1112. void SoftCPU::NOT_RM8(const X86::Instruction& insn)
  1113. {
  1114. insn.modrm().write8(*this, insn, ~insn.modrm().read8(*this, insn));
  1115. }
  1116. void SoftCPU::OUTSB(const X86::Instruction&) { TODO(); }
  1117. void SoftCPU::OUTSD(const X86::Instruction&) { TODO(); }
  1118. void SoftCPU::OUTSW(const X86::Instruction&) { TODO(); }
  1119. void SoftCPU::OUT_DX_AL(const X86::Instruction&) { TODO(); }
  1120. void SoftCPU::OUT_DX_AX(const X86::Instruction&) { TODO(); }
  1121. void SoftCPU::OUT_DX_EAX(const X86::Instruction&) { TODO(); }
  1122. void SoftCPU::OUT_imm8_AL(const X86::Instruction&) { TODO(); }
  1123. void SoftCPU::OUT_imm8_AX(const X86::Instruction&) { TODO(); }
  1124. void SoftCPU::OUT_imm8_EAX(const X86::Instruction&) { TODO(); }
  1125. void SoftCPU::PADDB_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  1126. void SoftCPU::PADDW_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  1127. void SoftCPU::PADDD_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  1128. void SoftCPU::POPA(const X86::Instruction&) { TODO(); }
  1129. void SoftCPU::POPAD(const X86::Instruction&) { TODO(); }
  1130. void SoftCPU::POPF(const X86::Instruction&) { TODO(); }
  1131. void SoftCPU::POPFD(const X86::Instruction&) { TODO(); }
  1132. void SoftCPU::POP_DS(const X86::Instruction&) { TODO(); }
  1133. void SoftCPU::POP_ES(const X86::Instruction&) { TODO(); }
  1134. void SoftCPU::POP_FS(const X86::Instruction&) { TODO(); }
  1135. void SoftCPU::POP_GS(const X86::Instruction&) { TODO(); }
  1136. void SoftCPU::POP_RM16(const X86::Instruction&) { TODO(); }
  1137. void SoftCPU::POP_RM32(const X86::Instruction&) { TODO(); }
  1138. void SoftCPU::POP_SS(const X86::Instruction&) { TODO(); }
  1139. void SoftCPU::POP_reg16(const X86::Instruction&) { TODO(); }
  1140. void SoftCPU::POP_reg32(const X86::Instruction& insn)
  1141. {
  1142. gpr32(insn.reg32()) = pop32();
  1143. }
  1144. void SoftCPU::PUSHA(const X86::Instruction&) { TODO(); }
  1145. void SoftCPU::PUSHAD(const X86::Instruction&) { TODO(); }
  1146. void SoftCPU::PUSHF(const X86::Instruction&) { TODO(); }
  1147. void SoftCPU::PUSHFD(const X86::Instruction&)
  1148. {
  1149. push32(m_eflags & 0x00fcffff);
  1150. }
  1151. void SoftCPU::PUSH_CS(const X86::Instruction&) { TODO(); }
  1152. void SoftCPU::PUSH_DS(const X86::Instruction&) { TODO(); }
  1153. void SoftCPU::PUSH_ES(const X86::Instruction&) { TODO(); }
  1154. void SoftCPU::PUSH_FS(const X86::Instruction&) { TODO(); }
  1155. void SoftCPU::PUSH_GS(const X86::Instruction&) { TODO(); }
  1156. void SoftCPU::PUSH_RM16(const X86::Instruction&) { TODO(); }
  1157. void SoftCPU::PUSH_RM32(const X86::Instruction& insn)
  1158. {
  1159. push32(insn.modrm().read32(*this, insn));
  1160. }
  1161. void SoftCPU::PUSH_SP_8086_80186(const X86::Instruction&) { TODO(); }
  1162. void SoftCPU::PUSH_SS(const X86::Instruction&) { TODO(); }
  1163. void SoftCPU::PUSH_imm16(const X86::Instruction&) { TODO(); }
  1164. void SoftCPU::PUSH_imm32(const X86::Instruction& insn)
  1165. {
  1166. push32(insn.imm32());
  1167. }
  1168. void SoftCPU::PUSH_imm8(const X86::Instruction& insn)
  1169. {
  1170. ASSERT(!insn.has_operand_size_override_prefix());
  1171. push32(sign_extended_to<i32>(insn.imm8()));
  1172. }
  1173. void SoftCPU::PUSH_reg16(const X86::Instruction&) { TODO(); }
  1174. void SoftCPU::PUSH_reg32(const X86::Instruction& insn)
  1175. {
  1176. push32(gpr32(insn.reg32()));
  1177. }
  1178. void SoftCPU::RCL_RM16_1(const X86::Instruction&) { TODO(); }
  1179. void SoftCPU::RCL_RM16_CL(const X86::Instruction&) { TODO(); }
  1180. void SoftCPU::RCL_RM16_imm8(const X86::Instruction&) { TODO(); }
  1181. void SoftCPU::RCL_RM32_1(const X86::Instruction&) { TODO(); }
  1182. void SoftCPU::RCL_RM32_CL(const X86::Instruction&) { TODO(); }
  1183. void SoftCPU::RCL_RM32_imm8(const X86::Instruction&) { TODO(); }
  1184. void SoftCPU::RCL_RM8_1(const X86::Instruction&) { TODO(); }
  1185. void SoftCPU::RCL_RM8_CL(const X86::Instruction&) { TODO(); }
  1186. void SoftCPU::RCL_RM8_imm8(const X86::Instruction&) { TODO(); }
  1187. void SoftCPU::RCR_RM16_1(const X86::Instruction&) { TODO(); }
  1188. void SoftCPU::RCR_RM16_CL(const X86::Instruction&) { TODO(); }
  1189. void SoftCPU::RCR_RM16_imm8(const X86::Instruction&) { TODO(); }
  1190. void SoftCPU::RCR_RM32_1(const X86::Instruction&) { TODO(); }
  1191. void SoftCPU::RCR_RM32_CL(const X86::Instruction&) { TODO(); }
  1192. void SoftCPU::RCR_RM32_imm8(const X86::Instruction&) { TODO(); }
  1193. void SoftCPU::RCR_RM8_1(const X86::Instruction&) { TODO(); }
  1194. void SoftCPU::RCR_RM8_CL(const X86::Instruction&) { TODO(); }
  1195. void SoftCPU::RCR_RM8_imm8(const X86::Instruction&) { TODO(); }
  1196. void SoftCPU::RDTSC(const X86::Instruction&) { TODO(); }
  1197. void SoftCPU::RET(const X86::Instruction& insn)
  1198. {
  1199. ASSERT(!insn.has_operand_size_override_prefix());
  1200. set_eip(pop32());
  1201. }
  1202. void SoftCPU::RETF(const X86::Instruction&) { TODO(); }
  1203. void SoftCPU::RETF_imm16(const X86::Instruction&) { TODO(); }
  1204. void SoftCPU::RET_imm16(const X86::Instruction& insn)
  1205. {
  1206. ASSERT(!insn.has_operand_size_override_prefix());
  1207. set_eip(pop32());
  1208. set_esp(esp() + insn.imm16());
  1209. }
  1210. void SoftCPU::ROL_RM16_1(const X86::Instruction&) { TODO(); }
  1211. void SoftCPU::ROL_RM16_CL(const X86::Instruction&) { TODO(); }
  1212. void SoftCPU::ROL_RM16_imm8(const X86::Instruction&) { TODO(); }
  1213. void SoftCPU::ROL_RM32_1(const X86::Instruction&) { TODO(); }
  1214. void SoftCPU::ROL_RM32_CL(const X86::Instruction&) { TODO(); }
  1215. void SoftCPU::ROL_RM32_imm8(const X86::Instruction&) { TODO(); }
  1216. void SoftCPU::ROL_RM8_1(const X86::Instruction&) { TODO(); }
  1217. void SoftCPU::ROL_RM8_CL(const X86::Instruction&) { TODO(); }
  1218. void SoftCPU::ROL_RM8_imm8(const X86::Instruction&) { TODO(); }
  1219. void SoftCPU::ROR_RM16_1(const X86::Instruction&) { TODO(); }
  1220. void SoftCPU::ROR_RM16_CL(const X86::Instruction&) { TODO(); }
  1221. void SoftCPU::ROR_RM16_imm8(const X86::Instruction&) { TODO(); }
  1222. void SoftCPU::ROR_RM32_1(const X86::Instruction&) { TODO(); }
  1223. void SoftCPU::ROR_RM32_CL(const X86::Instruction&) { TODO(); }
  1224. void SoftCPU::ROR_RM32_imm8(const X86::Instruction&) { TODO(); }
  1225. void SoftCPU::ROR_RM8_1(const X86::Instruction&) { TODO(); }
  1226. void SoftCPU::ROR_RM8_CL(const X86::Instruction&) { TODO(); }
  1227. void SoftCPU::ROR_RM8_imm8(const X86::Instruction&) { TODO(); }
  1228. void SoftCPU::SAHF(const X86::Instruction&) { TODO(); }
  1229. void SoftCPU::SALC(const X86::Instruction&) { TODO(); }
  1230. template<typename T>
  1231. static T op_sar(SoftCPU& cpu, T data, u8 steps)
  1232. {
  1233. if (steps == 0)
  1234. return data;
  1235. u32 result = 0;
  1236. u32 new_flags = 0;
  1237. if constexpr (sizeof(T) == 4) {
  1238. asm volatile("sarl %%cl, %%eax\n"
  1239. : "=a"(result)
  1240. : "a"(data), "c"(steps));
  1241. } else if constexpr (sizeof(T) == 2) {
  1242. asm volatile("sarw %%cl, %%ax\n"
  1243. : "=a"(result)
  1244. : "a"(data), "c"(steps));
  1245. } else if constexpr (sizeof(T) == 1) {
  1246. asm volatile("sarb %%cl, %%al\n"
  1247. : "=a"(result)
  1248. : "a"(data), "c"(steps));
  1249. }
  1250. asm volatile(
  1251. "pushf\n"
  1252. "pop %%ebx"
  1253. : "=b"(new_flags));
  1254. cpu.set_flags_oszapc(new_flags);
  1255. return result;
  1256. }
  1257. void SoftCPU::SAR_RM16_1(const X86::Instruction& insn)
  1258. {
  1259. auto data = insn.modrm().read16(*this, insn);
  1260. insn.modrm().write16(*this, insn, op_sar(*this, data, 1));
  1261. }
  1262. void SoftCPU::SAR_RM16_CL(const X86::Instruction& insn)
  1263. {
  1264. auto data = insn.modrm().read16(*this, insn);
  1265. insn.modrm().write16(*this, insn, op_sar(*this, data, cl()));
  1266. }
  1267. void SoftCPU::SAR_RM16_imm8(const X86::Instruction& insn)
  1268. {
  1269. auto data = insn.modrm().read16(*this, insn);
  1270. insn.modrm().write16(*this, insn, op_sar(*this, data, insn.imm8()));
  1271. }
  1272. void SoftCPU::SAR_RM32_1(const X86::Instruction& insn)
  1273. {
  1274. auto data = insn.modrm().read32(*this, insn);
  1275. insn.modrm().write32(*this, insn, op_sar(*this, data, 1));
  1276. }
  1277. void SoftCPU::SAR_RM32_CL(const X86::Instruction& insn)
  1278. {
  1279. auto data = insn.modrm().read32(*this, insn);
  1280. insn.modrm().write32(*this, insn, op_sar(*this, data, cl()));
  1281. }
  1282. void SoftCPU::SAR_RM32_imm8(const X86::Instruction& insn)
  1283. {
  1284. auto data = insn.modrm().read32(*this, insn);
  1285. insn.modrm().write32(*this, insn, op_sar(*this, data, insn.imm8()));
  1286. }
  1287. void SoftCPU::SAR_RM8_1(const X86::Instruction& insn)
  1288. {
  1289. auto data = insn.modrm().read8(*this, insn);
  1290. insn.modrm().write8(*this, insn, op_sar(*this, data, 1));
  1291. }
  1292. void SoftCPU::SAR_RM8_CL(const X86::Instruction& insn)
  1293. {
  1294. auto data = insn.modrm().read8(*this, insn);
  1295. insn.modrm().write8(*this, insn, op_sar(*this, data, cl()));
  1296. }
  1297. void SoftCPU::SAR_RM8_imm8(const X86::Instruction& insn)
  1298. {
  1299. auto data = insn.modrm().read8(*this, insn);
  1300. insn.modrm().write8(*this, insn, op_sar(*this, data, insn.imm8()));
  1301. }
  1302. void SoftCPU::SCASB(const X86::Instruction&) { TODO(); }
  1303. void SoftCPU::SCASD(const X86::Instruction&) { TODO(); }
  1304. void SoftCPU::SCASW(const X86::Instruction&) { TODO(); }
  1305. void SoftCPU::SETcc_RM8(const X86::Instruction& insn)
  1306. {
  1307. insn.modrm().write8(*this, insn, evaluate_condition(insn.cc()));
  1308. }
  1309. void SoftCPU::SGDT(const X86::Instruction&) { TODO(); }
  1310. void SoftCPU::SHLD_RM16_reg16_CL(const X86::Instruction&) { TODO(); }
  1311. void SoftCPU::SHLD_RM16_reg16_imm8(const X86::Instruction&) { TODO(); }
  1312. void SoftCPU::SHLD_RM32_reg32_CL(const X86::Instruction&) { TODO(); }
  1313. void SoftCPU::SHLD_RM32_reg32_imm8(const X86::Instruction&) { TODO(); }
  1314. void SoftCPU::SHL_RM16_1(const X86::Instruction& insn)
  1315. {
  1316. auto data = insn.modrm().read16(*this, insn);
  1317. insn.modrm().write16(*this, insn, op_shl(*this, data, 1));
  1318. }
  1319. void SoftCPU::SHL_RM16_CL(const X86::Instruction& insn)
  1320. {
  1321. auto data = insn.modrm().read16(*this, insn);
  1322. insn.modrm().write16(*this, insn, op_shl(*this, data, cl()));
  1323. }
  1324. void SoftCPU::SHL_RM16_imm8(const X86::Instruction& insn)
  1325. {
  1326. auto data = insn.modrm().read16(*this, insn);
  1327. insn.modrm().write16(*this, insn, op_shl(*this, data, insn.imm8()));
  1328. }
  1329. void SoftCPU::SHL_RM32_1(const X86::Instruction& insn)
  1330. {
  1331. auto data = insn.modrm().read32(*this, insn);
  1332. insn.modrm().write32(*this, insn, op_shl(*this, data, 1));
  1333. }
  1334. void SoftCPU::SHL_RM32_CL(const X86::Instruction& insn)
  1335. {
  1336. auto data = insn.modrm().read32(*this, insn);
  1337. insn.modrm().write32(*this, insn, op_shl(*this, data, cl()));
  1338. }
  1339. void SoftCPU::SHL_RM32_imm8(const X86::Instruction& insn)
  1340. {
  1341. auto data = insn.modrm().read32(*this, insn);
  1342. insn.modrm().write32(*this, insn, op_shl(*this, data, insn.imm8()));
  1343. }
  1344. void SoftCPU::SHL_RM8_1(const X86::Instruction& insn)
  1345. {
  1346. auto data = insn.modrm().read8(*this, insn);
  1347. insn.modrm().write8(*this, insn, op_shl(*this, data, 1));
  1348. }
  1349. void SoftCPU::SHL_RM8_CL(const X86::Instruction& insn)
  1350. {
  1351. auto data = insn.modrm().read8(*this, insn);
  1352. insn.modrm().write8(*this, insn, op_shl(*this, data, cl()));
  1353. }
  1354. void SoftCPU::SHL_RM8_imm8(const X86::Instruction& insn)
  1355. {
  1356. auto data = insn.modrm().read8(*this, insn);
  1357. insn.modrm().write8(*this, insn, op_shl(*this, data, insn.imm8()));
  1358. }
  1359. void SoftCPU::SHRD_RM16_reg16_CL(const X86::Instruction&) { TODO(); }
  1360. void SoftCPU::SHRD_RM16_reg16_imm8(const X86::Instruction&) { TODO(); }
  1361. void SoftCPU::SHRD_RM32_reg32_CL(const X86::Instruction&) { TODO(); }
  1362. void SoftCPU::SHRD_RM32_reg32_imm8(const X86::Instruction&) { TODO(); }
  1363. void SoftCPU::SHR_RM16_1(const X86::Instruction& insn)
  1364. {
  1365. auto data = insn.modrm().read16(*this, insn);
  1366. insn.modrm().write16(*this, insn, op_shr(*this, data, 1));
  1367. }
  1368. void SoftCPU::SHR_RM16_CL(const X86::Instruction& insn)
  1369. {
  1370. auto data = insn.modrm().read16(*this, insn);
  1371. insn.modrm().write16(*this, insn, op_shr(*this, data, cl()));
  1372. }
  1373. void SoftCPU::SHR_RM16_imm8(const X86::Instruction& insn)
  1374. {
  1375. auto data = insn.modrm().read16(*this, insn);
  1376. insn.modrm().write16(*this, insn, op_shr(*this, data, insn.imm8()));
  1377. }
  1378. void SoftCPU::SHR_RM32_1(const X86::Instruction& insn)
  1379. {
  1380. auto data = insn.modrm().read32(*this, insn);
  1381. insn.modrm().write32(*this, insn, op_shr(*this, data, 1));
  1382. }
  1383. void SoftCPU::SHR_RM32_CL(const X86::Instruction& insn)
  1384. {
  1385. auto data = insn.modrm().read32(*this, insn);
  1386. insn.modrm().write32(*this, insn, op_shr(*this, data, cl()));
  1387. }
  1388. void SoftCPU::SHR_RM32_imm8(const X86::Instruction& insn)
  1389. {
  1390. auto data = insn.modrm().read32(*this, insn);
  1391. insn.modrm().write32(*this, insn, op_shr(*this, data, insn.imm8()));
  1392. }
  1393. void SoftCPU::SHR_RM8_1(const X86::Instruction& insn)
  1394. {
  1395. auto data = insn.modrm().read8(*this, insn);
  1396. insn.modrm().write8(*this, insn, op_shr(*this, data, 1));
  1397. }
  1398. void SoftCPU::SHR_RM8_CL(const X86::Instruction& insn)
  1399. {
  1400. auto data = insn.modrm().read8(*this, insn);
  1401. insn.modrm().write8(*this, insn, op_shr(*this, data, cl()));
  1402. }
  1403. void SoftCPU::SHR_RM8_imm8(const X86::Instruction& insn)
  1404. {
  1405. auto data = insn.modrm().read8(*this, insn);
  1406. insn.modrm().write8(*this, insn, op_shr(*this, data, insn.imm8()));
  1407. }
  1408. void SoftCPU::SIDT(const X86::Instruction&) { TODO(); }
  1409. void SoftCPU::SLDT_RM16(const X86::Instruction&) { TODO(); }
  1410. void SoftCPU::SMSW_RM16(const X86::Instruction&) { TODO(); }
  1411. void SoftCPU::STC(const X86::Instruction&)
  1412. {
  1413. set_cf(true);
  1414. }
  1415. void SoftCPU::STD(const X86::Instruction&)
  1416. {
  1417. set_df(true);
  1418. }
  1419. void SoftCPU::STI(const X86::Instruction&) { TODO(); }
  1420. void SoftCPU::STOSB(const X86::Instruction& insn)
  1421. {
  1422. if (insn.has_address_size_override_prefix()) {
  1423. do_once_or_repeat<false>(insn, [&] {
  1424. write_memory8({ es(), di() }, al());
  1425. set_di(di() + (df() ? -1 : 1));
  1426. });
  1427. } else {
  1428. do_once_or_repeat<false>(insn, [&] {
  1429. write_memory8({ es(), edi() }, al());
  1430. set_edi(edi() + (df() ? -1 : 1));
  1431. });
  1432. }
  1433. }
  1434. void SoftCPU::STOSD(const X86::Instruction& insn)
  1435. {
  1436. if (insn.has_address_size_override_prefix()) {
  1437. do_once_or_repeat<false>(insn, [&] {
  1438. write_memory32({ es(), di() }, eax());
  1439. set_di(di() + (df() ? -4 : 4));
  1440. });
  1441. } else {
  1442. do_once_or_repeat<false>(insn, [&] {
  1443. write_memory32({ es(), edi() }, eax());
  1444. set_edi(edi() + (df() ? -4 : 4));
  1445. });
  1446. }
  1447. }
  1448. void SoftCPU::STOSW(const X86::Instruction& insn)
  1449. {
  1450. if (insn.has_address_size_override_prefix()) {
  1451. do_once_or_repeat<false>(insn, [&] {
  1452. write_memory16({ es(), di() }, ax());
  1453. set_di(di() + (df() ? -2 : 2));
  1454. });
  1455. } else {
  1456. do_once_or_repeat<false>(insn, [&] {
  1457. write_memory16({ es(), edi() }, ax());
  1458. set_edi(edi() + (df() ? -2 : 2));
  1459. });
  1460. }
  1461. }
  1462. void SoftCPU::STR_RM16(const X86::Instruction&) { TODO(); }
  1463. void SoftCPU::UD0(const X86::Instruction&) { TODO(); }
  1464. void SoftCPU::UD1(const X86::Instruction&) { TODO(); }
  1465. void SoftCPU::UD2(const X86::Instruction&) { TODO(); }
  1466. void SoftCPU::VERR_RM16(const X86::Instruction&) { TODO(); }
  1467. void SoftCPU::VERW_RM16(const X86::Instruction&) { TODO(); }
  1468. void SoftCPU::WAIT(const X86::Instruction&) { TODO(); }
  1469. void SoftCPU::WBINVD(const X86::Instruction&) { TODO(); }
  1470. void SoftCPU::XADD_RM16_reg16(const X86::Instruction& insn)
  1471. {
  1472. auto dest = insn.modrm().read16(*this, insn);
  1473. auto src = gpr16(insn.reg16());
  1474. auto result = op_add(*this, dest, src);
  1475. gpr16(insn.reg16()) = dest;
  1476. insn.modrm().write16(*this, insn, result);
  1477. }
  1478. void SoftCPU::XADD_RM32_reg32(const X86::Instruction& insn)
  1479. {
  1480. auto dest = insn.modrm().read32(*this, insn);
  1481. auto src = gpr32(insn.reg32());
  1482. auto result = op_add(*this, dest, src);
  1483. gpr32(insn.reg32()) = dest;
  1484. insn.modrm().write32(*this, insn, result);
  1485. }
  1486. void SoftCPU::XADD_RM8_reg8(const X86::Instruction& insn)
  1487. {
  1488. auto dest = insn.modrm().read8(*this, insn);
  1489. auto src = gpr8(insn.reg8());
  1490. auto result = op_add(*this, dest, src);
  1491. gpr8(insn.reg8()) = dest;
  1492. insn.modrm().write8(*this, insn, result);
  1493. }
  1494. void SoftCPU::XCHG_AX_reg16(const X86::Instruction& insn)
  1495. {
  1496. auto temp = gpr16(insn.reg16());
  1497. gpr16(insn.reg16()) = eax();
  1498. set_eax(temp);
  1499. }
  1500. void SoftCPU::XCHG_EAX_reg32(const X86::Instruction& insn)
  1501. {
  1502. auto temp = gpr32(insn.reg32());
  1503. gpr32(insn.reg32()) = eax();
  1504. set_eax(temp);
  1505. }
  1506. void SoftCPU::XCHG_reg16_RM16(const X86::Instruction& insn)
  1507. {
  1508. auto temp = insn.modrm().read16(*this, insn);
  1509. insn.modrm().write16(*this, insn, gpr16(insn.reg16()));
  1510. gpr16(insn.reg16()) = temp;
  1511. }
  1512. void SoftCPU::XCHG_reg32_RM32(const X86::Instruction& insn)
  1513. {
  1514. auto temp = insn.modrm().read32(*this, insn);
  1515. insn.modrm().write32(*this, insn, gpr32(insn.reg32()));
  1516. gpr32(insn.reg32()) = temp;
  1517. }
  1518. void SoftCPU::XCHG_reg8_RM8(const X86::Instruction& insn)
  1519. {
  1520. auto temp = insn.modrm().read8(*this, insn);
  1521. insn.modrm().write8(*this, insn, gpr8(insn.reg8()));
  1522. gpr8(insn.reg8()) = temp;
  1523. }
  1524. void SoftCPU::XLAT(const X86::Instruction&) { TODO(); }
  1525. #define DEFINE_GENERIC_INSN_HANDLERS_PARTIAL(mnemonic, op, update_dest) \
  1526. void SoftCPU::mnemonic##_AL_imm8(const X86::Instruction& insn) { generic_AL_imm8<update_dest>(op<u8>, insn); } \
  1527. void SoftCPU::mnemonic##_AX_imm16(const X86::Instruction& insn) { generic_AX_imm16<update_dest>(op<u16>, insn); } \
  1528. void SoftCPU::mnemonic##_EAX_imm32(const X86::Instruction& insn) { generic_EAX_imm32<update_dest>(op<u32>, insn); } \
  1529. void SoftCPU::mnemonic##_RM16_imm16(const X86::Instruction& insn) { generic_RM16_imm16<update_dest>(op<u16>, insn); } \
  1530. void SoftCPU::mnemonic##_RM16_reg16(const X86::Instruction& insn) { generic_RM16_reg16<update_dest>(op<u16>, insn); } \
  1531. void SoftCPU::mnemonic##_RM32_imm32(const X86::Instruction& insn) { generic_RM32_imm32<update_dest>(op<u32>, insn); } \
  1532. void SoftCPU::mnemonic##_RM32_reg32(const X86::Instruction& insn) { generic_RM32_reg32<update_dest>(op<u32>, insn); } \
  1533. void SoftCPU::mnemonic##_RM8_imm8(const X86::Instruction& insn) { generic_RM8_imm8<update_dest>(op<u8>, insn); } \
  1534. void SoftCPU::mnemonic##_RM8_reg8(const X86::Instruction& insn) { generic_RM8_reg8<update_dest>(op<u8>, insn); }
  1535. #define DEFINE_GENERIC_INSN_HANDLERS(mnemonic, op, update_dest) \
  1536. DEFINE_GENERIC_INSN_HANDLERS_PARTIAL(mnemonic, op, update_dest) \
  1537. void SoftCPU::mnemonic##_RM16_imm8(const X86::Instruction& insn) { generic_RM16_imm8<update_dest>(op<u16>, insn); } \
  1538. void SoftCPU::mnemonic##_RM32_imm8(const X86::Instruction& insn) { generic_RM32_imm8<update_dest>(op<u32>, insn); } \
  1539. void SoftCPU::mnemonic##_reg16_RM16(const X86::Instruction& insn) { generic_reg16_RM16<update_dest>(op<u16>, insn); } \
  1540. void SoftCPU::mnemonic##_reg32_RM32(const X86::Instruction& insn) { generic_reg32_RM32<update_dest>(op<u32>, insn); } \
  1541. void SoftCPU::mnemonic##_reg8_RM8(const X86::Instruction& insn) { generic_reg8_RM8<update_dest>(op<u8>, insn); }
  1542. DEFINE_GENERIC_INSN_HANDLERS(XOR, op_xor, true)
  1543. DEFINE_GENERIC_INSN_HANDLERS(OR, op_or, true)
  1544. DEFINE_GENERIC_INSN_HANDLERS(ADD, op_add, true)
  1545. DEFINE_GENERIC_INSN_HANDLERS(ADC, op_adc, true)
  1546. DEFINE_GENERIC_INSN_HANDLERS(SUB, op_sub, true)
  1547. DEFINE_GENERIC_INSN_HANDLERS(SBB, op_sbb, true)
  1548. DEFINE_GENERIC_INSN_HANDLERS(AND, op_and, true)
  1549. DEFINE_GENERIC_INSN_HANDLERS(CMP, op_sub, false)
  1550. DEFINE_GENERIC_INSN_HANDLERS_PARTIAL(TEST, op_and, false)
  1551. void SoftCPU::MOVQ_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  1552. void SoftCPU::EMMS(const X86::Instruction&) { TODO(); }
  1553. void SoftCPU::MOVQ_mm1_m64_mm2(const X86::Instruction&) { TODO(); }
  1554. void SoftCPU::wrap_0xC0(const X86::Instruction&) { TODO(); }
  1555. void SoftCPU::wrap_0xC1_16(const X86::Instruction&) { TODO(); }
  1556. void SoftCPU::wrap_0xC1_32(const X86::Instruction&) { TODO(); }
  1557. void SoftCPU::wrap_0xD0(const X86::Instruction&) { TODO(); }
  1558. void SoftCPU::wrap_0xD1_16(const X86::Instruction&) { TODO(); }
  1559. void SoftCPU::wrap_0xD1_32(const X86::Instruction&) { TODO(); }
  1560. void SoftCPU::wrap_0xD2(const X86::Instruction&) { TODO(); }
  1561. void SoftCPU::wrap_0xD3_16(const X86::Instruction&) { TODO(); }
  1562. void SoftCPU::wrap_0xD3_32(const X86::Instruction&) { TODO(); }
  1563. }