SoftCPU.cpp 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719
  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&) { TODO(); }
  1088. void SoftCPU::PUSH_CS(const X86::Instruction&) { TODO(); }
  1089. void SoftCPU::PUSH_DS(const X86::Instruction&) { TODO(); }
  1090. void SoftCPU::PUSH_ES(const X86::Instruction&) { TODO(); }
  1091. void SoftCPU::PUSH_FS(const X86::Instruction&) { TODO(); }
  1092. void SoftCPU::PUSH_GS(const X86::Instruction&) { TODO(); }
  1093. void SoftCPU::PUSH_RM16(const X86::Instruction&) { TODO(); }
  1094. void SoftCPU::PUSH_RM32(const X86::Instruction& insn)
  1095. {
  1096. push32(insn.modrm().read32(*this, insn));
  1097. }
  1098. void SoftCPU::PUSH_SP_8086_80186(const X86::Instruction&) { TODO(); }
  1099. void SoftCPU::PUSH_SS(const X86::Instruction&) { TODO(); }
  1100. void SoftCPU::PUSH_imm16(const X86::Instruction&) { TODO(); }
  1101. void SoftCPU::PUSH_imm32(const X86::Instruction& insn)
  1102. {
  1103. push32(insn.imm32());
  1104. }
  1105. void SoftCPU::PUSH_imm8(const X86::Instruction& insn)
  1106. {
  1107. ASSERT(!insn.has_operand_size_override_prefix());
  1108. push32(sign_extended_to<i32>(insn.imm8()));
  1109. }
  1110. void SoftCPU::PUSH_reg16(const X86::Instruction&) { TODO(); }
  1111. void SoftCPU::PUSH_reg32(const X86::Instruction& insn)
  1112. {
  1113. push32(gpr32(insn.reg32()));
  1114. }
  1115. void SoftCPU::RCL_RM16_1(const X86::Instruction&) { TODO(); }
  1116. void SoftCPU::RCL_RM16_CL(const X86::Instruction&) { TODO(); }
  1117. void SoftCPU::RCL_RM16_imm8(const X86::Instruction&) { TODO(); }
  1118. void SoftCPU::RCL_RM32_1(const X86::Instruction&) { TODO(); }
  1119. void SoftCPU::RCL_RM32_CL(const X86::Instruction&) { TODO(); }
  1120. void SoftCPU::RCL_RM32_imm8(const X86::Instruction&) { TODO(); }
  1121. void SoftCPU::RCL_RM8_1(const X86::Instruction&) { TODO(); }
  1122. void SoftCPU::RCL_RM8_CL(const X86::Instruction&) { TODO(); }
  1123. void SoftCPU::RCL_RM8_imm8(const X86::Instruction&) { TODO(); }
  1124. void SoftCPU::RCR_RM16_1(const X86::Instruction&) { TODO(); }
  1125. void SoftCPU::RCR_RM16_CL(const X86::Instruction&) { TODO(); }
  1126. void SoftCPU::RCR_RM16_imm8(const X86::Instruction&) { TODO(); }
  1127. void SoftCPU::RCR_RM32_1(const X86::Instruction&) { TODO(); }
  1128. void SoftCPU::RCR_RM32_CL(const X86::Instruction&) { TODO(); }
  1129. void SoftCPU::RCR_RM32_imm8(const X86::Instruction&) { TODO(); }
  1130. void SoftCPU::RCR_RM8_1(const X86::Instruction&) { TODO(); }
  1131. void SoftCPU::RCR_RM8_CL(const X86::Instruction&) { TODO(); }
  1132. void SoftCPU::RCR_RM8_imm8(const X86::Instruction&) { TODO(); }
  1133. void SoftCPU::RDTSC(const X86::Instruction&) { TODO(); }
  1134. void SoftCPU::RET(const X86::Instruction& insn)
  1135. {
  1136. ASSERT(!insn.has_operand_size_override_prefix());
  1137. set_eip(pop32());
  1138. }
  1139. void SoftCPU::RETF(const X86::Instruction&) { TODO(); }
  1140. void SoftCPU::RETF_imm16(const X86::Instruction&) { TODO(); }
  1141. void SoftCPU::RET_imm16(const X86::Instruction& insn)
  1142. {
  1143. ASSERT(!insn.has_operand_size_override_prefix());
  1144. set_eip(pop32());
  1145. set_esp(esp() + insn.imm16());
  1146. }
  1147. void SoftCPU::ROL_RM16_1(const X86::Instruction&) { TODO(); }
  1148. void SoftCPU::ROL_RM16_CL(const X86::Instruction&) { TODO(); }
  1149. void SoftCPU::ROL_RM16_imm8(const X86::Instruction&) { TODO(); }
  1150. void SoftCPU::ROL_RM32_1(const X86::Instruction&) { TODO(); }
  1151. void SoftCPU::ROL_RM32_CL(const X86::Instruction&) { TODO(); }
  1152. void SoftCPU::ROL_RM32_imm8(const X86::Instruction&) { TODO(); }
  1153. void SoftCPU::ROL_RM8_1(const X86::Instruction&) { TODO(); }
  1154. void SoftCPU::ROL_RM8_CL(const X86::Instruction&) { TODO(); }
  1155. void SoftCPU::ROL_RM8_imm8(const X86::Instruction&) { TODO(); }
  1156. void SoftCPU::ROR_RM16_1(const X86::Instruction&) { TODO(); }
  1157. void SoftCPU::ROR_RM16_CL(const X86::Instruction&) { TODO(); }
  1158. void SoftCPU::ROR_RM16_imm8(const X86::Instruction&) { TODO(); }
  1159. void SoftCPU::ROR_RM32_1(const X86::Instruction&) { TODO(); }
  1160. void SoftCPU::ROR_RM32_CL(const X86::Instruction&) { TODO(); }
  1161. void SoftCPU::ROR_RM32_imm8(const X86::Instruction&) { TODO(); }
  1162. void SoftCPU::ROR_RM8_1(const X86::Instruction&) { TODO(); }
  1163. void SoftCPU::ROR_RM8_CL(const X86::Instruction&) { TODO(); }
  1164. void SoftCPU::ROR_RM8_imm8(const X86::Instruction&) { TODO(); }
  1165. void SoftCPU::SAHF(const X86::Instruction&) { TODO(); }
  1166. void SoftCPU::SALC(const X86::Instruction&) { TODO(); }
  1167. template<typename T>
  1168. static T op_sar(SoftCPU& cpu, T data, u8 steps)
  1169. {
  1170. if (steps == 0)
  1171. return data;
  1172. u32 result = 0;
  1173. u32 new_flags = 0;
  1174. if constexpr (sizeof(T) == 4)
  1175. asm volatile("sarl %%cl, %%eax\n" ::"a"(data), "c"(steps));
  1176. else if constexpr (sizeof(T) == 2)
  1177. asm volatile("sarw %%cl, %%ax\n" ::"a"(data), "c"(steps));
  1178. else if constexpr (sizeof(T) == 1)
  1179. asm volatile("sarb %%cl, %%al\n" ::"a"(data), "c"(steps));
  1180. asm volatile(
  1181. "mov %%eax, %%ebx\n"
  1182. : "=b"(result));
  1183. asm volatile(
  1184. "pushf\n"
  1185. "pop %%eax"
  1186. : "=a"(new_flags));
  1187. cpu.set_flags_oszapc(new_flags);
  1188. return result;
  1189. }
  1190. void SoftCPU::SAR_RM16_1(const X86::Instruction& insn)
  1191. {
  1192. auto data = insn.modrm().read16(*this, insn);
  1193. insn.modrm().write16(*this, insn, op_sar(*this, data, 1));
  1194. }
  1195. void SoftCPU::SAR_RM16_CL(const X86::Instruction& insn)
  1196. {
  1197. auto data = insn.modrm().read16(*this, insn);
  1198. insn.modrm().write16(*this, insn, op_sar(*this, data, cl()));
  1199. }
  1200. void SoftCPU::SAR_RM16_imm8(const X86::Instruction& insn)
  1201. {
  1202. auto data = insn.modrm().read16(*this, insn);
  1203. insn.modrm().write16(*this, insn, op_sar(*this, data, insn.imm8()));
  1204. }
  1205. void SoftCPU::SAR_RM32_1(const X86::Instruction& insn)
  1206. {
  1207. auto data = insn.modrm().read32(*this, insn);
  1208. insn.modrm().write32(*this, insn, op_sar(*this, data, 1));
  1209. }
  1210. void SoftCPU::SAR_RM32_CL(const X86::Instruction& insn)
  1211. {
  1212. auto data = insn.modrm().read32(*this, insn);
  1213. insn.modrm().write32(*this, insn, op_sar(*this, data, cl()));
  1214. }
  1215. void SoftCPU::SAR_RM32_imm8(const X86::Instruction& insn)
  1216. {
  1217. auto data = insn.modrm().read32(*this, insn);
  1218. insn.modrm().write32(*this, insn, op_sar(*this, data, insn.imm8()));
  1219. }
  1220. void SoftCPU::SAR_RM8_1(const X86::Instruction& insn)
  1221. {
  1222. auto data = insn.modrm().read8(*this, insn);
  1223. insn.modrm().write8(*this, insn, op_sar(*this, data, 1));
  1224. }
  1225. void SoftCPU::SAR_RM8_CL(const X86::Instruction& insn)
  1226. {
  1227. auto data = insn.modrm().read8(*this, insn);
  1228. insn.modrm().write8(*this, insn, op_sar(*this, data, cl()));
  1229. }
  1230. void SoftCPU::SAR_RM8_imm8(const X86::Instruction& insn)
  1231. {
  1232. auto data = insn.modrm().read8(*this, insn);
  1233. insn.modrm().write8(*this, insn, op_sar(*this, data, insn.imm8()));
  1234. }
  1235. void SoftCPU::SCASB(const X86::Instruction&) { TODO(); }
  1236. void SoftCPU::SCASD(const X86::Instruction&) { TODO(); }
  1237. void SoftCPU::SCASW(const X86::Instruction&) { TODO(); }
  1238. void SoftCPU::SETcc_RM8(const X86::Instruction& insn)
  1239. {
  1240. insn.modrm().write8(*this, insn, evaluate_condition(insn.cc()));
  1241. }
  1242. void SoftCPU::SGDT(const X86::Instruction&) { TODO(); }
  1243. void SoftCPU::SHLD_RM16_reg16_CL(const X86::Instruction&) { TODO(); }
  1244. void SoftCPU::SHLD_RM16_reg16_imm8(const X86::Instruction&) { TODO(); }
  1245. void SoftCPU::SHLD_RM32_reg32_CL(const X86::Instruction&) { TODO(); }
  1246. void SoftCPU::SHLD_RM32_reg32_imm8(const X86::Instruction&) { TODO(); }
  1247. void SoftCPU::SHL_RM16_1(const X86::Instruction& insn)
  1248. {
  1249. auto data = insn.modrm().read16(*this, insn);
  1250. insn.modrm().write16(*this, insn, op_shl(*this, data, 1));
  1251. }
  1252. void SoftCPU::SHL_RM16_CL(const X86::Instruction& insn)
  1253. {
  1254. auto data = insn.modrm().read16(*this, insn);
  1255. insn.modrm().write16(*this, insn, op_shl(*this, data, cl()));
  1256. }
  1257. void SoftCPU::SHL_RM16_imm8(const X86::Instruction& insn)
  1258. {
  1259. auto data = insn.modrm().read16(*this, insn);
  1260. insn.modrm().write16(*this, insn, op_shl(*this, data, insn.imm8()));
  1261. }
  1262. void SoftCPU::SHL_RM32_1(const X86::Instruction& insn)
  1263. {
  1264. auto data = insn.modrm().read32(*this, insn);
  1265. insn.modrm().write32(*this, insn, op_shl(*this, data, 1));
  1266. }
  1267. void SoftCPU::SHL_RM32_CL(const X86::Instruction& insn)
  1268. {
  1269. auto data = insn.modrm().read32(*this, insn);
  1270. insn.modrm().write32(*this, insn, op_shl(*this, data, cl()));
  1271. }
  1272. void SoftCPU::SHL_RM32_imm8(const X86::Instruction& insn)
  1273. {
  1274. auto data = insn.modrm().read32(*this, insn);
  1275. insn.modrm().write32(*this, insn, op_shl(*this, data, insn.imm8()));
  1276. }
  1277. void SoftCPU::SHL_RM8_1(const X86::Instruction& insn)
  1278. {
  1279. auto data = insn.modrm().read8(*this, insn);
  1280. insn.modrm().write8(*this, insn, op_shl(*this, data, 1));
  1281. }
  1282. void SoftCPU::SHL_RM8_CL(const X86::Instruction& insn)
  1283. {
  1284. auto data = insn.modrm().read8(*this, insn);
  1285. insn.modrm().write8(*this, insn, op_shl(*this, data, cl()));
  1286. }
  1287. void SoftCPU::SHL_RM8_imm8(const X86::Instruction& insn)
  1288. {
  1289. auto data = insn.modrm().read8(*this, insn);
  1290. insn.modrm().write8(*this, insn, op_shl(*this, data, insn.imm8()));
  1291. }
  1292. void SoftCPU::SHRD_RM16_reg16_CL(const X86::Instruction&) { TODO(); }
  1293. void SoftCPU::SHRD_RM16_reg16_imm8(const X86::Instruction&) { TODO(); }
  1294. void SoftCPU::SHRD_RM32_reg32_CL(const X86::Instruction&) { TODO(); }
  1295. void SoftCPU::SHRD_RM32_reg32_imm8(const X86::Instruction&) { TODO(); }
  1296. void SoftCPU::SHR_RM16_1(const X86::Instruction& insn)
  1297. {
  1298. auto data = insn.modrm().read16(*this, insn);
  1299. insn.modrm().write16(*this, insn, op_shr(*this, data, 1));
  1300. }
  1301. void SoftCPU::SHR_RM16_CL(const X86::Instruction& insn)
  1302. {
  1303. auto data = insn.modrm().read16(*this, insn);
  1304. insn.modrm().write16(*this, insn, op_shr(*this, data, cl()));
  1305. }
  1306. void SoftCPU::SHR_RM16_imm8(const X86::Instruction& insn)
  1307. {
  1308. auto data = insn.modrm().read16(*this, insn);
  1309. insn.modrm().write16(*this, insn, op_shr(*this, data, insn.imm8()));
  1310. }
  1311. void SoftCPU::SHR_RM32_1(const X86::Instruction& insn)
  1312. {
  1313. auto data = insn.modrm().read32(*this, insn);
  1314. insn.modrm().write32(*this, insn, op_shr(*this, data, 1));
  1315. }
  1316. void SoftCPU::SHR_RM32_CL(const X86::Instruction& insn)
  1317. {
  1318. auto data = insn.modrm().read32(*this, insn);
  1319. insn.modrm().write32(*this, insn, op_shr(*this, data, cl()));
  1320. }
  1321. void SoftCPU::SHR_RM32_imm8(const X86::Instruction& insn)
  1322. {
  1323. auto data = insn.modrm().read32(*this, insn);
  1324. insn.modrm().write32(*this, insn, op_shr(*this, data, insn.imm8()));
  1325. }
  1326. void SoftCPU::SHR_RM8_1(const X86::Instruction& insn)
  1327. {
  1328. auto data = insn.modrm().read8(*this, insn);
  1329. insn.modrm().write8(*this, insn, op_shr(*this, data, 1));
  1330. }
  1331. void SoftCPU::SHR_RM8_CL(const X86::Instruction& insn)
  1332. {
  1333. auto data = insn.modrm().read8(*this, insn);
  1334. insn.modrm().write8(*this, insn, op_shr(*this, data, cl()));
  1335. }
  1336. void SoftCPU::SHR_RM8_imm8(const X86::Instruction& insn)
  1337. {
  1338. auto data = insn.modrm().read8(*this, insn);
  1339. insn.modrm().write8(*this, insn, op_shr(*this, data, insn.imm8()));
  1340. }
  1341. void SoftCPU::SIDT(const X86::Instruction&) { TODO(); }
  1342. void SoftCPU::SLDT_RM16(const X86::Instruction&) { TODO(); }
  1343. void SoftCPU::SMSW_RM16(const X86::Instruction&) { TODO(); }
  1344. void SoftCPU::STC(const X86::Instruction&) { TODO(); }
  1345. void SoftCPU::STD(const X86::Instruction&) { TODO(); }
  1346. void SoftCPU::STI(const X86::Instruction&) { TODO(); }
  1347. void SoftCPU::STOSB(const X86::Instruction& insn)
  1348. {
  1349. if (insn.has_address_size_override_prefix()) {
  1350. do_once_or_repeat<false>(insn, [&] {
  1351. write_memory8({ es(), di() }, al());
  1352. set_di(di() + (df() ? -1 : 1));
  1353. });
  1354. } else {
  1355. do_once_or_repeat<false>(insn, [&] {
  1356. write_memory8({ es(), edi() }, al());
  1357. set_edi(edi() + (df() ? -1 : 1));
  1358. });
  1359. }
  1360. }
  1361. void SoftCPU::STOSD(const X86::Instruction& insn)
  1362. {
  1363. if (insn.has_address_size_override_prefix()) {
  1364. do_once_or_repeat<false>(insn, [&] {
  1365. write_memory32({ es(), di() }, eax());
  1366. set_di(di() + (df() ? -4 : 4));
  1367. });
  1368. } else {
  1369. do_once_or_repeat<false>(insn, [&] {
  1370. write_memory32({ es(), edi() }, eax());
  1371. set_edi(edi() + (df() ? -4 : 4));
  1372. });
  1373. }
  1374. }
  1375. void SoftCPU::STOSW(const X86::Instruction& insn)
  1376. {
  1377. if (insn.has_address_size_override_prefix()) {
  1378. do_once_or_repeat<false>(insn, [&] {
  1379. write_memory16({ es(), di() }, ax());
  1380. set_di(di() + (df() ? -2 : 2));
  1381. });
  1382. } else {
  1383. do_once_or_repeat<false>(insn, [&] {
  1384. write_memory16({ es(), edi() }, ax());
  1385. set_edi(edi() + (df() ? -2 : 2));
  1386. });
  1387. }
  1388. }
  1389. void SoftCPU::STR_RM16(const X86::Instruction&) { TODO(); }
  1390. void SoftCPU::UD0(const X86::Instruction&) { TODO(); }
  1391. void SoftCPU::UD1(const X86::Instruction&) { TODO(); }
  1392. void SoftCPU::UD2(const X86::Instruction&) { TODO(); }
  1393. void SoftCPU::VERR_RM16(const X86::Instruction&) { TODO(); }
  1394. void SoftCPU::VERW_RM16(const X86::Instruction&) { TODO(); }
  1395. void SoftCPU::WAIT(const X86::Instruction&) { TODO(); }
  1396. void SoftCPU::WBINVD(const X86::Instruction&) { TODO(); }
  1397. void SoftCPU::XADD_RM16_reg16(const X86::Instruction& insn)
  1398. {
  1399. auto dest = insn.modrm().read16(*this, insn);
  1400. auto src = gpr16(insn.reg16());
  1401. auto result = op_add(*this, dest, src);
  1402. gpr16(insn.reg16()) = dest;
  1403. insn.modrm().write16(*this, insn, result);
  1404. }
  1405. void SoftCPU::XADD_RM32_reg32(const X86::Instruction& insn)
  1406. {
  1407. auto dest = insn.modrm().read32(*this, insn);
  1408. auto src = gpr32(insn.reg32());
  1409. auto result = op_add(*this, dest, src);
  1410. gpr32(insn.reg32()) = dest;
  1411. insn.modrm().write32(*this, insn, result);
  1412. }
  1413. void SoftCPU::XADD_RM8_reg8(const X86::Instruction& insn)
  1414. {
  1415. auto dest = insn.modrm().read8(*this, insn);
  1416. auto src = gpr8(insn.reg8());
  1417. auto result = op_add(*this, dest, src);
  1418. gpr8(insn.reg8()) = dest;
  1419. insn.modrm().write8(*this, insn, result);
  1420. }
  1421. void SoftCPU::XCHG_AX_reg16(const X86::Instruction& insn)
  1422. {
  1423. auto temp = gpr16(insn.reg16());
  1424. gpr16(insn.reg16()) = eax();
  1425. set_eax(temp);
  1426. }
  1427. void SoftCPU::XCHG_EAX_reg32(const X86::Instruction& insn)
  1428. {
  1429. auto temp = gpr32(insn.reg32());
  1430. gpr32(insn.reg32()) = eax();
  1431. set_eax(temp);
  1432. }
  1433. void SoftCPU::XCHG_reg16_RM16(const X86::Instruction& insn)
  1434. {
  1435. auto temp = insn.modrm().read16(*this, insn);
  1436. insn.modrm().write16(*this, insn, gpr16(insn.reg16()));
  1437. gpr16(insn.reg16()) = temp;
  1438. }
  1439. void SoftCPU::XCHG_reg32_RM32(const X86::Instruction& insn)
  1440. {
  1441. auto temp = insn.modrm().read32(*this, insn);
  1442. insn.modrm().write32(*this, insn, gpr32(insn.reg32()));
  1443. gpr32(insn.reg32()) = temp;
  1444. }
  1445. void SoftCPU::XCHG_reg8_RM8(const X86::Instruction& insn)
  1446. {
  1447. auto temp = insn.modrm().read8(*this, insn);
  1448. insn.modrm().write8(*this, insn, gpr8(insn.reg8()));
  1449. gpr8(insn.reg8()) = temp;
  1450. }
  1451. void SoftCPU::XLAT(const X86::Instruction&) { TODO(); }
  1452. #define DEFINE_GENERIC_INSN_HANDLERS_PARTIAL(mnemonic, op, update_dest) \
  1453. void SoftCPU::mnemonic##_AL_imm8(const X86::Instruction& insn) { generic_AL_imm8<update_dest>(op<u8>, insn); } \
  1454. void SoftCPU::mnemonic##_AX_imm16(const X86::Instruction& insn) { generic_AX_imm16<update_dest>(op<u16>, insn); } \
  1455. void SoftCPU::mnemonic##_EAX_imm32(const X86::Instruction& insn) { generic_EAX_imm32<update_dest>(op<u32>, insn); } \
  1456. void SoftCPU::mnemonic##_RM16_imm16(const X86::Instruction& insn) { generic_RM16_imm16<update_dest>(op<u16>, insn); } \
  1457. void SoftCPU::mnemonic##_RM16_reg16(const X86::Instruction& insn) { generic_RM16_reg16<update_dest>(op<u16>, insn); } \
  1458. void SoftCPU::mnemonic##_RM32_imm32(const X86::Instruction& insn) { generic_RM32_imm32<update_dest>(op<u32>, insn); } \
  1459. void SoftCPU::mnemonic##_RM32_reg32(const X86::Instruction& insn) { generic_RM32_reg32<update_dest>(op<u32>, insn); } \
  1460. void SoftCPU::mnemonic##_RM8_imm8(const X86::Instruction& insn) { generic_RM8_imm8<update_dest>(op<u8>, insn); } \
  1461. void SoftCPU::mnemonic##_RM8_reg8(const X86::Instruction& insn) { generic_RM8_reg8<update_dest>(op<u8>, insn); }
  1462. #define DEFINE_GENERIC_INSN_HANDLERS(mnemonic, op, update_dest) \
  1463. DEFINE_GENERIC_INSN_HANDLERS_PARTIAL(mnemonic, op, update_dest) \
  1464. void SoftCPU::mnemonic##_RM16_imm8(const X86::Instruction& insn) { generic_RM16_imm8<update_dest>(op<u16>, insn); } \
  1465. void SoftCPU::mnemonic##_RM32_imm8(const X86::Instruction& insn) { generic_RM32_imm8<update_dest>(op<u32>, insn); } \
  1466. void SoftCPU::mnemonic##_reg16_RM16(const X86::Instruction& insn) { generic_reg16_RM16<update_dest>(op<u16>, insn); } \
  1467. void SoftCPU::mnemonic##_reg32_RM32(const X86::Instruction& insn) { generic_reg32_RM32<update_dest>(op<u32>, insn); } \
  1468. void SoftCPU::mnemonic##_reg8_RM8(const X86::Instruction& insn) { generic_reg8_RM8<update_dest>(op<u8>, insn); }
  1469. DEFINE_GENERIC_INSN_HANDLERS(XOR, op_xor, true)
  1470. DEFINE_GENERIC_INSN_HANDLERS(OR, op_or, true)
  1471. DEFINE_GENERIC_INSN_HANDLERS(ADD, op_add, true)
  1472. DEFINE_GENERIC_INSN_HANDLERS(SUB, op_sub, true)
  1473. DEFINE_GENERIC_INSN_HANDLERS(SBB, op_sbb, true)
  1474. DEFINE_GENERIC_INSN_HANDLERS(AND, op_and, true)
  1475. DEFINE_GENERIC_INSN_HANDLERS(CMP, op_sub, false)
  1476. DEFINE_GENERIC_INSN_HANDLERS_PARTIAL(TEST, op_and, false)
  1477. void SoftCPU::MOVQ_mm1_mm2m64(const X86::Instruction&) { TODO(); }
  1478. void SoftCPU::EMMS(const X86::Instruction&) { TODO(); }
  1479. void SoftCPU::MOVQ_mm1_m64_mm2(const X86::Instruction&) { TODO(); }
  1480. void SoftCPU::wrap_0xC0(const X86::Instruction&) { TODO(); }
  1481. void SoftCPU::wrap_0xC1_16(const X86::Instruction&) { TODO(); }
  1482. void SoftCPU::wrap_0xC1_32(const X86::Instruction&) { TODO(); }
  1483. void SoftCPU::wrap_0xD0(const X86::Instruction&) { TODO(); }
  1484. void SoftCPU::wrap_0xD1_16(const X86::Instruction&) { TODO(); }
  1485. void SoftCPU::wrap_0xD1_32(const X86::Instruction&) { TODO(); }
  1486. void SoftCPU::wrap_0xD2(const X86::Instruction&) { TODO(); }
  1487. void SoftCPU::wrap_0xD3_16(const X86::Instruction&) { TODO(); }
  1488. void SoftCPU::wrap_0xD3_32(const X86::Instruction&) { TODO(); }
  1489. }