123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442 |
- /****************************************************************************
- *
- * 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 ----------------------------*/
- /*--------- 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;
- }
|