SoftCPU.cpp 54 KB

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