SoftCPU.cpp 55 KB

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