12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447 |
- /****************************************************************************
- *
- * Realmode X86 Emulator Library
- *
- * Copyright (C) 1991-2004 SciTech Software, Inc.
- * Copyright (C) David Mosberger-Tang
- * Copyright (C) 1999 Egbert Eich
- *
- * ========================================================================
- *
- * Permission to use, copy, modify, distribute, and sell this software and
- * its documentation for any purpose is hereby granted without fee,
- * provided that the above copyright notice appear in all copies and that
- * both that copyright notice and this permission notice appear in
- * supporting documentation, and that the name of the authors not be used
- * in advertising or publicity pertaining to distribution of the software
- * without specific, written prior permission. The authors makes no
- * representations about the suitability of this software for any purpose.
- * It is provided "as is" without express or implied warranty.
- *
- * THE AUTHORS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
- * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
- * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
- * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
- * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
- * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
- * PERFORMANCE OF THIS SOFTWARE.
- *
- * ========================================================================
- *
- * Language: ANSI C
- * Environment: Any
- * Developer: Kendall Bennett
- *
- * Description: This file contains the code to implement the primitive
- * machine operations used by the emulation code in ops.c
- *
- * Carry Chain Calculation
- *
- * This represents a somewhat expensive calculation which is
- * apparently required to emulate the setting of the OF343364 and AF flag.
- * The latter is not so important, but the former is. The overflow
- * flag is the XOR of the top two bits of the carry chain for an
- * addition (similar for subtraction). Since we do not want to
- * simulate the addition in a bitwise manner, we try to calculate the
- * carry chain given the two operands and the result.
- *
- * So, given the following table, which represents the addition of two
- * bits, we can derive a formula for the carry chain.
- *
- * a b cin r cout
- * 0 0 0 0 0
- * 0 0 1 1 0
- * 0 1 0 1 0
- * 0 1 1 0 1
- * 1 0 0 1 0
- * 1 0 1 0 1
- * 1 1 0 0 1
- * 1 1 1 1 1
- *
- * Construction of table for cout:
- *
- * ab
- * r \ 00 01 11 10
- * |------------------
- * 0 | 0 1 1 1
- * 1 | 0 0 1 0
- *
- * By inspection, one gets: cc = ab + r'(a + b)
- *
- * That represents alot of operations, but NO CHOICE....
- *
- * Borrow Chain Calculation.
- *
- * The following table represents the subtraction of two bits, from
- * which we can derive a formula for the borrow chain.
- *
- * a b bin r bout
- * 0 0 0 0 0
- * 0 0 1 1 1
- * 0 1 0 1 1
- * 0 1 1 0 1
- * 1 0 0 1 0
- * 1 0 1 0 0
- * 1 1 0 0 0
- * 1 1 1 1 1
- *
- * Construction of table for cout:
- *
- * ab
- * r \ 00 01 11 10
- * |------------------
- * 0 | 0 1 0 0
- * 1 | 1 1 1 0
- *
- * By inspection, one gets: bc = a'b + r(a' + b)
- *
- ****************************************************************************/
- #include <common.h>
- #define PRIM_OPS_NO_REDEFINE_ASM
- #include "x86emu/x86emui.h"
- /*------------------------- Global Variables ------------------------------*/
- static u32 x86emu_parity_tab[8] =
- {
- 0x96696996,
- 0x69969669,
- 0x69969669,
- 0x96696996,
- 0x69969669,
- 0x96696996,
- 0x96696996,
- 0x69969669,
- };
- #define PARITY(x) (((x86emu_parity_tab[(x) / 32] >> ((x) % 32)) & 1) == 0)
- #define XOR2(x) (((x) ^ ((x)>>1)) & 0x1)
- /*----------------------------- Implementation ----------------------------*/
- int abs(int v)
- {
- return (v>0)?v:-v;
- }
- /*----------------------------- Implementation ----------------------------*/
- /*--------- Side effects helper functions -------*/
- /****************************************************************************
- REMARKS:
- implements side efects for byte operations that don't overflow
- ****************************************************************************/
- static void set_parity_flag(u32 res)
- {
- CONDITIONAL_SET_FLAG(PARITY(res & 0xFF), F_PF);
- }
- static void set_szp_flags_8(u8 res)
- {
- CONDITIONAL_SET_FLAG(res & 0x80, F_SF);
- CONDITIONAL_SET_FLAG(res == 0, F_ZF);
- set_parity_flag(res);
- }
- static void set_szp_flags_16(u16 res)
- {
- CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
- CONDITIONAL_SET_FLAG(res == 0, F_ZF);
- set_parity_flag(res);
- }
- static void set_szp_flags_32(u32 res)
- {
- CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
- CONDITIONAL_SET_FLAG(res == 0, F_ZF);
- set_parity_flag(res);
- }
- static void no_carry_byte_side_eff(u8 res)
- {
- CLEAR_FLAG(F_OF);
- CLEAR_FLAG(F_CF);
- CLEAR_FLAG(F_AF);
- set_szp_flags_8(res);
- }
- static void no_carry_word_side_eff(u16 res)
- {
- CLEAR_FLAG(F_OF);
- CLEAR_FLAG(F_CF);
- CLEAR_FLAG(F_AF);
- set_szp_flags_16(res);
- }
- static void no_carry_long_side_eff(u32 res)
- {
- CLEAR_FLAG(F_OF);
- CLEAR_FLAG(F_CF);
- CLEAR_FLAG(F_AF);
- set_szp_flags_32(res);
- }
- static void calc_carry_chain(int bits, u32 d, u32 s, u32 res, int set_carry)
- {
- u32 cc;
- cc = (s & d) | ((~res) & (s | d));
- CONDITIONAL_SET_FLAG(XOR2(cc >> (bits - 2)), F_OF);
- CONDITIONAL_SET_FLAG(cc & 0x8, F_AF);
- if (set_carry) {
- CONDITIONAL_SET_FLAG(res & (1 << bits), F_CF);
- }
- }
- static void calc_borrow_chain(int bits, u32 d, u32 s, u32 res, int set_carry)
- {
- u32 bc;
- bc = (res & (~d | s)) | (~d & s);
- CONDITIONAL_SET_FLAG(XOR2(bc >> (bits - 2)), F_OF);
- CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
- if (set_carry) {
- CONDITIONAL_SET_FLAG(bc & (1 << (bits - 1)), F_CF);
- }
- }
- /****************************************************************************
- REMARKS:
- Implements the AAA instruction and side effects.
- ****************************************************************************/
- u16 aaa_word(u16 d)
- {
- u16 res;
- if ((d & 0xf) > 0x9 || ACCESS_FLAG(F_AF)) {
- d += 0x6;
- d += 0x100;
- SET_FLAG(F_AF);
- SET_FLAG(F_CF);
- } else {
- CLEAR_FLAG(F_CF);
- CLEAR_FLAG(F_AF);
- }
- res = (u16)(d & 0xFF0F);
- set_szp_flags_16(res);
- return res;
- }
- /****************************************************************************
- REMARKS:
- Implements the AAA instruction and side effects.
- ****************************************************************************/
- u16 aas_word(u16 d)
- {
- u16 res;
- if ((d & 0xf) > 0x9 || ACCESS_FLAG(F_AF)) {
- d -= 0x6;
- d -= 0x100;
- SET_FLAG(F_AF);
- SET_FLAG(F_CF);
- } else {
- CLEAR_FLAG(F_CF);
- CLEAR_FLAG(F_AF);
- }
- res = (u16)(d & 0xFF0F);
- set_szp_flags_16(res);
- return res;
- }
- /****************************************************************************
- REMARKS:
- Implements the AAD instruction and side effects.
- ****************************************************************************/
- u16 aad_word(u16 d)
- {
- u16 l;
- u8 hb, lb;
- hb = (u8)((d >> 8) & 0xff);
- lb = (u8)((d & 0xff));
- l = (u16)((lb + 10 * hb) & 0xFF);
- no_carry_byte_side_eff(l & 0xFF);
- return l;
- }
- /****************************************************************************
- REMARKS:
- Implements the AAM instruction and side effects.
- ****************************************************************************/
- u16 aam_word(u8 d)
- {
- u16 h, l;
- h = (u16)(d / 10);
- l = (u16)(d % 10);
- l |= (u16)(h << 8);
- no_carry_byte_side_eff(l & 0xFF);
- return l;
- }
- /****************************************************************************
- REMARKS:
- Implements the ADC instruction and side effects.
- ****************************************************************************/
- u8 adc_byte(u8 d, u8 s)
- {
- u32 res; /* all operands in native machine order */
- res = d + s;
- if (ACCESS_FLAG(F_CF)) res++;
- set_szp_flags_8(res);
- calc_carry_chain(8,s,d,res,1);
- return (u8)res;
- }
- /****************************************************************************
- REMARKS:
- Implements the ADC instruction and side effects.
- ****************************************************************************/
- u16 adc_word(u16 d, u16 s)
- {
- u32 res; /* all operands in native machine order */
- res = d + s;
- if (ACCESS_FLAG(F_CF))
- res++;
- set_szp_flags_16((u16)res);
- calc_carry_chain(16,s,d,res,1);
- return (u16)res;
- }
- /****************************************************************************
- REMARKS:
- Implements the ADC instruction and side effects.
- ****************************************************************************/
- u32 adc_long(u32 d, u32 s)
- {
- u32 lo; /* all operands in native machine order */
- u32 hi;
- u32 res;
- lo = (d & 0xFFFF) + (s & 0xFFFF);
- res = d + s;
- if (ACCESS_FLAG(F_CF)) {
- lo++;
- res++;
- }
- hi = (lo >> 16) + (d >> 16) + (s >> 16);
- set_szp_flags_32(res);
- calc_carry_chain(32,s,d,res,0);
- CONDITIONAL_SET_FLAG(hi & 0x10000, F_CF);
- return res;
- }
- /****************************************************************************
- REMARKS:
- Implements the ADD instruction and side effects.
- ****************************************************************************/
- u8 add_byte(u8 d, u8 s)
- {
- u32 res; /* all operands in native machine order */
- res = d + s;
- set_szp_flags_8((u8)res);
- calc_carry_chain(8,s,d,res,1);
- return (u8)res;
- }
- /****************************************************************************
- REMARKS:
- Implements the ADD instruction and side effects.
- ****************************************************************************/
- u16 add_word(u16 d, u16 s)
- {
- u32 res; /* all operands in native machine order */
- res = d + s;
- set_szp_flags_16((u16)res);
- calc_carry_chain(16,s,d,res,1);
- return (u16)res;
- }
- /****************************************************************************
- REMARKS:
- Implements the ADD instruction and side effects.
- ****************************************************************************/
- u32 add_long(u32 d, u32 s)
- {
- u32 res;
- res = d + s;
- set_szp_flags_32(res);
- calc_carry_chain(32,s,d,res,0);
- CONDITIONAL_SET_FLAG(res < d || res < s, F_CF);
- return res;
- }
- /****************************************************************************
- REMARKS:
- Implements the AND instruction and side effects.
- ****************************************************************************/
- u8 and_byte(u8 d, u8 s)
- {
- u8 res; /* all operands in native machine order */
- res = d & s;
- no_carry_byte_side_eff(res);
- return res;
- }
- /****************************************************************************
- REMARKS:
- Implements the AND instruction and side effects.
- ****************************************************************************/
- u16 and_word(u16 d, u16 s)
- {
- u16 res; /* all operands in native machine order */
- res = d & s;
- no_carry_word_side_eff(res);
- return res;
- }
- /****************************************************************************
- REMARKS:
- Implements the AND instruction and side effects.
- ****************************************************************************/
- u32 and_long(u32 d, u32 s)
- {
- u32 res; /* all operands in native machine order */
- res = d & s;
- no_carry_long_side_eff(res);
- return res;
- }
- /****************************************************************************
- REMARKS:
- Implements the CMP instruction and side effects.
- ****************************************************************************/
- u8 cmp_byte(u8 d, u8 s)
- {
- u32 res; /* all operands in native machine order */
- res = d - s;
- set_szp_flags_8((u8)res);
- calc_borrow_chain(8, d, s, res, 1);
- return d;
- }
- /****************************************************************************
- REMARKS:
- Implements the CMP instruction and side effects.
- ****************************************************************************/
- u16 cmp_word(u16 d, u16 s)
- {
- u32 res; /* all operands in native machine order */
- res = d - s;
- set_szp_flags_16((u16)res);
- calc_borrow_chain(16, d, s, res, 1);
- return d;
- }
- /****************************************************************************
- REMARKS:
- Implements the CMP instruction and side effects.
- ****************************************************************************/
- u32 cmp_long(u32 d, u32 s)
- {
- u32 res; /* all operands in native machine order */
- res = d - s;
- set_szp_flags_32(res);
- calc_borrow_chain(32, d, s, res, 1);
- return d;
- }
- /****************************************************************************
- REMARKS:
- Implements the DAA instruction and side effects.
- ****************************************************************************/
- u8 daa_byte(u8 d)
- {
- u32 res = d;
- if ((d & 0xf) > 9 || ACCESS_FLAG(F_AF)) {
- res += 6;
- SET_FLAG(F_AF);
- }
- if (res > 0x9F || ACCESS_FLAG(F_CF)) {
- res += 0x60;
- SET_FLAG(F_CF);
- }
- set_szp_flags_8((u8)res);
- return (u8)res;
- }
- /****************************************************************************
- REMARKS:
- Implements the DAS instruction and side effects.
- ****************************************************************************/
- u8 das_byte(u8 d)
- {
- if ((d & 0xf) > 9 || ACCESS_FLAG(F_AF)) {
- d -= 6;
- SET_FLAG(F_AF);
- }
- if (d > 0x9F || ACCESS_FLAG(F_CF)) {
- d -= 0x60;
- SET_FLAG(F_CF);
- }
- set_szp_flags_8(d);
- return d;
- }
- /****************************************************************************
- REMARKS:
- Implements the DEC instruction and side effects.
- ****************************************************************************/
- u8 dec_byte(u8 d)
- {
- u32 res; /* all operands in native machine order */
- res = d - 1;
- set_szp_flags_8((u8)res);
- calc_borrow_chain(8, d, 1, res, 0);
- return (u8)res;
- }
- /****************************************************************************
- REMARKS:
- Implements the DEC instruction and side effects.
- ****************************************************************************/
- u16 dec_word(u16 d)
- {
- u32 res; /* all operands in native machine order */
- res = d - 1;
- set_szp_flags_16((u16)res);
- calc_borrow_chain(16, d, 1, res, 0);
- return (u16)res;
- }
- /****************************************************************************
- REMARKS:
- Implements the DEC instruction and side effects.
- ****************************************************************************/
- u32 dec_long(u32 d)
- {
- u32 res; /* all operands in native machine order */
- res = d - 1;
- set_szp_flags_32(res);
- calc_borrow_chain(32, d, 1, res, 0);
- return res;
- }
- /****************************************************************************
- REMARKS:
- Implements the INC instruction and side effects.
- ****************************************************************************/
- u8 inc_byte(u8 d)
- {
- u32 res; /* all operands in native machine order */
- res = d + 1;
- set_szp_flags_8((u8)res);
- calc_carry_chain(8, d, 1, res, 0);
- return (u8)res;
- }
- /****************************************************************************
- REMARKS:
- Implements the INC instruction and side effects.
- ****************************************************************************/
- u16 inc_word(u16 d)
- {
- u32 res; /* all operands in native machine order */
- res = d + 1;
- set_szp_flags_16((u16)res);
- calc_carry_chain(16, d, 1, res, 0);
- return (u16)res;
- }
- /****************************************************************************
- REMARKS:
- Implements the INC instruction and side effects.
- ****************************************************************************/
- u32 inc_long(u32 d)
- {
- u32 res; /* all operands in native machine order */
- res = d + 1;
- set_szp_flags_32(res);
- calc_carry_chain(32, d, 1, res, 0);
- return res;
- }
- /****************************************************************************
- REMARKS:
- Implements the OR instruction and side effects.
- ****************************************************************************/
- u8 or_byte(u8 d, u8 s)
- {
- u8 res; /* all operands in native machine order */
- res = d | s;
- no_carry_byte_side_eff(res);
- return res;
- }
- /****************************************************************************
- REMARKS:
- Implements the OR instruction and side effects.
- ****************************************************************************/
- u16 or_word(u16 d, u16 s)
- {
- u16 res; /* all operands in native machine order */
- res = d | s;
- no_carry_word_side_eff(res);
- return res;
- }
- /****************************************************************************
- REMARKS:
- Implements the OR instruction and side effects.
- ****************************************************************************/
- u32 or_long(u32 d, u32 s)
- {
- u32 res; /* all operands in native machine order */
- res = d | s;
- no_carry_long_side_eff(res);
- return res;
- }
- /****************************************************************************
- REMARKS:
- Implements the OR instruction and side effects.
- ****************************************************************************/
- u8 neg_byte(u8 s)
- {
- u8 res;
- CONDITIONAL_SET_FLAG(s != 0, F_CF);
- res = (u8)-s;
- set_szp_flags_8(res);
- calc_borrow_chain(8, 0, s, res, 0);
- return res;
- }
- /****************************************************************************
- REMARKS:
- Implements the OR instruction and side effects.
- ****************************************************************************/
- u16 neg_word(u16 s)
- {
- u16 res;
- CONDITIONAL_SET_FLAG(s != 0, F_CF);
- res = (u16)-s;
- set_szp_flags_16((u16)res);
- calc_borrow_chain(16, 0, s, res, 0);
- return res;
- }
- /****************************************************************************
- REMARKS:
- Implements the OR instruction and side effects.
- ****************************************************************************/
- u32 neg_long(u32 s)
- {
- u32 res;
- CONDITIONAL_SET_FLAG(s != 0, F_CF);
- res = (u32)-s;
- set_szp_flags_32(res);
- calc_borrow_chain(32, 0, s, res, 0);
- return res;
- }
- /****************************************************************************
- REMARKS:
- Implements the NOT instruction and side effects.
- ****************************************************************************/
- u8 not_byte(u8 s)
- {
- return ~s;
- }
- /****************************************************************************
- REMARKS:
- Implements the NOT instruction and side effects.
- ****************************************************************************/
- u16 not_word(u16 s)
- {
- return ~s;
- }
- /****************************************************************************
- REMARKS:
- Implements the NOT instruction and side effects.
- ****************************************************************************/
- u32 not_long(u32 s)
- {
- return ~s;
- }
- /****************************************************************************
- REMARKS:
- Implements the RCL instruction and side effects.
- ****************************************************************************/
- u8 rcl_byte(u8 d, u8 s)
- {
- unsigned int res, cnt, mask, cf;
- /* s is the rotate distance. It varies from 0 - 8. */
- /* have
- CF B_7 B_6 B_5 B_4 B_3 B_2 B_1 B_0
- want to rotate through the carry by "s" bits. We could
- loop, but that's inefficient. So the width is 9,
- and we split into three parts:
- The new carry flag (was B_n)
- the stuff in B_n-1 .. B_0
- the stuff in B_7 .. B_n+1
- The new rotate is done mod 9, and given this,
- for a rotation of n bits (mod 9) the new carry flag is
- then located n bits from the MSB. The low part is
- then shifted up cnt bits, and the high part is or'd
- in. Using CAPS for new values, and lowercase for the
- original values, this can be expressed as:
- IF n > 0
- 1) CF <- b_(8-n)
- 2) B_(7) .. B_(n) <- b_(8-(n+1)) .. b_0
- 3) B_(n-1) <- cf
- 4) B_(n-2) .. B_0 <- b_7 .. b_(8-(n-1))
- */
- res = d;
- if ((cnt = s % 9) != 0) {
- /* extract the new CARRY FLAG. */
- /* CF <- b_(8-n) */
- cf = (d >> (8 - cnt)) & 0x1;
- /* get the low stuff which rotated
- into the range B_7 .. B_cnt */
- /* B_(7) .. B_(n) <- b_(8-(n+1)) .. b_0 */
- /* note that the right hand side done by the mask */
- res = (d << cnt) & 0xff;
- /* now the high stuff which rotated around
- into the positions B_cnt-2 .. B_0 */
- /* B_(n-2) .. B_0 <- b_7 .. b_(8-(n-1)) */
- /* shift it downward, 7-(n-2) = 9-n positions.
- and mask off the result before or'ing in.
- */
- mask = (1 << (cnt - 1)) - 1;
- res |= (d >> (9 - cnt)) & mask;
- /* if the carry flag was set, or it in. */
- if (ACCESS_FLAG(F_CF)) { /* carry flag is set */
- /* B_(n-1) <- cf */
- res |= 1 << (cnt - 1);
- }
- /* set the new carry flag, based on the variable "cf" */
- CONDITIONAL_SET_FLAG(cf, F_CF);
- /* OVERFLOW is set *IFF* cnt==1, then it is the
- xor of CF and the most significant bit. Blecck. */
- /* parenthesized this expression since it appears to
- be causing OF to be misset */
- CONDITIONAL_SET_FLAG(cnt == 1 && XOR2(cf + ((res >> 6) & 0x2)),
- F_OF);
- }
- return (u8)res;
- }
- /****************************************************************************
- REMARKS:
- Implements the RCL instruction and side effects.
- ****************************************************************************/
- u16 rcl_word(u16 d, u8 s)
- {
- unsigned int res, cnt, mask, cf;
- res = d;
- if ((cnt = s % 17) != 0) {
- cf = (d >> (16 - cnt)) & 0x1;
- res = (d << cnt) & 0xffff;
- mask = (1 << (cnt - 1)) - 1;
- res |= (d >> (17 - cnt)) & mask;
- if (ACCESS_FLAG(F_CF)) {
- res |= 1 << (cnt - 1);
- }
- CONDITIONAL_SET_FLAG(cf, F_CF);
- CONDITIONAL_SET_FLAG(cnt == 1 && XOR2(cf + ((res >> 14) & 0x2)),
- F_OF);
- }
- return (u16)res;
- }
- /****************************************************************************
- REMARKS:
- Implements the RCL instruction and side effects.
- ****************************************************************************/
- u32 rcl_long(u32 d, u8 s)
- {
- u32 res, cnt, mask, cf;
- res = d;
- if ((cnt = s % 33) != 0) {
- cf = (d >> (32 - cnt)) & 0x1;
- res = (d << cnt) & 0xffffffff;
- mask = (1 << (cnt - 1)) - 1;
- res |= (d >> (33 - cnt)) & mask;
- if (ACCESS_FLAG(F_CF)) { /* carry flag is set */
- res |= 1 << (cnt - 1);
- }
- CONDITIONAL_SET_FLAG(cf, F_CF);
- CONDITIONAL_SET_FLAG(cnt == 1 && XOR2(cf + ((res >> 30) & 0x2)),
- F_OF);
- }
- return res;
- }
- /****************************************************************************
- REMARKS:
- Implements the RCR instruction and side effects.
- ****************************************************************************/
- u8 rcr_byte(u8 d, u8 s)
- {
- u32 res, cnt;
- u32 mask, cf, ocf = 0;
- /* rotate right through carry */
- /*
- s is the rotate distance. It varies from 0 - 8.
- d is the byte object rotated.
- have
- CF B_7 B_6 B_5 B_4 B_3 B_2 B_1 B_0
- The new rotate is done mod 9, and given this,
- for a rotation of n bits (mod 9) the new carry flag is
- then located n bits from the LSB. The low part is
- then shifted up cnt bits, and the high part is or'd
- in. Using CAPS for new values, and lowercase for the
- original values, this can be expressed as:
- IF n > 0
- 1) CF <- b_(n-1)
- 2) B_(8-(n+1)) .. B_(0) <- b_(7) .. b_(n)
- 3) B_(8-n) <- cf
- 4) B_(7) .. B_(8-(n-1)) <- b_(n-2) .. b_(0)
- */
- res = d;
- if ((cnt = s % 9) != 0) {
- /* extract the new CARRY FLAG. */
- /* CF <- b_(n-1) */
- if (cnt == 1) {
- cf = d & 0x1;
- /* note hackery here. Access_flag(..) evaluates to either
- 0 if flag not set
- non-zero if flag is set.
- doing access_flag(..) != 0 casts that into either
- 0..1 in any representation of the flags register
- (i.e. packed bit array or unpacked.)
- */
- ocf = ACCESS_FLAG(F_CF) != 0;
- } else
- cf = (d >> (cnt - 1)) & 0x1;
- /* B_(8-(n+1)) .. B_(0) <- b_(7) .. b_n */
- /* note that the right hand side done by the mask
- This is effectively done by shifting the
- object to the right. The result must be masked,
- in case the object came in and was treated
- as a negative number. Needed??? */
- mask = (1 << (8 - cnt)) - 1;
- res = (d >> cnt) & mask;
- /* now the high stuff which rotated around
- into the positions B_cnt-2 .. B_0 */
- /* B_(7) .. B_(8-(n-1)) <- b_(n-2) .. b_(0) */
- /* shift it downward, 7-(n-2) = 9-n positions.
- and mask off the result before or'ing in.
- */
- res |= (d << (9 - cnt));
- /* if the carry flag was set, or it in. */
- if (ACCESS_FLAG(F_CF)) { /* carry flag is set */
- /* B_(8-n) <- cf */
- res |= 1 << (8 - cnt);
- }
- /* set the new carry flag, based on the variable "cf" */
- CONDITIONAL_SET_FLAG(cf, F_CF);
- /* OVERFLOW is set *IFF* cnt==1, then it is the
- xor of CF and the most significant bit. Blecck. */
- /* parenthesized... */
- if (cnt == 1) {
- CONDITIONAL_SET_FLAG(XOR2(ocf + ((d >> 6) & 0x2)),
- F_OF);
- }
- }
- return (u8)res;
- }
- /****************************************************************************
- REMARKS:
- Implements the RCR instruction and side effects.
- ****************************************************************************/
- u16 rcr_word(u16 d, u8 s)
- {
- u32 res, cnt;
- u32 mask, cf, ocf = 0;
- /* rotate right through carry */
- res = d;
- if ((cnt = s % 17) != 0) {
- if (cnt == 1) {
- cf = d & 0x1;
- ocf = ACCESS_FLAG(F_CF) != 0;
- } else
- cf = (d >> (cnt - 1)) & 0x1;
- mask = (1 << (16 - cnt)) - 1;
- res = (d >> cnt) & mask;
- res |= (d << (17 - cnt));
- if (ACCESS_FLAG(F_CF)) {
- res |= 1 << (16 - cnt);
- }
- CONDITIONAL_SET_FLAG(cf, F_CF);
- if (cnt == 1) {
- CONDITIONAL_SET_FLAG(XOR2(ocf + ((d >> 14) & 0x2)),
- F_OF);
- }
- }
- return (u16)res;
- }
- /****************************************************************************
- REMARKS:
- Implements the RCR instruction and side effects.
- ****************************************************************************/
- u32 rcr_long(u32 d, u8 s)
- {
- u32 res, cnt;
- u32 mask, cf, ocf = 0;
- /* rotate right through carry */
- res = d;
- if ((cnt = s % 33) != 0) {
- if (cnt == 1) {
- cf = d & 0x1;
- ocf = ACCESS_FLAG(F_CF) != 0;
- } else
- cf = (d >> (cnt - 1)) & 0x1;
- mask = (1 << (32 - cnt)) - 1;
- res = (d >> cnt) & mask;
- if (cnt != 1)
- res |= (d << (33 - cnt));
- if (ACCESS_FLAG(F_CF)) { /* carry flag is set */
- res |= 1 << (32 - cnt);
- }
- CONDITIONAL_SET_FLAG(cf, F_CF);
- if (cnt == 1) {
- CONDITIONAL_SET_FLAG(XOR2(ocf + ((d >> 30) & 0x2)),
- F_OF);
- }
- }
- return res;
- }
- /****************************************************************************
- REMARKS:
- Implements the ROL instruction and side effects.
- ****************************************************************************/
- u8 rol_byte(u8 d, u8 s)
- {
- unsigned int res, cnt, mask;
- /* rotate left */
- /*
- s is the rotate distance. It varies from 0 - 8.
- d is the byte object rotated.
- have
- CF B_7 ... B_0
- The new rotate is done mod 8.
- Much simpler than the "rcl" or "rcr" operations.
- IF n > 0
- 1) B_(7) .. B_(n) <- b_(8-(n+1)) .. b_(0)
- 2) B_(n-1) .. B_(0) <- b_(7) .. b_(8-n)
- */
- res = d;
- if ((cnt = s % 8) != 0) {
- /* B_(7) .. B_(n) <- b_(8-(n+1)) .. b_(0) */
- res = (d << cnt);
- /* B_(n-1) .. B_(0) <- b_(7) .. b_(8-n) */
- mask = (1 << cnt) - 1;
- res |= (d >> (8 - cnt)) & mask;
- /* set the new carry flag, Note that it is the low order
- bit of the result!!! */
- CONDITIONAL_SET_FLAG(res & 0x1, F_CF);
- /* OVERFLOW is set *IFF* s==1, then it is the
- xor of CF and the most significant bit. Blecck. */
- CONDITIONAL_SET_FLAG(s == 1 &&
- XOR2((res & 0x1) + ((res >> 6) & 0x2)),
- F_OF);
- } if (s != 0) {
- /* set the new carry flag, Note that it is the low order
- bit of the result!!! */
- CONDITIONAL_SET_FLAG(res & 0x1, F_CF);
- }
- return (u8)res;
- }
- /****************************************************************************
- REMARKS:
- Implements the ROL instruction and side effects.
- ****************************************************************************/
- u16 rol_word(u16 d, u8 s)
- {
- unsigned int res, cnt, mask;
- res = d;
- if ((cnt = s % 16) != 0) {
- res = (d << cnt);
- mask = (1 << cnt) - 1;
- res |= (d >> (16 - cnt)) & mask;
- CONDITIONAL_SET_FLAG(res & 0x1, F_CF);
- CONDITIONAL_SET_FLAG(s == 1 &&
- XOR2((res & 0x1) + ((res >> 14) & 0x2)),
- F_OF);
- } if (s != 0) {
- /* set the new carry flag, Note that it is the low order
- bit of the result!!! */
- CONDITIONAL_SET_FLAG(res & 0x1, F_CF);
- }
- return (u16)res;
- }
- /****************************************************************************
- REMARKS:
- Implements the ROL instruction and side effects.
- ****************************************************************************/
- u32 rol_long(u32 d, u8 s)
- {
- u32 res, cnt, mask;
- res = d;
- if ((cnt = s % 32) != 0) {
- res = (d << cnt);
- mask = (1 << cnt) - 1;
- res |= (d >> (32 - cnt)) & mask;
- CONDITIONAL_SET_FLAG(res & 0x1, F_CF);
- CONDITIONAL_SET_FLAG(s == 1 &&
- XOR2((res & 0x1) + ((res >> 30) & 0x2)),
- F_OF);
- } if (s != 0) {
- /* set the new carry flag, Note that it is the low order
- bit of the result!!! */
- CONDITIONAL_SET_FLAG(res & 0x1, F_CF);
- }
- return res;
- }
- /****************************************************************************
- REMARKS:
- Implements the ROR instruction and side effects.
- ****************************************************************************/
- u8 ror_byte(u8 d, u8 s)
- {
- unsigned int res, cnt, mask;
- /* rotate right */
- /*
- s is the rotate distance. It varies from 0 - 8.
- d is the byte object rotated.
- have
- B_7 ... B_0
- The rotate is done mod 8.
- IF n > 0
- 1) B_(8-(n+1)) .. B_(0) <- b_(7) .. b_(n)
- 2) B_(7) .. B_(8-n) <- b_(n-1) .. b_(0)
- */
- res = d;
- if ((cnt = s % 8) != 0) { /* not a typo, do nada if cnt==0 */
- /* B_(7) .. B_(8-n) <- b_(n-1) .. b_(0) */
- res = (d << (8 - cnt));
- /* B_(8-(n+1)) .. B_(0) <- b_(7) .. b_(n) */
- mask = (1 << (8 - cnt)) - 1;
- res |= (d >> (cnt)) & mask;
- /* set the new carry flag, Note that it is the low order
- bit of the result!!! */
- CONDITIONAL_SET_FLAG(res & 0x80, F_CF);
- /* OVERFLOW is set *IFF* s==1, then it is the
- xor of the two most significant bits. Blecck. */
- CONDITIONAL_SET_FLAG(s == 1 && XOR2(res >> 6), F_OF);
- } else if (s != 0) {
- /* set the new carry flag, Note that it is the low order
- bit of the result!!! */
- CONDITIONAL_SET_FLAG(res & 0x80, F_CF);
- }
- return (u8)res;
- }
- /****************************************************************************
- REMARKS:
- Implements the ROR instruction and side effects.
- ****************************************************************************/
- u16 ror_word(u16 d, u8 s)
- {
- unsigned int res, cnt, mask;
- res = d;
- if ((cnt = s % 16) != 0) {
- res = (d << (16 - cnt));
- mask = (1 << (16 - cnt)) - 1;
- res |= (d >> (cnt)) & mask;
- CONDITIONAL_SET_FLAG(res & 0x8000, F_CF);
- CONDITIONAL_SET_FLAG(s == 1 && XOR2(res >> 14), F_OF);
- } else if (s != 0) {
- /* set the new carry flag, Note that it is the low order
- bit of the result!!! */
- CONDITIONAL_SET_FLAG(res & 0x8000, F_CF);
- }
- return (u16)res;
- }
- /****************************************************************************
- REMARKS:
- Implements the ROR instruction and side effects.
- ****************************************************************************/
- u32 ror_long(u32 d, u8 s)
- {
- u32 res, cnt, mask;
- res = d;
- if ((cnt = s % 32) != 0) {
- res = (d << (32 - cnt));
- mask = (1 << (32 - cnt)) - 1;
- res |= (d >> (cnt)) & mask;
- CONDITIONAL_SET_FLAG(res & 0x80000000, F_CF);
- CONDITIONAL_SET_FLAG(s == 1 && XOR2(res >> 30), F_OF);
- } else if (s != 0) {
- /* set the new carry flag, Note that it is the low order
- bit of the result!!! */
- CONDITIONAL_SET_FLAG(res & 0x80000000, F_CF);
- }
- return res;
- }
- /****************************************************************************
- REMARKS:
- Implements the SHL instruction and side effects.
- ****************************************************************************/
- u8 shl_byte(u8 d, u8 s)
- {
- unsigned int cnt, res, cf;
- if (s < 8) {
- cnt = s % 8;
- /* last bit shifted out goes into carry flag */
- if (cnt > 0) {
- res = d << cnt;
- cf = d & (1 << (8 - cnt));
- CONDITIONAL_SET_FLAG(cf, F_CF);
- set_szp_flags_8((u8)res);
- } else {
- res = (u8) d;
- }
- if (cnt == 1) {
- /* Needs simplification. */
- CONDITIONAL_SET_FLAG(
- (((res & 0x80) == 0x80) ^
- (ACCESS_FLAG(F_CF) != 0)),
- /* was (M.x86.R_FLG&F_CF)==F_CF)), */
- F_OF);
- } else {
- CLEAR_FLAG(F_OF);
- }
- } else {
- res = 0;
- CONDITIONAL_SET_FLAG((d << (s-1)) & 0x80, F_CF);
- CLEAR_FLAG(F_OF);
- CLEAR_FLAG(F_SF);
- SET_FLAG(F_PF);
- SET_FLAG(F_ZF);
- }
- return (u8)res;
- }
- /****************************************************************************
- REMARKS:
- Implements the SHL instruction and side effects.
- ****************************************************************************/
- u16 shl_word(u16 d, u8 s)
- {
- unsigned int cnt, res, cf;
- if (s < 16) {
- cnt = s % 16;
- if (cnt > 0) {
- res = d << cnt;
- cf = d & (1 << (16 - cnt));
- CONDITIONAL_SET_FLAG(cf, F_CF);
- set_szp_flags_16((u16)res);
- } else {
- res = (u16) d;
- }
- if (cnt == 1) {
- CONDITIONAL_SET_FLAG(
- (((res & 0x8000) == 0x8000) ^
- (ACCESS_FLAG(F_CF) != 0)),
- F_OF);
- } else {
- CLEAR_FLAG(F_OF);
- }
- } else {
- res = 0;
- CONDITIONAL_SET_FLAG((d << (s-1)) & 0x8000, F_CF);
- CLEAR_FLAG(F_OF);
- CLEAR_FLAG(F_SF);
- SET_FLAG(F_PF);
- SET_FLAG(F_ZF);
- }
- return (u16)res;
- }
- /****************************************************************************
- REMARKS:
- Implements the SHL instruction and side effects.
- ****************************************************************************/
- u32 shl_long(u32 d, u8 s)
- {
- unsigned int cnt, res, cf;
- if (s < 32) {
- cnt = s % 32;
- if (cnt > 0) {
- res = d << cnt;
- cf = d & (1 << (32 - cnt));
- CONDITIONAL_SET_FLAG(cf, F_CF);
- set_szp_flags_32((u32)res);
- } else {
- res = d;
- }
- if (cnt == 1) {
- CONDITIONAL_SET_FLAG((((res & 0x80000000) == 0x80000000) ^
- (ACCESS_FLAG(F_CF) != 0)), F_OF);
- } else {
- CLEAR_FLAG(F_OF);
- }
- } else {
- res = 0;
- CONDITIONAL_SET_FLAG((d << (s-1)) & 0x80000000, F_CF);
- CLEAR_FLAG(F_OF);
- CLEAR_FLAG(F_SF);
- SET_FLAG(F_PF);
- SET_FLAG(F_ZF);
- }
- return res;
- }
- /****************************************************************************
- REMARKS:
- Implements the SHR instruction and side effects.
- ****************************************************************************/
- u8 shr_byte(u8 d, u8 s)
- {
- unsigned int cnt, res, cf;
- if (s < 8) {
- cnt = s % 8;
- if (cnt > 0) {
- cf = d & (1 << (cnt - 1));
- res = d >> cnt;
- CONDITIONAL_SET_FLAG(cf, F_CF);
- set_szp_flags_8((u8)res);
- } else {
- res = (u8) d;
- }
- if (cnt == 1) {
- CONDITIONAL_SET_FLAG(XOR2(res >> 6), F_OF);
- } else {
- CLEAR_FLAG(F_OF);
- }
- } else {
- res = 0;
- CONDITIONAL_SET_FLAG((d >> (s-1)) & 0x1, F_CF);
- CLEAR_FLAG(F_OF);
- CLEAR_FLAG(F_SF);
- SET_FLAG(F_PF);
- SET_FLAG(F_ZF);
- }
- return (u8)res;
- }
- /****************************************************************************
- REMARKS:
- Implements the SHR instruction and side effects.
- ****************************************************************************/
- u16 shr_word(u16 d, u8 s)
- {
- unsigned int cnt, res, cf;
- if (s < 16) {
- cnt = s % 16;
- if (cnt > 0) {
- cf = d & (1 << (cnt - 1));
- res = d >> cnt;
- CONDITIONAL_SET_FLAG(cf, F_CF);
- set_szp_flags_16((u16)res);
- } else {
- res = d;
- }
- if (cnt == 1) {
- CONDITIONAL_SET_FLAG(XOR2(res >> 14), F_OF);
- } else {
- CLEAR_FLAG(F_OF);
- }
- } else {
- res = 0;
- CLEAR_FLAG(F_CF);
- CLEAR_FLAG(F_OF);
- SET_FLAG(F_ZF);
- CLEAR_FLAG(F_SF);
- CLEAR_FLAG(F_PF);
- }
- return (u16)res;
- }
- /****************************************************************************
- REMARKS:
- Implements the SHR instruction and side effects.
- ****************************************************************************/
- u32 shr_long(u32 d, u8 s)
- {
- unsigned int cnt, res, cf;
- if (s < 32) {
- cnt = s % 32;
- if (cnt > 0) {
- cf = d & (1 << (cnt - 1));
- res = d >> cnt;
- CONDITIONAL_SET_FLAG(cf, F_CF);
- set_szp_flags_32((u32)res);
- } else {
- res = d;
- }
- if (cnt == 1) {
- CONDITIONAL_SET_FLAG(XOR2(res >> 30), F_OF);
- } else {
- CLEAR_FLAG(F_OF);
- }
- } else {
- res = 0;
- CLEAR_FLAG(F_CF);
- CLEAR_FLAG(F_OF);
- SET_FLAG(F_ZF);
- CLEAR_FLAG(F_SF);
- CLEAR_FLAG(F_PF);
- }
- return res;
- }
- /****************************************************************************
- REMARKS:
- Implements the SAR instruction and side effects.
- ****************************************************************************/
- u8 sar_byte(u8 d, u8 s)
- {
- unsigned int cnt, res, cf, mask, sf;
- res = d;
- sf = d & 0x80;
- cnt = s % 8;
- if (cnt > 0 && cnt < 8) {
- mask = (1 << (8 - cnt)) - 1;
- cf = d & (1 << (cnt - 1));
- res = (d >> cnt) & mask;
- CONDITIONAL_SET_FLAG(cf, F_CF);
- if (sf) {
- res |= ~mask;
- }
- set_szp_flags_8((u8)res);
- } else if (cnt >= 8) {
- if (sf) {
- res = 0xff;
- SET_FLAG(F_CF);
- CLEAR_FLAG(F_ZF);
- SET_FLAG(F_SF);
- SET_FLAG(F_PF);
- } else {
- res = 0;
- CLEAR_FLAG(F_CF);
- SET_FLAG(F_ZF);
- CLEAR_FLAG(F_SF);
- CLEAR_FLAG(F_PF);
- }
- }
- return (u8)res;
- }
- /****************************************************************************
- REMARKS:
- Implements the SAR instruction and side effects.
- ****************************************************************************/
- u16 sar_word(u16 d, u8 s)
- {
- unsigned int cnt, res, cf, mask, sf;
- sf = d & 0x8000;
- cnt = s % 16;
- res = d;
- if (cnt > 0 && cnt < 16) {
- mask = (1 << (16 - cnt)) - 1;
- cf = d & (1 << (cnt - 1));
- res = (d >> cnt) & mask;
- CONDITIONAL_SET_FLAG(cf, F_CF);
- if (sf) {
- res |= ~mask;
- }
- set_szp_flags_16((u16)res);
- } else if (cnt >= 16) {
- if (sf) {
- res = 0xffff;
- SET_FLAG(F_CF);
- CLEAR_FLAG(F_ZF);
- SET_FLAG(F_SF);
- SET_FLAG(F_PF);
- } else {
- res = 0;
- CLEAR_FLAG(F_CF);
- SET_FLAG(F_ZF);
- CLEAR_FLAG(F_SF);
- CLEAR_FLAG(F_PF);
- }
- }
- return (u16)res;
- }
- /****************************************************************************
- REMARKS:
- Implements the SAR instruction and side effects.
- ****************************************************************************/
- u32 sar_long(u32 d, u8 s)
- {
- u32 cnt, res, cf, mask, sf;
- sf = d & 0x80000000;
- cnt = s % 32;
- res = d;
- if (cnt > 0 && cnt < 32) {
- mask = (1 << (32 - cnt)) - 1;
- cf = d & (1 << (cnt - 1));
- res = (d >> cnt) & mask;
- CONDITIONAL_SET_FLAG(cf, F_CF);
- if (sf) {
- res |= ~mask;
- }
- set_szp_flags_32(res);
- } else if (cnt >= 32) {
- if (sf) {
- res = 0xffffffff;
- SET_FLAG(F_CF);
- CLEAR_FLAG(F_ZF);
- SET_FLAG(F_SF);
- SET_FLAG(F_PF);
- } else {
- res = 0;
- CLEAR_FLAG(F_CF);
- SET_FLAG(F_ZF);
- CLEAR_FLAG(F_SF);
- CLEAR_FLAG(F_PF);
- }
- }
- return res;
- }
- /****************************************************************************
- REMARKS:
- Implements the SHLD instruction and side effects.
- ****************************************************************************/
- u16 shld_word (u16 d, u16 fill, u8 s)
- {
- unsigned int cnt, res, cf;
- if (s < 16) {
- cnt = s % 16;
- if (cnt > 0) {
- res = (d << cnt) | (fill >> (16-cnt));
- cf = d & (1 << (16 - cnt));
- CONDITIONAL_SET_FLAG(cf, F_CF);
- set_szp_flags_16((u16)res);
- } else {
- res = d;
- }
- if (cnt == 1) {
- CONDITIONAL_SET_FLAG((((res & 0x8000) == 0x8000) ^
- (ACCESS_FLAG(F_CF) != 0)), F_OF);
- } else {
- CLEAR_FLAG(F_OF);
- }
- } else {
- res = 0;
- CONDITIONAL_SET_FLAG((d << (s-1)) & 0x8000, F_CF);
- CLEAR_FLAG(F_OF);
- CLEAR_FLAG(F_SF);
- SET_FLAG(F_PF);
- SET_FLAG(F_ZF);
- }
- return (u16)res;
- }
- /****************************************************************************
- REMARKS:
- Implements the SHLD instruction and side effects.
- ****************************************************************************/
- u32 shld_long (u32 d, u32 fill, u8 s)
- {
- unsigned int cnt, res, cf;
- if (s < 32) {
- cnt = s % 32;
- if (cnt > 0) {
- res = (d << cnt) | (fill >> (32-cnt));
- cf = d & (1 << (32 - cnt));
- CONDITIONAL_SET_FLAG(cf, F_CF);
- set_szp_flags_32((u32)res);
- } else {
- res = d;
- }
- if (cnt == 1) {
- CONDITIONAL_SET_FLAG((((res & 0x80000000) == 0x80000000) ^
- (ACCESS_FLAG(F_CF) != 0)), F_OF);
- } else {
- CLEAR_FLAG(F_OF);
- }
- } else {
- res = 0;
- CONDITIONAL_SET_FLAG((d << (s-1)) & 0x80000000, F_CF);
- CLEAR_FLAG(F_OF);
- CLEAR_FLAG(F_SF);
- SET_FLAG(F_PF);
- SET_FLAG(F_ZF);
- }
- return res;
- }
- /****************************************************************************
- REMARKS:
- Implements the SHRD instruction and side effects.
- ****************************************************************************/
- u16 shrd_word (u16 d, u16 fill, u8 s)
- {
- unsigned int cnt, res, cf;
- if (s < 16) {
- cnt = s % 16;
- if (cnt > 0) {
- cf = d & (1 << (cnt - 1));
- res = (d >> cnt) | (fill << (16 - cnt));
- CONDITIONAL_SET_FLAG(cf, F_CF);
- set_szp_flags_16((u16)res);
- } else {
- res = d;
- }
- if (cnt == 1) {
- CONDITIONAL_SET_FLAG(XOR2(res >> 14), F_OF);
- } else {
- CLEAR_FLAG(F_OF);
- }
- } else {
- res = 0;
- CLEAR_FLAG(F_CF);
- CLEAR_FLAG(F_OF);
- SET_FLAG(F_ZF);
- CLEAR_FLAG(F_SF);
- CLEAR_FLAG(F_PF);
- }
- return (u16)res;
- }
- /****************************************************************************
- REMARKS:
- Implements the SHRD instruction and side effects.
- ****************************************************************************/
- u32 shrd_long (u32 d, u32 fill, u8 s)
- {
- unsigned int cnt, res, cf;
- if (s < 32) {
- cnt = s % 32;
- if (cnt > 0) {
- cf = d & (1 << (cnt - 1));
- res = (d >> cnt) | (fill << (32 - cnt));
- CONDITIONAL_SET_FLAG(cf, F_CF);
- set_szp_flags_32((u32)res);
- } else {
- res = d;
- }
- if (cnt == 1) {
- CONDITIONAL_SET_FLAG(XOR2(res >> 30), F_OF);
- } else {
- CLEAR_FLAG(F_OF);
- }
- } else {
- res = 0;
- CLEAR_FLAG(F_CF);
- CLEAR_FLAG(F_OF);
- SET_FLAG(F_ZF);
- CLEAR_FLAG(F_SF);
- CLEAR_FLAG(F_PF);
- }
- return res;
- }
- /****************************************************************************
- REMARKS:
- Implements the SBB instruction and side effects.
- ****************************************************************************/
- u8 sbb_byte(u8 d, u8 s)
- {
- u32 res; /* all operands in native machine order */
- u32 bc;
- if (ACCESS_FLAG(F_CF))
- res = d - s - 1;
- else
- res = d - s;
- set_szp_flags_8((u8)res);
- /* calculate the borrow chain. See note at top */
- bc = (res & (~d | s)) | (~d & s);
- CONDITIONAL_SET_FLAG(bc & 0x80, F_CF);
- CONDITIONAL_SET_FLAG(XOR2(bc >> 6), F_OF);
- CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
- return (u8)res;
- }
- /****************************************************************************
- REMARKS:
- Implements the SBB instruction and side effects.
- ****************************************************************************/
- u16 sbb_word(u16 d, u16 s)
- {
- u32 res; /* all operands in native machine order */
- u32 bc;
- if (ACCESS_FLAG(F_CF))
- res = d - s - 1;
- else
- res = d - s;
- set_szp_flags_16((u16)res);
- /* calculate the borrow chain. See note at top */
- bc = (res & (~d | s)) | (~d & s);
- CONDITIONAL_SET_FLAG(bc & 0x8000, F_CF);
- CONDITIONAL_SET_FLAG(XOR2(bc >> 14), F_OF);
- CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
- return (u16)res;
- }
- /****************************************************************************
- REMARKS:
- Implements the SBB instruction and side effects.
- ****************************************************************************/
- u32 sbb_long(u32 d, u32 s)
- {
- u32 res; /* all operands in native machine order */
- u32 bc;
- if (ACCESS_FLAG(F_CF))
- res = d - s - 1;
- else
- res = d - s;
- set_szp_flags_32(res);
- /* calculate the borrow chain. See note at top */
- bc = (res & (~d | s)) | (~d & s);
- CONDITIONAL_SET_FLAG(bc & 0x80000000, F_CF);
- CONDITIONAL_SET_FLAG(XOR2(bc >> 30), F_OF);
- CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
- return res;
- }
- /****************************************************************************
- REMARKS:
- Implements the SUB instruction and side effects.
- ****************************************************************************/
- u8 sub_byte(u8 d, u8 s)
- {
- u32 res; /* all operands in native machine order */
- u32 bc;
- res = d - s;
- set_szp_flags_8((u8)res);
- /* calculate the borrow chain. See note at top */
- bc = (res & (~d | s)) | (~d & s);
- CONDITIONAL_SET_FLAG(bc & 0x80, F_CF);
- CONDITIONAL_SET_FLAG(XOR2(bc >> 6), F_OF);
- CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
- return (u8)res;
- }
- /****************************************************************************
- REMARKS:
- Implements the SUB instruction and side effects.
- ****************************************************************************/
- u16 sub_word(u16 d, u16 s)
- {
- u32 res; /* all operands in native machine order */
- u32 bc;
- res = d - s;
- set_szp_flags_16((u16)res);
- /* calculate the borrow chain. See note at top */
- bc = (res & (~d | s)) | (~d & s);
- CONDITIONAL_SET_FLAG(bc & 0x8000, F_CF);
- CONDITIONAL_SET_FLAG(XOR2(bc >> 14), F_OF);
- CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
- return (u16)res;
- }
- /****************************************************************************
- REMARKS:
- Implements the SUB instruction and side effects.
- ****************************************************************************/
- u32 sub_long(u32 d, u32 s)
- {
- u32 res; /* all operands in native machine order */
- u32 bc;
- res = d - s;
- set_szp_flags_32(res);
- /* calculate the borrow chain. See note at top */
- bc = (res & (~d | s)) | (~d & s);
- CONDITIONAL_SET_FLAG(bc & 0x80000000, F_CF);
- CONDITIONAL_SET_FLAG(XOR2(bc >> 30), F_OF);
- CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
- return res;
- }
- /****************************************************************************
- REMARKS:
- Implements the TEST instruction and side effects.
- ****************************************************************************/
- void test_byte(u8 d, u8 s)
- {
- u32 res; /* all operands in native machine order */
- res = d & s;
- CLEAR_FLAG(F_OF);
- set_szp_flags_8((u8)res);
- /* AF == dont care */
- CLEAR_FLAG(F_CF);
- }
- /****************************************************************************
- REMARKS:
- Implements the TEST instruction and side effects.
- ****************************************************************************/
- void test_word(u16 d, u16 s)
- {
- u32 res; /* all operands in native machine order */
- res = d & s;
- CLEAR_FLAG(F_OF);
- set_szp_flags_16((u16)res);
- /* AF == dont care */
- CLEAR_FLAG(F_CF);
- }
- /****************************************************************************
- REMARKS:
- Implements the TEST instruction and side effects.
- ****************************************************************************/
- void test_long(u32 d, u32 s)
- {
- u32 res; /* all operands in native machine order */
- res = d & s;
- CLEAR_FLAG(F_OF);
- set_szp_flags_32(res);
- /* AF == dont care */
- CLEAR_FLAG(F_CF);
- }
- /****************************************************************************
- REMARKS:
- Implements the XOR instruction and side effects.
- ****************************************************************************/
- u8 xor_byte(u8 d, u8 s)
- {
- u8 res; /* all operands in native machine order */
- res = d ^ s;
- no_carry_byte_side_eff(res);
- return res;
- }
- /****************************************************************************
- REMARKS:
- Implements the XOR instruction and side effects.
- ****************************************************************************/
- u16 xor_word(u16 d, u16 s)
- {
- u16 res; /* all operands in native machine order */
- res = d ^ s;
- no_carry_word_side_eff(res);
- return res;
- }
- /****************************************************************************
- REMARKS:
- Implements the XOR instruction and side effects.
- ****************************************************************************/
- u32 xor_long(u32 d, u32 s)
- {
- u32 res; /* all operands in native machine order */
- res = d ^ s;
- no_carry_long_side_eff(res);
- return res;
- }
- /****************************************************************************
- REMARKS:
- Implements the IMUL instruction and side effects.
- ****************************************************************************/
- void imul_byte(u8 s)
- {
- s16 res = (s16)((s8)M.x86.R_AL * (s8)s);
- M.x86.R_AX = res;
- if (((M.x86.R_AL & 0x80) == 0 && M.x86.R_AH == 0x00) ||
- ((M.x86.R_AL & 0x80) != 0 && M.x86.R_AH == 0xFF)) {
- CLEAR_FLAG(F_CF);
- CLEAR_FLAG(F_OF);
- } else {
- SET_FLAG(F_CF);
- SET_FLAG(F_OF);
- }
- }
- /****************************************************************************
- REMARKS:
- Implements the IMUL instruction and side effects.
- ****************************************************************************/
- void imul_word(u16 s)
- {
- s32 res = (s16)M.x86.R_AX * (s16)s;
- M.x86.R_AX = (u16)res;
- M.x86.R_DX = (u16)(res >> 16);
- if (((M.x86.R_AX & 0x8000) == 0 && M.x86.R_DX == 0x0000) ||
- ((M.x86.R_AX & 0x8000) != 0 && M.x86.R_DX == 0xFFFF)) {
- CLEAR_FLAG(F_CF);
- CLEAR_FLAG(F_OF);
- } else {
- SET_FLAG(F_CF);
- SET_FLAG(F_OF);
- }
- }
- /****************************************************************************
- REMARKS:
- Implements the IMUL instruction and side effects.
- ****************************************************************************/
- void imul_long_direct(u32 *res_lo, u32* res_hi,u32 d, u32 s)
- {
- #ifdef __HAS_LONG_LONG__
- s64 res = (s32)d * (s32)s;
- *res_lo = (u32)res;
- *res_hi = (u32)(res >> 32);
- #else
- u32 d_lo,d_hi,d_sign;
- u32 s_lo,s_hi,s_sign;
- u32 rlo_lo,rlo_hi,rhi_lo;
- if ((d_sign = d & 0x80000000) != 0)
- d = -d;
- d_lo = d & 0xFFFF;
- d_hi = d >> 16;
- if ((s_sign = s & 0x80000000) != 0)
- s = -s;
- s_lo = s & 0xFFFF;
- s_hi = s >> 16;
- rlo_lo = d_lo * s_lo;
- rlo_hi = (d_hi * s_lo + d_lo * s_hi) + (rlo_lo >> 16);
- rhi_lo = d_hi * s_hi + (rlo_hi >> 16);
- *res_lo = (rlo_hi << 16) | (rlo_lo & 0xFFFF);
- *res_hi = rhi_lo;
- if (d_sign != s_sign) {
- d = ~*res_lo;
- s = (((d & 0xFFFF) + 1) >> 16) + (d >> 16);
- *res_lo = ~*res_lo+1;
- *res_hi = ~*res_hi+(s >> 16);
- }
- #endif
- }
- /****************************************************************************
- REMARKS:
- Implements the IMUL instruction and side effects.
- ****************************************************************************/
- void imul_long(u32 s)
- {
- imul_long_direct(&M.x86.R_EAX,&M.x86.R_EDX,M.x86.R_EAX,s);
- if (((M.x86.R_EAX & 0x80000000) == 0 && M.x86.R_EDX == 0x00000000) ||
- ((M.x86.R_EAX & 0x80000000) != 0 && M.x86.R_EDX == 0xFFFFFFFF)) {
- CLEAR_FLAG(F_CF);
- CLEAR_FLAG(F_OF);
- } else {
- SET_FLAG(F_CF);
- SET_FLAG(F_OF);
- }
- }
- /****************************************************************************
- REMARKS:
- Implements the MUL instruction and side effects.
- ****************************************************************************/
- void mul_byte(u8 s)
- {
- u16 res = (u16)(M.x86.R_AL * s);
- M.x86.R_AX = res;
- if (M.x86.R_AH == 0) {
- CLEAR_FLAG(F_CF);
- CLEAR_FLAG(F_OF);
- } else {
- SET_FLAG(F_CF);
- SET_FLAG(F_OF);
- }
- }
- /****************************************************************************
- REMARKS:
- Implements the MUL instruction and side effects.
- ****************************************************************************/
- void mul_word(u16 s)
- {
- u32 res = M.x86.R_AX * s;
- M.x86.R_AX = (u16)res;
- M.x86.R_DX = (u16)(res >> 16);
- if (M.x86.R_DX == 0) {
- CLEAR_FLAG(F_CF);
- CLEAR_FLAG(F_OF);
- } else {
- SET_FLAG(F_CF);
- SET_FLAG(F_OF);
- }
- }
- /****************************************************************************
- REMARKS:
- Implements the MUL instruction and side effects.
- ****************************************************************************/
- void mul_long(u32 s)
- {
- #ifdef __HAS_LONG_LONG__
- u64 res = (u32)M.x86.R_EAX * (u32)s;
- M.x86.R_EAX = (u32)res;
- M.x86.R_EDX = (u32)(res >> 32);
- #else
- u32 a,a_lo,a_hi;
- u32 s_lo,s_hi;
- u32 rlo_lo,rlo_hi,rhi_lo;
- a = M.x86.R_EAX;
- a_lo = a & 0xFFFF;
- a_hi = a >> 16;
- s_lo = s & 0xFFFF;
- s_hi = s >> 16;
- rlo_lo = a_lo * s_lo;
- rlo_hi = (a_hi * s_lo + a_lo * s_hi) + (rlo_lo >> 16);
- rhi_lo = a_hi * s_hi + (rlo_hi >> 16);
- M.x86.R_EAX = (rlo_hi << 16) | (rlo_lo & 0xFFFF);
- M.x86.R_EDX = rhi_lo;
- #endif
- if (M.x86.R_EDX == 0) {
- CLEAR_FLAG(F_CF);
- CLEAR_FLAG(F_OF);
- } else {
- SET_FLAG(F_CF);
- SET_FLAG(F_OF);
- }
- }
- /****************************************************************************
- REMARKS:
- Implements the IDIV instruction and side effects.
- ****************************************************************************/
- void idiv_byte(u8 s)
- {
- s32 dvd, div, mod;
- dvd = (s16)M.x86.R_AX;
- if (s == 0) {
- x86emu_intr_raise(0);
- return;
- }
- div = dvd / (s8)s;
- mod = dvd % (s8)s;
- if (abs(div) > 0x7f) {
- x86emu_intr_raise(0);
- return;
- }
- M.x86.R_AL = (s8) div;
- M.x86.R_AH = (s8) mod;
- }
- /****************************************************************************
- REMARKS:
- Implements the IDIV instruction and side effects.
- ****************************************************************************/
- void idiv_word(u16 s)
- {
- s32 dvd, div, mod;
- dvd = (((s32)M.x86.R_DX) << 16) | M.x86.R_AX;
- if (s == 0) {
- x86emu_intr_raise(0);
- return;
- }
- div = dvd / (s16)s;
- mod = dvd % (s16)s;
- if (abs(div) > 0x7fff) {
- x86emu_intr_raise(0);
- return;
- }
- CLEAR_FLAG(F_CF);
- CLEAR_FLAG(F_SF);
- CONDITIONAL_SET_FLAG(div == 0, F_ZF);
- set_parity_flag(mod);
- M.x86.R_AX = (u16)div;
- M.x86.R_DX = (u16)mod;
- }
- /****************************************************************************
- REMARKS:
- Implements the IDIV instruction and side effects.
- ****************************************************************************/
- void idiv_long(u32 s)
- {
- #ifdef __HAS_LONG_LONG__
- s64 dvd, div, mod;
- dvd = (((s64)M.x86.R_EDX) << 32) | M.x86.R_EAX;
- if (s == 0) {
- x86emu_intr_raise(0);
- return;
- }
- div = dvd / (s32)s;
- mod = dvd % (s32)s;
- if (abs(div) > 0x7fffffff) {
- x86emu_intr_raise(0);
- return;
- }
- #else
- s32 div = 0, mod;
- s32 h_dvd = M.x86.R_EDX;
- u32 l_dvd = M.x86.R_EAX;
- u32 abs_s = s & 0x7FFFFFFF;
- u32 abs_h_dvd = h_dvd & 0x7FFFFFFF;
- u32 h_s = abs_s >> 1;
- u32 l_s = abs_s << 31;
- int counter = 31;
- int carry;
- if (s == 0) {
- x86emu_intr_raise(0);
- return;
- }
- do {
- div <<= 1;
- carry = (l_dvd >= l_s) ? 0 : 1;
- if (abs_h_dvd < (h_s + carry)) {
- h_s >>= 1;
- l_s = abs_s << (--counter);
- continue;
- } else {
- abs_h_dvd -= (h_s + carry);
- l_dvd = carry ? ((0xFFFFFFFF - l_s) + l_dvd + 1)
- : (l_dvd - l_s);
- h_s >>= 1;
- l_s = abs_s << (--counter);
- div |= 1;
- continue;
- }
- } while (counter > -1);
- /* overflow */
- if (abs_h_dvd || (l_dvd > abs_s)) {
- x86emu_intr_raise(0);
- return;
- }
- /* sign */
- div |= ((h_dvd & 0x10000000) ^ (s & 0x10000000));
- mod = l_dvd;
- #endif
- CLEAR_FLAG(F_CF);
- CLEAR_FLAG(F_AF);
- CLEAR_FLAG(F_SF);
- SET_FLAG(F_ZF);
- set_parity_flag(mod);
- M.x86.R_EAX = (u32)div;
- M.x86.R_EDX = (u32)mod;
- }
- /****************************************************************************
- REMARKS:
- Implements the DIV instruction and side effects.
- ****************************************************************************/
- void div_byte(u8 s)
- {
- u32 dvd, div, mod;
- dvd = M.x86.R_AX;
- if (s == 0) {
- x86emu_intr_raise(0);
- return;
- }
- div = dvd / (u8)s;
- mod = dvd % (u8)s;
- if (abs(div) > 0xff) {
- x86emu_intr_raise(0);
- return;
- }
- M.x86.R_AL = (u8)div;
- M.x86.R_AH = (u8)mod;
- }
- /****************************************************************************
- REMARKS:
- Implements the DIV instruction and side effects.
- ****************************************************************************/
- void div_word(u16 s)
- {
- u32 dvd, div, mod;
- dvd = (((u32)M.x86.R_DX) << 16) | M.x86.R_AX;
- if (s == 0) {
- x86emu_intr_raise(0);
- return;
- }
- div = dvd / (u16)s;
- mod = dvd % (u16)s;
- if (abs(div) > 0xffff) {
- x86emu_intr_raise(0);
- return;
- }
- CLEAR_FLAG(F_CF);
- CLEAR_FLAG(F_SF);
- CONDITIONAL_SET_FLAG(div == 0, F_ZF);
- set_parity_flag(mod);
- M.x86.R_AX = (u16)div;
- M.x86.R_DX = (u16)mod;
- }
- /****************************************************************************
- REMARKS:
- Implements the DIV instruction and side effects.
- ****************************************************************************/
- void div_long(u32 s)
- {
- #ifdef __HAS_LONG_LONG__
- u64 dvd, div, mod;
- dvd = (((u64)M.x86.R_EDX) << 32) | M.x86.R_EAX;
- if (s == 0) {
- x86emu_intr_raise(0);
- return;
- }
- div = dvd / (u32)s;
- mod = dvd % (u32)s;
- if (abs(div) > 0xffffffff) {
- x86emu_intr_raise(0);
- return;
- }
- #else
- s32 div = 0, mod;
- s32 h_dvd = M.x86.R_EDX;
- u32 l_dvd = M.x86.R_EAX;
- u32 h_s = s;
- u32 l_s = 0;
- int counter = 32;
- int carry;
- if (s == 0) {
- x86emu_intr_raise(0);
- return;
- }
- do {
- div <<= 1;
- carry = (l_dvd >= l_s) ? 0 : 1;
- if (h_dvd < (h_s + carry)) {
- h_s >>= 1;
- l_s = s << (--counter);
- continue;
- } else {
- h_dvd -= (h_s + carry);
- l_dvd = carry ? ((0xFFFFFFFF - l_s) + l_dvd + 1)
- : (l_dvd - l_s);
- h_s >>= 1;
- l_s = s << (--counter);
- div |= 1;
- continue;
- }
- } while (counter > -1);
- /* overflow */
- if (h_dvd || (l_dvd > s)) {
- x86emu_intr_raise(0);
- return;
- }
- mod = l_dvd;
- #endif
- CLEAR_FLAG(F_CF);
- CLEAR_FLAG(F_AF);
- CLEAR_FLAG(F_SF);
- SET_FLAG(F_ZF);
- set_parity_flag(mod);
- M.x86.R_EAX = (u32)div;
- M.x86.R_EDX = (u32)mod;
- }
- /****************************************************************************
- REMARKS:
- Implements the IN string instruction and side effects.
- ****************************************************************************/
- static void single_in(int size)
- {
- if(size == 1)
- store_data_byte_abs(M.x86.R_ES, M.x86.R_DI,(*sys_inb)(M.x86.R_DX));
- else if (size == 2)
- store_data_word_abs(M.x86.R_ES, M.x86.R_DI,(*sys_inw)(M.x86.R_DX));
- else
- store_data_long_abs(M.x86.R_ES, M.x86.R_DI,(*sys_inl)(M.x86.R_DX));
- }
- void ins(int size)
- {
- int inc = size;
- if (ACCESS_FLAG(F_DF)) {
- inc = -size;
- }
- if (M.x86.mode & (SYSMODE_PREFIX_REPE | SYSMODE_PREFIX_REPNE)) {
- /* dont care whether REPE or REPNE */
- /* in until CX is ZERO. */
- u32 count = ((M.x86.mode & SYSMODE_PREFIX_DATA) ?
- M.x86.R_ECX : M.x86.R_CX);
- while (count--) {
- single_in(size);
- M.x86.R_DI += inc;
- }
- M.x86.R_CX = 0;
- if (M.x86.mode & SYSMODE_PREFIX_DATA) {
- M.x86.R_ECX = 0;
- }
- M.x86.mode &= ~(SYSMODE_PREFIX_REPE | SYSMODE_PREFIX_REPNE);
- } else {
- single_in(size);
- M.x86.R_DI += inc;
- }
- }
- /****************************************************************************
- REMARKS:
- Implements the OUT string instruction and side effects.
- ****************************************************************************/
- static void single_out(int size)
- {
- if(size == 1)
- (*sys_outb)(M.x86.R_DX,fetch_data_byte_abs(M.x86.R_ES, M.x86.R_SI));
- else if (size == 2)
- (*sys_outw)(M.x86.R_DX,fetch_data_word_abs(M.x86.R_ES, M.x86.R_SI));
- else
- (*sys_outl)(M.x86.R_DX,fetch_data_long_abs(M.x86.R_ES, M.x86.R_SI));
- }
- void outs(int size)
- {
- int inc = size;
- if (ACCESS_FLAG(F_DF)) {
- inc = -size;
- }
- if (M.x86.mode & (SYSMODE_PREFIX_REPE | SYSMODE_PREFIX_REPNE)) {
- /* dont care whether REPE or REPNE */
- /* out until CX is ZERO. */
- u32 count = ((M.x86.mode & SYSMODE_PREFIX_DATA) ?
- M.x86.R_ECX : M.x86.R_CX);
- while (count--) {
- single_out(size);
- M.x86.R_SI += inc;
- }
- M.x86.R_CX = 0;
- if (M.x86.mode & SYSMODE_PREFIX_DATA) {
- M.x86.R_ECX = 0;
- }
- M.x86.mode &= ~(SYSMODE_PREFIX_REPE | SYSMODE_PREFIX_REPNE);
- } else {
- single_out(size);
- M.x86.R_SI += inc;
- }
- }
- /****************************************************************************
- PARAMETERS:
- addr - Address to fetch word from
- REMARKS:
- Fetches a word from emulator memory using an absolute address.
- ****************************************************************************/
- u16 mem_access_word(int addr)
- {
- DB( if (CHECK_MEM_ACCESS())
- x86emu_check_mem_access(addr);)
- return (*sys_rdw)(addr);
- }
- /****************************************************************************
- REMARKS:
- Pushes a word onto the stack.
- NOTE: Do not inline this, as (*sys_wrX) is already inline!
- ****************************************************************************/
- void push_word(u16 w)
- {
- DB( if (CHECK_SP_ACCESS())
- x86emu_check_sp_access();)
- M.x86.R_SP -= 2;
- (*sys_wrw)(((u32)M.x86.R_SS << 4) + M.x86.R_SP, w);
- }
- /****************************************************************************
- REMARKS:
- Pushes a long onto the stack.
- NOTE: Do not inline this, as (*sys_wrX) is already inline!
- ****************************************************************************/
- void push_long(u32 w)
- {
- DB( if (CHECK_SP_ACCESS())
- x86emu_check_sp_access();)
- M.x86.R_SP -= 4;
- (*sys_wrl)(((u32)M.x86.R_SS << 4) + M.x86.R_SP, w);
- }
- /****************************************************************************
- REMARKS:
- Pops a word from the stack.
- NOTE: Do not inline this, as (*sys_rdX) is already inline!
- ****************************************************************************/
- u16 pop_word(void)
- {
- u16 res;
- DB( if (CHECK_SP_ACCESS())
- x86emu_check_sp_access();)
- res = (*sys_rdw)(((u32)M.x86.R_SS << 4) + M.x86.R_SP);
- M.x86.R_SP += 2;
- return res;
- }
- /****************************************************************************
- REMARKS:
- Pops a long from the stack.
- NOTE: Do not inline this, as (*sys_rdX) is already inline!
- ****************************************************************************/
- u32 pop_long(void)
- {
- u32 res;
- DB( if (CHECK_SP_ACCESS())
- x86emu_check_sp_access();)
- res = (*sys_rdl)(((u32)M.x86.R_SS << 4) + M.x86.R_SP);
- M.x86.R_SP += 4;
- return res;
- }
|