SoftCPU.cpp 57 KB

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