SoftCPU.cpp 58 KB

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