arith.rs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  1. use cpu::cpu::*;
  2. use cpu::global_pointers::*;
  3. use cpu::memory::{read8, write8};
  4. use cpu::misc_instr::{getaf, getcf, getzf};
  5. pub fn int_log2(x: i32) -> i32 { 31 - x.leading_zeros() as i32 }
  6. #[no_mangle]
  7. pub unsafe fn add(dest_operand: i32, source_operand: i32, op_size: i32) -> i32 {
  8. let res = dest_operand + source_operand;
  9. *last_op1 = dest_operand;
  10. *last_result = res & (2 << op_size) - 1;
  11. *last_op_size = op_size;
  12. *flags_changed = FLAGS_ALL;
  13. return res;
  14. }
  15. #[no_mangle]
  16. pub unsafe fn adc(dest_operand: i32, source_operand: i32, op_size: i32) -> i32 {
  17. let cf = getcf() as i32;
  18. let res = dest_operand + source_operand + cf;
  19. *last_op1 = dest_operand;
  20. *last_result = res;
  21. *last_op_size = op_size;
  22. *flags_changed = FLAGS_ALL & !FLAG_CARRY & !FLAG_ADJUST & !FLAG_OVERFLOW;
  23. *flags = *flags & !FLAG_CARRY & !FLAG_ADJUST & !FLAG_OVERFLOW
  24. | (dest_operand ^ ((dest_operand ^ source_operand) & (source_operand ^ res))) >> op_size
  25. & FLAG_CARRY
  26. | (dest_operand ^ source_operand ^ res) & FLAG_ADJUST
  27. | ((source_operand ^ res) & (dest_operand ^ res)) >> op_size << 11 & FLAG_OVERFLOW;
  28. return res;
  29. }
  30. #[no_mangle]
  31. pub unsafe fn sub(dest_operand: i32, source_operand: i32, op_size: i32) -> i32 {
  32. let res = dest_operand - source_operand;
  33. *last_op1 = dest_operand;
  34. *last_result = res & (2 << op_size) - 1;
  35. *last_op_size = op_size;
  36. *flags_changed = FLAGS_ALL | FLAG_SUB;
  37. return res;
  38. }
  39. #[no_mangle]
  40. pub unsafe fn sbb(dest_operand: i32, source_operand: i32, op_size: i32) -> i32 {
  41. let cf = getcf() as i32;
  42. let res = dest_operand - source_operand - cf;
  43. *last_op1 = dest_operand;
  44. *last_result = res;
  45. *last_op_size = op_size;
  46. *flags_changed = FLAGS_ALL & !FLAG_CARRY & !FLAG_ADJUST & !FLAG_OVERFLOW | FLAG_SUB;
  47. *flags = *flags & !FLAG_CARRY & !FLAG_ADJUST & !FLAG_OVERFLOW
  48. | (res ^ ((res ^ source_operand) & (source_operand ^ dest_operand))) >> op_size
  49. & FLAG_CARRY
  50. | (dest_operand ^ source_operand ^ res) & FLAG_ADJUST
  51. | ((source_operand ^ dest_operand) & (res ^ dest_operand)) >> op_size << 11 & FLAG_OVERFLOW;
  52. return res;
  53. }
  54. #[no_mangle]
  55. pub unsafe fn add8(x: i32, y: i32) -> i32 { return add(x, y, OPSIZE_8); }
  56. #[no_mangle]
  57. pub unsafe fn add16(x: i32, y: i32) -> i32 { return add(x, y, OPSIZE_16); }
  58. #[no_mangle]
  59. pub unsafe fn add32(x: i32, y: i32) -> i32 { return add(x, y, OPSIZE_32); }
  60. #[no_mangle]
  61. pub unsafe fn sub8(x: i32, y: i32) -> i32 { return sub(x, y, OPSIZE_8); }
  62. #[no_mangle]
  63. pub unsafe fn sub16(x: i32, y: i32) -> i32 { return sub(x, y, OPSIZE_16); }
  64. #[no_mangle]
  65. pub unsafe fn sub32(x: i32, y: i32) -> i32 { return sub(x, y, OPSIZE_32); }
  66. #[no_mangle]
  67. pub unsafe fn adc8(x: i32, y: i32) -> i32 { return adc(x, y, OPSIZE_8); }
  68. #[no_mangle]
  69. pub unsafe fn adc16(x: i32, y: i32) -> i32 { return adc(x, y, OPSIZE_16); }
  70. #[no_mangle]
  71. pub unsafe fn adc32(x: i32, y: i32) -> i32 { return adc(x, y, OPSIZE_32); }
  72. #[no_mangle]
  73. pub unsafe fn sbb8(x: i32, y: i32) -> i32 { return sbb(x, y, OPSIZE_8); }
  74. #[no_mangle]
  75. pub unsafe fn sbb16(x: i32, y: i32) -> i32 { return sbb(x, y, OPSIZE_16); }
  76. #[no_mangle]
  77. pub unsafe fn sbb32(x: i32, y: i32) -> i32 { return sbb(x, y, OPSIZE_32); }
  78. #[no_mangle]
  79. pub unsafe fn cmp8(x: i32, y: i32) { sub(x, y, OPSIZE_8); }
  80. #[no_mangle]
  81. pub unsafe fn cmp16(x: i32, y: i32) { sub(x, y, OPSIZE_16); }
  82. #[no_mangle]
  83. pub unsafe fn cmp32(x: i32, y: i32) { sub(x, y, OPSIZE_32); }
  84. #[no_mangle]
  85. pub unsafe fn inc(dest_operand: i32, op_size: i32) -> i32 {
  86. *flags = *flags & !1 | getcf() as i32;
  87. let res = dest_operand + 1;
  88. *last_op1 = dest_operand;
  89. *last_result = res & (2 << op_size) - 1;
  90. *last_op_size = op_size;
  91. *flags_changed = FLAGS_ALL & !1;
  92. return res;
  93. }
  94. #[no_mangle]
  95. pub unsafe fn dec(dest_operand: i32, op_size: i32) -> i32 {
  96. *flags = *flags & !1 | getcf() as i32;
  97. let res = dest_operand - 1;
  98. *last_op1 = dest_operand;
  99. *last_result = res & (2 << op_size) - 1;
  100. *last_op_size = op_size;
  101. *flags_changed = FLAGS_ALL & !1 | FLAG_SUB;
  102. return res;
  103. }
  104. #[no_mangle]
  105. pub unsafe fn inc8(x: i32) -> i32 { return inc(x, OPSIZE_8); }
  106. #[no_mangle]
  107. pub unsafe fn inc16(x: i32) -> i32 { return inc(x, OPSIZE_16); }
  108. #[no_mangle]
  109. pub unsafe fn inc32(x: i32) -> i32 { return inc(x, OPSIZE_32); }
  110. #[no_mangle]
  111. pub unsafe fn dec8(x: i32) -> i32 { return dec(x, OPSIZE_8); }
  112. #[no_mangle]
  113. pub unsafe fn dec16(x: i32) -> i32 { return dec(x, OPSIZE_16); }
  114. #[no_mangle]
  115. pub unsafe fn dec32(x: i32) -> i32 { return dec(x, OPSIZE_32); }
  116. #[no_mangle]
  117. pub unsafe fn not8(x: i32) -> i32 { return !x; }
  118. #[no_mangle]
  119. pub unsafe fn not16(x: i32) -> i32 { return !x; }
  120. #[no_mangle]
  121. pub unsafe fn not32(x: i32) -> i32 { return !x; }
  122. #[no_mangle]
  123. pub unsafe fn neg(dest_operand: i32, op_size: i32) -> i32 { sub(0, dest_operand, op_size) }
  124. #[no_mangle]
  125. pub unsafe fn neg8(x: i32) -> i32 { return neg(x, OPSIZE_8); }
  126. #[no_mangle]
  127. pub unsafe fn neg16(x: i32) -> i32 { return neg(x, OPSIZE_16); }
  128. #[no_mangle]
  129. pub unsafe fn neg32(x: i32) -> i32 { return neg(x, OPSIZE_32); }
  130. #[no_mangle]
  131. pub unsafe fn mul8(source_operand: i32) {
  132. let result = source_operand * read_reg8(AL);
  133. write_reg16(AX, result);
  134. *last_result = result & 255;
  135. *last_op_size = OPSIZE_8;
  136. if result < 256 {
  137. *flags &= !1 & !FLAG_OVERFLOW
  138. }
  139. else {
  140. *flags |= 1 | FLAG_OVERFLOW
  141. }
  142. *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;
  143. }
  144. #[no_mangle]
  145. pub unsafe fn imul8(source_operand: i32) {
  146. let result = source_operand * (read_reg8(AL) << 24 >> 24);
  147. write_reg16(AX, result);
  148. *last_result = result & 255;
  149. *last_op_size = OPSIZE_8;
  150. if result > 127 || result < -128 {
  151. *flags |= 1 | FLAG_OVERFLOW
  152. }
  153. else {
  154. *flags &= !1 & !FLAG_OVERFLOW
  155. }
  156. *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;
  157. }
  158. #[no_mangle]
  159. pub unsafe fn mul16(source_operand: u32) {
  160. let result = source_operand.wrapping_mul(read_reg16(AX) as u32);
  161. let high_result = result >> 16;
  162. write_reg16(AX, result as i32);
  163. write_reg16(DX, high_result as i32);
  164. *last_result = (result & 0xFFFF) as i32;
  165. *last_op_size = OPSIZE_16;
  166. if high_result == 0 {
  167. *flags &= !1 & !FLAG_OVERFLOW
  168. }
  169. else {
  170. *flags |= 1 | FLAG_OVERFLOW
  171. }
  172. *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;
  173. }
  174. #[no_mangle]
  175. pub unsafe fn imul16(source_operand: i32) {
  176. let result = source_operand * (read_reg16(AX) << 16 >> 16);
  177. write_reg16(AX, result);
  178. write_reg16(DX, result >> 16);
  179. *last_result = result & 0xFFFF;
  180. *last_op_size = OPSIZE_16;
  181. if result > 32767 || result < -32768 {
  182. *flags |= 1 | FLAG_OVERFLOW
  183. }
  184. else {
  185. *flags &= !1 & !FLAG_OVERFLOW
  186. }
  187. *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;
  188. }
  189. #[no_mangle]
  190. pub unsafe fn imul_reg16(mut operand1: i32, mut operand2: i32) -> i32 {
  191. operand1 = operand1 << 16 >> 16;
  192. operand2 = operand2 << 16 >> 16;
  193. let result = operand1 * operand2;
  194. *last_result = result & 0xFFFF;
  195. *last_op_size = OPSIZE_16;
  196. if result > 32767 || result < -32768 {
  197. *flags |= 1 | FLAG_OVERFLOW
  198. }
  199. else {
  200. *flags &= !1 & !FLAG_OVERFLOW
  201. }
  202. *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;
  203. return result;
  204. }
  205. #[no_mangle]
  206. pub unsafe fn mul32(source_operand: i32) {
  207. let dest_operand = read_reg32(EAX);
  208. let result = (dest_operand as u32 as u64).wrapping_mul(source_operand as u32 as u64);
  209. let result_low = result as i32;
  210. let result_high = (result >> 32) as i32;
  211. write_reg32(EAX, result_low);
  212. write_reg32(EDX, result_high);
  213. *last_result = result_low;
  214. *last_op_size = OPSIZE_32;
  215. if result_high == 0 {
  216. *flags &= !1 & !FLAG_OVERFLOW
  217. }
  218. else {
  219. *flags |= 1 | FLAG_OVERFLOW
  220. }
  221. *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;
  222. }
  223. #[no_mangle]
  224. pub unsafe fn imul32(source_operand: i32) {
  225. let dest_operand = read_reg32(EAX);
  226. let result = dest_operand as i64 * source_operand as i64;
  227. let result_low = result as i32;
  228. let result_high = (result >> 32) as i32;
  229. write_reg32(EAX, result_low);
  230. write_reg32(EDX, result_high);
  231. *last_result = result_low;
  232. *last_op_size = OPSIZE_32;
  233. if result_high == result_low >> 31 {
  234. *flags &= !1 & !FLAG_OVERFLOW
  235. }
  236. else {
  237. *flags |= 1 | FLAG_OVERFLOW
  238. }
  239. *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;
  240. }
  241. #[no_mangle]
  242. pub unsafe fn imul_reg32(operand1: i32, operand2: i32) -> i32 {
  243. let result = operand1 as i64 * operand2 as i64;
  244. let result_low = result as i32;
  245. let result_high = (result >> 32) as i32;
  246. *last_result = result_low;
  247. *last_op_size = OPSIZE_32;
  248. if result_high == result_low >> 31 {
  249. *flags &= !1 & !FLAG_OVERFLOW
  250. }
  251. else {
  252. *flags |= 1 | FLAG_OVERFLOW
  253. }
  254. *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;
  255. return result_low;
  256. }
  257. #[no_mangle]
  258. pub unsafe fn xadd8(source_operand: i32, reg: i32) -> i32 {
  259. let tmp = read_reg8(reg);
  260. write_reg8(reg, source_operand);
  261. return add(source_operand, tmp, OPSIZE_8);
  262. }
  263. #[no_mangle]
  264. pub unsafe fn xadd16(source_operand: i32, reg: i32) -> i32 {
  265. let tmp = read_reg16(reg);
  266. write_reg16(reg, source_operand);
  267. return add(source_operand, tmp, OPSIZE_16);
  268. }
  269. #[no_mangle]
  270. pub unsafe fn xadd32(source_operand: i32, reg: i32) -> i32 {
  271. let tmp = read_reg32(reg);
  272. write_reg32(reg, source_operand);
  273. return add(source_operand, tmp, OPSIZE_32);
  274. }
  275. #[no_mangle]
  276. pub unsafe fn cmpxchg8(data: i32, r: i32) -> i32 {
  277. cmp8(read_reg8(AL), data);
  278. if getzf() {
  279. read_reg8(r)
  280. }
  281. else {
  282. write_reg8(AL, data);
  283. data
  284. }
  285. }
  286. #[no_mangle]
  287. pub unsafe fn cmpxchg16(data: i32, r: i32) -> i32 {
  288. cmp16(read_reg16(AX), data);
  289. if getzf() {
  290. read_reg16(r)
  291. }
  292. else {
  293. write_reg16(AX, data);
  294. data
  295. }
  296. }
  297. #[no_mangle]
  298. pub unsafe fn cmpxchg32(data: i32, r: i32) -> i32 {
  299. cmp32(read_reg32(EAX), data);
  300. if getzf() {
  301. read_reg32(r)
  302. }
  303. else {
  304. write_reg32(EAX, data);
  305. data
  306. }
  307. }
  308. #[no_mangle]
  309. pub unsafe fn bcd_daa() {
  310. let old_al = read_reg8(AL);
  311. let old_cf = getcf();
  312. let old_af = getaf();
  313. *flags &= !1 & !FLAG_ADJUST;
  314. if old_al & 15 > 9 || old_af {
  315. write_reg8(AL, read_reg8(AL) + 6);
  316. *flags |= FLAG_ADJUST
  317. }
  318. if old_al > 153 || old_cf {
  319. write_reg8(AL, read_reg8(AL) + 96);
  320. *flags |= 1
  321. }
  322. *last_result = read_reg8(AL);
  323. *last_op_size = OPSIZE_8;
  324. *flags_changed = FLAGS_ALL & !1 & !FLAG_ADJUST & !FLAG_OVERFLOW;
  325. }
  326. #[no_mangle]
  327. pub unsafe fn bcd_das() {
  328. let old_al = read_reg8(AL);
  329. let old_cf = getcf();
  330. *flags &= !1;
  331. if old_al & 15 > 9 || getaf() {
  332. write_reg8(AL, read_reg8(AL) - 6);
  333. *flags |= FLAG_ADJUST;
  334. *flags = *flags & !1 | old_cf as i32 | (old_al < 6) as i32
  335. }
  336. else {
  337. *flags &= !FLAG_ADJUST
  338. }
  339. if old_al > 153 || old_cf {
  340. write_reg8(AL, read_reg8(AL) - 96);
  341. *flags |= 1
  342. }
  343. *last_result = read_reg8(AL);
  344. *last_op_size = OPSIZE_8;
  345. *flags_changed = FLAGS_ALL & !1 & !FLAG_ADJUST & !FLAG_OVERFLOW;
  346. }
  347. #[no_mangle]
  348. pub unsafe fn bcd_aad(imm8: i32) {
  349. let result = read_reg8(AL) + read_reg8(AH) * imm8;
  350. *last_result = result & 255;
  351. write_reg16(AX, *last_result);
  352. *last_op_size = OPSIZE_8;
  353. *flags_changed = FLAGS_ALL & !1 & !FLAG_ADJUST & !FLAG_OVERFLOW;
  354. *flags &= !1 & !FLAG_ADJUST & !FLAG_OVERFLOW;
  355. if result > 0xFFFF {
  356. *flags |= 1
  357. };
  358. }
  359. #[no_mangle]
  360. pub unsafe fn bcd_aam(imm8: i32) {
  361. // ascii adjust after multiplication
  362. if imm8 == 0 {
  363. trigger_de();
  364. }
  365. else {
  366. let temp = read_reg8(AL);
  367. write_reg8(AH, temp as i32 / imm8);
  368. write_reg8(AL, temp as i32 % imm8);
  369. *last_result = read_reg8(AL);
  370. *flags_changed = FLAGS_ALL & !1 & !FLAG_ADJUST & !FLAG_OVERFLOW;
  371. *flags &= !1 & !FLAG_ADJUST & !FLAG_OVERFLOW
  372. };
  373. }
  374. #[no_mangle]
  375. pub unsafe fn bcd_aaa() {
  376. if read_reg8(AL) & 15 > 9 || getaf() {
  377. write_reg16(AX, read_reg16(AX) + 6);
  378. write_reg8(AH, read_reg8(AH) + 1);
  379. *flags |= FLAG_ADJUST | 1
  380. }
  381. else {
  382. *flags &= !FLAG_ADJUST & !1
  383. }
  384. write_reg8(AL, read_reg8(AL) & 15);
  385. *flags_changed &= !FLAG_ADJUST & !1;
  386. }
  387. #[no_mangle]
  388. pub unsafe fn bcd_aas() {
  389. if read_reg8(AL) & 15 > 9 || getaf() {
  390. write_reg16(AX, read_reg16(AX) - 6);
  391. write_reg8(AH, read_reg8(AH) - 1);
  392. *flags |= FLAG_ADJUST | 1
  393. }
  394. else {
  395. *flags &= !FLAG_ADJUST & !1
  396. }
  397. write_reg8(AL, read_reg8(AL) & 15);
  398. *flags_changed &= !FLAG_ADJUST & !1;
  399. }
  400. #[no_mangle]
  401. pub unsafe fn and(dest_operand: i32, source_operand: i32, op_size: i32) -> i32 {
  402. let result = dest_operand & source_operand;
  403. *last_result = result;
  404. *last_op_size = op_size;
  405. *flags &= !1 & !FLAG_OVERFLOW & !FLAG_ADJUST;
  406. *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW & !FLAG_ADJUST;
  407. return result;
  408. }
  409. #[no_mangle]
  410. pub unsafe fn or(dest_operand: i32, source_operand: i32, op_size: i32) -> i32 {
  411. let result = dest_operand | source_operand;
  412. *last_result = result;
  413. *last_op_size = op_size;
  414. *flags &= !1 & !FLAG_OVERFLOW & !FLAG_ADJUST;
  415. *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW & !FLAG_ADJUST;
  416. return result;
  417. }
  418. #[no_mangle]
  419. pub unsafe fn xor(dest_operand: i32, source_operand: i32, op_size: i32) -> i32 {
  420. let result = dest_operand ^ source_operand;
  421. *last_result = result;
  422. *last_op_size = op_size;
  423. *flags &= !1 & !FLAG_OVERFLOW & !FLAG_ADJUST;
  424. *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW & !FLAG_ADJUST;
  425. return result;
  426. }
  427. #[no_mangle]
  428. pub unsafe fn and8(x: i32, y: i32) -> i32 { return and(x, y, OPSIZE_8); }
  429. #[no_mangle]
  430. pub unsafe fn and16(x: i32, y: i32) -> i32 { return and(x, y, OPSIZE_16); }
  431. #[no_mangle]
  432. pub unsafe fn and32(x: i32, y: i32) -> i32 { return and(x, y, OPSIZE_32); }
  433. #[no_mangle]
  434. pub unsafe fn test8(x: i32, y: i32) { and(x, y, OPSIZE_8); }
  435. #[no_mangle]
  436. pub unsafe fn test16(x: i32, y: i32) { and(x, y, OPSIZE_16); }
  437. #[no_mangle]
  438. pub unsafe fn test32(x: i32, y: i32) { and(x, y, OPSIZE_32); }
  439. #[no_mangle]
  440. pub unsafe fn or8(x: i32, y: i32) -> i32 { return or(x, y, OPSIZE_8); }
  441. #[no_mangle]
  442. pub unsafe fn or16(x: i32, y: i32) -> i32 { return or(x, y, OPSIZE_16); }
  443. #[no_mangle]
  444. pub unsafe fn or32(x: i32, y: i32) -> i32 { return or(x, y, OPSIZE_32); }
  445. #[no_mangle]
  446. pub unsafe fn xor8(x: i32, y: i32) -> i32 { return xor(x, y, OPSIZE_8); }
  447. #[no_mangle]
  448. pub unsafe fn xor16(x: i32, y: i32) -> i32 { return xor(x, y, OPSIZE_16); }
  449. #[no_mangle]
  450. pub unsafe fn xor32(x: i32, y: i32) -> i32 { return xor(x, y, OPSIZE_32); }
  451. #[no_mangle]
  452. pub unsafe fn rol8(dest_operand: i32, mut count: i32) -> i32 {
  453. dbg_assert!(count >= 0 && count < 32);
  454. if 0 == count {
  455. return dest_operand;
  456. }
  457. else {
  458. count &= 7;
  459. let result = dest_operand << count | dest_operand >> 8 - count;
  460. *flags_changed &= !1 & !FLAG_OVERFLOW;
  461. *flags = *flags & !1 & !FLAG_OVERFLOW
  462. | result & 1
  463. | (result << 11 ^ result << 4) & FLAG_OVERFLOW;
  464. return result;
  465. };
  466. }
  467. #[no_mangle]
  468. pub unsafe fn rol16(dest_operand: i32, mut count: i32) -> i32 {
  469. dbg_assert!(count >= 0 && count < 32);
  470. if 0 == count {
  471. return dest_operand;
  472. }
  473. else {
  474. count &= 15;
  475. let result = dest_operand << count | dest_operand >> 16 - count;
  476. *flags_changed &= !1 & !FLAG_OVERFLOW;
  477. *flags = *flags & !1 & !FLAG_OVERFLOW
  478. | result & 1
  479. | (result << 11 ^ result >> 4) & FLAG_OVERFLOW;
  480. return result;
  481. };
  482. }
  483. #[no_mangle]
  484. pub unsafe fn rol32(dest_operand: i32, count: i32) -> i32 {
  485. dbg_assert!(count >= 0 && count < 32);
  486. if 0 == count {
  487. return dest_operand;
  488. }
  489. else {
  490. let result = ((dest_operand << count) as u32 | dest_operand as u32 >> 32 - count) as i32;
  491. *flags_changed &= !1 & !FLAG_OVERFLOW;
  492. *flags = *flags & !1 & !FLAG_OVERFLOW
  493. | result & 1
  494. | (result << 11 ^ result >> 20) & FLAG_OVERFLOW;
  495. return result;
  496. };
  497. }
  498. #[no_mangle]
  499. pub unsafe fn rcl8(dest_operand: i32, mut count: i32) -> i32 {
  500. dbg_assert!(count >= 0 && count < 32);
  501. count %= 9;
  502. if 0 == count {
  503. return dest_operand;
  504. }
  505. else {
  506. let result =
  507. dest_operand << count | (getcf() as i32) << count - 1 | dest_operand >> 9 - count;
  508. *flags_changed &= !1 & !FLAG_OVERFLOW;
  509. *flags = *flags & !1 & !FLAG_OVERFLOW
  510. | result >> 8 & 1
  511. | (result << 3 ^ result << 4) & FLAG_OVERFLOW;
  512. return result;
  513. };
  514. }
  515. #[no_mangle]
  516. pub unsafe fn rcl16(dest_operand: i32, mut count: i32) -> i32 {
  517. dbg_assert!(count >= 0 && count < 32);
  518. count %= 17;
  519. if 0 == count {
  520. return dest_operand;
  521. }
  522. else {
  523. let result =
  524. dest_operand << count | (getcf() as i32) << count - 1 | dest_operand >> 17 - count;
  525. *flags_changed &= !1 & !FLAG_OVERFLOW;
  526. *flags = *flags & !1 & !FLAG_OVERFLOW
  527. | result >> 16 & 1
  528. | (result >> 5 ^ result >> 4) & FLAG_OVERFLOW;
  529. return result;
  530. };
  531. }
  532. #[no_mangle]
  533. pub unsafe fn rcl32(dest_operand: i32, count: i32) -> i32 {
  534. dbg_assert!(count >= 0 && count < 32);
  535. if 0 == count {
  536. return dest_operand;
  537. }
  538. else {
  539. let mut result: i32 = dest_operand << count | (getcf() as i32) << count - 1;
  540. if count > 1 {
  541. result = (result as u32 | dest_operand as u32 >> 33 - count) as i32
  542. }
  543. *flags_changed &= !1 & !FLAG_OVERFLOW;
  544. let b = (dest_operand as u32 >> 32 - count & 1) as i32;
  545. *flags = (*flags & !1 & !FLAG_OVERFLOW | b) | (b << 11 ^ result >> 20) & FLAG_OVERFLOW;
  546. return result;
  547. };
  548. }
  549. #[no_mangle]
  550. pub unsafe fn ror8(dest_operand: i32, mut count: i32) -> i32 {
  551. dbg_assert!(count >= 0 && count < 32);
  552. if 0 == count {
  553. return dest_operand;
  554. }
  555. else {
  556. count &= 7;
  557. let result = dest_operand >> count | dest_operand << 8 - count;
  558. *flags_changed &= !1 & !FLAG_OVERFLOW;
  559. *flags = *flags & !1 & !FLAG_OVERFLOW
  560. | result >> 7 & 1
  561. | (result << 4 ^ result << 5) & FLAG_OVERFLOW;
  562. return result;
  563. };
  564. }
  565. #[no_mangle]
  566. pub unsafe fn ror16(dest_operand: i32, mut count: i32) -> i32 {
  567. dbg_assert!(count >= 0 && count < 32);
  568. if 0 == count {
  569. return dest_operand;
  570. }
  571. else {
  572. count &= 15;
  573. let result = dest_operand >> count | dest_operand << 16 - count;
  574. *flags_changed &= !1 & !FLAG_OVERFLOW;
  575. *flags = *flags & !1 & !FLAG_OVERFLOW
  576. | result >> 15 & 1
  577. | (result >> 4 ^ result >> 3) & FLAG_OVERFLOW;
  578. return result;
  579. };
  580. }
  581. #[no_mangle]
  582. pub unsafe fn ror32(dest_operand: i32, count: i32) -> i32 {
  583. dbg_assert!(count >= 0 && count < 32);
  584. if 0 == count {
  585. return dest_operand;
  586. }
  587. else {
  588. let result = (dest_operand as u32 >> count | (dest_operand << 32 - count) as u32) as i32;
  589. *flags_changed &= !1 & !FLAG_OVERFLOW;
  590. *flags = *flags & !1 & !FLAG_OVERFLOW
  591. | result >> 31 & 1
  592. | (result >> 20 ^ result >> 19) & FLAG_OVERFLOW;
  593. return result;
  594. };
  595. }
  596. #[no_mangle]
  597. pub unsafe fn rcr8(dest_operand: i32, mut count: i32) -> i32 {
  598. dbg_assert!(count >= 0 && count < 32);
  599. count %= 9;
  600. if 0 == count {
  601. return dest_operand;
  602. }
  603. else {
  604. let result =
  605. dest_operand >> count | (getcf() as i32) << 8 - count | dest_operand << 9 - count;
  606. *flags_changed &= !1 & !FLAG_OVERFLOW;
  607. *flags = *flags & !1 & !FLAG_OVERFLOW
  608. | result >> 8 & 1
  609. | (result << 4 ^ result << 5) & FLAG_OVERFLOW;
  610. return result;
  611. };
  612. }
  613. #[no_mangle]
  614. pub unsafe fn rcr16(dest_operand: i32, mut count: i32) -> i32 {
  615. dbg_assert!(count >= 0 && count < 32);
  616. count %= 17;
  617. if 0 == count {
  618. return dest_operand;
  619. }
  620. else {
  621. let result =
  622. dest_operand >> count | (getcf() as i32) << 16 - count | dest_operand << 17 - count;
  623. *flags_changed &= !1 & !FLAG_OVERFLOW;
  624. *flags = *flags & !1 & !FLAG_OVERFLOW
  625. | result >> 16 & 1
  626. | (result >> 4 ^ result >> 3) & FLAG_OVERFLOW;
  627. return result;
  628. };
  629. }
  630. #[no_mangle]
  631. pub unsafe fn rcr32(dest_operand: i32, count: i32) -> i32 {
  632. dbg_assert!(count >= 0 && count < 32);
  633. if 0 == count {
  634. return dest_operand;
  635. }
  636. else {
  637. let mut result: i32 =
  638. (dest_operand as u32 >> count | ((getcf() as i32) << 32 - count) as u32) as i32;
  639. if count > 1 {
  640. result |= dest_operand << 33 - count
  641. }
  642. *flags_changed &= !1 & !FLAG_OVERFLOW;
  643. *flags = *flags & !1 & !FLAG_OVERFLOW
  644. | dest_operand >> count - 1 & 1
  645. | (result >> 20 ^ result >> 19) & FLAG_OVERFLOW;
  646. return result;
  647. };
  648. }
  649. #[no_mangle]
  650. pub unsafe fn div8(source_operand: u32) {
  651. if source_operand == 0 {
  652. trigger_de();
  653. return;
  654. }
  655. else {
  656. let target_operand = read_reg16(AX);
  657. let result = (target_operand as u32).wrapping_div(source_operand) as u16;
  658. if result as i32 >= 256 {
  659. trigger_de();
  660. }
  661. else {
  662. write_reg8(AL, result as i32);
  663. write_reg8(
  664. AH,
  665. (target_operand as u32).wrapping_rem(source_operand) as i32,
  666. );
  667. }
  668. return;
  669. };
  670. }
  671. #[no_mangle]
  672. pub unsafe fn idiv8(source_operand: i32) {
  673. if source_operand == 0 {
  674. trigger_de();
  675. return;
  676. }
  677. else {
  678. let target_operand = read_reg16(AX) << 16 >> 16;
  679. let result = target_operand / source_operand;
  680. if result >= 128 || result <= -129 {
  681. trigger_de();
  682. }
  683. else {
  684. write_reg8(AL, result);
  685. write_reg8(AH, target_operand % source_operand);
  686. }
  687. return;
  688. };
  689. }
  690. #[no_mangle]
  691. pub unsafe fn div16_without_fault(source_operand: u32) -> bool {
  692. if source_operand == 0 {
  693. return false;
  694. }
  695. let target_operand = (read_reg16(AX) | read_reg16(DX) << 16) as u32;
  696. let result = target_operand.wrapping_div(source_operand);
  697. if result >= 0x10000 {
  698. return false;
  699. }
  700. write_reg16(AX, result as i32);
  701. write_reg16(DX, target_operand.wrapping_rem(source_operand) as i32);
  702. return true;
  703. }
  704. pub unsafe fn div16(source_operand: u32) {
  705. if !div16_without_fault(source_operand) {
  706. trigger_de()
  707. }
  708. }
  709. #[no_mangle]
  710. pub unsafe fn idiv16_without_fault(source_operand: i32) -> bool {
  711. if source_operand == 0 {
  712. return false;
  713. }
  714. let target_operand = read_reg16(AX) | read_reg16(DX) << 16;
  715. let result = target_operand / source_operand;
  716. if result >= 32768 || result <= -32769 {
  717. return false;
  718. }
  719. write_reg16(AX, result);
  720. write_reg16(DX, (target_operand % source_operand) as i32);
  721. return true;
  722. }
  723. pub unsafe fn idiv16(source_operand: i32) {
  724. if !idiv16_without_fault(source_operand) {
  725. trigger_de()
  726. }
  727. }
  728. #[no_mangle]
  729. pub unsafe fn div32_without_fault(source_operand: u32) -> bool {
  730. if source_operand == 0 {
  731. return false;
  732. }
  733. let target_low = read_reg32(EAX) as u32;
  734. let target_high = read_reg32(EDX) as u32;
  735. let target_operand = (target_high as u64) << 32 | target_low as u64;
  736. let result = target_operand.wrapping_div(source_operand as u64);
  737. if result > 0xFFFFFFFF {
  738. return false;
  739. }
  740. let mod_0 = target_operand.wrapping_rem(source_operand as u64) as i32;
  741. write_reg32(EAX, result as i32);
  742. write_reg32(EDX, mod_0);
  743. return true;
  744. }
  745. pub unsafe fn div32(source_operand: u32) {
  746. if !div32_without_fault(source_operand) {
  747. trigger_de()
  748. }
  749. }
  750. #[no_mangle]
  751. pub unsafe fn idiv32_without_fault(source_operand: i32) -> bool {
  752. if source_operand == 0 {
  753. return false;
  754. }
  755. let target_low = read_reg32(EAX) as u32;
  756. let target_high = read_reg32(EDX) as u32;
  757. let target_operand = ((target_high as u64) << 32 | target_low as u64) as i64;
  758. if source_operand == -1 && target_operand == -0x80000000_00000000 as i64 {
  759. return false;
  760. }
  761. let result = target_operand / source_operand as i64;
  762. if result < -0x80000000 || result > 0x7FFFFFFF {
  763. return false;
  764. }
  765. let mod_0 = (target_operand % source_operand as i64) as i32;
  766. write_reg32(EAX, result as i32);
  767. write_reg32(EDX, mod_0);
  768. return true;
  769. }
  770. pub unsafe fn idiv32(source_operand: i32) {
  771. if !idiv32_without_fault(source_operand) {
  772. trigger_de()
  773. }
  774. }
  775. #[no_mangle]
  776. pub unsafe fn shl8(dest_operand: i32, count: i32) -> i32 {
  777. dbg_assert!(count >= 0 && count < 32);
  778. if count == 0 {
  779. return dest_operand;
  780. }
  781. else {
  782. let result = dest_operand << count;
  783. *last_result = result;
  784. *last_op_size = OPSIZE_8;
  785. *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;
  786. *flags = *flags & !1 & !FLAG_OVERFLOW
  787. | result >> 8 & 1
  788. | (result << 3 ^ result << 4) & FLAG_OVERFLOW;
  789. return result;
  790. };
  791. }
  792. #[no_mangle]
  793. pub unsafe fn shl16(dest_operand: i32, count: i32) -> i32 {
  794. dbg_assert!(count >= 0 && count < 32);
  795. if count == 0 {
  796. return dest_operand;
  797. }
  798. else {
  799. let result = dest_operand << count;
  800. *last_result = result;
  801. *last_op_size = OPSIZE_16;
  802. *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;
  803. *flags = *flags & !1 & !FLAG_OVERFLOW
  804. | result >> 16 & 1
  805. | (result >> 5 ^ result >> 4) & FLAG_OVERFLOW;
  806. return result;
  807. };
  808. }
  809. #[no_mangle]
  810. pub unsafe fn shl32(dest_operand: i32, count: i32) -> i32 {
  811. dbg_assert!(count >= 0 && count < 32);
  812. if count == 0 {
  813. return dest_operand;
  814. }
  815. else {
  816. let result = dest_operand << count;
  817. *last_result = result;
  818. *last_op_size = OPSIZE_32;
  819. *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;
  820. let b = dest_operand >> 32 - count & 1;
  821. *flags = *flags & !1 & !FLAG_OVERFLOW | b | (b ^ result >> 31 & 1) << 11 & FLAG_OVERFLOW;
  822. return result;
  823. };
  824. }
  825. #[no_mangle]
  826. pub unsafe fn shr8(dest_operand: i32, count: i32) -> i32 {
  827. dbg_assert!(count >= 0 && count < 32);
  828. if count == 0 {
  829. return dest_operand;
  830. }
  831. else {
  832. let result = dest_operand >> count;
  833. *last_result = result;
  834. *last_op_size = OPSIZE_8;
  835. *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;
  836. *flags = *flags & !1 & !FLAG_OVERFLOW
  837. | dest_operand >> count - 1 & 1
  838. | (dest_operand >> 7 & 1) << 11 & FLAG_OVERFLOW;
  839. return result;
  840. };
  841. }
  842. #[no_mangle]
  843. pub unsafe fn shr16(dest_operand: i32, count: i32) -> i32 {
  844. dbg_assert!(count >= 0 && count < 32);
  845. if count == 0 {
  846. return dest_operand;
  847. }
  848. else {
  849. let result = dest_operand >> count;
  850. *last_result = result;
  851. *last_op_size = OPSIZE_16;
  852. *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;
  853. *flags = *flags & !1 & !FLAG_OVERFLOW
  854. | dest_operand >> count - 1 & 1
  855. | dest_operand >> 4 & FLAG_OVERFLOW;
  856. return result;
  857. };
  858. }
  859. #[no_mangle]
  860. pub unsafe fn shr32(dest_operand: i32, count: i32) -> i32 {
  861. dbg_assert!(count >= 0 && count < 32);
  862. if count == 0 {
  863. return dest_operand;
  864. }
  865. else {
  866. let result = (dest_operand as u32 >> count) as i32;
  867. *last_result = result;
  868. *last_op_size = OPSIZE_32;
  869. *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;
  870. *flags = (*flags & !1 & !FLAG_OVERFLOW)
  871. | (dest_operand as u32 >> count - 1 & 1) as i32
  872. | (dest_operand >> 20 & FLAG_OVERFLOW);
  873. return result;
  874. };
  875. }
  876. #[no_mangle]
  877. pub unsafe fn sar8(dest_operand: i32, count: i32) -> i32 {
  878. dbg_assert!(count >= 0 && count < 32);
  879. if count == 0 {
  880. return dest_operand;
  881. }
  882. else {
  883. let result;
  884. if count < 8 {
  885. result = dest_operand << 24 >> count + 24;
  886. // of is zero
  887. *flags = *flags & !1 & !FLAG_OVERFLOW | dest_operand >> count - 1 & 1
  888. }
  889. else {
  890. result = dest_operand << 24 >> 31;
  891. *flags = *flags & !1 & !FLAG_OVERFLOW | result & 1
  892. }
  893. *last_result = result;
  894. *last_op_size = OPSIZE_8;
  895. *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;
  896. return result;
  897. };
  898. }
  899. #[no_mangle]
  900. pub unsafe fn sar16(dest_operand: i32, count: i32) -> i32 {
  901. dbg_assert!(count >= 0 && count < 32);
  902. if count == 0 {
  903. return dest_operand;
  904. }
  905. else {
  906. let result;
  907. if count < 16 {
  908. result = dest_operand << 16 >> count + 16;
  909. *flags = *flags & !1 & !FLAG_OVERFLOW | dest_operand >> count - 1 & 1
  910. }
  911. else {
  912. result = dest_operand << 16 >> 31;
  913. *flags = *flags & !1 & !FLAG_OVERFLOW | result & 1
  914. }
  915. *last_result = result;
  916. *last_op_size = OPSIZE_16;
  917. *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;
  918. return result;
  919. };
  920. }
  921. #[no_mangle]
  922. pub unsafe fn sar32(dest_operand: i32, count: i32) -> i32 {
  923. dbg_assert!(count >= 0 && count < 32);
  924. if count == 0 {
  925. return dest_operand;
  926. }
  927. else {
  928. let result = dest_operand >> count;
  929. *last_result = result;
  930. *last_op_size = OPSIZE_32;
  931. *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;
  932. *flags = (*flags & !1 & !FLAG_OVERFLOW) | (dest_operand as u32 >> count - 1 & 1) as i32;
  933. return result;
  934. };
  935. }
  936. #[no_mangle]
  937. pub unsafe fn shrd16(dest_operand: i32, source_operand: i32, count: i32) -> i32 {
  938. dbg_assert!(count >= 0 && count < 32);
  939. if count == 0 {
  940. return dest_operand;
  941. }
  942. else {
  943. let result;
  944. if count <= 16 {
  945. result = dest_operand >> count | source_operand << 16 - count;
  946. *flags = *flags & !1 | dest_operand >> count - 1 & 1
  947. }
  948. else {
  949. result = dest_operand << 32 - count | source_operand >> count - 16;
  950. *flags = *flags & !1 | source_operand >> count - 17 & 1
  951. }
  952. *last_result = result;
  953. *last_op_size = OPSIZE_16;
  954. *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;
  955. *flags = *flags & !FLAG_OVERFLOW | (result ^ dest_operand) >> 4 & FLAG_OVERFLOW;
  956. return result;
  957. };
  958. }
  959. #[no_mangle]
  960. pub unsafe fn shrd32(dest_operand: i32, source_operand: i32, count: i32) -> i32 {
  961. dbg_assert!(count >= 0 && count < 32);
  962. if count == 0 {
  963. return dest_operand;
  964. }
  965. else {
  966. let result = (dest_operand as u32 >> count | (source_operand << 32 - count) as u32) as i32;
  967. *last_result = result;
  968. *last_op_size = OPSIZE_32;
  969. *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;
  970. *flags = ((*flags & !1 & !FLAG_OVERFLOW) | (dest_operand as u32 >> count - 1 & 1) as i32)
  971. | (result ^ dest_operand) >> 20 & FLAG_OVERFLOW;
  972. return result;
  973. };
  974. }
  975. #[no_mangle]
  976. pub unsafe fn shld16(dest_operand: i32, source_operand: i32, count: i32) -> i32 {
  977. dbg_assert!(count >= 0 && count < 32);
  978. if count == 0 {
  979. return dest_operand;
  980. }
  981. else {
  982. let result;
  983. if count <= 16 {
  984. result = ((dest_operand << count) as u32 | source_operand as u32 >> 16 - count) as i32;
  985. *flags = (*flags & !1) | (dest_operand as u32 >> 16 - count & 1) as i32;
  986. }
  987. else {
  988. result = dest_operand >> 32 - count | source_operand << count - 16;
  989. *flags = (*flags & !1) | (source_operand as u32 >> 32 - count & 1) as i32;
  990. }
  991. *last_result = result;
  992. *last_op_size = OPSIZE_16;
  993. *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;
  994. *flags = *flags & !FLAG_OVERFLOW | (*flags & 1 ^ result >> 15 & 1) << 11;
  995. return result;
  996. };
  997. }
  998. #[no_mangle]
  999. pub unsafe fn shld32(dest_operand: i32, source_operand: i32, count: i32) -> i32 {
  1000. dbg_assert!(count >= 0 && count < 32);
  1001. if count == 0 {
  1002. return dest_operand;
  1003. }
  1004. else {
  1005. let result = ((dest_operand << count) as u32 | source_operand as u32 >> 32 - count) as i32;
  1006. *last_result = result;
  1007. *last_op_size = OPSIZE_32;
  1008. *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;
  1009. *flags = (*flags & !1) | (dest_operand as u32 >> 32 - count & 1) as i32;
  1010. if count == 1 {
  1011. *flags = *flags & !FLAG_OVERFLOW | (*flags & 1 ^ result >> 31 & 1) << 11
  1012. }
  1013. else {
  1014. *flags &= !FLAG_OVERFLOW
  1015. }
  1016. return result;
  1017. };
  1018. }
  1019. #[no_mangle]
  1020. pub unsafe fn bt_reg(bit_base: i32, bit_offset: i32) {
  1021. *flags = *flags & !1 | bit_base >> bit_offset & 1;
  1022. *flags_changed &= !1;
  1023. }
  1024. #[no_mangle]
  1025. pub unsafe fn btc_reg(bit_base: i32, bit_offset: i32) -> i32 {
  1026. *flags = *flags & !1 | bit_base >> bit_offset & 1;
  1027. *flags_changed &= !1;
  1028. return bit_base ^ 1 << bit_offset;
  1029. }
  1030. #[no_mangle]
  1031. pub unsafe fn bts_reg(bit_base: i32, bit_offset: i32) -> i32 {
  1032. *flags = *flags & !1 | bit_base >> bit_offset & 1;
  1033. *flags_changed &= !1;
  1034. return bit_base | 1 << bit_offset;
  1035. }
  1036. #[no_mangle]
  1037. pub unsafe fn btr_reg(bit_base: i32, bit_offset: i32) -> i32 {
  1038. *flags = *flags & !1 | bit_base >> bit_offset & 1;
  1039. *flags_changed &= !1;
  1040. return bit_base & !(1 << bit_offset);
  1041. }
  1042. #[no_mangle]
  1043. pub unsafe fn bt_mem(virt_addr: i32, mut bit_offset: i32) {
  1044. let bit_base = return_on_pagefault!(safe_read8(virt_addr + (bit_offset >> 3)));
  1045. bit_offset &= 7;
  1046. *flags = *flags & !1 | bit_base >> bit_offset & 1;
  1047. *flags_changed &= !1;
  1048. }
  1049. #[no_mangle]
  1050. pub unsafe fn btc_mem(virt_addr: i32, mut bit_offset: i32) {
  1051. let phys_addr = return_on_pagefault!(translate_address_write(virt_addr + (bit_offset >> 3)));
  1052. let bit_base = read8(phys_addr);
  1053. bit_offset &= 7;
  1054. *flags = *flags & !1 | bit_base >> bit_offset & 1;
  1055. *flags_changed &= !1;
  1056. write8(phys_addr, bit_base ^ 1 << bit_offset);
  1057. }
  1058. #[no_mangle]
  1059. pub unsafe fn btr_mem(virt_addr: i32, mut bit_offset: i32) {
  1060. let phys_addr = return_on_pagefault!(translate_address_write(virt_addr + (bit_offset >> 3)));
  1061. let bit_base = read8(phys_addr);
  1062. bit_offset &= 7;
  1063. *flags = *flags & !1 | bit_base >> bit_offset & 1;
  1064. *flags_changed &= !1;
  1065. write8(phys_addr, bit_base & !(1 << bit_offset));
  1066. }
  1067. #[no_mangle]
  1068. pub unsafe fn bts_mem(virt_addr: i32, mut bit_offset: i32) {
  1069. let phys_addr = return_on_pagefault!(translate_address_write(virt_addr + (bit_offset >> 3)));
  1070. let bit_base = read8(phys_addr);
  1071. bit_offset &= 7;
  1072. *flags = *flags & !1 | bit_base >> bit_offset & 1;
  1073. *flags_changed &= !1;
  1074. write8(phys_addr, bit_base | 1 << bit_offset);
  1075. }
  1076. #[no_mangle]
  1077. pub unsafe fn bsf16(old: i32, bit_base: i32) -> i32 {
  1078. *flags_changed = FLAGS_ALL & !FLAG_ZERO & !FLAG_CARRY;
  1079. *flags &= !FLAG_CARRY;
  1080. *last_op_size = OPSIZE_16;
  1081. if bit_base == 0 {
  1082. *flags |= FLAG_ZERO;
  1083. *last_result = bit_base;
  1084. // not defined in the docs, but value doesn't change on my intel machine
  1085. return old;
  1086. }
  1087. else {
  1088. *flags &= !FLAG_ZERO;
  1089. *last_result = int_log2(-bit_base & bit_base);
  1090. return *last_result;
  1091. };
  1092. }
  1093. #[no_mangle]
  1094. pub unsafe fn bsf32(old: i32, bit_base: i32) -> i32 {
  1095. *flags_changed = FLAGS_ALL & !FLAG_ZERO & !FLAG_CARRY;
  1096. *flags &= !FLAG_CARRY;
  1097. *last_op_size = OPSIZE_32;
  1098. if bit_base == 0 {
  1099. *flags |= FLAG_ZERO;
  1100. *last_result = bit_base;
  1101. return old;
  1102. }
  1103. else {
  1104. *flags &= !FLAG_ZERO;
  1105. *last_result = int_log2(-bit_base & bit_base);
  1106. return *last_result;
  1107. };
  1108. }
  1109. #[no_mangle]
  1110. pub unsafe fn bsr16(old: i32, bit_base: i32) -> i32 {
  1111. *flags_changed = FLAGS_ALL & !FLAG_ZERO & !FLAG_CARRY;
  1112. *flags &= !FLAG_CARRY;
  1113. *last_op_size = OPSIZE_16;
  1114. if bit_base == 0 {
  1115. *flags |= FLAG_ZERO;
  1116. *last_result = bit_base;
  1117. return old;
  1118. }
  1119. else {
  1120. *flags &= !FLAG_ZERO;
  1121. *last_result = int_log2(bit_base);
  1122. return *last_result;
  1123. };
  1124. }
  1125. #[no_mangle]
  1126. pub unsafe fn bsr32(old: i32, bit_base: i32) -> i32 {
  1127. *flags_changed = FLAGS_ALL & !FLAG_ZERO & !FLAG_CARRY;
  1128. *flags &= !FLAG_CARRY;
  1129. *last_op_size = OPSIZE_32;
  1130. if bit_base == 0 {
  1131. *flags |= FLAG_ZERO;
  1132. *last_result = bit_base;
  1133. return old;
  1134. }
  1135. else {
  1136. *flags &= !FLAG_ZERO;
  1137. *last_result = int_log2(bit_base);
  1138. return *last_result;
  1139. };
  1140. }
  1141. #[no_mangle]
  1142. pub unsafe fn popcnt(v: i32) -> i32 {
  1143. *flags_changed = 0;
  1144. *flags &= !FLAGS_ALL;
  1145. if 0 != v {
  1146. return v.count_ones() as i32;
  1147. }
  1148. else {
  1149. *flags |= FLAG_ZERO;
  1150. return 0;
  1151. };
  1152. }
  1153. #[no_mangle]
  1154. pub unsafe fn saturate_sw_to_ub(v: u16) -> u8 {
  1155. let mut ret = v;
  1156. if ret >= 32768 {
  1157. ret = 0
  1158. }
  1159. else if ret > 255 {
  1160. ret = 255
  1161. }
  1162. return ret as u8;
  1163. }
  1164. #[no_mangle]
  1165. pub unsafe fn saturate_sw_to_sb(v: i32) -> u8 {
  1166. dbg_assert!(v as u32 & 0xFFFF_0000 == 0);
  1167. let mut ret: i32 = v;
  1168. if ret > 65408 {
  1169. ret = ret & 255
  1170. }
  1171. else if ret > 32767 {
  1172. ret = 128
  1173. }
  1174. else if ret > 127 {
  1175. ret = 127
  1176. }
  1177. dbg_assert!(ret as u32 & 0xFFFF_FF00 == 0);
  1178. return ret as u8;
  1179. }
  1180. #[no_mangle]
  1181. pub unsafe fn saturate_sd_to_sw(v: u32) -> u16 {
  1182. let mut ret: u32 = v;
  1183. if ret > 4294934528 {
  1184. ret = ret & 0xFFFF
  1185. }
  1186. else if ret > 0x7FFFFFFF {
  1187. ret = 32768
  1188. }
  1189. else if ret > 32767 {
  1190. ret = 32767
  1191. }
  1192. dbg_assert!(ret & 0xFFFF_0000 == 0);
  1193. return ret as u16;
  1194. }
  1195. #[no_mangle]
  1196. pub unsafe fn saturate_sd_to_sb(v: u32) -> i8 {
  1197. let mut ret: u32 = v;
  1198. if ret > 0xFFFFFF80 {
  1199. ret = ret & 255
  1200. }
  1201. else if ret > 0x7FFFFFFF {
  1202. ret = 128
  1203. }
  1204. else if ret > 127 {
  1205. ret = 127
  1206. }
  1207. dbg_assert!(ret & 0xFFFF_FF00 == 0);
  1208. return ret as i8;
  1209. }
  1210. #[no_mangle]
  1211. pub unsafe fn saturate_sd_to_ub(v: i32) -> i32 {
  1212. let mut ret: i32 = v;
  1213. if ret < 0 {
  1214. ret = 0
  1215. }
  1216. dbg_assert!(ret as u32 & 0xFFFF_FF00 == 0);
  1217. return ret;
  1218. }
  1219. #[no_mangle]
  1220. pub unsafe fn saturate_ud_to_ub(v: u32) -> u8 {
  1221. let mut ret: u32 = v;
  1222. if ret > 255 {
  1223. ret = 255
  1224. }
  1225. dbg_assert!(ret & 0xFFFF_FF00 == 0);
  1226. return ret as u8;
  1227. }
  1228. #[no_mangle]
  1229. pub unsafe fn saturate_uw(v: u32) -> u16 {
  1230. let mut ret: u32 = v;
  1231. if ret > 0x7FFFFFFF {
  1232. ret = 0
  1233. }
  1234. else if ret > 0xFFFF {
  1235. ret = 0xFFFF
  1236. }
  1237. dbg_assert!(ret & 0xFFFF_0000 == 0);
  1238. return ret as u16;
  1239. }