Previous 15994 Revisions Next

r2628 Thursday 15th May, 2008 at 11:26:28 UTC by Nathan Woods
Updated to MAME 0.125u1 core
[ROOT]makefile
[docs]messnew.txt
[src/emu]cpuexec.h cpuintrf.c emu.mak emuopts.c emuopts.h fileio.c fileio.h memory.c memory.h sound.c ui.c video.c video.h
[src/emu/cpu]cpu.mak drcbec.c* drcbeut.c* drcbeut.h* drcbex64.c* drcbex86.c* drccache.c* drccache.h* drcfe.c drcfe.h drcuml.c* drcuml.h* drcumld.c* drcumld.h* drcumlsh.h* x86emit.h
[src/emu/cpu/ds5002fp]ds5002fp.c* ds5002fp.h* ds5002fpdasm.c* ds5002fpops.c*
[src/emu/cpu/mips]mips3.c mips3.h mips3com.c mips3drc.c mips3dsm.c mips3fe.c mips3fe.h r3000.c
[src/emu/cpu/z180]z180dasm.c
[src/emu/cpu/z80]z80dasm.c
[src/emu/debug]debugvw.c
[src/emu/drivers]xtal.h
[src/emu/machine]6526cia.c 6526cia.h cdp1852.c* cdp1852.h*
[src/emu/sound]s14001a.c tms5110.c tms5220.c
[src/emu/video]cdp1869.c voodoo.c
[src/lib/util]aviio.c aviio.h
[src/mame]mame.mak mamedriv.c
[src/mame/drivers]arcadecl.c asteroid.c atarig42.c atarisy2.c blktiger.c cidelsa.c cps1.c cps2.c cps3.c djmain.c dynax.c funworld.c gaelco.c gatron.c* gottlieb.c hng64.c leland.c lkage.c lwings.c megadriv.c meritm.c missile.c namcona1.c segac2.c spaceg.c tagteam.c tail2nos.c tankbatt.c tankbust.c taotaido.c targeth.c taxidrvr.c tmaster.c toaplan1.c toaplan2.c undrfire.c wrally.c yunsun16.c
[src/mame/includes]gaelcrpt.h* namcona1.h
[src/mame/machine]gaelcrpt.c* snes.c wrally.c
[src/mame/video]cps1.c namcona1.c undrfire.c wrally.c
[src/mess]mess.mak
[src/mess/drivers]a2600.c a310.c abc80x.c avigo.c
[src/mess/machine]apple2gs.c apple3.c c64.c lisa.c osborne1.c
[src/tools]chdman.c

src/mame/machine/snes.c
r2627r2628
344344/* should we treat this as nvram in MAME? */
345345static OPBASE_HANDLER(spc_opbase)
346346{
347      opcode_base = opcode_arg_base = spc_ram;
347   opbase->rom = opbase->ram = spc_ram;
348348   return ~0;
349349}
350350
351351static OPBASE_HANDLER(snes_opbase)
352352{
353   opcode_base = opcode_arg_base = snes_ram;
353   opbase->rom = opbase->ram = snes_ram;
354354   return ~0;
355355}
356356
src/mame/machine/gaelcrpt.c
r0r2628
1/*
2
3Gaelco video RAM encryption
4
5Thanks to GAELCO SA for information on the algorithm.
6
7*/
8
9#include "driver.h"
10
11static int decrypt(int const param1, int const param2, int const enc_prev_word, int const dec_prev_word, int const enc_word)
12{
13   int const swap = (BIT(dec_prev_word, 8) << 1) | BIT(dec_prev_word, 7);
14   int const type = (BIT(dec_prev_word,12) << 1) | BIT(dec_prev_word, 2);
15   int res=0;
16   int k=0;
17
18   switch (swap)
19   {
20      case 0:   res = BITSWAP16(enc_word,  1, 2, 0,14,12,15, 4, 8,13, 7, 3, 6,11, 5,10, 9); break;
21      case 1:   res = BITSWAP16(enc_word, 14,10, 4,15, 1, 6,12,11, 8, 0, 9,13, 7, 3, 5, 2); break;
22      case 2:   res = BITSWAP16(enc_word,  2,13,15, 1,12, 8,14, 4, 6, 0, 9, 5,10, 7, 3,11); break;
23      case 3:   res = BITSWAP16(enc_word,  3, 8, 1,13,14, 4,15, 0,10, 2, 7,12, 6,11, 9, 5); break;
24   }
25
26   res ^= param2;
27
28   switch (type)
29   {
30      case 0:
31         k =   (0 << 0) |
32            (1 << 1) |
33            (0 << 2) |
34            (1 << 3) |
35            (1 << 4) |
36            (1 << 5);
37         break;
38
39      case 1:
40         k =   (BIT(dec_prev_word, 0) << 0) |
41            (BIT(dec_prev_word, 1) << 1) |
42            (BIT(dec_prev_word, 1) << 2) |
43            (BIT(enc_prev_word, 3) << 3) |
44            (BIT(enc_prev_word, 8) << 4) |
45            (BIT(enc_prev_word,15) << 5);
46         break;
47
48      case 2:
49         k =   (BIT(enc_prev_word, 5) << 0) |
50            (BIT(dec_prev_word, 5) << 1) |
51            (BIT(enc_prev_word, 7) << 2) |
52            (BIT(enc_prev_word, 3) << 3) |
53            (BIT(enc_prev_word,13) << 4) |
54            (BIT(enc_prev_word,14) << 5);
55         break;
56
57      case 3:
58         k =   (BIT(enc_prev_word, 0) << 0) |
59            (BIT(enc_prev_word, 9) << 1) |
60            (BIT(enc_prev_word, 6) << 2) |
61            (BIT(dec_prev_word, 4) << 3) |
62            (BIT(enc_prev_word, 2) << 4) |
63            (BIT(dec_prev_word,11) << 5);
64         break;
65   }
66
67   k ^= param1;
68
69   res = (res & 0xffc0) | ((res + k) & 0x003f);
70
71   res ^= param1;
72
73   switch (type)
74   {
75      case 0:
76         k =   (BIT(enc_word, 9) << 0) |
77            (BIT(res,2)       << 1) |
78            (BIT(enc_word, 5) << 2) |
79            (BIT(res,5)       << 3) |
80            (BIT(res,4)       << 4);
81         break;
82
83      case 1:
84         k =   (BIT(dec_prev_word, 2) << 0) |   // always 1
85            (BIT(enc_prev_word, 4) << 1) |
86            (BIT(dec_prev_word,14) << 2) |
87            (BIT(res, 1)           << 3) |
88            (BIT(dec_prev_word,12) << 4);   // always 0
89         break;
90
91      case 2:
92         k =   (BIT(enc_prev_word, 6) << 0) |
93            (BIT(dec_prev_word, 6) << 1) |
94            (BIT(dec_prev_word,15) << 2) |
95            (BIT(res,0)            << 3) |
96            (BIT(dec_prev_word, 7) << 4);
97         break;
98
99      case 3:
100         k =   (BIT(dec_prev_word, 2) << 0) |   // always 1
101            (BIT(dec_prev_word, 9) << 1) |
102            (BIT(enc_prev_word, 5) << 2) |
103            (BIT(dec_prev_word, 1) << 3) |
104            (BIT(enc_prev_word,10) << 4);
105
106         break;
107   }
108
109   k ^= param1;
110
111   res =   (res & 0x003f) |
112         ((res + (k <<  6)) & 0x07c0) |
113         ((res + (k << 11)) & 0xf800);
114
115   res ^= (param1 << 6) | (param1 << 11);
116
117   return BITSWAP16(res, 2,6,0,11,14,12,7,10,5,4,8,3,9,1,13,15);
118}
119
120
121
122UINT16 gaelco_decrypt(int offset, int data, int param1, int param2)
123{
124   static int lastpc, lastoffset, lastencword, lastdecword;
125
126   int thispc = activecpu_get_pc();
127//  int savedata = data;
128
129   /* check if 2nd half of 32 bit */
130   if(lastpc == thispc && offset == lastoffset + 1)
131   {
132      lastpc = 0;
133      data = decrypt(param1, param2, lastencword, lastdecword, data);
134   }
135   else
136   {
137      /* code as 1st word */
138
139      lastpc = thispc;
140      lastoffset = offset;
141      lastencword = data;
142
143      /* high word returned */
144      data = decrypt(param1, param2, 0, 0, data);
145
146      lastdecword = data;
147
148//      logerror("%8x : data1 = %4x > %4x @ %8x\n",activecpu_get_pc(),savedata,data,lastoffset);
149   }
150   return data;
151}
src/mame/machine/wrally.c
r2627r2628
88***************************************************************************/
99
1010#include "driver.h"
11#include "cpu/ds5002fp/ds5002fp.h"
12#include "gaelcrpt.h"
1113
12UINT16 *wrally_encr_table[2];
14/* from video/wrally.c */
15extern UINT16 *wrally_videoram;
16extern tilemap *wrally_pant[2];
1317
1418/***************************************************************************
1519
16    World Rally Video RAM encryption
20    World Rally memory handlers
1721
1822***************************************************************************/
1923
20static int subxor1(int data,int res)
24WRITE16_HANDLER( wrally_vram_w )
2125{
22   res ^= 0x0002;
23   if (BIT(data,0) ^ !BIT(data,5))
24   {
25      res ^= 0x0800;
26      if (BIT(data,2) ^ BIT(data,3)) res ^= 0x0001;
27      if (BIT(data,6) && !BIT(data,11) && BIT(data,5)) res ^= 0x0001;
28   }
26   data = gaelco_decrypt(offset, data, 0x1f, 0x522a);
27   COMBINE_DATA(&wrally_videoram[offset]);
2928
30   return res;
29   tilemap_mark_tile_dirty(wrally_pant[(offset & 0x1fff) >> 12], ((offset << 1) & 0x1fff) >> 2);
3130}
3231
33static int subxor2(int data,int res)
32WRITE16_HANDLER( wrally_flipscreen_w )
3433{
35   res ^= 0x0020;
36   if (BIT(data,8) ^ !BIT(data,5))
37   {
38      res ^= 0x0008;
39      if (BIT(data,4) ^ BIT(data,3)) res ^= 0x0100;
40      if (BIT(data,6) && !BIT(data,11) && BIT(data,5)) res ^= 0x0100;
41   }
42
43   return res;
34   flip_screen_set(data & 0x01);
4435}
4536
46static int wrally_decode_vram(int data)
47{
48   int res = 0;
49
50
51   res = BITSWAP16(data,5,7,9,12,2,14,13,15,3,6,8,11,4,10,0,1);
52
53   res ^= 0x0062;
54
55   if (BIT(data,9))  res ^= 0x5004;
56
57   if ( BIT(data,5) ^ (!BIT(data,9) &&  BIT(data,7)))  res ^= 0x0200;
58   if (!BIT(data,5) ^ (!BIT(data,9) &&  BIT(data,12))) res ^= 0x0400;
59
60   if (BIT(data,3) ^ BIT(data,6))  res ^= 0x0101;
61
62   if (BIT(data,3) ^ BIT(data,5))
63   {
64      res ^= 0x0808;
65      if (BIT(data,2) ^ BIT(data,3)) res ^= 0x0001;
66      if (BIT(data,4) ^ BIT(data,3)) res ^= 0x0100;
67   }
68
69   if ((BIT(data,9) && !BIT(data,10)) || BIT(data,5))
70   {
71      res ^= 0x0010;
72
73      if (!BIT(data,11)) res ^= 0x0141;
74
75      if (BIT(data,6) && !BIT(data,11))
76      {
77         res ^= 0x0888;
78         if (!BIT(data,2)) res ^= 0x0001;
79         if (!BIT(data,4)) res ^= 0x0100;
80      }
81   }
82
83   if (BIT(data,9) && !BIT(data,10))
84   {
85      res ^= 0x8600;
86      if (!BIT(data,14)) res = subxor1(data,res);
87      if (!BIT(data,5))  res = subxor2(data,res);
88
89      if (BIT(data,6) && !BIT(data,11))
90      {
91         if (BIT(data,0) && !BIT(data,5)) res ^= 0x0001;
92         if (BIT(data,8) && !BIT(data,5)) res ^= 0x0100;
93      }
94   }
95
96   if (BIT(data,5) ^  BIT(data,14))
97   {
98      if ( BIT(data,14))                 res = subxor1(data,res);
99      if (!BIT(data,9) &&  BIT(data,12)) res = subxor1(data,res);
100   }
101
102   if (BIT(data,5) ^ !BIT(data,13))
103   {
104      if ( BIT(data,5))                  res = subxor2(data,res);
105      if ( BIT(data,9) &&  BIT(data,10)) res = subxor2(data,res);
106      if (!BIT(data,9) && !BIT(data,7))  res = subxor2(data,res);
107   }
108
109   return res;
110}
111
112/***************************************************************************
113
114    World Rally memory handlers
115
116***************************************************************************/
117
11837WRITE16_HANDLER( OKIM6295_bankswitch_w )
11938{
12039   UINT8 *RAM = memory_region(REGION_SOUND1);
r2627r2628
13453   coin_lockout_w( (offset >> 3) & 0x01, ~data & 0x01);
13554}
13655
56/* Converts memory offsets to the format expected by the Dallas */
57READ32_HANDLER( wrally_external_ram_iaddr )
58{
59   return offset ^= 0x0001;
60}
61
13762/***************************************************************************
13863
139    World Rally init machine
64    World Rally init/reset machine
14065
14166***************************************************************************/
14267
14368DRIVER_INIT( wrally )
14469{
145   int i;
70}
14671
147   /* recreate encryption tables on start up */
148   wrally_encr_table[0] = (UINT16 *)auto_malloc(0x10000*2);
149   wrally_encr_table[1] = (UINT16 *)auto_malloc(0x10000*2);
150   for (i = 0; i < 0x10000; i++){
151      wrally_encr_table[0][i] = wrally_decode_vram(i);
152      wrally_encr_table[1][i] = wrally_decode_vram(i);
153   }
72MACHINE_RESET( wrally )
73{
74   /* sets the function to convert addresses for shared memory with the dallas */
75   ds5002fp_set_ebram_iaddr_callback(wrally_external_ram_iaddr);
15476}
src/mame/mamedriv.c
r2627r2628
15651565   DRIVER( pitnrun )   /* PR  (c) 1984 Taito Corporation */
15661566   DRIVER( pitnruna )   /* PR  (c) 1984 Taito Corporation */
15671567   DRIVER( lkage )      /* A54 (c) 1984 Taito Corporation */
1568   DRIVER( lkageo )   /* A54 (c) 1984 Taito Corporation */
15681569   DRIVER( lkageb )   /* bootleg */
15691570   DRIVER( lkageb2 )   /* bootleg */
15701571   DRIVER( lkageb3 )   /* bootleg */
r2627r2628
20572058   DRIVER( demonwld )   /* TP-016 (c) 1990 Toaplan (+ Taito license when set to Japan) */
20582059   DRIVER( demonwl1 )   /* TP-016 (c) 1989 Toaplan + Taito license */
20592060   DRIVER( demonwl2 )   /* TP-016 (c) 1989 Toaplan */
2061   DRIVER( demonwl3 )   /* TP-016 (c) 1989 Toaplan */
20602062   DRIVER( fireshrk )   /* TP-017 (c) 1990 Toaplan */
20612063   DRIVER( samesame )   /* TP-017 (c) 1989 Toaplan */
20622064   DRIVER( samesam2 )   /* TP-017 (c) 1989 Toaplan */
r2627r2628
20932095   /* More Toaplan Games */
20942096   DRIVER( tekipaki )   /* TP-020 (c) 1991 Toaplan */
20952097   DRIVER( ghox )      /* TP-021 (c) 1991 Toaplan */
2098   DRIVER( ghoxa )      /* TP-021 (c) 1991 Toaplan */
20962099   DRIVER( dogyuun )   /* TP-022 (c) 1992 Toaplan */
20972100   DRIVER( kbash )      /* TP-023 (c) 1993 Toaplan */
20982101   DRIVER( kbash2 )   /* bootleg */
r2627r2628
23122315   DRIVER( lwings )   /* 11/1986 (c) 1986 */
23132316   DRIVER( lwings2 )   /* 11/1986 (c) 1986 */
23142317   DRIVER( lwingsjp )   /* 11/1986 (c) 1986 */
2318   DRIVER( lwingsb )   /* 11/1986 (c) 1986 (bootleg)*/
23152319   DRIVER( sidearms )   /* 12/1986 (c) 1986 (World) */
23162320   DRIVER( sidearmr )   /* 12/1986 (c) 1986 + Romstar license (US) */
23172321   DRIVER( sidearjp )   /* 12/1986 (c) 1986 (Japan) */
r2627r2628
48114815   DRIVER( hmcompmx )   /* GX858 (c) 1999 */
48124816   DRIVER( bm4thmix )   /* GX847 (c) 1999 (Japan) */
48134817   DRIVER( bm5thmix )   /* GX981 (c) 1999 (Japan) */
4814   DRIVER( bmclubmx )   /*       (c) 2000 (Japan) */
4818   DRIVER( bmclubmx )   /* GX993 (c) 2000 (Japan) */
48154819   DRIVER( bmcorerm )   /* GXA05 (c) 1999 (Japan) */
48164820   DRIVER( bmdct )      /* GX995 (c) 2000 (Japan) */
48174821   DRIVER( bmcompm2 )   /* GX988 (c) 2000 (Japan) */
48184822   DRIVER( hmcompm2 )   /* GX988 (c) 2000 */
48194823   DRIVER( bm6thmix )   /* GXA21 (c) 2001 (Japan) */
4820   DRIVER( bm7thmix )   /*       (c) 2001 (Japan) */
4821   //DRIVER( bmfinal ) /*       (c) 2002 (Japan) */
4824   DRIVER( bm7thmix )   /* GXB07 (c) 2001 (Japan) */
4825   DRIVER( bmfinal )   /* GXC01 (c) 2002 (Japan) */
48224826
4823   //DRIVER( popn1 )       /*       (c) 1998 (Japan) */
4824   //DRIVER( popn2 )       /*       (c) 1998 (Japan) */
4825   //DRIVER( popn3 )       /*       (c) 1999 (Japan) */
4826   //DRIVER( popnstex )    /*       (c) 1999 (Japan) */
4827   //DRIVER( popn1 )       /* GX803 (c) 1998 (Japan) */
4828   //DRIVER( popn2 )       /* GX831 (c) 1998 (Japan) */
4829   //DRIVER( popn3 )       /* GX980 (c) 1999 (Japan) */
4830   //DRIVER( popnstex )    /* GX970 (c) 1999 (Japan) */
48274831
48284832   /* Firebeat games */
48294833   DRIVER( ppp )      /* GQ977 (c) 2000 */
r2627r2628
75817585   DRIVER( aztarac )   /* (c) 1983 Centuri (vector game) */
75827586   DRIVER( mole )      /* (c) 1982 Yachiyo Electronics, Ltd. */
75837587   DRIVER( thehand )   /* (c) 1981 T.I.C. */
7584   DRIVER( gotya )      /* (c) 1981 Game-A-Tron */
75857588   DRIVER( mrjong )   /* (c) 1983 Kiwako */
75867589   DRIVER( crazyblk )   /* (c) 1983 Kiwako + ECI license */
75877590   DRIVER( blkbustr )   /* (c) 1983 Kiwako + ECI license */
r2627r2628
77157718   DRIVER( cfarm )      /* (c) 1999 LAI Games */
77167719   DRIVER( cclownz )   /* (c) 1999 LAI Games */
77177720   DRIVER( rotaryf )
7718   DRIVER( sbugger )   /* (c) 1981 Game-A-Tron */
7719   DRIVER( sbuggera )   /* (c) 1981 Game-A-Tron */
77207721   DRIVER( portrait )   /* (c) 1983 Olympia */
77217722   DRIVER( portrata )   /* (c) 1983 Olympia */
77227723   DRIVER( enigma2 )   /* (c) 1981 Game Plan (Zilec Electronics license) */
r2627r2628
78117812   DRIVER( couplep )   /* (c) 1988 Merit */
78127813   DRIVER( couplei )   /* (c) 1988 Merit */
78137814   DRIVER( pitboss2 )   /* (c) 1988 Merit */
7814   DRIVER( spitboss )   /* (c) 198? Merit */
7815   DRIVER( spitboss )   /* (c) 1988 Merit */
7816   DRIVER( pitbosss )   /* (c) 1990 Merit */
78157817   DRIVER( pitbossm )   /* (c) 1994 Merit */
7816   DRIVER( megat3 )   /* (c) 1995 Merit */
7818   DRIVER( megat3 )   /* (c) 1996 Merit */
7819   DRIVER( megat3a )   /* (c) 1995 Merit */
78177820   DRIVER( megat3te )   /* (c) 1996 Merit */
78187821   DRIVER( megat4 )   /* (c) 1996 Merit */
78197822   DRIVER( megat4a )   /* (c) 1996 Merit */
r2627r2628
78907893   DRIVER( apple10 )   /* (c) 1998 Sandii' */
78917894   DRIVER( tenballs )   /* 1997 unknown */
78927895
7896   /* Game-A-Tron */
7897   DRIVER( gotya )      /* (c) 1981 Game-A-Tron */
7898   DRIVER( sbugger )   /* (c) 1981 Game-A-Tron */
7899   DRIVER( sbuggera )   /* (c) 1981 Game-A-Tron */
7900   DRIVER( gatcards )   /* (c) 1983 Game-A-Tron */
7901   DRIVER( gatslots )   /* (c) 1983 Game-A-Tron */
7902
78937903   DRIVER( vroulet )   /* (c) 1989 World Game */
78947904   DRIVER( rcasino )   /* (c) 1984 Dyna Electronics */
78957905   DRIVER( cmv801 )   /* (c) 198? Corsica */
src/mame/includes/namcona1.h
r2627r2628
2222
2323#define NA1_NVRAM_SIZE (0x800)
2424
25extern UINT16 *namcona1_rozvideoram;
2526extern UINT16 *namcona1_workram;
2627extern UINT16 *namcona1_vreg;
2728extern UINT16 *namcona1_scroll;
28extern UINT16 *namcona1_sparevram;
2929
30extern WRITE16_HANDLER( namcona1_rozvideoram_w );
31extern READ16_HANDLER( namcona1_rozvideoram_r );
32
3033extern WRITE16_HANDLER( namcona1_videoram_w );
3134extern READ16_HANDLER( namcona1_videoram_r );
3235
src/mame/includes/gaelcrpt.h
r0r2628
1UINT16 gaelco_decrypt(int offset, int data, int param1, int param2);
2
src/mame/mame.mak
r2627r2628
7474CPUS += I8052
7575CPUS += I8751
7676CPUS += I8752
77CPUS += DS5002FP
7778CPUS += M6800
7879CPUS += M6801
7980CPUS += M6802
r2627r2628
710711   $(DRIVERS)/fuukifg3.o $(VIDEO)/fuukifg3.o \
711712
712713$(MAMEOBJ)/gaelco.a: \
713   $(DRIVERS)/gaelco.o $(VIDEO)/gaelco.o \
714   $(DRIVERS)/gaelco.o $(VIDEO)/gaelco.o $(MACHINE)/gaelcrpt.o \
714715   $(DRIVERS)/gaelco2.o $(MACHINE)/gaelco2.o $(VIDEO)/gaelco2.o \
715716   $(DRIVERS)/gaelco3d.o $(VIDEO)/gaelco3d.o \
716717   $(DRIVERS)/glass.o $(VIDEO)/glass.o \
r2627r2628
15421543   $(DRIVERS)/freekick.o $(VIDEO)/freekick.o \
15431544   $(DRIVERS)/funworld.o $(VIDEO)/funworld.o \
15441545   $(DRIVERS)/gamecstl.o \
1546   $(DRIVERS)/gatron.o \
15451547   $(DRIVERS)/gtipoker.o \
15461548   $(DRIVERS)/go2000.o \
15471549   $(DRIVERS)/good.o \
src/mame/video/undrfire.c
r2627r2628
245245      data = spriteram32[offs+0];
246246      flipx =    (data & 0x00800000) >> 23;
247247      zoomx =    (data & 0x007f0000) >> 16;
248      tilenum =  (data & 0x00007fff);
248      tilenum =  (data & 0x0000ffff);
249249
250250      data = spriteram32[offs+2];
251251      priority = (data & 0x000c0000) >> 18;
r2627r2628
262262
263263      color /= 2;      /* as sprites are 5bpp */
264264      flipy = !flipy;
265//      y = (-y &0x3ff);
266265
267266      if (!tilenum) continue;
268267
269//      flipy = !flipy;
270268      zoomx += 1;
271269      zoomy += 1;
272270
r2627r2628
282280      total_chunks = ((dblsize*3) + 1) << 2;   // 4 or 16
283281      map_offset = tilenum << 2;
284282
283      for (sprite_chunk = 0; sprite_chunk < total_chunks; sprite_chunk++)
285284      {
286         for (sprite_chunk=0;sprite_chunk<total_chunks;sprite_chunk++)
287         {
288            j = sprite_chunk / dimension;   /* rows */
289            k = sprite_chunk % dimension;   /* chunks per row */
285         int map_addr;
290286
291            px = k;
292            py = j;
293            /* pick tiles back to front for x and y flips */
294            if (flipx)  px = dimension-1-k;
295            if (flipy)  py = dimension-1-j;
287         j = sprite_chunk / dimension;   /* rows */
288         k = sprite_chunk % dimension;   /* chunks per row */
296289
297            code = spritemap[map_offset + px + (py<<(dblsize+1))];
290         px = k;
291         py = j;
292         /* pick tiles back to front for x and y flips */
293         if (flipx)  px = dimension-1-k;
294         if (flipy)  py = dimension-1-j;
298295
299            if (spritemapHibit)
300            {
301               code|=spritemapHibit[map_offset + px + (py<<(dblsize+1))] << 16;
296         map_addr = map_offset + px + (py << (dblsize + 1));
297         code =  (spritemapHibit[map_addr] << 16) | spritemap[map_addr];
302298
303               //if (spritemapHibit[map_offset + px + (py<<(dblsize+1))])
304                  //color=mame_rand(machine);
305            }
299         curx = x + ((k*zoomx)/dimension);
300         cury = y + ((j*zoomy)/dimension);
306301
307            curx = x + ((k*zoomx)/dimension);
308            cury = y + ((j*zoomy)/dimension);
302         zx= x + (((k+1)*zoomx)/dimension) - curx;
303         zy= y + (((j+1)*zoomy)/dimension) - cury;
309304
310            zx= x + (((k+1)*zoomx)/dimension) - curx;
311            zy= y + (((j+1)*zoomy)/dimension) - cury;
312
313            if (sprites_flipscreen)
314            {
315               /* -zx/y is there to fix zoomed sprite coords in screenflip.
305         if (sprites_flipscreen)
306         {
307            /* -zx/y is there to fix zoomed sprite coords in screenflip.
316308                       drawgfxzoom does not know to draw from flip-side of sprites when
317309                       screen is flipped; so we must correct the coords ourselves. */
318310
319               curx = 320 - curx - zx;
320               cury = 256 - cury - zy;
321               flipx = !flipx;
322               flipy = !flipy;
323            }
311            curx = 320 - curx - zx;
312            cury = 256 - cury - zy;
313            flipx = !flipx;
314            flipy = !flipy;
315         }
324316
325            sprite_ptr->gfx = 0;
326            sprite_ptr->code = code;
327            sprite_ptr->color = color;
328            sprite_ptr->flipx = !flipx;
329            sprite_ptr->flipy = flipy;
330            sprite_ptr->x = curx;
331            sprite_ptr->y = cury;
332            sprite_ptr->zoomx = zx << 12;
333            sprite_ptr->zoomy = zy << 12;
317         sprite_ptr->gfx = 0;
318         sprite_ptr->code = code;
319         sprite_ptr->color = color;
320         sprite_ptr->flipx = !flipx;
321         sprite_ptr->flipy = flipy;
322         sprite_ptr->x = curx;
323         sprite_ptr->y = cury;
324         sprite_ptr->zoomx = zx << 12;
325         sprite_ptr->zoomy = zy << 12;
334326
335            if (primasks)
336            {
337               sprite_ptr->primask = primasks[priority];
338
339               sprite_ptr++;
340            }
341            else
342            {
343               drawgfxzoom(bitmap,machine->gfx[sprite_ptr->gfx],
344                     sprite_ptr->code,
345                     sprite_ptr->color,
346                     sprite_ptr->flipx,sprite_ptr->flipy,
347                     sprite_ptr->x,sprite_ptr->y,
348                     cliprect,TRANSPARENCY_PEN,0,
349                     sprite_ptr->zoomx,sprite_ptr->zoomy);
350            }
327         if (primasks)
328         {
329            sprite_ptr->primask = primasks[priority];
330            sprite_ptr++;
351331         }
332         else
333         {
334            drawgfxzoom(bitmap,machine->gfx[sprite_ptr->gfx],
335                  sprite_ptr->code,
336                  sprite_ptr->color,
337                  sprite_ptr->flipx,sprite_ptr->flipy,
338                  sprite_ptr->x,sprite_ptr->y,
339                  cliprect,TRANSPARENCY_PEN,0,
340                  sprite_ptr->zoomx,sprite_ptr->zoomy);
341         }
352342      }
353343   }
354344
r2627r2628
636626
637627   TC0480SCP_tilemap_draw(bitmap,cliprect,layer[4],0,0);   /* TC0480SCP text layer */
638628
639   /* See if we should draw artificial gun targets */
640   /* (not yet implemented...) */
641
642   if (input_port_read_indexed(screen->machine,7) & 0x1)   /* Fake DSW */
643   {
644      popmessage("Gunsights on");
645   }
646
647629/* Enable this to see rotation (?) control words */
648630#if 0
649631   {
src/mame/video/namcona1.c
r2627r2628
22
33/*
44TODO:
5- dynamic screen resolution
6- bg pen (or the pen of the lower tilemap?)
7- sprites, that have the shadow bit set but aren't shadows, have bad colors
8- check if the data&0x8000 condition in tilemaps is valid even when 4bpp mode it's used
9- namco logo in emeralda demo is missing (it's used with a roz effect)
10- invisible dolphin sprite in emeralda instruction screen
5- dynamic screen resolution (changes between emeralda test mode and normal game)
6- non-shadow pixels for sprites flagged to enable shadows have bad colors
117*/
128
139#include "driver.h"
r2627r2628
2016UINT16 *namcona1_scroll;
2117UINT16 *namcona1_workram;
2218UINT16 *namcona1_sparevram;
19UINT16 *namcona1_rozvideoram;
20UINT16 *namcona1_pixmap;
2321
2422/* private variables */
2523static char *dirtychar;
2624static char dirtygfx;
2725static UINT16 *shaperam;
2826static UINT16 *cgram;
27static tilemap *roz_tilemap;
28static int roz_palette;
29static int roz_dirty;
2930static tilemap *bg_tilemap[NAMCONA1_NUM_TILEMAPS];
3031static int tilemap_palette_bank[NAMCONA1_NUM_TILEMAPS];
3132static int palette_is_dirty;
3233
3334static void tilemap_get_info(
34   running_machine *machine,tile_data *tileinfo,int tile_index,const UINT16 *tilemap_videoram,int tilemap_color,int use_4bpp_gfx )
35   running_machine *machine,
36   tile_data *tileinfo,
37   int tile_index,
38   const UINT16 *tilemap_videoram,
39   int tilemap_color,
40   int use_4bpp_gfx )
3541{
3642#ifdef LSB_FIRST
3743   UINT16 *source;
r2627r2628
7783   }
7884} /* tilemap_get_info */
7985
80static TILE_GET_INFO( tilemap_get_info0 ){ tilemap_get_info(machine,tileinfo,tile_index,0*0x1000+videoram16,tilemap_palette_bank[0],namcona1_vreg[0x58+6]&1); }
81static TILE_GET_INFO( tilemap_get_info1 ){ tilemap_get_info(machine,tileinfo,tile_index,1*0x1000+videoram16,tilemap_palette_bank[1],namcona1_vreg[0x58+6]&2); }
82static TILE_GET_INFO( tilemap_get_info2 ){ tilemap_get_info(machine,tileinfo,tile_index,2*0x1000+videoram16,tilemap_palette_bank[2],namcona1_vreg[0x58+6]&4); }
83static TILE_GET_INFO( tilemap_get_info3 ){ tilemap_get_info(machine,tileinfo,tile_index,3*0x1000+videoram16,tilemap_palette_bank[3],namcona1_vreg[0x58+6]&8); }
86static TILE_GET_INFO( tilemap_get_info0 ){ tilemap_get_info(machine,tileinfo,tile_index,0*0x1000+videoram16,tilemap_palette_bank[0],namcona1_vreg[0xbc/2]&1); }
87static TILE_GET_INFO( tilemap_get_info1 ){ tilemap_get_info(machine,tileinfo,tile_index,1*0x1000+videoram16,tilemap_palette_bank[1],namcona1_vreg[0xbc/2]&2); }
88static TILE_GET_INFO( tilemap_get_info2 ){ tilemap_get_info(machine,tileinfo,tile_index,2*0x1000+videoram16,tilemap_palette_bank[2],namcona1_vreg[0xbc/2]&4); }
89static TILE_GET_INFO( tilemap_get_info3 ){ tilemap_get_info(machine,tileinfo,tile_index,3*0x1000+videoram16,tilemap_palette_bank[3],namcona1_vreg[0xbc/2]&8); }
8490
91static TILE_GET_INFO( roz_get_info )
92{
93   /* each logical tile is constructed from 4*4 normal tiles */
94   int tilemap_color = roz_palette;
95   int use_4bpp_gfx = namcona1_vreg[0xbc/2]&16;
96   int c = tile_index%0x40;
97   int r = tile_index/0x40;
98   int data = namcona1_rozvideoram[(r/4)*0x40+c/4]+(c%4)+(r%4)*0x40;
99   int tile = data&0xfff;
100   int gfx = use_4bpp_gfx;
101   if( use_4bpp_gfx )
102   {
103      tilemap_color *= 0x10;
104      tilemap_color += (data & 0x7000)>>12;
105   }
106   if( data & 0x8000 )
107   {
108      SET_TILE_INFO( gfx,tile,tilemap_color,TILE_FORCE_LAYER0 );
109   }
110   else
111   {
112#ifdef LSB_FIRST
113      UINT16 *source;
114      static UINT8 mask_data[8];
115      source = shaperam+4*tile;
116      mask_data[0] = source[0]>>8;
117      mask_data[1] = source[0]&0xff;
118      mask_data[2] = source[1]>>8;
119      mask_data[3] = source[1]&0xff;
120      mask_data[4] = source[2]>>8;
121      mask_data[5] = source[2]&0xff;
122      mask_data[6] = source[3]>>8;
123      mask_data[7] = source[3]&0xff;
124#else
125      UINT8 *mask_data = (UINT8 *)(shaperam+4*tile);
126#endif
127      SET_TILE_INFO( gfx,tile,tilemap_color,0 );
128      tileinfo->mask_data = mask_data;
129   }
130} /* roz_get_info */
131
132WRITE16_HANDLER( namcona1_rozvideoram_w )
133{
134   COMBINE_DATA( &namcona1_rozvideoram[offset] );
135   roz_dirty = 1;
136}
137READ16_HANDLER( namcona1_rozvideoram_r )
138{
139   return namcona1_rozvideoram[offset];
140}
141
85142/*************************************************************************/
86143
87144WRITE16_HANDLER( namcona1_videoram_w )
r2627r2628
100157static void
101158UpdatePalette(running_machine *machine, int offset )
102159{
103   UINT16 color;
104
105   color = paletteram16[offset];
106
107   /* -RRRRRGGGGGBBBBB */
160   UINT16 color = paletteram16[offset]; /* -RRRRRGGGGGBBBBB */
108161   palette_set_color_rgb( machine, offset, pal5bit(color >> 10), pal5bit(color >> 5), pal5bit(color >> 0));
109162} /* namcona1_paletteram_w */
110163
r2627r2628
221274   }
222275} /* namcona1_gfxram_w */
223276
224static void update_gfx(running_machine *machine)
277static void UpdateGfx(running_machine *machine)
225278{
226279   const UINT16 *pSource = videoram16;
227280   int page;
r2627r2628
229282
230283   if( dirtygfx )
231284   {
232      for( page = 0; page<4; page++ )
285      for( page = 0; page<NAMCONA1_NUM_TILEMAPS; page++ )
233286      {
234287         for( i=0; i<0x1000; i++ )
235288         {
r2627r2628
239292            }
240293         }
241294      }
295
296      pSource = namcona1_rozvideoram;
297      for( i=0; i<0x800; i++ )
298      {
299         if( dirtychar[*pSource++ & 0xfff] )
300         {
301            roz_dirty = 1;
302            break;
303         }
304      }
305
242306      for( i = 0;i < 0x1000;i++ )
243307      {
244308         if( dirtychar[i] )
r2627r2628
251315      }
252316      dirtygfx = 0;
253317   }
254} /* update_gfx */
318} /* UpdateGfx */
255319
256320VIDEO_START( namcona1 )
257321{
322   static const tile_get_info_func get_info[4] = { tilemap_get_info0, tilemap_get_info1, tilemap_get_info2, tilemap_get_info3 };
258323   int i;
259324   gfx_element *gfx0,*gfx1,*gfx2;
260   static const tile_get_info_func get_info[4] =
261   { tilemap_get_info0, tilemap_get_info1, tilemap_get_info2, tilemap_get_info3 };
262325
326   roz_tilemap = tilemap_create( roz_get_info, tilemap_scan_rows, 8,8,64,64 );
327   roz_palette = -1;
328
263329   for( i=0; i<NAMCONA1_NUM_TILEMAPS; i++ )
264330   {
265      bg_tilemap[i] = tilemap_create(
266         get_info[i],
267         tilemap_scan_rows,
268         8,8,64,64 );
269
331      bg_tilemap[i] = tilemap_create( get_info[i], tilemap_scan_rows, 8,8,64,64 );
270332      tilemap_palette_bank[i] = -1;
271333   }
272334
273   shaperam      = auto_malloc( 0x1000*4*2 );
274   cgram         = auto_malloc( 0x1000*0x20*2 );
275   dirtychar      = auto_malloc( 0x1000 );
335   shaperam           = auto_malloc( 0x2000*4 );
336   cgram              = auto_malloc( 0x1000*0x20*2 );
337   dirtychar           = auto_malloc( 0x1000 );
338   namcona1_rozvideoram = auto_malloc( 0x4000 );
276339
277      gfx0 = allocgfx( &cg_layout_8bpp );
278      gfx1 = allocgfx( &cg_layout_4bpp );
279      gfx2 = allocgfx( &shape_layout );
280         gfx0->total_colors = machine->config->total_colors/256;
281         machine->gfx[0] = gfx0;
340   gfx0 = allocgfx( &cg_layout_8bpp );
341   gfx0->total_colors = machine->config->total_colors/256;
342   machine->gfx[0] = gfx0;
282343
283         gfx1->total_colors = machine->config->total_colors/16;
284         machine->gfx[1] = gfx1;
344   gfx1 = allocgfx( &cg_layout_4bpp );
345   gfx1->total_colors = machine->config->total_colors/16;
346   machine->gfx[1] = gfx1;
285347
286         gfx2->total_colors = machine->config->total_colors/2;
287         machine->gfx[2] = gfx2;
348   gfx2 = allocgfx( &shape_layout );
349   gfx2->total_colors = machine->config->total_colors/2;
350   machine->gfx[2] = gfx2;
288351} /* namcona1_vh_start */
289352
290353/*************************************************************************/
r2627r2628
309372   UINT8 *mask_base = mask->gfxdata + (code % mask->total_elements) * mask->char_modulo;
310373
311374   int sprite_screen_height = ((1<<16)*gfx->height+0x8000)>>16;
312   int sprite_screen_width = ((1<<16)*gfx->width+0x8000)>>16;
375   int sprite_screen_width = ((1<<16)*gfx->width+0x8000)>>16;
313376
314377   if (sprite_screen_width && sprite_screen_height)
315378   {
r2627r2628
404467                        int c = source[x_index>>16];
405468
406469                        /* render a shadow only if the sprites color is $F (8bpp) or $FF (4bpp) */
407                        if( bShadow && ((gfx_region == 0 && color == 0x0f) || (gfx_region == 1 && color == 0xff)))
470                        if( bShadow )
408471                        {
409                           pen_t *palette_shadow_table = machine->shadow_table;
410                           dest[x] = palette_shadow_table[dest[x]];
472                           if( (gfx_region == 0 && color == 0x0f) ||
473                               (gfx_region == 1 && color == 0xff) )
474                           {
475                              pen_t *palette_shadow_table = machine->shadow_table;
476                              dest[x] = palette_shadow_table[dest[x]];
477                           }
478                           else
479                           { // bad?
480                              dest[x] = pal_base+c;
481                           }
411482                        }
412                        else if( bShadow && !((gfx_region == 0 && color == 0x0f) || (gfx_region == 1 && color == 0xff)))
413                        {
414                           //bad colors!
415                           dest[x] = pal_base + c;
416                        }
417483                        else
418484                        {
419485                           dest[x] = pal_base + c;
420486                        }
421487                     }
422
423488                     pri[x] = 0xff;
424489                  }
425490               }
r2627r2628
450515
451516   for( which=0; which<0x100; which++ )
452517   { /* max 256 sprites */
518      int bpp4,palbase;
453519      ypos   = source[0];   /* FHHH---Y YYYYYYYY    flipy, height, ypos */
454520      tile   = source[1];   /* O???TTTT TTTTTTTT    opaque tile */
455521      color   = source[2];   /* FSWWOOOO CCCCBPPP    flipx, shadow, width, color offset for 4bpp, color, 4bbp - 8bpp mode, pri*/
r2627r2628
461527      flipy = ypos&0x8000;
462528      flipx = color&0x8000;
463529
530      if( color&8 )
531      {
532         palbase = ((color>>4)&0xf) * 0x10 + ((color & 0xf00) >> 8);
533         bpp4 = 1;
534      }
535      else
536      {
537         palbase = (color>>4)&0xf;
538         bpp4 = 0;
539      }
540
464541      for( row=0; row<height; row++ )
465542      {
466543         sy = (ypos&0x1ff)-30+32;
r2627r2628
472549         {
473550            sy += row*8;
474551         }
552         sy = ((sy+8)&0x1ff)-8;
553
475554         for( col=0; col<width; col++ )
476555         {
477556            sx = (xpos&0x1ff)-10;
r2627r2628
483562            {
484563               sx+=col*8;
485564            }
565            sx = ((sx+16)&0x1ff)-8;
486566
487            if(color & 8)
488            {
489               pdraw_tile(machine,
490                  bitmap,
491                  cliprect,
492                  (tile & 0xfff) + row*64+col,
493                  ((color>>4)&0xf) * 0x10 + ((color & 0xf00) >> 8),
494                  ((sx+16)&0x1ff)-8,
495                  ((sy+8)&0x1ff)-8,
496                  flipx,flipy,
497                  priority,
498                  color & 0x4000, /* shadow */
499                  tile & 0x8000, /* opaque */
500                  1 /* 4bpp gfx */ );
501            }
502            else
503            {
504               pdraw_tile(machine,
505                  bitmap,
506                  cliprect,
507                  (tile & 0xfff) + row*64+col,
508                  (color>>4)&0xf,
509                  ((sx+16)&0x1ff)-8,
510                  ((sy+8)&0x1ff)-8,
511                  flipx,flipy,
512                  priority,
513                  color & 0x4000, /* shadow */
514                  tile & 0x8000, /* opaque */
515                  0 /* 8bpp gfx */ );
516            }
567            pdraw_tile(machine,
568               bitmap,
569               cliprect,
570               (tile & 0xfff) + row*64+col,
571               palbase,
572               sx,sy,flipx,flipy,
573               priority,
574               color & 0x4000, /* shadow */
575               tile & 0x8000, /* opaque */
576               bpp4 );
517577
518578         } /* next col */
519579      } /* next row */
r2627r2628
587647            draw_pixel_line(
588648               BITMAP_ADDR16(bitmap, line, 0),
589649               BITMAP_ADDR8(priority_bitmap, line, 0),
590               namcona1_sparevram + (ydata-0x4000) + 25,
650               namcona1_rozvideoram + (ydata-0x4000) + 25,
591651               paldata );
592652         }
593653         else
594654         {
595655            // used in emeraldia
596            if(which == 1 && namcona1_vreg[0x8a/2] == 0xfd /* maybe reg & 0x4 */)
656            if(which == NAMCONA1_NUM_TILEMAPS )// && namcona1_vreg[0x8a/2] == 0xfd /* maybe reg & 0x4 */)
597657            {
598658               int incxx = ((INT16)namcona1_vreg[0xc0/2])<<8; // or incyy ?
599659               int incxy = ((INT16)namcona1_vreg[0xc2/2])<<8;
r2627r2628
610670               UINT32 startx = (xoffset<<12)+incxx*dx+incyx*dy;
611671               UINT32 starty = (yoffset<<12)+incxy*dx+incyy*dy;
612672
613               tilemap_draw_roz_primask(bitmap, &clip, bg_tilemap[which],
673               tilemap_draw_roz_primask(bitmap, &clip, roz_tilemap,
614674                  startx, starty, incxx, incxy, incyx, incyy,
615675                  0, 0, primask, 0);
616
617676            }
618677            else
619678            {
r2627r2628
630689{
631690   int which;
632691   int priority;
692
633693   /* int flipscreen = namcona1_vreg[0x98/2]; (TBA) */
634694
635695   if( namcona1_vreg[0x8e/2] )
r2627r2628
643703         }
644704         palette_is_dirty = 0;
645705      }
646      update_gfx(screen->machine);
706      UpdateGfx(screen->machine);
647707      for( which=0; which<NAMCONA1_NUM_TILEMAPS; which++ )
648708      {
649         static int tilemap_color;
650         tilemap_color = namcona1_vreg[0x58+(which&3)]&0xf;
709         int tilemap_color = namcona1_vreg[0xb0/2+(which&3)]&0xf;
651710         if( tilemap_color!=tilemap_palette_bank[which] )
652711         {
653712            tilemap_mark_all_tiles_dirty( bg_tilemap[which] );
654713            tilemap_palette_bank[which] = tilemap_color;
655714         }
656715      } /* next tilemap */
716
717      { /* ROZ tilemap */
718         int color = namcona1_vreg[0xba/2]&0xf;
719         if( color != roz_palette )
720         {
721            roz_dirty = 1;
722            roz_palette = color;
723         }
724      }
725
726      if( roz_dirty )
727      {
728         tilemap_mark_all_tiles_dirty( roz_tilemap );
729         roz_dirty = 0;
730      }
731
657732      fillbitmap( priority_bitmap,0,cliprect );
658733
659      // It fixes bg in emeralda
660      fillbitmap( bitmap,/* 0 */ 0xff,cliprect );
734      fillbitmap( bitmap, 0xff, cliprect ); /* background color? */
661735
662736      for( priority = 0; priority<8; priority++ )
663737      {
664         for( which=NAMCONA1_NUM_TILEMAPS-1; which>=0; which-- )
738         for( which=4; which>=0; which-- )
665739         {
666            if( (namcona1_vreg[0x50+which]&0x7) == priority )
740            int pri = namcona1_vreg[0xa0/2+which]&0x7; /* likely wrong for roz plane */
741            if( pri == priority )
667742            {
668743               draw_background(screen->machine,bitmap,cliprect,which,priority);
669744            }
670745         } /* next tilemap */
671746      } /* next priority level */
747
672748      draw_sprites(screen->machine,bitmap,cliprect);
673749   } /* gfx enabled */
674750   return 0;
675751}
752
753/*
754$efff20: sprite control: 0x3a,0x3e,0x3f
755            bit 0x01 selects spriteram bank
756
757               0    2    4    6    8    a    c    e
758$efff00:    src0 src1 src2 dst0 dst1 dst2 BANK [src
759$efff10:    src] [dst dst] #BYT BLIT eINT 001f 0001
760$efff20:    003f 003f IACK ---- ---- ---- ---- ----
761$efff80:    0050 0170 0020 0100 0000 POSI 0000 GFXE     POSI: scanline for POSIRQ
762$efff90:    0000 0001 0002 0003 FLIP ---- ---- ----
763$efffa0:    PRI  PRI  PRI  PRI  ---- PRI? --?? ----     priority (0..7)
764$efffb0:    CLR  CLR  CLR  CLR  0001 CLR? BPP  ----     color (0..f), bpp flag per layer
765$efffc0:    RZXX RZXY RZYX RZYY RZX0 RZY0 ???? ----     ROZ
766*/
767
src/mame/video/cps1.c
r2627r2628
2222------------------------------------------- ----  --------  ---------------------  ---------  -----------------------  -------------
2323Forgotten Worlds / Lost Worlds              1988  88618B-2  LWCHR            LWIO  None       CPS-B-01  DL-0411-10001  None
2424  (alt B-board revision)                          88621B-2  LW621            LWIO  ?
25Ghouls 'n Ghosts (World / US)               1988  88620B-2 DM620            LWIO  None       CPS-B-01  DL-0411-10001  None
25Ghouls 'n Ghosts (World / US)               1988  88620-B-2 DM620            LWIO  None       CPS-B-01  DL-0411-10001  None
2626  (alt B-board revision - Japan)                  88622B-2  DM22A            LWIO  88622-C-1  CPS-B-01  DL-0411-10001  None
2727Strider                                     1989  89624B-2  ST24M1           LWIO  88622-C-1  CPS-B-01  DL-0411-10001  None
2828  (alt B-board revision)                          89624B-2  ST24M2           LWIO  88622-C-2  CPS-B-01  DL-0411-10001  None
2929  (alt B-board revision)                          89624B-3  ST24B2           LWIO  88622-C-2  CPS-B-01  DL-0411-10001  None
3030  (alt B-board revision - Japan)                  ?         ST22B            ?
3131Dynasty Wars                                1989  89624B-3  TK24B?           ?     88622-C-2  CPS-B-02  DL-0411-10002  None
32  (alt B-board revision - Japan)                  88622B-3  TK22B            IOB1
32  (alt B-board revision - Japan)                  88622B-3  TK22B            LWIO
3333Willow                                      1989  89624B-3  WL24B            LWIO  88622-C-4  CPS-B-03  DL-0411-10003  None
3434  (alt B-board revision - Japan)                  88622B-3  WL22B            LWIO  88622-C-2  CPS-B-03  DL-0411-10003  None
3535UN Squadron / Area 88                       1989  89624B-3  AR24B            LWIO  88622-C-4  CPS-B-11  DL-0411-10004  None
3636  (alt B-board revision - Japan)                  88622B-2  AR22B            LWIO
37  (alt B-board revision - Japan)                  88622B-3  AR22B            LWIO
3738Final Fight (first rev)                     1989  89624B-3  S224B            IOB1  88622-C-5  CPS-B-04  DL-0411-10005  None
3839Final Fight (US/Japan 900112)                                                      ?          CPS-B-04@ DL-0411-10001  None
3940Final Fight (US 900613)                           89624B-3                   IOB1  88622-C-5  CPS-B-05  DL-0411-10006  None
4041Final Fight (Japan 900305) (alt B-board revision) 88622B-2  S222B            LWIO  88622-C-5  CPS-B-02  DL-0411-10002  None
41421941                                        1990  ?         YI24B            ?     88622-C-5  CPS-B-05  DL-0411-10006  ?
42  (alt B-board revision - Japan)                  ?         ?                ?
43  (alt B-board revision - Japan)                  ?         YI22B?           ?
4344Mercs                                       1990  89624B-3  O224B            IOB1  90628-C-1  CPS-B-12  DL-0411-10007  C628
44  (alt B-board revision - Japan)                  ?         ?                ?
45  (alt B-board revision - Japan)                  ?         O222B?           ?
4546  (alt C-board revision)                                                           90628-C-2
4647Magic Sword                                 1990  89624B-3  MS24B            IOB1  88622-C-5  CPS-B-13  DL-0411-10008  None
4748  (alt B-board revision - Japan)                  89625B-1  MS22B            IOB1
4849Chiki Chiki Boys / Mega Twins               1990  89624B-3  CK24B            IOB1  88622-C-5  CPS-B-14  DL-0411-10009  None
49  (alt B-board revision - Japan)                  ?         ?                ?
50  (alt B-board revision - Japan)                  ?         CK22B?           ?
5051Nemo                                        1990  89624B-3  NM24B            IOB1  88622-C-5  CPS-B-15  DL-0411-10010  None
5152  (alt B-board revision - Japan)                  89625B-1  NM22B            ?
5253Carrier Air Wing / U.S. Navy                1990  89624B-3  CA24B            IOB1  88622-C-5  CPS-B-16  DL-0411-10011  None
r2627r2628
6364Street Fighter II (Japan 911210)                                                              CPS-B-13  DL-0411-10008
6465Three Wonders*                              1991  89624B-3  RT24B            LWIO  90630C-4   CPS-B-21  DL-0921-10014        IOC1
6566  (alt B-board revision - Japan)                  89625B-1  RT22B            IOB1
66  (alt B-board revision)                          91634B-2  ?                IOB1
67  (alt B-board revision)                          91634B-2  RT63B?           IOB1
6768King of Dragons*                            1991  90629B-3  KD29B            IOB1  90631C-5   CPS-B-21  DL-0921-10014  C632  IOC1
6869Captain Commando*                           1991  91635B-2  CC63B     CCPRG  IOB1  90631C-5   CPS-B-21  DL-0921-10014  C632  IOC1
6970Knights of the Round*                       1991  91635B-2  KR63B     BPRG1  IOB1  90631C-5   CPS-B-21  DL-0921-10014  C632  IOC1
70  (alt B-board revision)                          ?         ?                ?
71  (alt B-board revision)                          ?         KR29B?           ?
7172Street Fighter II' Champion Edition         1992  91635B-2  S9263B    BPRG1  IOB1  92631C-6   CPS-B-21  DL-0921-10014  C632  IOC1
7273  (alt B-board revision - Japan)                  91634B-2  S9263B    BPRG1  IOB1
7374Varth (World)                               1992  89624B-3  VA24B            IOB1  88622-C-5  CPS-B-04  DL-0411-10005  None
7475  (alt B-board revision - US)                     ?         VA63B?           ?
75Varth (Japan)*                                    ?         ?                      ?          CPS-B-21  DL-0921-10014  ?
76Varth (Japan)*                                    ?         VA22B?           ?     ?          CPS-B-21  DL-0921-10014  ?
7677Capcom World 2*                             1992  ?         Q522B            IOB1  ?          CPS-B-21  DL-0921-10014        IOC1
7778Quiz and Dragons (US)*                      1992  89625B-1  QD22B            IOB1  92641C-1   CPS-B-21  DL-0921-10014        IOC1
7879Quiz and Dragons (Japan)*                   1994  ?         ?                IOB1  ?          CPS-B-21  DL-0921-10014        IOC1
src/mame/video/wrally.c
r2627r2628
1212UINT16 *wrally_vregs;
1313UINT16 *wrally_videoram;
1414
15static tilemap *pant[2];
15tilemap *wrally_pant[2];
1616
17/* from machine/wrally.c */
18extern UINT16 *wrally_encr_table[2];
19
2017/***************************************************************************
2118
2219    Callbacks for the TileMap code
r2627r2628
3330    -----+-FEDCBA98-76543210-+--------------------------
3431      0  | --xxxxxx xxxxxxxx | code
3532      0  | xx------ -------- | not used?
36      1  | xxxxxxxx xxxxxxxx | unknown
37
38      preliminary
33      1  | -------- ---xxxxx | color
34      1  | -------- --x----- | priority
35      1  | -------- -x------ | flip y
36      1  | -------- x------- | flip x
37      1  | ---xxxxx -------- | data used to handle collisions, speed, etc
38      1  | xxx----- -------- | not used?
3939*/
4040
41
4241static TILE_GET_INFO( get_tile_info_wrally_screen0 )
4342{
4443   int data = wrally_videoram[tile_index << 1];
4544   int data2 = wrally_videoram[(tile_index << 1) + 1];
4645   int code = data & 0x3fff;
4746
48   SET_TILE_INFO(0, code, data2 & 0x1f, TILE_FLIPXY((data2 >> 5) & 0x00));
47   tileinfo->category = (data2 >> 5) & 0x01;
48
49   SET_TILE_INFO(0, code, data2 & 0x1f, TILE_FLIPYX((data2 >> 6) & 0x03));
4950}
5051
5152static TILE_GET_INFO( get_tile_info_wrally_screen1 )
r2627r2628
5455   int data2 = wrally_videoram[(0x2000/2) + (tile_index << 1) + 1];
5556   int code = data & 0x3fff;
5657
57   SET_TILE_INFO(0, code, data2 & 0x1f, TILE_FLIPXY((data2 >> 5) & 0x00));
58}
58   tileinfo->category = (data2 >> 5) & 0x01;
5959
60/***************************************************************************
61
62    Memory Handlers
63
64***************************************************************************/
65
66WRITE16_HANDLER( wrally_vram_w )
67{
68   wrally_videoram[offset] = wrally_encr_table[offset & 0x01][data];
69
70   tilemap_mark_tile_dirty(pant[(offset & 0x1fff) >> 12], ((offset << 1) & 0x1fff) >> 2);
60   SET_TILE_INFO(0, code, data2 & 0x1f, TILE_FLIPYX((data2 >> 6) & 0x03));
7161}
7262
73
7463/***************************************************************************
7564
7665    Start/Stop the video hardware emulation.
r2627r2628
7968
8069VIDEO_START( wrally )
8170{
82   pant[0] = tilemap_create(get_tile_info_wrally_screen0,tilemap_scan_rows,16,16,64,32);
83   pant[1] = tilemap_create(get_tile_info_wrally_screen1,tilemap_scan_rows,16,16,64,32);
71   wrally_pant[0] = tilemap_create(get_tile_info_wrally_screen0,tilemap_scan_rows,16,16,64,32);
72   wrally_pant[1] = tilemap_create(get_tile_info_wrally_screen1,tilemap_scan_rows,16,16,64,32);
8473
85   tilemap_set_transparent_pen(pant[0],0);
86   tilemap_set_transparent_pen(pant[1],0);
74   tilemap_set_transmask(wrally_pant[0],0,0xff01,0x00ff); /* this layer is split in two (pens 1..7, pens 8-15) */
75   tilemap_set_transparent_pen(wrally_pant[1],0);
8776}
8877
8978
r2627r2628
10695      1  | xxxxxxxx xxxxxxxx | unknown
10796      2  | ------xx xxxxxxxx | x position
10897      2  | --xxxx-- -------- | sprite color (low 4 bits)
109      2  | xx------ -------- | unknown
98      2  | -x------ -------- | shadows/highlights (see below)
99      2  | x------- -------- | not used?
110100      3  | --xxxxxx xxxxxxxx | sprite code
111101      3  | xx------ -------- | not used?
112102
113      preliminary
103    For shadows/highlights, the tile color below the sprite will be set using a
104    palette (from the 8 available) based on the gfx pen of the sprite. Only pens
105    in the range 0x8-0xf are used.
114106*/
115107
116static void draw_sprites(running_machine *machine, bitmap_t *bitmap, const rectangle *cliprect)
108static void draw_sprites(running_machine *machine, bitmap_t *bitmap, const rectangle *cliprect, int priority)
117109{
118   int i, x, y, ex, ey;
110   int i, px, py;
119111   const gfx_element *gfx = machine->gfx[0];
120112
121   static const int x_offset[2] = {0x0,0x2};
122   static const int y_offset[2] = {0x0,0x1};
123
124   for (i = 3; i < (0x1000 - 6)/2; i += 4){
113   for (i = 6/2; i < (0x1000 - 6)/2; i += 4) {
125114      int sx = wrally_spriteram[i+2] & 0x03ff;
126115      int sy = (240 - (wrally_spriteram[i] & 0x00ff)) & 0x00ff;
127116      int number = wrally_spriteram[i+3] & 0x3fff;
128      int color = ((wrally_spriteram[i+2] & 0xfc00) >> 10);
117      int color = (wrally_spriteram[i+2] & 0x7c00) >> 10;
129118      int attr = (wrally_spriteram[i] & 0xfe00) >> 9;
130119
131120      int xflip = attr & 0x20;
132121      int yflip = attr & 0x40;
133      int spr_size = 1;
122      int color_effect = (color & 0x10) >> 4;
123      int high_priority = number >= 0x3700;
124      color = color & 0x0f;
134125
135      color = (color & 0x0f);
126      if (high_priority != priority) continue;
136127
137      for (y = 0; y < spr_size; y++){
138         for (x = 0; x < spr_size; x++){
128      if (flip_screen_get()) {
129         sy = sy + 248;
130      }
139131
140            ex = xflip ? (spr_size-1-x) : x;
141            ey = yflip ? (spr_size-1-y) : y;
132      if (!color_effect) {
133         drawgfx(bitmap,gfx,number,
134               0x20 + color,xflip,yflip,
135               sx - 0x0f,sy,
136               cliprect,TRANSPARENCY_PEN,0);
137      } else {
138         /* get a pointer to the current sprite's gfx data */
139         UINT8 *gfx_src = gfx->gfxdata + (number % gfx->total_elements)*gfx->char_modulo;
142140
143            drawgfx(bitmap,gfx,number + x_offset[ex] + y_offset[ey],
144                  0x20 + color,xflip,yflip,
145                  sx-0x0f+x*16,sy+y*16,
146                  cliprect,TRANSPARENCY_PEN,0);
141         for (py = 0; py < gfx->height; py++){
142            /* get a pointer to the current line in the screen bitmap */
143            int ypos = ((sy + py) & 0x1ff);
144            UINT16 *srcy = BITMAP_ADDR16(bitmap, ypos, 0);
145
146            int gfx_py = yflip ? (gfx->height - 1 - py) : py;
147
148            if ((ypos < cliprect->min_y) || (ypos > cliprect->max_y)) continue;
149
150            for (px = 0; px < gfx->width; px++){
151               /* get current pixel */
152               int xpos = (((sx + px) & 0x3ff) - 0x0f) & 0x3ff;
153               UINT16 *pixel = srcy + xpos;
154               int src_color = *pixel;
155
156               int gfx_px = xflip ? (gfx->width - 1 - px) : px;
157
158               /* get asociated pen for the current sprite pixel */
159               int gfx_pen = gfx_src[gfx->line_modulo*gfx_py + gfx_px];
160
161               /* pens 8..15 are used to select a palette */
162               if ((gfx_pen < 8) || (gfx_pen >= 16)) continue;
163
164               if ((xpos < cliprect->min_x) || (xpos > cliprect->max_x)) continue;
165
166               /* modify the color of the tile */
167               *pixel = src_color + (gfx_pen-8)*1024;
168            }
147169         }
148170      }
149171   }
r2627r2628
158180VIDEO_UPDATE( wrally )
159181{
160182   /* set scroll registers */
161   tilemap_set_scrolly(pant[0], 0, wrally_vregs[0]);
162   tilemap_set_scrollx(pant[0], 0, wrally_vregs[1]);
163   tilemap_set_scrolly(pant[1], 0, wrally_vregs[2]);
164   tilemap_set_scrollx(pant[1], 0, wrally_vregs[3]);
183   if (!flip_screen_get()) {
184      tilemap_set_scrolly(wrally_pant[0], 0, wrally_vregs[0]);
185      tilemap_set_scrollx(wrally_pant[0], 0, wrally_vregs[1]+4);
186      tilemap_set_scrolly(wrally_pant[1], 0, wrally_vregs[2]);
187      tilemap_set_scrollx(wrally_pant[1], 0, wrally_vregs[3]);
188   } else {
189      tilemap_set_scrolly(wrally_pant[0], 0, 248 - wrally_vregs[0]);
190      tilemap_set_scrollx(wrally_pant[0], 0, 1024 - wrally_vregs[1] - 4);
191      tilemap_set_scrolly(wrally_pant[1], 0, 248 - wrally_vregs[2]);
192      tilemap_set_scrollx(wrally_pant[1], 0, 1024 - wrally_vregs[3]);
193   }
165194
166   fillbitmap( bitmap, 0, cliprect );
195   /* draw tilemaps + sprites */
196   tilemap_draw(bitmap,cliprect,wrally_pant[1],TILEMAP_DRAW_OPAQUE,0);
197   tilemap_draw(bitmap,cliprect,wrally_pant[0],TILEMAP_DRAW_CATEGORY(0) | TILEMAP_DRAW_LAYER0,0);
198   tilemap_draw(bitmap,cliprect,wrally_pant[0],TILEMAP_DRAW_CATEGORY(0) | TILEMAP_DRAW_LAYER1,0);
167199
168   tilemap_draw(bitmap,cliprect,pant[1],0,0);
169   tilemap_draw(bitmap,cliprect,pant[0],0,0);
170   draw_sprites(screen->machine, bitmap,cliprect);
200   tilemap_draw(bitmap,cliprect,wrally_pant[1],TILEMAP_DRAW_CATEGORY(1),0);
201   tilemap_draw(bitmap,cliprect,wrally_pant[0],TILEMAP_DRAW_CATEGORY(1) | TILEMAP_DRAW_LAYER0,0);
202
203   draw_sprites(screen->machine,bitmap,cliprect,0);
204
205   tilemap_draw(bitmap,cliprect,wrally_pant[0],TILEMAP_DRAW_CATEGORY(1) | TILEMAP_DRAW_LAYER1,0);
206
207   draw_sprites(screen->machine,bitmap,cliprect,1);
208
171209   return 0;
172210}
src/mame/drivers/targeth.c
r2627r2628
5454   }
5555}
5656
57
58static ADDRESS_MAP_START( targeth_readmem, ADDRESS_SPACE_PROGRAM, 16 )
59   AM_RANGE(0x000000, 0x0fffff) AM_READ(SMH_ROM)         /* ROM */
60   AM_RANGE(0x100000, 0x103fff) AM_READ(SMH_RAM)         /* Video RAM */
61   AM_RANGE(0x108000, 0x108001) AM_READ(input_port_0_word_r)/* Gun 1P X */
62   AM_RANGE(0x108002, 0x108003) AM_READ(input_port_1_word_r)/* Gun 1P Y */
63   AM_RANGE(0x108004, 0x108005) AM_READ(input_port_2_word_r)/* Gun 2P X */
64   AM_RANGE(0x108006, 0x108007) AM_READ(input_port_3_word_r)/* Gun 2P Y */
65   AM_RANGE(0x200000, 0x2007ff) AM_READ(SMH_RAM)         /* Palette */
66   AM_RANGE(0x440000, 0x440fff) AM_READ(SMH_RAM)         /* Sprite RAM */
67   AM_RANGE(0x700000, 0x700001) AM_READ(input_port_4_word_r)/* DIPSW #2 */
68   AM_RANGE(0x700002, 0x700003) AM_READ(input_port_5_word_r)/* DIPSW #1 */
69   AM_RANGE(0x700006, 0x700007) AM_READ(input_port_6_word_r)/* Coins, Start & Fire buttons */
70   AM_RANGE(0x700008, 0x700009) AM_READ(input_port_7_word_r)/* Service & Guns Reload? */
71   AM_RANGE(0x70000e, 0x70000f) AM_READ(OKIM6295_status_0_lsb_r)/* OKI6295 status register */
72   AM_RANGE(0xfe0000, 0xfeffff) AM_READ(SMH_RAM)         /* Work RAM (partially shared with DS5002FP) */
73ADDRESS_MAP_END
74
7557static WRITE16_HANDLER( OKIM6295_bankswitch_w )
7658{
7759   UINT8 *RAM = memory_region(REGION_SOUND1);
r2627r2628
8668   coin_counter_w( (offset >> 3) & 0x01, data & 0x01);
8769}
8870
89static ADDRESS_MAP_START( targeth_writemem, ADDRESS_SPACE_PROGRAM, 16 )
90   AM_RANGE(0x000000, 0x0fffff) AM_WRITE(SMH_ROM)                        /* ROM */
91   AM_RANGE(0x100000, 0x103fff) AM_WRITE(targeth_vram_w) AM_BASE(&targeth_videoram)      /* Video RAM */
92   AM_RANGE(0x108000, 0x108007) AM_WRITE(SMH_RAM) AM_BASE(&targeth_vregs)            /* Video Registers */
93   AM_RANGE(0x10800c, 0x10800d) AM_WRITE(SMH_NOP)                        /* CLR Video INT */
94   AM_RANGE(0x200000, 0x2007ff) AM_WRITE(paletteram16_xBBBBBGGGGGRRRRR_word_w) AM_BASE(&paletteram16)/* Palette */
95   AM_RANGE(0x440000, 0x440fff) AM_WRITE(SMH_RAM) AM_BASE(&targeth_spriteram)         /* Sprite RAM */
96   AM_RANGE(0x70000c, 0x70000d) AM_WRITE(OKIM6295_bankswitch_w)               /* OKI6295 bankswitch */
97   AM_RANGE(0x70000e, 0x70000f) AM_WRITE(OKIM6295_data_0_lsb_w)               /* OKI6295 data register */
98   AM_RANGE(0x70000a, 0x70001b) AM_WRITE(SMH_NOP)                        /* ??? Guns reload related? */
99   AM_RANGE(0x70002a, 0x70003b) AM_WRITE(targeth_coin_counter_w)               /* Coin counters */
100   AM_RANGE(0xfe0000, 0xfeffff) AM_WRITE(SMH_RAM)                        /* Work RAM (partially shared with DS5002FP) */
71static ADDRESS_MAP_START( main_map, ADDRESS_SPACE_PROGRAM, 16 )
72   AM_RANGE(0x000000, 0x0fffff) AM_ROM
73   AM_RANGE(0x100000, 0x103fff) AM_RAM_WRITE(targeth_vram_w) AM_BASE(&targeth_videoram)   /* Video RAM */
74   AM_RANGE(0x108000, 0x108007) AM_WRITE(SMH_RAM) AM_BASE(&targeth_vregs)   /* Video Registers */
75   AM_RANGE(0x108000, 0x108001) AM_READ(input_port_0_word_r)      /* Gun 1P X */
76   AM_RANGE(0x108002, 0x108003) AM_READ(input_port_1_word_r)      /* Gun 1P Y */
77   AM_RANGE(0x108004, 0x108005) AM_READ(input_port_2_word_r)      /* Gun 2P X */
78   AM_RANGE(0x108006, 0x108007) AM_READ(input_port_3_word_r)      /* Gun 2P Y */
79   AM_RANGE(0x108000, 0x108007) AM_WRITE(SMH_RAM) AM_BASE(&targeth_vregs)   /* Video Registers */
80   AM_RANGE(0x10800c, 0x10800d) AM_WRITE(SMH_NOP)               /* CLR Video INT */
81   AM_RANGE(0x200000, 0x2007ff) AM_RAM_WRITE(paletteram16_xBBBBBGGGGGRRRRR_word_w) AM_BASE(&paletteram16)   /* Palette */
82   AM_RANGE(0x440000, 0x440fff) AM_RAM AM_BASE(&targeth_spriteram)   /* Sprite RAM */
83   AM_RANGE(0x700000, 0x700001) AM_READ(input_port_4_word_r)      /* DIPSW #2 */
84   AM_RANGE(0x700002, 0x700003) AM_READ(input_port_5_word_r)      /* DIPSW #1 */
85   AM_RANGE(0x700006, 0x700007) AM_READ(input_port_6_word_r)      /* Coins, Start & Fire buttons */
86   AM_RANGE(0x700008, 0x700009) AM_READ(input_port_7_word_r)      /* Service & Guns Reload? */
87   AM_RANGE(0x70000a, 0x70001b) AM_WRITE(SMH_NOP)               /* ??? Guns reload related? */
88   AM_RANGE(0x70000c, 0x70000d) AM_WRITE(OKIM6295_bankswitch_w)   /* OKI6295 bankswitch */
89   AM_RANGE(0x70000e, 0x70000f) AM_READWRITE(OKIM6295_status_0_lsb_r, OKIM6295_data_0_lsb_w)   /* OKI6295 status register */
90   AM_RANGE(0x70002a, 0x70003b) AM_WRITE(targeth_coin_counter_w)   /* Coin counters */
91   AM_RANGE(0xfe0000, 0xfeffff) AM_RAM                        /* Work RAM (partially shared with DS5002FP) */
10192ADDRESS_MAP_END
10293
103
10494static INPUT_PORTS_START( targeth )
10595PORT_START   /* Gun 1 X */
10696   PORT_BIT( 0x01ff, 200, IPT_LIGHTGUN_X ) PORT_CROSSHAIR(X, 1.05, -0.028, 0) PORT_MINMAX( 0, 400 + 4) PORT_SENSITIVITY(100) PORT_KEYDELTA(20) PORT_PLAYER(1)
r2627r2628
190180
191181   /* basic machine hardware */
192182   MDRV_CPU_ADD(M68000,24000000/2)         /* 12 MHz */
193   MDRV_CPU_PROGRAM_MAP(targeth_readmem,targeth_writemem)
183   MDRV_CPU_PROGRAM_MAP(main_map,0)
194184   MDRV_CPU_VBLANK_INT_HACK(targeth_interrupt,3)
195185
196186   /* video hardware */
src/mame/drivers/blktiger.c
r2627r2628
253253static MACHINE_DRIVER_START( blktiger )
254254
255255   /* basic machine hardware */
256   MDRV_CPU_ADD(Z80, 4000000)   /* 4 MHz (?) */
256   MDRV_CPU_ADD(Z80, XTAL_24MHz/4)   /* verified on pcb */
257257   MDRV_CPU_PROGRAM_MAP(mem_map, 0)
258258   MDRV_CPU_IO_MAP(port_map, 0)
259259   MDRV_CPU_VBLANK_INT("main", irq0_line_hold)
260260
261   MDRV_CPU_ADD(Z80, 3000000)
261   MDRV_CPU_ADD(Z80, XTAL_3_579545MHz) /* verified on pcb */
262262   /* audio CPU */
263263   MDRV_CPU_PROGRAM_MAP(sound_mem_map, 0)
264264
r2627r2628
284284   /* sound hardware */
285285   MDRV_SPEAKER_STANDARD_MONO("mono")
286286
287   MDRV_SOUND_ADD(YM2203, 3579545)
287   MDRV_SOUND_ADD(YM2203, XTAL_3_579545MHz) /* verified on pcb */
288288   MDRV_SOUND_CONFIG(ym2203_interface)
289289   MDRV_SOUND_ROUTE(ALL_OUTPUTS, "mono", 0.15)
290290
291   MDRV_SOUND_ADD(YM2203, 3579545)
291   MDRV_SOUND_ADD(YM2203, XTAL_3_579545MHz) /* verified on pcb */
292292   MDRV_SOUND_ROUTE(ALL_OUTPUTS, "mono", 0.15)
293293MACHINE_DRIVER_END
294294
src/mame/drivers/dynax.c
r2627r2628
39573957   MDRV_SOUND_ADD(MSM5205, 384000)
39583958   MDRV_SOUND_CONFIG(jantouki_msm5205_interface)
39593959   MDRV_SOUND_ROUTE(ALL_OUTPUTS, "mono", 1.0)
3960
3961   /* devices */
3962   MDRV_DEVICE_ADD("rtc", MSM6242)
39603963MACHINE_DRIVER_END
39613964
39623965
r2627r2628
60906093GAME( 1990, mjelct3a, mjelctrn, mjelctrn, mjelct3,  mjelct3a, ROT180, "Dynax",                    "Mahjong Electron Base (parts 2 & 3, alt., Japan)",             0 )
60916094GAME( 1990, hjingi,   0,        majxtal7, majxtal7, 0,        ROT0,   "Dynax",                    "Hanajingi",                                                    GAME_NOT_WORKING )
60926095GAME( 1990, majxtal7, 0,        majxtal7, majxtal7, mjelct3,  ROT180, "Dynax",                    "Mahjong X-Tal 7 - Crystal Mahjong / Mahjong Diamond 7 (Japan)",GAME_IMPERFECT_GRAPHICS )
6093GAME( 1990, neruton,  0,        neruton,  neruton,  mjelct3,  ROT180, "Dynax / Yukiyoshi Tokoro", "Mahjong Neruton Haikujirada (Japan)",                          GAME_IMPERFECT_GRAPHICS )
6096GAME( 1990, neruton,  0,        neruton,  neruton,  mjelct3,  ROT180, "Dynax / Yukiyoshi Tokoro", "Mahjong Neruton Haikujiradan (Japan)",                         GAME_IMPERFECT_GRAPHICS )
60946097GAME( 1991, hanayara, 0,        yarunara, hanayara, 0,        ROT180, "Dynax",                    "Hana wo Yaraneba! (Japan)",                                    0 )
60956098GAME( 1991, mjcomv1,  0,        yarunara, yarunara, 0,        ROT180, "Dynax",                    "Mahjong Comic Gekijou Vol.1 (Japan)",                          0 )
60966099GAME( 1991, tenkai,   0,        tenkai,   tenkai,   0,        ROT180, "Dynax",                    "Mahjong Tenkaigen",                                            GAME_NOT_WORKING )
src/mame/drivers/megadriv.c
r2627r2628
5353
5454#define MEGADRIV_VDP_VRAM(address) megadrive_vdp_vram[(address)&0x7fff]
5555
56#ifdef MESS
57extern void setup_megadriv_custom_mappers(running_machine *);
58#endif
59
6056/* the same on all systems? */
6157#define MASTER_CLOCK      53693100
6258/* timing details */
r2627r2628
48204816
48214817   memset(megadrive_ram,0x00,0x10000);
48224818
4823#ifdef MESS
4824   setup_megadriv_custom_mappers(machine);
4825#endif
48264819
48274820}
48284821
src/mame/drivers/undrfire.c
r2627r2628
142142|                                           TC511664-80     MB3771                             |
143143|                                                                                            P1|
144144|  MACH120   MACH120                                                                           |
145|                                     ENSONIC         30.4761MHz  16MHz   ADC0809              |
145|                                     ENSONIQ         30.4761MHz  16MHz   ADC0809              |
146146|                                     ESP-R6                                                   |
147|                                                       ENSONIC                                |
148|                                     ENSONIC           5701     DSW1(8)  TC0510NIO            |
147|                                                       ENSONIQ                                |
148|                                     ENSONIQ           5701     DSW1(8)  TC0510NIO            |
149149|                                     OTIS-R2                                                 Z|
150150|                                                         93C46                                |
151151|           C3                              C4                                                 |
r2627r2628
221221
222222   {
223223      a = paletteram32[offset];
224      r = (a &0xff0000) >> 16;
225      g = (a &0xff00) >> 8;
226      b = (a &0xff);
224      r = (a & 0xff0000) >> 16;
225      g = (a & 0xff00) >> 8;
226      b = (a & 0xff);
227227
228228      palette_set_color(machine,offset,MAKE_RGB(r,g,b));
229229   }
r2627r2628
234234                INTERRUPTS
235235***********************************************************/
236236
237static TIMER_CALLBACK( undrfire_interrupt5 )
237static TIMER_CALLBACK( interrupt5 )
238238{
239   cpunum_set_input_line(machine, 0,5,HOLD_LINE);
239   cpunum_set_input_line(machine, 0, 5, HOLD_LINE);
240240}
241241
242242
r2627r2628
388388static WRITE32_HANDLER( unknown_int_req_w )
389389{
390390   /* 10000 cycle delay is arbitrary */
391   timer_set(ATTOTIME_IN_CYCLES(10000,0), NULL, 0, undrfire_interrupt5);
391   timer_set(ATTOTIME_IN_CYCLES(10000,0), NULL, 0, interrupt5);
392392}
393393
394394
r2627r2628
430430
431431static WRITE32_HANDLER( rotate_control_w )   /* only a guess that it's rotation */
432432{
433      if (ACCESSING_BITS_0_15)
434      {
435         undrfire_rotate_ctrl[port_sel] = data;
436         return;
437      }
433   if (ACCESSING_BITS_0_15)
434   {
435      undrfire_rotate_ctrl[port_sel] = data;
436      return;
437   }
438438
439      if (ACCESSING_BITS_16_31)
440      {
441         port_sel = (data &0x70000) >> 16;
442      }
439   if (ACCESSING_BITS_16_31)
440   {
441      port_sel = (data &0x70000) >> 16;
442   }
443443}
444444
445445
r2627r2628
461461
462462static WRITE32_HANDLER( cbombers_cpua_ctrl_w )
463463{
464   cpunum_set_input_line(machine, 2, INPUT_LINE_RESET, (data &0x1000) ? CLEAR_LINE : ASSERT_LINE);
464/*
465    ........ ..xxxxxx   Lamp 1-6 enables
466    ........ .x......   Vibration
467*/
468
469   cpunum_set_input_line(machine, 2, INPUT_LINE_RESET, (data & 0x1000) ? CLEAR_LINE : ASSERT_LINE);
465470}
466471
472static READ32_HANDLER( cbombers_adc_r )
473{
474   return (input_port_read_indexed(machine, 3) << 24);
475}
476
477static WRITE32_HANDLER( cbombers_adc_w )
478{
479   /* One interrupt per input port (4 per frame, though only 2 used).
480        1000 cycle delay is arbitrary */
481   timer_set(ATTOTIME_IN_CYCLES(1000, 0), NULL, 0, interrupt5);
482}
483
467484/***********************************************************
468485             MEMORY STRUCTURES
469486***********************************************************/
r2627r2628
488505   AM_RANGE(0xf00000, 0xf00007) AM_READ(undrfire_lightgun_r)   /* stick coords read at $11b2-bc */
489506ADDRESS_MAP_END
490507
491static ADDRESS_MAP_START( cbombers_readmem, ADDRESS_SPACE_PROGRAM, 32 )
492   AM_RANGE(0x000000, 0x1fffff) AM_READ(SMH_ROM)
493   AM_RANGE(0x200000, 0x21ffff) AM_READ(SMH_RAM)   /* main CPUA ram */
494   AM_RANGE(0x300000, 0x303fff) AM_READ(SMH_RAM)   /* Sprite ram */
495   AM_RANGE(0x800000, 0x80ffff) AM_READ(TC0480SCP_long_r)
496   AM_RANGE(0x830000, 0x83002f) AM_READ(TC0480SCP_ctrl_long_r)
497//  AM_RANGE(0x340000, 0x340003) AM_READ(superchs_stick_r)  /* stick coord read */
498508
499   AM_RANGE(0x500000, 0x500007) AM_READ(undrfire_input_r)
500   AM_RANGE(0xc00000, 0xc00007) AM_READ(undrfire_input_r)
501
502   AM_RANGE(0x700000, 0x7007ff) AM_READ(SMH_RAM)   /* Sound shared ram */
503
504   AM_RANGE(0x900000, 0x90ffff) AM_READ(SMH_RAM)
505   AM_RANGE(0xa00000, 0xa0ffff) AM_READ(SMH_RAM)   /* Palette ram */
506   AM_RANGE(0xe00000, 0xe0ffff) AM_READ(SMH_RAM)   /* Shared ram */
507ADDRESS_MAP_END
508
509static ADDRESS_MAP_START( cbombers_writemem, ADDRESS_SPACE_PROGRAM, 32 )
510   AM_RANGE(0x000000, 0x1fffff) AM_WRITE(SMH_ROM)
511   AM_RANGE(0x200000, 0x21ffff) AM_WRITE(SMH_RAM)// AM_BASE(&superchs_ram)
512   AM_RANGE(0x300000, 0x303fff) AM_WRITE(SMH_RAM) AM_BASE(&spriteram32) AM_SIZE(&spriteram_size)
513   AM_RANGE(0x800000, 0x80ffff) AM_WRITE(TC0480SCP_long_w)
514   AM_RANGE(0x830000, 0x83002f) AM_WRITE(TC0480SCP_ctrl_long_w)
515
516   AM_RANGE(0x900000, 0x90ffff) AM_WRITE(TC0100SCN_long_w)   /* piv tilemaps */
517   AM_RANGE(0x920000, 0x92000f) AM_WRITE(TC0100SCN_ctrl_long_w)
518
509static ADDRESS_MAP_START( cbombers_cpua_map, ADDRESS_SPACE_PROGRAM, 32 )
510   AM_RANGE(0x000000, 0x1fffff) AM_ROM
511   AM_RANGE(0x200000, 0x21ffff) AM_RAM
512   AM_RANGE(0x300000, 0x303fff) AM_RAM AM_BASE(&spriteram32) AM_SIZE(&spriteram_size)
519513   AM_RANGE(0x400000, 0x400003) AM_WRITE(cbombers_cpua_ctrl_w)
520   AM_RANGE(0x2c0000, 0x2c07ff) AM_WRITE(SMH_RAM) AM_BASE(&f3_shared_ram)
521//  AM_RANGE(0x340000, 0x340003) AM_WRITE(superchs_stick_w) /* stick int request */
522
523//  AM_RANGE(0x500000, 0x500003) AM_WRITE(MWA32_NOP)
524   AM_RANGE(0x500000, 0x500007) AM_WRITE(undrfire_input_w)   /* eerom etc. */
525
526//  AM_RANGE(0x600000, 0x600007) AM_WRITE(unknown_int_req_w)    /* int request for unknown hardware */
527
528   AM_RANGE(0x700000, 0x7007ff) AM_WRITE(SMH_RAM) AM_BASE(&f3_shared_ram)
529
530   AM_RANGE(0x900000, 0x90ffff) AM_WRITE(SMH_RAM)
531   AM_RANGE(0xa00000, 0xa0ffff) AM_WRITE(color_ram_w) AM_BASE(&paletteram32)
532   AM_RANGE(0xe00000, 0xe0ffff) AM_WRITE(SMH_RAM) AM_BASE(&shared_ram)
514   AM_RANGE(0x500000, 0x500007) AM_READWRITE(undrfire_input_r, undrfire_input_w)
515   AM_RANGE(0x600000, 0x600007) AM_READWRITE(cbombers_adc_r, cbombers_adc_w)
516   AM_RANGE(0x700000, 0x7007ff) AM_RAM AM_BASE(&f3_shared_ram)
517   AM_RANGE(0x800000, 0x80ffff) AM_READWRITE(TC0480SCP_long_r, TC0480SCP_long_w)      /* tilemaps */
518   AM_RANGE(0x830000, 0x83002f) AM_READWRITE(TC0480SCP_ctrl_long_r, TC0480SCP_ctrl_long_w)
519   AM_RANGE(0x900000, 0x90ffff) AM_READWRITE(TC0100SCN_long_r, TC0100SCN_long_w)      /* piv tilemaps */
520   AM_RANGE(0x920000, 0x92000f) AM_READWRITE(TC0100SCN_ctrl_long_r, TC0100SCN_ctrl_long_w)
521   AM_RANGE(0xa00000, 0xa0ffff) AM_RAM_WRITE(color_ram_w) AM_BASE(&paletteram32)
522   AM_RANGE(0xb00000, 0xb0000f) AM_RAM /* ? */
523   AM_RANGE(0xc00000, 0xc00007) AM_RAM /* LAN controller? */
524   AM_RANGE(0xd00000, 0xd00003) AM_WRITE(rotate_control_w)      /* perhaps port based rotate control? */
525   AM_RANGE(0xe00000, 0xe0ffff) AM_RAM AM_BASE(&shared_ram)
533526ADDRESS_MAP_END
534527
535static ADDRESS_MAP_START( cbombers_cpub_readmem, ADDRESS_SPACE_PROGRAM, 16 )
536   AM_RANGE(0x000000, 0x03ffff) AM_READ(SMH_ROM)
537   AM_RANGE(0x400000, 0x40ffff) AM_READ(SMH_RAM)   /* local ram */
538   AM_RANGE(0x800000, 0x80ffff) AM_READ(shared_ram_r)
539//  AM_RANGE(0xa00000, 0xa001ff) AM_READ(SMH_RAM)
540ADDRESS_MAP_END
541
542static ADDRESS_MAP_START( cbombers_cpub_writemem, ADDRESS_SPACE_PROGRAM, 16 )
543   AM_RANGE(0x000000, 0x03ffff) AM_WRITE(SMH_ROM)
544   AM_RANGE(0x400000, 0x40ffff) AM_WRITE(SMH_RAM)
528static ADDRESS_MAP_START( cbombers_cpub_map, ADDRESS_SPACE_PROGRAM, 16 )
529   AM_RANGE(0x000000, 0x03ffff) AM_ROM
530   AM_RANGE(0x400000, 0x40ffff) AM_RAM   /* local ram */
545531//  AM_RANGE(0x600000, 0x60ffff) AM_WRITE(TC0480SCP_word_w) /* Only written upon errors */
546   AM_RANGE(0x800000, 0x80ffff) AM_WRITE(shared_ram_w)
547//  AM_RANGE(0xa00000, 0xa001ff) AM_WRITE(SMH_RAM)  /* Extra road control?? */
532   AM_RANGE(0x800000, 0x80ffff) AM_READWRITE(shared_ram_r, shared_ram_w)
533//  AM_RANGE(0xa00000, 0xa001ff) AM_RAM /* Extra road control?? */
548534ADDRESS_MAP_END
549535
550536
551
552537/***********************************************************
553538             INPUT PORTS (dips in eprom)
554539***********************************************************/
r2627r2628
627612   PORT_BIT( 0x0001, IP_ACTIVE_LOW,  IPT_UNKNOWN )
628613   PORT_BIT( 0x0002, IP_ACTIVE_LOW,  IPT_UNKNOWN )
629614   PORT_BIT( 0x0004, IP_ACTIVE_LOW,  IPT_UNKNOWN )
630   PORT_BIT( 0x0008, IP_ACTIVE_HIGH, IPT_BUTTON3 ) PORT_PLAYER(1)   /* ? where is freeze input */
615   PORT_BIT( 0x0008, IP_ACTIVE_HIGH, IPT_BUTTON5 ) /* ? where is freeze input */
631616   PORT_BIT( 0x0010, IP_ACTIVE_LOW,  IPT_UNKNOWN )
632617   PORT_BIT( 0x0020, IP_ACTIVE_LOW,  IPT_UNKNOWN )
633618   PORT_BIT( 0x0040, IP_ACTIVE_LOW,  IPT_UNKNOWN )
634619   PORT_BIT( 0x0080, IP_ACTIVE_LOW,  IPT_UNKNOWN )
635   PORT_BIT( 0x0100, IP_ACTIVE_LOW,  IPT_BUTTON4 ) PORT_PLAYER(1) // Nitro
636   PORT_BIT( 0x0200, IP_ACTIVE_LOW,  IPT_BUTTON3 ) PORT_PLAYER(1) // Shift
637   PORT_BIT( 0x0400, IP_ACTIVE_LOW,  IPT_BUTTON1 ) PORT_PLAYER(1) // Accel
638   PORT_BIT( 0x0800, IP_ACTIVE_LOW,  IPT_BUTTON2 ) PORT_PLAYER(1) // Brake
620   PORT_BIT( 0x0100, IP_ACTIVE_LOW,  IPT_BUTTON4 ) PORT_NAME("Gear Shift") PORT_TOGGLE
621   PORT_BIT( 0x0200, IP_ACTIVE_LOW,  IPT_BUTTON3 ) PORT_NAME("Nitro")
622   PORT_BIT( 0x0400, IP_ACTIVE_LOW,  IPT_BUTTON1 ) PORT_NAME("Accelerator")
623   PORT_BIT( 0x0800, IP_ACTIVE_LOW,  IPT_BUTTON2 ) PORT_NAME("Brake")
639624   PORT_BIT( 0x1000, IP_ACTIVE_LOW,  IPT_UNKNOWN )
640625   PORT_BIT( 0x2000, IP_ACTIVE_LOW,  IPT_UNKNOWN )
641626   PORT_BIT( 0x4000, IP_ACTIVE_LOW,  IPT_UNKNOWN )
r2627r2628
669654   PORT_BIT( 0x40, IP_ACTIVE_LOW,  IPT_UNKNOWN )
670655   PORT_BIT( 0x80, IP_ACTIVE_LOW,  IPT_UNKNOWN )
671656
672   /* Gun inputs (real range is 0-0xffff: we use standard 0-255 and shift later) */
673
674   PORT_START_TAG("IN3")   /* IN 3, P1X */
675   PORT_BIT( 0xff, 0x80, IPT_LIGHTGUN_X ) PORT_CROSSHAIR(X, -1.0, 0.0, 0) PORT_SENSITIVITY(20) PORT_KEYDELTA(25) PORT_REVERSE PORT_PLAYER(1)
676
677   PORT_START_TAG("IN4")   /* IN 4, P1Y */
678   PORT_BIT( 0xff, 0x80, IPT_LIGHTGUN_Y ) PORT_CROSSHAIR(Y, 1.0, 0.0, 0) PORT_SENSITIVITY(20) PORT_KEYDELTA(25) PORT_PLAYER(1)
679
680   PORT_START_TAG("IN5")   /* IN 5, P2X */
681   PORT_BIT( 0xff, 0x80, IPT_LIGHTGUN_X ) PORT_CROSSHAIR(X, -1.0, 0.0, 0) PORT_SENSITIVITY(20) PORT_KEYDELTA(25) PORT_REVERSE PORT_PLAYER(2)
682
683   PORT_START_TAG("IN6")   /* IN 6, P2Y */
684   PORT_BIT( 0xff, 0x80, IPT_LIGHTGUN_Y ) PORT_CROSSHAIR(Y, 1.0, 0.0, 0) PORT_SENSITIVITY(20) PORT_KEYDELTA(25) PORT_PLAYER(2)
685
686   PORT_START_TAG("FAKE")
687   PORT_BIT(    0x01, 0x00, IPT_DIPSWITCH_NAME ) PORT_NAME("Show gun target") PORT_CODE(KEYCODE_F1) PORT_TOGGLE
688   PORT_DIPSETTING(    0x00, DEF_STR( No ) )
689   PORT_DIPSETTING(    0x01, DEF_STR( Yes ) )
657   PORT_START_TAG("IN3")   /* IN 3, steering wheel */
658   PORT_BIT( 0xff, 0x7f, IPT_AD_STICK_X ) PORT_SENSITIVITY(25) PORT_KEYDELTA(15) PORT_REVERSE PORT_PLAYER(1)
690659INPUT_PORTS_END
691660
692661
r2627r2628
793762
794763   /* basic machine hardware */
795764   MDRV_CPU_ADD(M68EC020, 16000000)   /* 16 MHz */
796   MDRV_CPU_PROGRAM_MAP(cbombers_readmem, cbombers_writemem)
765   MDRV_CPU_PROGRAM_MAP(cbombers_cpua_map, 0)
797766   MDRV_CPU_VBLANK_INT("main", irq4_line_hold)
798767
799768   TAITO_F3_SOUND_SYSTEM_CPU(16000000)
800769
801770   MDRV_CPU_ADD(M68000, 16000000)   /* 16 MHz */
802   MDRV_CPU_PROGRAM_MAP(cbombers_cpub_readmem, cbombers_cpub_writemem)
771   MDRV_CPU_PROGRAM_MAP(cbombers_cpub_map, 0)
803772   MDRV_CPU_VBLANK_INT("main", irq4_line_hold)
804773
805774   MDRV_INTERLEAVE(8)   /* CPU slices - Need to interleave Cpu's 1 & 3 */
r2627r2628
823792   MDRV_VIDEO_UPDATE(cbombers)
824793
825794   /* sound hardware */
826   TAITO_F3_SOUND_SYSTEM_ES5505(13343000)
795   TAITO_F3_SOUND_SYSTEM_ES5505(30476100/2)
827796MACHINE_DRIVER_END
828797
829798
r2627r2628
992961ROM_END
993962
994963
995
996static READ32_HANDLER( main_cycle_r )
997{
998   int ptr;
999   if ((activecpu_get_sp()&2)==0) ptr=undrfire_ram[(activecpu_get_sp()&0x1ffff)/4];
1000   else ptr=(((undrfire_ram[(activecpu_get_sp()&0x1ffff)/4])&0x1ffff)<<16) |
1001   (undrfire_ram[((activecpu_get_sp()&0x1ffff)/4)+1]>>16);
1002
1003   if (activecpu_get_pc()==0x682 && ptr==0x1156)
1004      cpu_spinuntil_int();
1005
1006   return undrfire_ram[0x4f8/4];
1007}
1008
1009964static DRIVER_INIT( undrfire )
1010965{
1011966   UINT32 offset,i;
r2627r2628
1013968   int size=memory_region_length(REGION_GFX3);
1014969   int data;
1015970
1016   /* Speedup handlers */
1017   memory_install_read32_handler(machine, 0, ADDRESS_SPACE_PROGRAM, 0x2004f8, 0x2004fb, 0, 0, main_cycle_r);
1018
1019971   /* make piv tile GFX format suitable for gfxdecode */
1020972   offset = size/2;
1021973   for (i = size/2+size/4; i<size; i++)
r2627r2628
10721024GAME( 1993, undrfire, 0,        undrfire, undrfire, undrfire, ROT0, "Taito Corporation Japan", "Under Fire (World)", 0 )
10731025GAME( 1993, undrfiru, undrfire, undrfire, undrfire, undrfire, ROT0, "Taito America Corporation", "Under Fire (US)", 0 )
10741026GAME( 1993, undrfirj, undrfire, undrfire, undrfire, undrfire, ROT0, "Taito Corporation", "Under Fire (Japan)", 0 )
1075GAME( 1994, cbombers, 0,        cbombers, cbombers, cbombers, ROT0, "Taito Corporation", "Chase Bombers", GAME_NOT_WORKING )
1027GAME( 1994, cbombers, 0,        cbombers, cbombers, cbombers, ROT0, "Taito Corporation", "Chase Bombers", 0 )
src/mame/drivers/lkage.c
r2627r2628
414414
415415ROM_START( lkage )
416416   ROM_REGION( 0x14000, REGION_CPU1, 0 ) /* Z80 code (main CPU) */
417   ROM_LOAD( "a54-01-2.37", 0x0000, 0x8000, CRC(60fd9734) SHA1(33b444b887d80acb3a63ca4534db65c4d8147712) )
418   ROM_LOAD( "a54-02-2.38", 0x8000, 0x8000, CRC(878a25ce) SHA1(6228a12774e116e333c3563ee6e20c0c70db514b) )
419
420   ROM_REGION( 0x10000, REGION_CPU2, 0 ) /* Z80 code (sound CPU) */
421   ROM_LOAD( "a54-04.54",   0x0000, 0x8000, CRC(541faf9a) SHA1(b142ff3bd198f700697ec06ea92db3109ab5818e) )
422
423   ROM_REGION( 0x10000, REGION_CPU3, 0 ) /* 68705 MCU code */
424   ROM_LOAD( "a54-09.53",   0x0000, 0x0800, CRC(0e8b8846) SHA1(a4a105462b0127229bb7edfadd2e581c7e40f1cc) )
425
426   ROM_REGION( 0x4000, REGION_USER1, 0 ) /* data */
427   ROM_LOAD( "a54-03.51",   0x0000, 0x4000, CRC(493e76d8) SHA1(13c6160edd94ba2801fd89bb33bcae3a1e3454ff) )
428
429   ROM_REGION( 0x10000, REGION_GFX1, ROMREGION_DISPOSE )
430   ROM_LOAD( "a54-05-1.84", 0x0000, 0x4000, CRC(0033c06a) SHA1(89964503fc338817c6511fd15942741996b7037a) )
431   ROM_LOAD( "a54-06-1.85", 0x4000, 0x4000, CRC(9f04d9ad) SHA1(3b9a4d30348fd02e5c8ae94655548bd4a02dd65d) )
432   ROM_LOAD( "a54-07-1.86", 0x8000, 0x4000, CRC(b20561a4) SHA1(0d6d83dfae79ea133e37704ca47426b4c978fb36) )
433   ROM_LOAD( "a54-08-1.87", 0xc000, 0x4000, CRC(3ff3b230) SHA1(ffcd964efb0af32b5d7a70305dfda615ea95acbe) )
434
435   ROM_REGION( 0x0200, REGION_PROMS, 0 )
436   ROM_LOAD( "a54-10.2",    0x0000, 0x0200, CRC(17dfbd14) SHA1(f8f0b6dfedd4ba108dad43ccc7697ef4ab9cbf86) )   /* unknown */
437
438   ROM_REGION( 0x0800, REGION_PLDS, ROMREGION_DISPOSE )
439   ROM_LOAD( "pal16l8-a54-11.34",  0x0000, 0x0104, CRC(56232113) SHA1(4cdc6732aa3e7fbe8df51966a1295253711ecc8f) )
440   ROM_LOAD( "pal16l8-a54-12.76",  0x0200, 0x0104, CRC(e57c3c89) SHA1(a23f91da254055bb990e8bb730564c40b5725f78) )
441   ROM_LOAD( "pal16l8a-a54-13.27", 0x0400, 0x0104, CRC(c9b1938e) SHA1(2fd1adc4bde8f07cf4b6314d56b48bb3d7144cc3) )
442   ROM_LOAD( "pal16l8a-a54-14.35", 0x0600, 0x0104, CRC(a89c644e) SHA1(b41a077d1d070d9563f924c776930c33a4ff27d0) )
443ROM_END
444
445ROM_START( lkageo )
446   ROM_REGION( 0x14000, REGION_CPU1, 0 ) /* Z80 code (main CPU) */
417447   ROM_LOAD( "a54-01-1.37", 0x0000, 0x8000, CRC(973da9c5) SHA1(ad3b5d6a329b784e47be563c6f8dc628f32ba0a5) )
418448   ROM_LOAD( "a54-02-1.38", 0x8000, 0x8000, CRC(27b509da) SHA1(c623950bd7dd2b5699ca948e3731455964106b89) )
419449
r2627r2628
566596}
567597
568598GAME( 1984, lkage,   0,     lkage,  lkage, 0,       ROT0, "Taito Corporation", "The Legend of Kage", 0 )
599GAME( 1984, lkageo,  lkage, lkage,  lkage, 0,       ROT0, "Taito Corporation", "The Legend of Kage (older)", 0 )
569600GAME( 1984, lkageb,  lkage, lkageb, lkage, lkageb,  ROT0, "bootleg", "The Legend of Kage (bootleg set 1)", 0 )
570601GAME( 1984, lkageb2, lkage, lkageb, lkage, lkageb,  ROT0, "bootleg", "The Legend of Kage (bootleg set 2)", 0 )
571602GAME( 1984, lkageb3, lkage, lkageb, lkage, lkageb,  ROT0, "bootleg", "The Legend of Kage (bootleg set 3)", 0 )
src/mame/drivers/lwings.c
r2627r2628
11101110   ROM_LOAD( "63s141.15g",   0x0000, 0x0100, CRC(d96bcc98) SHA1(99e69a624d5586e5eedacd2083fa68b36e7b5e40) )   /* timing (not used) */
11111111ROM_END
11121112
1113ROM_START( lwingsb )
1114   ROM_REGION( 0x20000, REGION_CPU1, 0 )     /* 64k for code + 3*16k for the banked ROMs images */
1115   ROM_LOAD( "ic17.bin",  0x00000, 0x8000, CRC(fe8a8823) SHA1(aa968fda368cc904b22ea68d7b5d4fcfba2227b1) )
1116   ROM_LOAD( "ic18.bin",  0x10000, 0x8000, CRC(2a00cde8) SHA1(5b2ef3bb08aed1b99eee0c6d7f5b9d3af807c13e) )
1117   ROM_LOAD( "ic19.bin",  0x18000, 0x8000, CRC(ec5cc201) SHA1(1043c6a9678c18fef920be91b0796c93b83e0f73) )
1118
1119   ROM_REGION( 0x10000, REGION_CPU2, 0 )
1120   ROM_LOAD( "ic37.bin", 0x0000, 0x8000, CRC(a20337a2) SHA1(649e13a69ad9154657894fa7bf7c6e49b029a506) )
1121
1122   ROM_REGION( 0x04000, REGION_GFX1, ROMREGION_DISPOSE )
1123   ROM_LOAD( "ic60.bin",  0x00000, 0x4000, CRC(091d923c) SHA1(d686c860f147c4749ac1ee23cde5a7b570312622) ) /* characters */
1124
1125   ROM_REGION( 0x40000, REGION_GFX2, ROMREGION_DISPOSE )
1126   ROM_LOAD( "ic50.bin",  0x00000, 0x8000, CRC(5436392c) SHA1(c33925c87e61aad278bef57fe9a8148ff2d4377f) ) /* tiles */
1127   ROM_LOAD( "ic49.bin",  0x08000, 0x8000, CRC(ffdbdd69) SHA1(746eb51ae2b70349bc51099092442fb05b02d64c) )
1128   ROM_LOAD( "ic26.bin",  0x10000, 0x8000, CRC(fdd1908a) SHA1(0b2de3d2f8e50f11c57822147bec6f2d9c9ff586) )
1129   ROM_LOAD( "ic25.bin",  0x18000, 0x8000, CRC(5c73d406) SHA1(85386f6b387a85d8df7d800ffcecb2590613a42c) )
1130   ROM_LOAD( "ic2.bin",   0x20000, 0x8000, CRC(32e17b3c) SHA1(db5488b7c48cd0df4571104169e42ff4094f1abd) )
1131   ROM_LOAD( "ic1.bin",   0x28000, 0x8000, CRC(52e533c1) SHA1(9f333c9fb6e35db1264286be5b4f7e4dd18150de) )
1132   ROM_LOAD( "ic63.bin",  0x30000, 0x8000, CRC(99e134ba) SHA1(9818a6ad3146ed95b29b9aeba2331a0e8e2a76b5) )
1133   ROM_LOAD( "ic62.bin",  0x38000, 0x8000, CRC(c8f28777) SHA1(d08571d34f96e7d33506e374d047647f131dce71) )
1134
1135   ROM_REGION( 0x20000, REGION_GFX3, ROMREGION_DISPOSE )
1136   ROM_LOAD( "ic99.bin",  0x00000, 0x8000, CRC(163946da) SHA1(719735ccb965c91b152ef07d64393506808c8f55) )  /* sprites */
1137   ROM_LOAD( "ic98.bin",  0x08000, 0x8000, CRC(7cc90a1d) SHA1(ff194749397f06ad054917664bd4583b0e4e8d92) )
1138   ROM_LOAD( "ic87.bin",  0x10000, 0x8000, CRC(bca275ac) SHA1(c1cdf9f7f5e99ff85521f27565047455f0c2b78b) )
1139   ROM_LOAD( "ic86.bin",  0x18000, 0x8000, CRC(3e396eda) SHA1(a736f108e0ed5fab6177f0d8a21feab8b686ee85) )
1140
1141   ROM_REGION( 0x0100, REGION_PROMS, 0 )
1142   ROM_LOAD( "63s141.15g",   0x0000, 0x0100, CRC(d96bcc98) SHA1(99e69a624d5586e5eedacd2083fa68b36e7b5e40) )   /* timing (not used) */
1143ROM_END
1144
11131145ROM_START( sectionz )
11141146   ROM_REGION( 0x20000, REGION_CPU1, 0 )     /* 64k for code + 3*16k for the banked ROMs images */
11151147   ROM_LOAD( "6c_sz01.bin",  0x00000, 0x8000, CRC(69585125) SHA1(a341e3a5507e961d5763be6acf420695bb32709e) )
r2627r2628
14611493GAME( 1986, lwings,   0,        lwings,   lwings,   0, ROT90, "Capcom", "Legendary Wings (US set 1)", 0 )
14621494GAME( 1986, lwings2,  lwings,   lwings,   lwings,   0, ROT90, "Capcom", "Legendary Wings (US set 2)", 0 )
14631495GAME( 1986, lwingsjp, lwings,   lwings,   lwings,   0, ROT90, "Capcom", "Ares no Tsubasa (Japan)", 0 )
1496GAME( 1986, lwingsb,  lwings,   lwings,   lwings,   0, ROT90, "bootleg", "Legendary Wings (bootleg)", 0 )
14641497GAME( 1986, trojan,   0,        trojan,   trojanls, 0, ROT0,  "Capcom", "Trojan (US)", 0 )
14651498GAME( 1986, trojanr,  trojan,   trojan,   trojan,   0, ROT0,  "Capcom (Romstar license)", "Trojan (Romstar)", 0 )
14661499GAME( 1986, trojanj,  trojan,   trojan,   trojan,   0, ROT0,  "Capcom", "Tatakai no Banka (Japan)", 0 )
src/mame/drivers/namcona1.c
r2627r2628
1919
2020To Do:
2121
22- Pressing "F3" (reset) crashes MAME
22- view area / screen resolution controlled by registers?
2323
24- Emeralda:
25    After selecting the game type, tilemap scrolling is briefly incorrect
26    The demo sprites are incorrect, you should see a dolphin speaking
27    the instructions and the playfield is wrong.
28    Byte 0x25 of the NVRAM controls the FBI logo. 0x00 is on, 0x01 is off
29
30- Hook up ROZ registers
31    Example:  In Emeralda the game logo rotates and zooms in against a black
32    background with bubbles during the intro.  Then at the second green screen
33    when flashing Please Insert Coin, the word NAMCO rotates past the viewer.
34
35- Is view area controlled by registers?
36
3724- Xday 2:
38    has some graphics glitches (wrong sprite tiles); probably blitter-related
39
4025    Rom board  M112
4126    Rom board custom Key chip i.d. C394
4227    Game uses a small cash-register type printer (connects to rom board)
r2627r2628
7560    button1 to up, button 2 to down, button 3 to left, button 4 to right.
7661    But Taito F2 quiz games assign button 3 to right and button 4 to left.
7762
63-   Emeralda: Byte 0x25 of the NVRAM, when zero, enables the FBI logo.
7864
65
7966Namco NA2 hardware
8067------------------
8168
r2627r2628
833820      logerror( "bad blt src %08x\n", source );
834821      return -1;
835822   }
836   if( dest>=0xf00000 && dest<=0xf02000 )
823   if( dest>=0xf00000 && dest<0xf02000 )
837824   {
838825      namcona1_paletteram_w( machine, (dest-0xf00000)/2, data, 0xffff );
839826   }
840   else if( dest>=0xf40000 && dest<=0xf80000 )
827   else if( dest>=0xf40000 && dest<0xf80000 )
841828   {
842829      namcona1_gfxram_w( machine, (dest-0xf40000)/2, data, 0xffff );
843830   }
r2627r2628
845832   {
846833      namcona1_videoram_w( machine, (dest-0xff0000)/2, data, 0xffff );
847834   }
848   else if( dest>=0xff8000 && dest<=0xffdfff )
835   else if( dest>=0xff8000 && dest<0xffc000 )
849836   {
850      namcona1_sparevram[(dest-0xff8000)/2] = data;
837      namcona1_rozvideoram_w( machine, (dest-0xff8000)/2, data, 0xffff );
851838   }
852   else if( dest>=0xfff000 && dest<=0xffffff )
839   else if( dest>=0xfff000 && dest<0x1000000 )
853840   {
854841      spriteram16[(dest-0xfff000)/2] = data;
855842   }
856843   else
857844   {
858      logerror( "bad blt dst %08x\n", dest );
845      logerror( "bad blit dest %08x\n", dest );
859846      return -1;
860847   }
861848   return 0;
r2627r2628
865852blit_setup( int format, int *bytes_per_row, int *pitch, int mode )
866853{
867854   if( mode == 3 )
868   {
855   { /* TILE DATA */
869856      switch( format )
870857      {
871858      case 0x0001:
r2627r2628
888875      }
889876   }
890877   else
891   {
878   { /* SHAPE DATA */
892879      switch( format )
893880      {
894881      case 0x00bd: /* Numan Athletics */
r2627r2628
933920   }
934921} /* blit_setup */
935922
936/*
937$efff20: sprite control: 0x3a,0x3e,0x3f
938            bit 0x01 selects spriteram bank
939
940               0    2    4    6    8    a    c    e
941$efff00:    src0 src1 src2 dst0 dst1 dst2 BANK [src
942$efff10:    src] [dst dst] #BYT BLIT eINT 001f 0001
943$efff20:    003f 003f IACK ---- ---- ---- ---- ----
944$efff30:    ---- ---- ---- ---- ---- ---- ---- ----
945$efff40:    ---- ---- ---- ---- ---- ---- ---- ----
946$efff50:    ---- ---- ---- ---- ---- ---- ---- ----
947$efff60:    ---- ---- ---- ---- ---- ---- ---- ----
948$efff70:    ---- ---- ---- ---- ---- ---- ---- ----
949$efff80:    0050 0170 0020 0100 0000 0000 0000 GFXE
950$efff90:    0000 0001 0002 0003 FLIP ---- ---- ----
951$efffa0:    PRI  PRI  PRI  PRI  ---- ---- 00c0 ----     priority (0..7)
952$efffb0:    COLR COLR COLR COLR 0001 0004 0000 ----     color (0..f)
953$efffc0:    ???? ???? ???? ???? ???? ???? ???? ----     ROZ
954
955Emeralda:
956$efff80:    0048 0177 0020 0100 0000 00fd 0000 GFXE
957*/
958
959923static void namcona1_blit( running_machine *machine )
960924{
961925   int src0 = namcona1_vreg[0x0];
r2627r2628
969933   int gfxbank = namcona1_vreg[0x6];
970934
971935   /* dest and source are provided as dword offsets */
972   UINT32 src_baseaddr   = 2*((namcona1_vreg[0x7]<<16)|namcona1_vreg[0x8]);
973   UINT32 dst_baseaddr   = 2*((namcona1_vreg[0x9]<<16)|namcona1_vreg[0xa]);
936   UINT32 src_baseaddr   = 2*(0xffffff&((namcona1_vreg[0x7]<<16)|namcona1_vreg[0x8]));
937   UINT32 dst_baseaddr   = 2*(0xffffff&((namcona1_vreg[0x9]<<16)|namcona1_vreg[0xa]));
974938
975939   int num_bytes = namcona1_vreg[0xb];
976940
977   int dest_offset, source_offset;
978   int dest_bytes_per_row, dst_pitch;
979   int source_bytes_per_row, src_pitch;
941   int dst_offset, src_offset;
942   int dst_bytes_per_row, dst_pitch;
943   int src_bytes_per_row, src_pitch;
980944
981945   (void)dst2;
982946   (void)dst0;
983947   (void)src2;
984948   (void)src0;
985
986/*  logerror( "0x%08x: blt(%08x,%08x,%08x);%04x %04x %04x; %04x %04x %04x; gfx=%04x\n",
949/*
950    logerror( "0x%08x: blt(%08x,%08x,numBytes=%04x);src=%04x %04x %04x; dst=%04x %04x %04x; gfx=%04x\n",
987951        activecpu_get_pc(),
988952        dst_baseaddr,src_baseaddr,num_bytes,
989953        src0,src1,src2,
990954        dst0,dst1,dst2,
991        gfxbank );*/
955        gfxbank );
956*/
957   blit_setup( dst1, &dst_bytes_per_row, &dst_pitch, gfxbank);
958   blit_setup( src1, &src_bytes_per_row, &src_pitch, gfxbank );
992959
993   blit_setup( dst1, &dest_bytes_per_row, &dst_pitch, gfxbank);
994   blit_setup( src1, &source_bytes_per_row, &src_pitch, gfxbank );
995
996960   if( num_bytes&1 )
997961   {
998962      num_bytes++;
r2627r2628
1003967      dst_baseaddr += 0xf40000;
1004968   }
1005969
1006   dest_offset      = 0;
1007   source_offset   = 0;
970   dst_offset = 0;
971   src_offset = 0;
1008972
1009973   while( num_bytes>0 )
1010974   {
1011975      if( transfer_dword(machine,
1012         dst_baseaddr + dest_offset,
1013         src_baseaddr + source_offset ) )
976         dst_baseaddr + dst_offset,
977         src_baseaddr + src_offset ) )
1014978      {
1015979         return;
1016980      }
1017981
1018982      num_bytes -= 2;
1019983
1020      dest_offset+=2;
1021      if( dest_offset >= dest_bytes_per_row )
984      dst_offset+=2;
985      if( dst_offset >= dst_bytes_per_row )
1022986      {
1023987         dst_baseaddr += dst_pitch;
1024         dest_offset = 0;
988         dst_offset = 0;
1025989      }
1026990
1027      source_offset+=2;
1028      if( source_offset >= source_bytes_per_row )
991      src_offset+=2;
992      if( src_offset >= src_bytes_per_row )
1029993      {
1030994         src_baseaddr += src_pitch;
1031         source_offset = 0;
995         src_offset = 0;
1032996      }
1033997   }
1034998} /* namcona1_blit */
r2627r2628
10851049   AM_RANGE(0xefff00, 0xefffff) AM_READWRITE(namcona1_vreg_r, namcona1_vreg_w) AM_BASE(&namcona1_vreg)
10861050   AM_RANGE(0xf00000, 0xf01fff) AM_READWRITE(namcona1_paletteram_r, namcona1_paletteram_w) AM_BASE(&paletteram16)
10871051   AM_RANGE(0xf40000, 0xf7ffff) AM_READWRITE(namcona1_gfxram_r, namcona1_gfxram_w)
1088   AM_RANGE(0xff0000, 0xff7fff) AM_READWRITE(namcona1_videoram_r, namcona1_videoram_w) AM_BASE(&videoram16)
1089   AM_RANGE(0xff8000, 0xffdfff) AM_RAM AM_BASE(&namcona1_sparevram)   /* spare videoram */
1052   AM_RANGE(0xff0000, 0xff7fff) AM_READWRITE(namcona1_videoram_r,    namcona1_videoram_w) AM_BASE(&videoram16)
1053   AM_RANGE(0xff8000, 0xffbfff) AM_READWRITE(namcona1_rozvideoram_r, namcona1_rozvideoram_w) AM_BASE(&namcona1_rozvideoram)
1054   AM_RANGE(0xffd000, 0xffdfff) AM_RAM /* unknown */
10901055   AM_RANGE(0xffe000, 0xffefff) AM_RAM   AM_BASE(&namcona1_scroll)      /* scroll registers */
10911056   AM_RANGE(0xfff000, 0xffffff) AM_RAM   AM_BASE(&spriteram16)         /* spriteram */
10921057ADDRESS_MAP_END
r2627r2628
12701235   AM_RANGE(0x10, 0x1f) AM_READ( portana_r )
12711236ADDRESS_MAP_END
12721237
1238
12731239static INTERRUPT_GEN( namcona1_interrupt )
12741240{
12751241   int level = cpu_getiloops(); /* 0,1,2,3,4 */
12761242   if( level==0 )
12771243   {
1278      simulate_mcu(machine);
1244      simulate_mcu( machine );
12791245   }
12801246   if( mEnableInterrupts )
12811247   {
12821248      if( (namcona1_vreg[0x1a/2]&(1<<level))==0 )
12831249      {
1250         if( level==2 )
1251         { // posirq used with dolphin in Emeraldia's "how to play" attract mode
1252            int scanline = namcona1_vreg[0x8a/2]&0xff;
1253            if( scanline )
1254            {
1255               video_screen_update_partial(machine->primary_screen, scanline );
1256            }
1257         }
12841258         cpunum_set_input_line(machine, 0, level+1, HOLD_LINE);
12851259      }
12861260   }
r2627r2628
12931267static INTERRUPT_GEN( mcu_interrupt )
12941268{
12951269   if (cpu_getiloops() == 0)
1270   {
12961271       cpunum_set_input_line(machine, 1, M37710_LINE_IRQ1, HOLD_LINE);
1272   }
12971273   else if (cpu_getiloops() == 1)
12981274   {
12991275      cpunum_set_input_line(machine, 1, M37710_LINE_ADC, HOLD_LINE);
r2627r2628
13601336MACHINE_DRIVER_END
13611337
13621338static ADDRESS_MAP_START( namcona2_readmem, ADDRESS_SPACE_PROGRAM, 16 )
1363   AM_RANGE(0x000000, 0x07ffff) AM_READ(SMH_RAM)      /* work RAM */
1339   AM_RANGE(0x000000, 0x07ffff) AM_READ(SMH_RAM) /* work RAM */
13641340   AM_RANGE(0x400000, 0xbfffff) AM_ROM AM_REGION(REGION_CPU1, 0x280000)   /* data */
13651341   AM_RANGE(0xc00000, 0xdfffff) AM_ROM AM_REGION(REGION_CPU1, 0x080000)   /* code */
13661342   AM_RANGE(0xe00000, 0xe00fff) AM_READ(namcona1_nvram_r)
1343   /* xday: additional battery-backed ram at 00E024FA? */
13671344   AM_RANGE(0xe40000, 0xe4000f) AM_READ(custom_key_r)
13681345   AM_RANGE(0xefff00, 0xefffff) AM_READ(namcona1_vreg_r)
13691346   AM_RANGE(0xf00000, 0xf01fff) AM_READ(namcona1_paletteram_r)
13701347   AM_RANGE(0xf40000, 0xf7ffff) AM_READ(namcona1_gfxram_r)
13711348   AM_RANGE(0xff0000, 0xff7fff) AM_READ(namcona1_videoram_r)
1372   AM_RANGE(0xff8000, 0xffdfff) AM_READ(SMH_RAM)      /* spare videoram */
1373   AM_RANGE(0xffe000, 0xffefff) AM_READ(SMH_RAM)      /* scroll registers */
1374   AM_RANGE(0xfff000, 0xffffff) AM_READ(SMH_RAM)      /* spriteram */
1349   AM_RANGE(0xff8000, 0xffbfff) AM_READ(namcona1_rozvideoram_r )
1350   AM_RANGE(0xffd000, 0xffdfff) AM_READ(SMH_RAM) /* unknown */
1351   AM_RANGE(0xffe000, 0xffefff) AM_READ(SMH_RAM) /* scroll registers */
1352   AM_RANGE(0xfff000, 0xffffff) AM_READ(SMH_RAM) /* spriteram */
13751353ADDRESS_MAP_END
13761354
13771355static ADDRESS_MAP_START( namcona2_writemem, ADDRESS_SPACE_PROGRAM, 16 )
13781356   AM_RANGE(0x000000, 0x000fff) AM_WRITE(namcona1_mcu_w) AM_BASE(&mcu_ram)
13791357   AM_RANGE(0x000000, 0x07ffff) AM_WRITE(SMH_RAM) AM_BASE(&namcona1_workram)
13801358   AM_RANGE(0x3f8008, 0x3f8009) AM_WRITE(mcu_command_w)
1359   AM_RANGE(0xd00000, 0xd00001) AM_WRITE(SMH_NOP) /* xday: serial out? */
1360   AM_RANGE(0xd40000, 0xd40001) AM_WRITE(SMH_NOP) /* xday: serial out? */
1361   AM_RANGE(0xd80000, 0xd80001) AM_WRITE(SMH_NOP) /* xday: serial out? */
1362   AM_RANGE(0xdc0000, 0xdc001f) AM_WRITE(SMH_NOP) /* xday: serial config? */
13811363   AM_RANGE(0x400000, 0xdfffff) AM_WRITE(SMH_ROM) /* data + code */
13821364   AM_RANGE(0xe00000, 0xe00fff) AM_WRITE(namcona1_nvram_w)
13831365   AM_RANGE(0xe40000, 0xe4000f) AM_WRITE(custom_key_w)
r2627r2628
13851367   AM_RANGE(0xf00000, 0xf01fff) AM_WRITE(namcona1_paletteram_w) AM_BASE(&paletteram16)
13861368   AM_RANGE(0xf40000, 0xf7ffff) AM_WRITE(namcona1_gfxram_w)
13871369   AM_RANGE(0xff0000, 0xff7fff) AM_WRITE(namcona1_videoram_w) AM_BASE(&videoram16)
1388   AM_RANGE(0xff8000, 0xffdfff) AM_WRITE(SMH_RAM) AM_BASE(&namcona1_sparevram)
1370   AM_RANGE(0xff8000, 0xffbfff) AM_WRITE(namcona1_rozvideoram_w) AM_BASE(&namcona1_rozvideoram)
1371   AM_RANGE(0xffd000, 0xffdfff) AM_WRITE(SMH_RAM) /* unknown */
13891372   AM_RANGE(0xffe000, 0xffefff) AM_WRITE(SMH_RAM) AM_BASE(&namcona1_scroll)
13901373   AM_RANGE(0xfff000, 0xffffff) AM_WRITE(SMH_RAM) AM_BASE(&spriteram16)
13911374ADDRESS_MAP_END
src/mame/drivers/cps1.c
r2627r2628
39373937
39383938/* FIXME B-Board unknown
39393939   from the number of the only new ROM found in this set (sthj23.bin), this could
3940   be from a 91635B, which would classify it as a hack since that board wasn't available at the
3940   be from a 91634B/91635B, which would classify it as a hack since that board wasn't available at the
39413941   time of release of this game.
39423942    All other ROMs copied from the US set
39433943*/
r2627r2628
66976697   ROM_LOAD( "s92_19.bin",    0x20000, 0x20000, CRC(beade53f) SHA1(277c397dc12752719ec6b47d2224750bd1c07f79) )
66986698ROM_END
66996699
6700/* B-Board 89624B */
67006701ROM_START( varth )
67016702   ROM_REGION( CODE_SIZE, REGION_CPU1, 0 )      /* 68000 code */
6702   ROM_LOAD16_BYTE( "vae_30b.rom",  0x00000, 0x20000, CRC(adb8d391) SHA1(5e7160509e0315eb32cc390ddd7e4ef7a4a1a70a) )
6703   ROM_LOAD16_BYTE( "vae_35b.rom",  0x00001, 0x20000, CRC(44e5548f) SHA1(17b4be1f4159f6b6803d8c2950823ece0bdde8b2) )
6704   ROM_LOAD16_BYTE( "vae_31b.rom",  0x40000, 0x20000, CRC(1749a71c) SHA1(bd9bfd5bbe2d426c94df755c977faa92a28f16ab) )
6705   ROM_LOAD16_BYTE( "vae_36b.rom",  0x40001, 0x20000, CRC(5f2e2450) SHA1(676e8d96406d81ceadd4a0a69959cdcb6d5d9ac8) )
6706   ROM_LOAD16_BYTE( "vae_28b.rom",  0x80000, 0x20000, CRC(e524ca50) SHA1(487d5ddabe852872f331362034c4fa16e0926e3d) )
6707   ROM_LOAD16_BYTE( "vae_33b.rom",  0x80001, 0x20000, CRC(c0bbf8c9) SHA1(447540b856776770af8022a291d46612c1bb5909) )
6708   ROM_LOAD16_BYTE( "vae_29b.rom",  0xc0000, 0x20000, CRC(6640996a) SHA1(3ed7bd947dc8224435680dedf4955ed6041c6028) )
6709   ROM_LOAD16_BYTE( "vae_34b.rom",  0xc0001, 0x20000, CRC(fa59be8a) SHA1(86a3d3a7126c021e2ca8ac20238695396367e098) )
6703   ROM_LOAD16_BYTE( "vae_30b.11f", 0x00000, 0x20000, CRC(adb8d391) SHA1(5e7160509e0315eb32cc390ddd7e4ef7a4a1a70a) )
6704   ROM_LOAD16_BYTE( "vae_35b.11h", 0x00001, 0x20000, CRC(44e5548f) SHA1(17b4be1f4159f6b6803d8c2950823ece0bdde8b2) )
6705   ROM_LOAD16_BYTE( "vae_31b.12f", 0x40000, 0x20000, CRC(1749a71c) SHA1(bd9bfd5bbe2d426c94df755c977faa92a28f16ab) )
6706   ROM_LOAD16_BYTE( "vae_36b.12h", 0x40001, 0x20000, CRC(5f2e2450) SHA1(676e8d96406d81ceadd4a0a69959cdcb6d5d9ac8) )
6707   ROM_LOAD16_BYTE( "vae_28b.9f",  0x80000, 0x20000, CRC(e524ca50) SHA1(487d5ddabe852872f331362034c4fa16e0926e3d) )
6708   ROM_LOAD16_BYTE( "vae_33b.9h",  0x80001, 0x20000, CRC(c0bbf8c9) SHA1(447540b856776770af8022a291d46612c1bb5909) )
6709   ROM_LOAD16_BYTE( "vae_29b.10f", 0xc0000, 0x20000, CRC(6640996a) SHA1(3ed7bd947dc8224435680dedf4955ed6041c6028) )
6710   ROM_LOAD16_BYTE( "vae_34b.10h", 0xc0001, 0x20000, CRC(fa59be8a) SHA1(86a3d3a7126c021e2ca8ac20238695396367e098) )
67106711
67116712   ROM_REGION( 0x200000, REGION_GFX1, 0 )
6712   ROMX_LOAD( "va_gfx5.rom",  0x000000, 0x80000, CRC(b1fb726e) SHA1(5ac0876b6c49d0a99710dda68653664f4d8c1167) , ROM_GROUPWORD | ROM_SKIP(6) )
6713   ROMX_LOAD( "va_gfx7.rom",  0x000002, 0x80000, CRC(4c6588cd) SHA1(d14e8cf051ac934ccc989d8c571c6cc9eed34af5) , ROM_GROUPWORD | ROM_SKIP(6) )
6714   ROMX_LOAD( "va_gfx1.rom",  0x000004, 0x80000, CRC(0b1ace37) SHA1(6f9493c22f667f683db2789972fd16bb94724679) , ROM_GROUPWORD | ROM_SKIP(6) )
6715   ROMX_LOAD( "va_gfx3.rom",  0x000006, 0x80000, CRC(44dfe706) SHA1(a013a434df3161a91aafbb35dc4e20dfb3f177f4) , ROM_GROUPWORD | ROM_SKIP(6) )
6713   ROMX_LOAD( "va-5m.7a", 0x000000, 0x80000, CRC(b1fb726e) SHA1(5ac0876b6c49d0a99710dda68653664f4d8c1167) , ROM_GROUPWORD | ROM_SKIP(6) )
6714   ROMX_LOAD( "va-7m.9a", 0x000002, 0x80000, CRC(4c6588cd) SHA1(d14e8cf051ac934ccc989d8c571c6cc9eed34af5) , ROM_GROUPWORD | ROM_SKIP(6) )
6715   ROMX_LOAD( "va-1m.3a", 0x000004, 0x80000, CRC(0b1ace37) SHA1(6f9493c22f667f683db2789972fd16bb94724679) , ROM_GROUPWORD | ROM_SKIP(6) )
6716   ROMX_LOAD( "va-3m.5a", 0x000006, 0x80000, CRC(44dfe706) SHA1(a013a434df3161a91aafbb35dc4e20dfb3f177f4) , ROM_GROUPWORD | ROM_SKIP(6) )
67166717
67176718   ROM_REGION( 0x8000, REGION_GFX2, 0 )
67186719   ROM_COPY( REGION_GFX1, 0x000000, 0x000000, 0x8000 )   /* stars */
67196720
67206721   ROM_REGION( 0x18000, REGION_CPU2, 0 ) /* 64k for the audio CPU (+banks) */
6721   ROM_LOAD( "va_09.rom",     0x00000, 0x08000, CRC(7a99446e) SHA1(ca027f41e3e58be5abc33ad7380746658cb5380a) )
6722   ROM_CONTINUE(              0x10000, 0x08000 )
6722   ROM_LOAD( "va_09.12b", 0x00000, 0x08000, CRC(7a99446e) SHA1(ca027f41e3e58be5abc33ad7380746658cb5380a) )
6723   ROM_CONTINUE(          0x10000, 0x08000 )
67236724
67246725   ROM_REGION( 0x40000, REGION_SOUND1, 0 )   /* Samples */
6725   ROM_LOAD( "va_18.rom",     0x00000, 0x20000, CRC(de30510e) SHA1(8e878696192606b76a3a0e53553e638d9621cff7) )
6726   ROM_LOAD( "va_19.rom",     0x20000, 0x20000, CRC(0610a4ac) SHA1(3da02ea6a7a56c85de898806d2a1cf6bc526c1b3) )
6726   ROM_LOAD( "va_18.11c", 0x00000, 0x20000, CRC(de30510e) SHA1(8e878696192606b76a3a0e53553e638d9621cff7) )
6727   ROM_LOAD( "va_19.12c", 0x20000, 0x20000, CRC(0610a4ac) SHA1(3da02ea6a7a56c85de898806d2a1cf6bc526c1b3) )
67276728ROM_END
67286729
6730/* B-Board 89624B */
67296731ROM_START( varthr1 )
67306732   ROM_REGION( CODE_SIZE, REGION_CPU1, 0 )      /* 68000 code */
6731   ROM_LOAD16_BYTE( "vae_30a.rom",  0x00000, 0x20000, CRC(7fcd0091) SHA1(7bed452736eda4a26c43c5dd54ec6799afa6e770) )
6732   ROM_LOAD16_BYTE( "vae_35a.rom",  0x00001, 0x20000, CRC(35cf9509) SHA1(a189ca7740d77262413ec2891af034d0057892be) )
6733   ROM_LOAD16_BYTE( "vae_31a.rom",  0x40000, 0x20000, CRC(15e5ee81) SHA1(6c6248b07f7e956a37d5dcb4b67d026f57fae13b) )
6734   ROM_LOAD16_BYTE( "vae_36a.rom",  0x40001, 0x20000, CRC(153a201e) SHA1(5936e447d5cd02ff13802cf78393b521431ad06c) )
6735   ROM_LOAD16_BYTE( "vae_28a.rom",  0x80000, 0x20000, CRC(7a0e0d25) SHA1(203692ef1daeef7ba08b154cf029cc07a2e0e23d) )
6736   ROM_LOAD16_BYTE( "vae_33a.rom",  0x80001, 0x20000, CRC(f2365922) SHA1(efb2221033e4b46fedaf3d8c850e208f849e6af0) )
6737   ROM_LOAD16_BYTE( "vae_29a.rom",  0xc0000, 0x20000, CRC(5e2cd2c3) SHA1(eff955c7dc0d8ae215e7188cc4865726104c7777) )
6738   ROM_LOAD16_BYTE( "vae_34a.rom",  0xc0001, 0x20000, CRC(3d9bdf83) SHA1(d655803a6f07b90e44aacaa3e6059ac330ef2ec6) )
6733   ROM_LOAD16_BYTE( "vae_30a.11f", 0x00000, 0x20000, CRC(7fcd0091) SHA1(7bed452736eda4a26c43c5dd54ec6799afa6e770) )
6734   ROM_LOAD16_BYTE( "vae_35a.11h", 0x00001, 0x20000, CRC(35cf9509) SHA1(a189ca7740d77262413ec2891af034d0057892be) )
6735   ROM_LOAD16_BYTE( "vae_31a.12f", 0x40000, 0x20000, CRC(15e5ee81) SHA1(6c6248b07f7e956a37d5dcb4b67d026f57fae13b) )
6736   ROM_LOAD16_BYTE( "vae_36a.12h", 0x40001, 0x20000, CRC(153a201e) SHA1(5936e447d5cd02ff13802cf78393b521431ad06c) )
6737   ROM_LOAD16_BYTE( "vae_28a.9f",  0x80000, 0x20000, CRC(7a0e0d25) SHA1(203692ef1daeef7ba08b154cf029cc07a2e0e23d) )
6738   ROM_LOAD16_BYTE( "vae_33a.9h",  0x80001, 0x20000, CRC(f2365922) SHA1(efb2221033e4b46fedaf3d8c850e208f849e6af0) )
6739   ROM_LOAD16_BYTE( "vae_29a.10f", 0xc0000, 0x20000, CRC(5e2cd2c3) SHA1(eff955c7dc0d8ae215e7188cc4865726104c7777) )
6740   ROM_LOAD16_BYTE( "vae_34a.10h", 0xc0001, 0x20000, CRC(3d9bdf83) SHA1(d655803a6f07b90e44aacaa3e6059ac330ef2ec6) )
67396741
67406742   ROM_REGION( 0x200000, REGION_GFX1, 0 )
6741   ROMX_LOAD( "va_gfx5.rom",  0x000000, 0x80000, CRC(b1fb726e) SHA1(5ac0876b6c49d0a99710dda68653664f4d8c1167) , ROM_GROUPWORD | ROM_SKIP(6) )
6742   ROMX_LOAD( "va_gfx7.rom",  0x000002, 0x80000, CRC(4c6588cd) SHA1(d14e8cf051ac934ccc989d8c571c6cc9eed34af5) , ROM_GROUPWORD | ROM_SKIP(6) )
6743   ROMX_LOAD( "va_gfx1.rom",  0x000004, 0x80000, CRC(0b1ace37) SHA1(6f9493c22f667f683db2789972fd16bb94724679) , ROM_GROUPWORD | ROM_SKIP(6) )
6744   ROMX_LOAD( "va_gfx3.rom",  0x000006, 0x80000, CRC(44dfe706) SHA1(a013a434df3161a91aafbb35dc4e20dfb3f177f4) , ROM_GROUPWORD | ROM_SKIP(6) )
6743   ROMX_LOAD( "va-5m.7a", 0x000000, 0x80000, CRC(b1fb726e) SHA1(5ac0876b6c49d0a99710dda68653664f4d8c1167) , ROM_GROUPWORD | ROM_SKIP(6) )
6744   ROMX_LOAD( "va-7m.9a", 0x000002, 0x80000, CRC(4c6588cd) SHA1(d14e8cf051ac934ccc989d8c571c6cc9eed34af5) , ROM_GROUPWORD | ROM_SKIP(6) )
6745   ROMX_LOAD( "va-1m.3a", 0x000004, 0x80000, CRC(0b1ace37) SHA1(6f9493c22f667f683db2789972fd16bb94724679) , ROM_GROUPWORD | ROM_SKIP(6) )
6746   ROMX_LOAD( "va-3m.5a", 0x000006, 0x80000, CRC(44dfe706) SHA1(a013a434df3161a91aafbb35dc4e20dfb3f177f4) , ROM_GROUPWORD | ROM_SKIP(6) )
67456747
67466748   ROM_REGION( 0x8000, REGION_GFX2, 0 )
67476749   ROM_COPY( REGION_GFX1, 0x000000, 0x000000, 0x8000 )   /* stars */
67486750
67496751   ROM_REGION( 0x18000, REGION_CPU2, 0 ) /* 64k for the audio CPU (+banks) */
6750   ROM_LOAD( "va_09.rom",     0x00000, 0x08000, CRC(7a99446e) SHA1(ca027f41e3e58be5abc33ad7380746658cb5380a) )
6751   ROM_CONTINUE(              0x10000, 0x08000 )
6752   ROM_LOAD( "va_09.12b", 0x00000, 0x08000, CRC(7a99446e) SHA1(ca027f41e3e58be5abc33ad7380746658cb5380a) )
6753   ROM_CONTINUE(          0x10000, 0x08000 )
67526754
67536755   ROM_REGION( 0x40000, REGION_SOUND1, 0 )   /* Samples */
6754   ROM_LOAD( "va_18.rom",     0x00000, 0x20000, CRC(de30510e) SHA1(8e878696192606b76a3a0e53553e638d9621cff7) )
6755   ROM_LOAD( "va_19.rom",     0x20000, 0x20000, CRC(0610a4ac) SHA1(3da02ea6a7a56c85de898806d2a1cf6bc526c1b3) )
6756   ROM_LOAD( "va_18.11c", 0x00000, 0x20000, CRC(de30510e) SHA1(8e878696192606b76a3a0e53553e638d9621cff7) )
6757   ROM_LOAD( "va_19.12c", 0x20000, 0x20000, CRC(0610a4ac) SHA1(3da02ea6a7a56c85de898806d2a1cf6bc526c1b3) )
67566758ROM_END
67576759
6760/* FIXME B-Board uncertain but should be 91634B/91635B from the program ROM names */
67586761ROM_START( varthu )
67596762   ROM_REGION( CODE_SIZE, REGION_CPU1, 0 )      /* 68000 code */
6760   ROM_LOAD16_WORD_SWAP( "vau23a.bin",  0x00000, 0x80000, CRC(fbe68726) SHA1(68917d366551d2203400adc3261355dd3b332bcb) )
6761   ROM_LOAD16_WORD_SWAP( "vau22a.bin",  0x80000, 0x80000, CRC(0ed71bbd) SHA1(e7f0f0edf0936a774e122842b09f5c5ce25a96ad) )
6763   ROM_LOAD16_WORD_SWAP( "vau23a.bin", 0x00000, 0x80000, CRC(fbe68726) SHA1(68917d366551d2203400adc3261355dd3b332bcb) )
6764   ROM_LOAD16_WORD_SWAP( "vau22a.bin", 0x80000, 0x80000, CRC(0ed71bbd) SHA1(e7f0f0edf0936a774e122842b09f5c5ce25a96ad) )
67626765
67636766   ROM_REGION( 0x200000, REGION_GFX1, 0 )
6764   ROMX_LOAD( "va_gfx5.rom",  0x000000, 0x80000, CRC(b1fb726e) SHA1(5ac0876b6c49d0a99710dda68653664f4d8c1167) , ROM_GROUPWORD | ROM_SKIP(6) )
6765   ROMX_LOAD( "va_gfx7.rom",  0x000002, 0x80000, CRC(4c6588cd) SHA1(d14e8cf051ac934ccc989d8c571c6cc9eed34af5) , ROM_GROUPWORD | ROM_SKIP(6) )
6766   ROMX_LOAD( "va_gfx1.rom",  0x000004, 0x80000, CRC(0b1ace37) SHA1(6f9493c22f667f683db2789972fd16bb94724679) , ROM_GROUPWORD | ROM_SKIP(6) )
6767   ROMX_LOAD( "va_gfx3.rom",  0x000006, 0x80000, CRC(44dfe706) SHA1(a013a434df3161a91aafbb35dc4e20dfb3f177f4) , ROM_GROUPWORD | ROM_SKIP(6) )
6767   ROMX_LOAD( "va-5m.bin", 0x000000, 0x80000, CRC(b1fb726e) SHA1(5ac0876b6c49d0a99710dda68653664f4d8c1167) , ROM_GROUPWORD | ROM_SKIP(6) )
6768   ROMX_LOAD( "va-7m.bin", 0x000002, 0x80000, CRC(4c6588cd) SHA1(d14e8cf051ac934ccc989d8c571c6cc9eed34af5) , ROM_GROUPWORD | ROM_SKIP(6) )
6769   ROMX_LOAD( "va-1m.bin", 0x000004, 0x80000, CRC(0b1ace37) SHA1(6f9493c22f667f683db2789972fd16bb94724679) , ROM_GROUPWORD | ROM_SKIP(6) )
6770   ROMX_LOAD( "va-3m.bin", 0x000006, 0x80000, CRC(44dfe706) SHA1(a013a434df3161a91aafbb35dc4e20dfb3f177f4) , ROM_GROUPWORD | ROM_SKIP(6) )
67686771
67696772   ROM_REGION( 0x8000, REGION_GFX2, 0 )
67706773   ROM_COPY( REGION_GFX1, 0x000000, 0x000000, 0x8000 )   /* stars */
67716774
67726775   ROM_REGION( 0x18000, REGION_CPU2, 0 ) /* 64k for the audio CPU (+banks) */
6773   ROM_LOAD( "va_09.rom",     0x00000, 0x08000, CRC(7a99446e) SHA1(ca027f41e3e58be5abc33ad7380746658cb5380a) )
6774   ROM_CONTINUE(              0x10000, 0x08000 )
6776   ROM_LOAD( "va_09.bin", 0x00000, 0x08000, CRC(7a99446e) SHA1(ca027f41e3e58be5abc33ad7380746658cb5380a) )
6777   ROM_CONTINUE(          0x10000, 0x08000 )
67756778
67766779   ROM_REGION( 0x40000, REGION_SOUND1, 0 )   /* Samples */
6777   ROM_LOAD( "va_18.rom",     0x00000, 0x20000, CRC(de30510e) SHA1(8e878696192606b76a3a0e53553e638d9621cff7) )
6778   ROM_LOAD( "va_19.rom",     0x20000, 0x20000, CRC(0610a4ac) SHA1(3da02ea6a7a56c85de898806d2a1cf6bc526c1b3) )
6780   ROM_LOAD( "va_18.bin", 0x00000, 0x20000, CRC(de30510e) SHA1(8e878696192606b76a3a0e53553e638d9621cff7) )
6781   ROM_LOAD( "va_19.bin", 0x20000, 0x20000, CRC(0610a4ac) SHA1(3da02ea6a7a56c85de898806d2a1cf6bc526c1b3) )
67796782ROM_END
67806783
6784/* FIXME B-Board uncertain but should be 88622B/89625B from the program ROM names */
67816785ROM_START( varthj )
67826786   ROM_REGION( CODE_SIZE, REGION_CPU1, 0 )      /* 68000 code */
67836787   ROM_LOAD16_BYTE( "vaj36b.bin",   0x00000, 0x20000, CRC(1d798d6a) SHA1(b12579e9dcb88416bc00653b143832d9347adbaf) )
r2627r2628
67906794   ROM_LOAD16_BYTE( "vaj41b.bin",   0xc0001, 0x20000, CRC(6542c8a4) SHA1(5f828cf28ef905e4701c92f317e1257a40964a65) )
67916795
67926796   ROM_REGION( 0x200000, REGION_GFX1, 0 )
6793   ROMX_LOAD( "va_gfx5.rom",  0x000000, 0x80000, CRC(b1fb726e) SHA1(5ac0876b6c49d0a99710dda68653664f4d8c1167) , ROM_GROUPWORD | ROM_SKIP(6) )
6794   ROMX_LOAD( "va_gfx7.rom",  0x000002, 0x80000, CRC(4c6588cd) SHA1(d14e8cf051ac934ccc989d8c571c6cc9eed34af5) , ROM_GROUPWORD | ROM_SKIP(6) )
6795   ROMX_LOAD( "va_gfx1.rom",  0x000004, 0x80000, CRC(0b1ace37) SHA1(6f9493c22f667f683db2789972fd16bb94724679) , ROM_GROUPWORD | ROM_SKIP(6) )
6796   ROMX_LOAD( "va_gfx3.rom",  0x000006, 0x80000, CRC(44dfe706) SHA1(a013a434df3161a91aafbb35dc4e20dfb3f177f4) , ROM_GROUPWORD | ROM_SKIP(6) )
6797   ROMX_LOAD( "va-5m.bin", 0x000000, 0x80000, CRC(b1fb726e) SHA1(5ac0876b6c49d0a99710dda68653664f4d8c1167) , ROM_GROUPWORD | ROM_SKIP(6) )
6798   ROMX_LOAD( "va-7m.bin", 0x000002, 0x80000, CRC(4c6588cd) SHA1(d14e8cf051ac934ccc989d8c571c6cc9eed34af5) , ROM_GROUPWORD | ROM_SKIP(6) )
6799   ROMX_LOAD( "va-1m.bin", 0x000004, 0x80000, CRC(0b1ace37) SHA1(6f9493c22f667f683db2789972fd16bb94724679) , ROM_GROUPWORD | ROM_SKIP(6) )
6800   ROMX_LOAD( "va-3m.bin", 0x000006, 0x80000, CRC(44dfe706) SHA1(a013a434df3161a91aafbb35dc4e20dfb3f177f4) , ROM_GROUPWORD | ROM_SKIP(6) )
67976801
67986802   ROM_REGION( 0x8000, REGION_GFX2, 0 )
67996803   ROM_COPY( REGION_GFX1, 0x000000, 0x000000, 0x8000 )   /* stars */
68006804
68016805   ROM_REGION( 0x18000, REGION_CPU2, 0 ) /* 64k for the audio CPU (+banks) */
6802   ROM_LOAD( "va_09.rom",     0x00000, 0x08000, CRC(7a99446e) SHA1(ca027f41e3e58be5abc33ad7380746658cb5380a) )
6803   ROM_CONTINUE(              0x10000, 0x08000 )
6806   ROM_LOAD( "va_09.bin", 0x00000, 0x08000, CRC(7a99446e) SHA1(ca027f41e3e58be5abc33ad7380746658cb5380a) )
6807   ROM_CONTINUE(          0x10000, 0x08000 )
68046808
68056809   ROM_REGION( 0x40000, REGION_SOUND1, 0 )   /* Samples */
6806   ROM_LOAD( "va_18.rom",     0x00000, 0x20000, CRC(de30510e) SHA1(8e878696192606b76a3a0e53553e638d9621cff7) )
6807   ROM_LOAD( "va_19.rom",     0x20000, 0x20000, CRC(0610a4ac) SHA1(3da02ea6a7a56c85de898806d2a1cf6bc526c1b3) )
6810   ROM_LOAD( "va_18.bin", 0x00000, 0x20000, CRC(de30510e) SHA1(8e878696192606b76a3a0e53553e638d9621cff7) )
6811   ROM_LOAD( "va_19.bin", 0x20000, 0x20000, CRC(0610a4ac) SHA1(3da02ea6a7a56c85de898806d2a1cf6bc526c1b3) )
68086812ROM_END
68096813
6814/* FIXME B-Board uncertain but should be 88622B/89625B from the program ROM names */
68106815ROM_START( cworld2j )
68116816   ROM_REGION( CODE_SIZE, REGION_CPU1, 0 )      /* 68000 code */
68126817   ROM_LOAD16_BYTE( "q536.bin",       0x00000, 0x20000, CRC(38a08099) SHA1(961f733baa2bbf8419e4de70f18660098dba7d52) )
r2627r2628
68486853   ROM_LOAD( "q531.bin",      0x20000, 0x20000, CRC(7d17e496) SHA1(a274b94ec4f042dddc239ecb9ac2e1e2375f5eb2) )
68496854ROM_END
68506855
6856/* FIXME B-Board uncertain but should be 91634B/91635B from the program ROM names */
68516857ROM_START( wof )
68526858   ROM_REGION( CODE_SIZE, REGION_CPU1, 0 )      /* 68000 code */
68536859   ROM_LOAD16_WORD_SWAP( "tk2e_23b.rom",  0x000000, 0x80000, CRC(11fb2ed1) SHA1(19e09ad6f9edc7997b030cddfe1d9c96d88135f2) )
r2627r2628
68776883   ROM_LOAD( "tk2_q4.rom",     0x180000, 0x80000, CRC(36642e88) SHA1(8ab25b19e2b67215a5cb1f3aa81b9d26009cfeb8) )
68786884ROM_END
68796885
6886/* FIXME B-Board uncertain but should be 91634B/91635B from the program ROM names */
68806887ROM_START( wofa )
68816888   ROM_REGION( CODE_SIZE, REGION_CPU1, 0 )      /* 68000 code */
68826889   ROM_LOAD16_WORD_SWAP( "tk2a_23b.rom",  0x000000, 0x80000, CRC(2e024628) SHA1(647f8700fe3b410d798a823bac2e4a89cc9ad8d5) )
r2627r2628
69066913   ROM_LOAD( "tk2_q4.rom",     0x180000, 0x80000, CRC(36642e88) SHA1(8ab25b19e2b67215a5cb1f3aa81b9d26009cfeb8) )
69076914ROM_END
69086915
6916/* FIXME B-Board uncertain but should be 91634B/91635B from the program ROM names */
69096917ROM_START( wofu )
69106918   ROM_REGION( CODE_SIZE, REGION_CPU1, 0 )      /* 68000 code */
69116919   ROM_LOAD16_WORD_SWAP( "tk2u.23c",  0x000000, 0x80000, CRC(29b89c12) SHA1(2b474b4f45a4ccb0db2a4d5e7ef30e28b5c6cc3a) )
r2627r2628
69356943   ROM_LOAD( "tk2_q4.rom",     0x180000, 0x80000, CRC(36642e88) SHA1(8ab25b19e2b67215a5cb1f3aa81b9d26009cfeb8) )
69366944ROM_END
69376945
6946/* FIXME B-Board uncertain but should be 91634B/91635B from the program ROM names */
69386947ROM_START( wofj )
69396948   ROM_REGION( CODE_SIZE, REGION_CPU1, 0 )      /* 68000 code */
69406949   ROM_LOAD16_WORD_SWAP( "tk2j23c.bin",  0x000000, 0x80000, CRC(9b215a68) SHA1(fc83ed26441fbfb15e21b093c7a6bed44b586e51) )
r2627r2628
70607069   ROM_LOAD( "s92_19.bin",    0x20000, 0x20000, CRC(beade53f) SHA1(277c397dc12752719ec6b47d2224750bd1c07f79) )
70617070ROM_END
70627071
7072/* FIXME B-Board uncertain but should be 91634B/91635B from the program ROM names */
70637073ROM_START( dino )
70647074   ROM_REGION( CODE_SIZE, REGION_CPU1, 0 )      /* 68000 code */
70657075   ROM_LOAD16_WORD_SWAP( "cde_23a.rom",  0x000000, 0x80000, CRC(8f4e585e) SHA1(55ecba3652066cdafad140c4524b1fc81228e69b) )
r2627r2628
70907100   ROM_LOAD( "cd_q4.rom",      0x180000, 0x80000, CRC(2c67821d) SHA1(6e2528d0b22508300a6a142a796dd3bf53a66946) )
70917101ROM_END
70927102
7103/* FIXME B-Board uncertain but should be 91634B/91635B from the program ROM names */
70937104ROM_START( dinou )
70947105   ROM_REGION( CODE_SIZE, REGION_CPU1, 0 )      /* 68000 code */
70957106   ROM_LOAD16_WORD_SWAP( "cdu.23a",      0x000000, 0x80000, CRC(7c2543cd) SHA1(6b7a90392fe4c31b2d57620b0ddcb3412401efc3) )
r2627r2628
71207131   ROM_LOAD( "cd_q4.rom",      0x180000, 0x80000, CRC(2c67821d) SHA1(6e2528d0b22508300a6a142a796dd3bf53a66946) )
71217132ROM_END
71227133
7134/* FIXME B-Board uncertain but should be 91634B/91635B from the program ROM names */
71237135ROM_START( dinoj )
71247136   ROM_REGION( CODE_SIZE, REGION_CPU1, 0 )      /* 68000 code */
71257137   ROM_LOAD16_WORD_SWAP( "cdj-23a.8f",   0x000000, 0x80000, CRC(5f3ece96) SHA1(33ffb08ff8c5d3bfb2fa17fa00f254da2fc61f44) )
r2627r2628
71507162   ROM_LOAD( "cd_q4.rom",      0x180000, 0x80000, CRC(2c67821d) SHA1(6e2528d0b22508300a6a142a796dd3bf53a66946) )
71517163ROM_END
71527164
7165/* B-Board 91635B */
71537166ROM_START( punisher )
71547167   ROM_REGION( CODE_SIZE, REGION_CPU1, 0 )      /* 68000 code */
71557168   ROM_LOAD16_BYTE( "pse_26.rom",       0x000000, 0x20000, CRC(389a99d2) SHA1(e97f4225554e4603cb0e3edd296a90bb2e467ca7) )
r2627r2628
71867199   ROM_LOAD( "ps_q4.rom",      0x180000, 0x80000, CRC(bed42f03) SHA1(21302f7e75f9c795392a3b34e16a959fc5f6e4e9) )
71877200ROM_END
71887201
7202/* B-Board 91635B */
71897203ROM_START( punishru )
71907204   ROM_REGION( CODE_SIZE, REGION_CPU1, 0 )      /* 68000 code */
71917205   ROM_LOAD16_BYTE ( "psu26.rom",       0x000000, 0x20000, CRC(9236d121) SHA1(52d5d00009f61089157319943cde8f1a1ed48ad4) )
r2627r2628
72227236   ROM_LOAD( "ps_q4.rom",      0x180000, 0x80000, CRC(bed42f03) SHA1(21302f7e75f9c795392a3b34e16a959fc5f6e4e9) )
72237237ROM_END
72247238
7239/* B-Board 91635B */
72257240ROM_START( punishrj )
72267241   ROM_REGION( CODE_SIZE, REGION_CPU1, 0 )      /* 68000 code */
72277242   ROM_LOAD16_WORD_SWAP( "psj23.bin",   0x000000, 0x80000, CRC(6b2fda52) SHA1(5f95a79b7b802609ae9ddd6641cc52610d428bf4) )
r2627r2628
72527267   ROM_LOAD( "ps_q4.rom",      0x180000, 0x80000, CRC(bed42f03) SHA1(21302f7e75f9c795392a3b34e16a959fc5f6e4e9) )
72537268ROM_END
72547269
7270/* FIXME B-Board uncertain but should be 91634B/91635B from the program ROM names */
72557271ROM_START( slammast )
72567272   ROM_REGION( CODE_SIZE, REGION_CPU1, 0 )      /* 68000 code */
72577273   ROM_LOAD16_WORD_SWAP( "mbe_23e.rom",  0x000000, 0x80000, CRC(5394057a) SHA1(57f8b40c0a15e82c98ce5f0a8c4bdf60a1bc3107) )
r2627r2628
72977313   ROM_LOAD( "mb_q8.bin",      0x380000, 0x80000, CRC(59fe702a) SHA1(807178dfc6d864e49fd7aabb5c4895835cf0e85b) )
72987314ROM_END
72997315
7316/* FIXME B-Board uncertain but should be 91634B/91635B from the program ROM names */
73007317ROM_START( slammasu )
73017318   ROM_REGION( CODE_SIZE, REGION_CPU1, 0 )      /* 68000 code */
73027319   ROM_LOAD16_WORD_SWAP( "mbu-23e.rom",  0x000000, 0x80000, CRC(224f0062) SHA1(d961f2e7db7acac576539c24a69e7dd9bf8fc406) )
r2627r2628
73427359   ROM_LOAD( "mb_q8.bin",      0x380000, 0x80000, CRC(59fe702a) SHA1(807178dfc6d864e49fd7aabb5c4895835cf0e85b) )
73437360ROM_END
73447361
7362/* FIXME B-Board uncertain but should be 91634B/91635B from the program ROM names */
73457363ROM_START( mbomberj )
73467364   ROM_REGION( CODE_SIZE, REGION_CPU1, 0 )      /* 68000 code */
73477365   ROM_LOAD16_WORD_SWAP( "mbj23e",       0x000000, 0x80000, CRC(0d06036a) SHA1(e1b102888038b4bb612a41ac94a43333d468a245) )
r2627r2628
73877405   ROM_LOAD( "mb_q8.bin",      0x380000, 0x80000, CRC(59fe702a) SHA1(807178dfc6d864e49fd7aabb5c4895835cf0e85b) )
73887406ROM_END
73897407
7408/* FIXME B-Board uncertain but should be 91634B/91635B from the program ROM names */
73907409ROM_START( mbombrd )
73917410   ROM_REGION( CODE_SIZE, REGION_CPU1, 0 )      /* 68000 code */
73927411   ROM_LOAD16_BYTE( "mbd_26.bin",        0x000000, 0x20000, CRC(72b7451c) SHA1(380ef57bb00f1c92d2f29e551b0a670eb5a56cb7) )
r2627r2628
74327451   ROM_LOAD( "mb_q8.bin",      0x380000, 0x80000, CRC(59fe702a) SHA1(807178dfc6d864e49fd7aabb5c4895835cf0e85b) )
74337452ROM_END
74347453
7454/* FIXME B-Board uncertain but should be 91634B/91635B from the program ROM names */
74357455ROM_START( mbombrdj )
74367456   ROM_REGION( CODE_SIZE, REGION_CPU1, 0 )      /* 68000 code */
74377457   ROM_LOAD16_BYTE( "mbd_26.bin",        0x000000, 0x20000, CRC(72b7451c) SHA1(380ef57bb00f1c92d2f29e551b0a670eb5a56cb7) )
r2627r2628
74777497   ROM_LOAD( "mb_q8.bin",      0x380000, 0x80000, CRC(59fe702a) SHA1(807178dfc6d864e49fd7aabb5c4895835cf0e85b) )
74787498ROM_END
74797499
7500/* FIXME B-Board uncertain but should be 88622B/89625B from the program ROM names */
74807501ROM_START( pnickj )
74817502   ROM_REGION( CODE_SIZE, REGION_CPU1, 0 )      /* 68000 code */
74827503   ROM_LOAD16_BYTE( "pnij36.bin",   0x00000, 0x20000, CRC(2d4ffb2b) SHA1(6e49cf89a36834fd1de8b4b860fe66f3d7d67a84) )
r2627r2628
75127533   ROM_LOAD( "pnij25.bin",    0x20000, 0x20000, CRC(22109aaa) SHA1(cf21e75674d81b2daae2083d02f9f4b6e52722c6) )
75137534ROM_END
75147535
7536/* B-Board 89625B */
75157537ROM_START( qad )
75167538   ROM_REGION( CODE_SIZE, REGION_CPU1, 0 )      /* 68000 code */
7517   ROM_LOAD16_BYTE( "qdu_36a.rom",  0x00000, 0x20000, CRC(de9c24a0) SHA1(458962943e8d97d1f4e5a15ac1c8d3bcaa32918b) )
7518   ROM_LOAD16_BYTE( "qdu_42a.rom",  0x00001, 0x20000, CRC(cfe36f0c) SHA1(370a47461b2dbb7807f547f5b4b33296572c5d78) )
7519   ROM_LOAD16_BYTE( "qdu_37a.rom",  0x40000, 0x20000, CRC(10d22320) SHA1(73b2876d5447f50a850c466789d9297269f732d6) )
7520   ROM_LOAD16_BYTE( "qdu_43a.rom",  0x40001, 0x20000, CRC(15e6beb9) SHA1(68d11e9bdd82775060281c5880f249e3515dc235) )
7539   ROM_LOAD16_BYTE( "qdu_36a.12f", 0x00000, 0x20000, CRC(de9c24a0) SHA1(458962943e8d97d1f4e5a15ac1c8d3bcaa32918b) )
7540   ROM_LOAD16_BYTE( "qdu_42a.12h", 0x00001, 0x20000, CRC(cfe36f0c) SHA1(370a47461b2dbb7807f547f5b4b33296572c5d78) )
7541   ROM_LOAD16_BYTE( "qdu_37a.13f", 0x40000, 0x20000, CRC(10d22320) SHA1(73b2876d5447f50a850c466789d9297269f732d6) )
7542   ROM_LOAD16_BYTE( "qdu_43a.13h", 0x40001, 0x20000, CRC(15e6beb9) SHA1(68d11e9bdd82775060281c5880f249e3515dc235) )
75217543
75227544   ROM_REGION( 0x200000, REGION_GFX1, 0 )
7523   ROMX_LOAD( "qdu_09.rom", 0x000000, 0x20000, CRC(8c3f9f44) SHA1(b5ab20515b7f3e7db023be42d4c7ed1941b37d9b) , ROM_SKIP(7) )
7524   ROMX_LOAD( "qdu_01.rom", 0x000001, 0x20000, CRC(f688cf8f) SHA1(1b20095e536a24406513715cded249c9be1aa1d2) , ROM_SKIP(7) )
7525   ROMX_LOAD( "qdu_13.rom", 0x000002, 0x20000, CRC(afbd551b) SHA1(02e2f12196c542a004325689bda8949213ef0333) , ROM_SKIP(7) )
7526   ROMX_LOAD( "qdu_05.rom", 0x000003, 0x20000, CRC(c3db0910) SHA1(cf3aa3d3b64031dea92a80e5650151315cf871bf) , ROM_SKIP(7) )
7527   ROMX_LOAD( "qdu_24.rom", 0x000004, 0x20000, CRC(2f1bd0ec) SHA1(017e0dc521bf402c700775ee06cbc124f7ce0e3f) , ROM_SKIP(7) )
7528   ROMX_LOAD( "qdu_17.rom", 0x000005, 0x20000, CRC(a812f9e2) SHA1(9b7ceb347fbe00c40338b97ee6e8e4d1db9e7cb3) , ROM_SKIP(7) )
7529   ROMX_LOAD( "qdu_38.rom", 0x000006, 0x20000, CRC(ccdddd1f) SHA1(8304c4cdfaa1ae6b37e2733e9a6ddce9252fd43a) , ROM_SKIP(7) )
7530   ROMX_LOAD( "qdu_32.rom", 0x000007, 0x20000, CRC(a8d295d3) SHA1(d4d0bdaeb40f652ef33b317cb2b566b4c3550242) , ROM_SKIP(7) )
7545   ROMX_LOAD( "qdu_09.4b", 0x000000, 0x20000, CRC(8c3f9f44) SHA1(b5ab20515b7f3e7db023be42d4c7ed1941b37d9b) , ROM_SKIP(7) )
7546   ROMX_LOAD( "qdu_01.4a", 0x000001, 0x20000, CRC(f688cf8f) SHA1(1b20095e536a24406513715cded249c9be1aa1d2) , ROM_SKIP(7) )
7547   ROMX_LOAD( "qdu_13.9b", 0x000002, 0x20000, CRC(afbd551b) SHA1(02e2f12196c542a004325689bda8949213ef0333) , ROM_SKIP(7) )
7548   ROMX_LOAD( "qdu_05.9a", 0x000003, 0x20000, CRC(c3db0910) SHA1(cf3aa3d3b64031dea92a80e5650151315cf871bf) , ROM_SKIP(7) )
7549   ROMX_LOAD( "qdu_24.5e", 0x000004, 0x20000, CRC(2f1bd0ec) SHA1(017e0dc521bf402c700775ee06cbc124f7ce0e3f) , ROM_SKIP(7) )
7550   ROMX_LOAD( "qdu_17.5c", 0x000005, 0x20000, CRC(a812f9e2) SHA1(9b7ceb347fbe00c40338b97ee6e8e4d1db9e7cb3) , ROM_SKIP(7) )
7551   ROMX_LOAD( "qdu_38.8h", 0x000006, 0x20000, CRC(ccdddd1f) SHA1(8304c4cdfaa1ae6b37e2733e9a6ddce9252fd43a) , ROM_SKIP(7) )
7552   ROMX_LOAD( "qdu_32.8f", 0x000007, 0x20000, CRC(a8d295d3) SHA1(d4d0bdaeb40f652ef33b317cb2b566b4c3550242) , ROM_SKIP(7) )
75317553
75327554   ROM_REGION( 0x8000, REGION_GFX2, 0 )
75337555   ROM_COPY( REGION_GFX1, 0x000000, 0x000000, 0x8000 )   /* stars */
75347556
75357557   ROM_REGION( 0x18000, REGION_CPU2, 0 ) /* 64k for the audio CPU (+banks) */
7536   ROM_LOAD( "qdu_23.rom",    0x00000, 0x08000, CRC(cfb5264b) SHA1(e662ed5555d02ccf4e62cdbcfa0bbfc019734ee1) )
7537   ROM_CONTINUE(              0x10000, 0x08000 )
7558   ROM_LOAD( "qdu_23.13b", 0x00000, 0x08000, CRC(cfb5264b) SHA1(e662ed5555d02ccf4e62cdbcfa0bbfc019734ee1) )
7559   ROM_CONTINUE(           0x10000, 0x08000 )
75387560
75397561   ROM_REGION( 0x40000, REGION_SOUND1, 0 )   /* Samples */
7540   ROM_LOAD( "qdu_30.rom",    0x00000, 0x20000, CRC(f190da84) SHA1(d5cd4c69b5d135a2f2fea8ca9631251c9da79e70) )
7541   ROM_LOAD( "qdu_31.rom",    0x20000, 0x20000, CRC(b7583f73) SHA1(3896e0fcf375e9e5d9ba70cc1ed001cd702f9ff7) )
7562   ROM_LOAD( "qdu_30.12c", 0x00000, 0x20000, CRC(f190da84) SHA1(d5cd4c69b5d135a2f2fea8ca9631251c9da79e70) )
7563   ROM_LOAD( "qdu_31.13c", 0x20000, 0x20000, CRC(b7583f73) SHA1(3896e0fcf375e9e5d9ba70cc1ed001cd702f9ff7) )
75427564ROM_END
75437565
7566/* FIXME B-Board uncertain but should be 91634B/91635B from the program ROM names */
75447567ROM_START( qadj )
75457568   ROM_REGION( CODE_SIZE, REGION_CPU1, 0 )      /* 68000 code */
75467569   ROM_LOAD16_WORD_SWAP( "qad23a.bin",   0x00000, 0x080000, CRC(4d3553de) SHA1(07eabcb02fbbe11397ce91405a2e6bb53b3d5d4f) )
r2627r2628
75647587   ROM_LOAD( "qad19.bin",     0x20000, 0x20000, CRC(13d3236b) SHA1(785d49de484e9ac6971eaceebebfecb8e58563f6) )
75657588ROM_END
75667589
7590/* FIXME B-Board uncertain but should be 90629B from the program ROM names */
75677591ROM_START( qtono2 )
75687592   ROM_REGION( CODE_SIZE, REGION_CPU1, 0 )      /* 68000 code */
7569   ROM_LOAD16_BYTE( "tn2j-30.11e",  0x00000, 0x20000, CRC(9226eb5e) SHA1(91649974f9652caed90eb28ec6caf4fe61f5d279) )
7570   ROM_LOAD16_BYTE( "tn2j-37.11f",  0x00001, 0x20000, CRC(d1d30da1) SHA1(7ca1695ed804b5860d4c15964cdbb922db3918ee) )
7571   ROM_LOAD16_BYTE( "tn2j-31.12e",  0x40000, 0x20000, CRC(015e6a8a) SHA1(0835bec4867438a167bd01e3550090c88e7ae779) )
7572   ROM_LOAD16_BYTE( "tn2j-38.12f",  0x40001, 0x20000, CRC(1f139bcc) SHA1(ee907f1bfef1a887e2c768648fe811e0733eddf7) )
7573   ROM_LOAD16_BYTE( "tn2j-28.9e",   0x80000, 0x20000, CRC(86d27f71) SHA1(89d6d18e05deaaa1ac7deb70ca03d051d2fde472) )
7574   ROM_LOAD16_BYTE( "tn2j-35.9f",   0x80001, 0x20000, CRC(7a1ab87d) SHA1(f1729a8c0c82cf42f60644a7796dc8a39bf7c6fa) )
7575   ROM_LOAD16_BYTE( "tn2j-29.10e",  0xc0000, 0x20000, CRC(9c384e99) SHA1(3d3961f625ccc4776531eff50fc1b4bee062370e) )
7576   ROM_LOAD16_BYTE( "tn2j-36.10f",  0xc0001, 0x20000, CRC(4c4b2a0a) SHA1(9a25fcfb9358ea42d9bc662df2cafea08febb411) )
7593   ROM_LOAD16_BYTE( "tn2j-30.11e", 0x00000, 0x20000, CRC(9226eb5e) SHA1(91649974f9652caed90eb28ec6caf4fe61f5d279) )
7594   ROM_LOAD16_BYTE( "tn2j-37.11f", 0x00001, 0x20000, CRC(d1d30da1) SHA1(7ca1695ed804b5860d4c15964cdbb922db3918ee) )
7595   ROM_LOAD16_BYTE( "tn2j-31.12e", 0x40000, 0x20000, CRC(015e6a8a) SHA1(0835bec4867438a167bd01e3550090c88e7ae779) )
7596   ROM_LOAD16_BYTE( "tn2j-38.12f", 0x40001, 0x20000, CRC(1f139bcc) SHA1(ee907f1bfef1a887e2c768648fe811e0733eddf7) )
7597   ROM_LOAD16_BYTE( "tn2j-28.9e",  0x80000, 0x20000, CRC(86d27f71) SHA1(89d6d18e05deaaa1ac7deb70ca03d051d2fde472) )
7598   ROM_LOAD16_BYTE( "tn2j-35.9f",  0x80001, 0x20000, CRC(7a1ab87d) SHA1(f1729a8c0c82cf42f60644a7796dc8a39bf7c6fa) )
7599   ROM_LOAD16_BYTE( "tn2j-29.10e", 0xc0000, 0x20000, CRC(9c384e99) SHA1(3d3961f625ccc4776531eff50fc1b4bee062370e) )
7600   ROM_LOAD16_BYTE( "tn2j-36.10f", 0xc0001, 0x20000, CRC(4c4b2a0a) SHA1(9a25fcfb9358ea42d9bc662df2cafea08febb411) )
75777601
75787602   ROM_REGION( 0x400000, REGION_GFX1, 0 )
7579   ROMX_LOAD( "tn2-02m.4a",   0x000000, 0x80000, CRC(f2016a34) SHA1(3862960fa14742547f6a6deacf0b9f409d08fee8) , ROM_GROUPWORD | ROM_SKIP(6) )
7580   ROMX_LOAD( "tn2-04m.6a",   0x000002, 0x80000, CRC(094e0fb1) SHA1(7c9a9a7d03e226109002dd389c872e3d4be43287) , ROM_GROUPWORD | ROM_SKIP(6) )
7581   ROMX_LOAD( "tn2-01m.3a",   0x000004, 0x80000, CRC(cb950cf9) SHA1(8337a500141c1aec82b6636ad79ecafbdbebd691) , ROM_GROUPWORD | ROM_SKIP(6) )
7582   ROMX_LOAD( "tn2-03m.5a",   0x000006, 0x80000, CRC(18a5bf59) SHA1(afbfcb28c40551747bb5276aac2b9c15a24328e1) , ROM_GROUPWORD | ROM_SKIP(6) )
7583   ROMX_LOAD( "tn2-11m.4c",   0x200000, 0x80000, CRC(d0edd30b) SHA1(a76d7f134f9e52f79a485402d17dcc7a1fe99f29) , ROM_GROUPWORD | ROM_SKIP(6) )
7584   ROMX_LOAD( "tn2-13m.6c",   0x200002, 0x80000, CRC(426621c3) SHA1(89156bc9d585f546cd619db419dd1f4d9871d930) , ROM_GROUPWORD | ROM_SKIP(6) )
7585   ROMX_LOAD( "tn2-10m.3c",   0x200004, 0x80000, CRC(a34ece70) SHA1(15864d6b280f624245add8a611f1699da570392b) , ROM_GROUPWORD | ROM_SKIP(6) )
7586   ROMX_LOAD( "tn2-12m.5c",   0x200006, 0x80000, CRC(e04ff2f4) SHA1(774c19909a2ae2c691f5d3f15b6e19cc94baf799) , ROM_GROUPWORD | ROM_SKIP(6) )
7603   ROMX_LOAD( "tn2-02m.4a", 0x000000, 0x80000, CRC(f2016a34) SHA1(3862960fa14742547f6a6deacf0b9f409d08fee8) , ROM_GROUPWORD | ROM_SKIP(6) )
7604   ROMX_LOAD( "tn2-04m.6a", 0x000002, 0x80000, CRC(094e0fb1) SHA1(7c9a9a7d03e226109002dd389c872e3d4be43287) , ROM_GROUPWORD | ROM_SKIP(6) )
7605   ROMX_LOAD( "tn2-01m.3a", 0x000004, 0x80000, CRC(cb950cf9) SHA1(8337a500141c1aec82b6636ad79ecafbdbebd691) , ROM_GROUPWORD | ROM_SKIP(6) )
7606   ROMX_LOAD( "tn2-03m.5a", 0x000006, 0x80000, CRC(18a5bf59) SHA1(afbfcb28c40551747bb5276aac2b9c15a24328e1) , ROM_GROUPWORD | ROM_SKIP(6) )
7607   ROMX_LOAD( "tn2-11m.4c", 0x200000, 0x80000, CRC(d0edd30b) SHA1(a76d7f134f9e52f79a485402d17dcc7a1fe99f29) , ROM_GROUPWORD | ROM_SKIP(6) )
7608   ROMX_LOAD( "tn2-13m.6c", 0x200002, 0x80000, CRC(426621c3) SHA1(89156bc9d585f546cd619db419dd1f4d9871d930) , ROM_GROUPWORD | ROM_SKIP(6) )
7609   ROMX_LOAD( "tn2-10m.3c", 0x200004, 0x80000, CRC(a34ece70) SHA1(15864d6b280f624245add8a611f1699da570392b) , ROM_GROUPWORD | ROM_SKIP(6) )
7610   ROMX_LOAD( "tn2-12m.5c", 0x200006, 0x80000, CRC(e04ff2f4) SHA1(774c19909a2ae2c691f5d3f15b6e19cc94baf799) , ROM_GROUPWORD | ROM_SKIP(6) )
75877611
75887612   ROM_REGION( 0x8000, REGION_GFX2, 0 )
75897613   ROM_COPY( REGION_GFX1, 0x000000, 0x000000, 0x8000 )   /* stars */
75907614
75917615   ROM_REGION( 0x18000, REGION_CPU2, 0 ) /* 64k for the audio CPU (+banks) */
7592   ROM_LOAD( "tn2j-09.12a",   0x00000, 0x08000, CRC(6d8edcef) SHA1(9ec2d64278b30cc4316238c3efee663a8bbb255e) )
7616   ROM_LOAD( "tn2j-09.12a", 0x00000, 0x08000, CRC(6d8edcef) SHA1(9ec2d64278b30cc4316238c3efee663a8bbb255e) )
75937617
75947618   ROM_REGION( 0x40000, REGION_SOUND1, 0 )   /* Samples */
7595   ROM_LOAD( "tn2j-18.11c",   0x00000, 0x20000, CRC(a40bf9a7) SHA1(07cb1076262a281e31a621cbcc10be0cae883175) )
7596   ROM_LOAD( "tn2j-19.12c",   0x20000, 0x20000, CRC(5b3b931e) SHA1(cf28891f84814cbfaa3adaade8bb08b1e0546a3d) )
7619   ROM_LOAD( "tn2j-18.11c", 0x00000, 0x20000, CRC(a40bf9a7) SHA1(07cb1076262a281e31a621cbcc10be0cae883175) )
7620   ROM_LOAD( "tn2j-19.12c", 0x20000, 0x20000, CRC(5b3b931e) SHA1(cf28891f84814cbfaa3adaade8bb08b1e0546a3d) )
75977621ROM_END
75987622
7623/* B-Board 94916-10 */
75997624ROM_START( pang3 )
76007625   ROM_REGION( CODE_SIZE, REGION_CPU1, 0 )      /* 68000 code */
7601   ROM_LOAD16_WORD_SWAP( "pa3w-17.11l",  0x00000, 0x80000, CRC(12138234) SHA1(956a2c847a3cfb94007d1a636167fd2bb9f826ec) )
7602   ROM_LOAD16_WORD_SWAP( "pa3w-16.10l",  0x80000, 0x80000, CRC(d1ba585c) SHA1(c6d04441fe97abf0a72b23c917777a7b58e94a85) )
7626   ROM_LOAD16_WORD_SWAP( "pa3w-17.11l", 0x00000, 0x80000, CRC(12138234) SHA1(956a2c847a3cfb94007d1a636167fd2bb9f826ec) )
7627   ROM_LOAD16_WORD_SWAP( "pa3w-16.10l", 0x80000, 0x80000, CRC(d1ba585c) SHA1(c6d04441fe97abf0a72b23c917777a7b58e94a85) )
76037628
76047629   ROM_REGION( 0x400000, REGION_GFX1, 0 )
7605   ROMX_LOAD( "pa3-01m.2c",   0x000000, 0x100000, CRC(068a152c) SHA1(fa491874068924c39bcc7de93dfda3b27f5d9613) , ROM_GROUPWORD | ROM_SKIP(6) )
7606   ROM_CONTINUE(              0x000004, 0x100000 )
7607   ROMX_LOAD( "pa3-07m.2f",   0x000002, 0x100000, CRC(3a4a619d) SHA1(cfe68e24632b53fb6cd6d03b2166d6b5ba28b778) , ROM_GROUPWORD | ROM_SKIP(6) )
7608   ROM_CONTINUE(              0x000006, 0x100000 )
7630   ROMX_LOAD( "pa3-01m.2c", 0x000000, 0x100000, CRC(068a152c) SHA1(fa491874068924c39bcc7de93dfda3b27f5d9613) , ROM_GROUPWORD | ROM_SKIP(6) )
7631   ROM_CONTINUE(            0x000004, 0x100000 )
7632   ROMX_LOAD( "pa3-07m.2f", 0x000002, 0x100000, CRC(3a4a619d) SHA1(cfe68e24632b53fb6cd6d03b2166d6b5ba28b778) , ROM_GROUPWORD | ROM_SKIP(6) )
7633   ROM_CONTINUE(            0x000006, 0x100000 )
76097634
76107635   ROM_REGION( 0x8000, REGION_GFX2, 0 )
76117636   ROM_COPY( REGION_GFX1, 0x000000, 0x000000, 0x8000 )   /* stars */
76127637
76137638   ROM_REGION( 0x18000, REGION_CPU2, 0 ) /* 64k for the audio CPU (+banks) */
7614   ROM_LOAD( "pa3-11.11f",    0x00000, 0x08000, CRC(90a08c46) SHA1(7544adab2d7e052e0d21c920bff7841d9d718345) )
7639   ROM_LOAD( "pa3-11.11f",  0x00000, 0x08000, CRC(90a08c46) SHA1(7544adab2d7e052e0d21c920bff7841d9d718345) )
76157640
76167641   ROM_REGION( 0x40000, REGION_SOUND1, 0 )   /* Samples */
7617   ROM_LOAD( "pa3-05.10d",    0x00000, 0x20000, CRC(73a10d5d) SHA1(999465e4fbc35a34746d2db61ad49f61403d5af7) )
7618   ROM_LOAD( "pa3-06.11d",    0x20000, 0x20000, CRC(affa4f82) SHA1(27b9292bbc121cf585f53297a79fe8f0d0a729ae) )
7642   ROM_LOAD( "pa3-05.10d",  0x00000, 0x20000, CRC(73a10d5d) SHA1(999465e4fbc35a34746d2db61ad49f61403d5af7) )
7643   ROM_LOAD( "pa3-06.11d",  0x20000, 0x20000, CRC(affa4f82) SHA1(27b9292bbc121cf585f53297a79fe8f0d0a729ae) )
76197644ROM_END
76207645
7646/* B-Board 94916-10 */
76217647ROM_START( pang3j )
76227648   ROM_REGION( CODE_SIZE, REGION_CPU1, 0 )      /* 68000 code */
7623   ROM_LOAD16_WORD_SWAP( "pa3j-17.11l",  0x00000, 0x80000, CRC(21f6e51f) SHA1(b447e05261f59b3b2e89bbc0f606d7136b29cb56) )
7624   ROM_LOAD16_WORD_SWAP( "pa3j-16.10l",  0x80000, 0x80000, CRC(ca1d7897) SHA1(46aa9232e81a838f3eff1e9b992492a264914fd5) )
7649   ROM_LOAD16_WORD_SWAP( "pa3j-17.11l", 0x00000, 0x80000, CRC(21f6e51f) SHA1(b447e05261f59b3b2e89bbc0f606d7136b29cb56) )
7650   ROM_LOAD16_WORD_SWAP( "pa3j-16.10l", 0x80000, 0x80000, CRC(ca1d7897) SHA1(46aa9232e81a838f3eff1e9b992492a264914fd5) )
76257651
76267652   ROM_REGION( 0x400000, REGION_GFX1, 0 )
7627   ROMX_LOAD( "pa3-01m.2c",   0x000000, 0x100000, CRC(068a152c) SHA1(fa491874068924c39bcc7de93dfda3b27f5d9613) , ROM_GROUPWORD | ROM_SKIP(6) )
7628   ROM_CONTINUE(              0x000004, 0x100000 )
7629   ROMX_LOAD( "pa3-07m.2f",   0x000002, 0x100000, CRC(3a4a619d) SHA1(cfe68e24632b53fb6cd6d03b2166d6b5ba28b778) , ROM_GROUPWORD | ROM_SKIP(6) )
7630   ROM_CONTINUE(              0x000006, 0x100000 )
7653   ROMX_LOAD( "pa3-01m.2c", 0x000000, 0x100000, CRC(068a152c) SHA1(fa491874068924c39bcc7de93dfda3b27f5d9613) , ROM_GROUPWORD | ROM_SKIP(6) )
7654   ROM_CONTINUE(            0x000004, 0x100000 )
7655   ROMX_LOAD( "pa3-07m.2f", 0x000002, 0x100000, CRC(3a4a619d) SHA1(cfe68e24632b53fb6cd6d03b2166d6b5ba28b778) , ROM_GROUPWORD | ROM_SKIP(6) )
7656   ROM_CONTINUE(            0x000006, 0x100000 )
76317657
76327658   ROM_REGION( 0x8000, REGION_GFX2, 0 )
76337659   ROM_COPY( REGION_GFX1, 0x000000, 0x000000, 0x8000 )   /* stars */
76347660
76357661   ROM_REGION( 0x18000, REGION_CPU2, 0 ) /* 64k for the audio CPU (+banks) */
7636   ROM_LOAD( "pa3-11.11f",    0x00000, 0x08000, CRC(90a08c46) SHA1(7544adab2d7e052e0d21c920bff7841d9d718345) )
7662   ROM_LOAD( "pa3-11.11f",  0x00000, 0x08000, CRC(90a08c46) SHA1(7544adab2d7e052e0d21c920bff7841d9d718345) )
76377663
76387664   ROM_REGION( 0x40000, REGION_SOUND1, 0 )   /* Samples */
7639   ROM_LOAD( "pa3-05.10d",    0x00000, 0x20000, CRC(73a10d5d) SHA1(999465e4fbc35a34746d2db61ad49f61403d5af7) )
7640   ROM_LOAD( "pa3-06.11d",    0x20000, 0x20000, CRC(affa4f82) SHA1(27b9292bbc121cf585f53297a79fe8f0d0a729ae) )
7665   ROM_LOAD( "pa3-05.10d",  0x00000, 0x20000, CRC(73a10d5d) SHA1(999465e4fbc35a34746d2db61ad49f61403d5af7) )
7666   ROM_LOAD( "pa3-06.11d",  0x20000, 0x20000, CRC(affa4f82) SHA1(27b9292bbc121cf585f53297a79fe8f0d0a729ae) )
76417667ROM_END
76427668
7669/* B-Board uncertain but should be 91634B/91635B from the program ROM names */
7670/* there's a photo of a 91635B board but it's clearly not original */
76437671ROM_START( megaman )
76447672   ROM_REGION( CODE_SIZE, REGION_CPU1, 0 )      /* 68000 code */
76457673   ROM_LOAD16_WORD_SWAP( "rcma_23b.rom",   0x000000, 0x80000, CRC(61e4a397) SHA1(a90b1cbef4206a4554398bc458a4b3e2c46d4c4f) )
r2627r2628
76767704   ROM_LOAD( "rcm_19.rom",    0x20000, 0x20000, CRC(f257dbe1) SHA1(967def6b6f93039dbc46373caabeb3301577be75) )
76777705ROM_END
76787706
7707/* B-Board 91634B */
76797708ROM_START( rockmanj )
76807709   ROM_REGION( CODE_SIZE, REGION_CPU1, 0 )      /* 68000 code */
76817710   ROM_LOAD16_WORD_SWAP( "rcm23a.bin",   0x000000, 0x80000, CRC(efd96cb2) SHA1(cbe81554f60d0c897f3f2ebc5bc966bb03cc23fe) )
src/mame/drivers/yunsun16.c
r2627r2628
1414---------------------------------------------------------------------------
1515Year + Game         Board#
1616---------------------------------------------------------------------------
1719?? Magic Bubble     YS-1302 / YS102
181997 Shocking
17199? Magic Bubble     YS-1302 / YS102
181996 Paparazzi        YS-0211? Looks identical
191997 Shocking         YS-0211
19201998 Bomb Kick        YS-0211
2021---------------------------------------------------------------------------
2122
r2627r2628
125126
126127static ADDRESS_MAP_START( main_map, ADDRESS_SPACE_PROGRAM, 16 )
127128   AM_RANGE(0x000000, 0x07ffff) AM_ROM
128   AM_RANGE(0x800000, 0x800001) AM_READ(input_port_0_word_r               )   // P1 + P2
129   AM_RANGE(0x800018, 0x800019) AM_READ(input_port_1_word_r               )   // Coins
130   AM_RANGE(0x80001a, 0x80001b) AM_READ(input_port_2_word_r               )   // DSW1
131   AM_RANGE(0x80001c, 0x80001d) AM_READ(input_port_3_word_r               )   // DSW2
132   AM_RANGE(0x800030, 0x800031) AM_WRITE(SMH_NOP                        )   // ? (value: don't care)
133   AM_RANGE(0x800100, 0x800101) AM_WRITE(SMH_NOP                        )   // ? $9100
134   AM_RANGE(0x800102, 0x800103) AM_WRITE(SMH_NOP                        )   // ? $9080
135   AM_RANGE(0x800104, 0x800105) AM_WRITE(SMH_NOP                        )   // ? $90c0
136   AM_RANGE(0x80010a, 0x80010b) AM_WRITE(SMH_NOP                        )   // ? $9000
129   AM_RANGE(0x800000, 0x800001) AM_READ(input_port_0_word_r            )   // P1 + P2
130   AM_RANGE(0x800018, 0x800019) AM_READ(input_port_1_word_r            )   // Coins
131   AM_RANGE(0x80001a, 0x80001b) AM_READ(input_port_2_word_r            )   // DSW1
132   AM_RANGE(0x80001c, 0x80001d) AM_READ(input_port_3_word_r            )   // DSW2
133   AM_RANGE(0x800030, 0x800031) AM_WRITE(SMH_NOP                  )   // ? (value: don't care)
134   AM_RANGE(0x800100, 0x800101) AM_WRITE(SMH_NOP                  )   // ? $9100
135   AM_RANGE(0x800102, 0x800103) AM_WRITE(SMH_NOP                  )   // ? $9080
136   AM_RANGE(0x800104, 0x800105) AM_WRITE(SMH_NOP                  )   // ? $90c0
137   AM_RANGE(0x80010a, 0x80010b) AM_WRITE(SMH_NOP                  )   // ? $9000
137138   AM_RANGE(0x80010c, 0x80010f) AM_RAM AM_BASE(&yunsun16_scroll_1            )   // Scrolling
138139   AM_RANGE(0x800114, 0x800117) AM_RAM AM_BASE(&yunsun16_scroll_0            )   // Scrolling
139140   AM_RANGE(0x800154, 0x800155) AM_RAM AM_BASE(&yunsun16_priority            )   // Priority
140141   AM_RANGE(0x800180, 0x800181) AM_WRITE(yunsun16_sound_bank_w               )   // Sound
141142   AM_RANGE(0x800188, 0x800189) AM_READWRITE(OKIM6295_status_0_lsb_r, OKIM6295_data_0_lsb_w   )   // Sound
142   AM_RANGE(0x8001fe, 0x8001ff) AM_WRITE(SMH_NOP                                    )   // ? 0 (during int)
143   AM_RANGE(0x8001fe, 0x8001ff) AM_WRITE(SMH_NOP                     )   // ? 0 (during int)
143144   AM_RANGE(0x900000, 0x903fff) AM_RAM_WRITE(paletteram16_xRRRRRGGGGGBBBBB_word_w) AM_BASE(&paletteram16)   // Palette
144145   AM_RANGE(0x908000, 0x90bfff) AM_RAM_WRITE(yunsun16_vram_1_w) AM_BASE(&yunsun16_vram_1   )   // Layer 1
145146   AM_RANGE(0x90c000, 0x90ffff) AM_RAM_WRITE(yunsun16_vram_0_w) AM_BASE(&yunsun16_vram_0   )   // Layer 0
146   AM_RANGE(0x910000, 0x910fff) AM_RAM   AM_BASE(&spriteram16) AM_SIZE(&spriteram_size               )   // Sprites
147   AM_RANGE(0x910000, 0x910fff) AM_RAM   AM_BASE(&spriteram16) AM_SIZE(&spriteram_size   )   // Sprites
147148   AM_RANGE(0xff0000, 0xffffff) AM_RAM
148149ADDRESS_MAP_END
149150
r2627r2628
185186
186187static ADDRESS_MAP_START( sound_port_map, ADDRESS_SPACE_IO, 8 )
187188   ADDRESS_MAP_GLOBAL_MASK(0xff)
188   AM_RANGE(0x10, 0x10) AM_READWRITE(YM3812_status_port_0_r, YM3812_control_port_0_w   )   // YM3812
189   AM_RANGE(0x11, 0x11) AM_WRITE(YM3812_write_port_0_w                           )
190   AM_RANGE(0x18, 0x18) AM_READ(soundlatch_r                                 )   // From Main CPU
191   AM_RANGE(0x1c, 0x1c) AM_READWRITE(OKIM6295_status_0_r, OKIM6295_data_0_w         )   // M6295
189   AM_RANGE(0x10, 0x10) AM_READWRITE(YM3812_status_port_0_r, YM3812_control_port_0_w )   // YM3812
190   AM_RANGE(0x11, 0x11) AM_WRITE(YM3812_write_port_0_w )
191   AM_RANGE(0x18, 0x18) AM_READ(soundlatch_r )                  // From Main CPU
192   AM_RANGE(0x1c, 0x1c) AM_READWRITE(OKIM6295_status_0_r, OKIM6295_data_0_w )      // M6295
192193ADDRESS_MAP_END
193194
194195
r2627r2628
669670
670671                                Magic Bubble
671672
672by Yun Sung YS1302
673Yun Sung 199x
673674
675YunSung YS1302
676
674677PCB Layout
675678----------
676679
r2627r2628
700703U67, 68, 69, 70 --------27c040
701704U32, 33 ----------------27c020
702705
703U143, 131 .......most likely sound
704U32, 33 .........most likely program
705U20-23 ..........most likely sprites
706U67-70 ..........most likely BG
706U143, 131 .......Sound CPU code & Samples
707U32, 33 .........Program code
708U20-23 ..........Sprites
709U67-70 ..........Backgrounds
707710
708711Actel A1020B is close to U67-70
709712
71068HC000 p16 is close to  U32,33
71368HC000P16 is close to  U32,33
711714
71271516.000000 MHz
713716
r2627r2628
778781
779782/***************************************************************************
780783
781                                Shocking
782Yunsung, 1997
784YunSung YS-0211 based games:
783785
786Paparazzi (c) 1996 (no PCB label but looks identical)
787Shocking  (c) 1997
788Bomb Kick (c) 1998
789
784790PCB Layout
785791----------
786792
787793|-------------------------------------------------|
788|UPC1242  ROM.U131           ROM.U20     6116     |
789|     VOL M6295              ROM.U22     6116     |
790|  PAL 6264                  ROM.U21              |
791|      6264                  ROM.U23     6116     |
794|UPC1242    u131               u20       6116     |
795|     VOL M6295                u22       6116     |
796|  PAL 6264                    u21                |
797|      6264                    u23       6116     |
792798|                                        6116     |
793799|                  PAL                      PAL   |
794800|J                 PAL                      PAL   |
795801|A  DSW1                       PAL                |
796802|M                                                |
797|M  DSW2                PAL   ACTEL         PAL   |
798|A        PAL           PAL   A1020B  6116  6116  |
799|         PAL  PAL      PAL           6116  6116  |
800|              PAL                    ROM.U70     |
801|                62256                ROM.U69     |
802|        68000   62256                ROM.U68     |
803|                ROM.U32              ROM.U67     |
804|16MHz           ROM.U33           62256  62256   |
803|M  DSW2  PAL           PAL                 PAL   |
804|A        PAL           PAL   ACTEL   6116  6116  |
805|              PAL      PAL   A1020B  6116  6116  |
806|          6   PAL                      u70 u74   |
807|          8     62256                  u69 u73   |
808|          0     62256                  u68 u72   |
809|          0      u32                   u67 u71   |
810|16MHz     0      u33              62256  62256   |
805811|-------------------------------------------------|
806812Notes:
807813      68000 clock - 16MHz
r2627r2628
811817
812818***************************************************************************/
813819
814ROM_START( shocking )
820/***************************************************************************
815821
822                       Paparazzi  -  Yun Sung, 1996
823
824***************************************************************************/
825
826ROM_START( paprazzi )
827
816828   ROM_REGION( 0x080000, REGION_CPU1, 0 )      /* 68000 Code */
817   ROM_LOAD16_BYTE( "yunsun16.u33", 0x000000, 0x040000, CRC(8a155521) SHA1(000c9095558e6cae30ce43a885c3fbcf55713f40) )
818   ROM_LOAD16_BYTE( "yunsun16.u32", 0x000001, 0x040000, CRC(c4998c10) SHA1(431ae1f9982a70421650e1bfe4bf87152e2fe85c) )
829   ROM_LOAD16_BYTE( "u33.bin", 0x000000, 0x020000, CRC(91f33abd) SHA1(694868bc1ef612ba47cb38957d965f271bf16105) )
830   ROM_LOAD16_BYTE( "u32.bin", 0x000001, 0x020000, CRC(ad5a3fec) SHA1(a2db3f2926bdbb5bc44f307b919a0431c9deb76d) )
819831
820832   ROM_REGION( 0x200000*8, REGION_GFX1, ROMREGION_ERASEFF | ROMREGION_DISPOSE )   /* 16x16x8 */
821   ROMX_LOAD( "yunsun16.u67", 0x000000, 0x080000, CRC(e30fb2c4) SHA1(0d33a1593d7ebcd5da6971a04c3300c0b4eef219) , ROM_GROUPWORD | ROM_SKIP(6))
822   ROMX_LOAD( "yunsun16.u68", 0x000002, 0x080000, CRC(7d702538) SHA1(ae4c8ca6f172e204589f2f70ca114f7c38e7cabd) , ROM_GROUPWORD | ROM_SKIP(6))
823   ROMX_LOAD( "yunsun16.u69", 0x000004, 0x080000, CRC(97447fec) SHA1(e52184f96b2337ccbef130ada21a959c8bc1d73b) , ROM_GROUPWORD | ROM_SKIP(6))
824   ROMX_LOAD( "yunsun16.u70", 0x000006, 0x080000, CRC(1b1f7895) SHA1(939c386dbef82e4833b7038e7c603d2ec67fa23e) , ROM_GROUPWORD | ROM_SKIP(6))
833   ROMX_LOAD( "u67.bin", 0x000000, 0x080000, CRC(ea0b9e27) SHA1(e68f728158d0c42523002fe4270784891f5492ce) , ROM_GROUPWORD | ROM_SKIP(6))
834   ROMX_LOAD( "u68.bin", 0x000002, 0x080000, CRC(6b7ff4dd) SHA1(b06036f08e8f65860077a71d91676bf5c2f804fc) , ROM_GROUPWORD | ROM_SKIP(6))
835   ROMX_LOAD( "u69.bin", 0x000004, 0x080000, CRC(06749294) SHA1(375fe1c05355f789f846aa28b2012d08bfa2b2b5) , ROM_GROUPWORD | ROM_SKIP(6))
836   ROMX_LOAD( "u70.bin", 0x000006, 0x080000, CRC(0adacdf8) SHA1(d33680e7139e78929284b81e880bd5baa45c6675) , ROM_GROUPWORD | ROM_SKIP(6))
837   ROMX_LOAD( "u71.bin", 0x200000, 0x080000, CRC(69178fc4) SHA1(1ec06d360e098e15cfb673e5de7124a7c10757f8) , ROM_GROUPWORD | ROM_SKIP(6))
838   ROMX_LOAD( "u72.bin", 0x200002, 0x080000, CRC(7c3384b9) SHA1(b9e1ba7ec009e15f1061c3994ed4cf48a8e700c6) , ROM_GROUPWORD | ROM_SKIP(6))
839   ROMX_LOAD( "u73.bin", 0x200004, 0x080000, CRC(73fbc13e) SHA1(a19a05764ca010be025aae12fa82f97f5dc7d4b9) , ROM_GROUPWORD | ROM_SKIP(6))
840   ROMX_LOAD( "u74.bin", 0x200006, 0x080000, CRC(f1afda11) SHA1(c62e318dde2ed7ac9b649764ccec8e991d2869c2) , ROM_GROUPWORD | ROM_SKIP(6))
825841
826842   ROM_REGION( 0x100000, REGION_GFX2, ROMREGION_DISPOSE )   /* 16x16x4 */
827   ROM_LOAD( "yunsun16.u20", 0x000000, 0x040000, CRC(124699d0) SHA1(e55c8fb35f193abf98b1df07b94b99bf33bb5207) )
828   ROM_LOAD( "yunsun16.u21", 0x040000, 0x040000, CRC(4eea29a2) SHA1(c8173eeef0228a7635a96251ae3776726ffaf0f4) )
829   ROM_LOAD( "yunsun16.u22", 0x080000, 0x040000, CRC(d6db0388) SHA1(f5d8f7740b602c402a8dd6c4ebd357cf15a0dfac) )
830   ROM_LOAD( "yunsun16.u23", 0x0c0000, 0x040000, CRC(1fa33b2e) SHA1(4aa0dee8d34aac19cf6b7ba3f79ca022ad8d7760) )
843   ROM_LOAD( "u20.bin", 0x000000, 0x040000, CRC(ccb0ad6b) SHA1(ca66b7c7cb1418a86f209d071935aa45bb0a6e7d) )
844   ROM_LOAD( "u21.bin", 0x040000, 0x040000, CRC(125badf0) SHA1(ae63469e1fb1328c554774ca8c47878df2b02b96) )
845   ROM_LOAD( "u22.bin", 0x080000, 0x040000, CRC(436499c7) SHA1(ec1390b6d5656c99d91cf6425d319f4796bcb28a) )
846   ROM_LOAD( "u23.bin", 0x0c0000, 0x040000, CRC(358280fe) SHA1(eac3cb65fe75bc2da14896734f4a339480b54a2c) )
831847
832848   ROM_REGION( 0x080000 * 2, REGION_SOUND1, 0 )   /* Samples */
833   ROM_LOAD( "yunsun16.131", 0x000000, 0x080000, CRC(d0a1bb8c) SHA1(10f33521bd6031ed73ee5c7be1382165925aa8f8) )
834   ROM_RELOAD(               0x080000, 0x080000             )
849   ROM_LOAD( "u131.bin", 0x000000, 0x080000, CRC(bcf7aa12) SHA1(f7bf5258396ed0eb7e85eccf250c6d0a333a4d61) )
850   ROM_RELOAD(           0x080000, 0x080000 )
835851
836852ROM_END
837853
838
839854/***************************************************************************
840855
841                              Bomb Kick
856                       Shocking  -  Yun Sung, 1997
842857
843YunSung YS-0211
844
845  CPU: 68HC000 P16
846Video: Actel A1020B PL84C
847  OSC: 16.000000 MHz
848Sound: Unmarked OKI M6295
849
850858***************************************************************************/
851859
852ROM_START( bombkick )
860ROM_START( shocking )
853861
854862   ROM_REGION( 0x080000, REGION_CPU1, 0 )      /* 68000 Code */
855   ROM_LOAD16_BYTE( "bk_u33",       0x000000, 0x040000, CRC(d6eb50bf) SHA1(a24c31f212f86f066c35d39da137ef0933323e43) )
856   ROM_LOAD16_BYTE( "bk_u32",       0x000001, 0x040000, CRC(d55388a2) SHA1(928f1a8933b986cf099e184002660e30ee1aeb0a) )
863   ROM_LOAD16_BYTE( "yunsun16.u33", 0x000000, 0x040000, CRC(8a155521) SHA1(000c9095558e6cae30ce43a885c3fbcf55713f40) )
864   ROM_LOAD16_BYTE( "yunsun16.u32", 0x000001, 0x040000, CRC(c4998c10) SHA1(431ae1f9982a70421650e1bfe4bf87152e2fe85c) )
857865
858866   ROM_REGION( 0x200000*8, REGION_GFX1, ROMREGION_ERASEFF | ROMREGION_DISPOSE )   /* 16x16x8 */
859   ROMX_LOAD( "bk_u67",       0x000000, 0x080000, CRC(1962f536) SHA1(36d3c73a322330058e963efcb9b81324724382cc) , ROM_GROUPWORD | ROM_SKIP(6))
860   ROMX_LOAD( "bk_u68",       0x000002, 0x080000, CRC(d80c75a4) SHA1(330c20d126b9f1f61f17750028c92843be55ec78) , ROM_GROUPWORD | ROM_SKIP(6))
861   ROMX_LOAD( "bk_u69",       0x000004, 0x080000, CRC(615e1e6f) SHA1(73875313010514ff5ca9e0bc96d6f93baaee391e) , ROM_GROUPWORD | ROM_SKIP(6))
862   ROMX_LOAD( "bk_u70",       0x000006, 0x080000, CRC(59817ef1) SHA1(d23df30b34223575d6a9c814f2ec3db990b18679) , ROM_GROUPWORD | ROM_SKIP(6))
867   ROMX_LOAD( "yunsun16.u67", 0x000000, 0x080000, CRC(e30fb2c4) SHA1(0d33a1593d7ebcd5da6971a04c3300c0b4eef219) , ROM_GROUPWORD | ROM_SKIP(6))
868   ROMX_LOAD( "yunsun16.u68", 0x000002, 0x080000, CRC(7d702538) SHA1(ae4c8ca6f172e204589f2f70ca114f7c38e7cabd) , ROM_GROUPWORD | ROM_SKIP(6))
869   ROMX_LOAD( "yunsun16.u69", 0x000004, 0x080000, CRC(97447fec) SHA1(e52184f96b2337ccbef130ada21a959c8bc1d73b) , ROM_GROUPWORD | ROM_SKIP(6))
870   ROMX_LOAD( "yunsun16.u70", 0x000006, 0x080000, CRC(1b1f7895) SHA1(939c386dbef82e4833b7038e7c603d2ec67fa23e) , ROM_GROUPWORD | ROM_SKIP(6))
863871
864872   ROM_REGION( 0x100000, REGION_GFX2, ROMREGION_DISPOSE )   /* 16x16x4 */
865   ROM_LOAD( "bk_u20",       0x000000, 0x040000, CRC(c2b83e3f) SHA1(8bcd862dbf56cf579058d045f89f900ebfea2f1d) )
866   ROM_LOAD( "bk_u21",       0x040000, 0x040000, CRC(d6890192) SHA1(3c26a08580ceecf2f61f008861a459e175c99ed9) )
867   ROM_LOAD( "bk_u22",       0x080000, 0x040000, CRC(9538c46c) SHA1(d7d0e167d5abc2ee81eae6fde152b2f5cc716c0e) )
868   ROM_LOAD( "bk_u23",       0x0c0000, 0x040000, CRC(e3831f3d) SHA1(096658ee5a7b83d774b671c0a38113533c8751d1) )
873   ROM_LOAD( "yunsun16.u20", 0x000000, 0x040000, CRC(124699d0) SHA1(e55c8fb35f193abf98b1df07b94b99bf33bb5207) )
874   ROM_LOAD( "yunsun16.u21", 0x040000, 0x040000, CRC(4eea29a2) SHA1(c8173eeef0228a7635a96251ae3776726ffaf0f4) )
875   ROM_LOAD( "yunsun16.u22", 0x080000, 0x040000, CRC(d6db0388) SHA1(f5d8f7740b602c402a8dd6c4ebd357cf15a0dfac) )
876   ROM_LOAD( "yunsun16.u23", 0x0c0000, 0x040000, CRC(1fa33b2e) SHA1(4aa0dee8d34aac19cf6b7ba3f79ca022ad8d7760) )
869877
870878   ROM_REGION( 0x080000 * 2, REGION_SOUND1, 0 )   /* Samples */
871   ROM_LOAD( "bk_u131",      0x000000, 0x080000, CRC(22cc5732) SHA1(38aefa4e543ea54e004eee428ee087121eb20905) )
872   ROM_RELOAD(               0x080000, 0x080000             )
879   ROM_LOAD( "yunsun16.131", 0x000000, 0x080000, CRC(d0a1bb8c) SHA1(10f33521bd6031ed73ee5c7be1382165925aa8f8) )
880   ROM_RELOAD(               0x080000, 0x080000 )
873881
874882ROM_END
875883
884
876885/***************************************************************************
877886
878                              Paparazzi
887                       Bomb Kick  -  Yun Sung, 1998
879888
880YunSung YS-0211
881
882  CPU: 68HC000 P16
883Video: Actel A1020B PL84C
884  OSC: 16.000000 MHz
885Sound: AD-65 (OKI M6295)
886
887889***************************************************************************/
888890
889ROM_START( paprazzi )
891ROM_START( bombkick )
890892
891893   ROM_REGION( 0x080000, REGION_CPU1, 0 )      /* 68000 Code */
892   ROM_LOAD16_BYTE( "u33.bin",       0x000000, 0x020000, CRC(91f33abd) SHA1(694868bc1ef612ba47cb38957d965f271bf16105) )
893   ROM_LOAD16_BYTE( "u32.bin",       0x000001, 0x020000, CRC(ad5a3fec) SHA1(a2db3f2926bdbb5bc44f307b919a0431c9deb76d) )
894   ROM_LOAD16_BYTE( "bk_u33", 0x000000, 0x040000, CRC(d6eb50bf) SHA1(a24c31f212f86f066c35d39da137ef0933323e43) )
895   ROM_LOAD16_BYTE( "bk_u32", 0x000001, 0x040000, CRC(d55388a2) SHA1(928f1a8933b986cf099e184002660e30ee1aeb0a) )
894896
895897   ROM_REGION( 0x200000*8, REGION_GFX1, ROMREGION_ERASEFF | ROMREGION_DISPOSE )   /* 16x16x8 */
896   ROMX_LOAD( "u67.bin",       0x000000, 0x080000, CRC(ea0b9e27) SHA1(e68f728158d0c42523002fe4270784891f5492ce) , ROM_GROUPWORD | ROM_SKIP(6))
897   ROMX_LOAD( "u68.bin",       0x000002, 0x080000, CRC(6b7ff4dd) SHA1(b06036f08e8f65860077a71d91676bf5c2f804fc) , ROM_GROUPWORD | ROM_SKIP(6))
898   ROMX_LOAD( "u69.bin",       0x000004, 0x080000, CRC(06749294) SHA1(375fe1c05355f789f846aa28b2012d08bfa2b2b5) , ROM_GROUPWORD | ROM_SKIP(6))
899   ROMX_LOAD( "u70.bin",       0x000006, 0x080000, CRC(0adacdf8) SHA1(d33680e7139e78929284b81e880bd5baa45c6675) , ROM_GROUPWORD | ROM_SKIP(6))
900   ROMX_LOAD( "u71.bin",       0x200000, 0x080000, CRC(69178fc4) SHA1(1ec06d360e098e15cfb673e5de7124a7c10757f8) , ROM_GROUPWORD | ROM_SKIP(6))
901   ROMX_LOAD( "u72.bin",       0x200002, 0x080000, CRC(7c3384b9) SHA1(b9e1ba7ec009e15f1061c3994ed4cf48a8e700c6) , ROM_GROUPWORD | ROM_SKIP(6))
902   ROMX_LOAD( "u73.bin",       0x200004, 0x080000, CRC(73fbc13e) SHA1(a19a05764ca010be025aae12fa82f97f5dc7d4b9) , ROM_GROUPWORD | ROM_SKIP(6))
903   ROMX_LOAD( "u74.bin",       0x200006, 0x080000, CRC(f1afda11) SHA1(c62e318dde2ed7ac9b649764ccec8e991d2869c2) , ROM_GROUPWORD | ROM_SKIP(6))
898   ROMX_LOAD( "bk_u67", 0x000000, 0x080000, CRC(1962f536) SHA1(36d3c73a322330058e963efcb9b81324724382cc) , ROM_GROUPWORD | ROM_SKIP(6))
899   ROMX_LOAD( "bk_u68", 0x000002, 0x080000, CRC(d80c75a4) SHA1(330c20d126b9f1f61f17750028c92843be55ec78) , ROM_GROUPWORD | ROM_SKIP(6))
900   ROMX_LOAD( "bk_u69", 0x000004, 0x080000, CRC(615e1e6f) SHA1(73875313010514ff5ca9e0bc96d6f93baaee391e) , ROM_GROUPWORD | ROM_SKIP(6))
901   ROMX_LOAD( "bk_u70", 0x000006, 0x080000, CRC(59817ef1) SHA1(d23df30b34223575d6a9c814f2ec3db990b18679) , ROM_GROUPWORD | ROM_SKIP(6))
904902
905903   ROM_REGION( 0x100000, REGION_GFX2, ROMREGION_DISPOSE )   /* 16x16x4 */
906   ROM_LOAD( "u20.bin",       0x000000, 0x040000, CRC(ccb0ad6b) SHA1(ca66b7c7cb1418a86f209d071935aa45bb0a6e7d) )
907   ROM_LOAD( "u21.bin",       0x040000, 0x040000, CRC(125badf0) SHA1(ae63469e1fb1328c554774ca8c47878df2b02b96) )
908   ROM_LOAD( "u22.bin",       0x080000, 0x040000, CRC(436499c7) SHA1(ec1390b6d5656c99d91cf6425d319f4796bcb28a) )
909   ROM_LOAD( "u23.bin",       0x0c0000, 0x040000, CRC(358280fe) SHA1(eac3cb65fe75bc2da14896734f4a339480b54a2c) )
904   ROM_LOAD( "bk_u20", 0x000000, 0x040000, CRC(c2b83e3f) SHA1(8bcd862dbf56cf579058d045f89f900ebfea2f1d) )
905   ROM_LOAD( "bk_u21", 0x040000, 0x040000, CRC(d6890192) SHA1(3c26a08580ceecf2f61f008861a459e175c99ed9) )
906   ROM_LOAD( "bk_u22", 0x080000, 0x040000, CRC(9538c46c) SHA1(d7d0e167d5abc2ee81eae6fde152b2f5cc716c0e) )
907   ROM_LOAD( "bk_u23", 0x0c0000, 0x040000, CRC(e3831f3d) SHA1(096658ee5a7b83d774b671c0a38113533c8751d1) )
910908
911909   ROM_REGION( 0x080000 * 2, REGION_SOUND1, 0 )   /* Samples */
912   ROM_LOAD( "u131.bin",      0x000000, 0x080000, CRC(bcf7aa12) SHA1(f7bf5258396ed0eb7e85eccf250c6d0a333a4d61) )
913   ROM_RELOAD(               0x080000, 0x080000             )
910   ROM_LOAD( "bk_u131", 0x000000, 0x080000, CRC(22cc5732) SHA1(38aefa4e543ea54e004eee428ee087121eb20905) )
911   ROM_RELOAD(          0x080000, 0x080000 )
914912
915913ROM_END
916914
r2627r2628
923921
924922***************************************************************************/
925923
926GAME( 19??, magicbub, 0,        magicbub, magicbub, magicbub, ROT0, "Yun Sung", "Magic Bubble", GAME_NO_COCKTAIL )
927GAME( 19??, magicbua, magicbub, magicbub, magicbua, magicbub, ROT0, "Yun Sung", "Magic Bubble (Adult version)", GAME_NO_COCKTAIL )
924GAME( 199?, magicbub, 0,        magicbub, magicbub, magicbub, ROT0,   "Yun Sung", "Magic Bubble", GAME_NO_COCKTAIL )
925GAME( 199?, magicbua, magicbub, magicbub, magicbua, magicbub, ROT0,   "Yun Sung", "Magic Bubble (Adult version)", GAME_NO_COCKTAIL )
928926GAME( 1996, paprazzi, 0,        shocking, paprazzi, 0,        ROT270, "Yun Sung", "Paparazzi",    GAME_NO_COCKTAIL )
929GAME( 1997, shocking, 0,        shocking, shocking, 0,        ROT0, "Yun Sung", "Shocking",     GAME_NO_COCKTAIL )
930GAME( 1998, bombkick, 0,        shocking, bombkick, 0,        ROT0, "Yun Sung", "Bomb Kick",    GAME_NO_COCKTAIL )
927GAME( 1997, shocking, 0,        shocking, shocking, 0,        ROT0,   "Yun Sung", "Shocking",     GAME_NO_COCKTAIL )
928GAME( 1998, bombkick, 0,        shocking, bombkick, 0,        ROT0,   "Yun Sung", "Bomb Kick",    GAME_NO_COCKTAIL )
931929
src/mame/drivers/arcadecl.c
r2627r2628
176176   AM_RANGE(0x3e0000, 0x3e07ff) AM_RAM_WRITE(atarimo_0_spriteram_w) AM_BASE(&atarimo_0_spriteram)
177177   AM_RANGE(0x3e0800, 0x3effbf) AM_RAM
178178   AM_RANGE(0x3effc0, 0x3effff) AM_RAM_WRITE(atarimo_0_slipram_w) AM_BASE(&atarimo_0_slipram)
179   AM_RANGE(0x640000, 0x640001) AM_READ(input_port_0_word_r)
180   AM_RANGE(0x640002, 0x640003) AM_READ(input_port_1_word_r)
181   AM_RANGE(0x640010, 0x640011) AM_READ(input_port_2_word_r)
182   AM_RANGE(0x640012, 0x640013) AM_READ(input_port_3_word_r)
183   AM_RANGE(0x640020, 0x640021) AM_READ(input_port_4_word_r)
184   AM_RANGE(0x640022, 0x640023) AM_READ(input_port_5_word_r)
185   AM_RANGE(0x640024, 0x640025) AM_READ(input_port_6_word_r)
186   AM_RANGE(0x640026, 0x640027) AM_READ(input_port_7_word_r)
179   AM_RANGE(0x640000, 0x640001) AM_READ_PORT("PLAYER1")
180   AM_RANGE(0x640002, 0x640003) AM_READ_PORT("PLAYER2")
181   AM_RANGE(0x640010, 0x640011) AM_READ_PORT("STATUS")
182   AM_RANGE(0x640012, 0x640013) AM_READ_PORT("COIN")
183   AM_RANGE(0x640020, 0x640021) AM_READ_PORT("TRACKX2")
184   AM_RANGE(0x640022, 0x640023) AM_READ_PORT("TRACKY2")
185   AM_RANGE(0x640024, 0x640025) AM_READ_PORT("TRACKX1")
186   AM_RANGE(0x640026, 0x640027) AM_READ_PORT("TRACKY1")
187187   AM_RANGE(0x640040, 0x64004f) AM_WRITE(latch_w)
188188   AM_RANGE(0x640060, 0x64006f) AM_WRITE(atarigen_eeprom_enable_w)
189189   AM_RANGE(0x641000, 0x641fff) AM_READWRITE(atarigen_eeprom_r, atarigen_eeprom_w) AM_BASE(&atarigen_eeprom) AM_SIZE(&atarigen_eeprom_size)
r2627r2628
201201 *************************************/
202202
203203static INPUT_PORTS_START( arcadecl )
204   PORT_START_TAG("640000")
204   PORT_START_TAG("PLAYER1")
205205   PORT_BIT( 0x00ff, IP_ACTIVE_LOW, IPT_UNUSED )
206206   PORT_BIT( 0x0100, IP_ACTIVE_LOW, IPT_START1 )
207207   PORT_BIT( 0x0200, IP_ACTIVE_LOW, IPT_BUTTON3 ) PORT_PLAYER(1)
r2627r2628
209209   PORT_BIT( 0x0800, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_PLAYER(1)
210210   PORT_BIT( 0xf000, IP_ACTIVE_LOW, IPT_UNUSED )
211211
212   PORT_START_TAG("640002")
212   PORT_START_TAG("PLAYER2")
213213   PORT_BIT( 0x00ff, IP_ACTIVE_LOW, IPT_UNUSED )
214214   PORT_BIT( 0x0100, IP_ACTIVE_LOW, IPT_START2 )
215215   PORT_BIT( 0x0200, IP_ACTIVE_LOW, IPT_BUTTON3 ) PORT_PLAYER(2)
r2627r2628
217217   PORT_BIT( 0x0800, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_PLAYER(2)
218218   PORT_BIT( 0xf000, IP_ACTIVE_LOW, IPT_UNUSED )
219219
220   PORT_START_TAG("640010")
220   PORT_START_TAG("STATUS")
221221   PORT_BIT(  0x0001, IP_ACTIVE_LOW, IPT_UNUSED )
222222   PORT_BIT(  0x0002, IP_ACTIVE_LOW, IPT_UNUSED )
223223   PORT_BIT(  0x0004, IP_ACTIVE_LOW, IPT_UNUSED )
r2627r2628
228228   PORT_BIT(  0x0080, IP_ACTIVE_HIGH, IPT_VBLANK )
229229   PORT_BIT(  0xff00, IP_ACTIVE_LOW, IPT_UNUSED )
230230
231   PORT_START_TAG("640012")
231   PORT_START_TAG("COIN")
232232   PORT_BIT(  0x0001, IP_ACTIVE_LOW, IPT_COIN1 )
233233   PORT_BIT(  0x0002, IP_ACTIVE_LOW, IPT_COIN2 )
234234   PORT_BIT(  0x000c, IP_ACTIVE_LOW, IPT_UNUSED )
235   PORT_BIT(  0x0010, IP_ACTIVE_LOW, IPT_SERVICE )
236   PORT_BIT(  0x0020, IP_ACTIVE_LOW, IPT_SERVICE )
235   PORT_BIT(  0x0010, IP_ACTIVE_LOW, IPT_SERVICE1 )
236   PORT_BIT(  0x0020, IP_ACTIVE_LOW, IPT_SERVICE2 )
237237   PORT_BIT(  0xffc0, IP_ACTIVE_LOW, IPT_UNUSED )
238238
239   PORT_START_TAG("640020")
240    PORT_BIT( 0x00ff, 0, IPT_TRACKBALL_X ) PORT_SENSITIVITY(50) PORT_KEYDELTA(32) PORT_CODE_DEC(KEYCODE_UP) PORT_CODE_INC(KEYCODE_DOWN) PORT_REVERSE PORT_PLAYER(2)
239   PORT_START_TAG("TRACKX2")
240    PORT_BIT( 0x00ff, 0, IPT_TRACKBALL_X ) PORT_SENSITIVITY(50) PORT_KEYDELTA(32) PORT_REVERSE PORT_PLAYER(2)
241241   PORT_BIT( 0xff00, IP_ACTIVE_LOW, IPT_UNUSED )
242242
243   PORT_START_TAG("640022")
243   PORT_START_TAG("TRACKY2")
244244   PORT_BIT( 0x00ff, 0, IPT_TRACKBALL_Y ) PORT_SENSITIVITY(50) PORT_KEYDELTA(32) PORT_PLAYER(2)
245245   PORT_BIT( 0xff00, IP_ACTIVE_LOW, IPT_UNUSED )
246246
247   PORT_START_TAG("640024")
247   PORT_START_TAG("TRACKX1")
248248    PORT_BIT( 0x00ff, 0, IPT_TRACKBALL_X ) PORT_SENSITIVITY(50) PORT_KEYDELTA(32) PORT_REVERSE PORT_PLAYER(1)
249249   PORT_BIT( 0xff00, IP_ACTIVE_LOW, IPT_UNUSED )
250250
251   PORT_START_TAG("640026")
251   PORT_START_TAG("TRACKY1")
252252    PORT_BIT( 0x00ff, 0, IPT_TRACKBALL_Y ) PORT_SENSITIVITY(50) PORT_KEYDELTA(32) PORT_PLAYER(1)
253253   PORT_BIT( 0xff00, IP_ACTIVE_LOW, IPT_UNUSED )
254254INPUT_PORTS_END
255255
256256
257257static INPUT_PORTS_START( sparkz )
258   PORT_START_TAG("640000")
258   PORT_START_TAG("PLAYER1")
259259   PORT_BIT( 0x00ff, IP_ACTIVE_LOW, IPT_UNUSED )
260260   PORT_BIT( 0x0100, IP_ACTIVE_LOW, IPT_START1 )
261261   PORT_BIT( 0x0200, IP_ACTIVE_LOW, IPT_BUTTON3 ) PORT_PLAYER(1)
r2627r2628
266266   PORT_BIT( 0x4000, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN ) PORT_PLAYER(1)
267267   PORT_BIT( 0x8000, IP_ACTIVE_LOW, IPT_JOYSTICK_UP ) PORT_PLAYER(1)
268268
269   PORT_START_TAG("640002")
269   PORT_START_TAG("PLAYER2")
270270   PORT_BIT( 0x00ff, IP_ACTIVE_LOW, IPT_UNUSED )
271271   PORT_BIT( 0x0100, IP_ACTIVE_LOW, IPT_START2 )
272272   PORT_BIT( 0x0200, IP_ACTIVE_LOW, IPT_BUTTON3 ) PORT_PLAYER(2)
r2627r2628
277277   PORT_BIT( 0x4000, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN ) PORT_PLAYER(2)
278278   PORT_BIT( 0x8000, IP_ACTIVE_LOW, IPT_JOYSTICK_UP ) PORT_PLAYER(2)
279279
280   PORT_START_TAG("640010")
280   PORT_START_TAG("STATUS")
281281   PORT_BIT( 0x0001, IP_ACTIVE_LOW, IPT_UNUSED )
282282   PORT_BIT( 0x0002, IP_ACTIVE_LOW, IPT_UNUSED )
283283   PORT_BIT( 0x0004, IP_ACTIVE_LOW, IPT_UNUSED )
r2627r2628
288288   PORT_BIT( 0x0080, IP_ACTIVE_HIGH, IPT_VBLANK )
289289   PORT_BIT( 0xff00, IP_ACTIVE_LOW, IPT_UNUSED )
290290
291   PORT_START_TAG("640012")
291   PORT_START_TAG("COIN")
292292   PORT_BIT( 0x0001, IP_ACTIVE_LOW, IPT_COIN1 )
293293   PORT_BIT( 0x0002, IP_ACTIVE_LOW, IPT_COIN2 )
294294   PORT_BIT( 0x000c, IP_ACTIVE_LOW, IPT_UNUSED )
295   PORT_BIT( 0x0010, IP_ACTIVE_LOW, IPT_SERVICE )
296   PORT_BIT( 0x0020, IP_ACTIVE_LOW, IPT_SERVICE )
295   PORT_BIT( 0x0010, IP_ACTIVE_LOW, IPT_SERVICE1 )
296   PORT_BIT( 0x0020, IP_ACTIVE_LOW, IPT_SERVICE2 )         /* not in "test mode" */
297297   PORT_BIT( 0xffc0, IP_ACTIVE_LOW, IPT_UNUSED )
298298
299   PORT_START_TAG("640020")
299   PORT_START_TAG("TRACKX2")
300300   PORT_BIT( 0xffff, IP_ACTIVE_LOW, IPT_UNUSED )
301301
302   PORT_START_TAG("640022")
302   PORT_START_TAG("TRACKY2")
303303   PORT_BIT( 0xffff, IP_ACTIVE_LOW, IPT_UNUSED )
304304
305   PORT_START_TAG("640024")
305   PORT_START_TAG("TRACKX1")
306306   PORT_BIT( 0xffff, IP_ACTIVE_LOW, IPT_UNUSED )
307307
308   PORT_START_TAG("640026")
308   PORT_START_TAG("TRACKY1")
309309   PORT_BIT( 0xffff, IP_ACTIVE_LOW, IPT_UNUSED )
310310INPUT_PORTS_END
311311
src/mame/drivers/leland.c
r2627r2628
18811881{
18821882   extern unsigned DasmZ80(char *buffer, unsigned _pc);
18831883
1884   UINT8 *old_rom = opcode_base;
1885   UINT8 *old_ram = opcode_arg_base;
1884   UINT8 *old_rom = opbase->rom;
1885   UINT8 *old_ram = opbase->ram;
18861886   char buffer[256];
18871887   int count, offset, i;
18881888
18891889   fprintf(output, "\n\n\n%s:\n", tag);
1890   opcode_base = opcode_arg_base = &base[-pc];
1890   opbase->rom = opbase->ram = &base[-pc];
18911891   for (offset = 0; offset < length; offset += count)
18921892   {
18931893      count = DasmZ80(buffer, pc);
r2627r2628
19041904            fprintf(output, "   ");
19051905      fprintf(output, "%s\n", buffer);
19061906   }
1907   opcode_base = old_rom;
1908   opcode_arg_base = old_ram;
1907   opbase->rom = old_rom;
1908   opbase->ram = old_ram;
19091909}
19101910#endif
19111911
src/mame/drivers/taxidrvr.c
r2627r2628
138138};
139139
140140
141static ADDRESS_MAP_START( readmem1, ADDRESS_SPACE_PROGRAM, 8 )
142   AM_RANGE(0x0000, 0x7fff) AM_READ(SMH_ROM)
143   AM_RANGE(0x8000, 0x8fff) AM_READ(SMH_RAM)
144   AM_RANGE(0x9000, 0x9fff) AM_READ(SMH_RAM)
145   AM_RANGE(0xa000, 0xafff) AM_READ(SMH_RAM)
146   AM_RANGE(0xb000, 0xbfff) AM_READ(SMH_RAM)
147   AM_RANGE(0xc000, 0xc7ff) AM_READ(SMH_RAM)
148   AM_RANGE(0xd800, 0xdfff) AM_READ(SMH_RAM)
149   AM_RANGE(0xe000, 0xf3ff) AM_READ(SMH_RAM)
150   AM_RANGE(0xf400, 0xf403) AM_DEVREAD(PPI8255, "ppi8255_0", ppi8255_r)
151   AM_RANGE(0xf480, 0xf483) AM_DEVREAD(PPI8255, "ppi8255_2", ppi8255_r)
152   AM_RANGE(0xf500, 0xf503) AM_DEVREAD(PPI8255, "ppi8255_3", ppi8255_r)
153   AM_RANGE(0xf580, 0xf583) AM_DEVREAD(PPI8255, "ppi8255_4", ppi8255_r)
154   AM_RANGE(0xf800, 0xffff) AM_READ(SMH_RAM)
155ADDRESS_MAP_END
156
157static ADDRESS_MAP_START( writemem1, ADDRESS_SPACE_PROGRAM, 8 )
158   AM_RANGE(0x0000, 0x7fff) AM_WRITE(SMH_ROM)
159   AM_RANGE(0x8000, 0x8fff) AM_WRITE(SMH_RAM)   /* ??? */
160   AM_RANGE(0x9000, 0x9fff) AM_WRITE(SMH_RAM)   /* ??? */
161   AM_RANGE(0xa000, 0xafff) AM_WRITE(SMH_RAM)   /* ??? */
162   AM_RANGE(0xb000, 0xbfff) AM_WRITE(SMH_RAM)   /* ??? */
163   AM_RANGE(0xc000, 0xc7ff) AM_WRITE(SMH_RAM) AM_BASE(&taxidrvr_vram4)   /* radar bitmap */
141static ADDRESS_MAP_START( main_map, ADDRESS_SPACE_PROGRAM, 8 )
142   AM_RANGE(0x0000, 0x7fff) AM_ROM
143   AM_RANGE(0x8000, 0x8fff) AM_RAM   /* ??? */
144   AM_RANGE(0x9000, 0x9fff) AM_RAM   /* ??? */
145   AM_RANGE(0xa000, 0xafff) AM_RAM   /* ??? */
146   AM_RANGE(0xb000, 0xbfff) AM_RAM   /* ??? */
147   AM_RANGE(0xc000, 0xc7ff) AM_RAM AM_BASE(&taxidrvr_vram4)         /* radar bitmap */
164148   AM_RANGE(0xc800, 0xcfff) AM_WRITE(SMH_RAM) AM_BASE(&taxidrvr_vram5)   /* "sprite1" bitmap */
165149   AM_RANGE(0xd000, 0xd7ff) AM_WRITE(SMH_RAM) AM_BASE(&taxidrvr_vram6)   /* "sprite2" bitmap */
166   AM_RANGE(0xd800, 0xdfff) AM_WRITE(SMH_RAM) AM_BASE(&taxidrvr_vram7)   /* "sprite3" bitmap */
150   AM_RANGE(0xd800, 0xdfff) AM_RAM AM_BASE(&taxidrvr_vram7)         /* "sprite3" bitmap */
151   AM_RANGE(0xe000, 0xf3ff) AM_READ(SMH_RAM)
167152   AM_RANGE(0xe000, 0xe3ff) AM_WRITE(SMH_RAM) AM_BASE(&taxidrvr_vram1)   /* car tilemap */
168153   AM_RANGE(0xe400, 0xebff) AM_WRITE(SMH_RAM) AM_BASE(&taxidrvr_vram2)   /* bg1 tilemap */
169154   AM_RANGE(0xec00, 0xefff) AM_WRITE(SMH_RAM) AM_BASE(&taxidrvr_vram0)   /* fg tilemap */
170155   AM_RANGE(0xf000, 0xf3ff) AM_WRITE(SMH_RAM) AM_BASE(&taxidrvr_vram3)   /* bg2 tilemap */
171   AM_RANGE(0xf400, 0xf403) AM_DEVWRITE(PPI8255, "ppi8255_0", ppi8255_w)
172   AM_RANGE(0xf480, 0xf483) AM_DEVWRITE(PPI8255, "ppi8255_2", ppi8255_w)   /* "sprite1" placement */
173   AM_RANGE(0xf500, 0xf503) AM_DEVWRITE(PPI8255, "ppi8255_3", ppi8255_w)   /* "sprite2" placement */
174   AM_RANGE(0xf580, 0xf583) AM_DEVWRITE(PPI8255, "ppi8255_4", ppi8255_w)   /* "sprite3" placement */
175//  AM_RANGE(0xf780, 0xf781) AM_WRITE(SMH_RAM)     /* more scroll registers? */
156   AM_RANGE(0xf400, 0xf403) AM_DEVREADWRITE(PPI8255, "ppi8255_0", ppi8255_r, ppi8255_w)
157   AM_RANGE(0xf480, 0xf483) AM_DEVREADWRITE(PPI8255, "ppi8255_2", ppi8255_r, ppi8255_w)   /* "sprite1" placement */
158   AM_RANGE(0xf500, 0xf503) AM_DEVREADWRITE(PPI8255, "ppi8255_3", ppi8255_r, ppi8255_w)   /* "sprite2" placement */
159   AM_RANGE(0xf580, 0xf583) AM_DEVREADWRITE(PPI8255, "ppi8255_4", ppi8255_r, ppi8255_w)   /* "sprite3" placement */
160   //AM_RANGE(0xf780, 0xf781) AM_WRITE(SMH_RAM)     /* more scroll registers? */
176161   AM_RANGE(0xf782, 0xf787) AM_WRITE(SMH_RAM) AM_BASE(&taxidrvr_scroll)   /* bg scroll (three copies always identical) */
177   AM_RANGE(0xf800, 0xffff) AM_WRITE(SMH_RAM)
162   AM_RANGE(0xf800, 0xffff) AM_RAM
178163ADDRESS_MAP_END
179164
180static ADDRESS_MAP_START( readmem2, ADDRESS_SPACE_PROGRAM, 8 )
181   AM_RANGE(0x0000, 0x3fff) AM_READ(SMH_ROM)
182   AM_RANGE(0x6000, 0x67ff) AM_READ(SMH_RAM)
183   AM_RANGE(0x8000, 0x87ff) AM_READ(SMH_RAM)
184   AM_RANGE(0xa000, 0xa003) AM_DEVREAD(PPI8255, "ppi8255_1", ppi8255_r)
165static ADDRESS_MAP_START( cpu2_map, ADDRESS_SPACE_PROGRAM, 8 )
166   AM_RANGE(0x0000, 0x3fff) AM_ROM
167   AM_RANGE(0x6000, 0x67ff) AM_RAM
168   AM_RANGE(0x8000, 0x87ff) AM_RAM
169   AM_RANGE(0xa000, 0xa003) AM_DEVREADWRITE(PPI8255, "ppi8255_1", ppi8255_r, ppi8255_w)
185170   AM_RANGE(0xe000, 0xe000) AM_READ(input_port_0_r)
186171   AM_RANGE(0xe001, 0xe001) AM_READ(input_port_1_r)
187172   AM_RANGE(0xe002, 0xe002) AM_READ(input_port_2_r)
r2627r2628
189174   AM_RANGE(0xe004, 0xe004) AM_READ(input_port_4_r)
190175ADDRESS_MAP_END
191176
192static ADDRESS_MAP_START( writemem2, ADDRESS_SPACE_PROGRAM, 8 )
193   AM_RANGE(0x0000, 0x3fff) AM_WRITE(SMH_ROM)
194   AM_RANGE(0x6000, 0x67ff) AM_WRITE(SMH_RAM)
195   AM_RANGE(0x8000, 0x87ff) AM_WRITE(SMH_RAM)
196   AM_RANGE(0xa000, 0xa003) AM_DEVWRITE(PPI8255, "ppi8255_1", ppi8255_w)
197ADDRESS_MAP_END
198
199static ADDRESS_MAP_START( readmem3, ADDRESS_SPACE_PROGRAM, 8 )
200   AM_RANGE(0x0000, 0x1fff) AM_READ(SMH_ROM)
177static ADDRESS_MAP_START( cpu3_map, ADDRESS_SPACE_PROGRAM, 8 )
178   AM_RANGE(0x0000, 0x1fff) AM_ROM
201179   AM_RANGE(0x2000, 0x2000) AM_READ(SMH_NOP)   /* irq ack? */
202   AM_RANGE(0xfc00, 0xffff) AM_READ(SMH_RAM)
180   AM_RANGE(0xfc00, 0xffff) AM_RAM
203181ADDRESS_MAP_END
204182
205static ADDRESS_MAP_START( writemem3, ADDRESS_SPACE_PROGRAM, 8 )
206   AM_RANGE(0x0000, 0x1fff) AM_WRITE(SMH_ROM)
207   AM_RANGE(0xfc00, 0xffff) AM_WRITE(SMH_RAM)
208ADDRESS_MAP_END
209
210static ADDRESS_MAP_START( readport3, ADDRESS_SPACE_IO, 8 )
183static ADDRESS_MAP_START( cpu3_port_map, ADDRESS_SPACE_IO, 8 )
211184   ADDRESS_MAP_GLOBAL_MASK(0xff)
212   AM_RANGE(0x01, 0x01) AM_READ(AY8910_read_port_0_r)
213   AM_RANGE(0x03, 0x03) AM_READ(AY8910_read_port_1_r)
214ADDRESS_MAP_END
215
216static ADDRESS_MAP_START( writeport3, ADDRESS_SPACE_IO, 8 )
217   ADDRESS_MAP_GLOBAL_MASK(0xff)
218185   AM_RANGE(0x00, 0x00) AM_WRITE(AY8910_control_port_0_w)
219   AM_RANGE(0x01, 0x01) AM_WRITE(AY8910_write_port_0_w)
186   AM_RANGE(0x01, 0x01) AM_READWRITE(AY8910_read_port_0_r, AY8910_write_port_0_w)
220187   AM_RANGE(0x02, 0x02) AM_WRITE(AY8910_control_port_1_w)
221   AM_RANGE(0x03, 0x03) AM_WRITE(AY8910_write_port_1_w)
188   AM_RANGE(0x03, 0x03) AM_READWRITE(AY8910_read_port_1_r, AY8910_write_port_1_w)
222189ADDRESS_MAP_END
223190
224191
225
226192static INPUT_PORTS_START( taxidrvr )
227193   PORT_START_TAG("DSW0")
228194   PORT_DIPNAME( 0x0f, 0x00, DEF_STR( Coin_A ) )
r2627r2628
395361
396362   /* basic machine hardware */
397363   MDRV_CPU_ADD(Z80,4000000)   /* 4 MHz ??? */
398   MDRV_CPU_PROGRAM_MAP(readmem1,writemem1)
364   MDRV_CPU_PROGRAM_MAP(main_map,0)
399365   MDRV_CPU_VBLANK_INT("main", irq0_line_hold)
400366
401367   MDRV_CPU_ADD(Z80,4000000)   /* 4 MHz ??? */
402   MDRV_CPU_PROGRAM_MAP(readmem2,writemem2)
368   MDRV_CPU_PROGRAM_MAP(cpu2_map,0)
403369   MDRV_CPU_VBLANK_INT("main", irq0_line_hold)   /* ??? */
404370
405371   MDRV_CPU_ADD(Z80,4000000)   /* 4 MHz ??? */
406   MDRV_CPU_PROGRAM_MAP(readmem3,writemem3)
407   MDRV_CPU_IO_MAP(readport3,writeport3)
372   MDRV_CPU_PROGRAM_MAP(cpu3_map,0)
373   MDRV_CPU_IO_MAP(cpu3_port_map,0)
408374   MDRV_CPU_VBLANK_INT("main", irq0_line_hold)   /* ??? */
409375
410376   MDRV_INTERLEAVE(100)   /* 100 CPU slices per frame - an high value to ensure proper */
src/mame/drivers/cps2.c
r2627r2628
73977397GAME( 2000, mpangj,   mpang,    cps2, cps2_2p1b, cps2,     ROT0,   "Mitchell, distributed by Capcom", "Mighty! Pang (Japan 001011)", 0 )
73987398GAME( 2001, pzloop2,  0,        cps2, pzloop2,   pzloop2,  ROT0,   "Mitchell, distributed by Capcom", "Puzz Loop 2 (Euro 010302)", 0 )
73997399GAME( 2001, pzloop2j, pzloop2,  cps2, pzloop2,   pzloop2,  ROT0,   "Mitchell, distributed by Capcom", "Puzz Loop 2 (Japan 010205)", 0 )
7400GAME( 2001, choko,    0,        cps2, choko,     cps2,     ROT0,   "Mitchell, distributed by Capcom", "Choko (Japan 010820)", 0 )
7400GAME( 2001, choko,    0,        cps2, choko,     cps2,     ROT0,   "Mitchell, distributed by Capcom", "Janpai Puzzle Choukou (Japan 010820)", 0 )
74017401
74027402/* Games released on CPS-2 hardware by Eighting/Raizing */
74037403
src/mame/drivers/toaplan1.c
r2627r2628
2222    zerowng2    TP-015      Zero Wing (2 player simultaneous version, Williams Electronics Games, Inc)
2323    demonwld    TP-016      Demon's World/Horror Story [1990]
2424    demonwl1    TP-016      Demon's World/Horror Story [1989] (Taito license)
25    demonwl2    TP-016      Demon's World/Horror Story [1989] (early edition)
26    demonwl3    TP-016      Demon's World/Horror Story [1989] (first edition)
2527    fireshrk    TP-017      Fire Shark (World)           [1990]
2628    samesame    TP-017      Same! Same! Same! (Japan)    [1989] (1 Player version)
2729    samesam2    TP-017      Same! Same! Same! (Japan)    [1989] (2 Player version)
r2627r2628
694696   PORT_DIPNAME( 0x01, 0x00, DEF_STR( Unused ) )
695697   PORT_DIPSETTING(    0x00, DEF_STR( Off ) )
696698   PORT_DIPSETTING(    0x01, DEF_STR( On ) )
697   PORT_DIPNAME( 0x02, 0x00, DEF_STR( Unused ) )
698   PORT_DIPSETTING(    0x00, DEF_STR( Off ) )
699   PORT_DIPSETTING(    0x02, DEF_STR( On ) )
699   PORT_DIPNAME( 0x02, 0x00, DEF_STR( Region ) )
700   PORT_DIPSETTING(    0x00, DEF_STR( USA ) )
701   PORT_DIPSETTING(    0x02, DEF_STR( Europe ) )
700702INPUT_PORTS_END
701703
702704static INPUT_PORTS_START( demonwld )
r2627r2628
17231725
17241726ROM_START( demonwld )
17251727   ROM_REGION( 0x040000, REGION_CPU1, 0 )   /* Main 68K code */
1726   ROM_LOAD16_BYTE( "rom10.v2",  0x000000, 0x20000, CRC(ca8194f3) SHA1(176da6739b35ba38b40150fc62380108bcae5a24) )
1727   ROM_LOAD16_BYTE( "rom09.v2",  0x000001, 0x20000, CRC(7baea7ba) SHA1(ae2b40f9efb4440ff7edbcc4f80641655f7c4671) )
1728   ROM_LOAD16_BYTE( "o16-10.v2", 0x000000, 0x20000, CRC(ca8194f3) SHA1(176da6739b35ba38b40150fc62380108bcae5a24) )
1729   ROM_LOAD16_BYTE( "o16-09.v2", 0x000001, 0x20000, CRC(7baea7ba) SHA1(ae2b40f9efb4440ff7edbcc4f80641655f7c4671) )
17281730
17291731   ROM_REGION( 0x10000, REGION_CPU2, 0 )   /* Sound Z80 code */
17301732   ROM_LOAD( "rom11.v2",  0x0000, 0x8000, CRC(dbe08c85) SHA1(536a242bfe916d15744b079261507af6f12b5b50) )
r2627r2628
17521754
17531755ROM_START( demonwl1 )
17541756   ROM_REGION( 0x040000, REGION_CPU1, 0 )   /* Main 68K code */
1755   ROM_LOAD16_BYTE( "rom10",  0x000000, 0x20000, CRC(036ee46c) SHA1(60868e5e08e0c9a538ae786de0de6b2531b30b11) )
1756   ROM_LOAD16_BYTE( "rom09",  0x000001, 0x20000, CRC(bed746e3) SHA1(056668edb7df99bbd240e387af17cf252d1448f3) )
1757   ROM_LOAD16_BYTE( "o16-10.rom", 0x000000, 0x20000, CRC(036ee46c) SHA1(60868e5e08e0c9a538ae786de0de6b2531b30b11) )
1758   ROM_LOAD16_BYTE( "o16-09.rom", 0x000001, 0x20000, CRC(bed746e3) SHA1(056668edb7df99bbd240e387af17cf252d1448f3) )
17571759
17581760   ROM_REGION( 0x10000, REGION_CPU2, 0 )   /* Sound Z80 code */
17591761   ROM_LOAD( "rom11",  0x0000, 0x8000, CRC(397eca1b) SHA1(84073ff6d1bc46ec6162d66ec5de305700938380) )
r2627r2628
17811783
17821784ROM_START( demonwl2 )
17831785   ROM_REGION( 0x040000, REGION_CPU1, 0 )   /* Main 68K code */
1784   ROM_LOAD16_BYTE( "10.bin",  0x000000, 0x20000, CRC(6f7468e0) SHA1(87ef7733fd0d00d0d375dbf30332cf0614480dc2) )
1785   ROM_LOAD16_BYTE( "9.bin",  0x000001, 0x20000, CRC(a572f5f7) SHA1(3d6a443cecd46734c7e1b761130909482c7a9914) )
1786   ROM_LOAD16_BYTE( "o16-10-2.bin", 0x000000, 0x20000, CRC(84ee5218) SHA1(dc2b017ee630330163be320008d8a0d761cb0cfb) )
1787   ROM_LOAD16_BYTE( "o16-09-2.bin", 0x000001, 0x20000, CRC(cf474cb2) SHA1(5c049082b8d7118e0d2e50c6ae07f9d3d0110498) )
17861788
17871789   ROM_REGION( 0x10000, REGION_CPU2, 0 )   /* Sound Z80 code */
17881790   ROM_LOAD( "rom11",  0x0000, 0x8000, CRC(397eca1b) SHA1(84073ff6d1bc46ec6162d66ec5de305700938380) )
r2627r2628
18081810   ROM_LOAD( "prom13.bpr",  0x20, 0x20, CRC(a1e17492) SHA1(9ddec4c97f2d541f69f3c32c47aaa21fd9699ae2) )   /* ??? */
18091811ROM_END
18101812
1813ROM_START( demonwl3 )
1814   ROM_REGION( 0x040000, REGION_CPU1, 0 )   /* Main 68K code */
1815   ROM_LOAD16_BYTE( "o16-10.bin", 0x000000, 0x20000, CRC(6f7468e0) SHA1(87ef7733fd0d00d0d375dbf30332cf0614480dc2) )
1816   ROM_LOAD16_BYTE( "o16-09.bin", 0x000001, 0x20000, CRC(a572f5f7) SHA1(3d6a443cecd46734c7e1b761130909482c7a9914) )
1817
1818   ROM_REGION( 0x10000, REGION_CPU2, 0 )   /* Sound Z80 code */
1819   ROM_LOAD( "rom11",  0x0000, 0x8000, CRC(397eca1b) SHA1(84073ff6d1bc46ec6162d66ec5de305700938380) )
1820
1821   ROM_REGION( 0x2000, REGION_CPU3, 0 )   /* Co-Processor TMS320C10 MCU code */
1822   ROM_LOAD16_BYTE( "dsp_21.bin",  0x0000, 0x0800, CRC(2d135376) SHA1(67a2cc774d272ee1cd6e6bc1c5fc33fc6968837e) )
1823   ROM_LOAD16_BYTE( "dsp_22.bin",  0x0001, 0x0800, CRC(79389a71) SHA1(14ec4c1c9b06702319e89a7a250d0038393437f4) )
1824
1825   ROM_REGION( 0x80000, REGION_GFX1, ROMREGION_DISPOSE )
1826   ROM_LOAD( "rom05",  0x00000, 0x20000, CRC(6506c982) SHA1(6d4c1ef91e5617724789ff196abb7abf23e4a7fb) )
1827   ROM_LOAD( "rom07",  0x20000, 0x20000, CRC(a3a0d993) SHA1(50311b9447eb04271b17b212ca31d083ab5b2414) )
1828   ROM_LOAD( "rom06",  0x40000, 0x20000, CRC(4fc5e5f3) SHA1(725d4b009d575ff8ffbe1c00df352ccf235465d7) )
1829   ROM_LOAD( "rom08",  0x60000, 0x20000, CRC(eb53ab09) SHA1(d98195cc1b65b76335b5b24adb31deae1b313f3a) )
1830
1831   ROM_REGION( 0x80000, REGION_GFX2, ROMREGION_DISPOSE )
1832   ROM_LOAD( "rom01",  0x00000, 0x20000, CRC(1b3724e9) SHA1(3dbb0450ab1e40e6df2b7c7356352419cd3f113d) )
1833   ROM_LOAD( "rom02",  0x20000, 0x20000, CRC(7b20a44d) SHA1(4dc1a2fa2058077b112c73492808ee9381060ec7) )
1834   ROM_LOAD( "rom03",  0x40000, 0x20000, CRC(2cacdcd0) SHA1(92216d1c6859e05d39363c30e0beb45bc0ae4e1c) )
1835   ROM_LOAD( "rom04",  0x60000, 0x20000, CRC(76fd3201) SHA1(7a12737bf90bd9760074132edeb22f3fd3e16b4f) )
1836
1837   ROM_REGION( 0x40, REGION_PROMS, 0 )      /* nibble bproms, lo/hi order to be determined */
1838   ROM_LOAD( "prom12.bpr",  0x00, 0x20, CRC(bc88cced) SHA1(5055362710c0f58823c05fb4c0e0eec638b91e3d) )   /* sprite attribute (flip/position) ?? */
1839   ROM_LOAD( "prom13.bpr",  0x20, 0x20, CRC(a1e17492) SHA1(9ddec4c97f2d541f69f3c32c47aaa21fd9699ae2) )   /* ??? */
1840ROM_END
1841
18111842ROM_START( samesame )
18121843   ROM_REGION( 0x080000, REGION_CPU1, 0 )   /* Main 68K code */
18131844   ROM_LOAD16_BYTE( "o17_09.bin",  0x000000, 0x08000, CRC(3f69e437) SHA1(f2a40fd42cb5ecb2e514b72e7550aa479a9f9ad6) )
r2627r2628
21152146GAME( 1989, zerowng2, zerowing, zerowing, zerowng2, toaplan1, ROT0,   "[Toaplan] Williams Electronics Games, Inc", "Zero Wing (2 player simultaneous ver.)", 0 )
21162147GAME( 1990, demonwld, 0,        demonwld, demonwld, demonwld, ROT0,   "Toaplan", "Demon's World / Horror Story", 0 )
21172148GAME( 1989, demonwl1, demonwld, demonwld, demonwl1, demonwld, ROT0,   "Toaplan (Taito license)", "Demon's World / Horror Story (Taito license)", 0 )
2118GAME( 1989, demonwl2, demonwld, demonwld, demonwl1, demonwld, ROT0,   "Toaplan", "Demon's World / Horror Story (first edition)", 0 )
2149GAME( 1989, demonwl2, demonwld, demonwld, demonwl1, demonwld, ROT0,   "Toaplan", "Demon's World / Horror Story (early edition)", 0 )
2150GAME( 1989, demonwl3, demonwld, demonwld, demonwl1, demonwld, ROT0,   "Toaplan", "Demon's World / Horror Story (first edition)", 0 )
21192151GAME( 1990, fireshrk, 0,        samesame, fireshrk, toaplan1, ROT270, "Toaplan", "Fire Shark", GAME_NO_SOUND )
21202152GAME( 1989, samesame, fireshrk, samesame, samesame, toaplan1, ROT270, "Toaplan", "Same! Same! Same!", GAME_NO_SOUND )
21212153GAME( 1989, samesam2, fireshrk, samesame, samesam2, toaplan1, ROT270, "Toaplan", "Same! Same! Same! (2P Ver.)", GAME_NO_SOUND )
src/mame/drivers/cps3.c
r2627r2628
13551355   /* BIOS ROM */
13561356   if (address < 0x80000)
13571357   {
1358      opcode_base = opcode_arg_base = memory_region(REGION_USER1);
1358      opbase->rom = opbase->ram = memory_region(REGION_USER1);
13591359      return ~0;
13601360   }
13611361   /* RAM */
13621362   else if (address >= 0x06000000 && address <= 0x06ffffff)
13631363   {
1364      opcode_base = (UINT8*)decrypted_gamerom-0x06000000;
1365      opcode_arg_base = (UINT8*)decrypted_gamerom-0x06000000;
1364      opbase->rom = (UINT8*)decrypted_gamerom-0x06000000;
1365      opbase->ram = (UINT8*)decrypted_gamerom-0x06000000;
13661366
1367      if (cps3_isSpecial) opcode_arg_base = (UINT8*) memory_region(REGION_USER4)-0x06000000;
1367      if (cps3_isSpecial) opbase->ram = (UINT8*) memory_region(REGION_USER4)-0x06000000;
13681368
13691369
13701370      return ~0;
13711371   }
13721372   else if (address >= 0xc0000000 && address <= 0xc00003ff)
13731373   {
1374//      opcode_base = (void*)cps3_0xc0000000_ram_decrypted;
1375      opcode_base = (UINT8*)cps3_0xc0000000_ram_decrypted-0xc0000000;
1376      opcode_arg_base = (UINT8*)cps3_0xc0000000_ram-0xc0000000;
1374      //opbase->rom = (void*)cps3_0xc0000000_ram_decrypted;
1375      opbase->rom = (UINT8*)cps3_0xc0000000_ram_decrypted-0xc0000000;
1376      opbase->ram = (UINT8*)cps3_0xc0000000_ram-0xc0000000;
13771377      return ~0;
13781378   }
13791379
13801380   /* anything else falls through to NOPs */
1381   opcode_base = (UINT8*)cps3_nops-address;
1382   opcode_arg_base = (UINT8*)cps3_nops-address;
1381   opbase->rom = (UINT8*)cps3_nops-address;
1382   opbase->ram = (UINT8*)cps3_nops-address;
13831383   return ~0;
13841384}
13851385
r2627r2628
17041704      romdata[offset] = newdata;
17051705      romdata2[offset] = newdata^cps3_mask(0x6000000+real_offset, cps3_key1, cps3_key2);
17061706   }
1707
1708
1709
1710/*
1711        opcode_base = (UINT8*)decrypted_gamerom-0x06000000;
1712        opcode_arg_base = (UINT8*)decrypted_gamerom-0x06000000;
1713
1714        if (cps3_isSpecial) opcode_arg_base = (UINT8*) memory_region(REGION_USER4)-0x06000000;
1715*/
1716
17171707}
1708
17181709static WRITE32_HANDLER( cps3_flash1_w )
17191710{
17201711   cps3_flashmain_w(0, offset,data,mem_mask);
src/mame/drivers/toaplan2.c
r2627r2628
42494249ROM_END
42504250
42514251
4252ROM_START( ghoxa )
4253   ROM_REGION( 0x040000, REGION_CPU1, 0 )         /* Main 68K code */
4254   ROM_LOAD16_BYTE( "tp021-01a.u10", 0x000000, 0x020000, CRC(c11b13c8) SHA1(da7defc1d3b6ddded910ba56c31fbbdb5ed57b09) )
4255   ROM_LOAD16_BYTE( "tp021-02a.u11", 0x000001, 0x020000, CRC(8d426767) SHA1(1ed4a8bcbf4352257e7d58cb5c2c91eb48c2f047) )
4256
4257#if USE_HD64x180
4258   ROM_REGION( 0x10000, REGION_CPU2, 0 )         /* Sound HD647180 code */
4259   /* sound CPU is a HD647180 (Z180) with internal ROM - not yet supported */
4260   ROM_LOAD( "hd647180.021", 0x00000, 0x08000, NO_DUMP )
4261#endif
4262
4263   ROM_REGION( 0x100000, REGION_GFX1, ROMREGION_DISPOSE )
4264   ROM_LOAD( "tp021-03.u36", 0x000000, 0x080000, CRC(a15d8e9d) SHA1(640a33997bdce8e84bea6a944139716379839037) )
4265   ROM_LOAD( "tp021-04.u37", 0x080000, 0x080000, CRC(26ed1c9a) SHA1(37da8af86ea24327444c2d4ad3dfbd936208d43d) )
4266ROM_END
4267
4268
42524269ROM_START( dogyuun )
42534270   ROM_REGION( 0x080000, REGION_CPU1, 0 )         /* Main 68K code */
42544271   ROM_LOAD16_WORD_SWAP( "tp022_01.r16", 0x000000, 0x080000, CRC(79eb2429) SHA1(088c5ed0ed77557ab71f52cafe35028e3648ae1e) )
r2627r2628
49154932
49164933/*  ( YEAR  NAME      PARENT    MACHINE   INPUT     INIT      MONITOR COMPANY    FULLNAME     FLAGS ) */
49174934GAME( 1991, tekipaki, 0,        tekipaki, tekipaki, T2_Z180,  ROT0,   "Toaplan", "Teki Paki", GAME_NO_SOUND )
4918GAME( 1991, ghox,     0,        ghox,     ghox,     T2_Z180,  ROT270, "Toaplan", "Ghox", GAME_NO_SOUND )
4935GAME( 1991, ghox,     0,        ghox,     ghox,     T2_Z180,  ROT270, "Toaplan", "Ghox (set 1)", GAME_NO_SOUND )
4936GAME( 1991, ghoxa,    ghox,     ghox,     ghox,     T2_Z180,  ROT270, "Toaplan", "Ghox (set 2)", GAME_NO_SOUND )
49194937GAME( 1992, dogyuun,  0,        dogyuun,  dogyuun,  T2_V25,   ROT270, "Toaplan", "Dogyuun", GAME_NO_SOUND )
49204938GAME( 1993, kbash,    0,        kbash,    kbash,    T2_V25,   ROT0,   "Toaplan", "Knuckle Bash", GAME_IMPERFECT_SOUND )
49214939GAME( 1999, kbash2,   0,        kbash2,   kbash2,   T2_noZ80, ROT0,   "bootleg", "Knuckle Bash 2 (bootleg)", 0 )
src/mame/drivers/gaelco.c
r2627r2628
1111#include "cpu/m68000/m68000.h"
1212#include "sound/okim6295.h"
1313#include "sound/3812intf.h"
14#include "gaelcrpt.h"
1415
1516extern UINT16 *gaelco_vregs;
1617extern UINT16 *gaelco_videoram;
r2627r2628
655656INPUT_PORTS_END
656657
657658
658static int lastpc=0;
659static int lastword=0;
660static int lastoffset=0;
661659
662
663#define CARRY(bit) {carry[bit] = (dst[bit]>>1); dst[bit]&=1;}
664
665static int decrypt(int game, int key, int high_word, int low_word)
666{
667   static const int swaps[4][16] =
668   {
669      { 5,7,9,12,2,14,13,15,3,6,8,11,4,10,0,1 },
670      { 11,2,5,14,8,13,10,4,7,12,0,6,15,9,1,3 },
671      { 7,0,11,12,13,1,6,8,9,5,4,10,14,3,15,2 },
672      { 3,0,2,1,10,15,8,6,9,13,11,7,12,5,4,14 },
673   };
674
675   int dst[16],carry[16];
676   int i;
677   const int *s = swaps[key & 3];
678   int flag1 = BIT(high_word, 9) ^ BIT(high_word, 12) ^ 1;
679   int flag2 = game ? BIT(high_word, 10) : (BIT(high_word, 9) ^ BIT(high_word, 10));
680
681   if (game)
682      flag2 = -flag2;
683
684   for (i = 0; i < 16; i++)
685      dst[i] = BIT(low_word, s[15-i]);
686   for (i = 0; i < 16; i++)
687      carry[i] = 0;
688
689   if (flag1 && flag2)
690   {
691      dst[13] += game ? 0 : 1;                                 CARRY(13)
692      dst[2]  +=               carry[13];                           CARRY(2)
693      dst[15] += 1           + carry[2];                           CARRY(15)
694      dst[4]  +=             - carry[15];                           CARRY(4)
695      dst[6]  += 1           - carry[4];                           CARRY(6)
696      dst[7]  +=               carry[6];                           CARRY(7)
697
698      dst[14] += game ? BIT(low_word, 9) : BIT(~low_word, 9);            CARRY(14)
699      dst[12] += game ? BIT(low_word, 9) : BIT(~low_word, 9);            CARRY(12)
700
701      dst[9]  += dst[15]     + carry[14];                           CARRY(9)
702      dst[10] += dst[15]     + carry[12];                           CARRY(10)
703
704      dst[5]  += BIT(~low_word, 5) + carry[9];                     CARRY(5)
705      dst[1]  += BIT(~low_word, 5) + carry[10];                     CARRY(1)
706
707      dst[3]  += 1           + dst[7]  - carry[5];                  CARRY(3)
708      dst[11] += 1           + dst[7]  - carry[1];                  CARRY(11)
709
710      dst[8]  += 1           + dst[6]  + carry[3];
711      dst[0]  += 1           + dst[6]  + carry[11];
712   }
713   else
714   {
715      dst[13] += BIT(key,2);                                    CARRY(13)
716      dst[2]  += BIT(key,3)  + carry[13];                           CARRY(2)
717      dst[15] += BIT(key,4)  + carry[2];                           CARRY(15)
718      dst[4]  += BIT(key,5)  - carry[15];                           CARRY(4)
719      dst[6]  += BIT(key,6)  - carry[4];                           CARRY(6)
720      dst[7]  += BIT(key,7)  + carry[6];                           CARRY(7)
721
722      dst[14] += BIT(key,8);                                    CARRY(14)
723      dst[12] += BIT(key,8);                                    CARRY(12)
724
725      dst[9]  += BIT(key,9)  + carry[14];                           CARRY(9)
726      dst[10] += BIT(key,9)  + carry[12];                           CARRY(10)
727
728      dst[5]  += BIT(key,10) + carry[9];                           CARRY(5)
729      dst[1]  += BIT(key,10) + carry[10];                           CARRY(1)
730
731      dst[3]  += BIT(key,11) - flag1*dst[2]  - flag2*dst[13] - carry[5];   CARRY(3)
732      dst[11] += BIT(key,11) - flag1*dst[2]  - flag2*dst[13] - carry[1];   CARRY(11)
733
734      dst[8]  += BIT(key,12) + carry[3];
735      dst[0]  += BIT(key,12) + carry[11];
736   }
737
738   low_word = 0;
739   for (i = 0; i < 16; i++)
740      low_word |= (dst[i] & 1) << i;
741
742   switch (game)
743   {
744      default:
745      case 0:
746         return low_word ^ 0xf626;
747      case 1:
748         return low_word ^ 0x8626;
749   }
750}
751
752
753static int get_key(int game, int high_word)
754{
755   int src[16],dst[16],carry[16];
756   int i;
757   int flag1 = BIT(high_word, 9) ^ BIT(high_word, 12) ^ 1;
758   int flag2 = game ? BIT(high_word, 10) : (BIT(high_word, 9) ^ BIT(high_word, 10));
759   int temp;
760
761   for (i = 0; i < 16; i++)
762   {
763      src[i] = BIT(high_word,i);
764      dst[i] = 0;
765      carry[i] = 0;
766   }
767
768
769   dst[10] = flag1 ? (src[7] ^ src[12]) : src[5];
770
771   dst[8]  = flag2 ? (1 ^ src[6]) : 0;
772
773   dst[5]  = (flag1 ^ flag2) ? (1 ^ src[3]) : (1 ^ (src[5] | src[10]) ^ src[11]);
774
775   if (flag1 == 0 && flag2 == 0)
776      dst[6]  = dst[5] + src[2];                           CARRY(6);
777   if (flag1 == 1 && flag2 == 0)
778      dst[6]  = dst[5] + src[8];                           CARRY(6);
779   if (flag1 == 0 && flag2 == 1)
780      dst[6]  = dst[5] + src[13];                           CARRY(6);
781
782   if (game == 0)
783   {
784      if (flag1 == 0 && flag2 == 0)
785      {
786         dst[7] = src[3];
787         dst[7] ^= (1^src[12]) & (1^src[5]) & src[0];
788         dst[7] ^= src[14] & src[5] & src[0];
789         dst[7] ^= ((1^src[12]) ^ src[11]) & (1^src[2]);
790         dst[7] ^= src[11] & src[6];
791         dst[7] ^= src[12] & src[5] & (1^src[2]);
792         dst[7] ^= src[12] & (1^src[11]) & (1^src[5]) & (1^src[6]);
793      }
794      if (flag1 == 1 && flag2 == 0)
795         dst[7] = 1 ^ src[15] ^ carry[6] ^ (src[8] & (1^src[3]));
796      if (flag1 == 0 && flag2 == 1)
797         dst[7] = 1 ^ src[14] ^ carry[6];
798   }
799   else
800   {
801      if (flag1 == 0 && flag2 == 0)
802      {
803         dst[7] = src[3];
804         dst[7] ^= ((1^src[14]) & (1^src[5])) & src[0];
805         dst[7] ^= src[6] ^ (src[14] & (1^src[11]));
806         dst[7] ^= src[5] & (1^src[6]^src[14]) & (1^src[11]);
807         dst[7] ^= (src[11] ^ src[5]) & src[2];
808         dst[7] ^= src[14] & (src[11] & (1^src[5]));
809      }
810      if (flag1 == 1 && flag2 == 0)
811         dst[7] = 1 ^ src[15] ^ carry[6] ^ (src[8] & (1^src[3]));
812      if (flag1 == 0 && flag2 == 1)
813         dst[7] = 1 ^ src[14] ^ carry[6];
814   }
815
816   if (game == 0)
817   {
818      temp = (src[5] | (src[9] & src[10]));
819   }
820   else
821   {
822      temp = src[5];
823   }
824
825   dst[0]  = src[3] ^ (src[6] | ((1^src[11]) & temp));
826
827   if (game == 0)
828   {
829      dst[1]  = src[15]
830            ^ (temp & src[4] & src[3])
831            ^ (temp & (src[11] ? (src[6] | src[4]) : (1^src[6])))
832            ^ (src[13] ?
833               (src[6] & (1^temp) & src[4]) ^ ((1^temp) & (1^src[4]) & src[3])
834               ^ (src[7] & src[8] & (1^src[9]) & temp & (src[6] ^ src[4] ^ src[3]))
835               ^ ((1^src[11]) & (1^src[6]) & temp & src[7] & src[8] & (1^src[9]))
836            ^ (src[10] & src[9] & src[8] & (1 ^ src[4] ^ src[3]))
837            ^ (src[11] & src[10] & src[9] & src[8] & (1^src[6]))
838               :
839               (src[6] & (1^temp) & (1 ^ src[4] ^ src[8]))
840               ^ ((1^temp) & (src[4] ? ((1^src[3]) & (1^src[8])) : (src[3] & src[8])))
841               ^ (src[7] & (1^src[8]) & (1^src[9]) & (1^temp) & (src[6] ^ src[4] ^ src[3]))
842            ^ (src[10] & src[9] & src[8] & (1 ^ src[4] ^ src[3]) & (1^src[5]))
843            ^ (src[11] & src[10] & src[9] & src[8] & (1^src[6]) & (1^src[5]))
844              )
845             ;
846   }
847   else
848   {
849      dst[1]  = src[15]
850            ^ (temp & src[4] & src[3])
851            ^ (temp & (src[11] ? (src[6] | src[4]) : (1^src[6])))
852            ^ (src[13] ?
853               (src[6] & (1^temp) & src[4]) ^ ((1^temp) & (1^src[4]) & src[3])
854               ^ (src[7] & src[8] & src[9] & temp & (src[6] ^ src[4] ^ src[3]))
855               ^ ((1^src[11]) & (1^src[6]) & temp & src[7] & src[8] & src[9])
856               :
857               (src[6] & (1^temp) & (1 ^ src[4] ^ src[8]))
858               ^ ((1^temp) & (src[4] ? ((1^src[3]) & (1^src[8])) : (src[3] & src[8])))
859               ^ (src[7] & (1^src[8]) & src[9] & (1^temp) & (src[6] ^ src[4] ^ src[3]))
860              );
861   }
862
863   if (game == 0)
864   {
865      if (flag1 == 0 && flag2 == 0)
866         dst[11] = src[0] ^ (src[5] ? src[14] : src[10]);
867      if (flag1 == 1 && flag2 == 0)
868         dst[11] = 0;
869      if (flag1 == 0 && flag2 == 1)
870         dst[11] = 0;
871   }
872   else
873   {
874      if (flag1 == 0 && flag2 == 0)
875         dst[11] = src[0] ^ (src[5] ? src[10] : (1^src[14]));   // note src[10] == 0
876      if (flag1 == 1 && flag2 == 0)
877         dst[11] = 0;
878      if (flag1 == 0 && flag2 == 1)
879         dst[11] = 1;
880   }
881
882   dst[12] = dst[11];
883   if (flag1 == 0 && flag2 == 0)
884      dst[12] ^= src[10];
885   if (flag1 == 1 && flag2 == 0)
886      dst[12] ^= 0;
887   if (flag1 == 0 && flag2 == 1)
888      dst[12] ^= src[3] ^ (src[11] ? src[6] : (src[6] | src[5]));
889
890   if (game == 0)
891   {
892      if (flag1 == 0 && flag2 == 0)
893         dst[4]  = src[6];
894      if (flag1 == 1 && flag2 == 0)
895         dst[4]  = src[0] ^ (src[14] ? (1^src[12]) : src[5]);
896      if (flag1 == 0 && flag2 == 1)
897         dst[4]  = src[7];
898   }
899   else
900   {
901      if (flag1 == 0 && flag2 == 0)
902         dst[4]  = src[6];
903      if (flag1 == 1 && flag2 == 0)
904         dst[4]  = src[0] ^ (src[14] ? ((1^src[12]) | (1^src[5])) : (src[12] | src[5]));
905      if (flag1 == 0 && flag2 == 1)
906         dst[4]  = src[7];
907   }
908
909   if (game == 0)
910   {
911      if (flag1 == 0 && flag2 == 0)
912         dst[9] = src[5] ^ (src[12] & (1^src[7])) ^ src[13];
913      if (flag1 == 1 && flag2 == 0)
914         dst[9] = 1 ^ src[4];
915      if (flag1 == 0 && flag2 == 1)
916         dst[9] = src[6] ^ ((1^src[11]) & src[5]);
917   }
918   else
919   {
920      if (flag1 == 0 && flag2 == 0)
921         dst[9] = 1 ^ src[5] ^ ((1^src[12]) & src[7]) ^ src[13];
922      if (flag1 == 1 && flag2 == 0)
923         dst[9] = 1 ^ src[4];
924      if (flag1 == 0 && flag2 == 1)
925         dst[9] = src[6] ^ ((1^src[11]) & src[5]);
926   }
927
928   if (game == 0)
929   {
930      if (flag1 == 0 && flag2 == 0)
931         dst[3] = src[12];
932      if (flag1 == 1 && flag2 == 0)
933         dst[3] = src[0] ^ (src[14] ? src[12] : (1^src[5]));
934      if (flag1 == 0 && flag2 == 1)
935         dst[3] = src[8] ^ ((1^src[13]) & (1^src[12]) & (1^src[5])) ^ (src[12] & (src[13] ? (src[7] & src[5]) : ((1^src[7]) & (1^src[5]))));
936   }
937   else
938   {
939      if (flag1 == 0 && flag2 == 0)
940         dst[3] = src[12];
941      if (flag1 == 1 && flag2 == 0)
942         dst[3] = src[0] ^ (src[14] ? (src[5] & src[12]) : ((1^src[5]) & (1^src[12])));
943      if (flag1 == 0 && flag2 == 1)
944         dst[3] = src[8] ^ ((1^src[13]) & (src[12]) & (1^src[5])) ^ ((1^src[12]) & (src[13] ? (src[7] & src[5]) : ((1^src[7]) & (1^src[5]))));
945   }
946
947   if (game == 0)
948   {
949      if (flag1 == 0 && flag2 == 0)
950         dst[2] = src[0];
951      if (flag1 == 1 && flag2 == 0)
952         dst[2] = src[1]
953               ^ (src[3] & (1^src[2]))
954               ^ ((1^src[11]) & src[6] & src[5])
955               ^ ((1^src[12]) & (1^src[5]) & src[0] & (1^src[14]) & (src[6] ^ src[2] ^ src[3]))
956               ^ (src[12] & (1^src[6]) & (1^src[5]) & src[3])
957               ^ (src[2] & (1^src[5]) & (1^src[6]) & (1^src[12]))
958               ^ (src[2] & src[5] & (1^src[6]) & src[11])
959               ^ ((1 ^ src[2] ^ src[3]) & (1^src[5]) & src[6] & src[12])
960               ^ (src[14] & (1^src[12]) & (src[2] ^ src[3] ^ src[6]) & (1^src[5]))
961               ^ (src[14] & src[12] & src[5] & (1 ^ src[3] ^ src[2] ^ src[11]) & src[0])
962               ^ (src[14] & src[12] & src[11] & src[6] & src[5] & src[0])
963            ^ (src[12] & (1^src[5]) & src[3])
964            ^ (src[12] & (1^src[5]) & src[0] & (1 ^ src[3] ^ src[2] ^ (src[11] & (1^src[6]))))
965            ^ (src[12] & (1^src[5]) & (1^src[11]) & src[6] & src[2])
966            ^ (src[12] & (1^src[5]) & src[11] & (src[6] ^ src[2]))
967               ;
968      if (flag1 == 0 && flag2 == 1)
969         dst[2] = src[5];
970   }
971   else
972   {
973      if (flag1 == 0 && flag2 == 0)
974         dst[2] = src[0];
975      if (flag1 == 1 && flag2 == 0)
976         dst[2] = src[1]
977               ^ (src[3] & (1^src[2]))
978               ^ ((1^src[11]) & src[6] & src[5])
979               ^ ((1^src[12]) & (1^src[5]) & src[0] & (1^src[14]) & (src[6] ^ src[2] ^ src[3]))
980               ^ (src[12] & (1^src[6]) & (1^src[5]) & src[3])
981               ^ (src[2] & (1^src[5]) & (1^src[6]) & (1^src[12]))
982               ^ (src[2] & src[5] & (1^src[6]) & src[11])
983               ^ ((1 ^ src[2] ^ src[3]) & (1^src[5]) & src[6] & src[12])
984               ^ (src[14] & (1^src[12]) & (src[2] ^ src[3] ^ src[6]) & (1^src[5]))
985               ^ (src[14] & src[12] & src[5] & (1 ^ src[3] ^ src[2] ^ src[11]) & src[0])
986               ^ (src[14] & src[12] & src[11] & src[6] & src[5] & src[0]);
987      if (flag1 == 0 && flag2 == 1)
988         dst[2] = src[5];
989   }
990
991   if (game == 0)
992   {
993      dst[10] ^= 1;
994      dst[4]  ^= 1;
995      dst[2]  ^= 1;
996   }
997   else
998   {
999      dst[10] ^= 1;
1000      dst[8]  ^= 1;
1001      dst[4]  ^= 1;
1002   }
1003
1004
1005   high_word = 0;
1006   for (i = 0; i < 16; i++)
1007      high_word |= (dst[i] & 1) << i;
1008
1009   if (flag1 == 1 && flag2 == 1)
1010      high_word &= 3;
1011
1012   return high_word;
1013}
1014
1015
1016static UINT16 squash_encrypt(int offset, int data, int game)
1017{
1018   int thispc = activecpu_get_pc();
1019   int savedata = data;
1020
1021   /* check if 2nd half of 32 bit */
1022   if(lastpc == thispc && offset == lastoffset + 1)
1023   {
1024      lastpc = 0;
1025      data = decrypt(game, get_key(game, lastword), lastword, data);
1026   }
1027   else
1028   {
1029      /* code as 1st word */
1030      lastword = data;
1031      lastpc = thispc;
1032      lastoffset = offset;
1033
1034      /* high word returned */
1035      data = decrypt(game, 0x0000, 0x0400, lastword);
1036
1037      logerror("%8x : data1 = %4x > %4x @ %8x\n",activecpu_get_pc(),savedata,data,lastoffset);
1038   }
1039   return data;
1040}
1041
1042660static WRITE16_HANDLER( gaelco_vram_encrypted_w )
1043661{
1044662//  mame_printf_debug("gaelco_vram_encrypted_w!!\n");
1045663
1046   data = squash_encrypt(offset,data,0);
664   data = gaelco_decrypt(offset, data, 0x0f, 0x4228);
1047665   COMBINE_DATA(&gaelco_videoram[offset]);
1048666
1049667   tilemap_mark_tile_dirty(gaelco_tilemap[offset >> 11],((offset << 1) & 0x0fff) >> 2);
r2627r2628
1054672{
1055673//  mame_printf_debug("gaelco_encrypted_w!!\n");
1056674
1057    data = squash_encrypt(offset,data,0);
1058        COMBINE_DATA(&gaelco_screen[offset]);
675   data = gaelco_decrypt(offset, data, 0x0f, 0x4228);
676   COMBINE_DATA(&gaelco_screen[offset]);
1059677}
1060678
1061679static WRITE16_HANDLER( thoop_vram_encrypted_w )
1062680{
1063681//  mame_printf_debug("gaelco_vram_encrypted_w!!\n");
1064682
1065   data = squash_encrypt(offset,data,1);
683   data = gaelco_decrypt(offset, data, 0x0e, 0x4228);
1066684   COMBINE_DATA(&gaelco_videoram[offset]);
1067685
1068686   tilemap_mark_tile_dirty(gaelco_tilemap[offset >> 11],((offset << 1) & 0x0fff) >> 2);
r2627r2628
1072690{
1073691//  mame_printf_debug("gaelco_encrypted_w!!\n");
1074692
1075    data = squash_encrypt(offset,data,1);
1076     COMBINE_DATA(&gaelco_screen[offset]);
693   data = gaelco_decrypt(offset, data, 0x0e, 0x4228);
694   COMBINE_DATA(&gaelco_screen[offset]);
1077695}
1078696
1079697
r2627r2628
1170788   PORT_DIPNAME( 0x04, 0x04, "Player Controls" )
1171789   PORT_DIPSETTING(    0x04, "2 Joysticks" )
1172790   PORT_DIPSETTING(    0x00, "1 Joystick" )
1173   PORT_DIPNAME( 0x18, 0x18, DEF_STR( Lives ) )
791   PORT_DIPNAME( 0x18, 0x08, DEF_STR( Lives ) )
1174792   PORT_DIPSETTING(    0x00, "4" )
1175793   PORT_DIPSETTING(    0x08, "3" )
1176794   PORT_DIPSETTING(    0x10, "2" )
r2627r2628
1336954GAME( 1991, bigkarnk, 0,        bigkarnk, bigkarnk, 0, ROT0, "Gaelco", "Big Karnak", 0 )
1337955GAME( 1995, biomtoy,  0,        maniacsq, biomtoy,  0, ROT0, "Gaelco", "Biomechanical Toy (unprotected)", 0 )
1338956GAME( 1996, maniacsp, maniacsq, maniacsq, maniacsq, 0, ROT0, "Gaelco", "Maniac Square (prototype)", 0 )
1339GAME( 1992, squash,   0,      squash,   squash,   0, ROT0, "Gaelco", "Squash (Ver. 1.0)", 0 )
1340GAME( 1992, thoop,    0,      thoop,    thoop,    0, ROT0, "Gaelco", "Thunder Hoop (Ver. 1)", 0 )
957GAME( 1992, squash,   0,        squash,   squash,   0, ROT0, "Gaelco", "Squash (Ver. 1.0)", 0 )
958GAME( 1992, thoop,    0,        thoop,    thoop,    0, ROT0, "Gaelco", "Thunder Hoop (Ver. 1)", 0 )
src/mame/drivers/tankbust.c
r2627r2628
191191   return variable_data;
192192}
193193
194static ADDRESS_MAP_START( readmem, ADDRESS_SPACE_PROGRAM, 8 )
195   AM_RANGE(0x0000, 0x5fff) AM_READ(SMH_ROM)
196   AM_RANGE(0x6000, 0x9fff) AM_READ(SMH_BANK1)
197   AM_RANGE(0xa000, 0xbfff) AM_READ(SMH_BANK2)
198
199   AM_RANGE(0xe000, 0xe007) AM_READ(debug_output_area_r)
200
201   AM_RANGE(0xf000, 0xf7ff) AM_READ(SMH_RAM)
202
203//AM_RANGE(0xf800, 0xffff) AM_READ(read_from_unmapped_memory)   /* a bug in game code ? */
204
205   AM_RANGE(0xe800, 0xe800) AM_READ(input_port_0_r)
194static ADDRESS_MAP_START( main_map, ADDRESS_SPACE_PROGRAM, 8 )
195   AM_RANGE(0x0000, 0x5fff) AM_ROM
196   AM_RANGE(0x6000, 0x9fff) AM_READWRITE(SMH_BANK1, SMH_ROM)
197   AM_RANGE(0xa000, 0xbfff) AM_READWRITE(SMH_BANK2, SMH_ROM)
198   AM_RANGE(0xc000, 0xc7ff) AM_READWRITE(tankbust_background_videoram_r, tankbust_background_videoram_w) AM_BASE(&videoram)
199   AM_RANGE(0xc800, 0xcfff) AM_READWRITE(tankbust_background_colorram_r, tankbust_background_colorram_w) AM_BASE(&colorram)
200   AM_RANGE(0xd000, 0xd7ff) AM_READWRITE(tankbust_txtram_r, tankbust_txtram_w) AM_BASE(&tankbust_txtram)
201   AM_RANGE(0xd800, 0xd8ff) AM_RAM AM_BASE(&spriteram) AM_SIZE(&spriteram_size)
202   AM_RANGE(0xe000, 0xe007) AM_READWRITE(debug_output_area_r, tankbust_e0xx_w)
203   AM_RANGE(0xe800, 0xe800) AM_READWRITE(input_port_0_r, tankbust_yscroll_w)
206204   AM_RANGE(0xe801, 0xe801) AM_READ(input_port_1_r)
207205   AM_RANGE(0xe802, 0xe802) AM_READ(input_port_2_r)
208   AM_RANGE(0xe803, 0xe803) AM_READ(some_changing_input)/*unknown. Game expects this to change so this is not player input */
209
210   AM_RANGE(0xc000, 0xc7ff) AM_READ(tankbust_background_videoram_r)
211   AM_RANGE(0xc800, 0xcfff) AM_READ(tankbust_background_colorram_r)
212   AM_RANGE(0xd000, 0xd7ff) AM_READ(tankbust_txtram_r)
213   AM_RANGE(0xd800, 0xd8ff) AM_READ(SMH_RAM)
214ADDRESS_MAP_END
215
216static ADDRESS_MAP_START( writemem, ADDRESS_SPACE_PROGRAM, 8 )
217   AM_RANGE(0x0000, 0x5fff) AM_WRITE(SMH_ROM)
218   AM_RANGE(0x6000, 0x9fff) AM_WRITE(SMH_ROM)
219   AM_RANGE(0xa000, 0xbfff) AM_WRITE(SMH_ROM)
220
221   AM_RANGE(0xf000, 0xf7ff) AM_WRITE(SMH_RAM)
222
223   AM_RANGE(0xe000, 0xe007) AM_WRITE(tankbust_e0xx_w)
224
225   AM_RANGE(0xe800, 0xe800) AM_WRITE(tankbust_yscroll_w)
226206   AM_RANGE(0xe801, 0xe802) AM_WRITE(tankbust_xscroll_w)
227   AM_RANGE(0xe803, 0xe803) AM_WRITE(tankbust_soundlatch_w)
207   AM_RANGE(0xe803, 0xe803) AM_READWRITE(some_changing_input, tankbust_soundlatch_w)   /*unknown. Game expects this to change so this is not player input */
228208   AM_RANGE(0xe804, 0xe804) AM_WRITE(SMH_NOP)   /* watchdog ? ; written in long-lasting loops */
229
230   AM_RANGE(0xc000, 0xc7ff) AM_WRITE(tankbust_background_videoram_w) AM_BASE(&videoram)
231   AM_RANGE(0xc800, 0xcfff) AM_WRITE(tankbust_background_colorram_w) AM_BASE(&colorram)
232   AM_RANGE(0xd000, 0xd7ff) AM_WRITE(tankbust_txtram_w) AM_BASE(&tankbust_txtram)
233   AM_RANGE(0xd800, 0xd8ff) AM_WRITE(SMH_RAM) AM_BASE(&spriteram) AM_SIZE(&spriteram_size)
209   AM_RANGE(0xf000, 0xf7ff) AM_RAM
210   //AM_RANGE(0xf800, 0xffff) AM_READ(read_from_unmapped_memory)   /* a bug in game code ? */
234211ADDRESS_MAP_END
235212
236
237static ADDRESS_MAP_START( readport2, ADDRESS_SPACE_IO, 8 )
213static ADDRESS_MAP_START( port_map_cpu2, ADDRESS_SPACE_IO, 8 )
238214   ADDRESS_MAP_GLOBAL_MASK(0xff)
239   AM_RANGE(0xc0, 0xc0) AM_READ(AY8910_read_port_0_r)
215   AM_RANGE(0x10, 0x10) AM_WRITE(AY8910_write_port_1_w)
240216   AM_RANGE(0x30, 0x30) AM_READ(AY8910_read_port_1_r)
241ADDRESS_MAP_END
242
243static ADDRESS_MAP_START( writeport2, ADDRESS_SPACE_IO, 8 )
244   ADDRESS_MAP_GLOBAL_MASK(0xff)
245   AM_RANGE(0xc0, 0xc0) AM_WRITE(AY8910_control_port_0_w)
246   AM_RANGE(0x40, 0x40) AM_WRITE(AY8910_write_port_0_w)
247217   AM_RANGE(0x30, 0x30) AM_WRITE(AY8910_control_port_1_w)
248   AM_RANGE(0x10, 0x10) AM_WRITE(AY8910_write_port_1_w)
218   AM_RANGE(0x40, 0x40) AM_WRITE(AY8910_write_port_0_w)
219   AM_RANGE(0xc0, 0xc0) AM_READ(AY8910_read_port_0_r)
220   AM_RANGE(0xc0, 0xc0) AM_WRITE(AY8910_control_port_0_w)
249221ADDRESS_MAP_END
250222
251223
252static ADDRESS_MAP_START( readmem2, ADDRESS_SPACE_PROGRAM, 8 )
224static ADDRESS_MAP_START( map_cpu2, ADDRESS_SPACE_PROGRAM, 8 )
253225   AM_RANGE(0x0000, 0x1fff) AM_READ(SMH_ROM)
254   AM_RANGE(0x8000, 0x87ff) AM_READ(SMH_RAM)
255ADDRESS_MAP_END
256
257static ADDRESS_MAP_START( writemem2, ADDRESS_SPACE_PROGRAM, 8 )
258226   AM_RANGE(0x0000, 0x1fff) AM_WRITE(SMH_ROM)
259   AM_RANGE(0x8000, 0x87ff) AM_WRITE(SMH_RAM)
260
261227   AM_RANGE(0x2000, 0x3fff) AM_WRITE(SMH_NOP)   /* garbage, written in initialization loop */
262//0x4000 and 0x4040-0x4045 seem to be used (referenced in the code)
228   //0x4000 and 0x4040-0x4045 seem to be used (referenced in the code)
263229   AM_RANGE(0x4000, 0x7fff) AM_WRITE(SMH_NOP)   /* garbage, written in initialization loop */
230   AM_RANGE(0x8000, 0x87ff) AM_READ(SMH_RAM)
231   AM_RANGE(0x8000, 0x87ff) AM_WRITE(SMH_RAM)
264232ADDRESS_MAP_END
265233
266234
267
268
269
270235static INPUT_PORTS_START( tankbust )
271236   PORT_START   /* IN0 */
272237   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_JOYSTICK_UP ) PORT_8WAY
r2627r2628
372337
373338   /* basic machine hardware */
374339   MDRV_CPU_ADD(Z80, 4000000)      /* 4 MHz ? */
375   MDRV_CPU_PROGRAM_MAP( readmem, writemem )
340   MDRV_CPU_PROGRAM_MAP(main_map,0)
376341   MDRV_CPU_VBLANK_INT("main", irq0_line_hold)
377342
378343   MDRV_CPU_ADD(Z80, 4000000)      /* 3.072 MHz ? */
379   MDRV_CPU_PROGRAM_MAP( readmem2, writemem2 )
380   MDRV_CPU_IO_MAP( readport2, writeport2 )
344   MDRV_CPU_PROGRAM_MAP(map_cpu2,0)
345   MDRV_CPU_IO_MAP(port_map_cpu2,0)
381346
382347   MDRV_INTERLEAVE(100)
383348
src/mame/drivers/tmaster.c
r2627r2628
174174{
175175   mame_system_time systime;
176176
177   mame_get_current_datetime(Machine, &systime);
177   mame_get_current_datetime(machine, &systime);
178178   rtc_ram[0x1] = binary_to_BCD(systime.local_time.second);
179179   rtc_ram[0x2] = binary_to_BCD(systime.local_time.minute);
180180   rtc_ram[0x3] = binary_to_BCD(systime.local_time.hour);
181   rtc_ram[0x4] = binary_to_BCD(systime.local_time.weekday+1);
181   rtc_ram[0x4] = binary_to_BCD(systime.local_time.weekday);
182182   rtc_ram[0x5] = binary_to_BCD(systime.local_time.mday);
183183   rtc_ram[0x6] = binary_to_BCD(systime.local_time.month+1);
184184   rtc_ram[0x7] = binary_to_BCD(systime.local_time.year % 100);
r2627r2628
421421
422422static READ16_HANDLER( tmaster_coins_r )
423423{
424   return input_port_read(machine, "COIN")|(mame_rand(Machine)&0x0800);
424   return input_port_read(machine, "COIN")|(mame_rand(machine)&0x0800);
425425}
426426
427427static ADDRESS_MAP_START( tmaster_map, ADDRESS_SPACE_PROGRAM, 16 )
src/mame/drivers/atarig42.c
r2627r2628
147147{
148148   if  (address < 0x80000)
149149   {
150      opcode_base = opcode_arg_base = (void *)sloop_base;
150      opbase->rom = opbase->ram = (void *)sloop_base;
151151      return (offs_t)-1;
152152   }
153153   return address;
src/mame/drivers/tagteam.c
r2627r2628
5151}
5252
5353
54static ADDRESS_MAP_START( readmem, ADDRESS_SPACE_PROGRAM, 8 )
55   AM_RANGE(0x0000, 0x07ff) AM_READ(SMH_RAM)
56   AM_RANGE(0x2000, 0x2000) AM_READ(input_port_1_r)     /* IN1 */
57   AM_RANGE(0x2001, 0x2001) AM_READ(input_port_0_r)     /* IN0 */
58   AM_RANGE(0x2002, 0x2002) AM_READ(input_port_2_r)     /* DSW2 */
54static ADDRESS_MAP_START( main_map, ADDRESS_SPACE_PROGRAM, 8 )
55   AM_RANGE(0x0000, 0x07ff) AM_RAM
56   AM_RANGE(0x2000, 0x2000) AM_READWRITE(input_port_1_r, tagteam_flipscreen_w)
57   AM_RANGE(0x2001, 0x2001) AM_READWRITE(input_port_0_r, tagteam_control_w)
58   AM_RANGE(0x2002, 0x2002) AM_READWRITE(input_port_2_r, sound_command_w)
5959   AM_RANGE(0x2003, 0x2003) AM_READ(input_port_3_r)     /* DSW1 */
60   AM_RANGE(0x4000, 0x43ff) AM_READ(tagteam_mirrorvideoram_r)
61   AM_RANGE(0x4400, 0x47ff) AM_READ(tagteam_mirrorcolorram_r)
62   AM_RANGE(0x4800, 0x4fff) AM_READ(SMH_RAM)
63   AM_RANGE(0x8000, 0xffff) AM_READ(SMH_ROM)
64ADDRESS_MAP_END
65
66static ADDRESS_MAP_START( writemem, ADDRESS_SPACE_PROGRAM, 8 )
67   AM_RANGE(0x0000, 0x07ff) AM_WRITE(SMH_RAM)
68   AM_RANGE(0x2000, 0x2000) AM_WRITE(tagteam_flipscreen_w)
69   AM_RANGE(0x2001, 0x2001) AM_WRITE(tagteam_control_w)
70   AM_RANGE(0x2002, 0x2002) AM_WRITE(sound_command_w)
7160//  AM_RANGE(0x2003, 0x2003) AM_WRITE(SMH_NOP) /* Appears to increment when you're out of the ring */
72   AM_RANGE(0x4000, 0x43ff) AM_WRITE(tagteam_mirrorvideoram_w)
73   AM_RANGE(0x4400, 0x47ff) AM_WRITE(tagteam_mirrorcolorram_w)
61   AM_RANGE(0x4000, 0x43ff) AM_READWRITE(tagteam_mirrorvideoram_r, tagteam_mirrorvideoram_w)
62   AM_RANGE(0x4400, 0x47ff) AM_READWRITE(tagteam_mirrorcolorram_r, tagteam_mirrorcolorram_w)
63   AM_RANGE(0x4800, 0x4fff) AM_READ(SMH_RAM)
7464   AM_RANGE(0x4800, 0x4bff) AM_WRITE(tagteam_videoram_w) AM_BASE(&videoram)
7565   AM_RANGE(0x4c00, 0x4fff) AM_WRITE(tagteam_colorram_w) AM_BASE(&colorram)
76   AM_RANGE(0x8000, 0xffff) AM_WRITE(SMH_ROM)
66   AM_RANGE(0x8000, 0xffff) AM_ROM
7767ADDRESS_MAP_END
7868
79
80static ADDRESS_MAP_START( sound_readmem, ADDRESS_SPACE_PROGRAM, 8 )
81   AM_RANGE(0x0000, 0x03ff) AM_READ(SMH_RAM)
82   AM_RANGE(0x2007, 0x2007) AM_READ(soundlatch_r)
83   AM_RANGE(0x4000, 0xffff) AM_READ(SMH_ROM)
84ADDRESS_MAP_END
85
86static ADDRESS_MAP_START( sound_writemem, ADDRESS_SPACE_PROGRAM, 8 )
87   AM_RANGE(0x0000, 0x03ff) AM_WRITE(SMH_RAM)
69static ADDRESS_MAP_START( sound_map, ADDRESS_SPACE_PROGRAM, 8 )
70   AM_RANGE(0x0000, 0x03ff) AM_RAM
8871   AM_RANGE(0x2000, 0x2000) AM_WRITE(AY8910_write_port_0_w)
8972   AM_RANGE(0x2001, 0x2001) AM_WRITE(AY8910_control_port_0_w)
9073   AM_RANGE(0x2002, 0x2002) AM_WRITE(AY8910_write_port_1_w)
9174   AM_RANGE(0x2003, 0x2003) AM_WRITE(AY8910_control_port_1_w)
9275   AM_RANGE(0x2004, 0x2004) AM_WRITE(DAC_0_data_w)
9376   AM_RANGE(0x2005, 0x2005) AM_WRITE(interrupt_enable_w)
77   AM_RANGE(0x2007, 0x2007) AM_READ(soundlatch_r)
78   AM_RANGE(0x4000, 0xffff) AM_READ(SMH_ROM)
9479ADDRESS_MAP_END
9580
9681
97
9882static INTERRUPT_GEN( tagteam_interrupt )
9983{
10084   static int coin;
r2627r2628
271255
272256   /* basic machine hardware */
273257   MDRV_CPU_ADD(M6502, 1500000)   /* 1.5 MHz ?? */
274   MDRV_CPU_PROGRAM_MAP(readmem,writemem)
258   MDRV_CPU_PROGRAM_MAP(main_map,0)
275259   MDRV_CPU_VBLANK_INT("main", tagteam_interrupt)
276260
277261   MDRV_CPU_ADD(M6502, 975000)
278262   /* audio CPU */  /* 975 kHz ?? */
279   MDRV_CPU_PROGRAM_MAP(sound_readmem,sound_writemem)
263   MDRV_CPU_PROGRAM_MAP(sound_map,0)
280264   MDRV_CPU_VBLANK_INT_HACK(nmi_line_pulse,16)   /* IRQs are triggered by the main CPU */
281265
282266   /* video hardware */
src/mame/drivers/atarisy2.c
r2627r2628
236236   /* make sure slapstic area looks like ROM */
237237   if (address >= 0x8000 && address < 0x8200)
238238   {
239      opcode_base = opcode_arg_base = (UINT8 *)atarisy2_slapstic - 0x8000;
239      opbase->rom = opbase->ram = (UINT8 *)atarisy2_slapstic - 0x8000;
240240      return ~0;
241241   }
242242   return address;
src/mame/drivers/asteroid.c
r2627r2628
373373   PORT_MODIFY("IN0")
374374   /* Bit 0 is VG_HALT, handled in the machine dependent part */
375375   /* Bit 2 is the 3 KHz source */
376   PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_BUTTON5 ) PORT_CODE(KEYCODE_SPACE) PORT_CODE(JOYCODE_BUTTON3)      /* hyperspace */
377   PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_BUTTON3 ) PORT_CODE(KEYCODE_LCONTROL) PORT_CODE(JOYCODE_BUTTON1)   /* fire */
378   PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_SERVICE ) PORT_NAME("Diagnostic Step") PORT_CODE(KEYCODE_F1)
379   PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_TILT )
376380   PORT_SERVICE( 0x80, IP_ACTIVE_LOW )
377381
378382   PORT_MODIFY("DSW")
src/mame/drivers/gatron.c
r0r2628
1/******************************************************************************
2
3    GAME-A-TRON gaming hardware
4    ---------------------------
5
6    Driver by Roberto Fresca.
7
8
9    Games running on this hardware:
10
11    * Card Games??,  1983, Game-A-Tron.
12    * Slots Game??,  1983, Game-A-Tron.
13
14
15    The names of both games are unknown. Only were arbitrary called "Card Games"
16    and "Slot Game" till we can find some evidence of the real names.
17
18
19*******************************************************************************
20
21
22    Hardware Notes:
23    ---------------
24
25
26    * PCB1: SLOTS GAME?.
27
28    Board silkscreend:
29
30     GAME-A-TRON CORP.
31    (c)1983 PAT.PENDING
32
33    ROMS: - U31 - 2732 (PT-1R-V)
34          - U32 - 2732 (PT-2G-V)
35          - U33 - 2732 (PT-3B-V)
36
37          - U00 - 2764 (PULL-TABS-1-90)
38
39    Most chips, except for the EPROMS, were covered in a hard black plastic so that
40    their numbers could not be read.
41
42    U30 was silkscreend VRAM,    24 pin.
43    U11 was silkscreend scp RAM, 24 pin.
44    U13 was silkscreend CPU,     40 pin.
45    U12 was silkscreend DECODE,  16 pin.
46
47    1x 16MHz Crystal
48
49    1x Duracell PX-2 I, 4.5V battery.
50
51
52
53    * PCB2: CARD GAMES?.
54
55    Board silkscreend:
56
57     GAME-A-TRON CORP.
58    (c)1983 PAT.PENDING
59
60    ROMS: - U31 - 2732 (POKER-R)
61          - U32 - 2732 (POKER-G)
62          - U33 - 2732 (BLACK)
63
64          - U00 - 2764 (2764-POKER)
65          - U08 - 2732 (2732-POKER-4-1)
66
67    Most chips, except for the EPROMS, were covered in a hard black plastic so that
68    their numbers could not be read.
69
70    CPU at u13 still covered in black plastic.
71    U05               M5L8255AP-5
72    U11    SCP RAM    TC5516APL  (2Kx8)
73    U30    VRAM       HM6116LP-3 (2Kx8)
74
75    1x 16MHz Crystal
76
77    1x Duracell PX-2 I, 4.5V battery.
78
79
80*******************************************************************************
81
82
83    *** Game Notes ***
84
85
86    All games:
87
88    The first time the machine is turned on, will show the legend "DATA ERROR".
89    You must to RESET (F3) the machine to initialize the NVRAM properly.
90
91
92    * Card Games:
93
94    Pressing SERVICE 1 (key 9) you enter the Test/Setting Mode. You can test
95    inputs there, and change all the game settings. Press "DISCARD 1" (key Z)
96    to choose an option, and "DISCARD 5" (key B) to change the settings.
97    Press "SERVICE 2" (key 0) to exit.
98
99    The setting options are:
100
101        HIGHEST-ANTE-IS:   1-5-10-15-20-25-30-35-40-45-50.
102        JOKERS:            0-1-2.
103        BONUS DRAWS:       0-1.
104        DOUBLE-UPS:        0-1-2-3-4-5-6-7-8-9.
105        WIN-ON:            JACKS AND UP - PAIR OF ACES.
106        SKILL LEVEL:       50-55-60-65-70-75-80-85-90-95-100.
107        CREDITS-PER-COIN:  1-5-10-15-20-25-30-35-40-45-50-55-60-65-70-75-80-85-90-95-100.
108
109    The game allow to choose one of the following card games:
110
111    - DRAW POKER.
112    - STUD POKER.
113    - ACEY-DEUCY.
114    - BLACKJACK.
115    - HIGH-LOW.
116
117    Press "DISCARD 1" (key Z) to switch between games.
118    Press "BET/ANTE" (key N) to bet credits and then start the game.
119
120    The rest of buttons are self-explanatory.
121
122
123    * Slots Game:
124
125    Pressing SERVICE 1 (key 9) you enter the Test/Setting Mode. You can test
126    inputs there, and change all the game settings. Press "SUPER STAR TICKET"
127    (key Z) to choose an option, and "BIG BAR TICKET" (key C) to change the
128    settings. Press "SERVICE 2" (key 0) to exit.
129
130    The setting options are:
131
132        HIGHEST-ANTE-IS:   1-5-10-15-20-25.
133        SKILL LEVEL:       50-55-60-65-70-75-80-85-90-95-100.
134        CREDITS-PER-COIN:  1-5-10-15-20-25-30-35-40-45-50-55-60-65-70-75-80-85-90-95-100.
135        MUSIC:             PLAYS - OFF
136
137    You must bet through "ANTE" (key 1), and then choose a ticket to play.
138
139    Press "SUPER STAR TICKET" (key Z) to play with Super Star (left) Ticket.
140    Press "LADY LUCK TICKET" (key X) to play with Lady Luck (center) Ticket.
141    Press "BIG BAR TICKET" (key C) to play with Big Bar (right) Ticket.
142
143
144*******************************************************************************
145
146    --------------------
147    ***  Memory Map  ***
148    --------------------
149
150    0x0000 - 0x5FFF    ; ROM space.
151    0x6000 - 0x67FF    ; Video RAM (only the first 0x300 bytes are used).
152    0x8000 - 0x87FF    ; Main RAM.
153    0xA000 - 0xA000    ; Unknown Writes.
154    0xE000 - 0xE000    ; Unknown Writes (watchdog?).
155
156
157*******************************************************************************
158
159
160    DRIVER UPDATES:
161
162
163    [2008-05-10]
164
165    - Initial release.
166    - Properly decoded graphics.
167    - Proper memory map.
168    - Added NVRAM support.
169    - Proper Inputs through 8255 PPI I/O chip.
170    - Both games are working.
171    - Added technical & game notes.
172
173
174    TODO:
175
176    - Outputs.
177    - Sound.
178
179
180*******************************************************************************/
181
182
183#define MASTER_CLOCK   XTAL_16MHz
184
185#include "driver.h"
186#include "machine/8255ppi.h"
187
188
189/*************************
190*     Video Hardware     *
191*************************/
192
193static tilemap *bg_tilemap;
194
195
196static WRITE8_HANDLER( gat_videoram_w )
197{
198   videoram[offset] = data;
199   tilemap_mark_tile_dirty(bg_tilemap, offset);
200}
201
202
203static TILE_GET_INFO( get_bg_tile_info )
204{
205/*  - bits -
206    7654 3210
207    xxxx xxxx   tiles code.
208
209    only one color code
210*/
211
212   int code = videoram[tile_index];
213
214   SET_TILE_INFO(0, code, 0, 0);
215}
216
217
218static VIDEO_START( gat )
219{
220   bg_tilemap = tilemap_create(get_bg_tile_info, tilemap_scan_cols, 8, 16, 48, 16);
221}
222
223
224static VIDEO_UPDATE( gat )
225{
226   tilemap_draw(bitmap, cliprect, bg_tilemap, 0, 0);
227   return 0;
228}
229
230
231static PALETTE_INIT( gat )
232{
233
234}
235
236
237/*************************
238*      Machine Init      *
239*************************/
240
241static const ppi8255_interface ppi8255_intf =
242{
243   input_port_0_r,   /* Port A read */
244   input_port_1_r,   /* Port B read */
245   NULL,         /* Port C read */
246   NULL,         /* Port A write */
247   NULL,         /* Port B write */
248   NULL,         /* Port C write */
249};
250
251
252/*************************
253* Memory Map Information *
254*************************/
255
256static ADDRESS_MAP_START( gat_map, ADDRESS_SPACE_PROGRAM, 8 )
257   AM_RANGE(0x0000, 0x5fff) AM_ROM
258   AM_RANGE(0x6000, 0x63ff) AM_RAM AM_WRITE(gat_videoram_w) AM_BASE(&videoram)
259   AM_RANGE(0x8000, 0x87ff) AM_RAM AM_BASE(&generic_nvram) AM_SIZE(&generic_nvram_size)   /* battery backed RAM */
260   AM_RANGE(0xa000, 0xa000) AM_NOP   /* unknown writes */
261   AM_RANGE(0xe000, 0xe000) AM_NOP /* watchdog? */
262ADDRESS_MAP_END
263
264static ADDRESS_MAP_START( gat_portmap, ADDRESS_SPACE_IO, 8 )
265   ADDRESS_MAP_GLOBAL_MASK(0xff)
266   AM_RANGE(0x00, 0x03) AM_DEVREADWRITE(PPI8255, "ppi8255", ppi8255_r, ppi8255_w)
267ADDRESS_MAP_END
268
269/* I/O byte R/W
270
271   0x0200 ;R   ???!
272
273   0x9200 ;R
274   0x9201 ;R
275   0x9203 ;W   (0x92)
276
277   0xC002 ;W   (0xC0)
278   0xC202 ;W   (0xC2)
279
280   0xFF01 ;R
281
282   -----------------
283
284   unknown writes:
285
286   0xA000 ;W   (3 consecutive writes: 8x - Cx - 93)
287   0xE000 ;W   (0x04)
288
289*/
290
291/*************************
292*      Input Ports       *
293*************************/
294
295static INPUT_PORTS_START( gatcards )
296   PORT_START_TAG("IN0")
297   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_BUTTON8 ) PORT_NAME("Discard 4") PORT_CODE(KEYCODE_V)
298   PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_NAME("Bet/Ante") PORT_CODE(KEYCODE_N)
299   PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_BUTTON3 ) PORT_NAME("Deal/Hit") PORT_CODE(KEYCODE_2)
300   PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_COIN1 )
301   PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_BUTTON4 ) PORT_NAME("Stand") PORT_CODE(KEYCODE_A)
302   PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_NAME("Start") PORT_CODE(KEYCODE_1)
303   PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_BUTTON9 ) PORT_NAME("Discard 5") PORT_CODE(KEYCODE_B)
304   PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_BUTTON7 ) PORT_NAME("Discard 3") PORT_CODE(KEYCODE_C)
305
306   PORT_START_TAG("IN1")
307   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_UNKNOWN )
308   PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_BUTTON6 )  PORT_NAME("Discard 2") PORT_CODE(KEYCODE_X)
309   PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_SERVICE2 ) PORT_NAME("Service 2 (Test Mode Out / Coin Stuck)") PORT_CODE(KEYCODE_0)
310   PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_UNKNOWN )   /* Payout? */
311   PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_UNKNOWN )
312   PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_SERVICE1 ) PORT_NAME("Service 1 (Test/Settings)") PORT_CODE(KEYCODE_9)
313   PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_UNKNOWN )
314   PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_BUTTON5 )  PORT_NAME("Discard 1") PORT_CODE(KEYCODE_Z)
315INPUT_PORTS_END
316
317static INPUT_PORTS_START( gatslots )
318   PORT_START_TAG("IN0")
319   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_UNKNOWN )
320   PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_NAME("Ante") PORT_CODE(KEYCODE_1)
321   PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_UNKNOWN )
322   PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_COIN1 )
323   PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_UNKNOWN )
324   PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_UNKNOWN )
325   PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_BUTTON4 ) PORT_NAME("Big Bar Ticket") PORT_CODE(KEYCODE_C)
326   PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_BUTTON3 ) PORT_NAME("Lady Luck Ticket") PORT_CODE(KEYCODE_X)
327
328   PORT_START_TAG("IN1")
329   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_UNKNOWN )
330   PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_UNKNOWN )
331   PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_SERVICE2 ) PORT_NAME("Service 2 (Test Mode Out / Coin Stuck)") PORT_CODE(KEYCODE_0)
332   PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_UNKNOWN )   /* Payout? */
333   PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_UNKNOWN )
334   PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_SERVICE1 ) PORT_NAME("Service 1 (Test/Settings)") PORT_CODE(KEYCODE_9)
335   PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_UNKNOWN )
336   PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_BUTTON2 )  PORT_NAME("Super Star Ticket") PORT_CODE(KEYCODE_Z)
337INPUT_PORTS_END
338
339
340/*************************
341*    Graphics Layouts    *
342*************************/
343
344static const gfx_layout charlayout =
345{
346
347   8, 16,
348   RGN_FRAC(1,3),   /* 256 tiles */
349   3,
350   { 0, RGN_FRAC(1,3), RGN_FRAC(2,3) },    /* bitplanes are separated */
351   { 0, 1, 2, 3, 4, 5, 6, 7 },
352   { 0*8, 1*8, 2*8, 3*8, 4*8, 5*8, 6*8, 7*8,
353     8*8, 9*8, 10*8, 11*8, 12*8, 13*8, 14*8, 15*8 },
354   16*8   /* every char takes 16 consecutive bytes */
355
356};
357
358
359/******************************
360* Graphics Decode Information *
361******************************/
362
363static GFXDECODE_START( gat )
364   GFXDECODE_ENTRY( REGION_GFX1, 0, charlayout, 0, 16 )
365GFXDECODE_END
366
367
368/*************************
369*    Machine Drivers     *
370*************************/
371
372static MACHINE_DRIVER_START( gat )
373
374   /* basic machine hardware */
375   MDRV_CPU_ADD_TAG("main", Z80, MASTER_CLOCK/24)   /* guess */
376   MDRV_CPU_PROGRAM_MAP(gat_map, 0)
377   MDRV_CPU_IO_MAP(gat_portmap,0)
378   MDRV_CPU_VBLANK_INT("main", nmi_line_pulse)
379
380   MDRV_NVRAM_HANDLER(generic_0fill)
381
382   MDRV_DEVICE_ADD( "ppi8255", PPI8255 )
383   MDRV_DEVICE_CONFIG( ppi8255_intf )
384
385   /* video hardware */
386   MDRV_SCREEN_ADD("main", RASTER)
387   MDRV_SCREEN_REFRESH_RATE(60)
388   MDRV_SCREEN_VBLANK_TIME(ATTOSECONDS_IN_USEC(0))
389   MDRV_SCREEN_FORMAT(BITMAP_FORMAT_INDEXED16)
390   MDRV_SCREEN_SIZE(48*8, 16*16)
391   MDRV_SCREEN_VISIBLE_AREA(0*8, 48*8-1, 0*8, 16*16-1)
392
393   MDRV_GFXDECODE(gat)
394
395   MDRV_PALETTE_INIT(gat)
396   MDRV_PALETTE_LENGTH(8)
397
398   MDRV_VIDEO_START(gat)
399   MDRV_VIDEO_UPDATE(gat)
400
401MACHINE_DRIVER_END
402
403
404/*************************
405*        Rom Load        *
406*************************/
407
408ROM_START( gatcards )
409   ROM_REGION( 0x10000, REGION_CPU1, 0 )
410   ROM_LOAD( "gat1.0",      0x0000, 0x2000, CRC(8361fccd) SHA1(4faae6bb3104c1f4a0939d613966085d7e34c1df))
411   ROM_LOAD( "gat1.8",      0x2000, 0x1000, CRC(61e71f31) SHA1(b8d162a47752cff7412b3920ec9dd7a469e81e62) )
412
413   ROM_REGION( 0x3000, REGION_GFX1, ROMREGION_DISPOSE )
414   ROM_LOAD( "gat1.33",      0x0000, 0x1000, CRC(3f8a2d59) SHA1(d61dce33aa8637105905830e2f37c1052c441194) )
415   ROM_LOAD( "gat1.32",      0x1000, 0x1000, CRC(3e7772b2) SHA1(c7499ff148e5a9cbf0958820c41ea09a843ab355) )
416   ROM_LOAD( "gat1.31",      0x2000, 0x1000, CRC(18d090ec) SHA1(3504f18b3984d16545dbe61a03fbf6b8e2027150) )
417ROM_END
418
419ROM_START( gatslots )
420   ROM_REGION( 0x10000, REGION_CPU1, 0 )
421   ROM_LOAD( "gat.0",      0x0000, 0x2000, CRC(7cfd490d) SHA1(8eb360f8f4806a4281dae12236d30aa86d00993d) )
422
423   ROM_REGION( 0x3000, REGION_GFX1, ROMREGION_DISPOSE )
424   ROM_LOAD( "gat.33",      0x0000, 0x1000, CRC(3505cec1) SHA1(98ab0383c4be382aea81ab93433f2f29a075f65d) )
425   ROM_LOAD( "gat.32",      0x1000, 0x1000, CRC(4a3f4f36) SHA1(3dc29f78b7df1a433d0b39bfeaa227615e70ceed) )
426   ROM_LOAD( "gat.31",      0x2000, 0x1000, CRC(6d1b80f4) SHA1(f2da4b4ae1eb05f9ea02e7495ee8110698cc5d1b) )
427ROM_END
428
429
430/*************************
431*      Game Drivers      *
432*************************/
433
434/*    YEAR  NAME      PARENT  MACHINE  INPUT     INIT  ROT    COMPANY        FULLNAME      FLAGS */
435GAME( 1983, gatcards, 0,      gat,     gatcards, 0,    ROT0, "Game-A-Tron", "Card Games?", GAME_NO_SOUND )
436GAME( 1983, gatslots, 0,      gat,     gatslots, 0,    ROT0, "Game-A-Tron", "Slots Game?", GAME_NO_SOUND )
src/mame/drivers/tail2nos.c
r2627r2628
6868   memory_set_bankptr(3,memory_region(REGION_CPU2) + 0x10000 + (data & 0x01) * 0x8000);
6969}
7070
71
72
73static ADDRESS_MAP_START( readmem, ADDRESS_SPACE_PROGRAM, 16 )
74   AM_RANGE(0x000000, 0x03ffff) AM_READ(SMH_ROM)
75   AM_RANGE(0x200000, 0x27ffff) AM_READ(SMH_BANK1)   /* extra ROM */
76   AM_RANGE(0x2c0000, 0x2dffff) AM_READ(SMH_BANK2)   /* extra ROM */
77   AM_RANGE(0x400000, 0x41ffff) AM_READ(tail2nos_zoomdata_r)
78   AM_RANGE(0x500000, 0x500fff) AM_READ(tail2nos_K051316_0_r)
79   AM_RANGE(0xff8000, 0xffbfff) AM_READ(SMH_RAM)   /* work RAM */
80   AM_RANGE(0xffc000, 0xffc2ff) AM_READ(SMH_RAM)   /* sprites */
81   AM_RANGE(0xffc300, 0xffcfff) AM_READ(SMH_RAM)
82   AM_RANGE(0xffd000, 0xffdfff) AM_READ(SMH_RAM)
83   AM_RANGE(0xffe000, 0xffefff) AM_READ(SMH_RAM)
84   AM_RANGE(0xfff000, 0xfff001) AM_READ(input_port_0_word_r)
85   AM_RANGE(0xfff004, 0xfff005) AM_READ(input_port_1_word_r)
86ADDRESS_MAP_END
87
88static ADDRESS_MAP_START( writemem, ADDRESS_SPACE_PROGRAM, 16 )
89   AM_RANGE(0x000000, 0x03ffff) AM_WRITE(SMH_ROM)
90   AM_RANGE(0x200000, 0x27ffff) AM_WRITE(SMH_ROM)
91   AM_RANGE(0x2c0000, 0x2dffff) AM_WRITE(SMH_ROM)
92   AM_RANGE(0x400000, 0x41ffff) AM_WRITE(tail2nos_zoomdata_w)
93   AM_RANGE(0x500000, 0x500fff) AM_WRITE(tail2nos_K051316_0_w)
71static ADDRESS_MAP_START( main_map, ADDRESS_SPACE_PROGRAM, 16 )
72   AM_RANGE(0x000000, 0x03ffff) AM_ROM
73   AM_RANGE(0x200000, 0x27ffff) AM_READWRITE(SMH_BANK1, SMH_ROM)   /* extra ROM */
74   AM_RANGE(0x2c0000, 0x2dffff) AM_READWRITE(SMH_BANK2, SMH_ROM)
75   AM_RANGE(0x400000, 0x41ffff) AM_READWRITE(tail2nos_zoomdata_r, tail2nos_zoomdata_w)
76   AM_RANGE(0x500000, 0x500fff) AM_READWRITE(tail2nos_K051316_0_r, tail2nos_K051316_0_w)
9477   AM_RANGE(0x510000, 0x51001f) AM_WRITE(tail2nos_K051316_ctrl_0_w)
95   AM_RANGE(0xff8000, 0xffbfff) AM_WRITE(SMH_RAM)   /* work RAM */
96   AM_RANGE(0xffc000, 0xffc2ff) AM_WRITE(SMH_RAM) AM_BASE(&spriteram16) AM_SIZE(&spriteram_size)
97   AM_RANGE(0xffc300, 0xffcfff) AM_WRITE(SMH_RAM)
98   AM_RANGE(0xffd000, 0xffdfff) AM_WRITE(tail2nos_bgvideoram_w) AM_BASE(&tail2nos_bgvideoram)
99   AM_RANGE(0xffe000, 0xffefff) AM_WRITE(paletteram16_xRRRRRGGGGGBBBBB_word_w) AM_BASE(&paletteram16)
100   AM_RANGE(0xfff000, 0xfff001) AM_WRITE(tail2nos_gfxbank_w)
78   AM_RANGE(0xff8000, 0xffbfff) AM_RAM                        /* work RAM */
79   AM_RANGE(0xffc000, 0xffc2ff) AM_RAM AM_BASE(&spriteram16) AM_SIZE(&spriteram_size)
80   AM_RANGE(0xffc300, 0xffcfff) AM_RAM
81   AM_RANGE(0xffd000, 0xffdfff) AM_RAM_WRITE(tail2nos_bgvideoram_w) AM_BASE(&tail2nos_bgvideoram)
82   AM_RANGE(0xffe000, 0xffefff) AM_RAM_WRITE(paletteram16_xRRRRRGGGGGBBBBB_word_w) AM_BASE(&paletteram16)
83   AM_RANGE(0xfff000, 0xfff001) AM_READWRITE(input_port_0_word_r, tail2nos_gfxbank_w)
84   AM_RANGE(0xfff004, 0xfff005) AM_READ(input_port_1_word_r)
10185   AM_RANGE(0xfff008, 0xfff009) AM_WRITE(sound_command_w)
10286ADDRESS_MAP_END
10387
104static ADDRESS_MAP_START( sound_readmem, ADDRESS_SPACE_PROGRAM, 8 )
105   AM_RANGE(0x0000, 0x77ff) AM_READ(SMH_ROM)
106   AM_RANGE(0x7800, 0x7fff) AM_READ(SMH_RAM)
107   AM_RANGE(0x8000, 0xffff) AM_READ(SMH_BANK3)
88static ADDRESS_MAP_START( sound_map, ADDRESS_SPACE_PROGRAM, 8 )
89   AM_RANGE(0x0000, 0x77ff) AM_ROM
90   AM_RANGE(0x7800, 0x7fff) AM_RAM
91   AM_RANGE(0x8000, 0xffff) AM_READWRITE(SMH_BANK3, SMH_ROM)
10892ADDRESS_MAP_END
10993
110static ADDRESS_MAP_START( sound_writemem, ADDRESS_SPACE_PROGRAM, 8 )
111   AM_RANGE(0x0000, 0x77ff) AM_WRITE(SMH_ROM)
112   AM_RANGE(0x7800, 0x7fff) AM_WRITE(SMH_RAM)
113   AM_RANGE(0x8000, 0xffff) AM_WRITE(SMH_ROM)
114ADDRESS_MAP_END
115
116static ADDRESS_MAP_START( sound_readport, ADDRESS_SPACE_IO, 8 )
94static ADDRESS_MAP_START( sound_port_map, ADDRESS_SPACE_IO, 8 )
11795   ADDRESS_MAP_GLOBAL_MASK(0xff)
118   AM_RANGE(0x07, 0x07) AM_READ(soundlatch_r)
96   AM_RANGE(0x07, 0x07) AM_READWRITE(soundlatch_r, SMH_NOP)   /* the write is a clear pending command */
97   AM_RANGE(0x08, 0x08) AM_WRITE(YM2608_control_port_0_A_w)
98   AM_RANGE(0x09, 0x09) AM_WRITE(YM2608_data_port_0_A_w)
99   AM_RANGE(0x0a, 0x0a) AM_WRITE(YM2608_control_port_0_B_w)
100   AM_RANGE(0x0b, 0x0b) AM_WRITE(YM2608_data_port_0_B_w)
119101#if 0
120102   AM_RANGE(0x18, 0x18) AM_READ(YM2610_status_port_0_A_r)
121103   AM_RANGE(0x1a, 0x1a) AM_READ(YM2610_status_port_0_B_r)
122104#endif
123105ADDRESS_MAP_END
124106
125static ADDRESS_MAP_START( sound_writeport, ADDRESS_SPACE_IO, 8 )
126   ADDRESS_MAP_GLOBAL_MASK(0xff)
127   AM_RANGE(0x07, 0x07) AM_WRITE(SMH_NOP)   /* clear pending command */
128   AM_RANGE(0x08, 0x08) AM_WRITE(YM2608_control_port_0_A_w)
129   AM_RANGE(0x09, 0x09) AM_WRITE(YM2608_data_port_0_A_w)
130   AM_RANGE(0x0a, 0x0a) AM_WRITE(YM2608_control_port_0_B_w)
131   AM_RANGE(0x0b, 0x0b) AM_WRITE(YM2608_data_port_0_B_w)
132ADDRESS_MAP_END
133107
134
135
136108static INPUT_PORTS_START( tail2nos )
137109   PORT_START
138110   PORT_BIT( 0x0001, IP_ACTIVE_LOW, IPT_UNKNOWN )
r2627r2628
270242
271243   /* basic machine hardware */
272244   MDRV_CPU_ADD(M68000,XTAL_20MHz/2)   /* verified on pcb */
273   MDRV_CPU_PROGRAM_MAP(readmem,writemem)
245   MDRV_CPU_PROGRAM_MAP(main_map,0)
274246   MDRV_CPU_VBLANK_INT("main", irq6_line_hold)
275247
276248   MDRV_CPU_ADD(Z80,XTAL_20MHz/4)   /* verified on pcb */
277249   /* audio CPU */
278   MDRV_CPU_PROGRAM_MAP(sound_readmem,sound_writemem)
279   MDRV_CPU_IO_MAP(sound_readport,sound_writeport)
250   MDRV_CPU_PROGRAM_MAP(sound_map,0)
251   MDRV_CPU_IO_MAP(sound_port_map,0)
280252                        /* IRQs are triggered by the YM2608 */
281253   MDRV_MACHINE_RESET(tail2nos)
282254
src/mame/drivers/meritm.c
r2627r2628
44  Driver by Mariusz Wojcieszek
55
66  CRT 250 (basic components, also used by CRT260)
7    Main CPU: 1xZ80B
8  Sound: 1xYM2149F (or compatible)
9    Video: 2xYamaha V9938 (MSX2 video chip!)
10  Other: 2xZ80APIO (I/O and interrupt controllers)
11         1x8255 (I/O)
7   Main CPU: Z80B
8      Sound: Yamaha YM2149F (or compatible)
9      Video: 2 Yamaha V9938 (MSX2 video chip!)
10      Other: 2 Z80APIO (I/O and interrupt controllers)
11             1 8255 (I/O)
1212
1313  CRT 260 additional components:
1414  - Microtouch touch screen controller (SMT-3)
1515  - PC16550 UART (for communication with touch screen controller)
1616  - DS1204 Electronic Key (for protection)
1717  - DS1232 Reset and Watchdog
18  - DS1644 (megat4te) 32K NVRAM + RTC
1918  - MAX232 (for MegaLink)
2019
20    One of the following Dallas Nonvolatile SRAM chips:
21    - DS1225Y 64K Non-volitile SRAM (Mega Touch 4)
22    - DS1230Y 256K Non-volitile SRAM (Mega Touch 6)
23    - DS1644 32K NVRAM + RTC (Tournament sets)
24
2125  Known Games:
2226
2327  CRT 250:
2428  Pit Boss II (c)1988
2529  Super Pit Boss (c)1988
26  *Pit Boss Superstar (c)1989
30  Pit Boss Superstar (c)199
2731  *Pit Boss Superstar 30 (c)1993
2832  Pit Boss Megastar (c)1994
2933
r2627r2628
3741  Megatouch IV Tournament Edition (c)1996
3842  *Super Megatouch IV (c) 1996
3943  *Megatouch 5 (c)1997
44  *Megatouch 5 Tournament Edition (c)1997
4045  Megatouch 6 (c)1998
4146  *Megatouch 7 Encore (c)2000
4247
r2627r2628
973978   ROM_LOAD( "u16", 0x70000, 0x10000, CRC(574fb3c7) SHA1(213741df3055b97ddd9889c2aa3d3e863e2c86d3) )
974979ROM_END
975980
981ROM_START( pitbosss )
982   ROM_REGION( 0x80000, REGION_CPU1, 0 )
983   ROM_LOAD( "9221-10-0b.u9",  0x00000, 0x10000, CRC(e1fbf7cb) SHA1(e04163219c357cd3da2a78ba2590d453df8e9477) )
984   ROM_LOAD( "9221-10-0.u10",  0x10000, 0x10000, CRC(853a1a99) SHA1(45e33442aa7e51c05c9ac8b8458937ee3ff4c21d) )
985   ROM_LOAD( "9221-10-0b.u11", 0x20000, 0x10000, CRC(6d6dfaf3) SHA1(de76c577eef1bb6637aacffedcc40266af92506e) )
986   ROM_LOAD( "9221-10-0.u12",  0x30000, 0x10000, CRC(3577a203) SHA1(80f9c827ad9dea2c6af788bd3b46ab65e8c594eb) )
987   ROM_LOAD( "9221-10-0.u13",  0x40000, 0x10000, CRC(466f81f9) SHA1(88429d9ff53d27bf639200852a7bf61768c8fd1b) )
988   ROM_LOAD( "9221-10-0.u14",  0x50000, 0x10000, CRC(0720faa6) SHA1(1d78d711e3aab1ecf604ae7b9c374d27639a97c3) )
989   ROM_LOAD( "9221-10-0.u15",  0x60000, 0x10000, CRC(c302b4c2) SHA1(d62d4bb33a9ccb95d1e550f9e439be3316b94c99) )
990   ROM_LOAD( "9221-10-0.u16",  0x70000, 0x10000, CRC(574fb3c7) SHA1(213741df3055b97ddd9889c2aa3d3e863e2c86d3) ) // matches pitboss2
991ROM_END
992
976993ROM_START( spitboss )
977994   ROM_REGION( 0x80000, REGION_CPU1, 0 )
978995   ROM_LOAD( "u9-0a.rom",  0x00000, 0x10000, CRC(e0c45c9c) SHA1(534bff67c8fee08f1c348275de8977659efa9f69) )
r2627r2628
10521069             one PC16550DN
10531070             one PB255a or L5220574
10541071             One Dallas DS1204 Data Key
1055             One dallas DS1225Y 16k Non-volitile SRAM
1072             One Dallas DS1225Y 64k Non-volitile SRAM (Mega Touch 4)
1073              or Dallas DS1230Y 256K Non-volitile SRAM (Mega Touch 6)
1074              or Dallas DS1644 32K NVRAM + RTC (Tournament sets)
10561075             Two Z80APIO (Z0842004PSC)
10571076
10581077    OSC 21.477270 MHz & 1.8432MHz (near the PC16550DN)
r2627r2628
10771096 U38-R0E     = 27C4001 (AKA 27C040)
10781097C1996 MII
10791098
10999255-40-01
1100U5-B-RO1     =  Dallas DS1204V
1101C1996 MII
10801102
1103
10811104Actual Megatouch 6 rom labels
10821105--------------------------------
10831106
r2627r2628
10971120 U38-R0A     = 27C801
10981121C1998 MII
10991122
11239255-80
1124U5-B-RO1     =  Dallas DS1204V
1125C1998 MII
1126
1127
11001128PAL:
11011129SC3944-0A.u19 = PALCE22V10H-25PC/4
11021130SC3980.u40    = PALCE16V8H-25
r2627r2628
11051133
11061134*/
11071135
1108ROM_START( megat3 )
1136ROM_START( megat3 ) /* Dallas DS1204V security key at U5 labeled 9255-20-01 U5-RO1 C1995 MII */
11091137   ROM_REGION( 0x400000, REGION_CPU1, 0 )
1110   ROM_LOAD( "9255-20-01_u32-r0", 0x000000, 0x080000, CRC(ac969296) SHA1(7e09e9141637339b83c21f2488560cdf8a460069) ) /* Location U32 */
1138   ROM_LOAD( "9255-20-01_u32-roa", 0x000000, 0x080000, CRC(69110f8f) SHA1(253487f0b4a82072efb7c70bebf953ea1c41d0d8) ) /* Location U32 */
11111139   ROM_RELOAD(                     0x080000, 0x080000)
1112   ROM_LOAD( "9255-01_u36-r0",     0x100000, 0x080000, CRC(96bb501e) SHA1(f48ef238e8543676c42e3b85464a25ac179dcdd1) ) /* Location U36 */
1140   ROM_LOAD( "qs9255-01_u36-ro",   0x100000, 0x080000, CRC(96bb501e) SHA1(f48ef238e8543676c42e3b85464a25ac179dcdd1) ) /* Location U36 */
11131141   ROM_RELOAD(                     0x180000, 0x080000)
1114   ROM_LOAD( "9255-01_u37-r0",     0x200000, 0x100000, CRC(273560bd) SHA1(5de8b9f5a7c4b676f131dd7d47ec71d35fa1755c) ) /* Location U37 */
1115   ROM_LOAD( "9255-20-01_u38-rof", 0x300000, 0x080000, CRC(85f48b91) SHA1(7a38644ac7ee55a254c037122af919fb268744a1) ) /* Location U38 */
1142   ROM_LOAD( "qs9255-01_u37-ro",   0x200000, 0x100000, CRC(273560bd) SHA1(5de8b9f5a7c4b676f131dd7d47ec71d35fa1755c) ) /* Location U37 */
1143   ROM_LOAD( "9255-20-06_u38-ron", 0x300000, 0x080000, CRC(f9ff003a) SHA1(6c32098593c444785de2deca0f8748042980d84d) ) /* Location U38, 02/20/1996 09:24:17 */
11161144   ROM_RELOAD(                     0x380000, 0x080000)
11171145
11181146   ROM_REGION( 0x1000, REGION_USER2, 0 ) // PALs
r2627r2628
11221150   ROM_LOAD( "sc3981-0a.u51",  0x000, 0x117, CRC(4fc750d0) SHA1(d09ff7a8c66aeb5c49e9fec84bd1521e3f5d8d0a) )
11231151ROM_END
11241152
1153ROM_START( megat3a ) /* Dallas DS1204V security key at U5 labeled 9255-20-01 U5-RO1 C1995 MII */
1154   ROM_REGION( 0x400000, REGION_CPU1, 0 )
1155   ROM_LOAD( "9255-20-01_u32-ro",  0x000000, 0x080000, CRC(ac969296) SHA1(7e09e9141637339b83c21f2488560cdf8a460069) ) /* Location U32 */
1156   ROM_RELOAD(                     0x080000, 0x080000)
1157   ROM_LOAD( "qs9255-01_u36-ro",   0x100000, 0x080000, CRC(96bb501e) SHA1(f48ef238e8543676c42e3b85464a25ac179dcdd1) ) /* Location U36 */
1158   ROM_RELOAD(                     0x180000, 0x080000)
1159   ROM_LOAD( "qs9255-01_u37-ro",   0x200000, 0x100000, CRC(273560bd) SHA1(5de8b9f5a7c4b676f131dd7d47ec71d35fa1755c) ) /* Location U37 */
1160   ROM_LOAD( "9255-20-01_u38-rof", 0x300000, 0x080000, CRC(85f48b91) SHA1(7a38644ac7ee55a254c037122af919fb268744a1) ) /* Location U38, 10/27/1995 14:23:00 */
1161   ROM_RELOAD(                     0x380000, 0x080000)
11251162
1126ROM_START( megat3te )
1163   ROM_REGION( 0x1000, REGION_USER2, 0 ) // PALs
1164   ROM_LOAD( "sc3943.u20",     0x000, 0x117, CRC(5a72fe78) SHA1(4b1a36904eb7048518507fe14bdade5c2589dbd7) )
1165   ROM_LOAD( "sc3944-0a.u19",  0x000, 0x2dd, CRC(4cc46c5e) SHA1(0bab970df1539ce905f43603ad13171b05449a01) )
1166   ROM_LOAD( "sc3980.u40",     0x000, 0x117, CRC(ee0cdab5) SHA1(216fef50a8a0f6a33b704d3501a4c5c3cbac2bad) )
1167   ROM_LOAD( "sc3981-0a.u51",  0x000, 0x117, CRC(4fc750d0) SHA1(d09ff7a8c66aeb5c49e9fec84bd1521e3f5d8d0a) )
1168ROM_END
1169
1170ROM_START( megat3te ) /* Dallas DS1204V security key at U5 labeled 9255-30-01 U5-RO1 C1995 MII */
11271171   ROM_REGION( 0x400000, REGION_CPU1, 0 )
1128   ROM_LOAD( "9255-30-01_u32-r0",  0x000000, 0x100000, CRC(31ac0004) SHA1(4bec97a852a7dadb0ab4f193bc376ed149102082) ) /* Location U32 */
1129   ROM_LOAD( "9255-01_u36-r0",     0x100000, 0x080000, CRC(96bb501e) SHA1(f48ef238e8543676c42e3b85464a25ac179dcdd1) ) /* Location U36 */
1172   ROM_LOAD( "9255-30-01_u32-ro",  0x000000, 0x100000, CRC(31ac0004) SHA1(4bec97a852a7dadb0ab4f193bc376ed149102082) ) /* Location U32 */
1173   ROM_LOAD( "qs9255-01_u36-ro",   0x100000, 0x080000, CRC(96bb501e) SHA1(f48ef238e8543676c42e3b85464a25ac179dcdd1) ) /* Location U36 */
11301174   ROM_RELOAD(                     0x180000, 0x080000)
1131   ROM_LOAD( "9255-01_u37-r0",     0x200000, 0x100000, CRC(273560bd) SHA1(5de8b9f5a7c4b676f131dd7d47ec71d35fa1755c) ) /* Location U37 */
1132   ROM_LOAD( "9255-30-0_u38-r0e",  0x300000, 0x080000, CRC(52ca7dd8) SHA1(9f44f158d67d7443405b87a18fc89d9c88be1dea) ) /* Location U38 */
1175   ROM_LOAD( "qs9255-01_u37-ro",   0x200000, 0x100000, CRC(273560bd) SHA1(5de8b9f5a7c4b676f131dd7d47ec71d35fa1755c) ) /* Location U37 */
1176   ROM_LOAD( "9255-30-01_u38-roe", 0x300000, 0x080000, CRC(52ca7dd8) SHA1(9f44f158d67d7443405b87a18fc89d9c88be1dea) ) /* Location U38, 02/15/1996 16:04:36 */
11331177   ROM_RELOAD(                     0x380000, 0x080000)
11341178
11351179   ROM_REGION( 0x1000, REGION_USER2, 0 ) // PALs
r2627r2628
11391183   ROM_LOAD( "sc3981-0a.u51",  0x000, 0x117, CRC(4fc750d0) SHA1(d09ff7a8c66aeb5c49e9fec84bd1521e3f5d8d0a) )
11401184ROM_END
11411185
1142
1143ROM_START( megat4 )
1186ROM_START( megat4 ) /* Dallas DS1204V security key at U5 labeled 9255-40-01 U5-B-RO1 C1996 MII */
11441187   ROM_REGION( 0x400000, REGION_CPU1, 0 )
11451188   ROM_LOAD( "9255-40-01_u32-ro",  0x000000, 0x100000, CRC(08b1b8fe) SHA1(c562f2e065d6d7f753f6fd1d0b8355b01cb089ec) ) /* Location U32 */
11461189   ROM_LOAD( "qs9255-02_u36-ro",   0x100000, 0x80000,  CRC(57322328) SHA1(12bc604c9d34cde431ef7cd2aa33c7b12ac01833) ) /* Location U36 */
11471190   ROM_RELOAD(                     0x180000, 0x80000)
11481191   ROM_LOAD( "qs9255-02_u37-ro",   0x200000, 0x80000,  CRC(f2e8bb4e) SHA1(5c5475b3c176a6aca9b2c6aa4aee422675d20bd1) ) /* Location U37 */
11491192   ROM_RELOAD(                     0x280000, 0x80000)
1150   ROM_LOAD( "9255-40-01_u38-roe", 0x300000, 0x80000,  CRC(407c5e57) SHA1(c7c907b3fd6a8e64dcc6c71288505980862effce) ) /* Location U38 */
1193   ROM_LOAD( "9255-40-01_u38-roe", 0x300000, 0x80000,  CRC(407c5e57) SHA1(c7c907b3fd6a8e64dcc6c71288505980862effce) ) /* Location U38, 07/22/1996 14:52:24 */
11511194   ROM_RELOAD(                     0x380000, 0x80000)
11521195
11531196   ROM_REGION( 0x1000, REGION_USER2, 0 ) // PALs
r2627r2628
11571200   ROM_LOAD( "sc3981-0a.u51",  0x000, 0x117, CRC(4fc750d0) SHA1(d09ff7a8c66aeb5c49e9fec84bd1521e3f5d8d0a) )
11581201ROM_END
11591202
1160ROM_START( megat4a )
1203ROM_START( megat4a ) /* Dallas DS1204V security key at U5 labeled 9255-40-01 U5-B-RO1 C1996 MII */
11611204   ROM_REGION( 0x400000, REGION_CPU1, 0 )
11621205   ROM_LOAD( "9255-40-01_u32-ro",  0x000000, 0x100000, CRC(08b1b8fe) SHA1(c562f2e065d6d7f753f6fd1d0b8355b01cb089ec) ) /* Location U32 */
11631206   ROM_LOAD( "qs9255-02_u36-ro",   0x100000, 0x80000,  CRC(57322328) SHA1(12bc604c9d34cde431ef7cd2aa33c7b12ac01833) ) /* Location U36 */
11641207   ROM_RELOAD(                     0x180000, 0x80000)
11651208   ROM_LOAD( "qs9255-02_u37-ro",   0x200000, 0x80000,  CRC(f2e8bb4e) SHA1(5c5475b3c176a6aca9b2c6aa4aee422675d20bd1) ) /* Location U37 */
11661209   ROM_RELOAD(                     0x280000, 0x80000)
1167   ROM_LOAD( "9255-40-01_u38-rod", 0x300000, 0x80000,  CRC(0d098424) SHA1(ef2810ccd636e69378fd353c8a95605274bb227f) ) /* Location U38 */
1210   ROM_LOAD( "9255-40-01_u38-rod", 0x300000, 0x80000,  CRC(0d098424) SHA1(ef2810ccd636e69378fd353c8a95605274bb227f) ) /* Location U38, 07/08/1996 14:16:56 */
11681211   ROM_RELOAD(                     0x380000, 0x80000)
11691212
11701213   ROM_REGION( 0x1000, REGION_USER2, 0 ) // PALs
r2627r2628
11741217   ROM_LOAD( "sc3981-0a.u51",  0x000, 0x117, CRC(4fc750d0) SHA1(d09ff7a8c66aeb5c49e9fec84bd1521e3f5d8d0a) )
11751218ROM_END
11761219
1177ROM_START( megat4te )
1220ROM_START( megat4te ) /* Dallas DS1204V security key at U5 labeled 9255-50-01 U5-B-RO1 C1996 MII */
11781221   ROM_REGION( 0x400000, REGION_CPU1, 0 )
11791222   ROM_LOAD( "9255-40-01_u32-ro",  0x000000, 0x100000, CRC(08b1b8fe) SHA1(c562f2e065d6d7f753f6fd1d0b8355b01cb089ec) ) /* Location U32 */
11801223   ROM_LOAD( "qs9255-02_u36-ro",   0x100000, 0x80000,  CRC(57322328) SHA1(12bc604c9d34cde431ef7cd2aa33c7b12ac01833) ) /* Location U36 */
11811224   ROM_RELOAD(                     0x180000, 0x80000)
11821225   ROM_LOAD( "qs9255-02_u37-ro",   0x200000, 0x80000,  CRC(f2e8bb4e) SHA1(5c5475b3c176a6aca9b2c6aa4aee422675d20bd1) ) /* Location U37 */
11831226   ROM_RELOAD(                     0x280000, 0x80000)
1184   ROM_LOAD( "9255-50-01_u38-rod", 0x300000, 0x080000, CRC(124d5b84) SHA1(3c2117f56d0dc406bfb508989729e36781e215a4) ) /* Location U38 */
1227   ROM_LOAD( "9255-50-01_u38-rod", 0x300000, 0x080000, CRC(124d5b84) SHA1(3c2117f56d0dc406bfb508989729e36781e215a4) ) /* Location U38, 07/02/1996 14:41:59 */
11851228   ROM_RELOAD(                     0x380000, 0x080000 )
11861229
11871230   ROM_REGION( 0x8000, REGION_USER1, 0 ) // DS1644 nv ram
r2627r2628
11941237   ROM_LOAD( "sc3981-0a.u51",  0x000, 0x117, CRC(4fc750d0) SHA1(d09ff7a8c66aeb5c49e9fec84bd1521e3f5d8d0a) )
11951238ROM_END
11961239
1197ROM_START( megat5 )
1240ROM_START( megat5 ) /* U32 is a bad dump, the other 3 roms look to be ok */
11981241   ROM_REGION( 0x400000, REGION_CPU1, 0 )
1199   ROM_LOAD( "megat5.u32",  0x000000, 0x80000, BAD_DUMP CRC(89932443) SHA1(68d2fbf2a5050fc5371595a105fe06f4276b0b67) )
1200   ROM_RELOAD(              0x080000, 0x80000)
1201   ROM_LOAD( "megat5.u36",  0x100000, 0x80000, BAD_DUMP CRC(0bed9e27) SHA1(1414385ce562b127e1ddeccc20ea4ff2a7098b7e) )
1202   ROM_RELOAD(              0x180000, 0x80000)
1203   ROM_LOAD( "megat5.u37",  0x200000, 0x80000, BAD_DUMP CRC(b713a1c5) SHA1(d6ccba2ea90fd0e2ecf15249514231eed54000c1) )
1204   ROM_RELOAD(              0x280000, 0x80000)
1205   ROM_LOAD( "megat5.u38",  0x300000, 0x80000, BAD_DUMP CRC(018e36c7) SHA1(8e9b457238a40b10d59887d13bac9c0a05c73614) )
1206   ROM_RELOAD(              0x380000, 0x80000)
1242   ROM_LOAD( "megat5.u32",         0x000000, 0x80000, BAD_DUMP CRC(89932443) SHA1(68d2fbf2a5050fc5371595a105fe06f4276b0b67) )
1243   ROM_RELOAD(                     0x080000, 0x80000)
1244   ROM_LOAD( "megat5.u36",         0x100000, 0x80000, BAD_DUMP CRC(0bed9e27) SHA1(1414385ce562b127e1ddeccc20ea4ff2a7098b7e) )
1245   ROM_RELOAD(                     0x180000, 0x80000)
1246   ROM_LOAD( "megat5.u37",         0x200000, 0x80000, BAD_DUMP CRC(b713a1c5) SHA1(d6ccba2ea90fd0e2ecf15249514231eed54000c1) )
1247   ROM_RELOAD(                     0x280000, 0x80000)
1248   ROM_LOAD( "9255-60-01_u38-roc", 0x300000, 0x80000, BAD_DUMP CRC(018e36c7) SHA1(8e9b457238a40b10d59887d13bac9c0a05c73614) ) /* Location U38, 07/10/1997 16:27:51 */
1249   ROM_RELOAD(                     0x380000, 0x80000)
12071250
12081251   ROM_REGION( 0x1000, REGION_USER2, 0 ) // PALs
12091252   ROM_LOAD( "sc3943.u20",     0x000, 0x117, CRC(5a72fe78) SHA1(4b1a36904eb7048518507fe14bdade5c2589dbd7) )
r2627r2628
12121255   ROM_LOAD( "sc3981-0a.u51",  0x000, 0x117, CRC(4fc750d0) SHA1(d09ff7a8c66aeb5c49e9fec84bd1521e3f5d8d0a) )
12131256ROM_END
12141257
1215ROM_START( megat6 )
1258ROM_START( megat6 ) /* Dallas DS1204V security key at U5 labeled 9255-80 U5-B-RO1 C1998 MII */
12161259   ROM_REGION( 0x400000, REGION_CPU1, 0 )
12171260   ROM_LOAD( "9255-60-01_u32-ro",  0x000000, 0x100000, CRC(f8f7f48e) SHA1(1bebe1f8898c60b795a0f794ca9b79e03d2744e4) ) /* Location U32 */
12181261   ROM_LOAD( "qs9255-08_u36-ro",   0x100000, 0x080000, CRC(800f5a1f) SHA1(4d3ee6fb896d6452aab1f279a3ee878284bd1acc) ) /* Location U36 */
12191262   ROM_RELOAD(                     0x180000, 0x080000 )
12201263   ROM_LOAD( "qs9255-08_u37-ro",   0x200000, 0x100000, CRC(5ba01949) SHA1(1598949ea18d07bbc78af0ddd279a687173c1229) ) /* Location U37 */
1221   ROM_LOAD( "9255-80-01_u38-roa", 0x300000, 0x100000, CRC(3df6b840) SHA1(31ba1ac04eed3e76cdf637507dedcc5f7e22c919) ) /* Location U38 */
1264   ROM_LOAD( "9255-80-01_u38-roa", 0x300000, 0x100000, CRC(3df6b840) SHA1(31ba1ac04eed3e76cdf637507dedcc5f7e22c919) ) /* Location U38, 08/07/1998 15:54:23 */
12221265
12231266   ROM_REGION( 0x8000, REGION_USER1, 0 ) // DS1230 nv ram
12241267   ROM_LOAD( "ds1230y.u31",  0x00000, 0x8000, CRC(51b6da5c) SHA1(1d53af89d7867bb48b9d46feff6fc3b7e8e80ac8) )
r2627r2628
12421285
12431286};
12441287
1245
12461288static DRIVER_INIT(megat3te)
12471289{
12481290   static const UINT8 megat3_ds1204_key[8] =
r2627r2628
12571299
12581300};
12591301
1260
12611302static DRIVER_INIT(megat4)
12621303{
12631304   static const UINT8 megat4_ds1204_nvram[16] =
r2627r2628
12871328}
12881329
12891330/* CRT 250 */
1290GAME( 1988, pitboss2,  0,    meritm_crt250, meritm_crt250, 0, ROT0, "Merit", "Pit Boss II", GAME_IMPERFECT_GRAPHICS )
1291GAME( 1988, spitboss,  0,    meritm_crt250, meritm_crt250, 0, ROT0, "Merit", "Super Pit Boss", GAME_IMPERFECT_GRAPHICS )
1331GAME( 1988, pitboss2,  0,      meritm_crt250, meritm_crt250, 0, ROT0, "Merit", "Pit Boss II", GAME_IMPERFECT_GRAPHICS )
1332GAME( 1988, spitboss,  0,      meritm_crt250, meritm_crt250, 0, ROT0, "Merit", "Super Pit Boss", GAME_IMPERFECT_GRAPHICS )
1333GAME( 1990, pitbosss,  0,      meritm_crt250, meritm_crt250, 0, ROT0, "Merit", "Pit Boss Superstar", GAME_IMPERFECT_GRAPHICS )
12921334
12931335/* CRT 250 + question roms */
12941336GAME( 1994, pitbossm,  0,      meritm_crt250_questions, pitbossm, 0, ROT0, "Merit", "Pit Boss Megastar", GAME_IMPERFECT_GRAPHICS )
12951337
12961338/* CRT 260 */
1297GAME( 1995, megat3,    0,      meritm_crt260, meritm_crt260, megat3,   ROT0, "Merit", "Megatouch III (9255-20-01 ROF)", GAME_IMPERFECT_GRAPHICS )
1339GAME( 1996, megat3,    0,      meritm_crt260, meritm_crt260, megat3,   ROT0, "Merit", "Megatouch III (9255-20-06 RON)", GAME_IMPERFECT_GRAPHICS )
1340GAME( 1995, megat3a,   megat3, meritm_crt260, meritm_crt260, megat3,   ROT0, "Merit", "Megatouch III (9255-20-01 ROF)", GAME_IMPERFECT_GRAPHICS )
12981341GAME( 1996, megat3te,  megat3, meritm_crt260, meritm_crt260, megat3te, ROT0, "Merit", "Megatouch III Tournament Edition (9255-30-01 ROE)", GAME_IMPERFECT_GRAPHICS )
12991342GAME( 1996, megat4,    0,      meritm_crt260, meritm_crt260, megat4,   ROT0, "Merit", "Megatouch IV (9255-40-01 ROE)", GAME_IMPERFECT_GRAPHICS )
13001343GAME( 1996, megat4a,   megat4, meritm_crt260, meritm_crt260, megat4,   ROT0, "Merit", "Megatouch IV (9255-40-01 ROD)", GAME_IMPERFECT_GRAPHICS )
13011344GAME( 1996, megat4te,  megat4, meritm_crt260, meritm_crt260, megat4te, ROT0, "Merit", "Megatouch IV Tournament Edition (9255-50-01 ROD)", GAME_IMPERFECT_GRAPHICS )
1302GAME( 1997, megat5,    0,      meritm_crt260, meritm_crt260, 0,          ROT0, "Merit", "Megatouch 5", GAME_IMPERFECT_GRAPHICS|GAME_NOT_WORKING )
1345GAME( 1997, megat5,    0,      meritm_crt260, meritm_crt260, 0,          ROT0, "Merit", "Megatouch 5 (9255-60-01 ROC)", GAME_IMPERFECT_GRAPHICS|GAME_NOT_WORKING )
13031346GAME( 1998, megat6,    0,      meritm_crt260, meritm_crt260, megat6,   ROT0, "Merit", "Megatouch 6 (9255-80-01 ROA)", GAME_IMPERFECT_GRAPHICS )
src/mame/drivers/missile.c
r2627r2628
365365   /* RAM? */
366366   if (address < 0x4000)
367367   {
368      opcode_base = opcode_arg_base = videoram - offset;
368      opbase->rom = opbase->ram = videoram - offset;
369369      return ~0;
370370   }
371371
372372   /* ROM? */
373373   else if (address >= 0x5000)
374374   {
375      opcode_base = opcode_arg_base = memory_region(REGION_CPU1) - offset;
375      opbase->rom = opbase->ram = memory_region(REGION_CPU1) - offset;
376376      return ~0;
377377   }
378378
r2627r2628
389389
390390   /* set up an opcode base handler since we use mapped handlers for RAM */
391391   memory_set_opbase_handler(0, missile_opbase_handler);
392   opcode_base = opcode_arg_base = videoram;
393392
394393   /* create a timer to speed/slow the CPU */
395394   cpu_timer = timer_alloc(adjust_cpu_speed, NULL);
src/mame/drivers/funworld.c
r2627r2628
115115
116116            B) General encryption. Managed through hardware:
117117
118                - Jolly Card (italian, blue TAB board, encrypted) & Magic Card II (Impera, Blue TAB board)
119                  use substitution for each byte nibble. See DRIVER_INIT for the algorithm.
118                - All games using the blue TAB PCB with 2x HY18CV85 (electrically-erasable PLDs), use
119                  complex operations for each byte nibble. See DRIVER_INIT for the final algorithm.
120120                - Jolly Card (austrian, encrypted) use simple XOR with a fixed value.
121121
122122    - Microcontroller. Some games are using an extra microcontroller mainly for protection.
r2627r2628
859859    - Documented the featured SUPER GAME with complete instructions.
860860    - Improved DIP switches to properly set the payout system.
861861
862    [2008/05/13]
863    - Found the proper algorithm to decrypt the blue TAB PCB.
864    - Replaced the old decryption tables with the proper decryption scheme.
865    - Updated technical notes.
862866
867
863868    *** TO DO ***
864869
865870    - Figure out the royalcdc, jokercrd and saloon encryption.
r2627r2628
23052310
23062311static DRIVER_INIT( tabblue )
23072312{
2308/*****************************************************************************
2309   +----------------------------------------------------------------------+
2310   | For TAB blue colored PCB with 2 HY18CV85 (electrically-erasable PLD) |
2311   +----------------------------------------------------------------------+
2313/****************************************************************************************************
23122314
2313    The encryption seems to be...
2315   +-------------------------+
2316   | Blue TAB PCB Decryption |
2317   +-------------------------+
23142318
2315    For each nibble:
2319    It perform by byte nibble a boolean XOR against the same value shifted to the right, then shift
2320    the result to the left carring the less significant bit and losing the most significant one.
23162321
2317    xe = (3 * xd) & 0f  ; where xe = encrypted value, xd = decrypted value.
23182322
2319    and then 3 swapped pairs... (3,7) (b,f) (6,e)
2323    Encrypted nibble:  0    1    2    3    4    5    6    7    8    9    A    B    C    D    E    F
2324    Bits:             0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
2325    -------------------------------------------------------------------------------------------------
2326    Bits:             0000 0011 0110 0101 1100 1111 1010 1001 1000 1011 1110 1101 0100 0111 0010 0001
2327    Decrypted nibble:  0    3    6    5    C    F    A    9    8    B    E    D    4    7    2    1
23202328
2321    ...or cases (3,7,b,f) ^ 4
2322    ...and cases (6,e) ^ 8
23232329
2324    to get the same result.
2330*****************************************************************************************************/
23252331
2326    Since the table is very short, we will substitute each nibble.
2327
2328*****************************************************************************/
2329
23302332   int x, na, nb, nad, nbd;
23312333   UINT8 *src = memory_region( REGION_GFX1 );
23322334
2333   for (x=0x0000;x<0x10000;x++)
2335
2336   for (x=0x0000; x < 0x10000; x++)
23342337   {
2335      na = src[x] & 0xf0;   /* nibble a */
2336      nb = src[x] & 0x0f;   /* nibble b */
2338      na = src[x] & 0xf0;      /* nibble A */
2339      nb = src[x] << 4;      /* nibble B */
23372340
2338      switch (na)
2339      {
2340         case 0x10: nad = 0x30; break;
2341         case 0x20: nad = 0x60; break;
2342         case 0x30: nad = 0x50; break;
2343         case 0x40: nad = 0xc0; break;
2344         case 0x50: nad = 0xf0; break;
2345         case 0x60: nad = 0xa0; break;
2346         case 0x70: nad = 0x90; break;
2347         case 0x90: nad = 0xb0; break;
2348         case 0xa0: nad = 0xe0; break;
2349         case 0xb0: nad = 0xd0; break;
2350         case 0xc0: nad = 0x40; break;
2351         case 0xd0: nad = 0x70; break;
2352         case 0xe0: nad = 0x20; break;
2353         case 0xf0: nad = 0x10; break;
2354         default: nad = na; break;
2355      }
2341         nad = (na ^ (na >> 1)) << 1;         /* nibble A decrypted */
2342         nbd = ((nb ^ (nb >> 1)) >> 3) & 0x0f;   /* nibble B decrypted */
23562343
2357      switch (nb)
2358      {
2359         case 0x01: nbd = 0x03; break;
2360         case 0x02: nbd = 0x06; break;
2361         case 0x03: nbd = 0x05; break;
2362         case 0x04: nbd = 0x0c; break;
2363         case 0x05: nbd = 0x0f; break;
2364         case 0x06: nbd = 0x0a; break;
2365         case 0x07: nbd = 0x09; break;
2366         case 0x09: nbd = 0x0b; break;
2367         case 0x0a: nbd = 0x0e; break;
2368         case 0x0b: nbd = 0x0d; break;
2369         case 0x0c: nbd = 0x04; break;
2370         case 0x0d: nbd = 0x07; break;
2371         case 0x0e: nbd = 0x02; break;
2372         case 0x0f: nbd = 0x01; break;
2373         default: nbd = nb; break;
2374      }
2375
2376      src[x] = nad + nbd;   /* decrypted value */
2344      src[x] = nad + nbd;      /* decrypted byte */
23772345   }
23782346
23792347   /* Initializing PIAs... */
r2627r2628
24552423}
24562424
24572425static DRIVER_INIT( magiccdb )
2458/*** same as blue TAB pcb, with the magiccda patch ***/
2426/*** same as blue TAB PCB, with the magiccda patch ***/
24592427{
24602428   int x, na, nb, nad, nbd;
24612429   UINT8 *src = memory_region( REGION_GFX1 );
2462   UINT8 *ROM = memory_region( REGION_CPU1 );
2430   UINT8 *ROM = memory_region(REGION_CPU1);
24632431
2464   for (x=0x0000;x<0x10000;x++)
2432   for (x=0x0000; x < 0x10000; x++)
24652433   {
2466      na = src[x] & 0xf0;   /* nibble a */
2467      nb = src[x] & 0x0f;   /* nibble b */
2434      na = src[x] & 0xf0;      /* nibble A */
2435      nb = src[x] << 4;      /* nibble B */
24682436
2469      switch (na)
2470      {
2471         case 0x10: nad = 0x30; break;
2472         case 0x20: nad = 0x60; break;
2473         case 0x30: nad = 0x50; break;
2474         case 0x40: nad = 0xc0; break;
2475         case 0x50: nad = 0xf0; break;
2476         case 0x60: nad = 0xa0; break;
2477         case 0x70: nad = 0x90; break;
2478         case 0x90: nad = 0xb0; break;
2479         case 0xa0: nad = 0xe0; break;
2480         case 0xb0: nad = 0xd0; break;
2481         case 0xc0: nad = 0x40; break;
2482         case 0xd0: nad = 0x70; break;
2483         case 0xe0: nad = 0x20; break;
2484         case 0xf0: nad = 0x10; break;
2485         default: nad = na; break;
2486      }
2437         nad = (na ^ (na >> 1)) << 1;         /* nibble A decrypted */
2438         nbd = ((nb ^ (nb >> 1)) >> 3) & 0x0f;   /* nibble B decrypted */
24872439
2488      switch (nb)
2489      {
2490         case 0x01: nbd = 0x03; break;
2491         case 0x02: nbd = 0x06; break;
2492         case 0x03: nbd = 0x05; break;
2493         case 0x04: nbd = 0x0c; break;
2494         case 0x05: nbd = 0x0f; break;
2495         case 0x06: nbd = 0x0a; break;
2496         case 0x07: nbd = 0x09; break;
2497         case 0x09: nbd = 0x0b; break;
2498         case 0x0a: nbd = 0x0e; break;
2499         case 0x0b: nbd = 0x0d; break;
2500         case 0x0c: nbd = 0x04; break;
2501         case 0x0d: nbd = 0x07; break;
2502         case 0x0e: nbd = 0x02; break;
2503         case 0x0f: nbd = 0x01; break;
2504         default: nbd = nb; break;
2505      }
2506
2507      src[x] = nad + nbd;   /* decrypted value */
2440      src[x] = nad + nbd;      /* decrypted byte */
25082441   }
25092442
25102443   ROM[0xc1c6] = 0x92;
src/mame/drivers/spaceg.c
r2627r2628
235235   unkram  [offset] = col;
236236}
237237
238static READ8_HANDLER(fake_r)
239{
240//  logerror("vb %04x\n", activecpu_get_pc() );
241   return 0;
242}
243238
244
245239static READ8_HANDLER(spaceg_colorram_r)
246240{
247241
r2627r2628
305299   AM_RANGE(0x0000, 0x1fff) AM_ROM
306300   AM_RANGE(0x3000, 0x3fff) AM_ROM
307301   AM_RANGE(0x7000, 0x77ff) AM_RAM
302
303   AM_RANGE(0xa000, 0xbfff) AM_READWRITE(spaceg_colorram_r, SMH_RAM) AM_BASE(&unkram)
308304   AM_RANGE(0xc000, 0xdfff) AM_RAM_WRITE(zvideoram_w) AM_BASE(&videoram)
309   AM_RANGE(0xa000, 0xbfff) AM_READWRITE(spaceg_colorram_r, SMH_RAM) AM_BASE(&unkram)
310305
311
312   AM_RANGE(0x9400, 0x9400) AM_WRITE(SMH_RAM) AM_BASE(&io9400)    /* gfx ctrl */
306   AM_RANGE(0x9400, 0x9400) AM_WRITE(SMH_RAM) AM_BASE(&io9400) /* gfx ctrl */
313307   AM_RANGE(0x9401, 0x9401) AM_WRITE(SMH_RAM) AM_BASE(&io9401)   /* gfx ctrl */
314308   /* 9402 -
315309        bits 0 and 1 probably control the lamps under the player 1 and player 2 start buttons
310        bit 2 - unknown -
316311        bit 3 is probably a flip screen
317312        bit 7 - unknown - set to 1 during the gameplay (coinlock ?)
318313    */
319   AM_RANGE(0x9402, 0x9407) AM_RAM
314   AM_RANGE(0x9402, 0x9407) AM_RAM     /* surely wrong */
320315
321   AM_RANGE(0x9800, 0x9800) AM_READ(fake_r) //watchdog read ? nmi mask/ack ?
322   AM_RANGE(0x9801, 0x9801) AM_READ(input_port_0_r)
323   AM_RANGE(0x9802, 0x9802) AM_READ(input_port_1_r)
324   AM_RANGE(0x9805, 0x9805) AM_READ(input_port_2_r) /* pl 1 */
325   AM_RANGE(0x9806, 0x9806) AM_READ(input_port_3_r) /* pl 2 */
316   AM_RANGE(0x9800, 0x9800) AM_READ_PORT("9800")
317   AM_RANGE(0x9801, 0x9801) AM_READ_PORT("9801")
318   AM_RANGE(0x9802, 0x9802) AM_READ_PORT("9802")
319   AM_RANGE(0x9805, 0x9805) AM_READ_PORT("9805")
320   AM_RANGE(0x9806, 0x9806) AM_READ_PORT("9806")
326321ADDRESS_MAP_END
327322
328323
329324
330325static INPUT_PORTS_START( spaceg )
331   PORT_START
326   PORT_START_TAG("9800")
327   PORT_DIPUNUSED(  0x01, IP_ACTIVE_HIGH )                 /* was related to coinage */
328   PORT_DIPUNKNOWN( 0x02, IP_ACTIVE_HIGH )                 /* check code at 0x127d - when bases are supposed to disappear */
329   PORT_DIPNAME( 0x0c, 0x00, DEF_STR( Lives ) )
330   PORT_DIPSETTING(    0x00, "3" )
331   PORT_DIPSETTING(    0x04, "4" )
332   PORT_DIPSETTING(    0x08, "5" )
333   PORT_DIPSETTING(    0x0c, "6" )
334   PORT_DIPNAME( 0x30, 0x00, DEF_STR( Bonus_Life ) )
335   PORT_DIPSETTING(    0x00, "10000" )
336   PORT_DIPSETTING(    0x20, "30000" )
337   PORT_DIPSETTING(    0x10, "40000" )
338   PORT_DIPSETTING(    0x30, "50000" )
339   PORT_DIPNAME( 0xc0, 0x00, DEF_STR( Unknown ) )          /* set bit 2 of 0x9402 depending on score - previously 2nd bonus life ? */
340   PORT_DIPSETTING(    0x00, "10000" )
341   PORT_DIPSETTING(    0x80, "30000" )
342   PORT_DIPSETTING(    0x40, "40000" )
343   PORT_DIPSETTING(    0xc0, "50000" )
344
345   PORT_START_TAG("9801")
332346   PORT_BIT( 0x01, IP_ACTIVE_HIGH, IPT_COIN1 )
333   PORT_BIT( 0x04, IP_ACTIVE_HIGH, IPT_BUTTON1 )
347   PORT_BIT( 0x04, IP_ACTIVE_HIGH, IPT_BUTTON1 ) PORT_PLAYER(1)
334348
335   PORT_START
349   PORT_START_TAG("9802")
350   PORT_BIT( 0x04, IP_ACTIVE_HIGH, IPT_BUTTON1 ) PORT_PLAYER(2)
336351   PORT_BIT( 0x08, IP_ACTIVE_HIGH, IPT_START1 )
337352   PORT_BIT( 0x10, IP_ACTIVE_HIGH, IPT_START2 )
338353
339   PORT_START /* player 1 */
340   PORT_BIT( 0x01, IP_ACTIVE_HIGH, IPT_JOYSTICK_LEFT )
341   PORT_BIT( 0x02, IP_ACTIVE_HIGH, IPT_JOYSTICK_RIGHT )
342   PORT_BIT( 0x04, IP_ACTIVE_HIGH, IPT_BUTTON1 )
343   PORT_BIT( 0x08, IP_ACTIVE_HIGH, IPT_BUTTON2 )
344   PORT_BIT( 0x10, IP_ACTIVE_HIGH, IPT_BUTTON4 )
345   PORT_BIT( 0x20, IP_ACTIVE_HIGH, IPT_JOYSTICK_UP )
346   PORT_BIT( 0x40, IP_ACTIVE_HIGH, IPT_JOYSTICK_DOWN )
347   PORT_BIT( 0x80, IP_ACTIVE_HIGH, IPT_BUTTON3 )
354   PORT_START_TAG("9805")    /* player 1 */
355   PORT_BIT( 0x01, IP_ACTIVE_HIGH, IPT_JOYSTICK_LEFT  ) PORT_8WAY PORT_PLAYER(1)
356   PORT_BIT( 0x02, IP_ACTIVE_HIGH, IPT_JOYSTICK_RIGHT ) PORT_8WAY PORT_PLAYER(1)
348357
349   PORT_START /* player 2 */
350   PORT_BIT( 0x01, IP_ACTIVE_HIGH, IPT_JOYSTICK_LEFT )
351   PORT_BIT( 0x02, IP_ACTIVE_HIGH, IPT_JOYSTICK_RIGHT )
352   PORT_BIT( 0x04, IP_ACTIVE_HIGH, IPT_BUTTON1 )
353   PORT_BIT( 0x08, IP_ACTIVE_HIGH, IPT_BUTTON2 )
354   PORT_BIT( 0x10, IP_ACTIVE_HIGH, IPT_BUTTON4 )
355   PORT_BIT( 0x20, IP_ACTIVE_HIGH, IPT_JOYSTICK_UP )
356   PORT_BIT( 0x40, IP_ACTIVE_HIGH, IPT_JOYSTICK_DOWN )
357   PORT_BIT( 0x80, IP_ACTIVE_HIGH, IPT_BUTTON3 )
358
359
358   PORT_START_TAG("9806")    /* player 2 */
359   PORT_BIT( 0x01, IP_ACTIVE_HIGH, IPT_JOYSTICK_LEFT  ) PORT_8WAY PORT_PLAYER(2)
360   PORT_BIT( 0x02, IP_ACTIVE_HIGH, IPT_JOYSTICK_RIGHT ) PORT_8WAY PORT_PLAYER(2)
360361INPUT_PORTS_END
361362
362363
r2627r2628
404405
405406ROM_START( spaceg )
406407   ROM_REGION( 0x10000, REGION_CPU1, 0 )
407   ROM_LOAD( "1.9a",      0x0000, 0x0400, CRC(d6f80b0e) SHA1(503e832c065bb302ec60ed239c4d99a605cb931a) )
408   ROM_LOAD( "2.8a",      0x0400, 0x0400, CRC(708b8eec) SHA1(70f9b1506a653985d9d3eacba220f0a4eb241318) )
409   ROM_LOAD( "3.9b",      0x0800, 0x0400, CRC(7f0b5cb4) SHA1(97a7125d046e7191b5d3be8f203d1bbb6f988209) )
410   ROM_LOAD( "4.8b",      0x0c00, 0x0400, CRC(8b40a154) SHA1(f42bdd8e94090fc5eae58e833b8443300d3ce991) )
411   ROM_LOAD( "5.9c",      0x1000, 0x0400, CRC(5279241c) SHA1(7278b6b037322b2f75311ed247f2de3c4816681b) )
412   ROM_LOAD( "6.8c",      0x1400, 0x0400, CRC(9b84fe3a) SHA1(7ebeca10ee11d22f4af06be9f381f46864464ec2) )
413   ROM_LOAD( "7.9d",      0x1800, 0x0400, CRC(95279b25) SHA1(367d129d4dd2cfea2a2f4703f41f24cc49453715) )
414   ROM_LOAD( "8.8d",      0x1c00, 0x0400, CRC(6a824383) SHA1(7c43f2c7d1f070d93f6a8b5b4f7f97f3578bd91d) )
415   ROM_LOAD( "13.9g",   0x3000, 0x0400, CRC(dccc386f) SHA1(5d493da3e7b8269314dd54f0b3ba9f71829a14da) )
416   ROM_LOAD( "14.8g",   0x3400, 0x0400, CRC(dc9a10c2) SHA1(8fb2316d6e8aeef558d0da5029e2932abf47a6b4) )
417   ROM_LOAD( "15.9h",   0x3800, 0x0400, CRC(55e2950d) SHA1(2241c3620c9a6df8b8bd234ccee9af5d3d19a5d4) )
418   ROM_LOAD( "16.8h",   0x3c00, 0x0400, CRC(567259c4) SHA1(b2c3f7aaceabea075af6a43b89fb7331732278c8) )
408   ROM_LOAD( "1.9a",  0x0000, 0x0400, CRC(d6f80b0e) SHA1(503e832c065bb302ec60ed239c4d99a605cb931a) )
409   ROM_LOAD( "2.8a",  0x0400, 0x0400, CRC(708b8eec) SHA1(70f9b1506a653985d9d3eacba220f0a4eb241318) )
410   ROM_LOAD( "3.9b",  0x0800, 0x0400, CRC(7f0b5cb4) SHA1(97a7125d046e7191b5d3be8f203d1bbb6f988209) )
411   ROM_LOAD( "4.8b",  0x0c00, 0x0400, CRC(8b40a154) SHA1(f42bdd8e94090fc5eae58e833b8443300d3ce991) )
412   ROM_LOAD( "5.9c",  0x1000, 0x0400, CRC(5279241c) SHA1(7278b6b037322b2f75311ed247f2de3c4816681b) )
413   ROM_LOAD( "6.8c",  0x1400, 0x0400, CRC(9b84fe3a) SHA1(7ebeca10ee11d22f4af06be9f381f46864464ec2) )
414   ROM_LOAD( "7.9d",  0x1800, 0x0400, CRC(95279b25) SHA1(367d129d4dd2cfea2a2f4703f41f24cc49453715) )
415   ROM_LOAD( "8.8d",  0x1c00, 0x0400, CRC(6a824383) SHA1(7c43f2c7d1f070d93f6a8b5b4f7f97f3578bd91d) )
416   ROM_LOAD( "13.9g", 0x3000, 0x0400, CRC(dccc386f) SHA1(5d493da3e7b8269314dd54f0b3ba9f71829a14da) )
417   ROM_LOAD( "14.8g", 0x3400, 0x0400, CRC(dc9a10c2) SHA1(8fb2316d6e8aeef558d0da5029e2932abf47a6b4) )
418   ROM_LOAD( "15.9h", 0x3800, 0x0400, CRC(55e2950d) SHA1(2241c3620c9a6df8b8bd234ccee9af5d3d19a5d4) )
419   ROM_LOAD( "16.8h", 0x3c00, 0x0400, CRC(567259c4) SHA1(b2c3f7aaceabea075af6a43b89fb7331732278c8) )
419420ROM_END
420421
421422
422GAME( 1979, spaceg, 0, spaceg, spaceg, 0, ROT270, "Omori", "Space Guerrilla", GAME_IMPERFECT_GRAPHICS | GAME_NO_SOUND)
423GAME( 1979, spaceg, 0, spaceg, spaceg, 0, ROT270, "Omori", "Space Guerrilla", GAME_IMPERFECT_GRAPHICS | GAME_NO_SOUND )
src/mame/drivers/djmain.c
r2627r2628
12881288   ROM_LOAD16_BYTE( "753jaa10.27d", 0x100001, 0x80000, CRC(391F4BFD) SHA1(791c9889ea3ce639bbfb87934a1cad9aa3c9ccde) )
12891289
12901290   DISK_REGION( REGION_DISKS )         /* IDE HARD DRIVE */
1291   DISK_IMAGE( "753jaa11", 0, MD5(d56ec7b9877d1f26d7fc1cabed404947) SHA1(71d200d1bd3f1f3a01f4daa78dc9abcca8b8a1fb) )   /* ver 1.00 JA */
12911292   // There is an alternate image
12921293   //DISK_IMAGE( "753jaa11", 0, MD5(260c9b72f4a03055e3abad61c6225324) SHA1(2cc3e149744516bf2353a2b47d33bc9d2072b6c4) ) /* ver 1.00 JA */
1293   DISK_IMAGE( "753jaa11", 0, MD5(d56ec7b9877d1f26d7fc1cabed404947) SHA1(71d200d1bd3f1f3a01f4daa78dc9abcca8b8a1fb) )   /* ver 1.00 JA */
12941294ROM_END
12951295
12961296ROM_START( bm2ndmix )
r2627r2628
13351335   DISK_IMAGE( "853jaa11", 0, MD5(37281741b748bea7dfa711a956649d1e) SHA1(03d6cc5aea5920163fbaba34c4f838ca605a87e3) )   /* ver 1.10 JA */
13361336ROM_END
13371337
1338
1339
13401338ROM_START( bm3rdmix )
13411339   ROM_REGION( 0x100000, REGION_CPU1, 0 )      /* MC68EC020FG25 MPU */
13421340   ROM_LOAD16_BYTE( "825jaa01.6a", 0x000000, 0x80000, CRC(CF7494A5) SHA1(994df0644817f44d135a16f04d8dae9ec73e3728) )
r2627r2628
13561354
13571355
13581356   DISK_REGION( REGION_DISKS )         /* IDE HARD DRIVE */
1359   DISK_IMAGE( "825jaa", 0, SHA1(dc10d8d281dd8153aeb620228e397aba0bbac198) MD5(3276e3ed57f1a6d9a18365054b0439ba) ) /* ver 1.00 JA */
1357   DISK_IMAGE( "825jaa11", 0, SHA1(dc10d8d281dd8153aeb620228e397aba0bbac198) MD5(3276e3ed57f1a6d9a18365054b0439ba) )   /* ver 1.00 JA */
13601358ROM_END
13611359
13621360ROM_START( bmcompmx )
r2627r2628
14631461   DISK_REGION( REGION_DISKS )         /* IDE HARD DRIVE */
14641462   DISK_IMAGE( "993jaa11.chd", 0, NO_DUMP )
14651463   // this image has not been verified
1466//  DISK_IMAGE( "993jaa11.chd", 0, MD5(e26eb62d7cf3357585f5066da6063143) )  /* ver 1.00 JA */
1464   //  DISK_IMAGE( "993jaa11.chd", 0, MD5(e26eb62d7cf3357585f5066da6063143) )  /* ver 1.00 JA */
14671465ROM_END
14681466
14691467ROM_START( bmcompm2 )
r2627r2628
15891587   ROM_LOAD16_BYTE( "b07jaa10.27d", 0x100001, 0x80000, CRC(6B75BA9C) SHA1(aee922adc3bc0296ae6e08e461b20a9e5e72a2df) )
15901588
15911589   DISK_REGION( REGION_DISKS )         /* IDE HARD DRIVE */
1592   DISK_IMAGE( "b07jab11.chd", 0, NO_DUMP )
1590   DISK_IMAGE( "b07jaa11", 0, MD5(8ee59f074ee79eb7a435a54b6745387c) SHA1(91957d353d2978ce46c5232dd182c056206eb722) )   /* ver 1.00 JA */
15931591   // this image has not been verified
15941592   //DISK_IMAGE( "b07jab11.chd", 0, MD5(0e9440787ca69567792095085e2a3619) )    /* ver 1.00 JA */
15951593ROM_END
15961594
1595ROM_START( bmfinal )
1596   ROM_REGION( 0x100000, REGION_CPU1, 0 )      /* MC68EC020FG25 MPU */
1597   ROM_LOAD16_BYTE( "c01jaa01.6a", 0x000000, 0x80000, CRC(A64EEFF7) SHA1(377eee1f41e3072f9154a7c17ec4c4f3fb63ea4a) )
1598   ROM_LOAD16_BYTE( "c01jaa02.8a", 0x000001, 0x80000, CRC(599BDAC5) SHA1(f85aff020c92fcd3c2a42036615226b54e5bee98) )
1599
1600   ROM_REGION( 0x200000, REGION_GFX1, 0)      /* SPRITE */
1601   ROM_LOAD16_BYTE( "c01jaa03.19a", 0x000000, 0x80000, CRC(1C9C6EB7) SHA1(bd1a9d8ed78095328817f599f52d9d34e09e9275) )
1602   ROM_LOAD16_BYTE( "c01jaa04.20a", 0x000001, 0x80000, CRC(4E5AA665) SHA1(22f3888a29497ff0a801cce620ca0373268e5cd9) )
1603   ROM_LOAD16_BYTE( "c01jaa05.22a", 0x100000, 0x80000, CRC(37DAB217) SHA1(66b07c36e7749a4c9d9dfaca633958a4922c4562) )
1604   ROM_LOAD16_BYTE( "c01jaa06.24a", 0x100001, 0x80000, CRC(D35C6818) SHA1(ce608603ea3662f8cda5cf958a676d64a0f74645) )
1605
1606   ROM_REGION( 0x200000, REGION_GFX2, 0 )      /* TILEMAP */
1607   ROM_LOAD16_BYTE( "c01jaa07.22d", 0x000000, 0x80000, CRC(3E70F506) SHA1(d3cd0b48383bf2514b7f47fade8549ea8e3c5555) )
1608   ROM_LOAD16_BYTE( "c01jaa08.23d", 0x000001, 0x80000, CRC(535E6065) SHA1(131f7eec4179145781bbd23474202f4eaf9cefd0) )
1609   ROM_LOAD16_BYTE( "c01jaa09.25d", 0x100000, 0x80000, CRC(45CF93B1) SHA1(7c5082bcd1fe15761a0a965e25dda121904ff1bd) )
1610   ROM_LOAD16_BYTE( "c01jaa10.27d", 0x100001, 0x80000, CRC(C9927749) SHA1(c2644877bda483e241381265e723ea8ab8357761) )
1611
1612   DISK_REGION( REGION_DISKS )         /* IDE HARD DRIVE */
1613   DISK_IMAGE( "c01jaa11", 0, NO_DUMP )
1614   // this image has not been verified
1615   //DISK_IMAGE( "c01jaa11", 0, MD5(8bb7e6b6bc63cac8a4f2997307c25748) )    /* ver 1.00 JA */
1616ROM_END
1617
15971618#if 0
15981619// for reference, these sets have not been verified
15991620ROM_START( bm3rdmxb )
r2627r2628
16171638   DISK_IMAGE( "825jab11", 0, MD5(f4360da10a932ba90e93469df7426d1d) SHA1(1) )  /* ver 1.01 JA */
16181639ROM_END
16191640
1620ROM_START( bmfinal )
1621   ROM_REGION( 0x100000, REGION_CPU1, 0 )      /* MC68EC020FG25 MPU */
1622   ROM_LOAD16_BYTE( "c01jaa01.6a", 0x000000, 0x80000, CRC(A64EEFF7) SHA1(377eee1f41e3072f9154a7c17ec4c4f3fb63ea4a) )
1623   ROM_LOAD16_BYTE( "c01jaa02.8a", 0x000001, 0x80000, CRC(599BDAC5) SHA1(f85aff020c92fcd3c2a42036615226b54e5bee98) )
1624
1625   ROM_REGION( 0x200000, REGION_GFX1, 0)      /* SPRITE */
1626   ROM_LOAD16_BYTE( "c01jaa03.19a", 0x000000, 0x80000, CRC(1C9C6EB7) SHA1(bd1a9d8ed78095328817f599f52d9d34e09e9275) )
1627   ROM_LOAD16_BYTE( "c01jaa04.20a", 0x000001, 0x80000, CRC(4E5AA665) SHA1(22f3888a29497ff0a801cce620ca0373268e5cd9) )
1628   ROM_LOAD16_BYTE( "c01jaa05.22a", 0x100000, 0x80000, CRC(37DAB217) SHA1(66b07c36e7749a4c9d9dfaca633958a4922c4562) )
1629   ROM_LOAD16_BYTE( "c01jaa06.24a", 0x100001, 0x80000, CRC(D35C6818) SHA1(ce608603ea3662f8cda5cf958a676d64a0f74645) )
1630
1631   ROM_REGION( 0x200000, REGION_GFX2, 0 )      /* TILEMAP */
1632   ROM_LOAD16_BYTE( "c01jaa07.22d", 0x000000, 0x80000, CRC(3E70F506) SHA1(d3cd0b48383bf2514b7f47fade8549ea8e3c5555) )
1633   ROM_LOAD16_BYTE( "c01jaa08.23d", 0x000001, 0x80000, CRC(535E6065) SHA1(131f7eec4179145781bbd23474202f4eaf9cefd0) )
1634   ROM_LOAD16_BYTE( "c01jaa09.25d", 0x100000, 0x80000, CRC(45CF93B1) SHA1(7c5082bcd1fe15761a0a965e25dda121904ff1bd) )
1635   ROM_LOAD16_BYTE( "c01jaa10.27d", 0x100001, 0x80000, CRC(C9927749) SHA1(c2644877bda483e241381265e723ea8ab8357761) )
1636
1637   DISK_REGION( REGION_DISKS )         /* IDE HARD DRIVE */
1638   DISK_IMAGE( "c01jaa11.chd", 0, MD5(8bb7e6b6bc63cac8a4f2997307c25748) )   /* ver 1.00 JA */
1639ROM_END
1640
16411641ROM_START( popn1 )
16421642   ROM_REGION( 0x100000, REGION_CPU1, 0 )      /* MC68EC020FG25 MPU */
16431643   ROM_LOAD16_BYTE( "803jaa01.6a", 0x000000, 0x80000, CRC(469CEE89) SHA1(d7c3e25e48492bceb17825db357830b08a20f09a) )
r2627r2628
19161916   ide_user_password = bm7thmix_user_password;
19171917}
19181918
1919#if 0
19201919static DRIVER_INIT( bmfinal )
19211920{
19221921   static const UINT8 bmfinal_user_password[2 + 32] =
r2627r2628
19331932   ide_master_password = beatmania_master_password;
19341933   ide_user_password = bmfinal_user_password;
19351934}
1936#endif
19371935
1936
19381937/*************************************
19391938 *
19401939 *  Game drivers
r2627r2628
19571956GAME( 2000, bmdct,    0,        djmain,   bmdct,     bmdct,     ROT0, "Konami", "beatmania f. Dreams Come True (ver JA-A)", 0 )
19581957GAME( 2000, bmcorerm, 0,        djmain,   beatmania, bmcorerm,  ROT0, "Konami", "beatmania CORE REMIX (ver JA-A)", 0 )
19591958GAME( 2001, bm6thmix, 0,        djmain,   beatmania, bm6thmix,  ROT0, "Konami", "beatmania 6th MIX (ver JA-A)", 0 )
1960GAME( 2001, bm7thmix, 0,        djmain,   beatmania, bm7thmix,  ROT0, "Konami", "beatmania 7th MIX (ver JA-B)", GAME_NOT_WORKING ) // missing hd
1959GAME( 2001, bm7thmix, 0,        djmain,   beatmania, bm7thmix,  ROT0, "Konami", "beatmania 7th MIX (ver JA-B)", 0 )
1960GAME( 2002, bmfinal,  0,        djmain,   beatmania, bmfinal,   ROT0, "Konami", "beatmania THE FINAL (ver JA-A)", GAME_NOT_WORKING ) // missing hd
19611961
19621962#if 0
19631963// for reference, these sets have not been verified
19641964GAME( 1998, bm3rdmxb, bm3rdmix, djmain,   bm3rdmix,  beatmania, ROT0, "Konami", "beatmania 3rd MIX (ver JA-B)", 0 )
19651965
1966GAME( 2002, bmfinal,  0,        djmain,   beatmania, bmfinal,   ROT0, "Konami", "beatmania THE FINAL (ver JA-A)", 0 )
1967
19681966GAME( 1998, popn1,    0,        djmain,   popn1,     beatmania, ROT0, "Konami", "Pop'n Music 1 (ver JA-A)", 0 )
19691967GAME( 1998, popn2,    0,        djmain,   popnmusic, beatmania, ROT0, "Konami", "Pop'n Music 2 (ver JA-A)", 0 )
19701968GAME( 1999, popn3,    0,        djmain,   popnmusic, beatmania, ROT0, "Konami", "Pop'n Music 3 (ver JA-A)", 0 )
src/mame/drivers/segac2.c
r2627r2628
799799    /* The first level increase (from 0 to 1) is always after destroying
800800       35 jewels. Then, the level gets 1 level more every : */
801801    PORT_DIPNAME( 0x30, 0x30, DEF_STR( Difficulty ) )
802    PORT_DIPSETTING(    0x00, DEF_STR( Easy ) )     // 50 jewels
803    PORT_DIPSETTING(    0x10, DEF_STR( Medium ) )   // 40 jewels
804    PORT_DIPSETTING(    0x30, DEF_STR( Hard ) )     // 35 jewels
805    PORT_DIPSETTING(    0x20, DEF_STR( Hardest ) )  // 25 jewels
802    PORT_DIPSETTING(    0x00, DEF_STR( Easiest ) )  // 50 jewels
803    PORT_DIPSETTING(    0x10, DEF_STR( Easy ) )     // 40 jewels
804    PORT_DIPSETTING(    0x30, DEF_STR( Normal ) )   // 35 jewels
805    PORT_DIPSETTING(    0x20, DEF_STR( Hard ) )     // 25 jewels
806806INPUT_PORTS_END
807807
808808static INPUT_PORTS_START( columnsu )
r2627r2628
823823   PORT_DIPNAME( 0x02, 0x00, DEF_STR( Demo_Sounds ) )
824824   PORT_DIPSETTING(    0x02, DEF_STR( Off ) )
825825   PORT_DIPSETTING(    0x00, DEF_STR( On ) )
826   PORT_DIPNAME( 0x04, 0x00, "Background Music" )   /* listed in the manual, ON by default */
827   PORT_DIPSETTING(    0x04, "BGM #1" )
828   PORT_DIPSETTING(    0x00, "BGM #2" )
826829    /* The first level increase (from 0 to 1) is always after destroying
827830       35 jewels. Then, the level gets 1 level more every : */
828831    PORT_DIPNAME( 0x30, 0x30, DEF_STR( Difficulty ) )
829    PORT_DIPSETTING(    0x00, DEF_STR( Easy ) )     // 50 jewels
830    PORT_DIPSETTING(    0x10, DEF_STR( Medium ) )   // 40 jewels
831    PORT_DIPSETTING(    0x30, DEF_STR( Hard ) )     // 35 jewels
832    PORT_DIPSETTING(    0x20, DEF_STR( Hardest ) )  // 25 jewels
832    PORT_DIPSETTING(    0x00, DEF_STR( Easiest ) )  // 50 jewels
833    PORT_DIPSETTING(    0x10, DEF_STR( Easy ) )     // 40 jewels
834    PORT_DIPSETTING(    0x30, DEF_STR( Normal ) )   // 35 jewels
835    PORT_DIPSETTING(    0x20, DEF_STR( Hard ) )     // 25 jewels
833836INPUT_PORTS_END
834837
835838static INPUT_PORTS_START( columns2 )
src/mame/drivers/gottlieb.c
r2627r2628
19701970   ROM_LOAD( "qq-rom0.bin",  0xe000, 0x2000, CRC(8ddbe438) SHA1(31112d711af5d4039491e99a0be0c088b3272482) )
19711971
19721972   ROM_REGION( 0x10000, REGION_CPU2, 0 )
1973   ROM_LOAD( "qb-snd1.bin",  0x7000, 0x800, CRC(15787c07) SHA1(8b7d03fbf2ebaa71b3a7e2f636a0d1bb9b796e43) )
1974   ROM_LOAD( "qb-snd2.bin",  0x7800, 0x800, CRC(58437508) SHA1(09d8053e7e99679b602dcda230d64db7fe6cb7f5) )
1973   ROM_LOAD( "qq-snd1.bin",  0x7000, 0x800, BAD_DUMP CRC(15787c07) SHA1(8b7d03fbf2ebaa71b3a7e2f636a0d1bb9b796e43) )
1974   ROM_LOAD( "qq-snd2.bin",  0x7800, 0x800, BAD_DUMP CRC(58437508) SHA1(09d8053e7e99679b602dcda230d64db7fe6cb7f5) )
19751975
19761976   ROM_REGION( 0x2000, REGION_GFX1, ROMREGION_DISPOSE )
19771977   ROM_LOAD( "qq-bg0.bin",   0x0000, 0x1000, CRC(050badde) SHA1(d049367e262cc6080e01d32227e86310166e00bb) )   /* chars */
r2627r2628
22602260GAME( 1983, krull,    0,        krull,    krull,    0,        ROT270, "Gottlieb", "Krull", 0 )
22612261GAME( 1983, kngtmare, 0,        gottlieb, kngtmare, 0,        ROT0,   "Gottlieb", "Knightmare (prototype)", GAME_NO_SOUND )
22622262GAME( 1983, sqbert,   0,        qbert,    qbert,    0,        ROT270, "Mylstar", "Faster, Harder, More Challenging Q*bert (prototype)", 0 )
2263GAME( 1983, qbertqub, 0,        qbert,    qbertqub, 0,        ROT270, "Mylstar", "Q*bert's Qubes", 0 )
2263GAME( 1983, qbertqub, 0,        qbert,    qbertqub, 0,        ROT270, "Mylstar", "Q*bert's Qubes", GAME_IMPERFECT_SOUND )
22642264GAME( 1983, mach3,    0,        gottlieb2,mach3,    laserdsc, ROT0,   "Mylstar", "M.A.C.H. 3", GAME_NOT_WORKING )
22652265GAME( 1983, screwloo, 0,        gottlieb2,screwloo, gottlieb, ROT0,   "Mylstar", "Screw Loose (prototype)", 0 )
22662266GAME( 1984, cobram3,  0,        gottlieb2,mach3,    laserdsc, ROT0,   "Data East","Cobra Command", GAME_NOT_WORKING )
src/mame/drivers/hng64.c
r2627r2628
10721072
10731073static OPBASE_HANDLER( KL5C80_opbase_handler )
10741074{
1075   opcode_base = opcode_arg_base = hng64_com_op_base;
1075   opbase->rom = opbase->ram = hng64_com_op_base;
10761076   return ~0;
10771077}
10781078
src/mame/drivers/cidelsa.c
r2627r2628
44#include "video/cdp1869.h"
55#include "sound/cdp1869.h"
66#include "sound/ay8910.h"
7#include "machine/cdp1852.h"
78#include "cidelsa.h"
89
910/*
r2627r2628
187188    */
188189}
189190
190static WRITE8_HANDLER( altair_out1_w )
191/* CDP1852 Interfaces */
192
193static CDP1852_DATA_READ( cidelsa_in0_r )
191194{
195   return input_port_read(device->machine, "IN0");
196}
197
198static CDP1852_DATA_READ( cidelsa_in1_r )
199{
200   return input_port_read(device->machine, "IN1");
201}
202
203static CDP1852_DATA_READ( cidelsa_in2_r )
204{
205   return input_port_read(device->machine, "IN2");
206}
207
208static CDP1852_DATA_WRITE( altair_out1_w )
209{
192210   /*
193211      bit   description
194212
r2627r2628
207225   set_led_status(2, data & 0x20); // FIRE
208226}
209227
210static WRITE8_HANDLER( draco_out1_w )
228static CDP1852_DATA_WRITE( draco_out1_w )
211229{
212   cidelsa_state *state = machine->driver_data;
230   cidelsa_state *state = device->machine->driver_data;
213231
214232   /*
215233      bit   description
r2627r2628
227245    state->draco_sound = (data & 0xe0) >> 5;
228246}
229247
248static CDP1852_INTERFACE( cidelsa_cdp1852_in0_intf )
249{
250   CDP1852_CLOCK_HIGH, // really tied to CDP1876 CMSEL (pin 32)
251   CDP1852_MODE_INPUT,
252   cidelsa_in0_r,
253   NULL,
254   NULL
255};
256
257static CDP1852_INTERFACE( cidelsa_cdp1852_in1_intf )
258{
259   CDP1852_CLOCK_HIGH,
260   CDP1852_MODE_INPUT,
261   cidelsa_in1_r,
262   NULL,
263   NULL
264};
265
266static CDP1852_INTERFACE( cidelsa_cdp1852_in2_intf )
267{
268   CDP1852_CLOCK_HIGH,
269   CDP1852_MODE_INPUT,
270   cidelsa_in2_r,
271   NULL,
272   NULL
273};
274
275static CDP1852_INTERFACE( altair_cdp1852_out1_intf )
276{
277   ALTAIR_CHR1 / 8, // CDP1802 TPB
278   CDP1852_MODE_OUTPUT,
279   NULL,
280   altair_out1_w,
281   NULL
282};
283
284static CDP1852_INTERFACE( draco_cdp1852_out1_intf )
285{
286   ALTAIR_CHR1 / 8, // CDP1802 TPB
287   CDP1852_MODE_OUTPUT,
288   NULL,
289   draco_out1_w,
290   NULL
291};
292
230293/* Memory Maps */
231294
232295// Destroyer
r2627r2628
265328ADDRESS_MAP_END
266329
267330static ADDRESS_MAP_START( altair_io_map, ADDRESS_SPACE_IO, 8 )
268   AM_RANGE(0x01, 0x01) AM_READ_PORT("IN0") AM_WRITE(altair_out1_w)
269   AM_RANGE(0x02, 0x02) AM_READ_PORT("IN1")
331   AM_RANGE(0x01, 0x01) AM_DEVREAD(CDP1852, "IC23", cdp1852_data_r) AM_DEVWRITE(CDP1852, "IC26", cdp1852_data_w)
332   AM_RANGE(0x02, 0x02) AM_DEVREAD(CDP1852, "IC24", cdp1852_data_r)
270333   AM_RANGE(0x03, 0x03) AM_DEVWRITE(CDP1869_VIDEO, CDP1869_TAG, cdp1869_out3_w)
271   AM_RANGE(0x04, 0x04) AM_READ_PORT("IN2") AM_DEVWRITE(CDP1869_VIDEO, CDP1869_TAG, cdp1869_out4_w)
334   AM_RANGE(0x04, 0x04) AM_DEVREAD(CDP1852, "IC25", cdp1852_data_r) AM_DEVWRITE(CDP1869_VIDEO, CDP1869_TAG, cdp1869_out4_w)
272335   AM_RANGE(0x05, 0x05) AM_DEVWRITE(CDP1869_VIDEO, CDP1869_TAG, cdp1869_out5_w)
273336   AM_RANGE(0x06, 0x06) AM_DEVWRITE(CDP1869_VIDEO, CDP1869_TAG, cdp1869_out6_w)
274337   AM_RANGE(0x07, 0x07) AM_DEVWRITE(CDP1869_VIDEO, CDP1869_TAG, cdp1869_out7_w)
r2627r2628
284347ADDRESS_MAP_END
285348
286349static ADDRESS_MAP_START( draco_io_map, ADDRESS_SPACE_IO, 8 )
287   AM_RANGE(0x01, 0x01) AM_READ_PORT("IN0") AM_WRITE(draco_out1_w)
288   AM_RANGE(0x02, 0x02) AM_READ_PORT("IN1")
350   AM_RANGE(0x01, 0x01) AM_DEVREAD(CDP1852, "IC29", cdp1852_data_r) AM_DEVWRITE(CDP1852, "IC32", cdp1852_data_w)
351   AM_RANGE(0x02, 0x02) AM_DEVREAD(CDP1852, "IC30", cdp1852_data_r)
289352   AM_RANGE(0x03, 0x03) AM_DEVWRITE(CDP1869_VIDEO, CDP1869_TAG, cdp1869_out3_w)
290   AM_RANGE(0x04, 0x04) AM_READ_PORT("IN2") AM_DEVWRITE(CDP1869_VIDEO, CDP1869_TAG, cdp1869_out4_w)
353   AM_RANGE(0x04, 0x04) AM_DEVREAD(CDP1852, "IC31", cdp1852_data_r) AM_DEVWRITE(CDP1869_VIDEO, CDP1869_TAG, cdp1869_out4_w)
291354   AM_RANGE(0x05, 0x05) AM_DEVWRITE(CDP1869_VIDEO, CDP1869_TAG, cdp1869_out5_w)
292355   AM_RANGE(0x06, 0x06) AM_DEVWRITE(CDP1869_VIDEO, CDP1869_TAG, cdp1869_out6_w)
293356   AM_RANGE(0x07, 0x07) AM_DEVWRITE(CDP1869_VIDEO, CDP1869_TAG, cdp1869_out7_w)
r2627r2628
578641   MDRV_MACHINE_START(cidelsa)
579642   MDRV_MACHINE_RESET(cidelsa)
580643
644   // input/output hardware
645
646   MDRV_DEVICE_ADD("IC23", CDP1852)
647   MDRV_DEVICE_CONFIG(cidelsa_cdp1852_in0_intf)
648
649   MDRV_DEVICE_ADD("IC24", CDP1852)
650   MDRV_DEVICE_CONFIG(cidelsa_cdp1852_in1_intf)
651
652   MDRV_DEVICE_ADD("IC25", CDP1852)
653   MDRV_DEVICE_CONFIG(cidelsa_cdp1852_in2_intf)
654
655   MDRV_DEVICE_ADD("IC26", CDP1852)
656   MDRV_DEVICE_CONFIG(altair_cdp1852_out1_intf)
657
581658   // video hardware
582659
583660   MDRV_IMPORT_FROM(altair_video)
r2627r2628
608685   MDRV_CPU_PROGRAM_MAP(draco_sound_map, 0)
609686   MDRV_CPU_IO_MAP(draco_sound_io_map, 0)
610687
688   // input/output hardware
689
690   MDRV_DEVICE_ADD("IC29", CDP1852)
691   MDRV_DEVICE_CONFIG(cidelsa_cdp1852_in0_intf)
692
693   MDRV_DEVICE_ADD("IC30", CDP1852)
694   MDRV_DEVICE_CONFIG(cidelsa_cdp1852_in1_intf)
695
696   MDRV_DEVICE_ADD("IC31", CDP1852)
697   MDRV_DEVICE_CONFIG(cidelsa_cdp1852_in2_intf)
698
699   MDRV_DEVICE_ADD("IC32", CDP1852)
700   MDRV_DEVICE_CONFIG(draco_cdp1852_out1_intf)
701
611702   // video hardware
612703
613704   MDRV_IMPORT_FROM(draco_video)
src/mame/drivers/taotaido.c
r2627r2628
9898      cpunum_set_input_line(machine, 1, INPUT_LINE_NMI, PULSE_LINE);
9999   }
100100}
101static ADDRESS_MAP_START( taotaido_readmem, ADDRESS_SPACE_PROGRAM, 16 )
102   AM_RANGE(0x000000, 0x0fffff) AM_READ(SMH_ROM)
103   AM_RANGE(0x800000, 0x803fff) AM_READ(SMH_RAM)         // bg ram?
104   AM_RANGE(0xa00000, 0xa01fff) AM_READ(SMH_RAM)         // sprite ram
105   AM_RANGE(0xc00000, 0xc0ffff) AM_READ(SMH_RAM)         // sprite tile look up
106   AM_RANGE(0xfe0000, 0xfeffff) AM_READ(SMH_RAM)         // main ram
107   AM_RANGE(0xffc000, 0xffcfff) AM_READ(SMH_RAM)         // palette ram
108   AM_RANGE(0xffe000, 0xffe3ff) AM_READ(SMH_RAM)         // rowscroll / rowselect / scroll ram
109
101static ADDRESS_MAP_START( main_map, ADDRESS_SPACE_PROGRAM, 16 )
102   AM_RANGE(0x000000, 0x0fffff) AM_ROM
103   AM_RANGE(0x800000, 0x803fff) AM_RAM_WRITE(taotaido_bgvideoram_w) AM_BASE(&taotaido_bgram)   // bg ram?
104   AM_RANGE(0xa00000, 0xa01fff) AM_RAM AM_BASE(&taotaido_spriteram)      // sprite ram
105   AM_RANGE(0xc00000, 0xc0ffff) AM_RAM AM_BASE(&taotaido_spriteram2)      // sprite tile lookup ram
106   AM_RANGE(0xfe0000, 0xfeffff) AM_RAM                  // main ram
107   AM_RANGE(0xffc000, 0xffcfff) AM_RAM_WRITE(paletteram16_xRRRRRGGGGGBBBBB_word_w) AM_BASE(&paletteram16)   // palette ram
108   AM_RANGE(0xffe000, 0xffe3ff) AM_RAM AM_BASE(&taotaido_scrollram)      // rowscroll / rowselect / scroll ram
110109   AM_RANGE(0xffff80, 0xffff81) AM_READ(input_port_0_word_r)   // player 1 inputs
111110   AM_RANGE(0xffff82, 0xffff83) AM_READ(input_port_1_word_r)   // player 2 inputs
112111   AM_RANGE(0xffff84, 0xffff85) AM_READ(input_port_2_word_r)   // system inputs
r2627r2628
119118   AM_RANGE(0xffffa0, 0xffffa1) AM_READ(input_port_7_word_r)   // player 3 inputs (unused)
120119   AM_RANGE(0xffffa2, 0xffffa3) AM_READ(input_port_8_word_r)   // player 4 inputs (unused)
121120#endif
122
123   AM_RANGE(0xffffe0, 0xffffe1) AM_READ(pending_command_r)   // guess - seems to be needed for all the sounds to work
124ADDRESS_MAP_END
125
126static ADDRESS_MAP_START( taotaido_writemem, ADDRESS_SPACE_PROGRAM, 16 )
127   AM_RANGE(0x000000, 0x0fffff) AM_WRITE(SMH_ROM)
128   AM_RANGE(0x800000, 0x803fff) AM_WRITE(taotaido_bgvideoram_w) AM_BASE(&taotaido_bgram)   // bg ram?
129   AM_RANGE(0xa00000, 0xa01fff) AM_WRITE(SMH_RAM) AM_BASE(&taotaido_spriteram)      // sprite ram
130   AM_RANGE(0xc00000, 0xc0ffff) AM_WRITE(SMH_RAM) AM_BASE(&taotaido_spriteram2)      // sprite tile lookup ram
131   AM_RANGE(0xfe0000, 0xfeffff) AM_WRITE(SMH_RAM)                  // main ram
132   AM_RANGE(0xffc000, 0xffcfff) AM_WRITE(paletteram16_xRRRRRGGGGGBBBBB_word_w) AM_BASE(&paletteram16)   // palette ram
133   AM_RANGE(0xffe000, 0xffe3ff) AM_WRITE(SMH_RAM) AM_BASE(&taotaido_scrollram)      // rowscroll / rowselect / scroll ram
134
135121   AM_RANGE(0xffff00, 0xffff0f) AM_WRITE(taotaido_tileregs_w)
136122   AM_RANGE(0xffff10, 0xffff11) AM_WRITE(SMH_NOP)                  // unknown
137123   AM_RANGE(0xffff20, 0xffff21) AM_WRITE(SMH_NOP)                  // unknown - flip screen related
138124   AM_RANGE(0xffff40, 0xffff47) AM_WRITE(taotaido_sprite_character_bank_select_w)
139125   AM_RANGE(0xffffc0, 0xffffc1) AM_WRITE(sound_command_w)               // seems right
126   AM_RANGE(0xffffe0, 0xffffe1) AM_READ(pending_command_r)   // guess - seems to be needed for all the sounds to work
140127ADDRESS_MAP_END
141128
142129/* sound cpu - same as aerofgt */
r2627r2628
154141   memory_set_bankptr(1,rom + (data & 0x03) * 0x8000);
155142}
156143
157static ADDRESS_MAP_START( sound_readmem, ADDRESS_SPACE_PROGRAM, 8 )
158   AM_RANGE(0x0000, 0x77ff) AM_READ(SMH_ROM)
159   AM_RANGE(0x7800, 0x7fff) AM_READ(SMH_RAM)
160   AM_RANGE(0x8000, 0xffff) AM_READ(SMH_BANK1)
144static ADDRESS_MAP_START( sound_map, ADDRESS_SPACE_PROGRAM, 8 )
145   AM_RANGE(0x0000, 0x77ff) AM_ROM
146   AM_RANGE(0x7800, 0x7fff) AM_RAM
147   AM_RANGE(0x8000, 0xffff) AM_READWRITE(SMH_BANK1, SMH_ROM)
161148ADDRESS_MAP_END
162149
163static ADDRESS_MAP_START( sound_writemem, ADDRESS_SPACE_PROGRAM, 8 )
164   AM_RANGE(0x0000, 0x77ff) AM_WRITE(SMH_ROM)
165   AM_RANGE(0x7800, 0x7fff) AM_WRITE(SMH_RAM)
166   AM_RANGE(0x8000, 0xffff) AM_WRITE(SMH_ROM)
167ADDRESS_MAP_END
168
169static ADDRESS_MAP_START( taotaido_sound_readport, ADDRESS_SPACE_IO, 8 )
150static ADDRESS_MAP_START( sound_port_map, ADDRESS_SPACE_IO, 8 )
170151   ADDRESS_MAP_GLOBAL_MASK(0xff)
171   AM_RANGE(0x00, 0x00) AM_READ(YM2610_status_port_0_A_r)
172   AM_RANGE(0x02, 0x02) AM_READ(YM2610_status_port_0_B_r)
173   AM_RANGE(0x0c, 0x0c) AM_READ(soundlatch_r)
174ADDRESS_MAP_END
175
176static ADDRESS_MAP_START( taotaido_sound_writeport, ADDRESS_SPACE_IO, 8 )
177   ADDRESS_MAP_GLOBAL_MASK(0xff)
178   AM_RANGE(0x00, 0x00) AM_WRITE(YM2610_control_port_0_A_w)
152   AM_RANGE(0x00, 0x00) AM_READWRITE(YM2610_status_port_0_A_r, YM2610_control_port_0_A_w)
179153   AM_RANGE(0x01, 0x01) AM_WRITE(YM2610_data_port_0_A_w)
180   AM_RANGE(0x02, 0x02) AM_WRITE(YM2610_control_port_0_B_w)
154   AM_RANGE(0x02, 0x02) AM_READWRITE(YM2610_status_port_0_B_r, YM2610_control_port_0_B_w)
181155   AM_RANGE(0x03, 0x03) AM_WRITE(YM2610_data_port_0_B_w)
182156   AM_RANGE(0x04, 0x04) AM_WRITE(taotaido_sh_bankswitch_w)
183157   AM_RANGE(0x08, 0x08) AM_WRITE(pending_command_clear_w)
158   AM_RANGE(0x0c, 0x0c) AM_READ(soundlatch_r)
184159ADDRESS_MAP_END
185160
186161
187
188162static INPUT_PORTS_START( taotaido )
189163   PORT_START_TAG("IN0")   /* Player 1 controls (0xffff81.b) */
190164   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_JOYSTICK_UP ) PORT_8WAY PORT_PLAYER(1)
r2627r2628
359333
360334static MACHINE_DRIVER_START( taotaido )
361335   MDRV_CPU_ADD(M68000, 32000000/2)
362   MDRV_CPU_PROGRAM_MAP(taotaido_readmem,taotaido_writemem)
336   MDRV_CPU_PROGRAM_MAP(main_map,0)
363337   MDRV_CPU_VBLANK_INT("main", irq1_line_hold)
364338
365   MDRV_CPU_ADD(Z80,20000000/4) // ??
366339   /* audio CPU */
367   MDRV_CPU_PROGRAM_MAP(sound_readmem,sound_writemem)
368   MDRV_CPU_IO_MAP(taotaido_sound_readport,taotaido_sound_writeport)
340   MDRV_CPU_ADD(Z80,20000000/4) // ??
341   MDRV_CPU_PROGRAM_MAP(sound_map,0)
342   MDRV_CPU_IO_MAP(sound_port_map,0)
369343                        /* IRQs are triggered by the YM2610 */
370344
371345   MDRV_GFXDECODE(taotaido)
src/mame/drivers/tankbatt.c
r2627r2628
149149      sample_start (0, 0, 0);
150150}
151151
152static ADDRESS_MAP_START( readmem, ADDRESS_SPACE_PROGRAM, 8 )
152static ADDRESS_MAP_START( main_map, ADDRESS_SPACE_PROGRAM, 8 )
153153   AM_RANGE(0x0000, 0x01ff) AM_READ(SMH_RAM)
154   AM_RANGE(0x0c00, 0x0c07) AM_READ(tankbatt_in0_r)
155   AM_RANGE(0x0c08, 0x0c0f) AM_READ(tankbatt_in1_r)
156   AM_RANGE(0x0c18, 0x0c1f) AM_READ(tankbatt_dsw_r)
157   AM_RANGE(0x0200, 0x0bff) AM_READ(SMH_RAM)
158   AM_RANGE(0x6000, 0x7fff) AM_READ(SMH_ROM)
159   AM_RANGE(0xf800, 0xffff) AM_READ(SMH_ROM)   /* for the reset / interrupt vectors */
160ADDRESS_MAP_END
161
162static ADDRESS_MAP_START( writemem, ADDRESS_SPACE_PROGRAM, 8 )
154   AM_RANGE(0x0000, 0x000f) AM_WRITE(SMH_RAM) AM_BASE(&tankbatt_bulletsram) AM_SIZE(&tankbatt_bulletsram_size)
163155   AM_RANGE(0x0010, 0x01ff) AM_WRITE(SMH_RAM)
164156   AM_RANGE(0x0800, 0x0bff) AM_WRITE(tankbatt_videoram_w) AM_BASE(&videoram)
165   AM_RANGE(0x0000, 0x000f) AM_WRITE(SMH_RAM) AM_BASE(&tankbatt_bulletsram) AM_SIZE(&tankbatt_bulletsram_size)
166   AM_RANGE(0x0c18, 0x0c18) AM_WRITE(SMH_NOP) /* watchdog ?? */
157   AM_RANGE(0x0c00, 0x0c07) AM_READ(tankbatt_in0_r)
167158   AM_RANGE(0x0c00, 0x0c01) AM_WRITE(tankbatt_led_w)
159   AM_RANGE(0x0c08, 0x0c0f) AM_READ(tankbatt_in1_r)
168160   AM_RANGE(0x0c0a, 0x0c0a) AM_WRITE(tankbatt_interrupt_enable_w)
169161   AM_RANGE(0x0c0b, 0x0c0b) AM_WRITE(tankbatt_sh_engine_w)
170162   AM_RANGE(0x0c0c, 0x0c0c) AM_WRITE(tankbatt_sh_fire_w)
171163   AM_RANGE(0x0c0d, 0x0c0d) AM_WRITE(tankbatt_sh_expl_w)
172164   AM_RANGE(0x0c0f, 0x0c0f) AM_WRITE(tankbatt_demo_interrupt_enable_w)
165   AM_RANGE(0x0c18, 0x0c1f) AM_READ(tankbatt_dsw_r)
166   AM_RANGE(0x0c18, 0x0c18) AM_WRITE(SMH_NOP)   /* watchdog ?? */
167   AM_RANGE(0x0200, 0x0bff) AM_READ(SMH_RAM)
173168   AM_RANGE(0x0200, 0x07ff) AM_WRITE(SMH_RAM)
174169   AM_RANGE(0x2000, 0x3fff) AM_WRITE(SMH_ROM)
170   AM_RANGE(0x6000, 0x7fff) AM_READ(SMH_ROM)
171   AM_RANGE(0xf800, 0xffff) AM_READ(SMH_ROM)   /* for the reset / interrupt vectors */
175172ADDRESS_MAP_END
176173
177174static INTERRUPT_GEN( tankbatt_interrupt )
r2627r2628
279276
280277   /* basic machine hardware */
281278   MDRV_CPU_ADD(M6502, 1000000)   /* 1 MHz ???? */
282   MDRV_CPU_PROGRAM_MAP(readmem,writemem)
279   MDRV_CPU_PROGRAM_MAP(main_map,0)
283280   MDRV_CPU_VBLANK_INT("main", tankbatt_interrupt)
284281
285282   /* video hardware */
src/mame/drivers/wrally.c
r2627r2628
22
33World Rally (c) 1993 Gaelco (Designed & Developed by Zigurat. Produced by Gaelco)
44
5Preliminary driver by Manuel Abadia <manu@teleline.es>
5Driver by Manuel Abadia, Mike Coates, Nicola Salmoria and Miguel Andel Horna
66
7Encyption tables provided by Mike Coates who connected a fluke to the PCB.
8Nicola Salmoria made the decode function based on that info.
7Thanks to GAELCO SA for the DS5002FP code and information about the encryption
98
10Current decryption is incomplete
11The DS5002FP has 32KB undumped gameplay code making the game unplayable :(
12
139Main PCB components:
1410====================
1511
r2627r2628
1814* 1xDS5002FP @ D12 (Dallas security processor @ 12 MHz)
1915* 1xHM62256ALFP-8T (32KB NVSRAM) @ C11 (encrypted DS5002FP program code)
2016* 1xLithium cell
21* 2xMS6264A-20NC (32KB SRAM) @ D14 & D15 (shared memory between M68000 & DS5002FP)
17* 2xMS6264A-20NC (16KB SRAM) @ D14 & D15 (shared memory between M68000 & DS5002FP)
2218* 4x74LS157 (Quad 2 input multiplexer) @ F14, F15, F16 & F17 (used to select M68000 or DS5002FP address bus)
2319* 4x74LS245 (Octal bus transceiver) @ C14, C15, C16 & C17 (used to store shared RAM data)
2420* 2x74LS373 (Octal tristate latch) @ D16 & D17 (used by DS5002FP to access data from shared RAM)
r2627r2628
2622* 1xOSC24MHz @ B20
2723* 2xM27C4001 @ C22 & C23 (M68000 program ROMs)
2824* 1xPAL20L8 @ B23 (handles 1st level M68000 memory map)
29    0 -> DTACK (M68000 data ack)
25    0 -> DTACK M68000 data ack
3026    1 -> SELACT
3127    2 -> Input/sound (see below)
3228    3 -> ACTEXT
r2627r2628
5551    6 -> ENA/D?
5652    7 -> CKA/D?
5753
54* 1x16AS @ B15
55    0 -> OE
56    1 -> XSRL   Shared RAM @ D14
57    2 -> XSRH   Shared RAM @ D15
58    3 -> SAD    Shared Access with DS5002FP
59    4 -> SRE    Shared Access with M68000
60    5 -> TRANS
61    6 -> XLD
62    7 -> XHI
63
5864Sound related:
5965==============
6066* 1xOKIM6295 @ C6
r2627r2628
7884
7985#include "driver.h"
8086#include "cpu/m68000/m68000.h"
87#include "cpu/ds5002fp/ds5002fp.h"
8188#include "sound/okim6295.h"
8289
8390/* from video/wrally.c */
r2627r2628
8592extern UINT16 *wrally_videoram;
8693extern UINT16 *wrally_spriteram;
8794
88WRITE16_HANDLER( wrally_vram_w );
8995VIDEO_START( wrally );
9096VIDEO_UPDATE( wrally );
9197
9298/* from machine/wrally.c */
9399DRIVER_INIT( wrally );
100MACHINE_RESET( wrally );
101READ32_HANDLER( wrally_external_ram_iaddr );
102WRITE16_HANDLER( wrally_vram_w );
103WRITE16_HANDLER( wrally_flipscreen_w );
94104WRITE16_HANDLER( OKIM6295_bankswitch_w );
95105WRITE16_HANDLER( wrally_coin_counter_w );
96106WRITE16_HANDLER( wrally_coin_lockout_w );
97107
108static ADDRESS_MAP_START( wrally_readmem, ADDRESS_SPACE_PROGRAM, 16 )
109   AM_RANGE(0x000000, 0x0fffff) AM_ROM                                             /* ROM */
110   AM_RANGE(0x100000, 0x103fff) AM_READWRITE(SMH_RAM, wrally_vram_w) AM_BASE(&wrally_videoram)   /* encrypted Video RAM */
111   AM_RANGE(0x108000, 0x108007) AM_RAM AM_BASE(&wrally_vregs)                           /* Video Registers */
112   AM_RANGE(0x10800c, 0x10800d) AM_WRITE(SMH_NOP)                                    /* CLR INT Video */
113   AM_RANGE(0x200000, 0x203fff) AM_READWRITE(SMH_RAM, paletteram16_xxxxBBBBRRRRGGGG_word_w) AM_BASE(&paletteram16)   /* Palette */
114   AM_RANGE(0x440000, 0x440fff) AM_RAM AM_BASE(&wrally_spriteram)                        /* Sprite RAM */
115   AM_RANGE(0x700000, 0x700001) AM_READ(input_port_0_word_r)                           /* DSW #1 & #2 */
116   AM_RANGE(0x700002, 0x700003) AM_READ(input_port_1_word_r)                           /* INPUT 1P & 2P, COINSW, STARTSW */
117   AM_RANGE(0x700004, 0x700005) AM_READ(input_port_2_word_r)                           /* Wheel */
118   AM_RANGE(0x700008, 0x700009) AM_READ(input_port_3_word_r)                           /* TESTSW & SERVICESW */
119   AM_RANGE(0x70000c, 0x70000d) AM_WRITE(OKIM6295_bankswitch_w)                        /* OKI6295 bankswitch */
120   AM_RANGE(0x70000e, 0x70000f) AM_READWRITE(OKIM6295_status_0_lsb_r, OKIM6295_data_0_lsb_w)   /* OKI6295 status/data register */
121   AM_RANGE(0x70000a, 0x70001b) AM_WRITE(wrally_coin_lockout_w)                        /* Coin lockouts */
122   AM_RANGE(0x70002a, 0x70003b) AM_WRITE(wrally_coin_counter_w)                        /* Coin counters */
123   AM_RANGE(0x70004a, 0x70004b) AM_WRITE(SMH_NOP)                                    /* Sound muting */
124   AM_RANGE(0x70005a, 0x70005b) AM_WRITE(wrally_flipscreen_w)                           /* Flip screen */
125   AM_RANGE(0x70006a, 0x70007b) AM_WRITE(SMH_NOP)                                    /* ??? */
126   AM_RANGE(0xfec000, 0xfeffff) AM_RAM AM_SHARE(1)                                    /* Work RAM (shared with DS5002FP) */
127ADDRESS_MAP_END
98128
99static WRITE16_HANDLER( unknown_w )
100{
101   popmessage("write %04x to %04x", data, offset*2 + 0x6a);
102}
129static ADDRESS_MAP_START( dallas_rom, ADDRESS_SPACE_PROGRAM, 8 )
130   AM_RANGE(0x0000, 0x7fff) AM_ROM                           /* Code in NVRAM */
131ADDRESS_MAP_END
103132
104static ADDRESS_MAP_START( main_map, ADDRESS_SPACE_PROGRAM, 16 )
105   AM_RANGE(0x000000, 0x0fffff) AM_ROM
106   AM_RANGE(0x100000, 0x103fff) AM_RAM_WRITE(wrally_vram_w) AM_BASE(&wrally_videoram)      /* encrypted Video RAM */
107   AM_RANGE(0x108000, 0x108007) AM_RAM AM_BASE(&wrally_vregs)         /* Video Registers */
108   AM_RANGE(0x10800c, 0x10800d) AM_WRITE(SMH_NOP)                  /* CLR INT Video */
109   AM_RANGE(0x200000, 0x203fff) AM_RAM_WRITE(paletteram16_xxxxBBBBRRRRGGGG_word_w) AM_BASE(&paletteram16)/* Palette */
110   AM_RANGE(0x440000, 0x440fff) AM_RAM AM_BASE(&wrally_spriteram)      /* Sprite RAM */
111   AM_RANGE(0x700000, 0x700001) AM_READ(input_port_0_word_r)         /* DSW #1 & #2 */
112   AM_RANGE(0x700002, 0x700003) AM_READ(input_port_1_word_r)         /* INPUT 1P & 2P, COINSW, STARTSW */
113   AM_RANGE(0x700004, 0x700005) AM_READ(input_port_2_word_r)         /* Wheel */
114   AM_RANGE(0x700008, 0x700009) AM_READ(input_port_3_word_r)         /* TESTSW & SERVICESW */
115   AM_RANGE(0x70000c, 0x70000d) AM_WRITE(OKIM6295_bankswitch_w)      /* OKI6295 bankswitch */
116   AM_RANGE(0x70000e, 0x70000f) AM_READWRITE(OKIM6295_status_0_lsb_r,OKIM6295_data_0_lsb_w)/* OKI6295 status register */
117   AM_RANGE(0x70000a, 0x70001b) AM_WRITE(wrally_coin_lockout_w)      /* Coin lockouts */
118   AM_RANGE(0x70002a, 0x70003b) AM_WRITE(wrally_coin_counter_w)      /* Coin counters */
119   AM_RANGE(0x70004a, 0x70004b) AM_WRITE(SMH_NOP)                  /* sound muting */
120   AM_RANGE(0x70005a, 0x70005b) AM_WRITE(SMH_NOP)                  /* flip screen */
121   AM_RANGE(0x70006a, 0x70007b) AM_WRITE(unknown_w)               /* ??? */
122   AM_RANGE(0xfe0000, 0xfeffff) AM_RAM               /* Work RAM (partially shared with DS5002FP) */
133static ADDRESS_MAP_START( dallas_ram, ADDRESS_SPACE_DATA, 8 )
134   AM_RANGE(0x0000, 0xffff) AM_RAM AM_SHARE(1)   AM_MASK(0x3fff)      /* Shared RAM with the main CPU */
123135ADDRESS_MAP_END
124136
137/* DS5002FP configuration */
138static const ds5002fp_config dallas_config =
139{
140   0x88,         /* bootstrap loader MCON register */
141   0x00,         /* bootstrap loader RPCTL register */
142   0x80         /* bootstrap loader CRC register */
143};
125144
126145static INPUT_PORTS_START( wrally )
127146PORT_START   /* DSW #1 & #2 */
r2627r2628
137156   PORT_DIPSETTING(      0x0018, DEF_STR( Joystick ) )
138157   PORT_DIPSETTING(      0x0010, "Pot Wheel" )
139158   PORT_DIPSETTING(      0x0000, "Opt Wheel" )
140   PORT_DIPNAME( 0x0020, 0x0020, DEF_STR( Demo_Sounds ) )
141   PORT_DIPSETTING(      0x0000, DEF_STR( Off ) )
142   PORT_DIPSETTING(      0x0020, DEF_STR( On ) )
159   PORT_DIPNAME( 0x0020, 0x0000, DEF_STR( Demo_Sounds ) )
160   PORT_DIPSETTING(      0x0020, DEF_STR( Off ) )
161   PORT_DIPSETTING(      0x0000, DEF_STR( On ) )
143162   PORT_DIPNAME( 0x0040, 0x0040, DEF_STR( Cabinet ) )
144163   PORT_DIPSETTING(      0x0040, DEF_STR( Upright ) )
145164   PORT_DIPSETTING(      0x0000, DEF_STR( Cocktail ) )
r2627r2628
174193   PORT_BIT( 0x0002, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN ) PORT_8WAY PORT_PLAYER(1)
175194   PORT_BIT( 0x0004, IP_ACTIVE_LOW, IPT_JOYSTICK_RIGHT ) PORT_8WAY PORT_PLAYER(1)
176195   PORT_BIT( 0x0008, IP_ACTIVE_LOW, IPT_JOYSTICK_LEFT ) PORT_8WAY PORT_PLAYER(1)
177   PORT_BIT( 0x0010, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_PLAYER(1)
178   PORT_BIT( 0x0020, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_PLAYER(1)
196   PORT_BIT( 0x0010, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_PLAYER(1)
197   PORT_BIT( 0x0020, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_PLAYER(1)
179198   PORT_BIT( 0x0040, IP_ACTIVE_LOW, IPT_COIN1 )
180199   PORT_BIT( 0x0080, IP_ACTIVE_LOW, IPT_COIN2 )
181200   PORT_BIT( 0x0100, IP_ACTIVE_LOW, IPT_JOYSTICK_UP ) PORT_8WAY PORT_PLAYER(2)
182201   PORT_BIT( 0x0200, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN ) PORT_8WAY PORT_PLAYER(2)
183202   PORT_BIT( 0x0400, IP_ACTIVE_LOW, IPT_JOYSTICK_RIGHT ) PORT_8WAY PORT_PLAYER(2)
184203   PORT_BIT( 0x0800, IP_ACTIVE_LOW, IPT_JOYSTICK_LEFT ) PORT_8WAY PORT_PLAYER(2)
185   PORT_BIT( 0x1000, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_PLAYER(2)
186   PORT_BIT( 0x2000, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_PLAYER(2)
204   PORT_BIT( 0x1000, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_PLAYER(2)
205   PORT_BIT( 0x2000, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_PLAYER(2)
187206   PORT_BIT( 0x4000, IP_ACTIVE_LOW, IPT_START1 )
188207   PORT_BIT( 0x8000, IP_ACTIVE_LOW, IPT_START2 )
189208
190209PORT_START   /* Wheel control? */
191   PORT_BIT( 0xffff, IP_ACTIVE_LOW, IPT_UNKNOWN ) /* not implemented yet */
210   PORT_BIT( 0xffff, IP_ACTIVE_LOW, IPT_UNKNOWN ) /* not implemented */
192211
193212PORT_START   /* INPUTS, TEST & SERVICE */
194213   PORT_BIT( 0x0001, IP_ACTIVE_LOW, IPT_SERVICE1 )
r2627r2628
196215   PORT_BIT( 0xfffc, IP_ACTIVE_LOW, IPT_UNKNOWN )
197216INPUT_PORTS_END
198217
199
200
201218static const gfx_layout wrally_tilelayout16 =
202219{
203220   16,16,                           /* 16x16 tiles */
r2627r2628
216233GFXDECODE_END
217234
218235
219
220236static MACHINE_DRIVER_START( wrally )
221237   /* basic machine hardware */
222   MDRV_CPU_ADD(M68000,XTAL_24MHz/2)   /* verified on pcb */
223   MDRV_CPU_PROGRAM_MAP(main_map,0)
238   MDRV_CPU_ADD(M68000,XTAL_24MHz/2)      /* verified on pcb */
239   MDRV_CPU_PROGRAM_MAP(wrally_readmem, 0)
224240   MDRV_CPU_VBLANK_INT("main", irq6_line_hold)
225241
242   MDRV_CPU_ADD(DS5002FP, XTAL_24MHz/2)   /* verified on pcb */
243   MDRV_CPU_CONFIG(dallas_config)
244   MDRV_CPU_PROGRAM_MAP(dallas_rom, 0)
245   MDRV_CPU_DATA_MAP(dallas_ram, 0)
246
247   MDRV_INTERLEAVE(640)               /* heavy sync */
248   MDRV_MACHINE_RESET(wrally)
249
226250   /* video hardware */
227251   MDRV_SCREEN_ADD("main", RASTER)
228252   MDRV_SCREEN_REFRESH_RATE(60)
229   MDRV_SCREEN_VBLANK_TIME(ATTOSECONDS_IN_USEC(2500) /* not accurate */)
253   MDRV_SCREEN_VBLANK_TIME(ATTOSECONDS_IN_USEC(2500)) /* not accurate */
230254   MDRV_SCREEN_FORMAT(BITMAP_FORMAT_INDEXED16)
231255   MDRV_SCREEN_SIZE(64*16, 32*16)
232   MDRV_SCREEN_VISIBLE_AREA(0, 64*16-1, 0, 32*16-1)
233//  MDRV_SCREEN_VISIBLE_AREA(0, 320-1, 16, 256-1)
256   MDRV_SCREEN_VISIBLE_AREA(8, 24*16-8-1, 16, 16*16-8-1)
234257
235258   MDRV_GFXDECODE(wrally)
236259   MDRV_PALETTE_LENGTH(1024*8)
r2627r2628
241264   /* sound hardware */
242265   MDRV_SPEAKER_STANDARD_MONO("mono")
243266
244   MDRV_SOUND_ADD(OKIM6295, XTAL_1MHz) /* verified on pcb */
267   MDRV_SOUND_ADD(OKIM6295, XTAL_1MHz)                /* verified on pcb */
245268   MDRV_SOUND_CONFIG(okim6295_interface_region_1_pin7high) /* verified on pcb */
246269   MDRV_SOUND_ROUTE(ALL_OUTPUTS, "mono", 1.0)
247270MACHINE_DRIVER_END
r2627r2628
249272
250273ROM_START( wrally )
251274   ROM_REGION( 0x100000, REGION_CPU1, 0 )   /* 68000 code */
252   ROM_LOAD16_BYTE(   "worldr17.c23",   0x000000, 0x080000, CRC(050f5629) SHA1(74fc2cd5114f3bc4b2429f1d8d7eeb1658f9f179) )
253   ROM_LOAD16_BYTE(   "worldr16.c22",   0x000001, 0x080000, CRC(9e0d126c) SHA1(369360b7ec2c3497af3bf62b4eba24c3d9f94675) )
275   ROM_LOAD16_BYTE( "worldr17.c23", 0x000000, 0x080000, CRC(050f5629) SHA1(74fc2cd5114f3bc4b2429f1d8d7eeb1658f9f179) ) /* Only difference compared to set 2 is how the Dallas DS5002FP */
276   ROM_LOAD16_BYTE( "worldr16.c22", 0x000001, 0x080000, CRC(9e0d126c) SHA1(369360b7ec2c3497af3bf62b4eba24c3d9f94675) ) /* power failure shows on screen, IE: "Tension  baja " */
254277
278   ROM_REGION( 0x10000, REGION_CPU2, 0 )   /* DS5002FP code */
279   ROM_LOAD( "wrdallas.bin", 0x00000, 0x8000, CRC(547d1768) SHA1(c58d1edd072d796be0663fb265f4739ec006b688) )
280
255281   ROM_REGION( 0x200000, REGION_GFX1, ROMREGION_DISPOSE )
256   ROM_LOAD16_BYTE( "worldr21.i13",   0x000000, 0x080000, CRC(b7fddb12) SHA1(619a75daac8cbba7e85c97ca19733e2196d66d5c) )
257   ROM_LOAD16_BYTE( "worldr20.i11",   0x000001, 0x080000, CRC(58b2809a) SHA1(8741ec544c54e2a2f5d17ac2f8400ee2ce382e83) )
258   ROM_LOAD16_BYTE( "worldr19.i09",   0x100000, 0x080000, CRC(018b35bb) SHA1(ca789e23d18cc7d7e48b6858e6b61e03bf88b475) )
259   ROM_LOAD16_BYTE( "worldr18.i07",   0x100001, 0x080000, CRC(b37c807e) SHA1(9e6155a2b5206c0d4dca669d24d9fe9830027651) )
282   ROM_LOAD16_BYTE( "worldr21.i13", 0x000000, 0x080000, CRC(b7fddb12) SHA1(619a75daac8cbba7e85c97ca19733e2196d66d5c) )
283   ROM_LOAD16_BYTE( "worldr20.i11", 0x000001, 0x080000, CRC(58b2809a) SHA1(8741ec544c54e2a2f5d17ac2f8400ee2ce382e83) )
284   ROM_LOAD16_BYTE( "worldr19.i09", 0x100000, 0x080000, CRC(018b35bb) SHA1(ca789e23d18cc7d7e48b6858e6b61e03bf88b475) )
285   ROM_LOAD16_BYTE( "worldr18.i07", 0x100001, 0x080000, CRC(b37c807e) SHA1(9e6155a2b5206c0d4dca669d24d9fe9830027651) )
260286
261287   ROM_REGION( 0x140000, REGION_SOUND1, 0 )   /* ADPCM samples - sound chip is OKIM6295 */
262288   ROM_LOAD( "worldr14.c01",   0x000000, 0x080000, CRC(e931c2ee) SHA1(ea1cf8ad52713e5136a370e289567eea9e6403d6) )
r2627r2628
274300
275301ROM_START( wrallya )
276302   ROM_REGION( 0x100000, REGION_CPU1, 0 )   /* 68000 code */
277   ROM_LOAD16_BYTE(   "c23.bin",   0x000000, 0x080000, CRC(8b7d93c3) SHA1(ce4163eebc5d4a0c1266d650523b1ffc702d1b87) )
278   ROM_LOAD16_BYTE(   "c22.bin",   0x000001, 0x080000, CRC(56da43b6) SHA1(02db8f969ed5e7f5e5356c45c0312faf5f000335) )
303   ROM_LOAD16_BYTE( "c23.bin", 0x000000, 0x080000, CRC(8b7d93c3) SHA1(ce4163eebc5d4a0c1266d650523b1ffc702d1b87) ) /* Only difference compared to set 1 is how the Dallas DS5002FP */
304   ROM_LOAD16_BYTE( "c22.bin", 0x000001, 0x080000, CRC(56da43b6) SHA1(02db8f969ed5e7f5e5356c45c0312faf5f000335) ) /* power failure shows on screen, IE: "Power  Failure" */
279305
306   ROM_REGION( 0x10000, REGION_CPU2, 0 )   /* DS5002FP code */
307   ROM_LOAD( "wrdallas.bin", 0x00000, 0x8000, CRC(547d1768) SHA1(c58d1edd072d796be0663fb265f4739ec006b688) )
308
280309   ROM_REGION( 0x200000, REGION_GFX1, ROMREGION_DISPOSE )
281   ROM_LOAD16_BYTE( "worldr21.i13",   0x000000, 0x080000, CRC(b7fddb12) SHA1(619a75daac8cbba7e85c97ca19733e2196d66d5c) )
282   ROM_LOAD16_BYTE( "worldr20.i11",   0x000001, 0x080000, CRC(58b2809a) SHA1(8741ec544c54e2a2f5d17ac2f8400ee2ce382e83) )
283   ROM_LOAD16_BYTE( "worldr19.i09",   0x100000, 0x080000, CRC(018b35bb) SHA1(ca789e23d18cc7d7e48b6858e6b61e03bf88b475) )
284   ROM_LOAD16_BYTE( "worldr18.i07",   0x100001, 0x080000, CRC(b37c807e) SHA1(9e6155a2b5206c0d4dca669d24d9fe9830027651) )
285//  same data, different layout
286//  ROM_LOAD( "h12.bin",    0x000000, 0x100000, CRC(3353dc00) )
287//  ROM_LOAD( "h8.bin",     0x100000, 0x100000, CRC(58dcd024) )
310   ROM_LOAD16_BYTE( "worldr21.i13", 0x000000, 0x080000, CRC(b7fddb12) SHA1(619a75daac8cbba7e85c97ca19733e2196d66d5c) )
311   ROM_LOAD16_BYTE( "worldr20.i11", 0x000001, 0x080000, CRC(58b2809a) SHA1(8741ec544c54e2a2f5d17ac2f8400ee2ce382e83) )
312   ROM_LOAD16_BYTE( "worldr19.i09", 0x100000, 0x080000, CRC(018b35bb) SHA1(ca789e23d18cc7d7e48b6858e6b61e03bf88b475) )
313   ROM_LOAD16_BYTE( "worldr18.i07", 0x100001, 0x080000, CRC(b37c807e) SHA1(9e6155a2b5206c0d4dca669d24d9fe9830027651) )
288314
289315   ROM_REGION( 0x140000, REGION_SOUND1, 0 )   /* ADPCM samples - sound chip is OKIM6295 */
290316   ROM_LOAD( "worldr14.c01",   0x000000, 0x080000, CRC(e931c2ee) SHA1(ea1cf8ad52713e5136a370e289567eea9e6403d6) )
r2627r2628
300326   ROM_LOAD( "pal16r8-b15.bin",      0x0000, 0x0104, CRC(b50337a6) SHA1(1f922753cb9982cad9a3c9246894ecd38273236e) )
301327ROM_END
302328
303ROM_START( wrallyb ) /* Board Marked 930217 */
329ROM_START( wrallyb ) /* Board Marked 930217, Atari License */
304330   ROM_REGION( 0x100000, REGION_CPU1, 0 )   /* 68000 code */
305   ROM_LOAD16_BYTE( "rally.c23", 0x000000, 0x080000, CRC(366595ad) SHA1(e16341ed9eacf9b729c28184268150ea9b62f185) )
331   ROM_LOAD16_BYTE( "rally.c23", 0x000000, 0x080000, CRC(366595ad) SHA1(e16341ed9eacf9b729c28184268150ea9b62f185) ) /* North & South America only... */
306332   ROM_LOAD16_BYTE( "rally.c22", 0x000001, 0x080000, CRC(0ad4ec6f) SHA1(991557cf25fe960b1c586e990e6019befe5a11d0) )
307333
308   ROM_REGION( 0x400000, REGION_GFX1, ROMREGION_DISPOSE )
309   ROM_LOAD( "rally h-12.h12", 0x000000, 0x200000, CRC(38a44370) SHA1(cb427aa337232ae3a8effab3804d3d1d85d1f40b) )
310   ROM_LOAD( "rally h-8.h8",   0x000000, 0x200000, CRC(c36d1b1e) SHA1(598f72f11100742eda1eda795ded69a0a2d62225) )
334   ROM_REGION( 0x10000, REGION_CPU2, 0 )   /* DS5002FP code */
335   ROM_LOAD( "wrdallas.bin", 0x00000, 0x8000, CRC(547d1768) SHA1(c58d1edd072d796be0663fb265f4739ec006b688) )
311336
312   ROM_REGION( 0x100000, REGION_SOUND1, 0 )   /* ADPCM samples - sound chip is OKIM6295 */
313   ROM_LOAD( "sound c-1.c1", 0x000000, 0x100000, CRC(2d69c9b8) SHA1(328cb3c928dc6921c0c3f0277f59bca6c747c504) )
337   ROM_REGION( 0x200000, REGION_GFX1, ROMREGION_DISPOSE )
338   ROM_LOAD( "rally h-12.h12", 0x000000, 0x100000, CRC(3353dc00) SHA1(db3b1686751dcaa231d66c08b5be81fcfe299ad9) ) /* Same data, different layout */
339   ROM_LOAD( "rally h-8.h8",   0x100000, 0x100000, CRC(58dcd024) SHA1(384ff296d3c7c8e0c4469231d1940de3cea89fc2) )
314340
341   ROM_REGION( 0x140000, REGION_SOUND1, 0 )   /* ADPCM samples - sound chip is OKIM6295 */
342   ROM_LOAD( "sound c-1.c1", 0x000000, 0x100000, CRC(2d69c9b8) SHA1(328cb3c928dc6921c0c3f0277f59bca6c747c504) ) /* Same data in a single rom */
343   ROM_RELOAD(        0x040000, 0x100000 )
344
315345   ROM_REGION( 0x0514, REGION_PLDS, 0 ) /* PAL's and GAL's */
316346   ROM_LOAD( "tibpal20l8-25cnt.b23", 0x0000, 0x0104, NO_DUMP )
317347   ROM_LOAD( "gal16v8-25lnc.h21",    0x0000, 0x0104, NO_DUMP )
r2627r2628
321351ROM_END
322352
323353
324GAME( 1993, wrally,  0,     wrally, wrally, wrally, ROT0, "Gaelco", "World Rally (set 1)", GAME_NOT_WORKING )
325GAME( 1993, wrallya, wrally, wrally, wrally, wrally, ROT0, "Gaelco", "World Rally (set 2)", GAME_NOT_WORKING )
326GAME( 1993, wrallyb, wrally, wrally, wrally, wrally, ROT0, "Gaelco", "World Rally (set 3 - 930217)", GAME_NOT_WORKING )
354GAME( 1993, wrally,  0,      wrally, wrally, wrally, ROT0, "Gaelco", "World Rally (set 1)", 0 ) /* Dallas DS5002FP power failure shows as: "Tension  baja " */
355GAME( 1993, wrallya, wrally, wrally, wrally, wrally, ROT0, "Gaelco", "World Rally (set 2)", 0 ) /* Dallas DS5002FP power failure shows as: "Power  Failure" */
356GAME( 1993, wrallyb, wrally, wrally, wrally, wrally, ROT0, "Gaelco (Atari license)", "World Rally (US, 930217)", 0 )
src/tools/chdman.c
r2627r2628
14301430      }
14311431
14321432      /* write the samples */
1433      avierr = avi_append_sound_samples(avi, chnum, temp, samples);
1433      avierr = avi_append_sound_samples(avi, chnum, temp, samples, 0);
14341434      if (avierr != AVIERR_NONE)
14351435         goto cleanup;
14361436   }
src/emu/debug/debugvw.c
r2627r2628
113113   UINT8         right_column;            /* right column? */
114114   UINT32         backwards_steps;         /* number of backwards steps */
115115   UINT32         dasm_width;               /* width of the disassembly area */
116   UINT8 *         last_opcode_base;         /* last opcode base */
117   UINT8 *         last_opcode_arg_base;      /* last opcode arg base */
116   UINT8 *         last_opbase_rom;         /* last opbase.rom */
117   UINT8 *         last_opbase_ram;         /* last opbase.ram */
118118   UINT32         last_change_count;         /* last comment change count */
119119   offs_t         last_pcbyte;            /* last PC byte value */
120120   UINT32         active_address;            /* the address cursor_row is pointing to */
r2627r2628
14611461
14621462static int disasm_recompute(debug_view *view, offs_t pc, int startline, int lines, int original_cpunum)
14631463{
1464   extern opbase_data opbase;
14641465   debug_view_disasm *dasmdata = view->extra_data;
14651466   const debug_cpu_info *cpuinfo = debug_get_cpu_info(dasmdata->cpunum);
14661467   int chunksize, minbytes, maxbytes, maxbytes_clamped;
r2627r2628
15961597      memory_set_opbase(activecpu_get_physical_pc_byte());
15971598
15981599   /* update opcode base information */
1599   dasmdata->last_opcode_base = opcode_base;
1600   dasmdata->last_opcode_arg_base = opcode_arg_base;
1600   dasmdata->last_opbase_rom = opbase.rom;
1601   dasmdata->last_opbase_ram = opbase.ram;
16011602   dasmdata->last_change_count = debug_comment_all_change_count();
16021603
16031604   /* now longer need to recompute */
r2627r2628
16131614
16141615static void disasm_update(debug_view *view)
16151616{
1617   extern opbase_data opbase;
16161618   debug_view_disasm *dasmdata = view->extra_data;
16171619   const debug_cpu_info *cpuinfo = debug_get_cpu_info(dasmdata->cpunum);
16181620   offs_t pc = cpunum_get_reg(dasmdata->cpunum, REG_PC);
r2627r2628
16761678   }
16771679
16781680   /* if the opcode base has changed, rework things */
1679   if (opcode_base != dasmdata->last_opcode_base || opcode_arg_base != dasmdata->last_opcode_arg_base)
1681   if (opbase.rom != dasmdata->last_opbase_rom || opbase.ram != dasmdata->last_opbase_ram)
16801682      dasmdata->recompute = TRUE;
16811683
16821684   /* if the comments have changed, redo it */
src/emu/cpu/cpu.mak
r2627r2628
1818# Dynamic recompiler objects
1919#-------------------------------------------------
2020
21DRCOBJ = \
22   $(CPUOBJ)/drcbec.o \
23   $(CPUOBJ)/drcbeut.o \
24   $(CPUOBJ)/drccache.o \
25   $(CPUOBJ)/drcfe.o \
26   $(CPUOBJ)/drcuml.o \
27   $(CPUOBJ)/drcumld.o \
28
29DRCDEPS = \
30   $(CPUSRC)/drcbeut.h \
31   $(CPUSRC)/drccache.h \
32   $(CPUSRC)/drcfe.h \
33   $(CPUSRC)/drcuml.h \
34   $(CPUSRC)/drcumld.h \
35   $(CPUSRC)/drcumlsh.h \
36
37# fixme - need to make this work for other target architectures (PPC)
38
39ifndef FORCE_DRC_C_BACKEND
2140ifdef PTR64
2241
23DRCOBJ = $(CPUOBJ)/x64drc.o $(CPUOBJ)/x86log.o $(CPUOBJ)/drcfe.o
42DRCOBJ += \
43   $(CPUOBJ)/drcbex64.o \
44   $(CPUOBJ)/x64drc.o \
45   $(CPUOBJ)/x86log.o \
2446
25DRCDEPS =    $(CPUSRC)/x86emit.h \
26         $(CPUSRC)/x64drc.c \
27         $(CPUSRC)/x64drc.h \
47DRCDEPS += \
48   $(CPUSRC)/x86emit.h \
49   $(CPUSRC)/x64drc.c \
50   $(CPUSRC)/x64drc.h \
2851
29$(DRCOBJ): $(DRCDEPS)
52DEFS += -DNATIVE_DRC=drcbe_x64_be_interface
3053
3154else
3255
33DRCOBJ = $(CPUOBJ)/x86drc.o $(CPUOBJ)/x86log.o $(CPUOBJ)/drcfe.o
56DRCOBJ += \
57   $(CPUOBJ)/drcbex86.o \
58   $(CPUOBJ)/x86drc.o \
59   $(CPUOBJ)/x86log.o \
3460
35DRCDEPS =    $(CPUSRC)/x86emit.h \
36         $(CPUSRC)/x86drc.c \
37         $(CPUSRC)/x86drc.h \
61DRCDEPS += \
62   $(CPUSRC)/x86emit.h \
63   $(CPUSRC)/x86drc.c \
64   $(CPUSRC)/x86drc.h \
3865
39$(DRCOBJ): $(DRCDEPS)
66DEFS += -DNATIVE_DRC=drcbe_x86_be_interface
4067
4168endif
69endif
4270
4371
72$(DRCOBJ): $(DRCDEPS)
73
74
75
4476#-------------------------------------------------
4577# Acorn ARM series
4678#-------------------------------------------------
r2627r2628
585617
586618
587619#-------------------------------------------------
620# DS5002FP
621#-------------------------------------------------
622
623CPUDEFS += -DHAS_DS5002FP=$(if $(filter DS5002FP,$(CPUS)),1,0)
624
625ifneq ($(filter DS5002FP,$(CPUS)),)
626OBJDIRS += $(CPUOBJ)/ds5002fp
627CPUOBJS += $(CPUOBJ)/ds5002fp/ds5002fp.o
628DBGOBJS += $(CPUOBJ)/ds5002fp/ds5002fpdasm.o
629endif
630
631
632
633#-------------------------------------------------
588634# Intel 80x86 series
589635#-------------------------------------------------
590636
r2627r2628
793839
794840ifneq ($(filter R4600 R4650 R4700 R5000 QED5271 RM7000,$(CPUS)),)
795841OBJDIRS += $(CPUOBJ)/mips
796CPUOBJS += $(CPUOBJ)/mips/mips3com.o
842CPUOBJS += $(CPUOBJ)/mips/mips3com.o $(CPUOBJ)/mips/mips3drc.o $(CPUOBJ)/mips/mips3fe.o $(DRCOBJ)
797843DBGOBJS += $(CPUOBJ)/mips/mips3dsm.o
798
799ifdef X86_MIPS3_DRC
800CPUOBJS += $(CPUOBJ)/mips/mips3drc.o $(CPUOBJ)/mips/mips3fe.o $(DRCOBJ)
801else
802CPUOBJS += $(CPUOBJ)/mips/mips3.o
803844endif
804endif
805845
806$(CPUOBJ)/mips/mips3.o:      $(CPUSRC)/mips/mips3.c \
807                     $(CPUSRC)/mips/mips3.h \
808                     $(CPUSRC)/mips/mips3com.h
809
810846$(CPUOBJ)/mips/mips3drc.o:   $(CPUSRC)/mips/mips3drc.c \
811                     $(CPUSRC)/mips/mdrcold.c \
812                     $(CPUSRC)/mips/mdrc64.c \
813847                     $(CPUSRC)/mips/mips3.h \
814848                     $(CPUSRC)/mips/mips3com.h \
815849                     $(CPUSRC)/mips/mips3fe.h \
src/emu/cpu/z180/z180dasm.c
r2627r2628
459459      if( op1 == 0xcb )
460460      {
461461         offset = (INT8) opram[pos++];
462         op1 = opram[pos++]; /* fourth byte from opcode_arg_base! */
462         op1 = opram[pos++]; /* fourth byte from opbase.ram! */
463463         d = &mnemonic_xx_cb[op1];
464464      }
465465      else d = &mnemonic_xx[op1];
r2627r2628
470470      if( op1 == 0xcb )
471471      {
472472         offset = (INT8) opram[pos++];
473         op1 = opram[pos++]; /* fourth byte from opcode_arg_base! */
473         op1 = opram[pos++]; /* fourth byte from opbase.ram! */
474474         d = &mnemonic_xx_cb[op1];
475475      }
476476      else d = &mnemonic_xx[op1];
src/emu/cpu/ds5002fp/ds5002fp.c
r0r2628
1/*****************************************************************************
2 *   DS5002FP emulator by Manuel Abadia
3 *
4 *   The emulator is just a modified version of the MCS-51 Family Emulator by Steve Ellenoff
5 *
6 *   What has been added?
7 *      - Extra SFRs
8 *      - Bytewide Bus Support
9 *      - Memory Partition and Memory Range
10 *      - Bootstrap Configuration
11 *      - Power Fail Interrupt
12 *      - Timed Access
13 *      - Stop Mode
14 *      - Idle Mode
15 *
16 *   What is not implemented?
17 *      - Peripherals and Reprogrammable Peripheral Controller
18 *      - CRC-16
19 *      - Watchdog timer
20 *
21 *   The main features of the DS5002FP are:
22 *      - 100% code-compatible with 8051
23 *      - Directly addresses 64kB program/64kB data memory
24 *      - Nonvolatile memory control circuitry
25 *      - 10-year data retention in the absence of power
26 *      - In-system reprogramming via serial port
27 *      - Dedicated memory bus, preserving four 8-bit ports for general purpose I/O
28 *      - Power-fail reset
29 *      - Early warning power-fail interrupt
30 *      - Watchdog timer
31 *      - Accesses up to 128kB on the bytewide bus
32 *      - Decodes memory for 32kB x 8 or 128kB x 8 SRAMs
33 *      - Four additional decoded peripheral-chip enables
34 *      - CRC hardware for checking memory validity
35 *      - Optionally emulates an 8042-style slave interface
36 *      - Memory encryption using an 80-bit encryption key
37 *      - Automatic random generation of encryption keys
38 *      - Self-destruct input for tamper protection
39 *      - Optional top-coating prevents microprobe
40 *
41 *****************************************************************************/
42
43/*****************************************************************************
44 *
45 *   i8051.c
46 *   Portable MCS-51 Family Emulator
47 *
48 *   Chips in the family:
49 *   8051 Product Line (8031,8051,8751)
50 *   8052 Product Line (8032,8052,8752)
51 *   8054 Product Line (8054)
52 *   8058 Product Line (8058)
53 *
54 *   Copyright Steve Ellenoff, all rights reserved.
55 *
56 *   - This source code is released as freeware for non-commercial purposes.
57 *   - You are free to use and redistribute this code in modified or
58 *     unmodified form, provided you list me in the credits.
59 *   - If you modify this source code, you must add a notice to each modified
60 *     source file that it has been changed.  If you're a nice person, you
61 *     will clearly mark each change too.  :)
62 *   - If you wish to use this for commercial purposes, please contact me at
63 *     sellenoff@hotmail.com
64 *   - The author of this copywritten work reserves the right to change the
65 *     terms of its usage and license at any time, including retroactively
66 *   - This entire notice must remain in the source code.
67 *
68 *  This work is based on:
69 *  #1) 'Intel(tm) MC51 Microcontroller Family Users Manual' and
70 *  #2) 8051 simulator by Travis Marlatte
71 *  #3) Portable UPI-41/8041/8741/8042/8742 emulator V0.1 by Juergen Buchmueller (MAME CORE)
72 *
73 *****************************************************************************/
74
75/******************************************************************************
76 *  Notes:
77 *        *Important*: Internal ROM needs to be treated the same as external rom by the programmer
78 *                     creating the driver (ie, use standard cpu rom region)
79 *
80 *        The term cycles is used here to really refer to clock oscilations, because 1 machine cycle
81 *        actually takes 12 oscilations.
82 *
83 *        Read/Write/Modify Instruction -
84 *          Data is read from the Port Latch (not the Port Pin!), possibly modified, and
85 *          written back to (the pin? and) the latch!
86 *
87 *          The following all perform this on a port address..
88 *          (anl, orl, xrl, jbc, cpl, inc, dec, djnz, mov px.y,c, clr px.y, setb px.y)
89 *
90 *        Serial UART emulation is not really accurate, but faked enough to work as far as i can tell
91 *
92 *        August 27,2003: Currently support for only 8031/8051/8751 chips (ie 128 RAM)
93 *        October 14,2003: Added initial support for the 8752 (ie 256 RAM)
94 *        October 22,2003: Full support for the 8752 (ie 256 RAM)
95 *        July 28,2004: Fixed MOVX command and added External Ram Paging Support
96 *        July 31,2004: Added Serial Mode 0 Support & Fixed Interrupt Flags for Serial Port
97 *
98 *        Todo: Full Timer support (all modes)
99 *
100 *        NOW Implemented: RAM paging using hardware configured addressing...
101 *        (July 28,2004)   the "MOVX a,@R0/R1" and "MOVX @R0/R1,a" commands can use any of the other ports
102 *                 to output a page offset into external ram, but it is totally based on the hardware setup.
103 *
104 *        Timing needs to be implemented via MAME timers perhaps?
105 *
106 *****************************************************************************/
107
108#include "debugger.h"
109#include "deprecat.h"
110#include "ds5002fp.h"
111
112#define VERBOSE 0
113
114#define LOG(x)   do { if (VERBOSE) logerror x; } while (0)
115
116
117//Prototypes
118INLINE void push_pc(void);
119INLINE void pop_pc(void);
120INLINE void set_parity(void);
121INLINE void do_add_flags(UINT8 a, UINT8 data, UINT8 c);
122INLINE void do_sub_flags(UINT8 a, UINT8 data, UINT8 c);
123INLINE UINT8 check_interrupts(void);
124INLINE void update_timer(int cyc);
125INLINE void   update_serial(int cyc);
126INLINE void serial_transmit(UINT8 data);
127static READ8_HANDLER(internal_ram_read);
128static WRITE8_HANDLER(internal_ram_write);
129static READ8_HANDLER(internal_ram_iread);
130static READ32_HANDLER(external_ram_iaddr);
131static WRITE8_HANDLER(internal_ram_iwrite);
132static READ8_HANDLER(sfr_read);
133static WRITE8_HANDLER(sfr_write);
134static WRITE8_HANDLER( bit_address_w );
135static READ8_HANDLER( bit_address_r );
136
137typedef struct {
138   UINT8   timerbaud;      //Flag set if timer overflow is controlling baud
139   UINT8   sending;      //Flag set when uart is sending
140   UINT8   data_out;      //Data to send out
141   UINT8   bits_to_send;   //How many bits left to send when transmitting out the serial port
142   UINT8   bitcycles;      //# of bitcycles passed since last bit was sent
143} DS5002FP_UART;
144
145typedef struct _DS5002FP {
146
147   //Internal stuff
148   UINT16   ppc;         //previous pc
149   UINT16   pc;            //current pc
150   UINT16   subtype;      //specific version of the cpu, ie 8031, or 8051 for example
151   UINT8   cur_irq;      //Holds value of any current IRQ being serviced
152   UINT8   irq_priority;   //Holds value of the current IRQ Priority Level
153   UINT8   rwm;         //Signals that the current instruction is a read/write/modify instruction
154   UINT8   prev_used_cycles;   //Track previous # of used cycles
155   UINT8   last_int0;         //Store state of int0
156   UINT8   last_int1;         //Store state of int1
157   UINT8   last_power_int;      //Store state of last power interrupt
158   UINT8   int_vec;         //Pending Interrupt Vector
159   UINT8   priority_request;    //Priority level of incoming new irq
160   UINT8   previous_ta;      //Previous Timed Access value
161   INT8   ta_window;         //Timed Access window
162   UINT8   range;            //Memory Range
163   const ds5002fp_config *config;   //Bootstrap Configuration
164   //SFR Registers         (Note: Appear in order as they do in memory)
165   UINT8   po;            //Port 0
166   UINT8   sp;            //Stack Pointer
167   UINT8   dpl;         //Data Pointer (DPTR) (Hi bit)
168   UINT8   dph;         //Data Pointer (DPTR) (Lo bit)
169   UINT8   pcon;         //Power Mode Control
170   UINT8   tcon;         //Timer/Counter Control
171   UINT8   tmod;         //Timer/Counter Mode Control
172   UINT8   tl0;         //Timer 0 Lo
173   UINT8   tl1;         //Timer 1 Lo
174   UINT8   th0;         //Timer 0 Hi
175   UINT8   th1;         //Timer 1 Hi
176   UINT8   p1;            //Port 1
177   UINT8   scon;         //Serial Control
178   UINT8   sbuf;         //Serial Data Buffer
179   UINT8   p2;            //Port 2
180   UINT8   ie;            //Interrupt Enable
181   UINT8   p3;            //Port 3
182   UINT8   ip;            //Interrupt Priority
183   UINT8   crc;         //CRC
184   UINT8   crcl;         //CRC Result (low nibble)
185   UINT8   crch;         //CRC Result (high nibble)
186   UINT8   mcon;         //Memory Control
187   UINT8   ta;            //Timed Access
188   UINT8   rnr;         //Random Number Generator
189   UINT8   psw;         //Program Status Word
190   UINT8   rpctl;         //RPC control
191   UINT8   rps;         //RPC status
192   UINT8   acc;         //Accumulator
193   UINT8   b;            //Register B
194
195   //Internal Ram
196   UINT8   IntRam[0xff+1];   //Max 256 Bytes of Internal RAM (8031/51 have 128, 8032/52 have 256)
197
198   //Interrupt Callback
199   int    (*irq_callback)(int irqline);
200
201   //Serial Port TX/RX Call backs
202   void    (*serial_tx_callback)(int data);   //Call back funciton when sending data out of serial port
203   int      (*serial_rx_callback)(void);      //Call back function to retrieve data when receiving serial port data
204
205   //Internal Indirect Read/Write Handlers
206   READ8_HANDLER((*iram_iread));
207   WRITE8_HANDLER((*iram_iwrite));
208
209   //External/Bytewide Ram Address Callback for generating the hardware specific access to external ram
210   READ32_HANDLER((*ebram_iaddr_callback));
211
212} DS5002FP;
213
214static int ds5002fp_icount;
215
216static DS5002FP ds5002fp;
217static DS5002FP_UART uart;
218
219//Hold callback functions so they can be set by caller (before the cpu reset)
220static void (*hold_serial_tx_callback)(int data);
221static int (*hold_serial_rx_callback)(void);
222static READ32_HANDLER((*hold_ebram_iaddr_callback));
223
224/*Short cuts*/
225
226/***************************************************************
227 * Read Opcode/Opcode Arguments from Program Code
228 ***************************************************************/
229#define ROP(pc)         cpu_readop(pc)
230#define ROP_ARG(pc)      cpu_readop_arg(pc)
231/*****************************************************************************
232 * Read a byte from External Code Memory (Usually Program Rom(s) Space)
233 *****************************************************************************
234 This area is mapped from 0-FFFF internally (64K) */
235#define CODEMEM_R(a)   (UINT8)program_read_byte_8le(a)
236/*****************************************************************************
237 * Read/Write a byte from/to Expanded/Byte-wide Data Memory (Usually RAM or other I/O)
238 *****************************************************************************
239 This area is mapped from 0-FFFF internally (64K) */
240#define DATAMEM_R(a)   (UINT8)data_read_byte_8le(a)
241#define DATAMEM_W(a,v)   data_write_byte_8le(a,v)
242
243/***************************************************************
244 * Read/Write a byte from/to the Internal RAM
245 ***************************************************************/
246#define IRAM_R(a)      internal_ram_read(Machine,a)
247#define IRAM_W(a,v)      internal_ram_write(Machine,a,v)
248/***************************************************************
249 * Read/Write a byte from/to the Internal RAM indirectly (called from indirect addressing)
250 ***************************************************************/
251#define IRAM_IR(a)      internal_ram_iread(Machine,a)
252#define IRAM_IW(a,v)   internal_ram_iwrite(Machine,a,v)
253/***************************************************************
254 * Form an Address to Read/Write to External RAM indirectly (called from indirect addressing)
255 ***************************************************************/
256#define ERAM_ADDR(a,m)   external_ram_iaddr(Machine,a,m)
257/***************************************************************
258 * Read/Write a byte from/to the SFR Registers
259 ***************************************************************/
260#define SFR_R(a)      sfr_read(Machine,a)
261#define SFR_W(a,v)      sfr_write(Machine,a,v)
262/***************************************************************
263 * Read/Write a bit from Bit Addressable Memory
264 ***************************************************************/
265#define BIT_R(a)      bit_address_r(Machine,a)
266#define BIT_W(a,v)      bit_address_w(Machine,a,v)
267/***************************************************************
268 * Input/Output a byte from given I/O port
269 ***************************************************************/
270#define IN(port)      ((UINT8)io_read_byte(port))
271#define OUT(port,value) io_write_byte(port,value)
272/***************************************************************
273 * Access the 4 banks of R registers (R0...R7)
274 ***************************************************************/
275#define R_R(n)         ds5002fp.IntRam[(GET_RS*8)+(n)]
276/***************************************************************
277 * Easy macro for working with 16 bit DPTR
278 ***************************************************************/
279#define R_DPTR         ((R_DPH<<8) | R_DPL)
280#define DPTR_W(n)      SFR_W(DPH, ((n)>>8)&0xff);\
281                  SFR_W(DPL, ((n)&0xff));
282/***************************************************************
283 * Easy macros for Setting Flags
284 ***************************************************************/
285/*PSW Flags*/
286#define SET_CY(n)      R_PSW = (R_PSW & 0x7f) | (n<<7);   //Carry Flag
287#define SET_AC(n)      R_PSW = (R_PSW & 0xbf) | (n<<6);   //Aux.Carry Flag
288#define SET_FO(n)      R_PSW = (R_PSW & 0xdf) | (n<<5);   //User Flag
289#define SET_RS(n)      R_PSW = (R_PSW & 0xe7) | (n<<3);   //R Bank Select
290#define SET_OV(n)      R_PSW = (R_PSW & 0xfb) | (n<<2);   //Overflow Flag
291#define SET_P(n)      R_PSW = (R_PSW & 0xfe) | (n<<0);   //Parity Flag
292/*IE Flags*/
293#define SET_EA(n)      R_IE = (R_IE & 0x7f) | (n<<7);      //Global Interrupt Enable/Disable
294#define SET_ES(n)      R_IE = (R_IE & 0xef) | (n<<4);      //Serial Interrupt Enable/Disable
295#define SET_ET1(n)      R_IE = (R_IE & 0xf7) | (n<<3);      //Timer 1 Interrupt Enable/Disable
296#define SET_EX1(n)      R_IE = (R_IE & 0xfb) | (n<<2);      //External Int 1 Interrupt Enable/Disable
297#define SET_ET0(n)      R_IE = (R_IE & 0xfd) | (n<<1);      //Timer 0 Interrupt Enable/Disable
298#define SET_EX0(n)      R_IE = (R_IE & 0xfe) | (n<<0);      //External Int 0 Interrupt Enable/Disable
299/*IP Flags*/
300#define SET_PS0(n)      R_IP = (R_IP & 0xef) | (n<<4);      //Set Serial Priority Level
301#define SET_PT1(n)      R_IP = (R_IP & 0xf7) | (n<<3);      //Set Timer 1 Priority Level
302#define SET_PX1(n)      R_IP = (R_IP & 0xfb) | (n<<2);      //Set External Int 1 Priority Level
303#define SET_PT0(n)      R_IP = (R_IP & 0xfd) | (n<<1);      //Set Timer 0 Priority Level
304#define SET_PX0(n)      R_IP = (R_IP & 0xfe) | (n<<0);      //Set External Int 0 Priority Level
305/*TCON Flags*/
306#define SET_TF1(n)      R_TCON = (R_TCON & 0x7f) | (n<<7);   //Indicated Timer 1 Overflow Int Triggered
307#define SET_TR1(n)      R_TCON = (R_TCON & 0xbf) | (n<<6);  //IndicateS Timer 1 is running
308#define SET_TF0(n)      R_TCON = (R_TCON & 0xdf) | (n<<5);   //Indicated Timer 0 Overflow Int Triggered
309#define SET_TR0(n)      R_TCON = (R_TCON & 0xef) | (n<<4);  //IndicateS Timer 0 is running
310#define SET_IE1(n)      R_TCON = (R_TCON & 0xf7) | (n<<3);  //Indicated External Int 1 Triggered
311#define SET_IT1(n)      R_TCON = (R_TCON & 0xfb) | (n<<2);  //Indicates how External Int 1 is Triggered
312#define SET_IE0(n)      R_TCON = (R_TCON & 0xfd) | (n<<1);  //Indicated External Int 0 Triggered
313#define SET_IT0(n)      R_TCON = (R_TCON & 0xfe) | (n<<0);  //Indicates how External Int 0 is Triggered
314/*SCON Flags*/
315#define SET_SM0(n)      R_SCON = (R_SCON & 0x7f) | (n<<7);   //Sets Serial Port Mode
316#define SET_SM1(n)      R_SCON = (R_SCON & 0xbf) | (n<<6);  //Sets Serial Port Mode
317#define SET_SM2(n)      R_SCON = (R_SCON & 0xdf) | (n<<5);   //Sets Serial Port Mode (Multiprocesser mode)
318#define SET_REN(n)      R_SCON = (R_SCON & 0xef) | (n<<4);  //Sets Serial Port Receive Enable
319#define SET_TB8(n)      R_SCON = (R_SCON & 0xf7) | (n<<3);  //Transmit 8th Bit
320#define SET_RB8(n)      R_SCON = (R_SCON & 0xfb) | (n<<2);  //Receive 8th Bit
321#define SET_TI(n)      R_SCON = (R_SCON & 0xfd) | (n<<1);  //Indicates Transmit Interrupt Occurred
322#define SET_RI(n)      R_SCON = (R_SCON & 0xfe) | (n<<0);  //Indicates Receive Interrupt Occurred
323/*TMOD Flags*/
324#define SET_GATE1(n)   R_TMOD = (R_TMOD & 0x7f) | (n<<7);   //Timer 1 Gate Mode
325#define SET_CT1(n)      R_TMOD = (R_TMOD & 0xbf) | (n<<6);  //Timer 1 Counter Mode
326#define SET_M1_1(n)      R_TMOD = (R_TMOD & 0xdf) | (n<<5);   //Timer 1 Timer Mode Bit 1
327#define SET_M1_0(n)      R_TMOD = (R_TMOD & 0xef) | (n<<4);  //Timer 1 Timer Mode Bit 0
328#define SET_GATE0(n)   R_TMOD = (R_TMOD & 0xf7) | (n<<3);  //Timer 0 Gate Mode
329#define SET_CT0(n)      R_TMOD = (R_TMOD & 0xfb) | (n<<2);  //Timer 0 Counter Mode
330#define SET_M0_1(n)      R_TMOD = (R_TMOD & 0xfd) | (n<<1);  //Timer 0 Timer Mode Bit 1
331#define SET_M0_0(n)      R_TMOD = (R_TMOD & 0xfe) | (n<<0);  //Timer 0 Timer Mode Bit 0
332/*PCON Flags*/
333#define SET_POR(n)      R_PCON = (R_PCON & 0xbf) | (n<<6);  //Power On Reset
334#define SET_PFW(n)      R_PCON = (R_PCON & 0xdf) | (n<<5);   //Power Fail Warning
335#define SET_WTR(n)      R_PCON = (R_PCON & 0xef) | (n<<4);  //Watchdog Timer Reset
336#define SET_EPFW(n)      R_PCON = (R_PCON & 0xf7) | (n<<3);  //Enable Power Fail Interrupt
337#define SET_EWT(n)      R_PCON = (R_PCON & 0xfb) | (n<<2);  //Enable Watchdog Timer
338#define SET_STOP(n)      R_PCON = (R_PCON & 0xfd) | (n<<1);  //Stop
339#define SET_IDLE(n)      R_PCON = (R_PCON & 0xfe) | (n<<0);  //Idle
340
341/***************************************************************
342 * Easy macros for Getting Flags
343 ***************************************************************/
344/*PSW Flags*/
345#define GET_CY         ((R_PSW & 0x80)>>7)
346#define GET_AC         ((R_PSW & 0x40)>>6)
347#define GET_FO         ((R_PSW & 0x20)>>5)
348#define GET_RS         ((R_PSW & 0x18)>>3)
349#define GET_OV         ((R_PSW & 0x04)>>2)
350#define GET_P         ((R_PSW & 0x01)>>0)
351/*IE Flags*/
352#define GET_EA         ((R_IE & 0x80)>>7)
353#define GET_ET2         ((R_IE & 0x20)>>5)
354#define GET_ES         ((R_IE & 0x10)>>4)
355#define GET_ET1         ((R_IE & 0x08)>>3)
356#define GET_EX1         ((R_IE & 0x04)>>2)
357#define GET_ET0         ((R_IE & 0x02)>>1)
358#define GET_EX0         ((R_IE & 0x01)>>0)
359#define GET_PS         ((R_IP & 0x10)>>4)
360#define GET_PT1         ((R_IP & 0x08)>>3)
361#define GET_PX1         ((R_IP & 0x04)>>2)
362#define GET_PT0         ((R_IP & 0x02)>>1)
363#define GET_PX0         ((R_IP & 0x01)>>0)
364/*TCON Flags*/
365#define GET_TF1         ((R_TCON & 0x80)>>7)
366#define GET_TR1         ((R_TCON & 0x40)>>6)
367#define GET_TF0         ((R_TCON & 0x20)>>5)
368#define GET_TR0         ((R_TCON & 0x10)>>4)
369#define GET_IE1         ((R_TCON & 0x08)>>3)
370#define GET_IT1         ((R_TCON & 0x04)>>2)
371#define GET_IE0         ((R_TCON & 0x02)>>1)
372#define GET_IT0         ((R_TCON & 0x01)>>0)
373/*SCON Flags*/
374#define GET_SM0         ((R_SCON & 0x80)>>7)
375#define GET_SM1         ((R_SCON & 0x40)>>6)
376#define GET_SM2         ((R_SCON & 0x20)>>5)
377#define GET_REN         ((R_SCON & 0x10)>>4)
378#define GET_TB8         ((R_SCON & 0x08)>>3)
379#define GET_RB8         ((R_SCON & 0x04)>>2)
380#define GET_TI         ((R_SCON & 0x02)>>1)
381#define GET_RI         ((R_SCON & 0x01)>>0)
382/*TMOD Flags*/
383#define GET_GATE1      ((R_TMOD & 0x80)>>7)
384#define GET_CT1         ((R_TMOD & 0x40)>>6)
385#define GET_M1_1      ((R_TMOD & 0x20)>>5)
386#define GET_M1_0      ((R_TMOD & 0x10)>>4)
387#define GET_GATE0      ((R_TMOD & 0x08)>>3)
388#define GET_CT0         ((R_TMOD & 0x04)>>2)
389#define GET_M0_1      ((R_TMOD & 0x02)>>1)
390#define GET_M0_0      ((R_TMOD & 0x01)>>0)
391/*PCON Flags*/
392#define GET_SMOD      ((R_PCON & 0x80)>>7)
393#define GET_POR         ((R_PCON & 0x40)>>6)
394#define GET_PFW         ((R_PCON & 0x20)>>5)
395#define GET_WTR         ((R_PCON & 0x10)>>4)
396#define GET_EPFW      ((R_PCON & 0x08)>>3)
397#define GET_EWT         ((R_PCON & 0x04)>>2)
398#define GET_STOP      ((R_PCON & 0x02)>>1)
399#define GET_IDLE      ((R_PCON & 0x01)>>0)
400/*MCON Flags*/
401#define GET_PA         ((R_MCON & 0xf0)>>4)
402#define GET_RG1         ((R_MCON & 0x08)>>3)
403#define GET_PES         ((R_MCON & 0x04)>>2)
404#define GET_PM         ((R_MCON & 0x02)>>1)
405#define GET_SL         ((R_MCON & 0x01)>>0)
406/*RPCTL Flags*/
407#define GET_RNR         ((R_RPCTL & 0x80)>>7)
408#define GET_EXBS      ((R_RPCTL & 0x20)>>5)
409#define GET_AE         ((R_RPCTL & 0x10)>>4)
410#define GET_IBI         ((R_RPCTL & 0x08)>>3)
411#define GET_DMA         ((R_RPCTL & 0x04)>>2)
412#define GET_RPCON      ((R_RPCTL & 0x02)>>1)
413#define GET_RG0         ((R_RPCTL & 0x01)>>0)
414
415/*Add and Subtract Flag settings*/
416#define DO_ADD_FLAGS(a,d,c)   do_add_flags(a,d,c);
417#define DO_SUB_FLAGS(a,d,c)   do_sub_flags(a,d,c);
418
419#define SET_PARITY   set_parity();
420#define PUSH_PC      push_pc();
421#define POP_PC      pop_pc();
422
423/* PC vectors */
424#define V_RESET 0x000   /* power on address */
425#define V_IE0   0x003   /* External Interrupt 0 */
426#define V_TF0   0x00b   /* Timer 0 Overflow */
427#define V_IE1   0x013   /* External Interrupt 1 */
428#define V_TF1   0x01b   /* Timer 1 Overflow */
429#define V_RITI   0x023   /* Serial Receive/Transmit */
430#define V_PFI   0x02b   /* Power Failure Interrupt */
431
432/* Any pending IRQ */
433#define SERIALPORT_IRQ    ((R_SCON & 0x03) && GET_ES)
434
435/* Clear Current IRQ  */
436#define CLEAR_CURRENT_IRQ ds5002fp.cur_irq = 0xff;\
437                    ds5002fp.irq_priority = 0;
438
439/* shorter names for the DS5002FP structure elements */
440
441//Internal stuff
442#define PPC    ds5002fp.ppc
443#define PC      ds5002fp.pc
444#define TYPE   ds5002fp.subtype
445#define RWM      ds5002fp.rwm
446
447//SFR Registers
448#define R_P0   ds5002fp.po
449#define R_SP   ds5002fp.sp
450#define R_DPL   ds5002fp.dpl
451#define R_DPH   ds5002fp.dph
452#define R_PCON   ds5002fp.pcon
453#define R_TCON   ds5002fp.tcon
454#define R_TMOD   ds5002fp.tmod
455#define R_TL0   ds5002fp.tl0
456#define R_TL1   ds5002fp.tl1
457#define R_TH0   ds5002fp.th0
458#define R_TH1   ds5002fp.th1
459#define R_P1   ds5002fp.p1
460#define R_SCON   ds5002fp.scon
461#define R_SBUF   ds5002fp.sbuf
462#define R_P2   ds5002fp.p2
463#define R_IE   ds5002fp.ie
464#define R_P3   ds5002fp.p3
465#define R_IP   ds5002fp.ip
466#define R_CRC   ds5002fp.crc
467#define R_CRCL   ds5002fp.crcl
468#define R_CRCH   ds5002fp.crch
469#define R_MCON   ds5002fp.mcon
470#define R_TA   ds5002fp.ta
471#define R_RNR   ds5002fp.rnr
472#define R_PSW   ds5002fp.psw
473#define R_RPCTL   ds5002fp.rpctl
474#define R_RPS   ds5002fp.rps
475#define R_ACC   ds5002fp.acc
476#define R_B      ds5002fp.b
477
478/* # of oscilations each opcode requires*/
479static const UINT8 ds5002fp_cycles[] = {
480   12,24,24,12,12,12,12,12,12,12,12,12,12,12,12,12,
481   24,24,24,12,12,12,12,12,12,12,12,12,12,12,12,12,
482   24,24,24,12,12,12,12,12,12,12,12,12,12,12,12,12,
483   24,24,24,12,12,12,12,12,12,12,12,12,12,12,12,12,
484   24,24,12,24,12,12,12,12,12,12,12,12,12,12,12,12,
485   24,24,12,24,12,12,12,12,12,12,12,12,12,12,12,12,
486   24,24,12,24,12,12,12,12,12,12,12,12,12,12,12,12,
487   24,24,24,24,12,24,12,12,12,12,12,12,12,12,12,12,
488   24,24,24,24,48,24,24,24,24,24,24,24,24,24,24,24,
489   24,24,24,24,12,12,12,12,12,12,12,12,12,12,12,12,
490   24,24,12,24,48,12,24,24,24,24,24,24,24,24,24,24,
491   24,24,12,12,24,24,24,24,24,24,24,24,24,24,24,24,
492   24,24,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
493   24,24,12,12,12,24,12,12,24,24,24,24,24,24,24,24,
494   24,24,24,24,12,12,12,12,12,12,12,12,12,12,12,12,
495   24,24,24,24,12,12,12,12,12,12,12,12,12,12,12,12
496};
497
498/* default configuration */
499static const ds5002fp_config default_config =
500{
501   0x00,
502   0x00,
503   0x00
504};
505
506/* Memory Range (RG1 and RG0 @ MCON and RPCTL registers) */
507static const UINT16 ds5002fp_ranges[4] = {
508   0x1fff,
509   0x3fff,
510   0x7fff,
511   0xffff
512};
513
514/* Memory Partition Table (RG1 & RG0 @ MCON & RPCTL registers) */
515static const UINT32 ds5002fp_partitions[16] = {
516   0x0000,
517   0x1000,
518   0x2000,
519   0x3000,
520   0x4000,
521   0x5000,
522   0x6000,
523   0x7000,
524   0x8000,
525   0x9000,
526   0xa000,
527   0xb000,
528   0xc000,
529   0xd000,
530   0xe000,
531   0x10000
532};
533
534/* Include Opcode functions */
535#include "ds5002fpops.c"
536
537void ds5002fp_init(int index, int clock, const void *_config, int (*irqcallback)(int))
538{
539   const ds5002fp_config *config = _config ? _config : &default_config;
540
541   ds5002fp.config = config;
542   ds5002fp_set_irq_callback(irqcallback);
543
544   //Internal stuff
545   state_save_register_item("ds5002fp", index, ds5002fp.ppc);
546   state_save_register_item("ds5002fp", index, ds5002fp.pc);
547   state_save_register_item("ds5002fp", index, ds5002fp.subtype);
548   state_save_register_item("ds5002fp", index, ds5002fp.rwm );
549   state_save_register_item("ds5002fp", index, ds5002fp.cur_irq );
550   state_save_register_item("ds5002fp", index, ds5002fp.irq_priority );
551   state_save_register_item("ds5002fp", index, ds5002fp.prev_used_cycles );
552   state_save_register_item("ds5002fp", index, ds5002fp.last_int0 );
553   state_save_register_item("ds5002fp", index, ds5002fp.last_int1 );
554   state_save_register_item("ds5002fp", index, ds5002fp.last_power_int );
555   state_save_register_item("ds5002fp", index, ds5002fp.int_vec );
556   state_save_register_item("ds5002fp", index, ds5002fp.priority_request );
557   state_save_register_item("ds5002fp", index, ds5002fp.previous_ta );
558   state_save_register_item("ds5002fp", index, ds5002fp.ta_window );
559   state_save_register_item("ds5002fp", index, ds5002fp.range );
560   //SFR Registers
561   state_save_register_item("ds5002fp", index, ds5002fp.po);
562   state_save_register_item("ds5002fp", index, ds5002fp.sp);
563   state_save_register_item("ds5002fp", index, ds5002fp.dpl);
564   state_save_register_item("ds5002fp", index, ds5002fp.dph);
565   state_save_register_item("ds5002fp", index, ds5002fp.pcon);
566   state_save_register_item("ds5002fp", index, ds5002fp.tcon);
567   state_save_register_item("ds5002fp", index, ds5002fp.tmod);
568   state_save_register_item("ds5002fp", index, ds5002fp.tl0);
569   state_save_register_item("ds5002fp", index, ds5002fp.tl1);
570   state_save_register_item("ds5002fp", index, ds5002fp.th0);
571   state_save_register_item("ds5002fp", index, ds5002fp.th1);
572   state_save_register_item("ds5002fp", index, ds5002fp.p1);
573   state_save_register_item("ds5002fp", index, ds5002fp.scon);
574   state_save_register_item("ds5002fp", index, ds5002fp.sbuf);
575   state_save_register_item("ds5002fp", index, ds5002fp.p2);
576   state_save_register_item("ds5002fp", index, ds5002fp.ie);
577   state_save_register_item("ds5002fp", index, ds5002fp.p3);
578   state_save_register_item("ds5002fp", index, ds5002fp.ip);
579   state_save_register_item("ds5002fp", index, ds5002fp.crc);
580   state_save_register_item("ds5002fp", index, ds5002fp.crcl);
581   state_save_register_item("ds5002fp", index, ds5002fp.crch);
582   state_save_register_item("ds5002fp", index, ds5002fp.mcon);
583   state_save_register_item("ds5002fp", index, ds5002fp.ta);
584   state_save_register_item("ds5002fp", index, ds5002fp.rnr);
585   state_save_register_item("ds5002fp", index, ds5002fp.psw);
586   state_save_register_item("ds5002fp", index, ds5002fp.rpctl);
587   state_save_register_item("ds5002fp", index, ds5002fp.rps);
588   state_save_register_item("ds5002fp", index, ds5002fp.acc);
589   state_save_register_item("ds5002fp", index, ds5002fp.b);
590   state_save_register_item_array("ds5002fp", index, ds5002fp.IntRam);
591}
592
593/* Reset registers to the initial values */
594void ds5002fp_reset(void)
595{
596   int (*save_irqcallback)(int);
597   const ds5002fp_config *save_config;
598
599   save_irqcallback = ds5002fp.irq_callback;
600   save_config = ds5002fp.config;
601   memset(&ds5002fp, 0, sizeof(DS5002FP));
602   ds5002fp.irq_callback = save_irqcallback;
603   ds5002fp.config = save_config;
604
605   memset(&uart, 0, sizeof(DS5002FP_UART));
606   ds5002fp.subtype = 8051;
607
608   //Set up 8051 specific internal read/write (indirect) handlers..
609   ds5002fp.iram_iread = internal_ram_read;      //Indirect ram read/write handled the same as direct for 8051!
610   ds5002fp.iram_iwrite = internal_ram_write;      //Indirect ram read/write handled the same as direct for 8051!
611
612   //Set up serial call back handlers
613   ds5002fp.serial_tx_callback = hold_serial_tx_callback;
614   hold_serial_tx_callback = NULL;
615   ds5002fp.serial_rx_callback = hold_serial_rx_callback;
616   hold_serial_rx_callback = NULL;
617
618   //Setup Expanded/Byte-wide ram callback handlers
619   ds5002fp.ebram_iaddr_callback = hold_ebram_iaddr_callback;
620   hold_ebram_iaddr_callback = NULL;
621
622   //Clear Ram (w/0xff)
623   memset(&ds5002fp.IntRam, 0xff, sizeof(ds5002fp.IntRam));
624
625   /* these are all defined reset states */
626   PC = 0;
627   SFR_W(ACC, 0);
628   SFR_W(B, 0);
629   SFR_W(PSW, 0);
630   SFR_W(SP, 0x7);
631   SFR_W(DPH, 0);
632   SFR_W(DPL, 0);
633   SFR_W(P3, 0xff);
634   SFR_W(P2, 0xff);
635   SFR_W(P1, 0xff);
636   SFR_W(P0, 0xff);
637   SFR_W(IP, 0);
638   SFR_W(IE, 0);
639   SFR_W(TMOD, 0);
640   SFR_W(TCON, 0);
641   SFR_W(TH1, 0);
642   SFR_W(TH0, 0);
643   SFR_W(TL1, 0);
644   SFR_W(TL0, 0);
645   SFR_W(SCON, 0);
646
647   // set initial values (some of them are set using the bootstrap loader)
648   R_PCON = 0;
649   R_MCON = ds5002fp.config->mcon & 0xfb;
650   R_RPCTL = ds5002fp.config->rpctl & 0x01;
651   R_RPS = 0;
652   R_RNR = 0;
653   R_CRC = ds5002fp.config->crc & 0xf0;
654   R_CRCL = 0;
655   R_CRCH = 0;
656   R_TA = 0;
657
658   /* Flag as NO IRQ in Progress */
659   CLEAR_CURRENT_IRQ
660
661   // set internal CPU state
662   ds5002fp.last_power_int = CLEAR_LINE;
663   ds5002fp.previous_ta = 0;
664   ds5002fp.ta_window = 0;
665   ds5002fp.range = (GET_RG1 << 1) | GET_RG0;
666}
667
668/* Shut down CPU core */
669void ds5002fp_exit(void)
670{
671   /* nothing to do */
672}
673
674/* Execute cycles - returns number of cycles actually run */
675int ds5002fp_execute(int cycles)
676{
677   //In stop mode, no op takes place
678   if (R_PCON & 0x02) {
679      return cycles;
680   }
681
682   ds5002fp_icount = cycles;
683
684   //In idle mode, update timers and serial port (not sure if we need to do more things here)
685   if (R_PCON & 0x01) {
686      do {
687         //Update Timer (if any timers are running)
688         if((GET_TR0 && GET_ET0) || (GET_TR1 && GET_ET1))
689            update_timer(12);
690
691         //Update Serial (if serial port sending data)
692         if(uart.sending)
693            update_serial(12);
694
695         ds5002fp_icount -= 12;
696      } while( ds5002fp_icount > 0 );
697
698      return cycles - ds5002fp_icount;
699   }
700
701   do {
702      //Read next opcode
703      UINT8 op = cpu_readop(PC);
704
705      //Store previous PC
706      PPC = PC;
707
708      //Call Debugger
709      CALL_DEBUGGER(PC);
710
711      //remove after testing
712      if(PC != PPC)   op = cpu_readop(PC);
713
714      //Update Timer (if any timers are running)
715      if((GET_TR0 && GET_ET0) || (GET_TR1 && GET_ET1))
716         update_timer(ds5002fp.prev_used_cycles);
717
718      //Update Serial (if serial port sending data)
719      if(uart.sending)
720         update_serial(ds5002fp.prev_used_cycles);
721
722      //Update PC
723      PC += 1;
724      //Decrement total count by # of cycles used for this opcode
725      ds5002fp_icount -= (ds5002fp_cycles[op]);
726
727      switch( op )
728      {
729         //NOP
730         case 0x00:                  /* 1: 0000 0000 */
731            nop();
732            break;
733         //AJMP code addr                /* 1: aaa0 0001 */
734         case 0x01:
735            ajmp();
736            break;
737         //LJMP code addr
738         case 0x02:                  /* 1: 0000 0010 */
739            ljmp();
740            break;
741         //RR A
742         case 0x03:                  /* 1: 0000 0011 */
743            rr_a();
744            break;
745         //INC A
746         case 0x04:                  /* 1: 0000 0100 */
747            inc_a();
748            break;
749         //INC data addr
750         case 0x05:                  /* 1: 0000 0101 */
751            RWM=1;
752            inc_mem();
753            RWM=0;
754         break;
755         //INC @R0/@R1                   /* 1: 0000 011i */
756         case 0x06:
757         case 0x07:
758            inc_ir(op&1);
759            break;
760         //INC R0 to R7                  /* 1: 0000 1rrr */
761         case 0x08:
762         case 0x09:
763         case 0x0a:
764         case 0x0b:
765         case 0x0c:
766         case 0x0d:
767         case 0x0e:
768         case 0x0f:
769            inc_r(op&7);
770            break;
771         //JBC bit addr, code addr
772         case 0x10:                  /* 1: 0001 0000 */
773            RWM=1;
774            jbc();
775            RWM=0;
776            break;
777         //ACALL code addr               /* 1: aaa1 0001 */
778         case 0x11:
779            acall();
780            break;
781         //LCALL code addr
782         case 0x12:                  /* 1: 0001 0010 */
783            lcall();
784            break;
785         //RRC A
786         case 0x13:                  /* 1: 0001 0011 */
787            rrc_a();
788            break;
789         //DEC A
790         case 0x14:                  /* 1: 0001 0100 */
791            dec_a();
792            break;
793         //DEC data addr
794         case 0x15:                  /* 1: 0001 0101 */
795            RWM=1;
796            dec_mem();
797            RWM=0;
798            break;
799         //DEC @R0/@R1                   /* 1: 0001 011i */
800         case 0x16:
801         case 0x17:
802            dec_ir(op&1);
803            break;
804         //DEC R0 to R7                  /* 1: 0001 1rrr */
805         case 0x18:
806         case 0x19:
807         case 0x1a:
808         case 0x1b:
809         case 0x1c:
810         case 0x1d:
811         case 0x1e:
812         case 0x1f:
813            dec_r(op&7);
814            break;
815         //JB  bit addr, code addr
816         case 0x20:                  /* 1: 0010 0000 */
817            jb();
818            break;
819         //AJMP code addr                /* 1: aaa0 0001 */
820         case 0x21:
821            ajmp();
822            break;
823         //RET
824         case 0x22:                  /* 1: 0010 0010 */
825            ret();
826            break;
827         //RL A
828         case 0x23:                  /* 1: 0010 0011 */
829            rl_a();
830            break;
831         //ADD A, #data
832         case 0x24:                  /* 1: 0010 0100 */
833            add_a_byte();
834            break;
835         //ADD A, data addr
836         case 0x25:                  /* 1: 0010 0101 */
837            add_a_mem();
838            break;
839         //ADD A, @R0/@R1                /* 1: 0010 011i */
840         case 0x26:
841         case 0x27:
842            add_a_ir(op&1);
843            break;
844         //ADD A, R0 to R7               /* 1: 0010 1rrr */
845         case 0x28:
846         case 0x29:
847         case 0x2a:
848         case 0x2b:
849         case 0x2c:
850         case 0x2d:
851         case 0x2e:
852         case 0x2f:
853            add_a_r(op&7);
854            break;
855         //JNB bit addr, code addr
856         case 0x30:                  /* 1: 0011 0000 */
857            jnb();
858            break;
859         //ACALL code addr               /* 1: aaa1 0001 */
860         case 0x31:
861            acall();
862            break;
863         //RETI
864         case 0x32:                  /* 1: 0011 0010 */
865            reti();
866            break;
867         //RLC A
868         case 0x33:                  /* 1: 0011 0011 */
869            rlc_a();
870            break;
871         //ADDC A, #data
872         case 0x34:                  /* 1: 0011 0100 */
873            addc_a_byte();
874            break;
875         //ADDC A, data addr
876         case 0x35:                  /* 1: 0011 0101 */
877            addc_a_mem();
878            break;
879         //ADDC A, @R0/@R1               /* 1: 0011 011i */
880         case 0x36:
881         case 0x37:
882            addc_a_ir(op&1);
883            break;
884         //ADDC A, R0 to R7              /* 1: 0011 1rrr */
885         case 0x38:
886         case 0x39:
887         case 0x3a:
888         case 0x3b:
889         case 0x3c:
890         case 0x3d:
891         case 0x3e:
892         case 0x3f:
893            addc_a_r(op&7);
894            break;
895         //JC code addr
896         case 0x40:                  /* 1: 0100 0000 */
897            jc();
898            break;
899         //AJMP code addr                /* 1: aaa0 0001 */
900         case 0x41:
901            ajmp();
902            break;
903         //ORL data addr, A
904         case 0x42:                  /* 1: 0100 0010 */
905            RWM=1;
906            orl_mem_a();
907            RWM=0;
908            break;
909         //ORL data addr, #data
910         case 0x43:                  /* 1: 0100 0011 */
911            RWM=1;
912            orl_mem_byte();
913            RWM=0;
914            break;
915         //ORL A, #data
916         case 0x44:                  /* 1: 0100 0100 */
917            orl_a_byte();
918            break;
919         //ORL A, data addr
920         case 0x45:                  /* 1: 0100 0101 */
921            orl_a_mem();
922            break;
923         //ORL A, @RO/@R1                /* 1: 0100 011i */
924         case 0x46:
925         case 0x47:
926            orl_a_ir(op&1);
927            break;
928         //ORL A, RO to R7               /* 1: 0100 1rrr */
929         case 0x48:
930         case 0x49:
931         case 0x4a:
932         case 0x4b:
933         case 0x4c:
934         case 0x4d:
935         case 0x4e:
936         case 0x4f:
937            orl_a_r(op&7);
938            break;
939         //JNC code addr
940         case 0x50:                  /* 1: 0101 0000 */
941            jnc();
942            break;
943         //ACALL code addr               /* 1: aaa1 0001 */
944         case 0x51:
945            acall();
946            break;
947         //ANL data addr, A
948         case 0x52:                  /* 1: 0101 0010 */
949            RWM=1;
950            anl_mem_a();
951            RWM=0;
952            break;
953         //ANL data addr, #data
954         case 0x53:                  /* 1: 0101 0011 */
955            RWM=1;
956            anl_mem_byte();
957            RWM=0;
958            break;
959         //ANL A, #data
960         case 0x54:                  /* 1: 0101 0100 */
961            anl_a_byte();
962            break;
963         //ANL A, data addr
964         case 0x55:                  /* 1: 0101 0101 */
965            anl_a_mem();
966            break;
967         //ANL A, @RO/@R1                /* 1: 0101 011i */
968         case 0x56:
969         case 0x57:
970            anl_a_ir(op&1);
971            break;
972         //ANL A, RO to R7               /* 1: 0101 1rrr */
973         case 0x58:
974         case 0x59:
975         case 0x5a:
976         case 0x5b:
977         case 0x5c:
978         case 0x5d:
979         case 0x5e:
980         case 0x5f:
981            anl_a_r(op&7);
982            break;
983         //JZ code addr
984         case 0x60:                  /* 1: 0110 0000 */
985            jz();
986            break;
987         //AJMP code addr                /* 1: aaa0 0001 */
988         case 0x61:
989            ajmp();
990            break;
991         //XRL data addr, A
992         case 0x62:                  /* 1: 0110 0010 */
993            RWM=1;
994            xrl_mem_a();
995            RWM=0;
996            break;
997         //XRL data addr, #data
998         case 0x63:                  /* 1: 0110 0011 */
999            RWM=1;
1000            xrl_mem_byte();
1001            RWM=0;
1002            break;
1003         //XRL A, #data
1004         case 0x64:                  /* 1: 0110 0100 */
1005            xrl_a_byte();
1006            break;
1007         //XRL A, data addr
1008         case 0x65:                  /* 1: 0110 0101 */
1009            xrl_a_mem();
1010            break;
1011         //XRL A, @R0/@R1                /* 1: 0110 011i */
1012         case 0x66:
1013         case 0x67:
1014            xrl_a_ir(op&1);
1015            break;
1016         //XRL A, R0 to R7               /* 1: 0110 1rrr */
1017         case 0x68:
1018         case 0x69:
1019         case 0x6a:
1020         case 0x6b:
1021         case 0x6c:
1022         case 0x6d:
1023         case 0x6e:
1024         case 0x6f:
1025            xrl_a_r(op&7);
1026            break;
1027         //JNZ code addr
1028         case 0x70:                  /* 1: 0111 0000 */
1029            jnz();
1030            break;
1031         //ACALL code addr               /* 1: aaa1 0001 */
1032         case 0x71:
1033            acall();
1034            break;
1035         //ORL C, bit addr
1036         case 0x72:                  /* 1: 0111 0010 */
1037            orl_c_bitaddr();
1038            break;
1039         //JMP @A+DPTR
1040         case 0x73:                  /* 1: 0111 0011 */
1041            jmp_iadptr();
1042            break;
1043         //MOV A, #data
1044         case 0x74:                  /* 1: 0111 0100 */
1045            mov_a_byte();
1046            break;
1047         //MOV data addr, #data
1048         case 0x75:                  /* 1: 0111 0101 */
1049            mov_mem_byte();
1050            break;
1051         //MOV @R0/@R1, #data            /* 1: 0111 011i */
1052         case 0x76:
1053         case 0x77:
1054            mov_ir_byte(op&1);
1055            break;
1056         //MOV R0 to R7, #data           /* 1: 0111 1rrr */
1057         case 0x78:
1058         case 0x79:
1059         case 0x7a:
1060         case 0x7b:
1061         case 0x7c:
1062         case 0x7d:
1063         case 0x7e:
1064         case 0x7f:
1065            mov_r_byte(op&7);
1066            break;
1067         //SJMP code addr
1068         case 0x80:                  /* 1: 1000 0000 */
1069            sjmp();
1070            break;
1071         //AJMP code addr                /* 1: aaa0 0001 */
1072         case 0x81:
1073            ajmp();
1074            break;
1075         //ANL C, bit addr
1076         case 0x82:                  /* 1: 1000 0010 */
1077            anl_c_bitaddr();
1078            break;
1079         //MOVC A, @A + PC
1080         case 0x83:                  /* 1: 1000 0011 */
1081            movc_a_iapc();
1082            break;
1083         //DIV AB
1084         case 0x84:                  /* 1: 1000 0100 */
1085            div_ab();
1086            break;
1087         //MOV data addr, data addr
1088         case 0x85:                  /* 1: 1000 0101 */
1089            mov_mem_mem();
1090            break;
1091         //MOV data addr, @R0/@R1        /* 1: 1000 011i */
1092         case 0x86:
1093         case 0x87:
1094            mov_mem_ir(op&1);
1095            break;
1096         //MOV data addr,R0 to R7        /* 1: 1000 1rrr */
1097         case 0x88:
1098         case 0x89:
1099         case 0x8a:
1100         case 0x8b:
1101         case 0x8c:
1102         case 0x8d:
1103         case 0x8e:
1104         case 0x8f:
1105            mov_mem_r(op&7);
1106            break;
1107         //MOV DPTR, #data
1108         case 0x90:                  /* 1: 1001 0000 */
1109            mov_dptr_byte();
1110            break;
1111         //ACALL code addr               /* 1: aaa1 0001 */
1112         case 0x91:
1113            acall();
1114            break;
1115         //MOV bit addr, C
1116         case 0x92:                  /* 1: 1001 0010 */
1117            RWM = 1;
1118            mov_bitaddr_c();
1119            RWM = 0;
1120            break;
1121         //MOVC A, @A + DPTR
1122         case 0x93:                  /* 1: 1001 0011 */
1123            movc_a_iadptr();
1124            break;
1125         //SUBB A, #data
1126         case 0x94:                  /* 1: 1001 0100 */
1127            subb_a_byte();
1128            break;
1129         //SUBB A, data addr
1130         case 0x95:                  /* 1: 1001 0101 */
1131            subb_a_mem();
1132            break;
1133         //SUBB A, @R0/@R1               /* 1: 1001 011i */
1134         case 0x96:
1135         case 0x97:
1136            subb_a_ir(op&1);
1137            break;
1138         //SUBB A, R0 to R7              /* 1: 1001 1rrr */
1139         case 0x98:
1140         case 0x99:
1141         case 0x9a:
1142         case 0x9b:
1143         case 0x9c:
1144         case 0x9d:
1145         case 0x9e:
1146         case 0x9f:
1147            subb_a_r(op&7);
1148            break;
1149         //ORL C, /bit addr
1150         case 0xa0:                  /* 1: 1010 0000 */
1151            orl_c_nbitaddr();
1152            break;
1153         //AJMP code addr                /* 1: aaa0 0001 */
1154         case 0xa1:
1155            ajmp();
1156            break;
1157         //MOV C, bit addr
1158         case 0xa2:                  /* 1: 1010 0010 */
1159            mov_c_bitaddr();
1160            break;
1161         //INC DPTR
1162         case 0xa3:                  /* 1: 1010 0011 */
1163            inc_dptr();
1164            break;
1165         //MUL AB
1166         case 0xa4:                  /* 1: 1010 0100 */
1167            mul_ab();
1168            break;
1169         //reserved
1170         case 0xa5:                  /* 1: 1010 0101 */
1171            illegal();
1172            break;
1173         //MOV @R0/@R1, data addr        /* 1: 1010 011i */
1174         case 0xa6:
1175         case 0xa7:
1176            mov_ir_mem(op&1);
1177            break;
1178         //MOV R0 to R7, data addr       /* 1: 1010 1rrr */
1179         case 0xa8:
1180         case 0xa9:
1181         case 0xaa:
1182         case 0xab:
1183         case 0xac:
1184         case 0xad:
1185         case 0xae:
1186         case 0xaf:
1187            mov_r_mem(op&7);
1188            break;
1189         //ANL C,/bit addr
1190         case 0xb0:                  /* 1: 1011 0000 */
1191            anl_c_nbitaddr();
1192            break;
1193         //ACALL code addr               /* 1: aaa1 0001 */
1194         case 0xb1:
1195            acall();
1196            break;
1197         //CPL bit addr
1198         case 0xb2:                  /* 1: 1011 0010 */
1199            RWM=1;
1200            cpl_bitaddr();
1201            RWM=0;
1202            break;
1203         //CPL C
1204         case 0xb3:                  /* 1: 1011 0011 */
1205            cpl_c();
1206            break;
1207         //CJNE A, #data, code addr
1208         case 0xb4:                  /* 1: 1011 0100 */
1209            cjne_a_byte();
1210            break;
1211         //CJNE A, data addr, code addr
1212         case 0xb5:                  /* 1: 1011 0101 */
1213            cjne_a_mem();
1214            break;
1215         //CJNE @R0/@R1, #data, code addr /* 1: 1011 011i */
1216         case 0xb6:
1217         case 0xb7:
1218            cjne_ir_byte(op&1);
1219            break;
1220         //CJNE R0 to R7, #data, code addr/* 1: 1011 1rrr */
1221         case 0xb8:
1222         case 0xb9:
1223         case 0xba:
1224         case 0xbb:
1225         case 0xbc:
1226         case 0xbd:
1227         case 0xbe:
1228         case 0xbf:
1229            cjne_r_byte(op&7);
1230            break;
1231         //PUSH data addr
1232         case 0xc0:                  /* 1: 1100 0000 */
1233            push();
1234            break;
1235         //AJMP code addr                /* 1: aaa0 0001 */
1236         case 0xc1:
1237            ajmp();
1238            break;
1239         //CLR bit addr
1240         case 0xc2:                  /* 1: 1100 0010 */
1241            RWM=1;
1242            clr_bitaddr();
1243            RWM=0;
1244            break;
1245         //CLR C
1246         case 0xc3:                  /* 1: 1100 0011 */
1247            clr_c();
1248            break;
1249         //SWAP A
1250         case 0xc4:                  /* 1: 1100 0100 */
1251            swap_a();
1252            break;
1253         //XCH A, data addr
1254         case 0xc5:                  /* 1: 1100 0101 */
1255            xch_a_mem();
1256            break;
1257         //XCH A, @RO/@R1                /* 1: 1100 011i */
1258         case 0xc6:
1259         case 0xc7:
1260            xch_a_ir(op&1);
1261            break;
1262         //XCH A, RO to R7               /* 1: 1100 1rrr */
1263         case 0xc8:
1264         case 0xc9:
1265         case 0xca:
1266         case 0xcb:
1267         case 0xcc:
1268         case 0xcd:
1269         case 0xce:
1270         case 0xcf:
1271            xch_a_r(op&7);
1272            break;
1273         //POP data addr
1274         case 0xd0:                  /* 1: 1101 0000 */
1275            pop();
1276            break;
1277         //ACALL code addr               /* 1: aaa1 0001 */
1278         case 0xd1:
1279            acall();
1280            break;
1281         //SETB bit addr
1282         case 0xd2:                  /* 1: 1101 0010 */
1283            RWM=1;
1284            setb_bitaddr();
1285            RWM=0;
1286            break;
1287         //SETB C
1288         case 0xd3:                  /* 1: 1101 0011 */
1289            setb_c();
1290            break;
1291         //DA A
1292         case 0xd4:                  /* 1: 1101 0100 */
1293            da_a();
1294            break;
1295         //DJNZ data addr, code addr
1296         case 0xd5:                  /* 1: 1101 0101 */
1297            RWM=1;
1298            djnz_mem();
1299            RWM=0;
1300            break;
1301         //XCHD A, @R0/@R1               /* 1: 1101 011i */
1302         case 0xd6:
1303         case 0xd7:
1304            xchd_a_ir(op&1);
1305            break;
1306         //DJNZ R0 to R7,code addr       /* 1: 1101 1rrr */
1307         case 0xd8:
1308         case 0xd9:
1309         case 0xda:
1310         case 0xdb:
1311         case 0xdc:
1312         case 0xdd:
1313         case 0xde:
1314         case 0xdf:
1315            djnz_r(op&7);
1316            break;
1317         //MOVX A,@DPTR
1318         case 0xe0:                  /* 1: 1110 0000 */
1319            movx_a_idptr();
1320            break;
1321         //AJMP code addr                /* 1: aaa0 0001 */
1322         case 0xe1:
1323            ajmp();
1324            break;
1325         //MOVX A, @R0/@R1               /* 1: 1110 001i */
1326         case 0xe2:
1327         case 0xe3:
1328            movx_a_ir(op&1);
1329            break;
1330         //CLR A
1331         case 0xe4:                  /* 1: 1110 0100 */
1332            clr_a();
1333            break;
1334         //MOV A, data addr
1335         case 0xe5:                  /* 1: 1110 0101 */
1336            mov_a_mem();
1337            break;
1338         //MOV A,@RO/@R1                 /* 1: 1110 011i */
1339         case 0xe6:
1340         case 0xe7:
1341            mov_a_ir(op&1);
1342            break;
1343         //MOV A,R0 to R7                /* 1: 1110 1rrr */
1344         case 0xe8:
1345         case 0xe9:
1346         case 0xea:
1347         case 0xeb:
1348         case 0xec:
1349         case 0xed:
1350         case 0xee:
1351         case 0xef:
1352            mov_a_r(op&7);
1353            break;
1354         //MOVX @DPTR,A
1355         case 0xf0:                  /* 1: 1111 0000 */
1356            movx_idptr_a();
1357            break;
1358         //ACALL code addr               /* 1: aaa1 0001 */
1359         case 0xf1:
1360            acall();
1361            break;
1362         //MOVX @R0/@R1,A                /* 1: 1111 001i */
1363         case 0xf2:
1364         case 0xf3:
1365            movx_ir_a(op&1);
1366            break;
1367         //CPL A
1368         case 0xf4:                  /* 1: 1111 0100 */
1369            cpl_a();
1370            break;
1371         //MOV data addr, A
1372         case 0xf5:                  /* 1: 1111 0101 */
1373            mov_mem_a();
1374            break;
1375         //MOV @R0/@R1, A                /* 1: 1111 011i */
1376         case 0xf6:
1377         case 0xf7:
1378            mov_ir_a(op&1);
1379            break;
1380         //MOV R0 to R7, A               /* 1: 1111 1rrr */
1381         case 0xf8:
1382         case 0xf9:
1383         case 0xfa:
1384         case 0xfb:
1385         case 0xfc:
1386         case 0xfd:
1387         case 0xfe:
1388         case 0xff:
1389            mov_r_a(op&7);
1390            break;
1391         default:
1392            illegal();
1393      }
1394
1395      //If the chip entered in stop mode, end execution
1396      if (R_PCON & 0x02) {
1397         return cycles;
1398      }
1399
1400      //Store # of used cycles for this opcode (for timer & serial check at top of code)
1401      ds5002fp.prev_used_cycles = ds5002fp_cycles[op];
1402
1403      // decrement the timed access window
1404      if (ds5002fp.ta_window > 0) {
1405         ds5002fp.ta_window -= ds5002fp_cycles[op];
1406         if (ds5002fp.ta_window < 0) {
1407            ds5002fp.ta_window = 0;
1408         }
1409      }
1410
1411      //Check for pending interrupts & handle - remove cycles used
1412      ds5002fp_icount-=check_interrupts();
1413
1414      //If the chip entered in idle mode, end the loop
1415      if (R_PCON & 0x01) {
1416         return cycles - ds5002fp_icount;
1417      }
1418   } while( ds5002fp_icount > 0 );
1419
1420   return cycles - ds5002fp_icount;
1421}
1422
1423/* Get registers, return context size */
1424void ds5002fp_get_context(void *dst)
1425{
1426   if( dst )
1427      memcpy(dst, &ds5002fp, sizeof(DS5002FP));
1428}
1429
1430/* Set registers */
1431void ds5002fp_set_context(void *src)
1432{
1433   if( src )
1434      memcpy(&ds5002fp, src, sizeof(DS5002FP));
1435}
1436
1437/* Get Internal RAM value */
1438unsigned ds5002fp_get_intram(int offset)
1439{
1440    UINT8 ram_ret = 0;
1441    RWM = 1;
1442    ram_ret = IRAM_R(offset);
1443    RWM = 0;
1444    return ram_ret;
1445}
1446
1447/*NOTE: These are not used by the opcode functions, they are here only for MAME requirements*/
1448unsigned ds5002fp_get_reg(int regnum)
1449{
1450   switch( regnum )
1451   {
1452   case REG_PREVIOUSPC: return PPC;
1453   case REG_PC:
1454   case DS5002FP_PC:   return PC;
1455   case REG_SP:
1456   case DS5002FP_SP:   return R_SP;
1457   case DS5002FP_ACC:   return R_ACC;
1458   case DS5002FP_PSW: return R_PSW;
1459   case DS5002FP_B:   return R_B;
1460   case DS5002FP_DPH:   return R_DPH;
1461   case DS5002FP_DPL:   return R_DPL;
1462   case DS5002FP_IE:   return R_IE;
1463
1464   case DS5002FP_PCON:   return R_PCON;
1465   case DS5002FP_MCON:   return R_MCON;
1466   case DS5002FP_RPCTL:return R_RPCTL;
1467   case DS5002FP_CRC:   return R_CRC;
1468   //Not real registers
1469   case DS5002FP_R0:   return R_R(0);
1470   case DS5002FP_R1:   return R_R(1);
1471   case DS5002FP_R2:   return R_R(2);
1472   case DS5002FP_R3:   return R_R(3);
1473   case DS5002FP_R4:   return R_R(4);
1474   case DS5002FP_R5:   return R_R(5);
1475   case DS5002FP_R6:   return R_R(6);
1476   case DS5002FP_R7:   return R_R(7);
1477   case DS5002FP_RB:   return GET_RS;
1478   default:
1479      return 0;
1480   }
1481}
1482
1483/*NOTE: These are not used by the opcode functions, they are here only for MAME requirements*/
1484void ds5002fp_set_reg (int regnum, unsigned val)
1485{
1486   switch( regnum )
1487   {
1488   case REG_PC:
1489   case DS5002FP_PC:   PC = val & 0xffff; break;
1490   case REG_SP:
1491   case DS5002FP_SP:   SFR_W(SP,val);  break;      //Use SFR to handle error checking
1492   case DS5002FP_ACC:   SFR_W(ACC,val); break;
1493   case DS5002FP_PSW: SFR_W(PSW,val); break;
1494   case DS5002FP_B:   SFR_W(B,val);   break;
1495   case DS5002FP_DPH: SFR_W(DPH,val); break;
1496   case DS5002FP_DPL: SFR_W(DPL,val); break;
1497   case DS5002FP_IE:  SFR_W(IE,val);  break;
1498   case DS5002FP_PCON:   SFR_W(PCON,val); break;
1499   case DS5002FP_MCON:   SFR_W(MCON,val); break;
1500   case DS5002FP_RPCTL: SFR_W(RPCTL,val); break;
1501   case DS5002FP_CRC:   SFR_W(CRCR,val); break;
1502   //Not real registers
1503   case DS5002FP_R0:   R_R(0) = val; break;
1504   case DS5002FP_R1:   R_R(1) = val; break;
1505   case DS5002FP_R2:   R_R(2) = val; break;
1506   case DS5002FP_R3:   R_R(3) = val; break;
1507   case DS5002FP_R4:   R_R(4) = val; break;
1508   case DS5002FP_R5:   R_R(5) = val; break;
1509   case DS5002FP_R6:   R_R(6) = val; break;
1510   case DS5002FP_R7:   R_R(7) = val; break;
1511   case DS5002FP_RB:  SET_RS( (val&3) ); break;
1512
1513   default:
1514      return;
1515   }
1516}
1517
1518void ds5002fp_set_irq_line(int irqline, int state)
1519{
1520   switch( irqline )
1521   {
1522      //External Interrupt 0
1523      case DS5002FP_INT0_LINE:
1524         //Line Asserted?
1525         if (state != CLEAR_LINE) {
1526            //Is the enable flag for this interrupt set?
1527            if(GET_EX0) {
1528               //Need cleared->active line transition? (Logical 1-0 Pulse on the line) - CLEAR->ASSERT Transition since INT0 active lo!
1529               if(GET_IT0){
1530                  if(ds5002fp.last_int0 == CLEAR_LINE)
1531                     SET_IE0(1);
1532               }
1533               else
1534                  SET_IE0(1);      //Nope, just set it..
1535            }
1536         }
1537         else
1538            SET_IE0(0);      //Clear Int occurred flag
1539         ds5002fp.last_int0 = state;
1540
1541         //Do the interrupt & handle - remove machine cycles used
1542         if(GET_IE0)
1543            ds5002fp_icount-=check_interrupts();
1544         break;
1545
1546      //External Interrupt 1
1547      case DS5002FP_INT1_LINE:
1548
1549         //Line Asserted?
1550         if (state != CLEAR_LINE) {
1551            if(GET_EX1) {
1552               //Need cleared->active line transition? (Logical 1-0 Pulse on the line) - CLEAR->ASSERT Transition since INT1 active lo!
1553               if(GET_IT1){
1554                  if(ds5002fp.last_int1 == CLEAR_LINE)
1555                     SET_IE1(1);
1556               }
1557            else
1558               SET_IE1(1);      //Nope, just set it..
1559            }
1560         }
1561         else
1562            SET_IE1(0);      //Clear Int occurred flag
1563         ds5002fp.last_int1 = state;
1564
1565         //Do the interrupt & handle - remove machine cycles used
1566         if(GET_IE1)
1567            ds5002fp_icount-=check_interrupts();
1568         break;
1569
1570      //Serial Port Receive
1571      case DS5002FP_RX_LINE:
1572         //Is the enable flags for this interrupt set?
1573         if(GET_ES && GET_REN) {
1574            int data = 0;
1575            //Call our callball function to retrieve the data
1576            if(ds5002fp.serial_rx_callback)
1577               data = ds5002fp.serial_rx_callback();
1578            //Update the register directly, since SFR_W() will trigger a serial transmit instead!
1579            R_SBUF=data;
1580            //Flag the IRQ
1581            SET_RI(1);
1582         }
1583         break;
1584         //Note: we won't call check interrupts, we'll let the main loop catch it
1585
1586      //Power Fail Interrupt
1587      case DS5002FP_PFI_LINE:
1588         //Line Asserted?
1589         if (state != CLEAR_LINE) {
1590            if (GET_EPFW) {
1591               SET_PFW(1);
1592               //Need cleared->active line transition? (Logical 1-0 Pulse on the line) - CLEAR->ASSERT Transition since INT1 active lo!
1593               if (ds5002fp.last_power_int == CLEAR_LINE) {
1594                  ds5002fp_icount-=check_interrupts();
1595               }
1596            }
1597         }
1598         ds5002fp.last_power_int = state;
1599         break;
1600   }
1601}
1602
1603/***********************************************************************************
1604 Check for pending Interrupts and process - returns # of cycles used for the int
1605
1606 Note about priority & interrupting interrupts..
1607 1) A high priority interrupt cannot be interrupted by anything!
1608 2) A low priority interrupt can ONLY be interrupted by a high priority interrupt
1609 3) If more than 1 Interrupt Flag is set (ie, 2 simultaneous requests occur),
1610    the following logic works as follows:
1611    1) If two requests come in of different priority levels, the higher one is selected..
1612    2) If the requests are of the same level, an internal order is used:
1613        a) IEO
1614        b) TFO
1615        c) IE1
1616        d) TF1
1617        e) RI+TI
1618        f) TF2+EXF2
1619 **********************************************************************************/
1620INLINE UINT8 check_interrupts(void)
1621{
1622   //If All Inerrupts Disabled or no pending abort if no Power Fail Interrupt is pending
1623   if(!GET_EA && !GET_PFW) {
1624      return 0;
1625   }
1626
1627   //Skip if current irq in progress is high priority and no Power Fail Interrupt is pending
1628   if(!GET_PFW && ds5002fp.irq_priority)   { /* LOG(("high priority irq in progress, skipping irq request\n")); */ return 0; }
1629
1630   //Check which interrupt(s) requests have occurred..
1631   //NOTE: The order of checking is based on the internal/default priority levels when levels are the same
1632
1633   // Power Fail Interrupt
1634   if(GET_PFW) {
1635      if (ds5002fp.last_power_int == CLEAR_LINE) {
1636         //Set vector & priority level request
1637         ds5002fp.int_vec = V_PFI;
1638         ds5002fp.priority_request = GET_PFW;
1639      } else if (!GET_EA) {
1640         return 0;
1641      }
1642   }
1643   //External Int 0
1644   if(!ds5002fp.priority_request && GET_IE0) {
1645      //Set vector & priority level request
1646      ds5002fp.int_vec = V_IE0;
1647      ds5002fp.priority_request = GET_PX0;
1648   }
1649   //Timer 0 overflow
1650   if(!ds5002fp.priority_request && GET_TF0 && (!ds5002fp.int_vec || (ds5002fp.int_vec && GET_PT0))) {
1651      //Set vector & priority level request
1652      ds5002fp.int_vec = V_TF0;
1653      ds5002fp.priority_request = GET_PT0;
1654   }
1655   //External Int 1
1656   if(!ds5002fp.priority_request && GET_IE1 && (!ds5002fp.int_vec || (ds5002fp.int_vec && GET_PX1))) {
1657      //Set vector & priority level request
1658      ds5002fp.int_vec = V_IE1;
1659      ds5002fp.priority_request = GET_PX1;
1660   }
1661   //Timer 1 overflow
1662   if(!ds5002fp.priority_request && GET_TF1 && (!ds5002fp.int_vec || (ds5002fp.int_vec && GET_PT1))) {
1663      //Set vector & priority level request
1664      ds5002fp.int_vec = V_TF1;
1665      ds5002fp.priority_request = GET_PT1;
1666   }
1667   //Serial Interrupt Transmit/Receive Interrupts (Note: ES Bit - Serial Interrupts must be enabled)
1668   if(!ds5002fp.priority_request && GET_ES && (GET_TI || GET_RI) && (!ds5002fp.int_vec || (ds5002fp.int_vec && GET_PS))) {
1669      //Set vector & priority level request
1670      ds5002fp.int_vec = V_RITI;
1671      ds5002fp.priority_request = GET_PS;
1672   }
1673
1674   //Skip the interrupt request if currently processing is lo priority, and the new request IS NOT HI PRIORITY!
1675   if(ds5002fp.cur_irq < 0xff && !ds5002fp.priority_request)
1676      { LOG(("low priority irq in progress already, skipping low irq request\n")); return 0; }
1677
1678    /*** --- Perform the interrupt --- ***/
1679
1680   //Save current pc to stack, set pc to new interrupt vector
1681   push_pc();
1682   PC = ds5002fp.int_vec;
1683
1684   //Set current Irq & Priority being serviced
1685   ds5002fp.cur_irq = ds5002fp.int_vec;
1686   ds5002fp.irq_priority = ds5002fp.priority_request;
1687
1688   //Clear any interrupt flags that should be cleared since we're servicing the irq!
1689   switch(ds5002fp.cur_irq) {
1690      case V_IE0:
1691         //External Int Flag only cleared when configured as Edge Triggered..
1692         //if(GET_IT0)   - for some reason having this, breaks alving dmd games
1693            SET_IE0(0);
1694         break;
1695      case V_TF0:
1696         //Timer 0 - Always clear Flag
1697         SET_TF0(0);
1698         break;
1699      case V_IE1:
1700         //External Int Flag only cleared when configured as Edge Triggered..
1701         //if(GET_IT1)   - for some reason having this, breaks alving dmd games
1702            SET_IE1(0);
1703         break;
1704      case V_TF1:
1705         //Timer 0 - Always clear Flag
1706         SET_TF1(0);
1707         break;
1708      case V_RITI:
1709         // no flags are cleared, TI and RI remain set until reset by software
1710         break;
1711      case V_PFI:
1712         // no flags are cleared, PFW is reset by software
1713         break;
1714   }
1715
1716   //Clear vars.. (these are part of the 8051 structure for speed, so we don't have to dynamically allocate space each time)
1717   ds5002fp.int_vec = 0;
1718   ds5002fp.priority_request = 0;
1719
1720   //All interrupts use 2 machine cycles
1721   return 24;
1722}
1723
1724
1725void ds5002fp_set_irq_callback(int (*callback)(int irqline))
1726{
1727   ds5002fp.irq_callback = callback;
1728}
1729
1730void ds5002fp_set_serial_tx_callback(void (*callback)(int data))
1731{
1732   //Hold in static variable since this function can get called before reset has run, which wipes ds5002fp memory clean
1733   hold_serial_tx_callback = callback;
1734}
1735void ds5002fp_set_serial_rx_callback(int (*callback)(void))
1736{
1737   //Hold in static variable since this function can get called before reset has run, which wipes ds5002fp memory clean
1738   hold_serial_rx_callback = callback;
1739}
1740
1741void ds5002fp_set_ebram_iaddr_callback(READ32_HANDLER((*callback)))
1742{
1743   //Hold in static variable since this function can get called before reset has run, which wipes ds5002fp memory clean
1744   hold_ebram_iaddr_callback = callback;
1745}
1746
1747
1748void ds5002fp_state_save(void *file)
1749{
1750}
1751
1752void ds5002fp_state_load(void *file)
1753{
1754}
1755
1756/* HELPER FUNCTIONS */
1757
1758/*All writes to SFR are handled here*/
1759static WRITE8_HANDLER(sfr_write)
1760{
1761   UINT8 is_timed_access;
1762   UINT8 reg_mask1;
1763   UINT8 reg_mask2;
1764
1765   data &= 0xff;      //Ensure only 8 bits
1766   switch (offset)
1767   {
1768      case P0:
1769         R_P0 = data;
1770         OUT(0,data);
1771         break;
1772
1773      case SP:
1774         if(offset > 0xff)
1775            LOG(("ds5002fp #%d: attemping to write value to SP past 256 bytes at 0x%04x\n", cpu_getactivecpu(), PC));
1776         R_SP = data&0xff; //keep sp w/in 256 bytes
1777         break;
1778
1779      case DPL:      R_DPL = data; break;
1780      case DPH:      R_DPH = data; break;
1781      case PCON:
1782         reg_mask1 = 0xb9;
1783
1784         is_timed_access = (ds5002fp.ta_window > 0) && (R_TA == 0x55);
1785         if (is_timed_access) {
1786            reg_mask1 = 0xff;
1787         }
1788         R_PCON = (R_PCON & (~reg_mask1)) | (data & reg_mask1);
1789         break;
1790      case TCON:      R_TCON = data; break;
1791      case TMOD:      R_TMOD = data; break;
1792      case TL0:      R_TL0 = data; break;
1793      case TL1:      R_TL1 = data; break;
1794      case TH0:      R_TH0 = data; break;
1795      case TH1:      R_TH1 = data; break;
1796
1797      case P1:
1798         R_P1 = data;
1799         OUT(1,data);
1800         break;
1801
1802      case SCON: {
1803         //Update register
1804         R_SCON = data;
1805         break;
1806      }
1807
1808      case SBUF:
1809         //R_SBUF = data;        //This register is used only for "Receiving data coming in!"
1810         serial_transmit(data);   //Set up to transmit the data
1811         break;
1812
1813      case P2:
1814         R_P2 = data;
1815         OUT(2,data);
1816         break;
1817
1818      case IE:      R_IE  = data; break;
1819
1820      case P3:
1821         R_P3 = data;
1822         OUT(3,data);
1823         break;
1824
1825      case IP:
1826         reg_mask1 = 0x7f;
1827
1828         is_timed_access = (ds5002fp.ta_window > 0) && (R_TA == 0x55);
1829         if (is_timed_access) {
1830            reg_mask1 = 0xff;
1831         }
1832         R_IP = (R_IP & (~reg_mask1)) | (data & reg_mask1);
1833         break;
1834
1835      case CRCR:      reg_mask2 = 0x0f;
1836                  R_CRC = (R_CRC & (~reg_mask2)) | (data & reg_mask2);
1837                  LOG(("ds5002fp #%d: write to CRC register at 0x%04x, data=%x\n", cpu_getactivecpu(), PC, data));
1838                  break;
1839      case CRCL:      R_CRCL = data;
1840                  LOG(("ds5002fp #%d: write to CRCL register at 0x%04x, data=%x\n", cpu_getactivecpu(), PC, data));
1841                  break;
1842      case CRCH:      R_CRCH = data;
1843                  LOG(("ds5002fp #%d: write to CRCH register at 0x%04x, data=%x\n", cpu_getactivecpu(), PC, data));
1844                  break;
1845      case MCON:      reg_mask1 = 0x0f;
1846                  reg_mask2 = 0xf7;
1847
1848                  is_timed_access = (ds5002fp.ta_window > 0) && (R_TA == 0x55);
1849                  if (is_timed_access) {
1850                     reg_mask1 = 0xff;
1851                  }
1852                  data = (R_MCON & (~reg_mask1)) | (data & reg_mask1);
1853                  R_MCON = (R_MCON & (~reg_mask2)) | (data & reg_mask2);
1854                  LOG(("ds5002fp #%d: write to MCON register at 0x%04x, data=%x\n", cpu_getactivecpu(), PC, data));
1855                  break;
1856      case TA:      ds5002fp.previous_ta = R_TA;
1857                  R_TA = data;
1858
1859                  // init the time window after having wrote 0xaa
1860                  if ((data == 0xaa) && (ds5002fp.ta_window == 0)) {
1861                     ds5002fp.ta_window = 4*12 + 2*12;
1862                     LOG(("ds5002fp #%d: TA window initiated at 0x%04x\n", cpu_getactivecpu(), PC));
1863                  }
1864                  //LOG(("ds5002fp #%d: write to TA register at 0x%04x, data=%x\n", cpu_getactivecpu(), PC, data));
1865                  break;
1866      case RNR:      R_RNR = data;
1867                  LOG(("ds5002fp #%d: write to RNR register at 0x%04x, data=%x\n", cpu_getactivecpu(), PC, data));
1868                  break;
1869
1870      case PSW:
1871         R_PSW = data;
1872         SET_PARITY;
1873         break;
1874
1875      case RPCTL:      reg_mask1 = 0xef;
1876                  reg_mask2 = 0xfe;
1877
1878                  is_timed_access = (ds5002fp.ta_window > 0) && (R_TA == 0x55);
1879                  if (is_timed_access) {
1880                     reg_mask1 = 0xff;
1881                  }
1882                  data = (R_RPCTL & (~reg_mask1)) | (data & reg_mask1);
1883                  R_RPCTL = (R_RPCTL & (~reg_mask2)) | (data & reg_mask2);
1884                  LOG(("ds5002fp #%d: write to RPCTL register at 0x%04x, data=%x\n", cpu_getactivecpu(), PC, data));
1885                  break;
1886      case RPS:      R_RPS = data;
1887                  LOG(("ds5002fp #%d: write to RPS register at 0x%04x, data=%x\n", cpu_getactivecpu(), PC, data));
1888                  break;
1889
1890      case ACC:
1891         R_ACC = data;
1892         SET_PARITY;
1893         break;
1894
1895      case B:         R_B   = data; break;
1896
1897      /* Illegal or non-implemented sfr */
1898      default:
1899         LOG(("ds5002fp #%d: attemping to write to an invalid/non-implemented SFR address: %x at 0x%04x, data=%x\n", cpu_getactivecpu(), offset, PC, data));
1900   }
1901}
1902
1903/*All reads to SFR are handled here*/
1904static READ8_HANDLER(sfr_read)
1905{
1906   switch (offset)
1907   {
1908      case P0:
1909         if(RWM)
1910            return R_P0;               //Read directly from port latch
1911         else
1912            return IN(0);               //Read from actual port
1913      case SP:      return R_SP;
1914      case DPL:      return R_DPL;
1915      case DPH:      return R_DPH;
1916      case PCON:      return R_PCON;
1917      case TCON:      return R_TCON;
1918      case TMOD:      return R_TMOD;
1919      case TL0:      return R_TL0;
1920      case TL1:      return R_TL1;
1921      case TH0:      return R_TH0;
1922      case TH1:      return R_TH1;
1923      case P1:
1924         if(RWM)
1925            return R_P1;               //Read directly from port latch
1926         else
1927            return IN(1);               //Read from actual port
1928      case SCON:      return R_SCON;
1929      case SBUF:      return R_SBUF;
1930      case P2:
1931         if(RWM)
1932            return R_P2;               //Read directly from port latch
1933         else
1934            return IN(2);               //Read from actual port
1935      case IE:      return R_IE;
1936      case P3:
1937         if(RWM)
1938            return R_P3;               //Read directly from port latch
1939         else
1940            return IN(3);               //Read from actual port
1941      case IP:      return R_IP;
1942
1943      case CRCR:      LOG(("ds5002fp #%d: read CRC register at 0x%04x\n", cpu_getactivecpu(), PC));
1944                  return R_CRC;
1945      case CRCL:      LOG(("ds5002fp #%d: read CRCL register at 0x%04x\n", cpu_getactivecpu(), PC));
1946                  return R_CRCL;
1947      case CRCH:      LOG(("ds5002fp #%d: read CRCH register at 0x%04x\n", cpu_getactivecpu(), PC));
1948                  return R_CRCH;
1949      case MCON:      LOG(("ds5002fp #%d: read MCON register at 0x%04x\n", cpu_getactivecpu(), PC));
1950                  return R_MCON;
1951      case TA:      LOG(("ds5002fp #%d: read TA register at 0x%04x\n", cpu_getactivecpu(), PC));
1952                  return R_TA;
1953      case RNR:      LOG(("ds5002fp #%d: read RNR register at 0x%04x\n", cpu_getactivecpu(), PC));
1954                  return R_RNR;
1955
1956      case PSW:      return R_PSW;
1957
1958      case RPCTL:      LOG(("ds5002fp #%d: read RPCTL register at 0x%04x\n", cpu_getactivecpu(), PC));
1959                  return R_RPCTL;
1960      case RPS:      LOG(("ds5002fp #%d: read RPS register at 0x%04x\n", cpu_getactivecpu(), PC));
1961                  return R_RPS;
1962
1963      case ACC:      return R_ACC;
1964      case B:         return R_B;
1965
1966      /* Illegal or non-implemented sfr */
1967      default:
1968         LOG(("ds5002fp #%d: attemping to read an invalid/non-implemented SFR address: %x at 0x%04x\n", cpu_getactivecpu(), offset,PC));
1969   }
1970   return 0xff;
1971}
1972
1973/* Reads the contents of the Internal RAM memory   */
1974/* Anything above 0x7f is a sfr/register */
1975static READ8_HANDLER(internal_ram_read)
1976{
1977   if (offset < 0x80)
1978      return ds5002fp.IntRam[offset];
1979   else {
1980      if (offset < 0x100)
1981         return SFR_R(offset);
1982      else
1983         LOG(("ds5002fp #%d: attemping to read from an invalid Internal Ram address: %x at 0x%04x\n", cpu_getactivecpu(), offset,PC));
1984   }
1985   return 0xff;
1986}
1987
1988/* Writes the contents of the Internal RAM memory   */
1989/* Anything above 0x7f is a sfr/register */
1990static WRITE8_HANDLER(internal_ram_write)
1991{
1992   data &= 0xff;            //Ensure it's only 8 bits
1993   if (offset < 0x80)
1994      ds5002fp.IntRam[offset] = data;
1995   else {
1996      if (offset < 0x100)
1997         SFR_W(offset,data);
1998      else
1999         LOG(("ds5002fp #%d: attemping to write to invalid Internal Ram address: %x at 0x%04x\n", cpu_getactivecpu(), offset,PC));
2000   }
2001}
2002
2003/* Reads the contents of the Internal RAM memory (BUT CALLED FROM AN INDIRECT ADDRESSING MODE)   */
2004/* Different chip types handle differently, for speed, simply call the chip's handler */
2005static READ8_HANDLER(internal_ram_iread)
2006{
2007   return ds5002fp.iram_iread(machine,offset);
2008}
2009
2010/* Writes the contents of the Internal RAM memory (BUT CALLED FROM AN INDIRECT ADDRESSING MODE)   */
2011/* Different chip types handle differently, for speed, simply call the chip's handler */
2012static WRITE8_HANDLER(internal_ram_iwrite)
2013{
2014   ds5002fp.iram_iwrite(machine,offset,data);
2015}
2016
2017/* Generate an expanded bus/bytewide bus ram address for read/writing */
2018/*
2019    The DS5002FP has 2 16 bits data address buses (the byte-wide bus and the expanded bus). The exact memory position accessed depends on the
2020    partition mode, the memory range and the expanded bus select. The partition mode and the expanded bus select can be changed at any time.
2021
2022    In order to simplify memory mapping to the data address bus, the following address map is assumed for partitioned mode:
2023
2024    0x00000-0x0ffff -> data memory on the expanded bus
2025    0x10000-0x1ffff -> data memory on the byte-wide bus
2026
2027    For non-partitioned mode the following memory map is assumed:
2028
2029    0x0000-0xffff -> data memory (the bus used to access it does not matter)
2030*/
2031static READ32_HANDLER(external_ram_iaddr)
2032{
2033   // if partition mode is set, adjust offset based on the bus
2034   if (!GET_PM) {
2035      if (!GET_EXBS) {
2036         if ((offset >= ds5002fp_partitions[GET_PA]) && (offset <= ds5002fp_ranges[ds5002fp.range])) {
2037            offset += 0x10000;
2038         }
2039      }
2040   }
2041    if(ds5002fp.ebram_iaddr_callback)
2042        return ds5002fp.ebram_iaddr_callback(machine,offset,mem_mask);
2043    else {
2044       //LOG(("ds5002fp #%d: external ram address requested (8 bit offset=%02x), but no callback available! at PC:%04x\n", cpu_getactivecpu(), offset, PC));
2045   }
2046
2047   return offset;
2048}
2049
2050/*Push the current PC to the stack*/
2051INLINE void push_pc()
2052{
2053   UINT8 tmpSP = R_SP;                     //Grab and Increment Stack Pointer
2054   tmpSP++;                           // ""
2055   SFR_W(SP,tmpSP);                     // ""
2056    if (tmpSP == R_SP)                     //Ensure it was able to write to new stack location
2057      IRAM_IW(tmpSP, (PC & 0xff));         //Store low byte of PC to Internal Ram (Use IRAM_IW to store stack above 128 bytes)
2058   tmpSP = R_SP;                        //Increment Stack Pointer
2059   tmpSP++;                           // ""
2060   SFR_W(SP,tmpSP);                     // ""
2061   if (tmpSP == R_SP)                     //Ensure it was able to write to new stack location
2062      IRAM_IW(tmpSP, ( (PC & 0xff00) >> 8));   //Store hi byte of PC to next address in Internal Ram (Use IRAM_IW to store stack above 128 bytes)
2063}
2064
2065/*Pop the current PC off the stack and into the pc*/
2066INLINE void pop_pc()
2067{
2068   UINT8 tmpSP = R_SP;                     //Grab Stack Pointer
2069   PC = (IRAM_IR(tmpSP) & 0xff) << 8;         //Store hi byte to PC (must use IRAM_IR to access stack pointing above 128 bytes)
2070   tmpSP = R_SP-1;                        //Decrement Stack Pointer
2071   SFR_W(SP,tmpSP);                     // ""
2072   if (tmpSP == R_SP)                     //Ensure it was able to write to new stack location
2073      PC = PC | IRAM_IR(tmpSP);            //Store lo byte to PC (must use IRAM_IR to access stack pointing above 128 bytes)
2074   SFR_W(SP,tmpSP-1);                     //Decrement Stack Pointer
2075}
2076
2077//Set the PSW Parity Flag
2078INLINE void set_parity()
2079{
2080   //This flag will be set when the accumulator contains an odd # of bits set..
2081   int i,
2082   p = 0;
2083   for (i=1; i<=128; i=i*2) {      //Test for each of the 8 bits in the ACC!
2084      if ((R_ACC & i) != 0)
2085         p++;               //Keep track of how many bits are set
2086   }
2087
2088   //Update the PSW Pairty bit
2089   SET_P(p & 1);
2090}
2091
2092static READ8_HANDLER(bit_address_r)
2093{
2094   int   word;
2095   int   mask;
2096   int   bit_pos;
2097   int   base;      /* base of bit space or sfr */
2098   int   distance;   /* distance between bit addressable words */
2099               /* 1 for normal bits, 8 for sfr bit addresses */
2100
2101   offset &= 0xff;
2102
2103   //User defined bit addresses 0x20-0x2f (values are 0x0-0x7f)
2104   if (offset < 0x80) {
2105      base = 0x20;
2106      distance = 1;
2107   }
2108   //SFR bit addressable registers
2109   else {
2110      base = 0x80;
2111      distance = 8;
2112   }
2113   word = ( (offset & 0x78) >> 3) * distance + base;
2114   bit_pos = offset & 0x7;
2115   mask = 0x1 << bit_pos;
2116   return((IRAM_R(word) & mask) >> bit_pos);         //Do not use IRAM_IR
2117}
2118
2119
2120static WRITE8_HANDLER(bit_address_w)
2121{
2122   int   word;
2123   int   mask;
2124   int   bit_pos;
2125   int   result;
2126   int   base;
2127   int   distance;
2128
2129   offset &= 0xff;
2130
2131   //User defined bit addresses 0x20-0x2f (values are 0x0-0x7f)
2132   if (offset < 0x80) {
2133      base = 0x20;
2134      distance = 1;
2135   }
2136   //SFR bit addressable registers
2137   else {
2138      base = 0x80;
2139      distance = 8;
2140   }
2141   word = ((offset & 0x78) >> 3) * distance + base;
2142   bit_pos = offset & 0x7;
2143   data = (data & 0x1) << bit_pos;
2144   mask = ~(1 << bit_pos) & 0xff;
2145   result = IRAM_R(word) & mask;   //Do not use IRAM_IR
2146   result = result | data;
2147   IRAM_W(word, result);         //Do not use IRAM_IW
2148}
2149
2150/* The following two handlers are used by the MAME Debugger Memory Window...
2151   By keeping these functions separate from the internally used IRAM_W/IRAM_R functions,
2152   we can manipulate and display internal memory in the debugger memory window in a layout
2153   that is not necessarily how the real memory is.. this will be especially useful for
2154   the 8052 chip where both the SFR and the upper 128 bytes of ram are mapped to the same
2155   address, so we can handle that here by mapping the sfr to a different address */
2156
2157READ8_HANDLER(ds5002fp_internal_r)
2158{
2159   //Restrict internal ram to 256 Bytes max
2160   if(offset < 0x100)
2161      return IRAM_R(offset);
2162   else
2163      return 0;
2164}
2165WRITE8_HANDLER(ds5002fp_internal_w)
2166{
2167   //Restrict internal ram to 256 Bytes max
2168   if(offset < 0x100)
2169      IRAM_W(offset,data);
2170}
2171
2172INLINE void do_add_flags(UINT8 a, UINT8 data, UINT8 c)
2173{
2174   UINT16 result = a+data+c;
2175   INT16 result1 = (INT8)a+(INT8)data+c;
2176   int cy, ac, ov;
2177
2178   cy = (result & 0x100) >> 8;
2179   result = (a&0x0f)+(data&0x0f)+c;
2180   ac = (result & 0x10) >> 4;
2181   ov = (result1 < -128 || result1 > 127);
2182
2183   SET_CY(cy);
2184   SET_AC(ac);
2185   SET_OV(ov);
2186
2187#ifdef ENABLE_DEBUGGER
2188//  mame_printf_debug("add: result=%x, c=%x, ac=%x, ov=%x\n",a+data+c,cy,ac,ov);
2189#endif
2190}
2191
2192INLINE void do_sub_flags(UINT8 a, UINT8 data, UINT8 c)
2193{
2194   UINT16 result = a-(data+c);
2195   INT16 result1 = (INT8)a-(INT8)(data+c);
2196   int cy, ac, ov;
2197   cy = (result & 0x100) >> 8;
2198   result = (a&0x0f)-((data&0x0f)+c);
2199   ac = (result & 0x10) >> 4;
2200   ov = (result1 < -128 || result1 > 127);
2201   SET_CY(cy);
2202   SET_AC(ac);
2203   SET_OV(ov);
2204
2205#ifdef ENABLE_DEBUGGER
2206//  mame_printf_debug("sub: a=%x, d=%x, c=%x, result=%x, cy=%x, ac=%x, ov=%x\n",a,data,c,a-data-c,cy,ac,ov);
2207#endif
2208}
2209
2210INLINE void update_timer(int cyc)
2211{
2212   //This code sucks, needs to be rewritten SJE
2213
2214   //Todo: Probably better to store the current mode of the timer on a write, so we don't waste time reading it.
2215
2216   //Note: Counting modes increment on 1 machine cycle (12 oscilator periods) - except Timer 2 in certain modes
2217
2218   //Update Timer 0
2219   if(GET_TR0) {
2220      //Determine Mode
2221      int mode = (GET_M0_1<<1) | GET_M0_0;
2222      int overflow;
2223      UINT16 count = 0;
2224      switch(mode) {
2225         case 0:         //13 Bit Timer Mode
2226            count = ((R_TH0<<8) | R_TL0);
2227            overflow = 0x3fff;
2228            //Todo - really, we update HI counter when LO counter hits 0x20
2229         case 1:         //16 Bit Timer Mode
2230            count = ((R_TH0<<8) | R_TL0);
2231            overflow = 0xffff;
2232            //Check for overflow
2233            if((UINT32)(count+(cyc/12))>overflow) {
2234               //Any overflow from cycles?
2235               cyc-= (overflow-count)*12;
2236               count = 0;
2237                    SET_TF0(1);
2238            }
2239            //Update the timer
2240            if(cyc) {
2241               int inctimer = 0;
2242               //Gate Bit Set? Timer only incremented if Int0 is set!
2243               if(GET_GATE0 && GET_IE0)
2244                  inctimer = (cyc/12);
2245               //Counter Mode? Only increment on 1-0 transition of the Port 3's T0 Line
2246               if(GET_CT0) {
2247                  //Not supported
2248               }
2249               //Neither, regular timer mode
2250               if(!GET_GATE0 && !GET_CT0)
2251                  inctimer = (cyc/12);
2252
2253               count+=inctimer;      //Increment counter
2254            }
2255            //Update new values of the counter
2256            R_TH0 = (count>>8) & 0xff;
2257            R_TL0 = count & 0xff;
2258            break;
2259         case 2:         //8 Bit Autoreload
2260            overflow = 0xff;
2261            count = R_TL0;
2262            //Check for overflow
2263            if(count+(cyc/12)>overflow) {
2264                    SET_TF0(1);
2265               //Reload
2266               count = R_TH0+(overflow-count);
2267            }
2268            else
2269               count+=(cyc/12);
2270            //Update new values of the counter
2271            R_TL0 = count & 0xff;
2272            break;
2273         case 3:         //Split Timer
2274                //Split Timer 1
2275            overflow = 0xff;
2276            count = R_TL0;
2277            //Check for overflow
2278                if(count+(cyc/12)>overflow) {
2279               count = overflow-count;
2280                    SET_TF0(1);
2281                }
2282            else
2283               count+=(cyc/12);
2284            //Update new values of the counter
2285            R_TL0 = count & 0xff;
2286
2287                //Split Timer 2
2288                if(GET_TR1) {
2289                overflow = 0xff;
2290                count = R_TH0;
2291                //Check for overflow
2292                    if(count+(cyc/12)>overflow) {
2293                   count = overflow-count;
2294                        SET_TF1(1);
2295                    }
2296                else
2297                        count+=(cyc/12);
2298                //Update new values of the counter
2299                R_TH0 = count & 0xff;
2300                }
2301            break;
2302      }
2303   }
2304
2305   //Update Timer 1
2306   if(GET_TR1) {
2307      //Determine Mode
2308      int mode = (GET_M1_1<<1) | GET_M1_0;
2309      int overflow;
2310      UINT16 count = 0;
2311      switch(mode) {
2312         case 0:         //13 Bit Timer Mode
2313            count = ((R_TH1<<8) | R_TL1);
2314            overflow = 0x3fff;
2315            //Todo - really, we update HI counter when LO counter hits 0x20
2316         case 1:         //16 Bit Timer Mode
2317            count = ((R_TH1<<8) | R_TL1);
2318            overflow = 0xffff;
2319            //Check for overflow
2320            if((UINT32)(count+(cyc/12))>overflow) {
2321
2322               //TODO: Timer 1 can be set as Serial Baud Rate in the 8051 only... process bits here..
2323
2324               //Any overflow from cycles?
2325               cyc-= (overflow-count)*12;
2326               count = 0;
2327                    SET_TF1(1);
2328            }
2329            //Update the timer
2330            if(cyc) {
2331               int inctimer = 0;
2332               //Gate Bit Set? Timer only incremented if Int0 is set!
2333               if(GET_GATE1 && GET_IE1)
2334                  inctimer = (cyc/12);
2335               //Counter Mode? Only increment on 1-0 transition of the Port 3's T0 Line
2336               if(GET_CT1) {
2337                  //Not supported
2338               }
2339               //Neither, regular timer mode
2340               if(!GET_GATE1 && !GET_CT1)
2341                  inctimer = (cyc/12);
2342
2343               count+=inctimer;      //Increment counter
2344            }
2345            //Update new values of the counter
2346            R_TH1 = (count>>8) & 0xff;
2347            R_TL1 = count & 0xff;
2348            break;
2349         case 2:         //8 Bit Autoreload
2350            overflow = 0xff;
2351            count = R_TL1;
2352            //Check for overflow
2353            if(count+(cyc/12)>overflow) {
2354                    SET_TF1(1);
2355               //Reload
2356               count = R_TH1+(overflow-count);
2357            }
2358            else
2359               count+=(cyc/12);
2360            //Update new values of the counter
2361            R_TL1 = count & 0xff;
2362            break;
2363         case 3:         //Split Timer
2364            break;
2365      }
2366   }
2367}
2368
2369//Set up to transmit data out of serial port
2370//NOTE: Enable Serial Port Interrupt bit is NOT required to send/receive data!
2371INLINE void serial_transmit(UINT8 data)
2372{
2373   int mode = (GET_SM0<<1) | GET_SM1;
2374
2375   //Flag that we're sending data
2376   uart.sending = 1;
2377   uart.data_out = data;
2378   switch(mode) {
2379      //8 bit shifter ( + start,stop bit ) - baud set by clock freq / 12
2380      case 0:
2381         uart.timerbaud = 0;
2382         uart.bitcycles = 0;
2383         uart.bits_to_send = 8+2;
2384         break;
2385      //8 bit uart ( + start,stop bit ) - baud set by timer1 or timer2
2386      case 1:
2387         uart.timerbaud = 1;
2388         uart.bits_to_send = 8+2;
2389         break;
2390      //9 bit uart
2391      case 2:
2392      case 3:
2393         LOG(("Serial mode 2 & 3 not supported in ds5002fp!\n"));
2394         break;
2395   }
2396}
2397
2398//Check and update status of serial port
2399INLINE void   update_serial(int cyc)
2400{
2401   //Any bits left to send?
2402   if(uart.bits_to_send) {
2403      //Timer Generated baud?
2404      if(uart.timerbaud) {
2405         //Let Timer overflow handle removing bits
2406      }
2407      else {
2408         //Oscillator Based baud rate = Osc/12 baud rate, however it also means 1 bit = 12 cycles.
2409         uart.bitcycles+=cyc;
2410         if(uart.bitcycles > 11)   {
2411            int bits_sent = uart.bitcycles / 12;
2412            int diff = uart.bitcycles % 12;
2413            //don't allow more bits sent than ready to send
2414            if(bits_sent > uart.bits_to_send) {
2415               bits_sent = uart.bits_to_send;
2416               diff = 0;
2417            }
2418            uart.bits_to_send-=bits_sent;
2419            uart.bitcycles = diff;
2420         }
2421      }
2422   }
2423   //If no bits left to send - flag the interrupt & call the callback
2424   if(!uart.bits_to_send) {
2425      //Clear sending flag
2426      uart.sending = 0;
2427      uart.bitcycles = 0;
2428      //Call the callback function
2429      if(ds5002fp.serial_tx_callback)
2430         ds5002fp.serial_tx_callback(uart.data_out);
2431      //Set Interrupt Flag
2432      SET_TI(1);
2433      //Note: we'll let the main loop catch the interrupt
2434   }
2435}
2436
2437/**************************************************************************
2438 * Generic set_info
2439 **************************************************************************/
2440
2441static void ds5002fp_set_info(UINT32 state, cpuinfo *info)
2442{
2443   switch (state)
2444   {
2445      /* --- the following bits of info are set as 64-bit signed integers --- */
2446      case CPUINFO_INT_PC:                     PC = info->i;                     break;
2447      case CPUINFO_INT_SP:                     ds5002fp.sp = info->i;                    break;
2448
2449      case CPUINFO_INT_INPUT_STATE + DS5002FP_INT0_LINE:   ds5002fp_set_irq_line(DS5002FP_INT0_LINE, info->i); break;
2450      case CPUINFO_INT_INPUT_STATE + DS5002FP_INT1_LINE:   ds5002fp_set_irq_line(DS5002FP_INT1_LINE, info->i); break;
2451      case CPUINFO_INT_INPUT_STATE + DS5002FP_T0_LINE:   ds5002fp_set_irq_line(DS5002FP_T0_LINE, info->i); break;
2452      case CPUINFO_INT_INPUT_STATE + DS5002FP_T1_LINE:   ds5002fp_set_irq_line(DS5002FP_T1_LINE, info->i); break;
2453      case CPUINFO_INT_INPUT_STATE + DS5002FP_RX_LINE:   ds5002fp_set_irq_line(DS5002FP_RX_LINE, info->i); break;
2454      case CPUINFO_INT_INPUT_STATE + DS5002FP_PFI_LINE:   ds5002fp_set_irq_line(DS5002FP_PFI_LINE, info->i); break;
2455
2456      case CPUINFO_INT_REGISTER + DS5002FP_PC:          PC = info->i;                     break;
2457      case CPUINFO_INT_REGISTER + DS5002FP_SP:          R_SP = info->i;                     break;
2458      case CPUINFO_INT_REGISTER + DS5002FP_PSW:         ds5002fp.psw = info->i;               break;
2459      case CPUINFO_INT_REGISTER + DS5002FP_ACC:         ds5002fp.acc = info->i;               break;
2460      case CPUINFO_INT_REGISTER + DS5002FP_B:           ds5002fp.b = info->i;                  break;
2461      case CPUINFO_INT_REGISTER + DS5002FP_DPH:         ds5002fp.dph = info->i;               break;
2462      case CPUINFO_INT_REGISTER + DS5002FP_DPL:         ds5002fp.dpl = info->i;               break;
2463      case CPUINFO_INT_REGISTER + DS5002FP_IE:          ds5002fp.ie = info->i;                  break;
2464      case CPUINFO_INT_REGISTER + DS5002FP_PCON:         ds5002fp.pcon = info->i;             break;
2465      case CPUINFO_INT_REGISTER + DS5002FP_MCON:         ds5002fp.mcon = info->i;             break;
2466      case CPUINFO_INT_REGISTER + DS5002FP_RPCTL:       ds5002fp.rpctl = info->i;             break;
2467      case CPUINFO_INT_REGISTER + DS5002FP_CRC:         ds5002fp.crc = info->i;             break;
2468      case CPUINFO_INT_REGISTER + DS5002FP_R0:          ds5002fp.IntRam[0+(8*((ds5002fp.psw & 0x18)>>3))] = info->i; break;
2469      case CPUINFO_INT_REGISTER + DS5002FP_R1:          ds5002fp.IntRam[1+(8*((ds5002fp.psw & 0x18)>>3))] = info->i; break;
2470      case CPUINFO_INT_REGISTER + DS5002FP_R2:          ds5002fp.IntRam[2+(8*((ds5002fp.psw & 0x18)>>3))] = info->i; break;
2471      case CPUINFO_INT_REGISTER + DS5002FP_R3:          ds5002fp.IntRam[3+(8*((ds5002fp.psw & 0x18)>>3))] = info->i; break;
2472      case CPUINFO_INT_REGISTER + DS5002FP_R4:          ds5002fp.IntRam[4+(8*((ds5002fp.psw & 0x18)>>3))] = info->i; break;
2473      case CPUINFO_INT_REGISTER + DS5002FP_R5:          ds5002fp.IntRam[5+(8*((ds5002fp.psw & 0x18)>>3))] = info->i; break;
2474      case CPUINFO_INT_REGISTER + DS5002FP_R6:          ds5002fp.IntRam[6+(8*((ds5002fp.psw & 0x18)>>3))] = info->i; break;
2475      case CPUINFO_INT_REGISTER + DS5002FP_R7:          ds5002fp.IntRam[7+(8*((ds5002fp.psw & 0x18)>>3))] = info->i; break;
2476      case CPUINFO_INT_REGISTER + DS5002FP_RB:          ds5002fp.IntRam[8+(8*((ds5002fp.psw & 0x18)>>3))] = info->i; break;
2477   }
2478}
2479
2480/**************************************************************************
2481 * Generic get_info
2482 **************************************************************************/
2483
2484void ds5002fp_get_info(UINT32 state, cpuinfo *info)
2485{
2486   DS5002FP *r = &ds5002fp;
2487
2488   switch (state)
2489   {
2490      /* --- the following bits of info are returned as 64-bit signed integers --- */
2491      case CPUINFO_INT_CONTEXT_SIZE:               info->i = sizeof(ds5002fp);            break;
2492      case CPUINFO_INT_DEFAULT_IRQ_VECTOR:         info->i = 0;                     break;
2493      case CPUINFO_INT_ENDIANNESS:               info->i = CPU_IS_LE;               break;
2494      case CPUINFO_INT_CLOCK_MULTIPLIER:            info->i = 1;                     break;
2495      case CPUINFO_INT_CLOCK_DIVIDER:               info->i = 1;                     break;
2496      case CPUINFO_INT_MIN_INSTRUCTION_BYTES:         info->i = 1;                     break;
2497      case CPUINFO_INT_MAX_INSTRUCTION_BYTES:         info->i = 5;                     break;
2498      case CPUINFO_INT_MIN_CYCLES:               info->i = 1;                     break;
2499      case CPUINFO_INT_MAX_CYCLES:               info->i = 20; /* rough guess */         break;
2500      case CPUINFO_INT_INPUT_LINES:                 info->i = 6;                     break;
2501
2502      case CPUINFO_INT_DATABUS_WIDTH + ADDRESS_SPACE_PROGRAM:   info->i = 8;               break;
2503      case CPUINFO_INT_ADDRBUS_WIDTH + ADDRESS_SPACE_PROGRAM: info->i = 16;               break;
2504      case CPUINFO_INT_ADDRBUS_SHIFT + ADDRESS_SPACE_PROGRAM: info->i = 0;               break;
2505      case CPUINFO_INT_DATABUS_WIDTH + ADDRESS_SPACE_DATA:   info->i = 8;               break;
2506      case CPUINFO_INT_ADDRBUS_WIDTH + ADDRESS_SPACE_DATA:    info->i = 17; /* really 16 bits, but it has 2 address bus */ break;
2507      case CPUINFO_INT_ADDRBUS_SHIFT + ADDRESS_SPACE_DATA:    info->i = 0;               break;
2508      case CPUINFO_INT_DATABUS_WIDTH + ADDRESS_SPACE_IO:   info->i = 8;                  break;
2509      case CPUINFO_INT_ADDRBUS_WIDTH + ADDRESS_SPACE_IO:    info->i = 16;                  break;
2510      case CPUINFO_INT_ADDRBUS_SHIFT + ADDRESS_SPACE_IO:    info->i = 0;                  break;
2511
2512      case CPUINFO_INT_PREVIOUSPC:               info->i = PPC;                     break;
2513      case CPUINFO_INT_PC:                      info->i = PC;                     break;
2514      case CPUINFO_INT_SP:                     info->i = R_SP;                     break;
2515
2516      case CPUINFO_INT_REGISTER + DS5002FP_PC:          info->i = PC;                     break;
2517      case CPUINFO_INT_REGISTER + DS5002FP_SP:          info->i = R_SP;                     break;
2518      case CPUINFO_INT_REGISTER + DS5002FP_PSW:         info->i = ds5002fp.psw;               break;
2519      case CPUINFO_INT_REGISTER + DS5002FP_ACC:         info->i = ds5002fp.acc;               break;
2520      case CPUINFO_INT_REGISTER + DS5002FP_B:           info->i = ds5002fp.b;                  break;
2521      case CPUINFO_INT_REGISTER + DS5002FP_DPH:         info->i = ds5002fp.dph;               break;
2522      case CPUINFO_INT_REGISTER + DS5002FP_DPL:         info->i = ds5002fp.dpl;               break;
2523      case CPUINFO_INT_REGISTER + DS5002FP_IE:          info->i = ds5002fp.ie;                  break;
2524      case CPUINFO_INT_REGISTER + DS5002FP_PCON:         info->i = ds5002fp.pcon;             break;
2525      case CPUINFO_INT_REGISTER + DS5002FP_MCON:         info->i = ds5002fp.mcon;             break;
2526      case CPUINFO_INT_REGISTER + DS5002FP_RPCTL:       info->i = ds5002fp.rpctl;             break;
2527      case CPUINFO_INT_REGISTER + DS5002FP_CRC:         info->i = ds5002fp.crc;             break;
2528      case CPUINFO_INT_REGISTER + DS5002FP_R0:          info->i = ds5002fp.IntRam[0+(8*((ds5002fp.psw & 0x18)>>3))]; break;
2529      case CPUINFO_INT_REGISTER + DS5002FP_R1:          info->i = ds5002fp.IntRam[1+(8*((ds5002fp.psw & 0x18)>>3))]; break;
2530      case CPUINFO_INT_REGISTER + DS5002FP_R2:          info->i = ds5002fp.IntRam[2+(8*((ds5002fp.psw & 0x18)>>3))]; break;
2531      case CPUINFO_INT_REGISTER + DS5002FP_R3:          info->i = ds5002fp.IntRam[3+(8*((ds5002fp.psw & 0x18)>>3))]; break;
2532      case CPUINFO_INT_REGISTER + DS5002FP_R4:          info->i = ds5002fp.IntRam[4+(8*((ds5002fp.psw & 0x18)>>3))]; break;
2533      case CPUINFO_INT_REGISTER + DS5002FP_R5:          info->i = ds5002fp.IntRam[5+(8*((ds5002fp.psw & 0x18)>>3))]; break;
2534      case CPUINFO_INT_REGISTER + DS5002FP_R6:          info->i = ds5002fp.IntRam[6+(8*((ds5002fp.psw & 0x18)>>3))]; break;
2535      case CPUINFO_INT_REGISTER + DS5002FP_R7:          info->i = ds5002fp.IntRam[7+(8*((ds5002fp.psw & 0x18)>>3))]; break;
2536      case CPUINFO_INT_REGISTER + DS5002FP_RB:          info->i = ds5002fp.IntRam[8+(8*((ds5002fp.psw & 0x18)>>3))]; break;
2537
2538      /* --- the following bits of info are returned as pointers to data or functions --- */
2539      case CPUINFO_PTR_SET_INFO:                  info->setinfo = ds5002fp_set_info;         break;
2540      case CPUINFO_PTR_GET_CONTEXT:               info->getcontext = ds5002fp_get_context;   break;
2541      case CPUINFO_PTR_SET_CONTEXT:               info->setcontext = ds5002fp_set_context;   break;
2542      case CPUINFO_PTR_INIT:                     info->init = ds5002fp_init;            break;
2543      case CPUINFO_PTR_RESET:                     info->reset = ds5002fp_reset;            break;
2544      case CPUINFO_PTR_EXIT:                     info->exit = ds5002fp_exit;            break;
2545      case CPUINFO_PTR_EXECUTE:                  info->execute = ds5002fp_execute;         break;
2546      case CPUINFO_PTR_BURN:                     info->burn = NULL;                  break;
2547#ifdef ENABLE_DEBUGGER
2548      case CPUINFO_PTR_DISASSEMBLE:               info->disassemble = ds5002fp_dasm;         break;
2549#endif /* ENABLE_DEBUGGER */
2550      case CPUINFO_PTR_INSTRUCTION_COUNTER:         info->icount = &ds5002fp_icount;         break;
2551
2552      case CPUINFO_PTR_INTERNAL_MEMORY_MAP + ADDRESS_SPACE_PROGRAM: info->internal_map8 = NULL;   break;
2553      case CPUINFO_PTR_INTERNAL_MEMORY_MAP + ADDRESS_SPACE_DATA:    info->internal_map8 = NULL;   break;
2554      case CPUINFO_PTR_INTERNAL_MEMORY_MAP + ADDRESS_SPACE_IO:      info->internal_map8 = NULL;   break;
2555
2556      /* --- the following bits of info are returned as NULL-terminated strings --- */
2557      case CPUINFO_STR_NAME:                     strcpy(info->s, "DS5002FP");            break;
2558      case CPUINFO_STR_CORE_FAMILY:               strcpy(info->s, "Dallas");            break;
2559      case CPUINFO_STR_CORE_VERSION:               strcpy(info->s, "1.0");               break;
2560      case CPUINFO_STR_CORE_FILE:                  strcpy(info->s, __FILE__);            break;
2561      case CPUINFO_STR_CORE_CREDITS:               strcpy(info->s, "Copyright Manuel Abadia"); break;
2562
2563      case CPUINFO_STR_FLAGS:
2564         sprintf(info->s, "%c%c%c%c%c%c%c%c",
2565            r->psw & 0x80 ? 'C':'.',
2566            r->psw & 0x40 ? 'A':'.',
2567            r->psw & 0x20 ? 'F':'.',
2568            r->psw & 0x10 ? '0':'.',
2569            r->psw & 0x08 ? '1':'.',
2570            r->psw & 0x04 ? 'V':'.',
2571            r->psw & 0x02 ? '?':'.',
2572            r->psw & 0x01 ? 'P':'.');
2573         break;
2574
2575      case CPUINFO_STR_REGISTER + DS5002FP_PC:           sprintf(info->s, "PC:%04X", r->pc);      break;
2576      case CPUINFO_STR_REGISTER + DS5002FP_SP:           sprintf(info->s, "SP:%02X", r->sp);      break;
2577      case CPUINFO_STR_REGISTER + DS5002FP_PSW:          sprintf(info->s, "PSW:%02X", r->psw);   break;
2578      case CPUINFO_STR_REGISTER + DS5002FP_ACC:          sprintf(info->s, "A:%02X", r->acc);      break;
2579      case CPUINFO_STR_REGISTER + DS5002FP_B:            sprintf(info->s, "B:%02X", r->b);      break;
2580      case CPUINFO_STR_REGISTER + DS5002FP_DPH:          sprintf(info->s, "DPH:%02X", r->dph);   break;
2581      case CPUINFO_STR_REGISTER + DS5002FP_DPL:          sprintf(info->s, "DPL:%02X", r->dpl);   break;
2582      case CPUINFO_STR_REGISTER + DS5002FP_IE:           sprintf(info->s, "IE:%02X", r->ie);      break;
2583      case CPUINFO_STR_REGISTER + DS5002FP_PCON:           sprintf(info->s, "PCON:%02X", r->pcon);   break;
2584      case CPUINFO_STR_REGISTER + DS5002FP_MCON:           sprintf(info->s, "MCON:%02X", r->mcon);   break;
2585      case CPUINFO_STR_REGISTER + DS5002FP_RPCTL:           sprintf(info->s, "RPCTL:%02X", r->rpctl);break;
2586      case CPUINFO_STR_REGISTER + DS5002FP_CRC:           sprintf(info->s, "CRC:%02X", r->crc);   break;
2587      case CPUINFO_STR_REGISTER + DS5002FP_R0:           sprintf(info->s, "R0:%02X", r->IntRam[0+(8*((r->psw & 0x18)>>3))]); break;
2588      case CPUINFO_STR_REGISTER + DS5002FP_R1:           sprintf(info->s, "R1:%02X", r->IntRam[1+(8*((r->psw & 0x18)>>3))]); break;
2589      case CPUINFO_STR_REGISTER + DS5002FP_R2:           sprintf(info->s, "R2:%02X", r->IntRam[2+(8*((r->psw & 0x18)>>3))]); break;
2590      case CPUINFO_STR_REGISTER + DS5002FP_R3:         sprintf(info->s, "R3:%02X", r->IntRam[3+(8*((r->psw & 0x18)>>3))]); break;
2591      case CPUINFO_STR_REGISTER + DS5002FP_R4:          sprintf(info->s, "R4:%02X", r->IntRam[4+(8*((r->psw & 0x18)>>3))]); break;
2592      case CPUINFO_STR_REGISTER + DS5002FP_R5:          sprintf(info->s, "R5:%02X", r->IntRam[5+(8*((r->psw & 0x18)>>3))]); break;
2593      case CPUINFO_STR_REGISTER + DS5002FP_R6:          sprintf(info->s, "R6:%02X", r->IntRam[6+(8*((r->psw & 0x18)>>3))]); break;
2594      case CPUINFO_STR_REGISTER + DS5002FP_R7:          sprintf(info->s, "R7:%02X", r->IntRam[7+(8*((r->psw & 0x18)>>3))]); break;
2595      case CPUINFO_STR_REGISTER + DS5002FP_RB:          sprintf(info->s, "RB:%02X", ((r->psw & 0x18)>>3)); break;
2596   }
2597}
src/emu/cpu/ds5002fp/ds5002fpops.c
r0r2628
1/*******************************************************************************************
2 NOTE: All registers are accessed directly, instead of using the SFR_R() function for speed
3 Direct register access is availabe from the R_(register name) macros.. ex: R_ACC for the ACC
4 with the exception of the PC
5********************************************************************************************/
6
7//ACALL code addr                           /* 1: aaa1 0001 */
8INLINE void acall(void)
9{
10   UINT8 op = ROP(PC-1);               //Grab the opcode for ACALL
11   UINT8 addr = ROP_ARG(PC++);            //Grab code address byte
12   PUSH_PC                           //Save PC to the stack
13   //Thanks Gerrit for help with this! :)
14   PC = (PC & 0xf800) | ((op & 0xe0) << 3) | addr;
15}
16
17//ADD A, #data                              /* 1: 0010 0100 */
18INLINE void add_a_byte(void)
19{
20   UINT8 data = ROP_ARG(PC++);            //Grab data
21   UINT8 result = R_ACC + data;         //Add data to accumulator
22   DO_ADD_FLAGS(R_ACC,data,0)            //Set Flags
23   SFR_W(ACC,result);                  //Store 8 bit result of addtion in ACC
24}
25
26//ADD A, data addr                          /* 1: 0010 0101 */
27INLINE void add_a_mem(void)
28{
29   UINT8 addr = ROP_ARG(PC++);            //Grab data address
30   UINT8 data = IRAM_R(addr);            //Grab data from data address
31   UINT8 result = R_ACC + data;         //Add data to accumulator
32   DO_ADD_FLAGS(R_ACC,data,0);            //Set Flags
33   SFR_W(ACC,result);                  //Store 8 bit result of addtion in ACC
34}
35
36//ADD A, @R0/@R1                            /* 1: 0010 011i */
37INLINE void add_a_ir(int r)
38{
39   UINT8 data = IRAM_IR(R_R(r));         //Grab data from memory pointed to by R0 or R1
40   UINT8 result = R_ACC + data;         //Add data to accumulator
41   DO_ADD_FLAGS(R_ACC,data,0);            //Set Flags
42   SFR_W(ACC,result);                  //Store 8 bit result of addtion in ACC
43}
44
45//ADD A, R0 to R7                           /* 1: 0010 1rrr */
46INLINE void add_a_r(int r)
47{
48   UINT8 data = R_R(r);               //Grab data from R0 - R7
49   UINT8 result = R_ACC + data;         //Add data to accumulator
50   DO_ADD_FLAGS(R_ACC,data,0);            //Set Flags
51   SFR_W(ACC,result);                  //Store 8 bit result of addtion in ACC
52}
53
54//ADDC A, #data                             /* 1: 0011 0100 */
55INLINE void addc_a_byte(void)
56{
57   UINT8 data = ROP_ARG(PC++);            //Grab data
58   UINT8 result = R_ACC + data + GET_CY;   //Add data + carry flag to accumulator
59   DO_ADD_FLAGS(R_ACC,data,GET_CY);      //Set Flags
60   SFR_W(ACC,result);                  //Store 8 bit result of addtion in ACC
61}
62
63//ADDC A, data addr                         /* 1: 0011 0101 */
64INLINE void addc_a_mem(void)
65{
66   UINT8 addr = ROP_ARG(PC++);            //Grab data address
67   UINT8 data = IRAM_R(addr);            //Grab data from data address
68   UINT8 result = R_ACC + data + GET_CY;   //Add data + carry flag to accumulator
69   DO_ADD_FLAGS(R_ACC,data,GET_CY);      //Set Flags
70   SFR_W(ACC,result);                  //Store 8 bit result of addtion in ACC
71}
72
73//ADDC A, @R0/@R1                           /* 1: 0011 011i */
74INLINE void addc_a_ir(int r)
75{
76   UINT8 data = IRAM_IR(R_R(r));         //Grab data from memory pointed to by R0 or R1
77   UINT8 result = R_ACC + data + GET_CY;   //Add data + carry flag to accumulator
78   DO_ADD_FLAGS(R_ACC,data,GET_CY);      //Set Flags
79   SFR_W(ACC,result);                  //Store 8 bit result of addtion in ACC
80}
81
82//ADDC A, R0 to R7                          /* 1: 0011 1rrr */
83INLINE void addc_a_r(int r)
84{
85   UINT8 data = R_R(r);               //Grab data from R0 - R7
86   UINT8 result = R_ACC + data + GET_CY;   //Add data + carry flag to accumulator
87   DO_ADD_FLAGS(R_ACC,data,GET_CY);      //Set Flags
88   SFR_W(ACC,result);                  //Store 8 bit result of addtion in ACC
89}
90
91//AJMP code addr                            /* 1: aaa0 0001 */
92INLINE void ajmp(void)
93{
94   UINT8 op = ROP(PC-1);               //Grab the opcode for AJMP
95   UINT8 addr = ROP_ARG(PC++);            //Grab code address byte
96   //Thanks Gerrit for help with this! :)
97   PC = (PC & 0xf800) | ((op & 0xe0) << 3) | addr;
98}
99
100//ANL data addr, A                          /* 1: 0101 0010 */
101INLINE void anl_mem_a(void)
102{
103   UINT8 addr = ROP_ARG(PC++);            //Grab data address
104   UINT8 data = IRAM_R(addr);            //Grab data from data address
105   IRAM_W(addr,data & R_ACC);            //Set data address value to it's value Logical AND with ACC
106}
107
108//ANL data addr, #data                      /* 1: 0101 0011 */
109INLINE void anl_mem_byte(void)
110{
111   UINT8 addr = ROP_ARG(PC++);            //Grab data address
112   UINT8 data = ROP_ARG(PC++);            //Grab data
113   UINT8 srcdata = IRAM_R(addr);         //Grab data from data address
114   IRAM_W(addr,srcdata & data);         //Set data address value to it's value Logical AND with Data
115}
116
117//ANL A, #data                              /* 1: 0101 0100 */
118INLINE void anl_a_byte(void)
119{
120   UINT8 data = ROP_ARG(PC++);            //Grab data
121   SFR_W(ACC,R_ACC & data);            //Set ACC to value of ACC Logical AND with Data
122}
123
124//ANL A, data addr                          /* 1: 0101 0101 */
125INLINE void anl_a_mem(void)
126{
127   UINT8 addr = ROP_ARG(PC++);            //Grab data address
128   UINT8 data = IRAM_R(addr);            //Grab data from data address
129   SFR_W(ACC,R_ACC & data);            //Set ACC to value of ACC Logical AND with Data
130}
131
132//ANL A, @RO/@R1                            /* 1: 0101 011i */
133INLINE void anl_a_ir(int r)
134{
135   UINT8 data = IRAM_IR(R_R(r));         //Grab data from address R0 or R1 points to
136   SFR_W(ACC,R_ACC & data);            //Set ACC to value of ACC Logical AND with Data
137}
138
139//ANL A, RO to R7                           /* 1: 0101 1rrr */
140INLINE void anl_a_r(int r)
141{
142   UINT8 data = R_R(r);               //Grab data from R0 - R7
143   SFR_W(ACC,R_ACC & data);            //Set ACC to value of ACC Logical AND with Data
144}
145
146//ANL C, bit addr                           /* 1: 1000 0010 */
147INLINE void anl_c_bitaddr(void)
148{
149   int cy = GET_CY;
150   UINT8 addr = ROP_ARG(PC++);            //Grab bit address
151   UINT8 bit = BIT_R(addr);            //Grab bit data from bit address
152   SET_CY( (cy & bit) );               //Set Carry flag to Carry Flag Value Logical AND with Bit
153}
154
155//ANL C,/bit addr                           /* 1: 1011 0000 */
156INLINE void anl_c_nbitaddr(void)
157{
158   int cy = GET_CY;
159   UINT8 addr = ROP_ARG(PC++);            //Grab bit address
160   UINT8 bit = BIT_R(addr);            //Grab bit data from bit address
161   bit = ((~bit)&1);                  //Complement bit
162   SET_CY( (cy & bit) );               //Set Carry flag to Carry Flag Value Logical AND with Complemented Bit
163}
164
165//CJNE A, #data, code addr                  /* 1: 1011 0100 */
166INLINE void cjne_a_byte(void)
167{
168   UINT8 data = ROP_ARG(PC++);            //Grab data
169   INT8 rel_addr = ROP_ARG(PC++);         //Grab relative code address
170
171   if(R_ACC != data)                  //Jump if values are not equal
172      PC = PC + rel_addr;
173
174   //Set carry flag to 1 if 1st compare value is < 2nd compare value
175   SET_CY( (R_ACC < data) );
176}
177
178//CJNE A, data addr, code addr              /* 1: 1011 0101 */
179INLINE void cjne_a_mem(void)
180{
181   UINT8 addr = ROP_ARG(PC++);            //Grab data address
182   INT8 rel_addr = ROP_ARG(PC++);         //Grab relative code address
183   UINT8 data = IRAM_R(addr);            //Pull value from data address
184
185   if(R_ACC != data)                  //Jump if values are not equal
186      PC = PC + rel_addr;
187
188   //Set carry flag to 1 if 1st compare value is < 2nd compare value
189   SET_CY( (R_ACC < data) );
190}
191
192//CJNE @R0/@R1, #data, code addr            /* 1: 1011 011i */
193INLINE void cjne_ir_byte(int r)
194{
195   UINT8 data = ROP_ARG(PC++);            //Grab data
196   INT8 rel_addr = ROP_ARG(PC++);         //Grab relative code address
197   UINT8 srcdata = IRAM_IR(R_R(r));      //Grab value pointed to by R0 or R1
198
199   if(srcdata != data)                  //Jump if values are not equal
200      PC = PC + rel_addr;
201
202   //Set carry flag to 1 if 1st compare value is < 2nd compare value
203   SET_CY( (srcdata < data) );
204}
205
206//CJNE R0 to R7, #data, code addr           /* 1: 1011 1rrr */
207INLINE void cjne_r_byte(int r)
208{
209   UINT8 data = ROP_ARG(PC++);            //Grab data
210   INT8 rel_addr = ROP_ARG(PC++);         //Grab relative code address
211   UINT8 srcdata = R_R(r);               //Grab value of R0 - R7
212
213   if(srcdata != data)                  //Jump if values are not equal
214      PC = PC + rel_addr;
215
216   //Set carry flag to 1 if 1st compare value is < 2nd compare value
217   SET_CY( (srcdata < data) );
218}
219
220//CLR bit addr                              /* 1: 1100 0010 */
221INLINE void clr_bitaddr(void)
222{
223   UINT8 addr = ROP_ARG(PC++);            //Grab bit address
224   BIT_W(addr,0);                     //Clear bit at specified bit address
225}
226
227//CLR C                                     /* 1: 1100 0011 */
228INLINE void clr_c(void)
229{
230   SET_CY(0);                        //Clear Carry Flag
231}
232
233//CLR A                                     /* 1: 1110 0100 */
234INLINE void clr_a(void)
235{
236   SFR_W(ACC,0);                     //Clear Accumulator
237}
238
239//CPL bit addr                              /* 1: 1011 0010 */
240INLINE void cpl_bitaddr(void)
241{
242   UINT8 addr = ROP_ARG(PC++);            //Grab bit address
243   UINT8 data = (~BIT_R(addr))&1;
244   BIT_W(addr,data);                  //Complement bit at specified bit address
245}
246
247//CPL C                                     /* 1: 1011 0011 */
248INLINE void cpl_c(void)
249{
250   UINT8 bit = (~GET_CY)&1;            //Complement Carry Flag
251   SET_CY(bit);
252}
253
254//CPL A                                     /* 1: 1111 0100 */
255INLINE void cpl_a(void)
256{
257   UINT8 data = ((~R_ACC)&0xff);
258   SFR_W(ACC,data);                  //Complement Accumulator
259}
260
261//DA A                                      /* 1: 1101 0100 */
262INLINE void da_a(void)
263{
264/*From several sources, since none said the same thing:
265 The decimal adjust instruction is associated with the use of the ADD and ADDC instructions.
266 The eight-bit value in the accumulator is adjusted to form two BCD digits of four bits each.
267 If the accumulator contents bits 0-3 are greater than 9, OR the AC flag is set, then six is added to
268 produce a proper BCD digit.
269 If the carry is set, OR the four high bits 4-7 exceed nine, six is added to the value of these bits.
270 The carry flag will be set if the result is > 0x99, but not cleared otherwise */
271
272 UINT16 new_acc = R_ACC & 0xff;
273 if(GET_AC || (new_acc & 0x0f) > 0x09)
274    new_acc += 0x06;
275 if(GET_CY || ((new_acc & 0xf0) > 0x90) || (new_acc & ~0xff))
276    new_acc += 0x60;
277 SFR_W(ACC,new_acc&0xff);
278 if(new_acc & ~0xff)
279   SET_CY(1);
280}
281
282//DEC A                                     /* 1: 0001 0100 */
283INLINE void dec_a(void)
284{
285   SFR_W(ACC,R_ACC-1);
286}
287
288//DEC data addr                             /* 1: 0001 0101 */
289INLINE void dec_mem(void)
290{
291   UINT8 addr = ROP_ARG(PC++);            //Grab data address
292   UINT8 data = IRAM_R(addr);
293   IRAM_W(addr,data-1);
294}
295
296//DEC @R0/@R1                               /* 1: 0001 011i */
297INLINE void dec_ir(int r)
298{
299   UINT8 data = IRAM_IR(R_R(r));
300   IRAM_W(R_R(r),data-1);
301}
302
303//DEC R0 to R7                              /* 1: 0001 1rrr */
304INLINE void dec_r(int r)
305{
306   R_R(r) = R_R(r) - 1;
307}
308
309//DIV AB                                    /* 1: 1000 0100 */
310INLINE void div_ab(void)
311{
312   if( R_B == 0 ) {
313      //Overflow flag is set!
314      SET_OV(1);
315      //Really the values are undefined according to the manual, but we'll just leave them as is..
316      //SFR_W(ACC,0xff);
317      //SFR_W(B,0xff);
318   }
319   else {
320      int a = (int)R_ACC/R_B;
321      int b = (int)R_ACC%R_B;
322      //A gets quotient byte, B gets remainder byte
323      SFR_W(ACC,a);
324      SFR_W(B,  b);
325      //Overflow flag is cleared
326      SET_OV(0);
327   }
328   //Carry Flag is always cleared
329   SET_CY(0);
330}
331
332//DJNZ data addr, code addr                 /* 1: 1101 0101 */
333INLINE void djnz_mem(void)
334{
335   UINT8 addr = ROP_ARG(PC++);            //Grab data address
336   INT8 rel_addr = ROP_ARG(PC++);         //Grab relative code address
337   IRAM_W(addr,IRAM_R(addr) - 1);         //Decrement value contained at data address
338   if(IRAM_R(addr) != 0)               //Branch if contents of data address is not 0
339      PC = PC + rel_addr;
340}
341
342//DJNZ R0 to R7,code addr                   /* 1: 1101 1rrr */
343INLINE void djnz_r(int r)
344{
345   INT8 rel_addr = ROP_ARG(PC++);         //Grab relative code address
346   R_R(r) = R_R(r) - 1;               //Decrement value
347   if(R_R(r) != 0)                     //Branch if contents of R0 - R7 is not 0
348      PC = PC + rel_addr;
349}
350
351//INC A                                     /* 1: 0000 0100 */
352INLINE void inc_a(void)
353{
354   SFR_W(ACC,R_ACC+1);
355}
356
357//INC data addr                             /* 1: 0000 0101 */
358INLINE void inc_mem(void)
359{
360   UINT8 addr = ROP_ARG(PC++);            //Grab data address
361   UINT8 data = IRAM_R(addr);
362   IRAM_W(addr,data+1);
363}
364
365//INC @R0/@R1                               /* 1: 0000 011i */
366INLINE void inc_ir(int r)
367{
368   UINT8 data = IRAM_IR(R_R(r));
369   IRAM_W(R_R(r),data+1);
370}
371
372//INC R0 to R7                              /* 1: 0000 1rrr */
373INLINE void inc_r(int r)
374{
375   UINT8 data = R_R(r);
376   R_R(r) = data + 1;
377}
378
379//INC DPTR                                  /* 1: 1010 0011 */
380INLINE void inc_dptr(void)
381{
382   UINT16 dptr = (R_DPTR)+1;
383   DPTR_W(dptr);
384}
385
386//JB  bit addr, code addr                   /* 1: 0010 0000 */
387INLINE void jb(void)
388{
389   UINT8 addr = ROP_ARG(PC++);            //Grab bit address
390   INT8 rel_addr = ROP_ARG(PC++);         //Grab relative code address
391   if(BIT_R(addr))                     //If bit set at specified bit address, jump
392      PC = PC + rel_addr;
393}
394
395//JBC bit addr, code addr                   /* 1: 0001 0000 */
396INLINE void jbc(void)
397{
398   UINT8 addr = ROP_ARG(PC++);            //Grab bit address
399   INT8 rel_addr = ROP_ARG(PC++);         //Grab relative code address
400   if(BIT_R(addr))   {                  //If bit set at specified bit address, jump
401      PC = PC + rel_addr;
402      BIT_W(addr,0);                  //Clear Bit also
403   }
404}
405
406//JC code addr                              /* 1: 0100 0000 */
407INLINE void jc(void)
408{
409   INT8 rel_addr = ROP_ARG(PC++);         //Grab relative code address
410   if(GET_CY)                        //Jump if Carry Flag Set
411      PC = PC + rel_addr;
412}
413
414//JMP @A+DPTR                               /* 1: 0111 0011 */
415INLINE void jmp_iadptr(void)
416{
417   PC = R_ACC+R_DPTR;
418}
419
420//JNB bit addr, code addr                   /* 1: 0011 0000 */
421INLINE void jnb(void)
422{
423   UINT8 addr = ROP_ARG(PC++);            //Grab bit address
424   INT8 rel_addr = ROP_ARG(PC++);         //Grab relative code address
425   if(!BIT_R(addr))                  //If bit NOT set at specified bit address, jump
426      PC = PC + rel_addr;
427}
428
429//JNC code addr                             /* 1: 0101 0000 */
430INLINE void jnc(void)
431{
432   INT8 rel_addr = ROP_ARG(PC++);         //Grab relative code address
433   if(!GET_CY)                        //Jump if Carry Flag not set
434      PC = PC + rel_addr;
435}
436
437//JNZ code addr                             /* 1: 0111 0000 */
438INLINE void jnz(void)
439{
440   INT8 rel_addr = ROP_ARG(PC++);         //Grab relative code address
441   if(R_ACC != 0)                     //Branch if ACC is not 0
442      PC = PC+rel_addr;
443}
444
445//JZ code addr                              /* 1: 0110 0000 */
446INLINE void jz(void)
447{
448   INT8 rel_addr = ROP_ARG(PC++);         //Grab relative code address
449   if(R_ACC == 0)                     //Branch if ACC is 0
450      PC = PC+rel_addr;
451}
452
453//LCALL code addr                           /* 1: 0001 0010 */
454INLINE void lcall(void)
455{
456   UINT8 addr_hi, addr_lo;
457   addr_hi = ROP_ARG(PC++);
458   addr_lo = ROP_ARG(PC++);
459   PUSH_PC
460   PC = (UINT16)((addr_hi<<8) | addr_lo);
461}
462
463//LJMP code addr                            /* 1: 0000 0010 */
464INLINE void ljmp(void)
465{
466   UINT8 addr_hi, addr_lo;
467   addr_hi = ROP_ARG(PC++);
468   addr_lo = ROP_ARG(PC++);
469   PC = (UINT16)((addr_hi<<8) | addr_lo);
470}
471
472//MOV A, #data                              /* 1: 0111 0100 */
473INLINE void mov_a_byte(void)
474{
475   UINT8 data = ROP_ARG(PC++);            //Grab data
476   SFR_W(ACC,data);                  //Store data to ACC
477}
478
479//MOV A, data addr                          /* 1: 1110 0101 */
480INLINE void mov_a_mem(void)
481{
482   UINT8 addr = ROP_ARG(PC++);            //Grab data address
483   SFR_W(ACC,IRAM_R(addr));            //Store contents of data address to ACC
484}
485
486//MOV A,@RO/@R1                             /* 1: 1110 011i */
487INLINE void mov_a_ir(int r)
488{
489   SFR_W(ACC,IRAM_IR(R_R(r)));            //Store contents of address pointed by R0 or R1 to ACC
490}
491
492//MOV A,R0 to R7                            /* 1: 1110 1rrr */
493INLINE void mov_a_r(int r)
494{
495   SFR_W(ACC,R_R(r));                  //Store contents of R0 - R7 to ACC
496}
497
498//MOV data addr, #data                      /* 1: 0111 0101 */
499INLINE void mov_mem_byte(void)
500{
501   UINT8 addr = ROP_ARG(PC++);            //Grab data address
502   UINT8 data = ROP_ARG(PC++);            //Grab data
503   IRAM_W(addr,data);                  //Store data to data address location
504}
505
506//MOV data addr, data addr                  /* 1: 1000 0101 */
507INLINE void mov_mem_mem(void)
508{
509   //1st address is src, 2nd is dst, but the mov command works as mov dst,src)
510   UINT8 src,dst;
511   src = ROP_ARG(PC++);               //Grab source data address
512   dst = ROP_ARG(PC++);               //Grab destination data address
513   IRAM_W(dst,IRAM_R(src));            //Read source address contents and store to destination address
514}
515
516//MOV @R0/@R1, #data                        /* 1: 0111 011i */
517INLINE void mov_ir_byte(int r)
518{
519   UINT8 data = ROP_ARG(PC++);            //Grab data
520   IRAM_IW(R_R(r),data);               //Store data to address pointed by R0 or R1
521}
522
523//MOV R0 to R7, #data                       /* 1: 0111 1rrr */
524INLINE void mov_r_byte(int r)
525{
526   UINT8 data = ROP_ARG(PC++);            //Grab data
527   R_R(r) = data;                     //Store to R0 - R7
528}
529
530//MOV data addr, @R0/@R1                    /* 1: 1000 011i */
531INLINE void mov_mem_ir(int r)
532{
533   UINT8 addr = ROP_ARG(PC++);            //Grab data address
534   IRAM_W(addr,IRAM_IR(R_R(r)));         //Store contents pointed to by R0 or R1 to data address
535}
536
537//MOV data addr,R0 to R7                    /* 1: 1000 1rrr */
538INLINE void mov_mem_r(int r)
539{
540   UINT8 addr = ROP_ARG(PC++);            //Grab data address
541   IRAM_W(addr,R_R(r));               //Store contents of R0 - R7 to data address
542}
543
544//MOV DPTR, #data16                         /* 1: 1001 0000 */
545INLINE void mov_dptr_byte(void)
546{
547   UINT8 data_hi, data_lo;
548   data_hi = ROP_ARG(PC++);            //Grab hi byte
549   data_lo = ROP_ARG(PC++);            //Grab lo byte
550   DPTR_W((UINT16)((data_hi<<8)|data_lo));   //Store to DPTR
551}
552
553//MOV bit addr, C                           /* 1: 1001 0010 */
554INLINE void mov_bitaddr_c(void)
555{
556   UINT8 addr = ROP_ARG(PC++);            //Grab bit address
557   BIT_W(addr,GET_CY);                  //Store Carry Flag to Bit Address
558}
559
560//MOV @R0/@R1, data addr                    /* 1: 1010 011i */
561INLINE void mov_ir_mem(int r)
562{
563   UINT8 addr = ROP_ARG(PC++);            //Grab data address
564   IRAM_IW(R_R(r),IRAM_R(addr));         //Store data from data address to address pointed to by R0 or R1
565}
566
567//MOV R0 to R7, data addr                   /* 1: 1010 1rrr */
568INLINE void mov_r_mem(int r)
569{
570   UINT8 addr = ROP_ARG(PC++);            //Grab data address
571   R_R(r) = IRAM_R(addr);               //Store to R0 - R7
572}
573
574//MOV data addr, A                          /* 1: 1111 0101 */
575INLINE void mov_mem_a(void)
576{
577   UINT8 addr = ROP_ARG(PC++);            //Grab data address
578   IRAM_W(addr,R_ACC);                  //Store A to data address
579}
580
581//MOV @R0/@R1, A                            /* 1: 1111 011i */
582INLINE void mov_ir_a(int r)
583{
584   IRAM_IW(R_R(r),R_ACC);               //Store A to location pointed to by R0 or R1
585}
586
587//MOV R0 to R7, A                           /* 1: 1111 1rrr */
588INLINE void mov_r_a(int r)
589{
590   R_R(r) = R_ACC;                     //Store A to R0-R7
591}
592
593//MOVC A, @A + PC                           /* 1: 1000 0011 */
594INLINE void movc_a_iapc(void)
595{
596   UINT8 data;
597   data = CODEMEM_R(R_ACC+PC);            //Move a byte from CODE(Program) Memory and store to ACC
598   SFR_W(ACC,data);
599}
600
601//MOV C, bit addr                           /* 1: 1010 0010 */
602INLINE void mov_c_bitaddr(void)
603{
604   UINT8 addr = ROP_ARG(PC++);            //Grab bit address
605   SET_CY( (BIT_R(addr)) );            //Store Bit from Bit Address to Carry Flag
606}
607
608//MOVC A, @A + DPTR                         /* 1: 1001 0011 */
609INLINE void movc_a_iadptr(void)
610{
611   UINT8 data;
612   data = CODEMEM_R(R_ACC+R_DPTR);         //Move a byte from CODE(Program) Memory and store to ACC
613   SFR_W(ACC,data);
614}
615
616//MOVX A,@DPTR                              /* 1: 1110 0000 */
617//(Move External Ram 16 bit address to A)
618INLINE void movx_a_idptr(void)
619{
620//  UINT8 byte = DATAMEM_R(R_DPTR);         //Grab 1 byte from External DATA memory pointed to by dptr
621   UINT32 addr = ERAM_ADDR(R_DPTR,0xFFFF);
622   UINT8 byte = DATAMEM_R(addr);         //Grab 1 byte from External DATA memory pointed to by dptr
623   SFR_W(ACC,byte);                  //Store to ACC
624}
625
626//MOVX A, @R0/@R1                           /* 1: 1110 001i */
627//(Move External Ram 8 bit address to A)
628INLINE void movx_a_ir(int r)
629{
630   UINT32 addr = ERAM_ADDR(R_R(r),0xFF);   //Grab address by reading location pointed to by R0 or R1
631   UINT8 byte = DATAMEM_R(addr);         //Grab 1 byte from External DATA memory pointed to by address
632   SFR_W(ACC,byte);                  //Store to ACC
633}
634
635//MOVX @DPTR,A                              /* 1: 1111 0000 */
636//(Move A to External Ram 16 bit address)
637INLINE void movx_idptr_a(void)
638{
639//  DATAMEM_W(R_DPTR, R_ACC);               //Store ACC to External DATA memory address pointed to by DPTR
640   UINT32 addr = ERAM_ADDR(R_DPTR,0xFFFF);
641   DATAMEM_W(addr, R_ACC);            //Store ACC to External DATA memory address pointed to by DPTR
642}
643
644//MOVX @R0/@R1,A                            /* 1: 1111 001i */
645//(Move A to External Ram 8 bit address)
646INLINE void movx_ir_a(int r)
647{
648   UINT32 addr = ERAM_ADDR(R_R(r),0xFF);   //Grab address by reading location pointed to by R0 or R1
649   DATAMEM_W(addr, R_ACC);               //Store ACC to External DATA memory address
650}
651
652//MUL AB                                    /* 1: 1010 0100 */
653INLINE void mul_ab(void)
654{
655   UINT16 result = R_ACC * R_B;
656   //A gets lo bits, B gets hi bits of result
657   SFR_W(B,(UINT8)((result & 0xFF00) >> 8));
658   SFR_W(ACC,(UINT8)(result & 0x00FF));
659   //Set flags
660   SET_OV( ((result & 0x100) >> 8) );      //Set/Clear Overflow Flag if result > 255
661   SET_CY(0);                        //Carry Flag always cleared
662}
663
664//NOP                                       /* 1: 0000 0000 */
665INLINE void nop(void)
666{
667}
668
669//ORL data addr, A                          /* 1: 0100 0010 */
670INLINE void orl_mem_a(void)
671{
672   UINT8 addr = ROP_ARG(PC++);            //Grab data address
673   UINT8 data = IRAM_R(addr);            //Grab data from data address
674   IRAM_W(addr,data | R_ACC);            //Set data address value to it's value Logical OR with ACC
675}
676
677//ORL data addr, #data                      /* 1: 0100 0011 */
678INLINE void orl_mem_byte(void)
679{
680   UINT8 addr = ROP_ARG(PC++);            //Grab data address
681   UINT8 data = ROP_ARG(PC++);            //Grab data
682   UINT8 srcdata = IRAM_R(addr);         //Grab data from data address
683   IRAM_W(addr,srcdata | data);         //Set data address value to it's value Logical OR with Data
684}
685
686//ORL A, #data                              /* 1: 0100 0100 */
687INLINE void orl_a_byte(void)
688{
689   UINT8 data = ROP_ARG(PC++);            //Grab data
690   SFR_W(ACC,R_ACC | data);            //Set ACC to value of ACC Logical OR with Data
691}
692
693//ORL A, data addr                          /* 1: 0100 0101 */
694INLINE void orl_a_mem(void)
695{
696   UINT8 addr = ROP_ARG(PC++);            //Grab data address
697   UINT8 data = IRAM_R(addr);            //Grab data from data address
698   SFR_W(ACC,R_ACC | data);            //Set ACC to value of ACC Logical OR with Data
699}
700
701//ORL A, @RO/@R1                            /* 1: 0100 011i */
702INLINE void orl_a_ir(int r)
703{
704   UINT8 data = IRAM_IR(R_R(r));         //Grab data from address R0 or R1 points to
705   SFR_W(ACC,R_ACC | data);            //Set ACC to value of ACC Logical OR with Data
706}
707
708//ORL A, RO to R7                           /* 1: 0100 1rrr */
709INLINE void orl_a_r(int r)
710{
711   UINT8 data = R_R(r);               //Grab data from R0 - R7
712   SFR_W(ACC,R_ACC | data);            //Set ACC to value of ACC Logical OR with Data
713}
714
715//ORL C, bit addr                           /* 1: 0111 0010 */
716INLINE void orl_c_bitaddr(void)
717{
718   int cy = GET_CY;
719   UINT8 addr = ROP_ARG(PC++);            //Grab bit address
720   UINT8 bit = BIT_R(addr);            //Grab bit data from bit address
721   SET_CY( (cy | bit) );               //Set Carry flag to Carry Flag Value Logical OR with Bit
722}
723
724//ORL C, /bit addr                          /* 1: 1010 0000 */
725INLINE void orl_c_nbitaddr(void)
726{
727   int cy = GET_CY;
728   UINT8 addr = ROP_ARG(PC++);            //Grab bit address
729   UINT8 bit = BIT_R(addr);            //Grab bit data from bit address
730   bit = ((~bit)&1);                  //Complement bit
731   SET_CY( (cy | bit) );               //Set Carry flag to Carry Flag Value Logical OR with Complemented Bit
732}
733
734//POP data addr                             /* 1: 1101 0000 */
735INLINE void pop(void)
736{
737   UINT8 addr = ROP_ARG(PC++);            //Grab data address
738   IRAM_W(addr, IRAM_IR(R_SP));         //Store to contents of data addr, data pointed to by Stack - IRAM_IR needed to access upper 128 bytes of stack
739   //IRAM_IW(addr, IRAM_IR(R_SP));         //Store to contents of data addr, data pointed to by Stack - doesn't work, sfr's are not restored this way and it's not an indirect access anyway
740   SFR_W(SP,R_SP-1);                  //Decrement SP
741}
742
743//PUSH data addr                            /* 1: 1100 0000 */
744INLINE void push(void)
745{
746   UINT8 addr = ROP_ARG(PC++);            //Grab data address
747   UINT8 tmpSP = R_SP;                  //Grab and Increment Stack Pointer
748   tmpSP++;                        // ""
749   SFR_W(SP,tmpSP);                  // ""
750    if (tmpSP == R_SP)                  //Ensure it was able to write to new stack location
751      IRAM_IW(tmpSP, IRAM_R(addr));      //Store to stack contents of data address - IRAM_IW needed to store to upper 128 bytes of stack, however, can't use IRAM_IR because that won't store the sfrs and it's not an indirect access anyway
752}
753
754//RET                                       /* 1: 0010 0010 */
755INLINE void ret(void)
756{
757   POP_PC
758}
759
760//RETI                                      /* 1: 0011 0010 */
761INLINE void reti(void)
762{
763   POP_PC
764   CLEAR_CURRENT_IRQ
765}
766
767//RL A                                      /* 1: 0010 0011 */
768INLINE void rl_a(void)
769{
770   //Left Shift A, Bit 7 carries to Bit 0
771   int carry = ((R_ACC & 0x80) >> 7);
772   int data = (R_ACC<<1) & 0xfe;
773   SFR_W(ACC, data | carry);
774}
775
776//RLC A                                     /* 1: 0011 0011 */
777INLINE void rlc_a(void)
778{
779   //Left Shift A, Bit 7 goes to Carry Flag, Original Carry Flag goes to Bit 0 of ACC
780   int carry = ((R_ACC & 0x80) >> 7);
781   int data = ((R_ACC<<1) & 0xfe) | GET_CY;
782   SFR_W(ACC, data);
783   SET_CY(carry);
784}
785
786//RR A                                      /* 1: 0000 0011 */
787INLINE void rr_a(void)
788{
789   //Right Shift A, Bit 0 carries to Bit 7
790   int carry = ((R_ACC & 1) << 7);
791   int data = (R_ACC>>1) & 0x7f;
792   SFR_W(ACC, data | carry);
793}
794
795//RRC A                                     /* 1: 0001 0011 */
796INLINE void rrc_a(void)
797{
798   //Right Shift A, Bit 0 goes to Carry Flag, Bit 7 of ACC gets set to original Carry Flag
799   int carry = (R_ACC & 1);
800   int data = ((R_ACC>>1) & 0x7f) | (GET_CY<<7);
801   SFR_W(ACC, data);
802   SET_CY(carry);
803}
804
805//SETB C                                    /* 1: 1101 0011 */
806INLINE void setb_c(void)
807{
808   SET_CY(1);      //Set Carry Flag
809}
810
811//SETB bit addr                             /* 1: 1101 0010 */
812INLINE void setb_bitaddr(void)
813{
814   UINT8 addr = ROP_ARG(PC++);            //Grab bit address
815   BIT_W(addr,1);                     //Set Bit at Bit Address
816}
817
818//SJMP code addr                            /* 1: 1000 0000 */
819INLINE void sjmp(void)
820{
821   INT8 rel_addr = ROP_ARG(PC++);         //Grab relative code address
822   PC = PC + rel_addr;                  //Update PC
823}
824
825//SUBB A, #data                             /* 1: 1001 0100 */
826INLINE void subb_a_byte(void)
827{
828   UINT8 data = ROP_ARG(PC++);            //Grab data
829   UINT8 result = R_ACC - data - GET_CY;   //Subtract data & carry flag from accumulator
830   DO_SUB_FLAGS(R_ACC,data,GET_CY);      //Set Flags
831   SFR_W(ACC,result);                  //Store 8 bit result of addtion in ACC
832
833}
834
835//SUBB A, data addr                         /* 1: 1001 0101 */
836INLINE void subb_a_mem(void)
837{
838   UINT8 addr = ROP_ARG(PC++);            //Grab data address
839   UINT8 data = IRAM_R(addr);            //Grab data from data address
840   UINT8 result = R_ACC - data - GET_CY;   //Subtract data & carry flag from accumulator
841   DO_SUB_FLAGS(R_ACC,data,GET_CY);      //Set Flags
842   SFR_W(ACC,result);                  //Store 8 bit result of addtion in ACC
843}
844
845//SUBB A, @R0/@R1                           /* 1: 1001 011i */
846INLINE void subb_a_ir(int r)
847{
848   UINT8 data = IRAM_IR(R_R(r));         //Grab data from memory pointed to by R0 or R1
849   UINT8 result = R_ACC - data - GET_CY;   //Subtract data & carry flag from accumulator
850   DO_SUB_FLAGS(R_ACC,data,GET_CY);      //Set Flags
851   SFR_W(ACC,result);                  //Store 8 bit result of addtion in ACC
852}
853
854//SUBB A, R0 to R7                          /* 1: 1001 1rrr */
855INLINE void subb_a_r(int r)
856{
857   UINT8 data = R_R(r);               //Grab data from R0 - R7
858   UINT8 result = R_ACC - data - GET_CY;   //Subtract data & carry flag from accumulator
859   DO_SUB_FLAGS(R_ACC,data,GET_CY);      //Set Flags
860   SFR_W(ACC,result);                  //Store 8 bit result of addtion in ACC
861}
862
863//SWAP A                                    /* 1: 1100 0100 */
864INLINE void swap_a(void)
865{
866   UINT8 a_nib_lo, a_nib_hi;
867   a_nib_hi = (R_ACC & 0x0f) << 4;         //Grab lo byte of ACC and move to hi
868   a_nib_lo = (R_ACC & 0xf0) >> 4;         //Grab hi byte of ACC and move to lo
869   SFR_W(ACC, a_nib_hi | a_nib_lo);
870}
871
872//XCH A, data addr                          /* 1: 1100 0101 */
873INLINE void xch_a_mem(void)
874{
875   UINT8 addr = ROP_ARG(PC++);            //Grab data address
876   UINT8 data = IRAM_R(addr);            //Grab data
877   UINT8 oldACC = R_ACC;               //Hold value of ACC
878   SFR_W(ACC,data);                  //Sets ACC to data
879   IRAM_W(addr,oldACC);               //Sets data address to old value of ACC
880}
881
882//XCH A, @RO/@R1                            /* 1: 1100 011i */
883INLINE void xch_a_ir(int r)
884{
885   UINT8 data = IRAM_IR(R_R(r));         //Grab data pointed to by R0 or R1
886   UINT8 oldACC = R_ACC;               //Hold value of ACC
887   SFR_W(ACC,data);                  //Sets ACC to data
888   IRAM_W(R_R(r),oldACC);               //Sets data address to old value of ACC
889}
890
891//XCH A, RO to R7                           /* 1: 1100 1rrr */
892INLINE void xch_a_r(int r)
893{
894   UINT8 data = R_R(r);               //Grab data from R0-R7
895   UINT8 oldACC = R_ACC;               //Hold value of ACC
896   SFR_W(ACC,data);                  //Sets ACC to data
897   R_R(r) = oldACC;                  //Sets data address to old value of ACC
898}
899
900//XCHD A, @R0/@R1                           /* 1: 1101 011i */
901INLINE void xchd_a_ir(int r)
902{
903   UINT8 acc, ir_data;
904   ir_data = IRAM_IR(R_R(r));            //Grab data pointed to by R0 or R1
905   acc = R_ACC;                     //Grab ACC value
906   SFR_W(ACC, (acc & 0xf0) | (ir_data & 0x0f) );      //Set ACC to lower nibble of data pointed to by R0 or R1
907   IRAM_W(R_R(r), (ir_data & 0xf0) | (acc & 0x0f) );   //Set data pointed to by R0 or R1 to lower nibble of ACC
908}
909
910//XRL data addr, A                          /* 1: 0110 0010 */
911INLINE void xrl_mem_a(void)
912{
913   UINT8 addr = ROP_ARG(PC++);            //Grab data address
914   UINT8 data = IRAM_R(addr);            //Grab data from data address
915   IRAM_W(addr,data ^ R_ACC);            //Set data address value to it's value Logical XOR with ACC
916}
917
918//XRL data addr, #data                      /* 1: 0110 0011 */
919INLINE void xrl_mem_byte(void)
920{
921   UINT8 addr = ROP_ARG(PC++);            //Grab data address
922   UINT8 data = ROP_ARG(PC++);            //Grab data
923   UINT8 srcdata = IRAM_R(addr);         //Grab data from data address
924   IRAM_W(addr,srcdata ^ data);         //Set data address value to it's value Logical XOR with Data
925}
926
927//XRL A, #data                              /* 1: 0110 0100 */
928INLINE void xrl_a_byte(void)
929{
930   UINT8 data = ROP_ARG(PC++);            //Grab data
931   SFR_W(ACC,R_ACC ^ data);            //Set ACC to value of ACC Logical XOR with Data
932}
933
934//XRL A, data addr                          /* 1: 0110 0101 */
935INLINE void xrl_a_mem(void)
936{
937   UINT8 addr = ROP_ARG(PC++);            //Grab data address
938   UINT8 data = IRAM_R(addr);            //Grab data from data address
939   SFR_W(ACC,R_ACC ^ data);            //Set ACC to value of ACC Logical XOR with Data
940}
941
942//XRL A, @R0/@R1                            /* 1: 0110 011i */
943INLINE void xrl_a_ir(int r)
944{
945   UINT8 data = IRAM_IR(R_R(r));         //Grab data from address R0 or R1 points to
946   SFR_W(ACC,R_ACC ^ data);            //Set ACC to value of ACC Logical XOR with Data
947}
948
949//XRL A, R0 to R7                           /* 1: 0110 1rrr */
950INLINE void xrl_a_r(int r)
951{
952   UINT8 data = R_R(r);               //Grab data from R0 - R7
953   SFR_W(ACC,R_ACC ^ data);            //Set ACC to value of ACC Logical XOR with Data
954}
955
956//illegal opcodes
957INLINE void illegal(void)
958{
959   LOG(("ds5002fp #%d: illegal opcode at 0x%03x: %02x\n", cpu_getactivecpu(), PC, ROP(PC)));
960}
src/emu/cpu/ds5002fp/ds5002fp.h
r0r2628
1/*****************************************************************************
2 *   DS5002FP emulator
3 *
4 *   The emulator is just a modified version of the MCS-51 Family Emulator by Steve Ellenoff
5 *
6 *****************************************************************************/
7
8/*****************************************************************************
9 *
10 *   i8051.h
11 *   Portable MCS-51 Family Emulator
12 *
13 *   Chips in the family:
14 *   8051 Product Line (8031,8051,8751)
15 *   8052 Product Line (8032,8052,8752)
16 *   8054 Product Line (8054)
17 *   8058 Product Line (8058)
18 *
19 *   Copyright Steve Ellenoff, all rights reserved.
20 *
21 *   - This source code is released as freeware for non-commercial purposes.
22 *   - You are free to use and redistribute this code in modified or
23 *     unmodified form, provided you list me in the credits.
24 *   - If you modify this source code, you must add a notice to each modified
25 *     source file that it has been changed.  If you're a nice person, you
26 *     will clearly mark each change too.  :)
27 *   - If you wish to use this for commercial purposes, please contact me at
28 *     sellenoff@hotmail.com
29 *   - The author of this copywritten work reserves the right to change the
30 *     terms of its usage and license at any time, including retroactively
31 *   - This entire notice must remain in the source code.
32 *
33 *  This work is based on:
34 *  #1) 'Intel(tm) MC51 Microcontroller Family Users Manual' and
35 *  #2) 8051 simulator by Travis Marlatte
36 *  #3) Portable UPI-41/8041/8741/8042/8742 emulator V0.1 by Juergen Buchmueller (MAME CORE)
37 *
38 *****************************************************************************/
39
40#ifndef _DS5002FP_H
41#define _DS5002FP_H
42
43#include "cpuintrf.h"
44
45enum {
46   DS5002FP_PC=1, DS5002FP_SP, DS5002FP_PSW, DS5002FP_ACC, DS5002FP_B, DS5002FP_DPH, DS5002FP_DPL, DS5002FP_IE,
47   DS5002FP_PCON, DS5002FP_MCON, DS5002FP_RPCTL, DS5002FP_CRC,
48   DS5002FP_R0, DS5002FP_R1, DS5002FP_R2, DS5002FP_R3, DS5002FP_R4, DS5002FP_R5, DS5002FP_R6, DS5002FP_R7, DS5002FP_RB
49};
50
51#define DS5002FP_INT0_LINE      0   /* External Interrupt 0 */
52#define DS5002FP_INT1_LINE      1   /* External Interrupt 1 */
53#define DS5002FP_T0_LINE      2   /* Timer 0 External Input */
54#define DS5002FP_T1_LINE      3   /* Timer 1 External Input */
55#define DS5002FP_RX_LINE      4   /* Serial Port Receive Line */
56#define DS5002FP_PFI_LINE      5   /* Power Fail Interrupt */
57
58/* definition of the special function registers. Note that the values are */
59/* the same as the internal memory address in the DS5002FP */
60#define      P0      0x80
61#define      SP      0x81
62#define      DPL      0x82
63#define      DPH      0x83
64#define      PCON   0x87
65#define      TCON   0x88
66#define      TMOD   0x89
67#define      TL0      0x8a
68#define      TL1      0x8b
69#define      TH0      0x8c
70#define      TH1      0x8d
71#define      P1      0x90
72#define      SCON   0x98
73#define      SBUF   0x99
74#define      P2      0xa0
75#define      IE      0xa8
76#define      P3      0xb0
77#define      IP      0xb8
78#define      CRCR   0xc1
79#define      CRCL   0xc2
80#define      CRCH   0xc3
81#define      MCON   0xc6
82#define      TA      0xc7
83#define      RNR      0xcf
84#define      PSW      0xd0
85#define      RPCTL   0xd8
86#define      RPS      0xda
87#define      ACC      0xe0
88#define      B      0xf0
89
90/* commonly used bit address for the DS5002FP */
91#define      C      0xd7
92#define      P      0xd0
93#define      AC      0xd6
94#define      OV      0xd2
95#define      PFW      0x8d
96#define      TF1      0x8f
97#define      IE0      0x89
98#define      IE1      0x8b
99#define      TI      0x99
100#define      RI      0x98
101
102#define TI_FLAG 1
103#define RI_FLAG 2
104
105/* configuration of the DS5002FP */
106typedef struct _ds5002fp_config ds5002fp_config;
107struct _ds5002fp_config
108{
109   UINT8   mcon;               /* bootstrap loader MCON register */
110   UINT8   rpctl;               /* bootstrap loader RPCTL register */
111   UINT8   crc;               /* bootstrap loader CRC register */
112};
113
114extern void ds5002fp_init (int index, int clock, const void *config, int (*irqcallback)(int));               /* Initialize save states */
115extern void ds5002fp_reset (void);         /* Reset registers to the initial values */
116extern void ds5002fp_exit   (void);             /* Shut down CPU core */
117extern int  ds5002fp_execute(int cycles);         /* Execute cycles - returns number of cycles actually run */
118extern void ds5002fp_get_context (void *dst);   /* Get registers, return context size */
119extern void ds5002fp_set_context (void *src);       /* Set registers */
120extern unsigned ds5002fp_get_intram (int offset);
121extern unsigned ds5002fp_get_reg (int regnum);
122extern void ds5002fp_set_reg (int regnum, unsigned val);
123extern void ds5002fp_set_irq_line(int irqline, int state);
124extern void ds5002fp_set_irq_callback(int (*callback)(int irqline));
125extern void ds5002fp_state_save(void *file);
126extern void ds5002fp_state_load(void *file);
127
128WRITE8_HANDLER( ds5002fp_internal_w );
129READ8_HANDLER( ds5002fp_internal_r );
130
131extern void ds5002fp_set_serial_tx_callback(void (*callback)(int data));
132extern void ds5002fp_set_serial_rx_callback(int (*callback)(void));
133extern void ds5002fp_set_ebram_iaddr_callback(READ32_HANDLER((*callback)));
134
135#ifdef ENABLE_DEBUGGER
136extern offs_t ds5002fp_dasm(char *buffer, offs_t pc, const UINT8 *oprom, const UINT8 *opram);
137#endif /* ENABLE_DEBUGGER */
138
139void ds5002fp_get_info(UINT32 state, cpuinfo *info);
140
141#endif /* _DS5002FP_H */
142
143
src/emu/cpu/ds5002fp/ds5002fpdasm.c
r0r2628
1/*****************************************************************************
2 *   DS5002FP emulator
3 *
4 *   The emulator is just a modified version of the MCS-51 Family Emulator by Steve Ellenoff
5 *
6 *****************************************************************************/
7
8/*****************************************************************************
9 *
10 *   i8051dasm.c
11 *   Portable MCS-51 Family Emulator
12 *
13 *   Chips in the family:
14 *   8051 Product Line (8031,8051,8751)
15 *   8052 Product Line (8032,8052,8752)
16 *   8054 Product Line (8054)
17 *   8058 Product Line (8058)
18 *
19 *   Copyright Steve Ellenoff, all rights reserved.
20 *
21 *   - This source code is released as freeware for non-commercial purposes.
22 *   - You are free to use and redistribute this code in modified or
23 *     unmodified form, provided you list me in the credits.
24 *   - If you modify this source code, you must add a notice to each modified
25 *     source file that it has been changed.  If you're a nice person, you
26 *     will clearly mark each change too.  :)
27 *   - If you wish to use this for commercial purposes, please contact me at
28 *     sellenoff@hotmail.com
29 *   - The author of this copywritten work reserves the right to change the
30 *     terms of its usage and license at any time, including retroactively
31 *   - This entire notice must remain in the source code.
32 *
33 *  This work is based on:
34 *  #1) 'Intel(tm) MC51 Microcontroller Family Users Manual' and
35 *  #2) 8051 simulator by Travis Marlatte
36 *  #3) Portable UPI-41/8041/8741/8042/8742 emulator V0.1 by Juergen Buchmueller (MAME CORE)
37 *
38 *****************************************************************************
39 * Symbol Memory Name Tables borrowed from:
40 * D52 8052 Disassembler - Copyright Jeffery L. Post
41 *****************************************************************************/
42
43#include "debugger.h"
44#include "ds5002fp.h"
45
46#define SHOW_MEMORY_NAMES   1
47
48#ifdef SHOW_MEMORY_NAMES
49
50/*Display the memory address names for data & bit address access*/
51
52//SFR Names
53static const char sfr[128][7] = {
54    "p0",     "sp",     "dpl",    "dph",    /* 80 - 83 */
55    "84h",    "85h",    "86h",    "pcon",   /* 84 - 87 */
56    "tcon",   "tmod",   "tl0",    "tl1",    /* 88 - 8b */
57    "th0",    "th1",    "pwcm",   "pwmp",   /* 8a - 8f */
58    "p1",     "91h",    "92h",    "93h",    /* 90 - 93 */
59    "94h",    "95h",    "96h",    "97h",    /* 94 - 97 */
60    "scon",   "sbuf",   "9ah",    "9bh",    /* 98 - 9b */
61    "9ch",    "9dh",    "9eh",    "9fh",    /* 9c - 9f */
62    "p2",     "0a1h",   "0a2h",   "0a3h",   /* a0 - a3 */
63    "0a4h",   "0a5h",   "0a6h",   "0a7h",   /* a4 - a7 */
64    "ie",     "0a9h",   "0aah",   "0abh",   /* a8 - ab */
65    "0ach",   "0adh",   "0aeh",   "0afh",   /* ac - af */
66    "p3",     "0b1h",   "0b2h",   "0b3h",   /* b0 - b3 */
67    "0b4h",   "0b5h",   "0b6h",   "0b7h",   /* b4 - b7 */
68    "ip",     "0b9h",   "0bah",   "0bbh",   /* b8 - bb */
69    "0bch",   "0bdh",   "0beh",   "0bfh",   /* bc - bf */
70    "0c0h",   "crc",    "crcl",   "crch",   /* c0 - c3 */
71    "0c4h",   "0c5h",   "mcon",   "ta",     /* c4 - c7 */
72    "0c8h",   "0c9h",   "0cah",   "0cbh",   /* c8 - cb */
73    "0cch",   "0cdh",   "0ceh",   "rnr",    /* cc - cf */
74    "psw",    "0d1h",   "0d2h",   "0d3h",   /* d0 - d3 */
75    "0d4h",   "0d5h",   "0d6h",   "0d7h",   /* d4 - d7 */
76    "rpctl",  "0d9h",   "rps",    "0dbh",   /* d8 - db */
77    "0dch",   "0ddh",   "0deh",   "0dfh",   /* dc - df */
78    "acc",    "0e1h",   "0e2h",   "0e3h",   /* e0 - e3 */
79    "0e4h",   "0e5h",   "0e6h",   "0e7h",   /* e4 - e7 */
80    "0e8h",   "0e9h",   "0eah",   "0ebh",   /* e8 - eb */
81    "0ech",   "0edh",   "0eeh",   "0efh",   /* ec - ef */
82    "b",      "0f1h",   "0f2h",   "0f3h",   /* f0 - f3 */
83    "0f4h",   "0f5h",   "0f6h",   "0f7h",   /* f4 - f7 */
84    "0f8h",   "0f9h",   "0fah",   "0fbh",   /* f8 - fb */
85    "0fch",   "0fdh",   "0feh",   "0ffh"    /* fc - ff */
86};
87
88//SFR Addressable bits
89static const char sfrbits[128][8] = {
90    "p0.0",   "p0.1",   "p0.2",   "p0.3",   /* 80 - 83 */
91    "p0.4",   "p0.5",   "p0.6",   "p0.7",   /* 84 - 87 */
92    "it0",    "ie0",    "it1",    "ie1",    /* 88 - 8b */
93    "tr0",    "tf0",    "tr1",    "tf1",    /* 8c - 8f */
94    "p1.0",   "p1.1",   "p1.2",   "p1.3",   /* 90 - 93 */
95    "p1.4",   "p1.5",   "p1.6",   "p1.7",   /* 94 - 97 */
96    "ri",     "ti",     "rb8",    "tb8",    /* 98 - 9b */
97    "ren",    "sm2",    "sm1",    "sm0",    /* 9c - 9f */
98    "p2.0",   "p2.1",   "p2.2",   "p2.3",   /* a0 - a3 */
99    "p2.4",   "p2.5",   "p2.6",   "p2.7",   /* a4 - a7 */
100    "ex0",    "et0",    "ex1",    "et1",    /* a8 - ab */
101    "es",     "ie.5",   "ie.6",   "ea",     /* ac - af */
102    "p3.0",   "p3.1",   "p3.2",   "p3.3",   /* b0 - b3 */
103    "p3.4",   "p3.5",   "p3.6",   "p3.7",   /* b4 - b7 */
104    "px0",    "pt0",    "px1",    "pt1",    /* b8 - bb */
105    "ps",     "ip.5",   "ip.6",   "rwt",    /* bc - bf */
106    "0c0h.0", "0c0h.1", "0c0h.2", "0c0h.3", /* c0 - c3 */
107    "0c0h.4", "0c0h.5", "0c0h.6", "0c0h.7", /* c4 - c7 */
108    "0c8h.0", "0c8h.1", "0c8h.2", "0c8h.3", /* c8 - cb */
109    "0c8h.4", "0c8h.5", "0c8h.6", "0c8h.7", /* cc - cf */
110    "p",      "psw.1",  "ov",     "rs0",    /* d0 - d3 */
111    "rs1",    "f0",     "ac",     "c",      /* d4 - d7 */
112    "rg0",    "rpc",    "dma",    "ibi",    /* d8 - db */
113    "ae",     "exbs",   "0d8h.6", "rnr",    /* dc - df */
114    "acc.0",  "acc.1",  "acc.2",  "acc.3",  /* e0 - e3 */
115    "acc.4",  "acc.5",  "acc.6",  "acc.7",  /* e4 - e7 */
116    "0e8h.0", "0e8h.1", "0e8h.2", "0e8h.3", /* e8 - eb */
117    "0e8h.4", "0e8h.5", "0e8h.6", "0e8h.7", /* ec - ef */
118    "b.0",    "b.1",    "b.2",    "b.3",    /* f0 - f3 */
119    "b.4",    "b.5",    "b.6",    "b.7",    /* f4 - f7 */
120    "0f8h.0", "0f8h.1", "0f8h.2", "0f8h.3", /* f8 - fb */
121    "0f8h.4", "0f8h.5", "0f8h.6", "0f8h.7"  /* fc - ff */
122} ;
123
124//Names for bit addressable memory
125static const char membits[128][6] = {
126    "20h.0",  "20h.1",  "20h.2",  "20h.3",
127    "20h.4",  "20h.5",  "20h.6",  "20h.7",
128    "21h.0",  "21h.1",  "21h.2",  "21h.3",
129    "21h.4",  "21h.5",  "21h.6",  "21h.7",
130    "22h.0",  "22h.1",  "22h.2",  "22h.3",
131    "22h.4",  "22h.5",  "22h.6",  "22h.7",
132    "23h.0",  "23h.1",  "23h.2",  "23h.3",
133    "23h.4",  "23h.5",  "23h.6",  "23h.7",
134    "24h.0",  "24h.1",  "24h.2",  "24h.3",
135    "24h.4",  "24h.5",  "24h.6",  "24h.7",
136    "25h.0",  "25h.1",  "25h.2",  "25h.3",
137    "25h.4",  "25h.5",  "25h.6",  "25h.7",
138    "26h.0",  "26h.1",  "26h.2",  "26h.3",
139    "26h.4",  "26h.5",  "26h.6",  "26h.7",
140    "27h.0",  "27h.1",  "27h.2",  "27h.3",
141    "27h.4",  "27h.5",  "27h.6",  "27h.7",
142    "28h.0",  "28h.1",  "28h.2",  "28h.3",
143    "28h.4",  "28h.5",  "28h.6",  "28h.7",
144    "29h.0",  "29h.1",  "29h.2",  "29h.3",
145    "29h.4",  "29h.5",  "29h.6",  "29h.7",
146    "2ah.0",  "2ah.1",  "2ah.2",  "2ah.3",
147    "2ah.4",  "2ah.5",  "2ah.6",  "2ah.7",
148    "2bh.0",  "2bh.1",  "2bh.2",  "2bh.3",
149    "2bh.4",  "2bh.5",  "2bh.6",  "2bh.7",
150    "2ch.0",  "2ch.1",  "2ch.2",  "2ch.3",
151    "2ch.4",  "2ch.5",  "2ch.6",  "2ch.7",
152    "2dh.0",  "2dh.1",  "2dh.2",  "2dh.3",
153    "2dh.4",  "2dh.5",  "2dh.6",  "2dh.7",
154    "2eh.0",  "2eh.1",  "2eh.2",  "2eh.3",
155    "2eh.4",  "2eh.5",  "2eh.6",  "2eh.7",
156    "2fh.0",  "2fh.1",  "2fh.2",  "2fh.3",
157    "2fh.4",  "2fh.5",  "2fh.6",  "2fh.7"
158};
159
160//Regiser Bank memory names
161static const char regbank[][6] = {
162    "rb0r0",  "rb0r1",  "rb0r2",  "rb0r3",
163    "rb0r4",  "rb0r5",  "rb0r6",  "rb0r7",
164    "rb1r0",  "rb1r1",  "rb1r2",  "rb1r3",
165    "rb1r4",  "rb1r5",  "rb1r6",  "rb1r7",
166    "rb2r0",  "rb2r1",  "rb2r2",  "rb2r3",
167    "rb2r4",  "rb2r5",  "rb2r6",  "rb2r7",
168    "rb3r0",  "rb3r1",  "rb3r2",  "rb3r3",
169    "rb3r4",  "rb3r5",  "rb3r6",  "rb3r7"
170};
171
172static const char *get_data_address( UINT8 arg )
173{
174   char *buffer = cpuintrf_temp_str();
175
176   if(arg < 0x80)
177   {
178      //Ram locations 0-0x1F are considered register access in 3 banks
179      if(arg < 0x1f)
180         sprintf(buffer,"%s",regbank[arg]);
181      else
182         sprintf(buffer,"$%02X",arg);
183   }
184   else
185      sprintf(buffer,"%s",sfr[arg-0x80]);
186   return buffer;
187}
188
189static const char *get_bit_address( UINT8 arg )
190{
191   char *buffer = cpuintrf_temp_str();
192
193   if(arg < 0x80)
194   {
195      //Bit address 0-7F can be referred to as 20.0, 20.1, to 20.7 for address 0, and 2f.0,2f.1 to 2f.7 for address 7f
196      if(arg < 0x7f)
197         sprintf(buffer,"%s",membits[arg]);
198      else
199         sprintf(buffer,"$%02X",arg);
200   }
201   else
202      sprintf(buffer,"%s",sfrbits[arg-0x80]);
203   return buffer;
204}
205
206#else
207
208/*Just display the actual memory address for data & bit address access*/
209
210static const char *get_data_address( UINT8 arg )
211{
212   char *buffer = cpuintrf_temp_str();
213   sprintf(buffer,"$%02X",arg);
214   return buffer;
215}
216
217static const char *get_bit_address( UINT8 arg )
218{
219   char *buffer = cpuintrf_temp_str();
220   sprintf(buffer,"$%02X",arg);
221   return buffer;
222}
223
224#endif
225
226
227unsigned ds5002fp_dasm(char *dst, unsigned pc, const UINT8 *oprom, const UINT8 *opram)
228{
229   UINT32 flags = 0;
230   unsigned PC = pc;
231   const char *sym, *sym2;
232    UINT8 op, data;
233   UINT16 addr;
234   INT8 rel;
235
236   op = oprom[PC++ - pc];
237   switch( op )
238   {
239      //NOP
240      case 0x00:            /* 1: 0000 0000 */
241         sprintf(dst, "nop");
242         break;
243
244      //AJMP code addr        /* 1: aaa0 0001 */
245      case 0x01:
246      case 0x21:
247      case 0x41:
248      case 0x61:
249      case 0x81:
250      case 0xa1:
251      case 0xc1:
252      case 0xe1:
253         addr = opram[PC++ - pc];
254         addr|= (PC++ & 0xf800) | ((op & 0xe0) << 3);
255         sprintf(dst, "ajmp  $%04X", addr);
256         break;
257
258      //LJMP code addr
259      case 0x02:            /* 1: 0000 0010 */
260         addr = (opram[PC++ - pc]<<8) & 0xff00;
261         addr|= opram[PC++ - pc];
262         sprintf(dst, "ljmp  $%04X", addr);
263         break;
264
265      //RR A
266      case 0x03:            /* 1: 0000 0011 */
267         sprintf(dst, "rr    a");
268         break;
269
270      //INC A
271      case 0x04:            /* 1: 0000 0100 */
272         sprintf(dst, "inc   a");
273         break;
274
275      //INC data addr
276      case 0x05:            /* 1: 0000 0101 */
277         sym = get_data_address(opram[PC++ - pc]);
278         sprintf(dst, "inc   %s", sym);
279         break;
280
281      //INC @R0/@R1           /* 1: 0000 011i */
282      case 0x06:
283      case 0x07:
284         sprintf(dst, "inc   @r%d", op&1);
285         break;
286
287      //INC R0 to R7          /* 1: 0000 1rrr */
288      case 0x08:
289      case 0x09:
290      case 0x0a:
291      case 0x0b:
292      case 0x0c:
293      case 0x0d:
294      case 0x0e:
295      case 0x0f:
296         sprintf(dst, "inc   r%d", op&7);
297         break;
298
299      //JBC bit addr, code addr
300      case 0x10:            /* 1: 0001 0000 */
301         sym = get_bit_address(opram[PC++ - pc]);
302         rel  = opram[PC++ - pc];
303         sprintf(dst, "jbc   %s,$%04X", sym, PC + rel);
304         break;
305
306      //ACALL code addr       /* 1: aaa1 0001 */
307      case 0x11:
308      case 0x31:
309      case 0x51:
310      case 0x71:
311      case 0x91:
312      case 0xb1:
313      case 0xd1:
314      case 0xf1:
315         sprintf(dst, "acall $%04X", (PC & 0xf800) | ((op & 0xe0) << 3) | opram[PC - pc]);
316         PC++;
317         flags = DASMFLAG_STEP_OVER;
318         break;
319
320      //LCALL code addr
321      case 0x12:            /* 1: 0001 0010 */
322         addr = (opram[PC++ - pc]<<8) & 0xff00;
323         addr|= opram[PC++ - pc];
324         sprintf(dst, "lcall $%04X", addr);
325         flags = DASMFLAG_STEP_OVER;
326         break;
327
328      //RRC A
329      case 0x13:            /* 1: 0001 0011 */
330         sprintf(dst, "rrc   a");
331         break;
332
333      //DEC A
334      case 0x14:            /* 1: 0001 0100 */
335         sprintf(dst, "dec   a");
336         break;
337
338      //DEC data addr
339      case 0x15:            /* 1: 0001 0101 */
340         sym = get_data_address(opram[PC++ - pc]);
341         sprintf(dst, "dec   %s", sym);
342         break;
343
344      //Unable to test
345      //DEC @R0/@R1           /* 1: 0001 011i */
346      case 0x16:
347      case 0x17:
348         sprintf(dst, "dec   @r%d", op&1);
349         break;
350
351      //DEC R0 to R7          /* 1: 0001 1rrr */
352      case 0x18:
353      case 0x19:
354      case 0x1a:
355      case 0x1b:
356      case 0x1c:
357      case 0x1d:
358      case 0x1e:
359      case 0x1f:
360         sprintf(dst, "dec   r%d", op&7);
361         break;
362
363      //JB  bit addr, code addr
364      case 0x20:            /* 1: 0010 0000 */
365         sym = get_bit_address(opram[PC++ - pc]);
366         rel  = opram[PC++ - pc];
367         sprintf(dst, "jb    %s,$%04X", sym, (PC + rel));
368         break;
369
370      //RET
371      case 0x22:            /* 1: 0010 0010 */
372         sprintf(dst, "ret");
373         flags = DASMFLAG_STEP_OUT;
374         break;
375
376      //RL A
377      case 0x23:            /* 1: 0010 0011 */
378         sprintf(dst, "rl    a");
379         break;
380
381      //ADD A, #data
382      case 0x24:            /* 1: 0010 0100 */
383         sprintf(dst, "add   a,#$%02X", opram[PC++ - pc]);
384         break;
385
386      //ADD A, data addr
387      case 0x25:            /* 1: 0010 0101 */
388         sym = get_data_address(opram[PC++ - pc]);
389         sprintf(dst, "add   a,%s", sym);
390         break;
391
392      //Unable to Test
393      //ADD A, @R0/@R1        /* 1: 0010 011i */
394      case 0x26:
395      case 0x27:
396         sprintf(dst, "add   a,@r%d", op&1);
397         break;
398
399      //ADD A, R0 to R7       /* 1: 0010 1rrr */
400      case 0x28:
401      case 0x29:
402      case 0x2a:
403      case 0x2b:
404      case 0x2c:
405      case 0x2d:
406      case 0x2e:
407      case 0x2f:
408         sprintf(dst, "add   a,r%d", op&7);
409         break;
410
411      //JNB bit addr, code addr
412      case 0x30:            /* 1: 0011 0000 */
413         sym = get_bit_address(opram[PC++ - pc]);
414         rel  = opram[PC++ - pc];
415         sprintf(dst, "jnb   %s,$%04X", sym, (PC + rel));
416         break;
417
418      //RETI
419      case 0x32:            /* 1: 0011 0010 */
420         sprintf(dst, "reti");
421         flags = DASMFLAG_STEP_OUT;
422         break;
423
424      //RLC A
425      case 0x33:            /* 1: 0011 0011 */
426         sprintf(dst, "rlc   a");
427         break;
428
429      //ADDC A, #data
430      case 0x34:            /* 1: 0011 0100 */
431         sprintf(dst, "addc  a,#$%02X", opram[PC++ - pc]);
432         break;
433
434      //ADDC A, data addr
435      case 0x35:            /* 1: 0011 0101 */
436         sym = get_data_address(opram[PC++ - pc]);
437         sprintf(dst, "addc  a,%s", sym);
438         break;
439
440      //ADDC A, @R0/@R1       /* 1: 0011 011i */
441      case 0x36:
442      case 0x37:
443         sprintf(dst, "addc  a,@r%d", op&1);
444         break;
445
446      //ADDC A, R0 to R7      /* 1: 0011 1rrr */
447      case 0x38:
448      case 0x39:
449      case 0x3a:
450      case 0x3b:
451      case 0x3c:
452      case 0x3d:
453      case 0x3e:
454      case 0x3f:
455         sprintf(dst, "addc  a,r%d", op&7);
456         break;
457
458      //JC code addr
459      case 0x40:            /* 1: 0100 0000 */
460         rel = opram[PC++ - pc];
461         sprintf(dst, "jc    $%04X", PC + rel);
462         break;
463
464      //ORL data addr, A
465      case 0x42:            /* 1: 0100 0010 */
466         sym = get_data_address(opram[PC++ - pc]);
467         sprintf(dst, "orl   %s,a", sym);
468         break;
469
470      //ORL data addr, #data
471      case 0x43:            /* 1: 0100 0011 */
472         sym = get_data_address(opram[PC++ - pc]);
473         sprintf(dst, "orl   %s,#$%02X", sym, opram[PC++ - pc]);
474         break;
475
476      //Unable to Test
477      //ORL A, #data
478      case 0x44:            /* 1: 0100 0100 */
479         sprintf(dst, "orl   a,#$%02X", opram[PC++ - pc]);
480         break;
481
482      //ORL A, data addr
483      case 0x45:            /* 1: 0100 0101 */
484         sym = get_data_address(opram[PC++ - pc]);
485         sprintf(dst, "orl   a,%s", sym);
486         break;
487
488      //ORL A, @RO/@R1        /* 1: 0100 011i */
489      case 0x46:
490      case 0x47:
491         sprintf(dst, "orl   a,@r%d", op&1);
492         break;
493
494      //ORL A, RO to R7       /* 1: 0100 1rrr */
495      case 0x48:
496      case 0x49:
497      case 0x4a:
498      case 0x4b:
499      case 0x4c:
500      case 0x4d:
501      case 0x4e:
502      case 0x4f:
503         sprintf(dst, "orl   a,r%d", op&7);
504         break;
505
506      //JNC code addr
507      case 0x50:            /* 1: 0101 0000 */
508         rel = opram[PC++ - pc];
509         sprintf(dst, "jnc   $%04X", PC + rel);
510         break;
511
512      //Unable to test
513      //ANL data addr, A
514      case 0x52:            /* 1: 0101 0010 */
515         sym = get_data_address(opram[PC++ - pc]);
516         sprintf(dst, "anl   %s,a", sym);
517         break;
518
519      //Unable to test
520      //ANL data addr, #data
521      case 0x53:            /* 1: 0101 0011 */
522         sym = get_data_address(opram[PC++ - pc]);
523         sprintf(dst, "anl   %s,#$%02X", sym, opram[PC++ - pc]);
524         break;
525
526      //ANL A, #data
527      case 0x54:            /* 1: 0101 0100 */
528         sprintf(dst, "anl   a,#$%02X", opram[PC++ - pc]);
529         break;
530
531      //ANL A, data addr
532      case 0x55:            /* 1: 0101 0101 */
533         sym = get_data_address(opram[PC++ - pc]);
534         sprintf(dst, "anl   a,%s", sym);
535         break;
536
537      //Unable to test
538      //ANL A, @RO/@R1        /* 1: 0101 011i */
539      case 0x56:
540      case 0x57:
541         sprintf(dst, "anl   a,@r%d", op&1);
542         break;
543
544      //ANL A, RO to R7       /* 1: 0101 1rrr */
545      case 0x58:
546      case 0x59:
547      case 0x5a:
548      case 0x5b:
549      case 0x5c:
550      case 0x5d:
551      case 0x5e:
552      case 0x5f:
553         sprintf(dst, "anl   a,r%d", op&7);
554         break;
555
556      //JZ code addr
557      case 0x60:            /* 1: 0110 0000 */
558         rel = opram[PC++ - pc];
559         sprintf(dst, "jz    $%04X", PC + rel);
560         break;
561
562      //Unable to test
563      //XRL data addr, A
564      case 0x62:            /* 1: 0110 0010 */
565         sym = get_data_address(opram[PC++ - pc]);
566         sprintf(dst, "xrl   %s,a", sym);
567         break;
568
569      //XRL data addr, #data
570      case 0x63:            /* 1: 0110 0011 */
571         sym = get_data_address(opram[PC++ - pc]);
572         sprintf(dst, "xrl   %s,#$%02X", sym, opram[PC++ - pc]);
573         break;
574
575      //XRL A, #data
576      case 0x64:            /* 1: 0110 0100 */
577         sprintf(dst, "xrl   a,#$%02X", opram[PC++ - pc]);
578         break;
579
580      //XRL A, data addr
581      case 0x65:            /* 1: 0110 0101 */
582         sym = get_data_address(opram[PC++ - pc]);
583         sprintf(dst, "xrl   a,%s", sym);
584         break;
585
586      //Unable to test
587      //XRL A, @R0/@R1        /* 1: 0110 011i */
588      case 0x66:
589      case 0x67:
590         sprintf(dst, "xrl   a,@r%d", op&1);
591         break;
592
593      //XRL A, R0 to R7       /* 1: 0110 1rrr */
594      case 0x68:
595      case 0x69:
596      case 0x6a:
597      case 0x6b:
598      case 0x6c:
599      case 0x6d:
600      case 0x6e:
601      case 0x6f:
602         sprintf(dst, "xrl   a,r%d", op&7);
603         break;
604
605      //JNZ code addr
606      case 0x70:            /* 1: 0111 0000 */
607         rel = opram[PC++ - pc];
608         sprintf(dst, "jnz   $%04X", PC + rel);
609         break;
610
611      //Unable to test
612      //ORL C, bit addr
613      case 0x72:            /* 1: 0111 0010 */
614         sym = get_bit_address(opram[PC++ - pc]);
615         sprintf(dst, "orl   c,%s", sym);
616         break;
617
618      //Unable to test
619      //JMP @A+DPTR
620      case 0x73:            /* 1: 0111 0011 */
621         sprintf(dst, "jmp   @a+dptr");
622         break;
623
624      //MOV A, #data
625      case 0x74:            /* 1: 0111 0100 */
626         sprintf(dst, "mov   a,#$%02X", opram[PC++ - pc]);
627         break;
628
629      //MOV data addr, #data
630      case 0x75:            /* 1: 0111 0101 */
631         sym = get_data_address(opram[PC++ - pc]);
632         sprintf(dst, "mov   %s,#$%02X", sym, opram[PC++ - pc]);
633         break;
634
635      //Unable to test
636      //MOV @R0/@R1, #data    /* 1: 0111 011i */
637      case 0x76:
638      case 0x77:
639         sprintf(dst, "mov   @r%d,#$%02X", op&1, opram[PC++ - pc]);
640         break;
641
642      //MOV R0 to R7, #data   /* 1: 0111 1rrr */
643      case 0x78:
644      case 0x79:
645      case 0x7a:
646      case 0x7b:
647      case 0x7c:
648      case 0x7d:
649      case 0x7e:
650      case 0x7f:
651         sprintf(dst, "mov   r%d,#$%02X", (op & 7), opram[PC++ - pc]);
652         break;
653
654      //SJMP code addr
655      case 0x80:            /* 1: 1000 0000 */
656         rel = opram[PC++ - pc];
657         sprintf(dst, "sjmp  $%04X", PC + rel);
658         break;
659
660      //ANL C, bit addr
661      case 0x82:            /* 1: 1000 0010 */
662         sym = get_bit_address(opram[PC++ - pc]);
663         sprintf(dst, "anl   c,%s", sym);
664         break;
665
666      //MOVC A, @A + PC
667      case 0x83:            /* 1: 1000 0011 */
668         sprintf(dst, "movc  a,@a+pc");
669         break;
670
671      //DIV AB
672      case 0x84:            /* 1: 1000 0100 */
673         sprintf(dst, "div   ab");
674         break;
675
676      //MOV data addr, data addr  (Note: 1st address is src, 2nd is dst, but the mov command works as mov dst,src)
677      case 0x85:            /* 1: 1000 0101 */
678         sym  = get_data_address(opram[PC++ - pc]);
679         sym2 = get_data_address(opram[PC++ - pc]);
680         sprintf(dst, "mov   %s,%s", sym2, sym);
681         break;
682
683      //Unable to test
684      //MOV data addr, @R0/@R1/* 1: 1000 011i */
685      case 0x86:
686      case 0x87:
687         sym = get_data_address(opram[PC++ - pc]);
688         sprintf(dst, "mov   %s,@r%d", sym, op&1);
689         break;
690
691      //MOV data addr,R0 to R7/* 1: 1000 1rrr */
692      case 0x88:
693      case 0x89:
694      case 0x8a:
695      case 0x8b:
696      case 0x8c:
697      case 0x8d:
698      case 0x8e:
699      case 0x8f:
700         sym = get_data_address(opram[PC++ - pc]);
701         sprintf(dst, "mov   %s,r%d", sym, op&7);
702         break;
703
704      //MOV DPTR, #data16
705      case 0x90:            /* 1: 1001 0000 */
706         addr = (opram[PC++ - pc]<<8) & 0xff00;
707         addr|= opram[PC++ - pc];
708         sprintf(dst, "mov   dptr,#$%04X", addr);
709         break;
710
711      //MOV bit addr, C
712      case 0x92:            /* 1: 1001 0010 */
713         sym = get_bit_address(opram[PC++ - pc]);
714         sprintf(dst, "mov   %s,c", sym);
715         break;
716
717      //MOVC A, @A + DPTR
718      case 0x93:            /* 1: 1001 0011 */
719         sprintf(dst, "movc  a,@a+dptr");
720         break;
721
722      //SUBB A, #data
723      case 0x94:            /* 1: 1001 0100 */
724         sprintf(dst, "subb  a,#$%02X", opram[PC++ - pc]);
725         break;
726
727      //SUBB A, data addr
728      case 0x95:            /* 1: 1001 0101 */
729         sym = get_data_address(opram[PC++ - pc]);
730         sprintf(dst, "subb  a,%s", sym);
731         break;
732
733      //Unable to test
734      //SUBB A, @R0/@R1       /* 1: 1001 011i */
735      case 0x96:
736      case 0x97:
737         sprintf(dst, "subb  a,@r%d", op&1);
738         break;
739
740      //SUBB A, R0 to R7      /* 1: 1001 1rrr */
741      case 0x98:
742      case 0x99:
743      case 0x9a:
744      case 0x9b:
745      case 0x9c:
746      case 0x9d:
747      case 0x9e:
748      case 0x9f:
749         sprintf(dst, "subb  a,r%d", op&7);
750         break;
751
752      //Unable to test
753      //ORL C, /bit addr
754      case 0xa0:              /* 1: 1010 0000 */
755         sym = get_bit_address(opram[PC++ - pc]);
756         sprintf(dst, "orl   c,/%s", sym);
757         break;
758
759      //MOV C, bit addr
760      case 0xa2:              /* 1: 1010 0010 */
761         sym = get_bit_address(opram[PC++ - pc]);
762         sprintf(dst, "mov   c,%s", sym);
763         break;
764
765      //INC DPTR
766      case 0xa3:              /* 1: 1010 0011 */
767         sprintf(dst, "inc   dptr");
768         break;
769
770      //MUL AB
771      case 0xa4:              /* 1: 1010 0100 */
772         sprintf(dst, "mul   ab");
773         break;
774
775      //reserved
776      case 0xa5:              /* 1: 1010 0101 */
777         sprintf(dst, "ill/rsv");
778         break;
779
780      //Unable to test
781      //MOV @R0/@R1, data addr  /* 1: 1010 011i */
782      case 0xa6:
783      case 0xa7:
784         sym = get_data_address(opram[PC++ - pc]);
785         sprintf(dst, "mov   @r%d,%s", op&1, sym);
786         break;
787
788      //MOV R0 to R7, data addr /* 1: 1010 1rrr */
789      case 0xa8:
790      case 0xa9:
791      case 0xaa:
792      case 0xab:
793      case 0xac:
794      case 0xad:
795      case 0xae:
796      case 0xaf:
797         sym = get_data_address(opram[PC++ - pc]);
798         sprintf(dst, "mov   r%d,%s", op&7, sym);
799         break;
800
801      //ANL C,/bit addr
802      case 0xb0:                   /* 1: 1011 0000 */
803         sym = get_bit_address(opram[PC++ - pc]);
804         sprintf(dst, "anl   c,/%s", sym);
805         break;
806
807      //CPL bit addr
808      case 0xb2:                   /* 1: 1011 0010 */
809         sym = get_bit_address(opram[PC++ - pc]);
810         sprintf(dst, "cpl   %s", sym);
811         break;
812
813      //Unable to test
814      //CPL C
815      case 0xb3:                   /* 1: 1011 0011 */
816         sprintf(dst, "cpl   c");
817         break;
818
819      //CJNE A, #data, code addr
820      case 0xb4:                   /* 1: 1011 0100 */
821         data = opram[PC++ - pc];
822         rel  = opram[PC++ - pc];
823         sprintf(dst, "cjne  a,#$%02X,$%04X", data, PC + rel);
824         break;
825
826      //CJNE A, data addr, code addr
827      case 0xb5:                   /* 1: 1011 0101 */
828         sym = get_data_address(opram[PC++ - pc]);
829         rel  = opram[PC++ - pc];
830         sprintf(dst, "cjne  a,%s,$%04X", sym, PC + rel);
831         break;
832
833      //Unable to test
834      //CJNE @R0/@R1, #data, code addr /* 1: 1011 011i */
835      case 0xb6:
836      case 0xb7:
837         data = opram[PC++ - pc];
838         rel  = opram[PC++ - pc];
839         sprintf(dst, "cjne  @r%d,#$%02X,$%04X", op&1, data, PC + rel);
840         break;
841
842      //CJNE R0 to R7, #data, code addr/* 1: 1011 1rrr */
843      case 0xb8:
844      case 0xb9:
845      case 0xba:
846      case 0xbb:
847      case 0xbc:
848      case 0xbd:
849      case 0xbe:
850      case 0xbf:
851         data = opram[PC++ - pc];
852         rel  = opram[PC++ - pc];
853         sprintf(dst, "cjne  r%d,#$%02X,$%04X", op&7, data, PC + rel);
854         break;
855
856      //PUSH data addr
857      case 0xc0:                  /* 1: 1100 0000 */
858         sym = get_data_address(opram[PC++ - pc]);
859         sprintf(dst, "push  %s", sym);
860         break;
861
862      //CLR bit addr
863      case 0xc2:                  /* 1: 1100 0010 */
864         sym = get_bit_address(opram[PC++ - pc]);
865         sprintf(dst, "clr   %s", sym);
866         break;
867
868      //CLR C
869      case 0xc3:                  /* 1: 1100 0011 */
870         sprintf(dst, "clr   c");
871         break;
872
873      //SWAP A
874      case 0xc4:                  /* 1: 1100 0100 */
875         sprintf(dst, "swap  a");
876         break;
877
878      //XCH A, data addr
879      case 0xc5:                  /* 1: 1100 0101 */
880         sym = get_data_address(opram[PC++ - pc]);
881         sprintf(dst, "xch   a,%s", sym);
882         break;
883
884      //XCH A, @RO/@R1                /* 1: 1100 011i */
885      case 0xc6:
886      case 0xc7:
887         sprintf(dst, "xch   a,@r%d", op&1);
888         break;
889
890      //XCH A, RO to R7               /* 1: 1100 1rrr */
891      case 0xc8:
892      case 0xc9:
893      case 0xca:
894      case 0xcb:
895      case 0xcc:
896      case 0xcd:
897      case 0xce:
898      case 0xcf:
899         sprintf(dst, "xch   a,r%d", op&7);
900         break;
901
902      //POP data addr
903      case 0xd0:                  /* 1: 1101 0000 */
904         sym = get_data_address(opram[PC++ - pc]);
905         sprintf(dst, "pop   %s", sym);
906         break;
907
908      //SETB bit addr
909      case 0xd2:                  /* 1: 1101 0010 */
910         sym = get_bit_address(opram[PC++ - pc]);
911         sprintf(dst, "setb  %s", sym);
912         break;
913
914      //SETB C
915      case 0xd3:                  /* 1: 1101 0011 */
916         sprintf(dst, "setb  c");
917         break;
918
919      //Unable to test
920      //DA A
921      case 0xd4:                  /* 1: 1101 0100 */
922         sprintf(dst, "da   a");
923         break;
924
925      //DJNZ data addr, code addr
926      case 0xd5:                  /* 1: 1101 0101 */
927         sym = get_data_address(opram[PC++ - pc]);
928         rel  = opram[PC++ - pc];
929         sprintf(dst, "djnz  %s,$%04X", sym, PC + rel);
930         flags = DASMFLAG_STEP_OVER;
931         break;
932
933      //XCHD A, @R0/@R1               /* 1: 1101 011i */
934      case 0xd6:
935      case 0xd7:
936         sprintf(dst, "xchd  a,@r%d", op&1);
937         break;
938
939      //DJNZ R0 to R7,code addr       /* 1: 1101 1rrr */
940      case 0xd8:
941      case 0xd9:
942      case 0xda:
943      case 0xdb:
944      case 0xdc:
945      case 0xdd:
946      case 0xde:
947      case 0xdf:
948         rel = opram[PC++ - pc];
949         sprintf(dst, "djnz  r%d,$%04X", op&7, (PC + rel));
950         flags = DASMFLAG_STEP_OVER;
951         break;
952
953      //MOVX A,@DPTR
954      case 0xe0:                  /* 1: 1110 0000 */
955         sprintf(dst, "movx  a,@dptr");
956         break;
957
958      //Unable to test
959      //MOVX A, @R0/@R1               /* 1: 1110 001i */
960      case 0xe2:
961      case 0xe3:
962         sprintf(dst, "movx  a,@r%d", op&1);
963         break;
964
965      //CLR A
966      case 0xe4:                  /* 1: 1110 0100 */
967         sprintf(dst, "clr   a");
968         break;
969
970      //MOV A, data addr
971      case 0xe5:                  /* 1: 1110 0101 */
972         sym = get_data_address(opram[PC++ - pc]);
973         sprintf(dst, "mov   a,%s", sym);
974         break;
975
976      //Unable to test
977      //MOV A,@RO/@R1                 /* 1: 1110 011i */
978      case 0xe6:
979      case 0xe7:
980         sprintf(dst, "mov   a,@r%d", op&1);
981         break;
982
983      //MOV A,R0 to R7                /* 1: 1110 1rrr */
984      case 0xe8:
985      case 0xe9:
986      case 0xea:
987      case 0xeb:
988      case 0xec:
989      case 0xed:
990      case 0xee:
991      case 0xef:
992         sprintf(dst, "mov   a,r%d", op&7);
993         break;
994
995      //MOVX @DPTR,A
996      case 0xf0:                  /* 1: 1111 0000 */
997         sprintf(dst, "movx  @dptr,a");
998         break;
999
1000      //Unable to test
1001      //MOVX @R0/@R1,A                /* 1: 1111 001i */
1002      case 0xf2:
1003      case 0xf3:
1004         sprintf(dst, "movx  @r%d,a", op&1);
1005         break;
1006
1007      //CPL A
1008      case 0xf4:                  /* 1: 1111 0100 */
1009         sprintf(dst, "cpl   a");
1010         break;
1011
1012      //MOV data addr, A
1013      case 0xf5:                  /* 1: 1111 0101 */
1014         sym = get_data_address(opram[PC++ - pc]);
1015         sprintf(dst, "mov   %s,a", sym);
1016         break;
1017
1018      //MOV @R0/@R1, A                /* 1: 1111 011i */
1019      case 0xf6:
1020      case 0xf7:
1021         sprintf(dst, "mov   @r%d,a", op&1);
1022         break;
1023
1024      //MOV R0 to R7, A               /* 1: 1111 1rrr */
1025      case 0xf8:
1026      case 0xf9:
1027      case 0xfa:
1028      case 0xfb:
1029      case 0xfc:
1030      case 0xfd:
1031      case 0xfe:
1032      case 0xff:
1033         sprintf(dst, "mov   r%d,a", op&7);
1034         break;
1035
1036      default:
1037         sprintf(dst, "illegal");
1038    }
1039   return (PC - pc) | flags | DASMFLAG_SUPPORTED;
1040}
src/emu/cpu/drcfe.c
r2627r2628
55    Generic dynamic recompiler frontend structures and utilities.
66
77    Copyright Aaron Giles
8    Released for general use under the MAME license
8    Released for general non-commercial use under the MAME license
99    Visit http://mamedev.org for licensing and usage restrictions.
1010
1111***************************************************************************/
r2627r2628
4444   UINT32            window_end;               /* code window end offset = startpc + window_end */
4545   UINT32            max_sequence;            /* maximum instructions to include in a sequence */
4646
47   drcfe_describe_func      describe;               /* callback to describe a single instruction */
47   drcfe_describe_func   describe;               /* callback to describe a single instruction */
4848   void *            param;                  /* parameter for the callback */
4949
5050   /* CPU parameters */
src/emu/cpu/drcuml.c
r0r2628
1/***************************************************************************
2
3    drcuml.c
4
5    Universal machine language for dynamic recompiling CPU cores.
6
7    Copyright Aaron Giles
8    Released for general non-commercial use under the MAME license
9    Visit http://mamedev.org for licensing and usage restrictions.
10
11****************************************************************************
12
13    Future improvements/changes:
14
15    * Write a back-end validator:
16        - checks all combinations of memory/register/immediate on all params
17        - checks behavior of all opcodes
18
19    * Extend registers to 16? Depends on if PPC can use them
20
21    * New instructions?
22        - EXTRACT dst,src,shift,mask
23            dst = (src >> shift) & mask
24
25        - INSERT dst,src,shift,mask
26            dst = (dst & ~mask) | ((src << shift) & mask)
27
28        - VALID opcode_desc,handle,param
29            checksum/compare code referenced by opcode_desc; if not
30            matching, generate exception with handle,param
31
32        - RECALL handle
33            change code at caller to call handle in the future
34
35    * Add interface for getting hints from the backend:
36        - number of direct-mapped integer registers
37        - number of direct-mapped floating point registers
38
39***************************************************************************/
40
41#include "drcuml.h"
42#include "drcumlsh.h"
43#include "drcumld.h"
44#include "mame.h"
45#include "deprecat.h"
46#include <stdarg.h>
47#include <setjmp.h>
48
49
50/***************************************************************************
51    CONSTANTS
52***************************************************************************/
53
54/* opcode validation condition/flag valid bitmasks */
55#define OV_CONDFLAG_NONE      0x00
56#define OV_CONDFLAG_COND      0x80
57#define OV_CONDFLAG_FLAGS      0x1f
58
59/* opcode validation parameter valid bitmasks */
60#define OV_PARAM_ALLOWED_NONE   (1 << DRCUML_PTYPE_NONE)
61#define OV_PARAM_ALLOWED_IMM   (1 << DRCUML_PTYPE_IMMEDIATE)
62#define OV_PARAM_ALLOWED_IREG   (1 << DRCUML_PTYPE_INT_REGISTER)
63#define OV_PARAM_ALLOWED_FREG   (1 << DRCUML_PTYPE_FLOAT_REGISTER)
64#define OV_PARAM_ALLOWED_MVAR   (1 << DRCUML_PTYPE_MAPVAR)
65#define OV_PARAM_ALLOWED_MEM   (1 << DRCUML_PTYPE_MEMORY)
66#define OV_PARAM_ALLOWED_NCMEM   (OV_PARAM_ALLOWED_MEM | 0x80)
67#define OV_PARAM_ALLOWED_IMV   (OV_PARAM_ALLOWED_IMM | OV_PARAM_ALLOWED_MVAR)
68#define OV_PARAM_ALLOWED_IRM   (OV_PARAM_ALLOWED_IREG | OV_PARAM_ALLOWED_MEM)
69#define OV_PARAM_ALLOWED_FRM   (OV_PARAM_ALLOWED_FREG | OV_PARAM_ALLOWED_MEM)
70#define OV_PARAM_ALLOWED_IANY   (OV_PARAM_ALLOWED_IRM | OV_PARAM_ALLOWED_IMV)
71#define OV_PARAM_ALLOWED_FANY   (OV_PARAM_ALLOWED_FRM)
72
73
74
75/***************************************************************************
76    TYPE DEFINITIONS
77***************************************************************************/
78
79/* structure describing UML generation state */
80struct _drcuml_state
81{
82   drccache *            cache;            /* pointer to the codegen cache */
83   drcuml_block *         blocklist;         /* list of active blocks */
84
85   const drcbe_interface *   beintf;            /* backend interface pointer */
86   drcbe_state *         bestate;         /* pointer to the back-end state */
87
88   drcuml_codehandle *      handlelist;         /* head of linked list of handles */
89
90   FILE *               umllog;            /* handle to the UML logfile */
91};
92
93
94/* structure describing UML codegen block */
95struct _drcuml_block
96{
97   drcuml_state *         drcuml;            /* pointer back to the owning UML */
98   drcuml_block *         next;            /* pointer to next block */
99   drcuml_instruction *   inst;            /* pointer to the instruction list */
100   UINT8               inuse;            /* this block is in use */
101   UINT32               maxinst;         /* maximum number of instructions */
102   UINT32               nextinst;         /* next instruction to fill in the cache */
103   jmp_buf   *            errorbuf;         /* setjmp buffer for deep error handling */
104};
105
106
107/* structure describing a global handle */
108struct _drcuml_codehandle
109{
110   drccodeptr            code;            /* pointer to the associated code */
111   char *               string;            /* pointer to string attached to handle */
112   drcuml_codehandle *      next;            /* link to next handle in the list */
113   drcuml_state *         drcuml;            /* pointer to owning object */
114};
115
116
117/* structure describing an opcode for validation */
118typedef struct _drcuml_opcode_valid drcuml_opcode_valid;
119struct _drcuml_opcode_valid
120{
121   drcuml_opcode         opcode;            /* the opcode itself */
122   UINT8               sizes;            /* allowed sizes */
123   UINT8               condflags;         /* allowed conditions/flags */
124   UINT8               p0types;         /* allowed types for parameter 0 */
125   UINT8               p1types;         /* allowed types for parameter 1 */
126   UINT8               p2types;         /* allowed types for parameter 2 */
127   UINT8               p3types;         /* allowed types for parameter 3 */
128};
129
130
131
132/***************************************************************************
133    TABLES
134***************************************************************************/
135
136/* macro to simplify the table */
137#define OPVALID_ENTRY_0(op,sizes,condflag)            { DRCUML_OP_##op, sizes, OV_CONDFLAG_##condflag, OV_PARAM_ALLOWED_NONE, OV_PARAM_ALLOWED_NONE, OV_PARAM_ALLOWED_NONE, OV_PARAM_ALLOWED_NONE },
138#define OPVALID_ENTRY_1(op,sizes,condflag,p0)         { DRCUML_OP_##op, sizes, OV_CONDFLAG_##condflag, OV_PARAM_ALLOWED_##p0, OV_PARAM_ALLOWED_NONE, OV_PARAM_ALLOWED_NONE, OV_PARAM_ALLOWED_NONE },
139#define OPVALID_ENTRY_2(op,sizes,condflag,p0,p1)      { DRCUML_OP_##op, sizes, OV_CONDFLAG_##condflag, OV_PARAM_ALLOWED_##p0, OV_PARAM_ALLOWED_##p1, OV_PARAM_ALLOWED_NONE, OV_PARAM_ALLOWED_NONE },
140#define OPVALID_ENTRY_3(op,sizes,condflag,p0,p1,p2)      { DRCUML_OP_##op, sizes, OV_CONDFLAG_##condflag, OV_PARAM_ALLOWED_##p0, OV_PARAM_ALLOWED_##p1, OV_PARAM_ALLOWED_##p2, OV_PARAM_ALLOWED_NONE },
141#define OPVALID_ENTRY_4(op,sizes,condflag,p0,p1,p2,p3)   { DRCUML_OP_##op, sizes, OV_CONDFLAG_##condflag, OV_PARAM_ALLOWED_##p0, OV_PARAM_ALLOWED_##p1, OV_PARAM_ALLOWED_##p2, OV_PARAM_ALLOWED_##p3 },
142
143/* opcode validation table */
144static const drcuml_opcode_valid opcode_valid_list[] =
145{
146   /* Compile-time opcodes */
147   OPVALID_ENTRY_1(HANDLE,  4,   NONE,  MEM)
148   OPVALID_ENTRY_2(HASH,    4,   NONE,  IMV,  IMV)
149   OPVALID_ENTRY_1(LABEL,   4,   NONE,  IMV)
150   OPVALID_ENTRY_1(COMMENT, 4,   NONE,  NCMEM)
151   OPVALID_ENTRY_2(MAPVAR,  4,   NONE,  MVAR, IMV)
152
153   /* Control Flow Operations */
154   OPVALID_ENTRY_1(DEBUG,   4,   NONE,  IANY)
155   OPVALID_ENTRY_1(EXIT,    4,   COND,  IANY)
156   OPVALID_ENTRY_3(HASHJMP, 4,   NONE,  IANY, IANY, MEM)
157   OPVALID_ENTRY_1(LABEL,   4,   NONE,  IMV)
158   OPVALID_ENTRY_1(JMP,     4,   COND,  IMV)
159   OPVALID_ENTRY_2(EXH,     4,   COND,  MEM,  IANY)
160   OPVALID_ENTRY_1(CALLH,   4,   COND,  MEM)
161   OPVALID_ENTRY_0(RET,     4,   COND)
162   OPVALID_ENTRY_2(CALLC,   4,   COND,  NCMEM,NCMEM)
163   OPVALID_ENTRY_2(RECOVER, 4,   NONE,  IRM,  MVAR)
164
165   /* Internal Register Operations */
166   OPVALID_ENTRY_1(SETFMOD, 4,   NONE,  IANY)
167   OPVALID_ENTRY_1(GETFMOD, 4,   NONE,  IRM)
168   OPVALID_ENTRY_1(GETEXP,  4,   NONE,  IRM)
169
170   /* Integer Operations */
171   OPVALID_ENTRY_3(LOAD1U,  4|8, NONE,  IRM,  NCMEM,IANY)
172   OPVALID_ENTRY_3(LOAD1S,  4|8, NONE,  IRM,  NCMEM,IANY)
173   OPVALID_ENTRY_3(LOAD2U,  4|8, NONE,  IRM,  NCMEM,IANY)
174   OPVALID_ENTRY_3(LOAD2S,  4|8, NONE,  IRM,  NCMEM,IANY)
175   OPVALID_ENTRY_3(LOAD4U,  4|8, NONE,  IRM,  NCMEM,IANY)
176   OPVALID_ENTRY_3(LOAD4S,    8, NONE,  IRM,  NCMEM,IANY)
177   OPVALID_ENTRY_3(LOAD8U,    8, NONE,  IRM,  NCMEM,IANY)
178   OPVALID_ENTRY_3(STORE1,  4|8, NONE,  NCMEM,IANY, IANY)
179   OPVALID_ENTRY_3(STORE2,  4|8, NONE,  NCMEM,IANY, IANY)
180   OPVALID_ENTRY_3(STORE4,  4|8, NONE,  NCMEM,IANY, IANY)
181   OPVALID_ENTRY_3(STORE8,    8, NONE,  NCMEM,IANY, IANY)
182   OPVALID_ENTRY_3(READ1U,  4|8, NONE,  IRM,  IMV,  IANY)
183   OPVALID_ENTRY_3(READ1S,  4|8, NONE,  IRM,  IMV,  IANY)
184   OPVALID_ENTRY_3(READ2U,  4|8, NONE,  IRM,  IMV,  IANY)
185   OPVALID_ENTRY_3(READ2S,  4|8, NONE,  IRM,  IMV,  IANY)
186   OPVALID_ENTRY_4(READ2M,  4|8, NONE,  IRM,  IMV,  IANY, IANY)
187   OPVALID_ENTRY_3(READ4U,  4|8, NONE,  IRM,  IMV,  IANY)
188   OPVALID_ENTRY_3(READ4S,    8, NONE,  IRM,  IMV,  IANY)
189   OPVALID_ENTRY_4(READ4M,  4|8, NONE,  IRM,  IMV,  IANY, IANY)
190   OPVALID_ENTRY_3(READ8U,    8, NONE,  IRM,  IMV,  IANY)
191   OPVALID_ENTRY_4(READ8M,    8, NONE,  IRM,  IMV,  IANY, IANY)
192   OPVALID_ENTRY_3(WRITE1,  4|8, NONE,  IMV,  IANY, IANY)
193   OPVALID_ENTRY_3(WRITE2,  4|8, NONE,  IMV,  IANY, IANY)
194   OPVALID_ENTRY_4(WRIT2M,  4|8, NONE,  IMV,  IANY, IANY, IANY)
195   OPVALID_ENTRY_3(WRITE4,  4|8, NONE,  IMV,  IANY, IANY)
196   OPVALID_ENTRY_4(WRIT4M,  4|8, NONE,  IMV,  IANY, IANY, IANY)
197   OPVALID_ENTRY_3(WRITE8,    8, NONE,  IMV,  IANY, IANY)
198   OPVALID_ENTRY_4(WRIT8M,    8, NONE,  IMV,  IANY, IANY, IANY)
199   OPVALID_ENTRY_3(FLAGS,   4|8, NONE,  IRM,  IMV,  MEM)
200   OPVALID_ENTRY_2(MOV,     4|8, COND,  IRM,  IANY)
201   OPVALID_ENTRY_2(ZEXT1,   4|8, NONE,  IRM,  IANY)
202   OPVALID_ENTRY_2(ZEXT2,   4|8, NONE,  IRM,  IANY)
203   OPVALID_ENTRY_2(ZEXT4,     8, NONE,  IRM,  IANY)
204   OPVALID_ENTRY_2(SEXT1,   4|8, NONE,  IRM,  IANY)
205   OPVALID_ENTRY_2(SEXT2,   4|8, NONE,  IRM,  IANY)
206   OPVALID_ENTRY_2(SEXT4,     8, NONE,  IRM,  IANY)
207   OPVALID_ENTRY_3(ADD,     4|8, FLAGS, IRM,  IANY, IANY)
208   OPVALID_ENTRY_3(ADDC,    4|8, FLAGS, IRM,  IANY, IANY)
209   OPVALID_ENTRY_3(SUB,     4|8, FLAGS, IRM,  IANY, IANY)
210   OPVALID_ENTRY_3(SUBB,    4|8, FLAGS, IRM,  IANY, IANY)
211   OPVALID_ENTRY_2(CMP,     4|8, FLAGS, IANY, IANY)
212   OPVALID_ENTRY_4(MULU,    4|8, FLAGS, IRM,  IRM,  IANY, IANY)
213   OPVALID_ENTRY_4(MULS,    4|8, FLAGS, IRM,  IRM,  IANY, IANY)
214   OPVALID_ENTRY_4(DIVU,    4|8, FLAGS, IRM,  IRM,  IANY, IANY)
215   OPVALID_ENTRY_4(DIVS,    4|8, FLAGS, IRM,  IRM,  IANY, IANY)
216   OPVALID_ENTRY_3(AND,     4|8, FLAGS, IRM,  IANY, IANY)
217   OPVALID_ENTRY_2(TEST,    4|8, FLAGS, IANY, IANY)
218   OPVALID_ENTRY_3(OR,      4|8, FLAGS, IRM,  IANY, IANY)
219   OPVALID_ENTRY_3(XOR,     4|8, FLAGS, IRM,  IANY, IANY)
220   OPVALID_ENTRY_3(SHL,     4|8, FLAGS, IRM,  IANY, IANY)
221   OPVALID_ENTRY_3(SHR,     4|8, FLAGS, IRM,  IANY, IANY)
222   OPVALID_ENTRY_3(SAR,     4|8, FLAGS, IRM,  IANY, IANY)
223   OPVALID_ENTRY_3(ROL,     4|8, FLAGS, IRM,  IANY, IANY)
224   OPVALID_ENTRY_3(ROLC,    4|8, FLAGS, IRM,  IANY, IANY)
225   OPVALID_ENTRY_3(ROR,     4|8, FLAGS, IRM,  IANY, IANY)
226   OPVALID_ENTRY_3(RORC,    4|8, FLAGS, IRM,  IANY, IANY)
227
228   /* Floating Point Operations */
229   OPVALID_ENTRY_3(FLOAD,   4|8, NONE,  FRM,  NCMEM,IANY)
230   OPVALID_ENTRY_3(FSTORE,  4|8, NONE,  NCMEM,IANY, FANY)
231   OPVALID_ENTRY_3(FREAD,   4|8, NONE,  FRM,  IMV,  IANY)
232   OPVALID_ENTRY_3(FWRITE,  4|8, NONE,  IMV,  IANY, FANY)
233   OPVALID_ENTRY_2(FMOV,    4|8, COND,  FRM,  FANY)
234   OPVALID_ENTRY_2(FTOI4,   4|8, NONE,  IRM,  FANY)
235   OPVALID_ENTRY_2(FTOI4T,  4|8, NONE,  IRM,  FANY)
236   OPVALID_ENTRY_2(FTOI4R,  4|8, NONE,  IRM,  FANY)
237   OPVALID_ENTRY_2(FTOI4F,  4|8, NONE,  IRM,  FANY)
238   OPVALID_ENTRY_2(FTOI4C,  4|8, NONE,  IRM,  FANY)
239   OPVALID_ENTRY_2(FTOI8,   4|8, NONE,  IRM,  FANY)
240   OPVALID_ENTRY_2(FTOI8T,  4|8, NONE,  IRM,  FANY)
241   OPVALID_ENTRY_2(FTOI8R,  4|8, NONE,  IRM,  FANY)
242   OPVALID_ENTRY_2(FTOI8F,  4|8, NONE,  IRM,  FANY)
243   OPVALID_ENTRY_2(FTOI8C,  4|8, NONE,  IRM,  FANY)
244   OPVALID_ENTRY_2(FFRFS,     8, NONE,  FRM,  FANY)
245   OPVALID_ENTRY_2(FFRFD,   4  , NONE,  FRM,  FANY)
246   OPVALID_ENTRY_2(FFRI4,   4|8, NONE,  FRM,  IANY)
247   OPVALID_ENTRY_2(FFRI8,   4|8, NONE,  FRM,  IANY)
248   OPVALID_ENTRY_3(FADD,    4|8, FLAGS, FRM,  FANY, FANY)
249   OPVALID_ENTRY_3(FSUB,    4|8, FLAGS, FRM,  FANY, FANY)
250   OPVALID_ENTRY_2(FCMP,    4|8, FLAGS, FANY, FANY)
251   OPVALID_ENTRY_3(FMUL,    4|8, FLAGS, FRM,  FANY, FANY)
252   OPVALID_ENTRY_3(FDIV,    4|8, FLAGS, FRM,  FANY, FANY)
253   OPVALID_ENTRY_2(FNEG,    4|8, FLAGS, FRM,  FANY)
254   OPVALID_ENTRY_2(FABS,    4|8, FLAGS, FRM,  FANY)
255   OPVALID_ENTRY_2(FSQRT,   4|8, FLAGS, FRM,  FANY)
256   OPVALID_ENTRY_2(FRECIP,  4|8, NONE,  FRM,  FANY)
257   OPVALID_ENTRY_2(FRSQRT,  4|8, NONE,  FRM,  FANY)
258};
259
260
261
262/***************************************************************************
263    GLOBAL VARIABLES
264***************************************************************************/
265
266/* we presume the existence of a C back-end interface */
267extern const drcbe_interface drcbe_c_be_interface;
268
269/* extern a reference to the native DRC interface if it exists */
270#ifndef NATIVE_DRC
271#define NATIVE_DRC drcbe_c_be_interface
272#else
273extern const drcbe_interface NATIVE_DRC;
274#endif
275
276/* table that is built up on first alloc which looks up via opcode */
277static drcuml_opcode_valid opcode_valid_table[DRCUML_OP_MAX];
278
279
280
281/***************************************************************************
282    FUNCTION PROTOTYPES
283***************************************************************************/
284
285#ifdef MAME_DEBUG
286static void validate_instruction(drcuml_block *block, const drcuml_instruction *inst);
287#else
288#define validate_instruction(block, inst) do { } while (0)
289#endif
290
291
292
293/***************************************************************************
294    INITIALIZATION/TEARDOWN
295***************************************************************************/
296
297/*-------------------------------------------------
298    drcuml_alloc - allocate state for the code
299    generator and initialize the back-end
300-------------------------------------------------*/
301
302drcuml_state *drcuml_alloc(drccache *cache, UINT32 flags, int modes, int addrbits, int ignorebits)
303{
304   drcuml_state *drcuml;
305   int opnum;
306
307   /* allocate state */
308   drcuml = drccache_memory_alloc(cache, sizeof(*drcuml));
309   if (drcuml == NULL)
310      return NULL;
311   memset(drcuml, 0, sizeof(*drcuml));
312
313   /* initialize the state */
314   drcuml->cache = cache;
315   drcuml->beintf = (flags & DRCUML_OPTION_USE_C) ? &drcbe_c_be_interface : &NATIVE_DRC;
316
317   /* if we're to log, create the logfile */
318   if (flags & DRCUML_OPTION_LOG_UML)
319      drcuml->umllog = fopen("drcuml.asm", "w");
320
321   /* allocate the back-end */
322   drcuml->bestate = (*drcuml->beintf->be_alloc)(drcuml, cache, flags, modes, addrbits, ignorebits);
323   if (drcuml->bestate == NULL)
324   {
325      drcuml_free(drcuml);
326      return NULL;
327   }
328
329   /* update the valid opcode table */
330   for (opnum = 0; opnum < ARRAY_LENGTH(opcode_valid_list); opnum++)
331      opcode_valid_table[opcode_valid_list[opnum].opcode] = opcode_valid_list[opnum];
332
333   return drcuml;
334}
335
336
337/*-------------------------------------------------
338    drcuml_reset - reset the state completely,
339    flushing the cache and all information
340-------------------------------------------------*/
341
342void drcuml_reset(drcuml_state *drcuml)
343{
344   drcuml_codehandle *handle;
345   jmp_buf errorbuf;
346
347   /* flush the cache */
348   drccache_flush(drcuml->cache);
349
350   /* if we error here, we are screwed */
351   if (setjmp(errorbuf) != 0)
352      fatalerror("Out of cache space in drcuml_reset");
353
354   /* reset all handle code pointers */
355   for (handle = drcuml->handlelist; handle != NULL; handle = handle->next)
356      handle->code = NULL;
357
358   /* call the backend to reset */
359   (*drcuml->beintf->be_reset)(drcuml->bestate);
360}
361
362
363/*-------------------------------------------------
364    drcuml_free - free state for the code generator
365    and the back-end
366-------------------------------------------------*/
367
368void drcuml_free(drcuml_state *drcuml)
369{
370   /* free the back-end */
371   if (drcuml->bestate != NULL)
372      (*drcuml->beintf->be_free)(drcuml->bestate);
373
374   /* free all the blocks */
375   while (drcuml->blocklist != NULL)
376   {
377      drcuml_block *block = drcuml->blocklist;
378
379      /* remove the item from the list */
380      drcuml->blocklist = block->next;
381
382      /* free memory */
383      if (block->inst != NULL)
384         free(block->inst);
385      free(block);
386   }
387
388   /* close any files */
389   if (drcuml->umllog != NULL)
390      fclose(drcuml->umllog);
391}
392
393
394
395/***************************************************************************
396    CODE BLOCK GENERATION
397***************************************************************************/
398
399/*-------------------------------------------------
400    drcuml_block_begin - begin a new code block
401-------------------------------------------------*/
402
403drcuml_block *drcuml_block_begin(drcuml_state *drcuml, UINT32 maxinst, jmp_buf *errorbuf)
404{
405   drcuml_block *bestblock = NULL;
406   drcuml_block *block;
407
408   /* find an inactive block that matches our qualifications */
409   for (block = drcuml->blocklist; block != NULL; block = block->next)
410      if (!block->inuse && block->maxinst >= maxinst && (bestblock == NULL || block->maxinst < bestblock->maxinst))
411         bestblock = block;
412
413   /* if we failed to find one, allocate a new one */
414   if (bestblock == NULL)
415   {
416      /* allocate the block structure itself */
417      bestblock = malloc(sizeof(*bestblock));
418      if (bestblock == NULL)
419         fatalerror("Out of memory allocating block in drcuml_block_begin");
420      memset(bestblock, 0, sizeof(*bestblock));
421
422      /* fill in the structure */
423      bestblock->drcuml = drcuml;
424      bestblock->next = drcuml->blocklist;
425      bestblock->maxinst = maxinst * 3 / 2;
426      bestblock->inst = malloc(sizeof(drcuml_instruction) * bestblock->maxinst);
427      if (bestblock->inst == NULL)
428         fatalerror("Out of memory allocating instruction array in drcuml_block_begin");
429
430      /* hook us into the list */
431      drcuml->blocklist = bestblock;
432   }
433
434   /* set up the block information and return it */
435   bestblock->inuse = TRUE;
436   bestblock->nextinst = 0;
437   bestblock->errorbuf = errorbuf;
438
439   return bestblock;
440}
441
442
443/*-------------------------------------------------
444    drcuml_block_append_0 - append an opcode with
445    0 parameters to the block
446-------------------------------------------------*/
447
448void drcuml_block_append_0(drcuml_block *block, drcuml_opcode op, UINT8 size, UINT8 condflags)
449{
450   drcuml_instruction *inst = &block->inst[block->nextinst++];
451
452   assert(block->inuse);
453
454   /* get a pointer to the next instruction */
455   if (block->nextinst > block->maxinst)
456      fatalerror("Overran maxinst in drcuml_block_append");
457
458   /* fill in the instruction */
459   inst->opcode = (UINT8)op;
460   inst->size = size;
461   inst->condflags = condflags;
462   inst->numparams = 0;
463
464   /* validation */
465   validate_instruction(block, inst);
466}
467
468
469/*-------------------------------------------------
470    drcuml_block_append_1 - append an opcode with
471    1 parameter to the block
472-------------------------------------------------*/
473
474void drcuml_block_append_1(drcuml_block *block, drcuml_opcode op, UINT8 size, UINT8 condflags, drcuml_ptype p0type, drcuml_pvalue p0value)
475{
476   drcuml_instruction *inst = &block->inst[block->nextinst++];
477
478   assert(block->inuse);
479
480   /* get a pointer to the next instruction */
481   if (block->nextinst > block->maxinst)
482      fatalerror("Overran maxinst in drcuml_block_append");
483
484   /* fill in the instruction */
485   inst->opcode = (UINT8)op;
486   inst->size = size;
487   inst->condflags = condflags;
488   inst->numparams = 1;
489   inst->param[0].type = p0type;
490   inst->param[0].value = p0value;
491
492   /* validation */
493   validate_instruction(block, inst);
494}
495
496
497/*-------------------------------------------------
498    drcuml_block_append_2 - append an opcode with
499    2 parameters to the block
500-------------------------------------------------*/
501
502void drcuml_block_append_2(drcuml_block *block, drcuml_opcode op, UINT8 size, UINT8 condflags, drcuml_ptype p0type, drcuml_pvalue p0value, drcuml_ptype p1type, drcuml_pvalue p1value)
503{
504   drcuml_instruction *inst = &block->inst[block->nextinst++];
505
506   assert(block->inuse);
507
508   /* get a pointer to the next instruction */
509   if (block->nextinst > block->maxinst)
510      fatalerror("Overran maxinst in drcuml_block_append");
511
512   /* fill in the instruction */
513   inst->opcode = (UINT8)op;
514   inst->size = size;
515   inst->condflags = condflags;
516   inst->numparams = 2;
517   inst->param[0].type = p0type;
518   inst->param[0].value = p0value;
519   inst->param[1].type = p1type;
520   inst->param[1].value = p1value;
521
522   /* validation */
523   validate_instruction(block, inst);
524}
525
526
527/*-------------------------------------------------
528    drcuml_block_append_3 - append an opcode with
529    3 parameters to the block
530-------------------------------------------------*/
531
532void drcuml_block_append_3(drcuml_block *block, drcuml_opcode op, UINT8 size, UINT8 condflags, drcuml_ptype p0type, drcuml_pvalue p0value, drcuml_ptype p1type, drcuml_pvalue p1value, drcuml_ptype p2type, drcuml_pvalue p2value)
533{
534   drcuml_instruction *inst = &block->inst[block->nextinst++];
535
536   assert(block->inuse);
537
538   /* get a pointer to the next instruction */
539   if (block->nextinst > block->maxinst)
540      fatalerror("Overran maxinst in drcuml_block_append");
541
542   /* fill in the instruction */
543   inst->opcode = (UINT8)op;
544   inst->size = size;
545   inst->condflags = condflags;
546   inst->numparams = 3;
547   inst->param[0].type = p0type;
548   inst->param[0].value = p0value;
549   inst->param[1].type = p1type;
550   inst->param[1].value = p1value;
551   inst->param[2].type = p2type;
552   inst->param[2].value = p2value;
553
554   /* validation */
555   validate_instruction(block, inst);
556}
557
558
559/*-------------------------------------------------
560    drcuml_block_append_4 - append an opcode with
561    4 parameters to the block
562-------------------------------------------------*/
563
564void drcuml_block_append_4(drcuml_block *block, drcuml_opcode op, UINT8 size, UINT8 condflags, drcuml_ptype p0type, drcuml_pvalue p0value, drcuml_ptype p1type, drcuml_pvalue p1value, drcuml_ptype p2type, drcuml_pvalue p2value, drcuml_ptype p3type, drcuml_pvalue p3value)
565{
566   drcuml_instruction *inst = &block->inst[block->nextinst++];
567
568   assert(block->inuse);
569
570   /* get a pointer to the next instruction */
571   if (block->nextinst > block->maxinst)
572      fatalerror("Overran maxinst in drcuml_block_append");
573
574   /* fill in the instruction */
575   inst->opcode = (UINT8)op;
576   inst->size = size;
577   inst->condflags = condflags;
578   inst->numparams = 4;
579   inst->param[0].type = p0type;
580   inst->param[0].value = p0value;
581   inst->param[1].type = p1type;
582   inst->param[1].value = p1value;
583   inst->param[2].type = p2type;
584   inst->param[2].value = p2value;
585   inst->param[3].type = p3type;
586   inst->param[3].value = p3value;
587
588   /* validation */
589   validate_instruction(block, inst);
590}
591
592
593/*-------------------------------------------------
594    drcuml_block_end - complete a code block and
595    commit it to the cache via the back-end
596-------------------------------------------------*/
597
598void drcuml_block_end(drcuml_block *block)
599{
600   drcuml_state *drcuml = block->drcuml;
601
602   assert(block->inuse);
603
604   /* if we have a logfile, generate a disassembly of the block */
605   if (drcuml->umllog != NULL)
606   {
607      int instnum;
608
609      /* iterate over instructions and output */
610      for (instnum = 0; instnum < block->nextinst; instnum++)
611      {
612         char dasm[512];
613         drcuml_disasm(&block->inst[instnum], dasm);
614         drcuml_log_printf(drcuml, "%4d: %s\n", instnum, dasm);
615      }
616      drcuml_log_printf(drcuml, "\n\n");
617      fflush(drcuml->umllog);
618   }
619
620   /* generate the code via the back-end */
621   (*drcuml->beintf->be_generate)(drcuml->bestate, block, block->inst, block->nextinst);
622
623   /* block is no longer in use */
624   block->inuse = FALSE;
625}
626
627
628/*-------------------------------------------------
629    drcuml_block_abort - abort a code block in
630    progress
631-------------------------------------------------*/
632
633void drcuml_block_abort(drcuml_block *block)
634{
635   assert(block->inuse);
636
637   /* block is no longer in use */
638   block->inuse = FALSE;
639
640   /* unwind */
641   longjmp(*block->errorbuf, 1);
642}
643
644
645/*-------------------------------------------------
646    drcuml_hash_exists - return true if a hash
647    entry exists for the given mode/pc
648-------------------------------------------------*/
649
650int drcuml_hash_exists(drcuml_state *drcuml, UINT32 mode, UINT32 pc)
651{
652   return (*drcuml->beintf->be_hash_exists)(drcuml->bestate, mode, pc);
653}
654
655
656
657/***************************************************************************
658    CODE EXECUTION
659***************************************************************************/
660
661/*-------------------------------------------------
662    drcuml_execute - execute at the given PC/mode
663    for the specified cycles
664-------------------------------------------------*/
665
666int drcuml_execute(drcuml_state *drcuml, drcuml_codehandle *entry)
667{
668   return (*drcuml->beintf->be_execute)(drcuml->bestate, entry);
669}
670
671
672
673/***************************************************************************
674    CODE HANDLES
675***************************************************************************/
676
677/*-------------------------------------------------
678    drcuml_handle_alloc - allocate a new handle
679-------------------------------------------------*/
680
681drcuml_codehandle *drcuml_handle_alloc(drcuml_state *drcuml, const char *name)
682{
683   drcuml_codehandle *handle;
684   char *string;
685
686   /* allocate space for a copy of the string */
687   string = drccache_memory_alloc(drcuml->cache, strlen(name) + 1);
688   if (string == NULL)
689      return NULL;
690   strcpy(string, name);
691
692   /* allocate a new handle info */
693   handle = drccache_memory_alloc_near(drcuml->cache, sizeof(*handle));
694   if (handle == NULL)
695   {
696      drccache_memory_free(drcuml->cache, string, strlen(name) + 1);
697      return NULL;
698   }
699   memset(handle, 0, sizeof(*handle));
700
701   /* fill in the rest of the info and add to the list of handles */
702   handle->drcuml = drcuml;
703   handle->string = string;
704   handle->next = drcuml->handlelist;
705   drcuml->handlelist = handle;
706
707   return handle;
708}
709
710
711/*-------------------------------------------------
712    drcuml_handle_set_codeptr - set a codeptr
713    associated with a handle
714-------------------------------------------------*/
715
716void drcuml_handle_set_codeptr(drcuml_codehandle *handle, drccodeptr code)
717{
718   assert(handle->code == NULL);
719   assert_in_cache(handle->drcuml->cache, code);
720   handle->code = code;
721}
722
723
724/*-------------------------------------------------
725    drcuml_handle_codeptr - get the pointer from
726    a handle
727-------------------------------------------------*/
728
729drccodeptr drcuml_handle_codeptr(const drcuml_codehandle *handle)
730{
731   return handle->code;
732}
733
734
735/*-------------------------------------------------
736    drcuml_handle_codeptr_addr - get the address
737    of the pointer from a handle
738-------------------------------------------------*/
739
740drccodeptr *drcuml_handle_codeptr_addr(drcuml_codehandle *handle)
741{
742   return &handle->code;
743}
744
745
746/*-------------------------------------------------
747    drcuml_handle_name - get the name of a handle
748-------------------------------------------------*/
749
750const char *drcuml_handle_name(const drcuml_codehandle *handle)
751{
752   return handle->string;
753}
754
755
756
757/***************************************************************************
758    CODE LOGGING
759***************************************************************************/
760
761/*-------------------------------------------------
762    drcuml_log_printf - directly printf to the UML
763    log if generated
764-------------------------------------------------*/
765
766void drcuml_log_printf(drcuml_state *drcuml, const char *format, ...)
767{
768   /* if we have a file, print to it */
769   if (drcuml->umllog != NULL)
770   {
771      va_list va;
772
773      /* do the printf */
774      va_start(va, format);
775      vfprintf(drcuml->umllog, format, va);
776      va_end(va);
777   }
778}
779
780
781/*-------------------------------------------------
782    drcuml_add_comment - attach a comment to the
783    current output location in the specified block
784-------------------------------------------------*/
785
786void drcuml_add_comment(drcuml_block *block, const char *format, ...)
787{
788   char buffer[512];
789   char *comment;
790   va_list va;
791
792   assert(block->inuse);
793
794   /* do the printf */
795   va_start(va, format);
796   vsprintf(buffer, format, va);
797   va_end(va);
798
799   /* allocate space in the cache to hold the comment */
800   comment = drccache_memory_alloc_temporary(block->drcuml->cache, strlen(buffer) + 1);
801   if (comment == NULL)
802      return;
803   strcpy(comment, buffer);
804
805   /* add an instruction with a pointer */
806   drcuml_block_append_1(block, DRCUML_OP_COMMENT, 4, DRCUML_COND_ALWAYS, MEM(comment));
807}
808
809
810
811/***************************************************************************
812    OPCODE VALIDATION
813***************************************************************************/
814
815/*-------------------------------------------------
816    validate_instruction - verify that the
817    instruction created meets all requirements
818-------------------------------------------------*/
819
820#ifdef MAME_DEBUG
821static void validate_instruction(drcuml_block *block, const drcuml_instruction *inst)
822{
823   const drcuml_opcode_valid *opvalid = &opcode_valid_table[inst->opcode];
824
825   /* validate information */
826   assert(inst->opcode != DRCUML_OP_INVALID && inst->opcode < DRCUML_OP_MAX);
827   assert(inst->size == 1 || inst->size == 2 || inst->size == 4 || inst->size == 8);
828   assert((opvalid->sizes & inst->size) != 0);
829   if (inst->numparams > 0)
830   {
831      assert(inst->param[0].type > DRCUML_PTYPE_NONE && inst->param[0].type < DRCUML_PTYPE_MAX);
832      assert(((opvalid->p0types >> inst->param[0].type) & 1) != 0);
833      if (inst->param[0].type == DRCUML_PTYPE_MEMORY && !(opvalid->p0types & 0x80))
834         assert_in_near_cache(block->drcuml->cache, (void *)(FPTR)inst->param[0].value);
835   }
836   if (inst->numparams > 1)
837   {
838      assert(inst->param[1].type > DRCUML_PTYPE_NONE && inst->param[1].type < DRCUML_PTYPE_MAX);
839      assert(((opvalid->p1types >> inst->param[1].type) & 1) != 0);
840      if (inst->param[1].type == DRCUML_PTYPE_MEMORY && !(opvalid->p1types & 0x80))
841         assert_in_near_cache(block->drcuml->cache, (void *)(FPTR)inst->param[1].value);
842   }
843   if (inst->numparams > 2)
844   {
845      assert(inst->param[2].type > DRCUML_PTYPE_NONE && inst->param[2].type < DRCUML_PTYPE_MAX);
846      assert(((opvalid->p2types >> inst->param[2].type) & 1) != 0);
847      if (inst->param[2].type == DRCUML_PTYPE_MEMORY && !(opvalid->p2types & 0x80))
848         assert_in_near_cache(block->drcuml->cache, (void *)(FPTR)inst->param[2].value);
849   }
850   if (inst->numparams > 3)
851   {
852      assert(inst->param[3].type > DRCUML_PTYPE_NONE && inst->param[3].type < DRCUML_PTYPE_MAX);
853      assert(((opvalid->p3types >> inst->param[3].type) & 1) != 0);
854      if (inst->param[3].type == DRCUML_PTYPE_MEMORY && !(opvalid->p3types & 0x80))
855         assert_in_near_cache(block->drcuml->cache, (void *)(FPTR)inst->param[3].value);
856   }
857}
858#endif
src/emu/cpu/drcfe.h
r2627r2628
55    Generic dynamic recompiler frontend structures and utilities.
66
77    Copyright Aaron Giles
8    Released for general use under the MAME license
8    Released for general non-commercial use under the MAME license
99    Visit http://mamedev.org for licensing and usage restrictions.
1010
1111****************************************************************************
r2627r2628
3434#ifndef __DRCFE_H__
3535#define __DRCFE_H__
3636
37#ifdef PTR64
38#include "x64drc.h"
39#else
40#include "x86drc.h"
41#endif
4237
43
44
4538/***************************************************************************
4639    CONSTANTS
4740***************************************************************************/
r2627r2628
159152   drcfe_describe_func   describe;            /* callback to describe a single instruction */
160153};
161154
162/*
163    for each register:
164        callback to load
165        callback to store
166        pointer to memory
167    if (pointer != NULL)
168        - can return reference to memory for operations
169    else
170        - must always load/store from register
171*/
172155
173156
174157/***************************************************************************
src/emu/cpu/drcuml.h
r0r2628
1/***************************************************************************
2
3    drcuml.h
4
5    Universal machine language for dynamic recompiling CPU cores.
6
7    Copyright Aaron Giles
8    Released for general non-commercial use under the MAME license
9    Visit http://mamedev.org for licensing and usage restrictions.
10
11***************************************************************************/
12
13#pragma once
14
15#ifndef __DRCUML_H__
16#define __DRCUML_H__
17
18#include "memory.h"
19#include "drccache.h"
20#include <setjmp.h>
21
22
23/***************************************************************************
24    CONSTANTS
25***************************************************************************/
26
27/* these options are passed into drcuml_alloc() and control global behaviors */
28#define DRCUML_OPTION_USE_C               0x0001      /* always use the C back-end */
29#define DRCUML_OPTION_LOG_UML            0x0002      /* generate a UML disassembly of each block */
30#define DRCUML_OPTION_LOG_NATIVE         0x0004      /* tell the back-end to generate a native disassembly of each block */
31
32
33/* opcode parameter types */
34enum _drcuml_ptype
35{
36   DRCUML_PTYPE_NONE = 0,
37   DRCUML_PTYPE_IMMEDIATE,
38   DRCUML_PTYPE_INT_REGISTER,
39   DRCUML_PTYPE_FLOAT_REGISTER,
40   DRCUML_PTYPE_MAPVAR,
41   DRCUML_PTYPE_MEMORY,
42   DRCUML_PTYPE_MAX
43};
44typedef enum _drcuml_ptype drcuml_ptype;
45
46
47/* these define the registers for the UML */
48enum
49{
50   DRCUML_REG_INVALID = 0,   /* 0 is invalid */
51
52   /* integer registers */
53   DRCUML_REG_I0 = 0x400,
54   DRCUML_REG_I1,
55   DRCUML_REG_I2,
56   DRCUML_REG_I3,
57   DRCUML_REG_I4,
58   DRCUML_REG_I5,
59   DRCUML_REG_I6,
60   DRCUML_REG_I7,
61   DRCUML_REG_I8,
62   DRCUML_REG_I9,
63   DRCUML_REG_I_END,
64
65   /* floating point registers */
66   DRCUML_REG_F0 = 0x800,
67   DRCUML_REG_F1,
68   DRCUML_REG_F2,
69   DRCUML_REG_F3,
70   DRCUML_REG_F4,
71   DRCUML_REG_F5,
72   DRCUML_REG_F6,
73   DRCUML_REG_F7,
74   DRCUML_REG_F8,
75   DRCUML_REG_F9,
76   DRCUML_REG_F_END,
77
78   /* map variables */
79   DRCUML_MAPVAR_M0 = 0xc00,
80   DRCUML_MAPVAR_M1,
81   DRCUML_MAPVAR_M2,
82   DRCUML_MAPVAR_M3,
83   DRCUML_MAPVAR_M4,
84   DRCUML_MAPVAR_M5,
85   DRCUML_MAPVAR_M6,
86   DRCUML_MAPVAR_M7,
87   DRCUML_MAPVAR_M8,
88   DRCUML_MAPVAR_M9,
89   DRCUML_MAPVAR_END
90};
91
92
93/* UML flag definitions */
94enum
95{
96   DRCUML_FLAG_C = 1,               /* carry flag */
97   DRCUML_FLAG_V = 2,               /* overflow flag (defined for integer only) */
98   DRCUML_FLAG_Z = 4,               /* zero flag */
99   DRCUML_FLAG_S = 8,               /* sign flag (defined for integer only) */
100   DRCUML_FLAG_U = 16               /* unordered flag (defined for FP only) */
101};
102
103
104/* these define the conditions for the UML */
105/* note that these are defined such that (condition ^ 1) is always the opposite */
106/* they are also defined so as not to conflict with the flag bits above */
107enum
108{
109   DRCUML_COND_ALWAYS = 0,
110
111   DRCUML_COND_Z = 0x80,   /* requires Z */
112   DRCUML_COND_NZ,         /* requires Z */
113   DRCUML_COND_S,         /* requires S */
114   DRCUML_COND_NS,         /* requires S */
115   DRCUML_COND_C,         /* requires C */
116   DRCUML_COND_NC,         /* requires C */
117   DRCUML_COND_V,         /* requires V */
118   DRCUML_COND_NV,         /* requires V */
119   DRCUML_COND_U,         /* requires U */
120   DRCUML_COND_NU,         /* requires U */
121   DRCUML_COND_A,         /* requires CZ */
122   DRCUML_COND_BE,         /* requires CZ */
123   DRCUML_COND_G,         /* requires SVZ */
124   DRCUML_COND_LE,         /* requires SVZ */
125   DRCUML_COND_L,         /* requires SV */
126   DRCUML_COND_GE,         /* requires SV */
127
128   DRCUML_COND_MAX
129};
130
131
132/* basic condition code aliases */
133enum
134{
135   DRCUML_COND_E = DRCUML_COND_Z,
136   DRCUML_COND_NE = DRCUML_COND_NZ,
137   DRCUML_COND_B = DRCUML_COND_C,
138   DRCUML_COND_AE = DRCUML_COND_NC
139};
140
141
142/* floating point modes */
143enum
144{
145   DRCUML_FMOD_TRUNC,      /* truncate */
146   DRCUML_FMOD_ROUND,      /* round */
147   DRCUML_FMOD_CEIL,      /* round up */
148   DRCUML_FMOD_FLOOR      /* round down */
149};
150
151
152/* these define the opcodes for the UML */
153enum _drcuml_opcode
154{
155   DRCUML_OP_INVALID,
156
157   /* Compile-time opcodes */
158   DRCUML_OP_HANDLE,      /* HANDLE  handle                 */
159   DRCUML_OP_HASH,         /* HASH    mode,pc                */
160   DRCUML_OP_LABEL,      /* LABEL   imm                    */
161   DRCUML_OP_COMMENT,      /* COMMENT string                 */
162   DRCUML_OP_MAPVAR,      /* MAPVAR  mapvar,value           */
163
164   /* Control Flow Operations */
165   DRCUML_OP_DEBUG,      /* DEBUG   pc                     */
166   DRCUML_OP_EXIT,         /* EXIT    src1[,c]               */
167   DRCUML_OP_HASHJMP,      /* HASHJMP mode,pc,handle         */
168   DRCUML_OP_JMP,         /* JMP     imm[,c]                */
169   DRCUML_OP_EXH,         /* EXH     handle,param[,c]       */
170   DRCUML_OP_CALLH,      /* CALLH   handle[,c]             */
171   DRCUML_OP_RET,         /* RET     [c]                    */
172   DRCUML_OP_CALLC,      /* CALLC   func,ptr[,c]           */
173   DRCUML_OP_RECOVER,      /* RECOVER dst,mapvar             */
174
175   /* Internal Register Operations */
176   DRCUML_OP_SETFMOD,      /* SETFMOD src                    */
177   DRCUML_OP_GETFMOD,      /* GETFMOD dst                    */
178   DRCUML_OP_GETEXP,      /* GETEXP  dst,index              */
179
180   /* Integer Operations */
181   DRCUML_OP_LOAD1U,      /* LOAD1U  dst,base,index         */
182   DRCUML_OP_LOAD1S,      /* LOAD1S  dst,base,index         */
183   DRCUML_OP_LOAD2U,      /* LOAD2U  dst,base,index         */
184   DRCUML_OP_LOAD2S,      /* LOAD2S  dst,base,index         */
185   DRCUML_OP_LOAD4U,      /* LOAD4U  dst,base,index         */
186   DRCUML_OP_LOAD4S,      /* LOAD4S  dst,base,index         */
187   DRCUML_OP_LOAD8U,      /* LOAD8U  dst,base,index         */
188   DRCUML_OP_STORE1,      /* STORE1  base,index,src         */
189   DRCUML_OP_STORE2,      /* STORE2  base,index,src         */
190   DRCUML_OP_STORE4,      /* STORE4  base,index,src         */
191   DRCUML_OP_STORE8,      /* STORE8  base,index,src         */
192   DRCUML_OP_READ1U,      /* READ1U  dst,space,src1         */
193   DRCUML_OP_READ1S,      /* READ1S  dst,space,src1         */
194   DRCUML_OP_READ2U,      /* READ2U  dst,space,src1         */
195   DRCUML_OP_READ2S,      /* READ2S  dst,space,src1         */
196   DRCUML_OP_READ2M,      /* READ2M  dst,space,src1,mask    */
197   DRCUML_OP_READ4U,      /* READ4U  dst,space,src1         */
198   DRCUML_OP_READ4S,      /* READ4S  dst,space,src1         */
199   DRCUML_OP_READ4M,      /* READ4M  dst,space,src1,mask    */
200   DRCUML_OP_READ8U,      /* READ8U  dst,space,src1         */
201   DRCUML_OP_READ8M,      /* READ8M  dst,space,src1,mask    */
202   DRCUML_OP_WRITE1,      /* WRITE1  space,dst,src1         */
203   DRCUML_OP_WRITE2,      /* WRITE2  space,dst,src1         */
204   DRCUML_OP_WRIT2M,      /* WRIT2M  space,dst,mask,src1    */
205   DRCUML_OP_WRITE4,      /* WRITE4  space,dst,src1         */
206   DRCUML_OP_WRIT4M,      /* WRIT4M  space,dst,mask,src1    */
207   DRCUML_OP_WRITE8,      /* WRITE8  space,dst,src1         */
208   DRCUML_OP_WRIT8M,      /* WRIT8M  space,dst,mask,src1    */
209   DRCUML_OP_FLAGS,      /* FLAGS   dst,mask,table         */
210   DRCUML_OP_MOV,         /* MOV     dst,src[,c]            */
211   DRCUML_OP_ZEXT1,      /* ZEXT1   dst,src                */
212   DRCUML_OP_ZEXT2,      /* ZEXT2   dst,src                */
213   DRCUML_OP_ZEXT4,      /* ZEXT4   dst,src                */
214   DRCUML_OP_SEXT1,      /* SEXT1   dst,src                */
215   DRCUML_OP_SEXT2,      /* SEXT2   dst,src                */
216   DRCUML_OP_SEXT4,      /* SEXT4   dst,src                */
217   DRCUML_OP_ADD,         /* ADD     dst,src1,src2[,f]      */
218   DRCUML_OP_ADDC,         /* ADDC    dst,src1,src2[,f]      */
219   DRCUML_OP_SUB,         /* SUB     dst,src1,src2[,f]      */
220   DRCUML_OP_SUBB,         /* SUBB    dst,src1,src2[,f]      */
221   DRCUML_OP_CMP,         /* CMP     src1,src2[,f]          */
222   DRCUML_OP_MULU,         /* MULU    dst,edst,src1,src2[,f] */
223   DRCUML_OP_MULS,         /* MULS    dst,edst,src1,src2[,f] */
224   DRCUML_OP_DIVU,         /* DIVU    dst,edst,src1,src2[,f] */
225   DRCUML_OP_DIVS,         /* DIVS    dst,edst,src1,src2[,f] */
226   DRCUML_OP_AND,         /* AND     dst,src1,src2[,f]      */
227   DRCUML_OP_TEST,         /* TEST    src1,src2[,f]          */
228   DRCUML_OP_OR,         /* OR      dst,src1,src2[,f]      */
229   DRCUML_OP_XOR,         /* XOR     dst,src1,src2[,f]      */
230   DRCUML_OP_SHL,         /* SHL     dst,src,count[,f]      */
231   DRCUML_OP_SHR,         /* SHR     dst,src,count[,f]      */
232   DRCUML_OP_SAR,         /* SAR     dst,src,count[,f]      */
233   DRCUML_OP_ROL,         /* ROL     dst,src,count[,f]      */
234   DRCUML_OP_ROLC,         /* ROLC    dst,src,count[,f]      */
235   DRCUML_OP_ROR,         /* ROL     dst,src,count[,f]      */
236   DRCUML_OP_RORC,         /* ROLC    dst,src,count[,f]      */
237
238   /* Floating Point Operations */
239   DRCUML_OP_FLOAD,      /* FLOAD   dst,base,index         */
240   DRCUML_OP_FSTORE,      /* FSTORE  base,index,src         */
241   DRCUML_OP_FREAD,      /* FREAD   dst,space,src1         */
242   DRCUML_OP_FWRITE,      /* FWRITE  space,dst,src1         */
243   DRCUML_OP_FMOV,         /* FSMOV   dst,src1[,c]           */
244   DRCUML_OP_FTOI4,      /* FTOI4   dst,src1               */
245   DRCUML_OP_FTOI4T,      /* FTOI4T  dst,src1               */
246   DRCUML_OP_FTOI4R,      /* FTOI4R  dst,src1               */
247   DRCUML_OP_FTOI4F,      /* FTOI4F  dst,src1               */
248   DRCUML_OP_FTOI4C,      /* FTOI4C  dst,src1               */
249   DRCUML_OP_FTOI8,      /* FTOI8   dst,src1               */
250   DRCUML_OP_FTOI8T,      /* FTOI8T  dst,src1               */
251   DRCUML_OP_FTOI8R,      /* FTOI8R  dst,src1               */
252   DRCUML_OP_FTOI8F,      /* FTOI8F  dst,src1               */
253   DRCUML_OP_FTOI8C,      /* FTOI8C  dst,src1               */
254   DRCUML_OP_FFRFS,      /* FFRFS   dst,src1               */
255   DRCUML_OP_FFRFD,      /* FFRFD   dst,src1               */
256   DRCUML_OP_FFRI4,      /* FFRI4   dst,src1               */
257   DRCUML_OP_FFRI8,      /* FFRI8   dst,src1               */
258   DRCUML_OP_FADD,         /* FADD    dst,src1,src2          */
259   DRCUML_OP_FSUB,         /* FSUB    dst,src1,src2          */
260   DRCUML_OP_FCMP,         /* FCMP    src1,src2              */
261   DRCUML_OP_FMUL,         /* FMUL    dst,src1,src2          */
262   DRCUML_OP_FDIV,         /* FDIV    dst,src1,src2          */
263   DRCUML_OP_FNEG,         /* FNEG    dst,src1               */
264   DRCUML_OP_FABS,         /* FABS    dst,src1               */
265   DRCUML_OP_FSQRT,      /* FSQRT   dst,src1               */
266   DRCUML_OP_FRECIP,      /* FRECIP  dst,src1               */
267   DRCUML_OP_FRSQRT,      /* FRSQRT  dst,src1               */
268
269   DRCUML_OP_MAX
270};
271typedef enum _drcuml_opcode drcuml_opcode;
272
273
274
275/***************************************************************************
276    TYPE DEFINITIONS
277***************************************************************************/
278
279/* represents a reference to a local label in the code */
280typedef UINT32 drcuml_codelabel;
281
282/* represents the value of an opcode parameter */
283typedef UINT64 drcuml_pvalue;
284
285
286/* opaque structure describing UML generation state */
287typedef struct _drcuml_state drcuml_state;
288
289/* opaque structure describing UML codegen block */
290typedef struct _drcuml_block drcuml_block;
291
292/* opaque structure describing back-end state */
293typedef struct _drcbe_state drcbe_state;
294
295/* opaque structure describing a code handle */
296typedef struct _drcuml_codehandle drcuml_codehandle;
297
298
299/* a parameter for a UML instructon is encoded like this */
300typedef struct _drcuml_parameter drcuml_parameter;
301struct _drcuml_parameter
302{
303   drcuml_ptype    type;            /* parameter type */
304   drcuml_pvalue   value;            /* parameter value */
305};
306
307
308/* a single UML instructon is encoded like this */
309typedef struct _drcuml_instruction drcuml_instruction;
310struct _drcuml_instruction
311{
312   drcuml_opcode   opcode;            /* opcode */
313   UINT8         condflags;         /* condition or flags */
314   UINT8         size;            /* operation size */
315   UINT8         numparams;         /* number of parameters */
316   drcuml_parameter param[4];         /* up to 4 parameters */
317};
318
319
320/* typedefs for back-end callback functions */
321typedef drcbe_state *(*drcbe_alloc_func)(drcuml_state *drcuml, drccache *cache, UINT32 flags, int modes, int addrbits, int ignorebits);
322typedef void (*drcbe_free_func)(drcbe_state *state);
323typedef void (*drcbe_reset_func)(drcbe_state *state);
324typedef int   (*drcbe_execute_func)(drcbe_state *state, drcuml_codehandle *entry);
325typedef void (*drcbe_generate_func)(drcbe_state *state, drcuml_block *block, const drcuml_instruction *instlist, UINT32 numinst);
326typedef int (*drcbe_hash_exists)(drcbe_state *state, UINT32 mode, UINT32 pc);
327
328
329/* interface structure for a back-end */
330typedef struct _drcbe_interface drcbe_interface;
331struct _drcbe_interface
332{
333   drcbe_alloc_func   be_alloc;
334   drcbe_free_func      be_free;
335   drcbe_reset_func   be_reset;
336   drcbe_execute_func   be_execute;
337   drcbe_generate_func   be_generate;
338   drcbe_hash_exists   be_hash_exists;
339};
340
341
342
343/***************************************************************************
344    FUNCTION PROTOTYPES
345***************************************************************************/
346
347/* ----- initialization/teardown ----- */
348
349/* allocate state for the code generator and initialize the back-end */
350drcuml_state *drcuml_alloc(drccache *cache, UINT32 flags, int modes, int addrbits, int ignorebits);
351
352/* reset the state completely, flushing the cache and all information */
353void drcuml_reset(drcuml_state *drcuml);
354
355/* free state for the code generator and the back-end */
356void drcuml_free(drcuml_state *drcuml);
357
358
359
360/* ----- code block generation ----- */
361
362/* begin a new code block */
363drcuml_block *drcuml_block_begin(drcuml_state *drcuml, UINT32 maxinst, jmp_buf *errorbuf);
364
365/* append an opcode to the block, with 0-4 parameters */
366void drcuml_block_append_0(drcuml_block *block, drcuml_opcode op, UINT8 size, UINT8 condflags);
367void drcuml_block_append_1(drcuml_block *block, drcuml_opcode op, UINT8 size, UINT8 condflags, drcuml_ptype p0type, drcuml_pvalue p0value);
368void drcuml_block_append_2(drcuml_block *block, drcuml_opcode op, UINT8 size, UINT8 condflags, drcuml_ptype p0type, drcuml_pvalue p0value, drcuml_ptype p1type, drcuml_pvalue p1value);
369void drcuml_block_append_3(drcuml_block *block, drcuml_opcode op, UINT8 size, UINT8 condflags, drcuml_ptype p0type, drcuml_pvalue p0value, drcuml_ptype p1type, drcuml_pvalue p1value, drcuml_ptype p2type, drcuml_pvalue p2value);
370void drcuml_block_append_4(drcuml_block *block, drcuml_opcode op, UINT8 size, UINT8 condflags, drcuml_ptype p0type, drcuml_pvalue p0value, drcuml_ptype p1type, drcuml_pvalue p1value, drcuml_ptype p2type, drcuml_pvalue p2value, drcuml_ptype p3type, drcuml_pvalue p3value);
371
372/* complete a code block and commit it to the cache via the back-end */
373void drcuml_block_end(drcuml_block *block);
374
375/* abort a code block in progress due to cache allocation failure */
376void drcuml_block_abort(drcuml_block *block);
377
378/* return true if a hash entry exists for the given mode/pc */
379int drcuml_hash_exists(drcuml_state *drcuml, UINT32 mode, UINT32 pc);
380
381
382
383/* ----- code execution ----- */
384
385/* execute at the given PC/mode for the specified cycles */
386int drcuml_execute(drcuml_state *drcuml, drcuml_codehandle *entry);
387
388
389
390/* ----- code handles ----- */
391
392/* allocate a new handle */
393drcuml_codehandle *drcuml_handle_alloc(drcuml_state *drcuml, const char *name);
394
395/* set the handle pointer */
396void drcuml_handle_set_codeptr(drcuml_codehandle *handle, drccodeptr code);
397
398/* get the pointer from a handle */
399drccodeptr drcuml_handle_codeptr(const drcuml_codehandle *handle);
400
401/* get the address of the pointer from a handle */
402drccodeptr *drcuml_handle_codeptr_addr(drcuml_codehandle *handle);
403
404/* get the name of a handle */
405const char *drcuml_handle_name(const drcuml_codehandle *handle);
406
407
408
409/* ----- code logging ----- */
410
411/* directly printf to the UML log if generated */
412void drcuml_log_printf(drcuml_state *state, const char *format, ...) ATTR_PRINTF(2,3);
413
414/* attach a comment to the current output location in the specified block */
415void drcuml_add_comment(drcuml_block *block, const char *format, ...) ATTR_PRINTF(2,3);
416
417
418#endif
src/emu/cpu/drcbex64.c
r0r2628
1/***************************************************************************
2
3    drcbex64.c
4
5    64-bit x64 back-end for the universal machine language.
6
7    Copyright Aaron Giles
8    Released for general non-commercial use under the MAME license
9    Visit http://mamedev.org for licensing and usage restrictions.
10
11****************************************************************************
12
13    Future improvements/changes:
14
15    * Add support for FP registers
16
17    * Optimize to avoid unnecessary reloads
18
19    * Identify common pairs and optimize output
20
21****************************************************************************
22
23    -------------------------
24    ABI/conventions (Windows)
25    -------------------------
26
27    Registers:
28        RAX        - volatile, function return value
29        RBX        - non-volatile
30        RCX        - volatile, integer function parameter 1
31        RDX        - volatile, integer function parameter 2
32        RSI        - non-volatile
33        RDI        - non-volatile
34        RBP        - non-volatile
35        R8         - volatile, integer function parameter 3
36        R9         - volatile, integer function parameter 4
37        R10        - volatile
38        R11        - volatile, scratch immediate storage
39        R12        - non-volatile
40        R13        - non-volatile
41        R14        - non-volatile
42        R15        - non-volatile
43
44        XMM0       - volatile, FP function parameter 1
45        XMM1       - volatile, FP function parameter 2
46        XMM2       - volatile, FP function parameter 3
47        XMM3       - volatile, FP function parameter 4
48        XMM4       - volatile
49        XMM5       - volatile
50        XMM6       - non-volatile
51        XMM7       - non-volatile
52        XMM8       - non-volatile
53        XMM9       - non-volatile
54        XMM10      - non-volatile
55        XMM11      - non-volatile
56        XMM12      - non-volatile
57        XMM13      - non-volatile
58        XMM14      - non-volatile
59        XMM15      - non-volatile
60
61
62    -----------------------------
63    ABI/conventions (Linux/MacOS)
64    -----------------------------
65
66    Registers:
67        RAX        - volatile, function return value
68        RBX        - non-volatile
69        RCX        - volatile, integer function parameter 4
70        RDX        - volatile, integer function parameter 3
71        RSI        - volatile, integer function parameter 2
72        RDI        - volatile, integer function parameter 1
73        RBP        - non-volatile
74        R8         - volatile, integer function parameter 5
75        R9         - volatile, integer function parameter 6
76        R10        - volatile
77        R11        - volatile, scratch immediate storage
78        R12        - non-volatile
79        R13        - non-volatile
80        R14        - non-volatile
81        R15        - non-volatile
82
83        XMM0       - volatile, FP function parameter 1
84        XMM1       - volatile, FP function parameter 2
85        XMM2       - volatile, FP function parameter 3
86        XMM3       - volatile, FP function parameter 4
87        XMM4       - volatile
88        XMM5       - volatile
89        XMM6       - volatile
90        XMM7       - volatile
91        XMM8       - volatile
92        XMM9       - volatile
93        XMM10      - volatile
94        XMM11      - volatile
95        XMM12      - volatile
96        XMM13      - volatile
97        XMM14      - volatile
98        XMM15      - volatile
99
100
101    ---------------
102    Execution model
103    ---------------
104
105    Registers (Windows):
106        RAX        - scratch register
107        RBX        - maps to I0
108        RCX        - scratch register
109        RDX        - scratch register
110        RSI        - maps to I1
111        RDI        - maps to I2
112        RBP        - pointer to code cache
113        R8         - scratch register
114        R9         - scratch register
115        R10        - scratch register
116        R11        - scratch register
117        R12        - maps to I3
118        R13        - maps to I4
119        R14        - maps to I5
120        R15        - maps to I6
121
122    Registers (Linux/MacOS):
123        RAX        - scratch register
124        RBX        - maps to I0
125        RCX        - scratch register
126        RDX        - scratch register
127        RSI        - unused
128        RDI        - unused
129        RBP        - pointer to code cache
130        R8         - scratch register
131        R9         - scratch register
132        R10        - scratch register
133        R11        - scratch register
134        R12        - maps to I1
135        R13        - maps to I2
136        R14        - maps to I3
137        R15        - maps to I4
138
139    Entry point:
140        Assumes 1 parameter passed, which is the codeptr of the code
141        to execute once the environment is set up.
142
143    Exit point:
144        Assumes exit value is in RAX.
145
146    Entry stack:
147        [rsp]      - return
148
149    Runtime stack:
150        [rsp]      - r9 home
151        [rsp+8]    - r8 home
152        [rsp+16]   - rdx home
153        [rsp+24]   - rcx home
154        [rsp+40]   - saved r15
155        [rsp+48]   - saved r14
156        [rsp+56]   - saved r13
157        [rsp+64]   - saved r12
158        [rsp+72]   - saved ebp
159        [rsp+80]   - saved edi
160        [rsp+88]   - saved esi
161        [rsp+96]   - saved ebx
162        [rsp+104]  - ret
163
164***************************************************************************/
165
166#include "drcuml.h"
167#include "drcumld.h"
168#include "drcbeut.h"
169#include "debugger.h"
170#include "x86emit.h"
171#undef REG_SP
172#include "x86log.h"
173#include <math.h>
174
175
176
177/***************************************************************************
178    DEBUGGING
179***************************************************************************/
180
181#define LOG_HASHJMPS      (0)
182
183
184
185/***************************************************************************
186    CONSTANTS
187***************************************************************************/
188
189#define PTYPE_M            (1 << DRCUML_PTYPE_MEMORY)
190#define PTYPE_I            (1 << DRCUML_PTYPE_IMMEDIATE)
191#define PTYPE_R            (1 << DRCUML_PTYPE_INT_REGISTER)
192#define PTYPE_F            (1 << DRCUML_PTYPE_FLOAT_REGISTER)
193#define PTYPE_MI         (PTYPE_M | PTYPE_I)
194#define PTYPE_RI         (PTYPE_R | PTYPE_I)
195#define PTYPE_MR         (PTYPE_M | PTYPE_R)
196#define PTYPE_MRI         (PTYPE_M | PTYPE_R | PTYPE_I)
197#define PTYPE_MF         (PTYPE_M | PTYPE_F)
198
199#ifdef X64_WINDOWS_ABI
200
201#define REG_PARAM1         REG_RCX
202#define REG_PARAM2         REG_RDX
203#define REG_PARAM3         REG_R8
204#define REG_PARAM4         REG_R9
205
206#else
207
208#define REG_PARAM1         REG_RDI
209#define REG_PARAM2         REG_RSI
210#define REG_PARAM3         REG_RDX
211#define REG_PARAM4         REG_RCX
212
213#endif
214
215
216
217/***************************************************************************
218    MACROS
219***************************************************************************/
220
221#define X86_CONDITION(condflags)      (condition_map[condflags - DRCUML_COND_Z])
222#define X86_NOT_CONDITION(condflags)   (condition_map[condflags - DRCUML_COND_Z] ^ 1)
223
224#undef MABS
225#define MABS(drcbe, ptr)   MBD(REG_RBP, offset_from_rbp(drcbe, (FPTR)(ptr)))
226
227
228
229/***************************************************************************
230    TYPE DEFINITIONS
231***************************************************************************/
232
233/* entry point */
234typedef UINT32 (*x86_entry_point_func)(UINT8 *rbpvalue, x86code *entry);
235
236/* opcode handler */
237typedef x86code *(*opcode_generate_func)(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
238
239
240/* opcode table entry */
241typedef struct _opcode_table_entry opcode_table_entry;
242struct _opcode_table_entry
243{
244   drcuml_opcode         opcode;               /* opcode in question */
245   opcode_generate_func   func;               /* function pointer to the work */
246};
247
248
249/* internal backend-specific state */
250struct _drcbe_state
251{
252   drcuml_state *         drcuml;               /* pointer back to our owner */
253   drccache *            cache;               /* pointer to the cache */
254   drcuml_machine_state    state;               /* state of the machine */
255   drchash_state *         hash;               /* hash table state */
256   drcmap_state *         map;               /* code map */
257   drclabel_list *         labels;                 /* label list */
258
259   x86_entry_point_func   entry;               /* entry point */
260   x86code *            exit;               /* exit point */
261   x86code *            nocode;               /* nocode handler */
262
263   x86code *            mame_debug_hook;      /* debugger callback */
264   x86code *            debug_log_hashjmp;      /* hashjmp debugging */
265   x86code *            drcmap_get_value;      /* map lookup helper */
266   data_accessors         accessors[ADDRESS_SPACES];/* memory accessors */
267
268   UINT32               ssemode;            /* saved SSE mode */
269   UINT32               ssemodesave;         /* temporary location for saving */
270   UINT32               ssecontrol[4];         /* copy of the sse_control array */
271   UINT32 *            absmask32;            /* absolute value mask (32-bit) */
272   UINT64 *            absmask64;            /* absolute value mask (32-bit) */
273
274   void *               stacksave;            /* saved stack pointer */
275   void *               hashstacksave;         /* saved stack pointer for hashjmp */
276
277   UINT8 *               rbpvalue;            /* value of RBP */
278   UINT8               flagsmap[0x1000];      /* flags map */
279
280   x86log_context *      log;               /* logging */
281};
282
283
284
285/***************************************************************************
286    FUNCTION PROTOTYPES
287***************************************************************************/
288
289/* primary back-end callbacks */
290static drcbe_state *drcbex64_alloc(drcuml_state *drcuml, drccache *cache, UINT32 flags, int modes, int addrbits, int ignorebits);
291static void drcbex64_free(drcbe_state *drcbe);
292static void drcbex64_reset(drcbe_state *drcbe);
293static int drcbex64_execute(drcbe_state *drcbe, drcuml_codehandle *entry);
294static void drcbex64_generate(drcbe_state *drcbe, drcuml_block *block, const drcuml_instruction *instlist, UINT32 numinst);
295static int drcbex64_hash_exists(drcbe_state *drcbe, UINT32 mode, UINT32 pc);
296
297/* private helper functions */
298static void fixup_label(void *parameter, drccodeptr labelcodeptr);
299
300
301
302/***************************************************************************
303    GLOBAL VARIABLES
304***************************************************************************/
305
306/* globally-accessible interface to the backend */
307const drcbe_interface drcbe_x64_be_interface =
308{
309   drcbex64_alloc,
310   drcbex64_free,
311   drcbex64_reset,
312   drcbex64_execute,
313   drcbex64_generate,
314   drcbex64_hash_exists
315};
316
317/* opcode table */
318static opcode_generate_func opcode_table[DRCUML_OP_MAX];
319
320/* size-to-mask table */
321static const UINT64 size_to_mask[] = { 0, 0xff, 0xffff, 0, 0xffffffff, 0, 0, 0, U64(0xffffffffffffffff) };
322
323/* register mapping tables */
324static UINT8 int_register_map[DRCUML_REG_I_END - DRCUML_REG_I0] =
325{
326#ifdef X64_WINDOWS_ABI
327   REG_RBX, REG_RSI, REG_RDI, REG_R12, REG_R13, REG_R14, REG_R15
328#else
329   REG_RBX, REG_R12, REG_R13, REG_R14, REG_R15
330#endif
331};
332
333static UINT8 float_register_map[DRCUML_REG_F_END - DRCUML_REG_F0] =
334{
335   0
336};
337
338/* condition mapping table */
339static UINT8 condition_map[DRCUML_COND_MAX - DRCUML_COND_Z] =
340{
341   COND_Z,      /* DRCUML_COND_Z = 0x80,    requires Z */
342   COND_NZ,   /* DRCUML_COND_NZ,          requires Z */
343   COND_S,      /* DRCUML_COND_S,           requires S */
344   COND_NS,   /* DRCUML_COND_NS,          requires S */
345   COND_C,      /* DRCUML_COND_C,           requires C */
346   COND_NC,   /* DRCUML_COND_NC,          requires C */
347   COND_O,      /* DRCUML_COND_V,           requires V */
348   COND_NO,   /* DRCUML_COND_NV,          requires V */
349   COND_P,      /* DRCUML_COND_U,           requires U */
350   COND_NP,   /* DRCUML_COND_NU,          requires U */
351   COND_A,      /* DRCUML_COND_A,           requires CZ */
352   COND_BE,   /* DRCUML_COND_BE,          requires CZ */
353   COND_G,      /* DRCUML_COND_G,           requires SVZ */
354   COND_LE,   /* DRCUML_COND_LE,          requires SVZ */
355   COND_L,      /* DRCUML_COND_L,           requires SV */
356   COND_GE,   /* DRCUML_COND_GE,          requires SV */
357};
358
359
360
361/***************************************************************************
362    TABLES
363***************************************************************************/
364
365static x86code *op_handle(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
366static x86code *op_hash(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
367static x86code *op_label(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
368static x86code *op_comment(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
369static x86code *op_mapvar(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
370
371static x86code *op_debug(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
372static x86code *op_exit(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
373static x86code *op_hashjmp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
374static x86code *op_jmp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
375static x86code *op_exh(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
376static x86code *op_callh(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
377static x86code *op_ret(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
378static x86code *op_callc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
379static x86code *op_recover(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
380
381static x86code *op_setfmod(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
382static x86code *op_getfmod(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
383static x86code *op_getexp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
384
385static x86code *op_load1u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
386static x86code *op_load1s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
387static x86code *op_load2u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
388static x86code *op_load2s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
389static x86code *op_load4u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
390static x86code *op_load4s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
391static x86code *op_load8u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
392static x86code *op_store1(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
393static x86code *op_store2(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
394static x86code *op_store4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
395static x86code *op_store8(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
396static x86code *op_read1u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
397static x86code *op_read1s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
398static x86code *op_read2u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
399static x86code *op_read2s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
400static x86code *op_read2m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
401static x86code *op_read4u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
402static x86code *op_read4s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
403static x86code *op_read4m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
404static x86code *op_read8u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
405static x86code *op_read8m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
406static x86code *op_write1(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
407static x86code *op_write2(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
408static x86code *op_writ2m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
409static x86code *op_write4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
410static x86code *op_writ4m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
411static x86code *op_write8(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
412static x86code *op_writ8m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
413static x86code *op_flags(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
414static x86code *op_mov(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
415static x86code *op_zext1(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
416static x86code *op_zext2(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
417static x86code *op_zext4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
418static x86code *op_sext1(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
419static x86code *op_sext2(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
420static x86code *op_sext4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
421static x86code *op_add(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
422static x86code *op_addc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
423static x86code *op_sub(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
424static x86code *op_subc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
425static x86code *op_cmp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
426static x86code *op_mulu(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
427static x86code *op_muls(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
428static x86code *op_divu(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
429static x86code *op_divs(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
430static x86code *op_and(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
431static x86code *op_test(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
432static x86code *op_or(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
433static x86code *op_xor(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
434static x86code *op_shl(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
435static x86code *op_shr(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
436static x86code *op_sar(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
437static x86code *op_ror(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
438static x86code *op_rol(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
439static x86code *op_rorc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
440static x86code *op_rolc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
441
442static x86code *op_fload(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
443static x86code *op_fstore(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
444static x86code *op_fread(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
445static x86code *op_fwrite(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
446static x86code *op_fmov(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
447static x86code *op_ftoi4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
448static x86code *op_ftoi4t(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
449static x86code *op_ftoi4r(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
450static x86code *op_ftoi4f(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
451static x86code *op_ftoi4c(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
452static x86code *op_ftoi8(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
453static x86code *op_ftoi8t(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
454static x86code *op_ftoi8r(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
455static x86code *op_ftoi8f(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
456static x86code *op_ftoi8c(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
457static x86code *op_ffrfs(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
458static x86code *op_ffrfd(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
459static x86code *op_ffri4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
460static x86code *op_ffri8(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
461static x86code *op_fadd(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
462static x86code *op_fsub(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
463static x86code *op_fcmp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
464static x86code *op_fmul(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
465static x86code *op_fdiv(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
466static x86code *op_fneg(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
467static x86code *op_fabs(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
468static x86code *op_fsqrt(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
469static x86code *op_frecip(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
470static x86code *op_frsqrt(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
471
472static const opcode_table_entry opcode_table_source[] =
473{
474   /* Compile-time opcodes */
475   { DRCUML_OP_HANDLE,    op_handle },   /* HANDLE  handle                 */
476   { DRCUML_OP_HASH,    op_hash },      /* HASH    mode,pc                */
477   { DRCUML_OP_LABEL,   op_label },   /* LABEL   imm                    */
478   { DRCUML_OP_COMMENT, op_comment },   /* COMMENT string                 */
479   { DRCUML_OP_MAPVAR,  op_mapvar },   /* MAPVAR  mapvar,value           */
480
481   /* Control Flow Operations */
482   { DRCUML_OP_DEBUG,   op_debug },   /* DEBUG   pc                     */
483   { DRCUML_OP_EXIT,    op_exit },      /* EXIT    src1[,c]               */
484   { DRCUML_OP_HASHJMP, op_hashjmp },   /* HASHJMP mode,pc,handle         */
485   { DRCUML_OP_JMP,     op_jmp },      /* JMP     imm[,c]                */
486   { DRCUML_OP_EXH,     op_exh },      /* EXH     handle,param[,c]       */
487   { DRCUML_OP_CALLH,   op_callh },   /* CALLH   handle[,c]             */
488   { DRCUML_OP_RET,     op_ret },      /* RET     [c]                    */
489   { DRCUML_OP_CALLC,   op_callc },   /* CALLC   func,ptr[,c]           */
490   { DRCUML_OP_RECOVER, op_recover },   /* RECOVER dst,mapvar             */
491
492   /* Internal Register Operations */
493   { DRCUML_OP_SETFMOD, op_setfmod },   /* SETFMOD src                    */
494   { DRCUML_OP_GETFMOD, op_getfmod },   /* GETFMOD dst                    */
495   { DRCUML_OP_GETEXP,  op_getexp },   /* GETEXP  dst,index              */
496
497   /* Integer Operations */
498   { DRCUML_OP_LOAD1U,  op_load1u },   /* LOAD1U  dst,base,index         */
499   { DRCUML_OP_LOAD1S,  op_load1s },   /* LOAD1S  dst,base,index         */
500   { DRCUML_OP_LOAD2U,  op_load2u },   /* LOAD2U  dst,base,index         */
501   { DRCUML_OP_LOAD2S,  op_load2s },   /* LOAD2S  dst,base,index         */
502   { DRCUML_OP_LOAD4U,  op_load4u },   /* LOAD4U  dst,base,index         */
503   { DRCUML_OP_LOAD4S,  op_load4s },   /* LOAD4S  dst,base,index         */
504   { DRCUML_OP_LOAD8U,  op_load8u },   /* LOAD8U  dst,base,index         */
505   { DRCUML_OP_STORE1,  op_store1 },   /* STORE1  base,index,src         */
506   { DRCUML_OP_STORE2,  op_store2 },   /* STORE2  base,index,src         */
507   { DRCUML_OP_STORE4,  op_store4 },   /* STORE4  base,index,src         */
508   { DRCUML_OP_STORE8,  op_store8 },   /* STORE8  base,index,src         */
509   { DRCUML_OP_READ1U,  op_read1u },   /* READ1U  dst,space,src1         */
510   { DRCUML_OP_READ1S,  op_read1s },   /* READ1S  dst,space,src1         */
511   { DRCUML_OP_READ2U,  op_read2u },   /* READ2U  dst,space,src1         */
512   { DRCUML_OP_READ2S,  op_read2s },   /* READ2S  dst,space,src1         */
513   { DRCUML_OP_READ2M,  op_read2m },   /* READ2M  dst,space,src1,mask    */
514   { DRCUML_OP_READ4U,  op_read4u },   /* READ4U  dst,space,src1         */
515   { DRCUML_OP_READ4S,  op_read4s },   /* READ4S  dst,space,src1         */
516   { DRCUML_OP_READ4M,  op_read4m },   /* READ4M  dst,space,src1,mask    */
517   { DRCUML_OP_READ8U,  op_read8u },   /* READ8U  dst,space,src1         */
518   { DRCUML_OP_READ8M,  op_read8m },   /* READ8M  dst,space,src1,mask    */
519   { DRCUML_OP_WRITE1,  op_write1 },   /* WRITE1  space,dst,src1         */
520   { DRCUML_OP_WRITE2,  op_write2 },   /* WRITE2  space,dst,src1         */
521   { DRCUML_OP_WRIT2M,  op_writ2m },   /* WRIT2M  space,dst,src1         */
522   { DRCUML_OP_WRITE4,  op_write4 },   /* WRITE4  space,dst,src1         */
523   { DRCUML_OP_WRIT4M,  op_writ4m },   /* WRIT4M  space,dst,mask,src1    */
524   { DRCUML_OP_WRITE8,  op_write8 },   /* WRITE8  space,dst,src1         */
525   { DRCUML_OP_WRIT8M,  op_writ8m },   /* WRIT8M  space,dst,mask,src1    */
526   { DRCUML_OP_FLAGS,   op_flags },   /* FLAGS   dst,mask,table         */
527   { DRCUML_OP_MOV,     op_mov },      /* MOV     dst,src[,c]            */
528   { DRCUML_OP_ZEXT1,   op_zext1 },   /* ZEXT1   dst,src                */
529   { DRCUML_OP_ZEXT2,   op_zext2 },   /* ZEXT2   dst,src                */
530   { DRCUML_OP_ZEXT4,   op_zext4 },   /* ZEXT4   dst,src                */
531   { DRCUML_OP_SEXT1,   op_sext1 },   /* SEXT1   dst,src                */
532   { DRCUML_OP_SEXT2,   op_sext2 },   /* SEXT2   dst,src                */
533   { DRCUML_OP_SEXT4,   op_sext4 },   /* SEXT4   dst,src                */
534   { DRCUML_OP_ADD,     op_add },      /* ADD     dst,src1,src2[,f]      */
535   { DRCUML_OP_ADDC,    op_addc },      /* ADDC    dst,src1,src2[,f]      */
536   { DRCUML_OP_SUB,     op_sub },      /* SUB     dst,src1,src2[,f]      */
537   { DRCUML_OP_SUBB,    op_subc },      /* SUBB    dst,src1,src2[,f]      */
538   { DRCUML_OP_CMP,     op_cmp },      /* CMP     src1,src2[,f]          */
539   { DRCUML_OP_MULU,    op_mulu },      /* MULU    dst,edst,src1,src2[,f] */
540   { DRCUML_OP_MULS,    op_muls },      /* MULS    dst,edst,src1,src2[,f] */
541   { DRCUML_OP_DIVU,    op_divu },      /* DIVU    dst,edst,src1,src2[,f] */
542   { DRCUML_OP_DIVS,    op_divs },      /* DIVS    dst,edst,src1,src2[,f] */
543   { DRCUML_OP_AND,     op_and },      /* AND     dst,src1,src2[,f]      */
544   { DRCUML_OP_TEST,    op_test },      /* TEST    src1,src2[,f]          */
545   { DRCUML_OP_OR,      op_or },      /* OR      dst,src1,src2[,f]      */
546   { DRCUML_OP_XOR,     op_xor },      /* XOR     dst,src1,src2[,f]      */
547   { DRCUML_OP_SHL,     op_shl },      /* SHL     dst,src,count[,f]      */
548   { DRCUML_OP_SHR,     op_shr },      /* SHR     dst,src,count[,f]      */
549   { DRCUML_OP_SAR,     op_sar },      /* SAR     dst,src,count[,f]      */
550   { DRCUML_OP_ROL,     op_rol },      /* ROL     dst,src,count[,f]      */
551   { DRCUML_OP_ROLC,    op_rolc },      /* ROLC    dst,src,count[,f]      */
552   { DRCUML_OP_ROR,     op_ror },      /* ROR     dst,src,count[,f]      */
553   { DRCUML_OP_RORC,    op_rorc },      /* RORC    dst,src,count[,f]      */
554
555   /* Floating Point Operations */
556   { DRCUML_OP_FLOAD,   op_fload },   /* FLOAD   dst,base,index         */
557   { DRCUML_OP_FSTORE,  op_fstore },   /* FSTORE  base,index,src         */
558   { DRCUML_OP_FREAD,   op_fread },   /* FREAD   dst,space,src1         */
559   { DRCUML_OP_FWRITE,  op_fwrite },   /* FWRITE  space,dst,src1         */
560   { DRCUML_OP_FMOV,    op_fmov },      /* FMOV    dst,src1[,c]           */
561   { DRCUML_OP_FTOI4,   op_ftoi4 },   /* FTOI4   dst,src1               */
562   { DRCUML_OP_FTOI4T,  op_ftoi4t },   /* FTOI4T  dst,src1               */
563   { DRCUML_OP_FTOI4R,  op_ftoi4r },   /* FTOI4R  dst,src1               */
564   { DRCUML_OP_FTOI4F,  op_ftoi4f },   /* FTOI4F  dst,src1               */
565   { DRCUML_OP_FTOI4C,  op_ftoi4c },   /* FTOI4C  dst,src1               */
566   { DRCUML_OP_FTOI8,   op_ftoi8 },   /* FTOI8   dst,src1               */
567   { DRCUML_OP_FTOI8T,  op_ftoi8t },   /* FTOI8T  dst,src1               */
568   { DRCUML_OP_FTOI8R,  op_ftoi8r },   /* FTOI8R  dst,src1               */
569   { DRCUML_OP_FTOI8F,  op_ftoi8f },   /* FTOI8F  dst,src1               */
570   { DRCUML_OP_FTOI8C,  op_ftoi8c },   /* FTOI8C  dst,src1               */
571   { DRCUML_OP_FFRFS,   op_ffrfs },   /* FFRFS   dst,src1               */
572   { DRCUML_OP_FFRFD,   op_ffrfd },   /* FFRFD   dst,src1               */
573   { DRCUML_OP_FFRI4,   op_ffri4 },   /* FFRI4   dst,src1               */
574   { DRCUML_OP_FFRI8,   op_ffri8 },   /* FFRI8   dst,src1               */
575   { DRCUML_OP_FADD,    op_fadd },      /* FADD    dst,src1,src2          */
576   { DRCUML_OP_FSUB,    op_fsub },      /* FSUB    dst,src1,src2          */
577   { DRCUML_OP_FCMP,    op_fcmp },      /* FCMP    src1,src2              */
578   { DRCUML_OP_FMUL,    op_fmul },      /* FMUL    dst,src1,src2          */
579   { DRCUML_OP_FDIV,    op_fdiv },      /* FDIV    dst,src1,src2          */
580   { DRCUML_OP_FNEG,    op_fneg },      /* FNEG    dst,src1               */
581   { DRCUML_OP_FABS,    op_fabs },      /* FABS    dst,src1               */
582   { DRCUML_OP_FSQRT,   op_fsqrt },   /* FSQRT   dst,src1               */
583   { DRCUML_OP_FRECIP,  op_frecip },   /* FRECIP  dst,src1               */
584   { DRCUML_OP_FRSQRT,  op_frsqrt }   /* FRSQRT  dst,src1               */
585};
586
587
588
589/***************************************************************************
590    INLINE FUNCTIONS
591***************************************************************************/
592
593/*-------------------------------------------------
594    param_select_register - select a register
595    to use, avoiding conflicts with the optional
596    checkparam
597-------------------------------------------------*/
598
599INLINE int param_select_register(int defreg, const drcuml_parameter *param, const drcuml_parameter *checkparam)
600{
601   if ((param->type == DRCUML_PTYPE_INT_REGISTER || param->type == DRCUML_PTYPE_FLOAT_REGISTER) && (checkparam == NULL || param->type != checkparam->type || param->value != checkparam->value))
602      return param->value;
603   return defreg;
604}
605
606
607/*-------------------------------------------------
608    offset_from_rbp - return the verified offset
609    from rbp
610-------------------------------------------------*/
611
612INLINE INT32 offset_from_rbp(drcbe_state *drcbe, FPTR ptr)
613{
614   INT64 delta = (UINT8 *)ptr - drcbe->rbpvalue;
615   assert_always((INT32)delta == delta, "offset_from_rbp: delta out of range");
616   return (INT32)delta;
617}
618
619
620/*-------------------------------------------------
621    short_immediate - true if the given immediate
622    fits as a signed 32-bit value
623-------------------------------------------------*/
624
625INLINE int short_immediate(INT64 immediate)
626{
627   return (INT32)immediate == immediate;
628}
629
630
631/*-------------------------------------------------
632    get_base_register_and_offset - determine right
633    base register and offset to access the given
634    target address
635-------------------------------------------------*/
636
637INLINE int get_base_register_and_offset(drcbe_state *drcbe, x86code **dst, FPTR target, UINT8 reg, INT32 *offset)
638{
639   INT64 delta = (UINT8 *)target - drcbe->rbpvalue;
640   if (short_immediate(delta))
641   {
642      *offset = delta;
643      return REG_RBP;
644   }
645   else
646   {
647      *offset = 0;
648      emit_mov_r64_imm(dst, reg, target);                                    // mov   reg,target
649      return reg;
650   }
651}
652
653
654/*-------------------------------------------------
655    emit_smart_call_r64 - generate a call either
656    directly or via a call through pointer
657-------------------------------------------------*/
658
659INLINE void emit_smart_call_r64(drcbe_state *drcbe, x86code **dst, x86code *target, UINT8 reg)
660{
661   INT64 delta = target - (*dst + 5);
662   if (short_immediate(delta))
663      emit_call(dst, target);                                             // call  target
664   else
665   {
666      emit_mov_r64_imm(dst, reg, (FPTR)target);                              // mov   reg,target
667      emit_call_r64(dst, reg);                                          // call  reg
668   }
669}
670
671
672/*-------------------------------------------------
673    emit_smart_call_m64 - generate a call either
674    directly or via a call through pointer
675-------------------------------------------------*/
676
677INLINE void emit_smart_call_m64(drcbe_state *drcbe, x86code **dst, x86code **target)
678{
679   INT64 delta = *target - (*dst + 5);
680   if (short_immediate(delta))
681      emit_call(dst, *target);                                          // call  *target
682   else
683      emit_call_m64(dst, MABS(drcbe, target));                              // call  [target]
684}
685
686
687
688/***************************************************************************
689    BACKEND CALLBACKS
690***************************************************************************/
691
692/*-------------------------------------------------
693    drcbex64_alloc - allocate back-end-specific
694    state
695-------------------------------------------------*/
696
697static drcbe_state *drcbex64_alloc(drcuml_state *drcuml, drccache *cache, UINT32 flags, int modes, int addrbits, int ignorebits)
698{
699   /* SSE control register mapping */
700   static const UINT32 sse_control[4] =
701   {
702      0xffc0,    /* DRCUML_FMOD_TRUNC */
703      0x9fc0,    /* DRCUML_FMOD_ROUND */
704      0xdfc0,    /* DRCUML_FMOD_CEIL */
705      0xbfc0       /* DRCUML_FMOD_FLOOR */
706   };
707   drcbe_state *drcbe;
708   int opnum, entry;
709
710   /* allocate space in the cache for our state */
711   drcbe = drccache_memory_alloc_near(cache, sizeof(*drcbe));
712   if (drcbe == NULL)
713      return NULL;
714   memset(drcbe, 0, sizeof(*drcbe));
715
716   /* remember our pointers */
717   drcbe->drcuml = drcuml;
718   drcbe->cache = cache;
719   drcbe->rbpvalue = drccache_near(cache) + 0x80;
720
721   /* build up necessary arrays */
722   memcpy(drcbe->ssecontrol, sse_control, sizeof(drcbe->ssecontrol));
723   drcbe->absmask32 = drccache_memory_alloc_near(cache, 16*2 + 15);
724   drcbe->absmask32 = (void *)(((FPTR)drcbe->absmask32 + 15) & ~15);
725   drcbe->absmask32[0] = drcbe->absmask32[1] = drcbe->absmask32[2] = drcbe->absmask32[3] = 0x7fffffff;
726   drcbe->absmask64 = (UINT64 *)&drcbe->absmask32[4];
727   drcbe->absmask64[0] = drcbe->absmask64[1] = U64(0x7fffffffffffffff);
728
729   /* get pointers to C functions we need to call */
730#ifdef ENABLE_DEBUGGER
731   drcbe->mame_debug_hook = (x86code *)mame_debug_hook;
732#endif
733#if LOG_HASHJMPS
734   drcbe->debug_log_hashjmp = (x86code *)debug_log_hashjmp;
735#endif
736   drcbe->drcmap_get_value = (x86code *)drcmap_get_value;
737
738   /* allocate hash tables */
739   drcbe->hash = drchash_alloc(cache, modes, addrbits, ignorebits);
740   if (drcbe->hash == NULL)
741      return NULL;
742
743   /* allocate code map */
744   drcbe->map = drcmap_alloc(cache, 0);
745   if (drcbe->map == NULL)
746      return NULL;
747
748   /* allocate a label tracker */
749   drcbe->labels = drclabel_list_alloc(cache);
750   if (drcbe->labels == NULL)
751      return NULL;
752
753   /* build the opcode table (static but it doesn't hurt to regenerate it) */
754   for (opnum = 0; opnum < ARRAY_LENGTH(opcode_table_source); opnum++)
755      opcode_table[opcode_table_source[opnum].opcode] = opcode_table_source[opnum].func;
756
757   /* build the flags map */
758   for (entry = 0; entry < ARRAY_LENGTH(drcbe->flagsmap); entry++)
759   {
760      UINT8 flags = 0;
761      if (entry & 0x001) flags |= DRCUML_FLAG_C;
762      if (entry & 0x004) flags |= DRCUML_FLAG_U;
763      if (entry & 0x040) flags |= DRCUML_FLAG_Z;
764      if (entry & 0x080) flags |= DRCUML_FLAG_S;
765      if (entry & 0x800) flags |= DRCUML_FLAG_V;
766      drcbe->flagsmap[entry] = flags;
767   }
768
769   /* create the log */
770   if (flags & DRCUML_OPTION_LOG_NATIVE)
771      drcbe->log = x86log_create_context("drcbex64.asm");
772
773   return drcbe;
774}
775
776
777/*-------------------------------------------------
778    drcbex64_free - free back-end specific state
779-------------------------------------------------*/
780
781static void drcbex64_free(drcbe_state *drcbe)
782{
783   /* free the log context */
784   if (drcbe->log != NULL)
785      x86log_free_context(drcbe->log);
786}
787
788
789/*-------------------------------------------------
790    drcbex64_reset - reset back-end specific state
791-------------------------------------------------*/
792
793static void drcbex64_reset(drcbe_state *drcbe)
794{
795   x86code **dst;
796   int spacenum;
797
798   /* output a note to the log */
799   if (drcbe->log != NULL)
800      x86log_printf(drcbe->log, "\n\n===========\nCACHE RESET\n===========\n\n");
801
802   /* fetch the accessors now that things are ready to go */
803   for (spacenum = 0; spacenum < ADDRESS_SPACES; spacenum++)
804      if (active_address_space[spacenum].accessors != NULL)
805         drcbe->accessors[spacenum] = *active_address_space[spacenum].accessors;
806
807   /* generate a little bit of glue code to set up the environment */
808   dst = (x86code **)drccache_begin_codegen(drcbe->cache, 500);
809   if (dst == NULL)
810      fatalerror("Out of cache space after a reset!");
811
812   /* generate an entry point */
813   drcbe->entry = (x86_entry_point_func)*dst;
814   emit_push_r64(dst, REG_RBX);                                          // push  rbx
815   emit_push_r64(dst, REG_RSI);                                          // push  rsi
816   emit_push_r64(dst, REG_RDI);                                          // push  rdi
817   emit_push_r64(dst, REG_RBP);                                          // push  rbp
818   emit_push_r64(dst, REG_R12);                                          // push  r12
819   emit_push_r64(dst, REG_R13);                                          // push  r13
820   emit_push_r64(dst, REG_R14);                                          // push  r14
821   emit_push_r64(dst, REG_R15);                                          // push  r15
822   emit_mov_r64_r64(dst, REG_RBP, REG_PARAM1);                                 // mov   rbp,param1
823   emit_sub_r64_imm(dst, REG_RSP, 32);                                       // sub   rsp,32
824   emit_mov_m64_r64(dst, MABS(drcbe, &drcbe->hashstacksave), REG_RSP);               // mov   [hashstacksave],rsp
825   emit_sub_r64_imm(dst, REG_RSP, 8);                                       // sub   rsp,8
826   emit_mov_m64_r64(dst, MABS(drcbe, &drcbe->stacksave), REG_RSP);                  // mov   [stacksave],rsp
827   emit_stmxcsr_m32(dst, MABS(drcbe, &drcbe->ssemode));                        // stmxcsr [ssemode]
828   emit_jmp_r64(dst, REG_PARAM2);                                          // jmp   param2
829   if (drcbe->log != NULL)
830      x86log_disasm_code_range(drcbe->log, "entry_point", (x86code *)drcbe->entry, *dst);
831
832   /* generate an exit point */
833   drcbe->exit = *dst;
834   emit_ldmxcsr_m32(dst, MABS(drcbe, &drcbe->ssemode));                        // ldmxcsr [ssemode]
835   emit_mov_r64_m64(dst, REG_RSP, MABS(drcbe, &drcbe->hashstacksave));               // mov   rsp,[hashstacksave]
836   emit_add_r64_imm(dst, REG_RSP, 32);                                       // add   rsp,32
837   emit_pop_r64(dst, REG_R15);                                             // pop   r15
838   emit_pop_r64(dst, REG_R14);                                             // pop   r14
839   emit_pop_r64(dst, REG_R13);                                             // pop   r13
840   emit_pop_r64(dst, REG_R12);                                             // pop   r12
841   emit_pop_r64(dst, REG_RBP);                                             // pop   rbp
842   emit_pop_r64(dst, REG_RDI);                                             // pop   rdi
843   emit_pop_r64(dst, REG_RSI);                                             // pop   rsi
844   emit_pop_r64(dst, REG_RBX);                                             // pop   rbx
845   emit_ret(dst);                                                      // ret
846   if (drcbe->log != NULL)
847      x86log_disasm_code_range(drcbe->log, "exit_point", drcbe->exit, *dst);
848
849   /* generate a no code point */
850   drcbe->nocode = *dst;
851   emit_ret(dst);                                                      // ret
852   if (drcbe->log != NULL)
853      x86log_disasm_code_range(drcbe->log, "nocode", drcbe->nocode, *dst);
854
855   /* finish up codegen */
856   drccache_end_codegen(drcbe->cache);
857
858   /* reset our hash tables */
859   drchash_reset(drcbe->hash);
860   drchash_set_default_codeptr(drcbe->hash, drcbe->nocode);
861}
862
863
864/*-------------------------------------------------
865    drcbex64_generate - generate code
866-------------------------------------------------*/
867
868static void drcbex64_generate(drcbe_state *drcbe, drcuml_block *block, const drcuml_instruction *instlist, UINT32 numinst)
869{
870   const char *blockname = NULL;
871   char blockbuffer[100];
872   drccodeptr *cachetop;
873   x86code *base;
874   x86code *dst;
875   int inum;
876
877   /* tell all of our utility objects that a block is beginning */
878   drchash_block_begin(drcbe->hash, block, instlist, numinst);
879   drclabel_block_begin(drcbe->labels, block);
880   drcmap_block_begin(drcbe->map, block);
881
882   /* begin codegen; fail if we can't */
883   cachetop = drccache_begin_codegen(drcbe->cache, numinst * 8 * 4);
884   if (cachetop == NULL)
885      drcuml_block_abort(block);
886
887   /* compute the base by aligning the cache top to a cache line (assumed to be 64 bytes) */
888   base = (x86code *)(((FPTR)*cachetop + 63) & ~63);
889   dst = base;
890
891   /* generate code */
892   for (inum = 0; inum < numinst; inum++)
893   {
894      const drcuml_instruction *inst = &instlist[inum];
895      assert(inst->opcode < ARRAY_LENGTH(opcode_table));
896
897      /* add a comment */
898      if (drcbe->log != NULL)
899      {
900         char dasm[100];
901         drcuml_disasm(inst, dasm);
902         x86log_add_comment(drcbe->log, dst, "%s", dasm);
903      }
904
905      /* extract a blockname */
906      if (blockname == NULL)
907      {
908         if (inst->opcode == DRCUML_OP_HANDLE)
909            blockname = drcuml_handle_name((drcuml_codehandle *)(FPTR)inst->param[0].value);
910         else if (inst->opcode == DRCUML_OP_HASH)
911         {
912            sprintf(blockbuffer, "Code: mode=%d PC=%08X", (UINT32)inst->param[0].value, (offs_t)inst->param[1].value);
913            blockname = blockbuffer;
914         }
915      }
916
917      /* generate code */
918      dst = (*opcode_table[inst->opcode])(drcbe, dst, inst);
919   }
920
921   /* complete codegen */
922   *cachetop = (drccodeptr)dst;
923   drccache_end_codegen(drcbe->cache);
924
925   /* log it */
926   if (drcbe->log != NULL)
927      x86log_disasm_code_range(drcbe->log, (blockname == NULL) ? "Unknown block" : blockname, base, drccache_top(drcbe->cache));
928
929   /* tell all of our utility objects that the block is finished */
930   drchash_block_end(drcbe->hash, block);
931   drclabel_block_end(drcbe->labels, block);
932   drcmap_block_end(drcbe->map, block);
933}
934
935
936/*-------------------------------------------------
937    drcbex64_hash_exists - return true if the
938    given mode/pc exists in the hash table
939-------------------------------------------------*/
940
941static int drcbex64_hash_exists(drcbe_state *drcbe, UINT32 mode, UINT32 pc)
942{
943   return drchash_code_exists(drcbe->hash, mode, pc);
944}
945
946
947/*-------------------------------------------------
948    drcbec_execute - execute a block of code
949    registered at the given mode/pc
950-------------------------------------------------*/
951
952static int drcbex64_execute(drcbe_state *drcbe, drcuml_codehandle *entry)
953{
954   /* call our entry point which will jump to the destination */
955   return (*drcbe->entry)(drcbe->rbpvalue, (x86code *)drcuml_handle_codeptr(entry));
956}
957
958
959
960/***************************************************************************
961    COMPILE HELPERS
962***************************************************************************/
963
964/*-------------------------------------------------
965    param_normalize - convert a full parameter
966    into a reduced set
967-------------------------------------------------*/
968
969static void param_normalize(drcbe_state *drcbe, const drcuml_parameter *src, drcuml_parameter *dest, UINT32 allowed)
970{
971   int regnum;
972
973   switch (src->type)
974   {
975      /* immediates pass through */
976      case DRCUML_PTYPE_IMMEDIATE:
977         assert(allowed & PTYPE_I);
978         dest->type = DRCUML_PTYPE_IMMEDIATE;
979         dest->value = src->value;
980         break;
981
982      /* mapvars are converted to immediates with their current value */
983      case DRCUML_PTYPE_MAPVAR:
984         assert(allowed & PTYPE_I);
985         dest->type = DRCUML_PTYPE_IMMEDIATE;
986         dest->value = drcmap_get_last_value(drcbe->map, src->value);
987         break;
988
989      /* memory passes through */
990      case DRCUML_PTYPE_MEMORY:
991         assert(allowed & PTYPE_M);
992         dest->type = DRCUML_PTYPE_MEMORY;
993         dest->value = (FPTR)src->value;
994         break;
995
996      /* if a register maps to a register, keep it as a register; otherwise map it to memory */
997      case DRCUML_PTYPE_INT_REGISTER:
998         assert(allowed & PTYPE_R);
999         assert(allowed & PTYPE_M);
1000         regnum = int_register_map[src->value - DRCUML_REG_I0];
1001         if (regnum != 0)
1002         {
1003            dest->type = DRCUML_PTYPE_INT_REGISTER;
1004            dest->value = regnum;
1005         }
1006         else
1007         {
1008            dest->type = DRCUML_PTYPE_MEMORY;
1009            dest->value = (FPTR)&drcbe->state.r[src->value - DRCUML_REG_I0];
1010         }
1011         break;
1012
1013      /* if a register maps to a register, keep it as a register; otherwise map it to memory */
1014      case DRCUML_PTYPE_FLOAT_REGISTER:
1015         assert(allowed & PTYPE_F);
1016         assert(allowed & PTYPE_M);
1017         regnum = float_register_map[src->value - DRCUML_REG_F0];
1018         if (regnum != 0)
1019         {
1020            dest->type = DRCUML_PTYPE_FLOAT_REGISTER;
1021            dest->value = regnum;
1022         }
1023         else
1024         {
1025            dest->type = DRCUML_PTYPE_MEMORY;
1026            dest->value = (FPTR)&drcbe->state.f[src->value - DRCUML_REG_F0];
1027         }
1028         break;
1029
1030      /* everything else is unexpected */
1031      default:
1032         fatalerror("Unexpected parameter type");
1033         break;
1034   }
1035}
1036
1037
1038/*-------------------------------------------------
1039    param_normalize_1 - normalize a single
1040    parameter instruction
1041-------------------------------------------------*/
1042
1043static void param_normalize_1(drcbe_state *drcbe, const drcuml_instruction *inst, drcuml_parameter *dest0, UINT32 allowed0)
1044{
1045   assert(inst->numparams == 1);
1046   param_normalize(drcbe, &inst->param[0], dest0, allowed0);
1047}
1048
1049
1050/*-------------------------------------------------
1051    param_normalize_2 - normalize a 2
1052    parameter instruction
1053-------------------------------------------------*/
1054
1055static void param_normalize_2(drcbe_state *drcbe, const drcuml_instruction *inst, drcuml_parameter *dest0, UINT32 allowed0, drcuml_parameter *dest1, UINT32 allowed1)
1056{
1057   assert(inst->numparams == 2);
1058   param_normalize(drcbe, &inst->param[0], dest0, allowed0);
1059   param_normalize(drcbe, &inst->param[1], dest1, allowed1);
1060}
1061
1062
1063/*-------------------------------------------------
1064    param_normalize_2_commutative - normalize a 2
1065    parameter instruction, shuffling the
1066    parameters on the assumption that the two
1067    parameters can be swapped
1068-------------------------------------------------*/
1069
1070static void param_normalize_2_commutative(drcbe_state *drcbe, const drcuml_instruction *inst, drcuml_parameter *dest0, UINT32 allowed0, drcuml_parameter *dest1, UINT32 allowed1)
1071{
1072   param_normalize_2(drcbe, inst, dest0, allowed0, dest1, allowed1);
1073
1074   /* if the inner parameter is a memory operand, push it to the outer */
1075   if (dest0->type == DRCUML_PTYPE_MEMORY)
1076   {
1077      drcuml_parameter temp = *dest0;
1078      *dest0 = *dest1;
1079      *dest1 = temp;
1080   }
1081
1082   /* if the inner parameter is an immediate, push it to the outer */
1083   if (dest0->type == DRCUML_PTYPE_IMMEDIATE)
1084   {
1085      drcuml_parameter temp = *dest0;
1086      *dest0 = *dest1;
1087      *dest1 = temp;
1088   }
1089}
1090
1091
1092/*-------------------------------------------------
1093    param_normalize_3 - normalize a 3
1094    parameter instruction
1095-------------------------------------------------*/
1096
1097static void param_normalize_3(drcbe_state *drcbe, const drcuml_instruction *inst, drcuml_parameter *dest0, UINT32 allowed0, drcuml_parameter *dest1, UINT32 allowed1, drcuml_parameter *dest2, UINT32 allowed2)
1098{
1099   assert(inst->numparams == 3);
1100   param_normalize(drcbe, &inst->param[0], dest0, allowed0);
1101   param_normalize(drcbe, &inst->param[1], dest1, allowed1);
1102   param_normalize(drcbe, &inst->param[2], dest2, allowed2);
1103}
1104
1105
1106/*-------------------------------------------------
1107    param_normalize_3_commutative - normalize a 3
1108    parameter instruction, shuffling the
1109    parameters on the assumption that the last
1110    2 can be swapped
1111-------------------------------------------------*/
1112
1113static void param_normalize_3_commutative(drcbe_state *drcbe, const drcuml_instruction *inst, drcuml_parameter *dest0, UINT32 allowed0, drcuml_parameter *dest1, UINT32 allowed1, drcuml_parameter *dest2, UINT32 allowed2)
1114{
1115   param_normalize_3(drcbe, inst, dest0, allowed0, dest1, allowed1, dest2, allowed2);
1116
1117   /* if the inner parameter is a memory operand, push it to the outer */
1118   if (dest1->type == DRCUML_PTYPE_MEMORY)
1119   {
1120      drcuml_parameter temp = *dest1;
1121      *dest1 = *dest2;
1122      *dest2 = temp;
1123   }
1124
1125   /* if the inner parameter is an immediate, push it to the outer */
1126   if (dest1->type == DRCUML_PTYPE_IMMEDIATE)
1127   {
1128      drcuml_parameter temp = *dest1;
1129      *dest1 = *dest2;
1130      *dest2 = temp;
1131   }
1132
1133   /* if the destination and outer parameters are equal, move the outer to the inner */
1134   if (dest0->type == dest2->type && dest0->value == dest2->value && dest0->type != DRCUML_PTYPE_IMMEDIATE)
1135   {
1136      drcuml_parameter temp = *dest1;
1137      *dest1 = *dest2;
1138      *dest2 = temp;
1139   }
1140}
1141
1142
1143/*-------------------------------------------------
1144    param_normalize_4 - normalize a 4
1145    parameter instruction
1146-------------------------------------------------*/
1147
1148static void param_normalize_4(drcbe_state *drcbe, const drcuml_instruction *inst, drcuml_parameter *dest0, UINT32 allowed0, drcuml_parameter *dest1, UINT32 allowed1, drcuml_parameter *dest2, UINT32 allowed2, drcuml_parameter *dest3, UINT32 allowed3)
1149{
1150   assert(inst->numparams == 4);
1151   param_normalize(drcbe, &inst->param[0], dest0, allowed0);
1152   param_normalize(drcbe, &inst->param[1], dest1, allowed1);
1153   param_normalize(drcbe, &inst->param[2], dest2, allowed2);
1154   param_normalize(drcbe, &inst->param[3], dest3, allowed3);
1155}
1156
1157
1158/*-------------------------------------------------
1159    param_normalize_4_commutative - normalize a 4
1160    parameter instruction, shuffling the
1161    parameters on the assumption that the last
1162    2 can be swapped
1163-------------------------------------------------*/
1164
1165static void param_normalize_4_commutative(drcbe_state *drcbe, const drcuml_instruction *inst, drcuml_parameter *dest0, UINT32 allowed0, drcuml_parameter *dest1, UINT32 allowed1, drcuml_parameter *dest2, UINT32 allowed2, drcuml_parameter *dest3, UINT32 allowed3)
1166{
1167   param_normalize_4(drcbe, inst, dest0, allowed0, dest1, allowed1, dest2, allowed2, dest3, allowed3);
1168
1169   /* if the inner parameter is a memory operand, push it to the outer */
1170   if (dest2->type == DRCUML_PTYPE_MEMORY)
1171   {
1172      drcuml_parameter temp = *dest2;
1173      *dest2 = *dest3;
1174      *dest3 = temp;
1175   }
1176
1177   /* if the inner parameter is an immediate, push it to the outer */
1178   if (dest2->type == DRCUML_PTYPE_IMMEDIATE)
1179   {
1180      drcuml_parameter temp = *dest2;
1181      *dest2 = *dest3;
1182      *dest3 = temp;
1183   }
1184
1185   /* if the destination and outer parameters are equal, move the outer to the inner */
1186   if (dest0->type == dest3->type && dest0->value == dest3->value && dest0->type != DRCUML_PTYPE_IMMEDIATE)
1187   {
1188      drcuml_parameter temp = *dest2;
1189      *dest2 = *dest3;
1190      *dest3 = temp;
1191   }
1192}
1193
1194
1195
1196/***************************************************************************
1197    EMITTERS FOR 32-BIT OPERATIONS WITH PARAMETERS
1198***************************************************************************/
1199
1200/*-------------------------------------------------
1201    emit_mov_r32_p32 - move a 32-bit parameter
1202    into a register
1203-------------------------------------------------*/
1204
1205static void emit_mov_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param)
1206{
1207   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1208   {
1209      if (param->value == 0)
1210         emit_xor_r32_r32(dst, reg, reg);                                 // xor   reg,reg
1211      else
1212         emit_mov_r32_imm(dst, reg, param->value);                           // mov   reg,param
1213   }
1214   else if (param->type == DRCUML_PTYPE_MEMORY)
1215      emit_mov_r32_m32(dst, reg, MABS(drcbe, param->value));                     // mov   reg,[param]
1216   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
1217   {
1218      if (reg != param->value)
1219         emit_mov_r32_r32(dst, reg, param->value);                           // mov   reg,param
1220   }
1221}
1222
1223
1224/*-------------------------------------------------
1225    emit_mov_m32_p32 - move a 32-bit parameter
1226    into a memory location
1227-------------------------------------------------*/
1228
1229static void emit_mov_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param)
1230{
1231   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1232      emit_mov_m32_imm(dst, MEMPARAMS, param->value);                           // mov   [mem],param
1233   else if (param->type == DRCUML_PTYPE_MEMORY)
1234   {
1235      emit_mov_r32_m32(dst, REG_EAX, MABS(drcbe, param->value));                  // mov   eax,[param]
1236      emit_mov_m32_r32(dst, MEMPARAMS, REG_EAX);                              // mov   [mem],eax
1237   }
1238   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
1239      emit_mov_m32_r32(dst, MEMPARAMS, param->value);                           // mov   [mem],param
1240}
1241
1242
1243/*-------------------------------------------------
1244    emit_mov_p32_r32 - move a register into a
1245    32-bit parameter
1246-------------------------------------------------*/
1247
1248static void emit_mov_p32_r32(drcbe_state *drcbe, x86code **dst, const drcuml_parameter *param, UINT8 reg)
1249{
1250   assert(param->type != DRCUML_PTYPE_IMMEDIATE);
1251   if (param->type == DRCUML_PTYPE_MEMORY)
1252      emit_mov_m32_r32(dst, MABS(drcbe, param->value), reg);                     // mov   [param],reg
1253   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
1254   {
1255      if (reg != param->value)
1256         emit_mov_r32_r32(dst, param->value, reg);                           // mov   param,reg
1257   }
1258}
1259
1260
1261/*-------------------------------------------------
1262    emit_add_r32_p32 - add operation to a 32-bit
1263    register from a 32-bit parameter
1264-------------------------------------------------*/
1265
1266static void emit_add_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
1267{
1268   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1269   {
1270      if (inst->condflags != 0 || param->value != 0)
1271         emit_add_r32_imm(dst, reg, param->value);                           // add   reg,param
1272   }
1273   else if (param->type == DRCUML_PTYPE_MEMORY)
1274      emit_add_r32_m32(dst, reg, MABS(drcbe, param->value));                     // add   reg,[param]
1275   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
1276      emit_add_r32_r32(dst, reg, param->value);                              // add   reg,param
1277}
1278
1279
1280/*-------------------------------------------------
1281    emit_add_m32_p32 - add operation to a 32-bit
1282    memory location from a 32-bit parameter
1283-------------------------------------------------*/
1284
1285static void emit_add_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
1286{
1287   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1288   {
1289      if (inst->condflags != 0 || param->value != 0)
1290         emit_add_m32_imm(dst, MEMPARAMS, param->value);                        // add   [dest],param
1291   }
1292   else
1293   {
1294      int reg = param_select_register(REG_EAX, param, NULL);
1295      emit_mov_r32_p32(drcbe, dst, reg, param);                              // mov   reg,param
1296      emit_add_m32_r32(dst, MEMPARAMS, reg);                                 // add   [dest],reg
1297   }
1298}
1299
1300
1301/*-------------------------------------------------
1302    emit_adc_r32_p32 - adc operation to a 32-bit
1303    register from a 32-bit parameter
1304-------------------------------------------------*/
1305
1306static void emit_adc_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
1307{
1308   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1309   {
1310      if (inst->condflags != 0 || param->value != 0)
1311         emit_adc_r32_imm(dst, reg, param->value);                           // adc   reg,param
1312   }
1313   else if (param->type == DRCUML_PTYPE_MEMORY)
1314      emit_adc_r32_m32(dst, reg, MABS(drcbe, param->value));                     // adc   reg,[param]
1315   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
1316      emit_adc_r32_r32(dst, reg, param->value);                              // adc   reg,param
1317}
1318
1319
1320/*-------------------------------------------------
1321    emit_adc_m32_p32 - adc operation to a 32-bit
1322    memory location from a 32-bit parameter
1323-------------------------------------------------*/
1324
1325static void emit_adc_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
1326{
1327   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1328   {
1329      if (inst->condflags != 0 || param->value != 0)
1330         emit_adc_m32_imm(dst, MEMPARAMS, param->value);                        // adc   [dest],param
1331   }
1332   else
1333   {
1334      int reg = param_select_register(REG_EAX, param, NULL);
1335      emit_mov_r32_p32(drcbe, dst, reg, param);                              // mov   reg,param
1336      emit_adc_m32_r32(dst, MEMPARAMS, reg);                                 // adc   [dest],reg
1337   }
1338}
1339
1340
1341/*-------------------------------------------------
1342    emit_sub_r32_p32 - sub operation to a 32-bit
1343    register from a 32-bit parameter
1344-------------------------------------------------*/
1345
1346static void emit_sub_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
1347{
1348   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1349   {
1350      if (inst->condflags != 0 || param->value != 0)
1351         emit_sub_r32_imm(dst, reg, param->value);                           // sub   reg,param
1352   }
1353   else if (param->type == DRCUML_PTYPE_MEMORY)
1354      emit_sub_r32_m32(dst, reg, MABS(drcbe, param->value));                     // sub   reg,[param]
1355   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
1356      emit_sub_r32_r32(dst, reg, param->value);                              // sub   reg,param
1357}
1358
1359
1360/*-------------------------------------------------
1361    emit_sub_m32_p32 - sub operation to a 32-bit
1362    memory location from a 32-bit parameter
1363-------------------------------------------------*/
1364
1365static void emit_sub_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
1366{
1367   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1368   {
1369      if (inst->condflags != 0 || param->value != 0)
1370         emit_sub_m32_imm(dst, MEMPARAMS, param->value);                        // sub   [dest],param
1371   }
1372   else
1373   {
1374      int reg = param_select_register(REG_EAX, param, NULL);
1375      emit_mov_r32_p32(drcbe, dst, reg, param);                              // mov   reg,param
1376      emit_sub_m32_r32(dst, MEMPARAMS, reg);                                 // sub   [dest],reg
1377   }
1378}
1379
1380
1381/*-------------------------------------------------
1382    emit_sbb_r32_p32 - sbb operation to a 32-bit
1383    register from a 32-bit parameter
1384-------------------------------------------------*/
1385
1386static void emit_sbb_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
1387{
1388   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1389   {
1390      if (inst->condflags != 0 || param->value != 0)
1391         emit_sbb_r32_imm(dst, reg, param->value);                           // sbb   reg,param
1392   }
1393   else if (param->type == DRCUML_PTYPE_MEMORY)
1394      emit_sbb_r32_m32(dst, reg, MABS(drcbe, param->value));                     // sbb   reg,[param]
1395   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
1396      emit_sbb_r32_r32(dst, reg, param->value);                              // sbb   reg,param
1397}
1398
1399
1400/*-------------------------------------------------
1401    emit_sbb_m32_p32 - sbb operation to a 32-bit
1402    memory location from a 32-bit parameter
1403-------------------------------------------------*/
1404
1405static void emit_sbb_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
1406{
1407   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1408   {
1409      if (inst->condflags != 0 || param->value != 0)
1410         emit_sbb_m32_imm(dst, MEMPARAMS, param->value);                        // sbb   [dest],param
1411   }
1412   else
1413   {
1414      int reg = param_select_register(REG_EAX, param, NULL);
1415      emit_mov_r32_p32(drcbe, dst, reg, param);                              // mov   reg,param
1416      emit_sbb_m32_r32(dst, MEMPARAMS, reg);                                 // sbb   [dest],reg
1417   }
1418}
1419
1420
1421/*-------------------------------------------------
1422    emit_cmp_r32_p32 - cmp operation to a 32-bit
1423    register from a 32-bit parameter
1424-------------------------------------------------*/
1425
1426static void emit_cmp_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
1427{
1428   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1429      emit_cmp_r32_imm(dst, reg, param->value);                              // cmp   reg,param
1430   else if (param->type == DRCUML_PTYPE_MEMORY)
1431      emit_cmp_r32_m32(dst, reg, MABS(drcbe, param->value));                     // cmp   reg,[param]
1432   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
1433      emit_cmp_r32_r32(dst, reg, param->value);                              // cmp   reg,param
1434}
1435
1436
1437/*-------------------------------------------------
1438    emit_cmp_m32_p32 - cmp operation to a 32-bit
1439    memory location from a 32-bit parameter
1440-------------------------------------------------*/
1441
1442static void emit_cmp_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
1443{
1444   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1445      emit_cmp_m32_imm(dst, MEMPARAMS, param->value);                           // cmp   [dest],param
1446   else
1447   {
1448      int reg = param_select_register(REG_EAX, param, NULL);
1449      emit_mov_r32_p32(drcbe, dst, reg, param);                              // mov   reg,param
1450      emit_cmp_m32_r32(dst, MEMPARAMS, reg);                                 // cmp   [dest],reg
1451   }
1452}
1453
1454
1455/*-------------------------------------------------
1456    emit_and_r32_p32 - and operation to a 32-bit
1457    register from a 32-bit parameter
1458-------------------------------------------------*/
1459
1460static void emit_and_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
1461{
1462   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1463   {
1464      if (inst->condflags != 0 && (UINT32)param->value == 0xffffffff)
1465         /* skip */;
1466      else if (inst->condflags != 0 && (UINT32)param->value == 0)
1467         emit_xor_r32_r32(dst, reg, reg);                                 // xor   reg,reg
1468      else
1469         emit_and_r32_imm(dst, reg, param->value);                           // and   reg,param
1470   }
1471   else if (param->type == DRCUML_PTYPE_MEMORY)
1472      emit_and_r32_m32(dst, reg, MABS(drcbe, param->value));                     // and   reg,[param]
1473   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
1474      emit_and_r32_r32(dst, reg, param->value);                              // and   reg,param
1475}
1476
1477
1478/*-------------------------------------------------
1479    emit_and_m32_p32 - and operation to a 32-bit
1480    memory location from a 32-bit parameter
1481-------------------------------------------------*/
1482
1483static void emit_and_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
1484{
1485   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1486   {
1487      if (inst->condflags != 0 && (UINT32)param->value == 0xffffffff)
1488         /* skip */;
1489      else if (inst->condflags != 0 && (UINT32)param->value == 0)
1490         emit_mov_m32_imm(dst, MEMPARAMS, 0);                              // mov   [dest],0
1491      else
1492         emit_and_m32_imm(dst, MEMPARAMS, param->value);                        // and   [dest],param
1493   }
1494   else
1495   {
1496      int reg = param_select_register(REG_EAX, param, NULL);
1497      emit_mov_r32_p32(drcbe, dst, reg, param);                              // mov   reg,param
1498      emit_and_m32_r32(dst, MEMPARAMS, reg);                                 // and   [dest],reg
1499   }
1500}
1501
1502
1503/*-------------------------------------------------
1504    emit_test_r32_p32 - test operation to a 32-bit
1505    register from a 32-bit parameter
1506-------------------------------------------------*/
1507
1508static void emit_test_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
1509{
1510   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1511      emit_test_r32_imm(dst, reg, param->value);                              // test   reg,param
1512   else if (param->type == DRCUML_PTYPE_MEMORY)
1513      emit_test_m32_r32(dst, MABS(drcbe, param->value), reg);                     // test   [param],reg
1514   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
1515      emit_test_r32_r32(dst, reg, param->value);                              // test   reg,param
1516}
1517
1518
1519/*-------------------------------------------------
1520    emit_test_m32_p32 - test operation to a 32-bit
1521    memory location from a 32-bit parameter
1522-------------------------------------------------*/
1523
1524static void emit_test_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
1525{
1526   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1527      emit_test_m32_imm(dst, MEMPARAMS, param->value);                        // test  [dest],param
1528   else if (param->type == DRCUML_PTYPE_MEMORY)
1529   {
1530      emit_mov_r32_p32(drcbe, dst, REG_EAX, param);                           // mov   reg,param
1531      emit_test_m32_r32(dst, MEMPARAMS, REG_EAX);                              // test  [dest],reg
1532   }
1533   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
1534      emit_test_m32_r32(dst, MEMPARAMS, param->value);                        // test  [dest],param
1535}
1536
1537
1538/*-------------------------------------------------
1539    emit_or_r32_p32 - or operation to a 32-bit
1540    register from a 32-bit parameter
1541-------------------------------------------------*/
1542
1543static void emit_or_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
1544{
1545   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1546   {
1547      if (inst->condflags != 0 && (UINT32)param->value == 0)
1548         /* skip */;
1549      else if (inst->condflags != 0 && (UINT32)param->value == 0xffffffff)
1550         emit_mov_r32_imm(dst, reg, -1);                                    // mov  reg,-1
1551      else
1552         emit_or_r32_imm(dst, reg, param->value);                           // or   reg,param
1553   }
1554   else if (param->type == DRCUML_PTYPE_MEMORY)
1555      emit_or_r32_m32(dst, reg, MABS(drcbe, param->value));                     // or   reg,[param]
1556   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
1557      emit_or_r32_r32(dst, reg, param->value);                              // or   reg,param
1558}
1559
1560
1561/*-------------------------------------------------
1562    emit_or_m32_p32 - or operation to a 32-bit
1563    memory location from a 32-bit parameter
1564-------------------------------------------------*/
1565
1566static void emit_or_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
1567{
1568   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1569   {
1570      if (inst->condflags != 0 && (UINT32)param->value == 0)
1571         /* skip */;
1572      else if (inst->condflags != 0 && (UINT32)param->value == 0xffffffff)
1573         emit_mov_m32_imm(dst, MEMPARAMS, -1);                              // mov   [dest],-1
1574      else
1575         emit_or_m32_imm(dst, MEMPARAMS, param->value);                        // or   [dest],param
1576   }
1577   else
1578   {
1579      int reg = param_select_register(REG_EAX, param, NULL);
1580      emit_mov_r32_p32(drcbe, dst, reg, param);                              // mov   reg,param
1581      emit_or_m32_r32(dst, MEMPARAMS, reg);                                 // or   [dest],reg
1582   }
1583}
1584
1585
1586/*-------------------------------------------------
1587    emit_xor_r32_p32 - xor operation to a 32-bit
1588    register from a 32-bit parameter
1589-------------------------------------------------*/
1590
1591static void emit_xor_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
1592{
1593   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1594   {
1595      if (inst->condflags != 0 && (UINT32)param->value == 0)
1596         /* skip */;
1597      else if (inst->condflags != 0 && (UINT32)param->value == 0xffffffff)
1598         emit_not_r32(dst, reg);                                          // not   reg
1599      else
1600         emit_xor_r32_imm(dst, reg, param->value);                           // xor   reg,param
1601   }
1602   else if (param->type == DRCUML_PTYPE_MEMORY)
1603      emit_xor_r32_m32(dst, reg, MABS(drcbe, param->value));                     // xor   reg,[param]
1604   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
1605      emit_xor_r32_r32(dst, reg, param->value);                              // xor   reg,param
1606}
1607
1608
1609/*-------------------------------------------------
1610    emit_xor_m32_p32 - xor operation to a 32-bit
1611    memory location from a 32-bit parameter
1612-------------------------------------------------*/
1613
1614static void emit_xor_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
1615{
1616   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1617   {
1618      if (inst->condflags != 0 && (UINT32)param->value == 0)
1619         /* skip */;
1620      else if (inst->condflags != 0 && (UINT32)param->value == 0xffffffff)
1621         emit_not_m32(dst, MEMPARAMS);                                    // not   [dest]
1622      else
1623         emit_xor_m32_imm(dst, MEMPARAMS, param->value);                        // xor   [dest],param
1624   }
1625   else
1626   {
1627      int reg = param_select_register(REG_EAX, param, NULL);
1628      emit_mov_r32_p32(drcbe, dst, reg, param);                              // mov   reg,param
1629      emit_xor_m32_r32(dst, MEMPARAMS, reg);                                 // xor   [dest],reg
1630   }
1631}
1632
1633
1634/*-------------------------------------------------
1635    emit_shl_r32_p32 - shl operation to a 32-bit
1636    register from a 32-bit parameter
1637-------------------------------------------------*/
1638
1639static void emit_shl_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
1640{
1641   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1642   {
1643      if (inst->condflags != 0 && (UINT32)param->value == 0)
1644         /* skip */;
1645      else
1646         emit_shl_r32_imm(dst, reg, param->value);                           // shl   reg,param
1647   }
1648   else
1649   {
1650      emit_mov_r32_p32(drcbe, dst, REG_ECX, param);                           // mov   ecx,param
1651      emit_shl_r32_cl(dst, reg);                                          // shl   reg,cl
1652   }
1653}
1654
1655
1656/*-------------------------------------------------
1657    emit_shl_m32_p32 - shl operation to a 32-bit
1658    memory location from a 32-bit parameter
1659-------------------------------------------------*/
1660
1661static void emit_shl_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
1662{
1663   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1664   {
1665      if (inst->condflags != 0 && (UINT32)param->value == 0)
1666         /* skip */;
1667      else
1668         emit_shl_m32_imm(dst, MEMPARAMS, param->value);                        // shl   [dest],param
1669   }
1670   else
1671   {
1672      emit_mov_r32_p32(drcbe, dst, REG_ECX, param);                           // mov   ecx,param
1673      emit_shl_m32_cl(dst, MEMPARAMS);                                    // shl   [dest],cl
1674   }
1675}
1676
1677
1678/*-------------------------------------------------
1679    emit_shr_r32_p32 - shr operation to a 32-bit
1680    register from a 32-bit parameter
1681-------------------------------------------------*/
1682
1683static void emit_shr_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
1684{
1685   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1686   {
1687      if (inst->condflags != 0 && (UINT32)param->value == 0)
1688         /* skip */;
1689      else
1690         emit_shr_r32_imm(dst, reg, param->value);                           // shr   reg,param
1691   }
1692   else
1693   {
1694      emit_mov_r32_p32(drcbe, dst, REG_ECX, param);                           // mov   ecx,param
1695      emit_shr_r32_cl(dst, reg);                                          // shr   reg,cl
1696   }
1697}
1698
1699
1700/*-------------------------------------------------
1701    emit_shr_m32_p32 - shr operation to a 32-bit
1702    memory location from a 32-bit parameter
1703-------------------------------------------------*/
1704
1705static void emit_shr_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
1706{
1707   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1708   {
1709      if (inst->condflags != 0 && (UINT32)param->value == 0)
1710         /* skip */;
1711      else
1712         emit_shr_m32_imm(dst, MEMPARAMS, param->value);                        // shr   [dest],param
1713   }
1714   else
1715   {
1716      emit_mov_r32_p32(drcbe, dst, REG_ECX, param);                           // mov   ecx,param
1717      emit_shr_m32_cl(dst, MEMPARAMS);                                    // shr   [dest],cl
1718   }
1719}
1720
1721
1722/*-------------------------------------------------
1723    emit_sar_r32_p32 - sar operation to a 32-bit
1724    register from a 32-bit parameter
1725-------------------------------------------------*/
1726
1727static void emit_sar_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
1728{
1729   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1730   {
1731      if (inst->condflags != 0 && (UINT32)param->value == 0)
1732         /* skip */;
1733      else
1734         emit_sar_r32_imm(dst, reg, param->value);                           // sar   reg,param
1735   }
1736   else
1737   {
1738      emit_mov_r32_p32(drcbe, dst, REG_ECX, param);                           // mov   ecx,param
1739      emit_sar_r32_cl(dst, reg);                                          // sar   reg,cl
1740   }
1741}
1742
1743
1744/*-------------------------------------------------
1745    emit_sar_m32_p32 - sar operation to a 32-bit
1746    memory location from a 32-bit parameter
1747-------------------------------------------------*/
1748
1749static void emit_sar_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
1750{
1751   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1752   {
1753      if (inst->condflags != 0 && (UINT32)param->value == 0)
1754         /* skip */;
1755      else
1756         emit_sar_m32_imm(dst, MEMPARAMS, param->value);                        // sar   [dest],param
1757   }
1758   else
1759   {
1760      emit_mov_r32_p32(drcbe, dst, REG_ECX, param);                           // mov   ecx,param
1761      emit_sar_m32_cl(dst, MEMPARAMS);                                    // sar   [dest],cl
1762   }
1763}
1764
1765
1766/*-------------------------------------------------
1767    emit_rol_r32_p32 - rol operation to a 32-bit
1768    register from a 32-bit parameter
1769-------------------------------------------------*/
1770
1771static void emit_rol_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
1772{
1773   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1774   {
1775      if (inst->condflags != 0 && (UINT32)param->value == 0)
1776         /* skip */;
1777      else
1778         emit_rol_r32_imm(dst, reg, param->value);                           // rol   reg,param
1779   }
1780   else
1781   {
1782      emit_mov_r32_p32(drcbe, dst, REG_ECX, param);                           // mov   ecx,param
1783      emit_rol_r32_cl(dst, reg);                                          // rol   reg,cl
1784   }
1785}
1786
1787
1788/*-------------------------------------------------
1789    emit_rol_m32_p32 - rol operation to a 32-bit
1790    memory location from a 32-bit parameter
1791-------------------------------------------------*/
1792
1793static void emit_rol_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
1794{
1795   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1796   {
1797      if (inst->condflags != 0 && (UINT32)param->value == 0)
1798         /* skip */;
1799      else
1800         emit_rol_m32_imm(dst, MEMPARAMS, param->value);                        // rol   [dest],param
1801   }
1802   else
1803   {
1804      emit_mov_r32_p32(drcbe, dst, REG_ECX, param);                           // mov   ecx,param
1805      emit_rol_m32_cl(dst, MEMPARAMS);                                    // rol   [dest],cl
1806   }
1807}
1808
1809
1810/*-------------------------------------------------
1811    emit_ror_r32_p32 - ror operation to a 32-bit
1812    register from a 32-bit parameter
1813-------------------------------------------------*/
1814
1815static void emit_ror_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
1816{
1817   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1818   {
1819      if (inst->condflags != 0 && (UINT32)param->value == 0)
1820         /* skip */;
1821      else
1822         emit_ror_r32_imm(dst, reg, param->value);                           // ror   reg,param
1823   }
1824   else
1825   {
1826      emit_mov_r32_p32(drcbe, dst, REG_ECX, param);                           // mov   ecx,param
1827      emit_ror_r32_cl(dst, reg);                                          // ror   reg,cl
1828   }
1829}
1830
1831
1832/*-------------------------------------------------
1833    emit_ror_m32_p32 - ror operation to a 32-bit
1834    memory location from a 32-bit parameter
1835-------------------------------------------------*/
1836
1837static void emit_ror_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
1838{
1839   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1840   {
1841      if (inst->condflags != 0 && (UINT32)param->value == 0)
1842         /* skip */;
1843      else
1844         emit_ror_m32_imm(dst, MEMPARAMS, param->value);                        // ror   [dest],param
1845   }
1846   else
1847   {
1848      emit_mov_r32_p32(drcbe, dst, REG_ECX, param);                           // mov   ecx,param
1849      emit_ror_m32_cl(dst, MEMPARAMS);                                    // ror   [dest],cl
1850   }
1851}
1852
1853
1854/*-------------------------------------------------
1855    emit_rcl_r32_p32 - rcl operation to a 32-bit
1856    register from a 32-bit parameter
1857-------------------------------------------------*/
1858
1859static void emit_rcl_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
1860{
1861   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1862   {
1863      if (inst->condflags != 0 && (UINT32)param->value == 0)
1864         /* skip */;
1865      else
1866         emit_rcl_r32_imm(dst, reg, param->value);                           // rcl   reg,param
1867   }
1868   else
1869   {
1870      emit_mov_r32_p32(drcbe, dst, REG_ECX, param);                           // mov   ecx,param
1871      emit_rcl_r32_cl(dst, reg);                                          // rcl   reg,cl
1872   }
1873}
1874
1875
1876/*-------------------------------------------------
1877    emit_rcl_m32_p32 - rcl operation to a 32-bit
1878    memory location from a 32-bit parameter
1879-------------------------------------------------*/
1880
1881static void emit_rcl_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
1882{
1883   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1884   {
1885      if (inst->condflags != 0 && (UINT32)param->value == 0)
1886         /* skip */;
1887      else
1888         emit_rcl_m32_imm(dst, MEMPARAMS, param->value);                        // rcl   [dest],param
1889   }
1890   else
1891   {
1892      emit_mov_r32_p32(drcbe, dst, REG_ECX, param);                           // mov   ecx,param
1893      emit_rcl_m32_cl(dst, MEMPARAMS);                                    // rcl   [dest],cl
1894   }
1895}
1896
1897
1898/*-------------------------------------------------
1899    emit_rcr_r32_p32 - rcr operation to a 32-bit
1900    register from a 32-bit parameter
1901-------------------------------------------------*/
1902
1903static void emit_rcr_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
1904{
1905   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1906   {
1907      if (inst->condflags != 0 && (UINT32)param->value == 0)
1908         /* skip */;
1909      else
1910         emit_rcr_r32_imm(dst, reg, param->value);                           // rcr   reg,param
1911   }
1912   else
1913   {
1914      emit_mov_r32_p32(drcbe, dst, REG_ECX, param);                           // mov   ecx,param
1915      emit_rcr_r32_cl(dst, reg);                                          // rcr   reg,cl
1916   }
1917}
1918
1919
1920/*-------------------------------------------------
1921    emit_rcr_m32_p32 - rcr operation to a 32-bit
1922    memory location from a 32-bit parameter
1923-------------------------------------------------*/
1924
1925static void emit_rcr_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
1926{
1927   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1928   {
1929      if (inst->condflags != 0 && (UINT32)param->value == 0)
1930         /* skip */;
1931      else
1932         emit_rcr_m32_imm(dst, MEMPARAMS, param->value);                        // rcr   [dest],param
1933   }
1934   else
1935   {
1936      emit_mov_r32_p32(drcbe, dst, REG_ECX, param);                           // mov   ecx,param
1937      emit_rcr_m32_cl(dst, MEMPARAMS);                                    // rcr   [dest],cl
1938   }
1939}
1940
1941
1942
1943/***************************************************************************
1944    EMITTERS FOR 64-BIT OPERATIONS WITH PARAMETERS
1945***************************************************************************/
1946
1947/*-------------------------------------------------
1948    emit_mov_r64_p64 - move a 64-bit parameter
1949    into a register
1950-------------------------------------------------*/
1951
1952static void emit_mov_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param)
1953{
1954   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1955   {
1956      if (param->value == 0)
1957         emit_xor_r32_r32(dst, reg, reg);                                 // xor   reg,reg
1958      else
1959         emit_mov_r64_imm(dst, reg, param->value);                           // mov   reg,param
1960   }
1961   else if (param->type == DRCUML_PTYPE_MEMORY)
1962      emit_mov_r64_m64(dst, reg, MABS(drcbe, param->value));                     // mov   reg,[param]
1963   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
1964   {
1965      if (reg != param->value)
1966         emit_mov_r64_r64(dst, reg, param->value);                           // mov   reg,param
1967   }
1968}
1969
1970
1971/*-------------------------------------------------
1972    emit_mov_p64_r64 - move a registers into a
1973    64-bit parameter
1974-------------------------------------------------*/
1975
1976static void emit_mov_p64_r64(drcbe_state *drcbe, x86code **dst, const drcuml_parameter *param, UINT8 reg)
1977{
1978   assert(param->type != DRCUML_PTYPE_IMMEDIATE);
1979   if (param->type == DRCUML_PTYPE_MEMORY)
1980      emit_mov_m64_r64(dst, MABS(drcbe, param->value), reg);                     // mov   [param],reg
1981   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
1982   {
1983      if (reg != param->value)
1984         emit_mov_r64_r64(dst, param->value, reg);                           // mov   param,reg
1985   }
1986}
1987
1988
1989/*-------------------------------------------------
1990    emit_add_r64_p64 - add operation to a 64-bit
1991    register from a 64-bit parameter
1992-------------------------------------------------*/
1993
1994static void emit_add_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
1995{
1996   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1997   {
1998      if (inst->condflags != 0 || param->value != 0)
1999      {
2000         if (short_immediate(param->value))
2001            emit_add_r64_imm(dst, reg, param->value);                        // add   reg,param
2002         else
2003         {
2004            emit_mov_r64_imm(dst, REG_R11, param->value);                     // mov   r11,param
2005            emit_add_r64_r64(dst, reg, REG_R11);                           // add   reg,r11
2006         }
2007      }
2008   }
2009   else if (param->type == DRCUML_PTYPE_MEMORY)
2010      emit_add_r64_m64(dst, reg, MABS(drcbe, param->value));                     // add   reg,[param]
2011   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
2012      emit_add_r64_r64(dst, reg, param->value);                              // add   reg,param
2013}
2014
2015
2016/*-------------------------------------------------
2017    emit_add_m64_p64 - add operation to a 64-bit
2018    memory location from a 64-bit parameter
2019-------------------------------------------------*/
2020
2021static void emit_add_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
2022{
2023   if (param->type == DRCUML_PTYPE_IMMEDIATE)
2024   {
2025      if (inst->condflags != 0 || param->value != 0)
2026      {
2027         if (short_immediate(param->value))
2028            emit_add_m64_imm(dst, MEMPARAMS, param->value);                     // add   [mem],param
2029         else
2030         {
2031            emit_mov_r64_imm(dst, REG_R11, param->value);                     // mov   r11,param
2032            emit_add_m64_r64(dst, MEMPARAMS, REG_R11);                        // add   [mem],r11
2033         }
2034      }
2035   }
2036   else
2037   {
2038      int reg = param_select_register(REG_EAX, param, NULL);
2039      emit_mov_r64_p64(drcbe, dst, reg, param);                              // mov   reg,param
2040      emit_add_m64_r64(dst, MEMPARAMS, reg);                                 // add   [dest],reg
2041   }
2042}
2043
2044
2045/*-------------------------------------------------
2046    emit_adc_r64_p64 - adc operation to a 64-bit
2047    register from a 64-bit parameter
2048-------------------------------------------------*/
2049
2050static void emit_adc_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
2051{
2052   if (param->type == DRCUML_PTYPE_IMMEDIATE)
2053   {
2054      if (short_immediate(param->value))
2055         emit_adc_r64_imm(dst, reg, param->value);                           // adc   reg,param
2056      else
2057      {
2058         emit_mov_r64_imm(dst, REG_R11, param->value);                        // mov   r11,param
2059         emit_adc_r64_r64(dst, reg, REG_R11);                              // adc   reg,r11
2060      }
2061   }
2062   else if (param->type == DRCUML_PTYPE_MEMORY)
2063      emit_adc_r64_m64(dst, reg, MABS(drcbe, param->value));                     // adc   reg,[param]
2064   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
2065      emit_adc_r64_r64(dst, reg, param->value);                              // adc   reg,param
2066}
2067
2068
2069/*-------------------------------------------------
2070    emit_adc_m64_p64 - adc operation to a 64-bit
2071    memory locaiton from a 64-bit parameter
2072-------------------------------------------------*/
2073
2074static void emit_adc_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
2075{
2076   if (param->type == DRCUML_PTYPE_IMMEDIATE && short_immediate(param->value))
2077      emit_adc_m64_imm(dst, MEMPARAMS, param->value);                           // adc   [mem],param
2078   else
2079   {
2080      int reg = param_select_register(REG_EAX, param, NULL);
2081      emit_mov_r64_p64(drcbe, dst, reg, param);                              // mov   reg,param
2082      emit_adc_m64_r64(dst, MEMPARAMS, reg);                                 // adc   [dest],reg
2083   }
2084}
2085
2086
2087/*-------------------------------------------------
2088    emit_sub_r64_p64 - sub operation to a 64-bit
2089    register from a 64-bit parameter
2090-------------------------------------------------*/
2091
2092static void emit_sub_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
2093{
2094   if (param->type == DRCUML_PTYPE_IMMEDIATE)
2095   {
2096      if (inst->condflags != 0 || param->value != 0)
2097      {
2098         if (short_immediate(param->value))
2099            emit_sub_r64_imm(dst, reg, param->value);                        // sub   reg,param
2100         else
2101         {
2102            emit_mov_r64_imm(dst, REG_R11, param->value);                     // mov   r11,param
2103            emit_sub_r64_r64(dst, reg, REG_R11);                           // sub   reg,r11
2104         }
2105      }
2106   }
2107   else if (param->type == DRCUML_PTYPE_MEMORY)
2108      emit_sub_r64_m64(dst, reg, MABS(drcbe, param->value));                     // sub   reg,[param]
2109   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
2110      emit_sub_r64_r64(dst, reg, param->value);                              // sub   reg,param
2111}
2112
2113
2114/*-------------------------------------------------
2115    emit_sub_m64_p64 - sub operation to a 64-bit
2116    memory location from a 64-bit parameter
2117-------------------------------------------------*/
2118
2119static void emit_sub_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
2120{
2121   if (param->type == DRCUML_PTYPE_IMMEDIATE)
2122   {
2123      if (inst->condflags != 0 || param->value != 0)
2124      {
2125         if (short_immediate(param->value))
2126            emit_sub_m64_imm(dst, MEMPARAMS, param->value);                     // sub   [mem],param
2127         else
2128         {
2129            emit_mov_r64_imm(dst, REG_R11, param->value);                     // mov   r11,param
2130            emit_sub_m64_r64(dst, MEMPARAMS, REG_R11);                        // sub   [mem],r11
2131         }
2132      }
2133   }
2134   else
2135   {
2136      int reg = param_select_register(REG_EAX, param, NULL);
2137      emit_mov_r64_p64(drcbe, dst, reg, param);                              // mov   reg,param
2138      emit_sub_m64_r64(dst, MEMPARAMS, reg);                                 // sub   [dest],reg
2139   }
2140}
2141
2142
2143/*-------------------------------------------------
2144    emit_sbb_r64_p64 - sbb operation to a 64-bit
2145    register from a 64-bit parameter
2146-------------------------------------------------*/
2147
2148static void emit_sbb_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
2149{
2150   if (param->type == DRCUML_PTYPE_IMMEDIATE)
2151   {
2152      if (short_immediate(param->value))
2153         emit_sbb_r64_imm(dst, reg, param->value);                           // sbb   reg,param
2154      else
2155      {
2156         emit_mov_r64_imm(dst, REG_R11, param->value);                        // mov   r11,param
2157         emit_sbb_r64_r64(dst, reg, REG_R11);                              // sbb   reg,r11
2158      }
2159   }
2160   else if (param->type == DRCUML_PTYPE_MEMORY)
2161      emit_sbb_r64_m64(dst, reg, MABS(drcbe, param->value));                     // sbb   reg,[param]
2162   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
2163      emit_sbb_r64_r64(dst, reg, param->value);                              // sbb   reg,param
2164}
2165
2166
2167/*-------------------------------------------------
2168    emit_sbb_m64_p64 - sbb operation to a 64-bit
2169    memory location from a 64-bit parameter
2170-------------------------------------------------*/
2171
2172static void emit_sbb_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
2173{
2174   if (param->type == DRCUML_PTYPE_IMMEDIATE && short_immediate(param->value))
2175      emit_sbb_m64_imm(dst, MEMPARAMS, param->value);                           // sbb   [mem],param
2176   else
2177   {
2178      int reg = param_select_register(REG_EAX, param, NULL);
2179      emit_mov_r64_p64(drcbe, dst, reg, param);                              // mov   reg,param
2180      emit_sbb_m64_r64(dst, MEMPARAMS, reg);                                 // sbb   [dest],reg
2181   }
2182}
2183
2184
2185/*-------------------------------------------------
2186    emit_cmp_r64_p64 - cmp operation to a 64-bit
2187    register from a 64-bit parameter
2188-------------------------------------------------*/
2189
2190static void emit_cmp_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
2191{
2192   if (param->type == DRCUML_PTYPE_IMMEDIATE)
2193   {
2194      if (short_immediate(param->value))
2195         emit_cmp_r64_imm(dst, reg, param->value);                           // cmp   reg,param
2196      else
2197      {
2198         emit_mov_r64_imm(dst, REG_R11, param->value);                        // mov   r11,param
2199         emit_cmp_r64_r64(dst, reg, REG_R11);                              // cmp   reg,r11
2200      }
2201   }
2202   else if (param->type == DRCUML_PTYPE_MEMORY)
2203      emit_cmp_r64_m64(dst, reg, MABS(drcbe, param->value));                     // cmp   reg,[param]
2204   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
2205      emit_cmp_r64_r64(dst, reg, param->value);                              // cmp   reg,param
2206}
2207
2208
2209/*-------------------------------------------------
2210    emit_cmp_m64_p64 - cmp operation to a 64-bit
2211    memory location from a 64-bit parameter
2212-------------------------------------------------*/
2213
2214static void emit_cmp_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
2215{
2216   if (param->type == DRCUML_PTYPE_IMMEDIATE && short_immediate(param->value))
2217      emit_cmp_m64_imm(dst, MEMPARAMS, param->value);                           // cmp   [dest],param
2218   else
2219   {
2220      int reg = param_select_register(REG_EAX, param, NULL);
2221      emit_mov_r64_p64(drcbe, dst, reg, param);                              // mov   reg,param
2222      emit_cmp_m64_r64(dst, MEMPARAMS, reg);                                 // cmp   [dest],reg
2223   }
2224}
2225
2226
2227/*-------------------------------------------------
2228    emit_and_r64_p64 - and operation to a 64-bit
2229    register from a 64-bit parameter
2230-------------------------------------------------*/
2231
2232static void emit_and_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
2233{
2234   if (param->type == DRCUML_PTYPE_IMMEDIATE)
2235   {
2236      if (inst->condflags != 0 || param->value != U64(0xffffffffffffffff))
2237      {
2238         if (short_immediate(param->value))
2239            emit_and_r64_imm(dst, reg, param->value);                        // and   reg,param
2240         else
2241         {
2242            emit_mov_r64_imm(dst, REG_R11, param->value);                     // mov   r11,param
2243            emit_and_r64_r64(dst, reg, REG_R11);                           // and   reg,r11
2244         }
2245      }
2246   }
2247   else if (param->type == DRCUML_PTYPE_MEMORY)
2248      emit_and_r64_m64(dst, reg, MABS(drcbe, param->value));                     // and   reg,[param]
2249   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
2250      emit_and_r64_r64(dst, reg, param->value);                              // and   reg,param
2251}
2252
2253
2254/*-------------------------------------------------
2255    emit_and_m64_p64 - and operation to a 64-bit
2256    memory location from a 64-bit parameter
2257-------------------------------------------------*/
2258
2259static void emit_and_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
2260{
2261   if (param->type == DRCUML_PTYPE_IMMEDIATE)
2262   {
2263      if (inst->condflags != 0 || param->value != U64(0xffffffffffffffff))
2264      {
2265         if (short_immediate(param->value))
2266            emit_and_m64_imm(dst, MEMPARAMS, param->value);                     // and   [mem],param
2267         else
2268         {
2269            emit_mov_r64_imm(dst, REG_R11, param->value);                     // mov   r11,param
2270            emit_and_m64_r64(dst, MEMPARAMS, REG_R11);                        // and   [mem],r11
2271         }
2272      }
2273   }
2274   else
2275   {
2276      int reg = param_select_register(REG_EAX, param, NULL);
2277      emit_mov_r64_p64(drcbe, dst, reg, param);                              // mov   reg,param
2278      emit_and_m64_r64(dst, MEMPARAMS, reg);                                 // and   [dest],reg
2279   }
2280}
2281
2282
2283/*-------------------------------------------------
2284    emit_test_r64_p64 - test operation to a 64-bit
2285    register from a 64-bit parameter
2286-------------------------------------------------*/
2287
2288static void emit_test_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
2289{
2290   if (param->type == DRCUML_PTYPE_IMMEDIATE)
2291   {
2292      if (short_immediate(param->value))
2293         emit_test_r64_imm(dst, reg, param->value);                           // test  reg,param
2294      else
2295      {
2296         emit_mov_r64_imm(dst, REG_R11, param->value);                        // mov   r11,param
2297         emit_test_r64_r64(dst, reg, REG_R11);                              // test  reg,r11
2298      }
2299   }
2300   else if (param->type == DRCUML_PTYPE_MEMORY)
2301      emit_test_m64_r64(dst, MABS(drcbe, param->value), reg);                     // test  [param],reg
2302   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
2303      emit_test_r64_r64(dst, reg, param->value);                              // test  reg,param
2304}
2305
2306
2307/*-------------------------------------------------
2308    emit_test_m64_p64 - test operation to a 64-bit
2309    memory location from a 64-bit parameter
2310-------------------------------------------------*/
2311
2312static void emit_test_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
2313{
2314   if (param->type == DRCUML_PTYPE_IMMEDIATE && short_immediate(param->value))
2315      emit_test_m64_imm(dst, MEMPARAMS, param->value);                        // test  [dest],param
2316   else if (param->type == DRCUML_PTYPE_MEMORY)
2317   {
2318      emit_mov_r64_p64(drcbe, dst, REG_EAX, param);                           // mov   reg,param
2319      emit_test_m64_r64(dst, MEMPARAMS, REG_EAX);                              // test  [dest],reg
2320   }
2321   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
2322      emit_test_m64_r64(dst, MEMPARAMS, param->value);                        // test  [dest],param
2323}
2324
2325
2326/*-------------------------------------------------
2327    emit_or_r64_p64 - or operation to a 64-bit
2328    register from a 64-bit parameter
2329-------------------------------------------------*/
2330
2331static void emit_or_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
2332{
2333   if (param->type == DRCUML_PTYPE_IMMEDIATE)
2334   {
2335      if (inst->condflags != 0 || param->value != 0)
2336      {
2337         if (short_immediate(param->value))
2338            emit_or_r64_imm(dst, reg, param->value);                        // or    reg,param
2339         else
2340         {
2341            emit_mov_r64_imm(dst, REG_R11, param->value);                     // mov   r11,param
2342            emit_or_r64_r64(dst, reg, REG_R11);                              // or    reg,r11
2343         }
2344      }
2345   }
2346   else if (param->type == DRCUML_PTYPE_MEMORY)
2347      emit_or_r64_m64(dst, reg, MABS(drcbe, param->value));                     // or    reg,[param]
2348   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
2349      emit_or_r64_r64(dst, reg, param->value);                              // or    reg,param
2350}
2351
2352
2353/*-------------------------------------------------
2354    emit_or_m64_p64 - or operation to a 64-bit
2355    memory location from a 64-bit parameter
2356-------------------------------------------------*/
2357
2358static void emit_or_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
2359{
2360   if (param->type == DRCUML_PTYPE_IMMEDIATE)
2361   {
2362      if (inst->condflags != 0 || param->value != 0)
2363      {
2364         if (short_immediate(param->value))
2365            emit_or_m64_imm(dst, MEMPARAMS, param->value);                     // or    [mem],param
2366         else
2367         {
2368            emit_mov_r64_imm(dst, REG_R11, param->value);                     // mov   r11,param
2369            emit_or_m64_r64(dst, MEMPARAMS, REG_R11);                        // or    [mem],r11
2370         }
2371      }
2372   }
2373   else
2374   {
2375      int reg = param_select_register(REG_EAX, param, NULL);
2376      emit_mov_r64_p64(drcbe, dst, reg, param);                              // mov   reg,param
2377      emit_or_m64_r64(dst, MEMPARAMS, reg);                                 // or    [dest],reg
2378   }
2379}
2380
2381
2382/*-------------------------------------------------
2383    emit_xor_r64_p64 - xor operation to a 64-bit
2384    register from a 64-bit parameter
2385-------------------------------------------------*/
2386
2387static void emit_xor_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
2388{
2389   if (param->type == DRCUML_PTYPE_IMMEDIATE)
2390   {
2391      if (inst->condflags != 0 || param->value != 0)
2392      {
2393         if (param->value == U64(0xffffffffffffffff))
2394            emit_not_r64(dst, reg);                                       // not   reg
2395         else if (short_immediate(param->value))
2396            emit_xor_r64_imm(dst, reg, param->value);                        // xor   reg,param
2397         else
2398         {
2399            emit_mov_r64_imm(dst, REG_R11, param->value);                     // mov   r11,param
2400            emit_xor_r64_r64(dst, reg, REG_R11);                           // xor   reg,r11
2401         }
2402      }
2403   }
2404   else if (param->type == DRCUML_PTYPE_MEMORY)
2405      emit_xor_r64_m64(dst, reg, MABS(drcbe, param->value));                     // xor   reg,[param]
2406   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
2407      emit_xor_r64_r64(dst, reg, param->value);                              // xor   reg,param
2408}
2409
2410
2411/*-------------------------------------------------
2412    emit_xor_m64_p64 - xor operation to a 64-bit
2413    memory location from a 64-bit parameter
2414-------------------------------------------------*/
2415
2416static void emit_xor_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
2417{
2418   if (param->type == DRCUML_PTYPE_IMMEDIATE)
2419   {
2420      if (inst->condflags != 0 || param->value != 0)
2421      {
2422         if (param->value == U64(0xffffffffffffffff))
2423            emit_not_m64(dst, MEMPARAMS);                                 // not   [mem]
2424         else if (short_immediate(param->value))
2425            emit_xor_m64_imm(dst, MEMPARAMS, param->value);                     // xor   [mem],param
2426         else
2427         {
2428            emit_mov_r64_imm(dst, REG_R11, param->value);                     // mov   r11,param
2429            emit_xor_m64_r64(dst, MEMPARAMS, REG_R11);                        // xor   [mem],r11
2430         }
2431      }
2432   }
2433   else
2434   {
2435      int reg = param_select_register(REG_EAX, param, NULL);
2436      emit_mov_r64_p64(drcbe, dst, reg, param);                              // mov   reg,param
2437      emit_xor_m64_r64(dst, MEMPARAMS, reg);                                 // xor   [dest],reg
2438   }
2439}
2440
2441
2442/*-------------------------------------------------
2443    emit_shl_r64_p64 - shl operation to a 64-bit
2444    register from a 64-bit parameter
2445-------------------------------------------------*/
2446
2447static void emit_shl_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
2448{
2449   if (param->type == DRCUML_PTYPE_IMMEDIATE)
2450   {
2451      if (inst->condflags != 0 && (UINT32)param->value == 0)
2452         /* skip */;
2453      else
2454         emit_shl_r64_imm(dst, reg, param->value);                           // shl   reg,param
2455   }
2456   else
2457   {
2458      emit_mov_r64_p64(drcbe, dst, REG_RCX, param);                           // mov   rcx,param
2459      emit_shl_r64_cl(dst, reg);                                          // shl   reg,cl
2460   }
2461}
2462
2463
2464/*-------------------------------------------------
2465    emit_shl_m64_p64 - shl operation to a 64-bit
2466    memory location from a 64-bit parameter
2467-------------------------------------------------*/
2468
2469static void emit_shl_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
2470{
2471   if (param->type == DRCUML_PTYPE_IMMEDIATE)
2472   {
2473      if (inst->condflags != 0 && (UINT64)param->value == 0)
2474         /* skip */;
2475      else
2476         emit_shl_m64_imm(dst, MEMPARAMS, param->value);                        // shl   [dest],param
2477   }
2478   else
2479   {
2480      emit_mov_r64_p64(drcbe, dst, REG_RCX, param);                           // mov   rcx,param
2481      emit_shl_m64_cl(dst, MEMPARAMS);                                    // shl   [dest],cl
2482   }
2483}
2484
2485
2486/*-------------------------------------------------
2487    emit_shr_r64_p64 - shr operation to a 64-bit
2488    register from a 64-bit parameter
2489-------------------------------------------------*/
2490
2491static void emit_shr_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
2492{
2493   if (param->type == DRCUML_PTYPE_IMMEDIATE)
2494   {
2495      if (inst->condflags != 0 && (UINT32)param->value == 0)
2496         /* skip */;
2497      else
2498         emit_shr_r64_imm(dst, reg, param->value);                           // shr   reg,param
2499   }
2500   else
2501   {
2502      emit_mov_r64_p64(drcbe, dst, REG_RCX, param);                           // mov   rcx,param
2503      emit_shr_r64_cl(dst, reg);                                          // shr   reg,cl
2504   }
2505}
2506
2507
2508/*-------------------------------------------------
2509    emit_shr_m64_p64 - shr operation to a 64-bit
2510    memory location from a 64-bit parameter
2511-------------------------------------------------*/
2512
2513static void emit_shr_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
2514{
2515   if (param->type == DRCUML_PTYPE_IMMEDIATE)
2516   {
2517      if (inst->condflags != 0 && (UINT64)param->value == 0)
2518         /* skip */;
2519      else
2520         emit_shr_m64_imm(dst, MEMPARAMS, param->value);                        // shr   [dest],param
2521   }
2522   else
2523   {
2524      emit_mov_r64_p64(drcbe, dst, REG_RCX, param);                           // mov   rcx,param
2525      emit_shr_m64_cl(dst, MEMPARAMS);                                    // shr   [dest],cl
2526   }
2527}
2528
2529
2530/*-------------------------------------------------
2531    emit_sar_r64_p64 - sar operation to a 64-bit
2532    register from a 64-bit parameter
2533-------------------------------------------------*/
2534
2535static void emit_sar_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
2536{
2537   if (param->type == DRCUML_PTYPE_IMMEDIATE)
2538   {
2539      if (inst->condflags != 0 && (UINT32)param->value == 0)
2540         /* skip */;
2541      else
2542         emit_sar_r64_imm(dst, reg, param->value);                           // sar   reg,param
2543   }
2544   else
2545   {
2546      emit_mov_r64_p64(drcbe, dst, REG_RCX, param);                           // mov   rcx,param
2547      emit_sar_r64_cl(dst, reg);                                          // sar   reg,cl
2548   }
2549}
2550
2551
2552/*-------------------------------------------------
2553    emit_sar_m64_p64 - sar operation to a 64-bit
2554    memory location from a 64-bit parameter
2555-------------------------------------------------*/
2556
2557static void emit_sar_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
2558{
2559   if (param->type == DRCUML_PTYPE_IMMEDIATE)
2560   {
2561      if (inst->condflags != 0 && (UINT64)param->value == 0)
2562         /* skip */;
2563      else
2564         emit_sar_m64_imm(dst, MEMPARAMS, param->value);                        // sar   [dest],param
2565   }
2566   else
2567   {
2568      emit_mov_r64_p64(drcbe, dst, REG_RCX, param);                           // mov   rcx,param
2569      emit_sar_m64_cl(dst, MEMPARAMS);                                    // sar   [dest],cl
2570   }
2571}
2572
2573
2574/*-------------------------------------------------
2575    emit_rol_r64_p64 - rol operation to a 64-bit
2576    register from a 64-bit parameter
2577-------------------------------------------------*/
2578
2579static void emit_rol_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
2580{
2581   if (param->type == DRCUML_PTYPE_IMMEDIATE)
2582   {
2583      if (inst->condflags != 0 && (UINT32)param->value == 0)
2584         /* skip */;
2585      else
2586         emit_rol_r64_imm(dst, reg, param->value);                           // rol   reg,param
2587   }
2588   else
2589   {
2590      emit_mov_r64_p64(drcbe, dst, REG_RCX, param);                           // mov   rcx,param
2591      emit_rol_r64_cl(dst, reg);                                          // rol   reg,cl
2592   }
2593}
2594
2595
2596/*-------------------------------------------------
2597    emit_rol_m64_p64 - rol operation to a 64-bit
2598    memory location from a 64-bit parameter
2599-------------------------------------------------*/
2600
2601static void emit_rol_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
2602{
2603   if (param->type == DRCUML_PTYPE_IMMEDIATE)
2604   {
2605      if (inst->condflags != 0 && (UINT64)param->value == 0)
2606         /* skip */;
2607      else
2608         emit_rol_m64_imm(dst, MEMPARAMS, param->value);                        // rol   [dest],param
2609   }
2610   else
2611   {
2612      emit_mov_r64_p64(drcbe, dst, REG_RCX, param);                           // mov   rcx,param
2613      emit_rol_m64_cl(dst, MEMPARAMS);                                    // rol   [dest],cl
2614   }
2615}
2616
2617
2618/*-------------------------------------------------
2619    emit_ror_r64_p64 - ror operation to a 64-bit
2620    register from a 64-bit parameter
2621-------------------------------------------------*/
2622
2623static void emit_ror_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
2624{
2625   if (param->type == DRCUML_PTYPE_IMMEDIATE)
2626   {
2627      if (inst->condflags != 0 && (UINT32)param->value == 0)
2628         /* skip */;
2629      else
2630         emit_ror_r64_imm(dst, reg, param->value);                           // ror   reg,param
2631   }
2632   else
2633   {
2634      emit_mov_r64_p64(drcbe, dst, REG_RCX, param);                           // mov   rcx,param
2635      emit_ror_r64_cl(dst, reg);                                          // ror   reg,cl
2636   }
2637}
2638
2639
2640/*-------------------------------------------------
2641    emit_ror_m64_p64 - ror operation to a 64-bit
2642    memory location from a 64-bit parameter
2643-------------------------------------------------*/
2644
2645static void emit_ror_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
2646{
2647   if (param->type == DRCUML_PTYPE_IMMEDIATE)
2648   {
2649      if (inst->condflags != 0 && (UINT64)param->value == 0)
2650         /* skip */;
2651      else
2652         emit_ror_m64_imm(dst, MEMPARAMS, param->value);                        // ror   [dest],param
2653   }
2654   else
2655   {
2656      emit_mov_r64_p64(drcbe, dst, REG_RCX, param);                           // mov   rcx,param
2657      emit_ror_m64_cl(dst, MEMPARAMS);                                    // ror   [dest],cl
2658   }
2659}
2660
2661
2662/*-------------------------------------------------
2663    emit_rcl_r64_p64 - rcl operation to a 64-bit
2664    register from a 64-bit parameter
2665-------------------------------------------------*/
2666
2667static void emit_rcl_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
2668{
2669   if (param->type == DRCUML_PTYPE_IMMEDIATE)
2670   {
2671      if (inst->condflags != 0 && (UINT32)param->value == 0)
2672         /* skip */;
2673      else
2674         emit_rcl_r64_imm(dst, reg, param->value);                           // rcl   reg,param
2675   }
2676   else
2677   {
2678      emit_mov_r64_p64(drcbe, dst, REG_RCX, param);                           // mov   rcx,param
2679      emit_rcl_r64_cl(dst, reg);                                          // rcl   reg,cl
2680   }
2681}
2682
2683
2684/*-------------------------------------------------
2685    emit_rcl_m64_p64 - rcl operation to a 64-bit
2686    memory location from a 64-bit parameter
2687-------------------------------------------------*/
2688
2689static void emit_rcl_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
2690{
2691   if (param->type == DRCUML_PTYPE_IMMEDIATE)
2692   {
2693      if (inst->condflags != 0 && (UINT64)param->value == 0)
2694         /* skip */;
2695      else
2696         emit_rcl_m64_imm(dst, MEMPARAMS, param->value);                        // rcl   [dest],param
2697   }
2698   else
2699   {
2700      emit_mov_r64_p64(drcbe, dst, REG_RCX, param);                           // mov   rcx,param
2701      emit_rcl_m64_cl(dst, MEMPARAMS);                                    // rcl   [dest],cl
2702   }
2703}
2704
2705
2706/*-------------------------------------------------
2707    emit_rcr_r64_p64 - rcr operation to a 64-bit
2708    register from a 64-bit parameter
2709-------------------------------------------------*/
2710
2711static void emit_rcr_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
2712{
2713   if (param->type == DRCUML_PTYPE_IMMEDIATE)
2714   {
2715      if (inst->condflags != 0 && (UINT32)param->value == 0)
2716         /* skip */;
2717      else
2718         emit_rcr_r64_imm(dst, reg, param->value);                           // rcr   reg,param
2719   }
2720   else
2721   {
2722      emit_mov_r64_p64(drcbe, dst, REG_RCX, param);                           // mov   rcx,param
2723      emit_rcr_r64_cl(dst, reg);                                          // rcr   reg,cl
2724   }
2725}
2726
2727
2728/*-------------------------------------------------
2729    emit_rcr_m64_p64 - rcr operation to a 64-bit
2730    memory location from a 64-bit parameter
2731-------------------------------------------------*/
2732
2733static void emit_rcr_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
2734{
2735   if (param->type == DRCUML_PTYPE_IMMEDIATE)
2736   {
2737      if (inst->condflags != 0 && (UINT64)param->value == 0)
2738         /* skip */;
2739      else
2740         emit_rcr_m64_imm(dst, MEMPARAMS, param->value);                        // rcr   [dest],param
2741   }
2742   else
2743   {
2744      emit_mov_r64_p64(drcbe, dst, REG_RCX, param);                           // mov   rcx,param
2745      emit_rcr_m64_cl(dst, MEMPARAMS);                                    // rcr   [dest],cl
2746   }
2747}
2748
2749
2750
2751/***************************************************************************
2752    EMITTERS FOR FLOATING POINT OPERATIONS WITH PARAMETERS
2753***************************************************************************/
2754
2755/*-------------------------------------------------
2756    emit_movss_r128_p32 - move a 32-bit parameter
2757    into a register
2758-------------------------------------------------*/
2759
2760static void emit_movss_r128_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param)
2761{
2762   assert(param->type != DRCUML_PTYPE_IMMEDIATE);
2763   if (param->type == DRCUML_PTYPE_MEMORY)
2764      emit_movss_r128_m32(dst, reg, MABS(drcbe, param->value));                  // movss reg,[param]
2765   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
2766   {
2767      if (reg != param->value)
2768         emit_movss_r128_r128(dst, reg, param->value);                        // movss reg,param
2769   }
2770}
2771
2772
2773/*-------------------------------------------------
2774    emit_movss_p32_r128 - move a register into a
2775    32-bit parameter
2776-------------------------------------------------*/
2777
2778static void emit_movss_p32_r128(drcbe_state *drcbe, x86code **dst, const drcuml_parameter *param, UINT8 reg)
2779{
2780   assert(param->type != DRCUML_PTYPE_IMMEDIATE);
2781   if (param->type == DRCUML_PTYPE_MEMORY)
2782      emit_movss_m32_r128(dst, MABS(drcbe, param->value), reg);                  // movss [param],reg
2783   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
2784   {
2785      if (reg != param->value)
2786         emit_movss_r128_r128(dst, param->value, reg);                        // movss param,reg
2787   }
2788}
2789
2790
2791/*-------------------------------------------------
2792    emit_movsd_r128_p64 - move a 64-bit parameter
2793    into a register
2794-------------------------------------------------*/
2795
2796static void emit_movsd_r128_p64(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param)
2797{
2798   assert(param->type != DRCUML_PTYPE_IMMEDIATE);
2799   if (param->type == DRCUML_PTYPE_MEMORY)
2800      emit_movsd_r128_m64(dst, reg, MABS(drcbe, param->value));                  // movsd reg,[param]
2801   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
2802   {
2803      if (reg != param->value)
2804         emit_movsd_r128_r128(dst, reg, param->value);                        // movsd reg,param
2805   }
2806}
2807
2808
2809/*-------------------------------------------------
2810    emit_movsd_p64_r128 - move a register into a
2811    64-bit parameter
2812-------------------------------------------------*/
2813
2814static void emit_movsd_p64_r128(drcbe_state *drcbe, x86code **dst, const drcuml_parameter *param, UINT8 reg)
2815{
2816   assert(param->type != DRCUML_PTYPE_IMMEDIATE);
2817   if (param->type == DRCUML_PTYPE_MEMORY)
2818      emit_movsd_m64_r128(dst, MABS(drcbe, param->value), reg);                  // movsd [param],reg
2819   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
2820   {
2821      if (reg != param->value)
2822         emit_movsd_r128_r128(dst, param->value, reg);                        // movsd param,reg
2823   }
2824}
2825
2826
2827
2828/***************************************************************************
2829    HELPERS TO CONVERT OTHER OPERATIONS TO MOVES
2830***************************************************************************/
2831
2832/*-------------------------------------------------
2833    convert_to_mov_imm - convert an instruction
2834    into a mov immediate
2835-------------------------------------------------*/
2836
2837static x86code *convert_to_mov_imm(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst, const drcuml_parameter *dstp, UINT64 imm)
2838{
2839   drcuml_instruction temp = *inst;
2840   temp.numparams = 2;
2841   temp.param[0] = *dstp;
2842   temp.param[1].type = DRCUML_PTYPE_IMMEDIATE;
2843   temp.param[1].value = imm;
2844   return op_mov(drcbe, dst, &temp);
2845}
2846
2847
2848/*-------------------------------------------------
2849    convert_to_mov_src1 - convert an instruction
2850    into a mov src1
2851-------------------------------------------------*/
2852
2853static x86code *convert_to_mov_src1(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst, const drcuml_parameter *dstp, const drcuml_parameter *srcp)
2854{
2855   drcuml_instruction temp = *inst;
2856   temp.numparams = 2;
2857   temp.param[0] = *dstp;
2858   temp.param[1] = *srcp;
2859   return op_mov(drcbe, dst, &temp);
2860}
2861
2862
2863
2864/***************************************************************************
2865    OUT-OF-BAND CODE FIXUP CALLBACKS
2866***************************************************************************/
2867
2868/*-------------------------------------------------
2869    fixup_label - callback to fixup forward-
2870    referenced labels
2871-------------------------------------------------*/
2872
2873static void fixup_label(void *parameter, drccodeptr labelcodeptr)
2874{
2875   drccodeptr src = parameter;
2876
2877   /* find the end of the instruction */
2878   if (src[0] == 0xe3)
2879   {
2880      src += 1 + 1;
2881      src[-1] = labelcodeptr - src;
2882   }
2883   else if (src[0] == 0xe9)
2884   {
2885      src += 1 + 4;
2886      ((UINT32 *)src)[-1] = labelcodeptr - src;
2887   }
2888   else if (src[0] == 0x0f && (src[1] & 0xf0) == 0x80)
2889   {
2890      src += 2 + 4;
2891      ((UINT32 *)src)[-1] = labelcodeptr - src;
2892   }
2893   else
2894      fatalerror("fixup_label called with invalid jmp source!");
2895}
2896
2897
2898/*-------------------------------------------------
2899    fixup_exception - callback to perform cleanup
2900    and jump to an exception handler
2901-------------------------------------------------*/
2902
2903static void fixup_exception(drccodeptr *codeptr, void *param1, void *param2, void *param3)
2904{
2905   drcuml_parameter handp, exp;
2906   drcbe_state *drcbe = param1;
2907   drccodeptr src = param2;
2908   const drcuml_instruction *inst = param3;
2909   drccodeptr dst = *codeptr;
2910   drccodeptr *targetptr;
2911
2912   /* normalize parameters */
2913   param_normalize_2(drcbe, inst, &handp, PTYPE_M, &exp, PTYPE_MRI);
2914
2915   /* look up the handle target */
2916   targetptr = drcuml_handle_codeptr_addr((drcuml_codehandle *)handp.value);
2917
2918   /* first fixup the jump to get us here */
2919   ((UINT32 *)src)[-1] = dst - src;
2920
2921   /* then store the exception parameter */
2922   emit_mov_m32_p32(drcbe, &dst, MABS(drcbe, &drcbe->state.exp), &exp);            // mov   [exp],exp
2923
2924   /* push the original return address on the stack */
2925   emit_lea_r64_m64(&dst, REG_RAX, MABS(drcbe, src));                           // lea   rax,[return]
2926   emit_push_r64(&dst, REG_RAX);                                          // push  rax
2927   if (*targetptr != NULL)
2928      emit_jmp(&dst, *targetptr);                                          // jmp   *targetptr
2929   else
2930      emit_jmp_m64(&dst, MABS(drcbe, targetptr));                              // jmp   [targetptr]
2931
2932   *codeptr = dst;
2933}
2934
2935
2936
2937/***************************************************************************
2938    DEBUG HELPERS
2939***************************************************************************/
2940
2941/*-------------------------------------------------
2942    debug_log_hashjmp - callback to handle
2943    logging of hashjmps
2944-------------------------------------------------*/
2945
2946#if LOG_HASHJMPS
2947static void debug_log_hashjmp(int mode, offs_t pc)
2948{
2949   printf("mode=%d PC=%08X\n", mode, pc);
2950}
2951#endif
2952
2953
2954
2955/***************************************************************************
2956    COMPILE-TIME OPCODES
2957***************************************************************************/
2958
2959/*-------------------------------------------------
2960    op_handle - process a HANDLE opcode
2961-------------------------------------------------*/
2962
2963static x86code *op_handle(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
2964{
2965   assert(inst->numparams == 1);
2966   assert(inst->param[0].type == DRCUML_PTYPE_MEMORY);
2967
2968   /* register the current pointer for the handle */
2969   drcuml_handle_set_codeptr((drcuml_codehandle *)(FPTR)inst->param[0].value, dst);
2970
2971   /* by default, the handle points to prolog code that moves the stack pointer */
2972   emit_sub_r64_imm(&dst, REG_RSP, 40);                                    // sub   rsp,40
2973   return dst;
2974}
2975
2976
2977/*-------------------------------------------------
2978    op_hash - process a HASH opcode
2979-------------------------------------------------*/
2980
2981static x86code *op_hash(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
2982{
2983   assert(inst->numparams == 2);
2984   assert(inst->param[0].type == DRCUML_PTYPE_IMMEDIATE);
2985   assert(inst->param[1].type == DRCUML_PTYPE_IMMEDIATE);
2986
2987   /* register the current pointer for the mode/PC */
2988   drchash_set_codeptr(drcbe->hash, inst->param[0].value, inst->param[1].value, dst);
2989   return dst;
2990}
2991
2992
2993/*-------------------------------------------------
2994    op_label - process a LABEL opcode
2995-------------------------------------------------*/
2996
2997static x86code *op_label(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
2998{
2999   assert(inst->numparams == 1);
3000   assert(inst->param[0].type == DRCUML_PTYPE_IMMEDIATE);
3001
3002   /* register the current pointer for the label */
3003   drclabel_set_codeptr(drcbe->labels, inst->param[0].value, dst);
3004   return dst;
3005}
3006
3007
3008/*-------------------------------------------------
3009    op_comment - process a COMMENT opcode
3010-------------------------------------------------*/
3011
3012static x86code *op_comment(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3013{
3014   assert(inst->numparams == 1);
3015   assert(inst->param[0].type == DRCUML_PTYPE_MEMORY);
3016
3017   /* do nothing */
3018   return dst;
3019}
3020
3021
3022/*-------------------------------------------------
3023    op_mapvar - process a MAPVAR opcode
3024-------------------------------------------------*/
3025
3026static x86code *op_mapvar(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3027{
3028   assert(inst->numparams == 2);
3029   assert(inst->param[0].type == DRCUML_PTYPE_MAPVAR);
3030   assert(inst->param[1].type == DRCUML_PTYPE_IMMEDIATE);
3031
3032   /* set the value of the specified mapvar */
3033   drcmap_set_value(drcbe->map, dst, inst->param[0].value, inst->param[1].value);
3034   return dst;
3035}
3036
3037
3038
3039/***************************************************************************
3040    CONTROL FLOW OPCODES
3041***************************************************************************/
3042
3043/*-------------------------------------------------
3044    op_debug - process a DEBUG opcode
3045-------------------------------------------------*/
3046
3047static x86code *op_debug(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3048{
3049#ifdef ENABLE_DEBUGGER
3050   if (Machine->debug_mode)
3051   {
3052      drcuml_parameter pcp;
3053
3054      /* validate instruction */
3055      assert(inst->size == 4);
3056      assert(inst->condflags == DRCUML_COND_ALWAYS);
3057
3058      /* normalize parameters */
3059      param_normalize_1(drcbe, inst, &pcp, PTYPE_MRI);
3060
3061      /* push the parameter */
3062      emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &pcp);                        // mov   param1,pcp
3063      emit_smart_call_m64(drcbe, &dst, &drcbe->mame_debug_hook);                  // call  mame_debug_hook
3064   }
3065#endif
3066
3067   return dst;
3068}
3069
3070
3071/*-------------------------------------------------
3072    op_exit - process an EXIT opcode
3073-------------------------------------------------*/
3074
3075static x86code *op_exit(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3076{
3077   drcuml_parameter retp;
3078
3079   /* validate instruction */
3080   assert(inst->size == 4);
3081   assert(inst->condflags == DRCUML_COND_ALWAYS || (inst->condflags >= COND_Z && inst->condflags < DRCUML_COND_MAX));
3082
3083   /* normalize parameters */
3084   param_normalize_1(drcbe, inst, &retp, PTYPE_MRI);
3085
3086   /* load the parameter into EAX */
3087   emit_mov_r32_p32(drcbe, &dst, REG_EAX, &retp);                              // mov   eax,retp
3088   if (inst->condflags == DRCUML_COND_ALWAYS)
3089      emit_jmp(&dst, drcbe->exit);                                       // jmp   exit
3090   else
3091      emit_jcc(&dst, X86_CONDITION(inst->condflags), drcbe->exit);               // jcc   exit
3092
3093   return dst;
3094}
3095
3096
3097/*-------------------------------------------------
3098    op_hashjmp - process a HASHJMP opcode
3099-------------------------------------------------*/
3100
3101static x86code *op_hashjmp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3102{
3103   drcuml_parameter modep, pcp, exp;
3104
3105   /* validate instruction */
3106   assert(inst->size == 4);
3107   assert(inst->condflags == DRCUML_COND_ALWAYS);
3108
3109   /* normalize parameters */
3110   param_normalize_3(drcbe, inst, &modep, PTYPE_MRI, &pcp, PTYPE_MRI, &exp, PTYPE_M);
3111
3112#if LOG_HASHJMPS
3113   emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &pcp);
3114   emit_mov_r32_p32(drcbe, &dst, REG_PARAM2, &modep);
3115   emit_smart_call_m64(drcbe, &dst, &drcbe->debug_log_hashjmp);
3116#endif
3117
3118   /* load the stack base one word early so we end up at the right spot after our call below */
3119   emit_mov_r64_m64(&dst, REG_RSP, MABS(drcbe, &drcbe->hashstacksave));            // mov   rsp,[hashstacksave]
3120
3121   /* fixed mode cases */
3122   if (modep.type == DRCUML_PTYPE_IMMEDIATE)
3123   {
3124      /* a straight immediate jump is direct, though we need the PC in EAX in case of failure */
3125      if (pcp.type == DRCUML_PTYPE_IMMEDIATE)
3126      {
3127         UINT32 l1val = (pcp.value >> drcbe->hash->l1shift) & drcbe->hash->l1mask;
3128         UINT32 l2val = (pcp.value >> drcbe->hash->l2shift) & drcbe->hash->l2mask;
3129         emit_call_m64(&dst, MABS(drcbe, &drcbe->hash->base[modep.value][l1val][l2val]));
3130                                                                  // call  hash[modep][l1val][l2val]
3131      }
3132
3133      /* a fixed mode but variable PC */
3134      else
3135      {
3136         emit_mov_r32_p32(drcbe, &dst, REG_EAX, &pcp);                        // mov   eax,pcp
3137         emit_mov_r32_r32(&dst, REG_EDX, REG_EAX);                           // mov   edx,eax
3138         emit_shr_r32_imm(&dst, REG_EDX, drcbe->hash->l1shift);                  // shr   edx,l1shift
3139         emit_and_r32_imm(&dst, REG_EAX, drcbe->hash->l2mask << drcbe->hash->l2shift);// and  eax,l2mask << l2shift
3140         emit_mov_r64_m64(&dst, REG_RDX, MBISD(REG_RBP, REG_RDX, 8, offset_from_rbp(drcbe, (FPTR)&drcbe->hash->base[modep.value][0])));
3141                                                                  // mov   rdx,hash[modep+edx*8]
3142         emit_call_m64(&dst, MBISD(REG_RDX, REG_RAX, 8 >> drcbe->hash->l2shift, 0));   // call  [rdx+rax*shift]
3143      }
3144   }
3145   else
3146   {
3147      /* variable mode */
3148      int modereg = param_select_register(REG_ECX, &modep, NULL);
3149      emit_mov_r32_p32(drcbe, &dst, modereg, &modep);                           // mov   modereg,modep
3150      emit_mov_r64_m64(&dst, REG_RCX, MBISD(REG_RBP, modereg, 8, offset_from_rbp(drcbe, (FPTR)&drcbe->hash->base[0])));
3151                                                                  // mov   rcx,hash[modereg*8]
3152
3153      /* fixed PC */
3154      if (pcp.type == DRCUML_PTYPE_IMMEDIATE)
3155      {
3156         UINT32 l1val = (pcp.value >> drcbe->hash->l1shift) & drcbe->hash->l1mask;
3157         UINT32 l2val = (pcp.value >> drcbe->hash->l2shift) & drcbe->hash->l2mask;
3158         emit_mov_r64_m64(&dst, REG_RDX, MBD(REG_RCX, l1val*8));                  // mov   rdx,[rcx+l1val*8]
3159         emit_call_m64(&dst, MBD(REG_RDX, l2val*8));                           // call  [l2val*8]
3160      }
3161
3162      /* variable PC */
3163      else
3164      {
3165         emit_mov_r32_p32(drcbe, &dst, REG_EAX, &pcp);                        // mov   eax,pcp
3166         emit_mov_r32_r32(&dst, REG_EDX, REG_EAX);                           // mov   edx,eax
3167         emit_shr_r32_imm(&dst, REG_EDX, drcbe->hash->l1shift);                  // shr   edx,l1shift
3168         emit_mov_r64_m64(&dst, REG_RDX, MBISD(REG_RCX, REG_RDX, 8, 0));            // mov   rdx,[rcx+rdx*8]
3169         emit_and_r32_imm(&dst, REG_EAX, drcbe->hash->l2mask << drcbe->hash->l2shift);// and  eax,l2mask << l2shift
3170         emit_call_m64(&dst, MBISD(REG_RDX, REG_RAX, 8 >> drcbe->hash->l2shift, 0));   // call  [rdx+rax*shift]
3171      }
3172   }
3173
3174   /* in all cases, if there is no code, we return here to generate the exception */
3175   emit_mov_m32_p32(drcbe, &dst, MABS(drcbe, &drcbe->state.exp.l), &pcp);            // mov   [exp],param
3176   emit_sub_r64_imm(&dst, REG_RSP, 8);                                       // sub   rsp,8
3177   emit_call_m64(&dst, MABS(drcbe, exp.value));                              // call  [exp]
3178
3179   return dst;
3180}
3181
3182
3183/*-------------------------------------------------
3184    op_jmp - process a JMP opcode
3185-------------------------------------------------*/
3186
3187static x86code *op_jmp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3188{
3189   drcuml_parameter labelp;
3190   x86code *jmptarget;
3191
3192   /* validate instruction */
3193   assert(inst->size == 4);
3194   assert(inst->condflags == DRCUML_COND_ALWAYS || (inst->condflags >= COND_Z && inst->condflags < DRCUML_COND_MAX));
3195
3196   /* normalize parameters */
3197   param_normalize_1(drcbe, inst, &labelp, PTYPE_I);
3198
3199   /* look up the jump target and jump there */
3200   jmptarget = (x86code *)drclabel_get_codeptr(drcbe->labels, labelp.value, fixup_label, dst);
3201   if (jmptarget == NULL)
3202      jmptarget = dst + 0x7ffffff0;
3203   if (inst->condflags == DRCUML_COND_ALWAYS)
3204      emit_jmp(&dst, jmptarget);                                          // jmp   target
3205   else
3206      emit_jcc(&dst, X86_CONDITION(inst->condflags), jmptarget);                  // jcc   target
3207
3208   return dst;
3209}
3210
3211
3212/*-------------------------------------------------
3213    op_exh - process an EXH opcode
3214-------------------------------------------------*/
3215
3216static x86code *op_exh(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3217{
3218   drcuml_parameter handp, exp;
3219   drccodeptr *targetptr;
3220
3221   /* validate instruction */
3222   assert(inst->size == 4);
3223   assert(inst->condflags == DRCUML_COND_ALWAYS || (inst->condflags >= COND_Z && inst->condflags < DRCUML_COND_MAX));
3224
3225   /* normalize parameters */
3226   param_normalize_2(drcbe, inst, &handp, PTYPE_M, &exp, PTYPE_MRI);
3227
3228   /* look up the handle target */
3229   targetptr = drcuml_handle_codeptr_addr((drcuml_codehandle *)handp.value);
3230
3231   /* perform the exception processing inline if unconditional */
3232   if (inst->condflags == DRCUML_COND_ALWAYS)
3233   {
3234      emit_mov_m32_p32(drcbe, &dst, MABS(drcbe, &drcbe->state.exp.l), &exp);         // mov   [exp],exp
3235      if (*targetptr != NULL)
3236         emit_call(&dst, *targetptr);                                    // call  *targetptr
3237      else
3238         emit_call_m64(&dst, MABS(drcbe, targetptr));                        // call  [targetptr]
3239   }
3240
3241   /* otherwise, jump to an out-of-band handler */
3242   else
3243   {
3244      emit_jcc(&dst, X86_CONDITION(inst->condflags), dst + 0x7ffffff0);            // jcc   exception
3245      drccache_request_oob_codegen(drcbe->cache, fixup_exception, drcbe, dst, (void *)inst);
3246   }
3247   return dst;
3248}
3249
3250
3251/*-------------------------------------------------
3252    op_callh - process a CALLH opcode
3253-------------------------------------------------*/
3254
3255static x86code *op_callh(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3256{
3257   drcuml_parameter handp;
3258   drccodeptr *targetptr;
3259   emit_link skip = { 0 };
3260
3261   /* validate instruction */
3262   assert(inst->size == 4);
3263   assert(inst->condflags == DRCUML_COND_ALWAYS || (inst->condflags >= COND_Z && inst->condflags < DRCUML_COND_MAX));
3264
3265   /* normalize parameters */
3266   param_normalize_1(drcbe, inst, &handp, PTYPE_M);
3267
3268   /* look up the handle target */
3269   targetptr = drcuml_handle_codeptr_addr((drcuml_codehandle *)handp.value);
3270
3271   /* skip if conditional */
3272   if (inst->condflags != DRCUML_COND_ALWAYS)
3273      emit_jcc_short_link(&dst, X86_NOT_CONDITION(inst->condflags), &skip);         // jcc   skip
3274
3275   /* jump through the handle; directly if a normal jump */
3276   if (*targetptr != NULL)
3277      emit_call(&dst, *targetptr);                                       // call  *targetptr
3278   else
3279      emit_call_m64(&dst, MABS(drcbe, targetptr));                           // call  [targetptr]
3280
3281   /* resolve the conditional link */
3282   if (inst->condflags != DRCUML_COND_ALWAYS)
3283      resolve_link(&dst, &skip);                                       // skip:
3284   return dst;
3285}
3286
3287
3288/*-------------------------------------------------
3289    op_ret - process a RET opcode
3290-------------------------------------------------*/
3291
3292static x86code *op_ret(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3293{
3294   emit_link skip = { 0 };
3295
3296   /* validate instruction */
3297   assert(inst->size == 4);
3298   assert(inst->condflags == DRCUML_COND_ALWAYS || (inst->condflags >= COND_Z && inst->condflags < DRCUML_COND_MAX));
3299   assert(inst->numparams == 0);
3300
3301   /* skip if conditional */
3302   if (inst->condflags != DRCUML_COND_ALWAYS)
3303      emit_jcc_short_link(&dst, X86_NOT_CONDITION(inst->condflags), &skip);         // jcc   skip
3304
3305   /* return */
3306   emit_add_r64_imm(&dst, REG_RSP, 40);                                    // add   rsp,40
3307   emit_ret(&dst);                                                      // ret
3308
3309   /* resolve the conditional link */
3310   if (inst->condflags != DRCUML_COND_ALWAYS)
3311      resolve_link(&dst, &skip);                                       // skip:
3312   return dst;
3313}
3314
3315
3316/*-------------------------------------------------
3317    op_callc - process a CALLC opcode
3318-------------------------------------------------*/
3319
3320static x86code *op_callc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3321{
3322   drcuml_parameter funcp, paramp;
3323   emit_link skip = { 0 };
3324
3325   /* validate instruction */
3326   assert(inst->size == 4);
3327   assert(inst->condflags == DRCUML_COND_ALWAYS || (inst->condflags >= COND_Z && inst->condflags < DRCUML_COND_MAX));
3328
3329   /* normalize parameters */
3330   param_normalize_2(drcbe, inst, &funcp, PTYPE_M, &paramp, PTYPE_M);
3331
3332   /* skip if conditional */
3333   if (inst->condflags != DRCUML_COND_ALWAYS)
3334      emit_jcc_short_link(&dst, X86_NOT_CONDITION(inst->condflags), &skip);         // jcc   skip
3335
3336   /* perform the call */
3337   emit_mov_r64_imm(&dst, REG_PARAM1, paramp.value);                           // mov   param1,paramp
3338   emit_smart_call_r64(drcbe, &dst, (x86code *)(FPTR)funcp.value, REG_RAX);         // call  funcp
3339
3340   /* resolve the conditional link */
3341   if (inst->condflags != DRCUML_COND_ALWAYS)
3342      resolve_link(&dst, &skip);                                       // skip:
3343   return dst;
3344}
3345
3346
3347/*-------------------------------------------------
3348    op_recover - process a RECOVER opcode
3349-------------------------------------------------*/
3350
3351static x86code *op_recover(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3352{
3353   drcuml_parameter dstp, mvparam;
3354
3355   /* validate instruction */
3356   assert(inst->size == 4);
3357   assert(inst->condflags == DRCUML_COND_ALWAYS);
3358
3359   /* normalize parameters */
3360   param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &mvparam, PTYPE_I);
3361
3362   /* call the recovery code */
3363   emit_mov_r64_m64(&dst, REG_RAX, MABS(drcbe, &drcbe->stacksave));               // mov   rax,stacksave
3364   emit_mov_r64_m64(&dst, REG_RAX, MBD(REG_RAX, -8));                           // mov   rax,[rax-4]
3365   emit_mov_r64_imm(&dst, REG_PARAM1, (FPTR)drcbe->map);                        // mov   param1,drcbe->map
3366   emit_lea_r64_m64(&dst, REG_PARAM2, MBD(REG_RAX, -1));                        // lea   param2,[rax-1]
3367   emit_mov_r64_imm(&dst, REG_PARAM3, inst->param[1].value);                     // mov   param3,param[1].value
3368   emit_smart_call_m64(drcbe, &dst, &drcbe->drcmap_get_value);                     // call  drcmap_get_value
3369   emit_mov_p32_r32(drcbe, &dst, &dstp, REG_EAX);                              // mov   dstp,eax
3370
3371   return dst;
3372}
3373
3374
3375
3376/***************************************************************************
3377    INTERNAL REGISTER OPCODES
3378***************************************************************************/
3379
3380/*-------------------------------------------------
3381    op_setfmod - process a SETFMOD opcode
3382-------------------------------------------------*/
3383
3384static x86code *op_setfmod(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3385{
3386   drcuml_parameter srcp;
3387
3388   /* validate instruction */
3389   assert(inst->size == 4);
3390   assert(inst->condflags == DRCUML_COND_ALWAYS);
3391
3392   /* normalize parameters */
3393   param_normalize_1(drcbe, inst, &srcp, PTYPE_MRI);
3394
3395   /* immediate case */
3396   if (srcp.type == DRCUML_PTYPE_IMMEDIATE)
3397   {
3398      srcp.value &= 3;
3399      emit_mov_m8_imm(&dst, MABS(drcbe, &drcbe->state.fmod), srcp.value);            // mov   [fmod],srcp
3400      emit_ldmxcsr_m32(&dst, MABS(drcbe, &drcbe->ssecontrol[srcp.value]));         // ldmxcsr fp_control[srcp]
3401   }
3402
3403   /* register/memory case */
3404   else
3405   {
3406      emit_mov_r32_p32(drcbe, &dst, REG_EAX, &srcp);                           // mov   eax,srcp
3407      emit_and_r32_imm(&dst, REG_EAX, 3);                                    // and   eax,3
3408      emit_mov_m8_r8(&dst, MABS(drcbe, &drcbe->state.fmod), REG_AL);               // mov   [fmod],al
3409      emit_ldmxcsr_m32(&dst, MBISD(REG_RBP, REG_RAX, 4, offset_from_rbp(drcbe, (FPTR)&drcbe->ssecontrol[0])));
3410                                                                  // ldmxcsr fp_control[eax]
3411   }
3412
3413   return dst;
3414}
3415
3416
3417/*-------------------------------------------------
3418    op_getfmod - process a GETFMOD opcode
3419-------------------------------------------------*/
3420
3421static x86code *op_getfmod(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3422{
3423   drcuml_parameter dstp;
3424
3425   /* validate instruction */
3426   assert(inst->size == 4);
3427   assert(inst->condflags == DRCUML_COND_ALWAYS);
3428
3429   /* normalize parameters */
3430   param_normalize_1(drcbe, inst, &dstp, PTYPE_MR);
3431
3432   /* fetch the current mode and store to the destination */
3433   if (dstp.type == DRCUML_PTYPE_INT_REGISTER)
3434      emit_movzx_r32_m8(&dst, dstp.value, MABS(drcbe, &drcbe->state.fmod));               // movzx reg,[fmod]
3435   else
3436   {
3437      emit_movzx_r32_m8(&dst, REG_EAX, MABS(drcbe, &drcbe->state.fmod));                  // movzx eax,[fmod]
3438      emit_mov_m32_r32(&dst, MABS(drcbe, dstp.value), REG_EAX);                        // mov   [dstp],eax
3439   }
3440
3441   return dst;
3442}
3443
3444
3445/*-------------------------------------------------
3446    op_getexp - process a GETEXP opcode
3447-------------------------------------------------*/
3448
3449static x86code *op_getexp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3450{
3451   drcuml_parameter dstp;
3452
3453   /* validate instruction */
3454   assert(inst->size == 4);
3455   assert(inst->condflags == DRCUML_COND_ALWAYS);
3456
3457   /* normalize parameters */
3458   param_normalize_1(drcbe, inst, &dstp, PTYPE_MR);
3459
3460   /* fetch the exception parameter and store to the destination */
3461   if (dstp.type == DRCUML_PTYPE_INT_REGISTER)
3462      emit_mov_r32_m32(&dst, dstp.value, MABS(drcbe, &drcbe->state.exp.l));               // mov   reg,[exp]
3463   else
3464   {
3465      emit_mov_r32_m32(&dst, REG_EAX, MABS(drcbe, &drcbe->state.exp.l));                  // mov   eax,[exp]
3466      emit_mov_m32_r32(&dst, MABS(drcbe, dstp.value), REG_EAX);                        // mov   [dstp],eax
3467   }
3468
3469   return dst;
3470}
3471
3472
3473
3474/***************************************************************************
3475    INTEGER OPERATIONS
3476***************************************************************************/
3477
3478/*-------------------------------------------------
3479    op_load1u - process a LOAD1U opcode
3480-------------------------------------------------*/
3481
3482static x86code *op_load1u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3483{
3484   drcuml_parameter dstp, basep, indp;
3485   int basereg, dstreg;
3486   INT32 baseoffs;
3487
3488   /* validate instruction */
3489   assert(inst->size == 4 || inst->size == 8);
3490   assert(inst->condflags == DRCUML_COND_ALWAYS);
3491
3492   /* normalize parameters */
3493   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &basep, PTYPE_M, &indp, PTYPE_MRI);
3494
3495   /* pick a target register for the general case */
3496   dstreg = param_select_register(REG_EAX, &dstp, NULL);
3497
3498   /* determine the pointer base */
3499   basereg = get_base_register_and_offset(drcbe, &dst, basep.value, REG_RDX, &baseoffs);
3500
3501   /* immediate index */
3502   if (indp.type == DRCUML_PTYPE_IMMEDIATE)
3503      emit_movzx_r32_m8(&dst, dstreg, MBD(basereg, baseoffs + 1*indp.value));         // movzx dstreg,[basep + 1*indp]
3504
3505   /* other index */
3506   else
3507   {
3508      int indreg = param_select_register(REG_ECX, &indp, NULL);
3509      emit_mov_r32_p32(drcbe, &dst, indreg, &indp);
3510      emit_movzx_r32_m8(&dst, dstreg, MBISD(basereg, indreg, 1, baseoffs));         // movzx dstreg,[basep + 1*indp]
3511   }
3512
3513   /* store to appropriate target size */
3514   if (inst->size == 4)
3515      emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                           // mov   dstp,dstreg
3516   else if (inst->size == 8)
3517      emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                           // mov   dstp,dstreg
3518
3519   return dst;
3520}
3521
3522
3523/*-------------------------------------------------
3524    op_load1s - process a LOAD1S opcode
3525-------------------------------------------------*/
3526
3527static x86code *op_load1s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3528{
3529   drcuml_parameter dstp, basep, indp;
3530   int basereg, dstreg;
3531   INT32 baseoffs;
3532
3533   /* validate instruction */
3534   assert(inst->size == 4 || inst->size == 8);
3535   assert(inst->condflags == DRCUML_COND_ALWAYS);
3536
3537   /* normalize parameters */
3538   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &basep, PTYPE_M, &indp, PTYPE_MRI);
3539
3540   /* pick a target register for the general case */
3541   dstreg = param_select_register(REG_EAX, &dstp, NULL);
3542
3543   /* determine the pointer base */
3544   basereg = get_base_register_and_offset(drcbe, &dst, basep.value, REG_RDX, &baseoffs);
3545
3546   /* 32-bit form */
3547   if (inst->size == 4)
3548   {
3549      /* immediate index */
3550      if (indp.type == DRCUML_PTYPE_IMMEDIATE)
3551         emit_movsx_r32_m8(&dst, dstreg, MBD(basereg, baseoffs + 1*indp.value));      // movsx eax,[basep + 1*indp]
3552
3553      /* other index */
3554      else
3555      {
3556         int indreg = param_select_register(REG_ECX, &indp, NULL);
3557         emit_mov_r32_p32(drcbe, &dst, indreg, &indp);
3558         emit_movsx_r32_m8(&dst, dstreg, MBISD(basereg, indreg, 1, baseoffs));      // movsx eax,[basep + 1*indp]
3559      }
3560
3561      /* general case */
3562      emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                           // mov   dstp,dstreg
3563   }
3564
3565   /* 64-bit form */
3566   else
3567   {
3568      /* immediate index */
3569      if (indp.type == DRCUML_PTYPE_IMMEDIATE)
3570         emit_movsx_r64_m8(&dst, dstreg, MBD(basereg, baseoffs + 1*indp.value));      // movsx rax,[basep + 1*indp]
3571
3572      /* other index */
3573      else
3574      {
3575         int indreg = param_select_register(REG_ECX, &indp, NULL);
3576         emit_mov_r32_p32(drcbe, &dst, indreg, &indp);
3577         emit_movsx_r64_m8(&dst, dstreg, MBISD(basereg, indreg, 1, baseoffs));      // movsx rax,[basep + 1*indp]
3578      }
3579
3580      /* general case */
3581      emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                           // mov   dstp,dstreg
3582   }
3583   return dst;
3584}
3585
3586
3587/*-------------------------------------------------
3588    op_load2u - process a LOAD2U opcode
3589-------------------------------------------------*/
3590
3591static x86code *op_load2u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3592{
3593   drcuml_parameter dstp, basep, indp;
3594   int basereg, dstreg;
3595   INT32 baseoffs;
3596
3597   /* validate instruction */
3598   assert(inst->size == 4 || inst->size == 8);
3599   assert(inst->condflags == DRCUML_COND_ALWAYS);
3600
3601   /* normalize parameters */
3602   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &basep, PTYPE_M, &indp, PTYPE_MRI);
3603
3604   /* pick a target register for the general case */
3605   dstreg = param_select_register(REG_EAX, &dstp, NULL);
3606
3607   /* determine the pointer base */
3608   basereg = get_base_register_and_offset(drcbe, &dst, basep.value, REG_RDX, &baseoffs);
3609
3610   /* immediate index */
3611   if (indp.type == DRCUML_PTYPE_IMMEDIATE)
3612      emit_movzx_r32_m16(&dst, dstreg, MBD(basereg, baseoffs + 2*indp.value));      // movzx dstreg,[basep + 2*indp]
3613
3614   /* other index */
3615   else
3616   {
3617      int indreg = param_select_register(REG_ECX, &indp, NULL);
3618      emit_mov_r32_p32(drcbe, &dst, indreg, &indp);
3619      emit_movzx_r32_m16(&dst, dstreg, MBISD(basereg, indreg, 2, baseoffs));         // movzx dstreg,[basep + 2*indp]
3620   }
3621
3622   /* store to appropriate target size */
3623   if (inst->size == 4)
3624      emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                           // mov   dstp,dstreg
3625   else if (inst->size == 8)
3626      emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                           // mov   dstp,dstreg
3627
3628   return dst;
3629}
3630
3631
3632/*-------------------------------------------------
3633    op_load2s - process a LOAD2S opcode
3634-------------------------------------------------*/
3635
3636static x86code *op_load2s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3637{
3638   drcuml_parameter dstp, basep, indp;
3639   int basereg, dstreg;
3640   INT32 baseoffs;
3641
3642   /* validate instruction */
3643   assert(inst->size == 4 || inst->size == 8);
3644   assert(inst->condflags == DRCUML_COND_ALWAYS);
3645
3646   /* normalize parameters */
3647   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &basep, PTYPE_M, &indp, PTYPE_MRI);
3648
3649   /* pick a target register for the general case */
3650   dstreg = param_select_register(REG_EAX, &dstp, NULL);
3651
3652   /* determine the pointer base */
3653   basereg = get_base_register_and_offset(drcbe, &dst, basep.value, REG_RDX, &baseoffs);
3654
3655   /* 32-bit form */
3656   if (inst->size == 4)
3657   {
3658      /* immediate index */
3659      if (indp.type == DRCUML_PTYPE_IMMEDIATE)
3660         emit_movsx_r32_m16(&dst, dstreg, MBD(basereg, baseoffs + 2*indp.value));   // movsx eax,[basep + 2*indp]
3661
3662      /* other index */
3663      else
3664      {
3665         int indreg = param_select_register(REG_ECX, &indp, NULL);
3666         emit_mov_r32_p32(drcbe, &dst, indreg, &indp);
3667         emit_movsx_r32_m16(&dst, dstreg, MBISD(basereg, indreg, 2, baseoffs));      // movsx eax,[basep + 2*indp]
3668      }
3669
3670      /* general case */
3671      emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                           // mov   dstp,dstreg
3672   }
3673
3674   /* 64-bit form */
3675   else
3676   {
3677      /* immediate index */
3678      if (indp.type == DRCUML_PTYPE_IMMEDIATE)
3679         emit_movsx_r64_m16(&dst, dstreg, MBD(basereg, baseoffs + 2*indp.value));   // movsx rax,[basep + 2*indp]
3680
3681      /* other index */
3682      else
3683      {
3684         int indreg = param_select_register(REG_ECX, &indp, NULL);
3685         emit_mov_r32_p32(drcbe, &dst, indreg, &indp);
3686         emit_movsx_r64_m16(&dst, dstreg, MBISD(basereg, indreg, 2, baseoffs));      // movsx rax,[basep + 2*indp]
3687      }
3688
3689      /* general case */
3690      emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                           // mov   dstp,dstreg
3691   }
3692   return dst;
3693}
3694
3695
3696/*-------------------------------------------------
3697    op_load4u - process a LOAD4U opcode
3698-------------------------------------------------*/
3699
3700static x86code *op_load4u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3701{
3702   drcuml_parameter dstp, basep, indp;
3703   int basereg, dstreg;
3704   INT32 baseoffs;
3705
3706   /* validate instruction */
3707   assert(inst->size == 4 || inst->size == 8);
3708   assert(inst->condflags == DRCUML_COND_ALWAYS);
3709
3710   /* normalize parameters */
3711   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &basep, PTYPE_M, &indp, PTYPE_MRI);
3712
3713   /* pick a target register for the general case */
3714   dstreg = param_select_register(REG_EAX, &dstp, NULL);
3715
3716   /* determine the pointer base */
3717   basereg = get_base_register_and_offset(drcbe, &dst, basep.value, REG_RDX, &baseoffs);
3718
3719   /* immediate index */
3720   if (indp.type == DRCUML_PTYPE_IMMEDIATE)
3721      emit_mov_r32_m32(&dst, dstreg, MBD(basereg, baseoffs + 4*indp.value));         // mov   dstreg,[basep + 4*indp]
3722
3723   /* other index */
3724   else
3725   {
3726      int indreg = param_select_register(REG_ECX, &indp, NULL);
3727      emit_mov_r32_p32(drcbe, &dst, indreg, &indp);
3728      emit_mov_r32_m32(&dst, dstreg, MBISD(basereg, indreg, 4, baseoffs));         // mov   dstreg,[basep + 4*indp]
3729   }
3730
3731   /* store to appropriate target size */
3732   if (inst->size == 4)
3733      emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                           // mov   dstp,dstreg
3734   else if (inst->size == 8)
3735      emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                           // mov   dstp,dstreg
3736
3737   return dst;
3738}
3739
3740
3741/*-------------------------------------------------
3742    op_load4s - process a LOAD4S opcode
3743-------------------------------------------------*/
3744
3745static x86code *op_load4s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3746{
3747   drcuml_parameter dstp, basep, indp;
3748   int basereg, dstreg;
3749   INT32 baseoffs;
3750
3751   /* validate instruction */
3752   assert(inst->size == 8);
3753   assert(inst->condflags == DRCUML_COND_ALWAYS);
3754
3755   /* normalize parameters */
3756   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &basep, PTYPE_M, &indp, PTYPE_MRI);
3757
3758   /* pick a target register for the general case */
3759   dstreg = param_select_register(REG_EAX, &dstp, NULL);
3760
3761   /* determine the pointer base */
3762   basereg = get_base_register_and_offset(drcbe, &dst, basep.value, REG_RDX, &baseoffs);
3763
3764   /* immediate index */
3765   if (indp.type == DRCUML_PTYPE_IMMEDIATE)
3766      emit_movsxd_r64_m32(&dst, dstreg, MBD(basereg, baseoffs + 4*indp.value));      // mov   dstreg,[basep + 4*indp]
3767
3768   /* other index */
3769   else
3770   {
3771      int indreg = param_select_register(REG_ECX, &indp, NULL);
3772      emit_mov_r32_p32(drcbe, &dst, indreg, &indp);
3773      emit_movsxd_r64_m32(&dst, dstreg, MISD(indreg, 4, basep.value));            // movsx dstreg,[basep + 4*indp]
3774   }
3775
3776   /* general case */
3777   emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                              // mov   dstp,dstreg
3778
3779   return dst;
3780}
3781
3782
3783/*-------------------------------------------------
3784    op_load8u - process a LOAD8U opcode
3785-------------------------------------------------*/
3786
3787static x86code *op_load8u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3788{
3789   drcuml_parameter dstp, basep, indp;
3790   int basereg, dstreg;
3791   INT32 baseoffs;
3792
3793   /* validate instruction */
3794   assert(inst->size == 8);
3795   assert(inst->condflags == DRCUML_COND_ALWAYS);
3796
3797   /* normalize parameters */
3798   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &basep, PTYPE_M, &indp, PTYPE_MRI);
3799
3800   /* pick a target register for the general case */
3801   dstreg = param_select_register(REG_EAX, &dstp, NULL);
3802
3803   /* determine the pointer base */
3804   basereg = get_base_register_and_offset(drcbe, &dst, basep.value, REG_RDX, &baseoffs);
3805
3806   /* immediate index */
3807   if (indp.type == DRCUML_PTYPE_IMMEDIATE)
3808      emit_mov_r64_m64(&dst, dstreg, MBD(basereg, baseoffs + 8*indp.value));         // mov   dstreg,[basep + 8*indp]
3809
3810   /* other index */
3811   else
3812   {
3813      int indreg = param_select_register(REG_ECX, &indp, NULL);
3814      emit_mov_r32_p32(drcbe, &dst, indreg, &indp);
3815      emit_mov_r64_m64(&dst, dstreg, MBISD(basereg, indreg, 8, baseoffs));         // mov   dstreg,[basep + 8*indp]
3816   }
3817
3818   /* general case */
3819   emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                              // mov   dstp,edx:dstreg
3820
3821   return dst;
3822}
3823
3824
3825/*-------------------------------------------------
3826    op_store1 - process a STORE1 opcode
3827-------------------------------------------------*/
3828
3829static x86code *op_store1(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3830{
3831   drcuml_parameter srcp, basep, indp;
3832   int basereg, srcreg;
3833   INT32 baseoffs;
3834
3835   /* validate instruction */
3836   assert(inst->size == 4 || inst->size == 8);
3837   assert(inst->condflags == DRCUML_COND_ALWAYS);
3838
3839   /* normalize parameters */
3840   param_normalize_3(drcbe, inst, &basep, PTYPE_M, &indp, PTYPE_MRI, &srcp, PTYPE_MRI);
3841
3842   /* pick a source register for the general case */
3843   srcreg = param_select_register(REG_EAX, &srcp, NULL);
3844
3845   /* determine the pointer base */
3846   basereg = get_base_register_and_offset(drcbe, &dst, basep.value, REG_RDX, &baseoffs);
3847
3848   /* degenerate case: constant index */
3849   if (indp.type == DRCUML_PTYPE_IMMEDIATE)
3850   {
3851      if (srcp.type == DRCUML_PTYPE_IMMEDIATE)
3852         emit_mov_m8_imm(&dst, MBD(basereg, baseoffs + 1*indp.value), srcp.value);   // mov   [basep + 1*indp],srcp
3853      else
3854      {
3855         emit_mov_r32_p32(drcbe, &dst, srcreg, &srcp);                        // mov   srcreg,srcp
3856         emit_mov_m8_r8(&dst, MBD(basereg, baseoffs + 1*indp.value), srcreg);      // mov   [basep + 1*indp],srcreg
3857      }
3858   }
3859
3860   /* normal case: variable index */
3861   else
3862   {
3863      int indreg = param_select_register(REG_ECX, &indp, NULL);
3864      emit_mov_r32_p32(drcbe, &dst, indreg, &indp);                           // mov   indreg,indp
3865      if (srcp.type == DRCUML_PTYPE_IMMEDIATE)
3866         emit_mov_m8_imm(&dst, MBISD(basereg, indreg, 1, baseoffs), srcp.value);      // mov   [basep + 1*ecx],srcp
3867      else
3868      {
3869         emit_mov_r32_p32(drcbe, &dst, srcreg, &srcp);                        // mov   srcreg,srcp
3870         emit_mov_m8_r8(&dst, MBISD(basereg, indreg, 1, baseoffs), srcreg);         // mov   [basep + 1*ecx],srcreg
3871      }
3872   }
3873   return dst;
3874}
3875
3876
3877/*-------------------------------------------------
3878    op_store2 - process a STORE2 opcode
3879-------------------------------------------------*/
3880
3881static x86code *op_store2(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3882{
3883   drcuml_parameter srcp, basep, indp;
3884   int basereg, srcreg;
3885   INT32 baseoffs;
3886
3887   /* validate instruction */
3888   assert(inst->size == 4 || inst->size == 8);
3889   assert(inst->condflags == DRCUML_COND_ALWAYS);
3890
3891   /* normalize parameters */
3892   param_normalize_3(drcbe, inst, &basep, PTYPE_M, &indp, PTYPE_MRI, &srcp, PTYPE_MRI);
3893
3894   /* pick a source register for the general case */
3895   srcreg = param_select_register(REG_EAX, &srcp, NULL);
3896
3897   /* determine the pointer base */
3898   basereg = get_base_register_and_offset(drcbe, &dst, basep.value, REG_RDX, &baseoffs);
3899
3900   /* degenerate case: constant index */
3901   if (indp.type == DRCUML_PTYPE_IMMEDIATE)
3902   {
3903      if (srcp.type == DRCUML_PTYPE_IMMEDIATE)
3904         emit_mov_m16_imm(&dst, MBD(basereg, baseoffs + 2*indp.value), srcp.value);   // mov   [basep + 2*indp],srcp
3905      else
3906      {
3907         emit_mov_r32_p32(drcbe, &dst, srcreg, &srcp);                        // mov   srcreg,srcp
3908         emit_mov_m16_r16(&dst, MBD(basereg, baseoffs + 2*indp.value), srcreg);      // mov   [basep + 2*indp],srcreg
3909      }
3910   }
3911
3912   /* normal case: variable index */
3913   else
3914   {
3915      int indreg = param_select_register(REG_ECX, &indp, NULL);
3916      emit_mov_r32_p32(drcbe, &dst, indreg, &indp);                           // mov   indreg,indp
3917      if (srcp.type == DRCUML_PTYPE_IMMEDIATE)
3918         emit_mov_m16_imm(&dst, MBISD(basereg, indreg, 2, baseoffs), srcp.value);   // mov   [basep + 2*ecx],srcp
3919      else
3920      {
3921         emit_mov_r32_p32(drcbe, &dst, srcreg, &srcp);                        // mov   srcreg,srcp
3922         emit_mov_m16_r16(&dst, MBISD(basereg, indreg, 2, baseoffs), srcreg);      // mov   [basep + 2*ecx],srcreg
3923      }
3924   }
3925   return dst;
3926}
3927
3928
3929/*-------------------------------------------------
3930    op_store4 - process a STORE4 opcode
3931-------------------------------------------------*/
3932
3933static x86code *op_store4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3934{
3935   drcuml_parameter srcp, basep, indp;
3936   int basereg, srcreg;
3937   INT32 baseoffs;
3938
3939   /* validate instruction */
3940   assert(inst->size == 4 || inst->size == 8);
3941   assert(inst->condflags == DRCUML_COND_ALWAYS);
3942
3943   /* normalize parameters */
3944   param_normalize_3(drcbe, inst, &basep, PTYPE_M, &indp, PTYPE_MRI, &srcp, PTYPE_MRI);
3945
3946   /* pick a source register for the general case */
3947   srcreg = param_select_register(REG_EAX, &srcp, NULL);
3948
3949   /* determine the pointer base */
3950   basereg = get_base_register_and_offset(drcbe, &dst, basep.value, REG_RDX, &baseoffs);
3951
3952   /* degenerate case: constant index */
3953   if (indp.type == DRCUML_PTYPE_IMMEDIATE)
3954   {
3955      if (srcp.type == DRCUML_PTYPE_IMMEDIATE)
3956         emit_mov_m32_imm(&dst, MBD(basereg, baseoffs + 4*indp.value), srcp.value);   // mov   [basep + 4*indp],srcp
3957      else
3958      {
3959         emit_mov_r32_p32(drcbe, &dst, srcreg, &srcp);                        // mov   srcreg,srcp
3960         emit_mov_m32_r32(&dst, MBD(basereg, baseoffs + 4*indp.value), srcreg);      // mov   [basep + 4*indp],srcreg
3961      }
3962   }
3963
3964   /* normal case: variable index */
3965   else
3966   {
3967      int indreg = param_select_register(REG_ECX, &indp, NULL);
3968      emit_mov_r32_p32(drcbe, &dst, indreg, &indp);                           // mov   indreg,indp
3969      if (srcp.type == DRCUML_PTYPE_IMMEDIATE)
3970         emit_mov_m32_imm(&dst, MBISD(basereg, indreg, 4, baseoffs), srcp.value);   // mov   [basep + 4*ecx],srcp
3971      else
3972      {
3973         emit_mov_r32_p32(drcbe, &dst, srcreg, &srcp);                        // mov   srcreg,srcp
3974         emit_mov_m32_r32(&dst, MBISD(basereg, indreg, 4, baseoffs), srcreg);      // mov   [basep + 4*ecx],srcreg
3975      }
3976   }
3977   return dst;
3978}
3979
3980
3981/*-------------------------------------------------
3982    op_store8 - process a STORE8 opcode
3983-------------------------------------------------*/
3984
3985static x86code *op_store8(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3986{
3987   drcuml_parameter srcp, basep, indp;
3988   int basereg, srcreg;
3989   INT32 baseoffs;
3990
3991   /* validate instruction */
3992   assert(inst->size == 8);
3993   assert(inst->condflags == DRCUML_COND_ALWAYS);
3994
3995   /* normalize parameters */
3996   param_normalize_3(drcbe, inst, &basep, PTYPE_M, &indp, PTYPE_MRI, &srcp, PTYPE_MRI);
3997
3998   /* pick a source register for the general case */
3999   srcreg = param_select_register(REG_EAX, &srcp, NULL);
4000
4001   /* determine the pointer base */
4002   basereg = get_base_register_and_offset(drcbe, &dst, basep.value, REG_RDX, &baseoffs);
4003
4004   /* degenerate case: constant index */
4005   if (indp.type == DRCUML_PTYPE_IMMEDIATE)
4006   {
4007      if (srcp.type == DRCUML_PTYPE_IMMEDIATE && short_immediate(srcp.value))
4008         emit_mov_m64_imm(&dst, MBD(basereg, baseoffs + 8*indp.value), srcp.value);   // mov   [basep + 8*indp],srcp
4009      else
4010      {
4011         emit_mov_r64_p64(drcbe, &dst, srcreg, &srcp);                        // mov   srcreg,srcp
4012         emit_mov_m64_r64(&dst, MBD(basereg, baseoffs + 8*indp.value), srcreg);      // mov   [basep + 8*indp],srcreg
4013      }
4014   }
4015
4016   /* normal case: variable index */
4017   else
4018   {
4019      int indreg = param_select_register(REG_ECX, &indp, NULL);
4020      emit_mov_r32_p32(drcbe, &dst, indreg, &indp);                           // mov   indreg,indp
4021      if (srcp.type == DRCUML_PTYPE_IMMEDIATE && short_immediate(srcp.value))
4022         emit_mov_m64_imm(&dst, MBISD(basereg, indreg, 8, baseoffs), srcp.value);   // mov   [basep + 8*ecx],srcp
4023      else
4024      {
4025         emit_mov_r64_p64(drcbe, &dst, srcreg, &srcp);                        // mov   srcreg,srcp
4026         emit_mov_m64_r64(&dst, MBISD(basereg, indreg, 8, baseoffs), srcreg);      // mov   [basep + 8*ecx],srcreg
4027      }
4028   }
4029   return dst;
4030}
4031
4032
4033/*-------------------------------------------------
4034    op_read1u - process a READ1U opcode
4035-------------------------------------------------*/
4036
4037static x86code *op_read1u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4038{
4039   drcuml_parameter dstp, spacep, addrp;
4040   int dstreg;
4041
4042   /* validate instruction */
4043   assert(inst->size == 4 || inst->size == 8);
4044   assert(inst->condflags == DRCUML_COND_ALWAYS);
4045
4046   /* normalize parameters */
4047   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI);
4048
4049   /* pick a target register for the general case */
4050   dstreg = param_select_register(REG_EAX, &dstp, NULL);
4051
4052   /* set up a call to the read byte handler */
4053   emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &addrp);                           // mov    param1,addrp
4054   emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].read_byte);// call   read_byte
4055   emit_movzx_r32_r8(&dst, dstreg, REG_AL);                                 // movzx  dstreg,al
4056
4057   /* store to appropriate target size */
4058   if (inst->size == 4)
4059      emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                           // mov   dstp,dstreg
4060   else if (inst->size == 8)
4061      emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                           // mov   dstp,dstreg
4062
4063   return dst;
4064}
4065
4066
4067/*-------------------------------------------------
4068    op_read1s - process a READ1S opcode
4069-------------------------------------------------*/
4070
4071static x86code *op_read1s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4072{
4073   drcuml_parameter dstp, spacep, addrp;
4074   int dstreg;
4075
4076   /* validate instruction */
4077   assert(inst->size == 4 || inst->size == 8);
4078   assert(inst->condflags == DRCUML_COND_ALWAYS);
4079
4080   /* normalize parameters */
4081   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI);
4082
4083   /* pick a target register for the general case */
4084   dstreg = param_select_register(REG_EAX, &dstp, NULL);
4085
4086   /* set up a call to the read byte handler */
4087   emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &addrp);                           // mov    param1,addrp
4088   emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].read_byte);// call   read_byte
4089
4090   /* 32-bit form */
4091   if (inst->size == 4)
4092   {
4093      /* general case */
4094      emit_movsx_r32_r8(&dst, dstreg, REG_AL);                              // movsx  dstreg,al
4095      emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                           // mov    dstp,dstreg
4096   }
4097
4098   /* 64-bit form */
4099   else
4100   {
4101      /* general case */
4102      emit_movsx_r64_r8(&dst, dstreg, REG_AL);                              // movsx  dstreg,al
4103      emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                           // mov    dstp,dstreg
4104   }
4105   return dst;
4106}
4107
4108
4109/*-------------------------------------------------
4110    op_read2u - process a READ2U opcode
4111-------------------------------------------------*/
4112
4113static x86code *op_read2u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4114{
4115   drcuml_parameter dstp, spacep, addrp;
4116   int dstreg;
4117
4118   /* validate instruction */
4119   assert(inst->size == 4 || inst->size == 8);
4120   assert(inst->condflags == DRCUML_COND_ALWAYS);
4121
4122   /* normalize parameters */
4123   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI);
4124
4125   /* pick a target register for the general case */
4126   dstreg = param_select_register(REG_EAX, &dstp, NULL);
4127
4128   /* set up a call to the read word handler */
4129   emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &addrp);                           // mov    param1,addrp
4130   emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].read_word);// call   read_word
4131   emit_movzx_r32_r16(&dst, dstreg, REG_AX);                                 // movzx  dstreg,ax
4132
4133   /* store to appropriate target size */
4134   if (inst->size == 4)
4135      emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                           // mov   dstp,dstreg
4136   else if (inst->size == 8)
4137      emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                           // mov   dstp,dstreg
4138
4139   return dst;
4140}
4141
4142
4143/*-------------------------------------------------
4144    op_read2s - process a READ2S opcode
4145-------------------------------------------------*/
4146
4147static x86code *op_read2s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4148{
4149   drcuml_parameter dstp, spacep, addrp;
4150   int dstreg;
4151
4152   /* validate instruction */
4153   assert(inst->size == 4 || inst->size == 8);
4154   assert(inst->condflags == DRCUML_COND_ALWAYS);
4155
4156   /* normalize parameters */
4157   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI);
4158
4159   /* pick a target register for the general case */
4160   dstreg = param_select_register(REG_EAX, &dstp, NULL);
4161
4162   /* set up a call to the read word handler */
4163   emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &addrp);                           // mov    param1,addrp
4164   emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].read_word);// call   read_word
4165
4166   /* 32-bit form */
4167   if (inst->size == 4)
4168   {
4169      /* general case */
4170      emit_movsx_r32_r16(&dst, dstreg, REG_AX);                              // movsx  dstreg,ax
4171      emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                           // mov    dstp,dstreg
4172   }
4173
4174   /* 64-bit form */
4175   else
4176   {
4177      /* general case */
4178      emit_movsx_r64_r16(&dst, dstreg, REG_AX);                              // movsx  dstreg,ax
4179      emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                           // mov    dstp,dstreg
4180   }
4181   return dst;
4182}
4183
4184
4185/*-------------------------------------------------
4186    op_read2m - process a READ2M opcode
4187-------------------------------------------------*/
4188
4189static x86code *op_read2m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4190{
4191   drcuml_parameter dstp, spacep, addrp, maskp;
4192   int dstreg;
4193
4194   /* validate instruction */
4195   assert(inst->size == 4 || inst->size == 8);
4196   assert(inst->condflags == DRCUML_COND_ALWAYS);
4197
4198   /* normalize parameters */
4199   param_normalize_4(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &maskp, PTYPE_MRI);
4200
4201   /* pick a target register for the general case */
4202   dstreg = param_select_register(REG_EAX, &dstp, NULL);
4203
4204   /* set up a call to the read dword masked handler */
4205   emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &addrp);                           // mov    param1,addrp
4206   emit_mov_r32_p32(drcbe, &dst, REG_PARAM2, &maskp);                           // mov    param1,maskp
4207   emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].read_word_masked);// call   read_word_masked
4208   emit_movzx_r32_r16(&dst, dstreg, REG_AX);                                 // movzx  dstreg,ax
4209
4210   /* store to appropriate target size */
4211   if (inst->size == 4)
4212      emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                           // mov   dstp,dstreg
4213   else if (inst->size == 8)
4214      emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                           // mov   dstp,dstreg
4215
4216   return dst;
4217}
4218
4219
4220/*-------------------------------------------------
4221    op_read4u - process a READ4U opcode
4222-------------------------------------------------*/
4223
4224static x86code *op_read4u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4225{
4226   drcuml_parameter dstp, spacep, addrp;
4227
4228   /* validate instruction */
4229   assert(inst->size == 4 || inst->size == 8);
4230   assert(inst->condflags == DRCUML_COND_ALWAYS);
4231
4232   /* normalize parameters */
4233   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI);
4234
4235   /* set up a call to the read dword handler */
4236   emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &addrp);                           // mov    param1,addrp
4237   emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].read_dword);// call   read_dword
4238
4239   /* store to appropriate target size */
4240   if (inst->size == 4)
4241      emit_mov_p32_r32(drcbe, &dst, &dstp, REG_EAX);                           // mov   dstp,eax
4242   else if (inst->size == 8)
4243      emit_mov_p64_r64(drcbe, &dst, &dstp, REG_RAX);                           // mov   dstp,rax
4244
4245   return dst;
4246}
4247
4248
4249/*-------------------------------------------------
4250    op_read4s - process a READ4S opcode
4251-------------------------------------------------*/
4252
4253static x86code *op_read4s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4254{
4255   drcuml_parameter dstp, spacep, addrp;
4256   int dstreg;
4257
4258   /* validate instruction */
4259   assert(inst->size == 8);
4260   assert(inst->condflags == DRCUML_COND_ALWAYS);
4261
4262   /* normalize parameters */
4263   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI);
4264
4265   /* pick a target register for the general case */
4266   dstreg = param_select_register(REG_EAX, &dstp, NULL);
4267
4268   /* set up a call to the read dword handler */
4269   emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &addrp);                           // mov    param1,addrp
4270   emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].read_dword);// call   read_dword
4271
4272   /* 64-bit form */
4273   emit_movsxd_r64_r32(&dst, dstreg, REG_EAX);                                 // movsxd dstreg,eax
4274   emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                              // mov   dstp,dstreg
4275
4276   return dst;
4277}
4278
4279
4280/*-------------------------------------------------
4281    op_read4m - process a READ4M opcode
4282-------------------------------------------------*/
4283
4284static x86code *op_read4m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4285{
4286   drcuml_parameter dstp, spacep, addrp, maskp;
4287
4288   /* validate instruction */
4289   assert(inst->size == 4 || inst->size == 8);
4290   assert(inst->condflags == DRCUML_COND_ALWAYS);
4291
4292   /* normalize parameters */
4293   param_normalize_4(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &maskp, PTYPE_MRI);
4294
4295   /* set up a call to the read dword masked handler */
4296   emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &addrp);                           // mov    param1,addrp
4297   emit_mov_r32_p32(drcbe, &dst, REG_PARAM2, &maskp);                           // mov    param2,maskp
4298   emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].read_dword_masked);// call   read_dword_masked
4299
4300   /* store to appropriate target size */
4301   if (inst->size == 4)
4302      emit_mov_p32_r32(drcbe, &dst, &dstp, REG_EAX);                           // mov   dstp,eax
4303   else if (inst->size == 8)
4304      emit_mov_p64_r64(drcbe, &dst, &dstp, REG_RAX);                           // mov   dstp,rax
4305
4306   return dst;
4307}
4308
4309
4310/*-------------------------------------------------
4311    op_read8u - process a READ8U opcode
4312-------------------------------------------------*/
4313
4314static x86code *op_read8u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4315{
4316   drcuml_parameter dstp, spacep, addrp;
4317
4318   /* validate instruction */
4319   assert(inst->size == 8);
4320   assert(inst->condflags == DRCUML_COND_ALWAYS);
4321
4322   /* normalize parameters */
4323   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI);
4324
4325   /* set up a call to the read qword handler */
4326   emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &addrp);                           // mov    param1,addrp
4327   emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].read_qword);// call   read_qword
4328
4329   /* 64-bit form */
4330   emit_mov_p64_r64(drcbe, &dst, &dstp, REG_RAX);                              // mov   dstp,rax
4331
4332   return dst;
4333}
4334
4335
4336/*-------------------------------------------------
4337    op_read8m - process a READ8M opcode
4338-------------------------------------------------*/
4339
4340static x86code *op_read8m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4341{
4342   drcuml_parameter dstp, spacep, addrp, maskp;
4343
4344   /* validate instruction */
4345   assert(inst->size == 8);
4346   assert(inst->condflags == DRCUML_COND_ALWAYS);
4347
4348   /* normalize parameters */
4349   param_normalize_4(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &maskp, PTYPE_MRI);
4350
4351   /* set up a call to the read qword masked handler */
4352   emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &addrp);                           // mov    param1,addrp
4353   emit_mov_r64_p64(drcbe, &dst, REG_PARAM2, &maskp);                           // mov    param2,maskp
4354   emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].read_qword_masked);// call   read_qword_masked
4355
4356   /* 64-bit form */
4357   emit_mov_p64_r64(drcbe, &dst, &dstp, REG_RAX);                              // mov   dstp,rax
4358
4359   return dst;
4360}
4361
4362
4363/*-------------------------------------------------
4364    op_write1 - process a WRITE1 opcode
4365-------------------------------------------------*/
4366
4367static x86code *op_write1(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4368{
4369   drcuml_parameter spacep, addrp, srcp;
4370
4371   /* validate instruction */
4372   assert(inst->size == 4 || inst->size == 8);
4373   assert(inst->condflags == DRCUML_COND_ALWAYS);
4374
4375   /* normalize parameters */
4376   param_normalize_3(drcbe, inst, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &srcp, PTYPE_MRI);
4377
4378   /* set up a call to the write byte handler */
4379   emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &addrp);                           // mov    param1,addrp
4380   emit_mov_r32_p32(drcbe, &dst, REG_PARAM2, &srcp);                           // mov    param2,srcp
4381   emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].write_byte);// call   write_byte
4382
4383   return dst;
4384}
4385
4386
4387/*-------------------------------------------------
4388    op_write2 - process a WRITE2 opcode
4389-------------------------------------------------*/
4390
4391static x86code *op_write2(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4392{
4393   drcuml_parameter spacep, addrp, srcp;
4394
4395   /* validate instruction */
4396   assert(inst->size == 4 || inst->size == 8);
4397   assert(inst->condflags == DRCUML_COND_ALWAYS);
4398
4399   /* normalize parameters */
4400   param_normalize_3(drcbe, inst, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &srcp, PTYPE_MRI);
4401
4402   /* set up a call to the write word handler */
4403   emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &addrp);                           // mov    param1,addrp
4404   emit_mov_r32_p32(drcbe, &dst, REG_PARAM2, &srcp);                           // mov    param2,srcp
4405   emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].write_word);// call   write_word
4406
4407   return dst;
4408}
4409
4410
4411/*-------------------------------------------------
4412    op_writ2m - process a WRIT2M opcode
4413-------------------------------------------------*/
4414
4415static x86code *op_writ2m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4416{
4417   drcuml_parameter spacep, addrp, maskp, srcp;
4418
4419   /* validate instruction */
4420   assert(inst->size == 4 || inst->size == 8);
4421   assert(inst->condflags == DRCUML_COND_ALWAYS);
4422
4423   /* normalize parameters */
4424   param_normalize_4(drcbe, inst, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &maskp, PTYPE_MRI, &srcp, PTYPE_MRI);
4425
4426   /* set up a call to the write word handler */
4427   emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &addrp);                           // mov    param1,addrp
4428   emit_mov_r32_p32(drcbe, &dst, REG_PARAM2, &srcp);                           // mov    param2,srcp
4429   emit_mov_r32_p32(drcbe, &dst, REG_PARAM3, &maskp);                           // mov    param3,maskp
4430   emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].write_word_masked);// call   write_word_masked
4431   return dst;
4432}
4433
4434
4435/*-------------------------------------------------
4436    op_write4 - process a WRITE4 opcode
4437-------------------------------------------------*/
4438
4439static x86code *op_write4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4440{
4441   drcuml_parameter spacep, addrp, srcp;
4442
4443   /* validate instruction */
4444   assert(inst->size == 4 || inst->size == 8);
4445   assert(inst->condflags == DRCUML_COND_ALWAYS);
4446
4447   /* normalize parameters */
4448   param_normalize_3(drcbe, inst, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &srcp, PTYPE_MRI);
4449
4450   /* set up a call to the write dword handler */
4451   emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &addrp);                           // mov    param1,addrp
4452   emit_mov_r32_p32(drcbe, &dst, REG_PARAM2, &srcp);                           // mov    param2,srcp
4453   emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].write_dword);// call   write_dword
4454
4455   return dst;
4456}
4457
4458
4459/*-------------------------------------------------
4460    op_writ4m - process a WRIT4M opcode
4461-------------------------------------------------*/
4462
4463static x86code *op_writ4m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4464{
4465   drcuml_parameter spacep, addrp, maskp, srcp;
4466
4467   /* validate instruction */
4468   assert(inst->size == 4 || inst->size == 8);
4469   assert(inst->condflags == DRCUML_COND_ALWAYS);
4470
4471   /* normalize parameters */
4472   param_normalize_4(drcbe, inst, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &maskp, PTYPE_MRI, &srcp, PTYPE_MRI);
4473
4474   /* set up a call to the write word handler */
4475   emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &addrp);                           // mov    param1,addrp
4476   emit_mov_r32_p32(drcbe, &dst, REG_PARAM2, &srcp);                           // mov    param2,srcp
4477   emit_mov_r32_p32(drcbe, &dst, REG_PARAM3, &maskp);                           // mov    param3,maskp
4478   emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].write_dword_masked);// call   write_dword_masked
4479   return dst;
4480}
4481
4482
4483/*-------------------------------------------------
4484    op_write8 - process a WRITE8 opcode
4485-------------------------------------------------*/
4486
4487static x86code *op_write8(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4488{
4489   drcuml_parameter spacep, addrp, srcp;
4490
4491   /* validate instruction */
4492   assert(inst->size == 8);
4493   assert(inst->condflags == DRCUML_COND_ALWAYS);
4494
4495   /* normalize parameters */
4496   param_normalize_3(drcbe, inst, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &srcp, PTYPE_MRI);
4497
4498   /* set up a call to the write qword handler */
4499   emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &addrp);                           // mov    param1,addrp
4500   emit_mov_r64_p64(drcbe, &dst, REG_PARAM2, &srcp);                           // mov    param2,srcp
4501   emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].write_qword);// call   write_qword
4502
4503   return dst;
4504}
4505
4506
4507/*-------------------------------------------------
4508    op_writ8m - process a WRIT8M opcode
4509-------------------------------------------------*/
4510
4511static x86code *op_writ8m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4512{
4513   drcuml_parameter spacep, addrp, maskp, srcp;
4514
4515   /* validate instruction */
4516   assert(inst->size == 8);
4517   assert(inst->condflags == DRCUML_COND_ALWAYS);
4518
4519   /* normalize parameters */
4520   param_normalize_4(drcbe, inst, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &maskp, PTYPE_MRI, &srcp, PTYPE_MRI);
4521
4522   /* set up a call to the write word handler */
4523   emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &addrp);                           // mov    param1,addrp
4524   emit_mov_r64_p64(drcbe, &dst, REG_PARAM2, &srcp);                           // mov    param2,srcp
4525   emit_mov_r64_p64(drcbe, &dst, REG_PARAM3, &maskp);                           // mov    param3,maskp
4526   emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].write_qword_masked);// call   write_qword_masked
4527   return dst;
4528}
4529
4530
4531/*-------------------------------------------------
4532    op_flags - process a FLAGS opcode
4533-------------------------------------------------*/
4534
4535static x86code *op_flags(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4536{
4537   drcuml_parameter dstp, maskp, tablep;
4538   int dstreg;
4539
4540   /* validate instruction */
4541   assert(inst->size == 4 || inst->size == 8);
4542   assert(inst->condflags == DRCUML_COND_ALWAYS);
4543
4544   /* normalize parameters */
4545   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &maskp, PTYPE_I, &tablep, PTYPE_M);
4546
4547   /* pick a target register for the general case */
4548   dstreg = param_select_register(REG_ECX, &dstp, NULL);
4549
4550   /* translate live flags into UML flags */
4551   emit_pushf(&dst);                                                   // pushf
4552   emit_pop_r64(&dst, REG_RAX);                                          // pop    rax
4553   emit_and_r32_imm(&dst, REG_EAX, 0x8c5);                                    // and    eax,0x8c5
4554   emit_movzx_r32_m8(&dst, REG_EAX, MBISD(REG_RBP, REG_RAX, 1, offset_from_rbp(drcbe, (FPTR)&drcbe->flagsmap[0])));
4555                                                                  // movzx  eax,[flags_map]
4556
4557   /* 32-bit form */
4558   if (inst->size == 4)
4559   {
4560      /* no masking */
4561      if (maskp.value == 0xffffffff)
4562      {
4563         emit_mov_r32_m32(&dst, dstreg, MBISD(REG_RBP, REG_EAX, 4, offset_from_rbp(drcbe, tablep.value)));
4564                                                                  // mov    dstreg,[eax*4+table]
4565         emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                        // mov    dstp,dstreg
4566      }
4567
4568      /* general case */
4569      else
4570      {
4571         emit_mov_r32_p32(drcbe, &dst, dstreg, &dstp);                        // mov    dstreg,dstp
4572         emit_mov_r32_m32(&dst, REG_EDX, MBISD(REG_RBP, REG_EAX, 4, offset_from_rbp(drcbe, tablep.value)));
4573                                                                  // mov    edx,[eax*4+table]
4574         emit_and_r32_imm(&dst, dstreg, ~maskp.value);                        // and    dstreg,~mask
4575         emit_and_r32_imm(&dst, REG_EDX, maskp.value);                        // and    edx,mask
4576         emit_or_r32_r32(&dst, dstreg, REG_EDX);                              // or     dstreg,edx
4577         emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                        // mov    dstp,dstreg
4578      }
4579   }
4580
4581   /* 64-bit form */
4582   else
4583   {
4584      /* no masking */
4585      if (maskp.value == U64(0xffffffffffffffff))
4586      {
4587         emit_mov_r64_m64(&dst, dstreg, MBISD(REG_RBP, REG_EAX, 8, offset_from_rbp(drcbe, tablep.value)));
4588                                                                  // mov    dstreg,[eax*8+table]
4589         emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                        // mov    dstp,dstreg
4590      }
4591
4592      /* general case */
4593      else
4594      {
4595         emit_mov_r64_p64(drcbe, &dst, dstreg, &dstp);                        // mov    dstreg,dstp
4596         emit_mov_r64_m64(&dst, REG_EDX, MBISD(REG_RBP, REG_EAX, 8, offset_from_rbp(drcbe, tablep.value)));
4597                                                                  // mov    edx,[eax*84+table]
4598         if (short_immediate(~maskp.value))
4599            emit_and_r64_imm(&dst, dstreg, ~maskp.value);                     // and    dstreg,~mask
4600         else
4601         {
4602            emit_mov_r64_imm(&dst, REG_R11, ~maskp.value);                     // mov    r11,~mask
4603            emit_and_r64_r64(&dst, dstreg, REG_R11);                        // and    dstreg,r11
4604         }
4605         if (short_immediate(maskp.value))
4606            emit_and_r64_imm(&dst, REG_EDX, maskp.value);                     // and    edx,mask
4607         else
4608         {
4609            emit_mov_r64_imm(&dst, REG_R11, maskp.value);                     // mov    r11,mask
4610            emit_and_r64_r64(&dst, REG_EDX, REG_R11);                        // and    edx,r11
4611         }
4612         emit_or_r64_r64(&dst, dstreg, REG_EDX);                              // or     dstreg,edx
4613         emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                        // mov    dstp,dstreg
4614      }
4615   }
4616   return dst;
4617}
4618
4619
4620/*-------------------------------------------------
4621    op_mov - process a MOV opcode
4622-------------------------------------------------*/
4623
4624static x86code *op_mov(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4625{
4626   drcuml_parameter dstp, srcp;
4627   x86code *savedst = dst;
4628   emit_link skip = { 0 };
4629   int dstreg;
4630
4631   /* validate instruction */
4632   assert(inst->size == 4 || inst->size == 8);
4633   assert(inst->condflags == DRCUML_COND_ALWAYS || (inst->condflags >= COND_Z && inst->condflags < DRCUML_COND_MAX));
4634
4635   /* normalize parameters, but only if we got here directly */
4636   /* other opcodes call through here with pre-normalized parameters */
4637   if (inst->opcode == DRCUML_OP_MOV)
4638      param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MRI);
4639   else
4640   {
4641      dstp = inst->param[0];
4642      srcp = inst->param[1];
4643   }
4644
4645   /* degenerate case: dest and source are equal */
4646   if (dstp.type == srcp.type && dstp.value == srcp.value)
4647      return dst;
4648
4649   /* pick a target register for the general case */
4650   dstreg = param_select_register(REG_EAX, &dstp, NULL);
4651
4652   /* always start with a jmp */
4653   if (inst->condflags != DRCUML_COND_ALWAYS)
4654      emit_jcc_short_link(&dst, X86_NOT_CONDITION(inst->condflags), &skip);         // jcc   skip
4655
4656   /* 32-bit form */
4657   if (inst->size == 4)
4658   {
4659      /* register to memory */
4660      if (dstp.type == DRCUML_PTYPE_MEMORY && srcp.type == DRCUML_PTYPE_INT_REGISTER)
4661         emit_mov_m32_r32(&dst, MABS(drcbe, dstp.value), srcp.value);            // mov   [dstp],srcp
4662
4663      /* immediate to memory */
4664      else if (dstp.type == DRCUML_PTYPE_MEMORY && srcp.type == DRCUML_PTYPE_IMMEDIATE)
4665         emit_mov_m32_imm(&dst, MABS(drcbe, dstp.value), srcp.value);            // mov   [dstp],srcp
4666
4667      /* conditional memory to register */
4668      else if (inst->condflags != 0 && dstp.type == DRCUML_PTYPE_INT_REGISTER && srcp.type == DRCUML_PTYPE_MEMORY)
4669      {
4670         dst = savedst;
4671         skip.target = NULL;
4672         emit_cmovcc_r32_m32(&dst, X86_CONDITION(inst->condflags), dstp.value, MABS(drcbe, srcp.value));
4673                                                                  // cmovcc dstp,[srcp]
4674      }
4675
4676      /* conditional register to register */
4677      else if (inst->condflags != 0 && dstp.type == DRCUML_PTYPE_INT_REGISTER && srcp.type == DRCUML_PTYPE_INT_REGISTER)
4678      {
4679         dst = savedst;
4680         skip.target = NULL;
4681         emit_cmovcc_r32_r32(&dst, X86_CONDITION(inst->condflags), dstp.value, srcp.value);
4682                                                                  // cmovcc dstp,srcp
4683      }
4684
4685      /* general case */
4686      else
4687      {
4688         emit_mov_r32_p32(drcbe, &dst, dstreg, &srcp);                        // mov   dstreg,srcp
4689         emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
4690      }
4691   }
4692
4693   /* 64-bit form */
4694   else if (inst->size == 8)
4695   {
4696      /* register to memory */
4697      if (dstp.type == DRCUML_PTYPE_MEMORY && srcp.type == DRCUML_PTYPE_INT_REGISTER)
4698         emit_mov_m64_r64(&dst, MABS(drcbe, dstp.value), srcp.value);            // mov   [dstp],srcp
4699
4700      /* immediate to memory */
4701      else if (dstp.type == DRCUML_PTYPE_MEMORY && srcp.type == DRCUML_PTYPE_IMMEDIATE && short_immediate(srcp.value))
4702         emit_mov_m64_imm(&dst, MABS(drcbe, dstp.value), srcp.value);            // mov   [dstp],srcp
4703
4704      /* conditional memory to register */
4705      else if (inst->condflags != 0 && dstp.type == DRCUML_PTYPE_INT_REGISTER && srcp.type == DRCUML_PTYPE_MEMORY)
4706      {
4707         dst = savedst;
4708         skip.target = NULL;
4709         emit_cmovcc_r64_m64(&dst, X86_CONDITION(inst->condflags), dstp.value, MABS(drcbe, srcp.value));
4710                                                                  // cmovcc dstp,[srcp]
4711      }
4712
4713      /* conditional register to register */
4714      else if (inst->condflags != 0 && dstp.type == DRCUML_PTYPE_INT_REGISTER && srcp.type == DRCUML_PTYPE_INT_REGISTER)
4715      {
4716         dst = savedst;
4717         skip.target = NULL;
4718         emit_cmovcc_r64_r64(&dst, X86_CONDITION(inst->condflags), dstp.value, srcp.value);
4719                                                                  // cmovcc dstp,srcp
4720      }
4721
4722      /* general case */
4723      else
4724      {
4725         emit_mov_r64_p64(drcbe, &dst, dstreg, &srcp);                        // mov   dstreg,srcp
4726         emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
4727      }
4728   }
4729
4730   /* resolve the jump */
4731   if (skip.target != NULL)
4732      resolve_link(&dst, &skip);
4733   return dst;
4734}
4735
4736
4737/*-------------------------------------------------
4738    op_zext1 - process a ZEXT1 opcode
4739-------------------------------------------------*/
4740
4741static x86code *op_zext1(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4742{
4743   drcuml_parameter dstp, srcp;
4744   int dstreg;
4745
4746   /* validate instruction */
4747   assert(inst->size == 4 || inst->size == 8);
4748   assert(inst->condflags == DRCUML_COND_ALWAYS);
4749
4750   /* normalize parameters */
4751   param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MRI);
4752
4753   /* degenerate cases -- convert to a move */
4754   if (srcp.type == DRCUML_PTYPE_IMMEDIATE)
4755      return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT8)srcp.value);
4756
4757   /* pick a target register for the general case */
4758   dstreg = param_select_register(REG_EAX, &dstp, NULL);
4759
4760   /* general case */
4761   if (srcp.type == DRCUML_PTYPE_MEMORY)
4762      emit_movzx_r32_m8(&dst, dstreg, MABS(drcbe, srcp.value));                  // movzx dstreg,[srcp]
4763   else if (srcp.type == DRCUML_PTYPE_INT_REGISTER)
4764      emit_movzx_r32_r8(&dst, dstreg, srcp.value);                           // movzx dstreg,srcp
4765
4766   /* 32-bit form */
4767   if (inst->size == 4)
4768      emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                           // mov   dstp,dstreg
4769
4770   /* 64-bit form */
4771   else if (inst->size == 8)
4772      emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                           // mov   dstp,dstreg
4773
4774   return dst;
4775}
4776
4777
4778/*-------------------------------------------------
4779    op_zext2 - process a ZEXT2 opcode
4780-------------------------------------------------*/
4781
4782static x86code *op_zext2(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4783{
4784   drcuml_parameter dstp, srcp;
4785   int dstreg;
4786
4787   /* validate instruction */
4788   assert(inst->size == 4 || inst->size == 8);
4789   assert(inst->condflags == DRCUML_COND_ALWAYS);
4790
4791   /* normalize parameters */
4792   param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MRI);
4793
4794   /* degenerate cases -- convert to a move */
4795   if (srcp.type == DRCUML_PTYPE_IMMEDIATE)
4796      return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT16)srcp.value);
4797
4798   /* pick a target register for the general case */
4799   dstreg = param_select_register(REG_EAX, &dstp, NULL);
4800
4801   /* general case */
4802   if (srcp.type == DRCUML_PTYPE_MEMORY)
4803      emit_movzx_r32_m16(&dst, dstreg, MABS(drcbe, srcp.value));                  // movzx dstreg,[srcp]
4804   else if (srcp.type == DRCUML_PTYPE_INT_REGISTER)
4805      emit_movzx_r32_r16(&dst, dstreg, srcp.value);                           // movzx dstreg,srcp
4806
4807   /* 32-bit form */
4808   if (inst->size == 4)
4809      emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                           // mov   dstp,dstreg
4810
4811   /* 64-bit form */
4812   else if (inst->size == 8)
4813      emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                           // mov   dstp,dstreg
4814
4815   return dst;
4816}
4817
4818
4819/*-------------------------------------------------
4820    op_zext4 - process a ZEXT4 opcode
4821-------------------------------------------------*/
4822
4823static x86code *op_zext4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4824{
4825   drcuml_parameter dstp, srcp;
4826   int dstreg;
4827
4828   /* validate instruction */
4829   assert(inst->size == 8);
4830   assert(inst->condflags == DRCUML_COND_ALWAYS);
4831
4832   /* normalize parameters */
4833   param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MRI);
4834
4835   /* degenerate cases -- convert to a move */
4836   if (srcp.type == DRCUML_PTYPE_IMMEDIATE)
4837      return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT32)srcp.value);
4838
4839   /* pick a target register for the general case */
4840   dstreg = param_select_register(REG_EAX, &dstp, NULL);
4841
4842   /* general case */
4843   if (srcp.type == DRCUML_PTYPE_MEMORY)
4844      emit_mov_r32_m32(&dst, dstreg, MABS(drcbe, srcp.value));                  // mov   dstreg,[srcp]
4845   else if (srcp.type == DRCUML_PTYPE_INT_REGISTER)
4846      emit_mov_r32_r32(&dst, dstreg, srcp.value);                              // mov   dstreg,srcp
4847   emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                              // mov   dstp,dstreg
4848
4849   return dst;
4850}
4851
4852
4853/*-------------------------------------------------
4854    op_sext1 - process a SEXT1 opcode
4855-------------------------------------------------*/
4856
4857static x86code *op_sext1(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4858{
4859   drcuml_parameter dstp, srcp;
4860   int dstreg;
4861
4862   /* validate instruction */
4863   assert(inst->size == 4 || inst->size == 8);
4864   assert(inst->condflags == DRCUML_COND_ALWAYS);
4865
4866   /* normalize parameters */
4867   param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MRI);
4868
4869   /* degenerate cases -- convert to a move */
4870   if (srcp.type == DRCUML_PTYPE_IMMEDIATE)
4871      return convert_to_mov_imm(drcbe, dst, inst, &dstp, (INT8)srcp.value);
4872
4873   /* pick a target register for the general case */
4874   dstreg = param_select_register(REG_EAX, &dstp, NULL);
4875
4876   /* convert 8-bit source registers */
4877   if (srcp.type == DRCUML_PTYPE_INT_REGISTER && (srcp.value & 4))
4878   {
4879      emit_mov_r32_r32(&dst, REG_EAX, srcp.value);                           // mov   eax,srcp
4880      srcp.value = REG_EAX;
4881   }
4882
4883   /* 32-bit form */
4884   if (inst->size == 4)
4885   {
4886      /* general case */
4887      if (srcp.type == DRCUML_PTYPE_MEMORY)
4888         emit_movsx_r32_m8(&dst, dstreg, MABS(drcbe, srcp.value));               // movsx dstreg,[srcp]
4889      else if (srcp.type == DRCUML_PTYPE_INT_REGISTER)
4890         emit_movsx_r32_r8(&dst, dstreg, srcp.value);                        // movsx dstreg,srcp
4891      emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                           // mov   dstp,dstreg
4892   }
4893
4894   /* 64-bit form */
4895   else if (inst->size == 8)
4896   {
4897      /* general case */
4898      if (srcp.type == DRCUML_PTYPE_MEMORY)
4899         emit_movsx_r64_m8(&dst, dstreg, MABS(drcbe, srcp.value));               // movsx dstreg,[srcp]
4900      else if (srcp.type == DRCUML_PTYPE_INT_REGISTER)
4901         emit_movsx_r64_r8(&dst, dstreg, srcp.value);                        // movsx dstreg,srcp
4902      emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                           // mov   dstp,dstreg
4903   }
4904   return dst;
4905}
4906
4907
4908/*-------------------------------------------------
4909    op_sext2 - process a SEXT2 opcode
4910-------------------------------------------------*/
4911
4912static x86code *op_sext2(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4913{
4914   drcuml_parameter dstp, srcp;
4915   int dstreg;
4916
4917   /* validate instruction */
4918   assert(inst->size == 4 || inst->size == 8);
4919   assert(inst->condflags == DRCUML_COND_ALWAYS);
4920
4921   /* normalize parameters */
4922   param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MRI);
4923
4924   /* degenerate cases -- convert to a move */
4925   if (srcp.type == DRCUML_PTYPE_IMMEDIATE)
4926      return convert_to_mov_imm(drcbe, dst, inst, &dstp, (INT16)srcp.value);
4927
4928   /* pick a target register for the general case */
4929   dstreg = param_select_register(REG_EAX, &dstp, NULL);
4930
4931   /* 32-bit form */
4932   if (inst->size == 4)
4933   {
4934      /* general case */
4935      if (srcp.type == DRCUML_PTYPE_MEMORY)
4936         emit_movsx_r32_m16(&dst, dstreg, MABS(drcbe, srcp.value));               // movsx dstreg,[srcp]
4937      else if (srcp.type == DRCUML_PTYPE_INT_REGISTER)
4938         emit_movsx_r32_r16(&dst, dstreg, srcp.value);                        // movsx dstreg,srcp
4939      emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                           // mov   dstp,dstreg
4940   }
4941
4942   /* 64-bit form */
4943   else if (inst->size == 8)
4944   {
4945      /* general case */
4946      if (srcp.type == DRCUML_PTYPE_MEMORY)
4947         emit_movsx_r64_m16(&dst, dstreg, MABS(drcbe, srcp.value));               // movsx dstreg,[srcp]
4948      else if (srcp.type == DRCUML_PTYPE_INT_REGISTER)
4949         emit_movsx_r64_r16(&dst, dstreg, srcp.value);                        // movsx dstreg,srcp
4950      emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                           // mov   dstp,dstreg
4951   }
4952   return dst;
4953}
4954
4955
4956/*-------------------------------------------------
4957    op_sext4 - process a SEXT4 opcode
4958-------------------------------------------------*/
4959
4960static x86code *op_sext4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4961{
4962   drcuml_parameter dstp, srcp;
4963   int dstreg;
4964
4965   /* validate instruction */
4966   assert(inst->size == 8);
4967   assert(inst->condflags == DRCUML_COND_ALWAYS);
4968
4969   /* normalize parameters */
4970   param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MRI);
4971
4972   /* degenerate cases -- convert to a move */
4973   if (srcp.type == DRCUML_PTYPE_IMMEDIATE)
4974      return convert_to_mov_imm(drcbe, dst, inst, &dstp, (INT32)srcp.value);
4975
4976   /* pick a target register for the general case */
4977   dstreg = param_select_register(REG_EAX, &dstp, NULL);
4978
4979   /* 64-bit form */
4980   if (inst->size == 8)
4981   {
4982      /* general case */
4983      if (srcp.type == DRCUML_PTYPE_MEMORY)
4984         emit_movsxd_r64_m32(&dst, dstreg, MABS(drcbe, srcp.value));               // movsxd dstreg,[srcp]
4985      else if (srcp.type == DRCUML_PTYPE_INT_REGISTER)
4986         emit_movsxd_r64_r32(&dst, dstreg, srcp.value);                        // movsxd dstreg,srcp
4987      emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                           // mov   dstp,dstreg
4988   }
4989   return dst;
4990}
4991
4992
4993/*-------------------------------------------------
4994    op_add - process a ADD opcode
4995-------------------------------------------------*/
4996
4997static x86code *op_add(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4998{
4999   drcuml_parameter dstp, src1p, src2p;
5000   int dstreg;
5001
5002   /* validate instruction */
5003   assert(inst->size == 4 || inst->size == 8);
5004   assert((inst->condflags & ~(DRCUML_FLAG_C | DRCUML_FLAG_V | DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
5005
5006   /* normalize parameters */
5007   param_normalize_3_commutative(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
5008
5009   /* degenerate cases -- convert to a move */
5010   if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0)
5011      return convert_to_mov_imm(drcbe, dst, inst, &dstp, src1p.value + src2p.value);
5012   if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0)
5013      return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p);
5014
5015   /* pick a target register for the general case */
5016   dstreg = param_select_register(REG_EAX, &dstp, &src2p);
5017
5018   /* 32-bit form */
5019   if (inst->size == 4)
5020   {
5021      /* dstp == src1p in memory */
5022      if (dstp.type == DRCUML_PTYPE_MEMORY && src1p.type == DRCUML_PTYPE_MEMORY && src1p.value == dstp.value)
5023         emit_add_m32_p32(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst);            // add   [dstp],src2p
5024
5025      /* reg = reg + imm */
5026      else if (dstp.type == DRCUML_PTYPE_INT_REGISTER && src1p.type == DRCUML_PTYPE_INT_REGISTER && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0)
5027         emit_lea_r32_m32(&dst, dstp.value, MBD(src1p.value, src2p.value));         // lea   dstp,[src1p+src2p]
5028
5029      /* reg = reg + reg */
5030      else if (dstp.type == DRCUML_PTYPE_INT_REGISTER && src1p.type == DRCUML_PTYPE_INT_REGISTER && src2p.type == DRCUML_PTYPE_INT_REGISTER && inst->condflags == 0)
5031         emit_lea_r32_m32(&dst, dstp.value, MBISD(src1p.value, src2p.value, 1, 0));   // lea   dstp,[src1p+src2p]
5032
5033      /* general case */
5034      else
5035      {
5036         emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
5037         emit_add_r32_p32(drcbe, &dst, dstreg, &src2p, inst);                  // add   dstreg,src2p
5038         emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
5039      }
5040   }
5041
5042   /* 64-bit form */
5043   else if (inst->size == 8)
5044   {
5045      /* dstp == src1p in memory */
5046      if (dstp.type == DRCUML_PTYPE_MEMORY && src1p.type == DRCUML_PTYPE_MEMORY && src1p.value == dstp.value)
5047         emit_add_m64_p64(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst);      // add   [dstp],src2p
5048
5049      /* reg = reg + imm */
5050      else if (dstp.type == DRCUML_PTYPE_INT_REGISTER && src1p.type == DRCUML_PTYPE_INT_REGISTER && src2p.type == DRCUML_PTYPE_IMMEDIATE && short_immediate(src2p.value) && inst->condflags == 0)
5051         emit_lea_r64_m64(&dst, dstp.value, MBD(src1p.value, src2p.value));         // lea   dstp,[src1p+src2p]
5052
5053      /* reg = reg + reg */
5054      else if (dstp.type == DRCUML_PTYPE_INT_REGISTER && src1p.type == DRCUML_PTYPE_INT_REGISTER && src2p.type == DRCUML_PTYPE_INT_REGISTER && inst->condflags == 0)
5055         emit_lea_r64_m64(&dst, dstp.value, MBISD(src1p.value, src2p.value, 1, 0));   // lea   dstp,[src1p+src2p]
5056
5057      /* general case */
5058      else
5059      {
5060         emit_mov_r64_p64(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
5061         emit_add_r64_p64(drcbe, &dst, dstreg, &src2p, inst);                  // add   dstreg,src2p
5062         emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
5063      }
5064   }
5065   return dst;
5066}
5067
5068
5069/*-------------------------------------------------
5070    op_addc - process a ADDC opcode
5071-------------------------------------------------*/
5072
5073static x86code *op_addc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
5074{
5075   drcuml_parameter dstp, src1p, src2p;
5076   int dstreg;
5077
5078   /* validate instruction */
5079   assert(inst->size == 4 || inst->size == 8);
5080   assert((inst->condflags & ~(DRCUML_FLAG_C | DRCUML_FLAG_V | DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
5081
5082   /* normalize parameters */
5083   param_normalize_3_commutative(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
5084
5085   /* pick a target register for the general case */
5086   dstreg = param_select_register(REG_EAX, &dstp, &src2p);
5087
5088   /* 32-bit form */
5089   if (inst->size == 4)
5090   {
5091      /* dstp == src1p in memory */
5092      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
5093         emit_adc_m32_p32(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst);      // adc   [dstp],src2p
5094
5095      /* general case */
5096      else
5097      {
5098         emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
5099         emit_adc_r32_p32(drcbe, &dst, dstreg, &src2p, inst);                  // adc   dstreg,src2p
5100         emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
5101      }
5102   }
5103
5104   /* 64-bit form */
5105   else if (inst->size == 8)
5106   {
5107      /* dstp == src1p in memory */
5108      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
5109         emit_adc_m64_p64(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst);      // adc   [dstp],src2p
5110
5111      /* general case */
5112      else
5113      {
5114         emit_mov_r64_p64(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
5115         emit_adc_r64_p64(drcbe, &dst, dstreg, &src2p, inst);                  // adc   dstreg,src2p
5116         emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
5117      }
5118   }
5119   return dst;
5120}
5121
5122
5123/*-------------------------------------------------
5124    op_sub - process a SUB opcode
5125-------------------------------------------------*/
5126
5127static x86code *op_sub(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
5128{
5129   drcuml_parameter dstp, src1p, src2p;
5130   int dstreg;
5131
5132   /* validate instruction */
5133   assert(inst->size == 4 || inst->size == 8);
5134   assert((inst->condflags & ~(DRCUML_FLAG_C | DRCUML_FLAG_V | DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
5135
5136   /* normalize parameters */
5137   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
5138
5139   /* degenerate cases -- convert to a move */
5140   if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0)
5141      return convert_to_mov_imm(drcbe, dst, inst, &dstp, src1p.value - src2p.value);
5142   if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0)
5143      return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p);
5144
5145   /* pick a target register for the general case */
5146   dstreg = param_select_register(REG_EAX, &dstp, &src2p);
5147
5148   /* 32-bit form */
5149   if (inst->size == 4)
5150   {
5151      /* dstp == src1p in memory */
5152      if (dstp.type == DRCUML_PTYPE_MEMORY && src1p.type == DRCUML_PTYPE_MEMORY && src1p.value == dstp.value)
5153         emit_sub_m32_p32(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst);      // sub   [dstp],src2p
5154
5155      /* reg = reg - imm */
5156      else if (dstp.type == DRCUML_PTYPE_INT_REGISTER && src1p.type == DRCUML_PTYPE_INT_REGISTER && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0)
5157         emit_lea_r32_m32(&dst, dstp.value, MBD(src1p.value, -src2p.value));         // lea   dstp,[src1p-src2p]
5158
5159      /* general case */
5160      else
5161      {
5162         emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
5163         emit_sub_r32_p32(drcbe, &dst, dstreg, &src2p, inst);                  // sub   dstreg,src2p
5164         emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
5165      }
5166   }
5167
5168   /* 64-bit form */
5169   else if (inst->size == 8)
5170   {
5171      /* dstp == src1p in memory */
5172      if (dstp.type == DRCUML_PTYPE_MEMORY && src1p.type == DRCUML_PTYPE_MEMORY && src1p.value == dstp.value)
5173         emit_sub_m64_p64(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst);      // sub   [dstp],src2p
5174
5175      /* reg = reg - imm */
5176      else if (dstp.type == DRCUML_PTYPE_INT_REGISTER && src1p.type == DRCUML_PTYPE_INT_REGISTER && src2p.type == DRCUML_PTYPE_IMMEDIATE && short_immediate(src2p.type) && inst->condflags == 0)
5177         emit_lea_r64_m64(&dst, dstp.value, MBD(src1p.value, -src2p.value));         // lea   dstp,[src1p-src2p]
5178
5179      /* general case */
5180      else
5181      {
5182         emit_mov_r64_p64(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
5183         emit_sub_r64_p64(drcbe, &dst, dstreg, &src2p, inst);                  // sub   dstreg,src2p
5184         emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
5185      }
5186   }
5187   return dst;
5188}
5189
5190
5191/*-------------------------------------------------
5192    op_subc - process a SUBC opcode
5193-------------------------------------------------*/
5194
5195static x86code *op_subc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
5196{
5197   drcuml_parameter dstp, src1p, src2p;
5198   int dstreg;
5199
5200   /* validate instruction */
5201   assert(inst->size == 4 || inst->size == 8);
5202   assert((inst->condflags & ~(DRCUML_FLAG_C | DRCUML_FLAG_V | DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
5203
5204   /* normalize parameters */
5205   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
5206
5207   /* pick a target register for the general case */
5208   dstreg = param_select_register(REG_EAX, &dstp, &src2p);
5209
5210   /* 32-bit form */
5211   if (inst->size == 4)
5212   {
5213      /* dstp == src1p in memory */
5214      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
5215         emit_sbb_m32_p32(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst);      // sbb   [dstp],src2p
5216
5217      /* general case */
5218      else
5219      {
5220         emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
5221         emit_sbb_r32_p32(drcbe, &dst, dstreg, &src2p, inst);                  // sbb   dstreg,src2p
5222         emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
5223      }
5224   }
5225
5226   /* 64-bit form */
5227   else if (inst->size == 8)
5228   {
5229      /* dstp == src1p in memory */
5230      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
5231         emit_sbb_m64_p64(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst);      // sbb   [dstp],src2p
5232
5233      /* general case */
5234      else
5235      {
5236         emit_mov_r64_p64(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
5237         emit_sbb_r64_p64(drcbe, &dst, dstreg, &src2p, inst);                  // sbb   dstreg,src2p
5238         emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
5239      }
5240   }
5241   return dst;
5242}
5243
5244
5245/*-------------------------------------------------
5246    op_cmp - process a CMP opcode
5247-------------------------------------------------*/
5248
5249static x86code *op_cmp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
5250{
5251   drcuml_parameter src1p, src2p;
5252   int src1reg;
5253
5254   /* validate instruction */
5255   assert(inst->size == 4 || inst->size == 8);
5256   assert((inst->condflags & ~(DRCUML_FLAG_C | DRCUML_FLAG_V | DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
5257
5258   /* normalize parameters */
5259   param_normalize_2(drcbe, inst, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
5260
5261   /* degenerate cases */
5262   if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && src1p.value == src2p.value)
5263   {
5264      emit_cmp_r32_r32(&dst, REG_EAX, REG_EAX);                              // cmp   eax,eax
5265      return dst;
5266   }
5267
5268   /* pick a target register for the general case */
5269   src1reg = param_select_register(REG_EAX, &src1p, NULL);
5270
5271   /* 32-bit form */
5272   if (inst->size == 4)
5273   {
5274      /* memory versus anything */
5275      if (src1p.type == DRCUML_PTYPE_MEMORY)
5276         emit_cmp_m32_p32(drcbe, &dst, MABS(drcbe, src1p.value), &src2p, inst);      // cmp   [dstp],src2p
5277
5278      /* general case */
5279      else
5280      {
5281         if (src1p.type == DRCUML_PTYPE_IMMEDIATE)
5282            emit_mov_r32_imm(&dst, src1reg, src1p.value);                     // mov   src1reg,imm
5283         emit_cmp_r32_p32(drcbe, &dst, src1reg, &src2p, inst);                  // cmp   src1reg,src2p
5284      }
5285   }
5286
5287   /* 64-bit form */
5288   else
5289   {
5290      /* memory versus anything */
5291      if (src1p.type == DRCUML_PTYPE_MEMORY)
5292         emit_cmp_m64_p64(drcbe, &dst, MABS(drcbe, src1p.value), &src2p, inst);      // cmp   [dstp],src2p
5293
5294      /* general case */
5295      else
5296      {
5297         if (src1p.type == DRCUML_PTYPE_IMMEDIATE)
5298            emit_mov_r64_imm(&dst, src1reg, src1p.value);                     // mov   src1reg,imm
5299         emit_cmp_r64_p64(drcbe, &dst, src1reg, &src2p, inst);                  // cmp   src1reg,src2p
5300      }
5301   }
5302   return dst;
5303}
5304
5305
5306/*-------------------------------------------------
5307    op_mulu - process a MULU opcode
5308-------------------------------------------------*/
5309
5310static x86code *op_mulu(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
5311{
5312   drcuml_parameter dstp, edstp, src1p, src2p;
5313   int compute_hi;
5314
5315   /* validate instruction */
5316   assert(inst->size == 4 || inst->size == 8);
5317   assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
5318
5319   /* normalize parameters */
5320   param_normalize_4_commutative(drcbe, inst, &dstp, PTYPE_MR, &edstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
5321   compute_hi = (dstp.type != edstp.type || dstp.value != edstp.value);
5322
5323   /* degenerate cases -- convert to a move */
5324   if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0)
5325   {
5326      if (inst->size == 4)
5327      {
5328         if (!compute_hi)
5329            return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT32)src1p.value * (UINT32)src2p.value);
5330         else
5331         {
5332            UINT64 result = (UINT64)(UINT32)src1p.value * (UINT64)(UINT32)src2p.value;
5333            dst = convert_to_mov_imm(drcbe, dst, inst, &dstp, result & 0xffffffff);
5334            return convert_to_mov_imm(drcbe, dst, inst, &dstp, result >> 32);
5335         }
5336      }
5337   }
5338
5339   /* 32-bit form */
5340   if (inst->size == 4)
5341   {
5342      /* general case */
5343      emit_mov_r32_p32(drcbe, &dst, REG_EAX, &src1p);                           // mov   eax,src1p
5344      if (src2p.type == DRCUML_PTYPE_MEMORY)
5345         emit_mul_m32(&dst, MABS(drcbe, src2p.value));                        // mul   [src2p]
5346      else if (src2p.type == DRCUML_PTYPE_INT_REGISTER)
5347         emit_mul_r32(&dst, src2p.value);                                 // mul   src2p
5348      else if (src2p.type == DRCUML_PTYPE_IMMEDIATE)
5349      {
5350         emit_mov_r32_imm(&dst, REG_EDX, src2p.value);                        // mov   edx,src2p
5351         emit_mul_r32(&dst, REG_EDX);                                    // mul   edx
5352      }
5353      emit_mov_p32_r32(drcbe, &dst, &dstp, REG_EAX);                           // mov   dstp,eax
5354      if (compute_hi)
5355         emit_mov_p32_r32(drcbe, &dst, &edstp, REG_EDX);                        // mov   edstp,edx
5356   }
5357
5358   /* 64-bit form */
5359   else if (inst->size == 8)
5360   {
5361      /* general case */
5362      emit_mov_r64_p64(drcbe, &dst, REG_RAX, &src1p);                           // mov   rax,src1p
5363      if (src2p.type == DRCUML_PTYPE_MEMORY)
5364         emit_mul_m64(&dst, MABS(drcbe, src2p.value));                        // mul   [src2p]
5365      else if (src2p.type == DRCUML_PTYPE_INT_REGISTER)
5366         emit_mul_r64(&dst, src2p.value);                                 // mul   src2p
5367      else if (src2p.type == DRCUML_PTYPE_IMMEDIATE)
5368      {
5369         emit_mov_r64_imm(&dst, REG_RDX, src2p.value);                        // mov   rdx,src2p
5370         emit_mul_r64(&dst, REG_RDX);                                    // mul   rdx
5371      }
5372      emit_mov_p64_r64(drcbe, &dst, &dstp, REG_RAX);                           // mov   dstp,rax
5373      if (compute_hi)
5374         emit_mov_p64_r64(drcbe, &dst, &edstp, REG_RDX);                        // mov   edstp,rdx
5375   }
5376   return dst;
5377}
5378
5379
5380/*-------------------------------------------------
5381    op_muls - process a MULS opcode
5382-------------------------------------------------*/
5383
5384static x86code *op_muls(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
5385{
5386   drcuml_parameter dstp, edstp, src1p, src2p;
5387   int compute_hi;
5388
5389   /* validate instruction */
5390   assert(inst->size == 4 || inst->size == 8);
5391   assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
5392
5393   /* normalize parameters */
5394   param_normalize_4_commutative(drcbe, inst, &dstp, PTYPE_MR, &edstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
5395   compute_hi = (dstp.type != edstp.type || dstp.value != edstp.value);
5396
5397   /* degenerate cases -- convert to a move */
5398   if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0)
5399   {
5400      if (inst->size == 4)
5401      {
5402         if (!compute_hi)
5403            return convert_to_mov_imm(drcbe, dst, inst, &dstp, (INT32)src1p.value * (INT32)src2p.value);
5404         else
5405         {
5406            UINT64 result = (INT64)(INT32)src1p.value * (INT64)(INT32)src2p.value;
5407            dst = convert_to_mov_imm(drcbe, dst, inst, &dstp, result & 0xffffffff);
5408            return convert_to_mov_imm(drcbe, dst, inst, &dstp, result >> 32);
5409         }
5410      }
5411   }
5412
5413   /* 32-bit form */
5414   if (inst->size == 4)
5415   {
5416      /* 32-bit destination with memory/immediate or register/immediate */
5417      if (!compute_hi && src1p.type != DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE)
5418      {
5419         if (src1p.type == DRCUML_PTYPE_MEMORY)
5420            emit_imul_r32_m32_imm(&dst, REG_EAX, MABS(drcbe, src1p.value), src2p.value);   // imul  eax,[src1p],src2p
5421         else if (src1p.type == DRCUML_PTYPE_INT_REGISTER)
5422            emit_imul_r32_r32_imm(&dst, REG_EAX, src1p.value, src2p.value);         // imul  eax,src1p,src2p
5423         emit_mov_p32_r32(drcbe, &dst, &dstp, REG_EAX);                        // mov   dstp,eax
5424      }
5425
5426      /* 32-bit destination, general case */
5427      else if (!compute_hi)
5428      {
5429         emit_mov_r32_p32(drcbe, &dst, REG_EAX, &src1p);                        // mov   eax,src1p
5430         if (src2p.type == DRCUML_PTYPE_MEMORY)
5431            emit_imul_r32_m32(&dst, REG_EAX, MABS(drcbe, src2p.value));               // imul  eax,[src2p]
5432         else if (src2p.type == DRCUML_PTYPE_INT_REGISTER)
5433            emit_imul_r32_r32(&dst, REG_EAX, src2p.value);                     // imul  eax,src2p
5434         emit_mov_p32_r32(drcbe, &dst, &dstp, REG_EAX);                        // mov   dstp,eax
5435      }
5436
5437      /* 64-bit destination, general case */
5438      else
5439      {
5440         emit_mov_r32_p32(drcbe, &dst, REG_EAX, &src1p);                        // mov   eax,src1p
5441         if (src2p.type == DRCUML_PTYPE_MEMORY)
5442            emit_imul_m32(&dst, MABS(drcbe, src2p.value));                           // imul  [src2p]
5443         else if (src2p.type == DRCUML_PTYPE_INT_REGISTER)
5444            emit_imul_r32(&dst, src2p.value);                              // imul  src2p
5445         else if (src2p.type == DRCUML_PTYPE_IMMEDIATE)
5446         {
5447            emit_mov_r32_imm(&dst, REG_EDX, src2p.value);                     // mov   edx,src2p
5448            emit_imul_r32(&dst, REG_EDX);                                 // imul  edx
5449         }
5450         emit_mov_p32_r32(drcbe, &dst, &dstp, REG_EAX);                        // mov   dstp,eax
5451         emit_mov_p32_r32(drcbe, &dst, &edstp, REG_EDX);                        // mov   edstp,edx
5452      }
5453   }
5454
5455   /* 64-bit form */
5456   else if (inst->size == 8)
5457   {
5458      /* 64-bit destination with memory/immediate or register/immediate */
5459      if (!compute_hi && src1p.type != DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && short_immediate(src2p.type))
5460      {
5461         if (src1p.type == DRCUML_PTYPE_MEMORY)
5462            emit_imul_r64_m64_imm(&dst, REG_RAX, MABS(drcbe, src1p.value), src2p.value);// imul  rax,[src1p],src2p
5463         else if (src1p.type == DRCUML_PTYPE_INT_REGISTER)
5464            emit_imul_r64_r64_imm(&dst, REG_RAX, src1p.value, src2p.value);         // imul  rax,src1p,src2p
5465         emit_mov_p64_r64(drcbe, &dst, &dstp, REG_RAX);                        // mov   dstp,rax
5466      }
5467
5468      /* 64-bit destination, general case */
5469      else if (!compute_hi)
5470      {
5471         emit_mov_r64_p64(drcbe, &dst, REG_RAX, &src1p);                        // mov   rax,src1p
5472         if (src2p.type == DRCUML_PTYPE_MEMORY)
5473            emit_imul_r64_m64(&dst, REG_RAX, MABS(drcbe, src2p.value));            // imul  rax,[src2p]
5474         else if (src2p.type == DRCUML_PTYPE_INT_REGISTER)
5475            emit_imul_r64_r64(&dst, REG_RAX, src2p.value);                     // imul  rax,src2p
5476         emit_mov_p64_r64(drcbe, &dst, &dstp, REG_RAX);                        // mov   dstp,rax
5477      }
5478
5479      /* 128-bit destination, general case */
5480      else
5481      {
5482         emit_mov_r64_p64(drcbe, &dst, REG_RAX, &src1p);                        // mov   rax,src1p
5483         if (src2p.type == DRCUML_PTYPE_MEMORY)
5484            emit_imul_m64(&dst, MABS(drcbe, src2p.value));                     // imul  [src2p]
5485         else if (src2p.type == DRCUML_PTYPE_INT_REGISTER)
5486            emit_imul_r64(&dst, src2p.value);                              // imul  src2p
5487         else if (src2p.type == DRCUML_PTYPE_IMMEDIATE)
5488         {
5489            emit_mov_r64_imm(&dst, REG_RDX, src2p.value);                     // mov   rdx,src2p
5490            emit_imul_r64(&dst, REG_RDX);                                 // imul  rdx
5491         }
5492         emit_mov_p64_r64(drcbe, &dst, &dstp, REG_RAX);                        // mov   dstp,rax
5493         emit_mov_p64_r64(drcbe, &dst, &edstp, REG_RDX);                        // mov   edstp,rdx
5494      }
5495   }
5496   return dst;
5497}
5498
5499
5500/*-------------------------------------------------
5501    op_divu - process a DIVU opcode
5502-------------------------------------------------*/
5503
5504static x86code *op_divu(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
5505{
5506   drcuml_parameter dstp, edstp, src1p, src2p;
5507   int compute_rem;
5508   emit_link skip;
5509
5510   /* validate instruction */
5511   assert(inst->size == 4 || inst->size == 8);
5512   assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
5513
5514   /* normalize parameters */
5515   param_normalize_4(drcbe, inst, &dstp, PTYPE_MR, &edstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
5516   compute_rem = (dstp.type != edstp.type || dstp.value != edstp.value);
5517
5518   /* degenerate cases -- convert to a move */
5519   if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0)
5520   {
5521      if (inst->size == 4)
5522      {
5523         if ((UINT32)src2p.value == 0)
5524            return dst;
5525         if (!compute_rem)
5526            return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT32)src1p.value / (UINT32)src2p.value);
5527         else
5528         {
5529            dst = convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT32)src1p.value / (UINT32)src2p.value);
5530            return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT32)src1p.value % (UINT32)src2p.value);
5531         }
5532      }
5533      else if (inst->size == 8)
5534      {
5535         UINT64 reslo, reshi;
5536         if (src2p.value == 0)
5537            return dst;
5538         if (!compute_rem)
5539         {
5540            reslo = (UINT64)src1p.value / (UINT64)src2p.value;
5541            return convert_to_mov_imm(drcbe, dst, inst, &dstp, reslo);
5542         }
5543         else
5544         {
5545            reslo = (UINT64)src1p.value / (UINT64)src2p.value;
5546            reshi = (UINT64)src1p.value % (UINT64)src2p.value;
5547            dst = convert_to_mov_imm(drcbe, dst, inst, &dstp, reslo);
5548            return convert_to_mov_imm(drcbe, dst, inst, &dstp, reshi);
5549         }
5550      }
5551   }
5552
5553   /* 32-bit form */
5554   if (inst->size == 4)
5555   {
5556      /* general case */
5557      emit_mov_r32_p32(drcbe, &dst, REG_ECX, &src2p);                           // mov   ecx,src2p
5558      emit_jecxz_link(&dst, &skip);                                       // jecxz skip
5559      emit_mov_r32_p32(drcbe, &dst, REG_EAX, &src1p);                           // mov   eax,src1p
5560      emit_xor_r32_r32(&dst, REG_EDX, REG_EDX);                              // xor   edx,edx
5561      emit_div_r32(&dst, REG_ECX);                                       // div   ecx
5562      emit_mov_p32_r32(drcbe, &dst, &dstp, REG_EAX);                           // mov   dstp,eax
5563      if (compute_rem)
5564         emit_mov_p32_r32(drcbe, &dst, &edstp, REG_EDX);                        // mov   edstp,edx
5565      resolve_link(&dst, &skip);                                       // skip:
5566   }
5567
5568   /* 64-bit form */
5569   else if (inst->size == 8)
5570   {
5571      /* general case */
5572      emit_mov_r64_p64(drcbe, &dst, REG_RCX, &src2p);                           // mov   rcx,src2p
5573      emit_jrcxz_link(&dst, &skip);                                       // jrcxz skip
5574      emit_mov_r64_p64(drcbe, &dst, REG_RAX, &src1p);                           // mov   rax,src1p
5575      emit_xor_r32_r32(&dst, REG_EDX, REG_EDX);                              // xor   edx,edx
5576      emit_div_r64(&dst, REG_RCX);                                       // div   rcx
5577      emit_mov_p64_r64(drcbe, &dst, &dstp, REG_RAX);                           // mov   dstp,rax
5578      if (compute_rem)
5579         emit_mov_p64_r64(drcbe, &dst, &edstp, REG_RDX);                        // mov   edstp,rdx
5580      resolve_link(&dst, &skip);                                       // skip:
5581   }
5582   return dst;
5583}
5584
5585
5586/*-------------------------------------------------
5587    op_divs - process a DIVS opcode
5588-------------------------------------------------*/
5589
5590static x86code *op_divs(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
5591{
5592   drcuml_parameter dstp, edstp, src1p, src2p;
5593   int compute_rem;
5594   emit_link skip;
5595
5596   /* validate instruction */
5597   assert(inst->size == 4 || inst->size == 8);
5598   assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
5599
5600   /* normalize parameters */
5601   param_normalize_4(drcbe, inst, &dstp, PTYPE_MR, &edstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
5602   compute_rem = (dstp.type != edstp.type || dstp.value != edstp.value);
5603
5604   /* degenerate cases -- convert to a move */
5605   if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0)
5606   {
5607      if (inst->size == 4)
5608      {
5609         if ((INT32)src2p.value == 0)
5610            return dst;
5611         if (!compute_rem)
5612            return convert_to_mov_imm(drcbe, dst, inst, &dstp, (INT32)src1p.value / (INT32)src2p.value);
5613         else
5614         {
5615            dst = convert_to_mov_imm(drcbe, dst, inst, &dstp, (INT32)src1p.value / (INT32)src2p.value);
5616            return convert_to_mov_imm(drcbe, dst, inst, &dstp, (INT32)src1p.value % (INT32)src2p.value);
5617         }
5618      }
5619      else if (inst->size == 8)
5620      {
5621         UINT64 reslo, reshi;
5622         if (src2p.value == 0)
5623            return dst;
5624         if (!compute_rem)
5625         {
5626            reslo = (INT64)src1p.value / (INT64)src2p.value;
5627            return convert_to_mov_imm(drcbe, dst, inst, &dstp, reslo);
5628         }
5629         else
5630         {
5631            reslo = (INT64)src1p.value / (INT64)src2p.value;
5632            reshi = (INT64)src1p.value % (INT64)src2p.value;
5633            dst = convert_to_mov_imm(drcbe, dst, inst, &dstp, reslo);
5634            return convert_to_mov_imm(drcbe, dst, inst, &dstp, reshi);
5635         }
5636      }
5637   }
5638
5639   /* 32-bit form */
5640   if (inst->size == 4)
5641   {
5642      /* general case */
5643      emit_mov_r32_p32(drcbe, &dst, REG_ECX, &src2p);                           // mov   ecx,src2p
5644      emit_jecxz_link(&dst, &skip);                                       // jecxz skip
5645      emit_mov_r32_p32(drcbe, &dst, REG_EAX, &src1p);                           // mov   eax,src1p
5646      emit_cdq(&dst);                                                   // cdq
5647      emit_idiv_r32(&dst, REG_ECX);                                       // idiv  ecx
5648      emit_mov_p32_r32(drcbe, &dst, &dstp, REG_EAX);                           // mov   dstp,eax
5649      if (compute_rem)
5650         emit_mov_p32_r32(drcbe, &dst, &edstp, REG_EDX);                        // mov   edstp,edx
5651      resolve_link(&dst, &skip);                                       // skip:
5652   }
5653
5654   /* 64-bit form */
5655   else if (inst->size == 8)
5656   {
5657      /* general case */
5658      emit_mov_r64_p64(drcbe, &dst, REG_RCX, &src2p);                           // mov   rcx,src2p
5659      emit_jrcxz_link(&dst, &skip);                                       // jrcxz skip
5660      emit_mov_r64_p64(drcbe, &dst, REG_RAX, &src1p);                           // mov   rax,src1p
5661      emit_cqo(&dst);                                                   // cqo
5662      emit_idiv_r64(&dst, REG_RCX);                                       // idiv  rcx
5663      emit_mov_p64_r64(drcbe, &dst, &dstp, REG_RAX);                           // mov   dstp,rax
5664      if (compute_rem)
5665         emit_mov_p64_r64(drcbe, &dst, &edstp, REG_RDX);                        // mov   edstp,rdx
5666      resolve_link(&dst, &skip);                                       // skip:
5667   }
5668   return dst;
5669}
5670
5671
5672/*-------------------------------------------------
5673    op_and - process a AND opcode
5674-------------------------------------------------*/
5675
5676static x86code *op_and(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
5677{
5678   drcuml_parameter dstp, src1p, src2p;
5679   int dstreg;
5680
5681   /* validate instruction */
5682   assert(inst->size == 4 || inst->size == 8);
5683   assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
5684
5685   /* normalize parameters */
5686   param_normalize_3_commutative(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
5687
5688   /* degenerate cases -- convert to a move */
5689   if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0)
5690      return convert_to_mov_imm(drcbe, dst, inst, &dstp, src1p.value & src2p.value);
5691   if (src2p.type == DRCUML_PTYPE_IMMEDIATE && (src2p.value & size_to_mask[inst->size]) == size_to_mask[inst->size] && inst->condflags == 0)
5692      return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p);
5693
5694   /* pick a target register for the general case */
5695   dstreg = param_select_register(REG_EAX, &dstp, &src2p);
5696
5697   /* 32-bit form */
5698   if (inst->size == 4)
5699   {
5700      /* dstp == src1p in memory */
5701      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
5702         emit_and_m32_p32(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst);      // and   [dstp],src2p
5703
5704      /* general case */
5705      else
5706      {
5707         emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
5708         emit_and_r32_p32(drcbe, &dst, dstreg, &src2p, inst);                  // and   dstreg,src2p
5709         emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
5710      }
5711   }
5712
5713   /* 64-bit form */
5714   else if (inst->size == 8)
5715   {
5716      /* dstp == src1p in memory */
5717      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
5718         emit_and_m64_p64(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst);      // and   [dstp],src2p
5719
5720      /* general case */
5721      else
5722      {
5723         emit_mov_r64_p64(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
5724         emit_and_r64_p64(drcbe, &dst, dstreg, &src2p, inst);                  // and   dstreg,src2p
5725         emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
5726      }
5727   }
5728   return dst;
5729}
5730
5731
5732/*-------------------------------------------------
5733    op_test - process a TEST opcode
5734-------------------------------------------------*/
5735
5736static x86code *op_test(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
5737{
5738   drcuml_parameter src1p, src2p;
5739   int src1reg;
5740
5741   /* validate instruction */
5742   assert(inst->size == 4 || inst->size == 8);
5743   assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
5744
5745   /* normalize parameters */
5746   param_normalize_2_commutative(drcbe, inst, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
5747
5748   /* pick a target register for the general case */
5749   src1reg = param_select_register(REG_EAX, &src1p, NULL);
5750
5751   /* 32-bit form */
5752   if (inst->size == 4)
5753   {
5754      /* src1p in memory */
5755      if (src1p.type == DRCUML_PTYPE_MEMORY)
5756         emit_test_m32_p32(drcbe, &dst, MABS(drcbe, src1p.value), &src2p, inst);      // test  [src1p],src2p
5757
5758      /* general case */
5759      else
5760      {
5761         emit_mov_r32_p32(drcbe, &dst, src1reg, &src1p);                        // mov   src1reg,src1p
5762         emit_test_r32_p32(drcbe, &dst, src1reg, &src2p, inst);                  // test  src1reg,src2p
5763      }
5764   }
5765
5766   /* 64-bit form */
5767   else if (inst->size == 8)
5768   {
5769      /* src1p in memory */
5770      if (src1p.type == DRCUML_PTYPE_MEMORY)
5771         emit_test_m64_p64(drcbe, &dst, MABS(drcbe, src1p.value), &src2p, inst);      // test  [src1p],src2p
5772
5773      /* general case */
5774      else
5775      {
5776         emit_mov_r64_p64(drcbe, &dst, src1reg, &src1p);                        // mov   src1reg,src1p
5777         emit_test_r64_p64(drcbe, &dst, src1reg, &src2p, inst);                  // test  src1reg,src2p
5778      }
5779   }
5780   return dst;
5781}
5782
5783
5784/*-------------------------------------------------
5785    op_or - process a OR opcode
5786-------------------------------------------------*/
5787
5788static x86code *op_or(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
5789{
5790   drcuml_parameter dstp, src1p, src2p;
5791   int dstreg;
5792
5793   /* validate instruction */
5794   assert(inst->size == 4 || inst->size == 8);
5795   assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
5796
5797   /* normalize parameters */
5798   param_normalize_3_commutative(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
5799
5800   /* degenerate cases -- convert to a move */
5801   if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0)
5802      return convert_to_mov_imm(drcbe, dst, inst, &dstp, src1p.value | src2p.value);
5803   if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0)
5804      return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p);
5805
5806   /* pick a target register for the general case */
5807   dstreg = param_select_register(REG_EAX, &dstp, &src2p);
5808
5809   /* 32-bit form */
5810   if (inst->size == 4)
5811   {
5812      /* dstp == src1p in memory */
5813      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
5814         emit_or_m32_p32(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst);      // or    [dstp],src2p
5815
5816      /* general case */
5817      else
5818      {
5819         emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
5820         emit_or_r32_p32(drcbe, &dst, dstreg, &src2p, inst);                     // or    dstreg,src2p
5821         emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
5822      }
5823   }
5824
5825   /* 64-bit form */
5826   else if (inst->size == 8)
5827   {
5828      /* dstp == src1p in memory */
5829      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
5830         emit_or_m64_p64(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst);      // or    [dstp],src2p
5831
5832      /* general case */
5833      else
5834      {
5835         emit_mov_r64_p64(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
5836         emit_or_r64_p64(drcbe, &dst, dstreg, &src2p, inst);                     // or    dstreg,src2p
5837         emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
5838      }
5839   }
5840   return dst;
5841}
5842
5843
5844/*-------------------------------------------------
5845    op_xor - process a XOR opcode
5846-------------------------------------------------*/
5847
5848static x86code *op_xor(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
5849{
5850   drcuml_parameter dstp, src1p, src2p;
5851   int dstreg;
5852
5853   /* validate instruction */
5854   assert(inst->size == 4 || inst->size == 8);
5855   assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
5856
5857   /* normalize parameters */
5858   param_normalize_3_commutative(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
5859
5860   /* degenerate cases -- convert to a move */
5861   if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0)
5862      return convert_to_mov_imm(drcbe, dst, inst, &dstp, src1p.value & src2p.value);
5863   if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0)
5864      return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p);
5865
5866   /* pick a target register for the general case */
5867   dstreg = param_select_register(REG_EAX, &dstp, &src2p);
5868
5869   /* 32-bit form */
5870   if (inst->size == 4)
5871   {
5872      /* dstp == src1p in memory */
5873      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
5874         emit_xor_m32_p32(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst);      // xor   [dstp],src2p
5875
5876      /* general case */
5877      else
5878      {
5879         emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
5880         emit_xor_r32_p32(drcbe, &dst, dstreg, &src2p, inst);                  // xor   dstreg,src2p
5881         emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
5882      }
5883   }
5884
5885   /* 64-bit form */
5886   else if (inst->size == 8)
5887   {
5888      /* dstp == src1p in memory */
5889      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
5890         emit_xor_m64_p64(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst);      // xor   [dstp],src2p
5891
5892      /* general case */
5893      else
5894      {
5895         emit_mov_r64_p64(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
5896         emit_xor_r64_p64(drcbe, &dst, dstreg, &src2p, inst);                  // xor   dstreg,src2p
5897         emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
5898      }
5899   }
5900   return dst;
5901}
5902
5903
5904/*-------------------------------------------------
5905    op_shl - process a SHL opcode
5906-------------------------------------------------*/
5907
5908static x86code *op_shl(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
5909{
5910   drcuml_parameter dstp, src1p, src2p;
5911   int dstreg;
5912
5913   /* validate instruction */
5914   assert(inst->size == 4 || inst->size == 8);
5915   assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
5916
5917   /* normalize parameters */
5918   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
5919
5920   /* degenerate cases -- convert to a move */
5921   if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0)
5922      return convert_to_mov_imm(drcbe, dst, inst, &dstp, src1p.value << src2p.value);
5923   if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0)
5924      return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p);
5925
5926   /* pick a target register for the general case */
5927   dstreg = param_select_register(REG_EAX, &dstp, &src2p);
5928
5929   /* 32-bit form */
5930   if (inst->size == 4)
5931   {
5932      /* dstp == src1p in memory */
5933      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
5934         emit_shl_m32_p32(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst);      // shl   [dstp],src2p
5935
5936      /* general case */
5937      else
5938      {
5939         emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
5940         emit_shl_r32_p32(drcbe, &dst, dstreg, &src2p, inst);                  // shl   dstreg,src2p
5941         emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
5942      }
5943   }
5944
5945   /* 64-bit form */
5946   else if (inst->size == 8)
5947   {
5948      /* dstp == src1p in memory */
5949      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
5950         emit_shl_m64_p64(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst);      // shl   [dstp],src2p
5951
5952      /* general case */
5953      else
5954      {
5955         emit_mov_r64_p64(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
5956         emit_shl_r64_p64(drcbe, &dst, dstreg, &src2p, inst);                  // shl   dstreg,src2p
5957         emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
5958      }
5959   }
5960   return dst;
5961}
5962
5963
5964/*-------------------------------------------------
5965    op_shr - process a SHR opcode
5966-------------------------------------------------*/
5967
5968static x86code *op_shr(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
5969{
5970   drcuml_parameter dstp, src1p, src2p;
5971   int dstreg;
5972
5973   /* validate instruction */
5974   assert(inst->size == 4 || inst->size == 8);
5975   assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
5976
5977   /* normalize parameters */
5978   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
5979
5980   /* degenerate cases -- convert to a move */
5981   if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0)
5982      return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT64)src1p.value >> src2p.value);
5983   if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0)
5984      return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p);
5985
5986   /* pick a target register for the general case */
5987   dstreg = param_select_register(REG_EAX, &dstp, &src2p);
5988
5989   /* 32-bit form */
5990   if (inst->size == 4)
5991   {
5992      /* dstp == src1p in memory */
5993      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
5994         emit_shr_m32_p32(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst);      // shr   [dstp],src2p
5995
5996      /* general case */
5997      else
5998      {
5999         emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
6000         emit_shr_r32_p32(drcbe, &dst, dstreg, &src2p, inst);                  // shr   dstreg,src2p
6001         emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
6002      }
6003   }
6004
6005   /* 64-bit form */
6006   else if (inst->size == 8)
6007   {
6008      /* dstp == src1p in memory */
6009      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
6010         emit_shr_m64_p64(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst);      // shr   [dstp],src2p
6011
6012      /* general case */
6013      else
6014      {
6015         emit_mov_r64_p64(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
6016         emit_shr_r64_p64(drcbe, &dst, dstreg, &src2p, inst);                  // shr   dstreg,src2p
6017         emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
6018      }
6019   }
6020   return dst;
6021}
6022
6023
6024/*-------------------------------------------------
6025    op_sar - process a SAR opcode
6026-------------------------------------------------*/
6027
6028static x86code *op_sar(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6029{
6030   drcuml_parameter dstp, src1p, src2p;
6031   int dstreg;
6032
6033   /* validate instruction */
6034   assert(inst->size == 4 || inst->size == 8);
6035   assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
6036
6037   /* normalize parameters */
6038   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
6039
6040   /* degenerate cases -- convert to a move */
6041   if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0)
6042   {
6043      if (inst->size == 4)
6044         return convert_to_mov_imm(drcbe, dst, inst, &dstp, (INT32)src1p.value >> src2p.value);
6045      else if (inst->size == 8)
6046         return convert_to_mov_imm(drcbe, dst, inst, &dstp, (INT64)src1p.value >> src2p.value);
6047   }
6048   if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0)
6049      return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p);
6050
6051   /* pick a target register for the general case */
6052   dstreg = param_select_register(REG_EAX, &dstp, &src2p);
6053
6054   /* 32-bit form */
6055   if (inst->size == 4)
6056   {
6057      /* dstp == src1p in memory */
6058      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
6059         emit_sar_m32_p32(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst);      // sar   [dstp],src2p
6060
6061      /* general case */
6062      else
6063      {
6064         emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
6065         emit_sar_r32_p32(drcbe, &dst, dstreg, &src2p, inst);                  // sar   dstreg,src2p
6066         emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
6067      }
6068   }
6069
6070   /* 64-bit form */
6071   else if (inst->size == 8)
6072   {
6073      /* dstp == src1p in memory */
6074      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
6075         emit_sar_m64_p64(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst);      // sar   [dstp],src2p
6076
6077      /* general case */
6078      else
6079      {
6080         emit_mov_r64_p64(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
6081         emit_sar_r64_p64(drcbe, &dst, dstreg, &src2p, inst);                  // sar   dstreg,src2p
6082         emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
6083      }
6084   }
6085   return dst;
6086}
6087
6088
6089/*-------------------------------------------------
6090    op_rol - process a rol opcode
6091-------------------------------------------------*/
6092
6093static x86code *op_rol(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6094{
6095   drcuml_parameter dstp, src1p, src2p;
6096   int dstreg;
6097
6098   /* validate instruction */
6099   assert(inst->size == 4 || inst->size == 8);
6100   assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
6101
6102   /* normalize parameters */
6103   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
6104
6105   /* degenerate cases -- convert to a move */
6106   if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0)
6107      return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT64)src1p.value >> src2p.value);
6108   if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0)
6109      return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p);
6110
6111   /* pick a target register for the general case */
6112   dstreg = param_select_register(REG_EAX, &dstp, &src2p);
6113
6114   /* 32-bit form */
6115   if (inst->size == 4)
6116   {
6117      /* dstp == src1p in memory */
6118      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
6119         emit_rol_m32_p32(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst);      // rol   [dstp],src2p
6120
6121      /* general case */
6122      else
6123      {
6124         emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
6125         emit_rol_r32_p32(drcbe, &dst, dstreg, &src2p, inst);                  // rol   dstreg,src2p
6126         emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
6127      }
6128   }
6129
6130   /* 64-bit form */
6131   else if (inst->size == 8)
6132   {
6133      /* dstp == src1p in memory */
6134      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
6135         emit_rol_m64_p64(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst);      // rol   [dstp],src2p
6136
6137      /* general case */
6138      else
6139      {
6140         emit_mov_r64_p64(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
6141         emit_rol_r64_p64(drcbe, &dst, dstreg, &src2p, inst);                  // rol   dstreg,src2p
6142         emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
6143      }
6144   }
6145   return dst;
6146}
6147
6148
6149/*-------------------------------------------------
6150    op_ror - process a ROR opcode
6151-------------------------------------------------*/
6152
6153static x86code *op_ror(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6154{
6155   drcuml_parameter dstp, src1p, src2p;
6156   int dstreg;
6157
6158   /* validate instruction */
6159   assert(inst->size == 4 || inst->size == 8);
6160   assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
6161
6162   /* normalize parameters */
6163   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
6164
6165   /* degenerate cases -- convert to a move */
6166   if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0)
6167      return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT64)src1p.value >> src2p.value);
6168   if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0)
6169      return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p);
6170
6171   /* pick a target register for the general case */
6172   dstreg = param_select_register(REG_EAX, &dstp, &src2p);
6173
6174   /* 32-bit form */
6175   if (inst->size == 4)
6176   {
6177      /* dstp == src1p in memory */
6178      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
6179         emit_ror_m32_p32(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst);      // ror   [dstp],src2p
6180
6181      /* general case */
6182      else
6183      {
6184         emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
6185         emit_ror_r32_p32(drcbe, &dst, dstreg, &src2p, inst);                  // ror   dstreg,src2p
6186         emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
6187      }
6188   }
6189
6190   /* 64-bit form */
6191   else if (inst->size == 8)
6192   {
6193      /* dstp == src1p in memory */
6194      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
6195         emit_ror_m64_p64(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst);      // ror   [dstp],src2p
6196
6197      /* general case */
6198      else
6199      {
6200         emit_mov_r64_p64(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
6201         emit_ror_r64_p64(drcbe, &dst, dstreg, &src2p, inst);                  // ror   dstreg,src2p
6202         emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
6203      }
6204   }
6205   return dst;
6206}
6207
6208
6209/*-------------------------------------------------
6210    op_rolc - process a ROLC opcode
6211-------------------------------------------------*/
6212
6213static x86code *op_rolc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6214{
6215   drcuml_parameter dstp, src1p, src2p;
6216   int dstreg;
6217
6218   /* validate instruction */
6219   assert(inst->size == 4 || inst->size == 8);
6220   assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
6221
6222   /* normalize parameters */
6223   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
6224
6225   /* degenerate cases -- convert to a move */
6226   if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0)
6227      return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT64)src1p.value >> src2p.value);
6228   if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0)
6229      return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p);
6230
6231   /* pick a target register for the general case */
6232   dstreg = param_select_register(REG_EAX, &dstp, &src2p);
6233
6234   /* 32-bit form */
6235   if (inst->size == 4)
6236   {
6237      /* dstp == src1p in memory */
6238      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
6239         emit_rcl_m32_p32(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst);      // rcl   [dstp],src2p
6240
6241      /* general case */
6242      else
6243      {
6244         emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
6245         emit_rcl_r32_p32(drcbe, &dst, dstreg, &src2p, inst);                  // rcl   dstreg,src2p
6246         emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
6247      }
6248   }
6249
6250   /* 64-bit form */
6251   else if (inst->size == 8)
6252   {
6253      /* dstp == src1p in memory */
6254      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
6255         emit_rcl_m64_p64(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst);      // rcl   [dstp],src2p
6256
6257      /* general case */
6258      else
6259      {
6260         emit_mov_r64_p64(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
6261         emit_rcl_r64_p64(drcbe, &dst, dstreg, &src2p, inst);                  // rcl   dstreg,src2p
6262         emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
6263      }
6264   }
6265   return dst;
6266}
6267
6268
6269/*-------------------------------------------------
6270    op_rorc - process a RORC opcode
6271-------------------------------------------------*/
6272
6273static x86code *op_rorc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6274{
6275   drcuml_parameter dstp, src1p, src2p;
6276   int dstreg;
6277
6278   /* validate instruction */
6279   assert(inst->size == 4 || inst->size == 8);
6280   assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
6281
6282   /* normalize parameters */
6283   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
6284
6285   /* degenerate cases -- convert to a move */
6286   if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0)
6287      return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT64)src1p.value >> src2p.value);
6288   if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0)
6289      return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p);
6290
6291   /* pick a target register for the general case */
6292   dstreg = param_select_register(REG_EAX, &dstp, &src2p);
6293
6294   /* 32-bit form */
6295   if (inst->size == 4)
6296   {
6297      /* dstp == src1p in memory */
6298      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
6299         emit_rcr_m32_p32(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst);      // rcr   [dstp],src2p
6300
6301      /* general case */
6302      else
6303      {
6304         emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
6305         emit_rcr_r32_p32(drcbe, &dst, dstreg, &src2p, inst);                  // rcr   dstreg,src2p
6306         emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
6307      }
6308   }
6309
6310   /* 64-bit form */
6311   else if (inst->size == 8)
6312   {
6313      /* dstp == src1p in memory */
6314      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
6315         emit_rcr_m64_p64(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst);      // rcr   [dstp],src2p
6316
6317      /* general case */
6318      else
6319      {
6320         emit_mov_r64_p64(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
6321         emit_rcr_r64_p64(drcbe, &dst, dstreg, &src2p, inst);                  // rcr   dstreg,src2p
6322         emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
6323      }
6324   }
6325   return dst;
6326}
6327
6328
6329
6330/***************************************************************************
6331    FLOATING POINT OPERATIONS
6332***************************************************************************/
6333
6334/*-------------------------------------------------
6335    op_fload - process a FLOAD opcode
6336-------------------------------------------------*/
6337
6338static x86code *op_fload(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6339{
6340   drcuml_parameter dstp, basep, indp;
6341   int basereg, dstreg;
6342   INT32 baseoffs;
6343
6344   /* validate instruction */
6345   assert(inst->size == 4 || inst->size == 8);
6346   assert(inst->condflags == DRCUML_COND_ALWAYS);
6347
6348   /* normalize parameters */
6349   param_normalize_3(drcbe, inst, &dstp, PTYPE_MF, &basep, PTYPE_M, &indp, PTYPE_MRI);
6350
6351   /* pick a target register for the general case */
6352   dstreg = param_select_register(REG_XMM0, &dstp, NULL);
6353
6354   /* determine the pointer base */
6355   basereg = get_base_register_and_offset(drcbe, &dst, basep.value, REG_RDX, &baseoffs);
6356
6357   /* 32-bit form */
6358   if (inst->size == 4)
6359   {
6360      if (indp.type == DRCUML_PTYPE_IMMEDIATE)
6361         emit_movss_r128_m32(&dst, dstreg, MBD(basereg, baseoffs + 4*indp.value));   // movss  dstreg,[basep + 4*indp]
6362      else
6363      {
6364         int indreg = param_select_register(REG_ECX, &indp, NULL);
6365         emit_mov_r32_p32(drcbe, &dst, indreg, &indp);                        // mov    indreg,indp
6366         emit_movss_r128_m32(&dst, dstreg, MBISD(basereg, indreg, 4, baseoffs));      // movss  dstreg,[basep + 4*indp]
6367      }
6368      emit_movss_p32_r128(drcbe, &dst, &dstp, dstreg);                        // movss  dstp,dstreg
6369   }
6370
6371   /* 64-bit form */
6372   else if (inst->size == 8)
6373   {
6374      if (indp.type == DRCUML_PTYPE_IMMEDIATE)
6375         emit_movsd_r128_m64(&dst, dstreg, MBD(basereg, baseoffs + 8*indp.value));   // movsd  dstreg,[basep + 8*indp]
6376      else
6377      {
6378         int indreg = param_select_register(REG_ECX, &indp, NULL);
6379         emit_mov_r32_p32(drcbe, &dst, indreg, &indp);                        // mov    indreg,indp
6380         emit_movsd_r128_m64(&dst, dstreg, MBISD(basereg, indreg, 8, baseoffs));      // movsd  dstreg,[basep + 8*indp]
6381      }
6382      emit_movsd_p64_r128(drcbe, &dst, &dstp, dstreg);                        // movsd  dstp,dstreg
6383   }
6384   return dst;
6385}
6386
6387
6388/*-------------------------------------------------
6389    op_fstore - process a FSTORE opcode
6390-------------------------------------------------*/
6391
6392static x86code *op_fstore(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6393{
6394   drcuml_parameter srcp, basep, indp;
6395   int basereg, srcreg;
6396   INT32 baseoffs;
6397
6398   /* validate instruction */
6399   assert(inst->size == 4 || inst->size == 8);
6400   assert(inst->condflags == DRCUML_COND_ALWAYS);
6401
6402   /* normalize parameters */
6403   param_normalize_3(drcbe, inst, &basep, PTYPE_M, &indp, PTYPE_MRI, &srcp, PTYPE_MF);
6404
6405   /* pick a target register for the general case */
6406   srcreg = param_select_register(REG_XMM0, &srcp, NULL);
6407
6408   /* determine the pointer base */
6409   basereg = get_base_register_and_offset(drcbe, &dst, basep.value, REG_RDX, &baseoffs);
6410
6411   /* 32-bit form */
6412   if (inst->size == 4)
6413   {
6414      emit_movss_r128_p32(drcbe, &dst, srcreg, &srcp);                        // movss  srcreg,srcp
6415      if (indp.type == DRCUML_PTYPE_IMMEDIATE)
6416         emit_movss_m32_r128(&dst, MBD(basereg, baseoffs + 4*indp.value), srcreg);   // movss  [basep + 4*indp],srcreg
6417      else
6418      {
6419         int indreg = param_select_register(REG_ECX, &indp, NULL);
6420         emit_mov_r32_p32(drcbe, &dst, indreg, &indp);                        // mov    indreg,indp
6421         emit_movss_m32_r128(&dst, MBISD(basereg, indreg, 4, baseoffs), srcreg);      // movss  [basep + 4*indp],srcreg
6422      }
6423   }
6424
6425   /* 64-bit form */
6426   else if (inst->size == 8)
6427   {
6428      emit_movsd_r128_p64(drcbe, &dst, srcreg, &srcp);                        // movsd  srcreg,srcp
6429      if (indp.type == DRCUML_PTYPE_IMMEDIATE)
6430         emit_movsd_m64_r128(&dst, MBD(basereg, baseoffs + 8*indp.value), srcreg);   // movsd  [basep + 8*indp],srcreg
6431      else
6432      {
6433         int indreg = param_select_register(REG_ECX, &indp, NULL);
6434         emit_mov_r32_p32(drcbe, &dst, indreg, &indp);                        // mov    indreg,indp
6435         emit_movsd_m64_r128(&dst, MBISD(basereg, indreg, 8, baseoffs), srcreg);      // movsd  [basep + 8*indp],srcreg
6436      }
6437   }
6438   return dst;
6439}
6440
6441
6442/*-------------------------------------------------
6443    op_fread - process a FREAD opcode
6444-------------------------------------------------*/
6445
6446static x86code *op_fread(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6447{
6448   drcuml_parameter dstp, spacep, addrp;
6449
6450   /* validate instruction */
6451   assert(inst->size == 4 || inst->size == 8);
6452   assert(inst->condflags == DRCUML_COND_ALWAYS);
6453
6454   /* normalize parameters */
6455   param_normalize_3(drcbe, inst, &dstp, PTYPE_MF, &spacep, PTYPE_I, &addrp, PTYPE_MRI);
6456
6457   /* set up a call to the read dword/qword handler */
6458   emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &addrp);                           // mov    param1,addrp
6459   if (inst->size == 4)
6460      emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].read_dword);// call   read_dword
6461   else if (inst->size == 8)
6462      emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].read_qword);// call   read_qword
6463
6464   /* store result */
6465   if (inst->size == 4)
6466   {
6467      if (dstp.type == DRCUML_PTYPE_MEMORY)
6468         emit_mov_m32_r32(&dst, MABS(drcbe, dstp.value), REG_EAX);               // mov   [dstp],eax
6469      else if (dstp.type == DRCUML_PTYPE_FLOAT_REGISTER)
6470         emit_movd_r128_r32(&dst, dstp.value, REG_EAX);                        // movd  dstp,eax
6471   }
6472   else if (inst->size == 8)
6473   {
6474      if (dstp.type == DRCUML_PTYPE_MEMORY)
6475         emit_mov_m64_r64(&dst, MABS(drcbe, dstp.value), REG_RAX);               // mov   [dstp],rax
6476      else if (dstp.type == DRCUML_PTYPE_FLOAT_REGISTER)
6477         emit_movq_r128_r64(&dst, dstp.value, REG_RAX);                        // movq  dstp,rax
6478   }
6479   return dst;
6480}
6481
6482
6483/*-------------------------------------------------
6484    op_fwrite - process a FWRITE opcode
6485-------------------------------------------------*/
6486
6487static x86code *op_fwrite(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6488{
6489   drcuml_parameter spacep, addrp, srcp;
6490
6491   /* validate instruction */
6492   assert(inst->size == 4 || inst->size == 8);
6493   assert(inst->condflags == DRCUML_COND_ALWAYS);
6494
6495   /* normalize parameters */
6496   param_normalize_3(drcbe, inst, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &srcp, PTYPE_MF);
6497
6498   /* general case */
6499   emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &addrp);                           // mov    param1,addrp
6500
6501   /* 32-bit form */
6502   if (inst->size == 4)
6503   {
6504      if (srcp.type == DRCUML_PTYPE_MEMORY)
6505         emit_mov_r32_m32(&dst, REG_PARAM2, MABS(drcbe, srcp.value));            // mov    param2,[srcp]
6506      else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER)
6507         emit_movd_r32_r128(&dst, REG_PARAM2, srcp.value);                     // movd   param2,srcp
6508      emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].write_dword);// call   write_dword
6509   }
6510
6511   /* 64-bit form */
6512   else if (inst->size == 8)
6513   {
6514      if (srcp.type == DRCUML_PTYPE_MEMORY)
6515         emit_mov_r64_m64(&dst, REG_PARAM2, MABS(drcbe, srcp.value));            // mov    param2,[srcp]
6516      else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER)
6517         emit_movq_r64_r128(&dst, REG_PARAM2, srcp.value);                     // movq   param2,srcp
6518      emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].write_qword);// call   write_qword
6519   }
6520
6521   return dst;
6522}
6523
6524
6525/*-------------------------------------------------
6526    op_fmov - process a FMOV opcode
6527-------------------------------------------------*/
6528
6529static x86code *op_fmov(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6530{
6531   drcuml_parameter dstp, srcp;
6532   emit_link skip = { 0 };
6533   int dstreg;
6534
6535   /* validate instruction */
6536   assert(inst->size == 4 || inst->size == 8);
6537   assert(inst->condflags == DRCUML_COND_ALWAYS || (inst->condflags >= COND_Z && inst->condflags < DRCUML_COND_MAX));
6538
6539   /* normalize parameters */
6540   param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MF);
6541
6542   /* pick a target register for the general case */
6543   dstreg = param_select_register(REG_XMM0, &dstp, NULL);
6544
6545   /* always start with a jmp */
6546   if (inst->condflags != DRCUML_COND_ALWAYS)
6547      emit_jcc_short_link(&dst, X86_NOT_CONDITION(inst->condflags), &skip);         // jcc   skip
6548
6549   /* 32-bit form */
6550   if (inst->size == 4)
6551   {
6552      emit_movss_r128_p32(drcbe, &dst, dstreg, &srcp);                        // movss dstreg,srcp
6553      emit_movss_p32_r128(drcbe, &dst, &dstp, dstreg);                        // movss dstp,dstreg
6554   }
6555
6556   /* 64-bit form */
6557   else if (inst->size == 8)
6558   {
6559      emit_movsd_r128_p64(drcbe, &dst, dstreg, &srcp);                        // movsd dstreg,srcp
6560      emit_movsd_p64_r128(drcbe, &dst, &dstp, dstreg);                        // movsd dstp,dstreg
6561   }
6562
6563   /* resolve the jump */
6564   if (inst->condflags != DRCUML_COND_ALWAYS)
6565      resolve_link(&dst, &skip);                                       // skip:
6566   return dst;
6567}
6568
6569
6570/*-------------------------------------------------
6571    op_ftoi4 - process a FTOI4 opcode
6572-------------------------------------------------*/
6573
6574static x86code *op_ftoi4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6575{
6576   drcuml_parameter dstp, srcp;
6577   int dstreg;
6578
6579   /* validate instruction */
6580   assert(inst->size == 4 || inst->size == 8);
6581   assert(inst->condflags == DRCUML_COND_ALWAYS);
6582
6583   /* normalize parameters */
6584   param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF);
6585
6586   /* pick a target register for the general case */
6587   dstreg = param_select_register(REG_EAX, &dstp, NULL);
6588
6589   /* 32-bit form */
6590   if (inst->size == 4)
6591   {
6592      if (srcp.type == DRCUML_PTYPE_MEMORY)
6593         emit_cvtss2si_r32_m32(&dst, dstreg, MABS(drcbe, srcp.value));            // cvtss2si dstreg,[srcp]
6594      else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER)
6595         emit_cvtss2si_r32_r128(&dst, dstreg, srcp.value);                     // cvtss2si dstreg,srcp
6596   }
6597
6598   /* 64-bit form */
6599   else if (inst->size == 8)
6600   {
6601      if (srcp.type == DRCUML_PTYPE_MEMORY)
6602         emit_cvtsd2si_r32_m64(&dst, dstreg, MABS(drcbe, srcp.value));            // cvtsd2si dstreg,[srcp]
6603      else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER)
6604         emit_cvtsd2si_r32_r128(&dst, dstreg, srcp.value);                     // cvtsd2si dstreg,srcp
6605   }
6606
6607   /* general case */
6608   emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                              // mov   dstp,dstreg
6609   return dst;
6610}
6611
6612
6613/*-------------------------------------------------
6614    op_ftoi4t - process a FTOI4T opcode
6615-------------------------------------------------*/
6616
6617static x86code *op_ftoi4t(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6618{
6619   drcuml_parameter dstp, srcp;
6620   int dstreg;
6621
6622   /* validate instruction */
6623   assert(inst->size == 4 || inst->size == 8);
6624   assert(inst->condflags == DRCUML_COND_ALWAYS);
6625
6626   /* normalize parameters */
6627   param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF);
6628
6629   /* pick a target register for the general case */
6630   dstreg = param_select_register(REG_EAX, &dstp, NULL);
6631
6632   /* 32-bit form */
6633   if (inst->size == 4)
6634   {
6635      if (srcp.type == DRCUML_PTYPE_MEMORY)
6636         emit_cvttss2si_r32_m32(&dst, dstreg, MABS(drcbe, srcp.value));            // cvttss2si dstreg,[srcp]
6637      else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER)
6638         emit_cvttss2si_r32_r128(&dst, dstreg, srcp.value);                     // cvttss2si dstreg,srcp
6639   }
6640
6641   /* 64-bit form */
6642   else if (inst->size == 8)
6643   {
6644      if (srcp.type == DRCUML_PTYPE_MEMORY)
6645         emit_cvttsd2si_r32_m64(&dst, dstreg, MABS(drcbe, srcp.value));            // cvttsd2si dstreg,[srcp]
6646      else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER)
6647         emit_cvttsd2si_r32_r128(&dst, dstreg, srcp.value);                     // cvttsd2si dstreg,srcp
6648   }
6649
6650   /* general case */
6651   emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                              // mov   dstp,dstreg
6652   return dst;
6653}
6654
6655
6656/*-------------------------------------------------
6657    op_ftoi4r - process a FTOI4R opcode
6658-------------------------------------------------*/
6659
6660static x86code *op_ftoi4r(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6661{
6662   drcuml_parameter dstp, srcp;
6663   int dstreg;
6664
6665   /* validate instruction */
6666   assert(inst->size == 4 || inst->size == 8);
6667   assert(inst->condflags == DRCUML_COND_ALWAYS);
6668
6669   /* normalize parameters */
6670   param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF);
6671
6672   /* pick a target register for the general case */
6673   dstreg = param_select_register(REG_EAX, &dstp, NULL);
6674
6675   /* save and set the control word */
6676   emit_stmxcsr_m32(&dst, MABS(drcbe, &drcbe->ssemodesave));                     // stmxcsr [ssemodesave]
6677   emit_ldmxcsr_m32(&dst, MABS(drcbe, &drcbe->ssecontrol[DRCUML_FMOD_ROUND]));         // ldmxcsr fpcontrol[DRCUML_FMOD_ROUND]
6678
6679   /* 32-bit form */
6680   if (inst->size == 4)
6681   {
6682      if (srcp.type == DRCUML_PTYPE_MEMORY)
6683         emit_cvtss2si_r32_m32(&dst, dstreg, MABS(drcbe, srcp.value));            // cvtss2si dstreg,[srcp]
6684      else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER)
6685         emit_cvtss2si_r32_r128(&dst, dstreg, srcp.value);                     // cvtss2si dstreg,srcp
6686   }
6687
6688   /* 64-bit form */
6689   else if (inst->size == 8)
6690   {
6691      if (srcp.type == DRCUML_PTYPE_MEMORY)
6692         emit_cvtsd2si_r32_m64(&dst, dstreg, MABS(drcbe, srcp.value));            // cvtsd2si dstreg,[srcp]
6693      else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER)
6694         emit_cvtsd2si_r32_r128(&dst, dstreg, srcp.value);                     // cvtsd2si dstreg,srcp
6695   }
6696
6697   /* restore control word and proceed */
6698   emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                              // mov   dstp,dstreg
6699   emit_ldmxcsr_m32(&dst, MABS(drcbe, &drcbe->ssemodesave));                     // ldmxcsr [ssemodesave]
6700   return dst;
6701}
6702
6703
6704/*-------------------------------------------------
6705    op_ftoi4f - process a FTOI4F opcode
6706-------------------------------------------------*/
6707
6708static x86code *op_ftoi4f(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6709{
6710   drcuml_parameter dstp, srcp;
6711   int dstreg;
6712
6713   /* validate instruction */
6714   assert(inst->size == 4 || inst->size == 8);
6715   assert(inst->condflags == DRCUML_COND_ALWAYS);
6716
6717   /* normalize parameters */
6718   param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF);
6719
6720   /* pick a target register for the general case */
6721   dstreg = param_select_register(REG_EAX, &dstp, NULL);
6722
6723   /* save and set the control word */
6724   emit_stmxcsr_m32(&dst, MABS(drcbe, &drcbe->ssemodesave));                     // stmxcsr [ssemodesave]
6725   emit_ldmxcsr_m32(&dst, MABS(drcbe, &drcbe->ssecontrol[DRCUML_FMOD_FLOOR]));         // ldmxcsr fpcontrol[DRCUML_FMOD_FLOOR]
6726
6727   /* 32-bit form */
6728   if (inst->size == 4)
6729   {
6730      if (srcp.type == DRCUML_PTYPE_MEMORY)
6731         emit_cvtss2si_r32_m32(&dst, dstreg, MABS(drcbe, srcp.value));            // cvtss2si dstreg,[srcp]
6732      else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER)
6733         emit_cvtss2si_r32_r128(&dst, dstreg, srcp.value);                     // cvtss2si dstreg,srcp
6734   }
6735
6736   /* 64-bit form */
6737   else if (inst->size == 8)
6738   {
6739      if (srcp.type == DRCUML_PTYPE_MEMORY)
6740         emit_cvtsd2si_r32_m64(&dst, dstreg, MABS(drcbe, srcp.value));            // cvtsd2si dstreg,[srcp]
6741      else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER)
6742         emit_cvtsd2si_r32_r128(&dst, dstreg, srcp.value);                     // cvtsd2si dstreg,srcp
6743   }
6744
6745   /* restore control word and proceed */
6746   emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                              // mov   dstp,dstreg
6747   emit_ldmxcsr_m32(&dst, MABS(drcbe, &drcbe->ssemodesave));                     // ldmxcsr [ssemodesave]
6748   return dst;
6749}
6750
6751
6752/*-------------------------------------------------
6753    op_ftoi4c - process a FTOI4C opcode
6754-------------------------------------------------*/
6755
6756static x86code *op_ftoi4c(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6757{
6758   drcuml_parameter dstp, srcp;
6759   int dstreg;
6760
6761   /* validate instruction */
6762   assert(inst->size == 4 || inst->size == 8);
6763   assert(inst->condflags == DRCUML_COND_ALWAYS);
6764
6765   /* normalize parameters */
6766   param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF);
6767
6768   /* pick a target register for the general case */
6769   dstreg = param_select_register(REG_EAX, &dstp, NULL);
6770
6771   /* save and set the control word */
6772   emit_stmxcsr_m32(&dst, MABS(drcbe, &drcbe->ssemodesave));                     // stmxcsr [ssemodesave]
6773   emit_ldmxcsr_m32(&dst, MABS(drcbe, &drcbe->ssecontrol[DRCUML_FMOD_CEIL]));         // ldmxcsr fpcontrol[DRCUML_FMOD_CEIL]
6774
6775   /* 32-bit form */
6776   if (inst->size == 4)
6777   {
6778      if (srcp.type == DRCUML_PTYPE_MEMORY)
6779         emit_cvtss2si_r32_m32(&dst, dstreg, MABS(drcbe, srcp.value));            // cvtss2si dstreg,[srcp]
6780      else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER)
6781         emit_cvtss2si_r32_r128(&dst, dstreg, srcp.value);                     // cvtss2si dstreg,srcp
6782   }
6783
6784   /* 64-bit form */
6785   else if (inst->size == 8)
6786   {
6787      if (srcp.type == DRCUML_PTYPE_MEMORY)
6788         emit_cvtsd2si_r32_m64(&dst, dstreg, MABS(drcbe, srcp.value));            // cvtsd2si dstreg,[srcp]
6789      else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER)
6790         emit_cvtsd2si_r32_r128(&dst, dstreg, srcp.value);                     // cvtsd2si dstreg,srcp
6791   }
6792
6793   /* restore control word and proceed */
6794   emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                              // mov   dstp,dstreg
6795   emit_ldmxcsr_m32(&dst, MABS(drcbe, &drcbe->ssemodesave));                     // ldmxcsr [ssemodesave]
6796   return dst;
6797}
6798
6799
6800/*-------------------------------------------------
6801    op_ftoi8 - process a FTOI8 opcode
6802-------------------------------------------------*/
6803
6804static x86code *op_ftoi8(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6805{
6806   drcuml_parameter dstp, srcp;
6807   int dstreg;
6808
6809   /* validate instruction */
6810   assert(inst->size == 4 || inst->size == 8);
6811   assert(inst->condflags == DRCUML_COND_ALWAYS);
6812
6813   /* normalize parameters */
6814   param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF);
6815
6816   /* pick a target register for the general case */
6817   dstreg = param_select_register(REG_EAX, &dstp, NULL);
6818
6819   /* 32-bit form */
6820   if (inst->size == 4)
6821   {
6822      if (srcp.type == DRCUML_PTYPE_MEMORY)
6823         emit_cvtss2si_r64_m32(&dst, dstreg, MABS(drcbe, srcp.value));            // cvtss2si dstreg,[srcp]
6824      else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER)
6825         emit_cvtss2si_r64_r128(&dst, dstreg, srcp.value);                     // cvtss2si dstreg,srcp
6826   }
6827
6828   /* 64-bit form */
6829   else if (inst->size == 8)
6830   {
6831      if (srcp.type == DRCUML_PTYPE_MEMORY)
6832         emit_cvtsd2si_r64_m64(&dst, dstreg, MABS(drcbe, srcp.value));            // cvtsd2si dstreg,[srcp]
6833      else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER)
6834         emit_cvtsd2si_r64_r128(&dst, dstreg, srcp.value);                     // cvtsd2si dstreg,srcp
6835   }
6836
6837   /* general case */
6838   emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                              // mov   dstp,dstreg
6839   return dst;
6840}
6841
6842
6843/*-------------------------------------------------
6844    op_ftoi8t - process a FTOI8T opcode
6845-------------------------------------------------*/
6846
6847static x86code *op_ftoi8t(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6848{
6849   drcuml_parameter dstp, srcp;
6850   int dstreg;
6851
6852   /* validate instruction */
6853   assert(inst->size == 4 || inst->size == 8);
6854   assert(inst->condflags == DRCUML_COND_ALWAYS);
6855
6856   /* normalize parameters */
6857   param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF);
6858
6859   /* pick a target register for the general case */
6860   dstreg = param_select_register(REG_EAX, &dstp, NULL);
6861
6862   /* 32-bit form */
6863   if (inst->size == 4)
6864   {
6865      if (srcp.type == DRCUML_PTYPE_MEMORY)
6866         emit_cvttss2si_r64_m32(&dst, dstreg, MABS(drcbe, srcp.value));            // cvttss2si dstreg,[srcp]
6867      else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER)
6868         emit_cvttss2si_r64_r128(&dst, dstreg, srcp.value);                     // cvttss2si dstreg,srcp
6869   }
6870
6871   /* 64-bit form */
6872   else if (inst->size == 8)
6873   {
6874      if (srcp.type == DRCUML_PTYPE_MEMORY)
6875         emit_cvttsd2si_r64_m64(&dst, dstreg, MABS(drcbe, srcp.value));            // cvttsd2si dstreg,[srcp]
6876      else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER)
6877         emit_cvttsd2si_r64_r128(&dst, dstreg, srcp.value);                     // cvttsd2si dstreg,srcp
6878   }
6879
6880   /* general case */
6881   emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                              // mov   dstp,dstreg
6882   return dst;
6883}
6884
6885
6886/*-------------------------------------------------
6887    op_ftoi8r - process a FTOI8R opcode
6888-------------------------------------------------*/
6889
6890static x86code *op_ftoi8r(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6891{
6892   drcuml_parameter dstp, srcp;
6893   int dstreg;
6894
6895   /* validate instruction */
6896   assert(inst->size == 4 || inst->size == 8);
6897   assert(inst->condflags == DRCUML_COND_ALWAYS);
6898
6899   /* normalize parameters */
6900   param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF);
6901
6902   /* pick a target register for the general case */
6903   dstreg = param_select_register(REG_EAX, &dstp, NULL);
6904
6905   /* save and set the control word */
6906   emit_stmxcsr_m32(&dst, MABS(drcbe, &drcbe->ssemodesave));                     // stmxcsr [ssemodesave]
6907   emit_ldmxcsr_m32(&dst, MABS(drcbe, &drcbe->ssecontrol[DRCUML_FMOD_ROUND]));         // ldmxcsr fpcontrol[DRCUML_FMOD_ROUND]
6908
6909   /* 32-bit form */
6910   if (inst->size == 4)
6911   {
6912      if (srcp.type == DRCUML_PTYPE_MEMORY)
6913         emit_cvtss2si_r64_m32(&dst, dstreg, MABS(drcbe, srcp.value));            // cvtss2si dstreg,[srcp]
6914      else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER)
6915         emit_cvtss2si_r64_r128(&dst, dstreg, srcp.value);                     // cvtss2si dstreg,srcp
6916   }
6917
6918   /* 64-bit form */
6919   else if (inst->size == 8)
6920   {
6921      if (srcp.type == DRCUML_PTYPE_MEMORY)
6922         emit_cvtsd2si_r64_m64(&dst, dstreg, MABS(drcbe, srcp.value));            // cvtsd2si dstreg,[srcp]
6923      else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER)
6924         emit_cvtsd2si_r64_r128(&dst, dstreg, srcp.value);                     // cvtsd2si dstreg,srcp
6925   }
6926
6927   /* restore control word and proceed */
6928   emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                              // mov   dstp,dstreg
6929   emit_ldmxcsr_m32(&dst, MABS(drcbe, &drcbe->ssemodesave));                     // ldmxcsr [ssemodesave]
6930   return dst;
6931}
6932
6933
6934/*-------------------------------------------------
6935    op_ftoi8f - process a FTOI8F opcode
6936-------------------------------------------------*/
6937
6938static x86code *op_ftoi8f(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6939{
6940   drcuml_parameter dstp, srcp;
6941   int dstreg;
6942
6943   /* validate instruction */
6944   assert(inst->size == 4 || inst->size == 8);
6945   assert(inst->condflags == DRCUML_COND_ALWAYS);
6946
6947   /* normalize parameters */
6948   param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF);
6949
6950   /* pick a target register for the general case */
6951   dstreg = param_select_register(REG_EAX, &dstp, NULL);
6952
6953   /* save and set the control word */
6954   emit_stmxcsr_m32(&dst, MABS(drcbe, &drcbe->ssemodesave));                     // stmxcsr [ssemodesave]
6955   emit_ldmxcsr_m32(&dst, MABS(drcbe, &drcbe->ssecontrol[DRCUML_FMOD_FLOOR]));         // ldmxcsr fpcontrol[DRCUML_FMOD_FLOOR]
6956
6957   /* 32-bit form */
6958   if (inst->size == 4)
6959   {
6960      if (srcp.type == DRCUML_PTYPE_MEMORY)
6961         emit_cvtss2si_r64_m32(&dst, dstreg, MABS(drcbe, srcp.value));            // cvtss2si dstreg,[srcp]
6962      else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER)
6963         emit_cvtss2si_r64_r128(&dst, dstreg, srcp.value);                     // cvtss2si dstreg,srcp
6964   }
6965
6966   /* 64-bit form */
6967   else if (inst->size == 8)
6968   {
6969      if (srcp.type == DRCUML_PTYPE_MEMORY)
6970         emit_cvtsd2si_r64_m64(&dst, dstreg, MABS(drcbe, srcp.value));            // cvtsd2si dstreg,[srcp]
6971      else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER)
6972         emit_cvtsd2si_r64_r128(&dst, dstreg, srcp.value);                     // cvtsd2si dstreg,srcp
6973   }
6974
6975   /* restore control word and proceed */
6976   emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                              // mov   dstp,dstreg
6977   emit_ldmxcsr_m32(&dst, MABS(drcbe, &drcbe->ssemodesave));                     // ldmxcsr [ssemodesave]
6978   return dst;
6979}
6980
6981
6982/*-------------------------------------------------
6983    op_ftoi8c - process a FTOI8C opcode
6984-------------------------------------------------*/
6985
6986static x86code *op_ftoi8c(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6987{
6988   drcuml_parameter dstp, srcp;
6989   int dstreg;
6990
6991   /* validate instruction */
6992   assert(inst->size == 4 || inst->size == 8);
6993   assert(inst->condflags == DRCUML_COND_ALWAYS);
6994
6995   /* normalize parameters */
6996   param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF);
6997
6998   /* pick a target register for the general case */
6999   dstreg = param_select_register(REG_EAX, &dstp, NULL);
7000
7001   /* save and set the control word */
7002   emit_stmxcsr_m32(&dst, MABS(drcbe, &drcbe->ssemodesave));                     // stmxcsr [ssemodesave]
7003   emit_ldmxcsr_m32(&dst, MABS(drcbe, &drcbe->ssecontrol[DRCUML_FMOD_CEIL]));         // ldmxcsr fpcontrol[DRCUML_FMOD_CEIL]
7004
7005   /* 32-bit form */
7006   if (inst->size == 4)
7007   {
7008      if (srcp.type == DRCUML_PTYPE_MEMORY)
7009         emit_cvtss2si_r64_m32(&dst, dstreg, MABS(drcbe, srcp.value));            // cvtss2si dstreg,[srcp]
7010      else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER)
7011         emit_cvtss2si_r64_r128(&dst, dstreg, srcp.value);                     // cvtss2si dstreg,srcp
7012   }
7013
7014   /* 64-bit form */
7015   else if (inst->size == 8)
7016   {
7017      if (srcp.type == DRCUML_PTYPE_MEMORY)
7018         emit_cvtsd2si_r64_m64(&dst, dstreg, MABS(drcbe, srcp.value));            // cvtsd2si dstreg,[srcp]
7019      else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER)
7020         emit_cvtsd2si_r64_r128(&dst, dstreg, srcp.value);                     // cvtsd2si dstreg,srcp
7021   }
7022
7023   /* restore control word and proceed */
7024   emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg);                              // mov   dstp,dstreg
7025   emit_ldmxcsr_m32(&dst, MABS(drcbe, &drcbe->ssemodesave));                     // ldmxcsr [ssemodesave]
7026   return dst;
7027}
7028
7029
7030/*-------------------------------------------------
7031    op_ffrfs - process a FFRFS opcode
7032-------------------------------------------------*/
7033
7034static x86code *op_ffrfs(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
7035{
7036   drcuml_parameter dstp, srcp;
7037   int dstreg;
7038
7039   /* validate instruction */
7040   assert(inst->size == 8);
7041   assert(inst->condflags == DRCUML_COND_ALWAYS);
7042
7043   /* normalize parameters */
7044   param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MF);
7045
7046   /* pick a target register for the general case */
7047   dstreg = param_select_register(REG_XMM0, &dstp, NULL);
7048
7049   /* general case */
7050   if (srcp.type == DRCUML_PTYPE_MEMORY)
7051      emit_cvtss2sd_r128_m32(&dst, dstreg, MABS(drcbe, srcp.value));               // cvtss2sd dstreg,[srcp]
7052   else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER)
7053      emit_cvtss2sd_r128_r128(&dst, dstreg, srcp.value);                        // cvtss2sd dstreg,srcp
7054   emit_movsd_p64_r128(drcbe, &dst, &dstp, dstreg);                           // movsd    dstp,dstreg
7055
7056   return dst;
7057}
7058
7059
7060/*-------------------------------------------------
7061    op_ffrfd - process a FFRFD opcode
7062-------------------------------------------------*/
7063
7064static x86code *op_ffrfd(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
7065{
7066   drcuml_parameter dstp, srcp;
7067   int dstreg;
7068
7069   /* validate instruction */
7070   assert(inst->size == 4);
7071   assert(inst->condflags == DRCUML_COND_ALWAYS);
7072
7073   /* normalize parameters */
7074   param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MF);
7075
7076   /* pick a target register for the general case */
7077   dstreg = param_select_register(REG_XMM0, &dstp, NULL);
7078
7079   /* general case */
7080   if (srcp.type == DRCUML_PTYPE_MEMORY)
7081      emit_cvtsd2ss_r128_m64(&dst, dstreg, MABS(drcbe, srcp.value));               // cvtsd2ss dstreg,[srcp]
7082   else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER)
7083      emit_cvtsd2ss_r128_r128(&dst, dstreg, srcp.value);                        // cvtsd2ss dstreg,srcp
7084   emit_movss_p32_r128(drcbe, &dst, &dstp, dstreg);                           // movss    dstp,dstreg
7085
7086   return dst;
7087}
7088
7089
7090/*-------------------------------------------------
7091    op_ffri4 - process a FFRI4 opcode
7092-------------------------------------------------*/
7093
7094static x86code *op_ffri4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
7095{
7096   drcuml_parameter dstp, srcp;
7097   int dstreg;
7098
7099   /* validate instruction */
7100   assert(inst->size == 4 || inst->size == 8);
7101   assert(inst->condflags == DRCUML_COND_ALWAYS);
7102
7103   /* normalize parameters */
7104   param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MF);
7105
7106   /* pick a target register for the general case */
7107   dstreg = param_select_register(REG_XMM0, &dstp, NULL);
7108
7109   /* 32-bit form */
7110   if (inst->size == 4)
7111   {
7112      if (srcp.type == DRCUML_PTYPE_MEMORY)
7113         emit_cvtsi2ss_r128_m32(&dst, dstreg, MABS(drcbe, srcp.value));            // cvtsi2ss dstreg,[srcp]
7114      else
7115      {
7116         int srcreg = param_select_register(REG_EAX, &srcp, NULL);
7117         emit_mov_r32_p32(drcbe, &dst, srcreg, &srcp);                        // mov      srcreg,srcp
7118         emit_cvtsi2ss_r128_r32(&dst, dstreg, srcreg);                        // cvtsi2ss dstreg,srcreg
7119      }
7120      emit_movss_p32_r128(drcbe, &dst, &dstp, dstreg);                        // movss    dstp,dstreg
7121   }
7122
7123   /* 64-bit form */
7124   else if (inst->size == 8)
7125   {
7126      if (srcp.type == DRCUML_PTYPE_MEMORY)
7127         emit_cvtsi2sd_r128_m32(&dst, dstreg, MABS(drcbe, srcp.value));            // cvtsi2sd dstreg,[srcp]
7128      else
7129      {
7130         int srcreg = param_select_register(REG_EAX, &srcp, NULL);
7131         emit_mov_r32_p32(drcbe, &dst, srcreg, &srcp);                        // mov      srcreg,srcp
7132         emit_cvtsi2sd_r128_r32(&dst, dstreg, srcreg);                        // cvtsi2sd dstreg,srcreg
7133      }
7134      emit_movsd_p64_r128(drcbe, &dst, &dstp, dstreg);                        // movsd    dstp,dstreg
7135   }
7136   return dst;
7137}
7138
7139
7140/*-------------------------------------------------
7141    op_ffri8 - process a FFRI8 opcode
7142-------------------------------------------------*/
7143
7144static x86code *op_ffri8(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
7145{
7146   drcuml_parameter dstp, srcp;
7147   int dstreg;
7148
7149   /* validate instruction */
7150   assert(inst->size == 4 || inst->size == 8);
7151   assert(inst->condflags == DRCUML_COND_ALWAYS);
7152
7153   /* normalize parameters */
7154   param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MF);
7155
7156   /* pick a target register for the general case */
7157   dstreg = param_select_register(REG_XMM0, &dstp, NULL);
7158
7159   /* 32-bit form */
7160   if (inst->size == 4)
7161   {
7162      if (srcp.type == DRCUML_PTYPE_MEMORY)
7163         emit_cvtsi2ss_r128_m64(&dst, dstreg, MABS(drcbe, srcp.value));            // cvtsi2ss dstreg,[srcp]
7164      else
7165      {
7166         int srcreg = param_select_register(REG_EAX, &srcp, NULL);
7167         emit_mov_r64_p64(drcbe, &dst, srcreg, &srcp);                        // mov      srcreg,srcp
7168         emit_cvtsi2ss_r128_r64(&dst, dstreg, srcreg);                        // cvtsi2ss dstreg,srcreg
7169      }
7170      emit_movss_p32_r128(drcbe, &dst, &dstp, dstreg);                        // movss    dstp,dstreg
7171   }
7172
7173   /* 64-bit form */
7174   else if (inst->size == 8)
7175   {
7176      if (srcp.type == DRCUML_PTYPE_MEMORY)
7177         emit_cvtsi2sd_r128_m64(&dst, dstreg, MABS(drcbe, srcp.value));            // cvtsi2sd dstreg,[srcp]
7178      else
7179      {
7180         int srcreg = param_select_register(REG_EAX, &srcp, NULL);
7181         emit_mov_r64_p64(drcbe, &dst, srcreg, &srcp);                        // mov      srcreg,srcp
7182         emit_cvtsi2sd_r128_r64(&dst, dstreg, srcreg);                        // cvtsi2sd dstreg,srcreg
7183      }
7184      emit_movsd_p64_r128(drcbe, &dst, &dstp, dstreg);                        // movsd    dstp,dstreg
7185   }
7186   return dst;
7187}
7188
7189
7190/*-------------------------------------------------
7191    op_fadd - process a FADD opcode
7192-------------------------------------------------*/
7193
7194static x86code *op_fadd(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
7195{
7196   drcuml_parameter dstp, src1p, src2p;
7197   int dstreg;
7198
7199   /* validate instruction */
7200   assert(inst->size == 4 || inst->size == 8);
7201   assert(inst->condflags == DRCUML_COND_ALWAYS);
7202
7203   /* normalize parameters */
7204   param_normalize_3_commutative(drcbe, inst, &dstp, PTYPE_MF, &src1p, PTYPE_MF, &src2p, PTYPE_MF);
7205
7206   /* pick a target register for the general case */
7207   dstreg = param_select_register(REG_XMM0, &dstp, &src2p);
7208
7209   /* 32-bit form */
7210   if (inst->size == 4)
7211   {
7212      emit_movss_r128_p32(drcbe, &dst, dstreg, &src1p);                        // movss dstreg,src1p
7213      if (src2p.type == DRCUML_PTYPE_MEMORY)
7214         emit_addss_r128_m32(&dst, dstreg, MABS(drcbe, src2p.value));            // addss dstreg,[src2p]
7215      else if (src2p.type == DRCUML_PTYPE_FLOAT_REGISTER)
7216         emit_addss_r128_r128(&dst, dstreg, src2p.value);                     // addss dstreg,src2p
7217      emit_movss_p32_r128(drcbe, &dst, &dstp, dstreg);                        // movss dstp,dstreg
7218   }
7219
7220   /* 64-bit form */
7221   else if (inst->size == 8)
7222   {
7223      emit_movsd_r128_p64(drcbe, &dst, dstreg, &src1p);                        // movsd dstreg,src1p
7224      if (src2p.type == DRCUML_PTYPE_MEMORY)
7225         emit_addsd_r128_m64(&dst, dstreg, MABS(drcbe, src2p.value));            // addsd dstreg,[src2p]
7226      else if (src2p.type == DRCUML_PTYPE_FLOAT_REGISTER)
7227         emit_addsd_r128_r128(&dst, dstreg, src2p.value);                     // addsd dstreg,src2p
7228      emit_movsd_p64_r128(drcbe, &dst, &dstp, dstreg);                        // movsd dstp,dstreg
7229   }
7230   return dst;
7231}
7232
7233
7234/*-------------------------------------------------
7235    op_fsub - process a FSUB opcode
7236-------------------------------------------------*/
7237
7238static x86code *op_fsub(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
7239{
7240   drcuml_parameter dstp, src1p, src2p;
7241   int dstreg;
7242
7243   /* validate instruction */
7244   assert(inst->size == 4 || inst->size == 8);
7245   assert(inst->condflags == DRCUML_COND_ALWAYS);
7246
7247   /* normalize parameters */
7248   param_normalize_3(drcbe, inst, &dstp, PTYPE_MF, &src1p, PTYPE_MF, &src2p, PTYPE_MF);
7249
7250   /* pick a target register for the general case */
7251   dstreg = param_select_register(REG_XMM0, &dstp, &src2p);
7252
7253   /* 32-bit form */
7254   if (inst->size == 4)
7255   {
7256      emit_movss_r128_p32(drcbe, &dst, dstreg, &src1p);                        // movss dstreg,src1p
7257      if (src2p.type == DRCUML_PTYPE_MEMORY)
7258         emit_subss_r128_m32(&dst, dstreg, MABS(drcbe, src2p.value));            // subss dstreg,[src2p]
7259      else if (src2p.type == DRCUML_PTYPE_FLOAT_REGISTER)
7260         emit_subss_r128_r128(&dst, dstreg, src2p.value);                     // subss dstreg,src2p
7261      emit_movss_p32_r128(drcbe, &dst, &dstp, dstreg);                        // movss dstp,dstreg
7262   }
7263
7264   /* 64-bit form */
7265   else if (inst->size == 8)
7266   {
7267      emit_movsd_r128_p64(drcbe, &dst, dstreg, &src1p);                        // movsd dstreg,src1p
7268      if (src2p.type == DRCUML_PTYPE_MEMORY)
7269         emit_subsd_r128_m64(&dst, dstreg, MABS(drcbe, src2p.value));            // subsd dstreg,[src2p]
7270      else if (src2p.type == DRCUML_PTYPE_FLOAT_REGISTER)
7271         emit_subsd_r128_r128(&dst, dstreg, src2p.value);                     // subsd dstreg,src2p
7272      emit_movsd_p64_r128(drcbe, &dst, &dstp, dstreg);                        // movsd dstp,dstreg
7273   }
7274   return dst;
7275}
7276
7277
7278/*-------------------------------------------------
7279    op_fcmp - process a FCMP opcode
7280-------------------------------------------------*/
7281
7282static x86code *op_fcmp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
7283{
7284   drcuml_parameter src1p, src2p;
7285   int src1reg;
7286
7287   /* validate instruction */
7288   assert(inst->size == 4 || inst->size == 8);
7289   assert((inst->condflags & ~(DRCUML_FLAG_C | DRCUML_FLAG_Z | DRCUML_FLAG_U)) == 0);
7290
7291   /* normalize parameters */
7292   param_normalize_2(drcbe, inst, &src1p, PTYPE_MF, &src2p, PTYPE_MF);
7293
7294   /* pick a target register for the general case */
7295   src1reg = param_select_register(REG_XMM0, &src1p, NULL);
7296
7297   /* 32-bit form */
7298   if (inst->size == 4)
7299   {
7300      emit_movss_r128_p32(drcbe, &dst, src1reg, &src1p);                        // movss src1reg,src1p
7301      if (src2p.type == DRCUML_PTYPE_MEMORY)
7302         emit_comiss_r128_m32(&dst, src1reg, MABS(drcbe, src2p.value));            // comiss src1reg,[src2p]
7303      else if (src2p.type == DRCUML_PTYPE_FLOAT_REGISTER)
7304         emit_comiss_r128_r128(&dst, src1reg, src2p.value);                     // comiss src1reg,src2p
7305   }
7306
7307   /* 64-bit form */
7308   else if (inst->size == 8)
7309   {
7310      emit_movsd_r128_p64(drcbe, &dst, src1reg, &src1p);                        // movsd src1reg,src1p
7311      if (src2p.type == DRCUML_PTYPE_MEMORY)
7312         emit_comisd_r128_m64(&dst, src1reg, MABS(drcbe, src2p.value));            // comisd src1reg,[src2p]
7313      else if (src2p.type == DRCUML_PTYPE_FLOAT_REGISTER)
7314         emit_comisd_r128_r128(&dst, src1reg, src2p.value);                     // comisd src1reg,src2p
7315   }
7316   return dst;
7317}
7318
7319
7320/*-------------------------------------------------
7321    op_fmul - process a FMUL opcode
7322-------------------------------------------------*/
7323
7324static x86code *op_fmul(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
7325{
7326   drcuml_parameter dstp, src1p, src2p;
7327   int dstreg;
7328
7329   /* validate instruction */
7330   assert(inst->size == 4 || inst->size == 8);
7331   assert(inst->condflags == DRCUML_COND_ALWAYS);
7332
7333   /* normalize parameters */
7334   param_normalize_3_commutative(drcbe, inst, &dstp, PTYPE_MF, &src1p, PTYPE_MF, &src2p, PTYPE_MF);
7335
7336   /* pick a target register for the general case */
7337   dstreg = param_select_register(REG_XMM0, &dstp, &src2p);
7338
7339   /* 32-bit form */
7340   if (inst->size == 4)
7341   {
7342      emit_movss_r128_p32(drcbe, &dst, dstreg, &src1p);                        // movss dstreg,src1p
7343      if (src2p.type == DRCUML_PTYPE_MEMORY)
7344         emit_mulss_r128_m32(&dst, dstreg, MABS(drcbe, src2p.value));            // mulss dstreg,[src2p]
7345      else if (src2p.type == DRCUML_PTYPE_FLOAT_REGISTER)
7346         emit_mulss_r128_r128(&dst, dstreg, src2p.value);                     // mulss dstreg,src2p
7347      emit_movss_p32_r128(drcbe, &dst, &dstp, dstreg);                        // movss dstp,dstreg
7348   }
7349
7350   /* 64-bit form */
7351   else if (inst->size == 8)
7352   {
7353      emit_movsd_r128_p64(drcbe, &dst, dstreg, &src1p);                        // movsd dstreg,src1p
7354      if (src2p.type == DRCUML_PTYPE_MEMORY)
7355         emit_mulsd_r128_m64(&dst, dstreg, MABS(drcbe, src2p.value));            // mulsd dstreg,[src2p]
7356      else if (src2p.type == DRCUML_PTYPE_FLOAT_REGISTER)
7357         emit_mulsd_r128_r128(&dst, dstreg, src2p.value);                     // mulsd dstreg,src2p
7358      emit_movsd_p64_r128(drcbe, &dst, &dstp, dstreg);                        // movsd dstp,dstreg
7359   }
7360   return dst;
7361}
7362
7363
7364/*-------------------------------------------------
7365    op_fdiv - process a FDIV opcode
7366-------------------------------------------------*/
7367
7368static x86code *op_fdiv(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
7369{
7370   drcuml_parameter dstp, src1p, src2p;
7371   int dstreg;
7372
7373   /* validate instruction */
7374   assert(inst->size == 4 || inst->size == 8);
7375   assert(inst->condflags == DRCUML_COND_ALWAYS);
7376
7377   /* normalize parameters */
7378   param_normalize_3(drcbe, inst, &dstp, PTYPE_MF, &src1p, PTYPE_MF, &src2p, PTYPE_MF);
7379
7380   /* pick a target register for the general case */
7381   dstreg = param_select_register(REG_XMM0, &dstp, &src2p);
7382
7383   /* 32-bit form */
7384   if (inst->size == 4)
7385   {
7386      emit_movss_r128_p32(drcbe, &dst, dstreg, &src1p);                        // movss dstreg,src1p
7387      if (src2p.type == DRCUML_PTYPE_MEMORY)
7388         emit_divss_r128_m32(&dst, dstreg, MABS(drcbe, src2p.value));            // divss dstreg,[src2p]
7389      else if (src2p.type == DRCUML_PTYPE_FLOAT_REGISTER)
7390         emit_divss_r128_r128(&dst, dstreg, src2p.value);                     // divss dstreg,src2p
7391      emit_movss_p32_r128(drcbe, &dst, &dstp, dstreg);                        // movss dstp,dstreg
7392   }
7393
7394   /* 64-bit form */
7395   else if (inst->size == 8)
7396   {
7397      emit_movsd_r128_p64(drcbe, &dst, dstreg, &src1p);                        // movsd dstreg,src1p
7398      if (src2p.type == DRCUML_PTYPE_MEMORY)
7399         emit_divsd_r128_m64(&dst, dstreg, MABS(drcbe, src2p.value));            // divsd dstreg,[src2p]
7400      else if (src2p.type == DRCUML_PTYPE_FLOAT_REGISTER)
7401         emit_divsd_r128_r128(&dst, dstreg, src2p.value);                     // divsd dstreg,src2p
7402      emit_movsd_p64_r128(drcbe, &dst, &dstp, dstreg);                        // movsd dstp,dstreg
7403   }
7404   return dst;
7405}
7406
7407
7408/*-------------------------------------------------
7409    op_fneg - process a FNEG opcode
7410-------------------------------------------------*/
7411
7412static x86code *op_fneg(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
7413{
7414   drcuml_parameter dstp, srcp;
7415   int dstreg;
7416
7417   /* validate instruction */
7418   assert(inst->size == 4 || inst->size == 8);
7419   assert(inst->condflags == DRCUML_COND_ALWAYS);
7420
7421   /* normalize parameters */
7422   param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MF);
7423
7424   /* pick a target register for the general case */
7425   dstreg = param_select_register(REG_XMM0, &dstp, &srcp);
7426
7427   /* 32-bit form */
7428   if (inst->size == 4)
7429   {
7430      emit_xorps_r128_r128(&dst, dstreg, dstreg);                              // xorps dstreg,dstreg
7431      if (srcp.type == DRCUML_PTYPE_MEMORY)
7432         emit_subss_r128_m32(&dst, dstreg, MABS(drcbe, srcp.value));               // subss dstreg,[srcp]
7433      else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER)
7434         emit_subss_r128_r128(&dst, dstreg, srcp.value);                        // subss dstreg,srcp
7435      emit_movss_p32_r128(drcbe, &dst, &dstp, dstreg);                        // movss dstp,dstreg
7436   }
7437
7438   /* 64-bit form */
7439   else if (inst->size == 8)
7440   {
7441      emit_xorpd_r128_r128(&dst, dstreg, dstreg);                              // xorpd dstreg,dstreg
7442      if (srcp.type == DRCUML_PTYPE_MEMORY)
7443         emit_subsd_r128_m64(&dst, dstreg, MABS(drcbe, srcp.value));               // subsd dstreg,[srcp]
7444      else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER)
7445         emit_subsd_r128_r128(&dst, dstreg, srcp.value);                        // subsd dstreg,srcp
7446      emit_movsd_p64_r128(drcbe, &dst, &dstp, dstreg);                        // movsd dstp,dstreg
7447   }
7448   return dst;
7449}
7450
7451
7452/*-------------------------------------------------
7453    op_fabs - process a FABS opcode
7454-------------------------------------------------*/
7455
7456static x86code *op_fabs(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
7457{
7458   drcuml_parameter dstp, srcp;
7459   int dstreg;
7460
7461   /* validate instruction */
7462   assert(inst->size == 4 || inst->size == 8);
7463   assert(inst->condflags == DRCUML_COND_ALWAYS);
7464
7465   /* normalize parameters */
7466   param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MF);
7467
7468   /* pick a target register for the general case */
7469   dstreg = param_select_register(REG_XMM0, &dstp, &srcp);
7470
7471   /* 32-bit form */
7472   if (inst->size == 4)
7473   {
7474      emit_movss_r128_p32(drcbe, &dst, dstreg, &srcp);                        // movss dstreg,srcp
7475      emit_andps_r128_m128(&dst, dstreg, MABS(drcbe, drcbe->absmask32));            // andps dstreg,[absmask32]
7476      emit_movss_p32_r128(drcbe, &dst, &dstp, dstreg);                        // movss dstp,dstreg
7477   }
7478
7479   /* 64-bit form */
7480   else if (inst->size == 8)
7481   {
7482      emit_movsd_r128_p64(drcbe, &dst, dstreg, &srcp);                        // movsd dstreg,srcp
7483      emit_andpd_r128_m128(&dst, dstreg, MABS(drcbe, drcbe->absmask64));            // andpd dstreg,[absmask64]
7484      emit_movsd_p64_r128(drcbe, &dst, &srcp, dstreg);                        // movsd dstp,dstreg
7485   }
7486   return dst;
7487}
7488
7489
7490/*-------------------------------------------------
7491    op_fsqrt - process a FSQRT opcode
7492-------------------------------------------------*/
7493
7494static x86code *op_fsqrt(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
7495{
7496   drcuml_parameter dstp, srcp;
7497   int dstreg;
7498
7499   /* validate instruction */
7500   assert(inst->size == 4 || inst->size == 8);
7501   assert(inst->condflags == DRCUML_COND_ALWAYS);
7502
7503   /* normalize parameters */
7504   param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MF);
7505
7506   /* pick a target register for the general case */
7507   dstreg = param_select_register(REG_XMM0, &dstp, NULL);
7508
7509   /* 32-bit form */
7510   if (inst->size == 4)
7511   {
7512      if (srcp.type == DRCUML_PTYPE_MEMORY)
7513         emit_sqrtss_r128_m32(&dst, dstreg, MABS(drcbe, srcp.value));            // sqrtss dstreg,[srcp]
7514      else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER)
7515         emit_sqrtss_r128_r128(&dst, dstreg, srcp.value);                     // sqrtss dstreg,srcp
7516      emit_movss_p32_r128(drcbe, &dst, &dstp, dstreg);                        // movss dstp,dstreg
7517   }
7518
7519   /* 64-bit form */
7520   else if (inst->size == 8)
7521   {
7522      if (srcp.type == DRCUML_PTYPE_MEMORY)
7523         emit_sqrtsd_r128_m64(&dst, dstreg, MABS(drcbe, srcp.value));            // sqrtsd dstreg,[srcp]
7524      else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER)
7525         emit_sqrtsd_r128_r128(&dst, dstreg, srcp.value);                     // sqrtsd dstreg,srcp
7526      emit_movsd_p64_r128(drcbe, &dst, &dstp, dstreg);                        // movsd dstp,dstreg
7527   }
7528   return dst;
7529}
7530
7531
7532/*-------------------------------------------------
7533    op_frecip - process a FRECIP opcode
7534-------------------------------------------------*/
7535
7536static x86code *op_frecip(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
7537{
7538   drcuml_parameter dstp, srcp;
7539   int dstreg;
7540
7541   /* validate instruction */
7542   assert(inst->size == 4 || inst->size == 8);
7543   assert(inst->condflags == DRCUML_COND_ALWAYS);
7544
7545   /* normalize parameters */
7546   param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MF);
7547
7548   /* pick a target register for the general case */
7549   dstreg = param_select_register(REG_XMM0, &dstp, NULL);
7550
7551   /* 32-bit form */
7552   if (inst->size == 4)
7553   {
7554      if (srcp.type == DRCUML_PTYPE_MEMORY)
7555         emit_rcpss_r128_m32(&dst, dstreg, MABS(drcbe, srcp.value));               // rcpss dstreg,[srcp]
7556      else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER)
7557         emit_rcpss_r128_r128(&dst, dstreg, srcp.value);                        // rcpss dstreg,srcp
7558      emit_movss_p32_r128(drcbe, &dst, &dstp, dstreg);                        // movss dstp,dstreg
7559   }
7560
7561   /* 64-bit form */
7562   else if (inst->size == 8)
7563   {
7564      if (srcp.type == DRCUML_PTYPE_MEMORY)
7565         emit_cvtsd2ss_r128_m64(&dst, dstreg, MABS(drcbe, srcp.value));            // cvtsd2ss dstreg,[srcp]
7566      else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER)
7567         emit_cvtsd2ss_r128_r128(&dst, dstreg, srcp.value);                     // cvtsd2ss dstreg,srcp
7568      emit_rcpss_r128_r128(&dst, dstreg, dstreg);                              // rcpss dstreg,dstreg
7569      emit_cvtss2sd_r128_r128(&dst, dstreg, dstreg);                           // cvtss2sd dstreg,dstreg
7570      emit_movsd_p64_r128(drcbe, &dst, &dstp, dstreg);                        // movsd dstp,dstreg
7571   }
7572   return dst;
7573}
7574
7575
7576/*-------------------------------------------------
7577    op_frsqrt - process a FRSQRT opcode
7578-------------------------------------------------*/
7579
7580static x86code *op_frsqrt(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
7581{
7582   drcuml_parameter dstp, srcp;
7583   int dstreg;
7584
7585   /* validate instruction */
7586   assert(inst->size == 4 || inst->size == 8);
7587   assert(inst->condflags == DRCUML_COND_ALWAYS);
7588
7589   /* normalize parameters */
7590   param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MF);
7591
7592   /* pick a target register for the general case */
7593   dstreg = param_select_register(REG_XMM0, &dstp, NULL);
7594
7595   /* 32-bit form */
7596   if (inst->size == 4)
7597   {
7598      if (srcp.type == DRCUML_PTYPE_MEMORY)
7599         emit_rsqrtss_r128_m32(&dst, dstreg, MABS(drcbe, srcp.value));            // rsqrtss dstreg,[srcp]
7600      else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER)
7601         emit_rsqrtss_r128_r128(&dst, dstreg, srcp.value);                     // rsqrtss dstreg,srcp
7602      emit_movss_p32_r128(drcbe, &dst, &dstp, dstreg);                        // movss dstp,dstreg
7603   }
7604
7605   /* 64-bit form */
7606   else if (inst->size == 8)
7607   {
7608      if (srcp.type == DRCUML_PTYPE_MEMORY)
7609         emit_cvtsd2ss_r128_m64(&dst, dstreg, MABS(drcbe, srcp.value));            // cvtsd2ss dstreg,[srcp]
7610      else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER)
7611         emit_cvtsd2ss_r128_r128(&dst, dstreg, srcp.value);                     // cvtsd2ss dstreg,srcp
7612      emit_rsqrtss_r128_r128(&dst, dstreg, dstreg);                           // rsqrtss dstreg,dstreg
7613      emit_cvtss2sd_r128_r128(&dst, dstreg, dstreg);                           // cvtss2sd dstreg,dstreg
7614      emit_movsd_p64_r128(drcbe, &dst, &dstp, dstreg);                        // movsd dstp,dstreg
7615   }
7616   return dst;
7617}
src/emu/cpu/drcbec.c
r0r2628
1/***************************************************************************
2
3    drcbec.c
4
5    Interpreted C core back-end for the universal machine language.
6
7    Copyright Aaron Giles
8    Released for general non-commercial use under the MAME license
9    Visit http://mamedev.org for licensing and usage restrictions.
10
11***************************************************************************/
12
13#include "drcuml.h"
14#include "drcbeut.h"
15#include "eminline.h"
16#include "debugger.h"
17#include <math.h>
18
19#ifdef _MSC_VER
20#include <float.h>
21#define isnan _isnan
22#endif
23
24
25/***************************************************************************
26    CONSTANTS
27***************************************************************************/
28
29/* define a bit to match each possible condition, starting at bit 12 */
30#define ZBIT         (0x1000 << (DRCUML_COND_Z & 15))
31#define NZBIT         (0x1000 << (DRCUML_COND_NZ & 15))
32#define SBIT         (0x1000 << (DRCUML_COND_S & 15))
33#define NSBIT         (0x1000 << (DRCUML_COND_NS & 15))
34#define CBIT         (0x1000 << (DRCUML_COND_C & 15))
35#define NCBIT         (0x1000 << (DRCUML_COND_NC & 15))
36#define VBIT         (0x1000 << (DRCUML_COND_V & 15))
37#define NVBIT         (0x1000 << (DRCUML_COND_NV & 15))
38#define UBIT         (0x1000 << (DRCUML_COND_U & 15))
39#define NUBIT         (0x1000 << (DRCUML_COND_NU & 15))
40#define ABIT         (0x1000 << (DRCUML_COND_A & 15))
41#define AEBIT         (0x1000 << (DRCUML_COND_AE & 15))
42#define BBIT         (0x1000 << (DRCUML_COND_B & 15))
43#define BEBIT         (0x1000 << (DRCUML_COND_BE & 15))
44#define GBIT         (0x1000 << (DRCUML_COND_G & 15))
45#define GEBIT         (0x1000 << (DRCUML_COND_GE & 15))
46#define LBIT         (0x1000 << (DRCUML_COND_L & 15))
47#define LEBIT         (0x1000 << (DRCUML_COND_LE & 15))
48
49
50
51/***************************************************************************
52    MACROS
53***************************************************************************/
54
55/* opcode format:
56
57    bits 31..28 == number of words following the opcode itself (0-15)
58    bits 27..12 == bitmask specify which condition code we care about
59    bits 11.. 2 == opcode
60    bit       1 == flags/condition summary (0 if no condition/flags, 1 otherwise)
61    bit       0 == operation size (0=32-bit, 1=64-bit)
62*/
63
64/* build a short opcode from the raw opcode and size */
65#define MAKE_OPCODE_SHORT(op, size, condflags) \
66   ((((size) == 8) << 0) | (((condflags) != 0) << 1) | ((op) << 2))
67
68/* build a full opcode from the raw opcode, size, condition/flags, and immediate count */
69#define MAKE_OPCODE_FULL(op, size, condflags, pwords) \
70   (MAKE_OPCODE_SHORT(op, size, condflags) | ((condflags & 0x80) ? (0x1000 << ((condflags) & 15)) : 0) | ((pwords) << 28))
71
72/* extract various parts of the opcode */
73#define OPCODE_GET_SHORT(op)      ((op) & 0xfff)
74#define OPCODE_PASS_CONDITION(op,f)   (((op) & condition_map[f]) != 0)
75#define OPCODE_FAIL_CONDITION(op,f)   (((op) & condition_map[f]) == 0)
76#define OPCODE_GET_PWORDS(op)      ((op) >> 28)
77
78/* shorthand for accessing parameters in the instruction stream */
79#define PARAM0                  (*inst[0].puint32)
80#define PARAM1                  (*inst[1].puint32)
81#define PARAM2                  (*inst[2].puint32)
82#define PARAM3                  (*inst[3].puint32)
83
84#define DPARAM0                  (*inst[0].puint64)
85#define DPARAM1                  (*inst[1].puint64)
86#define DPARAM2                  (*inst[2].puint64)
87#define DPARAM3                  (*inst[3].puint64)
88
89#define FSPARAM0               (*inst[0].pfloat)
90#define FSPARAM1               (*inst[1].pfloat)
91#define FSPARAM2               (*inst[2].pfloat)
92#define FSPARAM3               (*inst[3].pfloat)
93
94#define FDPARAM0               (*inst[0].pdouble)
95#define FDPARAM1               (*inst[1].pdouble)
96#define FDPARAM2               (*inst[2].pdouble)
97#define FDPARAM3               (*inst[3].pdouble)
98
99/* compute C and V flags for 32-bit add/subtract */
100#define FLAGS32_C_ADD(a,b)         ((UINT32)~(a) < (UINT32)(b))
101#define FLAGS32_C_SUB(a,b)         ((UINT32)(b) > (UINT32)(a))
102#define FLAGS32_V_SUB(r,a,b)      (((((a) ^ (b)) & ((a) ^ (r))) >> 30) & DRCUML_FLAG_V)
103#define FLAGS32_V_ADD(r,a,b)      (((~((a) ^ (b)) & ((a) ^ (r))) >> 30) & DRCUML_FLAG_V)
104
105/* compute N and Z flags for 32-bit operations */
106#define FLAGS32_NZ(v)            ((((v) >> 28) & DRCUML_FLAG_S) | (((UINT32)(v) == 0) << 2))
107#define FLAGS32_NZCV_ADD(r,a,b)      (FLAGS32_NZ(r) | FLAGS32_C_ADD(a,b) | FLAGS32_V_ADD(r,a,b))
108#define FLAGS32_NZCV_SUB(r,a,b)      (FLAGS32_NZ(r) | FLAGS32_C_SUB(a,b) | FLAGS32_V_SUB(r,a,b))
109
110/* compute C and V flags for 64-bit add/subtract */
111#define FLAGS64_C_ADD(a,b)         ((UINT64)~(a) < (UINT64)(b))
112#define FLAGS64_C_SUB(a,b)         ((UINT64)(b) > (UINT64)(a))
113#define FLAGS64_V_SUB(r,a,b)      (((((a) ^ (b)) & ((a) ^ (r))) >> 62) & DRCUML_FLAG_V)
114#define FLAGS64_V_ADD(r,a,b)      (((~((a) ^ (b)) & ((a) ^ (r))) >> 62) & DRCUML_FLAG_V)
115
116/* compute N and Z flags for 64-bit operations */
117#define FLAGS64_NZ(v)            ((((v) >> 60) & DRCUML_FLAG_S) | (((UINT64)(v) == 0) << 2))
118#define FLAGS64_NZCV_ADD(r,a,b)      (FLAGS64_NZ(r) | FLAGS64_C_ADD(a,b) | FLAGS64_V_ADD(r,a,b))
119#define FLAGS64_NZCV_SUB(r,a,b)      (FLAGS64_NZ(r) | FLAGS64_C_SUB(a,b) | FLAGS64_V_SUB(r,a,b))
120
121
122
123/***************************************************************************
124    TYPE DEFINITIONS
125***************************************************************************/
126
127/* internal backend-specific state */
128struct _drcbe_state
129{
130   drcuml_state *         drcuml;               /* pointer back to our owner */
131   drccache *            cache;               /* pointer to the cache */
132   drcuml_machine_state    state;               /* state of the machine */
133   drchash_state *         hash;               /* hash table state */
134   drcmap_state *         map;               /* code map */
135   drclabel_list *         labels;                 /* label list */
136};
137
138
139/* union to simplify accessing data via the instruction stream */
140typedef union _drcbec_instruction drcbec_instruction;
141union _drcbec_instruction
142{
143   UINT32            i;
144   void *            v;
145   char *            c;
146   UINT8 *            puint8;
147   INT8 *            pint8;
148   UINT16 *         puint16;
149   INT16 *            pint16;
150   UINT32 *         puint32;
151   INT32 *            pint32;
152   UINT64 *         puint64;
153   INT64 *            pint64;
154   float *            pfloat;
155   double *         pdouble;
156   void            (*cfunc)(void *);
157   const drcuml_codehandle *handle;
158   const drcbec_instruction *inst;
159   const drcbec_instruction **pinst;
160};
161
162
163
164/***************************************************************************
165    FUNCTION PROTOTYPES
166***************************************************************************/
167
168/* primary back-end callbacks */
169static drcbe_state *drcbec_alloc(drcuml_state *drcuml, drccache *cache, UINT32 flags, int modes, int addrbits, int ignorebits);
170static void drcbec_free(drcbe_state *drcbe);
171static void drcbec_reset(drcbe_state *drcbe);
172static int drcbec_execute(drcbe_state *state, drcuml_codehandle *entry);
173static void drcbec_generate(drcbe_state *drcbe, drcuml_block *block, const drcuml_instruction *instlist, UINT32 numinst);
174static int drcbec_hash_exists(drcbe_state *state, UINT32 mode, UINT32 pc);
175
176/* private helper functions */
177static void output_parameter(drcbe_state *drcbe, drcbec_instruction **dstptr, void **immedptr, int size, int immedsize, const drcuml_parameter *param);
178static void fixup_label(void *parameter, drccodeptr labelcodeptr);
179static int dmulu(UINT64 *dstlo, UINT64 *dsthi, UINT64 src1, UINT64 src2, int flags);
180static int dmuls(UINT64 *dstlo, UINT64 *dsthi, INT64 src1, INT64 src2, int flags);
181
182
183
184/***************************************************************************
185    TABLES
186***************************************************************************/
187
188static const UINT32 condition_map[] =
189{
190   /* ..... */      NCBIT | NVBIT | NZBIT | NSBIT | NUBIT | ABIT  | AEBIT | GBIT  | GEBIT,
191   /* ....C */      CBIT  | NVBIT | NZBIT | NSBIT | NUBIT | BEBIT | BBIT  | GBIT  | GEBIT,
192   /* ...V. */      NCBIT | VBIT  | NZBIT | NSBIT | NUBIT | ABIT  | AEBIT | LEBIT | LBIT,
193   /* ...VC */      CBIT  | VBIT  | NZBIT | NSBIT | NUBIT | BEBIT | BBIT  | LEBIT | LBIT,
194   /* ..Z.. */      NCBIT | NVBIT | ZBIT  | NSBIT | NUBIT | BEBIT | AEBIT | LEBIT | GEBIT,
195   /* ..Z.C */      CBIT  | NVBIT | ZBIT  | NSBIT | NUBIT | BEBIT | BBIT  | LEBIT | GEBIT,
196   /* ..ZV. */      NCBIT | VBIT  | ZBIT  | NSBIT | NUBIT | BEBIT | AEBIT | LEBIT | LBIT,
197   /* ..ZVC */      CBIT  | VBIT  | ZBIT  | NSBIT | NUBIT | BEBIT | BBIT  | LEBIT | LBIT,
198   /* .S... */      NCBIT | NVBIT | NZBIT | SBIT  | NUBIT | ABIT  | AEBIT | LEBIT | LBIT,
199   /* .S..C */      CBIT  | NVBIT | NZBIT | SBIT  | NUBIT | BEBIT | BBIT  | LEBIT | LBIT,
200   /* .S.V. */      NCBIT | VBIT  | NZBIT | SBIT  | NUBIT | ABIT  | AEBIT | GBIT  | GEBIT,
201   /* .S.VC */      CBIT  | VBIT  | NZBIT | SBIT  | NUBIT | BEBIT | BBIT  | GBIT  | GEBIT,
202   /* .SZ.. */      NCBIT | NVBIT | ZBIT  | SBIT  | NUBIT | BEBIT | AEBIT | LEBIT | LBIT,
203   /* .SZ.C */      CBIT  | NVBIT | ZBIT  | SBIT  | NUBIT | BEBIT | BBIT  | LEBIT | LBIT,
204   /* .SZV. */      NCBIT | VBIT  | ZBIT  | SBIT  | NUBIT | BEBIT | AEBIT | LEBIT | GEBIT,
205   /* .SZVC */      CBIT  | VBIT  | ZBIT  | SBIT  | NUBIT | BEBIT | BBIT  | LEBIT | GEBIT,
206   /* U.... */      NCBIT | NVBIT | NZBIT | NSBIT | UBIT  | ABIT  | AEBIT | GBIT  | GEBIT,
207   /* U...C */      CBIT  | NVBIT | NZBIT | NSBIT | UBIT  | BEBIT | BBIT  | GBIT  | GEBIT,
208   /* U..V. */      NCBIT | VBIT  | NZBIT | NSBIT | UBIT  | ABIT  | AEBIT | LEBIT | LBIT,
209   /* U..VC */      CBIT  | VBIT  | NZBIT | NSBIT | UBIT  | BEBIT | BBIT  | LEBIT | LBIT,
210   /* U.Z.. */      NCBIT | NVBIT | ZBIT  | NSBIT | UBIT  | BEBIT | AEBIT | LEBIT | GEBIT,
211   /* U.Z.C */      CBIT  | NVBIT | ZBIT  | NSBIT | UBIT  | BEBIT | BBIT  | LEBIT | GEBIT,
212   /* U.ZV. */      NCBIT | VBIT  | ZBIT  | NSBIT | UBIT  | BEBIT | AEBIT | LEBIT | LBIT,
213   /* U.ZVC */      CBIT  | VBIT  | ZBIT  | NSBIT | UBIT  | BEBIT | BBIT  | LEBIT | LBIT,
214   /* US... */      NCBIT | NVBIT | NZBIT | SBIT  | UBIT  | ABIT  | AEBIT | LEBIT | LBIT,
215   /* US..C */      CBIT  | NVBIT | NZBIT | SBIT  | UBIT  | BEBIT | BBIT  | LEBIT | LBIT,
216   /* US.V. */      NCBIT | VBIT  | NZBIT | SBIT  | UBIT  | ABIT  | AEBIT | GBIT  | GEBIT,
217   /* US.VC */      CBIT  | VBIT  | NZBIT | SBIT  | UBIT  | BEBIT | BBIT  | GBIT  | GEBIT,
218   /* USZ.. */      NCBIT | NVBIT | ZBIT  | SBIT  | UBIT  | BEBIT | AEBIT | LEBIT | LBIT,
219   /* USZ.C */      CBIT  | NVBIT | ZBIT  | SBIT  | UBIT  | BEBIT | BBIT  | LEBIT | LBIT,
220   /* USZV. */      NCBIT | VBIT  | ZBIT  | SBIT  | UBIT  | BEBIT | AEBIT | LEBIT | GEBIT,
221   /* USZVC */      CBIT  | VBIT  | ZBIT  | SBIT  | UBIT  | BEBIT | BBIT  | LEBIT | GEBIT
222};
223
224
225
226/***************************************************************************
227    GLOBAL VARIABLES
228***************************************************************************/
229
230static UINT64 immediate_zero = 0;
231
232const drcbe_interface drcbe_c_be_interface =
233{
234   drcbec_alloc,
235   drcbec_free,
236   drcbec_reset,
237   drcbec_execute,
238   drcbec_generate,
239   drcbec_hash_exists
240};
241
242
243
244/***************************************************************************
245    BACKEND CALLBACKS
246***************************************************************************/
247
248/*-------------------------------------------------
249    drcbec_alloc - allocate back-end-specific
250    state
251-------------------------------------------------*/
252
253static drcbe_state *drcbec_alloc(drcuml_state *drcuml, drccache *cache, UINT32 flags, int modes, int addrbits, int ignorebits)
254{
255   /* allocate space in the cache for our state */
256   drcbe_state *drcbe = drccache_memory_alloc(cache, sizeof(*drcbe));
257   if (drcbe == NULL)
258      return NULL;
259   memset(drcbe, 0, sizeof(*drcbe));
260
261   /* remember our pointers */
262   drcbe->drcuml = drcuml;
263   drcbe->cache = cache;
264
265   /* allocate hash tables */
266   drcbe->hash = drchash_alloc(cache, modes, addrbits, ignorebits);
267   if (drcbe->hash == NULL)
268      return NULL;
269
270   /* allocate code map */
271   drcbe->map = drcmap_alloc(cache, 0);
272   if (drcbe->map == NULL)
273      return NULL;
274
275   /* allocate a label tracker */
276   drcbe->labels = drclabel_list_alloc(cache);
277   if (drcbe->labels == NULL)
278      return NULL;
279
280   return drcbe;
281}
282
283
284/*-------------------------------------------------
285    drcbec_free - free back-end specific state
286-------------------------------------------------*/
287
288static void drcbec_free(drcbe_state *drcbe)
289{
290}
291
292
293/*-------------------------------------------------
294    drcbec_reset - reset back-end specific state
295-------------------------------------------------*/
296
297static void drcbec_reset(drcbe_state *drcbe)
298{
299   /* reset our hash tables */
300   drchash_reset(drcbe->hash);
301   drchash_set_default_codeptr(drcbe->hash, NULL);
302}
303
304
305/*-------------------------------------------------
306    drcbec_generate - generate code
307-------------------------------------------------*/
308
309static void drcbec_generate(drcbe_state *drcbe, drcuml_block *block, const drcuml_instruction *instlist, UINT32 numinst)
310{
311   drccodeptr *cachetop;
312   drcbec_instruction *base;
313   drcbec_instruction *dst;
314   int inum;
315
316   /* tell all of our utility objects that a block is beginning */
317   drchash_block_begin(drcbe->hash, block, instlist, numinst);
318   drclabel_block_begin(drcbe->labels, block);
319   drcmap_block_begin(drcbe->map, block);
320
321   /* begin codegen; fail if we can't */
322   cachetop = drccache_begin_codegen(drcbe->cache, numinst * sizeof(drcbec_instruction) * 4);
323   if (cachetop == NULL)
324      drcuml_block_abort(block);
325
326   /* compute the base by aligning the cache top to an even multiple of drcbec_instruction */
327   base = (drcbec_instruction *)(((FPTR)*cachetop + sizeof(drcbec_instruction) - 1) & ~(sizeof(drcbec_instruction) - 1));
328   dst = base;
329
330   /* generate code by copying the instructions and extracting immediates */
331   for (inum = 0; inum < numinst; inum++)
332   {
333      const drcuml_instruction *inst = &instlist[inum];
334      drcuml_instruction modified_inst;
335      int immedsize, immedbytes, immedwords, pnum;
336      void *immed;
337
338      /* handle most instructions generally, but a few special cases */
339      switch (inst->opcode)
340      {
341         /* when we hit a HANDLE opcode, register the current pointer for the handle */
342         case DRCUML_OP_HANDLE:
343            assert(inst->numparams == 1);
344            assert(inst->param[0].type == DRCUML_PTYPE_MEMORY);
345            drcuml_handle_set_codeptr((drcuml_codehandle *)(FPTR)inst->param[0].value, (drccodeptr)dst);
346            break;
347
348         /* when we hit a HASH opcode, register the current pointer for the mode/PC */
349         case DRCUML_OP_HASH:
350            /* we already verified the parameter count and types above */
351            drchash_set_codeptr(drcbe->hash, inst->param[0].value, inst->param[1].value, (drccodeptr)dst);
352            break;
353
354         /* when we hit a LABEL opcode, register the current pointer for the label */
355         case DRCUML_OP_LABEL:
356            assert(inst->numparams == 1);
357            assert(inst->param[0].type == DRCUML_PTYPE_IMMEDIATE);
358            drclabel_set_codeptr(drcbe->labels, inst->param[0].value, (drccodeptr)dst);
359            break;
360
361         /* ignore COMMENT opcodes */
362         case DRCUML_OP_COMMENT:
363            break;
364
365         /* when we hit a MAPVAR opcode, log the change for the current PC */
366         case DRCUML_OP_MAPVAR:
367            assert(inst->numparams == 2);
368            assert(inst->param[0].type == DRCUML_PTYPE_MAPVAR);
369            assert(inst->param[1].type == DRCUML_PTYPE_IMMEDIATE);
370            drcmap_set_value(drcbe->map, (drccodeptr)dst, inst->param[0].value, inst->param[1].value);
371            break;
372
373         /* JMP instructions need to resolve their labels */
374         case DRCUML_OP_JMP:
375            assert(inst->numparams == 1);
376            assert(inst->param[0].type == DRCUML_PTYPE_IMMEDIATE);
377            (dst++)->i = MAKE_OPCODE_FULL(inst->opcode, inst->size, inst->condflags, 1);
378            dst->inst = (drcbec_instruction *)drclabel_get_codeptr(drcbe->labels, inst->param[0].value, fixup_label, dst);
379            dst++;
380            break;
381
382         /* generically handle everything else */
383         default:
384
385            /* for RECOVER opcodes, we need to fixup the second parameter into an immediate */
386            if (inst->opcode == DRCUML_OP_RECOVER)
387            {
388               assert(inst->param[1].type == DRCUML_PTYPE_MAPVAR);
389               modified_inst = *inst;
390               modified_inst.param[1].type = DRCUML_PTYPE_IMMEDIATE;
391               inst = &modified_inst;
392            }
393
394            /* determine the maximum size of an immediate; mostly this is just the instruction size */
395            immedsize = inst->size;
396            if (inst->opcode == DRCUML_OP_FFRFD || inst->opcode == DRCUML_OP_FFRI8)
397               immedsize = 8;
398
399            /* count how many bytes of immediates we need */
400            immedbytes = 0;
401            for (pnum = 0; pnum < inst->numparams; pnum++)
402               if (inst->param[pnum].type == DRCUML_PTYPE_MAPVAR ||
403                  (inst->param[pnum].type == DRCUML_PTYPE_IMMEDIATE && inst->param[pnum].value != 0))
404                  immedbytes += immedsize;
405
406            /* compute how many instruction words we need for that */
407            immedwords = (immedbytes + sizeof(drcbec_instruction) - 1) / sizeof(drcbec_instruction);
408
409            /* first item is the opcode, size, condition flags and length */
410            (dst++)->i = MAKE_OPCODE_FULL(inst->opcode, inst->size, inst->condflags, inst->numparams + immedwords);
411
412            /* immediates start after parameters */
413            immed = dst + inst->numparams;
414
415            /* output each of the parameters */
416            for (pnum = 0; pnum < inst->numparams; pnum++)
417               output_parameter(drcbe, &dst, &immed, inst->size, immedsize, &inst->param[pnum]);
418
419            /* point past the end of the immediates */
420            dst += immedwords;
421            break;
422      }
423   }
424
425   /* complete codegen */
426   *cachetop = (drccodeptr)dst;
427   drccache_end_codegen(drcbe->cache);
428
429   /* tell all of our utility objects that the block is finished */
430   drchash_block_end(drcbe->hash, block);
431   drclabel_block_end(drcbe->labels, block);
432   drcmap_block_end(drcbe->map, block);
433}
434
435
436/*-------------------------------------------------
437    drcbec_hash_exists - return true if the
438    given mode/pc exists in the hash table
439-------------------------------------------------*/
440
441static int drcbec_hash_exists(drcbe_state *state, UINT32 mode, UINT32 pc)
442{
443   return drchash_code_exists(state->hash, mode, pc);
444}
445
446
447
448/***************************************************************************
449    BACK-END EXECUTION
450***************************************************************************/
451
452/*-------------------------------------------------
453    drcbec_execute - execute a block of code
454    registered at the given mode/pc
455-------------------------------------------------*/
456
457static int drcbec_execute(drcbe_state *drcbe, drcuml_codehandle *entry)
458{
459   const drcbec_instruction *callstack[32];
460   const drcbec_instruction *newinst;
461   const drcbec_instruction *inst;
462   UINT32 temp32;
463   UINT64 temp64;
464   int shift;
465   UINT8 flags = 0;
466   UINT8 sp = 0;
467
468   /* get the entry point */
469   inst = (const drcbec_instruction *)drcuml_handle_codeptr(entry);
470   assert_in_cache(drcbe->cache, inst);
471
472   /* loop while we have cycles */
473   while (TRUE)
474   {
475      UINT32 opcode = (inst++)->i;
476      switch (OPCODE_GET_SHORT(opcode))
477      {
478         /* ----------------------- Control Flow Operations ----------------------- */
479
480         case MAKE_OPCODE_SHORT(DRCUML_OP_HANDLE, 4, 0):      /* HANDLE  handle                 */
481         case MAKE_OPCODE_SHORT(DRCUML_OP_HASH, 4, 0):      /* HASH    mode,pc                */
482         case MAKE_OPCODE_SHORT(DRCUML_OP_LABEL, 4, 0):      /* LABEL   imm                    */
483         case MAKE_OPCODE_SHORT(DRCUML_OP_COMMENT, 4, 0):   /* COMMENT string                 */
484         case MAKE_OPCODE_SHORT(DRCUML_OP_MAPVAR, 4, 0):      /* MAPVAR  mapvar,value           */
485
486            /* these opcodes should be processed at compile-time only */
487            fatalerror("Unexpected opcode");
488            break;
489
490         case MAKE_OPCODE_SHORT(DRCUML_OP_DEBUG, 4, 0):      /* DEBUG   pc                     */
491            CALL_DEBUGGER(PARAM0);
492            break;
493
494         case MAKE_OPCODE_SHORT(DRCUML_OP_HASHJMP, 4, 0):   /* HASHJMP mode,pc,handle         */
495            sp = 0;
496            newinst = (const drcbec_instruction *)drchash_get_codeptr(drcbe->hash, PARAM0, PARAM1);
497            if (newinst == NULL)
498            {
499               assert(sp < ARRAY_LENGTH(callstack));
500               drcbe->state.exp.l = PARAM1;
501               newinst = (const drcbec_instruction *)drcuml_handle_codeptr(inst[2].handle);
502               callstack[sp++] = inst;
503            }
504            assert_in_cache(drcbe->cache, newinst);
505            inst = newinst;
506            continue;
507
508         case MAKE_OPCODE_SHORT(DRCUML_OP_EXIT, 4, 1):      /* EXIT    src1[,c]               */
509            if (OPCODE_FAIL_CONDITION(opcode, flags))
510               break;
511            /* fall through... */
512
513         case MAKE_OPCODE_SHORT(DRCUML_OP_EXIT, 4, 0):
514            return PARAM0;
515
516         case MAKE_OPCODE_SHORT(DRCUML_OP_JMP, 4, 1):      /* JMP     imm[,c]                */
517            if (OPCODE_FAIL_CONDITION(opcode, flags))
518               break;
519            /* fall through... */
520
521         case MAKE_OPCODE_SHORT(DRCUML_OP_JMP, 4, 0):
522            newinst = inst[0].inst;
523            assert_in_cache(drcbe->cache, newinst);
524            inst = newinst;
525            continue;
526
527         case MAKE_OPCODE_SHORT(DRCUML_OP_CALLH, 4, 1):      /* CALLH   handle[,c]             */
528            if (OPCODE_FAIL_CONDITION(opcode, flags))
529               break;
530            /* fall through... */
531
532         case MAKE_OPCODE_SHORT(DRCUML_OP_CALLH, 4, 0):
533            assert(sp < ARRAY_LENGTH(callstack));
534            newinst = (const drcbec_instruction *)drcuml_handle_codeptr(inst[0].handle);
535            assert_in_cache(drcbe->cache, newinst);
536            callstack[sp++] = inst + OPCODE_GET_PWORDS(opcode);
537            inst = newinst;
538            continue;
539
540         case MAKE_OPCODE_SHORT(DRCUML_OP_RET, 4, 1):      /* RET     [c]                    */
541            if (OPCODE_FAIL_CONDITION(opcode, flags))
542               break;
543            /* fall through... */
544
545         case MAKE_OPCODE_SHORT(DRCUML_OP_RET, 4, 0):
546            assert(sp > 0);
547            newinst = callstack[--sp];
548            assert_in_cache(drcbe->cache, newinst);
549            inst = newinst;
550            continue;
551
552         case MAKE_OPCODE_SHORT(DRCUML_OP_EXH, 4, 1):      /* EXH     handle,param[,c]       */
553            if (OPCODE_FAIL_CONDITION(opcode, flags))
554               break;
555            /* fall through... */
556
557         case MAKE_OPCODE_SHORT(DRCUML_OP_EXH, 4, 0):
558            assert(sp < ARRAY_LENGTH(callstack));
559            newinst = (const drcbec_instruction *)drcuml_handle_codeptr(inst[0].handle);
560            assert_in_cache(drcbe->cache, newinst);
561            drcbe->state.exp.l = PARAM1;
562            callstack[sp++] = inst;
563            inst = newinst;
564            continue;
565
566         case MAKE_OPCODE_SHORT(DRCUML_OP_CALLC, 4, 1):      /* CALLC   func,ptr[,c]           */
567            if (OPCODE_FAIL_CONDITION(opcode, flags))
568               break;
569            /* fall through... */
570
571         case MAKE_OPCODE_SHORT(DRCUML_OP_CALLC, 4, 0):
572            (*inst[0].cfunc)(inst[1].v);
573            break;
574
575         case MAKE_OPCODE_SHORT(DRCUML_OP_RECOVER, 4, 0):   /* RECOVER dst,mapvar             */
576            assert(sp > 0);
577            PARAM0 = drcmap_get_value(drcbe->map, (drccodeptr)callstack[0], PARAM1);
578            break;
579
580
581         /* ----------------------- Internal Register Operations ----------------------- */
582
583         case MAKE_OPCODE_SHORT(DRCUML_OP_SETFMOD, 4, 0):   /* SETFMOD src                    */
584            drcbe->state.fmod = PARAM0;
585            break;
586
587         case MAKE_OPCODE_SHORT(DRCUML_OP_GETFMOD, 4, 0):   /* GETFMOD dst                    */
588            PARAM0 = drcbe->state.fmod;
589            break;
590
591         case MAKE_OPCODE_SHORT(DRCUML_OP_GETEXP, 4, 0):      /* GETEXP  dst                    */
592            PARAM0 = drcbe->state.exp.l;
593            break;
594
595
596         /* ----------------------- 32-Bit Integer Operations ----------------------- */
597
598         case MAKE_OPCODE_SHORT(DRCUML_OP_LOAD1U, 4, 0):      /* LOAD1U  dst,base,index         */
599            PARAM0 = inst[1].puint8[PARAM2];
600            break;
601
602         case MAKE_OPCODE_SHORT(DRCUML_OP_LOAD1S, 4, 0):      /* LOAD1S  dst,base,index         */
603            PARAM0 = inst[1].pint8[PARAM2];
604            break;
605
606         case MAKE_OPCODE_SHORT(DRCUML_OP_LOAD2U, 4, 0):      /* LOAD2U  dst,base,index         */
607            PARAM0 = inst[1].puint16[PARAM2];
608            break;
609
610         case MAKE_OPCODE_SHORT(DRCUML_OP_LOAD2S, 4, 0):      /* LOAD2S  dst,base,index         */
611            PARAM0 = inst[1].pint16[PARAM2];
612            break;
613
614         case MAKE_OPCODE_SHORT(DRCUML_OP_LOAD4U, 4, 0):      /* LOAD4U  dst,base,index         */
615            PARAM0 = inst[1].puint32[PARAM2];
616            break;
617
618         case MAKE_OPCODE_SHORT(DRCUML_OP_STORE1, 4, 0):      /* STORE1  dst,base,index         */
619            inst[0].puint8[PARAM1] = PARAM2;
620            break;
621
622         case MAKE_OPCODE_SHORT(DRCUML_OP_STORE2, 4, 0):      /* STORE2  dst,base,index         */
623            inst[0].puint16[PARAM1] = PARAM2;
624            break;
625
626         case MAKE_OPCODE_SHORT(DRCUML_OP_STORE4, 4, 0):      /* STORE4  dst,base,index         */
627            inst[0].puint32[PARAM1] = PARAM2;
628            break;
629
630         case MAKE_OPCODE_SHORT(DRCUML_OP_READ1U, 4, 0):      /* READ1U  dst,space,src1         */
631            PARAM0 = (UINT8)(*active_address_space[PARAM1].accessors->read_byte)(PARAM2);
632            break;
633
634         case MAKE_OPCODE_SHORT(DRCUML_OP_READ1S, 4, 0):      /* READ1S  dst,space,src1         */
635            PARAM0 = (INT8)(*active_address_space[PARAM1].accessors->read_byte)(PARAM2);
636            break;
637
638         case MAKE_OPCODE_SHORT(DRCUML_OP_READ2U, 4, 0):      /* READ2U  dst,space,src1         */
639            PARAM0 = (UINT16)(*active_address_space[PARAM1].accessors->read_word)(PARAM2);
640            break;
641
642         case MAKE_OPCODE_SHORT(DRCUML_OP_READ2S, 4, 0):      /* READ2S  dst,space,src1         */
643            PARAM0 = (INT16)(*active_address_space[PARAM1].accessors->read_word)(PARAM2);
644            break;
645
646         case MAKE_OPCODE_SHORT(DRCUML_OP_READ2M, 4, 0):      /* READ2M  dst,space,src1,mask    */
647            PARAM0 = (UINT16)(*active_address_space[PARAM1].accessors->read_word_masked)(PARAM2, PARAM3);
648            break;
649
650         case MAKE_OPCODE_SHORT(DRCUML_OP_READ4U, 4, 0):      /* READ4U  dst,space,src1         */
651            PARAM0 = (UINT32)(*active_address_space[PARAM1].accessors->read_dword)(PARAM2);
652            break;
653
654         case MAKE_OPCODE_SHORT(DRCUML_OP_READ4M, 4, 0):      /* READ4M  dst,space,src1,mask    */
655            PARAM0 = (UINT32)(*active_address_space[PARAM1].accessors->read_dword_masked)(PARAM2, PARAM3);
656            break;
657
658         case MAKE_OPCODE_SHORT(DRCUML_OP_WRITE1, 4, 0):      /* WRITE1  space,dst,src1         */
659            (*active_address_space[PARAM0].accessors->write_byte)(PARAM1, PARAM2);
660            break;
661
662         case MAKE_OPCODE_SHORT(DRCUML_OP_WRITE2, 4, 0):      /* WRITE2  space,dst,src1         */
663            (*active_address_space[PARAM0].accessors->write_word)(PARAM1, PARAM2);
664            break;
665
666         case MAKE_OPCODE_SHORT(DRCUML_OP_WRIT2M, 4, 0):      /* WRIT2M  space,dst,mask,src1    */
667            (*active_address_space[PARAM0].accessors->write_word_masked)(PARAM1, PARAM2, PARAM3);
668            break;
669
670         case MAKE_OPCODE_SHORT(DRCUML_OP_WRITE4, 4, 0):      /* WRITE4  space,dst,src1         */
671            (*active_address_space[PARAM0].accessors->write_dword)(PARAM1, PARAM2);
672            break;
673
674         case MAKE_OPCODE_SHORT(DRCUML_OP_WRIT4M, 4, 0):      /* WRIT4M  space,dst,mask,src1    */
675            (*active_address_space[PARAM0].accessors->write_dword_masked)(PARAM1, PARAM2, PARAM3);
676            break;
677
678         case MAKE_OPCODE_SHORT(DRCUML_OP_FLAGS, 4, 0):      /* FLAGS   dst,mask,table         */
679            PARAM0 = (PARAM0 & ~PARAM1) | (inst[2].puint32[flags & 0x1f] & PARAM1);
680            break;
681
682         case MAKE_OPCODE_SHORT(DRCUML_OP_MOV, 4, 1):      /* MOV     dst,src[,c]            */
683            if (OPCODE_FAIL_CONDITION(opcode, flags))
684               break;
685            /* fall through... */
686
687         case MAKE_OPCODE_SHORT(DRCUML_OP_MOV, 4, 0):
688            PARAM0 = PARAM1;
689            break;
690
691         case MAKE_OPCODE_SHORT(DRCUML_OP_ZEXT1, 4, 0):      /* ZEXT1   dst,src                */
692            PARAM0 = (UINT8)PARAM1;
693            break;
694
695         case MAKE_OPCODE_SHORT(DRCUML_OP_ZEXT2, 4, 0):      /* ZEXT2   dst,src                */
696            PARAM0 = (UINT16)PARAM1;
697            break;
698
699         case MAKE_OPCODE_SHORT(DRCUML_OP_SEXT1, 4, 0):      /* SEXT1   dst,src                */
700            PARAM0 = (INT8)PARAM1;
701            break;
702
703         case MAKE_OPCODE_SHORT(DRCUML_OP_SEXT2, 4, 0):      /* SEXT2   dst,src                */
704            PARAM0 = (INT16)PARAM1;
705            break;
706
707         case MAKE_OPCODE_SHORT(DRCUML_OP_ADD, 4, 0):      /* ADD     dst,src1,src2[,f]      */
708            PARAM0 = PARAM1 + PARAM2;
709            break;
710
711         case MAKE_OPCODE_SHORT(DRCUML_OP_ADD, 4, 1):
712            temp32 = PARAM1 + PARAM2;
713            flags = FLAGS32_NZCV_ADD(temp32, PARAM1, PARAM2);
714            PARAM0 = temp32;
715            break;
716
717         case MAKE_OPCODE_SHORT(DRCUML_OP_ADDC, 4, 0):      /* ADDC    dst,src1,src2[,f]      */
718            PARAM0 = PARAM1 + PARAM2 + (flags & DRCUML_FLAG_C);
719            break;
720
721         case MAKE_OPCODE_SHORT(DRCUML_OP_ADDC, 4, 1):
722            temp32 = PARAM1 + PARAM2 + (flags & DRCUML_FLAG_C);
723            flags = FLAGS32_NZCV_ADD(temp32, PARAM1, PARAM2);
724            PARAM0 = temp32;
725            break;
726
727         case MAKE_OPCODE_SHORT(DRCUML_OP_SUB, 4, 0):      /* SUB     dst,src1,src2[,f]      */
728            PARAM0 = PARAM1 - PARAM2;
729            break;
730
731         case MAKE_OPCODE_SHORT(DRCUML_OP_SUB, 4, 1):
732            temp32 = PARAM1 - PARAM2;
733            flags = FLAGS32_NZCV_SUB(temp32, PARAM1, PARAM2);
734            PARAM0 = temp32;
735            break;
736
737         case MAKE_OPCODE_SHORT(DRCUML_OP_SUBB, 4, 0):      /* SUBB    dst,src1,src2[,f]      */
738            PARAM0 = PARAM1 - PARAM2 - (flags & DRCUML_FLAG_C);
739            break;
740
741         case MAKE_OPCODE_SHORT(DRCUML_OP_SUBB, 4, 1):
742            temp32 = PARAM1 - PARAM2 - (flags & DRCUML_FLAG_C);
743            flags = FLAGS32_NZCV_SUB(temp32, PARAM1, PARAM2);
744            PARAM0 = temp32;
745            break;
746
747         case MAKE_OPCODE_SHORT(DRCUML_OP_CMP, 4, 1):      /* CMP     src1,src2[,f]          */
748            temp32 = PARAM0 - PARAM1;
749            flags = FLAGS32_NZCV_SUB(temp32, PARAM0, PARAM1);
750            break;
751
752         case MAKE_OPCODE_SHORT(DRCUML_OP_MULU, 4, 0):      /* MULU    dst,edst,src1,src2[,f] */
753            temp64 = (UINT64)(UINT32)PARAM2 * (UINT64)(UINT32)PARAM3;
754            PARAM1 = temp64 >> 32;
755            PARAM0 = (UINT32)temp64;
756            break;
757
758         case MAKE_OPCODE_SHORT(DRCUML_OP_MULU, 4, 1):
759            temp64 = (UINT64)(UINT32)PARAM2 * (UINT64)(UINT32)PARAM3;
760            flags = FLAGS64_NZ(temp64);
761            PARAM1 = temp64 >> 32;
762            PARAM0 = (UINT32)temp64;
763            break;
764
765         case MAKE_OPCODE_SHORT(DRCUML_OP_MULS, 4, 0):      /* MULS    dst,edst,src1,src2[,f] */
766            temp64 = (INT64)(INT32)PARAM2 * (INT64)(INT32)PARAM3;
767            PARAM1 = temp64 >> 32;
768            PARAM0 = (UINT32)temp64;
769            break;
770
771         case MAKE_OPCODE_SHORT(DRCUML_OP_MULS, 4, 1):
772            temp64 = (INT64)(INT32)PARAM2 * (INT64)(INT32)PARAM3;
773            flags = FLAGS64_NZ(temp64);
774            PARAM1 = temp64 >> 32;
775            PARAM0 = (UINT32)temp64;
776            break;
777
778         case MAKE_OPCODE_SHORT(DRCUML_OP_DIVU, 4, 0):      /* DIVU    dst,edst,src1,src2[,f] */
779            if (PARAM3 != 0)
780            {
781               temp32 = (UINT32)PARAM2 / (UINT32)PARAM3;
782               PARAM1 = (UINT32)PARAM2 % (UINT32)PARAM3;
783               PARAM0 = temp32;
784            }
785            break;
786
787         case MAKE_OPCODE_SHORT(DRCUML_OP_DIVU, 4, 1):
788            if (PARAM3 != 0)
789            {
790               temp32 = (UINT32)PARAM2 / (UINT32)PARAM3;
791               PARAM1 = (UINT32)PARAM2 % (UINT32)PARAM3;
792               flags = FLAGS32_NZ(temp32);
793               PARAM0 = temp32;
794            }
795            break;
796
797         case MAKE_OPCODE_SHORT(DRCUML_OP_DIVS, 4, 0):      /* DIVS    dst,edst,src1,src2[,f] */
798            if (PARAM3 != 0)
799            {
800               temp32 = (INT32)PARAM2 / (INT32)PARAM3;
801               PARAM1 = (INT32)PARAM2 % (INT32)PARAM3;
802               PARAM0 = temp32;
803            }
804            break;
805
806         case MAKE_OPCODE_SHORT(DRCUML_OP_DIVS, 4, 1):
807            if (PARAM3 != 0)
808            {
809               temp32 = (INT32)PARAM2 / (INT32)PARAM3;
810               PARAM1 = (INT32)PARAM2 % (INT32)PARAM3;
811               flags = FLAGS32_NZ(temp32);
812               PARAM0 = temp32;
813            }
814            break;
815
816         case MAKE_OPCODE_SHORT(DRCUML_OP_AND, 4, 0):      /* AND     dst,src1,src2[,f]      */
817            PARAM0 = PARAM1 & PARAM2;
818            break;
819
820         case MAKE_OPCODE_SHORT(DRCUML_OP_AND, 4, 1):
821            temp32 = PARAM1 & PARAM2;
822            flags = FLAGS32_NZ(temp32);
823            PARAM0 = temp32;
824            break;
825
826         case MAKE_OPCODE_SHORT(DRCUML_OP_TEST, 4, 1):      /* TEST    src1,src2[,f]          */
827            temp32 = PARAM0 & PARAM1;
828            flags = FLAGS32_NZ(temp32);
829            break;
830
831         case MAKE_OPCODE_SHORT(DRCUML_OP_OR, 4, 0):         /* OR      dst,src1,src2[,f]      */
832            PARAM0 = PARAM1 | PARAM2;
833            break;
834
835         case MAKE_OPCODE_SHORT(DRCUML_OP_OR, 4, 1):
836            temp32 = PARAM1 | PARAM2;
837            flags = FLAGS32_NZ(temp32);
838            PARAM0 = temp32;
839            break;
840
841         case MAKE_OPCODE_SHORT(DRCUML_OP_XOR, 4, 0):      /* XOR     dst,src1,src2[,f]      */
842            PARAM0 = PARAM1 ^ PARAM2;
843            break;
844
845         case MAKE_OPCODE_SHORT(DRCUML_OP_XOR, 4, 1):
846            temp32 = PARAM1 ^ PARAM2;
847            flags = FLAGS32_NZ(temp32);
848            PARAM0 = temp32;
849            break;
850
851         case MAKE_OPCODE_SHORT(DRCUML_OP_SHL, 4, 0):      /* SHL     dst,src,count[,f]      */
852            PARAM0 = PARAM1 << (PARAM2 & 31);
853            break;
854
855         case MAKE_OPCODE_SHORT(DRCUML_OP_SHL, 4, 1):
856            shift = PARAM2 & 31;
857            temp32 = PARAM1 << shift;
858            flags = FLAGS32_NZ(temp32);
859            if (shift != 0) flags |= ((PARAM1 << (shift - 1)) >> 31) & DRCUML_FLAG_C;
860            PARAM0 = temp32;
861            break;
862
863         case MAKE_OPCODE_SHORT(DRCUML_OP_SHR, 4, 0):      /* SHR     dst,src,count[,f]      */
864            PARAM0 = PARAM1 >> (PARAM2 & 31);
865            break;
866
867         case MAKE_OPCODE_SHORT(DRCUML_OP_SHR, 4, 1):
868            shift = PARAM2 & 31;
869            temp32 = PARAM1 >> shift;
870            flags = FLAGS32_NZ(temp32);
871            if (shift != 0) flags |= (PARAM1 >> (shift - 1)) & DRCUML_FLAG_C;
872            PARAM0 = temp32;
873            break;
874
875         case MAKE_OPCODE_SHORT(DRCUML_OP_SAR, 4, 0):      /* SAR     dst,src,count[,f]      */
876            PARAM0 = (INT32)PARAM1 >> (PARAM2 & 31);
877            break;
878
879         case MAKE_OPCODE_SHORT(DRCUML_OP_SAR, 4, 1):
880            shift = PARAM2 & 31;
881            temp32 = (INT32)PARAM1 >> shift;
882            flags = FLAGS32_NZ(temp32);
883            if (shift != 0) flags |= (PARAM1 >> (shift - 1)) & DRCUML_FLAG_C;
884            PARAM0 = temp32;
885            break;
886
887         case MAKE_OPCODE_SHORT(DRCUML_OP_ROL, 4, 0):      /* ROL     dst,src,count[,f]      */
888            shift = PARAM2 & 31;
889            PARAM0 = (PARAM1 << shift) | (PARAM1 >> ((32 - shift) & 31));
890            break;
891
892         case MAKE_OPCODE_SHORT(DRCUML_OP_ROL, 4, 1):
893            shift = PARAM2 & 31;
894            temp32 = (PARAM1 << shift) | (PARAM1 >> ((32 - shift) & 31));
895            flags = FLAGS32_NZ(temp32);
896            if (shift != 0) flags |= ((PARAM1 << (shift - 1)) >> 31) & DRCUML_FLAG_C;
897            PARAM0 = temp32;
898            break;
899
900         case MAKE_OPCODE_SHORT(DRCUML_OP_ROLC, 4, 0):      /* ROLC    dst,src,count[,f]      */
901            shift = PARAM2 & 31;
902            if (shift > 1)
903               PARAM0 = (PARAM1 << shift) | ((flags & DRCUML_FLAG_C) << (shift - 1)) | (PARAM1 >> (33 - shift));
904            else if (shift == 1)
905               PARAM0 = (PARAM1 << shift) | (flags & DRCUML_FLAG_C);
906            break;
907
908         case MAKE_OPCODE_SHORT(DRCUML_OP_ROLC, 4, 1):
909            shift = PARAM2 & 31;
910            if (shift > 1)
911               temp32 = (PARAM1 << shift) | ((flags & DRCUML_FLAG_C) << (shift - 1)) | (PARAM1 >> (33 - shift));
912            else if (shift == 1)
913               temp32 = (PARAM1 << shift) | (flags & DRCUML_FLAG_C);
914            else
915               temp32 = PARAM1;
916            flags = FLAGS32_NZ(temp32);
917            if (shift != 0) flags |= ((PARAM1 << (shift - 1)) >> 31) & DRCUML_FLAG_C;
918            PARAM0 = temp32;
919            break;
920
921         case MAKE_OPCODE_SHORT(DRCUML_OP_ROR, 4, 0):      /* ROR     dst,src,count[,f]      */
922            shift = PARAM2 & 31;
923            PARAM0 = (PARAM1 >> shift) | (PARAM1 << ((32 - shift) & 31));
924            break;
925
926         case MAKE_OPCODE_SHORT(DRCUML_OP_ROR, 4, 1):
927            shift = PARAM2 & 31;
928            temp32 = (PARAM1 >> shift) | (PARAM1 << ((32 - shift) & 31));
929            flags = FLAGS32_NZ(temp32);
930            if (shift != 0) flags |= (PARAM1 >> (shift - 1)) & DRCUML_FLAG_C;
931            PARAM0 = temp32;
932            break;
933
934         case MAKE_OPCODE_SHORT(DRCUML_OP_RORC, 4, 0):      /* RORC    dst,src,count[,f]      */
935            shift = PARAM2 & 31;
936            if (shift > 1)
937               PARAM0 = (PARAM1 >> shift) | (((flags & DRCUML_FLAG_C) << 31) >> (shift - 1)) | (PARAM1 << (33 - shift));
938            else if (shift == 1)
939               PARAM0 = (PARAM1 >> shift) | ((flags & DRCUML_FLAG_C) << 31);
940            break;
941
942         case MAKE_OPCODE_SHORT(DRCUML_OP_RORC, 4, 1):
943            shift = PARAM2 & 31;
944            if (shift > 1)
945               temp32 = (PARAM1 >> shift) | (((flags & DRCUML_FLAG_C) << 31) >> (shift - 1)) | (PARAM1 << (33 - shift));
946            else if (shift == 1)
947               temp32 = (PARAM1 >> shift) | ((flags & DRCUML_FLAG_C) << 31);
948            else
949               temp32 = PARAM1;
950            flags = FLAGS32_NZ(temp32);
951            if (shift != 0) flags |= (PARAM1 >> (shift - 1)) & DRCUML_FLAG_C;
952            PARAM0 = temp32;
953            break;
954
955
956         /* ----------------------- 64-Bit Integer Operations ----------------------- */
957
958         case MAKE_OPCODE_SHORT(DRCUML_OP_LOAD1U, 8, 0):      /* DLOAD1U dst,base,index         */
959            DPARAM0 = inst[1].puint8[PARAM2];
960            break;
961
962         case MAKE_OPCODE_SHORT(DRCUML_OP_LOAD1S, 8, 0):      /* DLOAD1S dst,base,index         */
963            DPARAM0 = inst[1].pint8[PARAM2];
964            break;
965
966         case MAKE_OPCODE_SHORT(DRCUML_OP_LOAD2U, 8, 0):      /* DLOAD2U dst,base,index         */
967            DPARAM0 = inst[1].puint16[PARAM2];
968            break;
969
970         case MAKE_OPCODE_SHORT(DRCUML_OP_LOAD2S, 8, 0):      /* DLOAD2S dst,base,index         */
971            DPARAM0 = inst[1].pint16[PARAM2];
972            break;
973
974         case MAKE_OPCODE_SHORT(DRCUML_OP_LOAD4U, 8, 0):      /* DLOAD4U dst,base,index         */
975            DPARAM0 = inst[1].puint32[PARAM2];
976            break;
977
978         case MAKE_OPCODE_SHORT(DRCUML_OP_LOAD4S, 8, 0):      /* DLOAD4S dst,base,index         */
979            DPARAM0 = inst[1].pint32[PARAM2];
980            break;
981
982         case MAKE_OPCODE_SHORT(DRCUML_OP_LOAD8U, 8, 0):      /* DLOAD8U dst,base,index         */
983            DPARAM0 = inst[1].puint64[PARAM2];
984            break;
985
986         case MAKE_OPCODE_SHORT(DRCUML_OP_STORE1, 8, 0):      /* DSTORE1 dst,base,index         */
987            inst[0].puint8[PARAM1] = DPARAM2;
988            break;
989
990         case MAKE_OPCODE_SHORT(DRCUML_OP_STORE2, 8, 0):      /* DSTORE2 dst,base,index         */
991            inst[0].puint16[PARAM1] = DPARAM2;
992            break;
993
994         case MAKE_OPCODE_SHORT(DRCUML_OP_STORE4, 8, 0):      /* DSTORE4 dst,base,index         */
995            inst[0].puint32[PARAM1] = DPARAM2;
996            break;
997
998         case MAKE_OPCODE_SHORT(DRCUML_OP_STORE8, 8, 0):      /* DSTORE8 dst,base,index         */
999            inst[0].puint64[PARAM1] = DPARAM2;
1000            break;
1001
1002         case MAKE_OPCODE_SHORT(DRCUML_OP_READ1U, 8, 0):      /* DREAD1U dst,space,src1         */
1003            DPARAM0 = (UINT8)(*active_address_space[PARAM1].accessors->read_byte)(PARAM2);
1004            break;
1005
1006         case MAKE_OPCODE_SHORT(DRCUML_OP_READ1S, 8, 0):      /* DREAD1S dst,space,src1         */
1007            DPARAM0 = (INT8)(*active_address_space[PARAM1].accessors->read_byte)(PARAM2);
1008            break;
1009
1010         case MAKE_OPCODE_SHORT(DRCUML_OP_READ2U, 8, 0):      /* DREAD2U dst,space,src1         */
1011            DPARAM0 = (UINT16)(*active_address_space[PARAM1].accessors->read_word)(PARAM2);
1012            break;
1013
1014         case MAKE_OPCODE_SHORT(DRCUML_OP_READ2S, 8, 0):      /* DREAD2S dst,space,src1         */
1015            DPARAM0 = (INT16)(*active_address_space[PARAM1].accessors->read_word)(PARAM2);
1016            break;
1017
1018         case MAKE_OPCODE_SHORT(DRCUML_OP_READ2M, 8, 0):      /* DREAD2M dst,space,src1,mask    */
1019            DPARAM0 = (UINT16)(*active_address_space[PARAM1].accessors->read_word_masked)(PARAM2, DPARAM3);
1020            break;
1021
1022         case MAKE_OPCODE_SHORT(DRCUML_OP_READ4U, 8, 0):      /* DREAD4U dst,space,src1         */
1023            DPARAM0 = (UINT32)(*active_address_space[PARAM1].accessors->read_dword)(PARAM2);
1024            break;
1025
1026         case MAKE_OPCODE_SHORT(DRCUML_OP_READ4S, 8, 0):      /* DREAD4S dst,space,src1         */
1027            DPARAM0 = (INT32)(*active_address_space[PARAM1].accessors->read_dword)(PARAM2);
1028            break;
1029
1030         case MAKE_OPCODE_SHORT(DRCUML_OP_READ4M, 8, 0):      /* DREAD4M dst,space,src1,mask    */
1031            DPARAM0 = (UINT32)(*active_address_space[PARAM1].accessors->read_dword_masked)(PARAM2, DPARAM3);
1032            break;
1033
1034         case MAKE_OPCODE_SHORT(DRCUML_OP_READ8U, 8, 0):      /* DREAD8U dst,space,src1         */
1035            DPARAM0 = (UINT64)(*active_address_space[PARAM1].accessors->read_qword)(PARAM2);
1036            break;
1037
1038         case MAKE_OPCODE_SHORT(DRCUML_OP_READ8M, 8, 0):      /* DREAD8M dst,space,src1,mask    */
1039            DPARAM0 = (UINT64)(*active_address_space[PARAM1].accessors->read_qword_masked)(PARAM2, DPARAM3);
1040            break;
1041
1042         case MAKE_OPCODE_SHORT(DRCUML_OP_WRITE1, 8, 0):      /* DWRITE1 space,dst,src1         */
1043            (*active_address_space[PARAM0].accessors->write_byte)(PARAM1, DPARAM2);
1044            break;
1045
1046         case MAKE_OPCODE_SHORT(DRCUML_OP_WRITE2, 8, 0):      /* DWRITE2 space,dst,src1         */
1047            (*active_address_space[PARAM0].accessors->write_word)(PARAM1, DPARAM2);
1048            break;
1049
1050         case MAKE_OPCODE_SHORT(DRCUML_OP_WRIT2M, 8, 0):      /* DWRIT2M space,dst,mask,src1    */
1051            (*active_address_space[PARAM0].accessors->write_word_masked)(PARAM1, DPARAM3, DPARAM2);
1052            break;
1053
1054         case MAKE_OPCODE_SHORT(DRCUML_OP_WRITE4, 8, 0):      /* DWRITE4 space,dst,src1         */
1055            (*active_address_space[PARAM0].accessors->write_dword)(PARAM1, DPARAM2);
1056            break;
1057
1058         case MAKE_OPCODE_SHORT(DRCUML_OP_WRIT4M, 8, 0):      /* DWRIT4M space,dst,mask,src1    */
1059            (*active_address_space[PARAM0].accessors->write_dword_masked)(PARAM1, DPARAM3, DPARAM2);
1060            break;
1061
1062         case MAKE_OPCODE_SHORT(DRCUML_OP_WRITE8, 8, 0):      /* DWRITE8 space,dst,src1         */
1063            (*active_address_space[PARAM0].accessors->write_qword)(PARAM1, DPARAM2);
1064            break;
1065
1066         case MAKE_OPCODE_SHORT(DRCUML_OP_WRIT8M, 8, 0):      /* DWRIT8M space,dst,mask,src1    */
1067            (*active_address_space[PARAM0].accessors->write_qword_masked)(PARAM1, DPARAM3, DPARAM2);
1068            break;
1069
1070         case MAKE_OPCODE_SHORT(DRCUML_OP_FLAGS, 8, 0):      /* DFLAGS  dst,mask,table         */
1071            DPARAM0 = (DPARAM0 & ~DPARAM1) | (inst[2].puint64[flags & 0x0f] & DPARAM1);
1072            break;
1073
1074         case MAKE_OPCODE_SHORT(DRCUML_OP_MOV, 8, 1):      /* DMOV    dst,src[,c]            */
1075            if (OPCODE_FAIL_CONDITION(opcode, flags))
1076               break;
1077            /* fall through... */
1078
1079         case MAKE_OPCODE_SHORT(DRCUML_OP_MOV, 8, 0):
1080            DPARAM0 = DPARAM1;
1081            break;
1082
1083         case MAKE_OPCODE_SHORT(DRCUML_OP_ZEXT1, 8, 0):      /* DZEXT1  dst,src                */
1084            DPARAM0 = (UINT8)DPARAM1;
1085            break;
1086
1087         case MAKE_OPCODE_SHORT(DRCUML_OP_ZEXT2, 8, 0):      /* DZEXT2  dst,src                */
1088            DPARAM0 = (UINT16)DPARAM1;
1089            break;
1090
1091         case MAKE_OPCODE_SHORT(DRCUML_OP_ZEXT4, 8, 0):      /* DZEXT4  dst,src                */
1092            DPARAM0 = (UINT32)DPARAM1;
1093            break;
1094
1095         case MAKE_OPCODE_SHORT(DRCUML_OP_SEXT1, 8, 0):      /* DSEXT1  dst,src                */
1096            DPARAM0 = (INT8)DPARAM1;
1097            break;
1098
1099         case MAKE_OPCODE_SHORT(DRCUML_OP_SEXT2, 8, 0):      /* DSEXT2  dst,src                */
1100            DPARAM0 = (INT16)DPARAM1;
1101            break;
1102
1103         case MAKE_OPCODE_SHORT(DRCUML_OP_SEXT4, 8, 0):      /* DSEXT4  dst,src                */
1104            DPARAM0 = (INT32)DPARAM1;
1105            break;
1106
1107         case MAKE_OPCODE_SHORT(DRCUML_OP_ADD, 8, 0):      /* DADD    dst,src1,src2[,f]      */
1108            DPARAM0 = DPARAM1 + DPARAM2;
1109            break;
1110
1111         case MAKE_OPCODE_SHORT(DRCUML_OP_ADD, 8, 1):
1112            temp64 = DPARAM1 + DPARAM2;
1113            flags = FLAGS64_NZCV_ADD(temp64, DPARAM1, DPARAM2);
1114            DPARAM0 = temp64;
1115            break;
1116
1117         case MAKE_OPCODE_SHORT(DRCUML_OP_ADDC, 8, 0):      /* DADDC   dst,src1,src2[,f]      */
1118            DPARAM0 = DPARAM1 + DPARAM2 + (flags & DRCUML_FLAG_C);
1119            break;
1120
1121         case MAKE_OPCODE_SHORT(DRCUML_OP_ADDC, 8, 1):
1122            temp64 = DPARAM1 + DPARAM2 + (flags & DRCUML_FLAG_C);
1123            flags = FLAGS64_NZCV_ADD(temp64, DPARAM1, DPARAM2);
1124            DPARAM0 = temp64;
1125            break;
1126
1127         case MAKE_OPCODE_SHORT(DRCUML_OP_SUB, 8, 0):      /* DSUB    dst,src1,src2[,f]      */
1128            DPARAM0 = DPARAM1 - DPARAM2;
1129            break;
1130
1131         case MAKE_OPCODE_SHORT(DRCUML_OP_SUB, 8, 1):
1132            temp64 = DPARAM1 - DPARAM2;
1133            flags = FLAGS64_NZCV_SUB(temp64, DPARAM1, DPARAM2);
1134            DPARAM0 = temp64;
1135            break;
1136
1137         case MAKE_OPCODE_SHORT(DRCUML_OP_SUBB, 8, 0):      /* DSUBB   dst,src1,src2[,f]      */
1138            DPARAM0 = DPARAM1 - DPARAM2 - (flags & DRCUML_FLAG_C);
1139            break;
1140
1141         case MAKE_OPCODE_SHORT(DRCUML_OP_SUBB, 8, 1):
1142            temp64 = DPARAM1 - DPARAM2 - (flags & DRCUML_FLAG_C);
1143            flags = FLAGS64_NZCV_SUB(temp64, DPARAM1, DPARAM2);
1144            DPARAM0 = temp64;
1145            break;
1146
1147         case MAKE_OPCODE_SHORT(DRCUML_OP_CMP, 8, 1):      /* DCMP    src1,src2[,f]          */
1148            temp64 = DPARAM0 - DPARAM1;
1149            flags = FLAGS64_NZCV_SUB(temp64, DPARAM0, DPARAM1);
1150            break;
1151
1152         case MAKE_OPCODE_SHORT(DRCUML_OP_MULU, 8, 0):      /* DMULU   dst,edst,src1,src2[,f] */
1153            dmulu(inst[0].puint64, inst[1].puint64, DPARAM2, DPARAM3, FALSE);
1154            break;
1155
1156         case MAKE_OPCODE_SHORT(DRCUML_OP_MULU, 8, 1):
1157            flags = dmulu(inst[0].puint64, inst[1].puint64, DPARAM2, DPARAM3, TRUE);
1158            break;
1159
1160         case MAKE_OPCODE_SHORT(DRCUML_OP_MULS, 8, 0):      /* DMULS   dst,edst,src1,src2[,f] */
1161            dmuls(inst[0].puint64, inst[1].puint64, DPARAM2, DPARAM3, FALSE);
1162            break;
1163
1164         case MAKE_OPCODE_SHORT(DRCUML_OP_MULS, 8, 1):
1165            flags = dmuls(inst[0].puint64, inst[1].puint64, DPARAM2, DPARAM3, TRUE);
1166            break;
1167
1168         case MAKE_OPCODE_SHORT(DRCUML_OP_DIVU, 8, 0):      /* DDIVU   dst,edst,src1,src2[,f] */
1169            if (DPARAM3 != 0)
1170            {
1171               temp64 = (UINT64)DPARAM2 / (UINT64)DPARAM3;
1172               DPARAM1 = (UINT64)DPARAM2 % (UINT64)DPARAM3;
1173               DPARAM0 = temp64;
1174            }
1175            break;
1176
1177         case MAKE_OPCODE_SHORT(DRCUML_OP_DIVU, 8, 1):
1178            if (DPARAM3 != 0)
1179            {
1180               temp64 = (UINT64)DPARAM2 / (UINT64)DPARAM3;
1181               DPARAM1 = (UINT64)DPARAM2 % (UINT64)DPARAM3;
1182               flags = FLAGS64_NZ(temp64);
1183               DPARAM0 = temp64;
1184            }
1185            break;
1186
1187         case MAKE_OPCODE_SHORT(DRCUML_OP_DIVS, 8, 0):      /* DDIVS   dst,edst,src1,src2[,f] */
1188            if (DPARAM3 != 0)
1189            {
1190               temp64 = (INT64)DPARAM2 / (INT64)DPARAM3;
1191               DPARAM1 = (INT64)DPARAM2 % (INT64)DPARAM3;
1192               DPARAM0 = temp64;
1193            }
1194            break;
1195
1196         case MAKE_OPCODE_SHORT(DRCUML_OP_DIVS, 8, 1):
1197            if (DPARAM3 != 0)
1198            {
1199               temp64 = (INT64)DPARAM2 / (INT64)DPARAM3;
1200               DPARAM1 = (INT64)DPARAM2 % (INT64)DPARAM3;
1201               flags = FLAGS64_NZ(temp64);
1202               DPARAM0 = temp64;
1203            }
1204            break;
1205
1206         case MAKE_OPCODE_SHORT(DRCUML_OP_AND, 8, 0):      /* DAND    dst,src1,src2[,f]      */
1207            DPARAM0 = DPARAM1 & DPARAM2;
1208            break;
1209
1210         case MAKE_OPCODE_SHORT(DRCUML_OP_AND, 8, 1):
1211            temp64 = DPARAM1 & DPARAM2;
1212            flags = FLAGS64_NZ(temp64);
1213            DPARAM0 = temp64;
1214            break;
1215
1216         case MAKE_OPCODE_SHORT(DRCUML_OP_TEST, 8, 1):      /* DTEST   src1,src2[,f]          */
1217            temp64 = DPARAM1 & DPARAM2;
1218            flags = FLAGS64_NZ(temp64);
1219            break;
1220
1221         case MAKE_OPCODE_SHORT(DRCUML_OP_OR, 8, 0):         /* DOR     dst,src1,src2[,f]      */
1222            DPARAM0 = DPARAM1 | DPARAM2;
1223            break;
1224
1225         case MAKE_OPCODE_SHORT(DRCUML_OP_OR, 8, 1):
1226            temp64 = DPARAM1 | DPARAM2;
1227            flags = FLAGS64_NZ(temp64);
1228            DPARAM0 = temp64;
1229            break;
1230
1231         case MAKE_OPCODE_SHORT(DRCUML_OP_XOR, 8, 0):      /* DXOR    dst,src1,src2[,f]      */
1232            DPARAM0 = DPARAM1 ^ DPARAM2;
1233            break;
1234
1235         case MAKE_OPCODE_SHORT(DRCUML_OP_XOR, 8, 1):
1236            temp64 = DPARAM1 ^ DPARAM2;
1237            flags = FLAGS64_NZ(temp64);
1238            DPARAM0 = temp64;
1239            break;
1240
1241         case MAKE_OPCODE_SHORT(DRCUML_OP_SHL, 8, 0):      /* DSHL    dst,src,count[,f]      */
1242            DPARAM0 = DPARAM1 << (DPARAM2 & 63);
1243            break;
1244
1245         case MAKE_OPCODE_SHORT(DRCUML_OP_SHL, 8, 1):
1246            shift = DPARAM2 & 63;
1247            temp64 = DPARAM1 << shift;
1248            flags = FLAGS64_NZ(temp64);
1249            if (shift != 0) flags |= ((DPARAM1 << (shift - 1)) >> 63) & DRCUML_FLAG_C;
1250            DPARAM0 = temp64;
1251            break;
1252
1253         case MAKE_OPCODE_SHORT(DRCUML_OP_SHR, 8, 0):      /* DSHR    dst,src,count[,f]      */
1254            DPARAM0 = DPARAM1 >> (DPARAM2 & 63);
1255            break;
1256
1257         case MAKE_OPCODE_SHORT(DRCUML_OP_SHR, 8, 1):
1258            shift = DPARAM2 & 63;
1259            temp64 = DPARAM1 >> shift;
1260            flags = FLAGS64_NZ(temp64);
1261            if (shift != 0) flags |= (DPARAM1 >> (shift - 1)) & DRCUML_FLAG_C;
1262            DPARAM0 = temp64;
1263            break;
1264
1265         case MAKE_OPCODE_SHORT(DRCUML_OP_SAR, 8, 0):      /* DSAR    dst,src,count[,f]      */
1266            DPARAM0 = (INT64)DPARAM1 >> (DPARAM2 & 63);
1267            break;
1268
1269         case MAKE_OPCODE_SHORT(DRCUML_OP_SAR, 8, 1):
1270            shift = DPARAM2 & 63;
1271            temp64 = (INT32)DPARAM1 >> shift;
1272            flags = FLAGS64_NZ(temp64);
1273            if (shift != 0) flags |= (DPARAM1 >> (shift - 1)) & DRCUML_FLAG_C;
1274            DPARAM0 = temp64;
1275            break;
1276
1277         case MAKE_OPCODE_SHORT(DRCUML_OP_ROL, 8, 0):      /* DROL    dst,src,count[,f]      */
1278            shift = DPARAM2 & 31;
1279            DPARAM0 = (DPARAM1 << shift) | (DPARAM1 >> ((64 - shift) & 63));
1280            break;
1281
1282         case MAKE_OPCODE_SHORT(DRCUML_OP_ROL, 8, 1):
1283            shift = DPARAM2 & 63;
1284            temp64 = (DPARAM1 << shift) | (DPARAM1 >> ((64 - shift) & 63));
1285            flags = FLAGS64_NZ(temp64);
1286            if (shift != 0) flags |= ((DPARAM1 << (shift - 1)) >> 63) & DRCUML_FLAG_C;
1287            DPARAM0 = temp64;
1288            break;
1289
1290         case MAKE_OPCODE_SHORT(DRCUML_OP_ROLC, 8, 0):      /* DROLC   dst,src,count[,f]      */
1291            shift = DPARAM2 & 63;
1292            if (shift > 1)
1293               DPARAM0 = (DPARAM1 << shift) | ((flags & DRCUML_FLAG_C) << (shift - 1)) | (DPARAM1 >> (65 - shift));
1294            else if (shift == 1)
1295               DPARAM0 = (DPARAM1 << shift) | (flags & DRCUML_FLAG_C);
1296            break;
1297
1298         case MAKE_OPCODE_SHORT(DRCUML_OP_ROLC, 8, 1):
1299            shift = DPARAM2 & 63;
1300            if (shift > 1)
1301               temp64 = (DPARAM1 << shift) | ((flags & DRCUML_FLAG_C) << (shift - 1)) | (DPARAM1 >> (65 - shift));
1302            else if (shift == 1)
1303               temp64 = (DPARAM1 << shift) | (flags & DRCUML_FLAG_C);
1304            else
1305               temp64 = DPARAM1;
1306            flags = FLAGS64_NZ(temp64);
1307            if (shift != 0) flags |= ((DPARAM1 << (shift - 1)) >> 63) & DRCUML_FLAG_C;
1308            DPARAM0 = temp64;
1309            break;
1310
1311         case MAKE_OPCODE_SHORT(DRCUML_OP_ROR, 8, 0):      /* DROR    dst,src,count[,f]      */
1312            shift = DPARAM2 & 63;
1313            DPARAM0 = (DPARAM1 >> shift) | (DPARAM1 << ((64 - shift) & 63));
1314            break;
1315
1316         case MAKE_OPCODE_SHORT(DRCUML_OP_ROR, 8, 1):
1317            shift = DPARAM2 & 63;
1318            temp64 = (DPARAM1 >> shift) | (DPARAM1 << ((64 - shift) & 63));
1319            flags = FLAGS64_NZ(temp64);
1320            if (shift != 0) flags |= (DPARAM1 >> (shift - 1)) & DRCUML_FLAG_C;
1321            DPARAM0 = temp64;
1322            break;
1323
1324         case MAKE_OPCODE_SHORT(DRCUML_OP_RORC, 8, 0):      /* DRORC   dst,src,count[,f]      */
1325            shift = DPARAM2 & 63;
1326            if (shift > 1)
1327               DPARAM0 = (DPARAM1 >> shift) | ((((UINT64)flags & DRCUML_FLAG_C) << 63) >> (shift - 1)) | (DPARAM1 << (65 - shift));
1328            else if (shift == 1)
1329               DPARAM0 = (DPARAM1 >> shift) | (((UINT64)flags & DRCUML_FLAG_C) << 63);
1330            break;
1331
1332         case MAKE_OPCODE_SHORT(DRCUML_OP_RORC, 8, 1):
1333            shift = DPARAM2 & 63;
1334            if (shift > 1)
1335               temp64 = (DPARAM1 >> shift) | ((((UINT64)flags & DRCUML_FLAG_C) << 63) >> (shift - 1)) | (DPARAM1 << (65 - shift));
1336            else if (shift == 1)
1337               temp64 = (DPARAM1 >> shift) | (((UINT64)flags & DRCUML_FLAG_C) << 63);
1338            else
1339               temp64 = DPARAM1;
1340            flags = FLAGS64_NZ(temp64);
1341            if (shift != 0) flags |= (DPARAM1 >> (shift - 1)) & DRCUML_FLAG_C;
1342            DPARAM0 = temp64;
1343            break;
1344
1345
1346         /* ----------------------- 32-Bit Floating Point Operations ----------------------- */
1347
1348         case MAKE_OPCODE_SHORT(DRCUML_OP_FLOAD, 4, 0):      /* FSLOAD  dst,base,index         */
1349            FSPARAM0 = inst[1].pfloat[PARAM2];
1350            break;
1351
1352         case MAKE_OPCODE_SHORT(DRCUML_OP_FSTORE, 4, 0):      /* FSSTORE dst,base,index         */
1353            inst[0].pfloat[PARAM1] = FSPARAM2;
1354            break;
1355
1356         case MAKE_OPCODE_SHORT(DRCUML_OP_FREAD, 4, 0):      /* FSREAD  dst,space,src1         */
1357            PARAM0 = (UINT32)(*active_address_space[PARAM1].accessors->read_dword)(PARAM2);
1358            break;
1359
1360         case MAKE_OPCODE_SHORT(DRCUML_OP_FWRITE, 4, 0):      /* FSWRITE space,dst,src1         */
1361            (*active_address_space[PARAM0].accessors->write_dword)(PARAM1, PARAM2);
1362            break;
1363
1364         case MAKE_OPCODE_SHORT(DRCUML_OP_FMOV, 4, 1):      /* FSMOV   dst,src[,c]            */
1365            if (OPCODE_FAIL_CONDITION(opcode, flags))
1366               break;
1367            /* fall through... */
1368
1369         case MAKE_OPCODE_SHORT(DRCUML_OP_FMOV, 4, 0):
1370            FSPARAM0 = FSPARAM1;
1371            break;
1372
1373         case MAKE_OPCODE_SHORT(DRCUML_OP_FTOI4, 4, 0):      /* FSTOI4  dst,src1               */
1374            *inst[0].pint32 = FSPARAM1;
1375            break;
1376
1377         case MAKE_OPCODE_SHORT(DRCUML_OP_FTOI4T, 4, 0):      /* FSTOI4T dst,src1               */
1378            if (FSPARAM1 >= 0)
1379               *inst[0].pint32 = floor(FSPARAM1);
1380            else
1381               *inst[0].pint32 = ceil(FSPARAM1);
1382            break;
1383
1384         case MAKE_OPCODE_SHORT(DRCUML_OP_FTOI4R, 4, 0):      /* FSTOI4R dst,src1               */
1385            if (FSPARAM1 >= 0)
1386               *inst[0].pint32 = floor(FSPARAM1 + 0.5f);
1387            else
1388               *inst[0].pint32 = ceil(FSPARAM1 - 0.5f);
1389            break;
1390
1391         case MAKE_OPCODE_SHORT(DRCUML_OP_FTOI4F, 4, 0):      /* FSTOI4F dst,src1               */
1392            *inst[0].pint32 = floor(FSPARAM1);
1393            break;
1394
1395         case MAKE_OPCODE_SHORT(DRCUML_OP_FTOI4C, 4, 0):      /* FSTOI4C dst,src1               */
1396            *inst[0].pint32 = ceil(FSPARAM1);
1397            break;
1398
1399         case MAKE_OPCODE_SHORT(DRCUML_OP_FTOI8, 4, 0):      /* FSTOI8  dst,src1               */
1400            *inst[0].pint64 = FSPARAM1;
1401            break;
1402
1403         case MAKE_OPCODE_SHORT(DRCUML_OP_FTOI8T, 4, 0):      /* FSTOI8T dst,src1               */
1404            if (FSPARAM1 >= 0)
1405               *inst[0].pint64 = floor(FSPARAM1);
1406            else
1407               *inst[0].pint64 = ceil(FSPARAM1);
1408            break;
1409
1410         case MAKE_OPCODE_SHORT(DRCUML_OP_FTOI8R, 4, 0):      /* FSTOI8R dst,src1               */
1411            if (FSPARAM1 >= 0)
1412               *inst[0].pint64 = floor(FSPARAM1 + 0.5f);
1413            else
1414               *inst[0].pint64 = ceil(FSPARAM1 - 0.5f);
1415            break;
1416
1417         case MAKE_OPCODE_SHORT(DRCUML_OP_FTOI8F, 4, 0):      /* FSTOI8F dst,src1               */
1418            *inst[0].pint64 = floor(FSPARAM1);
1419            break;
1420
1421         case MAKE_OPCODE_SHORT(DRCUML_OP_FTOI8C, 4, 0):      /* FSTOI8C dst,src1               */
1422            *inst[0].pint64 = ceil(FSPARAM1);
1423            break;
1424
1425         case MAKE_OPCODE_SHORT(DRCUML_OP_FFRFD, 4, 0):      /* FSFRFD  dst,src1               */
1426            FSPARAM0 = FDPARAM1;
1427            break;
1428
1429         case MAKE_OPCODE_SHORT(DRCUML_OP_FFRI4, 4, 0):      /* FSFRI4  dst,src1               */
1430            FSPARAM0 = *inst[1].pint32;
1431            break;
1432
1433         case MAKE_OPCODE_SHORT(DRCUML_OP_FFRI8, 4, 0):      /* FSFRI8  dst,src1               */
1434            FSPARAM0 = *inst[1].pint64;
1435            break;
1436
1437         case MAKE_OPCODE_SHORT(DRCUML_OP_FADD, 4, 0):      /* FSADD   dst,src1,src2          */
1438            FSPARAM0 = FSPARAM1 + FSPARAM2;
1439            break;
1440
1441         case MAKE_OPCODE_SHORT(DRCUML_OP_FSUB, 4, 0):      /* FSSUB   dst,src1,src2          */
1442            FSPARAM0 = FSPARAM1 - FSPARAM2;
1443            break;
1444
1445         case MAKE_OPCODE_SHORT(DRCUML_OP_FCMP, 4, 1):      /* FSCMP   src1,src2              */
1446            if (isnan(FSPARAM0) || isnan(FSPARAM1))
1447               flags = DRCUML_FLAG_U;
1448            else
1449               flags = (FSPARAM0 < FSPARAM1) | ((FSPARAM0 == FSPARAM1) << 2);
1450            break;
1451
1452         case MAKE_OPCODE_SHORT(DRCUML_OP_FMUL, 4, 0):      /* FSMUL   dst,src1,src2          */
1453            FSPARAM0 = FSPARAM1 * FSPARAM2;
1454            break;
1455
1456         case MAKE_OPCODE_SHORT(DRCUML_OP_FDIV, 4, 0):      /* FSDIV   dst,src1,src2          */
1457            FSPARAM0 = FSPARAM1 / FSPARAM2;
1458            break;
1459
1460         case MAKE_OPCODE_SHORT(DRCUML_OP_FNEG, 4, 0):      /* FSNEG   dst,src1               */
1461            FSPARAM0 = -FSPARAM1;
1462            break;
1463
1464         case MAKE_OPCODE_SHORT(DRCUML_OP_FABS, 4, 0):      /* FSABS   dst,src1               */
1465            FSPARAM0 = fabs(FSPARAM1);
1466            break;
1467
1468         case MAKE_OPCODE_SHORT(DRCUML_OP_FSQRT, 4, 0):      /* FSSQRT  dst,src1               */
1469            FSPARAM0 = sqrt(FSPARAM1);
1470            break;
1471
1472         case MAKE_OPCODE_SHORT(DRCUML_OP_FRECIP, 4, 0):      /* FSRECIP dst,src1               */
1473            FSPARAM0 = 1.0f / FSPARAM1;
1474            break;
1475
1476         case MAKE_OPCODE_SHORT(DRCUML_OP_FRSQRT, 4, 0):      /* FSRSQRT dst,src1               */
1477            FSPARAM0 = 1.0f / sqrt(FSPARAM1);
1478            break;
1479
1480
1481         /* ----------------------- 64-Bit Floating Point Operations ----------------------- */
1482
1483         case MAKE_OPCODE_SHORT(DRCUML_OP_FLOAD, 8, 0):      /* FDLOAD  dst,base,index         */
1484            FDPARAM0 = inst[1].pdouble[PARAM2];
1485            break;
1486
1487         case MAKE_OPCODE_SHORT(DRCUML_OP_FSTORE, 8, 0):      /* FDSTORE dst,base,index         */
1488            inst[0].pdouble[PARAM1] = FDPARAM2;
1489            break;
1490
1491         case MAKE_OPCODE_SHORT(DRCUML_OP_FREAD, 8, 0):      /* FDREAD  dst,space,src1         */
1492            PARAM0 = (UINT64)(*active_address_space[PARAM1].accessors->read_qword)(PARAM2);
1493            break;
1494
1495         case MAKE_OPCODE_SHORT(DRCUML_OP_FWRITE, 8, 0):      /* FDWRITE space,dst,src1         */
1496            (*active_address_space[PARAM0].accessors->write_qword)(PARAM1, PARAM2);
1497            break;
1498
1499         case MAKE_OPCODE_SHORT(DRCUML_OP_FMOV, 8, 1):      /* FDMOV   dst,src[,c]            */
1500            if (OPCODE_FAIL_CONDITION(opcode, flags))
1501               break;
1502            /* fall through... */
1503
1504         case MAKE_OPCODE_SHORT(DRCUML_OP_FMOV, 8, 0):
1505            FDPARAM0 = FDPARAM1;
1506            break;
1507
1508         case MAKE_OPCODE_SHORT(DRCUML_OP_FTOI4, 8, 0):      /* FDTOI4  dst,src1               */
1509            *inst[0].pint32 = FDPARAM1;
1510            break;
1511
1512         case MAKE_OPCODE_SHORT(DRCUML_OP_FTOI4T, 8, 0):      /* FDTOI4T dst,src1               */
1513            if (FDPARAM1 >= 0)
1514               *inst[0].pint32 = floor(FDPARAM1);
1515            else
1516               *inst[0].pint32 = ceil(FDPARAM1);
1517            break;
1518
1519         case MAKE_OPCODE_SHORT(DRCUML_OP_FTOI4R, 8, 0):      /* FDTOI4R dst,src1               */
1520            if (FDPARAM1 >= 0)
1521               *inst[0].pint32 = floor(FDPARAM1 + 0.5);
1522            else
1523               *inst[0].pint32 = ceil(FDPARAM1 - 0.5);
1524            break;
1525
1526         case MAKE_OPCODE_SHORT(DRCUML_OP_FTOI4F, 8, 0):      /* FDTOI4F dst,src1               */
1527            *inst[0].pint32 = floor(FDPARAM1);
1528            break;
1529
1530         case MAKE_OPCODE_SHORT(DRCUML_OP_FTOI4C, 8, 0):      /* FDTOI4C dst,src1               */
1531            *inst[0].pint32 = ceil(FDPARAM1);
1532            break;
1533
1534         case MAKE_OPCODE_SHORT(DRCUML_OP_FTOI8, 8, 0):      /* FDTOI8  dst,src1               */
1535            *inst[0].pint64 = FDPARAM1;
1536            break;
1537
1538         case MAKE_OPCODE_SHORT(DRCUML_OP_FTOI8T, 8, 0):      /* FDTOI8T dst,src1               */
1539            if (FDPARAM1 >= 0)
1540               *inst[0].pint64 = floor(FDPARAM1);
1541            else
1542               *inst[0].pint64 = ceil(FDPARAM1);
1543            break;
1544
1545         case MAKE_OPCODE_SHORT(DRCUML_OP_FTOI8R, 8, 0):      /* FDTOI8R  dst,src1               */
1546            if (FDPARAM1 >= 0)
1547               *inst[0].pint64 = floor(FDPARAM1 + 0.5);
1548            else
1549               *inst[0].pint64 = ceil(FDPARAM1 - 0.5);
1550            break;
1551
1552         case MAKE_OPCODE_SHORT(DRCUML_OP_FTOI8F, 8, 0):      /* FDTOI8F dst,src1               */
1553            *inst[0].pint64 = floor(FDPARAM1);
1554            break;
1555
1556         case MAKE_OPCODE_SHORT(DRCUML_OP_FTOI8C, 8, 0):      /* FDTOI8C dst,src1               */
1557            *inst[0].pint64 = ceil(FDPARAM1);
1558            break;
1559
1560         case MAKE_OPCODE_SHORT(DRCUML_OP_FFRFS, 8, 0):      /* FDFRFS  dst,src1               */
1561            FDPARAM0 = FSPARAM1;
1562            break;
1563
1564         case MAKE_OPCODE_SHORT(DRCUML_OP_FFRI4, 8, 0):      /* FDFRI4  dst,src1               */
1565            FDPARAM0 = *inst[1].pint32;
1566            break;
1567
1568         case MAKE_OPCODE_SHORT(DRCUML_OP_FFRI8, 8, 0):      /* FDFRI8  dst,src1               */
1569            FDPARAM0 = *inst[1].pint64;
1570            break;
1571
1572         case MAKE_OPCODE_SHORT(DRCUML_OP_FADD, 8, 0):      /* FDADD   dst,src1,src2          */
1573            FDPARAM0 = FDPARAM1 + FDPARAM2;
1574            break;
1575
1576         case MAKE_OPCODE_SHORT(DRCUML_OP_FSUB, 8, 0):      /* FDSUB   dst,src1,src2          */
1577            FDPARAM0 = FDPARAM1 - FDPARAM2;
1578            break;
1579
1580         case MAKE_OPCODE_SHORT(DRCUML_OP_FCMP, 8, 1):      /* FDCMP   src1,src2              */
1581            if (isnan(FDPARAM0) || isnan(FDPARAM1))
1582               flags = DRCUML_FLAG_U;
1583            else
1584               flags = (FDPARAM0 < FDPARAM1) | ((FDPARAM0 == FDPARAM1) << 2);
1585            break;
1586
1587         case MAKE_OPCODE_SHORT(DRCUML_OP_FMUL, 8, 0):      /* FDMUL   dst,src1,src2          */
1588            FDPARAM0 = FDPARAM1 * FDPARAM2;
1589            break;
1590
1591         case MAKE_OPCODE_SHORT(DRCUML_OP_FDIV, 8, 0):      /* FDDIV   dst,src1,src2          */
1592            FDPARAM0 = FDPARAM1 / FDPARAM2;
1593            break;
1594
1595         case MAKE_OPCODE_SHORT(DRCUML_OP_FNEG, 8, 0):      /* FDNEG   dst,src1               */
1596            FDPARAM0 = -FDPARAM1;
1597            break;
1598
1599         case MAKE_OPCODE_SHORT(DRCUML_OP_FABS, 8, 0):      /* FDABS   dst,src1               */
1600            FDPARAM0 = fabs(FDPARAM1);
1601            break;
1602
1603         case MAKE_OPCODE_SHORT(DRCUML_OP_FSQRT, 8, 0):      /* FDSQRT  dst,src1               */
1604            FDPARAM0 = sqrt(FDPARAM1);
1605            break;
1606
1607         case MAKE_OPCODE_SHORT(DRCUML_OP_FRECIP, 8, 0):      /* FDRECIP dst,src1               */
1608            FDPARAM0 = 1.0 / FDPARAM1;
1609            break;
1610
1611         case MAKE_OPCODE_SHORT(DRCUML_OP_FRSQRT, 8, 0):      /* FDRSQRT dst,src1               */
1612            FDPARAM0 = 1.0 / sqrt(FDPARAM1);
1613            break;
1614
1615         default:
1616            fatalerror("Unexpected opcode!");
1617            break;
1618      }
1619
1620      /* advance past the parameters and immediates */
1621      inst += OPCODE_GET_PWORDS(opcode);
1622   }
1623
1624   return 0;
1625}
1626
1627
1628
1629/***************************************************************************
1630    PRIVATE HELPER FUNCTIONS
1631***************************************************************************/
1632
1633/*-------------------------------------------------
1634    output_parameter - output a parameter
1635-------------------------------------------------*/
1636
1637static void output_parameter(drcbe_state *drcbe, drcbec_instruction **dstptr, void **immedptr, int size, int immedsize, const drcuml_parameter *param)
1638{
1639   drcbec_instruction *dst = *dstptr;
1640   void *immed = *immedptr;
1641   drcuml_parameter temp_param;
1642
1643   switch (param->type)
1644   {
1645      /* convert mapvars to immediates */
1646      case DRCUML_PTYPE_MAPVAR:
1647         temp_param.value = drcmap_get_last_value(drcbe->map, param->value);
1648         param = &temp_param;
1649         /* fall through to immediate case */
1650
1651      /* immediates store a pointer to the immediate data, which is stored at the end of the instruction */
1652      case DRCUML_PTYPE_IMMEDIATE:
1653         if (param->value == 0)
1654            (dst++)->v = &immediate_zero;
1655         else
1656         {
1657            (dst++)->v = immed;
1658            if (immedsize == 4)
1659               *(UINT32 *)immed = (UINT32)param->value;
1660            else
1661               *(UINT64 *)immed = (UINT64)param->value;
1662            immed = (UINT8 *)immed + immedsize;
1663         }
1664         break;
1665
1666      /* int registers point to the appropriate part of the integer register state */
1667      case DRCUML_PTYPE_INT_REGISTER:
1668         if (size == 4)
1669            (dst++)->puint32 = &drcbe->state.r[param->value - DRCUML_REG_I0].l;
1670         else
1671            (dst++)->puint64 = &drcbe->state.r[param->value - DRCUML_REG_I0].d;
1672         break;
1673
1674      /* float registers point to the appropriate part of the floating point register state */
1675      case DRCUML_PTYPE_FLOAT_REGISTER:
1676         if (size == 4)
1677            (dst++)->pfloat = &drcbe->state.f[param->value - DRCUML_REG_F0].l;
1678         else
1679            (dst++)->pdouble = &drcbe->state.f[param->value - DRCUML_REG_F0].d;
1680         break;
1681
1682      /* memory just points to the memory */
1683      case DRCUML_PTYPE_MEMORY:
1684         (dst++)->v = (void *)(FPTR)param->value;
1685         break;
1686
1687      default:
1688         fatalerror("Unexpected param->type");
1689         break;
1690   }
1691
1692   *dstptr = dst;
1693   *immedptr = immed;
1694}
1695
1696
1697/*-------------------------------------------------
1698    fixup_label - callback to fixup forward-
1699    referenced labels
1700-------------------------------------------------*/
1701
1702static void fixup_label(void *parameter, drccodeptr labelcodeptr)
1703{
1704   drcbec_instruction *dst = parameter;
1705   dst->inst = (drcbec_instruction *)labelcodeptr;
1706}
1707
1708
1709/*-------------------------------------------------
1710    dmulu - perform a double-wide unsigned multiply
1711-------------------------------------------------*/
1712
1713static int dmulu(UINT64 *dstlo, UINT64 *dsthi, UINT64 src1, UINT64 src2, int flags)
1714{
1715   UINT64 lo, hi, prevlo;
1716   UINT64 a, b, temp;
1717
1718   /* shortcut if we don't care about the high bits or the flags */
1719   if (dstlo == dsthi && !flags)
1720   {
1721      *dstlo = src1 * src2;
1722      return 0;
1723   }
1724
1725   /* fetch source values */
1726   a = src1;
1727   b = src2;
1728   if (a == 0 || b == 0)
1729   {
1730      *dsthi = *dstlo = 0;
1731      return DRCUML_FLAG_Z;
1732   }
1733
1734   /* compute high and low parts first */
1735   lo = (UINT64)(UINT32)(a >> 0)  * (UINT64)(UINT32)(b >> 0);
1736   hi = (UINT64)(UINT32)(a >> 32) * (UINT64)(UINT32)(b >> 32);
1737
1738   /* compute middle parts */
1739   prevlo = lo;
1740   temp = (UINT64)(UINT32)(a >> 32)  * (UINT64)(UINT32)(b >> 0);
1741   lo += temp << 32;
1742   hi += (temp >> 32) + (lo < prevlo);
1743
1744   prevlo = lo;
1745   temp = (UINT64)(UINT32)(a >> 0)  * (UINT64)(UINT32)(b >> 32);
1746   lo += temp << 32;
1747   hi += (temp >> 32) + (lo < prevlo);
1748
1749   /* store the results */
1750   *dsthi = hi;
1751   *dstlo = lo;
1752   return ((hi >> 60) & DRCUML_FLAG_S);
1753}
1754
1755
1756/*-------------------------------------------------
1757    dmuls - perform a double-wide signed multiply
1758-------------------------------------------------*/
1759
1760static int dmuls(UINT64 *dstlo, UINT64 *dsthi, INT64 src1, INT64 src2, int flags)
1761{
1762   UINT64 lo, hi, prevlo;
1763   UINT64 a, b, temp;
1764
1765   /* shortcut if we don't care about the high bits or the flags */
1766   if (dstlo == dsthi && !flags)
1767   {
1768      *dstlo = src1 * src2;
1769      return 0;
1770   }
1771
1772   /* fetch absolute source values */
1773   a = src1; if ((INT64)a < 0) a = -a;
1774   b = src2; if ((INT64)b < 0) b = -b;
1775   if (a == 0 || b == 0)
1776   {
1777      *dsthi = *dstlo = 0;
1778      return DRCUML_FLAG_Z;
1779   }
1780
1781   /* compute high and low parts first */
1782   lo = (UINT64)(UINT32)(a >> 0)  * (UINT64)(UINT32)(b >> 0);
1783   hi = (UINT64)(UINT32)(a >> 32) * (UINT64)(UINT32)(b >> 32);
1784
1785   /* compute middle parts */
1786   prevlo = lo;
1787   temp = (UINT64)(UINT32)(a >> 32)  * (UINT64)(UINT32)(b >> 0);
1788   lo += temp << 32;
1789   hi += (temp >> 32) + (lo < prevlo);
1790
1791   prevlo = lo;
1792   temp = (UINT64)(UINT32)(a >> 0)  * (UINT64)(UINT32)(b >> 32);
1793   lo += temp << 32;
1794   hi += (temp >> 32) + (lo < prevlo);
1795
1796   /* adjust for signage */
1797   if ((INT64)(src1 ^ src2) < 0)
1798   {
1799      hi = ~hi + (lo == 0);
1800      lo = ~lo + 1;
1801   }
1802
1803   /* store the results */
1804   *dsthi = hi;
1805   *dstlo = lo;
1806   return ((hi >> 60) & DRCUML_FLAG_S);
1807}
src/emu/cpu/drccache.c
r0r2628
1/***************************************************************************
2
3    drccache.h
4
5    Universal dynamic recompiler cache management.
6
7    Copyright Aaron Giles
8    Released for general non-commercial use under the MAME license
9    Visit http://mamedev.org for licensing and usage restrictions.
10
11***************************************************************************/
12
13#include <stddef.h>
14#include "cpuintrf.h"
15#include "drccache.h"
16
17
18
19/***************************************************************************
20    CONSTANTS
21***************************************************************************/
22
23/* largest block of code that can be generated at once */
24#define CODEGEN_MAX_BYTES      65536
25
26/* minimum alignment, in bytes (must be power of 2) */
27#define CACHE_ALIGNMENT         8
28
29/* largest permanent allocation we allow */
30#define MAX_PERMANENT_ALLOC      1024
31
32/* size of "near" area at the base of the cache */
33#define NEAR_CACHE_SIZE         65536
34
35
36
37/***************************************************************************
38    MACROS
39***************************************************************************/
40
41/* ensure that all memory allocated is aligned to an 8-byte boundary */
42#define ALIGN_PTR_UP(p)         ((void *)(((FPTR)(p) + (CACHE_ALIGNMENT - 1)) & ~(CACHE_ALIGNMENT - 1)))
43#define ALIGN_PTR_DOWN(p)      ((void *)((FPTR)(p) & ~(CACHE_ALIGNMENT - 1)))
44
45
46
47/***************************************************************************
48    TYPE DEFINITIONS
49***************************************************************************/
50
51/* out-of-bounds codegen handlers */
52typedef struct _oob_handler oob_handler;
53struct _oob_handler
54{
55   oob_handler *      next;            /* next handler */
56   drccache_oob_func   callback;         /* callback function */
57   void *            param1;            /* 1st pointer parameter */
58   void *            param2;            /* 2nd pointer parameter */
59   void *            param3;            /* 3rd pointer parameter */
60};
61
62
63/* a linked list of free items */
64typedef struct _free_link free_link;
65struct _free_link
66{
67   free_link *         next;            /* pointer to the next guy */
68};
69
70
71/* cache state */
72struct _drccache
73{
74   /* core parameters */
75   drccodeptr         near;            /* pointer to the near part of the cache */
76   drccodeptr         neartop;         /* top of the near part of the cache */
77   drccodeptr         base;            /* base pointer to the compiler cache */
78   drccodeptr         top;            /* current top of cache */
79   drccodeptr         end;            /* end of cache memory */
80   drccodeptr         codegen;         /* start of generated code */
81   size_t            size;            /* size of the cache in bytes */
82
83   /* oob management */
84   oob_handler *      ooblist;         /* list of oob handlers */
85   oob_handler **      oobtail;         /* pointer to tail oob pointer */
86
87   /* free lists */
88   free_link *         free[MAX_PERMANENT_ALLOC / CACHE_ALIGNMENT];
89   free_link *         nearfree[MAX_PERMANENT_ALLOC / CACHE_ALIGNMENT];
90};
91
92
93
94/***************************************************************************
95    INITIALIZATION/TEARDOWN
96***************************************************************************/
97
98/*-------------------------------------------------
99    drccache_alloc - allocate the cache itself
100-------------------------------------------------*/
101
102drccache *drccache_alloc(size_t bytes)
103{
104   drccache cache, *cacheptr;
105
106   assert(bytes >= sizeof(cache) + NEAR_CACHE_SIZE);
107
108   /* build a local structure first */
109   memset(&cache, 0, sizeof(cache));
110   cache.near = osd_alloc_executable(bytes);
111   cache.neartop = cache.near;
112   cache.base = cache.near + NEAR_CACHE_SIZE;
113   cache.top = cache.base;
114   cache.end = cache.near + bytes;
115   cache.size = bytes;
116
117   /* now allocate the cache structure itself from that */
118   cacheptr = drccache_memory_alloc(&cache, sizeof(cache));
119   *cacheptr = cache;
120
121   /* return the allocated result */
122   return cacheptr;
123}
124
125
126/*-------------------------------------------------
127    drccache_free - free the cache
128-------------------------------------------------*/
129
130void drccache_free(drccache *cache)
131{
132   /* release the memory; this includes the cache object itself */
133   osd_free_executable(cache->near, cache->size);
134}
135
136
137
138/***************************************************************************
139    CACHE INFORMATION
140***************************************************************************/
141
142/*-------------------------------------------------
143    drccache_contains_pointer - return true if a
144    pointer is within the cache
145-------------------------------------------------*/
146
147int drccache_contains_pointer(drccache *cache, const void *ptr)
148{
149   return ((const drccodeptr)ptr >= cache->near && (const drccodeptr)ptr < cache->near + cache->size);
150}
151
152
153/*-------------------------------------------------
154    drccache_contains_near_pointer - return true
155    if a pointer is within the cache
156-------------------------------------------------*/
157
158int drccache_contains_near_pointer(drccache *cache, const void *ptr)
159{
160   return ((const drccodeptr)ptr >= cache->near && (const drccodeptr)ptr < cache->neartop);
161}
162
163
164/*-------------------------------------------------
165    drccache_near - return a pointer to the near
166    part of the cache
167-------------------------------------------------*/
168
169drccodeptr drccache_near(drccache *cache)
170{
171   return cache->near;
172}
173
174
175/*-------------------------------------------------
176    drccache_base - return a pointer to the base
177    of the cache
178-------------------------------------------------*/
179
180drccodeptr drccache_base(drccache *cache)
181{
182   return cache->base;
183}
184
185
186/*-------------------------------------------------
187    drccache_top - return the current top of the
188    cache
189-------------------------------------------------*/
190
191drccodeptr drccache_top(drccache *cache)
192{
193   return cache->top;
194}
195
196
197
198/***************************************************************************
199    MEMORY MANAGEMENT
200***************************************************************************/
201
202/*-------------------------------------------------
203    drccache_flush - flush the cache contents
204-------------------------------------------------*/
205
206void drccache_flush(drccache *cache)
207{
208   /* can't flush in the middle of codegen */
209   assert(cache->codegen == NULL);
210
211   /* just reset the top back to the base and re-seed */
212   cache->top = cache->base;
213}
214
215
216/*-------------------------------------------------
217    drccache_memory_alloc - allocate permanent
218    memory from the cache
219-------------------------------------------------*/
220
221void *drccache_memory_alloc(drccache *cache, size_t bytes)
222{
223   drccodeptr ptr;
224
225   assert(bytes > 0);
226
227   /* pick first from the free list */
228   if (bytes < MAX_PERMANENT_ALLOC)
229   {
230      free_link **linkptr = &cache->free[(bytes + CACHE_ALIGNMENT - 1) / CACHE_ALIGNMENT];
231      free_link *link = *linkptr;
232      if (link != NULL)
233      {
234         *linkptr = link->next;
235         return link;
236      }
237   }
238
239   /* if no space, we just fail */
240   ptr = ALIGN_PTR_DOWN(cache->end - bytes);
241   if (cache->top > ptr)
242      return NULL;
243
244   /* otherwise update the end of the cache */
245   cache->end = ptr;
246   return ptr;
247}
248
249
250/*-------------------------------------------------
251    drccache_memory_alloc_near - allocate
252    permanent memory from the near part of the
253    cache
254-------------------------------------------------*/
255
256void *drccache_memory_alloc_near(drccache *cache, size_t bytes)
257{
258   drccodeptr ptr;
259
260   assert(bytes > 0);
261
262   /* pick first from the free list */
263   if (bytes < MAX_PERMANENT_ALLOC)
264   {
265      free_link **linkptr = &cache->nearfree[(bytes + CACHE_ALIGNMENT - 1) / CACHE_ALIGNMENT];
266      free_link *link = *linkptr;
267      if (link != NULL)
268      {
269         *linkptr = link->next;
270         return link;
271      }
272   }
273
274   /* if no space, we just fail */
275   ptr = ALIGN_PTR_UP(cache->neartop);
276   if (ptr + bytes > cache->base)
277      return NULL;
278
279   /* otherwise update the top of the near part of the cache */
280   cache->neartop = ptr + bytes;
281   return ptr;
282}
283
284
285/*-------------------------------------------------
286    drccache_memory_free - release permanent
287    memory allocated from the cache
288-------------------------------------------------*/
289
290void drccache_memory_free(drccache *cache, void *memory, size_t bytes)
291{
292   free_link **linkptr;
293   free_link *link = memory;
294
295   assert(bytes < MAX_PERMANENT_ALLOC);
296   assert(((drccodeptr)memory >= cache->near && (drccodeptr)memory < cache->base) || ((drccodeptr)memory >= cache->end && (drccodeptr)memory < cache->near + cache->size));
297
298   /* determine which free list to add to */
299   if ((drccodeptr)memory < cache->base)
300      linkptr = &cache->nearfree[(bytes + CACHE_ALIGNMENT - 1) / CACHE_ALIGNMENT];
301   else
302      linkptr = &cache->free[(bytes + CACHE_ALIGNMENT - 1) / CACHE_ALIGNMENT];
303
304   /* link is into the free list for our size */
305   link->next = *linkptr;
306   *linkptr = link;
307}
308
309
310/*-------------------------------------------------
311    drccache_memory_alloc_temporary - allocate
312    temporary memory from the cache
313-------------------------------------------------*/
314
315void *drccache_memory_alloc_temporary(drccache *cache, size_t bytes)
316{
317   drccodeptr ptr = cache->top;
318
319   /* can't allocate in the middle of codegen */
320   assert(cache->codegen == NULL);
321
322   /* if no space, we just fail */
323   if (ptr + bytes >= cache->end)
324      return NULL;
325
326   /* otherwise, update the cache top */
327   cache->top = ALIGN_PTR_UP(ptr + bytes);
328   return ptr;
329}
330
331
332
333/***************************************************************************
334    CODE GENERATION
335***************************************************************************/
336
337/*-------------------------------------------------
338    drccache_begin_codegen - begin code
339    generation
340-------------------------------------------------*/
341
342drccodeptr *drccache_begin_codegen(drccache *cache, UINT32 reserve_bytes)
343{
344   drccodeptr ptr = cache->top;
345
346   /* can't restart in the middle of codegen */
347   assert(cache->codegen == NULL);
348   assert(cache->ooblist == NULL);
349
350   /* if still no space, we just fail */
351   if (ptr + reserve_bytes >= cache->end)
352      return NULL;
353
354   /* otherwise, return a pointer to the cache top */
355   cache->codegen = cache->top;
356   cache->oobtail = &cache->ooblist;
357   return &cache->top;
358}
359
360
361/*-------------------------------------------------
362    drccache_end_codegen - complete code
363    generation
364-------------------------------------------------*/
365
366drccodeptr drccache_end_codegen(drccache *cache)
367{
368   drccodeptr result = cache->codegen;
369
370   /* run the OOB handlers */
371   while (cache->ooblist != NULL)
372   {
373      /* remove us from the list */
374      oob_handler *oob = cache->ooblist;
375      cache->ooblist = oob->next;
376
377      /* call the callback */
378      (*oob->callback)(&cache->top, oob->param1, oob->param2, oob->param3);
379      assert(cache->top - cache->codegen < CODEGEN_MAX_BYTES);
380
381      /* release our memory */
382      drccache_memory_free(cache, oob, sizeof(*oob));
383   }
384
385   /* update the cache top */
386   cache->top = ALIGN_PTR_UP(cache->top);
387   cache->codegen = NULL;
388
389   return result;
390}
391
392
393/*-------------------------------------------------
394    drccache_request_oob_codegen - request
395    callback for out-of-band codegen
396-------------------------------------------------*/
397
398void drccache_request_oob_codegen(drccache *cache, drccache_oob_func callback, void *param1, void *param2, void *param3)
399{
400   oob_handler *oob;
401
402   assert(cache->codegen != NULL);
403
404   /* pull an item from the free list */
405   oob = drccache_memory_alloc(cache, sizeof(*oob));
406   assert(oob != NULL);
407
408   /* fill it in */
409   oob->next = NULL;
410   oob->callback = callback;
411   oob->param1 = param1;
412   oob->param2 = param2;
413   oob->param3 = param3;
414
415   /* add to the tail */
416   *cache->oobtail = oob;
417   cache->oobtail = &oob->next;
418}
src/emu/cpu/drccache.h
r0r2628
1/***************************************************************************
2
3    drccache.h
4
5    Universal dynamic recompiler cache management.
6
7    Copyright Aaron Giles
8    Released for general non-commercial use under the MAME license
9    Visit http://mamedev.org for licensing and usage restrictions.
10
11***************************************************************************/
12
13#ifndef __DRCCACHE_H__
14#define __DRCCACHE_H__
15
16
17
18/***************************************************************************
19    MACROS
20***************************************************************************/
21
22/* ensure that a given pointer is within the cache boundaries */
23#define assert_in_cache(c,p)      assert(drccache_contains_pointer(c, p))
24#define assert_in_near_cache(c,p)   assert(drccache_contains_near_pointer(c, p))
25
26
27
28/***************************************************************************
29    TYPE DEFINITIONS
30***************************************************************************/
31
32/* generic code pointer */
33typedef UINT8 *drccodeptr;
34
35/* opaque cache state */
36typedef struct _drccache drccache;
37
38/* out of band codegen callback */
39typedef void (*drccache_oob_func)(drccodeptr *codeptr, void *param1, void *param2, void *param3);
40
41
42
43/***************************************************************************
44    FUNCTION PROTOTYPES
45***************************************************************************/
46
47/* ----- initialization/teardown ----- */
48
49/* allocate the cache itself */
50drccache *drccache_alloc(size_t bytes);
51
52/* free the cache */
53void drccache_free(drccache *cache);
54
55
56
57/* ----- cache information ----- */
58
59/* return true if a pointer is within the cache */
60int drccache_contains_pointer(drccache *cache, const void *ptr);
61
62/* return true if a pointer is within the near area of the cache */
63int drccache_contains_near_pointer(drccache *cache, const void *ptr);
64
65/* return a pointer to the near part of the cache */
66drccodeptr drccache_near(drccache *cache);
67
68/* return a pointer to the base of the cache, which is where code generation starts */
69drccodeptr drccache_base(drccache *cache);
70
71/* return the current top of the cache, which is where the next code will be generated */
72drccodeptr drccache_top(drccache *cache);
73
74
75
76/* ----- memory management ----- */
77
78/* flush the cache contents */
79void drccache_flush(drccache *cache);
80
81/* allocate permanent memory from the cache */
82void *drccache_memory_alloc(drccache *cache, size_t bytes);
83
84/* allocate permanent memory from the near portion of the cache */
85void *drccache_memory_alloc_near(drccache *cache, size_t bytes);
86
87/* release permanent memory allocated from the cache */
88void drccache_memory_free(drccache *cache, void *memory, size_t bytes);
89
90/* allocate temporary memory from the cache (released on a reset) */
91void *drccache_memory_alloc_temporary(drccache *cache, size_t bytes);
92
93
94
95/* ----- code generation ----- */
96
97/* begin code generation */
98drccodeptr *drccache_begin_codegen(drccache *cache, UINT32 reserve_bytes);
99
100/* complete code generation */
101drccodeptr drccache_end_codegen(drccache *cache);
102
103/* request callback for out-of-band codegen */
104void drccache_request_oob_codegen(drccache *cache, drccache_oob_func callback, void *param1, void *param2, void *param3);
105
106
107#endif
src/emu/cpu/drcumlsh.h
r0r2628
1/***************************************************************************
2
3    drcumlsh.h
4
5    Shorthand definitions for the universal machine language.
6
7    Copyright Aaron Giles
8    Released for general non-commercial use under the MAME license
9    Visit http://mamedev.org for licensing and usage restrictions.
10
11***************************************************************************/
12
13#pragma once
14
15#ifndef __DRCUMLSH_H__
16#define __DRCUMLSH_H__
17
18#include "drcuml.h"
19
20
21
22/***************************************************************************
23    CONSTANTS
24***************************************************************************/
25
26/* shorthand for conditions */
27#define IF_Z      DRCUML_COND_Z
28#define IF_NZ      DRCUML_COND_NZ
29#define IF_S      DRCUML_COND_S
30#define IF_NS      DRCUML_COND_NS
31#define IF_C      DRCUML_COND_C
32#define IF_NC      DRCUML_COND_NC
33#define IF_V      DRCUML_COND_V
34#define IF_NV      DRCUML_COND_NV
35#define IF_U      DRCUML_COND_U
36#define IF_NU      DRCUML_COND_NU
37#define IF_E      DRCUML_COND_E
38#define IF_NE      DRCUML_COND_NE
39#define IF_A      DRCUML_COND_A
40#define IF_AE      DRCUML_COND_AE
41#define IF_B      DRCUML_COND_B
42#define IF_BE      DRCUML_COND_BE
43#define IF_G      DRCUML_COND_G
44#define IF_GE      DRCUML_COND_GE
45#define IF_L      DRCUML_COND_L
46#define IF_LE      DRCUML_COND_LE
47#define IF_ALWAYS   DRCUML_COND_ALWAYS
48
49/* shorthand for flags */
50#define FLAGS_ALLI   (DRCUML_FLAG_C | DRCUML_FLAG_V | DRCUML_FLAG_Z | DRCUML_FLAG_S)
51#define FLAGS_ALLF   (DRCUML_FLAG_C | DRCUML_FLAG_Z | DRCUML_FLAG_U)
52#define FLAGS_NONE   (0)
53
54/* flags needed for condition codes */
55#define FLAGS_Z      (DRCUML_FLAG_Z)
56#define FLAGS_NZ   (DRCUML_FLAG_Z)
57#define FLAGS_E      (DRCUML_FLAG_Z)
58#define FLAGS_NE   (DRCUML_FLAG_Z)
59#define FLAGS_S      (DRCUML_FLAG_S)
60#define FLAGS_NS   (DRCUML_FLAG_S)
61#define FLAGS_C      (DRCUML_FLAG_C)
62#define FLAGS_NC   (DRCUML_FLAG_C)
63#define FLAGS_V      (DRCUML_FLAG_V)
64#define FLAGS_NV   (DRCUML_FLAG_V)
65#define FLAGS_U      (DRCUML_FLAG_U)
66#define FLAGS_NU   (DRCUML_FLAG_U)
67#define FLAGS_A      (DRCUML_FLAG_C | DRCUML_FLAG_Z)
68#define FLAGS_BE   (DRCUML_FLAG_C | DRCUML_FLAG_Z)
69#define FLAGS_B      (DRCUML_FLAG_C)
70#define FLAGS_AE   (DRCUML_FLAG_C)
71#define FLAGS_G      (DRCUML_FLAG_S | DRCUML_FLAG_V | DRCUML_FLAG_Z)
72#define FLAGS_LE   (DRCUML_FLAG_S | DRCUML_FLAG_V | DRCUML_FLAG_Z)
73#define FLAGS_L      (DRCUML_FLAG_S | DRCUML_FLAG_V)
74#define FLAGS_GE   (DRCUML_FLAG_S | DRCUML_FLAG_V)
75
76
77
78/***************************************************************************
79    MACROS
80***************************************************************************/
81
82/* macros for wrapping parameters */
83#define NONE            DRCUML_PTYPE_NONE, 0
84#define IMM(x)            DRCUML_PTYPE_IMMEDIATE, (x)
85#define IREG(x)            DRCUML_PTYPE_INT_REGISTER, (DRCUML_REG_I0 + (x))
86#define FREG(x)            DRCUML_PTYPE_FLOAT_REGISTER, (DRCUML_REG_F0 + (x))
87#define MVAR(x)            DRCUML_PTYPE_MAPVAR, (DRCUML_MAPVAR_M0 + (x))
88#define MEM(x)            DRCUML_PTYPE_MEMORY, (FPTR)(x)
89
90
91
92/***************************************************************************
93    INLINE FUNCTIONS
94***************************************************************************/
95
96/* ----- Compile-time Opcodes ----- */
97#define UML_HANDLE(block, handle)                     do { drcuml_block_append_1(block, DRCUML_OP_HANDLE,  4, IF_ALWAYS,  MEM(handle)); } while (0)
98#define UML_HASH(block, mode, pc)                     do { drcuml_block_append_2(block, DRCUML_OP_HASH,    4, IF_ALWAYS,  IMM(mode), IMM(pc)); } while (0)
99#define UML_LABEL(block, label)                        do { drcuml_block_append_1(block, DRCUML_OP_LABEL,   4, IF_ALWAYS,  IMM(label)); } while (0)
100#define UML_COMMENT/*(block, format, ...)*/               drcuml_add_comment
101#define UML_MAPVAR(block, mapvar, value)               do { drcuml_block_append_2(block, DRCUML_OP_MAPVAR,  4, IF_ALWAYS,  mapvar, IMM(value)); } while (0)
102
103
104/* ----- Control Flow Operations ----- */
105#define UML_DEBUG(block, pc)                        do { drcuml_block_append_1(block, DRCUML_OP_DEBUG,   4, IF_ALWAYS,  pc); } while (0)
106
107#define UML_EXIT(block, param)                        do { drcuml_block_append_1(block, DRCUML_OP_EXIT,    4, IF_ALWAYS,  param); } while (0)
108#define UML_EXITc(block, cond, param)                  do { drcuml_block_append_1(block, DRCUML_OP_EXIT,    4, cond,       param); } while (0)
109
110#define UML_HASHJMP(block, mode, pc, handle)            do { drcuml_block_append_3(block, DRCUML_OP_HASHJMP, 4, IF_ALWAYS,  mode, pc, MEM(handle)); } while (0)
111
112#define UML_JMP(block, label)                        do { drcuml_block_append_1(block, DRCUML_OP_JMP,     4, IF_ALWAYS,  IMM(label)); } while (0)
113#define UML_JMPc(block, cond, label)                  do { drcuml_block_append_1(block, DRCUML_OP_JMP,     4, cond,       IMM(label)); } while (0)
114
115#define UML_JMPH(block, handle)                        do { drcuml_block_append_1(block, DRCUML_OP_JMPH,    4, IF_ALWAYS,  MEM(handle)); } while (0)
116#define UML_JMPHc(block, cond, handle)                  do { drcuml_block_append_1(block, DRCUML_OP_JMPH,    4, cond,       MEM(handle)); } while (0)
117#define UML_EXH(block, handle, param)                  do { drcuml_block_append_2(block, DRCUML_OP_EXH,     4, IF_ALWAYS,  MEM(handle), param); } while (0)
118#define UML_EXHc(block, cond, handle, param)            do { drcuml_block_append_2(block, DRCUML_OP_EXH,     4, cond,       MEM(handle), param); } while (0)
119#define UML_CALLH(block, handle)                     do { drcuml_block_append_1(block, DRCUML_OP_CALLH,   4, IF_ALWAYS,  MEM(handle)); } while (0)
120#define UML_CALLHc(block, cond, handle)                  do { drcuml_block_append_1(block, DRCUML_OP_CALLH,   4, cond,       MEM(handle)); } while (0)
121#define UML_RET(block)                              do { drcuml_block_append_0(block, DRCUML_OP_RET,     4, IF_ALWAYS); } while (0)
122#define UML_RETc(block, cond)                        do { drcuml_block_append_0(block, DRCUML_OP_RET,     4, cond); } while (0)
123
124#define UML_CALLC(block, func, ptr)                     do { drcuml_block_append_2(block, DRCUML_OP_CALLC,   4, IF_ALWAYS,  MEM(func), MEM(ptr)); } while (0)
125#define UML_CALLCc(block, cond, func, ptr)               do { drcuml_block_append_2(block, DRCUML_OP_CALLC,   4, cond,       MEM(func), MEM(ptr)); } while (0)
126
127#define UML_RECOVER(block, dst, mapvar)                  do { drcuml_block_append_2(block, DRCUML_OP_RECOVER, 4, IF_ALWAYS,  dst, mapvar); } while (0)
128
129
130/* ----- Internal Register Operations ----- */
131#define UML_SETMODE(block, mode)                     do { drcuml_block_append_1(block, DRCUML_OP_SETMODE, 4, IF_ALWAYS,  mode); } while (0)
132#define UML_GETMODE(block, dst)                        do { drcuml_block_append_1(block, DRCUML_OP_GETMODE, 4, IF_ALWAYS,  dst); } while (0)
133#define UML_SETFMOD(block, mode)                     do { drcuml_block_append_1(block, DRCUML_OP_SETFMOD, 4, IF_ALWAYS,  mode); } while (0)
134#define UML_GETFMOD(block, dst)                        do { drcuml_block_append_1(block, DRCUML_OP_GETFMOD, 4, IF_ALWAYS,  dst); } while (0)
135#define UML_SUBICNT(block, delta, param)               do { drcuml_block_append_2(block, DRCUML_OP_SUBICNT, 4, IF_ALWAYS,  delta, param); } while (0)
136#define UML_GETEXP(block, dst)                        do { drcuml_block_append_1(block, DRCUML_OP_GETEXP,  4, IF_ALWAYS,  dst); } while (0)
137
138
139/* ----- 32-Bit Integer Operations ----- */
140#define UML_LOAD1U(block, dst, base, index)               do { drcuml_block_append_3(block, DRCUML_OP_LOAD1U,  4, IF_ALWAYS,  dst, MEM(base), index); } while (0)
141#define UML_LOAD1S(block, dst, base, index)               do { drcuml_block_append_3(block, DRCUML_OP_LOAD1S,  4, IF_ALWAYS,  dst, MEM(base), index); } while (0)
142#define UML_LOAD2U(block, dst, base, index)               do { drcuml_block_append_3(block, DRCUML_OP_LOAD2U,  4, IF_ALWAYS,  dst, MEM(base), index); } while (0)
143#define UML_LOAD2S(block, dst, base, index)               do { drcuml_block_append_3(block, DRCUML_OP_LOAD2S,  4, IF_ALWAYS,  dst, MEM(base), index); } while (0)
144#define UML_LOAD4(block, dst, base, index)               do { drcuml_block_append_3(block, DRCUML_OP_LOAD4U,  4, IF_ALWAYS,  dst, MEM(base), index); } while (0)
145
146#define UML_STORE1(block, base, index, src1)            do { drcuml_block_append_3(block, DRCUML_OP_STORE1,  4, IF_ALWAYS,  MEM(base), index, src1); } while (0)
147#define UML_STORE2(block, base, index, src1)            do { drcuml_block_append_3(block, DRCUML_OP_STORE2,  4, IF_ALWAYS,  MEM(base), index, src1); } while (0)
148#define UML_STORE4(block, base, index, src1)            do { drcuml_block_append_3(block, DRCUML_OP_STORE4,  4, IF_ALWAYS,  MEM(base), index, src1); } while (0)
149
150#define UML_READ1U(block, dst, space, src1)               do { drcuml_block_append_3(block, DRCUML_OP_READ1U,  4, IF_ALWAYS,  dst, IMM(ADDRESS_SPACE_##space), src1); } while (0)
151#define UML_READ1S(block, dst, space, src1)               do { drcuml_block_append_3(block, DRCUML_OP_READ1S,  4, IF_ALWAYS,  dst, IMM(ADDRESS_SPACE_##space), src1); } while (0)
152#define UML_READ2U(block, dst, space, src1)               do { drcuml_block_append_3(block, DRCUML_OP_READ2U,  4, IF_ALWAYS,  dst, IMM(ADDRESS_SPACE_##space), src1); } while (0)
153#define UML_READ2S(block, dst, space, src1)               do { drcuml_block_append_3(block, DRCUML_OP_READ2S,  4, IF_ALWAYS,  dst, IMM(ADDRESS_SPACE_##space), src1); } while (0)
154#define UML_READ4(block, dst, space, src1)               do { drcuml_block_append_3(block, DRCUML_OP_READ4U,  4, IF_ALWAYS,  dst, IMM(ADDRESS_SPACE_##space), src1); } while (0)
155#define UML_READ4M(block, dst, space, src1, mask)         do { drcuml_block_append_4(block, DRCUML_OP_READ4M,  4, IF_ALWAYS,  dst, IMM(ADDRESS_SPACE_##space), src1, mask); } while (0)
156
157#define UML_WRITE1(block, space, dst, src1)               do { drcuml_block_append_3(block, DRCUML_OP_WRITE1,  4, IF_ALWAYS,  IMM(ADDRESS_SPACE_##space), dst, src1); } while (0)
158#define UML_WRITE2(block, space, dst, src1)               do { drcuml_block_append_3(block, DRCUML_OP_WRITE2,  4, IF_ALWAYS,  IMM(ADDRESS_SPACE_##space), dst, src1); } while (0)
159#define UML_WRITE4(block, space, dst, src1)               do { drcuml_block_append_3(block, DRCUML_OP_WRITE4,  4, IF_ALWAYS,  IMM(ADDRESS_SPACE_##space), dst, src1); } while (0)
160#define UML_WRIT4M(block, space, dst, mask, src1)         do { drcuml_block_append_4(block, DRCUML_OP_WRIT4M,  4, IF_ALWAYS,  IMM(ADDRESS_SPACE_##space), dst, mask, src1); } while (0)
161
162#define UML_FLAGS(block, dst, mask, table)               do { drcuml_block_append_3(block, DRCUML_OP_FLAGS,   4, IF_ALWAYS,  dst, IMM(mask), MEM(table)); } while (0)
163
164#define UML_MOV(block, dst, src1)                     do { drcuml_block_append_2(block, DRCUML_OP_MOV,     4, IF_ALWAYS,  dst, src1); } while (0)
165#define UML_MOVc(block, cond, dst, src1)               do { drcuml_block_append_2(block, DRCUML_OP_MOV,     4, cond,       dst, src1); } while (0)
166
167#define UML_ZEXT1(block, dst, src1)                     do { drcuml_block_append_2(block, DRCUML_OP_ZEXT1,   4, IF_ALWAYS,  dst, src1); } while (0)
168#define UML_ZEXT2(block, dst, src1)                     do { drcuml_block_append_2(block, DRCUML_OP_ZEXT2,   4, IF_ALWAYS,  dst, src1); } while (0)
169
170#define UML_SEXT1(block, dst, src1)                     do { drcuml_block_append_2(block, DRCUML_OP_SEXT1,   4, IF_ALWAYS,  dst, src1); } while (0)
171#define UML_SEXT2(block, dst, src1)                     do { drcuml_block_append_2(block, DRCUML_OP_SEXT2,   4, IF_ALWAYS,  dst, src1); } while (0)
172
173#define UML_NEG(block, dst, src1)                     do { drcuml_block_append_2(block, DRCUML_OP_NEG,     4, FLAGS_NONE, dst, src1); } while (0)
174#define UML_NEGf(block, dst, src1, flags)               do { drcuml_block_append_2(block, DRCUML_OP_NEG,     4, flags,      dst, src1); } while (0)
175
176#define UML_ADD(block, dst, src1, src2)                  do { drcuml_block_append_3(block, DRCUML_OP_ADD,     4, FLAGS_NONE, dst, src1, src2); } while (0)
177#define UML_ADDf(block, dst, src1, src2, flags)            do { drcuml_block_append_3(block, DRCUML_OP_ADD,     4, flags,      dst, src1, src2); } while (0)
178#define UML_ADDC(block, dst, src1, src2)               do { drcuml_block_append_3(block, DRCUML_OP_ADDC,    4, FLAGS_NONE, dst, src1, src2); } while (0)
179#define UML_ADDCf(block, dst, src1, src2, flags)         do { drcuml_block_append_3(block, DRCUML_OP_ADDC,    4, flags,      dst, src1, src2); } while (0)
180
181#define UML_SUB(block, dst, src1, src2)                  do { drcuml_block_append_3(block, DRCUML_OP_SUB,     4, FLAGS_NONE, dst, src1, src2); } while (0)
182#define UML_SUBf(block, dst, src1, src2, flags)            do { drcuml_block_append_3(block, DRCUML_OP_SUB,     4, flags,      dst, src1, src2); } while (0)
183#define UML_SUBB(block, dst, src1, src2)               do { drcuml_block_append_3(block, DRCUML_OP_SUBB,    4, FLAGS_NONE, dst, src1, src2); } while (0)
184#define UML_SUBBf(block, dst, src1, src2, flags)         do { drcuml_block_append_3(block, DRCUML_OP_SUBB,    4, flags,      dst, src1, src2); } while (0)
185
186#define UML_CMP(block, src1, src2)                     do { drcuml_block_append_2(block, DRCUML_OP_CMP,     4, FLAGS_ALLI, src1, src2); } while (0)
187#define UML_CMPf(block, src1, src2, flags)               do { drcuml_block_append_2(block, DRCUML_OP_CMP,     4, flags,      src1, src2); } while (0)
188
189#define UML_MULU(block, dst, edst, src1, src2)            do { drcuml_block_append_4(block, DRCUML_OP_MULU,    4, FLAGS_NONE, dst, edst, src1, src2); } while (0)
190#define UML_MULUf(block, dst, edst, src1, src2, flags)      do { drcuml_block_append_4(block, DRCUML_OP_MULU,    4, flags,      dst, edst, src1, src2); } while (0)
191#define UML_MULS(block, dst, edst, src1, src2)            do { drcuml_block_append_4(block, DRCUML_OP_MULS,    4, FLAGS_NONE, dst, edst, src1, src2); } while (0)
192#define UML_MULSf(block, dst, edst, src1, src2, flags)      do { drcuml_block_append_4(block, DRCUML_OP_MULS,    4, flags,      dst, edst, src1, src2); } while (0)
193
194#define UML_DIVU(block, dst, edst, src1, src2)            do { drcuml_block_append_4(block, DRCUML_OP_DIVU,    4, FLAGS_NONE, dst, edst, src1, src2); } while (0)
195#define UML_DIVUf(block, dst, edst, src1, src2, flags)      do { drcuml_block_append_4(block, DRCUML_OP_DIVU,    4, flags,      dst, edst, src1, src2); } while (0)
196#define UML_DIVS(block, dst, edst, src1, src2)            do { drcuml_block_append_4(block, DRCUML_OP_DIVS,    4, FLAGS_NONE, dst, edst, src1, src2); } while (0)
197#define UML_DIVSf(block, dst, edst, src1, src2, flags)      do { drcuml_block_append_4(block, DRCUML_OP_DIVS,    4, flags,      dst, edst, src1, src2); } while (0)
198
199#define UML_AND(block, dst, src1, src2)                  do { drcuml_block_append_3(block, DRCUML_OP_AND,     4, FLAGS_NONE, dst, src1, src2); } while (0)
200#define UML_ANDf(block, dst, src1, src2, flags)            do { drcuml_block_append_3(block, DRCUML_OP_AND,     4, flags,      dst, src1, src2); } while (0)
201
202#define UML_TEST(block, src1, src2)                     do { drcuml_block_append_2(block, DRCUML_OP_TEST,    4, FLAGS_S|FLAGS_Z,src1, src2); } while (0)
203#define UML_TESTf(block, src1, src2, flags)               do { drcuml_block_append_2(block, DRCUML_OP_TEST,    4, flags,      src1, src2); } while (0)
204
205#define UML_OR(block, dst, src1, src2)                  do { drcuml_block_append_3(block, DRCUML_OP_OR,      4, FLAGS_NONE, dst, src1, src2); } while (0)
206#define UML_ORf(block, dst, src1, src2, flags)            do { drcuml_block_append_3(block, DRCUML_OP_OR,      4, flags,      dst, src1, src2); } while (0)
207
208#define UML_XOR(block, dst, src1, src2)                  do { drcuml_block_append_3(block, DRCUML_OP_XOR,     4, FLAGS_NONE, dst, src1, src2); } while (0)
209#define UML_XORf(block, dst, src1, src2, flags)            do { drcuml_block_append_3(block, DRCUML_OP_XOR,     4, flags,      dst, src1, src2); } while (0)
210
211#define UML_SHL(block, dst, src, count)                  do { drcuml_block_append_3(block, DRCUML_OP_SHL,     4, FLAGS_NONE, dst, src, count); } while (0)
212#define UML_SHLf(block, dst, src, count, flags)            do { drcuml_block_append_3(block, DRCUML_OP_SHL,     4, flags,      dst, src, count); } while (0)
213
214#define UML_SHR(block, dst, src, count)                  do { drcuml_block_append_3(block, DRCUML_OP_SHR,     4, FLAGS_NONE, dst, src, count); } while (0)
215#define UML_SHRf(block, dst, src, count, flags)            do { drcuml_block_append_3(block, DRCUML_OP_SHR,     4, flags,      dst, src, count); } while (0)
216
217#define UML_SAR(block, dst, src, count)                  do { drcuml_block_append_3(block, DRCUML_OP_SAR,     4, FLAGS_NONE, dst, src, count); } while (0)
218#define UML_SARf(block, dst, src, count, flags)            do { drcuml_block_append_3(block, DRCUML_OP_SAR,     4, flags,      dst, src, count); } while (0)
219
220#define UML_ROL(block, dst, src, count)                  do { drcuml_block_append_3(block, DRCUML_OP_ROL,     4, FLAGS_NONE, dst, src, count); } while (0)
221#define UML_ROLf(block, dst, src, count, flags)            do { drcuml_block_append_3(block, DRCUML_OP_ROL,     4, flags,      dst, src, count); } while (0)
222#define UML_ROLC(block, dst, src, count)               do { drcuml_block_append_3(block, DRCUML_OP_ROLC,    4, FLAGS_NONE, dst, src, count); } while (0)
223#define UML_ROLCf(block, dst, src, count, flags)         do { drcuml_block_append_3(block, DRCUML_OP_ROLC,    4, flags,      dst, src, count); } while (0)
224
225#define UML_ROR(block, dst, src, count)                  do { drcuml_block_append_3(block, DRCUML_OP_ROR,     4, FLAGS_NONE, dst, src, count); } while (0)
226#define UML_RORf(block, dst, src, count, flags)            do { drcuml_block_append_3(block, DRCUML_OP_ROR,     4, flags,      dst, src, count); } while (0)
227#define UML_RORC(block, dst, src, count)               do { drcuml_block_append_3(block, DRCUML_OP_RORC,    4, FLAGS_NONE, dst, src, count); } while (0)
228#define UML_RORCf(block, dst, src, count, flags)         do { drcuml_block_append_3(block, DRCUML_OP_RORC,    4, flags,      dst, src, count); } while (0)
229
230
231/* ----- 64-Bit Integer Operations ----- */
232#define UML_DLOAD1U(block, dst, base, index)            do { drcuml_block_append_3(block, DRCUML_OP_LOAD1U,  8, IF_ALWAYS,  dst, MEM(base), index); } while (0)
233#define UML_DLOAD1S(block, dst, base, index)            do { drcuml_block_append_3(block, DRCUML_OP_LOAD1S,  8, IF_ALWAYS,  dst, MEM(base), index); } while (0)
234#define UML_DLOAD2U(block, dst, base, index)            do { drcuml_block_append_3(block, DRCUML_OP_LOAD2U,  8, IF_ALWAYS,  dst, MEM(base), index); } while (0)
235#define UML_DLOAD2S(block, dst, base, index)            do { drcuml_block_append_3(block, DRCUML_OP_LOAD2S,  8, IF_ALWAYS,  dst, MEM(base), index); } while (0)
236#define UML_DLOAD4U(block, dst, base, index)            do { drcuml_block_append_3(block, DRCUML_OP_LOAD4U,  8, IF_ALWAYS,  dst, MEM(base), index); } while (0)
237#define UML_DLOAD4S(block, dst, base, index)            do { drcuml_block_append_3(block, DRCUML_OP_LOAD4S,  8, IF_ALWAYS,  dst, MEM(base), index); } while (0)
238#define UML_DLOAD8(block, dst, base, index)               do { drcuml_block_append_3(block, DRCUML_OP_LOAD8U,  8, IF_ALWAYS,  dst, MEM(base), index); } while (0)
239
240#define UML_DSTORE1(block, base, index, src1)            do { drcuml_block_append_3(block, DRCUML_OP_STORE1,  8, IF_ALWAYS,  MEM(base), index, src1); } while (0)
241#define UML_DSTORE2(block, base, index, src1)            do { drcuml_block_append_3(block, DRCUML_OP_STORE2,  8, IF_ALWAYS,  MEM(base), index, src1); } while (0)
242#define UML_DSTORE4(block, base, index, src1)            do { drcuml_block_append_3(block, DRCUML_OP_STORE4,  8, IF_ALWAYS,  MEM(base), index, src1); } while (0)
243#define UML_DSTORE8(block, base, index, src1)            do { drcuml_block_append_3(block, DRCUML_OP_STORE8,  8, IF_ALWAYS,  MEM(base), index, src1); } while (0)
244
245#define UML_DREAD1U(block, dst, space, src1)            do { drcuml_block_append_3(block, DRCUML_OP_READ1U,  8, IF_ALWAYS,  dst, IMM(ADDRESS_SPACE_##space), src1); } while (0)
246#define UML_DREAD1S(block, dst, space, src1)            do { drcuml_block_append_3(block, DRCUML_OP_READ1S,  8, IF_ALWAYS,  dst, IMM(ADDRESS_SPACE_##space), src1); } while (0)
247#define UML_DREAD2U(block, dst, space, src1)            do { drcuml_block_append_3(block, DRCUML_OP_READ2U,  8, IF_ALWAYS,  dst, IMM(ADDRESS_SPACE_##space), src1); } while (0)
248#define UML_DREAD2S(block, dst, space, src1)            do { drcuml_block_append_3(block, DRCUML_OP_READ2S,  8, IF_ALWAYS,  dst, IMM(ADDRESS_SPACE_##space), src1); } while (0)
249#define UML_DREAD2M(block, dst, space, src1, mask)         do { drcuml_block_append_4(block, DRCUML_OP_READ2M,  8, IF_ALWAYS,  dst, IMM(ADDRESS_SPACE_##space), src1, mask); } while (0)
250#define UML_DREAD4U(block, dst, space, src1)            do { drcuml_block_append_3(block, DRCUML_OP_READ4U,  8, IF_ALWAYS,  dst, IMM(ADDRESS_SPACE_##space), src1); } while (0)
251#define UML_DREAD4S(block, dst, space, src1)            do { drcuml_block_append_3(block, DRCUML_OP_READ4S,  8, IF_ALWAYS,  dst, IMM(ADDRESS_SPACE_##space), src1); } while (0)
252#define UML_DREAD4M(block, dst, space, src1, mask)         do { drcuml_block_append_4(block, DRCUML_OP_READ4M,  8, IF_ALWAYS,  dst, IMM(ADDRESS_SPACE_##space), src1, mask); } while (0)
253#define UML_DREAD8(block, dst, space, src1)               do { drcuml_block_append_3(block, DRCUML_OP_READ8U,  8, IF_ALWAYS,  dst, IMM(ADDRESS_SPACE_##space), src1); } while (0)
254#define UML_DREAD8M(block, dst, space, src1, mask)         do { drcuml_block_append_4(block, DRCUML_OP_READ8M,  8, IF_ALWAYS,  dst, IMM(ADDRESS_SPACE_##space), src1, mask); } while (0)
255
256#define UML_DWRITE1(block, space, dst, src1)            do { drcuml_block_append_3(block, DRCUML_OP_WRITE1,  8, IF_ALWAYS,  IMM(ADDRESS_SPACE_##space), dst, src1); } while (0)
257#define UML_DWRITE2(block, space, dst, src1)            do { drcuml_block_append_3(block, DRCUML_OP_WRITE2,  8, IF_ALWAYS,  IMM(ADDRESS_SPACE_##space), dst, src1); } while (0)
258#define UML_DWRIT2M(block, space, dst, mask, src1)         do { drcuml_block_append_4(block, DRCUML_OP_WRIT2M,  8, IF_ALWAYS,  IMM(ADDRESS_SPACE_##space), dst, mask, src1); } while (0)
259#define UML_DWRITE4(block, space, dst, src1)            do { drcuml_block_append_3(block, DRCUML_OP_WRITE4,  8, IF_ALWAYS,  IMM(ADDRESS_SPACE_##space), dst, src1); } while (0)
260#define UML_DWRIT4M(block, space, dst, mask, src1)         do { drcuml_block_append_4(block, DRCUML_OP_WRIT4M,  8, IF_ALWAYS,  IMM(ADDRESS_SPACE_##space), dst, mask, src1); } while (0)
261#define UML_DWRITE8(block, space, dst, src1)            do { drcuml_block_append_3(block, DRCUML_OP_WRITE8,  8, IF_ALWAYS,  IMM(ADDRESS_SPACE_##space), dst, src1); } while (0)
262#define UML_DWRIT8M(block, space, dst, mask, src1)         do { drcuml_block_append_4(block, DRCUML_OP_WRIT8M,  8, IF_ALWAYS,  IMM(ADDRESS_SPACE_##space), dst, mask, src1); } while (0)
263
264#define UML_DFLAGS(block, dst, mask, table)               do { drcuml_block_append_3(block, DRCUML_OP_FLAGS,   8, IF_ALWAYS,  dst, IMM(mask), MEM(table)); } while (0)
265
266#define UML_DMOV(block, dst, src1)                     do { drcuml_block_append_2(block, DRCUML_OP_MOV,     8, IF_ALWAYS,  dst, src1); } while (0)
267#define UML_DMOVc(block, cond, dst, src1)               do { drcuml_block_append_2(block, DRCUML_OP_MOV,     8, cond,       dst, src1); } while (0)
268
269#define UML_DZEXT1(block, dst, src1)                  do { drcuml_block_append_2(block, DRCUML_OP_ZEXT1,   8, IF_ALWAYS,  dst, src1); } while (0)
270#define UML_DZEXT2(block, dst, src1)                  do { drcuml_block_append_2(block, DRCUML_OP_ZEXT2,   8, IF_ALWAYS,  dst, src1); } while (0)
271#define UML_DZEXT4(block, dst, src1)                  do { drcuml_block_append_2(block, DRCUML_OP_ZEXT4,   8, IF_ALWAYS,  dst, src1); } while (0)
272
273#define UML_DSEXT1(block, dst, src1)                  do { drcuml_block_append_2(block, DRCUML_OP_SEXT1,   8, IF_ALWAYS,  dst, src1); } while (0)
274#define UML_DSEXT2(block, dst, src1)                  do { drcuml_block_append_2(block, DRCUML_OP_SEXT2,   8, IF_ALWAYS,  dst, src1); } while (0)
275#define UML_DSEXT4(block, dst, src1)                  do { drcuml_block_append_2(block, DRCUML_OP_SEXT4,   8, IF_ALWAYS,  dst, src1); } while (0)
276
277#define UML_DNEG(block, dst, src1)                     do { drcuml_block_append_2(block, DRCUML_OP_NEG,     8, FLAGS_NONE, dst, src1); } while (0)
278#define UML_DNEGf(block, dst, src1, flags)               do { drcuml_block_append_2(block, DRCUML_OP_NEG,     8, flags,      dst, src1); } while (0)
279
280#define UML_DADD(block, dst, src1, src2)               do { drcuml_block_append_3(block, DRCUML_OP_ADD,     8, FLAGS_NONE, dst, src1, src2); } while (0)
281#define UML_DADDf(block, dst, src1, src2, flags)         do { drcuml_block_append_3(block, DRCUML_OP_ADD,     8, flags,      dst, src1, src2); } while (0)
282#define UML_DADDC(block, dst, src1, src2)               do { drcuml_block_append_3(block, DRCUML_OP_ADDC,    8, FLAGS_NONE, dst, src1, src2); } while (0)
283#define UML_DADDCf(block, dst, src1, src2, flags)         do { drcuml_block_append_3(block, DRCUML_OP_ADDC,    8, flags,      dst, src1, src2); } while (0)
284
285#define UML_DSUB(block, dst, src1, src2)               do { drcuml_block_append_3(block, DRCUML_OP_SUB,     8, FLAGS_NONE, dst, src1, src2); } while (0)
286#define UML_DSUBf(block, dst, src1, src2, flags)         do { drcuml_block_append_3(block, DRCUML_OP_SUB,     8, flags,      dst, src1, src2); } while (0)
287#define UML_DSUBB(block, dst, src1, src2)               do { drcuml_block_append_3(block, DRCUML_OP_SUBB,    8, FLAGS_NONE, dst, src1, src2); } while (0)
288#define UML_DSUBBf(block, dst, src1, src2, flags)         do { drcuml_block_append_3(block, DRCUML_OP_SUBB,    8, flags,      dst, src1, src2); } while (0)
289
290#define UML_DCMP(block, src1, src2)                     do { drcuml_block_append_2(block, DRCUML_OP_CMP,     8, FLAGS_ALLI, src1, src2); } while (0)
291#define UML_DCMPf(block, src1, src2, flags)               do { drcuml_block_append_2(block, DRCUML_OP_CMP,     8, flags,      src1, src2); } while (0)
292
293#define UML_DMULU(block, dst, edst, src1, src2)            do { drcuml_block_append_4(block, DRCUML_OP_MULU,    8, FLAGS_NONE, dst, edst, src1, src2); } while (0)
294#define UML_DMULUf(block, dst, edst, src1, src2, flags)      do { drcuml_block_append_4(block, DRCUML_OP_MULU,    8, flags,      dst, edst, src1, src2); } while (0)
295#define UML_DMULS(block, dst, edst, src1, src2)            do { drcuml_block_append_4(block, DRCUML_OP_MULS,    8, FLAGS_NONE, dst, edst, src1, src2); } while (0)
296#define UML_DMULSf(block, dst, edst, src1, src2, flags)      do { drcuml_block_append_4(block, DRCUML_OP_MULS,    8, flags,      dst, edst, src1, src2); } while (0)
297
298#define UML_DDIVU(block, dst, edst, src1, src2)            do { drcuml_block_append_4(block, DRCUML_OP_DIVU,    8, FLAGS_NONE, dst, edst, src1, src2); } while (0)
299#define UML_DDIVUf(block, dst, edst, src1, src2, flags)      do { drcuml_block_append_4(block, DRCUML_OP_DIVU,    8, flags,      dst, edst, src1, src2); } while (0)
300#define UML_DDIVS(block, dst, edst, src1, src2)            do { drcuml_block_append_4(block, DRCUML_OP_DIVS,    8, FLAGS_NONE, dst, edst, src1, src2); } while (0)
301#define UML_DDIVSf(block, dst, edst, src1, src2, flags)      do { drcuml_block_append_4(block, DRCUML_OP_DIVS,    8, flags,      dst, edst, src1, src2); } while (0)
302
303#define UML_DAND(block, dst, src1, src2)               do { drcuml_block_append_3(block, DRCUML_OP_AND,     8, FLAGS_NONE, dst, src1, src2); } while (0)
304#define UML_DANDf(block, dst, src1, src2, flags)         do { drcuml_block_append_3(block, DRCUML_OP_AND,     8, flags,      dst, src1, src2); } while (0)
305
306#define UML_DTEST(block, src1, src2)                  do { drcuml_block_append_2(block, DRCUML_OP_TEST,    8, FLAGS_S|FLAGS_Z,src1, src2); } while (0)
307#define UML_DTESTf(block, src1, src2, flags)            do { drcuml_block_append_2(block, DRCUML_OP_TEST,    8, flags,      src1, src2); } while (0)
308
309#define UML_DOR(block, dst, src1, src2)                  do { drcuml_block_append_3(block, DRCUML_OP_OR,      8, FLAGS_NONE, dst, src1, src2); } while (0)
310#define UML_DORf(block, dst, src1, src2, flags)            do { drcuml_block_append_3(block, DRCUML_OP_OR,      8, flags,      dst, src1, src2); } while (0)
311
312#define UML_DXOR(block, dst, src1, src2)               do { drcuml_block_append_3(block, DRCUML_OP_XOR,     8, FLAGS_NONE, dst, src1, src2); } while (0)
313#define UML_DXORf(block, dst, src1, src2, flags)         do { drcuml_block_append_3(block, DRCUML_OP_XOR,     8, flags,      dst, src1, src2); } while (0)
314
315#define UML_DSHL(block, dst, src, count)               do { drcuml_block_append_3(block, DRCUML_OP_SHL,     8, FLAGS_NONE, dst, src, count); } while (0)
316#define UML_DSHLf(block, dst, src, count, flags)         do { drcuml_block_append_3(block, DRCUML_OP_SHL,     8, flags,      dst, src, count); } while (0)
317
318#define UML_DSHR(block, dst, src, count)               do { drcuml_block_append_3(block, DRCUML_OP_SHR,     8, FLAGS_NONE, dst, src, count); } while (0)
319#define UML_DSHRf(block, dst, src, count, flags)         do { drcuml_block_append_3(block, DRCUML_OP_SHR,     8, flags,      dst, src, count); } while (0)
320
321#define UML_DSAR(block, dst, src, count)               do { drcuml_block_append_3(block, DRCUML_OP_SAR,     8, FLAGS_NONE, dst, src, count); } while (0)
322#define UML_DSARf(block, dst, src, count, flags)         do { drcuml_block_append_3(block, DRCUML_OP_SAR,     8, flags,      dst, src, count); } while (0)
323
324#define UML_DROL(block, dst, src, count)               do { drcuml_block_append_3(block, DRCUML_OP_ROL,     8, FLAGS_NONE, dst, src, count); } while (0)
325#define UML_DROLf(block, dst, src, count, flags)         do { drcuml_block_append_3(block, DRCUML_OP_ROL,     8, flags,      dst, src, count); } while (0)
326#define UML_DROLC(block, dst, src, count)               do { drcuml_block_append_3(block, DRCUML_OP_ROLC,    8, FLAGS_NONE, dst, src, count); } while (0)
327#define UML_DROLCf(block, dst, src, count, flags)         do { drcuml_block_append_3(block, DRCUML_OP_ROLC,    8, flags,      dst, src, count); } while (0)
328
329#define UML_DROR(block, dst, src, count)               do { drcuml_block_append_3(block, DRCUML_OP_ROR,     8, FLAGS_NONE, dst, src, count); } while (0)
330#define UML_DRORf(block, dst, src, count, flags)         do { drcuml_block_append_3(block, DRCUML_OP_ROR,     8, flags,      dst, src, count); } while (0)
331#define UML_DRORC(block, dst, src, count)               do { drcuml_block_append_3(block, DRCUML_OP_RORC,    8, FLAGS_NONE, dst, src, count); } while (0)
332#define UML_DRORCf(block, dst, src, count, flags)         do { drcuml_block_append_3(block, DRCUML_OP_RORC,    8, flags,      dst, src, count); } while (0)
333
334
335/* ----- 32-bit Floating Point Arithmetic Operations ----- */
336#define UML_FSLOAD(block, dst, base, index)               do { drcuml_block_append_3(block, DRCUML_OP_FLOAD,   4, IF_ALWAYS,  dst, MEM(base), index); } while (0)
337#define UML_FSSTORE(block, base, index, src1)            do { drcuml_block_append_3(block, DRCUML_OP_FSTORE,  4, IF_ALWAYS,  MEM(base), index, src1); } while (0)
338
339#define UML_FSREAD(block, dst, space, src1)               do { drcuml_block_append_3(block, DRCUML_OP_FREAD,   4, IF_ALWAYS,  dst, IMM(ADDRESS_SPACE_##space), src1); } while (0)
340#define UML_FSWRITE(block, space, dst, src1)            do { drcuml_block_append_3(block, DRCUML_OP_FWRITE,  4, IF_ALWAYS,  IMM(ADDRESS_SPACE_##space), dst, src1); } while (0)
341
342#define UML_FSMOV(block, dst, src1)                     do { drcuml_block_append_2(block, DRCUML_OP_FMOV,    4, IF_ALWAYS,  dst, src1); } while (0)
343#define UML_FSMOVc(block, cond, dst, src1)               do { drcuml_block_append_2(block, DRCUML_OP_FMOV,    4, cond,       dst, src1); } while (0)
344
345#define UML_FSTOI4(block, dst, src1)                  do { drcuml_block_append_2(block, DRCUML_OP_FTOI4,   4, IF_ALWAYS,  dst, src1); } while (0)
346#define UML_FSTOI4T(block, dst, src1)                  do { drcuml_block_append_2(block, DRCUML_OP_FTOI4T,  4, IF_ALWAYS,  dst, src1); } while (0)
347#define UML_FSTOI4R(block, dst, src1)                  do { drcuml_block_append_2(block, DRCUML_OP_FTOI4R,  4, IF_ALWAYS,  dst, src1); } while (0)
348#define UML_FSTOI4C(block, dst, src1)                  do { drcuml_block_append_2(block, DRCUML_OP_FTOI4C,  4, IF_ALWAYS,  dst, src1); } while (0)
349#define UML_FSTOI4F(block, dst, src1)                  do { drcuml_block_append_2(block, DRCUML_OP_FTOI4F,  4, IF_ALWAYS,  dst, src1); } while (0)
350#define UML_FSTOI8(block, dst, src1)                  do { drcuml_block_append_2(block, DRCUML_OP_FTOI8,   4, IF_ALWAYS,  dst, src1); } while (0)
351#define UML_FSTOI8T(block, dst, src1)                  do { drcuml_block_append_2(block, DRCUML_OP_FTOI8T,  4, IF_ALWAYS,  dst, src1); } while (0)
352#define UML_FSTOI8R(block, dst, src1)                  do { drcuml_block_append_2(block, DRCUML_OP_FTOI8R,  4, IF_ALWAYS,  dst, src1); } while (0)
353#define UML_FSTOI8C(block, dst, src1)                  do { drcuml_block_append_2(block, DRCUML_OP_FTOI8C,  4, IF_ALWAYS,  dst, src1); } while (0)
354#define UML_FSTOI8F(block, dst, src1)                  do { drcuml_block_append_2(block, DRCUML_OP_FTOI8F,  4, IF_ALWAYS,  dst, src1); } while (0)
355
356#define UML_FSFRFD(block, dst, src1)                  do { drcuml_block_append_2(block, DRCUML_OP_FFRFD,   4, IF_ALWAYS,  dst, src1); } while (0)
357#define UML_FSFRI4(block, dst, src1)                  do { drcuml_block_append_2(block, DRCUML_OP_FFRI4,   4, IF_ALWAYS,  dst, src1); } while (0)
358#define UML_FSFRI8(block, dst, src1)                  do { drcuml_block_append_2(block, DRCUML_OP_FFRI8,   4, IF_ALWAYS,  dst, src1); } while (0)
359
360#define UML_FSADD(block, dst, src1, src2)               do { drcuml_block_append_3(block, DRCUML_OP_FADD,    4, FLAGS_NONE, dst, src1, src2); } while (0)
361#define UML_FSSUB(block, dst, src1, src2)               do { drcuml_block_append_3(block, DRCUML_OP_FSUB,    4, FLAGS_NONE, dst, src1, src2); } while (0)
362#define UML_FSCMP(block, src1, src2)                  do { drcuml_block_append_2(block, DRCUML_OP_FCMP,    4, FLAGS_ALLF, src1, src2); } while (0)
363#define UML_FSMUL(block, dst, src1, src2)               do { drcuml_block_append_3(block, DRCUML_OP_FMUL,    4, FLAGS_NONE, dst, src1, src2); } while (0)
364#define UML_FSDIV(block, dst, src1, src2)               do { drcuml_block_append_3(block, DRCUML_OP_FDIV,    4, FLAGS_NONE, dst, src1, src2); } while (0)
365#define UML_FSNEG(block, dst, src1)                     do { drcuml_block_append_2(block, DRCUML_OP_FNEG,    4, FLAGS_NONE, dst, src1); } while (0)
366#define UML_FSABS(block, dst, src1)                     do { drcuml_block_append_2(block, DRCUML_OP_FABS,    4, FLAGS_NONE, dst, src1); } while (0)
367#define UML_FSSQRT(block, dst, src1)                  do { drcuml_block_append_2(block, DRCUML_OP_FSQRT,   4, FLAGS_NONE, dst, src1); } while (0)
368#define UML_FSRECIP(block, dst, src1)                  do { drcuml_block_append_2(block, DRCUML_OP_FRECIP,  4, FLAGS_NONE, dst, src1); } while (0)
369#define UML_FSRSQRT(block, dst, src1)                  do { drcuml_block_append_2(block, DRCUML_OP_FRSQRT,  4, FLAGS_NONE, dst, src1); } while (0)
370
371
372/* ----- 64-bit Floating Point Arithmetic Operations ----- */
373#define UML_FDLOAD(block, dst, base, index)               do { drcuml_block_append_3(block, DRCUML_OP_FLOAD,   8, IF_ALWAYS,  dst, MEM(base), index); } while (0)
374#define UML_FDSTORE(block, base, index, src1)            do { drcuml_block_append_3(block, DRCUML_OP_FSTORE,  8, IF_ALWAYS,  MEM(base), index, src1); } while (0)
375
376#define UML_FDREAD(block, dst, space, src1)               do { drcuml_block_append_3(block, DRCUML_OP_FREAD,   8, IF_ALWAYS,  dst, IMM(ADDRESS_SPACE_##space), src1); } while (0)
377#define UML_FDWRITE(block, space, dst, src1)            do { drcuml_block_append_3(block, DRCUML_OP_FWRITE,  8, IF_ALWAYS,  IMM(ADDRESS_SPACE_##space), dst, src1); } while (0)
378
379#define UML_FDMOV(block, dst, src1)                     do { drcuml_block_append_2(block, DRCUML_OP_FMOV,    8, IF_ALWAYS,  dst, src1); } while (0)
380#define UML_FDMOVc(block, cond, dst, src1)               do { drcuml_block_append_2(block, DRCUML_OP_FMOV,    8, cond,       dst, src1); } while (0)
381
382#define UML_FDTOI4(block, dst, src1)                  do { drcuml_block_append_2(block, DRCUML_OP_FTOI4,   8, IF_ALWAYS,  dst, src1); } while (0)
383#define UML_FDTOI4T(block, dst, src1)                  do { drcuml_block_append_2(block, DRCUML_OP_FTOI4T,  8, IF_ALWAYS,  dst, src1); } while (0)
384#define UML_FDTOI4R(block, dst, src1)                  do { drcuml_block_append_2(block, DRCUML_OP_FTOI4R,  8, IF_ALWAYS,  dst, src1); } while (0)
385#define UML_FDTOI4C(block, dst, src1)                  do { drcuml_block_append_2(block, DRCUML_OP_FTOI4C,  8, IF_ALWAYS,  dst, src1); } while (0)
386#define UML_FDTOI4F(block, dst, src1)                  do { drcuml_block_append_2(block, DRCUML_OP_FTOI4F,  8, IF_ALWAYS,  dst, src1); } while (0)
387#define UML_FDTOI8(block, dst, src1)                  do { drcuml_block_append_2(block, DRCUML_OP_FTOI8,   8, IF_ALWAYS,  dst, src1); } while (0)
388#define UML_FDTOI8T(block, dst, src1)                  do { drcuml_block_append_2(block, DRCUML_OP_FTOI8T,  8, IF_ALWAYS,  dst, src1); } while (0)
389#define UML_FDTOI8R(block, dst, src1)                  do { drcuml_block_append_2(block, DRCUML_OP_FTOI8R,  8, IF_ALWAYS,  dst, src1); } while (0)
390#define UML_FDTOI8C(block, dst, src1)                  do { drcuml_block_append_2(block, DRCUML_OP_FTOI8C,  8, IF_ALWAYS,  dst, src1); } while (0)
391#define UML_FDTOI8F(block, dst, src1)                  do { drcuml_block_append_2(block, DRCUML_OP_FTOI8F,  8, IF_ALWAYS,  dst, src1); } while (0)
392
393#define UML_FDFRFS(block, dst, src1)                  do { drcuml_block_append_2(block, DRCUML_OP_FFRFS,   8, IF_ALWAYS,  dst, src1); } while (0)
394#define UML_FDFRI4(block, dst, src1)                  do { drcuml_block_append_2(block, DRCUML_OP_FFRI4,   8, IF_ALWAYS,  dst, src1); } while (0)
395#define UML_FDFRI8(block, dst, src1)                  do { drcuml_block_append_2(block, DRCUML_OP_FFRI8,   8, IF_ALWAYS,  dst, src1); } while (0)
396
397#define UML_FDADD(block, dst, src1, src2)               do { drcuml_block_append_3(block, DRCUML_OP_FADD,    8, FLAGS_NONE, dst, src1, src2); } while (0)
398#define UML_FDSUB(block, dst, src1, src2)               do { drcuml_block_append_3(block, DRCUML_OP_FSUB,    8, FLAGS_NONE, dst, src1, src2); } while (0)
399#define UML_FDCMP(block, src1, src2)                  do { drcuml_block_append_2(block, DRCUML_OP_FCMP,    8, FLAGS_ALLF, src1, src2); } while (0)
400#define UML_FDMUL(block, dst, src1, src2)               do { drcuml_block_append_3(block, DRCUML_OP_FMUL,    8, FLAGS_NONE, dst, src1, src2); } while (0)
401#define UML_FDDIV(block, dst, src1, src2)               do { drcuml_block_append_3(block, DRCUML_OP_FDIV,    8, FLAGS_NONE, dst, src1, src2); } while (0)
402#define UML_FDNEG(block, dst, src1)                     do { drcuml_block_append_2(block, DRCUML_OP_FNEG,    8, FLAGS_NONE, dst, src1); } while (0)
403#define UML_FDABS(block, dst, src1)                     do { drcuml_block_append_2(block, DRCUML_OP_FABS,    8, FLAGS_NONE, dst, src1); } while (0)
404#define UML_FDSQRT(block, dst, src1)                  do { drcuml_block_append_2(block, DRCUML_OP_FSQRT,   8, FLAGS_NONE, dst, src1); } while (0)
405#define UML_FDRECIP(block, dst, src1)                  do { drcuml_block_append_2(block, DRCUML_OP_FRECIP,  8, FLAGS_NONE, dst, src1); } while (0)
406#define UML_FDRSQRT(block, dst, src1)                  do { drcuml_block_append_2(block, DRCUML_OP_FRSQRT,  8, FLAGS_NONE, dst, src1); } while (0)
407
408
409#endif
src/emu/cpu/mips/mips3dsm.c
r2627r2628
353353                     sprintf(buffer, "nop");
354354                     else
355355                     sprintf(buffer, "sll    %s,%s,%d", reg[rd], reg[rt], shift);         break;
356            case 0x01:   sprintf(buffer, "mov%c   %s,%s,%d", ((op >> 16) & 1) ? 't' : 'f', reg[rd], reg[rt], (op >> 18) & 7); break;
356            case 0x01:   sprintf(buffer, "mov%c   %s,%s,%d", ((op >> 16) & 1) ? 't' : 'f', reg[rd], reg[rs], (op >> 18) & 7); break;
357357            case 0x02:   sprintf(buffer, "srl    %s,%s,%d", reg[rd], reg[rt], shift);         break;
358358            case 0x03:   sprintf(buffer, "sra    %s,%s,%d", reg[rd], reg[rt], shift);         break;
359359            case 0x04:   sprintf(buffer, "sllv   %s,%s,%s", reg[rd], reg[rt], reg[rs]);         break;
src/emu/cpu/mips/mips3.h
r2627r2628
5454   MIPS3_HI,
5555   MIPS3_LO,
5656   MIPS3_FPR0,
57   MIPS3_FPS0,
58   MIPS3_FPD0,
5759   MIPS3_FPR1,
60   MIPS3_FPS1,
61   MIPS3_FPD1,
5862   MIPS3_FPR2,
63   MIPS3_FPS2,
64   MIPS3_FPD2,
5965   MIPS3_FPR3,
66   MIPS3_FPS3,
67   MIPS3_FPD3,
6068   MIPS3_FPR4,
69   MIPS3_FPS4,
70   MIPS3_FPD4,
6171   MIPS3_FPR5,
72   MIPS3_FPS5,
73   MIPS3_FPD5,
6274   MIPS3_FPR6,
75   MIPS3_FPS6,
76   MIPS3_FPD6,
6377   MIPS3_FPR7,
78   MIPS3_FPS7,
79   MIPS3_FPD7,
6480   MIPS3_FPR8,
81   MIPS3_FPS8,
82   MIPS3_FPD8,
6583   MIPS3_FPR9,
84   MIPS3_FPS9,
85   MIPS3_FPD9,
6686   MIPS3_FPR10,
87   MIPS3_FPS10,
88   MIPS3_FPD10,
6789   MIPS3_FPR11,
90   MIPS3_FPS11,
91   MIPS3_FPD11,
6892   MIPS3_FPR12,
93   MIPS3_FPS12,
94   MIPS3_FPD12,
6995   MIPS3_FPR13,
96   MIPS3_FPS13,
97   MIPS3_FPD13,
7098   MIPS3_FPR14,
99   MIPS3_FPS14,
100   MIPS3_FPD14,
71101   MIPS3_FPR15,
102   MIPS3_FPS15,
103   MIPS3_FPD15,
72104   MIPS3_FPR16,
105   MIPS3_FPS16,
106   MIPS3_FPD16,
73107   MIPS3_FPR17,
108   MIPS3_FPS17,
109   MIPS3_FPD17,
74110   MIPS3_FPR18,
111   MIPS3_FPS18,
112   MIPS3_FPD18,
75113   MIPS3_FPR19,
114   MIPS3_FPS19,
115   MIPS3_FPD19,
76116   MIPS3_FPR20,
117   MIPS3_FPS20,
118   MIPS3_FPD20,
77119   MIPS3_FPR21,
120   MIPS3_FPS21,
121   MIPS3_FPD21,
78122   MIPS3_FPR22,
123   MIPS3_FPS22,
124   MIPS3_FPD22,
79125   MIPS3_FPR23,
126   MIPS3_FPS23,
127   MIPS3_FPD23,
80128   MIPS3_FPR24,
129   MIPS3_FPS24,
130   MIPS3_FPD24,
81131   MIPS3_FPR25,
132   MIPS3_FPS25,
133   MIPS3_FPD25,
82134   MIPS3_FPR26,
135   MIPS3_FPS26,
136   MIPS3_FPD26,
83137   MIPS3_FPR27,
138   MIPS3_FPS27,
139   MIPS3_FPD27,
84140   MIPS3_FPR28,
141   MIPS3_FPS28,
142   MIPS3_FPD28,
85143   MIPS3_FPR29,
144   MIPS3_FPS29,
145   MIPS3_FPD29,
86146   MIPS3_FPR30,
147   MIPS3_FPS30,
148   MIPS3_FPD30,
87149   MIPS3_FPR31,
150   MIPS3_FPS31,
151   MIPS3_FPD31,
152   MIPS3_CCR1_31,
88153   MIPS3_SR,
89154   MIPS3_EPC,
90155   MIPS3_CAUSE,
src/emu/cpu/mips/r3000.c
r2627r2628
77***************************************************************************/
88
99#include "debugger.h"
10#include "deprecat.h"
1011#include "r3000.h"
1112
1213
src/emu/cpu/mips/mips3fe.c
r2627r2628
55    Front-end for MIPS3 recompiler
66
77    Copyright Aaron Giles
8    Released for general use under the MAME license
8    Released for general non-commercial use under the MAME license
99    Visit http://mamedev.org for licensing and usage restrictions.
1010
1111***************************************************************************/
r2627r2628
465465   {
466466      case 0x00:   /* MFCz */
467467      case 0x01:   /* DMFCz */
468         if (RDREG == COP0_Count)
469            desc->cycles += MIPS3_COUNT_READ_CYCLES;
470         if (RDREG == COP0_Cause)
471            desc->cycles += MIPS3_CAUSE_READ_CYCLES;
472         desc->gpr.modified |= REGFLAG_R(RTREG);
473         return TRUE;
474
468475      case 0x02:   /* CFCz */
469476         desc->gpr.modified |= REGFLAG_R(RTREG);
470477         return TRUE;
src/emu/cpu/mips/mips3fe.h
r2627r2628
55    Front-end for MIPS3 recompiler
66
77    Copyright Aaron Giles
8    Released for general use under the MAME license
8    Released for general non-commercial use under the MAME license
99    Visit http://mamedev.org for licensing and usage restrictions.
1010
1111***************************************************************************/
src/emu/cpu/mips/mips3drc.c
r2627r2628
11/***************************************************************************
22
33    mips3drc.c
4    x86 Dynamic recompiler for MIPS III/IV emulator.
5    Written by Aaron Giles
64
7    Philosophy: this is intended to be a very basic implementation of a
8    dynamic compiler in order to keep things simple. There are certainly
9    more optimizations that could be added but for now, we keep it
10    strictly to NOP stripping and LUI optimizations.
5    Universal machine language-based MIPS III/IV emulator.
116
7    Copyright Aaron Giles
8    Released for general non-commercial use under the MAME license
9    Visit http://mamedev.org for licensing and usage restrictions.
10
11****************************************************************************
12
13    Future improvements/changes:
14
15    * Add DRC option to flush PC before calling memory handlers
16
17    * Map MIPS registers to i5-i7 (or more)
18        - should rely on backend hints to know how many to assign
19        - add instrumentation to figure out which ones are most used
20        - on entry, load registers; on exit, flush them
21        - also flush before calling debugger
22
23    * Constant tracking? (hasn't bought us much in the past)
24
25    * Customized mapped/unmapped memory handlers
26        - create 3 sets of handlers: cached, uncached, general
27        - default to general
28        - in general case, if cached use RECALL to point to cached code
29        - (same for uncached)
30        - in cached/uncached case, fall back to general case
31
1232***************************************************************************/
1333
1434#include <stddef.h>
r2627r2628
1737#include "mips3com.h"
1838#include "mips3fe.h"
1939#include "deprecat.h"
20#include "cpu/x86log.h"
2140#include "cpu/drcfe.h"
41#include "cpu/drcuml.h"
42#include "cpu/drcumlsh.h"
2243
2344extern unsigned dasmmips3(char *buffer, unsigned pc, UINT32 op);
2445
r2627r2628
2849    DEBUGGING
2950***************************************************************************/
3051
31#define LOG_CODE            (0)
32#define SINGLE_INSTRUCTION_MODE   (0)
52#define FORCE_C_BACKEND               (0)
53#define LOG_UML                     (0)
54#define LOG_NATIVE                  (0)
3355
34#ifdef ENABLE_DEBUGGER
35#define COMPARE_AGAINST_C      (0)
36#else
37#define COMPARE_AGAINST_C      (0)
38#endif
56#define SINGLE_INSTRUCTION_MODE         (0)
57#define PRINTF_EXCEPTIONS            (0)
3958
59#define PROBE_ADDRESS               ~0
4060
4161
62
4263/***************************************************************************
43    CONFIGURATION
64    CONSTANTS
4465***************************************************************************/
4566
67/* map variables */
68#define MAPVAR_PC                  MVAR(0)
69#define MAPVAR_CYCLES               MVAR(1)
70
4671/* size of the execution code cache */
4772#define CACHE_SIZE                  (32 * 1024 * 1024)
4873
r2627r2628
5277#define COMPILE_MAX_INSTRUCTIONS      ((COMPILE_BACKWARDS_BYTES/4) + (COMPILE_FORWARDS_BYTES/4))
5378#define COMPILE_MAX_SEQUENCE         64
5479
55/* hack when running comparison against the C core */
56#if COMPARE_AGAINST_C
57#undef  MIPS3_COUNT_READ_CYCLES
58#undef  MIPS3_CAUSE_READ_CYCLES
59#define MIPS3_COUNT_READ_CYCLES         0
60#define MIPS3_CAUSE_READ_CYCLES         0
61#endif
80/* exit codes */
81#define EXECUTE_OUT_OF_CYCLES         0
82#define EXECUTE_MISSING_CODE         1
6283
6384
6485
r2627r2628
6687    MACROS
6788***************************************************************************/
6889
69#define SR                  mips3.core->cpr[0][COP0_Status]
70#define IS_FR0               (!(SR & SR_FR))
71#define IS_FR1               (SR & SR_FR)
90#ifdef LSB_FIRST
91#define LOPTR(x)            ((UINT32 *)(x))
92#else
93#define LOPTR(x)            ((UINT32 *)(x) + 1)
94#endif
7295
96#define R32(reg)            ((reg == 0) ? DRCUML_PTYPE_IMMEDIATE : DRCUML_PTYPE_MEMORY), ((reg) == 0) ? 0 : (FPTR)LOPTR(&mips3.core->r[reg])
97#define LO32               R32(REG_LO)
98#define HI32               R32(REG_HI)
99#define CPR032(reg)            MEM(LOPTR(&mips3.core->cpr[0][reg]))
100#define CCR032(reg)            MEM(LOPTR(&mips3.core->ccr[0][reg]))
101#define FPR32(reg)            MEM((mips3.cstate->mode == 0) ? &((float *)&mips3.core->cpr[1][0])[reg] : (float *)&mips3.core->cpr[1][reg])
102#define CCR132(reg)            MEM(LOPTR(&mips3.core->ccr[1][reg]))
103#define CPR232(reg)            MEM(LOPTR(&mips3.core->cpr[2][reg]))
104#define CCR232(reg)            MEM(LOPTR(&mips3.core->ccr[2][reg]))
73105
106#define R64(reg)            ((reg == 0) ? DRCUML_PTYPE_IMMEDIATE : DRCUML_PTYPE_MEMORY), ((reg) == 0) ? 0 : (FPTR)&mips3.core->r[reg]
107#define LO64               R64(REG_LO)
108#define HI64               R64(REG_HI)
109#define CPR064(reg)            MEM(&mips3.core->cpr[0][reg])
110#define CCR064(reg)            MEM(&mips3.core->ccr[0][reg])
111#define FPR64(reg)            MEM((mips3.cstate->mode == 0) ? (double *)&mips3.core->cpr[1][(reg)/2] : (double *)&mips3.core->cpr[1][reg])
112#define CCR164(reg)            MEM(&mips3.core->ccr[1][reg])
113#define CPR264(reg)            MEM(&mips3.core->cpr[2][reg])
114#define CCR264(reg)            MEM(&mips3.core->ccr[2][reg])
74115
116#define FCCMASK(which)         fcc_mask[(mips3.core->flavor < MIPS3_TYPE_MIPS_IV) ? 0 : ((which) & 7)]
117
118
119
75120/***************************************************************************
76121    STRUCTURES & TYPEDEFS
77122***************************************************************************/
r2627r2628
80125typedef struct _fast_ram_info fast_ram_info;
81126struct _fast_ram_info
82127{
83   offs_t      start;
84   offs_t      end;
85   UINT8      readonly;
86   void *      base;
128   offs_t            start;                  /* start of the RAM block */
129   offs_t            end;                  /* end of the RAM block */
130   UINT8            readonly;               /* TRUE if read-only */
131   void *            base;                  /* base in memory where the RAM lives */
87132};
88133
89134
r2627r2628
91136typedef struct _hotspot_info hotspot_info;
92137struct _hotspot_info
93138{
94   offs_t      pc;
95   UINT32      opcode;
96   UINT32      cycles;
139   offs_t            pc;                     /* PC to consider */
140   UINT32            opcode;                  /* required opcode at that PC */
141   UINT32            cycles;                  /* number of cycles to eat when hit */
97142};
98143
99144
100/* opaque drc-specific info */
101typedef struct _mips3drc_data mips3drc_data;
145/* internal compiler state */
146typedef struct _compiler_state compiler_state;
147struct _compiler_state
148{
149   UINT32            cycles;                  /* accumulated cycles */
150   UINT8            checkints;               /* need to check interrupts before next instruction */
151   UINT8            checksoftints;            /* need to check software interrupts before next instruction */
152   drcuml_codelabel   labelnum;               /* index for local labels */
153};
102154
103155
156/* cache-based state */
157typedef struct _mips3_cache_state mips3_cache_state;
158struct _mips3_cache_state
159{
160   /* parameters for subroutines */
161   UINT64            numcycles;               /* return value from gettotalcycles */
162   UINT32            mode;                  /* current global mode */
163
164   /* tables */
165   UINT8            fpmode[4];               /* FPU mode table */
166   UINT64            slt_table[32];            /* FLAGS table for slt */
167   UINT64            sltu_table[32];            /* FLAGS table for sltu */
168   UINT32            c_un_table[32];            /* FLAGS table for c_un */
169   UINT32            c_eq_table[32];            /* FLAGS table for c_eq */
170   UINT32            c_ueq_table[32];         /* FLAGS table for c_ueq */
171   UINT32            c_olt_table[32];         /* FLAGS table for c_olt */
172   UINT32            c_ult_table[32];         /* FLAGS table for c_ult */
173   UINT32            c_ole_table[32];         /* FLAGS table for c_ole */
174   UINT32            c_ule_table[32];         /* FLAGS table for c_ule */
175};
176
177
104178/* MIPS3 registers */
105179typedef struct _mips3_regs mips3_regs;
106180struct _mips3_regs
107181{
108182   /* core state */
109   UINT8 *         cache;                  /* base of the cache */
110   mips3_state *   core;                  /* pointer to the core MIPS3 state */
111   drcfe_state *   drcfe;                  /* pointer to the DRC front-end state */
112   drc_core *      drc;                  /* pointer to the DRC core */
113   mips3drc_data *   drcdata;               /* pointer to the DRC-specific data */
114   UINT32         drcoptions;               /* configurable DRC options */
183   drccache *         cache;                  /* pointer to the DRC code cache */
184   drcuml_state *      drcuml;                  /* DRC UML generator state */
185   drcfe_state *      drcfe;                  /* pointer to the DRC front-end state */
186   UINT32            drcoptions;               /* configurable DRC options */
187   mips3_state *      core;                  /* pointer to the core MIPS3 state */
188   mips3_cache_state *   cstate;                  /* state that needs to live in the cache */
115189
116190   /* internal stuff */
117   UINT32         nextpc;
118   UINT8         cache_dirty;
191   UINT8            cache_dirty;            /* true if we need to flush the cache */
119192
193   /* subroutines */
194   drcuml_codehandle *   entry;                  /* entry point */
195   drcuml_codehandle *   nocode;                  /* nocode exception handler */
196   drcuml_codehandle *   out_of_cycles;            /* out of cycles exception handler */
197   drcuml_codehandle *   tlb_mismatch;            /* tlb mismatch at current PC */
198   drcuml_codehandle *   read8;                  /* read byte */
199   drcuml_codehandle *   write8;                  /* write byte */
200   drcuml_codehandle *   read16;                  /* read half */
201   drcuml_codehandle *   write16;               /* write half */
202   drcuml_codehandle *   read32;                  /* read word */
203   drcuml_codehandle *   read32mask;               /* read word masked */
204   drcuml_codehandle *   write32;               /* write word */
205   drcuml_codehandle *   write32mask;            /* write word masked */
206   drcuml_codehandle *   read64;                  /* read double */
207   drcuml_codehandle *   read64mask;               /* read double masked */
208   drcuml_codehandle *   write64;               /* write double */
209   drcuml_codehandle *   write64mask;            /* write double masked */
210   drcuml_codehandle *   exception[EXCEPTION_COUNT];   /* array of exception handlers */
211   drcuml_codehandle *   exception_norecover[EXCEPTION_COUNT];   /* array of no-recover exception handlers */
212
120213   /* fast RAM */
121   UINT32         fastram_select;
122   fast_ram_info   fastram[MIPS3_MAX_FASTRAM];
214   UINT32            fastram_select;
215   fast_ram_info      fastram[MIPS3_MAX_FASTRAM];
123216
124217   /* hotspots */
125   UINT32         hotspot_select;
126   hotspot_info   hotspot[MIPS3_MAX_HOTSPOTS];
127
128   /* code logging */
129   x86log_context *log;
130
131#if COMPARE_AGAINST_C
132   /* memory state */
133   UINT8         access_size;
134   UINT8         access_type;
135   offs_t         access_addr;
136   UINT64         access_data;
137   UINT64         access_mask;
138   memory_handlers   orighandler;
139#endif
218   UINT32            hotspot_select;
219   hotspot_info      hotspot[MIPS3_MAX_HOTSPOTS];
140220};
141221
142222
r2627r2628
145225    FUNCTION PROTOTYPES
146226***************************************************************************/
147227
148static void mips3drc_init(void);
149static void mips3drc_exit(void);
228static void code_flush_cache(drcuml_state *drcuml);
229static void code_compile_block(drcuml_state *drcuml, UINT8 mode, offs_t pc);
150230
151#if COMPARE_AGAINST_C
152static void execute_c_version(void);
153static void compare_c_version(void);
154static void mips3c_init(mips3_flavor flavor, int bigendian, int index, int clock, const struct mips3_config *config, int (*irqcallback)(int));
155static void mips3c_reset(void);
156#endif
231static void cfunc_printf_exception(void *param);
232static void cfunc_get_cycles(void *param);
233static void cfunc_printf_probe(void *param);
157234
235static void static_generate_entry_point(drcuml_state *drcuml);
236static void static_generate_nocode_handler(drcuml_state *drcuml);
237static void static_generate_out_of_cycles(drcuml_state *drcuml);
238static void static_generate_exception(drcuml_state *drcuml, UINT8 exception, int recover, const char *name);
239static void static_generate_memory_accessor(drcuml_state *drcuml, int size, int iswrite, int ismasked, const char *name, drcuml_codehandle **handleptr);
158240
241static void generate_update_cycles(drcuml_block *block, compiler_state *compiler, UINT32 nextpc, UINT32 *mempc, int allow_exception);
242static void generate_checksum_block(drcuml_block *block, compiler_state *compiler, const opcode_desc *seqhead, const opcode_desc *seqlast);
243static void generate_sequence_instruction(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc);
244static void generate_delay_slot_and_branch(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc, UINT8 linkreg);
245static int generate_opcode(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc);
246static int generate_special(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc);
247static int generate_regimm(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc);
248static int generate_idt(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc);
249static int generate_set_cop0_reg(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc, UINT8 reg);
250static int generate_get_cop0_reg(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc, UINT8 reg);
251static int generate_cop0(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc);
252static int generate_cop1(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc);
253static int generate_cop1x(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc);
159254
255static void log_add_disasm_comment(drcuml_block *block, UINT32 pc, UINT32 op);
256static const char *log_desc_flags_to_string(UINT32 flags);
257static void log_register_list(drcuml_state *drcuml, const char *string, UINT64 gprmask, UINT64 fprmask);
258static void log_opcode_desc(drcuml_state *drcuml, const opcode_desc *desclist, int indent);
259
260
261
160262/***************************************************************************
161263    PRIVATE GLOBAL VARIABLES
162264***************************************************************************/
163265
164266static mips3_regs mips3;
165static size_t allocatedsize;
166267
167268
269/* bit indexes for various FCCs */
270static const UINT32 fcc_mask[8] = { 1 << 23, 1 << 25, 1 << 26, 1 << 27, 1 << 28, 1 << 29, 1 << 30, 1 << 31 };
168271
272/* lookup table for FP modes */
273static const UINT8 fpmode_source[4] =
274{
275   DRCUML_FMOD_ROUND,
276   DRCUML_FMOD_TRUNC,
277   DRCUML_FMOD_CEIL,
278   DRCUML_FMOD_FLOOR
279};
280
281/* flag lookup table for SLT */
282static const UINT64 slt_table_source[32] =
283{
284   /* ..... */   0,
285   /* ....C */   0,
286   /* ...V. */   1,
287   /* ...VC */   1,
288   /* ..Z.. */   0,
289   /* ..Z.C */   0,
290   /* ..ZV. */   1,
291   /* ..ZVC */   1,
292   /* .S... */   1,
293   /* .S..C */   1,
294   /* .S.V. */   0,
295   /* .S.VC */   0,
296   /* .SZ.. */   1,
297   /* .SZ.C */   1,
298   /* .SZV. */   0,
299   /* .SZVC */   0,
300   /* U.... */   0,
301   /* U...C */   0,
302   /* U..V. */   1,
303   /* U..VC */   1,
304   /* U.Z.. */   0,
305   /* U.Z.C */   0,
306   /* U.ZV. */   1,
307   /* U.ZVC */   1,
308   /* US... */   1,
309   /* US..C */   1,
310   /* US.V. */   0,
311   /* US.VC */   0,
312   /* USZ.. */   1,
313   /* USZ.C */   1,
314   /* USZV. */   0,
315   /* USZVC */   0
316};
317
318/* flag lookup table for SLTU */
319static const UINT64 sltu_table_source[32] =
320{
321   /* ..... */   0,
322   /* ....C */   1,
323   /* ...V. */   0,
324   /* ...VC */   1,
325   /* ..Z.. */   0,
326   /* ..Z.C */   1,
327   /* ..ZV. */   0,
328   /* ..ZVC */   1,
329   /* .S... */   0,
330   /* .S..C */   1,
331   /* .S.V. */   0,
332   /* .S.VC */   1,
333   /* .SZ.. */   0,
334   /* .SZ.C */   1,
335   /* .SZV. */   0,
336   /* .SZVC */   1,
337   /* U.... */   0,
338   /* U...C */   1,
339   /* U..V. */   0,
340   /* U..VC */   1,
341   /* U.Z.. */   0,
342   /* U.Z.C */   1,
343   /* U.ZV. */   0,
344   /* U.ZVC */   1,
345   /* US... */   0,
346   /* US..C */   1,
347   /* US.V. */   0,
348   /* US.VC */   1,
349   /* USZ.. */   0,
350   /* USZ.C */   1,
351   /* USZV. */   0,
352   /* USZVC */   1
353};
354
355/* flag lookup table for C.UN */
356static const UINT32 c_un_table_source[32] =
357{
358   /* ..... */   0,
359   /* ....C */   0,
360   /* ...V. */   0,
361   /* ...VC */   0,
362   /* ..Z.. */   0,
363   /* ..Z.C */   0,
364   /* ..ZV. */   0,
365   /* ..ZVC */   0,
366   /* .S... */   0,
367   /* .S..C */   0,
368   /* .S.V. */   0,
369   /* .S.VC */   0,
370   /* .SZ.. */   0,
371   /* .SZ.C */   0,
372   /* .SZV. */   0,
373   /* .SZVC */   0,
374   /* U.... */   ~0,
375   /* U...C */   ~0,
376   /* U..V. */   ~0,
377   /* U..VC */   ~0,
378   /* U.Z.. */   ~0,
379   /* U.Z.C */   ~0,
380   /* U.ZV. */   ~0,
381   /* U.ZVC */   ~0,
382   /* US... */   ~0,
383   /* US..C */   ~0,
384   /* US.V. */   ~0,
385   /* US.VC */   ~0,
386   /* USZ.. */   ~0,
387   /* USZ.C */   ~0,
388   /* USZV. */   ~0,
389   /* USZVC */   ~0
390};
391
392/* flag lookup table for C.EQ */
393static const UINT32 c_eq_table_source[32] =
394{
395   /* ..... */   0,
396   /* ....C */   0,
397   /* ...V. */   0,
398   /* ...VC */   0,
399   /* ..Z.. */   ~0,
400   /* ..Z.C */   ~0,
401   /* ..ZV. */   ~0,
402   /* ..ZVC */   ~0,
403   /* .S... */   0,
404   /* .S..C */   0,
405   /* .S.V. */   0,
406   /* .S.VC */   0,
407   /* .SZ.. */   ~0,
408   /* .SZ.C */   ~0,
409   /* .SZV. */   ~0,
410   /* .SZVC */   ~0,
411   /* U.... */   0,
412   /* U...C */   0,
413   /* U..V. */   0,
414   /* U..VC */   0,
415   /* U.Z.. */   0,
416   /* U.Z.C */   0,
417   /* U.ZV. */   0,
418   /* U.ZVC */   0,
419   /* US... */   0,
420   /* US..C */   0,
421   /* US.V. */   0,
422   /* US.VC */   0,
423   /* USZ.. */   0,
424   /* USZ.C */   0,
425   /* USZV. */   0,
426   /* USZVC */   0
427};
428
429/* flag lookup table for C.UEQ */
430static const UINT32 c_ueq_table_source[32] =
431{
432   /* ..... */   0,
433   /* ....C */   0,
434   /* ...V. */   0,
435   /* ...VC */   0,
436   /* ..Z.. */   ~0,
437   /* ..Z.C */   ~0,
438   /* ..ZV. */   ~0,
439   /* ..ZVC */   ~0,
440   /* .S... */   0,
441   /* .S..C */   0,
442   /* .S.V. */   0,
443   /* .S.VC */   0,
444   /* .SZ.. */   ~0,
445   /* .SZ.C */   ~0,
446   /* .SZV. */   ~0,
447   /* .SZVC */   ~0,
448   /* U.... */   ~0,
449   /* U...C */   ~0,
450   /* U..V. */   ~0,
451   /* U..VC */   ~0,
452   /* U.Z.. */   ~0,
453   /* U.Z.C */   ~0,
454   /* U.ZV. */   ~0,
455   /* U.ZVC */   ~0,
456   /* US... */   ~0,
457   /* US..C */   ~0,
458   /* US.V. */   ~0,
459   /* US.VC */   ~0,
460   /* USZ.. */   ~0,
461   /* USZ.C */   ~0,
462   /* USZV. */   ~0,
463   /* USZVC */   ~0
464};
465
466/* flag lookup table for C.OLT */
467static const UINT32 c_olt_table_source[32] =
468{
469   /* ..... */   0,
470   /* ....C */   ~0,
471   /* ...V. */   0,
472   /* ...VC */   ~0,
473   /* ..Z.. */   0,
474   /* ..Z.C */   ~0,
475   /* ..ZV. */   0,
476   /* ..ZVC */   ~0,
477   /* .S... */   0,
478   /* .S..C */   ~0,
479   /* .S.V. */   0,
480   /* .S.VC */   ~0,
481   /* .SZ.. */   0,
482   /* .SZ.C */   ~0,
483   /* .SZV. */   0,
484   /* .SZVC */   ~0,
485   /* U.... */   0,
486   /* U...C */   0,
487   /* U..V. */   0,
488   /* U..VC */   0,
489   /* U.Z.. */   0,
490   /* U.Z.C */   0,
491   /* U.ZV. */   0,
492   /* U.ZVC */   0,
493   /* US... */   0,
494   /* US..C */   0,
495   /* US.V. */   0,
496   /* US.VC */   0,
497   /* USZ.. */   0,
498   /* USZ.C */   0,
499   /* USZV. */   0,
500   /* USZVC */   0
501};
502
503/* flag lookup table for C.ULT */
504static const UINT32 c_ult_table_source[32] =
505{
506   /* ..... */   0,
507   /* ....C */   ~0,
508   /* ...V. */   0,
509   /* ...VC */   ~0,
510   /* ..Z.. */   0,
511   /* ..Z.C */   ~0,
512   /* ..ZV. */   0,
513   /* ..ZVC */   ~0,
514   /* .S... */   0,
515   /* .S..C */   ~0,
516   /* .S.V. */   0,
517   /* .S.VC */   ~0,
518   /* .SZ.. */   0,
519   /* .SZ.C */   ~0,
520   /* .SZV. */   0,
521   /* .SZVC */   ~0,
522   /* U.... */   ~0,
523   /* U...C */   ~0,
524   /* U..V. */   ~0,
525   /* U..VC */   ~0,
526   /* U.Z.. */   ~0,
527   /* U.Z.C */   ~0,
528   /* U.ZV. */   ~0,
529   /* U.ZVC */   ~0,
530   /* US... */   ~0,
531   /* US..C */   ~0,
532   /* US.V. */   ~0,
533   /* US.VC */   ~0,
534   /* USZ.. */   ~0,
535   /* USZ.C */   ~0,
536   /* USZV. */   ~0,
537   /* USZVC */   ~0
538};
539
540/* flag lookup table for C.OLE */
541static const UINT32 c_ole_table_source[32] =
542{
543   /* ..... */   0,
544   /* ....C */   ~0,
545   /* ...V. */   0,
546   /* ...VC */   ~0,
547   /* ..Z.. */   ~0,
548   /* ..Z.C */   ~0,
549   /* ..ZV. */   ~0,
550   /* ..ZVC */   ~0,
551   /* .S... */   0,
552   /* .S..C */   ~0,
553   /* .S.V. */   0,
554   /* .S.VC */   ~0,
555   /* .SZ.. */   ~0,
556   /* .SZ.C */   ~0,
557   /* .SZV. */   ~0,
558   /* .SZVC */   ~0,
559   /* U.... */   0,
560   /* U...C */   0,
561   /* U..V. */   0,
562   /* U..VC */   0,
563   /* U.Z.. */   0,
564   /* U.Z.C */   0,
565   /* U.ZV. */   0,
566   /* U.ZVC */   0,
567   /* US... */   0,
568   /* US..C */   0,
569   /* US.V. */   0,
570   /* US.VC */   0,
571   /* USZ.. */   0,
572   /* USZ.C */   0,
573   /* USZV. */   0,
574   /* USZVC */   0
575};
576
577/* flag lookup table for C.ULE */
578static const UINT32 c_ule_table_source[32] =
579{
580   /* ..... */   0,
581   /* ....C */   ~0,
582   /* ...V. */   0,
583   /* ...VC */   ~0,
584   /* ..Z.. */   ~0,
585   /* ..Z.C */   ~0,
586   /* ..ZV. */   ~0,
587   /* ..ZVC */   ~0,
588   /* .S... */   0,
589   /* .S..C */   ~0,
590   /* .S.V. */   0,
591   /* .S.VC */   ~0,
592   /* .SZ.. */   ~0,
593   /* .SZ.C */   ~0,
594   /* .SZV. */   ~0,
595   /* .SZVC */   ~0,
596   /* U.... */   ~0,
597   /* U...C */   ~0,
598   /* U..V. */   ~0,
599   /* U..VC */   ~0,
600   /* U.Z.. */   ~0,
601   /* U.Z.C */   ~0,
602   /* U.ZV. */   ~0,
603   /* U.ZVC */   ~0,
604   /* US... */   ~0,
605   /* US..C */   ~0,
606   /* US.V. */   ~0,
607   /* US.VC */   ~0,
608   /* USZ.. */   ~0,
609   /* USZ.C */   ~0,
610   /* USZV. */   ~0,
611   /* USZVC */   ~0
612};
613
614
615
169616/***************************************************************************
617    INLINE FUNCTIONS
618***************************************************************************/
619
620/*-------------------------------------------------
621    epc - compute the exception PC from a
622    descriptor
623-------------------------------------------------*/
624
625INLINE UINT32 epc(const opcode_desc *desc)
626{
627   return (desc->flags & OPFLAG_IN_DELAY_SLOT) ? (desc->pc - 3) : desc->pc;
628}
629
630
631/*-------------------------------------------------
632    alloc_handle - allocate a handle if not
633    already allocated
634-------------------------------------------------*/
635
636INLINE void alloc_handle(drcuml_state *drcuml, drcuml_codehandle **handleptr, const char *name)
637{
638   if (*handleptr == NULL)
639      *handleptr = drcuml_handle_alloc(drcuml, name);
640}
641
642
643
644/***************************************************************************
170645    CORE CALLBACKS
171646***************************************************************************/
172647
r2627r2628
183658      COMPILE_MAX_SEQUENCE,      /* maximum instructions to include in a sequence */
184659      mips3fe_describe         /* callback to describe a single instruction */
185660   };
186   size_t memsize = mips3com_init(NULL, flavor, bigendian, index, clock, config, irqcallback, NULL);
661   UINT32 flags = 0;
662   size_t extrasize;
663   void *extramem;
187664
188   /* allocate a cache and memory for the core data in a single block */
189   allocatedsize = CACHE_SIZE + sizeof(*mips3.core) + memsize;
190   mips3.core = osd_alloc_executable(allocatedsize);
191   if (mips3.core == NULL)
192      fatalerror("Unable to allocate cache of size %d\n", (UINT32)allocatedsize);
193   mips3.cache = (UINT8 *)(mips3.core + 1) + memsize;
665   /* determine how much memory beyond the core size we need */
666    extrasize = mips3com_init(NULL, flavor, bigendian, index, clock, config, irqcallback, NULL);
194667
668   /* allocate enough space for the cache and the core */
669   mips3.cache = drccache_alloc(CACHE_SIZE + sizeof(*mips3.core) + extrasize);
670   if (mips3.cache == NULL)
671      fatalerror("Unable to allocate cache of size %d", (UINT32)(CACHE_SIZE + sizeof(*mips3.core) + extrasize));
672
673   /* allocate the core and the extra memory */
674   mips3.core = drccache_memory_alloc_near(mips3.cache, sizeof(*mips3.core));
675   extramem = drccache_memory_alloc(mips3.cache, extrasize);
676
195677   /* initialize the core */
196   mips3com_init(mips3.core, flavor, bigendian, index, clock, config, irqcallback, mips3.core + 1);
197#if COMPARE_AGAINST_C
198   mips3c_init(flavor, bigendian, index, clock, config, irqcallback);
199#endif
678   mips3com_init(mips3.core, flavor, bigendian, index, clock, config, irqcallback, extramem);
200679
201   /* initialize the DRC to use the cache */
202   mips3drc_init();
203   if (Machine->debug_mode || SINGLE_INSTRUCTION_MODE)
680   /* allocate memory for cache-local state and initialize it */
681   mips3.cstate = drccache_memory_alloc_near(mips3.cache, sizeof(*mips3.cstate));
682   memset(mips3.cstate, 0, sizeof(*mips3.cstate));
683   memcpy(mips3.cstate->fpmode, fpmode_source, sizeof(fpmode_source));
684   memcpy(mips3.cstate->slt_table, slt_table_source, sizeof(slt_table_source));
685   memcpy(mips3.cstate->sltu_table, sltu_table_source, sizeof(sltu_table_source));
686   memcpy(mips3.cstate->c_un_table, c_un_table_source, sizeof(c_un_table_source));
687   memcpy(mips3.cstate->c_eq_table, c_eq_table_source, sizeof(c_eq_table_source));
688   memcpy(mips3.cstate->c_ueq_table, c_ueq_table_source, sizeof(c_ueq_table_source));
689   memcpy(mips3.cstate->c_olt_table, c_olt_table_source, sizeof(c_olt_table_source));
690   memcpy(mips3.cstate->c_ult_table, c_ult_table_source, sizeof(c_ult_table_source));
691   memcpy(mips3.cstate->c_ole_table, c_ole_table_source, sizeof(c_ole_table_source));
692   memcpy(mips3.cstate->c_ule_table, c_ule_table_source, sizeof(c_ule_table_source));
693
694   /* initialize the UML generator */
695   if (FORCE_C_BACKEND)
696      flags |= DRCUML_OPTION_USE_C;
697   if (LOG_UML)
698      flags |= DRCUML_OPTION_LOG_UML;
699   if (LOG_NATIVE)
700      flags |= DRCUML_OPTION_LOG_NATIVE;
701   mips3.drcuml = drcuml_alloc(mips3.cache, flags, 2, 32, 2);
702   if (mips3.drcuml == NULL)
703      fatalerror("Error initializing the UML");
704
705   /* initialize the front-end helper */
706   if (SINGLE_INSTRUCTION_MODE)
204707      feconfig.max_sequence = 1;
205708   mips3.drcfe = drcfe_init(&feconfig, mips3.core);
206709
207   /* start up code logging */
208   if (LOG_CODE)
209      mips3.log = x86log_create_context("mips3drc.asm");
710   /* mark the cache dirty so it is updated on next execute */
711   mips3.cache_dirty = TRUE;
210712}
211713
212714
r2627r2628
216718
217719static void mips3_reset(void)
218720{
219   /* reset the common code and flush the cache */
721   /* reset the common code and mark the cache dirty */
220722   mips3com_reset(mips3.core);
221   drc_cache_reset(mips3.drc);
222
223#if COMPARE_AGAINST_C
224   mips3c_reset();
225#endif
723   mips3.cache_dirty = TRUE;
226724}
227725
228726
r2627r2628
233731
234732static int mips3_execute(int cycles)
235733{
734   drcuml_state *drcuml = mips3.drcuml;
735   int execute_result;
736
236737   /* reset the cache if dirty */
237738   if (mips3.cache_dirty)
238      drc_cache_reset(mips3.drc);
739      code_flush_cache(drcuml);
239740   mips3.cache_dirty = FALSE;
240741
241742   /* execute */
242743   mips3.core->icount = cycles;
243   drc_execute(mips3.drc);
744   do
745   {
746      /* run as much as we can */
747      execute_result = drcuml_execute(drcuml, mips3.entry);
748
749      /* if we need to recompile, do it */
750      if (execute_result == EXECUTE_MISSING_CODE)
751         code_compile_block(drcuml, mips3.cstate->mode, mips3.core->pc);
752
753   } while (execute_result != EXECUTE_OUT_OF_CYCLES);
754
755   /* return the number of cycles executed */
244756   return cycles - mips3.core->icount;
245757}
246758
r2627r2628
251763
252764static void mips3_exit(void)
253765{
254   /* clean up code logging */
255   if (LOG_CODE)
256      x86log_free_context(mips3.log);
257
258766   /* clean up the DRC */
259767   drcfe_exit(mips3.drcfe);
260   mips3drc_exit();
261
262   /* free the cache */
263   osd_free_executable(mips3.core, CACHE_SIZE + sizeof(*mips3.core));
768   drcuml_free(mips3.drcuml);
769   drccache_free(mips3.cache);
264770}
265771
266772
r2627r2628
314820
315821
316822/***************************************************************************
317    COMMON UTILITIES
823    CACHE MANAGEMENT
318824***************************************************************************/
319825
320826/*-------------------------------------------------
321    fastram_ptr - return a pointer to the base of
322    memory containing the given address, if it
323    is within a fast RAM range; returns 0
324    otherwise
827    code_flush_cache - flush the cache and
828    regenerate static code
325829-------------------------------------------------*/
326830
327#ifdef PTR64
328static void *fastram_ptr(offs_t addr, int size, int iswrite)
831static void code_flush_cache(drcuml_state *drcuml)
329832{
330   int ramnum;
833   /* empty the transient cache contents */
834   drcuml_reset(drcuml);
331835
332   /* only direct maps are supported */
333   if (addr < 0x80000000 || addr >= 0xc0000000)
334      return NULL;
335   addr &= 0x1fffffff;
836   /* generate the entry point and out-of-cycles handlers */
837   static_generate_entry_point(drcuml);
838   static_generate_nocode_handler(drcuml);
839   static_generate_out_of_cycles(drcuml);
336840
337   /* adjust address for endianness */
338   if (mips3.core->bigendian)
841   /* append exception handlers for various types */
842   static_generate_exception(drcuml, EXCEPTION_INTERRUPT, TRUE,  "exception_interrupt");
843   static_generate_exception(drcuml, EXCEPTION_INTERRUPT, FALSE, "exception_interrupt_norecover");
844   static_generate_exception(drcuml, EXCEPTION_TLBMOD,    TRUE,  "exception_tlbmod");
845   static_generate_exception(drcuml, EXCEPTION_TLBLOAD,   TRUE,  "exception_tlbload");
846   static_generate_exception(drcuml, EXCEPTION_TLBSTORE,  TRUE,  "exception_tlbstore");
847   static_generate_exception(drcuml, EXCEPTION_SYSCALL,   TRUE,  "exception_syscall");
848   static_generate_exception(drcuml, EXCEPTION_BREAK,     TRUE,  "exception_break");
849   static_generate_exception(drcuml, EXCEPTION_INVALIDOP, TRUE,  "exception_invalidop");
850   static_generate_exception(drcuml, EXCEPTION_BADCOP,    TRUE,  "exception_badcop");
851   static_generate_exception(drcuml, EXCEPTION_OVERFLOW,  TRUE,  "exception_overflow");
852   static_generate_exception(drcuml, EXCEPTION_TRAP,      TRUE,  "exception_trap");
853
854   /* add subroutines for memory accesses */
855   static_generate_memory_accessor(drcuml, 1, FALSE, FALSE, "read8",       &mips3.read8);
856   static_generate_memory_accessor(drcuml, 1, TRUE,  FALSE, "write8",      &mips3.write8);
857   static_generate_memory_accessor(drcuml, 2, FALSE, FALSE, "read16",      &mips3.read16);
858   static_generate_memory_accessor(drcuml, 2, TRUE,  FALSE, "write16",     &mips3.write16);
859   static_generate_memory_accessor(drcuml, 4, FALSE, FALSE, "read32",      &mips3.read32);
860   static_generate_memory_accessor(drcuml, 4, FALSE, TRUE,  "read32mask",  &mips3.read32mask);
861   static_generate_memory_accessor(drcuml, 4, TRUE,  FALSE, "write32",     &mips3.write32);
862   static_generate_memory_accessor(drcuml, 4, TRUE,  TRUE,  "write32mask", &mips3.write32mask);
863   static_generate_memory_accessor(drcuml, 8, FALSE, FALSE, "read64",      &mips3.read64);
864   static_generate_memory_accessor(drcuml, 8, FALSE, TRUE,  "read64mask",  &mips3.read64mask);
865   static_generate_memory_accessor(drcuml, 8, TRUE,  FALSE, "write64",     &mips3.write64);
866   static_generate_memory_accessor(drcuml, 8, TRUE,  TRUE,  "write64mask", &mips3.write64mask);
867}
868
869
870/*-------------------------------------------------
871    code_compile_block - compile a block of the
872    given mode at the specified pc
873-------------------------------------------------*/
874
875static void code_compile_block(drcuml_state *drcuml, UINT8 mode, offs_t pc)
876{
877   compiler_state compiler = { 0 };
878   const opcode_desc *seqhead, *seqlast;
879   const opcode_desc *desclist;
880   int override = FALSE;
881   drcuml_block *block;
882   jmp_buf errorbuf;
883
884   /* get a description of this sequence */
885   desclist = drcfe_describe_code(mips3.drcfe, pc);
886   if (LOG_UML)
887      log_opcode_desc(drcuml, desclist, 0);
888
889   /* if we get an error back, flush the cache and try again */
890   if (setjmp(errorbuf) != 0)
891      code_flush_cache(drcuml);
892
893   /* start the block */
894   block = drcuml_block_begin(drcuml, 4096, &errorbuf);
895
896   /* loop until we get through all instruction sequences */
897   for (seqhead = desclist; seqhead != NULL; seqhead = seqlast->next)
339898   {
340      if (size == 1)
341         addr ^= 3;
342      else if (size == 2)
343         addr ^= 2;
899      const opcode_desc *curdesc;
900      UINT32 nextpc;
901
902      /* add a code log entry */
903      if (LOG_UML)
904         UML_COMMENT(block, "-------------------------");                  // comment
905
906      /* determine the last instruction in this sequence */
907      for (seqlast = seqhead; seqlast != NULL; seqlast = seqlast->next)
908         if (seqlast->flags & OPFLAG_END_SEQUENCE)
909            break;
910      assert(seqlast != NULL);
911
912      /* if we don't have a hash for this mode/pc, or if we are overriding all, add one */
913      if (override || !drcuml_hash_exists(drcuml, mode, seqhead->pc))
914         UML_HASH(block, mode, seqhead->pc);                              // hash    mode,pc
915
916      /* if we already have a hash, and this is the first sequence, assume that we */
917      /* are recompiling due to being out of sync and allow future overrides */
918      else if (seqhead == desclist)
919      {
920         override = TRUE;
921         UML_HASH(block, mode, seqhead->pc);                              // hash    mode,pc
922      }
923
924      /* otherwise, redispatch to that fixed PC and skip the rest of the processing */
925      else
926      {
927         UML_LABEL(block, seqhead->pc | 0x80000000);                        // label   seqhead->pc | 0x80000000
928         UML_HASHJMP(block, IMM(mips3.cstate->mode), IMM(seqhead->pc), mips3.nocode);
929                                                               // hashjmp <mode>,seqhead->pc,nocode
930         continue;
931      }
932
933      /* validate this code block if we're not pointing into ROM */
934      if (memory_get_write_ptr(cpu_getactivecpu(), ADDRESS_SPACE_PROGRAM, seqhead->physpc) != NULL)
935         generate_checksum_block(block, &compiler, seqhead, seqlast);
936
937      /* label this instruction, if it may be jumped to locally */
938      if (seqhead->flags & OPFLAG_IS_BRANCH_TARGET)
939         UML_LABEL(block, seqhead->pc | 0x80000000);                        // label   seqhead->pc | 0x80000000
940
941      /* iterate over instructions in the sequence and compile them */
942      for (curdesc = seqhead; curdesc != seqlast->next; curdesc = curdesc->next)
943         generate_sequence_instruction(block, &compiler, curdesc);
944
945      /* if we need to return to the start, do it */
946      if (seqlast->flags & OPFLAG_RETURN_TO_START)
947         nextpc = pc;
948
949      /* otherwise we just go to the next instruction */
950      else
951         nextpc = seqlast->pc + (seqlast->skipslots + 1) * 4;
952
953      /* count off cycles and go there */
954      generate_update_cycles(block, &compiler, nextpc, NULL, TRUE);            // <subtract cycles>
955
956      /* if the last instruction can change modes, use a variable mode; otherwise, assume the same mode */
957      if (seqlast->flags & OPFLAG_CAN_CHANGE_MODES)
958         UML_HASHJMP(block, MEM(&mips3.cstate->mode), IMM(nextpc), mips3.nocode);// hashjmp <mode>,nextpc,nocode
959      else if (seqlast->next == NULL || seqlast->next->pc != nextpc)
960         UML_HASHJMP(block, IMM(mips3.cstate->mode), IMM(nextpc), mips3.nocode);   // hashjmp <mode>,nextpc,nocode
344961   }
345962
346   /* search for fastram */
963   /* end the sequence */
964   drcuml_block_end(block);
965}
966
967
968
969/***************************************************************************
970    C FUNCTION CALLBACKS
971***************************************************************************/
972
973/*-------------------------------------------------
974    cfunc_printf_exception - log any exceptions that
975    aren't interrupts
976-------------------------------------------------*/
977
978static void cfunc_printf_exception(void *param)
979{
980   printf("Exception: EPC=%08X Cause=%08X BadVAddr=%08X Jmp=%08X\n", (UINT32)mips3.core->cpr[0][COP0_EPC], (UINT32)mips3.core->cpr[0][COP0_Cause], (UINT32)mips3.core->cpr[0][COP0_BadVAddr], mips3.core->pc);
981   cfunc_printf_probe((void *)(FPTR)mips3.core->cpr[0][COP0_EPC]);
982}
983
984
985/*-------------------------------------------------
986    cfunc_get_cycles - compute the total number
987    of cycles executed so far
988-------------------------------------------------*/
989
990static void cfunc_get_cycles(void *param)
991{
992   UINT64 *dest = param;
993   *dest = activecpu_gettotalcycles();
994}
995
996
997/*-------------------------------------------------
998    cfunc_printf_probe - print the current CPU
999    state and return
1000-------------------------------------------------*/
1001
1002static void cfunc_printf_probe(void *param)
1003{
1004   UINT32 pc = (UINT32)(FPTR)param;
1005
1006   printf(" PC=%08X          r1=%08X%08X  r2=%08X%08X  r3=%08X%08X\n",
1007      pc,
1008      (UINT32)(mips3.core->r[1] >> 32), (UINT32)mips3.core->r[1],
1009      (UINT32)(mips3.core->r[2] >> 32), (UINT32)mips3.core->r[2],
1010      (UINT32)(mips3.core->r[3] >> 32), (UINT32)mips3.core->r[3]);
1011   printf(" r4=%08X%08X  r5=%08X%08X  r6=%08X%08X  r7=%08X%08X\n",
1012      (UINT32)(mips3.core->r[4] >> 32), (UINT32)mips3.core->r[4],
1013      (UINT32)(mips3.core->r[5] >> 32), (UINT32)mips3.core->r[5],
1014      (UINT32)(mips3.core->r[6] >> 32), (UINT32)mips3.core->r[6],
1015      (UINT32)(mips3.core->r[7] >> 32), (UINT32)mips3.core->r[7]);
1016   printf(" r8=%08X%08X  r9=%08X%08X r10=%08X%08X r11=%08X%08X\n",
1017      (UINT32)(mips3.core->r[8] >> 32), (UINT32)mips3.core->r[8],
1018      (UINT32)(mips3.core->r[9] >> 32), (UINT32)mips3.core->r[9],
1019      (UINT32)(mips3.core->r[10] >> 32), (UINT32)mips3.core->r[10],
1020      (UINT32)(mips3.core->r[11] >> 32), (UINT32)mips3.core->r[11]);
1021   printf("r12=%08X%08X r13=%08X%08X r14=%08X%08X r15=%08X%08X\n",
1022      (UINT32)(mips3.core->r[12] >> 32), (UINT32)mips3.core->r[12],
1023      (UINT32)(mips3.core->r[13] >> 32), (UINT32)mips3.core->r[13],
1024      (UINT32)(mips3.core->r[14] >> 32), (UINT32)mips3.core->r[14],
1025      (UINT32)(mips3.core->r[15] >> 32), (UINT32)mips3.core->r[15]);
1026   printf("r16=%08X%08X r17=%08X%08X r18=%08X%08X r19=%08X%08X\n",
1027      (UINT32)(mips3.core->r[16] >> 32), (UINT32)mips3.core->r[16],
1028      (UINT32)(mips3.core->r[17] >> 32), (UINT32)mips3.core->r[17],
1029      (UINT32)(mips3.core->r[18] >> 32), (UINT32)mips3.core->r[18],
1030      (UINT32)(mips3.core->r[19] >> 32), (UINT32)mips3.core->r[19]);
1031   printf("r20=%08X%08X r21=%08X%08X r22=%08X%08X r23=%08X%08X\n",
1032      (UINT32)(mips3.core->r[20] >> 32), (UINT32)mips3.core->r[20],
1033      (UINT32)(mips3.core->r[21] >> 32), (UINT32)mips3.core->r[21],
1034      (UINT32)(mips3.core->r[22] >> 32), (UINT32)mips3.core->r[22],
1035      (UINT32)(mips3.core->r[23] >> 32), (UINT32)mips3.core->r[23]);
1036   printf("r24=%08X%08X r25=%08X%08X r26=%08X%08X r27=%08X%08X\n",
1037      (UINT32)(mips3.core->r[24] >> 32), (UINT32)mips3.core->r[24],
1038      (UINT32)(mips3.core->r[25] >> 32), (UINT32)mips3.core->r[25],
1039      (UINT32)(mips3.core->r[26] >> 32), (UINT32)mips3.core->r[26],
1040      (UINT32)(mips3.core->r[27] >> 32), (UINT32)mips3.core->r[27]);
1041   printf("r28=%08X%08X r29=%08X%08X r30=%08X%08X r31=%08X%08X\n",
1042      (UINT32)(mips3.core->r[28] >> 32), (UINT32)mips3.core->r[28],
1043      (UINT32)(mips3.core->r[29] >> 32), (UINT32)mips3.core->r[29],
1044      (UINT32)(mips3.core->r[30] >> 32), (UINT32)mips3.core->r[30],
1045      (UINT32)(mips3.core->r[31] >> 32), (UINT32)mips3.core->r[31]);
1046   printf(" hi=%08X%08X  lo=%08X%08X\n",
1047      (UINT32)(mips3.core->r[REG_HI] >> 32), (UINT32)mips3.core->r[REG_HI],
1048      (UINT32)(mips3.core->r[REG_LO] >> 32), (UINT32)mips3.core->r[REG_LO]);
1049}
1050
1051
1052
1053/***************************************************************************
1054    STATIC CODEGEN
1055***************************************************************************/
1056
1057/*-------------------------------------------------
1058    static_generate_entry_point - generate a
1059    static entry point
1060-------------------------------------------------*/
1061
1062static void static_generate_entry_point(drcuml_state *drcuml)
1063{
1064   drcuml_codelabel skip = 1;
1065   drcuml_block *block;
1066   jmp_buf errorbuf;
1067
1068   /* if we get an error back, we're screwed */
1069   if (setjmp(errorbuf) != 0)
1070      fatalerror("Unrecoverable error in static_generate_entry_point");
1071
1072   /* begin generating */
1073   block = drcuml_block_begin(drcuml, 20, &errorbuf);
1074
1075   /* forward references */
1076   alloc_handle(drcuml, &mips3.exception_norecover[EXCEPTION_INTERRUPT], "interrupt_norecover");
1077   alloc_handle(drcuml, &mips3.nocode, "nocode");
1078
1079   alloc_handle(drcuml, &mips3.entry, "entry");
1080   UML_HANDLE(block, mips3.entry);                                       // handle  entry
1081
1082   /* reset the FPU mode */
1083   UML_AND(block, IREG(0), CCR164(31), IMM(3));                           // and     i0,ccr1[31],3
1084   UML_LOAD1U(block, IREG(0), &mips3.cstate->fpmode[0], IREG(0));               // load1u  i0,fpmode,i0
1085   UML_SETFMOD(block, IREG(0));                                       // setfmod i0
1086
1087   /* check for interrupts */
1088   UML_AND(block, IREG(0), CPR032(COP0_Cause), CPR032(COP0_Status));            // and     i0,[Cause],[Status]
1089   UML_ANDf(block, IREG(0), IREG(0), IMM(0xfc00), FLAGS_Z);                  // and     i0,i0,0xfc00,Z
1090   UML_JMPc(block, IF_Z, skip);                                       // jmp     skip,Z
1091   UML_TEST(block, CPR032(COP0_Status), IMM(SR_IE));                        // test    [Status],SR_IE
1092   UML_JMPc(block, IF_Z, skip);                                       // jmp     skip,Z
1093   UML_TEST(block, CPR032(COP0_Status), IMM(SR_EXL | SR_ERL));                  // test    [Status],SR_EXL | SR_ERL
1094   UML_JMPc(block, IF_NZ, skip);                                       // jmp     skip,NZ
1095   UML_MOV(block, IREG(0), MEM(&mips3.core->pc));                           // mov     i0,pc
1096   UML_MOV(block, IREG(1), IMM(0));                                    // mov     i1,0
1097   UML_CALLH(block, mips3.exception_norecover[EXCEPTION_INTERRUPT]);            // callh   exception_norecover
1098   UML_LABEL(block, skip);                                          // skip:
1099
1100   /* generate a hash jump via the current mode and PC */
1101   UML_HASHJMP(block, MEM(&mips3.cstate->mode), MEM(&mips3.core->pc), mips3.nocode);// hashjmp <mode>,<pc>,nocode
1102
1103   drcuml_block_end(block);
1104}
1105
1106
1107/*-------------------------------------------------
1108    static_generate_nocode_handler - generate an
1109    exception handler for "out of code"
1110-------------------------------------------------*/
1111
1112static void static_generate_nocode_handler(drcuml_state *drcuml)
1113{
1114   drcuml_block *block;
1115   jmp_buf errorbuf;
1116
1117   /* if we get an error back, we're screwed */
1118   if (setjmp(errorbuf) != 0)
1119      fatalerror("Unrecoverable error in static_generate_nocode_handler");
1120
1121   /* begin generating */
1122   block = drcuml_block_begin(drcuml, 10, &errorbuf);
1123
1124   /* generate a hash jump via the current mode and PC */
1125   alloc_handle(drcuml, &mips3.nocode, "nocode");
1126   UML_HANDLE(block, mips3.nocode);                                    // handle  nocode
1127   UML_GETEXP(block, IREG(0));                                          // getexp  i0
1128   UML_MOV(block, MEM(&mips3.core->pc), IREG(0));                           // mov     [pc],i0
1129   UML_EXIT(block, IMM(EXECUTE_MISSING_CODE));                              // exit    EXECUTE_MISSING_CODE
1130
1131   drcuml_block_end(block);
1132}
1133
1134
1135/*-------------------------------------------------
1136    static_generate_out_of_cycles - generate an
1137    out of cycles exception handler
1138-------------------------------------------------*/
1139
1140static void static_generate_out_of_cycles(drcuml_state *drcuml)
1141{
1142   drcuml_block *block;
1143   jmp_buf errorbuf;
1144
1145   /* if we get an error back, we're screwed */
1146   if (setjmp(errorbuf) != 0)
1147      fatalerror("Unrecoverable error in static_generate_out_of_cycles");
1148
1149   /* begin generating */
1150   block = drcuml_block_begin(drcuml, 10, &errorbuf);
1151
1152   /* generate a hash jump via the current mode and PC */
1153   alloc_handle(drcuml, &mips3.out_of_cycles, "out_of_cycles");
1154   UML_HANDLE(block, mips3.out_of_cycles);                                 // handle  out_of_cycles
1155   UML_GETEXP(block, IREG(0));                                          // getexp  i0
1156   UML_MOV(block, MEM(&mips3.core->pc), IREG(0));                           // mov     <pc>,i0
1157   UML_EXIT(block, IMM(EXECUTE_OUT_OF_CYCLES));                           // exit    EXECUTE_OUT_OF_CYCLES
1158
1159   drcuml_block_end(block);
1160}
1161
1162
1163/*-------------------------------------------------
1164    static_generate_exception - generate a static
1165    exception handler
1166-------------------------------------------------*/
1167
1168static void static_generate_exception(drcuml_state *drcuml, UINT8 exception, int recover, const char *name)
1169{
1170   UINT32 offset = (exception >= EXCEPTION_TLBMOD && exception <= EXCEPTION_TLBSTORE) ? 0x00 : 0x180;
1171   drcuml_codelabel next = 1;
1172   drcuml_block *block;
1173   jmp_buf errorbuf;
1174
1175   /* if we get an error back, we're screwed */
1176   if (setjmp(errorbuf) != 0)
1177      fatalerror("Unrecoverable error in static_generate_exception");
1178
1179   /* begin generating */
1180   block = drcuml_block_begin(drcuml, 1024, &errorbuf);
1181
1182   /* add a global entry for this */
1183   if (recover)
1184   {
1185      alloc_handle(drcuml, &mips3.exception[exception], name);
1186      UML_HANDLE(block, mips3.exception[exception]);                        // handle  name
1187   }
1188   else
1189   {
1190      alloc_handle(drcuml, &mips3.exception_norecover[exception], name);
1191      UML_HANDLE(block, mips3.exception_norecover[exception]);               // handle  name
1192   }
1193
1194   /* exception parameter is expected to be the fault address in this case */
1195   if (exception == EXCEPTION_TLBLOAD || exception == EXCEPTION_TLBSTORE)
1196   {
1197      /* set BadVAddr to the fault address */
1198      UML_GETEXP(block, CPR032(COP0_BadVAddr));                           // mov     [BadVAddr],exp
1199
1200      /* set the upper bits of EntryHi to the fault page */
1201      UML_AND(block, IREG(0), IREG(0), IMM(0xffffe000));                     // and     i0,i0,0xffffe000
1202      UML_AND(block, IREG(1), CPR032(COP0_EntryHi), IMM(0x000000ff));            // and     i1,[EntryHi],0x000000ff
1203      UML_OR(block, CPR032(COP0_EntryHi), IREG(0), IREG(1));                  // or      [EntryHi],i0,i1
1204
1205      /* set the lower bits of Context to the fault page */
1206      UML_SHR(block, IREG(0), IREG(0), IMM(9));                           // shr     i0,i0,9
1207      UML_AND(block, IREG(1), CPR032(COP0_Context), IMM(0xff800000));            // and     i1,[Context],0xff800000
1208      UML_OR(block, CPR032(COP0_Context), IREG(0), IREG(1));                  // or      [Context],i0,i1
1209   }
1210
1211   /* set the EPC and Cause registers */
1212   if (recover)
1213   {
1214      UML_RECOVER(block, IREG(0), MAPVAR_PC);                              // recover i0,PC
1215      UML_RECOVER(block, IREG(1), MAPVAR_CYCLES);                           // recover i1,CYCLES
1216   }
1217
1218   UML_AND(block, IREG(2), CPR032(COP0_Cause), IMM(~0x800000ff));               // and     i2,[Cause],~0x800000ff
1219   UML_TEST(block, IREG(0), IMM(1));                                    // test    i0,1
1220   UML_JMPc(block, IF_Z, next);                                       // jz      <next>
1221   UML_OR(block, IREG(2), IREG(2), IMM(0x80000000));                        // or      i2,i2,0x80000000
1222   UML_SUB(block, IREG(0), IREG(0), IMM(1));                              // sub     i0,i0,1
1223   UML_LABEL(block, next);                                          // <next>:
1224   UML_MOV(block, CPR032(COP0_EPC), IREG(0));                              // mov     [EPC],i0
1225   UML_OR(block, CPR032(COP0_Cause), IREG(2), IMM(exception << 2));            // or      [Cause],i2,exception << 2
1226
1227   /* set EXL in the SR */
1228   UML_OR(block, CPR032(COP0_Status), CPR032(COP0_Status), IMM(SR_EXL));         // or      [Status],[Status],SR_EXL
1229
1230   /* optionally print exceptions */
1231   if (PRINTF_EXCEPTIONS && exception != EXCEPTION_INTERRUPT && exception != EXCEPTION_SYSCALL)
1232      UML_CALLC(block, cfunc_printf_exception, NULL);                        // callc   cfunc_printf_exception,NULL
1233
1234   /* choose our target PC */
1235   UML_MOV(block, IREG(0), IMM(0xbfc00200 + offset));                        // mov     i0,0xbfc00200 + offset
1236   UML_TEST(block, CPR032(COP0_Status), IMM(SR_BEV));                        // test    [Status],SR_BEV
1237   UML_MOVc(block, IF_Z, IREG(0), IMM(0x80000000 + offset));                  // mov     i0,0x80000000 + offset,z
1238
1239   /* adjust cycles */
1240   UML_SUBf(block, MEM(&mips3.core->icount), MEM(&mips3.core->icount), IREG(1), FLAGS_S); // sub icount,icount,cycles,S
1241   UML_EXHc(block, IF_S, mips3.out_of_cycles, IREG(0));                     // exh     out_of_cycles,i0
1242
1243   UML_HASHJMP(block, MEM(&mips3.cstate->mode), IREG(0), mips3.nocode);         // hashjmp <mode>,i0,nocode
1244
1245   drcuml_block_end(block);
1246}
1247
1248
1249/*------------------------------------------------------------------
1250    static_generate_memory_accessor
1251------------------------------------------------------------------*/
1252
1253static void static_generate_memory_accessor(drcuml_state *drcuml, int size, int iswrite, int ismasked, const char *name, drcuml_codehandle **handleptr)
1254{
1255   /* on entry, address is in I0; data for writes is in I1; mask for accesses is in I2 */
1256   /* on exit, read result is in I0 */
1257   /* routine trashes I0-I3 */
1258   drcuml_codehandle *exception_target = mips3.exception[iswrite ? EXCEPTION_TLBSTORE : EXCEPTION_TLBLOAD];
1259   drcuml_block *block;
1260   jmp_buf errorbuf;
1261   int label = 0;
1262   int ramnum;
1263
1264   /* if we get an error back, we're screwed */
1265   if (setjmp(errorbuf) != 0)
1266      fatalerror("Unrecoverable error in static_generate_exception");
1267
1268   /* begin generating */
1269   block = drcuml_block_begin(drcuml, 1024, &errorbuf);
1270
1271   /* add a global entry for this */
1272   alloc_handle(drcuml, handleptr, name);
1273   UML_HANDLE(block, *handleptr);                                       // handle  *handleptr
1274
1275   /* general case: assume paging and perform a translation */
1276   UML_SHR(block, IREG(3), IREG(0), IMM(12));                              // shr     i3,i0,12
1277   UML_LOAD4(block, IREG(3), mips3.core->tlb_table, IREG(3));                  // load4   i3,[tlb_table],i3
1278   UML_TEST(block, IREG(3), IMM(iswrite ? 1 : 2));                           // test    i3,iswrite ? 1 : 2
1279   UML_EXHc(block, IF_NZ, exception_target, IREG(0));                        // exnz    exception_target,i0
1280   UML_AND(block, IREG(0), IREG(0), IMM(0xfff));                           // and     i0,i0,0xfff
1281   UML_AND(block, IREG(3), IREG(3), IMM(~0xfff));                           // and     i3,i3,~0xfff
1282   UML_OR(block, IREG(0), IREG(0), IREG(3));                              // or      i0,i0,i3
1283
3471284   for (ramnum = 0; ramnum < MIPS3_MAX_FASTRAM; ramnum++)
348      if (mips3.fastram[ramnum].base != NULL && (!iswrite || !mips3.fastram[ramnum].readonly) &&
349         addr >= mips3.fastram[ramnum].start && addr <= mips3.fastram[ramnum].end)
1285      if (!Machine->debug_mode && mips3.fastram[ramnum].base != NULL && (!iswrite || !mips3.fastram[ramnum].readonly))
3501286      {
351         return (UINT8 *)mips3.fastram[ramnum].base + (addr - mips3.fastram[ramnum].start);
1287         void *fastbase = (UINT8 *)mips3.fastram[ramnum].base - mips3.fastram[ramnum].start;
1288         UINT32 skip = label++;
1289         if (mips3.fastram[ramnum].end != 0xffffffff)
1290         {
1291            UML_CMP(block, IREG(0), IMM(mips3.fastram[ramnum].end));         // cmp     i0,end
1292            UML_JMPc(block, IF_A, skip);                              // ja      skip
1293         }
1294         if (mips3.fastram[ramnum].start != 0x00000000)
1295         {
1296            UML_CMP(block, IREG(0), IMM(mips3.fastram[ramnum].start));         // cmp     i0,fastram_start
1297            UML_JMPc(block, IF_B, skip);                              // jb      skip
1298         }
1299
1300         if (!iswrite)
1301         {
1302            if (size == 1)
1303            {
1304               UML_XOR(block, IREG(0), IREG(0), IMM(mips3.core->bigendian ? BYTE4_XOR_BE(0) : BYTE4_XOR_LE(0)));
1305                                                               // xor     i0,i0,bytexor
1306               UML_LOAD1U(block, IREG(0), fastbase, IREG(0));               // load1u  i0,fastbase,i0
1307            }
1308            else if (size == 2)
1309            {
1310               UML_SHR(block, IREG(0), IREG(0), IMM(1));                  // shr     i0,i0,1
1311               UML_XOR(block, IREG(0), IREG(0), IMM(mips3.core->bigendian ? BYTE_XOR_BE(0) : BYTE_XOR_LE(0)));
1312                                                               // xor     i0,i0,bytexor
1313               UML_LOAD2U(block, IREG(0), fastbase, IREG(0));               // load2u  i0,fastbase,i0
1314            }
1315            else if (size == 4)
1316            {
1317               UML_SHR(block, IREG(0), IREG(0), IMM(2));                  // shr     i0,i0,2
1318               UML_LOAD4(block, IREG(0), fastbase, IREG(0));               // load4   i0,fastbase,i0
1319            }
1320            else if (size == 8)
1321            {
1322               UML_SHR(block, IREG(0), IREG(0), IMM(3));                  // shr     i0,i0,3
1323               UML_DLOAD8(block, IREG(0), fastbase, IREG(0));               // dload8  i0,fastbase,i0
1324               UML_DROR(block, IREG(0), IREG(0), IMM(32 * (mips3.core->bigendian ? BYTE_XOR_BE(0) : BYTE_XOR_LE(0))));
1325                                                               // dror    i0,i0,32*bytexor
1326            }
1327            UML_RET(block);                                          // ret
1328         }
1329         else
1330         {
1331            if (size == 1)
1332            {
1333               UML_XOR(block, IREG(0), IREG(0), IMM(mips3.core->bigendian ? BYTE4_XOR_BE(0) : BYTE4_XOR_LE(0)));
1334                                                               // xor     i0,i0,bytexor
1335               UML_STORE1(block, fastbase, IREG(0), IREG(1));               // store1  fastbase,i0,i1
1336            }
1337            else if (size == 2)
1338            {
1339               UML_SHR(block, IREG(0), IREG(0), IMM(1));                  // shr     i0,i0,1
1340               UML_XOR(block, IREG(0), IREG(0), IMM(mips3.core->bigendian ? BYTE_XOR_BE(0) : BYTE_XOR_LE(0)));
1341                                                               // xor     i0,i0,bytexor
1342               UML_STORE2(block, fastbase, IREG(0), IREG(1));               // store2  fastbase,i0,i1
1343            }
1344            else if (size == 4)
1345            {
1346               UML_SHR(block, IREG(0), IREG(0), IMM(2));                  // shr     i0,i0,2
1347               if (ismasked)
1348               {
1349                  UML_LOAD4(block, IREG(3), fastbase, IREG(0));            // load4   i3,fastbase,i0
1350                  UML_AND(block, IREG(1), IREG(1), IREG(2));               // and     i1,i1,i2
1351                  UML_XOR(block, IREG(2), IREG(2), IMM(0xffffffff));         // xor     i2,i2,0xfffffffff
1352                  UML_AND(block, IREG(3), IREG(3), IREG(2));               // and     i3,i3,i2
1353                  UML_OR(block, IREG(1), IREG(1), IREG(3));               // or      i1,i1,i3
1354               }
1355               UML_STORE4(block, fastbase, IREG(0), IREG(1));               // store4  fastbase,i0,i1
1356            }
1357            else if (size == 8)
1358            {
1359               UML_SHR(block, IREG(0), IREG(0), IMM(3));                  // shr     i0,i0,3
1360               UML_DROR(block, IREG(1), IREG(1), IMM(32 * (mips3.core->bigendian ? BYTE_XOR_BE(0) : BYTE_XOR_LE(0))));
1361                                                               // dror    i1,i1,32*bytexor
1362               if (ismasked)
1363               {
1364                  UML_DROR(block, IREG(2), IREG(2), IMM(32 * (mips3.core->bigendian ? BYTE_XOR_BE(0) : BYTE_XOR_LE(0))));
1365                                                               // dror    i2,i2,32*bytexor
1366                  UML_DLOAD8(block, IREG(3), fastbase, IREG(0));            // dload8  i3,fastbase,i0
1367                  UML_DAND(block, IREG(1), IREG(1), IREG(2));               // dand    i1,i1,i2
1368                  UML_DXOR(block, IREG(2), IREG(2), IMM(U64(0xffffffffffffffff)));
1369                                                               // dxor    i2,i2,0xfffffffffffffffff
1370                  UML_DAND(block, IREG(3), IREG(3), IREG(2));               // dand    i3,i3,i2
1371                  UML_DOR(block, IREG(1), IREG(1), IREG(3));               // dor     i1,i1,i3
1372               }
1373               UML_DSTORE8(block, fastbase, IREG(0), IREG(1));               // dstore8 fastbase,i0,i1
1374            }
1375            UML_RET(block);                                          // ret
1376         }
1377
1378         UML_LABEL(block, skip);                                    // skip:
3521379      }
3531380
354   return NULL;
1381   switch (size)
1382   {
1383      case 1:
1384         if (iswrite)
1385            UML_WRITE1(block, PROGRAM, IREG(0), IREG(1));                  // write1  i0,i1
1386         else
1387            UML_READ1U(block, IREG(0), PROGRAM, IREG(0));                  // read1u  i0,i0
1388         break;
1389
1390      case 2:
1391         if (iswrite)
1392            UML_WRITE2(block, PROGRAM, IREG(0), IREG(1));                  // write2  i0,i1
1393         else
1394            UML_READ2U(block, IREG(0), PROGRAM, IREG(0));                  // read2u  i0,i0
1395         break;
1396
1397      case 4:
1398         if (iswrite)
1399         {
1400            if (!ismasked)
1401               UML_WRITE4(block, PROGRAM, IREG(0), IREG(1));               // write4  PROGRAM,i0,i1
1402            else
1403               UML_WRIT4M(block, PROGRAM, IREG(0), IREG(2), IREG(1));         // writ4m  PROGRAM,i0,i2,i1
1404         }
1405         else
1406         {
1407            if (!ismasked)
1408               UML_READ4(block, IREG(0), PROGRAM, IREG(0));               // read4   i0,PROGRAM,i0
1409            else
1410               UML_READ4M(block, IREG(0), PROGRAM, IREG(0), IREG(2));         // read4m  i0,PROGRAM,i0,i2
1411         }
1412         break;
1413
1414      case 8:
1415         if (iswrite)
1416         {
1417            if (!ismasked)
1418               UML_DWRITE8(block, PROGRAM, IREG(0), IREG(1));               // dwrite8 PROGRAM,i0,i1
1419            else
1420               UML_DWRIT8M(block, PROGRAM, IREG(0), IREG(2), IREG(1));         // dwrit8m PROGRAM,i0,i2,i1
1421         }
1422         else
1423         {
1424            if (!ismasked)
1425               UML_DREAD8(block, IREG(0), PROGRAM, IREG(0));               // dread8  i0,PROGRAM,i0
1426            else
1427               UML_DREAD8M(block, IREG(0), PROGRAM, IREG(0), IREG(2));         // dread8m i0,PROGRAM,i0,i2
1428         }
1429         break;
1430   }
1431   UML_RET(block);                                                   // ret
1432
1433   drcuml_block_end(block);
3551434}
1435
1436
1437
1438/***************************************************************************
1439    CODE GENERATION
1440***************************************************************************/
1441
1442/*-------------------------------------------------
1443    generate_update_cycles - generate code to
1444    subtract cycles from the icount and generate
1445    an exception if out
1446-------------------------------------------------*/
1447
1448static void generate_update_cycles(drcuml_block *block, compiler_state *compiler, UINT32 nextpc, UINT32 *mempc, int allow_exception)
1449{
1450   /* check software interrupts if pending */
1451   if (compiler->checksoftints)
1452   {
1453      drcuml_codelabel skip;
1454
1455      compiler->checksoftints = FALSE;
1456      UML_AND(block, IREG(0), CPR032(COP0_Cause), CPR032(COP0_Status));         // and     i0,[Cause],[Status]
1457      UML_ANDf(block, IREG(0), IREG(0), IMM(0x0300), FLAGS_Z);               // and     i0,i0,0x0300,Z
1458      UML_JMPc(block, IF_Z, skip = compiler->labelnum++);                     // jmp     skip,Z
1459      if (mempc == NULL)
1460         UML_MOV(block, IREG(0), IMM(nextpc));                           // mov     i0,nextpc
1461      else
1462         UML_MOV(block, IREG(0), MEM(mempc));                           // mov     i0,[mempc]
1463      UML_MOV(block, IREG(1), IMM(compiler->cycles));                        // mov     i1,cycles
1464      UML_CALLH(block, mips3.exception_norecover[EXCEPTION_INTERRUPT]);         // callh   interrupt_norecover
1465      UML_LABEL(block, skip);                                       // skip:
1466   }
1467
1468   /* check full interrupts if pending */
1469   if (compiler->checkints)
1470   {
1471      drcuml_codelabel skip;
1472
1473      compiler->checkints = FALSE;
1474      UML_AND(block, IREG(0), CPR032(COP0_Cause), CPR032(COP0_Status));         // and     i0,[Cause],[Status]
1475      UML_ANDf(block, IREG(0), IREG(0), IMM(0xfc00), FLAGS_Z);               // and     i0,i0,0xfc00,Z
1476      UML_JMPc(block, IF_Z, skip = compiler->labelnum++);                     // jmp     skip,Z
1477      UML_TEST(block, CPR032(COP0_Status), IMM(SR_IE));                     // test    [Status],SR_IE
1478      UML_JMPc(block, IF_Z, skip);                                    // jmp     skip,Z
1479      UML_TEST(block, CPR032(COP0_Status), IMM(SR_EXL | SR_ERL));               // test    [Status],SR_EXL | SR_ERL
1480      UML_JMPc(block, IF_NZ, skip);                                    // jmp     skip,NZ
1481      if (mempc == NULL)
1482         UML_MOV(block, IREG(0), IMM(nextpc));                           // mov     i0,nextpc
1483      else
1484         UML_MOV(block, IREG(0), MEM(mempc));                           // mov     i0,[mempc]
1485      UML_MOV(block, IREG(1), IMM(compiler->cycles));                        // mov     i1,cycles
1486      UML_CALLH(block, mips3.exception_norecover[EXCEPTION_INTERRUPT]);         // callh   interrupt_norecover
1487      UML_LABEL(block, skip);                                       // skip:
1488   }
1489
1490   /* account for cycles */
1491   if (compiler->cycles > 0)
1492   {
1493      UML_SUBf(block, MEM(&mips3.core->icount), MEM(&mips3.core->icount), MAPVAR_CYCLES, allow_exception ? FLAGS_S : 0);
1494                                                               // sub     icount,icount,cycles,S
1495      UML_MAPVAR(block, MAPVAR_CYCLES, 0);                              // mapvar  cycles,0
1496      if (allow_exception)
1497      {
1498         if (mempc == NULL)
1499            UML_EXHc(block, IF_S, mips3.out_of_cycles, IMM(nextpc));         // exh     out_of_cycles,nextpc
1500         else
1501            UML_EXHc(block, IF_S, mips3.out_of_cycles, MEM(mempc));            // exh     out_of_cycles,<mempc>
1502      }
1503   }
1504   compiler->cycles = 0;
1505}
1506
1507
1508/*-------------------------------------------------
1509    generate_checksum_block - generate code to
1510    validate a sequence of opcodes
1511-------------------------------------------------*/
1512
1513static void generate_checksum_block(drcuml_block *block, compiler_state *compiler, const opcode_desc *seqhead, const opcode_desc *seqlast)
1514{
1515   const opcode_desc *curdesc;
1516   if (LOG_UML)
1517      UML_COMMENT(block, "[Validation for %08X]", seqhead->pc);               // comment
1518
1519   /* loose verify or single instruction: just compare and fail */
1520   if (!(mips3.drcoptions & MIPS3DRC_STRICT_VERIFY) || seqhead->next == NULL)
1521   {
1522      UML_LOAD4(block, IREG(0), seqhead->opptr.l, IMM(0));                  // load4   i0,*opptr
1523      UML_CMP(block, IREG(0), IMM(*seqhead->opptr.l));                     // cmp     i0,*opptr
1524      UML_EXHc(block, IF_NE, mips3.nocode, IMM(epc(seqhead)));               // exne    nocode,seqhead->pc
1525   }
1526
1527   /* full verification; sum up everything */
1528   else
1529   {
1530      UINT32 sum = 0;
1531      UML_LOAD4(block, IREG(0), seqhead->opptr.l, IMM(0));                  // load4   i0,*opptr
1532      sum += *seqhead->opptr.l;
1533      for (curdesc = seqhead->next; curdesc != seqlast->next; curdesc = curdesc->next)
1534      {
1535         UML_LOAD4(block, IREG(1), curdesc->opptr.l, IMM(0));               // load4   i1,*opptr
1536         UML_ADD(block, IREG(0), IREG(0), IREG(1));                        // add     i0,i0,i1
1537         sum += *curdesc->opptr.l;
1538      }
1539      UML_CMP(block, IREG(0), IMM(sum));                                 // cmp     i0,sum
1540      UML_EXHc(block, IF_NE, mips3.nocode, IMM(epc(seqhead)));               // exne    nocode,seqhead->pc
1541   }
1542}
1543
1544
1545/*-------------------------------------------------
1546    generate_sequence_instruction - generate code
1547    for a single instruction in a sequence
1548-------------------------------------------------*/
1549
1550static void generate_sequence_instruction(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc)
1551{
1552   offs_t expc;
1553   int hotnum;
1554
1555   /* add an entry for the log */
1556   if (LOG_UML)
1557      log_add_disasm_comment(block, desc->pc, *desc->opptr.l);
1558
1559   /* set the PC map variable */
1560   expc = (desc->flags & OPFLAG_IN_DELAY_SLOT) ? desc->pc - 3 : desc->pc;
1561   UML_MAPVAR(block, MAPVAR_PC, expc);                                    // mapvar  PC,expc
1562
1563   /* accumulate total cycles */
1564   compiler->cycles += desc->cycles;
1565
1566   /* is this a hotspot? */
1567   for (hotnum = 0; hotnum < MIPS3_MAX_HOTSPOTS; hotnum++)
1568      if (desc->pc == mips3.hotspot[hotnum].pc && *desc->opptr.l == mips3.hotspot[hotnum].opcode)
1569      {
1570         compiler->cycles += mips3.hotspot[hotnum].cycles;
1571         break;
1572      }
1573
1574   /* update the icount map variable */
1575   UML_MAPVAR(block, MAPVAR_CYCLES, compiler->cycles);                        // mapvar  CYCLES,compiler->cycles
1576
1577   /* if we want a probe, add it here */
1578   if (desc->pc == PROBE_ADDRESS)
1579      UML_CALLC(block, cfunc_printf_probe, desc->pc);                        // callc   cfunc_printf_probe,desc->pc
1580
1581   /* if we are debugging, call the debugger */
1582   if (Machine->debug_mode)
1583   {
1584      UML_MOV(block, MEM(&mips3.core->pc), IMM(desc->pc));                  // mov     [pc],desc->pc
1585      UML_DEBUG(block, IMM(desc->pc));                                 // debug   desc->pc
1586   }
1587
1588   /* validate our TLB entry at this PC; if we fail, we need to recompile */
1589   if ((desc->flags & OPFLAG_VALIDATE_TLB) && (desc->pc < 0x80000000 || desc->pc >= 0xc0000000))
1590   {
1591      UML_CMP(block, MEM(&mips3.core->tlb_table[desc->pc >> 12]),
1592                     IMM(mips3.core->tlb_table[desc->pc >> 12]));               // cmp     [tlbentry],*tlbentry
1593      UML_EXHc(block, IF_NE, mips3.tlb_mismatch, IMM(0));                     // exh     tlb_mismatch,0,NE
1594   }
1595
1596   /* if this is an invalid opcode, generate the exception now */
1597   if (desc->flags & OPFLAG_INVALID_OPCODE)
1598      UML_EXH(block, mips3.exception[EXCEPTION_INVALIDOP], IMM(0));            // exh     invalidop,0
1599
1600   /* otherwise, it's a regular instruction */
1601   else
1602   {
1603      /* compile the instruction */
1604      if (!generate_opcode(block, compiler, desc))
1605         fatalerror("Unimplemented op %08X (%02X,%02X)", *desc->opptr.l, *desc->opptr.l >> 26, *desc->opptr.l & 0x3f);
1606   }
1607}
1608
1609
1610/*------------------------------------------------------------------
1611    generate_delay_slot_and_branch
1612------------------------------------------------------------------*/
1613
1614static void generate_delay_slot_and_branch(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc, UINT8 linkreg)
1615{
1616   compiler_state compiler_temp = *compiler;
1617   UINT32 op = *desc->opptr.l;
1618
1619   /* set the link if needed */
1620   if (linkreg != 0)
1621      UML_DSEXT4(block, R64(linkreg), IMM((INT32)(desc->pc + 8)));            // dsext4  <linkreg>,desc->pc + 8
1622
1623   /* compile the delay slot using temporary compiler state */
1624   assert(desc->delay != NULL);
1625   generate_sequence_instruction(block, &compiler_temp, desc->delay);            // <next instruction>
1626
1627   /* update the cycles and jump through the hash table to the target */
1628   if (desc->targetpc != BRANCH_TARGET_DYNAMIC)
1629   {
1630      generate_update_cycles(block, &compiler_temp, desc->targetpc, NULL, TRUE);   // <subtract cycles>
1631      if (desc->flags & OPFLAG_INTRABLOCK_BRANCH)
1632         UML_JMP(block, desc->targetpc | 0x80000000);                     // jmp     desc->targetpc | 0x80000000
1633      else
1634         UML_HASHJMP(block, IMM(mips3.cstate->mode), IMM(desc->targetpc), mips3.nocode);
1635                                                               // hashjmp <mode>,desc->targetpc,nocode
1636   }
1637   else
1638   {
1639      generate_update_cycles(block, &compiler_temp, 0, LOPTR(&mips3.core->r[RSREG]), TRUE);
1640                                                               // <subtract cycles>
1641      UML_HASHJMP(block, IMM(mips3.cstate->mode), R32(RSREG), mips3.nocode);      // hashjmp <mode>,<rsreg>,nocode
1642   }
1643
1644   /* update the label */
1645   compiler->labelnum = compiler_temp.labelnum;
1646
1647   /* reset the mapvar to the current cycles */
1648   UML_MAPVAR(block, MAPVAR_CYCLES, compiler->cycles);                        // mapvar  CYCLES,compiler->cycles
1649}
1650
1651
1652/*-------------------------------------------------
1653    generate_opcode - generate code for a specific
1654    opcode
1655-------------------------------------------------*/
1656
1657static int generate_opcode(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc)
1658{
1659   int in_delay_slot = ((desc->flags & OPFLAG_IN_DELAY_SLOT) != 0);
1660   UINT32 op = *desc->opptr.l;
1661   UINT8 opswitch = op >> 26;
1662   drcuml_codelabel skip;
1663
1664   switch (opswitch)
1665   {
1666      /* ----- sub-groups ----- */
1667
1668      case 0x00:   /* SPECIAL - MIPS I */
1669         return generate_special(block, compiler, desc);
1670
1671      case 0x01:   /* REGIMM - MIPS I */
1672         return generate_regimm(block, compiler, desc);
1673
1674      case 0x1c:   /* IDT-specific */
1675         return generate_idt(block, compiler, desc);
1676
1677
1678      /* ----- jumps and branches ----- */
1679
1680      case 0x02:   /* J - MIPS I */
1681         generate_delay_slot_and_branch(block, compiler, desc, 0);            // <next instruction + hashjmp>
1682         return TRUE;
1683
1684      case 0x03:   /* JAL - MIPS I */
1685         generate_delay_slot_and_branch(block, compiler, desc, 31);            // <next instruction + hashjmp>
1686         return TRUE;
1687
1688      case 0x04:   /* BEQ - MIPS I */
1689      case 0x14:   /* BEQL - MIPS II */
1690         UML_DCMP(block, R64(RSREG), R64(RTREG));                        // dcmp    <rsreg>,<rtreg>
1691         UML_JMPc(block, IF_NE, skip = compiler->labelnum++);               // jmp     skip,NE
1692         generate_delay_slot_and_branch(block, compiler, desc, 0);            // <next instruction + hashjmp>
1693         UML_LABEL(block, skip);                                    // skip:
1694         return TRUE;
1695
1696      case 0x05:   /* BNE - MIPS I */
1697      case 0x15:   /* BNEL - MIPS II */
1698         UML_DCMP(block, R64(RSREG), R64(RTREG));                        // dcmp    <rsreg>,<rtreg>
1699         UML_JMPc(block, IF_E, skip = compiler->labelnum++);                  // jmp     skip,E
1700         generate_delay_slot_and_branch(block, compiler, desc, 0);            // <next instruction + hashjmp>
1701         UML_LABEL(block, skip);                                    // skip:
1702         return TRUE;
1703
1704      case 0x06:   /* BLEZ - MIPS I */
1705      case 0x16:   /* BLEZL - MIPS II */
1706         if (RSREG != 0)
1707         {
1708            UML_DCMP(block, R64(RSREG), IMM(0));                        // dcmp    <rsreg>,0
1709            UML_JMPc(block, IF_G, skip = compiler->labelnum++);               // jmp     skip,G
1710            generate_delay_slot_and_branch(block, compiler, desc, 0);         // <next instruction + hashjmp>
1711            UML_LABEL(block, skip);                                 // skip:
1712         }
1713         else
1714            generate_delay_slot_and_branch(block, compiler, desc, 0);         // <next instruction + hashjmp>
1715         return TRUE;
1716
1717      case 0x07:   /* BGTZ - MIPS I */
1718      case 0x17:   /* BGTZL - MIPS II */
1719         UML_DCMP(block, R64(RSREG), IMM(0));                           // dcmp    <rsreg>,0
1720         UML_JMPc(block, IF_LE, skip = compiler->labelnum++);               // jmp     skip,LE
1721         generate_delay_slot_and_branch(block, compiler, desc, 0);            // <next instruction + hashjmp>
1722         UML_LABEL(block, skip);                                    // skip:
1723         return TRUE;
1724
1725
1726      /* ----- immediate arithmetic ----- */
1727
1728      case 0x0f:   /* LUI - MIPS I */
1729         if (RTREG != 0)
1730            UML_DSEXT4(block, R64(RTREG), IMM(SIMMVAL << 16));               // dsext4  <rtreg>,SIMMVAL << 16
1731         return TRUE;
1732
1733      case 0x08:   /* ADDI - MIPS I */
1734         if (mips3.drcoptions & MIPS3DRC_CHECK_OVERFLOWS)
1735         {
1736            UML_ADDf(block, IREG(0), R32(RSREG), IMM(SIMMVAL), FLAGS_V);      // add     i0,<rsreg>,SIMMVAL,V
1737            UML_EXHc(block, IF_V, mips3.exception[EXCEPTION_OVERFLOW], IMM(0));   // exh    overflow,0
1738         }
1739         else
1740            UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL));               // add     i0,<rsreg>,SIMMVAL
1741         if (RTREG != 0)
1742            UML_DSEXT4(block, R64(RTREG), IREG(0));                        // dsext4  <rtreg>,i0
1743         return TRUE;
1744
1745      case 0x09:   /* ADDIU - MIPS I */
1746         if (RTREG != 0)
1747         {
1748            UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL));               // add     i0,<rsreg>,SIMMVAL,V
1749            UML_DSEXT4(block, R64(RTREG), IREG(0));                        // dsext4  <rtreg>,i0
1750         }
1751         return TRUE;
1752
1753      case 0x18:   /* DADDI - MIPS III */
1754         if (mips3.drcoptions & MIPS3DRC_CHECK_OVERFLOWS)
1755         {
1756            UML_DADDf(block, IREG(0), R64(RSREG), IMM(SIMMVAL), FLAGS_V);      // dadd    i0,<rsreg>,SIMMVAL,V
1757            UML_EXHc(block, IF_V, mips3.exception[EXCEPTION_OVERFLOW], IMM(0));   // exh    overflow,0
1758         }
1759         else
1760            UML_DADD(block, IREG(0), R64(RSREG), IMM(SIMMVAL));               // dadd    i0,<rsreg>,SIMMVAL
1761         if (RTREG != 0)
1762            UML_DMOV(block, R64(RTREG), IREG(0));                        // dmov    <rtreg>,i0
1763         return TRUE;
1764
1765      case 0x19:   /* DADDIU - MIPS III */
1766         if (RTREG != 0)
1767            UML_DADD(block, R64(RTREG), R64(RSREG), IMM(SIMMVAL));            // dadd    <rtreg>,<rsreg>,SIMMVAL
1768         return TRUE;
1769
1770      case 0x0c:   /* ANDI - MIPS I */
1771         if (RTREG != 0)
1772            UML_DAND(block, R64(RTREG), R64(RSREG), IMM(UIMMVAL));            // dand    <rtreg>,<rsreg>,UIMMVAL
1773         return TRUE;
1774
1775      case 0x0d:   /* ORI - MIPS I */
1776         if (RTREG != 0)
1777            UML_DOR(block, R64(RTREG), R64(RSREG), IMM(UIMMVAL));            // dor     <rtreg>,<rsreg>,UIMMVAL
1778         return TRUE;
1779
1780      case 0x0e:   /* XORI - MIPS I */
1781         if (RTREG != 0)
1782            UML_DXOR(block, R64(RTREG), R64(RSREG), IMM(UIMMVAL));            // dxor    <rtreg>,<rsreg>,UIMMVAL
1783         return TRUE;
1784
1785      case 0x0a:   /* SLTI - MIPS I */
1786         if (RTREG != 0)
1787         {
1788            UML_DCMP(block, R64(RSREG), IMM(SIMMVAL));                     // dcmp    <rsreg>,SIMMVAL
1789            UML_DFLAGS(block, R64(RTREG), (UINT64)~0, mips3.cstate->slt_table);   // dflags  <rtreg>,~0,slt_table
1790         }
1791         return TRUE;
1792
1793      case 0x0b:   /* SLTIU - MIPS I */
1794         if (RTREG != 0)
1795         {
1796            UML_DCMP(block, R64(RSREG), IMM(SIMMVAL));                     // dcmp    <rsreg>,SIMMVAL
1797            UML_DFLAGS(block, R64(RTREG), (UINT64)~0, mips3.cstate->sltu_table);// dflags  <rtreg>,~0,sltu_table
1798         }
1799         return TRUE;
1800
1801
1802      /* ----- memory load operations ----- */
1803
1804      case 0x20:   /* LB - MIPS I */
1805         UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL));                  // add     i0,<rsreg>,SIMMVAL
1806         UML_CALLH(block, mips3.read8);                                 // callh   read8
1807         if (RTREG != 0)
1808            UML_DSEXT1(block, R64(RTREG), IREG(0));                        // dsext1  <rtreg>,i0
1809         if (!in_delay_slot)
1810            generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE);
1811         return TRUE;
1812
1813      case 0x21:   /* LH - MIPS I */
1814         UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL));                  // add     i0,<rsreg>,SIMMVAL
1815         UML_CALLH(block, mips3.read16);                                 // callh   read16
1816         if (RTREG != 0)
1817            UML_DSEXT2(block, R64(RTREG), IREG(0));                        // dsext2  <rtreg>,i0
1818         if (!in_delay_slot)
1819            generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE);
1820         return TRUE;
1821
1822      case 0x23:   /* LW - MIPS I */
1823         UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL));                  // add     i0,<rsreg>,SIMMVAL
1824         UML_CALLH(block, mips3.read32);                                 // callh   read32
1825         if (RTREG != 0)
1826            UML_DSEXT4(block, R64(RTREG), IREG(0));                        // dsext4  <rtreg>,i0
1827         if (!in_delay_slot)
1828            generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE);
1829         return TRUE;
1830
1831      case 0x24:   /* LBU - MIPS I */
1832         UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL));                  // add     i0,<rsreg>,SIMMVAL
1833         UML_CALLH(block, mips3.read8);                                 // callh   read8
1834         if (RTREG != 0)
1835            UML_DZEXT1(block, R64(RTREG), IREG(0));                        // dzext1  <rtreg>,i0
1836         if (!in_delay_slot)
1837            generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE);
1838         return TRUE;
1839
1840      case 0x25:   /* LHU - MIPS I */
1841         UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL));                  // add     i0,<rsreg>,SIMMVAL
1842         UML_CALLH(block, mips3.read16);                                 // callh   read16
1843         if (RTREG != 0)
1844            UML_DZEXT2(block, R64(RTREG), IREG(0));                        // dzext2  <rtreg>,i0
1845         if (!in_delay_slot)
1846            generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE);
1847         return TRUE;
1848
1849      case 0x27:   /* LWU - MIPS III */
1850         UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL));                  // add     i0,<rsreg>,SIMMVAL
1851         UML_CALLH(block, mips3.read32);                                 // callh   read32
1852         if (RTREG != 0)
1853            UML_DZEXT4(block, R64(RTREG), IREG(0));                        // dzext4  <rtreg>,i0
1854         if (!in_delay_slot)
1855            generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE);
1856         return TRUE;
1857
1858      case 0x37:   /* LD - MIPS III */
1859         UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL));                  // add     i0,<rsreg>,SIMMVAL
1860         UML_CALLH(block, mips3.read64);                                 // callh   read64
1861         if (RTREG != 0)
1862            UML_DMOV(block, R64(RTREG), IREG(0));                        // dmov    <rtreg>,i0
1863         if (!in_delay_slot)
1864            generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE);
1865         return TRUE;
1866
1867      case 0x22:   /* LWL - MIPS I */
1868         UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL));                  // add     i0,<rsreg>,SIMMVAL
1869         UML_SHL(block, IREG(4), IREG(0), IMM(3));                        // shl     i4,i0,3
1870         if (!mips3.core->bigendian)
1871            UML_XOR(block, IREG(4), IREG(4), IMM(0x18));                  // xor     i4,i4,0x18
1872         UML_SHR(block, IREG(2), IMM(~0), IREG(4));                        // shr     i2,~0,i4
1873         UML_CALLH(block, mips3.read32mask);                              // callh   read32mask
1874         if (RTREG != 0)
1875         {
1876            UML_SHL(block, IREG(2), IMM(~0), IREG(4));                     // shl     i2,~0,i4
1877            UML_XOR(block, IREG(2), IREG(2), IMM(~0));                     // xor     i2,i2,~0
1878            UML_SHL(block, IREG(0), IREG(0), IREG(4));                     // shl     i0,i0,i4
1879            UML_AND(block, IREG(2), IREG(2), R32(RTREG));                  // and     i2,i2,<rtreg>
1880            UML_OR(block, IREG(0), IREG(0), IREG(2));                     // or      i0,i0,i2
1881            UML_DSEXT4(block, R64(RTREG), IREG(0));                        // dsext4  <rtreg>,i0
1882         }
1883         if (!in_delay_slot)
1884            generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE);
1885         return TRUE;
1886
1887      case 0x26:   /* LWR - MIPS I */
1888         UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL));                  // add     i0,<rsreg>,SIMMVAL
1889         UML_SHL(block, IREG(4), IREG(0), IMM(3));                        // shl     i4,i0,3
1890         if (mips3.core->bigendian)
1891            UML_XOR(block, IREG(4), IREG(4), IMM(0x18));                  // xor     i4,i4,0x18
1892         UML_SHL(block, IREG(2), IMM(~0), IREG(4));                        // shl     i2,~0,i4
1893         UML_CALLH(block, mips3.read32mask);                              // callh   read32mask
1894         if (RTREG != 0)
1895         {
1896            UML_SHR(block, IREG(2), IMM(~0), IREG(4));                     // shr     i2,~0,i4
1897            UML_XOR(block, IREG(2), IREG(2), IMM(~0));                     // xor     i2,i2,~0
1898            UML_SHR(block, IREG(0), IREG(0), IREG(4));                     // shr     i0,i0,i4
1899            UML_AND(block, IREG(2), IREG(2), R32(RTREG));                  // and     i2,i2,<rtreg>
1900            UML_OR(block, IREG(0), IREG(0), IREG(2));                     // or      i0,i0,i2
1901            UML_DSEXT4(block, R64(RTREG), IREG(0));                        // dsext4  <rtreg>,i0
1902         }
1903         if (!in_delay_slot)
1904            generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE);
1905         return TRUE;
1906
1907      case 0x1a:   /* LDL - MIPS III */
1908         UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL));                  // add     i0,<rsreg>,SIMMVAL
1909         UML_SHL(block, IREG(4), IREG(0), IMM(3));                        // shl     i4,i0,3
1910         if (!mips3.core->bigendian)
1911            UML_XOR(block, IREG(4), IREG(4), IMM(0x38));                  // xor     i4,i4,0x38
1912         UML_DSHR(block, IREG(2), IMM((UINT64)~0), IREG(4));                  // dshr    i2,~0,i4
1913         UML_CALLH(block, mips3.read64mask);                              // callh   read64mask
1914         if (RTREG != 0)
1915         {
1916            UML_DSHL(block, IREG(2), IMM((UINT64)~0), IREG(4));               // dshl    i2,~0,i4
1917            UML_DXOR(block, IREG(2), IREG(2), IMM((UINT64)~0));               // dxor    i2,i2,~0
1918            UML_DSHL(block, IREG(0), IREG(0), IREG(4));                     // dshl    i0,i0,i4
1919            UML_DAND(block, IREG(2), IREG(2), R64(RTREG));                  // dand    i2,i2,<rtreg>
1920            UML_DOR(block, R64(RTREG), IREG(0), IREG(2));                  // dor     <rtreg>,i0,i2
1921         }
1922         if (!in_delay_slot)
1923            generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE);
1924         return TRUE;
1925
1926      case 0x1b:   /* LDR - MIPS III */
1927         UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL));                  // add     i0,<rsreg>,SIMMVAL
1928         UML_SHL(block, IREG(4), IREG(0), IMM(3));                        // shl     i4,i0,3
1929         if (mips3.core->bigendian)
1930            UML_XOR(block, IREG(4), IREG(4), IMM(0x38));                  // xor     i4,i4,0x38
1931         UML_DSHL(block, IREG(2), IMM((UINT64)~0), IREG(4));                  // dshl    i2,~0,i4
1932         UML_CALLH(block, mips3.read64mask);                              // callh   read64mask
1933         if (RTREG != 0)
1934         {
1935            UML_DSHR(block, IREG(2), IMM((UINT64)~0), IREG(4));               // dshr    i2,~0,i4
1936            UML_DXOR(block, IREG(2), IREG(2), IMM((UINT64)~0));               // dxor    i2,i2,~0
1937            UML_DSHR(block, IREG(0), IREG(0), IREG(4));                     // dshr    i0,i0,i4
1938            UML_DAND(block, IREG(2), IREG(2), R64(RTREG));                  // dand    i2,i2,<rtreg>
1939            UML_DOR(block, R64(RTREG), IREG(0), IREG(2));                  // dor     <rtreg>,i0,i2
1940         }
1941         if (!in_delay_slot)
1942            generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE);
1943         return TRUE;
1944
1945      case 0x31:   /* LWC1 - MIPS I */
1946         UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL));                  // add     i0,<rsreg>,SIMMVAL
1947         UML_CALLH(block, mips3.read32);                                 // callh   read32
1948         UML_MOV(block, FPR32(RTREG), IREG(0));                           // mov     <cpr1_rt>,i0
1949         if (!in_delay_slot)
1950            generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE);
1951         return TRUE;
1952
1953      case 0x35:   /* LDC1 - MIPS III */
1954         UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL));                  // add     i0,<rsreg>,SIMMVAL
1955         UML_CALLH(block, mips3.read64);                                 // callh   read64
1956         UML_DMOV(block, FPR64(RTREG), IREG(0));                           // dmov    <cpr1_rt>,i0
1957         if (!in_delay_slot)
1958            generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE);
1959         return TRUE;
1960
1961      case 0x32:   /* LWC2 - MIPS I */
1962         UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL));                  // add     i0,<rsreg>,SIMMVAL
1963         UML_CALLH(block, mips3.read32);                                 // callh   read32
1964         UML_DZEXT4(block, CPR264(RTREG), IREG(0));                        // dzext4  <cpr2_rt>,i0
1965         if (!in_delay_slot)
1966            generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE);
1967         return TRUE;
1968
1969      case 0x36:   /* LDC2 - MIPS II */
1970         UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL));                  // add     i0,<rsreg>,SIMMVAL
1971         UML_CALLH(block, mips3.read64);                                 // callh   read64
1972         UML_DMOV(block, CPR264(RTREG), IREG(0));                        // dmov    <cpr2_rt>,i0
1973         if (!in_delay_slot)
1974            generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE);
1975         return TRUE;
1976
1977
1978      /* ----- memory store operations ----- */
1979
1980      case 0x28:   /* SB - MIPS I */
1981         UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL));                  // add     i0,<rsreg>,SIMMVAL
1982         UML_MOV(block, IREG(1), R32(RTREG));                           // mov     i1,<rtreg>
1983         UML_CALLH(block, mips3.write8);                                 // callh   write8
1984         if (!in_delay_slot)
1985            generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE);
1986         return TRUE;
1987
1988      case 0x29:   /* SH - MIPS I */
1989         UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL));                  // add     i0,<rsreg>,SIMMVAL
1990         UML_MOV(block, IREG(1), R32(RTREG));                           // mov     i1,<rtreg>
1991         UML_CALLH(block, mips3.write16);                              // callh   write16
1992         if (!in_delay_slot)
1993            generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE);
1994         return TRUE;
1995
1996      case 0x2b:   /* SW - MIPS I */
1997         UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL));                  // add     i0,<rsreg>,SIMMVAL
1998         UML_MOV(block, IREG(1), R32(RTREG));                           // mov     i1,<rtreg>
1999         UML_CALLH(block, mips3.write32);                              // callh   write32
2000         if (!in_delay_slot)
2001            generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE);
2002         return TRUE;
2003
2004      case 0x3f:   /* SD - MIPS III */
2005         UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL));                  // add     i0,<rsreg>,SIMMVAL
2006         UML_DMOV(block, IREG(1), R64(RTREG));                           // dmov    i1,<rtreg>
2007         UML_CALLH(block, mips3.write64);                              // callh   write64
2008         if (!in_delay_slot)
2009            generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE);
2010         return TRUE;
2011
2012      case 0x2a:   /* SWL - MIPS I */
2013         UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL));                  // add     i0,<rsreg>,SIMMVAL
2014         UML_SHL(block, IREG(4), IREG(0), IMM(3));                        // shl     i4,i0,3
2015         UML_MOV(block, IREG(1), R32(RTREG));                           // mov     i1,<rtreg>
2016         if (!mips3.core->bigendian)
2017            UML_XOR(block, IREG(4), IREG(4), IMM(0x18));                  // xor     i4,i4,0x18
2018         UML_SHR(block, IREG(2), IMM(~0), IREG(4));                        // shr     i2,~0,i4
2019         UML_SHR(block, IREG(1), IREG(1), IREG(4));                        // shr     i1,i1,i4
2020         UML_CALLH(block, mips3.write32mask);                           // callh   write32mask
2021         if (!in_delay_slot)
2022            generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE);
2023         return TRUE;
2024
2025      case 0x2e:   /* SWR - MIPS I */
2026         UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL));                  // add     i0,<rsreg>,SIMMVAL
2027         UML_SHL(block, IREG(4), IREG(0), IMM(3));                        // shl     i4,i0,3
2028         UML_MOV(block, IREG(1), R32(RTREG));                           // mov     i1,<rtreg>
2029         if (mips3.core->bigendian)
2030            UML_XOR(block, IREG(4), IREG(4), IMM(0x18));                  // xor     i4,i4,0x18
2031         UML_SHL(block, IREG(2), IMM(~0), IREG(4));                        // shl     i2,~0,i4
2032         UML_SHL(block, IREG(1), IREG(1), IREG(4));                        // shl     i1,i1,i4
2033         UML_CALLH(block, mips3.write32mask);                           // callh   write32mask
2034         if (!in_delay_slot)
2035            generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE);
2036         return TRUE;
2037
2038      case 0x2c:   /* SDL - MIPS III */
2039         UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL));                  // add     i0,<rsreg>,SIMMVAL
2040         UML_SHL(block, IREG(4), IREG(0), IMM(3));                        // shl     i4,i0,3
2041         UML_DMOV(block, IREG(1), R64(RTREG));                           // dmov    i1,<rtreg>
2042         if (!mips3.core->bigendian)
2043            UML_XOR(block, IREG(4), IREG(4), IMM(0x38));                  // xor     i4,i4,0x38
2044         UML_DSHR(block, IREG(2), IMM((UINT64)~0), IREG(4));                  // dshr    i2,~0,i4
2045         UML_DSHR(block, IREG(1), IREG(1), IREG(4));                        // dshr    i1,i1,i4
2046         UML_CALLH(block, mips3.write64mask);                           // callh   write64mask
2047         if (!in_delay_slot)
2048            generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE);
2049         return TRUE;
2050
2051      case 0x2d:   /* SDR - MIPS III */
2052         UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL));                  // add     i0,<rsreg>,SIMMVAL
2053         UML_SHL(block, IREG(4), IREG(0), IMM(3));                        // shl     i4,i0,3
2054         UML_DMOV(block, IREG(1), R64(RTREG));                           // dmov    i1,<rtreg>
2055         if (mips3.core->bigendian)
2056            UML_XOR(block, IREG(4), IREG(4), IMM(0x38));                  // xor     i4,i4,0x38
2057         UML_DSHL(block, IREG(2), IMM((UINT64)~0), IREG(4));                  // dshl    i2,~0,i4
2058         UML_DSHL(block, IREG(1), IREG(1), IREG(4));                        // dshl    i1,i1,i4
2059         UML_CALLH(block, mips3.write64mask);                           // callh   write64mask
2060         if (!in_delay_slot)
2061            generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE);
2062         return TRUE;
2063
2064      case 0x39:   /* SWC1 - MIPS I */
2065         UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL));                  // add     i0,<rsreg>,SIMMVAL
2066         UML_MOV(block, IREG(1), FPR32(RTREG));                           // mov     i1,<cpr1_rt>
2067         UML_CALLH(block, mips3.write32);                              // callh   write32
2068         if (!in_delay_slot)
2069            generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE);
2070         return TRUE;
2071
2072      case 0x3d:   /* SDC1 - MIPS III */
2073         UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL));                  // add     i0,<rsreg>,SIMMVAL
2074         UML_DMOV(block, IREG(1), FPR64(RTREG));                           // dmov    i1,<cpr1_rt>
2075         UML_CALLH(block, mips3.write64);                              // callh   write64
2076         if (!in_delay_slot)
2077            generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE);
2078         return TRUE;
2079
2080      case 0x3a:   /* SWC2 - MIPS I */
2081         UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL));                  // add     i0,<rsreg>,SIMMVAL
2082         UML_MOV(block, IREG(1), CPR232(RTREG));                           // mov     i1,<cpr2_rt>
2083         UML_CALLH(block, mips3.write32);                              // callh   write32
2084         if (!in_delay_slot)
2085            generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE);
2086         return TRUE;
2087
2088      case 0x3e:   /* SDC2 - MIPS II */
2089         UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL));                  // add     i0,<rsreg>,SIMMVAL
2090         UML_DMOV(block, IREG(1), CPR264(RTREG));                        // dmov    i1,<cpr2_rt>
2091         UML_CALLH(block, mips3.write64);                              // callh   write64
2092         if (!in_delay_slot)
2093            generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE);
2094         return TRUE;
2095
2096
2097      /* ----- effective no-ops ----- */
2098
2099      case 0x2f:   /* CACHE - MIPS II */
2100      case 0x33:   /* PREF - MIPS IV */
2101         return TRUE;
2102
2103
2104      /* ----- coprocessor instructions ----- */
2105
2106      case 0x10:   /* COP0 - MIPS I */
2107         return generate_cop0(block, compiler, desc);
2108
2109      case 0x11:   /* COP1 - MIPS I */
2110         return generate_cop1(block, compiler, desc);
2111
2112      case 0x13:   /* COP1X - MIPS IV */
2113         return generate_cop1x(block, compiler, desc);
2114
2115      case 0x12:   /* COP2 - MIPS I */
2116         UML_EXH(block, mips3.exception[EXCEPTION_INVALIDOP], IMM(0));            // exh     invalidop,0
2117         return TRUE;
2118
2119
2120      /* ----- unimplemented/illegal instructions ----- */
2121
2122//      case 0x30:  /* LL - MIPS II */        logerror("mips3 Unhandled op: LL\n");                                   break;
2123//      case 0x34:  /* LLD - MIPS III */       logerror("mips3 Unhandled op: LLD\n");                                  break;
2124//      case 0x38:  /* SC - MIPS II */        logerror("mips3 Unhandled op: SC\n");                                   break;
2125//      case 0x3c:  /* SCD - MIPS III */       logerror("mips3 Unhandled op: SCD\n");                                  break;
2126//      default:    /* ??? */       invalid_instruction(op);                                                break;
2127   }
2128
2129   return FALSE;
2130}
2131
2132
2133/*-------------------------------------------------
2134    generate_special - compile opcodes in the
2135    'SPECIAL' group
2136-------------------------------------------------*/
2137
2138static int generate_special(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc)
2139{
2140   UINT32 op = *desc->opptr.l;
2141   UINT8 opswitch = op & 63;
2142
2143   switch (opswitch)
2144   {
2145      /* ----- shift instructions ----- */
2146
2147      case 0x00:   /* SLL - MIPS I */
2148         if (RDREG != 0)
2149         {
2150            UML_SHL(block, IREG(0), R32(RTREG), IMM(SHIFT));               // shl     i0,<rtreg>,<shift>
2151            UML_DSEXT4(block, R64(RDREG), IREG(0));                        // dsext4  <rdreg>,i0
2152         }
2153         return TRUE;
2154
2155      case 0x02:   /* SRL - MIPS I */
2156         if (RDREG != 0)
2157         {
2158            UML_SHR(block, IREG(0), R32(RTREG), IMM(SHIFT));               // shr     i0,<rtreg>,<shift>
2159            UML_DSEXT4(block, R64(RDREG), IREG(0));                        // dsext4  <rdreg>,i0
2160         }
2161         return TRUE;
2162
2163      case 0x03:   /* SRA - MIPS I */
2164         if (RDREG != 0)
2165         {
2166            UML_SAR(block, IREG(0), R32(RTREG), IMM(SHIFT));               // sar     i0,<rtreg>,<shift>
2167            UML_DSEXT4(block, R64(RDREG), IREG(0));                        // dsext4  <rdreg>,i0
2168         }
2169         return TRUE;
2170
2171      case 0x04:   /* SLLV - MIPS I */
2172         if (RDREG != 0)
2173         {
2174            UML_SHL(block, IREG(0), R32(RTREG), R32(RSREG));               // shl     i0,<rtreg>,<rsreg>
2175            UML_DSEXT4(block, R64(RDREG), IREG(0));                        // dsext4  <rdreg>,i0
2176         }
2177         return TRUE;
2178
2179      case 0x06:   /* SRLV - MIPS I */
2180         if (RDREG != 0)
2181         {
2182            UML_SHR(block, IREG(0), R32(RTREG), R32(RSREG));               // shr     i0,<rtreg>,<rsreg>
2183            UML_DSEXT4(block, R64(RDREG), IREG(0));                        // dsext4  <rdreg>,i0
2184         }
2185         return TRUE;
2186
2187      case 0x07:   /* SRAV - MIPS I */
2188         if (RDREG != 0)
2189         {
2190            UML_SAR(block, IREG(0), R32(RTREG), R32(RSREG));               // sar     i0,<rtreg>,<rsreg>
2191            UML_DSEXT4(block, R64(RDREG), IREG(0));                        // dsext4  <rdreg>,i0
2192         }
2193         return TRUE;
2194
2195      case 0x38:   /* DSLL - MIPS III */
2196         if (RDREG != 0)
2197            UML_DSHL(block, R64(RDREG), R64(RTREG), IMM(SHIFT));            // dshl    <rdreg>,<rtreg>,<shift>
2198         return TRUE;
2199
2200      case 0x3a:   /* DSRL - MIPS III */
2201         if (RDREG != 0)
2202            UML_DSHR(block, R64(RDREG), R64(RTREG), IMM(SHIFT));            // dshr    <rdreg>,<rtreg>,<shift>
2203         return TRUE;
2204
2205      case 0x3b:   /* DSRA - MIPS III */
2206         if (RDREG != 0)
2207            UML_DSAR(block, R64(RDREG), R64(RTREG), IMM(SHIFT));            // dsar    <rdreg>,<rtreg>,<shift>
2208         return TRUE;
2209
2210      case 0x3c:   /* DSLL32 - MIPS III */
2211         if (RDREG != 0)
2212            UML_DSHL(block, R64(RDREG), R64(RTREG), IMM(SHIFT + 32));         // dshl    <rdreg>,<rtreg>,<shift>+32
2213         return TRUE;
2214
2215      case 0x3e:   /* DSRL32 - MIPS III */
2216         if (RDREG != 0)
2217            UML_DSHR(block, R64(RDREG), R64(RTREG), IMM(SHIFT + 32));         // dshr    <rdreg>,<rtreg>,<shift>+32
2218         return TRUE;
2219
2220      case 0x3f:   /* DSRA32 - MIPS III */
2221         if (RDREG != 0)
2222            UML_DSAR(block, R64(RDREG), R64(RTREG), IMM(SHIFT + 32));         // dsar    <rdreg>,<rtreg>,<shift>+32
2223         return TRUE;
2224
2225      case 0x14:   /* DSLLV - MIPS III */
2226         if (RDREG != 0)
2227            UML_DSHL(block, R64(RDREG), R64(RTREG), R64(RSREG));            // dshl    <rdreg>,<rtreg>,<rsreg>
2228         return TRUE;
2229
2230      case 0x16:   /* DSRLV - MIPS III */
2231         if (RDREG != 0)
2232            UML_DSHR(block, R64(RDREG), R64(RTREG), R64(RSREG));            // dshr    <rdreg>,<rtreg>,<rsreg>
2233         return TRUE;
2234
2235      case 0x17:   /* DSRAV - MIPS III */
2236         if (RDREG != 0)
2237            UML_DSAR(block, R64(RDREG), R64(RTREG), R64(RSREG));            // dsar    <rdreg>,<rtreg>,<rsreg>
2238         return TRUE;
2239
2240
2241      /* ----- basic arithmetic ----- */
2242
2243      case 0x20:   /* ADD - MIPS I */
2244         if (mips3.drcoptions & MIPS3DRC_CHECK_OVERFLOWS)
2245         {
2246            UML_ADDf(block, IREG(0), R32(RSREG), R32(RTREG), FLAGS_V);         // add     i0,<rsreg>,<rtreg>,V
2247            UML_EXHc(block, IF_V, mips3.exception[EXCEPTION_OVERFLOW], IMM(0));   // exh     overflow,0,V
2248            if (RDREG != 0)
2249               UML_DSEXT4(block, R64(RDREG), IREG(0));                     // dsext4  <rdreg>,i0
2250         }
2251         else if (RDREG != 0)
2252         {
2253            UML_ADD(block, IREG(0), R32(RSREG), R32(RTREG));               // add     i0,<rsreg>,<rtreg>
2254            UML_DSEXT4(block, R64(RDREG), IREG(0));                        // dsext4  <rdreg>,i0
2255         }
2256         return TRUE;
2257
2258      case 0x21:   /* ADDU - MIPS I */
2259         if (RDREG != 0)
2260         {
2261            UML_ADD(block, IREG(0), R32(RSREG), R32(RTREG));               // add     i0,<rsreg>,<rtreg>
2262            UML_DSEXT4(block, R64(RDREG), IREG(0));                        // dsext4  <rdreg>,i0
2263         }
2264         return TRUE;
2265
2266      case 0x2c:   /* DADD - MIPS III */
2267         if (mips3.drcoptions & MIPS3DRC_CHECK_OVERFLOWS)
2268         {
2269            UML_DADDf(block, IREG(0), R64(RSREG), R64(RTREG), FLAGS_V);         // dadd    i0,<rsreg>,<rtreg>,V
2270            UML_EXHc(block, IF_V, mips3.exception[EXCEPTION_OVERFLOW], IMM(0));   // exh     overflow,0,V
2271            if (RDREG != 0)
2272               UML_DMOV(block, R64(RDREG), IREG(0));                     // dmov    <rdreg>,i0
2273         }
2274         else if (RDREG != 0)
2275            UML_DADD(block, R64(RDREG), R64(RSREG), R64(RTREG));            // dadd    <rdreg>,<rsreg>,<rtreg>
2276         return TRUE;
2277
2278      case 0x2d:   /* DADDU - MIPS III */
2279         if (RDREG != 0)
2280            UML_DADD(block, R64(RDREG), R64(RSREG), R64(RTREG));            // dadd    <rdreg>,<rsreg>,<rtreg>
2281         return TRUE;
2282
2283      case 0x22:   /* SUB - MIPS I */
2284         if (mips3.drcoptions & MIPS3DRC_CHECK_OVERFLOWS)
2285         {
2286            UML_SUBf(block, IREG(0), R32(RSREG), R32(RTREG), FLAGS_V);         // sub     i0,<rsreg>,<rtreg>,V
2287            UML_EXHc(block, IF_V, mips3.exception[EXCEPTION_OVERFLOW], IMM(0));   // exh     overflow,0,V
2288            if (RDREG != 0)
2289               UML_DSEXT4(block, R64(RDREG), IREG(0));                     // dsext4  <rdreg>,i0
2290         }
2291         else if (RDREG != 0)
2292         {
2293            UML_SUB(block, IREG(0), R32(RSREG), R32(RTREG));               // sub     i0,<rsreg>,<rtreg>
2294            UML_DSEXT4(block, R64(RDREG), IREG(0));                        // dsext4  <rdreg>,i0
2295         }
2296         return TRUE;
2297
2298      case 0x23:   /* SUBU - MIPS I */
2299         if (RDREG != 0)
2300         {
2301            UML_SUB(block, IREG(0), R32(RSREG), R32(RTREG));               // sub     i0,<rsreg>,<rtreg>
2302            UML_DSEXT4(block, R64(RDREG), IREG(0));                        // dsext4  <rdreg>,i0
2303         }
2304         return TRUE;
2305
2306      case 0x2e:   /* DSUB - MIPS III */
2307         if (mips3.drcoptions & MIPS3DRC_CHECK_OVERFLOWS)
2308         {
2309            UML_DSUBf(block, IREG(0), R64(RSREG), R64(RTREG), FLAGS_V);         // dsub    i0,<rsreg>,<rtreg>,V
2310            UML_EXHc(block, IF_V, mips3.exception[EXCEPTION_OVERFLOW], IMM(0));   // exh     overflow,0,V
2311            if (RDREG != 0)
2312               UML_DMOV(block, R64(RDREG), IREG(0));                     // dmov    <rdreg>,i0
2313         }
2314         else if (RDREG != 0)
2315            UML_DSUB(block, R64(RDREG), R64(RSREG), R64(RTREG));            // dsub    <rdreg>,<rsreg>,<rtreg>
2316         return TRUE;
2317
2318      case 0x2f:   /* DSUBU - MIPS III */
2319         if (RDREG != 0)
2320            UML_DSUB(block, R64(RDREG), R64(RSREG), R64(RTREG));            // dsub    <rdreg>,<rsreg>,<rtreg>
2321         return TRUE;
2322
2323      case 0x18:   /* MULT - MIPS I */
2324         UML_MULS(block, IREG(0), IREG(1), R32(RSREG), R32(RTREG));            // muls    i0,i1,<rsreg>,<rtreg>
2325         UML_DSEXT4(block, LO64, IREG(0));                              // dsext4  lo,i0
2326         UML_DSEXT4(block, HI64, IREG(1));                              // dsext4  hi,i1
2327         return TRUE;
2328
2329      case 0x19:   /* MULTU - MIPS I */
2330         UML_MULU(block, IREG(0), IREG(1), R32(RSREG), R32(RTREG));            // mulu    i0,i1,<rsreg>,<rtreg>
2331         UML_DSEXT4(block, LO64, IREG(0));                              // dsext4  lo,i0
2332         UML_DSEXT4(block, HI64, IREG(1));                              // dsext4  hi,i1
2333         return TRUE;
2334
2335      case 0x1c:   /* DMULT - MIPS III */
2336         UML_DMULS(block, LO64, HI64, R64(RSREG), R64(RTREG));               // dmuls   lo,hi,<rsreg>,<rtreg>
2337         return TRUE;
2338
2339      case 0x1d:   /* DMULTU - MIPS III */
2340         UML_DMULU(block, LO64, HI64, R64(RSREG), R64(RTREG));               // dmulu   lo,hi,<rsreg>,<rtreg>
2341         return TRUE;
2342
2343      case 0x1a:   /* DIV - MIPS I */
2344         UML_DIVS(block, IREG(0), IREG(1), R32(RSREG), R32(RTREG));            // divs    i0,i1,<rsreg>,<rtreg>
2345         UML_DSEXT4(block, LO64, IREG(0));                              // dsext4  lo,i0
2346         UML_DSEXT4(block, HI64, IREG(1));                              // dsext4  hi,i1
2347         return TRUE;
2348
2349      case 0x1b:   /* DIVU - MIPS I */
2350         UML_DIVU(block, IREG(0), IREG(1), R32(RSREG), R32(RTREG));            // divu    i0,i1,<rsreg>,<rtreg>
2351         UML_DSEXT4(block, LO64, IREG(0));                              // dsext4  lo,i0
2352         UML_DSEXT4(block, HI64, IREG(1));                              // dsext4  hi,i1
2353         return TRUE;
2354
2355      case 0x1e:   /* DDIV - MIPS III */
2356         UML_DDIVS(block, LO64, HI64, R64(RSREG), R64(RTREG));               // ddivs    lo,hi,<rsreg>,<rtreg>
2357         return TRUE;
2358
2359      case 0x1f:   /* DDIVU - MIPS III */
2360         UML_DDIVU(block, LO64, HI64, R64(RSREG), R64(RTREG));               // ddivu    lo,hi,<rsreg>,<rtreg>
2361         return TRUE;
2362
2363
2364      /* ----- basic logical ops ----- */
2365
2366      case 0x24:   /* AND - MIPS I */
2367         if (RDREG != 0)
2368            UML_DAND(block, R64(RDREG), R64(RSREG), R64(RTREG));            // dand     <rdreg>,<rsreg>,<rtreg>
2369         return TRUE;
2370
2371      case 0x25:   /* OR - MIPS I */
2372         if (RDREG != 0)
2373            UML_DOR(block, R64(RDREG), R64(RSREG), R64(RTREG));               // dor      <rdreg>,<rsreg>,<rtreg>
2374         return TRUE;
2375
2376      case 0x26:   /* XOR - MIPS I */
2377         if (RDREG != 0)
2378            UML_DXOR(block, R64(RDREG), R64(RSREG), R64(RTREG));            // dxor     <rdreg>,<rsreg>,<rtreg>
2379         return TRUE;
2380
2381      case 0x27:   /* NOR - MIPS I */
2382         if (RDREG != 0)
2383         {
2384            UML_DOR(block, IREG(0), R64(RSREG), R64(RTREG));               // dor      i0,<rsreg>,<rtreg>
2385            UML_DXOR(block, R64(RDREG), IREG(0), IMM((UINT64)~0));            // dxor     <rdreg>,i0,~0
2386         }
2387         return TRUE;
2388
2389
2390      /* ----- basic comparisons ----- */
2391
2392      case 0x2a:   /* SLT - MIPS I */
2393         if (RDREG != 0)
2394         {
2395            UML_DCMP(block, R64(RSREG), R64(RTREG));                     // dcmp    <rsreg>,<rtreg>
2396            UML_DFLAGS(block, R64(RDREG), (UINT64)~0, mips3.cstate->slt_table);   // dflags  <rdreg>,~0,slt_table
2397         }
2398         return TRUE;
2399
2400      case 0x2b:   /* SLTU - MIPS I */
2401         if (RDREG != 0)
2402         {
2403            UML_DCMP(block, R64(RSREG), R64(RTREG));                     // dcmp    <rsreg>,<rtreg>
2404            UML_DFLAGS(block, R64(RDREG), (UINT64)~0, mips3.cstate->sltu_table);// dflags  <rdreg>,~0,sltu_table
2405         }
2406         return TRUE;
2407
2408
2409      /* ----- conditional traps ----- */
2410
2411      case 0x30:   /* TGE - MIPS II */
2412         UML_DCMP(block, R64(RSREG), R64(RTREG));                        // dcmp    <rsreg>,<rtreg>
2413         UML_EXHc(block, IF_GE, mips3.exception[EXCEPTION_TRAP], IMM(0));      // exh     trap,0,GE
2414         return TRUE;
2415
2416      case 0x31:   /* TGEU - MIPS II */
2417         UML_DCMP(block, R64(RSREG), R64(RTREG));                        // dcmp    <rsreg>,<rtreg>
2418         UML_EXHc(block, IF_AE, mips3.exception[EXCEPTION_TRAP], IMM(0));      // exh     trap,0,AE
2419         return TRUE;
2420
2421      case 0x32:   /* TLT - MIPS II */
2422         UML_DCMP(block, R64(RSREG), R64(RTREG));                        // dcmp    <rsreg>,<rtreg>
2423         UML_EXHc(block, IF_L, mips3.exception[EXCEPTION_TRAP], IMM(0));         // exh     trap,0,LT
2424         return TRUE;
2425
2426      case 0x33:   /* TLTU - MIPS II */
2427         UML_DCMP(block, R64(RSREG), R64(RTREG));                        // dcmp    <rsreg>,<rtreg>
2428         UML_EXHc(block, IF_B, mips3.exception[EXCEPTION_TRAP], IMM(0));         // exh     trap,0,B
2429         return TRUE;
2430
2431      case 0x34:   /* TEQ - MIPS II */
2432         UML_DCMP(block, R64(RSREG), R64(RTREG));                        // dcmp    <rsreg>,<rtreg>
2433         UML_EXHc(block, IF_E, mips3.exception[EXCEPTION_TRAP], IMM(0));         // exh     trap,0,E
2434         return TRUE;
2435
2436      case 0x36:   /* TNE - MIPS II */
2437         UML_DCMP(block, R64(RSREG), R64(RTREG));                        // dcmp    <rsreg>,<rtreg>
2438         UML_EXHc(block, IF_NE, mips3.exception[EXCEPTION_TRAP], IMM(0));      // exh     trap,0,NE
2439         return TRUE;
2440
2441
2442      /* ----- conditional moves ----- */
2443
2444      case 0x0a:   /* MOVZ - MIPS IV */
2445         if (RDREG != 0)
2446         {
2447            UML_DCMP(block, R64(RTREG), IMM(0));                        // dcmp    <rtreg>,0
2448            UML_DMOVc(block, IF_Z, R64(RDREG), R64(RSREG));                  // mov     <rdreg>,<rsreg>,Z
2449         }
2450         return TRUE;
2451
2452      case 0x0b:   /* MOVN - MIPS IV */
2453         if (RDREG != 0)
2454         {
2455            UML_DCMP(block, R64(RTREG), IMM(0));                        // dcmp    <rtreg>,0
2456            UML_DMOVc(block, IF_NZ, R64(RDREG), R64(RSREG));               // mov     <rdreg>,<rsreg>,NZ
2457         }
2458         return TRUE;
2459
2460      case 0x01:   /* MOVF/MOVT - MIPS IV */
2461         if (RDREG != 0)
2462         {
2463            UML_TEST(block, CCR132(31), IMM(fcc_mask[(op >> 18) & 7]));         // test    ccr31,fcc_mask[x]
2464            UML_DMOVc(block, ((op >> 16) & 1) ? IF_NZ : IF_Z, R64(RDREG), R64(RSREG));
2465                                                               // mov     <rdreg>,<rsreg>,NZ/Z
2466         }
2467         return TRUE;
2468
2469
2470      /* ----- jumps and branches ----- */
2471
2472      case 0x08:   /* JR - MIPS I */
2473         generate_delay_slot_and_branch(block, compiler, desc, 0);            // <next instruction + hashjmp>
2474         return TRUE;
2475
2476      case 0x09:   /* JALR - MIPS I */
2477         generate_delay_slot_and_branch(block, compiler, desc, 31);            // <next instruction + hashjmp>
2478         return TRUE;
2479
2480
2481      /* ----- system calls ----- */
2482
2483      case 0x0c:   /* SYSCALL - MIPS I */
2484         UML_EXH(block, mips3.exception[EXCEPTION_SYSCALL], IMM(0));            // exh     syscall,0
2485         return TRUE;
2486
2487      case 0x0d:   /* BREAK - MIPS I */
2488         UML_EXH(block, mips3.exception[EXCEPTION_BREAK], IMM(0));            // exh     break,0
2489         return TRUE;
2490
2491
2492      /* ----- effective no-ops ----- */
2493
2494      case 0x0f:   /* SYNC - MIPS II */
2495         return TRUE;
2496
2497
2498      /* ----- hi/lo register access ----- */
2499
2500      case 0x10:   /* MFHI - MIPS I */
2501         if (RDREG != 0)
2502            UML_DMOV(block, R64(RDREG), HI64);                           // dmov    <rdreg>,hi
2503         return TRUE;
2504
2505      case 0x11:   /* MTHI - MIPS I */
2506         UML_DMOV(block, HI64, R64(RSREG));                              // dmov    hi,<rsreg>
2507         return TRUE;
2508
2509      case 0x12:   /* MFLO - MIPS I */
2510         if (RDREG != 0)
2511            UML_DMOV(block, R64(RDREG), LO64);                           // dmov    <rdreg>,lo
2512         return TRUE;
2513
2514      case 0x13:   /* MTLO - MIPS I */
2515         UML_DMOV(block, LO64, R64(RSREG));                              // dmov    lo,<rsreg>
2516         return TRUE;
2517   }
2518   return FALSE;
2519}
2520
2521
2522/*-------------------------------------------------
2523    generate_regimm - compile opcodes in the
2524    'REGIMM' group
2525-------------------------------------------------*/
2526
2527static int generate_regimm(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc)
2528{
2529   UINT32 op = *desc->opptr.l;
2530   UINT8 opswitch = RTREG;
2531   drcuml_codelabel skip;
2532
2533   switch (opswitch)
2534   {
2535      case 0x00:   /* BLTZ */
2536      case 0x02:   /* BLTZL */
2537      case 0x10:   /* BLTZAL */
2538      case 0x12:   /* BLTZALL */
2539         if (RSREG != 0)
2540         {
2541            UML_DCMP(block, R64(RSREG), IMM(0));                        // dcmp    <rsreg>,0
2542            UML_JMPc(block, IF_GE, skip = compiler->labelnum++);            // jmp     skip,GE
2543            generate_delay_slot_and_branch(block, compiler, desc, (opswitch & 0x10) ? 31 : 0);
2544                                                               // <next instruction + hashjmp>
2545            UML_LABEL(block, skip);                                 // skip:
2546         }
2547         return TRUE;
2548
2549      case 0x01:   /* BGEZ */
2550      case 0x03:   /* BGEZL */
2551      case 0x11:   /* BGEZAL */
2552      case 0x13:   /* BGEZALL */
2553         if (RSREG != 0)
2554         {
2555            UML_DCMP(block, R64(RSREG), IMM(0));                        // dcmp    <rsreg>,0
2556            UML_JMPc(block, IF_L, skip = compiler->labelnum++);               // jmp     skip,L
2557            generate_delay_slot_and_branch(block, compiler, desc, (opswitch & 0x10) ? 31 : 0);
2558                                                               // <next instruction + hashjmp>
2559            UML_LABEL(block, skip);                                 // skip:
2560         }
2561         else
2562            generate_delay_slot_and_branch(block, compiler, desc, (opswitch & 0x10) ? 31 : 0);
2563                                                               // <next instruction + hashjmp>
2564         return TRUE;
2565
2566      case 0x08:   /* TGEI */
2567         UML_DCMP(block, R64(RSREG), IMM(SIMMVAL));                        // dcmp    <rsreg>,SIMMVAL
2568         UML_EXHc(block, IF_GE, mips3.exception[EXCEPTION_TRAP], IMM(0));      // exh     trap,0,GE
2569         return TRUE;
2570
2571      case 0x09:   /* TGEIU */
2572         UML_DCMP(block, R64(RSREG), IMM(SIMMVAL));                        // dcmp    <rsreg>,SIMMVAL
2573         UML_EXHc(block, IF_AE, mips3.exception[EXCEPTION_TRAP], IMM(0));      // exh     trap,0,AE
2574         return TRUE;
2575
2576      case 0x0a:   /* TLTI */
2577         UML_DCMP(block, R64(RSREG), IMM(SIMMVAL));                        // dcmp    <rsreg>,SIMMVAL
2578         UML_EXHc(block, IF_L, mips3.exception[EXCEPTION_TRAP], IMM(0));         // exh     trap,0,L
2579         return TRUE;
2580
2581      case 0x0b:   /* TLTIU */
2582         UML_DCMP(block, R64(RSREG), IMM(SIMMVAL));                        // dcmp    <rsreg>,SIMMVAL
2583         UML_EXHc(block, IF_B, mips3.exception[EXCEPTION_TRAP], IMM(0));         // exh     trap,0,B
2584         return TRUE;
2585
2586      case 0x0c:   /* TEQI */
2587         UML_DCMP(block, R64(RSREG), IMM(SIMMVAL));                        // dcmp    <rsreg>,SIMMVAL
2588         UML_EXHc(block, IF_E, mips3.exception[EXCEPTION_TRAP], IMM(0));         // exh     trap,0,E
2589         return TRUE;
2590
2591      case 0x0e:   /* TNEI */
2592         UML_DCMP(block, R64(RSREG), IMM(SIMMVAL));                        // dcmp    <rsreg>,SIMMVAL
2593         UML_EXHc(block, IF_NE, mips3.exception[EXCEPTION_TRAP], IMM(0));      // exh     trap,0,NE
2594         return TRUE;
2595   }
2596   return FALSE;
2597}
2598
2599
2600/*-------------------------------------------------
2601    generate_idt - compile opcodes in the IDT-
2602    specific group
2603-------------------------------------------------*/
2604
2605static int generate_idt(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc)
2606{
2607   fatalerror("Unimplemented IDT instructions");
2608#if 0
2609   UINT32 op = *desc->opptr.l;
2610   UINT8 opswitch = op & 0x1f;
2611
2612   switch (opswitch)
2613   {
2614      case 0: /* MAD */
2615         if (RSREG != 0 && RTREG != 0)
2616         {
2617            emit_mov_r32_m32(REG_EAX, REGADDR(RSREG));                     // mov  eax,[rsreg]
2618            emit_mov_r32_m32(REG_EDX, REGADDR(RTREG));                     // mov  edx,[rtreg]
2619            emit_imul_r32(DRCTOP, REG_EDX);                              // imul edx
2620            emit_add_r32_m32(DRCTOP, LOADDR);                           // add  eax,[lo]
2621            emit_adc_r32_m32(DRCTOP, HIADDR);                           // adc  edx,[hi]
2622            emit_mov_r32_r32(DRCTOP, REG_EBX, REG_EDX);                     // mov  ebx,edx
2623            emit_cdq(DRCTOP);                                       // cdq
2624            emit_mov_m64_r64(DRCTOP, LOADDR, REG_EAX);                     // mov  [lo],edx:eax
2625            emit_mov_r32_r32(DRCTOP, REG_EAX, REG_EBX);                     // mov  eax,ebx
2626            emit_cdq(DRCTOP);                                       // cdq
2627            emit_mov_m64_r64(DRCTOP, HIADDR, REG_EAX);                     // mov  [hi],edx:eax
2628         }
2629         return compile_SUCCESSFUL_CP(3,4);
2630
2631      case 1: /* MADU */
2632         if (RSREG != 0 && RTREG != 0)
2633         {
2634            emit_mov_r32_m32(REG_EAX, REGADDR(RSREG));                     // mov  eax,[rsreg]
2635            emit_mov_r32_m32(REG_EDX, REGADDR(RTREG));                     // mov  edx,[rtreg]
2636            emit_mul_r32(DRCTOP, REG_EDX);                              // mul  edx
2637            emit_add_r32_m32(DRCTOP, LOADDR);                           // add  eax,[lo]
2638            emit_adc_r32_m32(DRCTOP, HIADDR);                           // adc  edx,[hi]
2639            emit_mov_r32_r32(DRCTOP, REG_EBX, REG_EDX);                     // mov  ebx,edx
2640            emit_cdq(DRCTOP);                                       // cdq
2641            emit_mov_m64_r64(DRCTOP, LOADDR, REG_EDX, REG_EAX);               // mov  [lo],edx:eax
2642            emit_mov_r32_r32(DRCTOP, REG_EAX, REG_EBX);                     // mov  eax,ebx
2643            emit_cdq(DRCTOP);                                       // cdq
2644            emit_mov_m64_r64(DRCTOP, HIADDR, REG_EDX, REG_EAX);               // mov  [hi],edx:eax
2645         }
2646         return compile_SUCCESSFUL_CP(3,4);
2647
2648      case 2: /* MUL */
2649         if (RDREG != 0)
2650         {
2651            if (RSREG != 0 && RTREG != 0)
2652            {
2653               emit_mov_r32_m32(REG_EAX, REGADDR(RSREG));                  // mov  eax,[rsreg]
2654               emit_imul_r32_m32(REG_EAX, REGADDR(RTREG));                  // imul eax,[rtreg]
2655               emit_cdq(DRCTOP);                                    // cdq
2656               emit_mov_m64_r64(REG_ESI, REGADDR(RDREG), REG_EDX, REG_EAX);   // mov  [rd],edx:eax
2657            }
2658            else
2659               emit_zero_m64(REG_ESI, REGADDR(RDREG));
2660         }
2661         return compile_SUCCESSFUL_CP(3,4);
2662   }
3562663#endif
2664   return FALSE;
2665}
3572666
3582667
2668/*-------------------------------------------------
2669    generate_set_cop0_reg - generate code to
2670    handle special COP0 registers
2671-------------------------------------------------*/
3592672
2673static int generate_set_cop0_reg(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc, UINT8 reg)
2674{
2675   int in_delay_slot = ((desc->flags & OPFLAG_IN_DELAY_SLOT) != 0);
2676   drcuml_codelabel link;
2677
2678   switch (reg)
2679   {
2680      case COP0_Cause:
2681         UML_AND(block, IREG(0), IREG(0), IMM(~0xfc00));                     // and     i0,i0,~0xfc00
2682         UML_AND(block, IREG(1), CPR032(COP0_Cause), IMM(0xfc00));            // and     i1,[Cause],0xfc00
2683         UML_OR(block, CPR032(COP0_Cause), IREG(0), IREG(1));               // or      [Cause],i0,i1
2684         compiler->checksoftints = TRUE;
2685         if (!in_delay_slot)
2686            generate_update_cycles(block, compiler, desc->pc + 4, NULL, TRUE);
2687         return TRUE;
2688
2689      case COP0_Status:
2690         generate_update_cycles(block, compiler, desc->pc, NULL, !in_delay_slot);// <subtract cycles>
2691         UML_MOV(block, IREG(1), CPR032(COP0_Status));                     // mov     i1,[Status]
2692         UML_MOV(block, CPR032(COP0_Status), IREG(0));                     // mov     [Status],i0
2693         UML_SHR(block, IREG(2), IREG(1), IMM(26));                        // shr     i2,i1,26
2694         UML_AND(block, MEM(&mips3.cstate->mode), IREG(2), IMM(1));            // and     [mode],i2,1
2695         UML_XOR(block, IREG(0), IREG(0), IREG(1));                        // xor     i0,i0,i1
2696         UML_TEST(block, IREG(0), IMM(0x8000));                           // test    i0,0x8000
2697         UML_CALLCc(block, IF_NZ, mips3com_update_cycle_counting, mips3.core);   // callc   mips3com_update_cycle_counting,mips.core,NZ
2698         compiler->checkints = TRUE;
2699         if (!in_delay_slot)
2700            generate_update_cycles(block, compiler, desc->pc + 4, NULL, TRUE);
2701         return TRUE;
2702
2703      case COP0_Count:
2704         generate_update_cycles(block, compiler, desc->pc, NULL, !in_delay_slot);// <subtract cycles>
2705         UML_MOV(block, CPR032(COP0_Count), IREG(0));                     // mov     [Count],i0
2706         UML_CALLC(block, cfunc_get_cycles, &mips3.cstate->numcycles);         // callc   cfunc_get_cycles,&mips3.numcycles
2707         UML_DZEXT4(block, IREG(0), IREG(0));                           // dzext4  i0,i0
2708         UML_DADD(block, IREG(0), IREG(0), IREG(0));                        // dadd    i0,i0,i0
2709         UML_DSUB(block, MEM(&mips3.core->count_zero_time), MEM(&mips3.cstate->numcycles), IREG(0));
2710                                                               // dsub    [count_zero_time],[mips3.numcycles],i0
2711         UML_CALLC(block, mips3com_update_cycle_counting, mips3.core);         // callc   mips3com_update_cycle_counting,mips.core
2712         return TRUE;
2713
2714      case COP0_Compare:
2715         generate_update_cycles(block, compiler, desc->pc, NULL, !in_delay_slot);// <subtract cycles>
2716         UML_MOV(block, CPR032(COP0_Compare), IREG(0));                     // mov     [Compare],i0
2717         UML_AND(block, CPR032(COP0_Cause), CPR032(COP0_Cause), IMM(~0x8000));   // and    [Cause],[Cause],~0x8000
2718         UML_CALLC(block, mips3com_update_cycle_counting, mips3.core);         // callc   mips3com_update_cycle_counting,mips.core
2719         return TRUE;
2720
2721      case COP0_PRId:
2722         return TRUE;
2723
2724      case COP0_Config:
2725         UML_AND(block, IREG(0), IREG(0), IMM(0x0007));                     // and     i0,i0,0x0007
2726         UML_AND(block, IREG(1), CPR032(COP0_Config), IMM(~0x0007));            // and     i1,[Config],~0x0007
2727         UML_OR(block, CPR032(COP0_Config), IREG(0), IREG(1));               // or      [Config],i0,i1
2728         return TRUE;
2729
2730      case COP0_EntryHi:
2731         UML_XOR(block, IREG(1), IREG(0), CPR032(reg));                     // xor     i1,i0,cpr0[reg]
2732         UML_MOV(block, CPR032(reg), IREG(0));                           // mov     cpr0[reg],i0
2733         UML_TEST(block, IREG(1), IMM(0xff));                           // test    i1,0xff
2734         UML_JMPc(block, IF_Z, link = compiler->labelnum++);                  // jmp     link,z
2735         UML_CALLC(block, mips3com_unmap_tlb_entries, mips3.core);            // callc   mips3com_unmap_tlb_entries
2736         UML_CALLC(block, mips3com_map_tlb_entries, mips3.core);               // callc   mips3com_map_tlb_entries
2737         UML_LABEL(block, link);                                    // link:
2738         return TRUE;
2739
2740      default:
2741         UML_MOV(block, CPR032(reg), IREG(0));                           // mov     cpr0[reg],i0
2742         return TRUE;
2743   }
2744}
2745
2746
2747/*-------------------------------------------------
2748    generate_get_cop0_reg - generate code to
2749    read special COP0 registers
2750-------------------------------------------------*/
2751
2752static int generate_get_cop0_reg(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc, UINT8 reg)
2753{
2754   drcuml_codelabel link1, link2;
2755
2756   switch (reg)
2757   {
2758      case COP0_Count:
2759         generate_update_cycles(block, compiler, desc->pc, NULL, FALSE);         // <subtract cycles>
2760         UML_CALLC(block, cfunc_get_cycles, &mips3.cstate->numcycles);         // callc   cfunc_get_cycles,&mips3.numcycles
2761         UML_DSUB(block, IREG(0), MEM(&mips3.cstate->numcycles), MEM(&mips3.core->count_zero_time));
2762                                                               // dsub    i0,[numcycles],[count_zero_time]
2763         UML_DSHR(block, IREG(0), IREG(0), IMM(1));                        // dshr    i0,i0,1
2764         UML_DSEXT4(block, IREG(0), IREG(0));                           // dsext4  i0,i0
2765         return TRUE;
2766
2767      case COP0_Random:
2768         generate_update_cycles(block, compiler, desc->pc, NULL, FALSE);         // <subtract cycles>
2769         UML_CALLC(block, cfunc_get_cycles, &mips3.cstate->numcycles);         // callc   cfunc_get_cycles,&mips3.numcycles
2770         UML_DSUB(block, IREG(0), MEM(&mips3.cstate->numcycles), MEM(&mips3.core->count_zero_time));
2771                                                               // dsub    i0,[numcycles],[count_zero_time]
2772         UML_AND(block, IREG(1), CPR032(COP0_Wired), IMM(0x3f));               // and     i1,[Wired],0x3f
2773         UML_SUBf(block, IREG(2), IMM(48), IREG(1), FLAGS_BE);               // sub     i2,48,i1,ALL
2774         UML_JMPc(block, IF_BE, link1 = compiler->labelnum++);               // jmp     link1,BE
2775         UML_DZEXT4(block, IREG(2), IREG(2));                           // dzext4  i2,i2
2776         UML_DDIVU(block, IREG(0), IREG(2), IREG(0), IREG(2));               // ddivu   i0,i2,i0,i2
2777         UML_ADD(block, IREG(0), IREG(2), IREG(1));                        // add     i0,i2,i1
2778         UML_DAND(block, IREG(0), IREG(0), IMM(0x3f));                     // dand    i0,i0,0x3f
2779         UML_JMP(block, link2 = compiler->labelnum++);                     // jmp     link2
2780         UML_LABEL(block, link1);                                 // link1:
2781         UML_DSEXT4(block, IREG(0), IMM(47));                           // dsext4  i0,47
2782         UML_LABEL(block, link2);                                 // link2:
2783         return TRUE;
2784
2785      default:
2786         UML_DSEXT4(block, IREG(0), CPR032(reg));                        // dsext4  i0,cpr0[reg]
2787         return TRUE;
2788   }
2789}
2790
2791
2792/*-------------------------------------------------
2793    generate_cop0 - compile COP0 opcodes
2794-------------------------------------------------*/
2795
2796static int generate_cop0(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc)
2797{
2798   UINT32 op = *desc->opptr.l;
2799   UINT8 opswitch = RSREG;
2800
2801   /* generate an exception if COP0 is disabled or we are not in kernel mode */
2802   if (mips3.drcoptions & MIPS3DRC_STRICT_COP0)
2803   {
2804      drcuml_codelabel okay;
2805
2806      UML_TEST(block, CPR032(COP0_Status), IMM(SR_KSU_MASK));                  // test    [Status],SR_KSU_MASK
2807      UML_JMPc(block, IF_Z, okay = compiler->labelnum++);                     // jmp     okay,Z
2808      UML_TEST(block, CPR032(COP0_Status), IMM(SR_COP0));                     // test    [Status],SR_COP0
2809      UML_EXHc(block, IF_Z, mips3.exception[EXCEPTION_BADCOP], IMM(0));         // exh     cop,0,Z
2810      UML_LABEL(block, okay);                                       // okay:
2811   }
2812
2813   switch (opswitch)
2814   {
2815      case 0x00:   /* MFCz */
2816         if (RTREG != 0)
2817         {
2818            generate_get_cop0_reg(block, compiler, desc, RDREG);            // <get cop0 reg>
2819            UML_DSEXT4(block, R64(RTREG), IREG(0));                        // dsext4  <rtreg>,i0
2820         }
2821         return TRUE;
2822
2823      case 0x01:   /* DMFCz */
2824         if (RTREG != 0)
2825         {
2826            generate_get_cop0_reg(block, compiler, desc, RDREG);            // <get cop0 reg>
2827            UML_DMOV(block, R64(RTREG), IREG(0));                        // dmov    <rtreg>,i0
2828         }
2829         return TRUE;
2830
2831      case 0x02:   /* CFCz */
2832         if (RTREG != 0)
2833            UML_DSEXT4(block, R32(RTREG), CCR032(RDREG));                  // dsext4  <rtreg>,ccr0[rdreg]
2834         return TRUE;
2835
2836      case 0x04:   /* MTCz */
2837         UML_DSEXT4(block, IREG(0), R32(RTREG));                           // dsext4  i0,<rtreg>
2838         generate_set_cop0_reg(block, compiler, desc, RDREG);               // <set cop0 reg>
2839         return TRUE;
2840
2841      case 0x05:   /* DMTCz */
2842         UML_DMOV(block, IREG(0), R64(RTREG));                           // dmov    i0,<rtreg>
2843         generate_set_cop0_reg(block, compiler, desc, RDREG);               // <set cop0 reg>
2844         return TRUE;
2845
2846      case 0x06:   /* CTCz */
2847         UML_DSEXT4(block, CCR064(RDREG), R32(RTREG));                     // dsext4  ccr0[rdreg],<rtreg>
2848         return TRUE;
2849
2850      case 0x10:
2851      case 0x11:
2852      case 0x12:
2853      case 0x13:
2854      case 0x14:
2855      case 0x15:
2856      case 0x16:
2857      case 0x17:
2858      case 0x18:
2859      case 0x19:
2860      case 0x1a:
2861      case 0x1b:
2862      case 0x1c:
2863      case 0x1d:
2864      case 0x1e:
2865      case 0x1f:   /* COP */
2866         switch (op & 0x01ffffff)
2867         {
2868            case 0x01:   /* TLBR */
2869               UML_CALLC(block, mips3com_tlbr, mips3.core);               // callc   mips3com_tlbr,mips3.core
2870               return TRUE;
2871
2872            case 0x02:   /* TLBWI */
2873               UML_CALLC(block, mips3com_tlbwi, mips3.core);               // callc   mips3com_tlbwi,mips3.core
2874               return TRUE;
2875
2876            case 0x06:   /* TLBWR */
2877               UML_CALLC(block, mips3com_tlbwr, mips3.core);               // callc   mips3com_tlbwr,mips3.core
2878               return TRUE;
2879
2880            case 0x08:   /* TLBP */
2881               UML_CALLC(block, mips3com_tlbp, mips3.core);               // callc   mips3com_tlbp,mips3.core
2882               return TRUE;
2883
2884            case 0x18:   /* ERET */
2885               UML_AND(block, CPR032(COP0_Status), CPR032(COP0_Status), IMM(~SR_EXL));
2886                                                               // and     [Status],[Status],~SR_EXL
2887               compiler->checkints = TRUE;
2888               generate_update_cycles(block, compiler, 0, LOPTR(&mips3.core->cpr[0][COP0_EPC]), TRUE);
2889                                                               // <subtract cycles>
2890               UML_HASHJMP(block, IMM(mips3.cstate->mode), CPR032(COP0_EPC), mips3.nocode);
2891                                                               // hashjmp <mode>,[EPC],nocode
2892               return TRUE;
2893
2894            case 0x20:   /* WAIT */
2895               return TRUE;
2896         }
2897         break;
2898   }
2899
2900   return FALSE;
2901}
2902
2903
2904
3602905/***************************************************************************
2906    COP1 RECOMPILATION
2907***************************************************************************/
2908
2909/*-------------------------------------------------
2910    generate_cop1 - compile COP1 opcodes
2911-------------------------------------------------*/
2912
2913static int generate_cop1(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc)
2914{
2915   UINT32 op = *desc->opptr.l;
2916   drcuml_codelabel skip;
2917   int condition;
2918
2919   /* generate an exception if COP1 is disabled */
2920   if (mips3.drcoptions & MIPS3DRC_STRICT_COP1)
2921   {
2922      UML_TEST(block, CPR032(COP0_Status), IMM(SR_COP1));                     // test    [Status],SR_COP1
2923      UML_EXHc(block, IF_Z, mips3.exception[EXCEPTION_BADCOP], IMM(0));         // exh     cop,0,Z
2924   }
2925
2926   switch (RSREG)
2927   {
2928      case 0x00:   /* MFCz - MIPS I */
2929         if (RTREG != 0)
2930            UML_DSEXT4(block, R64(RTREG), FPR32(RDREG));                  // dsext4  <rtreg>,fpr[rdreg]
2931         return TRUE;
2932
2933      case 0x01:   /* DMFCz - MIPS III */
2934         if (RTREG != 0)
2935            UML_DMOV(block, R64(RTREG), FPR64(RDREG));                     // dmov    <rtreg>,fpr[rdreg]
2936         return TRUE;
2937
2938      case 0x02:   /* CFCz - MIPS I */
2939         if (RTREG != 0)
2940            UML_DSEXT4(block, R64(RTREG), CCR132(RDREG));                  // dsext4  <rtreg>,ccr132[rdreg]
2941         return TRUE;
2942
2943      case 0x04:   /* MTCz - MIPS I */
2944         UML_MOV(block, FPR32(RDREG), R32(RTREG));                        // mov     fpr[rdreg],<rtreg>
2945         return TRUE;
2946
2947      case 0x05:   /* DMTCz - MIPS III */
2948         UML_DMOV(block, FPR64(RDREG), R64(RTREG));                        // dmov    fpr[rdreg],<rtreg>
2949         return TRUE;
2950
2951      case 0x06:   /* CTCz - MIPS I */
2952         if (RDREG != 31)
2953            UML_DSEXT4(block, CCR164(RDREG), R32(RTREG));                  // dsext4  ccr1[rdreg],<rtreg>
2954         else
2955         {
2956            UML_XOR(block, IREG(0), CCR132(31), R32(RTREG));               // xor     i0,ccr1[31],<rtreg>
2957            UML_DSEXT4(block, CCR164(31), R32(RTREG));                     // dsext4  ccr1[31],<rtreg>
2958            UML_TEST(block, IREG(0), IMM(3));                           // test    i0,3
2959            UML_JMPc(block, IF_Z, skip = compiler->labelnum++);               // jmp     skip,Z
2960            UML_AND(block, IREG(0), CCR132(31), IMM(3));                  // and     i0,ccr1[31],3
2961            UML_LOAD1U(block, IREG(0), &mips3.cstate->fpmode[0], IREG(0));      // load1u  i0,fpmode,i0
2962            UML_SETFMOD(block, IREG(0));                              // setfmod i0
2963            UML_LABEL(block, skip);                                 // skip:
2964         }
2965         return TRUE;
2966
2967      case 0x08:   /* BC */
2968         switch ((op >> 16) & 3)
2969         {
2970            case 0x00:   /* BCzF - MIPS I */
2971            case 0x02:   /* BCzFL - MIPS II */
2972               UML_TEST(block, CCR132(31), IMM(FCCMASK(op >> 18)));         // test    ccr1[31],fccmask[which]
2973               UML_JMPc(block, IF_NZ, skip = compiler->labelnum++);         // jmp     skip,NZ
2974               generate_delay_slot_and_branch(block, compiler, desc, 0);      // <next instruction + hashjmp>
2975               UML_LABEL(block, skip);                              // skip:
2976               return TRUE;
2977
2978            case 0x01:   /* BCzT - MIPS I */
2979            case 0x03:   /* BCzTL - MIPS II */
2980               UML_TEST(block, CCR132(31), IMM(FCCMASK(op >> 18)));         // test    ccr1[31],fccmask[which]
2981               UML_JMPc(block, IF_Z, skip = compiler->labelnum++);            // jmp     skip,Z
2982               generate_delay_slot_and_branch(block, compiler, desc, 0);      // <next instruction + hashjmp>
2983               UML_LABEL(block, skip);                              // skip:
2984               return TRUE;
2985         }
2986         break;
2987
2988      default:
2989         switch (op & 0x3f)
2990         {
2991            case 0x00:
2992               if (IS_SINGLE(op))   /* ADD.S - MIPS I */
2993                  UML_FSADD(block, FPR32(FDREG), FPR32(FSREG), FPR32(FTREG));   // fsadd   <fdreg>,<fsreg>,<ftreg>
2994               else            /* ADD.D - MIPS I */
2995                  UML_FDADD(block, FPR64(FDREG), FPR64(FSREG), FPR64(FTREG));   // fdadd   <fdreg>,<fsreg>,<ftreg>
2996               return TRUE;
2997
2998            case 0x01:
2999               if (IS_SINGLE(op))   /* SUB.S - MIPS I */
3000                  UML_FSSUB(block, FPR32(FDREG), FPR32(FSREG), FPR32(FTREG));   // fssub   <fdreg>,<fsreg>,<ftreg>
3001               else            /* SUB.D - MIPS I */
3002                  UML_FDSUB(block, FPR64(FDREG), FPR64(FSREG), FPR64(FTREG));   // fdsub   <fdreg>,<fsreg>,<ftreg>
3003               return TRUE;
3004
3005            case 0x02:
3006               if (IS_SINGLE(op))   /* MUL.S - MIPS I */
3007                  UML_FSMUL(block, FPR32(FDREG), FPR32(FSREG), FPR32(FTREG));   // fsmul   <fdreg>,<fsreg>,<ftreg>
3008               else            /* MUL.D - MIPS I */
3009                  UML_FDMUL(block, FPR64(FDREG), FPR64(FSREG), FPR64(FTREG));   // fdmul   <fdreg>,<fsreg>,<ftreg>
3010               return TRUE;
3011
3012            case 0x03:
3013               if (IS_SINGLE(op))   /* DIV.S - MIPS I */
3014                  UML_FSDIV(block, FPR32(FDREG), FPR32(FSREG), FPR32(FTREG));   // fsdiv   <fdreg>,<fsreg>,<ftreg>
3015               else            /* DIV.D - MIPS I */
3016                  UML_FDDIV(block, FPR64(FDREG), FPR64(FSREG), FPR64(FTREG));   // fddiv   <fdreg>,<fsreg>,<ftreg>
3017               return TRUE;
3018
3019            case 0x04:
3020               if (IS_SINGLE(op))   /* SQRT.S - MIPS II */
3021                  UML_FSSQRT(block, FPR32(FDREG), FPR32(FSREG));            // fssqrt  <fdreg>,<fsreg>
3022               else            /* SQRT.D - MIPS II */
3023                  UML_FDSQRT(block, FPR64(FDREG), FPR64(FSREG));            // fdsqrt  <fdreg>,<fsreg>
3024               return TRUE;
3025
3026            case 0x05:
3027               if (IS_SINGLE(op))   /* ABS.S - MIPS I */
3028                  UML_FSABS(block, FPR32(FDREG), FPR32(FSREG));            // fsabs   <fdreg>,<fsreg>
3029               else            /* ABS.D - MIPS I */
3030                  UML_FDABS(block, FPR64(FDREG), FPR64(FSREG));            // fdabs   <fdreg>,<fsreg>
3031               return TRUE;
3032
3033            case 0x06:
3034               if (IS_SINGLE(op))   /* MOV.S - MIPS I */
3035                  UML_FSMOV(block, FPR32(FDREG), FPR32(FSREG));            // fsmov   <fdreg>,<fsreg>
3036               else            /* MOV.D - MIPS I */
3037                  UML_FDMOV(block, FPR64(FDREG), FPR64(FSREG));            // fdmov   <fdreg>,<fsreg>
3038               return TRUE;
3039
3040            case 0x07:
3041               if (IS_SINGLE(op))   /* NEG.S - MIPS I */
3042                  UML_FSNEG(block, FPR32(FDREG), FPR32(FSREG));            // fsneg   <fdreg>,<fsreg>
3043               else            /* NEG.D - MIPS I */
3044                  UML_FDNEG(block, FPR64(FDREG), FPR64(FSREG));            // fdneg   <fdreg>,<fsreg>
3045               return TRUE;
3046
3047            case 0x08:
3048               if (IS_SINGLE(op))   /* ROUND.L.S - MIPS III */
3049                  UML_FSTOI8R(block, FPR64(FDREG), FPR32(FSREG));            // fstoi8r <fdreg>,<fsreg>
3050               else            /* ROUND.L.D - MIPS III */
3051                  UML_FDTOI8R(block, FPR64(FDREG), FPR64(FSREG));            // fdtoi8r <fdreg>,<fsreg>
3052               return TRUE;
3053
3054            case 0x09:
3055               if (IS_SINGLE(op))   /* TRUNC.L.S - MIPS III */
3056                  UML_FSTOI8T(block, FPR64(FDREG), FPR32(FSREG));            // fstoi8t <fdreg>,<fsreg>
3057               else            /* TRUNC.L.D - MIPS III */
3058                  UML_FDTOI8T(block, FPR64(FDREG), FPR64(FSREG));            // fdtoi8t <fdreg>,<fsreg>
3059               return TRUE;
3060
3061            case 0x0a:
3062               if (IS_SINGLE(op))   /* CEIL.L.S - MIPS III */
3063                  UML_FSTOI8C(block, FPR64(FDREG), FPR32(FSREG));            // fstoi8c <fdreg>,<fsreg>
3064               else            /* CEIL.L.D - MIPS III */
3065                  UML_FDTOI8C(block, FPR64(FDREG), FPR64(FSREG));            // fdtoi8c <fdreg>,<fsreg>
3066               return TRUE;
3067
3068            case 0x0b:
3069               if (IS_SINGLE(op))   /* FLOOR.L.S - MIPS III */
3070                  UML_FSTOI8F(block, FPR64(FDREG), FPR32(FSREG));            // fstoi8f <fdreg>,<fsreg>
3071               else            /* FLOOR.L.D - MIPS III */
3072                  UML_FDTOI8F(block, FPR64(FDREG), FPR64(FSREG));            // fdtoi8f <fdreg>,<fsreg>
3073               return TRUE;
3074
3075            case 0x0c:
3076               if (IS_SINGLE(op))   /* ROUND.W.S - MIPS II */
3077                  UML_FSTOI4R(block, FPR32(FDREG), FPR32(FSREG));            // fstoi4r <fdreg>,<fsreg>
3078               else            /* ROUND.W.D - MIPS II */
3079                  UML_FDTOI4R(block, FPR32(FDREG), FPR64(FSREG));            // fdtoi4r <fdreg>,<fsreg>
3080               return TRUE;
3081
3082            case 0x0d:
3083               if (IS_SINGLE(op))   /* TRUNC.W.S - MIPS II */
3084                  UML_FSTOI4T(block, FPR32(FDREG), FPR32(FSREG));            // fstoi4t <fdreg>,<fsreg>
3085               else            /* TRUNC.W.D - MIPS II */
3086                  UML_FDTOI4T(block, FPR32(FDREG), FPR64(FSREG));            // fdtoi4t <fdreg>,<fsreg>
3087               return TRUE;
3088
3089            case 0x0e:
3090               if (IS_SINGLE(op))   /* CEIL.W.S - MIPS II */
3091                  UML_FSTOI4C(block, FPR32(FDREG), FPR32(FSREG));            // fstoi4c <fdreg>,<fsreg>
3092               else            /* CEIL.W.D - MIPS II */
3093                  UML_FDTOI4C(block, FPR32(FDREG), FPR64(FSREG));            // fdtoi4c <fdreg>,<fsreg>
3094               return TRUE;
3095
3096            case 0x0f:
3097               if (IS_SINGLE(op))   /* FLOOR.W.S - MIPS II */
3098                  UML_FSTOI4F(block, FPR32(FDREG), FPR32(FSREG));            // fstoi4f <fdreg>,<fsreg>
3099               else            /* FLOOR.W.D - MIPS II */
3100                  UML_FDTOI4F(block, FPR32(FDREG), FPR64(FSREG));            // fdtoi4f <fdreg>,<fsreg>
3101               return TRUE;
3102
3103            case 0x11:
3104               condition = ((op >> 16) & 1) ? IF_NZ : IF_Z;
3105               UML_TEST(block, CCR132(31), IMM(FCCMASK(op >> 18)));         // test    ccr31,fccmask[op]
3106               if (IS_SINGLE(op))   /* MOVT/F.S - MIPS IV */
3107                  UML_FSMOVc(block, condition, FPR32(FDREG), FPR32(FSREG));   // fsmov   <fdreg>,<fsreg>,condition
3108               else            /* MOVT/F.D - MIPS IV */
3109                  UML_FDMOVc(block, condition, FPR64(FDREG), FPR64(FSREG));   // fdmov   <fdreg>,<fsreg>,condition
3110               return TRUE;
3111
3112            case 0x12:
3113               UML_DCMP(block, R64(RTREG), IMM(0));                     // dcmp    <rtreg>,0
3114               if (IS_SINGLE(op))   /* MOVZ.S - MIPS IV */
3115                  UML_FSMOVc(block, IF_Z, FPR32(FDREG), FPR32(FSREG));      // fsmov   <fdreg>,<fsreg>,Z
3116               else            /* MOVZ.D - MIPS IV */
3117                  UML_FDMOVc(block, IF_Z, FPR64(FDREG), FPR64(FSREG));      // fdmov   <fdreg>,<fsreg>,Z
3118               return TRUE;
3119
3120            case 0x13:
3121               UML_DCMP(block, R64(RTREG), IMM(0));                     // dcmp    <rtreg>,0
3122               if (IS_SINGLE(op))   /* MOVN.S - MIPS IV */
3123                  UML_FSMOVc(block, IF_NZ, FPR32(FDREG), FPR32(FSREG));      // fsmov   <fdreg>,<fsreg>,NZ
3124               else            /* MOVN.D - MIPS IV */
3125                  UML_FDMOVc(block, IF_NZ, FPR64(FDREG), FPR64(FSREG));      // fdmov   <fdreg>,<fsreg>,NZ
3126               return TRUE;
3127
3128            case 0x15:
3129               if (IS_SINGLE(op))   /* RECIP.S - MIPS IV */
3130                  UML_FSRECIP(block, FPR32(FDREG), FPR32(FSREG));            // fsrecip <fdreg>,<fsreg>
3131               else            /* RECIP.D - MIPS IV */
3132                  UML_FDRECIP(block, FPR64(FDREG), FPR64(FSREG));            // fdrecip <fdreg>,<fsreg>
3133               return TRUE;
3134
3135            case 0x16:
3136               if (IS_SINGLE(op))   /* RSQRT.S - MIPS IV */
3137                  UML_FSRSQRT(block, FPR32(FDREG), FPR32(FSREG));            // fsrsqrt <fdreg>,<fsreg>
3138               else            /* RSQRT.D - MIPS IV */
3139                  UML_FDRSQRT(block, FPR64(FDREG), FPR64(FSREG));            // fdrsqrt <fdreg>,<fsreg>
3140               return TRUE;
3141
3142            case 0x20:
3143               if (IS_INTEGRAL(op))
3144               {
3145                  if (IS_SINGLE(op))   /* CVT.S.W - MIPS I */
3146                     UML_FSFRI4(block, FPR32(FDREG), FPR32(FSREG));         // fsfri4  <fdreg>,<fsreg>
3147                  else            /* CVT.S.L - MIPS I */
3148                     UML_FSFRI8(block, FPR32(FDREG), FPR64(FSREG));         // fsfri8  <fdreg>,<fsreg>
3149               }
3150               else               /* CVT.S.D - MIPS I */
3151                  UML_FSFRFD(block, FPR32(FDREG), FPR64(FSREG));            // fsfrfd  <fdreg>,<fsreg>
3152               return TRUE;
3153
3154            case 0x21:
3155               if (IS_INTEGRAL(op))
3156               {
3157                  if (IS_SINGLE(op))   /* CVT.D.W - MIPS I */
3158                     UML_FDFRI4(block, FPR64(FDREG), FPR32(FSREG));         // fdfri4  <fdreg>,<fsreg>
3159                  else            /* CVT.D.L - MIPS I */
3160                     UML_FDFRI8(block, FPR64(FDREG), FPR64(FSREG));         // fdfri8  <fdreg>,<fsreg>
3161               }
3162               else               /* CVT.D.S - MIPS I */
3163                  UML_FDFRFS(block, FPR64(FDREG), FPR32(FSREG));            // fdfrfs  <fdreg>,<fsreg>
3164               return TRUE;
3165
3166            case 0x24:
3167               if (IS_SINGLE(op))   /* CVT.W.S - MIPS I */
3168                  UML_FSTOI4(block, FPR32(FDREG), FPR32(FSREG));            // fstoi4  <fdreg>,<fsreg>
3169               else            /* CVT.W.D - MIPS I */
3170                  UML_FDTOI4(block, FPR32(FDREG), FPR64(FSREG));            // fdtoi4  <fdreg>,<fsreg>
3171               return TRUE;
3172
3173            case 0x25:
3174               if (IS_SINGLE(op))   /* CVT.L.S - MIPS I */
3175                  UML_FSTOI8(block, FPR64(FDREG), FPR32(FSREG));            // fstoi8  <fdreg>,<fsreg>
3176               else            /* CVT.L.D - MIPS I */
3177                  UML_FDTOI8(block, FPR64(FDREG), FPR64(FSREG));            // fdtoi8  <fdreg>,<fsreg>
3178               return TRUE;
3179
3180            case 0x30:
3181            case 0x38:            /* C.F.S/D - MIPS I */
3182               UML_AND(block, CCR132(31), CCR132(31), IMM(~FCCMASK(op >> 8)));   // and     ccr31,ccr31,~fccmask[op]
3183               return TRUE;
3184
3185            case 0x31:
3186            case 0x39:
3187               if (IS_SINGLE(op))   /* C.UN.S - MIPS I */
3188                  UML_FSCMP(block, FPR32(FSREG), FPR32(FTREG));            // fscmp   <fsreg>,<ftreg>
3189               else            /* C.UN.D - MIPS I */
3190                  UML_FDCMP(block, FPR64(FSREG), FPR64(FTREG));            // fdcmp   <fsreg>,<ftreg>
3191               UML_FLAGS(block, CCR132(31), FCCMASK(op >> 8), mips3.cstate->c_un_table);
3192                                                               // flags   ccr31,fccmask[op],c_eq_table
3193               return TRUE;
3194
3195            case 0x32:
3196            case 0x3a:
3197               if (IS_SINGLE(op))   /* C.EQ.S - MIPS I */
3198                  UML_FSCMP(block, FPR32(FSREG), FPR32(FTREG));            // fscmp   <fsreg>,<ftreg>
3199               else            /* C.EQ.D - MIPS I */
3200                  UML_FDCMP(block, FPR64(FSREG), FPR64(FTREG));            // fdcmp   <fsreg>,<ftreg>
3201               UML_FLAGS(block, CCR132(31), FCCMASK(op >> 8), mips3.cstate->c_eq_table);
3202                                                               // flags   ccr31,fccmask[op],c_eq_table
3203               return TRUE;
3204
3205            case 0x33:
3206            case 0x3b:
3207               if (IS_SINGLE(op))   /* C.UEQ.S - MIPS I */
3208                  UML_FSCMP(block, FPR32(FSREG), FPR32(FTREG));            // fscmp   <fsreg>,<ftreg>
3209               else            /* C.UEQ.D - MIPS I */
3210                  UML_FDCMP(block, FPR64(FSREG), FPR64(FTREG));            // fdcmp   <fsreg>,<ftreg>
3211               UML_FLAGS(block, CCR132(31), FCCMASK(op >> 8), mips3.cstate->c_ueq_table);
3212                                                               // flags   ccr31,fccmask[op],c_eq_table
3213               return TRUE;
3214
3215            case 0x34:
3216            case 0x3c:
3217               if (IS_SINGLE(op))   /* C.OLT.S - MIPS I */
3218                  UML_FSCMP(block, FPR32(FSREG), FPR32(FTREG));            // fscmp   <fsreg>,<ftreg>
3219               else            /* C.OLT.D - MIPS I */
3220                  UML_FDCMP(block, FPR64(FSREG), FPR64(FTREG));            // fdcmp   <fsreg>,<ftreg>
3221               UML_FLAGS(block, CCR132(31), FCCMASK(op >> 8), mips3.cstate->c_olt_table);
3222                                                               // flags   ccr31,fccmask[op],c_lt_table
3223               return TRUE;
3224
3225            case 0x35:
3226            case 0x3d:
3227               if (IS_SINGLE(op))   /* C.ULT.S - MIPS I */
3228                  UML_FSCMP(block, FPR32(FSREG), FPR32(FTREG));            // fscmp   <fsreg>,<ftreg>
3229               else            /* C.ULT.D - MIPS I */
3230                  UML_FDCMP(block, FPR64(FSREG), FPR64(FTREG));            // fdcmp   <fsreg>,<ftreg>
3231               UML_FLAGS(block, CCR132(31), FCCMASK(op >> 8), mips3.cstate->c_ult_table);
3232                                                               // flags   ccr31,fccmask[op],c_lt_table
3233               return TRUE;
3234
3235            case 0x36:
3236            case 0x3e:
3237               if (IS_SINGLE(op))   /* C.OLE.S - MIPS I */
3238                  UML_FSCMP(block, FPR32(FSREG), FPR32(FTREG));            // fscmp   <fsreg>,<ftreg>
3239               else            /* C.OLE.D - MIPS I */
3240                  UML_FDCMP(block, FPR64(FSREG), FPR64(FTREG));            // fdcmp   <fsreg>,<ftreg>
3241               UML_FLAGS(block, CCR132(31), FCCMASK(op >> 8), mips3.cstate->c_ole_table);
3242                                                               // flags   ccr31,fccmask[op],c_le_table
3243               return TRUE;
3244
3245            case 0x37:
3246            case 0x3f:
3247               if (IS_SINGLE(op))   /* C.ULE.S - MIPS I */
3248                  UML_FSCMP(block, FPR32(FSREG), FPR32(FTREG));            // fscmp   <fsreg>,<ftreg>
3249               else            /* C.ULE.D - MIPS I */
3250                  UML_FDCMP(block, FPR64(FSREG), FPR64(FTREG));            // fdcmp   <fsreg>,<ftreg>
3251               UML_FLAGS(block, CCR132(31), FCCMASK(op >> 8), mips3.cstate->c_ule_table);
3252                                                               // flags   ccr31,fccmask[op],c_le_table
3253               return TRUE;
3254         }
3255         break;
3256   }
3257   return FALSE;
3258}
3259
3260
3261
3262/***************************************************************************
3263    COP1X RECOMPILATION
3264***************************************************************************/
3265
3266/*-------------------------------------------------
3267    generate_cop1x - compile COP1X opcodes
3268-------------------------------------------------*/
3269
3270static int generate_cop1x(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc)
3271{
3272   int in_delay_slot = ((desc->flags & OPFLAG_IN_DELAY_SLOT) != 0);
3273   UINT32 op = *desc->opptr.l;
3274
3275   if (mips3.drcoptions & MIPS3DRC_STRICT_COP1)
3276   {
3277      UML_TEST(block, CPR032(COP0_Status), IMM(SR_COP1));                     // test    [Status],SR_COP1
3278      UML_EXHc(block, IF_Z, mips3.exception[EXCEPTION_BADCOP], IMM(0));         // exh     cop,0,Z
3279   }
3280
3281   switch (op & 0x3f)
3282   {
3283      case 0x00:      /* LWXC1 - MIPS IV */
3284         UML_ADD(block, IREG(0), R32(RSREG), R32(RTREG));                  // add     i0,<rsreg>,<rtreg>
3285         UML_CALLH(block, mips3.read32);                                 // callh   read32
3286         UML_MOV(block, FPR32(RDREG), IREG(0));                           // mov     <cpr1_rd>,i0
3287         if (!in_delay_slot)
3288            generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE);
3289         return TRUE;
3290
3291      case 0x01:      /* LDXC1 - MIPS IV */
3292         UML_ADD(block, IREG(0), R32(RSREG), R32(RTREG));                  // add     i0,<rsreg>,<rtreg>
3293         UML_CALLH(block, mips3.read64);                                 // callh   read64
3294         UML_DMOV(block, FPR64(RDREG), IREG(0));                           // dmov    <cpr1_rd>,i0
3295         if (!in_delay_slot)
3296            generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE);
3297         return TRUE;
3298
3299      case 0x08:      /* SWXC1 - MIPS IV */
3300         UML_ADD(block, IREG(0), R32(RSREG), R32(RTREG));                  // add     i0,<rsreg>,<rtreg>
3301         UML_MOV(block, IREG(1), FPR32(RTREG));                           // mov     i1,<cpr1_rt>
3302         UML_CALLH(block, mips3.write32);                              // callh   write32
3303         if (!in_delay_slot)
3304            generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE);
3305         return TRUE;
3306
3307      case 0x09:      /* SDXC1 - MIPS IV */
3308         UML_ADD(block, IREG(0), R32(RSREG), R32(RTREG));                  // add     i0,<rsreg>,<rtreg>
3309         UML_DMOV(block, IREG(1), FPR64(RTREG));                           // dmov    i1,<cpr1_rt>
3310         UML_CALLH(block, mips3.write64);                              // callh   write64
3311         if (!in_delay_slot)
3312            generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE);
3313         return TRUE;
3314
3315      case 0x0f:      /* PREFX */
3316         return TRUE;
3317
3318      case 0x20:      /* MADD.S - MIPS IV */
3319         UML_FSMUL(block, FREG(0), FPR32(FSREG), FPR32(FTREG));               // fsmul   f0,<fsreg>,<ftreg>
3320         UML_FSADD(block, FPR32(FDREG), FREG(0), FPR32(FRREG));               // fsadd   <fdreg>,f0,<frreg>
3321         return TRUE;
3322
3323      case 0x21:      /* MADD.D - MIPS IV */
3324         UML_FDMUL(block, FREG(0), FPR64(FSREG), FPR64(FTREG));               // fdmul   f0,<fsreg>,<ftreg>
3325         UML_FDADD(block, FPR64(FDREG), FREG(0), FPR64(FRREG));               // fdadd   <fdreg>,f0,<frreg>
3326         return TRUE;
3327
3328      case 0x28:      /* MSUB.S - MIPS IV */
3329         UML_FSMUL(block, FREG(0), FPR32(FSREG), FPR32(FTREG));               // fsmul   f0,<fsreg>,<ftreg>
3330         UML_FSSUB(block, FPR32(FDREG), FREG(0), FPR32(FRREG));               // fssub   <fdreg>,f0,<frreg>
3331         return TRUE;
3332
3333      case 0x29:      /* MSUB.D - MIPS IV */
3334         UML_FDMUL(block, FREG(0), FPR64(FSREG), FPR64(FTREG));               // fdmul   f0,<fsreg>,<ftreg>
3335         UML_FDSUB(block, FPR64(FDREG), FREG(0), FPR64(FRREG));               // fdsub   <fdreg>,f0,<frreg>
3336         return TRUE;
3337
3338      case 0x30:      /* NMADD.S - MIPS IV */
3339         UML_FSMUL(block, FREG(0), FPR32(FSREG), FPR32(FTREG));               // fsmul   f0,<fsreg>,<ftreg>
3340         UML_FSADD(block, FREG(0), FREG(0), FPR32(FRREG));                  // fsadd   f0,f0,<frreg>
3341         UML_FSNEG(block, FPR32(FDREG), FREG(0));                        // fsneg   <fdreg>,f0
3342         return TRUE;
3343
3344      case 0x31:      /* NMADD.D - MIPS IV */
3345         UML_FDMUL(block, FREG(0), FPR64(FSREG), FPR64(FTREG));               // fdmul   f0,<fsreg>,<ftreg>
3346         UML_FDADD(block, FREG(0), FREG(0), FPR64(FRREG));                  // fdadd   f0,f0,<frreg>
3347         UML_FDNEG(block, FPR64(FDREG), FREG(0));                        // fdneg   <fdreg>,f0
3348         return TRUE;
3349
3350      case 0x38:      /* NMSUB.S - MIPS IV */
3351         UML_FSMUL(block, FREG(0), FPR32(FSREG), FPR32(FTREG));               // fsmul   f0,<fsreg>,<ftreg>
3352         UML_FSSUB(block, FPR32(FDREG), FPR32(FRREG), FREG(0));               // fssub   <fdreg>,<frreg>,f0
3353         return TRUE;
3354
3355      case 0x39:      /* NMSUB.D - MIPS IV */
3356         UML_FDMUL(block, FREG(0), FPR64(FSREG), FPR64(FTREG));               // fdmul   f0,<fsreg>,<ftreg>
3357         UML_FDSUB(block, FPR64(FDREG), FPR64(FRREG), FREG(0));               // fdsub   <fdreg>,<frreg>,f0
3358         return TRUE;
3359
3360      default:
3361         fprintf(stderr, "cop1x %X\n", op);
3362         break;
3363   }
3364   return FALSE;
3365}
3366
3367
3368
3369/***************************************************************************
3613370    CODE LOGGING HELPERS
3623371***************************************************************************/
3633372
r2627r2628
3663375    including disassembly of a MIPS instruction
3673376-------------------------------------------------*/
3683377
369static void log_add_disasm_comment(x86log_context *log, x86code *base, UINT32 pc, UINT32 op)
3378static void log_add_disasm_comment(drcuml_block *block, UINT32 pc, UINT32 op)
3703379{
371#if (LOG_CODE)
372   {
373      char buffer[100];
374      dasmmips3(buffer, pc, op);
375      x86log_add_comment(log, base, "%08X: %s", pc, buffer);
376   }
3380#if (LOG_UML)
3381   char buffer[100];
3382   dasmmips3(buffer, pc, op);
3383   UML_COMMENT(block, "%08X: %s", pc, buffer);                              // comment
3773384#endif
3783385}
3793386
3803387
381#ifdef PTR64
382
3833388/*-------------------------------------------------
384    desc_flags_to_string - generate a string
3389    log_desc_flags_to_string - generate a string
3853390    representing the instruction description
3863391    flags
3873392-------------------------------------------------*/
3883393
389static const char *desc_flags_to_string(UINT32 flags)
3394static const char *log_desc_flags_to_string(UINT32 flags)
3903395{
3913396   static char tempbuf[30];
3923397   char *dest = tempbuf;
r2627r2628
4403445    log_register_list - log a list of GPR registers
4413446-------------------------------------------------*/
4423447
443static void log_register_list(x86log_context *log, const char *string, UINT64 gprmask, UINT64 fprmask)
3448static void log_register_list(drcuml_state *drcuml, const char *string, UINT64 gprmask, UINT64 fprmask)
4443449{
4453450   int count = 0;
4463451   int regnum;
r2627r2628
4493454   if ((gprmask & ~1) == 0 && fprmask == 0)
4503455      return;
4513456
452   x86log_printf(log, "[%s:", string);
3457   drcuml_log_printf(drcuml, "[%s:", string);
4533458
4543459   for (regnum = 1; regnum < 32; regnum++)
4553460      if (gprmask & ((UINT64)1 << regnum))
456         x86log_printf(log, "%sr%d", (count++ == 0) ? "" : ",", regnum);
3461         drcuml_log_printf(drcuml, "%sr%d", (count++ == 0) ? "" : ",", regnum);
4573462   if (gprmask & ((UINT64)1 << REG_LO))
458      x86log_printf(log, "%slo", (count++ == 0) ? "" : ",");
3463      drcuml_log_printf(drcuml, "%slo", (count++ == 0) ? "" : ",");
4593464   if (gprmask & ((UINT64)1 << REG_HI))
460      x86log_printf(log, "%shi", (count++ == 0) ? "" : ",");
3465      drcuml_log_printf(drcuml, "%shi", (count++ == 0) ? "" : ",");
4613466
4623467   for (regnum = 0; regnum < 32; regnum++)
4633468      if (fprmask & ((UINT64)1 << regnum))
464         x86log_printf(log, "%sfpr%d", (count++ == 0) ? "" : ",", regnum);
3469         drcuml_log_printf(drcuml, "%sfpr%d", (count++ == 0) ? "" : ",", regnum);
4653470   if (fprmask & REGFLAG_FCC)
466      x86log_printf(log, "%sfcc", (count++ == 0) ? "" : ",");
467   x86log_printf(log, "] ");
3471      drcuml_log_printf(drcuml, "%sfcc", (count++ == 0) ? "" : ",");
3472   drcuml_log_printf(drcuml, "] ");
4683473}
4693474
4703475
r2627r2628
4723477    log_opcode_desc - log a list of descriptions
4733478-------------------------------------------------*/
4743479
475static void log_opcode_desc(x86log_context *log, const opcode_desc *desclist, int indent)
3480static void log_opcode_desc(drcuml_state *drcuml, const opcode_desc *desclist, int indent)
4763481{
4773482   /* open the file, creating it if necessary */
4783483   if (indent == 0)
479      x86log_printf(log, "\nDescriptor list @ %08X\n", desclist->pc);
3484      drcuml_log_printf(drcuml, "\nDescriptor list @ %08X\n", desclist->pc);
4803485
4813486   /* output each descriptor */
4823487   for ( ; desclist != NULL; desclist = desclist->next)
r2627r2628
4843489      char buffer[100];
4853490
4863491      /* disassemle the current instruction and output it to the log */
487#if LOG_CODE
3492#if LOG_UML
4883493      dasmmips3(buffer, desclist->pc, *desclist->opptr.l);
4893494#else
4903495      strcpy(buffer, "???");
4913496#endif
492      x86log_printf(log, "%08X [%08X] t:%08X f:%s: %-30s", desclist->pc, desclist->physpc, desclist->targetpc, desc_flags_to_string(desclist->flags), buffer);
3497      drcuml_log_printf(drcuml, "%08X [%08X] t:%08X f:%s: %-30s", desclist->pc, desclist->physpc, desclist->targetpc, log_desc_flags_to_string(desclist->flags), buffer);
4933498
4943499      /* output register states */
495      log_register_list(log, "use", desclist->gpr.used, desclist->fpr.used);
496      log_register_list(log, "mod", desclist->gpr.modified, desclist->fpr.modified);
497      log_register_list(log, "lrd", desclist->gpr.liveread, desclist->fpr.liveread);
498      log_register_list(log, "lwr", desclist->gpr.livewrite, desclist->fpr.livewrite);
499      x86log_printf(log, "\n");
3500      log_register_list(drcuml, "use", desclist->gpr.used, desclist->fpr.used);
3501      log_register_list(drcuml, "mod", desclist->gpr.modified, desclist->fpr.modified);
3502      log_register_list(drcuml, "lrd", desclist->gpr.liveread, desclist->fpr.liveread);
3503      log_register_list(drcuml, "lwr", desclist->gpr.livewrite, desclist->fpr.livewrite);
3504      drcuml_log_printf(drcuml, "\n");
5003505
5013506      /* if we have a delay slot, output it recursively */
5023507      if (desclist->delay != NULL)
503         log_opcode_desc(log, desclist->delay, indent + 1);
3508         log_opcode_desc(drcuml, desclist->delay, indent + 1);
5043509
5053510      /* at the end of a sequence add a dividing line */
5063511      if (desclist->flags & OPFLAG_END_SEQUENCE)
507         x86log_printf(log, "-----\n");
3512         drcuml_log_printf(drcuml, "-----\n");
5083513   }
5093514}
510#endif
5113515
5123516
5133517
5143518/***************************************************************************
515    RECOMPILER CORE
516***************************************************************************/
517
518#ifdef PTR64
519#include "mdrc64.c"
520#else
521#include "mdrcold.c"
522#endif
523
524
525
526/***************************************************************************
5273519    GENERIC GET/SET INFO
5283520***************************************************************************/
5293521
r2627r2628
7253717   }
7263718}
7273719
728
7293720void r4700le_get_info(UINT32 state, cpuinfo *info)
7303721{
7313722   switch (state)
r2627r2628
9133904    DISASSEMBLERS
9143905***************************************************************************/
9153906
916#if !defined(ENABLE_DEBUGGER) && (LOG_CODE)
3907#if !defined(MAME_DEBUG) && (LOG_UML)
9173908#include "mips3dsm.c"
9183909#endif
9193910
920
921
922/***************************************************************************
923    C CORE FOR COMPARISON
924***************************************************************************/
925
926#if COMPARE_AGAINST_C
927
928static UINT8 dummy_read_byte_32le(offs_t offs)
929{
930   if (mips3.access_type == 0)
931   {
932      mips3.access_type = 1;
933      mips3.access_size = 1;
934      mips3.access_addr = offs;
935      mips3.access_mask = 0;
936      mips3.access_data = program_read_byte_32le(offs);
937   }
938   else
939   {
940      assert(mips3.access_type == 1);
941      assert(mips3.access_size == 1);
942      assert(mips3.access_addr == offs);
943      assert(mips3.access_mask == 0);
944      mips3.access_type = 0;
945   }
946   return mips3.access_data;
947}
948
949static UINT16 dummy_read_word_32le(offs_t offs)
950{
951   if (mips3.access_type == 0)
952   {
953      mips3.access_type = 1;
954      mips3.access_size = 2;
955      mips3.access_addr = offs;
956      mips3.access_mask = 0;
957      mips3.access_data = program_read_word_32le(offs);
958   }
959   else
960   {
961      assert(mips3.access_type == 1);
962      assert(mips3.access_size == 2);
963      assert(mips3.access_addr == offs);
964      assert(mips3.access_mask == 0);
965      mips3.access_type = 0;
966   }
967   return mips3.access_data;
968}
969
970static UINT32 dummy_read_dword_32le(offs_t offs)
971{
972   if (mips3.access_type == 0)
973   {
974      mips3.access_type = 1;
975      mips3.access_size = 4;
976      mips3.access_addr = offs;
977      mips3.access_mask = 0;
978      mips3.access_data = program_read_dword_32le(offs);
979   }
980   else
981   {
982      assert(mips3.access_type == 1);
983      assert(mips3.access_size == 4);
984      assert(mips3.access_addr == offs);
985      assert(mips3.access_mask == 0);
986      mips3.access_type = 0;
987   }
988   return mips3.access_data;
989}
990
991static UINT32 dummy_read_masked_32le(offs_t offs, UINT32 mask)
992{
993   if (mips3.access_type == 0)
994   {
995      mips3.access_type = 1;
996      mips3.access_size = 4;
997      mips3.access_addr = offs;
998      mips3.access_mask = mask;
999      mips3.access_data = program_read_masked_32le(offs, mask);
1000   }
1001   else
1002   {
1003      assert(mips3.access_type == 1);
1004      assert(mips3.access_size == 4);
1005      assert(mips3.access_addr == offs);
1006      assert(mips3.access_mask == mask);
1007      mips3.access_type = 0;
1008   }
1009   return mips3.access_data;
1010}
1011
1012static UINT64 dummy_read_qword_32le(offs_t offs)
1013{
1014   if (mips3.access_type == 0)
1015   {
1016      mips3.access_type = 1;
1017      mips3.access_size = 8;
1018      mips3.access_addr = offs;
1019      mips3.access_mask = 0;
1020      mips3.access_data = (*mips3.orighandler.readdouble)(offs);
1021   }
1022   else
1023   {
1024      assert(mips3.access_type == 1);
1025      assert(mips3.access_size == 8);
1026      assert(mips3.access_addr == offs);
1027      assert(mips3.access_mask == 0);
1028      mips3.access_type = 0;
1029   }
1030   return mips3.access_data;
1031}
1032
1033static UINT64 dummy_read_qword_masked_32le(offs_t offs, UINT64 mask)
1034{
1035   if (mips3.access_type == 0)
1036   {
1037      mips3.access_type = 1;
1038      mips3.access_size = 8;
1039      mips3.access_addr = offs;
1040      mips3.access_mask = mask;
1041      mips3.access_data = (*mips3.orighandler.readdouble_masked)(offs, mask);
1042   }
1043   else
1044   {
1045      assert(mips3.access_type == 1);
1046      assert(mips3.access_size == 8);
1047      assert(mips3.access_addr == offs);
1048      assert(mips3.access_mask == mask);
1049      mips3.access_type = 0;
1050   }
1051   return mips3.access_data;
1052}
1053
1054static void dummy_write_byte_32le(offs_t offs, UINT8 data)
1055{
1056   if (mips3.access_type == 0)
1057   {
1058      mips3.access_type = 2;
1059      mips3.access_size = 1;
1060      mips3.access_addr = offs;
1061      mips3.access_data = data;
1062      mips3.access_mask = 0;
1063   }
1064   else
1065   {
1066      assert(mips3.access_type == 2);
1067      assert(mips3.access_size == 1);
1068      assert(mips3.access_addr == offs);
1069      assert(mips3.access_data == data);
1070      assert(mips3.access_mask == 0);
1071      mips3.access_type = 0;
1072      program_write_byte_32le(offs, data);
1073   }
1074}
1075
1076static void dummy_write_word_32le(offs_t offs, UINT16 data)
1077{
1078   if (mips3.access_type == 0)
1079   {
1080      mips3.access_type = 2;
1081      mips3.access_size = 2;
1082      mips3.access_addr = offs;
1083      mips3.access_data = data;
1084      mips3.access_mask = 0;
1085   }
1086   else
1087   {
1088      assert(mips3.access_type == 2);
1089      assert(mips3.access_size == 2);
1090      assert(mips3.access_addr == offs);
1091      assert(mips3.access_data == data);
1092      assert(mips3.access_mask == 0);
1093      mips3.access_type = 0;
1094      program_write_word_32le(offs, data);
1095   }
1096}
1097
1098static void dummy_write_dword_32le(offs_t offs, UINT32 data)
1099{
1100   if (mips3.access_type == 0)
1101   {
1102      mips3.access_type = 2;
1103      mips3.access_size = 4;
1104      mips3.access_addr = offs;
1105      mips3.access_data = data;
1106      mips3.access_mask = 0;
1107   }
1108   else
1109   {
1110      assert(mips3.access_type == 2);
1111      assert(mips3.access_size == 4);
1112      assert(mips3.access_addr == offs);
1113      assert(mips3.access_data == data);
1114      assert(mips3.access_mask == 0);
1115      mips3.access_type = 0;
1116      program_write_dword_32le(offs, data);
1117   }
1118}
1119
1120static void dummy_write_masked_32le(offs_t offs, UINT32 data, UINT32 mask)
1121{
1122   if (mips3.access_type == 0)
1123   {
1124      mips3.access_type = 2;
1125      mips3.access_size = 4;
1126      mips3.access_addr = offs;
1127      mips3.access_data = data;
1128      mips3.access_mask = mask;
1129   }
1130   else
1131   {
1132      assert(mips3.access_type == 2);
1133      assert(mips3.access_size == 4);
1134      assert(mips3.access_addr == offs);
1135      assert(mips3.access_data == data);
1136      assert(mips3.access_mask == mask);
1137      mips3.access_type = 0;
1138      program_write_masked_32le(offs, data, mask);
1139   }
1140}
1141
1142static void dummy_write_qword_32le(offs_t offs, UINT64 data)
1143{
1144   if (mips3.access_type == 0)
1145   {
1146      mips3.access_type = 2;
1147      mips3.access_size = 8;
1148      mips3.access_addr = offs;
1149      mips3.access_data = data;
1150      mips3.access_mask = 0;
1151   }
1152   else
1153   {
1154      assert(mips3.access_type == 2);
1155      assert(mips3.access_size == 8);
1156      assert(mips3.access_addr == offs);
1157      assert(mips3.access_data == data);
1158      assert(mips3.access_mask == 0);
1159      mips3.access_type = 0;
1160      (*mips3.orighandler.writedouble)(offs, data);
1161   }
1162}
1163
1164static void dummy_write_qword_masked_32le(offs_t offs, UINT64 data, UINT64 mask)
1165{
1166   if (mips3.access_type == 0)
1167   {
1168      mips3.access_type = 2;
1169      mips3.access_size = 8;
1170      mips3.access_addr = offs;
1171      mips3.access_data = data;
1172      mips3.access_mask = mask;
1173   }
1174   else
1175   {
1176      assert(mips3.access_type == 2);
1177      assert(mips3.access_size == 8);
1178      assert(mips3.access_addr == offs);
1179      assert(mips3.access_data == data);
1180      assert(mips3.access_mask == mask);
1181      mips3.access_type = 0;
1182      (*mips3.orighandler.writedouble_masked)(offs, data, mask);
1183   }
1184}
1185
1186static const memory_handlers override_memory =
1187{
1188   dummy_read_byte_32le,
1189   dummy_read_word_32le,
1190   dummy_read_dword_32le,
1191   dummy_read_masked_32le,
1192   dummy_read_qword_32le,
1193   dummy_read_qword_masked_32le,
1194
1195   dummy_write_byte_32le,
1196   dummy_write_word_32le,
1197   dummy_write_dword_32le,
1198   dummy_write_masked_32le,
1199   dummy_write_qword_32le,
1200   dummy_write_qword_masked_32le
1201};
1202
1203#undef SR
1204
1205#define mips3            c_mips3
1206#define mips3_regs         c_mips3_regs
1207
1208#define mips3_get_context    c_mips3_get_context
1209#define mips3_set_context    c_mips3_set_context
1210#define mips3_reset       c_mips3_reset
1211#define mips3_translate    c_mips3_translate
1212#define mips3_dasm          c_mips3_dasm
1213#define mips3_execute       c_mips3_execute
1214#define mips3_set_info       c_mips3_set_info
1215#define mips3_get_info       c_mips3_get_info
1216
1217#define r4600be_init      c_r4600be_init
1218#define r4600le_init      c_r4600le_init
1219#define r4600be_get_info   c_r4600be_get_info
1220#define r4600le_get_info   c_r4600le_get_info
1221
1222#define r4650be_init      c_r4650be_init
1223#define r4650le_init      c_r4650le_init
1224#define r4650be_get_info   c_r4650be_get_info
1225#define r4650le_get_info   c_r4650le_get_info
1226
1227#define r4700be_init      c_r4700be_init
1228#define r4700le_init      c_r4700le_init
1229#define r4700be_get_info   c_r4700be_get_info
1230#define r4700le_get_info   c_r4700le_get_info
1231
1232#define r5000be_init      c_r5000be_init
1233#define r5000le_init      c_r5000le_init
1234#define r5000be_get_info   c_r5000be_get_info
1235#define r5000le_get_info   c_r5000le_get_info
1236
1237#define qed5271be_init      c_qed5271be_init
1238#define qed5271le_init      c_qed5271le_init
1239#define qed5271be_get_info   c_qed5271be_get_info
1240#define qed5271le_get_info   c_qed5271le_get_info
1241
1242#define rm7000be_init      c_rm7000be_init
1243#define rm7000le_init      c_rm7000le_init
1244#define rm7000be_get_info   c_rm7000be_get_info
1245#define rm7000le_get_info   c_rm7000le_get_info
1246
1247#include "mips3.c"
1248
1249#undef mips3
1250
1251static void mips3c_init(mips3_flavor flavor, int bigendian, int index, int clock, const struct mips3_config *config, int (*irqcallback)(int))
1252{
1253   size_t memsize = mips3com_init(&c_mips3.core, flavor, bigendian, index, clock, config, irqcallback, NULL);
1254   void *memory = auto_malloc(memsize);
1255   mips3com_init(&c_mips3.core, flavor, bigendian, index, clock, config, irqcallback, memory);
1256   mips3.orighandler = mips3.core->memory;
1257   mips3.core->memory = override_memory;
1258   c_mips3.core.memory = override_memory;
1259}
1260
1261static void mips3c_reset(void)
1262{
1263   c_mips3_reset();
1264}
1265
1266static void execute_c_version(void)
1267{
1268   int save_debug_mode = Machine->debug_mode;
1269   int i;
1270
1271   assert(mips3.access_type == 0);
1272
1273   c_mips3.core.cpr[0][COP0_Cause] = (c_mips3.core.cpr[0][COP0_Cause] & ~0xfc00) | (mips3.core->cpr[0][COP0_Cause] & 0xfc00);
1274   check_irqs();
1275
1276   assert(c_mips3.core.pc == mips3.core->pc);
1277   assert(c_mips3.core.r[REG_HI] == mips3.core->r[REG_HI]);
1278   assert(c_mips3.core.r[REG_LO] == mips3.core->r[REG_LO]);
1279   for (i = 0; i < 32; i++)
1280   {
1281      assert(c_mips3.core.r[i] == mips3.core->r[i]);
1282      assert(c_mips3.core.cpr[0][i] == mips3.core->cpr[0][i]);
1283      assert(c_mips3.core.ccr[0][i] == mips3.core->ccr[0][i]);
1284      assert(c_mips3.core.cpr[1][i] == mips3.core->cpr[1][i]);
1285      assert(c_mips3.core.ccr[1][i] == mips3.core->ccr[1][i]);
1286   }
1287   for (i = 0; i < 8; i++)
1288      assert(c_mips3.core.cf[1][i] == mips3.core->cf[1][i]);
1289   assert(c_mips3.core.count_zero_time == mips3.core->count_zero_time);
1290
1291   mips3.access_type = 0;
1292
1293   Machine->debug_mode = 0;
1294   c_mips3_execute(0);
1295   Machine->debug_mode = save_debug_mode;
1296}
1297
1298static void compare_c_version(void)
1299{
1300}
1301
1302#endif
src/emu/cpu/mips/mips3com.c
r2627r2628
171171   {
172172      UINT32 count = (activecpu_gettotalcycles() - mips->count_zero_time) / 2;
173173      UINT32 compare = mips->cpr[0][COP0_Compare];
174      UINT32 cyclesleft = compare - count;
175      attotime newtime = ATTOTIME_IN_CYCLES(((INT64)cyclesleft * 2), cpu_getactivecpu());
176      timer_adjust_oneshot(mips->compare_int_timer, newtime, cpu_getactivecpu());
174      if (compare > count)
175      {
176         attotime newtime = ATTOTIME_IN_CYCLES(((INT64)(compare - count) * 2), cpu_getactivecpu());
177         timer_adjust_oneshot(mips->compare_int_timer, newtime, cpu_getactivecpu());
178         return;
179      }
177180   }
178   else
179      timer_adjust_oneshot(mips->compare_int_timer, attotime_never, cpu_getactivecpu());
181   timer_adjust_oneshot(mips->compare_int_timer, attotime_never, cpu_getactivecpu());
180182}
181183
182184
r2627r2628
665667      case CPUINFO_STR_REGISTER + MIPS3_HI:         sprintf(info->s, "HI: %08X%08X", (UINT32)(mips->r[REG_HI] >> 32), (UINT32)mips->r[REG_HI]); break;
666668      case CPUINFO_STR_REGISTER + MIPS3_LO:         sprintf(info->s, "LO: %08X%08X", (UINT32)(mips->r[REG_LO] >> 32), (UINT32)mips->r[REG_LO]); break;
667669
670      case CPUINFO_STR_REGISTER + MIPS3_CCR1_31:      sprintf(info->s, "CCR31:%08X", (UINT32)mips->ccr[1][0]); break;
671
668672      case CPUINFO_STR_REGISTER + MIPS3_FPR0:         sprintf(info->s, "FPR0: %08X%08X", (UINT32)(mips->cpr[1][0] >> 32), (UINT32)mips->cpr[1][0]); break;
673      case CPUINFO_STR_REGISTER + MIPS3_FPS0:         sprintf(info->s, "FPS0: !%16g", *(float *)&mips->cpr[1][0]); break;
674      case CPUINFO_STR_REGISTER + MIPS3_FPD0:         sprintf(info->s, "FPD0: !%16g", *(double *)&mips->cpr[1][0]); break;
669675      case CPUINFO_STR_REGISTER + MIPS3_FPR1:         sprintf(info->s, "FPR1: %08X%08X", (UINT32)(mips->cpr[1][1] >> 32), (UINT32)mips->cpr[1][1]); break;
676      case CPUINFO_STR_REGISTER + MIPS3_FPS1:         sprintf(info->s, "FPS1: !%16g", *(float *)&mips->cpr[1][1]); break;
677      case CPUINFO_STR_REGISTER + MIPS3_FPD1:         sprintf(info->s, "FPD1: !%16g", *(double *)&mips->cpr[1][1]); break;
670678      case CPUINFO_STR_REGISTER + MIPS3_FPR2:         sprintf(info->s, "FPR2: %08X%08X", (UINT32)(mips->cpr[1][2] >> 32), (UINT32)mips->cpr[1][2]); break;
679      case CPUINFO_STR_REGISTER + MIPS3_FPS2:         sprintf(info->s, "FPS2: !%16g", *(float *)&mips->cpr[1][2]); break;
680      case CPUINFO_STR_REGISTER + MIPS3_FPD2:         sprintf(info->s, "FPD2: !%16g", *(double *)&mips->cpr[1][2]); break;
671681      case CPUINFO_STR_REGISTER + MIPS3_FPR3:         sprintf(info->s, "FPR3: %08X%08X", (UINT32)(mips->cpr[1][3] >> 32), (UINT32)mips->cpr[1][3]); break;
682      case CPUINFO_STR_REGISTER + MIPS3_FPS3:         sprintf(info->s, "FPS3: !%16g", *(float *)&mips->cpr[1][3]); break;
683      case CPUINFO_STR_REGISTER + MIPS3_FPD3:         sprintf(info->s, "FPD3: !%16g", *(double *)&mips->cpr[1][3]); break;
672684      case CPUINFO_STR_REGISTER + MIPS3_FPR4:         sprintf(info->s, "FPR4: %08X%08X", (UINT32)(mips->cpr[1][4] >> 32), (UINT32)mips->cpr[1][4]); break;
685      case CPUINFO_STR_REGISTER + MIPS3_FPS4:         sprintf(info->s, "FPS4: !%16g", *(float *)&mips->cpr[1][4]); break;
686      case CPUINFO_STR_REGISTER + MIPS3_FPD4:         sprintf(info->s, "FPD4: !%16g", *(double *)&mips->cpr[1][4]); break;
673687      case CPUINFO_STR_REGISTER + MIPS3_FPR5:         sprintf(info->s, "FPR5: %08X%08X", (UINT32)(mips->cpr[1][5] >> 32), (UINT32)mips->cpr[1][5]); break;
688      case CPUINFO_STR_REGISTER + MIPS3_FPS5:         sprintf(info->s, "FPS5: !%16g", *(float *)&mips->cpr[1][5]); break;
689      case CPUINFO_STR_REGISTER + MIPS3_FPD5:         sprintf(info->s, "FPD5: !%16g", *(double *)&mips->cpr[1][5]); break;
674690      case CPUINFO_STR_REGISTER + MIPS3_FPR6:         sprintf(info->s, "FPR6: %08X%08X", (UINT32)(mips->cpr[1][6] >> 32), (UINT32)mips->cpr[1][6]); break;
691      case CPUINFO_STR_REGISTER + MIPS3_FPS6:         sprintf(info->s, "FPS6: !%16g", *(float *)&mips->cpr[1][6]); break;
692      case CPUINFO_STR_REGISTER + MIPS3_FPD6:         sprintf(info->s, "FPD6: !%16g", *(double *)&mips->cpr[1][6]); break;
675693      case CPUINFO_STR_REGISTER + MIPS3_FPR7:         sprintf(info->s, "FPR7: %08X%08X", (UINT32)(mips->cpr[1][7] >> 32), (UINT32)mips->cpr[1][7]); break;
694      case CPUINFO_STR_REGISTER + MIPS3_FPS7:         sprintf(info->s, "FPS7: !%16g", *(float *)&mips->cpr[1][7]); break;
695      case CPUINFO_STR_REGISTER + MIPS3_FPD7:         sprintf(info->s, "FPD7: !%16g", *(double *)&mips->cpr[1][7]); break;
676696      case CPUINFO_STR_REGISTER + MIPS3_FPR8:         sprintf(info->s, "FPR8: %08X%08X", (UINT32)(mips->cpr[1][8] >> 32), (UINT32)mips->cpr[1][8]); break;
697      case CPUINFO_STR_REGISTER + MIPS3_FPS8:         sprintf(info->s, "FPS8: !%16g", *(float *)&mips->cpr[1][8]); break;
698      case CPUINFO_STR_REGISTER + MIPS3_FPD8:         sprintf(info->s, "FPD8: !%16g", *(double *)&mips->cpr[1][8]); break;
677699      case CPUINFO_STR_REGISTER + MIPS3_FPR9:         sprintf(info->s, "FPR9: %08X%08X", (UINT32)(mips->cpr[1][9] >> 32), (UINT32)mips->cpr[1][9]); break;
700      case CPUINFO_STR_REGISTER + MIPS3_FPS9:         sprintf(info->s, "FPS9: !%16g", *(float *)&mips->cpr[1][9]); break;
701      case CPUINFO_STR_REGISTER + MIPS3_FPD9:         sprintf(info->s, "FPD9: !%16g", *(double *)&mips->cpr[1][9]); break;
678702      case CPUINFO_STR_REGISTER + MIPS3_FPR10:      sprintf(info->s, "FPR10:%08X%08X", (UINT32)(mips->cpr[1][10] >> 32), (UINT32)mips->cpr[1][10]); break;
703      case CPUINFO_STR_REGISTER + MIPS3_FPS10:      sprintf(info->s, "FPS10:!%16g", *(float *)&mips->cpr[1][10]); break;
704      case CPUINFO_STR_REGISTER + MIPS3_FPD10:      sprintf(info->s, "FPD10:!%16g", *(double *)&mips->cpr[1][10]); break;
679705      case CPUINFO_STR_REGISTER + MIPS3_FPR11:      sprintf(info->s, "FPR11:%08X%08X", (UINT32)(mips->cpr[1][11] >> 32), (UINT32)mips->cpr[1][11]); break;
706      case CPUINFO_STR_REGISTER + MIPS3_FPS11:      sprintf(info->s, "FPS11:!%16g", *(float *)&mips->cpr[1][11]); break;
707      case CPUINFO_STR_REGISTER + MIPS3_FPD11:      sprintf(info->s, "FPD11:!%16g", *(double *)&mips->cpr[1][11]); break;
680708      case CPUINFO_STR_REGISTER + MIPS3_FPR12:      sprintf(info->s, "FPR12:%08X%08X", (UINT32)(mips->cpr[1][12] >> 32), (UINT32)mips->cpr[1][12]); break;
709      case CPUINFO_STR_REGISTER + MIPS3_FPS12:      sprintf(info->s, "FPS12:!%16g", *(float *)&mips->cpr[1][12]); break;
710      case CPUINFO_STR_REGISTER + MIPS3_FPD12:      sprintf(info->s, "FPD12:!%16g", *(double *)&mips->cpr[1][12]); break;
681711      case CPUINFO_STR_REGISTER + MIPS3_FPR13:      sprintf(info->s, "FPR13:%08X%08X", (UINT32)(mips->cpr[1][13] >> 32), (UINT32)mips->cpr[1][13]); break;
712      case CPUINFO_STR_REGISTER + MIPS3_FPS13:      sprintf(info->s, "FPS13:!%16g", *(float *)&mips->cpr[1][13]); break;
713      case CPUINFO_STR_REGISTER + MIPS3_FPD13:      sprintf(info->s, "FPD13:!%16g", *(double *)&mips->cpr[1][13]); break;
682714      case CPUINFO_STR_REGISTER + MIPS3_FPR14:      sprintf(info->s, "FPR14:%08X%08X", (UINT32)(mips->cpr[1][14] >> 32), (UINT32)mips->cpr[1][14]); break;
715      case CPUINFO_STR_REGISTER + MIPS3_FPS14:      sprintf(info->s, "FPS14:!%16g", *(float *)&mips->cpr[1][14]); break;
716      case CPUINFO_STR_REGISTER + MIPS3_FPD14:      sprintf(info->s, "FPD14:!%16g", *(double *)&mips->cpr[1][14]); break;
683717      case CPUINFO_STR_REGISTER + MIPS3_FPR15:      sprintf(info->s, "FPR15:%08X%08X", (UINT32)(mips->cpr[1][15] >> 32), (UINT32)mips->cpr[1][15]); break;
718      case CPUINFO_STR_REGISTER + MIPS3_FPS15:      sprintf(info->s, "FPS15:!%16g", *(float *)&mips->cpr[1][15]); break;
719      case CPUINFO_STR_REGISTER + MIPS3_FPD15:      sprintf(info->s, "FPD15:!%16g", *(double *)&mips->cpr[1][15]); break;
684720      case CPUINFO_STR_REGISTER + MIPS3_FPR16:      sprintf(info->s, "FPR16:%08X%08X", (UINT32)(mips->cpr[1][16] >> 32), (UINT32)mips->cpr[1][16]); break;
721      case CPUINFO_STR_REGISTER + MIPS3_FPS16:      sprintf(info->s, "FPS16:!%16g", *(float *)&mips->cpr[1][16]); break;
722      case CPUINFO_STR_REGISTER + MIPS3_FPD16:      sprintf(info->s, "FPD16:!%16g", *(double *)&mips->cpr[1][16]); break;
685723      case CPUINFO_STR_REGISTER + MIPS3_FPR17:      sprintf(info->s, "FPR17:%08X%08X", (UINT32)(mips->cpr[1][17] >> 32), (UINT32)mips->cpr[1][17]); break;
724      case CPUINFO_STR_REGISTER + MIPS3_FPS17:      sprintf(info->s, "FPS17:!%16g", *(float *)&mips->cpr[1][17]); break;
725      case CPUINFO_STR_REGISTER + MIPS3_FPD17:      sprintf(info->s, "FPD17:!%16g", *(double *)&mips->cpr[1][17]); break;
686726      case CPUINFO_STR_REGISTER + MIPS3_FPR18:      sprintf(info->s, "FPR18:%08X%08X", (UINT32)(mips->cpr[1][18] >> 32), (UINT32)mips->cpr[1][18]); break;
727      case CPUINFO_STR_REGISTER + MIPS3_FPS18:      sprintf(info->s, "FPS18:!%16g", *(float *)&mips->cpr[1][18]); break;
728      case CPUINFO_STR_REGISTER + MIPS3_FPD18:      sprintf(info->s, "FPD18:!%16g", *(double *)&mips->cpr[1][18]); break;
687729      case CPUINFO_STR_REGISTER + MIPS3_FPR19:      sprintf(info->s, "FPR19:%08X%08X", (UINT32)(mips->cpr[1][19] >> 32), (UINT32)mips->cpr[1][19]); break;
730      case CPUINFO_STR_REGISTER + MIPS3_FPS19:      sprintf(info->s, "FPS19:!%16g", *(float *)&mips->cpr[1][19]); break;
731      case CPUINFO_STR_REGISTER + MIPS3_FPD19:      sprintf(info->s, "FPD19:!%16g", *(double *)&mips->cpr[1][19]); break;
688732      case CPUINFO_STR_REGISTER + MIPS3_FPR20:      sprintf(info->s, "FPR20:%08X%08X", (UINT32)(mips->cpr[1][20] >> 32), (UINT32)mips->cpr[1][20]); break;
733      case CPUINFO_STR_REGISTER + MIPS3_FPS20:      sprintf(info->s, "FPS20:!%16g", *(float *)&mips->cpr[1][20]); break;
734      case CPUINFO_STR_REGISTER + MIPS3_FPD20:      sprintf(info->s, "FPD20:!%16g", *(double *)&mips->cpr[1][20]); break;
689735      case CPUINFO_STR_REGISTER + MIPS3_FPR21:      sprintf(info->s, "FPR21:%08X%08X", (UINT32)(mips->cpr[1][21] >> 32), (UINT32)mips->cpr[1][21]); break;
736      case CPUINFO_STR_REGISTER + MIPS3_FPS21:      sprintf(info->s, "FPS21:!%16g", *(float *)&mips->cpr[1][21]); break;
737      case CPUINFO_STR_REGISTER + MIPS3_FPD21:      sprintf(info->s, "FPD21:!%16g", *(double *)&mips->cpr[1][21]); break;
690738      case CPUINFO_STR_REGISTER + MIPS3_FPR22:      sprintf(info->s, "FPR22:%08X%08X", (UINT32)(mips->cpr[1][22] >> 32), (UINT32)mips->cpr[1][22]); break;
739      case CPUINFO_STR_REGISTER + MIPS3_FPS22:      sprintf(info->s, "FPS22:!%16g", *(float *)&mips->cpr[1][22]); break;
740      case CPUINFO_STR_REGISTER + MIPS3_FPD22:      sprintf(info->s, "FPD22:!%16g", *(double *)&mips->cpr[1][22]); break;
691741      case CPUINFO_STR_REGISTER + MIPS3_FPR23:      sprintf(info->s, "FPR23:%08X%08X", (UINT32)(mips->cpr[1][23] >> 32), (UINT32)mips->cpr[1][23]); break;
742      case CPUINFO_STR_REGISTER + MIPS3_FPS23:      sprintf(info->s, "FPS23:!%16g", *(float *)&mips->cpr[1][23]); break;
743      case CPUINFO_STR_REGISTER + MIPS3_FPD23:      sprintf(info->s, "FPD23:!%16g", *(double *)&mips->cpr[1][23]); break;
692744      case CPUINFO_STR_REGISTER + MIPS3_FPR24:      sprintf(info->s, "FPR24:%08X%08X", (UINT32)(mips->cpr[1][24] >> 32), (UINT32)mips->cpr[1][24]); break;
745      case CPUINFO_STR_REGISTER + MIPS3_FPS24:      sprintf(info->s, "FPS24:!%16g", *(float *)&mips->cpr[1][24]); break;
746      case CPUINFO_STR_REGISTER + MIPS3_FPD24:      sprintf(info->s, "FPD24:!%16g", *(double *)&mips->cpr[1][24]); break;
693747      case CPUINFO_STR_REGISTER + MIPS3_FPR25:      sprintf(info->s, "FPR25:%08X%08X", (UINT32)(mips->cpr[1][25] >> 32), (UINT32)mips->cpr[1][25]); break;
748      case CPUINFO_STR_REGISTER + MIPS3_FPS25:      sprintf(info->s, "FPS25:!%16g", *(float *)&mips->cpr[1][25]); break;
749      case CPUINFO_STR_REGISTER + MIPS3_FPD25:      sprintf(info->s, "FPD25:!%16g", *(double *)&mips->cpr[1][25]); break;
694750      case CPUINFO_STR_REGISTER + MIPS3_FPR26:      sprintf(info->s, "FPR26:%08X%08X", (UINT32)(mips->cpr[1][26] >> 32), (UINT32)mips->cpr[1][26]); break;
751      case CPUINFO_STR_REGISTER + MIPS3_FPS26:      sprintf(info->s, "FPS26:!%16g", *(float *)&mips->cpr[1][26]); break;
752      case CPUINFO_STR_REGISTER + MIPS3_FPD26:      sprintf(info->s, "FPD26:!%16g", *(double *)&mips->cpr[1][26]); break;
695753      case CPUINFO_STR_REGISTER + MIPS3_FPR27:      sprintf(info->s, "FPR27:%08X%08X", (UINT32)(mips->cpr[1][27] >> 32), (UINT32)mips->cpr[1][27]); break;
754      case CPUINFO_STR_REGISTER + MIPS3_FPS27:      sprintf(info->s, "FPS27:!%16g", *(float *)&mips->cpr[1][27]); break;
755      case CPUINFO_STR_REGISTER + MIPS3_FPD27:      sprintf(info->s, "FPD27:!%16g", *(double *)&mips->cpr[1][27]); break;
696756      case CPUINFO_STR_REGISTER + MIPS3_FPR28:      sprintf(info->s, "FPR28:%08X%08X", (UINT32)(mips->cpr[1][28] >> 32), (UINT32)mips->cpr[1][28]); break;
757      case CPUINFO_STR_REGISTER + MIPS3_FPS28:      sprintf(info->s, "FPS28:!%16g", *(float *)&mips->cpr[1][28]); break;
758      case CPUINFO_STR_REGISTER + MIPS3_FPD28:      sprintf(info->s, "FPD28:!%16g", *(double *)&mips->cpr[1][28]); break;
697759      case CPUINFO_STR_REGISTER + MIPS3_FPR29:      sprintf(info->s, "FPR29:%08X%08X", (UINT32)(mips->cpr[1][29] >> 32), (UINT32)mips->cpr[1][29]); break;
760      case CPUINFO_STR_REGISTER + MIPS3_FPS29:      sprintf(info->s, "FPS29:!%16g", *(float *)&mips->cpr[1][29]); break;
761      case CPUINFO_STR_REGISTER + MIPS3_FPD29:      sprintf(info->s, "FPD29:!%16g", *(double *)&mips->cpr[1][29]); break;
698762      case CPUINFO_STR_REGISTER + MIPS3_FPR30:      sprintf(info->s, "FPR30:%08X%08X", (UINT32)(mips->cpr[1][30] >> 32), (UINT32)mips->cpr[1][30]); break;
763      case CPUINFO_STR_REGISTER + MIPS3_FPS30:      sprintf(info->s, "FPS30:!%16g", *(float *)&mips->cpr[1][30]); break;
764      case CPUINFO_STR_REGISTER + MIPS3_FPD30:      sprintf(info->s, "FPD30:!%16g", *(double *)&mips->cpr[1][30]); break;
699765      case CPUINFO_STR_REGISTER + MIPS3_FPR31:      sprintf(info->s, "FPR31:%08X%08X", (UINT32)(mips->cpr[1][31] >> 32), (UINT32)mips->cpr[1][31]); break;
766      case CPUINFO_STR_REGISTER + MIPS3_FPS31:      sprintf(info->s, "FPS31:!%16g", *(float *)&mips->cpr[1][31]); break;
767      case CPUINFO_STR_REGISTER + MIPS3_FPD31:      sprintf(info->s, "FPD31:!%16g", *(double *)&mips->cpr[1][31]); break;
700768   }
701769}
702770
src/emu/cpu/mips/mips3.c
r2627r2628
1010***************************************************************************/
1111
1212#include "debugger.h"
13#include "deprecat.h"
1314#include "mips3com.h"
1415
1516
r2627r2628
17791780                  break;
17801781               case 0x20:   /* ADD */
17811782                  if (ENABLE_OVERFLOWS && RSVAL32 > ~RTVAL32) generate_exception(EXCEPTION_OVERFLOW, 1);
1782                  else RDVAL64 = (INT32)(RSVAL32 + RTVAL32);
1783                  else if (RDREG) RDVAL64 = (INT32)(RSVAL32 + RTVAL32);
17831784                  break;
17841785               case 0x21:   /* ADDU */      if (RDREG) RDVAL64 = (INT32)(RSVAL32 + RTVAL32);            break;
17851786               case 0x22:   /* SUB */
17861787                  if (ENABLE_OVERFLOWS && RSVAL32 < RTVAL32) generate_exception(EXCEPTION_OVERFLOW, 1);
1787                  else RDVAL64 = (INT32)(RSVAL32 - RTVAL32);
1788                  else if (RDREG) RDVAL64 = (INT32)(RSVAL32 - RTVAL32);
17881789                  break;
17891790               case 0x23:   /* SUBU */      if (RDREG) RDVAL64 = (INT32)(RSVAL32 - RTVAL32);            break;
17901791               case 0x24:   /* AND */      if (RDREG) RDVAL64 = RSVAL64 & RTVAL64;                     break;
r2627r2628
17951796               case 0x2b:   /* SLTU */      if (RDREG) RDVAL64 = (UINT64)RSVAL64 < (UINT64)RTVAL64;         break;
17961797               case 0x2c:   /* DADD */
17971798                  if (ENABLE_OVERFLOWS && RSVAL64 > ~RTVAL64) generate_exception(EXCEPTION_OVERFLOW, 1);
1798                  else RDVAL64 = RSVAL64 + RTVAL64;
1799                  else if (RDREG) RDVAL64 = RSVAL64 + RTVAL64;
17991800                  break;
18001801               case 0x2d:   /* DADDU */      if (RDREG) RDVAL64 = RSVAL64 + RTVAL64;                     break;
18011802               case 0x2e:   /* DSUB */
18021803                  if (ENABLE_OVERFLOWS && RSVAL64 < RTVAL64) generate_exception(EXCEPTION_OVERFLOW, 1);
1803                  else RDVAL64 = RSVAL64 - RTVAL64;
1804                  else if (RDREG) RDVAL64 = RSVAL64 - RTVAL64;
18041805                  break;
18051806               case 0x2f:   /* DSUBU */      if (RDREG) RDVAL64 = RSVAL64 - RTVAL64;                     break;
18061807               case 0x30:   /* TGE */      if ((INT64)RSVAL64 >= (INT64)RTVAL64) generate_exception(EXCEPTION_TRAP, 1); break;
src/emu/cpu/drcbeut.c
r0r2628
1/***************************************************************************
2
3    drcbeut.c
4
5    Utility functions for dynamic recompiling backends.
6
7    Copyright Aaron Giles
8    Released for general non-commercial use under the MAME license
9    Visit http://mamedev.org for licensing and usage restrictions.
10
11***************************************************************************/
12
13#include "drcbeut.h"
14
15
16/***************************************************************************
17    TYPE DEFINITIONS
18***************************************************************************/
19
20/* structure holding information about a single label */
21typedef struct _drcmap_entry drcmap_entry;
22struct _drcmap_entry
23{
24   drcmap_entry *      next;            /* pointer to next map entry */
25   drccodeptr         codeptr;         /* pointer to the relevant code */
26   UINT32            mapvar;            /* map variable id */
27   UINT32            newval;            /* value of the variable starting at codeptr */
28};
29
30
31/* structure describing the state of the code map */
32struct _drcmap_state
33{
34   drccache *         cache;            /* pointer to the cache */
35   UINT64            uniquevalue;      /* unique value used to find the table */
36   drcmap_entry *      head;            /* head of the live list */
37   drcmap_entry **      tailptr;         /* pointer to tail of the live list */
38   UINT32            numvalues;         /* number of values  in the list */
39   UINT32             mapvalue[DRCUML_MAPVAR_END - DRCUML_MAPVAR_M0]; /* array of current values */
40};
41
42
43/* structure holding information about a single label */
44typedef struct _drclabel drclabel;
45struct _drclabel
46{
47   drclabel *         next;            /* pointer to next label */
48   drcuml_codelabel   label;            /* the label specified */
49   drccodeptr         codeptr;         /* pointer to the relevant code */
50};
51
52
53/* structure holding a live list of labels */
54struct _drclabel_list
55{
56   drccache *         cache;             /* pointer to the cache */
57   drclabel *         head;            /* head of the live list */
58};
59
60
61
62/***************************************************************************
63    FUNCTION PROTOTYPES
64***************************************************************************/
65
66static void label_list_reset(drclabel_list *list, int fatal_on_leftovers);
67static drclabel *label_find_or_allocate(drclabel_list *list, drcuml_codelabel label);
68static void label_oob_callback(drccodeptr *codeptr, void *param1, void *param2, void *param3);
69
70
71
72/***************************************************************************
73    HASH TABLE MANAGEMENT
74***************************************************************************/
75
76/*-------------------------------------------------
77    drchash_alloc - allocate memory in the cache
78    for the hash table tracker (it auto-frees
79    with the cache)
80-------------------------------------------------*/
81
82drchash_state *drchash_alloc(drccache *cache, int modes, int addrbits, int ignorebits)
83{
84   int effaddrbits = addrbits - ignorebits;
85   drchash_state *drchash;
86
87   /* allocate permanent state from the cache */
88   drchash = drccache_memory_alloc(cache, sizeof(*drchash) + modes * sizeof(drchash->base[0]));
89   if (drchash == NULL)
90      return NULL;
91   memset(drchash, 0, sizeof(*drchash) + modes * sizeof(drchash->base[0]));
92
93   /* copy in parameters */
94   drchash->cache = cache;
95   drchash->modes = modes;
96
97   /* compute the sizes of the tables */
98   drchash->l1bits = effaddrbits / 2;
99   drchash->l2bits = effaddrbits - drchash->l1bits;
100   drchash->l1shift = ignorebits + drchash->l2bits;
101   drchash->l2shift = ignorebits;
102   drchash->l1mask = (1 << drchash->l1bits) - 1;
103   drchash->l2mask = (1 << drchash->l2bits) - 1;
104
105   /* reset the hash table, which allocates any subsequent tables */
106   if (!drchash_reset(drchash))
107      return NULL;
108
109   return drchash;
110}
111
112
113/*-------------------------------------------------
114    drchash_reset - flush existing hash tables and
115    create new ones
116-------------------------------------------------*/
117
118int drchash_reset(drchash_state *drchash)
119{
120   int modenum, entry;
121
122   /* allocate an empty l2 hash table */
123   drchash->emptyl2 = drccache_memory_alloc_temporary(drchash->cache, sizeof(drccodeptr) << drchash->l2bits);
124   if (drchash->emptyl2 == NULL)
125      return FALSE;
126
127   /* populate it with pointers to the recompile_exit code */
128   for (entry = 0; entry < (1 << drchash->l2bits); entry++)
129      drchash->emptyl2[entry] = drchash->nocodeptr;
130
131   /* allocate an empty l1 hash table */
132   drchash->emptyl1 = drccache_memory_alloc_temporary(drchash->cache, sizeof(drccodeptr *) << drchash->l1bits);
133   if (drchash->emptyl1 == NULL)
134      return FALSE;
135
136   /* populate it with pointers to the empty l2 table */
137   for (entry = 0; entry < (1 << drchash->l1bits); entry++)
138      drchash->emptyl1[entry] = drchash->emptyl2;
139
140   /* reset the hash tables */
141   for (modenum = 0; modenum < drchash->modes; modenum++)
142      drchash->base[modenum] = drchash->emptyl1;
143
144   return TRUE;
145}
146
147
148/*-------------------------------------------------
149    drchash_block_begin - note the beginning of a
150    block
151-------------------------------------------------*/
152
153void drchash_block_begin(drchash_state *drchash, drcuml_block *block, const drcuml_instruction *instlist, UINT32 numinst)
154{
155   int inum;
156
157   /* before generating code, pre-allocate any hash entries; we do this by setting dummy hash values */
158   for (inum = 0; inum < numinst; inum++)
159   {
160      const drcuml_instruction *inst = &instlist[inum];
161
162      /* if the opcode is a hash, verify that it makes sense and then set a NULL entry */
163      if (inst->opcode == DRCUML_OP_HASH)
164      {
165         assert(inst->numparams == 2);
166         assert(inst->param[0].type == DRCUML_PTYPE_IMMEDIATE);
167         assert(inst->param[1].type == DRCUML_PTYPE_IMMEDIATE);
168
169         /* if we fail to allocate, we must abort the block */
170         if (!drchash_set_codeptr(drchash, inst->param[0].value, inst->param[1].value, NULL))
171            drcuml_block_abort(block);
172      }
173
174      /* if the opcode is a hashjmp to a fixed location, make sure we preallocate the tables */
175      if (inst->opcode == DRCUML_OP_HASHJMP && inst->param[0].type == DRCUML_PTYPE_IMMEDIATE && inst->param[1].type == DRCUML_PTYPE_IMMEDIATE)
176      {
177         /* if we fail to allocate, we must abort the block */
178         drccodeptr code = drchash_get_codeptr(drchash, inst->param[0].value, inst->param[1].value);
179         if (!drchash_set_codeptr(drchash, inst->param[0].value, inst->param[1].value, code))
180            drcuml_block_abort(block);
181      }
182   }
183}
184
185
186/*-------------------------------------------------
187    drchash_block_end - note the end of a block
188-------------------------------------------------*/
189
190void drchash_block_end(drchash_state *drchash, drcuml_block *block)
191{
192   /* nothing to do here, yet */
193}
194
195
196/*-------------------------------------------------
197    drchash_set_default_codeptr - change the
198    default codeptr
199-------------------------------------------------*/
200
201void drchash_set_default_codeptr(drchash_state *drchash, drccodeptr nocodeptr)
202{
203   drccodeptr old = drchash->nocodeptr;
204   int modenum, l1entry, l2entry;
205
206   /* nothing to do if the same */
207   if (old == nocodeptr)
208      return;
209   drchash->nocodeptr = nocodeptr;
210
211   /* update the empty L2 table first */
212   for (l2entry = 0; l2entry < (1 << drchash->l2bits); l2entry++)
213      drchash->emptyl2[l2entry] = nocodeptr;
214
215   /* now scan all existing hashtables for entries */
216   for (modenum = 0; modenum < drchash->modes; modenum++)
217      if (drchash->base[modenum] != drchash->emptyl1)
218         for (l1entry = 0; l1entry < (1 << drchash->l1bits); l1entry++)
219            if (drchash->base[modenum][l1entry] != drchash->emptyl2)
220               for (l2entry = 0; l2entry < (1 << drchash->l2bits); l2entry++)
221                  if (drchash->base[modenum][l1entry][l2entry] == old)
222                     drchash->base[modenum][l1entry][l2entry] = nocodeptr;
223}
224
225
226/*-------------------------------------------------
227    drchash_set_codeptr - set the codeptr for the
228    given mode/pc
229-------------------------------------------------*/
230
231int drchash_set_codeptr(drchash_state *drchash, UINT32 mode, UINT32 pc, drccodeptr code)
232{
233   UINT32 l1 = (pc >> drchash->l1shift) & drchash->l1mask;
234   UINT32 l2 = (pc >> drchash->l2shift) & drchash->l2mask;
235
236   assert(mode < drchash->modes);
237
238   /* copy-on-write for the l1 hash table */
239   if (drchash->base[mode] == drchash->emptyl1)
240   {
241      drccodeptr **newtable = drccache_memory_alloc_temporary(drchash->cache, sizeof(drccodeptr *) << drchash->l1bits);
242      if (newtable == NULL)
243         return FALSE;
244      memcpy(newtable, drchash->emptyl1, sizeof(drccodeptr *) << drchash->l1bits);
245      drchash->base[mode] = newtable;
246   }
247
248   /* copy-on-write for the l2 hash table */
249   if (drchash->base[mode][l1] == drchash->emptyl2)
250   {
251      drccodeptr *newtable = drccache_memory_alloc_temporary(drchash->cache, sizeof(drccodeptr) << drchash->l2bits);
252      if (newtable == NULL)
253         return FALSE;
254      memcpy(newtable, drchash->emptyl2, sizeof(drccodeptr) << drchash->l2bits);
255      drchash->base[mode][l1] = newtable;
256   }
257
258   /* set the new entry */
259   drchash->base[mode][l1][l2] = code;
260   return TRUE;
261}
262
263
264
265/***************************************************************************
266    CODE MAP MANAGEMENT
267***************************************************************************/
268
269/*-------------------------------------------------
270    drcmap_alloc - allocate memory in the cache
271    for the code mapper (it auto-frees with the
272    cache)
273-------------------------------------------------*/
274
275drcmap_state *drcmap_alloc(drccache *cache, UINT64 uniquevalue)
276{
277   drcmap_state *drcmap;
278
279   /* allocate permanent state from the cache */
280   drcmap = drccache_memory_alloc(cache, sizeof(*drcmap));
281   if (drcmap == NULL)
282      return NULL;
283   memset(drcmap, 0, sizeof(*drcmap));
284
285   /* remember the cache */
286   drcmap->cache = cache;
287   drcmap->tailptr = &drcmap->head;
288
289   return drcmap;
290}
291
292
293/*-------------------------------------------------
294    drcmap_block_begin - note the beginning of a
295    block
296-------------------------------------------------*/
297
298void drcmap_block_begin(drcmap_state *drcmap, drcuml_block *block)
299{
300   /* release any remaining live entries */
301   while (drcmap->head != NULL)
302   {
303      drcmap_entry *entry = drcmap->head;
304      drcmap->head = entry->next;
305      drccache_memory_free(drcmap->cache, entry, sizeof(*entry));
306   }
307
308   /* reset the tailptr and count */
309   drcmap->tailptr = &drcmap->head;
310   drcmap->numvalues = 0;
311}
312
313
314/*-------------------------------------------------
315    drcmap_block_end - note the end of a block
316-------------------------------------------------*/
317
318void drcmap_block_end(drcmap_state *drcmap, drcuml_block *block)
319{
320   UINT32 curvalue[DRCUML_MAPVAR_END - DRCUML_MAPVAR_M0];
321   UINT8 changed[DRCUML_MAPVAR_END - DRCUML_MAPVAR_M0];
322   drcmap_entry *entry;
323   drccodeptr lastptr;
324   drccodeptr *top;
325   UINT32 *dest;
326
327   /* only process if we have data */
328   if (drcmap->head == NULL)
329      return;
330
331   /* begin "code generation" aligned to an 8-byte boundary */
332   top = drccache_begin_codegen(drcmap->cache, sizeof(UINT64) + sizeof(UINT32) + 2 * sizeof(UINT32) * drcmap->numvalues);
333   if (top == NULL)
334      drcuml_block_abort(block);
335   dest = (UINT32 *)(((FPTR)*top + 7) & ~7);
336
337   /* store the cookie first */
338   *(UINT64 *)dest = drcmap->uniquevalue;
339   dest += 2;
340
341   /* get the pointer to the first item and store an initial backwards offset */
342   lastptr = drcmap->head->codeptr;
343   *dest = (drccodeptr)dest - lastptr;
344   dest++;
345
346   /* reset the current values */
347   memset(curvalue, 0, sizeof(curvalue));
348   memset(changed, 0, sizeof(changed));
349
350   /* now iterate over entries and store them */
351   for (entry = drcmap->head; entry != NULL; entry = entry->next)
352   {
353      /* update the current value of the variable and detect changes */
354      if (curvalue[entry->mapvar] != entry->newval)
355      {
356         curvalue[entry->mapvar] = entry->newval;
357         changed[entry->mapvar] = TRUE;
358      }
359
360      /* if the next code pointer is different, or if we're at the end, flush changes */
361      if (entry->next == NULL || entry->next->codeptr != entry->codeptr)
362      {
363         UINT32 codedelta = entry->codeptr - lastptr;
364         UINT32 varmask = 0;
365         int numchanged;
366         int varnum;
367
368         /* build a mask of changed variables */
369         for (numchanged = varnum = 0; varnum < ARRAY_LENGTH(changed); varnum++)
370            if (changed[varnum])
371            {
372               changed[varnum] = FALSE;
373               varmask |= 1 << varnum;
374               numchanged++;
375            }
376
377         /* if nothing really changed, skip it */
378         if (numchanged == 0)
379            continue;
380
381         /* first word is a code delta plus mask of changed variables */
382         while (codedelta > 0xffff)
383         {
384            *dest++ = 0xffff << 16;
385            codedelta -= 0xffff;
386         }
387         *dest++ = (codedelta << 16) | (varmask << 4) | numchanged;
388
389         /* now output updated variable values */
390         for (varnum = 0; varnum < ARRAY_LENGTH(changed); varnum++)
391            if ((varmask >> varnum) & 1)
392               *dest++ = curvalue[varnum];
393
394         /* remember our lastptr */
395         lastptr = entry->codeptr;
396      }
397   }
398
399   /* add a terminator */
400   *dest++ = 0;
401
402   /* complete codegen */
403   *top = (drccodeptr)dest;
404   drccache_end_codegen(drcmap->cache);
405}
406
407
408/*-------------------------------------------------
409    drcmap_set_value - set a map value for the
410    given code pointer
411-------------------------------------------------*/
412
413void drcmap_set_value(drcmap_state *drcmap, drccodeptr codebase, UINT32 mapvar, UINT32 newvalue)
414{
415   drcmap_entry *entry;
416
417   assert(mapvar >= DRCUML_MAPVAR_M0 && mapvar < DRCUML_MAPVAR_END);
418
419   /* if this value isn't different, skip it */
420   if (drcmap->mapvalue[mapvar - DRCUML_MAPVAR_M0] == newvalue)
421      return;
422
423   /* allocate a new entry and fill it in */
424   entry = drccache_memory_alloc(drcmap->cache, sizeof(*entry));
425   entry->next = NULL;
426   entry->codeptr = codebase;
427   entry->mapvar = mapvar - DRCUML_MAPVAR_M0;
428   entry->newval = newvalue;
429
430   /* hook us into the end of the list */
431   *drcmap->tailptr = entry;
432   drcmap->tailptr = &entry->next;
433
434   /* update our state in the table as well */
435   drcmap->mapvalue[mapvar - DRCUML_MAPVAR_M0] = newvalue;
436
437   /* and increment the count */
438   drcmap->numvalues++;
439}
440
441
442/*-------------------------------------------------
443    drcmap_get_value - return a map value for the
444    given code pointer
445-------------------------------------------------*/
446
447UINT32 drcmap_get_value(drcmap_state *drcmap, drccodeptr codebase, UINT32 mapvar)
448{
449   UINT64 *endscan = (UINT64 *)drccache_top(drcmap->cache);
450   UINT32 varmask = 0x10 << mapvar;
451   drccodeptr curcode;
452   UINT32 result = 0;
453   UINT64 *curscan;
454   UINT32 *data;
455
456   assert(mapvar >= DRCUML_MAPVAR_M0 && mapvar < DRCUML_MAPVAR_END);
457   mapvar -= DRCUML_MAPVAR_M0;
458
459   /* get an aligned pointer to start scanning */
460   curscan = (UINT64 *)(((FPTR)codebase | 7) + 1);
461
462   /* look for the signature */
463   while (curscan < endscan && *curscan++ != drcmap->uniquevalue) ;
464   assert(curscan < endscan);
465   if (curscan >= endscan)
466      return 0;
467
468   /* switch to 32-bit pointers for processing the rest */
469   data = (UINT32 *)curscan;
470
471   /* first get the 32-bit starting offset to the code */
472   curcode = (drccodeptr)data - *data;
473   data++;
474
475   /* now loop until we advance past our target */
476   while (TRUE)
477   {
478      UINT32 controlword = *data++;
479
480      /* a 0 is a terminator */
481      if (controlword == 0)
482         break;
483
484      /* update the codeptr; if this puts us past the end, we're done */
485      curcode += (controlword >> 16) & 0xffff;
486      if (curcode > codebase)
487         break;
488
489      /* if our mapvar has changed, process this word */
490      if ((controlword & varmask) != 0)
491      {
492         int dataoffs = 0;
493         UINT32 skipmask;
494
495         /* count how many words precede the one we care about */
496         for (skipmask = (controlword & (varmask - 1)) >> 4; skipmask != 0; skipmask = skipmask & (skipmask - 1))
497            dataoffs++;
498
499         /* fetch the one we want */
500         result = data[dataoffs];
501      }
502
503      /* low 4 bits contain the total number of words of data */
504      data += controlword & 0x0f;
505   }
506   return result;
507}
508
509
510/*-------------------------------------------------
511    drcmap_get_last_value - return the most
512    recently set map value
513-------------------------------------------------*/
514
515UINT32 drcmap_get_last_value(drcmap_state *drcmap, UINT32 mapvar)
516{
517   assert(mapvar >= DRCUML_MAPVAR_M0 && mapvar < DRCUML_MAPVAR_END);
518   return drcmap->mapvalue[mapvar - DRCUML_MAPVAR_M0];
519}
520
521
522
523/***************************************************************************
524    LABEL MANAGEMENT
525***************************************************************************/
526
527/*-------------------------------------------------
528    drclabel_list_alloc - allocate a label
529    list within the cache (it auto-frees with the
530    cache)
531-------------------------------------------------*/
532
533drclabel_list *drclabel_list_alloc(drccache *cache)
534{
535   drclabel_list *list;
536
537   /* allocate permanent state from the cache */
538   list = drccache_memory_alloc(cache, sizeof(*list));
539   if (list == NULL)
540      return NULL;
541   memset(list, 0, sizeof(*list));
542
543   /* remember the cache */
544   list->cache = cache;
545
546   return list;
547}
548
549
550/*-------------------------------------------------
551    drclabel_block_begin - note the beginning of
552    a block
553-------------------------------------------------*/
554
555void drclabel_block_begin(drclabel_list *list, drcuml_block *block)
556{
557   /* make sure the label list is clear, but don't fatalerror */
558   label_list_reset(list, FALSE);
559}
560
561
562/*-------------------------------------------------
563    drclabel_block_end - note the end of a block
564-------------------------------------------------*/
565
566void drclabel_block_end(drclabel_list *list, drcuml_block *block)
567{
568   /* make sure the label list is clear, and fatalerror if we missed anything */
569   label_list_reset(list, TRUE);
570}
571
572
573/*-------------------------------------------------
574    drclabel_get_codeptr - find or allocate a new
575    label; returns NULL and requests an OOB
576    callback if undefined
577-------------------------------------------------*/
578
579drccodeptr drclabel_get_codeptr(drclabel_list *list, drcuml_codelabel label, drclabel_fixup_func fixup, void *param)
580{
581   drclabel *curlabel = label_find_or_allocate(list, label);
582
583   /* if no code pointer, request an OOB callback */
584   if (curlabel->codeptr == NULL && fixup != NULL)
585      drccache_request_oob_codegen(list->cache, label_oob_callback, curlabel, fixup, param);
586
587   return curlabel->codeptr;
588}
589
590
591/*-------------------------------------------------
592    drclabel_set_codeptr - set the pointer to a new
593    label
594-------------------------------------------------*/
595
596void drclabel_set_codeptr(drclabel_list *list, drcuml_codelabel label, drccodeptr codeptr)
597{
598   /* set the code pointer */
599   drclabel *curlabel = label_find_or_allocate(list, label);
600   assert(curlabel->codeptr == NULL);
601   curlabel->codeptr = codeptr;
602}
603
604
605
606/***************************************************************************
607    LABEL MANAGEMENT
608***************************************************************************/
609
610/*-------------------------------------------------
611    label_list_reset - reset a label
612    list (add all entries to the free list)
613-------------------------------------------------*/
614
615static void label_list_reset(drclabel_list *list, int fatal_on_leftovers)
616{
617   /* loop until out of labels */
618   while (list->head != NULL)
619   {
620      /* remove from the list */
621      drclabel *label = list->head;
622      list->head = label->next;
623
624      /* fatal if we were a leftover */
625      if (fatal_on_leftovers && label->codeptr == NULL)
626         fatalerror("Label %08X never defined!", label->label);
627
628      /* free the label */
629      drccache_memory_free(list->cache, label, sizeof(*label));
630   }
631}
632
633
634/*-------------------------------------------------
635    label_find_or_allocate - look up a label and
636    allocate a new one if not found
637-------------------------------------------------*/
638
639static drclabel *label_find_or_allocate(drclabel_list *list, drcuml_codelabel label)
640{
641   drclabel *curlabel;
642
643   /* find the label, or else allocate a new one */
644   for (curlabel = list->head; curlabel != NULL; curlabel = curlabel->next)
645      if (curlabel->label == label)
646         break;
647
648   /* if none found, allocate */
649   if (curlabel == NULL)
650   {
651      curlabel = drccache_memory_alloc(list->cache, sizeof(*curlabel));
652      curlabel->next = list->head;
653      curlabel->label = label;
654      curlabel->codeptr = NULL;
655      list->head = curlabel;
656   }
657
658   return curlabel;
659}
660
661
662/*-------------------------------------------------
663    label_oob_callback - out-of-band codegen
664    callback for labels
665-------------------------------------------------*/
666
667static void label_oob_callback(drccodeptr *codeptr, void *param1, void *param2, void *param3)
668{
669   drclabel *label = param1;
670   drclabel_fixup_func callback = param2;
671
672   (*callback)(param3, label->codeptr);
673}
src/emu/cpu/drcumld.c
r0r2628
1/***************************************************************************
2
3    drcumld.c
4
5    Universal machine language disassembler.
6
7    Copyright Aaron Giles
8    Released for general non-commercial use under the MAME license
9    Visit http://mamedev.org for licensing and usage restrictions.
10
11***************************************************************************/
12
13#include "drcumld.h"
14#include "drcumlsh.h"
15#include "memory.h"
16
17
18
19/***************************************************************************
20    TYPE DEFINITIONS
21***************************************************************************/
22
23typedef char *(*output_function)(char *dest, int size, const drcuml_parameter *param);
24
25
26typedef struct _drcuml_opdesc drcuml_opdesc;
27struct _drcuml_opdesc
28{
29   UINT16         opcode;
30   const char *   opstring;
31   output_function   param[5];
32};
33
34
35
36/***************************************************************************
37    FUNCTION PROTOTYPES
38***************************************************************************/
39
40static char *output_cond(char *dest, int size, const drcuml_parameter *param);
41static char *output_flags(char *dest, int size, const drcuml_parameter *param);
42static char *output_flags_cmptest(char *dest, int size, const drcuml_parameter *param);
43static char *output_param(char *dest, int size, const drcuml_parameter *param);
44static char *output_param_string(char *dest, int size, const drcuml_parameter *param);
45static char *output_param_handle(char *dest, int size, const drcuml_parameter *param);
46static char *output_param_space(char *dest, int size, const drcuml_parameter *param);
47
48
49
50/***************************************************************************
51    TABLES
52***************************************************************************/
53
54/* these define the conditions for the UML */
55static const char *condname[] =
56{
57   "z",   "nz",
58   "s",   "ns",
59   "c",   "nc",
60   "v",   "nv",
61   "u",   "nu",
62   "a",   "be",
63   "g",   "le",
64   "l",   "ge"
65};
66
67/* the source opcode table */
68static const drcuml_opdesc opcode_source_table[] =
69{
70   /* Compile-time opcodes */
71   { DRCUML_OP_HANDLE,      "handle",   { output_param_handle } },
72   { DRCUML_OP_HASH,      "hash",      { output_param, output_param } },
73   { DRCUML_OP_LABEL,      "label",   { output_param } },
74   { DRCUML_OP_COMMENT,   "comment",   { output_param_string } },
75   { DRCUML_OP_MAPVAR,      "mapvar",   { output_param, output_param } },
76
77   /* Control Flow Operations */
78   { DRCUML_OP_DEBUG,      "debug",   { output_param } },
79   { DRCUML_OP_EXIT,      "exit",      { output_param, output_cond } },
80   { DRCUML_OP_HASHJMP,   "hashjmp",   { output_param, output_param, output_param_handle } },
81   { DRCUML_OP_JMP,      "jmp",      { output_param, output_cond } },
82   { DRCUML_OP_EXH,      "exh",      { output_param_handle, output_param, output_cond } } ,
83   { DRCUML_OP_CALLH,      "callh",   { output_param_handle, output_cond } },
84   { DRCUML_OP_RET,      "ret",      { output_cond } },
85   { DRCUML_OP_CALLC,      "callc",   { output_param, output_param, output_cond } },
86   { DRCUML_OP_RECOVER,   "recover",   { output_param, output_param } },
87
88   /* Internal Register Operations */
89   { DRCUML_OP_SETFMOD,   "setfmod",   { output_param } },
90   { DRCUML_OP_GETFMOD,   "getfmod",   { output_param } },
91   { DRCUML_OP_GETEXP,      "getexp",   { output_param } },
92
93   /* Integer Operations */
94   { DRCUML_OP_LOAD1U,      "!load1u",   { output_param, output_param, output_param } },
95   { DRCUML_OP_LOAD1S,      "!load1s",   { output_param, output_param, output_param } },
96   { DRCUML_OP_LOAD2U,      "!load2u",   { output_param, output_param, output_param } },
97   { DRCUML_OP_LOAD2S,      "!load2s",   { output_param, output_param, output_param } },
98   { DRCUML_OP_LOAD4U,      "!load4u",   { output_param, output_param, output_param } },
99   { DRCUML_OP_LOAD4S,      "!load4s",   { output_param, output_param, output_param } },
100   { DRCUML_OP_LOAD8U,      "!load8u",   { output_param, output_param, output_param } },
101   { DRCUML_OP_STORE1,      "!store1",   { output_param, output_param, output_param } },
102   { DRCUML_OP_STORE2,      "!store2",   { output_param, output_param, output_param } },
103   { DRCUML_OP_STORE4,      "!store4",   { output_param, output_param, output_param } },
104   { DRCUML_OP_STORE8,      "!store8",   { output_param, output_param, output_param } },
105   { DRCUML_OP_READ1U,      "!read1u",   { output_param, output_param_space, output_param } },
106   { DRCUML_OP_READ1S,      "!read1s",   { output_param, output_param_space, output_param } },
107   { DRCUML_OP_READ2U,      "!read2u",   { output_param, output_param_space, output_param } },
108   { DRCUML_OP_READ2S,      "!read2s",   { output_param, output_param_space, output_param } },
109   { DRCUML_OP_READ2M,      "!read2m",   { output_param, output_param_space, output_param, output_param } },
110   { DRCUML_OP_READ4U,      "!read4u",   { output_param, output_param_space, output_param } },
111   { DRCUML_OP_READ4S,      "!read4s",   { output_param, output_param_space, output_param } },
112   { DRCUML_OP_READ4M,      "!read4m",   { output_param, output_param_space, output_param, output_param } },
113   { DRCUML_OP_READ8U,      "!read8u",   { output_param, output_param_space, output_param } },
114   { DRCUML_OP_READ8M,      "!read8m",   { output_param, output_param_space, output_param, output_param } },
115   { DRCUML_OP_WRITE1,      "!write1",   { output_param_space, output_param, output_param } },
116   { DRCUML_OP_WRITE2,      "!write2",   { output_param_space, output_param, output_param } },
117   { DRCUML_OP_WRIT2M,      "!writ2m",   { output_param_space, output_param, output_param, output_param } },
118   { DRCUML_OP_WRITE4,      "!write4",   { output_param_space, output_param, output_param } },
119   { DRCUML_OP_WRIT4M,      "!writ4m",   { output_param_space, output_param, output_param, output_param } },
120   { DRCUML_OP_WRITE8,      "!write8",   { output_param_space, output_param, output_param } },
121   { DRCUML_OP_WRIT8M,      "!writ8m",   { output_param_space, output_param, output_param, output_param } },
122   { DRCUML_OP_FLAGS,      "!flags",   { output_param, output_param, output_param } },
123   { DRCUML_OP_MOV,      "!mov",      { output_param, output_param, output_cond } },
124   { DRCUML_OP_ZEXT1,      "!zext1",   { output_param, output_param } },
125   { DRCUML_OP_ZEXT2,      "!zext2",   { output_param, output_param } },
126   { DRCUML_OP_ZEXT4,      "!zext4",   { output_param, output_param } },
127   { DRCUML_OP_SEXT1,      "!sext1",   { output_param, output_param } },
128   { DRCUML_OP_SEXT2,      "!sext2",   { output_param, output_param } },
129   { DRCUML_OP_SEXT4,      "!sext4",   { output_param, output_param } },
130   { DRCUML_OP_ADD,      "!add",      { output_param, output_param, output_param, output_flags } },
131   { DRCUML_OP_ADDC,      "!addc",   { output_param, output_param, output_param, output_flags } },
132   { DRCUML_OP_SUB,      "!sub",      { output_param, output_param, output_param, output_flags } },
133   { DRCUML_OP_SUBB,      "!subb",   { output_param, output_param, output_param, output_flags } },
134   { DRCUML_OP_CMP,      "!cmp",      { output_param, output_param, output_flags_cmptest } },
135   { DRCUML_OP_MULU,      "!mulu",   { output_param, output_param, output_param, output_param, output_flags } },
136   { DRCUML_OP_MULS,      "!muls",   { output_param, output_param, output_param, output_param, output_flags } },
137   { DRCUML_OP_DIVU,      "!divu",   { output_param, output_param, output_param, output_param, output_flags } },
138   { DRCUML_OP_DIVS,      "!divs",   { output_param, output_param, output_param, output_param, output_flags } },
139   { DRCUML_OP_AND,      "!and",      { output_param, output_param, output_param, output_flags } },
140   { DRCUML_OP_TEST,      "!test",   { output_param, output_param, output_flags_cmptest } },
141   { DRCUML_OP_OR,         "!or",      { output_param, output_param, output_param, output_flags } },
142   { DRCUML_OP_XOR,      "!xor",      { output_param, output_param, output_param, output_flags } },
143   { DRCUML_OP_SHL,      "!shl",      { output_param, output_param, output_param, output_flags } },
144   { DRCUML_OP_SHR,      "!shr",      { output_param, output_param, output_param, output_flags } },
145   { DRCUML_OP_SAR,      "!sar",      { output_param, output_param, output_param, output_flags } },
146   { DRCUML_OP_ROL,      "!rol",      { output_param, output_param, output_param, output_flags } },
147   { DRCUML_OP_ROLC,      "!rolc",   { output_param, output_param, output_param, output_flags } },
148   { DRCUML_OP_ROR,      "!ror",      { output_param, output_param, output_param, output_flags } },
149   { DRCUML_OP_RORC,      "!rorc",   { output_param, output_param, output_param, output_flags } },
150
151   /* Floating-Point Operations */
152   { DRCUML_OP_FLOAD,      "f!load",   { output_param, output_param, output_param } },
153   { DRCUML_OP_FSTORE,      "f!store",   { output_param, output_param, output_param } },
154   { DRCUML_OP_FREAD,      "f!read",   { output_param, output_param_space, output_param } },
155   { DRCUML_OP_FWRITE,      "f!write",   { output_param_space, output_param, output_param } },
156   { DRCUML_OP_FMOV,      "f!mov",   { output_param, output_param, output_cond } },
157   { DRCUML_OP_FTOI4,      "f!toi4",   { output_param, output_param } },
158   { DRCUML_OP_FTOI4T,      "f!toi4t",   { output_param, output_param } },
159   { DRCUML_OP_FTOI4R,      "f!toi4r",   { output_param, output_param } },
160   { DRCUML_OP_FTOI4F,      "f!toi4f",   { output_param, output_param } },
161   { DRCUML_OP_FTOI4C,      "f!toi4c",   { output_param, output_param } },
162   { DRCUML_OP_FTOI8,      "f!toi8",   { output_param, output_param } },
163   { DRCUML_OP_FTOI8T,      "f!toi8t",   { output_param, output_param } },
164   { DRCUML_OP_FTOI8R,      "f!toi8r",   { output_param, output_param } },
165   { DRCUML_OP_FTOI8F,      "f!toi8f",   { output_param, output_param } },
166   { DRCUML_OP_FTOI8C,      "f!toi8c",   { output_param, output_param } },
167   { DRCUML_OP_FFRFS,      "f!frfs",   { output_param, output_param } },
168   { DRCUML_OP_FFRFD,      "f!frfd",   { output_param, output_param } },
169   { DRCUML_OP_FFRI4,      "f!fri4",   { output_param, output_param } },
170   { DRCUML_OP_FFRI8,      "f!fri8",   { output_param, output_param } },
171   { DRCUML_OP_FADD,      "f!add",   { output_param, output_param, output_param } },
172   { DRCUML_OP_FSUB,      "f!sub",   { output_param, output_param, output_param } },
173   { DRCUML_OP_FCMP,      "f!cmp",   { output_param, output_param, output_flags_cmptest } },
174   { DRCUML_OP_FMUL,      "f!mul",   { output_param, output_param, output_param } },
175   { DRCUML_OP_FDIV,      "f!div",   { output_param, output_param, output_param } },
176   { DRCUML_OP_FNEG,      "f!neg",   { output_param, output_param } },
177   { DRCUML_OP_FABS,      "f!abs",   { output_param, output_param } },
178   { DRCUML_OP_FSQRT,      "f!sqrt",   { output_param, output_param } },
179   { DRCUML_OP_FRECIP,      "f!recip",   { output_param, output_param } },
180   { DRCUML_OP_FRSQRT,      "f!rsqrt",   { output_param, output_param } },
181};
182
183
184
185/***************************************************************************
186    GLOBAL VARIABLES
187***************************************************************************/
188
189static const drcuml_opdesc *opcode_table[DRCUML_OP_MAX];
190
191
192
193/***************************************************************************
194    CORE DISASSEMBLER
195***************************************************************************/
196
197/*-------------------------------------------------
198    drcuml_disasm - disassemble one instruction
199-------------------------------------------------*/
200
201void drcuml_disasm(const drcuml_instruction *inst, char *buffer)
202{
203   const drcuml_opdesc *opdesc;
204   const char *stringptr;
205   char *dest = buffer;
206   int pnum, iparam = 0;
207
208   /* if this is the first time through, populate the opcode table */
209   if (opcode_table[DRCUML_OP_HASH] == NULL)
210   {
211      int tabledex;
212
213      for (tabledex = 0; tabledex < ARRAY_LENGTH(opcode_source_table); tabledex++)
214         opcode_table[opcode_source_table[tabledex].opcode] = &opcode_source_table[tabledex];
215   }
216
217   /* find the opcode in the list */
218   opdesc = opcode_table[inst->opcode];
219
220   /* NULL means invalid opcode */
221   if (opdesc == NULL)
222   {
223      strcpy(buffer, "???");
224      return;
225   }
226
227   /* start with the opcode itself */
228   stringptr = opdesc->opstring;
229
230   /* insert a prefix for integer operations */
231   if (stringptr[0] == '!')
232   {
233      stringptr++;
234      switch (inst->size)
235      {
236         case 1:   *dest++ = 'b'; break;
237         case 2:   *dest++ = 'w'; break;
238         case 4:   break;
239         case 8:   *dest++ = 'd'; break;
240         default: *dest++ = '?'; break;
241      }
242   }
243
244   /* insert a prefix for floating point operations */
245   if (stringptr[0] == 'f' && stringptr[1] == '!')
246   {
247      *dest++ = *stringptr++;
248      stringptr++;
249      switch (inst->size)
250      {
251         case 4:   *dest++ = 's'; break;
252         case 8:   *dest++ = 'd'; break;
253         default: *dest++ = '?'; break;
254      }
255   }
256
257   /* copy the rest of the string */
258   dest += sprintf(dest, "%s", stringptr);
259
260   /* iterate over parameters */
261   for (pnum = 0; pnum < ARRAY_LENGTH(opdesc->param); pnum++)
262   {
263      output_function func = opdesc->param[pnum];
264      drcuml_parameter param;
265
266      /* stop when we hit NULL */
267      if (func == NULL)
268         break;
269
270      /* if we are outputting a condition but it's "always", skip it */
271      if (func == output_cond)
272      {
273         if (inst->condflags == DRCUML_COND_ALWAYS)
274            continue;
275         param.value = inst->condflags;
276      }
277
278      /* if we are outputting flags but they are "none", skip it */
279      else if (func == output_flags)
280      {
281         if (inst->condflags == FLAGS_NONE)
282            continue;
283         param.value = inst->condflags;
284      }
285
286      /* if we are outputting test/cmp flags but they are "all", skip it */
287      else if (func == output_flags_cmptest)
288      {
289         if (inst->opcode == DRCUML_OP_CMP && inst->condflags == FLAGS_ALLI)
290            continue;
291         if (inst->opcode == DRCUML_OP_FCMP && inst->condflags == FLAGS_ALLF)
292            continue;
293         if (inst->opcode == DRCUML_OP_TEST && inst->condflags == (FLAGS_S|FLAGS_Z))
294            continue;
295         param.value = inst->condflags;
296      }
297
298      /* otherwise, we are fetching an actual parameter */
299      else
300         param = inst->param[iparam++];
301
302      /* if we are less than 8 characters, pad to 8 */
303      if (dest - buffer < 8)
304         dest += sprintf(dest, "%*s", (int)(8 - (dest - buffer)), "");
305
306      /* otherwise, add a comma */
307      else
308         dest += sprintf(dest, ",");
309
310      /* then append the parameter */
311      dest = (*func)(dest, inst->size, &param);
312   }
313}
314
315
316
317/***************************************************************************
318    OUTPUT HELPERS
319***************************************************************************/
320
321/*-------------------------------------------------
322    output_cond - output condition
323-------------------------------------------------*/
324
325static char *output_cond(char *dest, int size, const drcuml_parameter *param)
326{
327   if (param->value >= DRCUML_COND_Z && param->value < DRCUML_COND_MAX)
328      return dest + sprintf(dest, "%s", condname[param->value - DRCUML_COND_Z]);
329   else
330      return dest + sprintf(dest, "??");
331}
332
333
334/*-------------------------------------------------
335    output_flags - output flags
336-------------------------------------------------*/
337
338static char *output_flags(char *dest, int size, const drcuml_parameter *param)
339{
340   if ((param->value & (DRCUML_FLAG_C | DRCUML_FLAG_V | DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0)
341      return dest + sprintf(dest, "noflags");
342   if (param->value & DRCUML_FLAG_C)
343      *dest++ = 'C';
344   if (param->value & DRCUML_FLAG_V)
345      *dest++ = 'V';
346   if (param->value & DRCUML_FLAG_Z)
347      *dest++ = 'Z';
348   if (param->value & DRCUML_FLAG_S)
349      *dest++ = 'S';
350   *dest = 0;
351   return dest;
352}
353
354
355/*-------------------------------------------------
356    output_flags_cmptest - output flags
357-------------------------------------------------*/
358
359static char *output_flags_cmptest(char *dest, int size, const drcuml_parameter *param)
360{
361   if ((param->value & (DRCUML_FLAG_C | DRCUML_FLAG_V | DRCUML_FLAG_Z | DRCUML_FLAG_S)) == (DRCUML_FLAG_C | DRCUML_FLAG_V | DRCUML_FLAG_Z | DRCUML_FLAG_S))
362      return dest + sprintf(dest, "allflags");
363   return output_flags(dest, size, param);
364}
365
366
367/*-------------------------------------------------
368    output_param - generic parameter output
369-------------------------------------------------*/
370
371static char *output_param(char *dest, int size, const drcuml_parameter *param)
372{
373   UINT64 value;
374   switch (param->type)
375   {
376      case DRCUML_PTYPE_NONE:
377         return dest += sprintf(dest, "<none?>");
378
379      case DRCUML_PTYPE_IMMEDIATE:
380         value = param->value;
381         if (size == 1) value = (UINT8)value;
382         if (size == 2) value = (UINT16)value;
383         if (size == 4) value = (UINT32)value;
384         if ((UINT32)value == value)
385            return dest += sprintf(dest, "$%X", (UINT32)value);
386         else
387            return dest += sprintf(dest, "$%X%08X", (UINT32)(value >> 32), (UINT32)value);
388
389      case DRCUML_PTYPE_MAPVAR:
390         if (param->value >= DRCUML_MAPVAR_M0 && param->value < DRCUML_MAPVAR_END)
391            return dest += sprintf(dest, "m%d", (UINT32)(param->value - DRCUML_MAPVAR_M0));
392         else
393            return dest += sprintf(dest, "m(%X?)", (UINT32)param->value);
394
395      case DRCUML_PTYPE_INT_REGISTER:
396         if (param->value >= DRCUML_REG_I0 && param->value < DRCUML_REG_I_END)
397            return dest += sprintf(dest, "i%d", (UINT32)(param->value - DRCUML_REG_I0));
398         else
399            return dest += sprintf(dest, "i(%X?)", (UINT32)param->value);
400
401      case DRCUML_PTYPE_FLOAT_REGISTER:
402         if (param->value >= DRCUML_REG_F0 && param->value < DRCUML_REG_F_END)
403            return dest += sprintf(dest, "f%d", (UINT32)(param->value - DRCUML_REG_F0));
404         else
405            return dest += sprintf(dest, "f(%X?)", (UINT32)param->value);
406
407      case DRCUML_PTYPE_MEMORY:
408         return dest += sprintf(dest, "[$%p]", (void *)(FPTR)param->value);
409
410      default:
411         return dest += sprintf(dest, "???");
412   }
413   return dest;
414}
415
416
417/*-------------------------------------------------
418    output_param_string - output parameter 0,
419    assuming it is a string
420-------------------------------------------------*/
421
422static char *output_param_string(char *dest, int size, const drcuml_parameter *param)
423{
424   if (param->type == DRCUML_PTYPE_MEMORY)
425      return dest += sprintf(dest, "%s", (char *)(FPTR)param->value);
426   return output_param(dest, size, param);
427}
428
429
430/*-------------------------------------------------
431    output_param_handle - output parameter 0,
432    assuming it is a handle
433-------------------------------------------------*/
434
435static char *output_param_handle(char *dest, int size, const drcuml_parameter *param)
436{
437   if (param->type == DRCUML_PTYPE_MEMORY)
438      return dest += sprintf(dest, "%s", drcuml_handle_name((drcuml_codehandle *)(FPTR)param->value));
439   return output_param(dest, size, param);
440}
441
442
443/*-------------------------------------------------
444    output_param_space - output parameter 0,
445    assuming it is an address space
446-------------------------------------------------*/
447
448static char *output_param_space(char *dest, int size, const drcuml_parameter *param)
449{
450   if (param->type == DRCUML_PTYPE_IMMEDIATE && param->value >= ADDRESS_SPACE_PROGRAM && param->value <= ADDRESS_SPACE_IO)
451      return dest + sprintf(dest, "%s", address_space_names[param->value]);
452   return output_param(dest, size, param);
453}
src/emu/cpu/drcbeut.h
r0r2628
1/***************************************************************************
2
3    drcbeut.h
4
5    Utility functions for dynamic recompiling backends.
6
7    Copyright Aaron Giles
8    Released for general non-commercial use under the MAME license
9    Visit http://mamedev.org for licensing and usage restrictions.
10
11***************************************************************************/
12
13#pragma once
14
15#ifndef __DRCBEUT_H__
16#define __DRCBEUT_H__
17
18#include "drcuml.h"
19
20
21/***************************************************************************
22    TYPE DEFINITIONS
23***************************************************************************/
24
25/* callback function for forward-referenced labels */
26typedef void (*drclabel_fixup_func)(void *parameter, drccodeptr labelcodeptr);
27
28
29/* opaque structure representing a managed list of labels */
30typedef struct _drclabel_list drclabel_list;
31
32
33/* opaque structure representing a managed code map */
34typedef struct _drcmap_state drcmap_state;
35
36
37/* information about the hash tables used by the UML and backend */
38typedef struct _drchash_state drchash_state;
39struct _drchash_state
40{
41   drccache *      cache;            /* cache where allocations come from */
42   int            modes;            /* number of modes supported */
43
44   drccodeptr      nocodeptr;         /* pointer to code which will handle missing entries */
45
46   UINT8         l1bits;            /* bits worth of entries in l1 hash tables */
47   UINT8         l1shift;         /* shift to apply to the PC to get the l1 hash entry */
48   offs_t         l1mask;            /* mask to apply after shifting */
49   UINT8         l2bits;            /* bits worth of entries in l2 hash tables */
50   UINT8         l2shift;         /* shift to apply to the PC to get the l2 hash entry */
51   offs_t         l2mask;            /* mask to apply after shifting */
52
53   drccodeptr **   emptyl1;         /* pointer to empty l1 hash table */
54   drccodeptr *   emptyl2;         /* pointer to empty l2 hash table */
55
56   drccodeptr **   base[1];         /* pointer to the l1 table for each mode */
57};
58
59
60/* an integer register, with low/high parts */
61typedef union _drcuml_ireg drcuml_ireg;
62union _drcuml_ireg
63{
64#ifdef LSB_FIRST
65   UINT32         l,h;            /* 32-bit low, high parts of the register */
66#else
67   UINT32         h,l;            /* 32-bit low, high parts of the register */
68#endif
69   UINT64         d;               /* 64-bit full register */
70};
71
72
73/* an floating-point register, with low/high parts */
74typedef union _drcuml_freg drcuml_freg;
75union _drcuml_freg
76{
77#ifdef LSB_FIRST
78   float         l,unused;         /* 32-bit low, high parts of the register */
79#else
80   float         unused,l;         /* 32-bit low, high parts of the register */
81#endif
82   double         d;               /* 64-bit full register */
83};
84
85
86/* the collected machine state of a system */
87typedef struct _drcuml_machine_state drcuml_machine_state;
88struct _drcuml_machine_state
89{
90   drcuml_ireg      r[DRCUML_REG_I_END - DRCUML_REG_I0];   /* integer registers */
91   drcuml_freg      f[DRCUML_REG_F_END - DRCUML_REG_F0];   /* floating-point registers */
92   drcuml_ireg      exp;                           /* exception parameter register */
93   UINT8         fmod;                           /* fmod (floating-point mode) register */
94};
95
96
97
98/***************************************************************************
99    FUNCTION PROTOTYPES
100***************************************************************************/
101
102/* ----- hash table management ----- */
103
104/* allocate memory in the cache for the hash table tracker (it auto-frees with the cache) */
105drchash_state *drchash_alloc(drccache *cache, int modes, int addrbits, int ignorebits);
106
107/* flush existing hash tables and create new ones */
108int drchash_reset(drchash_state *drchash);
109
110/* note the beginning of a block */
111void drchash_block_begin(drchash_state *drchash, drcuml_block *block, const drcuml_instruction *instlist, UINT32 numinst);
112
113/* note the end of a block */
114void drchash_block_end(drchash_state *drchash, drcuml_block *block);
115
116/* set the default codeptr for any empty hash entries (defaults to NULL) */
117void drchash_set_default_codeptr(drchash_state *drchash, drccodeptr code);
118
119/* set the codeptr for the given mode/pc */
120int drchash_set_codeptr(drchash_state *drchash, UINT32 mode, UINT32 pc, drccodeptr code);
121
122
123
124/* ----- code map management ----- */
125
126/* allocate memory in the cache for the code mapper (it auto-frees with the cache) */
127drcmap_state *drcmap_alloc(drccache *cache, UINT64 uniquevalue);
128
129/* note the beginning of a block */
130void drcmap_block_begin(drcmap_state *drcmap, drcuml_block *block);
131
132/* note the end of a block */
133void drcmap_block_end(drcmap_state *drcmap, drcuml_block *block);
134
135/* set a map value for the given code pointer */
136void drcmap_set_value(drcmap_state *drcmap, drccodeptr codebase, UINT32 mapvar, UINT32 newvalue);
137
138/* return a map value for the given code pointer */
139UINT32 drcmap_get_value(drcmap_state *drcmap, drccodeptr codebase, UINT32 mapvar);
140
141/* return the most recently set map value */
142UINT32 drcmap_get_last_value(drcmap_state *drcmap, UINT32 mapvar);
143
144
145
146/* ----- label management ----- */
147
148/* allocate a label list within the cache (it auto-frees with the cache) */
149drclabel_list *drclabel_list_alloc(drccache *cache);
150
151/* note the beginning of a block */
152void drclabel_block_begin(drclabel_list *drcmap, drcuml_block *block);
153
154/* note the end of a block */
155void drclabel_block_end(drclabel_list *drcmap, drcuml_block *block);
156
157/* find or allocate a new label; returns NULL and requests an OOB callback if undefined */
158drccodeptr drclabel_get_codeptr(drclabel_list *list, drcuml_codelabel label, drclabel_fixup_func fixup, void *param);
159
160/* set the pointer to a new label */
161void drclabel_set_codeptr(drclabel_list *list, drcuml_codelabel label, drccodeptr codeptr);
162
163
164
165/***************************************************************************
166    INLINE FUNCTIONS
167***************************************************************************/
168
169/*-------------------------------------------------
170    drchash_get_codeptr - return the codeptr
171    allocated for the given mode/pc
172-------------------------------------------------*/
173
174INLINE drccodeptr drchash_get_codeptr(drchash_state *drchash, UINT32 mode, UINT32 pc)
175{
176   assert(mode < drchash->modes);
177   return drchash->base[mode][(pc >> drchash->l1shift) & drchash->l1mask][(pc >> drchash->l2shift) & drchash->l2mask];
178}
179
180
181/*-------------------------------------------------
182    drchash_code_exists - return TRUE if there is
183    a matching hash entry for the given mode/pc
184-------------------------------------------------*/
185
186INLINE int drchash_code_exists(drchash_state *drchash, UINT32 mode, UINT32 pc)
187{
188   return (drchash_get_codeptr(drchash, mode, pc) != drchash->nocodeptr);
189}
190
191
192#endif
src/emu/cpu/drcumld.h
r0r2628
1/***************************************************************************
2
3    drcumld.h
4
5    Universal machine language disassembler.
6
7    Copyright Aaron Giles
8    Released for general non-commercial use under the MAME license
9    Visit http://mamedev.org for licensing and usage restrictions.
10
11***************************************************************************/
12
13#pragma once
14
15#ifndef __DRCUMLD_H__
16#define __DRCUMLD_H__
17
18#include "drcuml.h"
19
20
21
22/***************************************************************************
23    FUNCTION PROTOTYPES
24***************************************************************************/
25
26/* disassemble one instruction */
27void drcuml_disasm(const drcuml_instruction *inst, char *buffer);
28
29
30#endif
src/emu/cpu/z80/z80dasm.c
r2627r2628
455455      if( op1 == 0xcb )
456456      {
457457         offset = (INT8) opram[pos++];
458         op1 = opram[pos++]; /* fourth byte from opcode_arg_base! */
458         op1 = opram[pos++]; /* fourth byte from opbase.ram! */
459459         d = &mnemonic_xx_cb[op1];
460460      }
461461      else d = &mnemonic_xx[op1];
r2627r2628
466466      if( op1 == 0xcb )
467467      {
468468         offset = (INT8) opram[pos++];
469         op1 = opram[pos++]; /* fourth byte from opcode_arg_base! */
469         op1 = opram[pos++]; /* fourth byte from opbase.ram! */
470470         d = &mnemonic_xx_cb[op1];
471471      }
472472      else d = &mnemonic_xx[op1];
src/emu/cpu/x86emit.h
r2627r2628
16081608   resolve_link(&target, &link);
16091609}
16101610
1611#ifdef PTR64
16111612
1613INLINE void emit_jrcxz_link(x86code **emitptr, emit_link *linkinfo)
1614{
1615   emit_op_simple(emitptr, OP_JrCXZ_Jb, OP_64BIT);
1616   emit_byte(emitptr, 0);
1617   linkinfo->target = *emitptr;
1618   linkinfo->size = 1;
1619}
1620
1621INLINE void emit_jrcxz(x86code **emitptr, x86code *target)
1622{
1623   emit_link link;
1624   emit_jrcxz_link(emitptr, &link);
1625   resolve_link(&target, &link);
1626}
1627
1628#endif
1629
1630
16121631/*-------------------------------------------------
16131632    emit_call/jmp_*
16141633-------------------------------------------------*/
r2627r2628
27542773
27552774
27562775/***************************************************************************
2776    MISC SSE EMITTERS
2777***************************************************************************/
2778
2779INLINE void emit_movd_r128_r32(x86code **emitptr, UINT8 dreg, UINT8 sreg)            { emit_op_modrm_reg(emitptr, OP_MOVD_Vd_Ed, OP_32BIT, dreg, sreg); }
2780INLINE void emit_movd_r128_m32(x86code **emitptr, UINT8 dreg, DECLARE_MEMPARAMS)      { emit_op_modrm_mem(emitptr, OP_MOVD_Vd_Ed, OP_32BIT, dreg, MEMPARAMS); }
2781INLINE void emit_movd_r32_r128(x86code **emitptr, UINT8 dreg, UINT8 sreg)            { emit_op_modrm_reg(emitptr, OP_MOVD_Ed_Vd, OP_32BIT, sreg, dreg); }
2782INLINE void emit_movd_m32_r128(x86code **emitptr, DECLARE_MEMPARAMS, UINT8 sreg)      { emit_op_modrm_mem(emitptr, OP_MOVD_Ed_Vd, OP_32BIT, sreg, MEMPARAMS); }
2783
2784#ifdef PTR64
2785
2786INLINE void emit_movq_r128_r64(x86code **emitptr, UINT8 dreg, UINT8 sreg)            { emit_op_modrm_reg(emitptr, OP_MOVD_Vd_Ed, OP_64BIT, dreg, sreg); }
2787INLINE void emit_movq_r128_m64(x86code **emitptr, UINT8 dreg, DECLARE_MEMPARAMS)      { emit_op_modrm_mem(emitptr, OP_MOVD_Vd_Ed, OP_64BIT, dreg, MEMPARAMS); }
2788INLINE void emit_movq_r64_r128(x86code **emitptr, UINT8 dreg, UINT8 sreg)            { emit_op_modrm_reg(emitptr, OP_MOVD_Ed_Vd, OP_64BIT, sreg, dreg); }
2789INLINE void emit_movq_m64_r128(x86code **emitptr, DECLARE_MEMPARAMS, UINT8 sreg)      { emit_op_modrm_mem(emitptr, OP_MOVD_Ed_Vd, OP_64BIT, sreg, MEMPARAMS); }
2790
2791#endif
2792
2793
2794
2795/***************************************************************************
27572796    SSE SCALAR SINGLE EMITTERS
27582797***************************************************************************/
27592798
src/emu/cpu/drcbex86.c
r0r2628
1/***************************************************************************
2
3    drcbex86.c
4
5    32-bit x86 back-end for the universal machine language.
6
7    Copyright Aaron Giles
8    Released for general non-commercial use under the MAME license
9    Visit http://mamedev.org for licensing and usage restrictions.
10
11****************************************************************************
12
13    Future improvements/changes:
14
15    * Optimize to avoid unnecessary reloads
16        - especially EDX for 64-bit operations
17        - also FCMP/FLAGS has unnecessary PUSHF/POP EAX
18
19    * Identify common pairs and optimize output
20
21****************************************************************************
22
23    ---------------
24    ABI/conventions
25    ---------------
26
27    Registers:
28        EAX        - volatile, function return value
29        EBX        - non-volatile
30        ECX        - volatile
31        EDX        - volatile, function return value (upper 32 bits)
32        ESI        - non-volatile
33        EDI        - non-volatile
34        EBP        - non-volatile
35
36        FP stack   - volatile
37
38
39    ---------------
40    Execution model
41    ---------------
42
43    Registers:
44        EAX        - scratch register
45        EBX        - maps to I0 (low 32 bits)
46        ECX        - scratch register
47        EDX        - scratch register
48        ESI        - maps to I1 (low 32 bits)
49        EDI        - maps to I2 (low 32 bits)
50        EBP        - maps to I3 (low 32 bits)
51
52        FP stack   - scratch registers
53
54    Entry point:
55        Assumes 1 parameter passed, which is the codeptr of the code
56        to execute once the environment is set up.
57
58    Exit point:
59        Assumes exit value is in EAX.
60
61    Entry stack:
62        [esp]      - return
63        [esp+4]    - input parameter (entry handle)
64
65    Runtime stack:
66        [esp]      - temporary space for hash jump
67        [esp+4]    - saved ebp
68        [esp+8]    - saved edi
69        [esp+12]   - saved esi
70        [esp+16]   - saved ebx
71        [esp+20]   - ret
72        [esp+24]   - input parameter (entry handle)
73
74***************************************************************************/
75
76#include "drcuml.h"
77#include "drcumld.h"
78#include "drcbeut.h"
79#include "debugger.h"
80#include "x86emit.h"
81#undef REG_SP
82#include "x86log.h"
83#include <math.h>
84
85
86
87/***************************************************************************
88    DEBUGGING
89***************************************************************************/
90
91#define LOG_HASHJMPS      (0)
92
93
94
95/***************************************************************************
96    CONSTANTS
97***************************************************************************/
98
99#define PTYPE_M            (1 << DRCUML_PTYPE_MEMORY)
100#define PTYPE_I            (1 << DRCUML_PTYPE_IMMEDIATE)
101#define PTYPE_R            (1 << DRCUML_PTYPE_INT_REGISTER)
102#define PTYPE_F            (1 << DRCUML_PTYPE_FLOAT_REGISTER)
103#define PTYPE_MI         (PTYPE_M | PTYPE_I)
104#define PTYPE_RI         (PTYPE_R | PTYPE_I)
105#define PTYPE_MR         (PTYPE_M | PTYPE_R)
106#define PTYPE_MRI         (PTYPE_M | PTYPE_R | PTYPE_I)
107#define PTYPE_MF         (PTYPE_M | PTYPE_F)
108
109
110
111/***************************************************************************
112    MACROS
113***************************************************************************/
114
115#define X86_CONDITION(condflags)      (condition_map[condflags - DRCUML_COND_Z])
116#define X86_NOT_CONDITION(condflags)   (condition_map[condflags - DRCUML_COND_Z] ^ 1)
117
118
119
120/***************************************************************************
121    TYPE DEFINITIONS
122***************************************************************************/
123
124/* entry point */
125typedef UINT32 (*x86_entry_point_func)(x86code *entry);
126
127/* opcode handler */
128typedef x86code *(*opcode_generate_func)(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
129
130
131/* opcode table entry */
132typedef struct _opcode_table_entry opcode_table_entry;
133struct _opcode_table_entry
134{
135   drcuml_opcode         opcode;               /* opcode in question */
136   opcode_generate_func   func;               /* function pointer to the work */
137};
138
139
140/* internal backend-specific state */
141struct _drcbe_state
142{
143   drcuml_state *         drcuml;               /* pointer back to our owner */
144   drccache *            cache;               /* pointer to the cache */
145   drcuml_machine_state    state;               /* state of the machine */
146   drchash_state *         hash;               /* hash table state */
147   drcmap_state *         map;               /* code map */
148   drclabel_list *         labels;                 /* label list */
149
150   x86_entry_point_func   entry;               /* entry point */
151   x86code *            exit;               /* exit point */
152   x86code *            nocode;               /* nocode handler */
153
154   UINT32 *            reglo[REG_MAX];         /* pointer to low part of data for each register */
155   UINT32 *            reghi[REG_MAX];         /* pointer to high part of data for each register */
156   double               fptemp;               /* temporary storage for floating point */
157
158   UINT16               fpumode;            /* saved FPU mode */
159   UINT16               fmodesave;            /* temporary location for saving */
160
161   void *               stacksave;            /* saved stack pointer */
162   void *               hashstacksave;         /* saved stack pointer for hashjmp */
163   UINT64               reslo;               /* extended low result */
164   UINT64               reshi;               /* extended high result */
165
166   x86log_context *      log;               /* logging */
167};
168
169
170
171/***************************************************************************
172    FUNCTION PROTOTYPES
173***************************************************************************/
174
175/* primary back-end callbacks */
176static drcbe_state *drcbex86_alloc(drcuml_state *drcuml, drccache *cache, UINT32 flags, int modes, int addrbits, int ignorebits);
177static void drcbex86_free(drcbe_state *drcbe);
178static void drcbex86_reset(drcbe_state *drcbe);
179static int drcbex86_execute(drcbe_state *drcbe, drcuml_codehandle *entry);
180static void drcbex86_generate(drcbe_state *drcbe, drcuml_block *block, const drcuml_instruction *instlist, UINT32 numinst);
181static int drcbex86_hash_exists(drcbe_state *drcbe, UINT32 mode, UINT32 pc);
182
183/* private helper functions */
184static void fixup_label(void *parameter, drccodeptr labelcodeptr);
185
186/* miscellaneous functions */
187static void dmulu(UINT64 *dstlo, UINT64 *dsthi, UINT64 src1, UINT64 src2);
188static void dmuls(UINT64 *dstlo, UINT64 *dsthi, INT64 src1, INT64 src2);
189static void ddivu(UINT64 *dstlo, UINT64 *dsthi, UINT64 src1, UINT64 src2);
190static void ddivs(UINT64 *dstlo, UINT64 *dsthi, INT64 src1, INT64 src2);
191
192
193
194/***************************************************************************
195    GLOBAL VARIABLES
196***************************************************************************/
197
198/* globally-accessible interface to the backend */
199const drcbe_interface drcbe_x86_be_interface =
200{
201   drcbex86_alloc,
202   drcbex86_free,
203   drcbex86_reset,
204   drcbex86_execute,
205   drcbex86_generate,
206   drcbex86_hash_exists
207};
208
209/* opcode table */
210static opcode_generate_func opcode_table[DRCUML_OP_MAX];
211
212/* size-to-mask table */
213static const UINT64 size_to_mask[] = { 0, 0xff, 0xffff, 0, 0xffffffff, 0, 0, 0, U64(0xffffffffffffffff) };
214
215/* register mapping tables */
216static UINT8 int_register_map[DRCUML_REG_I_END - DRCUML_REG_I0] =
217{
218   REG_EBX, REG_ESI, REG_EDI, REG_EBP
219};
220
221/* flags mapping tables */
222static UINT8 flags_map[0x1000];
223
224/* condition mapping table */
225static UINT8 condition_map[DRCUML_COND_MAX - DRCUML_COND_Z] =
226{
227   COND_Z,      /* DRCUML_COND_Z = 0x80,    requires Z */
228   COND_NZ,   /* DRCUML_COND_NZ,          requires Z */
229   COND_S,      /* DRCUML_COND_S,           requires S */
230   COND_NS,   /* DRCUML_COND_NS,          requires S */
231   COND_C,      /* DRCUML_COND_C,           requires C */
232   COND_NC,   /* DRCUML_COND_NC,          requires C */
233   COND_O,      /* DRCUML_COND_V,           requires V */
234   COND_NO,   /* DRCUML_COND_NV,          requires V */
235   COND_P,      /* DRCUML_COND_U,           requires U */
236   COND_NP,   /* DRCUML_COND_NU,          requires U */
237   COND_A,      /* DRCUML_COND_A,           requires CZ */
238   COND_BE,   /* DRCUML_COND_BE,          requires CZ */
239   COND_G,      /* DRCUML_COND_G,           requires SVZ */
240   COND_LE,   /* DRCUML_COND_LE,          requires SVZ */
241   COND_L,      /* DRCUML_COND_L,           requires SV */
242   COND_GE,   /* DRCUML_COND_GE,          requires SV */
243};
244
245/* FPU control register mapping */
246static const UINT16 fp_control[4] =
247{
248   0x0e3f,    /* DRCUML_FMOD_TRUNC */
249   0x023f,    /* DRCUML_FMOD_ROUND */
250   0x0a3f,    /* DRCUML_FMOD_CEIL */
251   0x063f       /* DRCUML_FMOD_FLOOR */
252};
253
254
255
256/***************************************************************************
257    TABLES
258***************************************************************************/
259
260static x86code *op_handle(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
261static x86code *op_hash(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
262static x86code *op_label(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
263static x86code *op_comment(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
264static x86code *op_mapvar(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
265
266static x86code *op_debug(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
267static x86code *op_exit(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
268static x86code *op_hashjmp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
269static x86code *op_jmp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
270static x86code *op_exh(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
271static x86code *op_callh(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
272static x86code *op_ret(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
273static x86code *op_callc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
274static x86code *op_recover(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
275
276static x86code *op_setfmod(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
277static x86code *op_getfmod(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
278static x86code *op_getexp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
279
280static x86code *op_load1u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
281static x86code *op_load1s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
282static x86code *op_load2u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
283static x86code *op_load2s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
284static x86code *op_load4u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
285static x86code *op_load4s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
286static x86code *op_load8u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
287static x86code *op_store1(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
288static x86code *op_store2(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
289static x86code *op_store4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
290static x86code *op_store8(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
291static x86code *op_read1u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
292static x86code *op_read1s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
293static x86code *op_read2u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
294static x86code *op_read2s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
295static x86code *op_read2m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
296static x86code *op_read4u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
297static x86code *op_read4s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
298static x86code *op_read4m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
299static x86code *op_read8u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
300static x86code *op_read8m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
301static x86code *op_write1(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
302static x86code *op_write2(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
303static x86code *op_writ2m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
304static x86code *op_write4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
305static x86code *op_writ4m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
306static x86code *op_write8(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
307static x86code *op_writ8m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
308static x86code *op_flags(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
309static x86code *op_mov(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
310static x86code *op_zext1(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
311static x86code *op_zext2(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
312static x86code *op_zext4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
313static x86code *op_sext1(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
314static x86code *op_sext2(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
315static x86code *op_sext4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
316static x86code *op_add(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
317static x86code *op_addc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
318static x86code *op_sub(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
319static x86code *op_subc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
320static x86code *op_cmp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
321static x86code *op_mulu(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
322static x86code *op_muls(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
323static x86code *op_divu(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
324static x86code *op_divs(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
325static x86code *op_and(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
326static x86code *op_test(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
327static x86code *op_or(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
328static x86code *op_xor(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
329static x86code *op_shl(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
330static x86code *op_shr(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
331static x86code *op_sar(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
332static x86code *op_ror(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
333static x86code *op_rol(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
334static x86code *op_rorc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
335static x86code *op_rolc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
336
337static x86code *op_fload(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
338static x86code *op_fstore(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
339static x86code *op_fread(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
340static x86code *op_fwrite(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
341static x86code *op_fmov(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
342static x86code *op_ftoi4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
343static x86code *op_ftoi4t(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
344static x86code *op_ftoi4r(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
345static x86code *op_ftoi4f(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
346static x86code *op_ftoi4c(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
347static x86code *op_ftoi8(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
348static x86code *op_ftoi8t(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
349static x86code *op_ftoi8r(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
350static x86code *op_ftoi8f(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
351static x86code *op_ftoi8c(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
352static x86code *op_ffrfs(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
353static x86code *op_ffrfd(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
354static x86code *op_ffri4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
355static x86code *op_ffri8(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
356static x86code *op_fadd(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
357static x86code *op_fsub(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
358static x86code *op_fcmp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
359static x86code *op_fmul(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
360static x86code *op_fdiv(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
361static x86code *op_fneg(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
362static x86code *op_fabs(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
363static x86code *op_fsqrt(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
364static x86code *op_frecip(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
365static x86code *op_frsqrt(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst);
366
367static const opcode_table_entry opcode_table_source[] =
368{
369   /* Compile-time opcodes */
370   { DRCUML_OP_HANDLE,    op_handle },   /* HANDLE  handle                 */
371   { DRCUML_OP_HASH,    op_hash },      /* HASH    mode,pc                */
372   { DRCUML_OP_LABEL,   op_label },   /* LABEL   imm                    */
373   { DRCUML_OP_COMMENT, op_comment },   /* COMMENT string                 */
374   { DRCUML_OP_MAPVAR,  op_mapvar },   /* MAPVAR  mapvar,value           */
375
376   /* Control Flow Operations */
377   { DRCUML_OP_DEBUG,   op_debug },   /* DEBUG   pc                     */
378   { DRCUML_OP_EXIT,    op_exit },      /* EXIT    src1[,c]               */
379   { DRCUML_OP_HASHJMP, op_hashjmp },   /* HASHJMP mode,pc,handle         */
380   { DRCUML_OP_JMP,     op_jmp },      /* JMP     imm[,c]                */
381   { DRCUML_OP_EXH,     op_exh },      /* EXH     handle,param[,c]       */
382   { DRCUML_OP_CALLH,   op_callh },   /* CALLH   handle[,c]             */
383   { DRCUML_OP_RET,     op_ret },      /* RET     [c]                    */
384   { DRCUML_OP_CALLC,   op_callc },   /* CALLC   func,ptr[,c]           */
385   { DRCUML_OP_RECOVER, op_recover },   /* RECOVER dst,mapvar             */
386
387   /* Internal Register Operations */
388   { DRCUML_OP_SETFMOD, op_setfmod },   /* SETFMOD src                    */
389   { DRCUML_OP_GETFMOD, op_getfmod },   /* GETFMOD dst                    */
390   { DRCUML_OP_GETEXP,  op_getexp },   /* GETEXP  dst,index              */
391
392   /* Integer Operations */
393   { DRCUML_OP_LOAD1U,  op_load1u },   /* LOAD1U  dst,base,index         */
394   { DRCUML_OP_LOAD1S,  op_load1s },   /* LOAD1S  dst,base,index         */
395   { DRCUML_OP_LOAD2U,  op_load2u },   /* LOAD2U  dst,base,index         */
396   { DRCUML_OP_LOAD2S,  op_load2s },   /* LOAD2S  dst,base,index         */
397   { DRCUML_OP_LOAD4U,  op_load4u },   /* LOAD4U  dst,base,index         */
398   { DRCUML_OP_LOAD4S,  op_load4s },   /* LOAD4S  dst,base,index         */
399   { DRCUML_OP_LOAD8U,  op_load8u },   /* LOAD8U  dst,base,index         */
400   { DRCUML_OP_STORE1,  op_store1 },   /* STORE1  base,index,src         */
401   { DRCUML_OP_STORE2,  op_store2 },   /* STORE2  base,index,src         */
402   { DRCUML_OP_STORE4,  op_store4 },   /* STORE4  base,index,src         */
403   { DRCUML_OP_STORE8,  op_store8 },   /* STORE8  base,index,src         */
404   { DRCUML_OP_READ1U,  op_read1u },   /* READ1U  dst,space,src1         */
405   { DRCUML_OP_READ1S,  op_read1s },   /* READ1S  dst,space,src1         */
406   { DRCUML_OP_READ2U,  op_read2u },   /* READ2U  dst,space,src1         */
407   { DRCUML_OP_READ2S,  op_read2s },   /* READ2S  dst,space,src1         */
408   { DRCUML_OP_READ2M,  op_read2m },   /* READ2M  dst,space,src1,mask    */
409   { DRCUML_OP_READ4U,  op_read4u },   /* READ4U  dst,space,src1         */
410   { DRCUML_OP_READ4S,  op_read4s },   /* READ4S  dst,space,src1         */
411   { DRCUML_OP_READ4M,  op_read4m },   /* READ4M  dst,space,src1,mask    */
412   { DRCUML_OP_READ8U,  op_read8u },   /* READ8U  dst,space,src1         */
413   { DRCUML_OP_READ8M,  op_read8m },   /* READ8M  dst,space,src1,mask    */
414   { DRCUML_OP_WRITE1,  op_write1 },   /* WRITE1  space,dst,src1         */
415   { DRCUML_OP_WRITE2,  op_write2 },   /* WRITE2  space,dst,src1         */
416   { DRCUML_OP_WRIT2M,  op_writ2m },   /* WRIT2M  space,dst,src1         */
417   { DRCUML_OP_WRITE4,  op_write4 },   /* WRITE4  space,dst,src1         */
418   { DRCUML_OP_WRIT4M,  op_writ4m },   /* WRIT4M  space,dst,mask,src1    */
419   { DRCUML_OP_WRITE8,  op_write8 },   /* WRITE8  space,dst,src1         */
420   { DRCUML_OP_WRIT8M,  op_writ8m },   /* WRIT8M  space,dst,mask,src1    */
421   { DRCUML_OP_FLAGS,   op_flags },   /* FLAGS   dst,mask,table         */
422   { DRCUML_OP_MOV,     op_mov },      /* MOV     dst,src[,c]            */
423   { DRCUML_OP_ZEXT1,   op_zext1 },   /* ZEXT1   dst,src                */
424   { DRCUML_OP_ZEXT2,   op_zext2 },   /* ZEXT2   dst,src                */
425   { DRCUML_OP_ZEXT4,   op_zext4 },   /* ZEXT4   dst,src                */
426   { DRCUML_OP_SEXT1,   op_sext1 },   /* SEXT1   dst,src                */
427   { DRCUML_OP_SEXT2,   op_sext2 },   /* SEXT2   dst,src                */
428   { DRCUML_OP_SEXT4,   op_sext4 },   /* SEXT4   dst,src                */
429   { DRCUML_OP_ADD,     op_add },      /* ADD     dst,src1,src2[,f]      */
430   { DRCUML_OP_ADDC,    op_addc },      /* ADDC    dst,src1,src2[,f]      */
431   { DRCUML_OP_SUB,     op_sub },      /* SUB     dst,src1,src2[,f]      */
432   { DRCUML_OP_SUBB,    op_subc },      /* SUBB    dst,src1,src2[,f]      */
433   { DRCUML_OP_CMP,     op_cmp },      /* CMP     src1,src2[,f]          */
434   { DRCUML_OP_MULU,    op_mulu },      /* MULU    dst,edst,src1,src2[,f] */
435   { DRCUML_OP_MULS,    op_muls },      /* MULS    dst,edst,src1,src2[,f] */
436   { DRCUML_OP_DIVU,    op_divu },      /* DIVU    dst,edst,src1,src2[,f] */
437   { DRCUML_OP_DIVS,    op_divs },      /* DIVS    dst,edst,src1,src2[,f] */
438   { DRCUML_OP_AND,     op_and },      /* AND     dst,src1,src2[,f]      */
439   { DRCUML_OP_TEST,    op_test },      /* TEST    src1,src2[,f]          */
440   { DRCUML_OP_OR,      op_or },      /* OR      dst,src1,src2[,f]      */
441   { DRCUML_OP_XOR,     op_xor },      /* XOR     dst,src1,src2[,f]      */
442   { DRCUML_OP_SHL,     op_shl },      /* SHL     dst,src,count[,f]      */
443   { DRCUML_OP_SHR,     op_shr },      /* SHR     dst,src,count[,f]      */
444   { DRCUML_OP_SAR,     op_sar },      /* SAR     dst,src,count[,f]      */
445   { DRCUML_OP_ROL,     op_rol },      /* ROL     dst,src,count[,f]      */
446   { DRCUML_OP_ROLC,    op_rolc },      /* ROLC    dst,src,count[,f]      */
447   { DRCUML_OP_ROR,     op_ror },      /* ROR     dst,src,count[,f]      */
448   { DRCUML_OP_RORC,    op_rorc },      /* RORC    dst,src,count[,f]      */
449
450   /* Floating Point Operations */
451   { DRCUML_OP_FLOAD,   op_fload },   /* FLOAD   dst,base,index         */
452   { DRCUML_OP_FSTORE,  op_fstore },   /* FSTORE  base,index,src         */
453   { DRCUML_OP_FREAD,   op_fread },   /* FREAD   dst,space,src1         */
454   { DRCUML_OP_FWRITE,  op_fwrite },   /* FWRITE  space,dst,src1         */
455   { DRCUML_OP_FMOV,    op_fmov },      /* FMOV    dst,src1[,c]           */
456   { DRCUML_OP_FTOI4,   op_ftoi4 },   /* FTOI4   dst,src1               */
457   { DRCUML_OP_FTOI4T,  op_ftoi4t },   /* FTOI4T  dst,src1               */
458   { DRCUML_OP_FTOI4R,  op_ftoi4r },   /* FTOI4R  dst,src1               */
459   { DRCUML_OP_FTOI4F,  op_ftoi4f },   /* FTOI4F  dst,src1               */
460   { DRCUML_OP_FTOI4C,  op_ftoi4c },   /* FTOI4C  dst,src1               */
461   { DRCUML_OP_FTOI8,   op_ftoi8 },   /* FTOI8   dst,src1               */
462   { DRCUML_OP_FTOI8T,  op_ftoi8t },   /* FTOI8T  dst,src1               */
463   { DRCUML_OP_FTOI8R,  op_ftoi8r },   /* FTOI8R  dst,src1               */
464   { DRCUML_OP_FTOI8F,  op_ftoi8f },   /* FTOI8F  dst,src1               */
465   { DRCUML_OP_FTOI8C,  op_ftoi8c },   /* FTOI8C  dst,src1               */
466   { DRCUML_OP_FFRFS,   op_ffrfs },   /* FFRFS   dst,src1               */
467   { DRCUML_OP_FFRFD,   op_ffrfd },   /* FFRFD   dst,src1               */
468   { DRCUML_OP_FFRI4,   op_ffri4 },   /* FFRI4   dst,src1               */
469   { DRCUML_OP_FFRI8,   op_ffri8 },   /* FFRI8   dst,src1               */
470   { DRCUML_OP_FADD,    op_fadd },      /* FADD    dst,src1,src2          */
471   { DRCUML_OP_FSUB,    op_fsub },      /* FSUB    dst,src1,src2          */
472   { DRCUML_OP_FCMP,    op_fcmp },      /* FCMP    src1,src2              */
473   { DRCUML_OP_FMUL,    op_fmul },      /* FMUL    dst,src1,src2          */
474   { DRCUML_OP_FDIV,    op_fdiv },      /* FDIV    dst,src1,src2          */
475   { DRCUML_OP_FNEG,    op_fneg },      /* FNEG    dst,src1               */
476   { DRCUML_OP_FABS,    op_fabs },      /* FABS    dst,src1               */
477   { DRCUML_OP_FSQRT,   op_fsqrt },   /* FSQRT   dst,src1               */
478   { DRCUML_OP_FRECIP,  op_frecip },   /* FRECIP  dst,src1               */
479   { DRCUML_OP_FRSQRT,  op_frsqrt }   /* FRSQRT  dst,src1               */
480};
481
482
483
484/***************************************************************************
485    INLINE FUNCTIONS
486***************************************************************************/
487
488/*-------------------------------------------------
489    param_select_register - select a register
490    to use, avoiding conflicts with the optional
491    checkparam
492-------------------------------------------------*/
493
494INLINE int param_select_register(int defreg, const drcuml_parameter *param, const drcuml_parameter *checkparam)
495{
496   if (param->type == DRCUML_PTYPE_INT_REGISTER && (checkparam == NULL || param->type != checkparam->type || param->value != checkparam->value))
497      return param->value;
498   return defreg;
499}
500
501
502/*-------------------------------------------------
503    emit_combine_z_flags - combine the Z flag from
504    two 32-bit operations
505-------------------------------------------------*/
506
507INLINE void emit_combine_z_flags(x86code **dst)
508{
509   /* this assumes that the flags from the low 32-bit op are on the stack */
510   /* and the flags from the high 32-bit op are live */
511   emit_pushf(dst);                                                   // pushf
512   emit_mov_r32_m32(dst, REG_ECX, MBD(REG_ESP, 4));                           // mov   ecx,[esp+4]
513   emit_or_r32_imm(dst, REG_ECX, ~0x40);                                    // or    ecx,~0x40
514   emit_and_m32_r32(dst, MBD(REG_ESP, 0), REG_ECX);                           // and   [esp],ecx
515   emit_popf(dst);                                                      // popf
516   emit_lea_r32_m32(dst, REG_ESP, MBD(REG_ESP, 4));                           // lea   esp,[esp+4]
517}
518
519
520/*-------------------------------------------------
521    emit_combine_z_shl_flags - combine the Z
522    flags from two 32-bit shift left operations
523-------------------------------------------------*/
524
525INLINE void emit_combine_z_shl_flags(x86code **dst)
526{
527   /* this assumes that the flags from the high 32-bit op are on the stack */
528   /* and the flags from the low 32-bit op are live */
529   emit_pushf(dst);                                                   // pushf
530   emit_pop_r32(dst, REG_ECX);                                             // pop   ecx
531   emit_or_r32_imm(dst, REG_ECX, ~0x40);                                    // or    ecx,~0x40
532   emit_and_m32_r32(dst, MBD(REG_ESP, 0), REG_ECX);                           // and   [esp],ecx
533   emit_popf(dst);                                                      // popf
534}
535
536
537
538/***************************************************************************
539    BACKEND CALLBACKS
540***************************************************************************/
541
542/*-------------------------------------------------
543    drcbex86_alloc - allocate back-end-specific
544    state
545-------------------------------------------------*/
546
547static drcbe_state *drcbex86_alloc(drcuml_state *drcuml, drccache *cache, UINT32 flags, int modes, int addrbits, int ignorebits)
548{
549   int opnum, regnum, entry;
550   drcbe_state *drcbe;
551
552   /* allocate space in the cache for our state */
553   drcbe = drccache_memory_alloc(cache, sizeof(*drcbe));
554   if (drcbe == NULL)
555      return NULL;
556   memset(drcbe, 0, sizeof(*drcbe));
557
558   /* remember our pointers */
559   drcbe->drcuml = drcuml;
560   drcbe->cache = cache;
561
562   /* allocate hash tables */
563   drcbe->hash = drchash_alloc(cache, modes, addrbits, ignorebits);
564   if (drcbe->hash == NULL)
565      return NULL;
566
567   /* allocate code map */
568   drcbe->map = drcmap_alloc(cache, 0);
569   if (drcbe->map == NULL)
570      return NULL;
571
572   /* allocate a label tracker */
573   drcbe->labels = drclabel_list_alloc(cache);
574   if (drcbe->labels == NULL)
575      return NULL;
576
577   /* build the opcode table (static but it doesn't hurt to regenerate it) */
578   for (opnum = 0; opnum < ARRAY_LENGTH(opcode_table_source); opnum++)
579      opcode_table[opcode_table_source[opnum].opcode] = opcode_table_source[opnum].func;
580
581   /* build the flags map (static but it doesn't hurt to regenerate it) */
582   for (entry = 0; entry < ARRAY_LENGTH(flags_map); entry++)
583   {
584      UINT8 flags = 0;
585      if (entry & 0x001) flags |= DRCUML_FLAG_C;
586      if (entry & 0x004) flags |= DRCUML_FLAG_U;
587      if (entry & 0x040) flags |= DRCUML_FLAG_Z;
588      if (entry & 0x080) flags |= DRCUML_FLAG_S;
589      if (entry & 0x800) flags |= DRCUML_FLAG_V;
590      flags_map[entry] = flags;
591   }
592
593   /* compute hi pointers for each register */
594   for (regnum = 0; regnum < ARRAY_LENGTH(int_register_map); regnum++)
595      if (int_register_map[regnum] != 0)
596      {
597         drcbe->reglo[int_register_map[regnum]] = &drcbe->state.r[regnum].l;
598         drcbe->reghi[int_register_map[regnum]] = &drcbe->state.r[regnum].h;
599      }
600
601   /* create the log */
602   if (flags & DRCUML_OPTION_LOG_NATIVE)
603      drcbe->log = x86log_create_context("drcbex86.asm");
604
605   return drcbe;
606}
607
608
609/*-------------------------------------------------
610    drcbex86_free - free back-end specific state
611-------------------------------------------------*/
612
613static void drcbex86_free(drcbe_state *drcbe)
614{
615   /* free the log context */
616   if (drcbe->log != NULL)
617      x86log_free_context(drcbe->log);
618}
619
620
621/*-------------------------------------------------
622    drcbex86_reset - reset back-end specific state
623-------------------------------------------------*/
624
625static void drcbex86_reset(drcbe_state *drcbe)
626{
627   x86code **dst;
628
629   /* output a note to the log */
630   if (drcbe->log != NULL)
631      x86log_printf(drcbe->log, "\n\n===========\nCACHE RESET\n===========\n\n");
632
633   /* generate a little bit of glue code to set up the environment */
634   dst = (x86code **)drccache_begin_codegen(drcbe->cache, 500);
635   if (dst == NULL)
636      fatalerror("Out of cache space after a reset!");
637
638   /* generate an entry point */
639   drcbe->entry = (x86_entry_point_func)*dst;
640   emit_mov_r32_m32(dst, REG_EAX, MBD(REG_ESP, 4));                           // mov   eax,[esp+4]
641   emit_push_r32(dst, REG_EBX);                                          // push  ebx
642   emit_push_r32(dst, REG_ESI);                                          // push  esi
643   emit_push_r32(dst, REG_EDI);                                          // push  edi
644   emit_push_r32(dst, REG_EBP);                                          // push  ebp
645   emit_mov_m32_r32(dst, MABS(&drcbe->hashstacksave), REG_ESP);                  // mov   [hashstacksave],esp
646   emit_sub_r32_imm(dst, REG_ESP, 4);                                       // sub   esp,4
647   emit_mov_m32_r32(dst, MABS(&drcbe->stacksave), REG_ESP);                     // mov   [stacksave],esp
648   emit_fstcw_m16(dst, MABS(&drcbe->fpumode));                                 // fstcw [fpumode]
649   emit_jmp_r32(dst, REG_EAX);                                             // jmp   eax
650   if (drcbe->log != NULL)
651      x86log_disasm_code_range(drcbe->log, "entry_point", (x86code *)drcbe->entry, *dst);
652
653   /* generate an exit point */
654   drcbe->exit = *dst;
655   emit_fldcw_m16(dst, MABS(&drcbe->fpumode));                                 // fldcw [fpumode]
656   emit_mov_r32_m32(dst, REG_ESP, MABS(&drcbe->hashstacksave));                  // mov   esp,[hashstacksave]
657   emit_pop_r32(dst, REG_EBP);                                             // pop   ebp
658   emit_pop_r32(dst, REG_EDI);                                             // pop   edi
659   emit_pop_r32(dst, REG_ESI);                                             // pop   esi
660   emit_pop_r32(dst, REG_EBX);                                             // pop   ebx
661   emit_ret(dst);                                                      // ret
662   if (drcbe->log != NULL)
663      x86log_disasm_code_range(drcbe->log, "exit_point", drcbe->exit, *dst);
664
665   /* generate a no code point */
666   drcbe->nocode = *dst;
667   emit_ret(dst);                                                      // ret
668   if (drcbe->log != NULL)
669      x86log_disasm_code_range(drcbe->log, "nocode", drcbe->nocode, *dst);
670
671   /* finish up codegen */
672   drccache_end_codegen(drcbe->cache);
673
674   /* reset our hash tables */
675   drchash_reset(drcbe->hash);
676   drchash_set_default_codeptr(drcbe->hash, drcbe->nocode);
677}
678
679
680/*-------------------------------------------------
681    drcbex86_generate - generate code
682-------------------------------------------------*/
683
684static void drcbex86_generate(drcbe_state *drcbe, drcuml_block *block, const drcuml_instruction *instlist, UINT32 numinst)
685{
686   const char *blockname = NULL;
687   char blockbuffer[100];
688   drccodeptr *cachetop;
689   x86code *base;
690   x86code *dst;
691   int inum;
692
693   /* tell all of our utility objects that a block is beginning */
694   drchash_block_begin(drcbe->hash, block, instlist, numinst);
695   drclabel_block_begin(drcbe->labels, block);
696   drcmap_block_begin(drcbe->map, block);
697
698   /* begin codegen; fail if we can't */
699   cachetop = drccache_begin_codegen(drcbe->cache, numinst * 8 * 4);
700   if (cachetop == NULL)
701      drcuml_block_abort(block);
702
703   /* compute the base by aligning the cache top to a cache line (assumed to be 64 bytes) */
704   base = (x86code *)(((FPTR)*cachetop + 63) & ~63);
705   dst = base;
706
707   /* generate code */
708   for (inum = 0; inum < numinst; inum++)
709   {
710      const drcuml_instruction *inst = &instlist[inum];
711      assert(inst->opcode < ARRAY_LENGTH(opcode_table));
712
713      /* add a comment */
714      if (drcbe->log != NULL)
715      {
716         char dasm[100];
717         drcuml_disasm(inst, dasm);
718         x86log_add_comment(drcbe->log, dst, "%s", dasm);
719      }
720
721      /* extract a blockname */
722      if (blockname == NULL)
723      {
724         if (inst->opcode == DRCUML_OP_HANDLE)
725            blockname = drcuml_handle_name((drcuml_codehandle *)(FPTR)inst->param[0].value);
726         else if (inst->opcode == DRCUML_OP_HASH)
727         {
728            sprintf(blockbuffer, "Code: mode=%d PC=%08X", (UINT32)inst->param[0].value, (offs_t)inst->param[1].value);
729            blockname = blockbuffer;
730         }
731      }
732
733      /* generate code */
734      dst = (*opcode_table[inst->opcode])(drcbe, dst, inst);
735   }
736
737   /* complete codegen */
738   *cachetop = (drccodeptr)dst;
739   drccache_end_codegen(drcbe->cache);
740
741   /* log it */
742   if (drcbe->log != NULL)
743      x86log_disasm_code_range(drcbe->log, (blockname == NULL) ? "Unknown block" : blockname, base, drccache_top(drcbe->cache));
744
745   /* tell all of our utility objects that the block is finished */
746   drchash_block_end(drcbe->hash, block);
747   drclabel_block_end(drcbe->labels, block);
748   drcmap_block_end(drcbe->map, block);
749}
750
751
752/*-------------------------------------------------
753    drcbex86_hash_exists - return true if the
754    given mode/pc exists in the hash table
755-------------------------------------------------*/
756
757static int drcbex86_hash_exists(drcbe_state *drcbe, UINT32 mode, UINT32 pc)
758{
759   return drchash_code_exists(drcbe->hash, mode, pc);
760}
761
762
763/*-------------------------------------------------
764    drcbec_execute - execute a block of code
765    registered at the given mode/pc
766-------------------------------------------------*/
767
768static int drcbex86_execute(drcbe_state *drcbe, drcuml_codehandle *entry)
769{
770   /* call our entry point which will jump to the destination */
771   return (*drcbe->entry)((x86code *)drcuml_handle_codeptr(entry));
772}
773
774
775
776/***************************************************************************
777    COMPILE HELPERS
778***************************************************************************/
779
780/*-------------------------------------------------
781    param_normalize - convert a full parameter
782    into a reduced set
783-------------------------------------------------*/
784
785static void param_normalize(drcbe_state *drcbe, const drcuml_parameter *src, drcuml_parameter *dest, UINT32 allowed)
786{
787   int regnum;
788
789   switch (src->type)
790   {
791      /* immediates pass through */
792      case DRCUML_PTYPE_IMMEDIATE:
793         assert(allowed & PTYPE_I);
794         dest->type = DRCUML_PTYPE_IMMEDIATE;
795         dest->value = src->value;
796         break;
797
798      /* mapvars are converted to immediates with their current value */
799      case DRCUML_PTYPE_MAPVAR:
800         assert(allowed & PTYPE_I);
801         dest->type = DRCUML_PTYPE_IMMEDIATE;
802         dest->value = drcmap_get_last_value(drcbe->map, src->value);
803         break;
804
805      /* memory passes through */
806      case DRCUML_PTYPE_MEMORY:
807         assert(allowed & PTYPE_M);
808         dest->type = DRCUML_PTYPE_MEMORY;
809         dest->value = src->value;
810         break;
811
812      /* if a register maps to a register, keep it as a register; otherwise map it to memory */
813      case DRCUML_PTYPE_INT_REGISTER:
814         assert(allowed & PTYPE_R);
815         assert(allowed & PTYPE_M);
816         regnum = int_register_map[src->value - DRCUML_REG_I0];
817         if (regnum != 0)
818         {
819            dest->type = DRCUML_PTYPE_INT_REGISTER;
820            dest->value = regnum;
821         }
822         else
823         {
824            dest->type = DRCUML_PTYPE_MEMORY;
825            dest->value = (FPTR)&drcbe->state.r[src->value - DRCUML_REG_I0];
826         }
827         break;
828
829      /* all floating point registers map to memory */
830      case DRCUML_PTYPE_FLOAT_REGISTER:
831         assert(allowed & PTYPE_F);
832         assert(allowed & PTYPE_M);
833         dest->type = DRCUML_PTYPE_MEMORY;
834         dest->value = (FPTR)&drcbe->state.f[src->value - DRCUML_REG_F0];
835         break;
836
837      /* everything else is unexpected */
838      default:
839         fatalerror("Unexpected parameter type");
840         break;
841   }
842}
843
844
845/*-------------------------------------------------
846    param_normalize_1 - normalize a single
847    parameter instruction
848-------------------------------------------------*/
849
850static void param_normalize_1(drcbe_state *drcbe, const drcuml_instruction *inst, drcuml_parameter *dest0, UINT32 allowed0)
851{
852   assert(inst->numparams == 1);
853   param_normalize(drcbe, &inst->param[0], dest0, allowed0);
854}
855
856
857/*-------------------------------------------------
858    param_normalize_2 - normalize a 2
859    parameter instruction
860-------------------------------------------------*/
861
862static void param_normalize_2(drcbe_state *drcbe, const drcuml_instruction *inst, drcuml_parameter *dest0, UINT32 allowed0, drcuml_parameter *dest1, UINT32 allowed1)
863{
864   assert(inst->numparams == 2);
865   param_normalize(drcbe, &inst->param[0], dest0, allowed0);
866   param_normalize(drcbe, &inst->param[1], dest1, allowed1);
867}
868
869
870/*-------------------------------------------------
871    param_normalize_2_commutative - normalize a 2
872    parameter instruction, shuffling the
873    parameters on the assumption that the two
874    parameters can be swapped
875-------------------------------------------------*/
876
877static void param_normalize_2_commutative(drcbe_state *drcbe, const drcuml_instruction *inst, drcuml_parameter *dest0, UINT32 allowed0, drcuml_parameter *dest1, UINT32 allowed1)
878{
879   param_normalize_2(drcbe, inst, dest0, allowed0, dest1, allowed1);
880
881   /* if the inner parameter is a memory operand, push it to the outer */
882   if (dest0->type == DRCUML_PTYPE_MEMORY)
883   {
884      drcuml_parameter temp = *dest0;
885      *dest0 = *dest1;
886      *dest1 = temp;
887   }
888
889   /* if the inner parameter is an immediate, push it to the outer */
890   if (dest0->type == DRCUML_PTYPE_IMMEDIATE)
891   {
892      drcuml_parameter temp = *dest0;
893      *dest0 = *dest1;
894      *dest1 = temp;
895   }
896}
897
898
899/*-------------------------------------------------
900    param_normalize_3 - normalize a 3
901    parameter instruction
902-------------------------------------------------*/
903
904static void param_normalize_3(drcbe_state *drcbe, const drcuml_instruction *inst, drcuml_parameter *dest0, UINT32 allowed0, drcuml_parameter *dest1, UINT32 allowed1, drcuml_parameter *dest2, UINT32 allowed2)
905{
906   assert(inst->numparams == 3);
907   param_normalize(drcbe, &inst->param[0], dest0, allowed0);
908   param_normalize(drcbe, &inst->param[1], dest1, allowed1);
909   param_normalize(drcbe, &inst->param[2], dest2, allowed2);
910}
911
912
913/*-------------------------------------------------
914    param_normalize_3_commutative - normalize a 3
915    parameter instruction, shuffling the
916    parameters on the assumption that the last
917    2 can be swapped
918-------------------------------------------------*/
919
920static void param_normalize_3_commutative(drcbe_state *drcbe, const drcuml_instruction *inst, drcuml_parameter *dest0, UINT32 allowed0, drcuml_parameter *dest1, UINT32 allowed1, drcuml_parameter *dest2, UINT32 allowed2)
921{
922   param_normalize_3(drcbe, inst, dest0, allowed0, dest1, allowed1, dest2, allowed2);
923
924   /* if the inner parameter is a memory operand, push it to the outer */
925   if (dest1->type == DRCUML_PTYPE_MEMORY)
926   {
927      drcuml_parameter temp = *dest1;
928      *dest1 = *dest2;
929      *dest2 = temp;
930   }
931
932   /* if the inner parameter is an immediate, push it to the outer */
933   if (dest1->type == DRCUML_PTYPE_IMMEDIATE)
934   {
935      drcuml_parameter temp = *dest1;
936      *dest1 = *dest2;
937      *dest2 = temp;
938   }
939
940   /* if the destination and outer parameters are equal, move the outer to the inner */
941   if (dest0->type == dest2->type && dest0->value == dest2->value && dest0->type != DRCUML_PTYPE_IMMEDIATE)
942   {
943      drcuml_parameter temp = *dest1;
944      *dest1 = *dest2;
945      *dest2 = temp;
946   }
947}
948
949
950/*-------------------------------------------------
951    param_normalize_4 - normalize a 4
952    parameter instruction
953-------------------------------------------------*/
954
955static void param_normalize_4(drcbe_state *drcbe, const drcuml_instruction *inst, drcuml_parameter *dest0, UINT32 allowed0, drcuml_parameter *dest1, UINT32 allowed1, drcuml_parameter *dest2, UINT32 allowed2, drcuml_parameter *dest3, UINT32 allowed3)
956{
957   assert(inst->numparams == 4);
958   param_normalize(drcbe, &inst->param[0], dest0, allowed0);
959   param_normalize(drcbe, &inst->param[1], dest1, allowed1);
960   param_normalize(drcbe, &inst->param[2], dest2, allowed2);
961   param_normalize(drcbe, &inst->param[3], dest3, allowed3);
962}
963
964
965/*-------------------------------------------------
966    param_normalize_4_commutative - normalize a 4
967    parameter instruction, shuffling the
968    parameters on the assumption that the last
969    2 can be swapped
970-------------------------------------------------*/
971
972static void param_normalize_4_commutative(drcbe_state *drcbe, const drcuml_instruction *inst, drcuml_parameter *dest0, UINT32 allowed0, drcuml_parameter *dest1, UINT32 allowed1, drcuml_parameter *dest2, UINT32 allowed2, drcuml_parameter *dest3, UINT32 allowed3)
973{
974   param_normalize_4(drcbe, inst, dest0, allowed0, dest1, allowed1, dest2, allowed2, dest3, allowed3);
975
976   /* if the inner parameter is a memory operand, push it to the outer */
977   if (dest2->type == DRCUML_PTYPE_MEMORY)
978   {
979      drcuml_parameter temp = *dest2;
980      *dest2 = *dest3;
981      *dest3 = temp;
982   }
983
984   /* if the inner parameter is an immediate, push it to the outer */
985   if (dest2->type == DRCUML_PTYPE_IMMEDIATE)
986   {
987      drcuml_parameter temp = *dest2;
988      *dest2 = *dest3;
989      *dest3 = temp;
990   }
991
992   /* if the destination and outer parameters are equal, move the outer to the inner */
993   if (dest0->type == dest3->type && dest0->value == dest3->value && dest0->type != DRCUML_PTYPE_IMMEDIATE)
994   {
995      drcuml_parameter temp = *dest2;
996      *dest2 = *dest3;
997      *dest3 = temp;
998   }
999}
1000
1001
1002
1003/***************************************************************************
1004    EMITTERS FOR 32-BIT OPERATIONS WITH PARAMETERS
1005***************************************************************************/
1006
1007/*-------------------------------------------------
1008    emit_mov_r32_p32 - move a 32-bit parameter
1009    into a register
1010-------------------------------------------------*/
1011
1012static void emit_mov_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param)
1013{
1014   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1015   {
1016      if (param->value == 0)
1017         emit_xor_r32_r32(dst, reg, reg);                                 // xor   reg,reg
1018      else
1019         emit_mov_r32_imm(dst, reg, param->value);                           // mov   reg,param
1020   }
1021   else if (param->type == DRCUML_PTYPE_MEMORY)
1022      emit_mov_r32_m32(dst, reg, MABS(param->value));                           // mov   reg,[param]
1023   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
1024   {
1025      if (reg != param->value)
1026         emit_mov_r32_r32(dst, reg, param->value);                           // mov   reg,param
1027   }
1028}
1029
1030
1031/*-------------------------------------------------
1032    emit_mov_m32_p32 - move a 32-bit parameter
1033    into a memory location
1034-------------------------------------------------*/
1035
1036static void emit_mov_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param)
1037{
1038   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1039      emit_mov_m32_imm(dst, MEMPARAMS, param->value);                           // mov   [mem],param
1040   else if (param->type == DRCUML_PTYPE_MEMORY)
1041   {
1042      emit_mov_r32_m32(dst, REG_EAX, MABS(param->value));                        // mov   eax,[param]
1043      emit_mov_m32_r32(dst, MEMPARAMS, REG_EAX);                              // mov   [mem],eax
1044   }
1045   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
1046      emit_mov_m32_r32(dst, MEMPARAMS, param->value);                           // mov   [mem],param
1047}
1048
1049
1050/*-------------------------------------------------
1051    emit_mov_p32_r32 - move a register into a
1052    32-bit parameter
1053-------------------------------------------------*/
1054
1055static void emit_mov_p32_r32(drcbe_state *drcbe, x86code **dst, const drcuml_parameter *param, UINT8 reg)
1056{
1057   assert(param->type != DRCUML_PTYPE_IMMEDIATE);
1058   if (param->type == DRCUML_PTYPE_MEMORY)
1059      emit_mov_m32_r32(dst, MABS(param->value), reg);                           // mov   [param],reg
1060   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
1061   {
1062      if (reg != param->value)
1063         emit_mov_r32_r32(dst, param->value, reg);                           // mov   param,reg
1064   }
1065}
1066
1067
1068/*-------------------------------------------------
1069    emit_push_p32 - push a 32-bit parameter onto
1070    the stack
1071-------------------------------------------------*/
1072
1073static void emit_push_p32(drcbe_state *drcbe, x86code **dst, const drcuml_parameter *src)
1074{
1075   if (src->type == DRCUML_PTYPE_IMMEDIATE)
1076      emit_push_imm(dst, src->value);                                       // push  src
1077   else if (src->type == DRCUML_PTYPE_MEMORY)
1078      emit_push_m32(dst, MABS(src->value));                                 // push  [src]
1079   else if (src->type == DRCUML_PTYPE_INT_REGISTER)
1080      emit_push_r32(dst, src->value);                                       // push  src
1081}
1082
1083
1084/*-------------------------------------------------
1085    emit_add_r32_p32 - add operation to a 32-bit
1086    register from a 32-bit parameter
1087-------------------------------------------------*/
1088
1089static void emit_add_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
1090{
1091   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1092   {
1093      if (inst->condflags != 0 || param->value != 0)
1094         emit_add_r32_imm(dst, reg, param->value);                           // add   reg,param
1095   }
1096   else if (param->type == DRCUML_PTYPE_MEMORY)
1097      emit_add_r32_m32(dst, reg, MABS(param->value));                           // add   reg,[param]
1098   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
1099      emit_add_r32_r32(dst, reg, param->value);                              // add   reg,param
1100}
1101
1102
1103/*-------------------------------------------------
1104    emit_add_m32_p32 - add operation to a 32-bit
1105    memory location from a 32-bit parameter
1106-------------------------------------------------*/
1107
1108static void emit_add_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
1109{
1110   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1111   {
1112      if (inst->condflags != 0 || param->value != 0)
1113         emit_add_m32_imm(dst, MEMPARAMS, param->value);                        // add   [dest],param
1114   }
1115   else
1116   {
1117      int reg = param_select_register(REG_EAX, param, NULL);
1118      emit_mov_r32_p32(drcbe, dst, reg, param);                              // mov   reg,param
1119      emit_add_m32_r32(dst, MEMPARAMS, reg);                                 // add   [dest],reg
1120   }
1121}
1122
1123
1124/*-------------------------------------------------
1125    emit_adc_r32_p32 - adc operation to a 32-bit
1126    register from a 32-bit parameter
1127-------------------------------------------------*/
1128
1129static void emit_adc_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
1130{
1131   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1132   {
1133      if (inst->condflags != 0 || param->value != 0)
1134         emit_adc_r32_imm(dst, reg, param->value);                           // adc   reg,param
1135   }
1136   else if (param->type == DRCUML_PTYPE_MEMORY)
1137      emit_adc_r32_m32(dst, reg, MABS(param->value));                           // adc   reg,[param]
1138   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
1139      emit_adc_r32_r32(dst, reg, param->value);                              // adc   reg,param
1140}
1141
1142
1143/*-------------------------------------------------
1144    emit_adc_m32_p32 - adc operation to a 32-bit
1145    memory location from a 32-bit parameter
1146-------------------------------------------------*/
1147
1148static void emit_adc_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
1149{
1150   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1151   {
1152      if (inst->condflags != 0 || param->value != 0)
1153         emit_adc_m32_imm(dst, MEMPARAMS, param->value);                        // adc   [dest],param
1154   }
1155   else
1156   {
1157      int reg = param_select_register(REG_EAX, param, NULL);
1158      emit_mov_r32_p32(drcbe, dst, reg, param);                              // mov   reg,param
1159      emit_adc_m32_r32(dst, MEMPARAMS, reg);                                 // adc   [dest],reg
1160   }
1161}
1162
1163
1164/*-------------------------------------------------
1165    emit_sub_r32_p32 - sub operation to a 32-bit
1166    register from a 32-bit parameter
1167-------------------------------------------------*/
1168
1169static void emit_sub_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
1170{
1171   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1172   {
1173      if (inst->condflags != 0 || param->value != 0)
1174         emit_sub_r32_imm(dst, reg, param->value);                           // sub   reg,param
1175   }
1176   else if (param->type == DRCUML_PTYPE_MEMORY)
1177      emit_sub_r32_m32(dst, reg, MABS(param->value));                           // sub   reg,[param]
1178   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
1179      emit_sub_r32_r32(dst, reg, param->value);                              // sub   reg,param
1180}
1181
1182
1183/*-------------------------------------------------
1184    emit_sub_m32_p32 - sub operation to a 32-bit
1185    memory location from a 32-bit parameter
1186-------------------------------------------------*/
1187
1188static void emit_sub_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
1189{
1190   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1191   {
1192      if (inst->condflags != 0 || param->value != 0)
1193         emit_sub_m32_imm(dst, MEMPARAMS, param->value);                        // sub   [dest],param
1194   }
1195   else
1196   {
1197      int reg = param_select_register(REG_EAX, param, NULL);
1198      emit_mov_r32_p32(drcbe, dst, reg, param);                              // mov   reg,param
1199      emit_sub_m32_r32(dst, MEMPARAMS, reg);                                 // sub   [dest],reg
1200   }
1201}
1202
1203
1204/*-------------------------------------------------
1205    emit_sbb_r32_p32 - sbb operation to a 32-bit
1206    register from a 32-bit parameter
1207-------------------------------------------------*/
1208
1209static void emit_sbb_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
1210{
1211   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1212   {
1213      if (inst->condflags != 0 || param->value != 0)
1214         emit_sbb_r32_imm(dst, reg, param->value);                           // sbb   reg,param
1215   }
1216   else if (param->type == DRCUML_PTYPE_MEMORY)
1217      emit_sbb_r32_m32(dst, reg, MABS(param->value));                           // sbb   reg,[param]
1218   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
1219      emit_sbb_r32_r32(dst, reg, param->value);                              // sbb   reg,param
1220}
1221
1222
1223/*-------------------------------------------------
1224    emit_sbb_m32_p32 - sbb operation to a 32-bit
1225    memory location from a 32-bit parameter
1226-------------------------------------------------*/
1227
1228static void emit_sbb_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
1229{
1230   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1231   {
1232      if (inst->condflags != 0 || param->value != 0)
1233         emit_sbb_m32_imm(dst, MEMPARAMS, param->value);                        // sbb   [dest],param
1234   }
1235   else
1236   {
1237      int reg = param_select_register(REG_EAX, param, NULL);
1238      emit_mov_r32_p32(drcbe, dst, reg, param);                              // mov   reg,param
1239      emit_sbb_m32_r32(dst, MEMPARAMS, reg);                                 // sbb   [dest],reg
1240   }
1241}
1242
1243
1244/*-------------------------------------------------
1245    emit_cmp_r32_p32 - cmp operation to a 32-bit
1246    register from a 32-bit parameter
1247-------------------------------------------------*/
1248
1249static void emit_cmp_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
1250{
1251   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1252      emit_cmp_r32_imm(dst, reg, param->value);                              // cmp   reg,param
1253   else if (param->type == DRCUML_PTYPE_MEMORY)
1254      emit_cmp_r32_m32(dst, reg, MABS(param->value));                           // cmp   reg,[param]
1255   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
1256      emit_cmp_r32_r32(dst, reg, param->value);                              // cmp   reg,param
1257}
1258
1259
1260/*-------------------------------------------------
1261    emit_cmp_r32_p32hi - cmp operation to a 32-bit
1262    register from the upper 32 bits of a 64-bit
1263    parameter
1264-------------------------------------------------*/
1265
1266static void emit_cmp_r32_p32hi(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
1267{
1268   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1269      emit_cmp_r32_imm(dst, reg, param->value >> 32);                           // cmp   reg,param >> 32
1270   else if (param->type == DRCUML_PTYPE_MEMORY)
1271      emit_cmp_r32_m32(dst, reg, MABS(param->value + 4));                        // cmp   reg,[param+4]
1272   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
1273      emit_cmp_r32_m32(dst, reg, MABS(drcbe->reghi[param->value]));               // cmp   reg,reghi[param]
1274}
1275
1276
1277/*-------------------------------------------------
1278    emit_cmp_m32_p32 - cmp operation to a 32-bit
1279    memory location from a 32-bit parameter
1280-------------------------------------------------*/
1281
1282static void emit_cmp_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
1283{
1284   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1285      emit_cmp_m32_imm(dst, MEMPARAMS, param->value);                           // cmp   [dest],param
1286   else
1287   {
1288      int reg = param_select_register(REG_EAX, param, NULL);
1289      emit_mov_r32_p32(drcbe, dst, reg, param);                              // mov   reg,param
1290      emit_cmp_m32_r32(dst, MEMPARAMS, reg);                                 // cmp   [dest],reg
1291   }
1292}
1293
1294
1295/*-------------------------------------------------
1296    emit_cmp_m32_p32hi - cmp operation to a 32-bit
1297    memory location from the upper half of a
1298    64-bit parameter
1299-------------------------------------------------*/
1300
1301static void emit_cmp_m32_p32hi(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
1302{
1303   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1304      emit_cmp_m32_imm(dst, MEMPARAMS, param->value >> 32);                     // cmp   [dest],param >> 32
1305   else
1306   {
1307      if (param->type == DRCUML_PTYPE_MEMORY)
1308         emit_mov_r32_m32(dst, REG_EAX, MABS(param->value + 4));                  // mov   eax,[param+4]
1309      else if (param->type == DRCUML_PTYPE_INT_REGISTER)
1310         emit_mov_r32_m32(dst, REG_EAX, MABS(drcbe->reghi[param->value]));         // mov   eax,reghi[param]
1311      emit_cmp_m32_r32(dst, MEMPARAMS, REG_EAX);                              // cmp   [dest],eax
1312   }
1313}
1314
1315
1316/*-------------------------------------------------
1317    emit_and_r32_p32 - and operation to a 32-bit
1318    register from a 32-bit parameter
1319-------------------------------------------------*/
1320
1321static void emit_and_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
1322{
1323   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1324   {
1325      if (inst->condflags != 0 && (UINT32)param->value == 0xffffffff)
1326         /* skip */;
1327      else if (inst->condflags != 0 && (UINT32)param->value == 0)
1328         emit_xor_r32_r32(dst, reg, reg);                                 // xor   reg,reg
1329      else
1330         emit_and_r32_imm(dst, reg, param->value);                           // and   reg,param
1331   }
1332   else if (param->type == DRCUML_PTYPE_MEMORY)
1333      emit_and_r32_m32(dst, reg, MABS(param->value));                           // and   reg,[param]
1334   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
1335      emit_and_r32_r32(dst, reg, param->value);                              // and   reg,param
1336}
1337
1338
1339/*-------------------------------------------------
1340    emit_and_m32_p32 - and operation to a 32-bit
1341    memory location from a 32-bit parameter
1342-------------------------------------------------*/
1343
1344static void emit_and_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
1345{
1346   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1347   {
1348      if (inst->condflags != 0 && (UINT32)param->value == 0xffffffff)
1349         /* skip */;
1350      else if (inst->condflags != 0 && (UINT32)param->value == 0)
1351         emit_mov_m32_imm(dst, MEMPARAMS, 0);                              // mov   [dest],0
1352      else
1353         emit_and_m32_imm(dst, MEMPARAMS, param->value);                        // and   [dest],param
1354   }
1355   else
1356   {
1357      int reg = param_select_register(REG_EAX, param, NULL);
1358      emit_mov_r32_p32(drcbe, dst, reg, param);                              // mov   reg,param
1359      emit_and_m32_r32(dst, MEMPARAMS, reg);                                 // and   [dest],reg
1360   }
1361}
1362
1363
1364/*-------------------------------------------------
1365    emit_test_r32_p32 - test operation to a 32-bit
1366    register from a 32-bit parameter
1367-------------------------------------------------*/
1368
1369static void emit_test_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
1370{
1371   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1372      emit_test_r32_imm(dst, reg, param->value);                              // test   reg,param
1373   else if (param->type == DRCUML_PTYPE_MEMORY)
1374      emit_test_m32_r32(dst, MABS(param->value), reg);                        // test   [param],reg
1375   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
1376      emit_test_r32_r32(dst, reg, param->value);                              // test   reg,param
1377}
1378
1379
1380/*-------------------------------------------------
1381    emit_test_m32_p32 - test operation to a 32-bit
1382    memory location from a 32-bit parameter
1383-------------------------------------------------*/
1384
1385static void emit_test_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
1386{
1387   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1388      emit_test_m32_imm(dst, MEMPARAMS, param->value);                        // test  [dest],param
1389   else if (param->type == DRCUML_PTYPE_MEMORY)
1390   {
1391      emit_mov_r32_p32(drcbe, dst, REG_EAX, param);                           // mov   reg,param
1392      emit_test_m32_r32(dst, MEMPARAMS, REG_EAX);                              // test  [dest],reg
1393   }
1394   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
1395      emit_test_m32_r32(dst, MEMPARAMS, param->value);                        // test  [dest],param
1396}
1397
1398
1399/*-------------------------------------------------
1400    emit_or_r32_p32 - or operation to a 32-bit
1401    register from a 32-bit parameter
1402-------------------------------------------------*/
1403
1404static void emit_or_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
1405{
1406   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1407   {
1408      if (inst->condflags != 0 && (UINT32)param->value == 0)
1409         /* skip */;
1410      else if (inst->condflags != 0 && (UINT32)param->value == 0xffffffff)
1411         emit_mov_r32_imm(dst, reg, -1);                                    // mov  reg,-1
1412      else
1413         emit_or_r32_imm(dst, reg, param->value);                           // or   reg,param
1414   }
1415   else if (param->type == DRCUML_PTYPE_MEMORY)
1416      emit_or_r32_m32(dst, reg, MABS(param->value));                           // or   reg,[param]
1417   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
1418      emit_or_r32_r32(dst, reg, param->value);                              // or   reg,param
1419}
1420
1421
1422/*-------------------------------------------------
1423    emit_or_m32_p32 - or operation to a 32-bit
1424    memory location from a 32-bit parameter
1425-------------------------------------------------*/
1426
1427static void emit_or_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
1428{
1429   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1430   {
1431      if (inst->condflags != 0 && (UINT32)param->value == 0)
1432         /* skip */;
1433      else if (inst->condflags != 0 && (UINT32)param->value == 0xffffffff)
1434         emit_mov_m32_imm(dst, MEMPARAMS, -1);                              // mov   [dest],-1
1435      else
1436         emit_or_m32_imm(dst, MEMPARAMS, param->value);                        // or   [dest],param
1437   }
1438   else
1439   {
1440      int reg = param_select_register(REG_EAX, param, NULL);
1441      emit_mov_r32_p32(drcbe, dst, reg, param);                              // mov   reg,param
1442      emit_or_m32_r32(dst, MEMPARAMS, reg);                                 // or   [dest],reg
1443   }
1444}
1445
1446
1447/*-------------------------------------------------
1448    emit_xor_r32_p32 - xor operation to a 32-bit
1449    register from a 32-bit parameter
1450-------------------------------------------------*/
1451
1452static void emit_xor_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
1453{
1454   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1455   {
1456      if (inst->condflags != 0 && (UINT32)param->value == 0)
1457         /* skip */;
1458      else if (inst->condflags != 0 && (UINT32)param->value == 0xffffffff)
1459         emit_not_r32(dst, reg);                                          // not   reg
1460      else
1461         emit_xor_r32_imm(dst, reg, param->value);                           // xor   reg,param
1462   }
1463   else if (param->type == DRCUML_PTYPE_MEMORY)
1464      emit_xor_r32_m32(dst, reg, MABS(param->value));                           // xor   reg,[param]
1465   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
1466      emit_xor_r32_r32(dst, reg, param->value);                              // xor   reg,param
1467}
1468
1469
1470/*-------------------------------------------------
1471    emit_xor_m32_p32 - xor operation to a 32-bit
1472    memory location from a 32-bit parameter
1473-------------------------------------------------*/
1474
1475static void emit_xor_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
1476{
1477   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1478   {
1479      if (inst->condflags != 0 && (UINT32)param->value == 0)
1480         /* skip */;
1481      else if (inst->condflags != 0 && (UINT32)param->value == 0xffffffff)
1482         emit_not_m32(dst, MEMPARAMS);                                    // not   [dest]
1483      else
1484         emit_xor_m32_imm(dst, MEMPARAMS, param->value);                        // xor   [dest],param
1485   }
1486   else
1487   {
1488      int reg = param_select_register(REG_EAX, param, NULL);
1489      emit_mov_r32_p32(drcbe, dst, reg, param);                              // mov   reg,param
1490      emit_xor_m32_r32(dst, MEMPARAMS, reg);                                 // xor   [dest],reg
1491   }
1492}
1493
1494
1495/*-------------------------------------------------
1496    emit_shl_r32_p32 - shl operation to a 32-bit
1497    register from a 32-bit parameter
1498-------------------------------------------------*/
1499
1500static void emit_shl_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
1501{
1502   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1503   {
1504      if (inst->condflags != 0 && (UINT32)param->value == 0)
1505         /* skip */;
1506      else
1507         emit_shl_r32_imm(dst, reg, param->value);                           // shl   reg,param
1508   }
1509   else
1510   {
1511      emit_mov_r32_p32(drcbe, dst, REG_ECX, param);                           // mov   ecx,param
1512      emit_shl_r32_cl(dst, reg);                                          // shl   reg,cl
1513   }
1514}
1515
1516
1517/*-------------------------------------------------
1518    emit_shl_m32_p32 - shl operation to a 32-bit
1519    memory location from a 32-bit parameter
1520-------------------------------------------------*/
1521
1522static void emit_shl_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
1523{
1524   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1525   {
1526      if (inst->condflags != 0 && (UINT32)param->value == 0)
1527         /* skip */;
1528      else
1529         emit_shl_m32_imm(dst, MEMPARAMS, param->value);                        // shl   [dest],param
1530   }
1531   else
1532   {
1533      emit_mov_r32_p32(drcbe, dst, REG_ECX, param);                           // mov   ecx,param
1534      emit_shl_m32_cl(dst, MEMPARAMS);                                    // shl   [dest],cl
1535   }
1536}
1537
1538
1539/*-------------------------------------------------
1540    emit_shr_r32_p32 - shr operation to a 32-bit
1541    register from a 32-bit parameter
1542-------------------------------------------------*/
1543
1544static void emit_shr_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
1545{
1546   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1547   {
1548      if (inst->condflags != 0 && (UINT32)param->value == 0)
1549         /* skip */;
1550      else
1551         emit_shr_r32_imm(dst, reg, param->value);                           // shr   reg,param
1552   }
1553   else
1554   {
1555      emit_mov_r32_p32(drcbe, dst, REG_ECX, param);                           // mov   ecx,param
1556      emit_shr_r32_cl(dst, reg);                                          // shr   reg,cl
1557   }
1558}
1559
1560
1561/*-------------------------------------------------
1562    emit_shr_m32_p32 - shr operation to a 32-bit
1563    memory location from a 32-bit parameter
1564-------------------------------------------------*/
1565
1566static void emit_shr_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
1567{
1568   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1569   {
1570      if (inst->condflags != 0 && (UINT32)param->value == 0)
1571         /* skip */;
1572      else
1573         emit_shr_m32_imm(dst, MEMPARAMS, param->value);                        // shr   [dest],param
1574   }
1575   else
1576   {
1577      emit_mov_r32_p32(drcbe, dst, REG_ECX, param);                           // mov   ecx,param
1578      emit_shr_m32_cl(dst, MEMPARAMS);                                    // shr   [dest],cl
1579   }
1580}
1581
1582
1583/*-------------------------------------------------
1584    emit_sar_r32_p32 - sar operation to a 32-bit
1585    register from a 32-bit parameter
1586-------------------------------------------------*/
1587
1588static void emit_sar_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
1589{
1590   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1591   {
1592      if (inst->condflags != 0 && (UINT32)param->value == 0)
1593         /* skip */;
1594      else
1595         emit_sar_r32_imm(dst, reg, param->value);                           // sar   reg,param
1596   }
1597   else
1598   {
1599      emit_mov_r32_p32(drcbe, dst, REG_ECX, param);                           // mov   ecx,param
1600      emit_sar_r32_cl(dst, reg);                                          // sar   reg,cl
1601   }
1602}
1603
1604
1605/*-------------------------------------------------
1606    emit_sar_m32_p32 - sar operation to a 32-bit
1607    memory location from a 32-bit parameter
1608-------------------------------------------------*/
1609
1610static void emit_sar_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
1611{
1612   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1613   {
1614      if (inst->condflags != 0 && (UINT32)param->value == 0)
1615         /* skip */;
1616      else
1617         emit_sar_m32_imm(dst, MEMPARAMS, param->value);                        // sar   [dest],param
1618   }
1619   else
1620   {
1621      emit_mov_r32_p32(drcbe, dst, REG_ECX, param);                           // mov   ecx,param
1622      emit_sar_m32_cl(dst, MEMPARAMS);                                    // sar   [dest],cl
1623   }
1624}
1625
1626
1627/*-------------------------------------------------
1628    emit_rol_r32_p32 - rol operation to a 32-bit
1629    register from a 32-bit parameter
1630-------------------------------------------------*/
1631
1632static void emit_rol_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
1633{
1634   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1635   {
1636      if (inst->condflags != 0 && (UINT32)param->value == 0)
1637         /* skip */;
1638      else
1639         emit_rol_r32_imm(dst, reg, param->value);                           // rol   reg,param
1640   }
1641   else
1642   {
1643      emit_mov_r32_p32(drcbe, dst, REG_ECX, param);                           // mov   ecx,param
1644      emit_rol_r32_cl(dst, reg);                                          // rol   reg,cl
1645   }
1646}
1647
1648
1649/*-------------------------------------------------
1650    emit_rol_m32_p32 - rol operation to a 32-bit
1651    memory location from a 32-bit parameter
1652-------------------------------------------------*/
1653
1654static void emit_rol_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
1655{
1656   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1657   {
1658      if (inst->condflags != 0 && (UINT32)param->value == 0)
1659         /* skip */;
1660      else
1661         emit_rol_m32_imm(dst, MEMPARAMS, param->value);                        // rol   [dest],param
1662   }
1663   else
1664   {
1665      emit_mov_r32_p32(drcbe, dst, REG_ECX, param);                           // mov   ecx,param
1666      emit_rol_m32_cl(dst, MEMPARAMS);                                    // rol   [dest],cl
1667   }
1668}
1669
1670
1671/*-------------------------------------------------
1672    emit_ror_r32_p32 - ror operation to a 32-bit
1673    register from a 32-bit parameter
1674-------------------------------------------------*/
1675
1676static void emit_ror_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
1677{
1678   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1679   {
1680      if (inst->condflags != 0 && (UINT32)param->value == 0)
1681         /* skip */;
1682      else
1683         emit_ror_r32_imm(dst, reg, param->value);                           // ror   reg,param
1684   }
1685   else
1686   {
1687      emit_mov_r32_p32(drcbe, dst, REG_ECX, param);                           // mov   ecx,param
1688      emit_ror_r32_cl(dst, reg);                                          // ror   reg,cl
1689   }
1690}
1691
1692
1693/*-------------------------------------------------
1694    emit_ror_m32_p32 - ror operation to a 32-bit
1695    memory location from a 32-bit parameter
1696-------------------------------------------------*/
1697
1698static void emit_ror_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
1699{
1700   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1701   {
1702      if (inst->condflags != 0 && (UINT32)param->value == 0)
1703         /* skip */;
1704      else
1705         emit_ror_m32_imm(dst, MEMPARAMS, param->value);                        // ror   [dest],param
1706   }
1707   else
1708   {
1709      emit_mov_r32_p32(drcbe, dst, REG_ECX, param);                           // mov   ecx,param
1710      emit_ror_m32_cl(dst, MEMPARAMS);                                    // ror   [dest],cl
1711   }
1712}
1713
1714
1715/*-------------------------------------------------
1716    emit_rcl_r32_p32 - rcl operation to a 32-bit
1717    register from a 32-bit parameter
1718-------------------------------------------------*/
1719
1720static void emit_rcl_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
1721{
1722   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1723   {
1724      if (inst->condflags != 0 && (UINT32)param->value == 0)
1725         /* skip */;
1726      else
1727         emit_rcl_r32_imm(dst, reg, param->value);                           // rcl   reg,param
1728   }
1729   else
1730   {
1731      emit_mov_r32_p32(drcbe, dst, REG_ECX, param);                           // mov   ecx,param
1732      emit_rcl_r32_cl(dst, reg);                                          // rcl   reg,cl
1733   }
1734}
1735
1736
1737/*-------------------------------------------------
1738    emit_rcl_m32_p32 - rcl operation to a 32-bit
1739    memory location from a 32-bit parameter
1740-------------------------------------------------*/
1741
1742static void emit_rcl_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
1743{
1744   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1745   {
1746      if (inst->condflags != 0 && (UINT32)param->value == 0)
1747         /* skip */;
1748      else
1749         emit_rcl_m32_imm(dst, MEMPARAMS, param->value);                        // rcl   [dest],param
1750   }
1751   else
1752   {
1753      emit_mov_r32_p32(drcbe, dst, REG_ECX, param);                           // mov   ecx,param
1754      emit_rcl_m32_cl(dst, MEMPARAMS);                                    // rcl   [dest],cl
1755   }
1756}
1757
1758
1759/*-------------------------------------------------
1760    emit_rcr_r32_p32 - rcr operation to a 32-bit
1761    register from a 32-bit parameter
1762-------------------------------------------------*/
1763
1764static void emit_rcr_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst)
1765{
1766   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1767   {
1768      if (inst->condflags != 0 && (UINT32)param->value == 0)
1769         /* skip */;
1770      else
1771         emit_rcr_r32_imm(dst, reg, param->value);                           // rcr   reg,param
1772   }
1773   else
1774   {
1775      emit_mov_r32_p32(drcbe, dst, REG_ECX, param);                           // mov   ecx,param
1776      emit_rcr_r32_cl(dst, reg);                                          // rcr   reg,cl
1777   }
1778}
1779
1780
1781/*-------------------------------------------------
1782    emit_rcr_m32_p32 - rcr operation to a 32-bit
1783    memory location from a 32-bit parameter
1784-------------------------------------------------*/
1785
1786static void emit_rcr_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
1787{
1788   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1789   {
1790      if (inst->condflags != 0 && (UINT32)param->value == 0)
1791         /* skip */;
1792      else
1793         emit_rcr_m32_imm(dst, MEMPARAMS, param->value);                        // rcr   [dest],param
1794   }
1795   else
1796   {
1797      emit_mov_r32_p32(drcbe, dst, REG_ECX, param);                           // mov   ecx,param
1798      emit_rcr_m32_cl(dst, MEMPARAMS);                                    // rcr   [dest],cl
1799   }
1800}
1801
1802
1803
1804/***************************************************************************
1805    EMITTERS FOR 64-BIT OPERATIONS WITH PARAMETERS
1806***************************************************************************/
1807
1808/*-------------------------------------------------
1809    emit_mov_r64_p64 - move a 64-bit parameter
1810    into a pair of registers
1811-------------------------------------------------*/
1812
1813static void emit_mov_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reglo, UINT8 reghi, const drcuml_parameter *param)
1814{
1815   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1816   {
1817      if ((UINT32)param->value == 0)
1818         emit_xor_r32_r32(dst, reglo, reglo);                              // xor   reglo,reglo
1819      else
1820         emit_mov_r32_imm(dst, reglo, param->value);                           // mov   reglo,param
1821      if ((UINT32)(param->value >> 32) == 0)
1822         emit_xor_r32_r32(dst, reghi, reghi);                              // xor   reghi,reghi
1823      else
1824         emit_mov_r32_imm(dst, reghi, param->value >> 32);                     // mov   reghi,param >> 32
1825   }
1826   else if (param->type == DRCUML_PTYPE_MEMORY)
1827   {
1828      emit_mov_r32_m32(dst, reglo, MABS(param->value));                        // mov   reglo,[param]
1829      emit_mov_r32_m32(dst, reghi, MABS(param->value + 4));                     // mov   reghi,[param+4]
1830   }
1831   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
1832   {
1833      if (reglo != param->value)
1834         emit_mov_r32_r32(dst, reglo, param->value);                           // mov   reglo,param
1835      emit_mov_r32_m32(dst, reghi, MABS(drcbe->reghi[param->value]));               // mov   reghi,reghi[param]
1836   }
1837}
1838
1839
1840/*-------------------------------------------------
1841    emit_mov_p64_r64 - move a pair of registers
1842    into a 64-bit parameter
1843-------------------------------------------------*/
1844
1845static void emit_mov_p64_r64(drcbe_state *drcbe, x86code **dst, const drcuml_parameter *param, UINT8 reglo, UINT8 reghi)
1846{
1847   assert(param->type != DRCUML_PTYPE_IMMEDIATE);
1848   if (param->type == DRCUML_PTYPE_MEMORY)
1849   {
1850      emit_mov_m32_r32(dst, MABS(param->value), reglo);                        // mov   [param],reglo
1851      emit_mov_m32_r32(dst, MABS(param->value + 4), reghi);                     // mov   [param+4],reghi
1852   }
1853   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
1854   {
1855      if (reglo != param->value)
1856         emit_mov_r32_r32(dst, param->value, reglo);                           // mov   param,reglo
1857      emit_mov_m32_r32(dst, MABS(drcbe->reghi[param->value]), reghi);               // mov   reghi[param],reghi
1858   }
1859}
1860
1861
1862/*-------------------------------------------------
1863    emit_push_p64 - push a 64-bit parameter onto
1864    the stack
1865-------------------------------------------------*/
1866
1867static void emit_push_p64(drcbe_state *drcbe, x86code **dst, const drcuml_parameter *src)
1868{
1869   if (src->type == DRCUML_PTYPE_IMMEDIATE)
1870   {
1871      emit_push_imm(dst, src->value >> 32);                                 // push  src >> 32
1872      emit_push_imm(dst, src->value);                                       // push  src
1873   }
1874   else if (src->type == DRCUML_PTYPE_MEMORY)
1875   {
1876      emit_push_m32(dst, MABS(src->value + 4));                              // push  [src+4]
1877      emit_push_m32(dst, MABS(src->value));                                 // push  [src]
1878   }
1879   else if (src->type == DRCUML_PTYPE_INT_REGISTER)
1880   {
1881      emit_push_m32(dst, MABS(drcbe->reghi[src->value]));                        // push  reghi[src]
1882      emit_push_r32(dst, src->value);                                       // push  src
1883   }
1884}
1885
1886
1887/*-------------------------------------------------
1888    emit_add_r64_p64 - add operation to a 64-bit
1889    pair of registers from a 64-bit parameter
1890-------------------------------------------------*/
1891
1892static void emit_add_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reglo, UINT8 reghi, const drcuml_parameter *param, const drcuml_instruction *inst)
1893{
1894   int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0);
1895   if (param->type == DRCUML_PTYPE_MEMORY)
1896   {
1897      emit_add_r32_m32(dst, reglo, MABS(param->value));                        // add   reglo,[param]
1898      if (saveflags) emit_pushf(dst);                                       // pushf
1899      emit_adc_r32_m32(dst, reghi, MABS(param->value + 4));                     // adc   reghi,[param]
1900   }
1901   else if (param->type == DRCUML_PTYPE_IMMEDIATE)
1902   {
1903      emit_add_r32_imm(dst, reglo, param->value);                              // add   reglo,param
1904      if (saveflags) emit_pushf(dst);                                       // pushf
1905      emit_adc_r32_imm(dst, reghi, param->value >> 32);                        // adc   reghi,param >> 32
1906   }
1907   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
1908   {
1909      emit_add_r32_r32(dst, reglo, param->value);                              // add   reglo,param
1910      if (saveflags) emit_pushf(dst);                                       // pushf
1911      emit_adc_r32_m32(dst, reghi, MABS(drcbe->reghi[param->value]));               // adc   reghi,reghi[param]
1912   }
1913   if (saveflags)
1914      emit_combine_z_flags(dst);
1915}
1916
1917
1918/*-------------------------------------------------
1919    emit_add_m64_p64 - add operation to a 64-bit
1920    memory location from a 64-bit parameter
1921-------------------------------------------------*/
1922
1923static void emit_add_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
1924{
1925   int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0);
1926   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1927   {
1928      emit_add_m32_imm(dst, MEMPARAMS, param->value);                           // add   [dest],param
1929      if (saveflags) emit_pushf(dst);                                       // pushf
1930      emit_adc_m32_imm(dst, MEMPARAMS + 4, param->value >> 32);                  // adc   [dest+4],param >> 32
1931   }
1932   else
1933   {
1934      int reglo = (param->type == DRCUML_PTYPE_INT_REGISTER) ? param->value : REG_EAX;
1935      emit_mov_r64_p64(drcbe, dst, reglo, REG_EDX, param);                     // mov   edx:reglo,param
1936      emit_add_m32_r32(dst, MEMPARAMS, reglo);                              // add   [dest],reglo
1937      if (saveflags) emit_pushf(dst);                                       // pushf
1938      emit_adc_m32_r32(dst, MEMPARAMS + 4, REG_EDX);                           // adc   [dest+4],edx
1939   }
1940   if (saveflags)
1941      emit_combine_z_flags(dst);
1942}
1943
1944
1945/*-------------------------------------------------
1946    emit_adc_r64_p64 - adc operation to a 64-bit
1947    pair of registers from a 64-bit parameter
1948-------------------------------------------------*/
1949
1950static void emit_adc_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reglo, UINT8 reghi, const drcuml_parameter *param, const drcuml_instruction *inst)
1951{
1952   int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0);
1953   if (param->type == DRCUML_PTYPE_MEMORY)
1954   {
1955      emit_adc_r32_m32(dst, reglo, MABS(param->value));                        // adc   reglo,[param]
1956      if (saveflags) emit_pushf(dst);                                       // pushf
1957      emit_adc_r32_m32(dst, reghi, MABS(param->value + 4));                     // adc   reghi,[param]
1958   }
1959   else if (param->type == DRCUML_PTYPE_IMMEDIATE)
1960   {
1961      emit_adc_r32_imm(dst, reglo, param->value);                              // adc   reglo,param
1962      if (saveflags) emit_pushf(dst);                                       // pushf
1963      emit_adc_r32_imm(dst, reghi, param->value >> 32);                        // adc   reghi,param >> 32
1964   }
1965   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
1966   {
1967      emit_adc_r32_r32(dst, reglo, param->value);                              // adc   reglo,param
1968      if (saveflags) emit_pushf(dst);                                       // pushf
1969      emit_adc_r32_m32(dst, reghi, MABS(drcbe->reghi[param->value]));               // adc   reghi,reghi[param]
1970   }
1971   if (saveflags)
1972      emit_combine_z_flags(dst);
1973}
1974
1975
1976/*-------------------------------------------------
1977    emit_adc_m64_p64 - adc operation to a 64-bit
1978    memory location from a 64-bit parameter
1979-------------------------------------------------*/
1980
1981static void emit_adc_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
1982{
1983   int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0);
1984   if (param->type == DRCUML_PTYPE_IMMEDIATE)
1985   {
1986      emit_adc_m32_imm(dst, MEMPARAMS, param->value);                           // adc   [dest],param
1987      if (saveflags) emit_pushf(dst);                                       // pushf
1988      emit_adc_m32_imm(dst, MEMPARAMS + 4, param->value >> 32);                  // adc   [dest+4],param >> 32
1989   }
1990   else
1991   {
1992      int reglo = (param->type == DRCUML_PTYPE_INT_REGISTER) ? param->value : REG_EAX;
1993      emit_mov_r64_p64(drcbe, dst, reglo, REG_EDX, param);                     // mov   edx:reglo,param
1994      emit_adc_m32_r32(dst, MEMPARAMS, reglo);                              // adc   [dest],reglo
1995      if (saveflags) emit_pushf(dst);                                       // pushf
1996      emit_adc_m32_r32(dst, MEMPARAMS + 4, REG_EDX);                           // adc   [dest+4],edx
1997   }
1998   if (saveflags)
1999      emit_combine_z_flags(dst);
2000}
2001
2002
2003/*-------------------------------------------------
2004    emit_sub_r64_p64 - sub operation to a 64-bit
2005    pair of registers from a 64-bit parameter
2006-------------------------------------------------*/
2007
2008static void emit_sub_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reglo, UINT8 reghi, const drcuml_parameter *param, const drcuml_instruction *inst)
2009{
2010   int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0);
2011   if (param->type == DRCUML_PTYPE_MEMORY)
2012   {
2013      emit_sub_r32_m32(dst, reglo, MABS(param->value));                        // sub   reglo,[param]
2014      if (saveflags) emit_pushf(dst);                                       // pushf
2015      emit_sbb_r32_m32(dst, reghi, MABS(param->value + 4));                     // sbb   reghi,[param]
2016   }
2017   else if (param->type == DRCUML_PTYPE_IMMEDIATE)
2018   {
2019      emit_sub_r32_imm(dst, reglo, param->value);                              // sub   reglo,param
2020      if (saveflags) emit_pushf(dst);                                       // pushf
2021      emit_sbb_r32_imm(dst, reghi, param->value >> 32);                        // sbb   reghi,param >> 32
2022   }
2023   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
2024   {
2025      emit_sub_r32_r32(dst, reglo, param->value);                              // sub   reglo,param
2026      if (saveflags) emit_pushf(dst);                                       // pushf
2027      emit_sbb_r32_m32(dst, reghi, MABS(drcbe->reghi[param->value]));               // sbb   reghi,reghi[param]
2028   }
2029   if (saveflags)
2030      emit_combine_z_flags(dst);
2031}
2032
2033
2034/*-------------------------------------------------
2035    emit_sub_m64_p64 - sub operation to a 64-bit
2036    memory location from a 64-bit parameter
2037-------------------------------------------------*/
2038
2039static void emit_sub_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
2040{
2041   int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0);
2042   if (param->type == DRCUML_PTYPE_IMMEDIATE)
2043   {
2044      emit_sub_m32_imm(dst, MEMPARAMS, param->value);                           // sub   [dest],param
2045      if (saveflags) emit_pushf(dst);                                       // pushf
2046      emit_sbb_m32_imm(dst, MEMPARAMS + 4, param->value >> 32);                  // sbb   [dest+4],param >> 32
2047   }
2048   else
2049   {
2050      int reglo = (param->type == DRCUML_PTYPE_INT_REGISTER) ? param->value : REG_EAX;
2051      emit_mov_r64_p64(drcbe, dst, reglo, REG_EDX, param);                     // mov   edx:reglo,param
2052      emit_sub_m32_r32(dst, MEMPARAMS, reglo);                              // sub   [dest],reglo
2053      if (saveflags) emit_pushf(dst);                                       // pushf
2054      emit_sbb_m32_r32(dst, MEMPARAMS + 4, REG_EDX);                           // sbb   [dest+4],edx
2055   }
2056   if (saveflags)
2057      emit_combine_z_flags(dst);
2058}
2059
2060
2061/*-------------------------------------------------
2062    emit_sbb_r64_p64 - sbb operation to a 64-bit
2063    pair of registers from a 64-bit parameter
2064-------------------------------------------------*/
2065
2066static void emit_sbb_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reglo, UINT8 reghi, const drcuml_parameter *param, const drcuml_instruction *inst)
2067{
2068   int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0);
2069   if (param->type == DRCUML_PTYPE_MEMORY)
2070   {
2071      emit_sbb_r32_m32(dst, reglo, MABS(param->value));                        // sbb   reglo,[param]
2072      if (saveflags) emit_pushf(dst);                                       // pushf
2073      emit_sbb_r32_m32(dst, reghi, MABS(param->value + 4));                     // sbb   reghi,[param]
2074   }
2075   else if (param->type == DRCUML_PTYPE_IMMEDIATE)
2076   {
2077      emit_sbb_r32_imm(dst, reglo, param->value);                              // sbb   reglo,param
2078      if (saveflags) emit_pushf(dst);                                       // pushf
2079      emit_sbb_r32_imm(dst, reghi, param->value >> 32);                        // sbb   reghi,param >> 32
2080   }
2081   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
2082   {
2083      emit_sbb_r32_r32(dst, reglo, param->value);                              // sbb   reglo,param
2084      if (saveflags) emit_pushf(dst);                                       // pushf
2085      emit_sbb_r32_m32(dst, reghi, MABS(drcbe->reghi[param->value]));               // sbb   reghi,reghi[param]
2086   }
2087   if (saveflags)
2088      emit_combine_z_flags(dst);
2089}
2090
2091
2092/*-------------------------------------------------
2093    emit_sbb_m64_p64 - sbb operation to a 64-bit
2094    memory location from a 64-bit parameter
2095-------------------------------------------------*/
2096
2097static void emit_sbb_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
2098{
2099   int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0);
2100   if (param->type == DRCUML_PTYPE_IMMEDIATE)
2101   {
2102      emit_sbb_m32_imm(dst, MEMPARAMS, param->value);                           // sbb   [dest],param
2103      if (saveflags) emit_pushf(dst);                                       // pushf
2104      emit_sbb_m32_imm(dst, MEMPARAMS + 4, param->value >> 32);                  // sbb   [dest+4],param >> 32
2105   }
2106   else
2107   {
2108      int reglo = (param->type == DRCUML_PTYPE_INT_REGISTER) ? param->value : REG_EAX;
2109      emit_mov_r64_p64(drcbe, dst, reglo, REG_EDX, param);                     // mov   edx:reglo,param
2110      emit_sbb_m32_r32(dst, MEMPARAMS, reglo);                              // sbb   [dest],reglo
2111      if (saveflags) emit_pushf(dst);                                       // pushf
2112      emit_sbb_m32_r32(dst, MEMPARAMS + 4, REG_EDX);                           // sbb   [dest+4],edx
2113   }
2114   if (saveflags)
2115      emit_combine_z_flags(dst);
2116}
2117
2118
2119/*-------------------------------------------------
2120    emit_and_r64_p64 - and operation to a 64-bit
2121    pair of registers from a 64-bit parameter
2122-------------------------------------------------*/
2123
2124static void emit_and_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reglo, UINT8 reghi, const drcuml_parameter *param, const drcuml_instruction *inst)
2125{
2126   int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0);
2127   if (param->type == DRCUML_PTYPE_MEMORY)
2128   {
2129      emit_and_r32_m32(dst, reglo, MABS(param->value));                        // and   reglo,[param]
2130      if (saveflags) emit_pushf(dst);                                       // pushf
2131      emit_and_r32_m32(dst, reghi, MABS(param->value + 4));                     // and   reghi,[param]
2132   }
2133   else if (param->type == DRCUML_PTYPE_IMMEDIATE)
2134   {
2135      if (inst->condflags == 0 && (UINT32)param->value == 0xffffffff)
2136         /* skip */;
2137      else if (inst->condflags == 0 && (UINT32)param->value == 0)
2138         emit_xor_r32_r32(dst, reglo, reglo);                              // xor   reglo,reglo
2139      else
2140         emit_and_r32_imm(dst, reglo, param->value);                           // and   reglo,param
2141      if (saveflags) emit_pushf(dst);                                       // pushf
2142      if (inst->condflags == 0 && (UINT32)(param->value >> 32) == 0xffffffff)
2143         /* skip */;
2144      else if (inst->condflags == 0 && (UINT32)(param->value >> 32) == 0)
2145         emit_xor_r32_r32(dst, reghi, reghi);                              // xor   reghi,reghi
2146      else
2147         emit_and_r32_imm(dst, reghi, param->value >> 32);                     // and   reghi,param >> 32
2148   }
2149   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
2150   {
2151      emit_and_r32_r32(dst, reglo, param->value);                              // and   reglo,param
2152      if (saveflags) emit_pushf(dst);                                       // pushf
2153      emit_and_r32_m32(dst, reghi, MABS(drcbe->reghi[param->value]));               // and   reghi,reghi[param]
2154   }
2155   if (saveflags)
2156      emit_combine_z_flags(dst);
2157}
2158
2159
2160/*-------------------------------------------------
2161    emit_and_m64_p64 - and operation to a 64-bit
2162    memory location from a 64-bit parameter
2163-------------------------------------------------*/
2164
2165static void emit_and_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
2166{
2167   int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0);
2168   if (param->type == DRCUML_PTYPE_IMMEDIATE)
2169   {
2170      if (inst->condflags == 0 && (UINT32)param->value == 0xffffffff)
2171         /* skip */;
2172      else if (inst->condflags == 0 && (UINT32)param->value == 0)
2173         emit_mov_m32_imm(dst, MEMPARAMS, 0);                              // mov   [dest],0
2174      else
2175         emit_and_m32_imm(dst, MEMPARAMS, param->value);                        // and   [dest],param
2176      if (saveflags) emit_pushf(dst);                                       // pushf
2177      if (inst->condflags == 0 && (UINT32)(param->value >> 32) == 0xffffffff)
2178         /* skip */;
2179      else if (inst->condflags == 0 && (UINT32)(param->value >> 32) == 0)
2180         emit_mov_m32_imm(dst, MEMPARAMS + 4, 0);                           // mov   [dest+4],0
2181      else
2182         emit_and_m32_imm(dst, MEMPARAMS + 4, param->value >> 32);               // and   [dest+4],param >> 32
2183   }
2184   else
2185   {
2186      int reglo = (param->type == DRCUML_PTYPE_INT_REGISTER) ? param->value : REG_EAX;
2187      emit_mov_r64_p64(drcbe, dst, reglo, REG_EDX, param);                     // mov   edx:reglo,param
2188      emit_and_m32_r32(dst, MEMPARAMS, reglo);                              // and   [dest],reglo
2189      if (saveflags) emit_pushf(dst);                                       // pushf
2190      emit_and_m32_r32(dst, MEMPARAMS + 4, REG_EDX);                           // and   [dest+4],edx
2191   }
2192   if (saveflags)
2193      emit_combine_z_flags(dst);
2194}
2195
2196
2197/*-------------------------------------------------
2198    emit_test_r64_p64 - test operation to a 64-bit
2199    pair of registers from a 64-bit parameter
2200-------------------------------------------------*/
2201
2202static void emit_test_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reglo, UINT8 reghi, const drcuml_parameter *param, const drcuml_instruction *inst)
2203{
2204   int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0);
2205   if (param->type == DRCUML_PTYPE_MEMORY)
2206   {
2207      emit_test_m32_r32(dst, MABS(param->value), reglo);                        // test  [param],reglo
2208      if (saveflags) emit_pushf(dst);                                       // pushf
2209      emit_test_m32_r32(dst, MABS(param->value + 4), reghi);                     // test  [param],reghi
2210   }
2211   else if (param->type == DRCUML_PTYPE_IMMEDIATE)
2212   {
2213      emit_test_r32_imm(dst, reglo, param->value);                           // test  reglo,param
2214      if (saveflags) emit_pushf(dst);                                       // pushf
2215      emit_test_r32_imm(dst, reghi, param->value >> 32);                        // test  reghi,param >> 32
2216   }
2217   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
2218   {
2219      emit_test_r32_r32(dst, reglo, param->value);                           // test  reglo,param
2220      if (saveflags) emit_pushf(dst);                                       // pushf
2221      emit_test_m32_r32(dst, MABS(drcbe->reghi[param->value]), reghi);            // test  reghi[param],reghi
2222   }
2223   if (saveflags)
2224      emit_combine_z_flags(dst);
2225}
2226
2227
2228/*-------------------------------------------------
2229    emit_test_m64_p64 - test operation to a 64-bit
2230    memory location from a 64-bit parameter
2231-------------------------------------------------*/
2232
2233static void emit_test_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
2234{
2235   int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0);
2236   if (param->type == DRCUML_PTYPE_IMMEDIATE)
2237   {
2238      emit_test_m32_imm(dst, MEMPARAMS, param->value);                        // test   [dest],param
2239      if (saveflags) emit_pushf(dst);                                       // pushf
2240      emit_test_m32_imm(dst, MEMPARAMS + 4, param->value >> 32);                  // test   [dest+4],param >> 32
2241   }
2242   else
2243   {
2244      int reglo = (param->type == DRCUML_PTYPE_INT_REGISTER) ? param->value : REG_EAX;
2245      emit_mov_r64_p64(drcbe, dst, reglo, REG_EDX, param);                     // mov   edx:reglo,param
2246      emit_test_m32_r32(dst, MEMPARAMS, reglo);                              // test  [dest],reglo
2247      if (saveflags) emit_pushf(dst);                                       // pushf
2248      emit_test_m32_r32(dst, MEMPARAMS + 4, REG_EDX);                           // test  [dest+4],edx
2249   }
2250   if (saveflags)
2251      emit_combine_z_flags(dst);
2252}
2253
2254
2255/*-------------------------------------------------
2256    emit_or_r64_p64 - or operation to a 64-bit
2257    pair of registers from a 64-bit parameter
2258-------------------------------------------------*/
2259
2260static void emit_or_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reglo, UINT8 reghi, const drcuml_parameter *param, const drcuml_instruction *inst)
2261{
2262   int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0);
2263   if (param->type == DRCUML_PTYPE_MEMORY)
2264   {
2265      emit_or_r32_m32(dst, reglo, MABS(param->value));                        // or    reglo,[param]
2266      if (saveflags) emit_pushf(dst);                                       // pushf
2267      emit_or_r32_m32(dst, reghi, MABS(param->value + 4));                     // or    reghi,[param]
2268   }
2269   else if (param->type == DRCUML_PTYPE_IMMEDIATE)
2270   {
2271      if (inst->condflags == 0 && (UINT32)param->value == 0)
2272         /* skip */;
2273      else if (inst->condflags == 0 && (UINT32)param->value == 0xffffffff)
2274         emit_mov_r32_imm(dst, reglo, -1);                                 // mov   reglo,-1
2275      else
2276         emit_or_r32_imm(dst, reglo, param->value);                           // or    reglo,param
2277      if (saveflags) emit_pushf(dst);                                       // pushf
2278      if (inst->condflags == 0 && (UINT32)(param->value >> 32) == 0)
2279         /* skip */;
2280      else if (inst->condflags == 0 && (UINT32)(param->value >> 32) == 0xffffffff)
2281         emit_mov_r32_imm(dst, reghi, -1);                                 // mov   reghi,-1
2282      else
2283         emit_or_r32_imm(dst, reghi, param->value >> 32);                     // or    reghi,param >> 32
2284   }
2285   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
2286   {
2287      emit_or_r32_r32(dst, reglo, param->value);                              // or    reglo,param
2288      if (saveflags) emit_pushf(dst);                                       // pushf
2289      emit_or_r32_m32(dst, reghi, MABS(drcbe->reghi[param->value]));               // or    reghi,reghi[param]
2290   }
2291   if (saveflags)
2292      emit_combine_z_flags(dst);
2293}
2294
2295
2296/*-------------------------------------------------
2297    emit_or_m64_p64 - or operation to a 64-bit
2298    memory location from a 64-bit parameter
2299-------------------------------------------------*/
2300
2301static void emit_or_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
2302{
2303   int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0);
2304   if (param->type == DRCUML_PTYPE_IMMEDIATE)
2305   {
2306      if (inst->condflags == 0 && (UINT32)param->value == 0)
2307         /* skip */;
2308      else if (inst->condflags == 0 && (UINT32)param->value == 0xffffffff)
2309         emit_mov_m32_imm(dst, MEMPARAMS, -1);                              // mov   [dest],-1
2310      else
2311         emit_or_m32_imm(dst, MEMPARAMS, param->value);                        // or    [dest],param
2312      if (saveflags) emit_pushf(dst);                                       // pushf
2313      if (inst->condflags == 0 && (UINT32)(param->value >> 32) == 0)
2314         /* skip */;
2315      else if (inst->condflags == 0 && (UINT32)(param->value >> 32) == 0xffffffff)
2316         emit_mov_m32_imm(dst, MEMPARAMS + 4, -1);                           // mov   [dest+4],-1
2317      else
2318         emit_or_m32_imm(dst, MEMPARAMS + 4, param->value >> 32);               // or    [dest+4],param >> 32
2319   }
2320   else
2321   {
2322      int reglo = (param->type == DRCUML_PTYPE_INT_REGISTER) ? param->value : REG_EAX;
2323      emit_mov_r64_p64(drcbe, dst, reglo, REG_EDX, param);                     // mov   edx:reglo,param
2324      emit_or_m32_r32(dst, MEMPARAMS, reglo);                                 // or    [dest],reglo
2325      if (saveflags) emit_pushf(dst);                                       // pushf
2326      emit_or_m32_r32(dst, MEMPARAMS + 4, REG_EDX);                           // or    [dest+4],edx
2327   }
2328   if (saveflags)
2329      emit_combine_z_flags(dst);
2330}
2331
2332
2333/*-------------------------------------------------
2334    emit_xor_r64_p64 - xor operation to a 64-bit
2335    pair of registers from a 64-bit parameter
2336-------------------------------------------------*/
2337
2338static void emit_xor_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reglo, UINT8 reghi, const drcuml_parameter *param, const drcuml_instruction *inst)
2339{
2340   int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0);
2341   if (param->type == DRCUML_PTYPE_MEMORY)
2342   {
2343      emit_xor_r32_m32(dst, reglo, MABS(param->value));                        // xor   reglo,[param]
2344      if (saveflags) emit_pushf(dst);                                       // pushf
2345      emit_xor_r32_m32(dst, reghi, MABS(param->value + 4));                     // xor   reghi,[param]
2346   }
2347   else if (param->type == DRCUML_PTYPE_IMMEDIATE)
2348   {
2349      if (inst->condflags == 0 && (UINT32)param->value == 0)
2350         /* skip */;
2351      else if (inst->condflags == 0 && (UINT32)param->value == 0xffffffff)
2352         emit_not_r32(dst, reglo);                                       // not   reglo
2353      else
2354         emit_xor_r32_imm(dst, reglo, param->value);                           // xor   reglo,param
2355      if (saveflags) emit_pushf(dst);                                       // pushf
2356      if (inst->condflags == 0 && (UINT32)(param->value >> 32) == 0)
2357         /* skip */;
2358      else if (inst->condflags == 0 && (UINT32)(param->value >> 32) == 0xffffffff)
2359         emit_not_r32(dst, reghi);                                       // not   reghi
2360      else
2361         emit_xor_r32_imm(dst, reghi, param->value >> 32);                     // xor   reghi,param >> 32
2362   }
2363   else if (param->type == DRCUML_PTYPE_INT_REGISTER)
2364   {
2365      emit_xor_r32_r32(dst, reglo, param->value);                              // xor   reglo,param
2366      if (saveflags) emit_pushf(dst);                                       // pushf
2367      emit_xor_r32_m32(dst, reghi, MABS(drcbe->reghi[param->value]));               // xor   reghi,reghi[param]
2368   }
2369   if (saveflags)
2370      emit_combine_z_flags(dst);
2371}
2372
2373
2374/*-------------------------------------------------
2375    emit_xor_m64_p64 - xor operation to a 64-bit
2376    memory location from a 64-bit parameter
2377-------------------------------------------------*/
2378
2379static void emit_xor_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst)
2380{
2381   int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0);
2382   if (param->type == DRCUML_PTYPE_IMMEDIATE)
2383   {
2384      if (inst->condflags == 0 && (UINT32)param->value == 0)
2385         /* skip */;
2386      else if (inst->condflags == 0 && (UINT32)param->value == 0xffffffff)
2387         emit_not_m32(dst, MEMPARAMS);                                    // not   [dest]
2388      else
2389         emit_xor_m32_imm(dst, MEMPARAMS, param->value);                        // xor   [dest],param
2390      if (saveflags) emit_pushf(dst);                                       // pushf
2391      if (inst->condflags == 0 && (UINT32)(param->value >> 32) == 0)
2392         /* skip */;
2393      else if (inst->condflags == 0 && (UINT32)(param->value >> 32) == 0xffffffff)
2394         emit_not_m32(dst, MEMPARAMS + 4);                                 // not   [dest+4]
2395      else
2396         emit_xor_m32_imm(dst, MEMPARAMS + 4, param->value >> 32);               // xor   [dest+4],param >> 32
2397   }
2398   else
2399   {
2400      int reglo = (param->type == DRCUML_PTYPE_INT_REGISTER) ? param->value : REG_EAX;
2401      emit_mov_r64_p64(drcbe, dst, reglo, REG_EDX, param);                     // mov   edx:reglo,param
2402      emit_xor_m32_r32(dst, MEMPARAMS, reglo);                              // xor   [dest],reglo
2403      if (saveflags) emit_pushf(dst);                                       // pushf
2404      emit_xor_m32_r32(dst, MEMPARAMS + 4, REG_EDX);                           // xor   [dest+4],edx
2405   }
2406   if (saveflags)
2407      emit_combine_z_flags(dst);
2408}
2409
2410
2411/*-------------------------------------------------
2412    emit_shl_r64_p64 - shl operation to a 64-bit
2413    pair of registers from a 64-bit parameter
2414-------------------------------------------------*/
2415
2416static void emit_shl_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reglo, UINT8 reghi, const drcuml_parameter *param, const drcuml_instruction *inst)
2417{
2418   int saveflags = (inst->condflags != 0);
2419   if (param->type == DRCUML_PTYPE_IMMEDIATE)
2420   {
2421      int count = param->value & 63;
2422      if (inst->condflags != 0 && count == 0)
2423         /* skip */;
2424      else
2425      {
2426         while (count >= 32)
2427         {
2428            if (inst->condflags != 0)
2429            {
2430               emit_shld_r32_r32_imm(dst, reghi, reglo, 31);                  // shld  reghi,reglo,31
2431               emit_shl_r32_imm(dst, reglo, 31);                           // shl   reglo,31
2432               count -= 31;
2433            }
2434            else
2435            {
2436               emit_mov_r32_r32(dst, reghi, reglo);                        // mov   reghi,reglo
2437               emit_xor_r32_r32(dst, reglo, reglo);                        // xor   reglo,reglo
2438               count -= 32;
2439            }
2440         }
2441         if (inst->condflags != 0 || count > 0)
2442         {
2443            emit_shld_r32_r32_imm(dst, reghi, reglo, count);                  // shld  reghi,reglo,count
2444            if (saveflags) emit_pushf(dst);                                 // pushf
2445            emit_shl_r32_imm(dst, reglo, count);                           // shl   reglo,count
2446         }
2447      }
2448   }
2449   else
2450   {
2451      emit_link skip1, skip2;
2452      emit_mov_r32_p32(drcbe, dst, REG_ECX, param);                           // mov   ecx,param
2453      emit_test_r32_imm(dst, REG_ECX, 0x20);                                 // test  ecx,0x20
2454      emit_jcc_short_link(dst, COND_Z, &skip1);                              // jz    skip1
2455      if (inst->condflags != 0)
2456      {
2457         emit_sub_r32_imm(dst, REG_ECX, 31);                                 // sub   ecx,31
2458         emit_shld_r32_r32_imm(dst, reghi, reglo, 31);                        // shld  reghi,reglo,31
2459         emit_shl_r32_imm(dst, reglo, 31);                                 // shl   reglo,31
2460         emit_test_r32_imm(dst, REG_ECX, 0x20);                              // test  ecx,0x20
2461         emit_jcc_short_link(dst, COND_Z, &skip2);                           // jz    skip2
2462         emit_sub_r32_imm(dst, REG_ECX, 31);                                 // sub   ecx,31
2463         emit_shld_r32_r32_imm(dst, reghi, reglo, 31);                        // shld  reghi,reglo,31
2464         emit_shl_r32_imm(dst, reglo, 31);                                 // shl   reglo,31
2465         resolve_link(dst, &skip2);                                    // skip2:
2466      }
2467      else
2468      {
2469         emit_mov_r32_r32(dst, reghi, reglo);                              // mov   reghi,reglo
2470         emit_xor_r32_r32(dst, reglo, reglo);                              // xor   reglo,reglo
2471      }
2472      resolve_link(dst, &skip1);                                       // skip1:
2473      emit_shld_r32_r32_cl(dst, reghi, reglo);                              // shld  reghi,reglo,cl
2474      if (saveflags) emit_pushf(dst);                                       // pushf
2475      emit_shl_r32_cl(dst, reglo);                                       // shl   reglo,cl
2476   }
2477   if (saveflags)
2478      emit_combine_z_shl_flags(dst);
2479}
2480
2481
2482/*-------------------------------------------------
2483    emit_shr_r64_p64 - shr operation to a 64-bit
2484    pair of registers from a 64-bit parameter
2485-------------------------------------------------*/
2486
2487static void emit_shr_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reglo, UINT8 reghi, const drcuml_parameter *param, const drcuml_instruction *inst)
2488{
2489   int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0);
2490   if (param->type == DRCUML_PTYPE_IMMEDIATE)
2491   {
2492      int count = param->value & 63;
2493      if (inst->condflags != 0 && count == 0)
2494         /* skip */;
2495      else
2496      {
2497         while (count >= 32)
2498         {
2499            if (inst->condflags != 0)
2500            {
2501               emit_shrd_r32_r32_imm(dst, reglo, reghi, 31);                  // shrd  reglo,reghi,31
2502               emit_shr_r32_imm(dst, reghi, 31);                           // shr   reghi,31
2503               count -= 31;
2504            }
2505            else
2506            {
2507               emit_mov_r32_r32(dst, reglo, reghi);                        // mov   reglo,reghi
2508               emit_xor_r32_r32(dst, reghi, reghi);                        // xor   reghi,reghi
2509               count -= 32;
2510            }
2511         }
2512         if (inst->condflags != 0 || count > 0)
2513         {
2514            emit_shrd_r32_r32_imm(dst, reglo, reghi, count);                  // shrd  reglo,reghi,count
2515            if (saveflags) emit_pushf(dst);                                 // pushf
2516            emit_shr_r32_imm(dst, reghi, count);                           // shr   reghi,count
2517         }
2518      }
2519   }
2520   else
2521   {
2522      emit_link skip1, skip2;
2523      emit_mov_r32_p32(drcbe, dst, REG_ECX, param);                           // mov   ecx,param
2524      emit_test_r32_imm(dst, REG_ECX, 0x20);                                 // test  ecx,0x20
2525      emit_jcc_short_link(dst, COND_Z, &skip1);                              // jz    skip1
2526      if (inst->condflags != 0)
2527      {
2528         emit_sub_r32_imm(dst, REG_ECX, 31);                                 // sub   ecx,31
2529         emit_shrd_r32_r32_imm(dst, reglo, reghi, 31);                        // shrd  reglo,reghi,31
2530         emit_shr_r32_imm(dst, reghi, 31);                                 // shr   reghi,31
2531         emit_test_r32_imm(dst, REG_ECX, 0x20);                              // test  ecx,0x20
2532         emit_jcc_short_link(dst, COND_Z, &skip2);                           // jz    skip2
2533         emit_sub_r32_imm(dst, REG_ECX, 31);                                 // sub   ecx,31
2534         emit_shrd_r32_r32_imm(dst, reglo, reghi, 31);                        // shrd  reglo,reghi,31
2535         emit_shr_r32_imm(dst, reghi, 31);                                 // shr   reghi,31
2536         resolve_link(dst, &skip2);                                    // skip2:
2537      }
2538      else
2539      {
2540         emit_mov_r32_r32(dst, reglo, reghi);                              // mov   reglo,reghi
2541         emit_xor_r32_r32(dst, reghi, reghi);                              // xor   reghi,reghi
2542      }
2543      resolve_link(dst, &skip1);                                       // skip1:
2544      emit_shrd_r32_r32_cl(dst, reglo, reghi);                              // shrd  reglo,reghi,cl
2545      if (saveflags) emit_pushf(dst);                                       // pushf
2546      emit_shr_r32_cl(dst, reghi);                                       // shr   reghi,cl
2547   }
2548   if (saveflags)
2549      emit_combine_z_flags(dst);
2550}
2551
2552
2553/*-------------------------------------------------
2554    emit_sar_r64_p64 - sar operation to a 64-bit
2555    pair of registers from a 64-bit parameter
2556-------------------------------------------------*/
2557
2558static void emit_sar_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reglo, UINT8 reghi, const drcuml_parameter *param, const drcuml_instruction *inst)
2559{
2560   int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0);
2561   if (param->type == DRCUML_PTYPE_IMMEDIATE)
2562   {
2563      int count = param->value & 63;
2564      if (inst->condflags != 0 && count == 0)
2565         /* skip */;
2566      else
2567      {
2568         while (count >= 32)
2569         {
2570            if (inst->condflags != 0)
2571            {
2572               emit_shrd_r32_r32_imm(dst, reglo, reghi, 31);                  // shrd  reglo,reghi,31
2573               emit_sar_r32_imm(dst, reghi, 31);                           // sar   reghi,31
2574               count -= 31;
2575            }
2576            else
2577            {
2578               emit_mov_r32_r32(dst, reglo, reghi);                        // mov   reglo,reghi
2579               emit_sar_r32_imm(dst, reghi, 31);                           // sar   reghi,31
2580               count -= 32;
2581            }
2582         }
2583         if (inst->condflags != 0 || count > 0)
2584         {
2585            emit_shrd_r32_r32_imm(dst, reglo, reghi, count);                  // shrd  reglo,reghi,count
2586            if (saveflags) emit_pushf(dst);                                 // pushf
2587            emit_sar_r32_imm(dst, reghi, count);                           // sar   reghi,count
2588         }
2589      }
2590   }
2591   else
2592   {
2593      emit_link skip1, skip2;
2594      emit_mov_r32_p32(drcbe, dst, REG_ECX, param);                           // mov   ecx,param
2595      emit_test_r32_imm(dst, REG_ECX, 0x20);                                 // test  ecx,0x20
2596      emit_jcc_short_link(dst, COND_Z, &skip1);                              // jz    skip1
2597      if (inst->condflags != 0)
2598      {
2599         emit_sub_r32_imm(dst, REG_ECX, 31);                                 // sub   ecx,31
2600         emit_shrd_r32_r32_imm(dst, reglo, reghi, 31);                        // shrd  reglo,reghi,31
2601         emit_sar_r32_imm(dst, reghi, 31);                                 // sar   reghi,31
2602         emit_test_r32_imm(dst, REG_ECX, 0x20);                              // test  ecx,0x20
2603         emit_jcc_short_link(dst, COND_Z, &skip2);                           // jz    skip
2604         emit_sub_r32_imm(dst, REG_ECX, 31);                                 // sub   ecx,31
2605         emit_shrd_r32_r32_imm(dst, reglo, reghi, 31);                        // shrd  reglo,reghi,31
2606         emit_sar_r32_imm(dst, reghi, 31);                                 // sar   reghi,31
2607         resolve_link(dst, &skip2);                                    // skip2:
2608      }
2609      else
2610      {
2611         emit_mov_r32_r32(dst, reglo, reghi);                              // mov   reglo,reghi
2612         emit_sar_r32_imm(dst, reghi, 31);                                 // sar   reghi,31
2613      }
2614      resolve_link(dst, &skip1);                                       // skip1:
2615      emit_shrd_r32_r32_cl(dst, reglo, reghi);                              // shrd  reglo,reghi,cl
2616      if (saveflags) emit_pushf(dst);                                       // pushf
2617      emit_sar_r32_cl(dst, reghi);                                       // sar   reghi,cl
2618   }
2619   if (saveflags)
2620      emit_combine_z_flags(dst);
2621}
2622
2623
2624/*-------------------------------------------------
2625    emit_rol_r64_p64 - rol operation to a 64-bit
2626    pair of registers from a 64-bit parameter
2627-------------------------------------------------*/
2628
2629static void emit_rol_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reglo, UINT8 reghi, const drcuml_parameter *param, const drcuml_instruction *inst)
2630{
2631   int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0);
2632   if (param->type == DRCUML_PTYPE_IMMEDIATE)
2633   {
2634      int count = param->value & 63;
2635      if (inst->condflags != 0 && count == 0)
2636         /* skip */;
2637      else
2638      {
2639         while (count >= 32)
2640         {
2641            if (inst->condflags != 0)
2642            {
2643               emit_mov_r32_r32(dst, REG_ECX, reglo);                        // mov   ecx,reglo
2644               emit_shld_r32_r32_imm(dst, reglo, reghi, 31);                  // shld  reglo,reghi,31
2645               emit_shld_r32_r32_imm(dst, reghi, REG_ECX, 31);                  // shld  reghi,ecx,31
2646               count -= 31;
2647            }
2648            else
2649            {
2650               emit_xchg_r32_r32(dst, reghi, reglo);                        // xchg  reghi,reglo
2651               count -= 32;
2652            }
2653         }
2654         if (inst->condflags != 0 || count > 0)
2655         {
2656            emit_mov_r32_r32(dst, REG_ECX, reglo);                           // mov   ecx,reglo
2657            emit_shld_r32_r32_imm(dst, reglo, reghi, count);                  // shld  reglo,reghi,count
2658            if (saveflags) emit_pushf(dst);                                 // pushf
2659            emit_shld_r32_r32_imm(dst, reghi, REG_ECX, count);                  // shld  reghi,ecx,count
2660         }
2661      }
2662   }
2663   else
2664   {
2665      emit_link skip1, skip2;
2666      emit_mov_m32_r32(dst, MBD(REG_ESP, -8), REG_EBX);                        // mov   [esp-8],ebx
2667      emit_mov_r32_p32(drcbe, dst, REG_ECX, param);                           // mov   ecx,param
2668      emit_test_r32_imm(dst, REG_ECX, 0x20);                                 // test  ecx,0x20
2669      emit_jcc_short_link(dst, COND_Z, &skip1);                              // jz    skip1
2670      if (inst->condflags != 0)
2671      {
2672         emit_sub_r32_imm(dst, REG_ECX, 31);                                 // sub   ecx,31
2673         emit_mov_r32_r32(dst, REG_EBX, reglo);                              // mov   ebx,reglo
2674         emit_shld_r32_r32_imm(dst, reglo, reghi, 31);                        // shld  reglo,reghi,31
2675         emit_shld_r32_r32_imm(dst, reghi, REG_EBX, 31);                        // shld  reghi,ebx,31
2676         emit_test_r32_imm(dst, REG_ECX, 0x20);                              // test  ecx,0x20
2677         emit_jcc_short_link(dst, COND_Z, &skip2);                           // jz    skip2
2678         emit_sub_r32_imm(dst, REG_ECX, 31);                                 // sub   ecx,31
2679         emit_mov_r32_r32(dst, REG_EBX, reglo);                              // mov   ebx,reglo
2680         emit_shld_r32_r32_imm(dst, reglo, reghi, 31);                        // shld  reglo,reghi,31
2681         emit_shld_r32_r32_imm(dst, reghi, REG_EBX, 31);                        // shld  reghi,ebx,31
2682         resolve_link(dst, &skip2);                                    // skip2:
2683      }
2684      else
2685         emit_xchg_r32_r32(dst, reghi, reglo);                              // xchg  reghi,reglo
2686      resolve_link(dst, &skip1);                                       // skip1:
2687      emit_mov_r32_r32(dst, REG_EBX, reglo);                                 // mov   ebx,reglo
2688      emit_shld_r32_r32_cl(dst, reglo, reghi);                              // shld  reglo,reghi,cl
2689      if (saveflags) emit_pushf(dst);                                       // pushf
2690      emit_shld_r32_r32_cl(dst, reghi, REG_EBX);                              // shld  reghi,ebx,cl
2691      emit_mov_r32_m32(dst, MBD(REG_ESP, saveflags ? -4 : -8), REG_EBX);            // mov   ebx,[esp-8]
2692   }
2693   if (saveflags)
2694      emit_combine_z_flags(dst);
2695}
2696
2697
2698/*-------------------------------------------------
2699    emit_ror_r64_p64 - ror operation to a 64-bit
2700    pair of registers from a 64-bit parameter
2701-------------------------------------------------*/
2702
2703static void emit_ror_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reglo, UINT8 reghi, const drcuml_parameter *param, const drcuml_instruction *inst)
2704{
2705   int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0);
2706   if (param->type == DRCUML_PTYPE_IMMEDIATE)
2707   {
2708      int count = param->value & 63;
2709      if (inst->condflags != 0 && count == 0)
2710         /* skip */;
2711      else
2712      {
2713         while (count >= 32)
2714         {
2715            if (inst->condflags != 0)
2716            {
2717               emit_mov_r32_r32(dst, REG_ECX, reglo);                        // mov   ecx,reglo
2718               emit_shrd_r32_r32_imm(dst, reglo, reghi, 31);                  // shrd  reglo,reghi,31
2719               emit_shrd_r32_r32_imm(dst, reghi, REG_ECX, 31);                  // shrd  reghi,ecx,31
2720               count -= 31;
2721            }
2722            else
2723            {
2724               emit_xchg_r32_r32(dst, reghi, reglo);                        // xchg  reghi,reglo
2725               count -= 32;
2726            }
2727         }
2728         if (inst->condflags != 0 || count > 0)
2729         {
2730            emit_mov_r32_r32(dst, REG_ECX, reglo);                           // mov   ecx,reglo
2731            emit_shrd_r32_r32_imm(dst, reglo, reghi, count);                  // shrd  reglo,reghi,count
2732            if (saveflags) emit_pushf(dst);                                 // pushf
2733            emit_shrd_r32_r32_imm(dst, reghi, REG_ECX, count);                  // shrd  reghi,ecx,count
2734         }
2735      }
2736   }
2737   else
2738   {
2739      emit_link skip1, skip2;
2740      emit_mov_m32_r32(dst, MBD(REG_ESP, -8), REG_EBX);                        // mov   [esp-8],ebx
2741      emit_mov_r32_p32(drcbe, dst, REG_ECX, param);                           // mov   ecx,param
2742      emit_test_r32_imm(dst, REG_ECX, 0x20);                                 // test  ecx,0x20
2743      emit_jcc_short_link(dst, COND_Z, &skip1);                              // jz    skip1
2744      if (inst->condflags != 0)
2745      {
2746         emit_sub_r32_imm(dst, REG_ECX, 31);                                 // sub   ecx,31
2747         emit_mov_r32_r32(dst, REG_EBX, reglo);                              // mov   ebx,reglo
2748         emit_shrd_r32_r32_imm(dst, reglo, reghi, 31);                        // shrd  reglo,reghi,31
2749         emit_shrd_r32_r32_imm(dst, reghi, REG_EBX, 31);                        // shrd  reghi,ebx,31
2750         emit_test_r32_imm(dst, REG_ECX, 0x20);                              // test  ecx,0x20
2751         emit_jcc_short_link(dst, COND_Z, &skip2);                           // jz    skip2
2752         emit_sub_r32_imm(dst, REG_ECX, 31);                                 // sub   ecx,31
2753         emit_mov_r32_r32(dst, REG_EBX, reglo);                              // mov   ebx,reglo
2754         emit_shrd_r32_r32_imm(dst, reglo, reghi, 31);                        // shrd  reglo,reghi,31
2755         emit_shrd_r32_r32_imm(dst, reghi, REG_EBX, 31);                        // shrd  reghi,ebx,31
2756         resolve_link(dst, &skip2);                                    // skip2:
2757      }
2758      else
2759         emit_xchg_r32_r32(dst, reghi, reglo);                              // xchg  reghi,reglo
2760      resolve_link(dst, &skip1);                                       // skip1:
2761      emit_mov_r32_r32(dst, REG_EBX, reglo);                                 // mov   ebx,reglo
2762      emit_shrd_r32_r32_cl(dst, reglo, reghi);                              // shrd  reglo,reghi,cl
2763      if (saveflags) emit_pushf(dst);                                       // pushf
2764      emit_shrd_r32_r32_cl(dst, reghi, REG_EBX);                              // shrd  reghi,ebx,cl
2765      emit_mov_r32_m32(dst, MBD(REG_ESP, saveflags ? -4 : -8), REG_EBX);            // mov   ebx,[esp-8]
2766   }
2767   if (saveflags)
2768      emit_combine_z_flags(dst);
2769}
2770
2771
2772/*-------------------------------------------------
2773    emit_rcl_r64_p64 - rcl operation to a 64-bit
2774    pair of registers from a 64-bit parameter
2775-------------------------------------------------*/
2776
2777static void emit_rcl_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reglo, UINT8 reghi, const drcuml_parameter *param, const drcuml_instruction *inst)
2778{
2779   int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0);
2780   emit_link skipall, skiploop;
2781   x86code *loop;
2782
2783   emit_mov_r32_p32(drcbe, dst, REG_ECX, param);                              // mov   ecx,param
2784   if (!saveflags)
2785   {
2786      loop = *dst;                                                // loop:
2787      emit_jecxz_link(dst, &skipall);                                       // jecxz skipall
2788      emit_lea_r32_m32(dst, REG_ECX, MBD(REG_ECX, -1));                        // lea   ecx,[ecx-1]
2789      emit_rcl_r32_imm(dst, reglo, 1);                                    // rcl   reglo,1
2790      emit_rcl_r32_imm(dst, reghi, 1);                                    // rcl   reghi,1
2791      emit_jmp(dst, loop);                                             // jmp   loop
2792      resolve_link(dst, &skipall);                                    // skipall:
2793   }
2794   else
2795   {
2796      emit_jecxz_link(dst, &skipall);                                       // jecxz skipall
2797      emit_lea_r32_m32(dst, REG_ECX, MBD(REG_ECX, -1));                        // lea   ecx,[ecx-1]
2798      loop = *dst;                                                // loop:
2799      emit_jecxz_link(dst, &skiploop);                                    // jecxz skiploop
2800      emit_lea_r32_m32(dst, REG_ECX, MBD(REG_ECX, -1));                        // lea   ecx,[ecx-1]
2801      emit_rcl_r32_imm(dst, reglo, 1);                                    // rcl   reglo,1
2802      emit_rcl_r32_imm(dst, reghi, 1);                                    // rcl   reghi,1
2803      emit_jmp(dst, loop);                                             // jmp   loop
2804      resolve_link(dst, &skiploop);                                    // skiploop:
2805      emit_rcl_r32_imm(dst, reglo, 1);                                    // rcl   reglo,1
2806      emit_pushf(dst);                                                // pushf
2807      emit_rcl_r32_imm(dst, reghi, 1);                                    // rcl   reghi,1
2808      resolve_link(dst, &skipall);                                    // skipall:
2809      emit_combine_z_flags(dst);
2810   }
2811}
2812
2813
2814/*-------------------------------------------------
2815    emit_rcr_r64_p64 - rcr operation to a 64-bit
2816    pair of registers from a 64-bit parameter
2817-------------------------------------------------*/
2818
2819static void emit_rcr_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reglo, UINT8 reghi, const drcuml_parameter *param, const drcuml_instruction *inst)
2820{
2821   int saveflags = (inst->condflags != 0);
2822   emit_link skipall, skiploop;
2823   x86code *loop;
2824
2825   emit_mov_r32_p32(drcbe, dst, REG_ECX, param);                              // mov   ecx,param
2826   if (!saveflags)
2827   {
2828      loop = *dst;                                                // loop:
2829      emit_jecxz_link(dst, &skipall);                                       // jecxz skipall
2830      emit_lea_r32_m32(dst, REG_ECX, MBD(REG_ECX, -1));                        // lea   ecx,[ecx-1]
2831      emit_rcr_r32_imm(dst, reghi, 1);                                    // rcr   reghi,1
2832      emit_rcr_r32_imm(dst, reglo, 1);                                    // rcr   reglo,1
2833      emit_jmp(dst, loop);                                             // jmp   loop
2834      resolve_link(dst, &skipall);                                    // skipall:
2835   }
2836   else
2837   {
2838      emit_jecxz_link(dst, &skipall);                                       // jecxz skipall
2839      emit_lea_r32_m32(dst, REG_ECX, MBD(REG_ECX, -1));                        // lea   ecx,[ecx-1]
2840      loop = *dst;                                                // loop:
2841      emit_jecxz_link(dst, &skiploop);                                    // jecxz skiploop
2842      emit_lea_r32_m32(dst, REG_ECX, MBD(REG_ECX, -1));                        // lea   ecx,[ecx-1]
2843      emit_rcr_r32_imm(dst, reghi, 1);                                    // rcr   reghi,1
2844      emit_rcr_r32_imm(dst, reglo, 1);                                    // rcr   reglo,1
2845      emit_jmp(dst, loop);                                             // jmp   loop
2846      resolve_link(dst, &skiploop);                                    // skiploop:
2847      emit_rcr_r32_imm(dst, reghi, 1);                                    // rcr   reghi,1
2848      emit_pushf(dst);                                                // pushf
2849      emit_rcr_r32_imm(dst, reglo, 1);                                    // rcr   reglo,1
2850      resolve_link(dst, &skipall);                                    // skipall:
2851      emit_combine_z_shl_flags(dst);
2852   }
2853}
2854
2855
2856
2857/***************************************************************************
2858    EMITTERS FOR FLOATING POINT
2859***************************************************************************/
2860
2861/*-------------------------------------------------
2862    emit_fld_p - load a floating point parameter
2863    onto the stack
2864-------------------------------------------------*/
2865
2866static void emit_fld_p(x86code **dst, int size, const drcuml_parameter *param)
2867{
2868   assert(param->type == DRCUML_PTYPE_MEMORY);
2869   assert(size == 4 || size == 8);
2870   if (size == 4)
2871      emit_fld_m32(dst, MABS(param->value));
2872   else if (size == 8)
2873      emit_fld_m64(dst, MABS(param->value));
2874}
2875
2876
2877/*-------------------------------------------------
2878    emit_fstp_p - store a floating point parameter
2879    from the stack and pop it
2880-------------------------------------------------*/
2881
2882static void emit_fstp_p(x86code **dst, int size, const drcuml_parameter *param)
2883{
2884   assert(param->type == DRCUML_PTYPE_MEMORY);
2885   assert(size == 4 || size == 8);
2886   if (size == 4)
2887      emit_fstp_m32(dst, MABS(param->value));
2888   else if (size == 8)
2889      emit_fstp_m64(dst, MABS(param->value));
2890}
2891
2892
2893
2894/***************************************************************************
2895    HELPERS TO CONVERT OTHER OPERATIONS TO MOVES
2896***************************************************************************/
2897
2898/*-------------------------------------------------
2899    convert_to_mov_imm - convert an instruction
2900    into a mov immediate
2901-------------------------------------------------*/
2902
2903static x86code *convert_to_mov_imm(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst, const drcuml_parameter *dstp, UINT64 imm)
2904{
2905   drcuml_instruction temp = *inst;
2906   temp.numparams = 2;
2907   temp.param[0] = *dstp;
2908   temp.param[1].type = DRCUML_PTYPE_IMMEDIATE;
2909   temp.param[1].value = imm;
2910   return op_mov(drcbe, dst, &temp);
2911}
2912
2913
2914/*-------------------------------------------------
2915    convert_to_mov_src1 - convert an instruction
2916    into a mov src1
2917-------------------------------------------------*/
2918
2919static x86code *convert_to_mov_src1(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst, const drcuml_parameter *dstp, const drcuml_parameter *srcp)
2920{
2921   drcuml_instruction temp = *inst;
2922   temp.numparams = 2;
2923   temp.param[0] = *dstp;
2924   temp.param[1] = *srcp;
2925   return op_mov(drcbe, dst, &temp);
2926}
2927
2928
2929
2930/***************************************************************************
2931    OUT-OF-BAND CODE FIXUP CALLBACKS
2932***************************************************************************/
2933
2934/*-------------------------------------------------
2935    fixup_label - callback to fixup forward-
2936    referenced labels
2937-------------------------------------------------*/
2938
2939static void fixup_label(void *parameter, drccodeptr labelcodeptr)
2940{
2941   drccodeptr src = parameter;
2942
2943   /* find the end of the instruction */
2944   if (src[0] == 0xe3)
2945   {
2946      src += 1 + 1;
2947      src[-1] = labelcodeptr - src;
2948   }
2949   else if (src[0] == 0xe9)
2950   {
2951      src += 1 + 4;
2952      ((UINT32 *)src)[-1] = labelcodeptr - src;
2953   }
2954   else if (src[0] == 0x0f && (src[1] & 0xf0) == 0x80)
2955   {
2956      src += 2 + 4;
2957      ((UINT32 *)src)[-1] = labelcodeptr - src;
2958   }
2959   else
2960      fatalerror("fixup_label called with invalid jmp source!");
2961}
2962
2963
2964/*-------------------------------------------------
2965    fixup_exception - callback to perform cleanup
2966    and jump to an exception handler
2967-------------------------------------------------*/
2968
2969static void fixup_exception(drccodeptr *codeptr, void *param1, void *param2, void *param3)
2970{
2971   drcuml_parameter handp, exp;
2972   drcbe_state *drcbe = param1;
2973   drccodeptr src = param2;
2974   const drcuml_instruction *inst = param3;
2975   drccodeptr dst = *codeptr;
2976   drccodeptr *targetptr;
2977
2978   /* normalize parameters */
2979   param_normalize_2(drcbe, inst, &handp, PTYPE_M, &exp, PTYPE_MRI);
2980
2981   /* look up the handle target */
2982   targetptr = drcuml_handle_codeptr_addr((drcuml_codehandle *)(FPTR)handp.value);
2983
2984   /* first fixup the jump to get us here */
2985   ((UINT32 *)src)[-1] = dst - src;
2986
2987   /* then store the exception parameter */
2988   emit_mov_m32_p32(drcbe, &dst, MABS(&drcbe->state.exp), &exp);                  // mov   [exp],exp
2989
2990   /* push the original return address on the stack */
2991   emit_push_imm(&dst, (FPTR)src);                                          // push  <return>
2992   if (*targetptr != NULL)
2993      emit_jmp(&dst, *targetptr);                                          // jmp   *targetptr
2994   else
2995      emit_jmp_m32(&dst, MABS(targetptr));                                 // jmp   [targetptr]
2996
2997   *codeptr = dst;
2998}
2999
3000
3001
3002/***************************************************************************
3003    DEBUG HELPERS
3004***************************************************************************/
3005
3006/*-------------------------------------------------
3007    debug_log_hashjmp - callback to handle
3008    logging of hashjmps
3009-------------------------------------------------*/
3010
3011#if LOG_HASHJMPS
3012static void debug_log_hashjmp(int mode, offs_t pc)
3013{
3014   printf("mode=%d PC=%08X\n", mode, pc);
3015}
3016#endif
3017
3018
3019
3020/***************************************************************************
3021    COMPILE-TIME OPCODES
3022***************************************************************************/
3023
3024/*-------------------------------------------------
3025    op_handle - process a HANDLE opcode
3026-------------------------------------------------*/
3027
3028static x86code *op_handle(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3029{
3030   assert(inst->numparams == 1);
3031   assert(inst->param[0].type == DRCUML_PTYPE_MEMORY);
3032
3033   /* register the current pointer for the handle */
3034   drcuml_handle_set_codeptr((drcuml_codehandle *)(FPTR)inst->param[0].value, dst);
3035   return dst;
3036}
3037
3038
3039/*-------------------------------------------------
3040    op_hash - process a HASH opcode
3041-------------------------------------------------*/
3042
3043static x86code *op_hash(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3044{
3045   assert(inst->numparams == 2);
3046   assert(inst->param[0].type == DRCUML_PTYPE_IMMEDIATE);
3047   assert(inst->param[1].type == DRCUML_PTYPE_IMMEDIATE);
3048
3049   /* register the current pointer for the mode/PC */
3050   drchash_set_codeptr(drcbe->hash, inst->param[0].value, inst->param[1].value, dst);
3051   return dst;
3052}
3053
3054
3055/*-------------------------------------------------
3056    op_label - process a LABEL opcode
3057-------------------------------------------------*/
3058
3059static x86code *op_label(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3060{
3061   assert(inst->numparams == 1);
3062   assert(inst->param[0].type == DRCUML_PTYPE_IMMEDIATE);
3063
3064   /* register the current pointer for the label */
3065   drclabel_set_codeptr(drcbe->labels, inst->param[0].value, dst);
3066   return dst;
3067}
3068
3069
3070/*-------------------------------------------------
3071    op_comment - process a COMMENT opcode
3072-------------------------------------------------*/
3073
3074static x86code *op_comment(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3075{
3076   assert(inst->numparams == 1);
3077   assert(inst->param[0].type == DRCUML_PTYPE_MEMORY);
3078
3079   /* do nothing */
3080   return dst;
3081}
3082
3083
3084/*-------------------------------------------------
3085    op_mapvar - process a MAPVAR opcode
3086-------------------------------------------------*/
3087
3088static x86code *op_mapvar(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3089{
3090   assert(inst->numparams == 2);
3091   assert(inst->param[0].type == DRCUML_PTYPE_MAPVAR);
3092   assert(inst->param[1].type == DRCUML_PTYPE_IMMEDIATE);
3093
3094   /* set the value of the specified mapvar */
3095   drcmap_set_value(drcbe->map, dst, inst->param[0].value, inst->param[1].value);
3096   return dst;
3097}
3098
3099
3100
3101/***************************************************************************
3102    CONTROL FLOW OPCODES
3103***************************************************************************/
3104
3105/*-------------------------------------------------
3106    op_debug - process a DEBUG opcode
3107-------------------------------------------------*/
3108
3109static x86code *op_debug(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3110{
3111#ifdef ENABLE_DEBUGGER
3112   if (Machine->debug_mode)
3113   {
3114      drcuml_parameter pcp;
3115
3116      /* validate instruction */
3117      assert(inst->size == 4);
3118      assert(inst->condflags == DRCUML_COND_ALWAYS);
3119
3120      /* normalize parameters */
3121      param_normalize_1(drcbe, inst, &pcp, PTYPE_MRI);
3122
3123      /* push the parameter */
3124      emit_push_p32(drcbe, &dst, &pcp);                                    // push  pcp
3125      emit_call(&dst, (x86code *)mame_debug_hook);                           // call  mame_debug_hook
3126      emit_add_r32_imm(&dst, REG_ESP, 4);                                    // add   esp,4
3127   }
3128#endif
3129
3130   return dst;
3131}
3132
3133
3134/*-------------------------------------------------
3135    op_exit - process an EXIT opcode
3136-------------------------------------------------*/
3137
3138static x86code *op_exit(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3139{
3140   drcuml_parameter retp;
3141
3142   /* validate instruction */
3143   assert(inst->size == 4);
3144   assert(inst->condflags == DRCUML_COND_ALWAYS || (inst->condflags >= COND_Z && inst->condflags < DRCUML_COND_MAX));
3145
3146   /* normalize parameters */
3147   param_normalize_1(drcbe, inst, &retp, PTYPE_MRI);
3148
3149   /* load the parameter into EAX */
3150   emit_mov_r32_p32(drcbe, &dst, REG_EAX, &retp);                              // mov   eax,retp
3151   if (inst->condflags == DRCUML_COND_ALWAYS)
3152      emit_jmp(&dst, drcbe->exit);                                       // jmp   exit
3153   else
3154      emit_jcc(&dst, X86_CONDITION(inst->condflags), drcbe->exit);               // jcc   exit
3155
3156   return dst;
3157}
3158
3159
3160/*-------------------------------------------------
3161    op_hashjmp - process a HASHJMP opcode
3162-------------------------------------------------*/
3163
3164static x86code *op_hashjmp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3165{
3166   drcuml_parameter modep, pcp, exp;
3167
3168   /* validate instruction */
3169   assert(inst->size == 4);
3170   assert(inst->condflags == DRCUML_COND_ALWAYS);
3171
3172   /* normalize parameters */
3173   param_normalize_3(drcbe, inst, &modep, PTYPE_MRI, &pcp, PTYPE_MRI, &exp, PTYPE_M);
3174
3175#if LOG_HASHJMPS
3176   emit_push_p32(drcbe, &dst, &pcp);
3177   emit_push_p32(drcbe, &dst, &modep);
3178   emit_call(&dst, (x86code *)debug_log_hashjmp);
3179   emit_add_r32_imm(&dst, REG_ESP, 8);
3180#endif
3181
3182   /* load the stack base one word early so we end up at the right spot after our call below */
3183   emit_mov_r32_m32(&dst, REG_ESP, MABS(&drcbe->hashstacksave));                  // mov   esp,[hashstacksave]
3184
3185   /* fixed mode cases */
3186   if (modep.type == DRCUML_PTYPE_IMMEDIATE)
3187   {
3188      /* a straight immediate jump is direct, though we need the PC in EAX in case of failure */
3189      if (pcp.type == DRCUML_PTYPE_IMMEDIATE)
3190      {
3191         UINT32 l1val = (pcp.value >> drcbe->hash->l1shift) & drcbe->hash->l1mask;
3192         UINT32 l2val = (pcp.value >> drcbe->hash->l2shift) & drcbe->hash->l2mask;
3193         emit_call_m32(&dst, MABS(&drcbe->hash->base[modep.value][l1val][l2val]));   // call  hash[modep][l1val][l2val]
3194      }
3195
3196      /* a fixed mode but variable PC */
3197      else
3198      {
3199         emit_mov_r32_p32(drcbe, &dst, REG_EAX, &pcp);                        // mov   eax,pcp
3200         emit_mov_r32_r32(&dst, REG_EDX, REG_EAX);                           // mov   edx,eax
3201         emit_shr_r32_imm(&dst, REG_EDX, drcbe->hash->l1shift);                  // shr   edx,l1shift
3202         emit_and_r32_imm(&dst, REG_EAX, drcbe->hash->l2mask << drcbe->hash->l2shift);// and  eax,l2mask << l2shift
3203         emit_mov_r32_m32(&dst, REG_EDX, MISD(REG_EDX, 4, &drcbe->hash->base[modep.value][0]));
3204                                                                  // mov   edx,hash[modep+edx*4]
3205         emit_call_m32(&dst, MBISD(REG_EDX, REG_EAX, 4 >> drcbe->hash->l2shift, 0));   // call  [edx+eax*shift]
3206      }
3207   }
3208   else
3209   {
3210      /* variable mode */
3211      int modereg = param_select_register(REG_ECX, &modep, NULL);
3212      emit_mov_r32_p32(drcbe, &dst, modereg, &modep);                           // mov   modereg,modep
3213      emit_mov_r32_m32(&dst, REG_ECX, MISD(modereg, 4, &drcbe->hash->base[0]));      // mov   ecx,hash[modereg*4]
3214
3215      /* fixed PC */
3216      if (pcp.type == DRCUML_PTYPE_IMMEDIATE)
3217      {
3218         UINT32 l1val = (pcp.value >> drcbe->hash->l1shift) & drcbe->hash->l1mask;
3219         UINT32 l2val = (pcp.value >> drcbe->hash->l2shift) & drcbe->hash->l2mask;
3220         emit_mov_r32_m32(&dst, REG_EDX, MBD(REG_ECX, l1val*4));                  // mov   edx,[ecx+l1val*4]
3221         emit_call_m32(&dst, MBD(REG_EDX, l2val*4));                           // call  [l2val*4]
3222      }
3223
3224      /* variable PC */
3225      else
3226      {
3227         emit_mov_r32_p32(drcbe, &dst, REG_EAX, &pcp);                        // mov   eax,pcp
3228         emit_mov_r32_r32(&dst, REG_EDX, REG_EAX);                           // mov   edx,eax
3229         emit_shr_r32_imm(&dst, REG_EDX, drcbe->hash->l1shift);                  // shr   edx,l1shift
3230         emit_mov_r32_m32(&dst, REG_EDX, MBISD(REG_ECX, REG_EDX, 4, 0));            // mov   edx,[ecx+edx*4]
3231         emit_and_r32_imm(&dst, REG_EAX, drcbe->hash->l2mask << drcbe->hash->l2shift);// and  eax,l2mask << l2shift
3232         emit_call_m32(&dst, MBISD(REG_EDX, REG_EAX, 4 >> drcbe->hash->l2shift, 0));   // call  [edx+eax*shift]
3233      }
3234   }
3235
3236   /* in all cases, if there is no code, we return here to generate the exception */
3237   emit_mov_m32_p32(drcbe, &dst, MABS(&drcbe->state.exp.l), &pcp);                  // mov   [exp],param
3238   emit_sub_r32_imm(&dst, REG_ESP, 4);                                       // sub   esp,4
3239   emit_call_m32(&dst, MABS(exp.value));                                    // call  [exp]
3240
3241   return dst;
3242}
3243
3244
3245/*-------------------------------------------------
3246    op_jmp - process a JMP opcode
3247-------------------------------------------------*/
3248
3249static x86code *op_jmp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3250{
3251   drcuml_parameter labelp;
3252   x86code *jmptarget;
3253
3254   /* validate instruction */
3255   assert(inst->size == 4);
3256   assert(inst->condflags == DRCUML_COND_ALWAYS || (inst->condflags >= COND_Z && inst->condflags < DRCUML_COND_MAX));
3257
3258   /* normalize parameters */
3259   param_normalize_1(drcbe, inst, &labelp, PTYPE_I);
3260
3261   /* look up the jump target and jump there */
3262   jmptarget = (x86code *)drclabel_get_codeptr(drcbe->labels, labelp.value, fixup_label, dst);
3263   if (inst->condflags == DRCUML_COND_ALWAYS)
3264      emit_jmp(&dst, jmptarget);                                          // jmp   target
3265   else
3266      emit_jcc(&dst, X86_CONDITION(inst->condflags), jmptarget);                  // jcc   target
3267
3268   return dst;
3269}
3270
3271
3272/*-------------------------------------------------
3273    op_exh - process an EXH opcode
3274-------------------------------------------------*/
3275
3276static x86code *op_exh(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3277{
3278   drcuml_parameter handp, exp;
3279   drccodeptr *targetptr;
3280
3281   /* validate instruction */
3282   assert(inst->size == 4);
3283   assert(inst->condflags == DRCUML_COND_ALWAYS || (inst->condflags >= COND_Z && inst->condflags < DRCUML_COND_MAX));
3284
3285   /* normalize parameters */
3286   param_normalize_2(drcbe, inst, &handp, PTYPE_M, &exp, PTYPE_MRI);
3287
3288   /* look up the handle target */
3289   targetptr = drcuml_handle_codeptr_addr((drcuml_codehandle *)(FPTR)handp.value);
3290
3291   /* perform the exception processing inline if unconditional */
3292   if (inst->condflags == DRCUML_COND_ALWAYS)
3293   {
3294      emit_mov_m32_p32(drcbe, &dst, MABS(&drcbe->state.exp.l), &exp);               // mov   [exp],exp
3295      if (*targetptr != NULL)
3296         emit_call(&dst, *targetptr);                                    // call  *targetptr
3297      else
3298         emit_call_m32(&dst, MABS(targetptr));                              // call  [targetptr]
3299   }
3300
3301   /* otherwise, jump to an out-of-band handler */
3302   else
3303   {
3304      emit_jcc(&dst, X86_CONDITION(inst->condflags), 0);                        // jcc   exception
3305      drccache_request_oob_codegen(drcbe->cache, fixup_exception, drcbe, dst, (void *)inst);
3306   }
3307   return dst;
3308}
3309
3310
3311/*-------------------------------------------------
3312    op_callh - process a CALLH opcode
3313-------------------------------------------------*/
3314
3315static x86code *op_callh(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3316{
3317   drcuml_parameter handp;
3318   drccodeptr *targetptr;
3319   emit_link skip = { 0 };
3320
3321   /* validate instruction */
3322   assert(inst->size == 4);
3323   assert(inst->condflags == DRCUML_COND_ALWAYS || (inst->condflags >= COND_Z && inst->condflags < DRCUML_COND_MAX));
3324
3325   /* normalize parameters */
3326   param_normalize_1(drcbe, inst, &handp, PTYPE_M);
3327
3328   /* look up the handle target */
3329   targetptr = drcuml_handle_codeptr_addr((drcuml_codehandle *)(FPTR)handp.value);
3330
3331   /* skip if conditional */
3332   if (inst->condflags != DRCUML_COND_ALWAYS)
3333      emit_jcc_short_link(&dst, X86_NOT_CONDITION(inst->condflags), &skip);         // jcc   skip
3334
3335   /* jump through the handle; directly if a normal jump */
3336   if (*targetptr != NULL)
3337      emit_call(&dst, *targetptr);                                       // call  *targetptr
3338   else
3339      emit_call_m32(&dst, MABS(targetptr));                                 // call  [targetptr]
3340
3341   /* resolve the conditional link */
3342   if (inst->condflags != DRCUML_COND_ALWAYS)
3343      resolve_link(&dst, &skip);                                       // skip:
3344   return dst;
3345}
3346
3347
3348/*-------------------------------------------------
3349    op_ret - process a RET opcode
3350-------------------------------------------------*/
3351
3352static x86code *op_ret(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3353{
3354   emit_link skip = { 0 };
3355
3356   /* validate instruction */
3357   assert(inst->size == 4);
3358   assert(inst->condflags == DRCUML_COND_ALWAYS || (inst->condflags >= COND_Z && inst->condflags < DRCUML_COND_MAX));
3359   assert(inst->numparams == 0);
3360
3361   /* skip if conditional */
3362   if (inst->condflags != DRCUML_COND_ALWAYS)
3363      emit_jcc_short_link(&dst, X86_NOT_CONDITION(inst->condflags), &skip);         // jcc   skip
3364
3365   /* return */
3366   emit_ret(&dst);                                                      // ret
3367
3368   /* resolve the conditional link */
3369   if (inst->condflags != DRCUML_COND_ALWAYS)
3370      resolve_link(&dst, &skip);                                       // skip:
3371   return dst;
3372}
3373
3374
3375/*-------------------------------------------------
3376    op_callc - process a CALLC opcode
3377-------------------------------------------------*/
3378
3379static x86code *op_callc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3380{
3381   drcuml_parameter funcp, paramp;
3382   emit_link skip = { 0 };
3383
3384   /* validate instruction */
3385   assert(inst->size == 4);
3386   assert(inst->condflags == DRCUML_COND_ALWAYS || (inst->condflags >= COND_Z && inst->condflags < DRCUML_COND_MAX));
3387
3388   /* normalize parameters */
3389   param_normalize_2(drcbe, inst, &funcp, PTYPE_M, &paramp, PTYPE_M);
3390
3391   /* skip if conditional */
3392   if (inst->condflags != DRCUML_COND_ALWAYS)
3393      emit_jcc_short_link(&dst, X86_NOT_CONDITION(inst->condflags), &skip);         // jcc   skip
3394
3395   /* perform the call */
3396   emit_push_imm(&dst, paramp.value);                                       // push  paramp
3397   emit_call(&dst, (x86code *)(FPTR)funcp.value);                              // call  funcp
3398   emit_add_r32_imm(&dst, REG_ESP, 4);                                       // add   esp,4
3399
3400   /* resolve the conditional link */
3401   if (inst->condflags != DRCUML_COND_ALWAYS)
3402      resolve_link(&dst, &skip);                                       // skip:
3403   return dst;
3404}
3405
3406
3407/*-------------------------------------------------
3408    op_recover - process a RECOVER opcode
3409-------------------------------------------------*/
3410
3411static x86code *op_recover(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3412{
3413   drcuml_parameter dstp, mvparam;
3414
3415   /* validate instruction */
3416   assert(inst->size == 4);
3417   assert(inst->condflags == DRCUML_COND_ALWAYS);
3418
3419   /* normalize parameters */
3420   param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &mvparam, PTYPE_I);
3421
3422   /* call the recovery code */
3423   emit_mov_r32_m32(&dst, REG_EAX, MABS(&drcbe->stacksave));                     // mov   eax,stacksave
3424   emit_mov_r32_m32(&dst, REG_EAX, MBD(REG_EAX, -4));                           // mov   eax,[eax-4]
3425   emit_sub_r32_imm(&dst, REG_EAX, 1);                                       // sub   eax,1
3426   emit_push_imm(&dst, inst->param[1].value);                                 // push  param1
3427   emit_push_r32(&dst, REG_EAX);                                          // push  eax
3428   emit_push_imm(&dst, (FPTR)drcbe->map);                                    // push  drcbe->map
3429   emit_call(&dst, (x86code *)drcmap_get_value);                              // call  drcmap_get_value
3430   emit_add_r32_imm(&dst, REG_ESP, 12);                                    // add   esp,12
3431   emit_mov_p32_r32(drcbe, &dst, &dstp, REG_EAX);                              // mov   dstp,eax
3432
3433   return dst;
3434}
3435
3436
3437
3438/***************************************************************************
3439    INTERNAL REGISTER OPCODES
3440***************************************************************************/
3441
3442/*-------------------------------------------------
3443    op_setfmod - process a SETFMOD opcode
3444-------------------------------------------------*/
3445
3446static x86code *op_setfmod(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3447{
3448   drcuml_parameter srcp;
3449
3450   /* validate instruction */
3451   assert(inst->size == 4);
3452   assert(inst->condflags == DRCUML_COND_ALWAYS);
3453
3454   /* normalize parameters */
3455   param_normalize_1(drcbe, inst, &srcp, PTYPE_MRI);
3456
3457   /* immediate case */
3458   if (srcp.type == DRCUML_PTYPE_IMMEDIATE)
3459   {
3460      srcp.value &= 3;
3461      emit_mov_m8_imm(&dst, MABS(&drcbe->state.fmod), srcp.value);               // mov   [fmod],srcp
3462      emit_fldcw_m16(&dst, MABS(&fp_control[srcp.value]));                     // fldcw fp_control[srcp]
3463   }
3464
3465   /* register/memory case */
3466   else
3467   {
3468      emit_mov_r32_p32(drcbe, &dst, REG_EAX, &srcp);                           // mov   eax,srcp
3469      emit_and_r32_imm(&dst, REG_EAX, 3);                                    // and   eax,3
3470      emit_mov_m8_r8(&dst, MABS(&drcbe->state.fmod), REG_AL);                     // mov   [fmod],al
3471      emit_fldcw_m16(&dst, MISD(REG_EAX, 2, &fp_control[0]));                     // fldcw fp_control[eax]
3472   }
3473
3474   return dst;
3475}
3476
3477
3478/*-------------------------------------------------
3479    op_getfmod - process a GETFMOD opcode
3480-------------------------------------------------*/
3481
3482static x86code *op_getfmod(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3483{
3484   drcuml_parameter dstp;
3485
3486   /* validate instruction */
3487   assert(inst->size == 4);
3488   assert(inst->condflags == DRCUML_COND_ALWAYS);
3489
3490   /* normalize parameters */
3491   param_normalize_1(drcbe, inst, &dstp, PTYPE_MR);
3492
3493   /* fetch the current mode and store to the destination */
3494   if (dstp.type == DRCUML_PTYPE_INT_REGISTER)
3495      emit_movzx_r32_m8(&dst, dstp.value, MABS(&drcbe->state.fmod));               // movzx reg,[fmod]
3496   else
3497   {
3498      emit_movzx_r32_m8(&dst, REG_EAX, MABS(&drcbe->state.fmod));                  // movzx eax,[fmod]
3499      emit_mov_m32_r32(&dst, MABS(dstp.value), REG_EAX);                        // mov   [dstp],eax
3500   }
3501
3502   return dst;
3503}
3504
3505
3506/*-------------------------------------------------
3507    op_getexp - process a GETEXP opcode
3508-------------------------------------------------*/
3509
3510static x86code *op_getexp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3511{
3512   drcuml_parameter dstp;
3513
3514   /* validate instruction */
3515   assert(inst->size == 4);
3516   assert(inst->condflags == DRCUML_COND_ALWAYS);
3517
3518   /* normalize parameters */
3519   param_normalize_1(drcbe, inst, &dstp, PTYPE_MR);
3520
3521   /* fetch the exception parameter and store to the destination */
3522   if (dstp.type == DRCUML_PTYPE_INT_REGISTER)
3523      emit_mov_r32_m32(&dst, dstp.value, MABS(&drcbe->state.exp.l));               // mov   reg,[exp]
3524   else
3525   {
3526      emit_mov_r32_m32(&dst, REG_EAX, MABS(&drcbe->state.exp.l));                  // mov   eax,[exp]
3527      emit_mov_m32_r32(&dst, MABS(dstp.value), REG_EAX);                        // mov   [dstp],eax
3528   }
3529
3530   return dst;
3531}
3532
3533
3534
3535/***************************************************************************
3536    INTEGER OPERATIONS
3537***************************************************************************/
3538
3539/*-------------------------------------------------
3540    op_load1u - process a LOAD1U opcode
3541-------------------------------------------------*/
3542
3543static x86code *op_load1u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3544{
3545   drcuml_parameter dstp, basep, indp;
3546   int dstreg;
3547
3548   /* validate instruction */
3549   assert(inst->size == 4 || inst->size == 8);
3550   assert(inst->condflags == DRCUML_COND_ALWAYS);
3551
3552   /* normalize parameters */
3553   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &basep, PTYPE_M, &indp, PTYPE_MRI);
3554
3555   /* pick a target register for the general case */
3556   dstreg = param_select_register(REG_EAX, &dstp, NULL);
3557
3558   /* immediate index */
3559   if (indp.type == DRCUML_PTYPE_IMMEDIATE)
3560      emit_movzx_r32_m8(&dst, dstreg, MABS(basep.value + 1*indp.value));            // movzx dstreg,[basep + 1*indp]
3561
3562   /* other index */
3563   else
3564   {
3565      int indreg = param_select_register(REG_ECX, &indp, NULL);
3566      emit_mov_r32_p32(drcbe, &dst, indreg, &indp);
3567      emit_movzx_r32_m8(&dst, dstreg, MISD(indreg, 1, basep.value));               // movzx dstreg,[basep + 1*indp]
3568   }
3569
3570   /* store low 32 bits */
3571   emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                              // mov   dstp,dstreg
3572
3573   /* 64-bit form stores upper 32 bits */
3574   if (inst->size == 8)
3575   {
3576      /* general case */
3577      if (dstp.type == DRCUML_PTYPE_MEMORY)
3578         emit_mov_m32_imm(&dst, MABS(dstp.value + 4), 0);                     // mov   [dstp+4],0
3579      else if (dstp.type == DRCUML_PTYPE_INT_REGISTER)
3580         emit_mov_m32_imm(&dst, MABS(drcbe->reghi[dstp.value]), 0);               // mov   [reghi],0
3581   }
3582   return dst;
3583}
3584
3585
3586/*-------------------------------------------------
3587    op_load1s - process a LOAD1S opcode
3588-------------------------------------------------*/
3589
3590static x86code *op_load1s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3591{
3592   drcuml_parameter dstp, basep, indp;
3593   int dstreg;
3594
3595   /* validate instruction */
3596   assert(inst->size == 4 || inst->size == 8);
3597   assert(inst->condflags == DRCUML_COND_ALWAYS);
3598
3599   /* normalize parameters */
3600   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &basep, PTYPE_M, &indp, PTYPE_MRI);
3601
3602   /* pick a target register for the general case */
3603   dstreg = (inst->size == 8) ? REG_EAX : param_select_register(REG_EAX, &dstp, NULL);
3604
3605   /* immediate index */
3606   if (indp.type == DRCUML_PTYPE_IMMEDIATE)
3607      emit_movsx_r32_m8(&dst, dstreg, MABS(basep.value + 1*indp.value));            // movsx eax,[basep + 1*indp]
3608
3609   /* other index */
3610   else
3611   {
3612      int indreg = param_select_register(REG_ECX, &indp, NULL);
3613      emit_mov_r32_p32(drcbe, &dst, indreg, &indp);
3614      emit_movsx_r32_m8(&dst, dstreg, MISD(indreg, 1, basep.value));               // movsx eax,[basep + 1*indp]
3615   }
3616
3617   /* 32-bit form */
3618   if (inst->size == 4)
3619   {
3620      /* general case */
3621      emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                           // mov   dstp,dstreg
3622   }
3623
3624   /* 64-bit form */
3625   else
3626   {
3627      /* general case */
3628      emit_cdq(&dst);                                                   // cdq
3629      emit_mov_p64_r64(drcbe, &dst, &dstp, REG_EAX, REG_EDX);                     // mov   dstp,edx:eax
3630   }
3631   return dst;
3632}
3633
3634
3635/*-------------------------------------------------
3636    op_load2u - process a LOAD2U opcode
3637-------------------------------------------------*/
3638
3639static x86code *op_load2u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3640{
3641   drcuml_parameter dstp, basep, indp;
3642   int dstreg;
3643
3644   /* validate instruction */
3645   assert(inst->size == 4 || inst->size == 8);
3646   assert(inst->condflags == DRCUML_COND_ALWAYS);
3647
3648   /* normalize parameters */
3649   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &basep, PTYPE_M, &indp, PTYPE_MRI);
3650
3651   /* pick a target register for the general case */
3652   dstreg = param_select_register(REG_EAX, &dstp, NULL);
3653
3654   /* immediate index */
3655   if (indp.type == DRCUML_PTYPE_IMMEDIATE)
3656      emit_movzx_r32_m16(&dst, dstreg, MABS(basep.value + 2*indp.value));            // movzx dstreg,[basep + 2*indp]
3657
3658   /* other index */
3659   else
3660   {
3661      int indreg = param_select_register(REG_ECX, &indp, NULL);
3662      emit_mov_r32_p32(drcbe, &dst, indreg, &indp);
3663      emit_movzx_r32_m16(&dst, dstreg, MISD(indreg, 2, basep.value));               // movzx dstreg,[basep + 2*indp]
3664   }
3665
3666   /* store low 32 bits */
3667   emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                              // mov   dstp,dstreg
3668
3669   /* 64-bit form stores upper 32 bits */
3670   if (inst->size == 8)
3671   {
3672      /* general case */
3673      if (dstp.type == DRCUML_PTYPE_MEMORY)
3674         emit_mov_m32_imm(&dst, MABS(dstp.value + 4), 0);                     // mov   [dstp+4],0
3675      else if (dstp.type == DRCUML_PTYPE_INT_REGISTER)
3676         emit_mov_m32_imm(&dst, MABS(drcbe->reghi[dstp.value]), 0);               // mov   [reghi],0
3677   }
3678   return dst;
3679}
3680
3681
3682/*-------------------------------------------------
3683    op_load2s - process a LOAD2S opcode
3684-------------------------------------------------*/
3685
3686static x86code *op_load2s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3687{
3688   drcuml_parameter dstp, basep, indp;
3689   int dstreg;
3690
3691   /* validate instruction */
3692   assert(inst->size == 4 || inst->size == 8);
3693   assert(inst->condflags == DRCUML_COND_ALWAYS);
3694
3695   /* normalize parameters */
3696   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &basep, PTYPE_M, &indp, PTYPE_MRI);
3697
3698   /* pick a target register for the general case */
3699   dstreg = (inst->size == 8) ? REG_EAX : param_select_register(REG_EAX, &dstp, NULL);
3700
3701   /* immediate index */
3702   if (indp.type == DRCUML_PTYPE_IMMEDIATE)
3703      emit_movsx_r32_m16(&dst, dstreg, MABS(basep.value + 2*indp.value));            // movsx eax,[basep + 2*indp]
3704
3705   /* other index */
3706   else
3707   {
3708      int indreg = param_select_register(REG_ECX, &indp, NULL);
3709      emit_mov_r32_p32(drcbe, &dst, indreg, &indp);
3710      emit_movsx_r32_m16(&dst, dstreg, MISD(indreg, 2, basep.value));               // movsx eax,[basep + 2*indp]
3711   }
3712
3713   /* 32-bit form */
3714   if (inst->size == 4)
3715   {
3716      /* general case */
3717      emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                           // mov   dstp,dstreg
3718   }
3719
3720   /* 64-bit form */
3721   else
3722   {
3723      /* general case */
3724      emit_cdq(&dst);                                                   // cdq
3725      emit_mov_p64_r64(drcbe, &dst, &dstp, REG_EAX, REG_EDX);                     // mov   dstp,edx:eax
3726   }
3727   return dst;
3728}
3729
3730
3731/*-------------------------------------------------
3732    op_load4u - process a LOAD4U opcode
3733-------------------------------------------------*/
3734
3735static x86code *op_load4u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3736{
3737   drcuml_parameter dstp, basep, indp;
3738   int dstreg;
3739
3740   /* validate instruction */
3741   assert(inst->size == 4 || inst->size == 8);
3742   assert(inst->condflags == DRCUML_COND_ALWAYS);
3743
3744   /* normalize parameters */
3745   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &basep, PTYPE_M, &indp, PTYPE_MRI);
3746
3747   /* pick a target register for the general case */
3748   dstreg = param_select_register(REG_EAX, &dstp, NULL);
3749
3750   /* immediate index */
3751   if (indp.type == DRCUML_PTYPE_IMMEDIATE)
3752      emit_mov_r32_m32(&dst, dstreg, MABS(basep.value + 4*indp.value));            // mov   dstreg,[basep + 4*indp]
3753
3754   /* other index */
3755   else
3756   {
3757      int indreg = param_select_register(REG_ECX, &indp, NULL);
3758      emit_mov_r32_p32(drcbe, &dst, indreg, &indp);
3759      emit_mov_r32_m32(&dst, dstreg, MISD(indreg, 4, basep.value));               // mov   dstreg,[basep + 4*indp]
3760   }
3761
3762   /* store low 32 bits */
3763   emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                              // mov   dstp,dstreg
3764
3765   /* 64-bit form stores upper 32 bits */
3766   if (inst->size == 8)
3767   {
3768      /* general case */
3769      if (dstp.type == DRCUML_PTYPE_MEMORY)
3770         emit_mov_m32_imm(&dst, MABS(dstp.value + 4), 0);                     // mov   [dstp+4],0
3771      else if (dstp.type == DRCUML_PTYPE_INT_REGISTER)
3772         emit_mov_m32_imm(&dst, MABS(drcbe->reghi[dstp.value]), 0);               // mov   [reghi],0
3773   }
3774   return dst;
3775}
3776
3777
3778/*-------------------------------------------------
3779    op_load4s - process a LOAD4S opcode
3780-------------------------------------------------*/
3781
3782static x86code *op_load4s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3783{
3784   drcuml_parameter dstp, basep, indp;
3785
3786   /* validate instruction */
3787   assert(inst->size == 8);
3788   assert(inst->condflags == DRCUML_COND_ALWAYS);
3789
3790   /* normalize parameters */
3791   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &basep, PTYPE_M, &indp, PTYPE_MRI);
3792
3793   /* immediate index */
3794   if (indp.type == DRCUML_PTYPE_IMMEDIATE)
3795      emit_mov_r32_m32(&dst, REG_EAX, MABS(basep.value + 4*indp.value));            // mov   eax,[basep + 4*indp]
3796
3797   /* other index */
3798   else
3799   {
3800      int indreg = param_select_register(REG_ECX, &indp, NULL);
3801      emit_mov_r32_p32(drcbe, &dst, indreg, &indp);
3802      emit_mov_r32_m32(&dst, REG_EAX, MISD(indreg, 4, basep.value));               // movsx eax,[basep + 4*indp]
3803   }
3804
3805   /* general case */
3806   emit_cdq(&dst);                                                      // cdq
3807   emit_mov_p64_r64(drcbe, &dst, &dstp, REG_EAX, REG_EDX);                        // mov   dstp,edx:eax
3808
3809   return dst;
3810}
3811
3812
3813/*-------------------------------------------------
3814    op_load8u - process a LOAD8U opcode
3815-------------------------------------------------*/
3816
3817static x86code *op_load8u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3818{
3819   drcuml_parameter dstp, basep, indp;
3820   int dstreg;
3821
3822   /* validate instruction */
3823   assert(inst->size == 8);
3824   assert(inst->condflags == DRCUML_COND_ALWAYS);
3825
3826   /* normalize parameters */
3827   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &basep, PTYPE_M, &indp, PTYPE_MRI);
3828
3829   /* pick a target register for the general case */
3830   dstreg = param_select_register(REG_EAX, &dstp, NULL);
3831
3832   /* immediate index */
3833   if (indp.type == DRCUML_PTYPE_IMMEDIATE)
3834   {
3835      emit_mov_r32_m32(&dst, REG_EDX, MABS(basep.value + 8*indp.value + 4));         // mov   edx,[basep + 8*indp + 4]
3836      emit_mov_r32_m32(&dst, dstreg, MABS(basep.value + 8*indp.value));            // mov   dstreg,[basep + 8*indp]
3837   }
3838
3839   /* other index */
3840   else
3841   {
3842      int indreg = param_select_register(REG_ECX, &indp, NULL);
3843      emit_mov_r32_p32(drcbe, &dst, indreg, &indp);
3844      emit_mov_r32_m32(&dst, REG_EDX, MISD(indreg, 8, basep.value + 4));            // mov   edx,[basep + 8*indp + 4]
3845      emit_mov_r32_m32(&dst, dstreg, MISD(indreg, 8, basep.value));               // mov   dstreg,[basep + 8*indp]
3846   }
3847
3848   /* general case */
3849   emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg, REG_EDX);                        // mov   dstp,edx:dstreg
3850
3851   return dst;
3852}
3853
3854
3855/*-------------------------------------------------
3856    op_store1 - process a STORE1 opcode
3857-------------------------------------------------*/
3858
3859static x86code *op_store1(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3860{
3861   drcuml_parameter srcp, basep, indp;
3862   int srcreg;
3863
3864   /* validate instruction */
3865   assert(inst->size == 4 || inst->size == 8);
3866   assert(inst->condflags == DRCUML_COND_ALWAYS);
3867
3868   /* normalize parameters */
3869   param_normalize_3(drcbe, inst, &basep, PTYPE_M, &indp, PTYPE_MRI, &srcp, PTYPE_MRI);
3870
3871   /* pick a source register for the general case */
3872   srcreg = param_select_register(REG_EAX, &srcp, NULL);
3873   if (srcreg & 4)
3874      srcreg = REG_EAX;
3875
3876   /* degenerate case: constant index */
3877   if (indp.type == DRCUML_PTYPE_IMMEDIATE)
3878   {
3879      if (srcp.type == DRCUML_PTYPE_IMMEDIATE)
3880         emit_mov_m8_imm(&dst, MABS(basep.value + 1*indp.value), srcp.value);      // mov   [basep + 1*indp],srcp
3881      else
3882      {
3883         emit_mov_r32_p32(drcbe, &dst, srcreg, &srcp);                        // mov   srcreg,srcp
3884         emit_mov_m8_r8(&dst, MABS(basep.value + 1*indp.value), srcreg);            // mov   [basep + 1*indp],srcreg
3885      }
3886   }
3887
3888   /* normal case: variable index */
3889   else
3890   {
3891      int indreg = param_select_register(REG_ECX, &indp, NULL);
3892      emit_mov_r32_p32(drcbe, &dst, indreg, &indp);                           // mov   indreg,indp
3893      if (srcp.type == DRCUML_PTYPE_IMMEDIATE)
3894         emit_mov_m8_imm(&dst, MISD(indreg, 1, basep.value), srcp.value);         // mov   [basep + 1*ecx],srcp
3895      else
3896      {
3897         emit_mov_r32_p32(drcbe, &dst, srcreg, &srcp);                        // mov   srcreg,srcp
3898         emit_mov_m8_r8(&dst, MISD(indreg, 1, basep.value), srcreg);               // mov   [basep + 1*ecx],srcreg
3899      }
3900   }
3901   return dst;
3902}
3903
3904
3905/*-------------------------------------------------
3906    op_store2 - process a STORE2 opcode
3907-------------------------------------------------*/
3908
3909static x86code *op_store2(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3910{
3911   drcuml_parameter srcp, basep, indp;
3912   int srcreg;
3913
3914   /* validate instruction */
3915   assert(inst->size == 4 || inst->size == 8);
3916   assert(inst->condflags == DRCUML_COND_ALWAYS);
3917
3918   /* normalize parameters */
3919   param_normalize_3(drcbe, inst, &basep, PTYPE_M, &indp, PTYPE_MRI, &srcp, PTYPE_MRI);
3920
3921   /* pick a source register for the general case */
3922   srcreg = param_select_register(REG_EAX, &srcp, NULL);
3923
3924   /* degenerate case: constant index */
3925   if (indp.type == DRCUML_PTYPE_IMMEDIATE)
3926   {
3927      if (srcp.type == DRCUML_PTYPE_IMMEDIATE)
3928         emit_mov_m16_imm(&dst, MABS(basep.value + 2*indp.value), srcp.value);      // mov   [basep + 2*indp],srcp
3929      else
3930      {
3931         emit_mov_r32_p32(drcbe, &dst, srcreg, &srcp);                        // mov   srcreg,srcp
3932         emit_mov_m16_r16(&dst, MABS(basep.value + 2*indp.value), srcreg);         // mov   [basep + 2*indp],srcreg
3933      }
3934   }
3935
3936   /* normal case: variable index */
3937   else
3938   {
3939      int indreg = param_select_register(REG_ECX, &indp, NULL);
3940      emit_mov_r32_p32(drcbe, &dst, indreg, &indp);                           // mov   indreg,indp
3941      if (srcp.type == DRCUML_PTYPE_IMMEDIATE)
3942         emit_mov_m16_imm(&dst, MISD(indreg, 2, basep.value), srcp.value);         // mov   [basep + 2*ecx],srcp
3943      else
3944      {
3945         emit_mov_r32_p32(drcbe, &dst, srcreg, &srcp);                        // mov   srcreg,srcp
3946         emit_mov_m16_r16(&dst, MISD(indreg, 2, basep.value), srcreg);            // mov   [basep + 2*ecx],srcreg
3947      }
3948   }
3949   return dst;
3950}
3951
3952
3953/*-------------------------------------------------
3954    op_store4 - process a STORE4 opcode
3955-------------------------------------------------*/
3956
3957static x86code *op_store4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
3958{
3959   drcuml_parameter srcp, basep, indp;
3960   int srcreg;
3961
3962   /* validate instruction */
3963   assert(inst->size == 4 || inst->size == 8);
3964   assert(inst->condflags == DRCUML_COND_ALWAYS);
3965
3966   /* normalize parameters */
3967   param_normalize_3(drcbe, inst, &basep, PTYPE_M, &indp, PTYPE_MRI, &srcp, PTYPE_MRI);
3968
3969   /* pick a source register for the general case */
3970   srcreg = param_select_register(REG_EAX, &srcp, NULL);
3971
3972   /* degenerate case: constant index */
3973   if (indp.type == DRCUML_PTYPE_IMMEDIATE)
3974   {
3975      if (srcp.type == DRCUML_PTYPE_IMMEDIATE)
3976         emit_mov_m32_imm(&dst, MABS(basep.value + 4*indp.value), srcp.value);      // mov   [basep + 4*indp],srcp
3977      else
3978      {
3979         emit_mov_r32_p32(drcbe, &dst, srcreg, &srcp);                        // mov   srcreg,srcp
3980         emit_mov_m32_r32(&dst, MABS(basep.value + indp.value), srcreg);            // mov   [basep + 4*indp],srcreg
3981      }
3982   }
3983
3984   /* normal case: variable index */
3985   else
3986   {
3987      int indreg = param_select_register(REG_ECX, &indp, NULL);
3988      emit_mov_r32_p32(drcbe, &dst, indreg, &indp);                           // mov   indreg,indp
3989      if (srcp.type == DRCUML_PTYPE_IMMEDIATE)
3990         emit_mov_m32_imm(&dst, MISD(indreg, 4, basep.value), srcp.value);         // mov   [basep + 4*ecx],srcp
3991      else
3992      {
3993         emit_mov_r32_p32(drcbe, &dst, srcreg, &srcp);                        // mov   srcreg,srcp
3994         emit_mov_m32_r32(&dst, MISD(indreg, 4, basep.value), srcreg);            // mov   [basep + 4*ecx],srcreg
3995      }
3996   }
3997   return dst;
3998}
3999
4000
4001/*-------------------------------------------------
4002    op_store8 - process a STORE8 opcode
4003-------------------------------------------------*/
4004
4005static x86code *op_store8(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4006{
4007   drcuml_parameter srcp, basep, indp;
4008   int srcreg;
4009
4010   /* validate instruction */
4011   assert(inst->size == 8);
4012   assert(inst->condflags == DRCUML_COND_ALWAYS);
4013
4014   /* normalize parameters */
4015   param_normalize_3(drcbe, inst, &basep, PTYPE_M, &indp, PTYPE_MRI, &srcp, PTYPE_MRI);
4016
4017   /* pick a source register for the general case */
4018   srcreg = param_select_register(REG_EAX, &srcp, NULL);
4019
4020   /* degenerate case: constant index */
4021   if (indp.type == DRCUML_PTYPE_IMMEDIATE)
4022   {
4023      if (srcp.type == DRCUML_PTYPE_IMMEDIATE)
4024      {
4025         emit_mov_m32_imm(&dst, MABS(basep.value + 8*indp.value), srcp.value);      // mov   [basep + 8*indp],srcp
4026         emit_mov_m32_imm(&dst, MABS(basep.value + 4 + 8*indp.value), srcp.value >> 32);
4027                                                                  // mov   [basep + 4 + 8*indp],srcp >> 32
4028      }
4029      else
4030      {
4031         emit_mov_r64_p64(drcbe, &dst, srcreg, REG_EDX, &srcp);                  // mov   edx:srcreg,srcp
4032         emit_mov_m32_r32(&dst, MABS(basep.value + 8*indp.value), srcreg);         // mov   [basep + 8*indp],srcreg
4033         emit_mov_m32_r32(&dst, MABS(basep.value + 4 + 8*indp.value), REG_EDX);      // mov   [basep + 8*indp + 4],edx
4034      }
4035   }
4036
4037   /* normal case: variable index */
4038   else
4039   {
4040      int indreg = param_select_register(REG_ECX, &indp, NULL);
4041      emit_mov_r32_p32(drcbe, &dst, indreg, &indp);                           // mov   indreg,indp
4042      if (srcp.type == DRCUML_PTYPE_IMMEDIATE)
4043      {
4044         emit_mov_m32_imm(&dst, MISD(indreg, 8, basep.value), srcp.value);         // mov   [basep + 8*ecx],srcp
4045         emit_mov_m32_imm(&dst, MISD(indreg, 8, basep.value + 4), srcp.value >> 32);   // mov   [basep + 8*ecx + 4],srcp >> 32
4046      }
4047      else
4048      {
4049         emit_mov_r64_p64(drcbe, &dst, srcreg, REG_EDX, &srcp);                  // mov   edx:srcreg,srcp
4050         emit_mov_m32_r32(&dst, MISD(indreg, 8, basep.value), srcreg);            // mov   [basep + 8*ecx],srcreg
4051         emit_mov_m32_r32(&dst, MISD(indreg, 8, basep.value + 4), REG_EDX);         // mov   [basep + 4 + 8*ecx],edx
4052      }
4053   }
4054   return dst;
4055}
4056
4057
4058/*-------------------------------------------------
4059    op_read1u - process a READ1U opcode
4060-------------------------------------------------*/
4061
4062static x86code *op_read1u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4063{
4064   drcuml_parameter dstp, spacep, addrp;
4065   int dstreg;
4066
4067   /* validate instruction */
4068   assert(inst->size == 4 || inst->size == 8);
4069   assert(inst->condflags == DRCUML_COND_ALWAYS);
4070
4071   /* normalize parameters */
4072   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI);
4073
4074   /* pick a target register for the general case */
4075   dstreg = param_select_register(REG_EAX, &dstp, NULL);
4076
4077   /* set up a call to the read byte handler */
4078   emit_push_p32(drcbe, &dst, &addrp);                                       // push   addrp
4079   emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->read_byte);// call   read_byte
4080   emit_add_r32_imm(&dst, REG_ESP, 4);                                       // add    esp,4
4081   emit_movzx_r32_r8(&dst, dstreg, REG_AL);                                 // movzx  dstreg,al
4082
4083   /* store low 32 bits */
4084   emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                              // mov   dstp,dstreg
4085
4086   /* 64-bit form stores upper 32 bits */
4087   if (inst->size == 8)
4088   {
4089      /* general case */
4090      if (dstp.type == DRCUML_PTYPE_MEMORY)
4091         emit_mov_m32_imm(&dst, MABS(dstp.value + 4), 0);                     // mov   [dstp+4],0
4092      else if (dstp.type == DRCUML_PTYPE_INT_REGISTER)
4093         emit_mov_m32_imm(&dst, MABS(drcbe->reghi[dstp.value]), 0);               // mov   [reghi],0
4094   }
4095   return dst;
4096}
4097
4098
4099/*-------------------------------------------------
4100    op_read1s - process a READ1S opcode
4101-------------------------------------------------*/
4102
4103static x86code *op_read1s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4104{
4105   drcuml_parameter dstp, spacep, addrp;
4106   int dstreg;
4107
4108   /* validate instruction */
4109   assert(inst->size == 4 || inst->size == 8);
4110   assert(inst->condflags == DRCUML_COND_ALWAYS);
4111
4112   /* normalize parameters */
4113   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI);
4114
4115   /* pick a target register for the general case */
4116   dstreg = param_select_register(REG_EAX, &dstp, NULL);
4117
4118   /* set up a call to the read byte handler */
4119   emit_push_p32(drcbe, &dst, &addrp);                                       // push   addrp
4120   emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->read_byte);// call   read_byte
4121   emit_add_r32_imm(&dst, REG_ESP, 4);                                       // add    esp,4
4122   emit_movsx_r32_r8(&dst, dstreg, REG_AL);                                 // movsx  dstreg,al
4123
4124   /* 32-bit form */
4125   if (inst->size == 4)
4126   {
4127      /* general case */
4128      emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                           // mov   dstp,dstreg
4129   }
4130
4131   /* 64-bit form */
4132   else
4133   {
4134      /* general case */
4135      emit_cdq(&dst);                                                   // cdq
4136      emit_mov_p64_r64(drcbe, &dst, &dstp, REG_EAX, REG_EDX);                     // mov   dstp,edx:eax
4137   }
4138   return dst;
4139}
4140
4141
4142/*-------------------------------------------------
4143    op_read2u - process a READ2U opcode
4144-------------------------------------------------*/
4145
4146static x86code *op_read2u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4147{
4148   drcuml_parameter dstp, spacep, addrp;
4149   int dstreg;
4150
4151   /* validate instruction */
4152   assert(inst->size == 4 || inst->size == 8);
4153   assert(inst->condflags == DRCUML_COND_ALWAYS);
4154
4155   /* normalize parameters */
4156   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI);
4157
4158   /* pick a target register for the general case */
4159   dstreg = param_select_register(REG_EAX, &dstp, NULL);
4160
4161   /* set up a call to the read word handler */
4162   emit_push_p32(drcbe, &dst, &addrp);                                       // push   addrp
4163   emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->read_word);// call   read_word
4164   emit_add_r32_imm(&dst, REG_ESP, 4);                                       // add    esp,4
4165   emit_movzx_r32_r16(&dst, dstreg, REG_AX);                                 // movzx  dstreg,ax
4166
4167   /* store low 32 bits */
4168   emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                              // mov   dstp,dstreg
4169
4170   /* 64-bit form stores upper 32 bits */
4171   if (inst->size == 8)
4172   {
4173      /* general case */
4174      if (dstp.type == DRCUML_PTYPE_MEMORY)
4175         emit_mov_m32_imm(&dst, MABS(dstp.value + 4), 0);                     // mov   [dstp+4],0
4176      else if (dstp.type == DRCUML_PTYPE_INT_REGISTER)
4177         emit_mov_m32_imm(&dst, MABS(drcbe->reghi[dstp.value]), 0);               // mov   [reghi],0
4178   }
4179   return dst;
4180}
4181
4182
4183/*-------------------------------------------------
4184    op_read2s - process a READ2S opcode
4185-------------------------------------------------*/
4186
4187static x86code *op_read2s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4188{
4189   drcuml_parameter dstp, spacep, addrp;
4190   int dstreg;
4191
4192   /* validate instruction */
4193   assert(inst->size == 4 || inst->size == 8);
4194   assert(inst->condflags == DRCUML_COND_ALWAYS);
4195
4196   /* normalize parameters */
4197   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI);
4198
4199   /* pick a target register for the general case */
4200   dstreg = param_select_register(REG_EAX, &dstp, NULL);
4201
4202   /* set up a call to the read word handler */
4203   emit_push_p32(drcbe, &dst, &addrp);                                       // push   addrp
4204   emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->read_word);// call   read_word
4205   emit_add_r32_imm(&dst, REG_ESP, 4);                                       // add    esp,4
4206   emit_movsx_r32_r16(&dst, dstreg, REG_AX);                                 // movsx  dstreg,ax
4207
4208   /* 32-bit form */
4209   if (inst->size == 4)
4210   {
4211      /* general case */
4212      emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                           // mov   dstp,dstreg
4213   }
4214
4215   /* 64-bit form */
4216   else
4217   {
4218      /* general case */
4219      emit_cdq(&dst);                                                   // cdq
4220      emit_mov_p64_r64(drcbe, &dst, &dstp, REG_EAX, REG_EDX);                     // mov   dstp,edx:eax
4221   }
4222   return dst;
4223}
4224
4225
4226/*-------------------------------------------------
4227    op_read2m - process a READ2M opcode
4228-------------------------------------------------*/
4229
4230static x86code *op_read2m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4231{
4232   drcuml_parameter dstp, spacep, addrp, maskp;
4233   int dstreg;
4234
4235   /* validate instruction */
4236   assert(inst->size == 4 || inst->size == 8);
4237   assert(inst->condflags == DRCUML_COND_ALWAYS);
4238
4239   /* normalize parameters */
4240   param_normalize_4(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &maskp, PTYPE_MRI);
4241
4242   /* pick a target register for the general case */
4243   dstreg = param_select_register(REG_EAX, &dstp, NULL);
4244
4245   /* set up a call to the read dword masked handler */
4246   emit_push_p32(drcbe, &dst, &maskp);                                       // push   maskp
4247   emit_push_p32(drcbe, &dst, &addrp);                                       // push   addrp
4248   emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->read_word_masked);
4249                                                                  // call   read_word_masked
4250   emit_add_r32_imm(&dst, REG_ESP, 8);                                       // add    esp,8
4251   emit_movzx_r32_r16(&dst, dstreg, REG_AX);                                 // movzx  dstreg,ax
4252
4253   /* store low 32 bits */
4254   emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                              // mov   dstp,dstreg
4255
4256   /* 64-bit form stores upper 32 bits */
4257   if (inst->size == 8)
4258   {
4259      /* general case */
4260      if (dstp.type == DRCUML_PTYPE_MEMORY)
4261         emit_mov_m32_imm(&dst, MABS(dstp.value + 4), 0);                     // mov   [dstp+4],0
4262      else if (dstp.type == DRCUML_PTYPE_INT_REGISTER)
4263         emit_mov_m32_imm(&dst, MABS(drcbe->reghi[dstp.value]), 0);               // mov   [reghi],0
4264   }
4265   return dst;
4266}
4267
4268
4269/*-------------------------------------------------
4270    op_read4u - process a READ4U opcode
4271-------------------------------------------------*/
4272
4273static x86code *op_read4u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4274{
4275   drcuml_parameter dstp, spacep, addrp;
4276
4277   /* validate instruction */
4278   assert(inst->size == 4 || inst->size == 8);
4279   assert(inst->condflags == DRCUML_COND_ALWAYS);
4280
4281   /* normalize parameters */
4282   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI);
4283
4284   /* set up a call to the read dword handler */
4285   emit_push_p32(drcbe, &dst, &addrp);                                       // push   addrp
4286   emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->read_dword);// call   read_dword
4287   emit_add_r32_imm(&dst, REG_ESP, 4);                                       // add    esp,4
4288
4289   /* store low 32 bits */
4290   emit_mov_p32_r32(drcbe, &dst, &dstp, REG_EAX);                              // mov   dstp,eax
4291
4292   /* 64-bit form stores upper 32 bits */
4293   if (inst->size == 8)
4294   {
4295      /* general case */
4296      if (dstp.type == DRCUML_PTYPE_MEMORY)
4297         emit_mov_m32_imm(&dst, MABS(dstp.value + 4), 0);                     // mov   [dstp+4],0
4298      else if (dstp.type == DRCUML_PTYPE_INT_REGISTER)
4299         emit_mov_m32_imm(&dst, MABS(drcbe->reghi[dstp.value]), 0);               // mov   [reghi],0
4300   }
4301   return dst;
4302}
4303
4304
4305/*-------------------------------------------------
4306    op_read4s - process a READ4S opcode
4307-------------------------------------------------*/
4308
4309static x86code *op_read4s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4310{
4311   drcuml_parameter dstp, spacep, addrp;
4312
4313   /* validate instruction */
4314   assert(inst->size == 8);
4315   assert(inst->condflags == DRCUML_COND_ALWAYS);
4316
4317   /* normalize parameters */
4318   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI);
4319
4320   /* set up a call to the read dword handler */
4321   emit_push_p32(drcbe, &dst, &addrp);                                       // push   addrp
4322   emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->read_dword);// call   read_dword
4323   emit_add_r32_imm(&dst, REG_ESP, 4);                                       // add    esp,4
4324
4325   /* 64-bit form */
4326   emit_cdq(&dst);                                                      // cdq
4327   emit_mov_p64_r64(drcbe, &dst, &dstp, REG_EAX, REG_EDX);                        // mov   dst,edx:eax
4328
4329   return dst;
4330}
4331
4332
4333/*-------------------------------------------------
4334    op_read4m - process a READ4M opcode
4335-------------------------------------------------*/
4336
4337static x86code *op_read4m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4338{
4339   drcuml_parameter dstp, spacep, addrp, maskp;
4340
4341   /* validate instruction */
4342   assert(inst->size == 4 || inst->size == 8);
4343   assert(inst->condflags == DRCUML_COND_ALWAYS);
4344
4345   /* normalize parameters */
4346   param_normalize_4(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &maskp, PTYPE_MRI);
4347
4348   /* set up a call to the read dword masked handler */
4349   emit_push_p32(drcbe, &dst, &maskp);                                       // push   maskp
4350   emit_push_p32(drcbe, &dst, &addrp);                                       // push   addrp
4351   emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->read_dword_masked);
4352                                                                  // call   read_dword_masked
4353   emit_add_r32_imm(&dst, REG_ESP, 8);                                       // add    esp,8
4354
4355   /* store low 32 bits */
4356   emit_mov_p32_r32(drcbe, &dst, &dstp, REG_EAX);                              // mov   dstp,eax
4357
4358   /* 64-bit form stores upper 32 bits */
4359   if (inst->size == 8)
4360   {
4361      /* general case */
4362      if (dstp.type == DRCUML_PTYPE_MEMORY)
4363         emit_mov_m32_imm(&dst, MABS(dstp.value + 4), 0);                     // mov   [dstp+4],0
4364      else if (dstp.type == DRCUML_PTYPE_INT_REGISTER)
4365         emit_mov_m32_imm(&dst, MABS(drcbe->reghi[dstp.value]), 0);               // mov   [reghi],0
4366   }
4367   return dst;
4368}
4369
4370
4371/*-------------------------------------------------
4372    op_read8u - process a READ8U opcode
4373-------------------------------------------------*/
4374
4375static x86code *op_read8u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4376{
4377   drcuml_parameter dstp, spacep, addrp;
4378
4379   /* validate instruction */
4380   assert(inst->size == 8);
4381   assert(inst->condflags == DRCUML_COND_ALWAYS);
4382
4383   /* normalize parameters */
4384   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI);
4385
4386   /* set up a call to the read qword handler */
4387   emit_push_p32(drcbe, &dst, &addrp);                                       // push   addrp
4388   emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->read_qword);// call   read_qword
4389   emit_add_r32_imm(&dst, REG_ESP, 4);                                       // add    esp,4
4390
4391   /* 64-bit form */
4392   emit_mov_p64_r64(drcbe, &dst, &dstp, REG_EAX, REG_EDX);                        // mov   dst,edx:eax
4393
4394   return dst;
4395}
4396
4397
4398/*-------------------------------------------------
4399    op_read8m - process a READ8M opcode
4400-------------------------------------------------*/
4401
4402static x86code *op_read8m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4403{
4404   drcuml_parameter dstp, spacep, addrp, maskp;
4405
4406   /* validate instruction */
4407   assert(inst->size == 8);
4408   assert(inst->condflags == DRCUML_COND_ALWAYS);
4409
4410   /* normalize parameters */
4411   param_normalize_4(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &maskp, PTYPE_MRI);
4412
4413   /* set up a call to the read qword masked handler */
4414   emit_push_p64(drcbe, &dst, &maskp);                                       // push   maskp
4415   emit_push_p32(drcbe, &dst, &addrp);                                       // push   addrp
4416   emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->read_qword_masked);
4417                                                                  // call   read_qword_masked
4418   emit_add_r32_imm(&dst, REG_ESP, 12);                                    // add    esp,12
4419
4420   /* 64-bit form */
4421   emit_mov_p64_r64(drcbe, &dst, &dstp, REG_EAX, REG_EDX);                        // mov   dst,edx:eax
4422
4423   return dst;
4424}
4425
4426
4427/*-------------------------------------------------
4428    op_write1 - process a WRITE1 opcode
4429-------------------------------------------------*/
4430
4431static x86code *op_write1(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4432{
4433   drcuml_parameter spacep, addrp, srcp;
4434
4435   /* validate instruction */
4436   assert(inst->size == 4 || inst->size == 8);
4437   assert(inst->condflags == DRCUML_COND_ALWAYS);
4438
4439   /* normalize parameters */
4440   param_normalize_3(drcbe, inst, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &srcp, PTYPE_MRI);
4441
4442   /* set up a call to the write byte handler */
4443   emit_push_p32(drcbe, &dst, &srcp);                                       // push   srcp
4444   emit_push_p32(drcbe, &dst, &addrp);                                       // push   addrp
4445   emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->write_byte);// call   write_byte
4446   emit_add_r32_imm(&dst, REG_ESP, 8);                                       // add    esp,8
4447
4448   return dst;
4449}
4450
4451
4452/*-------------------------------------------------
4453    op_write2 - process a WRITE2 opcode
4454-------------------------------------------------*/
4455
4456static x86code *op_write2(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4457{
4458   drcuml_parameter spacep, addrp, srcp;
4459
4460   /* validate instruction */
4461   assert(inst->size == 4 || inst->size == 8);
4462   assert(inst->condflags == DRCUML_COND_ALWAYS);
4463
4464   /* normalize parameters */
4465   param_normalize_3(drcbe, inst, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &srcp, PTYPE_MRI);
4466
4467   /* set up a call to the write word handler */
4468   emit_push_p32(drcbe, &dst, &srcp);                                       // push   srcp
4469   emit_push_p32(drcbe, &dst, &addrp);                                       // push   addrp
4470   emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->write_word);// call   write_word
4471   emit_add_r32_imm(&dst, REG_ESP, 8);                                       // add    esp,8
4472
4473   return dst;
4474}
4475
4476
4477/*-------------------------------------------------
4478    op_writ2m - process a WRIT2M opcode
4479-------------------------------------------------*/
4480
4481static x86code *op_writ2m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4482{
4483   drcuml_parameter spacep, addrp, maskp, srcp;
4484
4485   /* validate instruction */
4486   assert(inst->size == 4 || inst->size == 8);
4487   assert(inst->condflags == DRCUML_COND_ALWAYS);
4488
4489   /* normalize parameters */
4490   param_normalize_4(drcbe, inst, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &maskp, PTYPE_MRI, &srcp, PTYPE_MRI);
4491
4492   /* set up a call to the write word handler */
4493   emit_push_p32(drcbe, &dst, &maskp);                                       // push   maskp
4494   emit_push_p32(drcbe, &dst, &srcp);                                       // push   srcp
4495   emit_push_p32(drcbe, &dst, &addrp);                                       // push   addrp
4496   emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->write_word_masked);
4497                                                                  // call   write_word_masked
4498   emit_add_r32_imm(&dst, REG_ESP, 12);                                    // add    esp,12
4499   return dst;
4500}
4501
4502
4503/*-------------------------------------------------
4504    op_write4 - process a WRITE4 opcode
4505-------------------------------------------------*/
4506
4507static x86code *op_write4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4508{
4509   drcuml_parameter spacep, addrp, srcp;
4510
4511   /* validate instruction */
4512   assert(inst->size == 4 || inst->size == 8);
4513   assert(inst->condflags == DRCUML_COND_ALWAYS);
4514
4515   /* normalize parameters */
4516   param_normalize_3(drcbe, inst, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &srcp, PTYPE_MRI);
4517
4518   /* set up a call to the write dword handler */
4519   emit_push_p32(drcbe, &dst, &srcp);                                       // push   srcp
4520   emit_push_p32(drcbe, &dst, &addrp);                                       // push   addrp
4521   emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->write_dword);// call   write_dword
4522   emit_add_r32_imm(&dst, REG_ESP, 8);                                       // add    esp,8
4523
4524   return dst;
4525}
4526
4527
4528/*-------------------------------------------------
4529    op_writ4m - process a WRIT4M opcode
4530-------------------------------------------------*/
4531
4532static x86code *op_writ4m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4533{
4534   drcuml_parameter spacep, addrp, maskp, srcp;
4535
4536   /* validate instruction */
4537   assert(inst->size == 4 || inst->size == 8);
4538   assert(inst->condflags == DRCUML_COND_ALWAYS);
4539
4540   /* normalize parameters */
4541   param_normalize_4(drcbe, inst, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &maskp, PTYPE_MRI, &srcp, PTYPE_MRI);
4542
4543   /* set up a call to the write word handler */
4544   emit_push_p32(drcbe, &dst, &maskp);                                       // push   maskp
4545   emit_push_p32(drcbe, &dst, &srcp);                                       // push   srcp
4546   emit_push_p32(drcbe, &dst, &addrp);                                       // push   addrp
4547   emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->write_dword_masked);
4548                                                                  // call   write_dword_masked
4549   emit_add_r32_imm(&dst, REG_ESP, 12);                                    // add    esp,12
4550   return dst;
4551}
4552
4553
4554/*-------------------------------------------------
4555    op_write8 - process a WRITE8 opcode
4556-------------------------------------------------*/
4557
4558static x86code *op_write8(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4559{
4560   drcuml_parameter spacep, addrp, srcp;
4561
4562   /* validate instruction */
4563   assert(inst->size == 8);
4564   assert(inst->condflags == DRCUML_COND_ALWAYS);
4565
4566   /* normalize parameters */
4567   param_normalize_3(drcbe, inst, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &srcp, PTYPE_MRI);
4568
4569   /* set up a call to the write qword handler */
4570   emit_push_p64(drcbe, &dst, &srcp);                                       // push   srcp
4571   emit_push_p32(drcbe, &dst, &addrp);                                       // push   addrp
4572   emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->write_qword);// call   write_qword
4573
4574   emit_add_r32_imm(&dst, REG_ESP, 12);                                    // add    esp,12
4575   return dst;
4576}
4577
4578
4579/*-------------------------------------------------
4580    op_writ8m - process a WRIT8M opcode
4581-------------------------------------------------*/
4582
4583static x86code *op_writ8m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4584{
4585   drcuml_parameter spacep, addrp, maskp, srcp;
4586
4587   /* validate instruction */
4588   assert(inst->size == 8);
4589   assert(inst->condflags == DRCUML_COND_ALWAYS);
4590
4591   /* normalize parameters */
4592   param_normalize_4(drcbe, inst, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &maskp, PTYPE_MRI, &srcp, PTYPE_MRI);
4593
4594   /* set up a call to the write word handler */
4595   emit_push_p64(drcbe, &dst, &maskp);                                       // push   maskp
4596   emit_push_p64(drcbe, &dst, &srcp);                                       // push   srcp
4597   emit_push_p32(drcbe, &dst, &addrp);                                       // push   addrp
4598   emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->write_qword_masked);
4599                                                                  // call   write_qword_masked
4600   emit_add_r32_imm(&dst, REG_ESP, 20);                                    // add    esp,20
4601   return dst;
4602}
4603
4604
4605/*-------------------------------------------------
4606    op_flags - process a FLAGS opcode
4607-------------------------------------------------*/
4608
4609static x86code *op_flags(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4610{
4611   drcuml_parameter dstp, maskp, tablep;
4612   int dstreg;
4613
4614   /* validate instruction */
4615   assert(inst->size == 4 || inst->size == 8);
4616   assert(inst->condflags == DRCUML_COND_ALWAYS);
4617
4618   /* normalize parameters */
4619   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &maskp, PTYPE_I, &tablep, PTYPE_M);
4620
4621   /* pick a target register for the general case */
4622   dstreg = param_select_register(REG_ECX, &dstp, NULL);
4623
4624   /* translate live flags into UML flags */
4625   emit_pushf(&dst);                                                   // pushf
4626   emit_pop_r32(&dst, REG_EAX);                                          // pop    eax
4627   emit_and_r32_imm(&dst, REG_EAX, 0x8c5);                                    // and    eax,0x8c5
4628   emit_movzx_r32_m8(&dst, REG_EAX, MBD(REG_EAX, flags_map));                     // movzx  eax,[flags_map]
4629
4630   /* 32-bit form */
4631   if (inst->size == 4)
4632   {
4633      /* no masking */
4634      if (maskp.value == 0xffffffff)
4635      {
4636         emit_mov_r32_m32(&dst, dstreg, MISD(REG_EAX, 4, tablep.value));            // mov    dstreg,[eax*4+table]
4637         emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                        // mov    dstp,dstreg
4638      }
4639
4640      /* general case */
4641      else
4642      {
4643         emit_mov_r32_p32(drcbe, &dst, dstreg, &dstp);                        // mov    dstreg,dstp
4644         emit_mov_r32_m32(&dst, REG_EDX, MISD(REG_EAX, 4, tablep.value));         // mov    edx,[eax*4+table]
4645         emit_and_r32_imm(&dst, dstreg, ~maskp.value);                        // and    dstreg,~mask
4646         emit_and_r32_imm(&dst, REG_EDX, maskp.value);                        // and    edx,mask
4647         emit_or_r32_r32(&dst, dstreg, REG_EDX);                              // or     dstreg,edx
4648         emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                        // mov    dstp,dstreg
4649      }
4650   }
4651
4652   /* 64-bit form */
4653   else
4654   {
4655      /* no masking */
4656      if (maskp.value == U64(0xffffffffffffffff))
4657      {
4658         emit_mov_r32_m32(&dst, dstreg, MISD(REG_EAX, 8, tablep.value));            // mov    dstreg,[eax*4+table]
4659         emit_mov_r32_m32(&dst, REG_EDX, MISD(REG_EAX, 8, tablep.value + 4));      // mov    edx,[eax*4+table+4]
4660         emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg, REG_EDX);                  // mov    dst,edx:dstreg
4661      }
4662
4663      /* general case */
4664      else
4665      {
4666         if ((UINT32)maskp.value != 0)
4667         {
4668            emit_mov_r32_p32(drcbe, &dst, dstreg, &dstp);                     // mov    dstreg,dstp
4669            emit_mov_r32_m32(&dst, REG_EDX, MISD(REG_EAX, 8, tablep.value));      // mov    edx,[eax*8+table]
4670            emit_and_r32_imm(&dst, dstreg, ~maskp.value);                     // and    dstreg,~mask
4671            emit_and_r32_imm(&dst, REG_EDX, maskp.value);                     // and    edx,mask
4672            emit_or_r32_r32(&dst, dstreg, REG_EDX);                           // or     dstreg,edx
4673            emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                     // mov    dstp,dstreg.lo
4674         }
4675         if ((UINT32)(maskp.value >> 32) != 0)
4676         {
4677            if (dstp.type == DRCUML_PTYPE_INT_REGISTER)
4678               emit_mov_r32_m32(&dst, REG_ECX, MABS(drcbe->reghi[dstp.value]));   // mov    ecx,dstp.hi
4679            else
4680               emit_mov_r32_m32(&dst, REG_ECX, MABS(dstp.value + 4));            // mov    ecx,dstp.hi
4681            emit_mov_r32_m32(&dst, REG_EDX, MISD(REG_EDX, 8, tablep.value+4));      // mov    edx,[eax*8+table+4]
4682            emit_and_r32_imm(&dst, REG_ECX, ~maskp.value);                     // and    ecx,~mask
4683            emit_and_r32_imm(&dst, REG_EDX, maskp.value);                     // and    edx,mask
4684            emit_or_r32_r32(&dst, REG_ECX, REG_EDX);                        // or     ecx,edx
4685            if (dstp.type == DRCUML_PTYPE_INT_REGISTER)
4686               emit_mov_m32_r32(&dst, MABS(drcbe->reghi[dstp.value]), REG_ECX);   // mov    dstp.hi,ecx
4687            else
4688               emit_mov_m32_r32(&dst, MABS(dstp.value + 4), REG_ECX);            // mov    dstp.hi,ecx
4689         }
4690      }
4691   }
4692   return dst;
4693}
4694
4695
4696/*-------------------------------------------------
4697    op_mov - process a MOV opcode
4698-------------------------------------------------*/
4699
4700static x86code *op_mov(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4701{
4702   drcuml_parameter dstp, srcp;
4703   x86code *savedst = dst;
4704   emit_link skip = { 0 };
4705   int dstreg;
4706
4707   /* validate instruction */
4708   assert(inst->size == 4 || inst->size == 8);
4709   assert(inst->condflags == DRCUML_COND_ALWAYS || (inst->condflags >= COND_Z && inst->condflags < DRCUML_COND_MAX));
4710
4711   /* normalize parameters, but only if we got here directly */
4712   /* other opcodes call through here with pre-normalized parameters */
4713   if (inst->opcode == DRCUML_OP_MOV)
4714      param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MRI);
4715   else
4716   {
4717      dstp = inst->param[0];
4718      srcp = inst->param[1];
4719   }
4720
4721   /* degenerate case: dest and source are equal */
4722   if (dstp.type == srcp.type && dstp.value == srcp.value)
4723      return dst;
4724
4725   /* pick a target register for the general case */
4726   dstreg = param_select_register(REG_EAX, &dstp, NULL);
4727
4728   /* always start with a jmp */
4729   if (inst->condflags != DRCUML_COND_ALWAYS)
4730      emit_jcc_short_link(&dst, X86_NOT_CONDITION(inst->condflags), &skip);         // jcc   skip
4731
4732   /* 32-bit form */
4733   if (inst->size == 4)
4734   {
4735      /* register to memory */
4736      if (dstp.type == DRCUML_PTYPE_MEMORY && srcp.type == DRCUML_PTYPE_INT_REGISTER)
4737         emit_mov_m32_r32(&dst, MABS(dstp.value), srcp.value);                  // mov   [dstp],srcp
4738
4739      /* immediate to memory */
4740      else if (dstp.type == DRCUML_PTYPE_MEMORY && srcp.type == DRCUML_PTYPE_IMMEDIATE)
4741         emit_mov_m32_imm(&dst, MABS(dstp.value), srcp.value);                  // mov   [dstp],srcp
4742
4743      /* conditional memory to register */
4744      else if (inst->condflags != 0 && dstp.type == DRCUML_PTYPE_INT_REGISTER && srcp.type == DRCUML_PTYPE_MEMORY)
4745      {
4746         dst = savedst;
4747         skip.target = NULL;
4748         emit_cmovcc_r32_m32(&dst, X86_CONDITION(inst->condflags), dstp.value, MABS(srcp.value));
4749                                                                  // cmovcc dstp,[srcp]
4750      }
4751
4752      /* conditional register to register */
4753      else if (inst->condflags != 0 && dstp.type == DRCUML_PTYPE_INT_REGISTER && srcp.type == DRCUML_PTYPE_INT_REGISTER)
4754      {
4755         dst = savedst;
4756         skip.target = NULL;
4757         emit_cmovcc_r32_r32(&dst, X86_CONDITION(inst->condflags), dstp.value, srcp.value);
4758                                                                  // cmovcc dstp,srcp
4759      }
4760
4761      /* general case */
4762      else
4763      {
4764         emit_mov_r32_p32(drcbe, &dst, dstreg, &srcp);                        // mov   dstreg,srcp
4765         emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
4766      }
4767   }
4768
4769   /* 64-bit form */
4770   else if (inst->size == 8)
4771   {
4772      /* register to memory */
4773      if (dstp.type == DRCUML_PTYPE_MEMORY && srcp.type == DRCUML_PTYPE_INT_REGISTER)
4774      {
4775         emit_mov_r32_m32(&dst, REG_EAX, MABS(drcbe->reghi[srcp.value]));         // mov   eax,reghi[srcp]
4776         emit_mov_m32_r32(&dst, MABS(dstp.value), srcp.value);                  // mov   [dstp],srcp
4777         emit_mov_m32_r32(&dst, MABS(dstp.value + 4), REG_EAX);                  // mov   [dstp+4],eax
4778      }
4779
4780      /* immediate to memory */
4781      else if (dstp.type == DRCUML_PTYPE_MEMORY && srcp.type == DRCUML_PTYPE_INT_REGISTER)
4782      {
4783         emit_mov_m32_imm(&dst, MABS(dstp.value), srcp.value);                  // mov   [dstp],srcp
4784         emit_mov_m32_imm(&dst, MABS(dstp.value + 4), srcp.value >> 32);            // mov   [dstp+4],srcp >> 32
4785      }
4786
4787      /* general case */
4788      else
4789      {
4790         emit_mov_r64_p64(drcbe, &dst, dstreg, REG_EDX, &srcp);                  // mov   edx:dstreg,srcp
4791         emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg, REG_EDX);                  // mov   dstp,edx:dstreg
4792      }
4793   }
4794
4795   /* resolve the jump */
4796   if (skip.target != NULL)
4797      resolve_link(&dst, &skip);
4798   return dst;
4799}
4800
4801
4802/*-------------------------------------------------
4803    op_zext1 - process a ZEXT1 opcode
4804-------------------------------------------------*/
4805
4806static x86code *op_zext1(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4807{
4808   drcuml_parameter dstp, srcp;
4809   int dstreg;
4810
4811   /* validate instruction */
4812   assert(inst->size == 4 || inst->size == 8);
4813   assert(inst->condflags == DRCUML_COND_ALWAYS);
4814
4815   /* normalize parameters */
4816   param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MRI);
4817
4818   /* degenerate cases -- convert to a move */
4819   if (srcp.type == DRCUML_PTYPE_IMMEDIATE)
4820      return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT8)srcp.value);
4821
4822   /* pick a target register for the general case */
4823   dstreg = param_select_register(REG_EAX, &dstp, NULL);
4824
4825   /* convert 8-bit source registers */
4826   if (srcp.type == DRCUML_PTYPE_INT_REGISTER && (srcp.value & 4))
4827   {
4828      emit_mov_r32_r32(&dst, REG_EAX, srcp.value);                           // mov   eax,srcp
4829      srcp.value = REG_EAX;
4830   }
4831
4832   /* 32-bit form */
4833   if (inst->size == 4)
4834   {
4835      /* general case */
4836      if (srcp.type == DRCUML_PTYPE_MEMORY)
4837         emit_movzx_r32_m8(&dst, dstreg, MABS(srcp.value));                     // movzx dstreg,[srcp]
4838      else if (srcp.type == DRCUML_PTYPE_INT_REGISTER)
4839         emit_movzx_r32_r8(&dst, dstreg, srcp.value);                        // movzx dstreg,srcp
4840      emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                           // mov   dstp,dstreg
4841   }
4842
4843   /* 64-bit form */
4844   else if (inst->size == 8)
4845   {
4846      /* general case */
4847      if (srcp.type == DRCUML_PTYPE_MEMORY)
4848         emit_movzx_r32_m8(&dst, dstreg, MABS(srcp.value));                     // movzx dstreg,[srcp]
4849      else if (srcp.type == DRCUML_PTYPE_INT_REGISTER)
4850         emit_movzx_r32_r8(&dst, dstreg, srcp.value);                        // movzx dstreg,srcp
4851      emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                           // mov   dstp,dstreg
4852      if (dstp.type == DRCUML_PTYPE_MEMORY)
4853         emit_mov_m32_imm(&dst, MABS(dstp.value + 4), 0);                     // mov   [dstp+4],0
4854      else if (dstp.type == DRCUML_PTYPE_INT_REGISTER)
4855         emit_mov_m32_imm(&dst, MABS(drcbe->reghi[dstp.value]), 0);               // mov   reghi[dstp],0
4856   }
4857   return dst;
4858}
4859
4860
4861/*-------------------------------------------------
4862    op_zext2 - process a ZEXT2 opcode
4863-------------------------------------------------*/
4864
4865static x86code *op_zext2(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4866{
4867   drcuml_parameter dstp, srcp;
4868   int dstreg;
4869
4870   /* validate instruction */
4871   assert(inst->size == 4 || inst->size == 8);
4872   assert(inst->condflags == DRCUML_COND_ALWAYS);
4873
4874   /* normalize parameters */
4875   param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MRI);
4876
4877   /* degenerate cases -- convert to a move */
4878   if (srcp.type == DRCUML_PTYPE_IMMEDIATE)
4879      return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT16)srcp.value);
4880
4881   /* pick a target register for the general case */
4882   dstreg = param_select_register(REG_EAX, &dstp, NULL);
4883
4884   /* 32-bit form */
4885   if (inst->size == 4)
4886   {
4887      /* general case */
4888      if (srcp.type == DRCUML_PTYPE_MEMORY)
4889         emit_movzx_r32_m16(&dst, dstreg, MABS(srcp.value));                     // movzx dstreg,[srcp]
4890      else if (srcp.type == DRCUML_PTYPE_INT_REGISTER)
4891         emit_movzx_r32_r16(&dst, dstreg, srcp.value);                        // movzx dstreg,srcp
4892      emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                           // mov   dstp,dstreg
4893   }
4894
4895   /* 64-bit form */
4896   else if (inst->size == 8)
4897   {
4898      /* general case */
4899      if (srcp.type == DRCUML_PTYPE_MEMORY)
4900         emit_movzx_r32_m16(&dst, dstreg, MABS(srcp.value));                     // movzx dstreg,[srcp]
4901      else if (srcp.type == DRCUML_PTYPE_INT_REGISTER)
4902         emit_movzx_r32_r16(&dst, dstreg, srcp.value);                        // movzx dstreg,srcp
4903      emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                           // mov   dstp,dstreg
4904      if (dstp.type == DRCUML_PTYPE_MEMORY)
4905         emit_mov_m32_imm(&dst, MABS(dstp.value + 4), 0);                     // mov   [dstp+4],0
4906      else if (dstp.type == DRCUML_PTYPE_INT_REGISTER)
4907         emit_mov_m32_imm(&dst, MABS(drcbe->reghi[dstp.value]), 0);               // mov   reghi[dstp],0
4908   }
4909   return dst;
4910}
4911
4912
4913/*-------------------------------------------------
4914    op_zext4 - process a ZEXT4 opcode
4915-------------------------------------------------*/
4916
4917static x86code *op_zext4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4918{
4919   drcuml_parameter dstp, srcp;
4920   int dstreg;
4921
4922   /* validate instruction */
4923   assert(inst->size == 8);
4924   assert(inst->condflags == DRCUML_COND_ALWAYS);
4925
4926   /* normalize parameters */
4927   param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MRI);
4928
4929   /* degenerate cases -- convert to a move */
4930   if (srcp.type == DRCUML_PTYPE_IMMEDIATE)
4931      return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT32)srcp.value);
4932
4933   /* pick a target register for the general case */
4934   dstreg = param_select_register(REG_EAX, &dstp, NULL);
4935
4936   /* 64-bit form */
4937   if (inst->size == 8)
4938   {
4939      /* general case */
4940      emit_mov_r32_p32(drcbe, &dst, dstreg, &srcp);                           // mov   dstreg,srcp
4941      emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                           // mov   dstp,dstreg
4942      if (dstp.type == DRCUML_PTYPE_MEMORY)
4943         emit_mov_m32_imm(&dst, MABS(dstp.value + 4), 0);                     // mov   [dstp+4],0
4944      else if (dstp.type == DRCUML_PTYPE_INT_REGISTER)
4945         emit_mov_m32_imm(&dst, MABS(drcbe->reghi[dstp.value]), 0);               // mov   reghi[dstp],0
4946   }
4947   return dst;
4948}
4949
4950
4951/*-------------------------------------------------
4952    op_sext1 - process a SEXT1 opcode
4953-------------------------------------------------*/
4954
4955static x86code *op_sext1(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
4956{
4957   drcuml_parameter dstp, srcp;
4958   int dstreg;
4959
4960   /* validate instruction */
4961   assert(inst->size == 4 || inst->size == 8);
4962   assert(inst->condflags == DRCUML_COND_ALWAYS);
4963
4964   /* normalize parameters */
4965   param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MRI);
4966
4967   /* degenerate cases -- convert to a move */
4968   if (srcp.type == DRCUML_PTYPE_IMMEDIATE)
4969      return convert_to_mov_imm(drcbe, dst, inst, &dstp, (INT8)srcp.value);
4970
4971   /* pick a target register for the general case */
4972   dstreg = param_select_register(REG_EAX, &dstp, NULL);
4973
4974   /* convert 8-bit source registers */
4975   if (srcp.type == DRCUML_PTYPE_INT_REGISTER && (srcp.value & 4))
4976   {
4977      emit_mov_r32_r32(&dst, REG_EAX, srcp.value);                           // mov   eax,srcp
4978      srcp.value = REG_EAX;
4979   }
4980
4981   /* 32-bit form */
4982   if (inst->size == 4)
4983   {
4984      /* general case */
4985      if (srcp.type == DRCUML_PTYPE_MEMORY)
4986         emit_movsx_r32_m8(&dst, dstreg, MABS(srcp.value));                     // movsx dstreg,[srcp]
4987      else if (srcp.type == DRCUML_PTYPE_INT_REGISTER)
4988         emit_movsx_r32_r8(&dst, dstreg, srcp.value);                        // movsx dstreg,srcp
4989      emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                           // mov   dstp,dstreg
4990   }
4991
4992   /* 64-bit form */
4993   else if (inst->size == 8)
4994   {
4995      /* general case */
4996      if (srcp.type == DRCUML_PTYPE_MEMORY)
4997         emit_movsx_r32_m8(&dst, REG_EAX, MABS(srcp.value));                     // movsx eax,[srcp]
4998      else if (srcp.type == DRCUML_PTYPE_INT_REGISTER)
4999         emit_movsx_r32_r8(&dst, REG_EAX, srcp.value);                        // movsx eax,srcp
5000      emit_cdq(&dst);                                                   // cdq
5001      emit_mov_p64_r64(drcbe, &dst, &dstp, REG_EAX, REG_EDX);                     // mov   dstp,edx:eax
5002   }
5003   return dst;
5004}
5005
5006
5007/*-------------------------------------------------
5008    op_sext2 - process a SEXT2 opcode
5009-------------------------------------------------*/
5010
5011static x86code *op_sext2(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
5012{
5013   drcuml_parameter dstp, srcp;
5014   int dstreg;
5015
5016   /* validate instruction */
5017   assert(inst->size == 4 || inst->size == 8);
5018   assert(inst->condflags == DRCUML_COND_ALWAYS);
5019
5020   /* normalize parameters */
5021   param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MRI);
5022
5023   /* degenerate cases -- convert to a move */
5024   if (srcp.type == DRCUML_PTYPE_IMMEDIATE)
5025      return convert_to_mov_imm(drcbe, dst, inst, &dstp, (INT16)srcp.value);
5026
5027   /* pick a target register for the general case */
5028   dstreg = param_select_register(REG_EAX, &dstp, NULL);
5029
5030   /* 32-bit form */
5031   if (inst->size == 4)
5032   {
5033      /* general case */
5034      if (srcp.type == DRCUML_PTYPE_MEMORY)
5035         emit_movsx_r32_m16(&dst, dstreg, MABS(srcp.value));                     // movsx dstreg,[srcp]
5036      else if (srcp.type == DRCUML_PTYPE_INT_REGISTER)
5037         emit_movsx_r32_r16(&dst, dstreg, srcp.value);                        // movsx dstreg,srcp
5038      emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                           // mov   dstp,dstreg
5039   }
5040
5041   /* 64-bit form */
5042   else if (inst->size == 8)
5043   {
5044      /* general case */
5045      if (srcp.type == DRCUML_PTYPE_MEMORY)
5046         emit_movsx_r32_m16(&dst, REG_EAX, MABS(srcp.value));                  // movsx eax,[srcp]
5047      else if (srcp.type == DRCUML_PTYPE_INT_REGISTER)
5048         emit_movsx_r32_r16(&dst, REG_EAX, srcp.value);                        // movsx eax,srcp
5049      emit_cdq(&dst);                                                   // cdq
5050      emit_mov_p64_r64(drcbe, &dst, &dstp, REG_EAX, REG_EDX);                     // mov   dstp,edx:eax
5051   }
5052   return dst;
5053}
5054
5055
5056/*-------------------------------------------------
5057    op_sext4 - process a SEXT4 opcode
5058-------------------------------------------------*/
5059
5060static x86code *op_sext4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
5061{
5062   drcuml_parameter dstp, srcp;
5063
5064   /* validate instruction */
5065   assert(inst->size == 8);
5066   assert(inst->condflags == DRCUML_COND_ALWAYS);
5067
5068   /* normalize parameters */
5069   param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MRI);
5070
5071   /* degenerate cases -- convert to a move */
5072   if (srcp.type == DRCUML_PTYPE_IMMEDIATE)
5073      return convert_to_mov_imm(drcbe, dst, inst, &dstp, (INT32)srcp.value);
5074
5075   /* 64-bit form */
5076   if (inst->size == 8)
5077   {
5078      /* general case */
5079      emit_mov_r32_p32(drcbe, &dst, REG_EAX, &srcp);                           // mov   eax,srcp
5080      emit_cdq(&dst);                                                   // cdq
5081      emit_mov_p64_r64(drcbe, &dst, &dstp, REG_EAX, REG_EDX);                     // mov   dstp,edx:eax
5082   }
5083   return dst;
5084}
5085
5086
5087/*-------------------------------------------------
5088    op_add - process a ADD opcode
5089-------------------------------------------------*/
5090
5091static x86code *op_add(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
5092{
5093   drcuml_parameter dstp, src1p, src2p;
5094   int dstreg;
5095
5096   /* validate instruction */
5097   assert(inst->size == 4 || inst->size == 8);
5098   assert((inst->condflags & ~(DRCUML_FLAG_C | DRCUML_FLAG_V | DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
5099
5100   /* normalize parameters */
5101   param_normalize_3_commutative(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
5102
5103   /* degenerate cases -- convert to a move */
5104   if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0)
5105      return convert_to_mov_imm(drcbe, dst, inst, &dstp, src1p.value + src2p.value);
5106   if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0)
5107      return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p);
5108
5109   /* pick a target register for the general case */
5110   dstreg = param_select_register(REG_EAX, &dstp, &src2p);
5111
5112   /* 32-bit form */
5113   if (inst->size == 4)
5114   {
5115      /* dstp == src1p in memory */
5116      if (dstp.type == DRCUML_PTYPE_MEMORY && src1p.type == DRCUML_PTYPE_MEMORY && src1p.value == dstp.value)
5117         emit_add_m32_p32(drcbe, &dst, MABS(dstp.value), &src2p, inst);            // add   [dstp],src2p
5118
5119      /* reg = reg + imm */
5120      else if (dstp.type == DRCUML_PTYPE_INT_REGISTER && src1p.type == DRCUML_PTYPE_INT_REGISTER && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0)
5121         emit_lea_r32_m32(&dst, dstp.value, MBD(src1p.value, src2p.value));         // lea   dstp,[src1p+src2p]
5122
5123      /* reg = reg + reg */
5124      else if (dstp.type == DRCUML_PTYPE_INT_REGISTER && src1p.type == DRCUML_PTYPE_INT_REGISTER && src2p.type == DRCUML_PTYPE_INT_REGISTER && inst->condflags == 0)
5125         emit_lea_r32_m32(&dst, dstp.value, MBISD(src1p.value, src2p.value, 1, 0));   // lea   dstp,[src1p+src2p]
5126
5127      /* general case */
5128      else
5129      {
5130         emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
5131         emit_add_r32_p32(drcbe, &dst, dstreg, &src2p, inst);                  // add   dstreg,src2p
5132         emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
5133      }
5134   }
5135
5136   /* 64-bit form */
5137   else if (inst->size == 8)
5138   {
5139      /* dstp == src1p in memory */
5140      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
5141         emit_add_m64_p64(drcbe, &dst, MABS(dstp.value), &src2p, inst);            // add   [dstp],src2p
5142
5143      /* general case */
5144      else
5145      {
5146         emit_mov_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src1p);                  // mov   dstreg:dstp,[src1p]
5147         emit_add_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src2p, inst);            // add   dstreg:dstp,src2p
5148         emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg, REG_EDX);                  // mov   dstp,dstreg:eax
5149      }
5150   }
5151   return dst;
5152}
5153
5154
5155/*-------------------------------------------------
5156    op_addc - process a ADDC opcode
5157-------------------------------------------------*/
5158
5159static x86code *op_addc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
5160{
5161   drcuml_parameter dstp, src1p, src2p;
5162   int dstreg;
5163
5164   /* validate instruction */
5165   assert(inst->size == 4 || inst->size == 8);
5166   assert((inst->condflags & ~(DRCUML_FLAG_C | DRCUML_FLAG_V | DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
5167
5168   /* normalize parameters */
5169   param_normalize_3_commutative(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
5170
5171   /* pick a target register for the general case */
5172   dstreg = param_select_register(REG_EAX, &dstp, &src2p);
5173
5174   /* 32-bit form */
5175   if (inst->size == 4)
5176   {
5177      /* dstp == src1p in memory */
5178      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
5179         emit_adc_m32_p32(drcbe, &dst, MABS(dstp.value), &src2p, inst);            // adc   [dstp],src2p
5180
5181      /* general case */
5182      else
5183      {
5184         emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
5185         emit_adc_r32_p32(drcbe, &dst, dstreg, &src2p, inst);                  // adc   dstreg,src2p
5186         emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
5187      }
5188   }
5189
5190   /* 64-bit form */
5191   else if (inst->size == 8)
5192   {
5193      /* dstp == src1p in memory */
5194      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
5195         emit_adc_m64_p64(drcbe, &dst, MABS(dstp.value), &src2p, inst);            // adc   [dstp],src2p
5196
5197      /* general case */
5198      else
5199      {
5200         emit_mov_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src1p);                  // mov   dstreg:dstp,[src1p]
5201         emit_adc_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src2p, inst);            // adc   dstreg:dstp,src2p
5202         emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg, REG_EDX);                  // mov   dstp,dstreg:eax
5203      }
5204   }
5205   return dst;
5206}
5207
5208
5209/*-------------------------------------------------
5210    op_sub - process a SUB opcode
5211-------------------------------------------------*/
5212
5213static x86code *op_sub(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
5214{
5215   drcuml_parameter dstp, src1p, src2p;
5216   int dstreg;
5217
5218   /* validate instruction */
5219   assert(inst->size == 4 || inst->size == 8);
5220   assert((inst->condflags & ~(DRCUML_FLAG_C | DRCUML_FLAG_V | DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
5221
5222   /* normalize parameters */
5223   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
5224
5225   /* degenerate cases -- convert to a move */
5226   if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0)
5227      return convert_to_mov_imm(drcbe, dst, inst, &dstp, src1p.value - src2p.value);
5228   if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0)
5229      return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p);
5230
5231   /* pick a target register for the general case */
5232   dstreg = param_select_register(REG_EAX, &dstp, &src2p);
5233
5234   /* 32-bit form */
5235   if (inst->size == 4)
5236   {
5237      /* dstp == src1p in memory */
5238      if (dstp.type == DRCUML_PTYPE_MEMORY && src1p.type == DRCUML_PTYPE_MEMORY && src1p.value == dstp.value)
5239         emit_sub_m32_p32(drcbe, &dst, MABS(dstp.value), &src2p, inst);            // sub   [dstp],src2p
5240
5241      /* reg = reg - imm */
5242      else if (dstp.type == DRCUML_PTYPE_INT_REGISTER && src1p.type == DRCUML_PTYPE_INT_REGISTER && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0)
5243         emit_lea_r32_m32(&dst, dstp.value, MBD(src1p.value, -src2p.value));         // lea   dstp,[src1p-src2p]
5244
5245      /* general case */
5246      else
5247      {
5248         emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
5249         emit_sub_r32_p32(drcbe, &dst, dstreg, &src2p, inst);                  // sub   dstreg,src2p
5250         emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
5251      }
5252   }
5253
5254   /* 64-bit form */
5255   else if (inst->size == 8)
5256   {
5257      /* dstp == src1p in memory */
5258      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
5259         emit_sub_m64_p64(drcbe, &dst, MABS(dstp.value), &src2p, inst);            // sub   [dstp],src2p
5260
5261      /* general case */
5262      else
5263      {
5264         emit_mov_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src1p);                  // mov   dstreg:dstp,[src1p]
5265         emit_sub_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src2p, inst);            // sub   dstreg:dstp,src2p
5266         emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg, REG_EDX);                  // mov   dstp,dstreg:eax
5267      }
5268   }
5269   return dst;
5270}
5271
5272
5273/*-------------------------------------------------
5274    op_subc - process a SUBC opcode
5275-------------------------------------------------*/
5276
5277static x86code *op_subc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
5278{
5279   drcuml_parameter dstp, src1p, src2p;
5280   int dstreg;
5281
5282   /* validate instruction */
5283   assert(inst->size == 4 || inst->size == 8);
5284   assert((inst->condflags & ~(DRCUML_FLAG_C | DRCUML_FLAG_V | DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
5285
5286   /* normalize parameters */
5287   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
5288
5289   /* pick a target register for the general case */
5290   dstreg = param_select_register(REG_EAX, &dstp, &src2p);
5291
5292   /* 32-bit form */
5293   if (inst->size == 4)
5294   {
5295      /* dstp == src1p in memory */
5296      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
5297         emit_sbb_m32_p32(drcbe, &dst, MABS(dstp.value), &src2p, inst);            // sbb   [dstp],src2p
5298
5299      /* general case */
5300      else
5301      {
5302         emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
5303         emit_sbb_r32_p32(drcbe, &dst, dstreg, &src2p, inst);                  // sbb   dstreg,src2p
5304         emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
5305      }
5306   }
5307
5308   /* 64-bit form */
5309   else if (inst->size == 8)
5310   {
5311      /* dstp == src1p in memory */
5312      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
5313         emit_sbb_m64_p64(drcbe, &dst, MABS(dstp.value), &src2p, inst);            // sbb   [dstp],src2p
5314
5315      /* general case */
5316      else
5317      {
5318         emit_mov_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src1p);                  // mov   dstreg:dstp,[src1p]
5319         emit_sbb_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src2p, inst);            // sbb   dstreg:dstp,src2p
5320         emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg, REG_EDX);                  // mov   dstp,dstreg:eax
5321      }
5322   }
5323   return dst;
5324}
5325
5326
5327/*-------------------------------------------------
5328    op_cmp - process a CMP opcode
5329-------------------------------------------------*/
5330
5331static x86code *op_cmp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
5332{
5333   drcuml_parameter src1p, src2p;
5334   emit_link skip;
5335   int src1reg;
5336
5337   /* validate instruction */
5338   assert(inst->size == 4 || inst->size == 8);
5339   assert((inst->condflags & ~(DRCUML_FLAG_C | DRCUML_FLAG_V | DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
5340
5341   /* normalize parameters */
5342   param_normalize_2(drcbe, inst, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
5343
5344   /* degenerate cases */
5345   if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && src1p.value == src2p.value)
5346   {
5347      emit_cmp_r32_r32(&dst, REG_EAX, REG_EAX);                              // cmp   eax,eax
5348      return dst;
5349   }
5350
5351   /* pick a target register for the general case */
5352   src1reg = param_select_register(REG_EAX, &src1p, NULL);
5353
5354   /* 32-bit form */
5355   if (inst->size == 4)
5356   {
5357      /* memory versus anything */
5358      if (src1p.type == DRCUML_PTYPE_MEMORY)
5359         emit_cmp_m32_p32(drcbe, &dst, MABS(src1p.value), &src2p, inst);            // cmp   [dstp],src2p
5360
5361      /* general case */
5362      else
5363      {
5364         if (src1p.type == DRCUML_PTYPE_IMMEDIATE)
5365            emit_mov_r32_imm(&dst, src1reg, src1p.value);                     // mov   src1reg,imm
5366         emit_cmp_r32_p32(drcbe, &dst, src1reg, &src2p, inst);                  // cmp   src1reg,src2p
5367      }
5368   }
5369
5370   /* 64-bit form */
5371   else
5372   {
5373      /* memory versus anything */
5374      if (src1p.type == DRCUML_PTYPE_MEMORY)
5375      {
5376         emit_cmp_m32_p32hi(drcbe, &dst, MABS(src1p.value + 4), &src2p, inst);      // cmp   [dstp],src2p.hi
5377         emit_jcc_short_link(&dst, COND_NE, &skip);                           // jne   skip
5378         emit_cmp_m32_p32(drcbe, &dst, MABS(src1p.value), &src2p, inst);            // cmp   [dstp],src2p
5379         resolve_link(&dst, &skip);                                    // skip:
5380      }
5381
5382      /* general case */
5383      else
5384      {
5385         if (src1p.type == DRCUML_PTYPE_IMMEDIATE)
5386            emit_mov_r32_imm(&dst, REG_EAX, src1p.value >> 32);                  // mov   eax,imm >> 32
5387         else if (src1p.type == DRCUML_PTYPE_INT_REGISTER)
5388            emit_mov_r32_m32(&dst, REG_EAX, MABS(drcbe->reghi[src1p.value]));      // mov   eax,reghi[src1p]
5389         emit_cmp_r32_p32hi(drcbe, &dst, REG_EAX, &src2p, inst);                  // cmp   eax,src2p.hi
5390         emit_jcc_short_link(&dst, COND_NE, &skip);                           // jne   skip
5391         if (src1p.type == DRCUML_PTYPE_IMMEDIATE)
5392            emit_mov_r32_imm(&dst, src1reg, src1p.value);                     // mov   src1reg,imm
5393         emit_cmp_r32_p32(drcbe, &dst, src1reg, &src2p, inst);                  // cmp   src1reg,src2p
5394         resolve_link(&dst, &skip);                                    // skip:
5395      }
5396   }
5397   return dst;
5398}
5399
5400
5401/*-------------------------------------------------
5402    op_mulu - process a MULU opcode
5403-------------------------------------------------*/
5404
5405static x86code *op_mulu(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
5406{
5407   drcuml_parameter dstp, edstp, src1p, src2p;
5408   int compute_hi;
5409
5410   /* validate instruction */
5411   assert(inst->size == 4 || inst->size == 8);
5412   assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
5413
5414   /* normalize parameters */
5415   param_normalize_4_commutative(drcbe, inst, &dstp, PTYPE_MR, &edstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
5416   compute_hi = (dstp.type != edstp.type || dstp.value != edstp.value);
5417
5418   /* degenerate cases -- convert to a move */
5419   if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0)
5420   {
5421      if (inst->size == 4)
5422      {
5423         if (!compute_hi)
5424            return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT32)src1p.value * (UINT32)src2p.value);
5425         else
5426         {
5427            UINT64 result = (UINT64)(UINT32)src1p.value * (UINT64)(UINT32)src2p.value;
5428            dst = convert_to_mov_imm(drcbe, dst, inst, &dstp, result & 0xffffffff);
5429            return convert_to_mov_imm(drcbe, dst, inst, &dstp, result >> 32);
5430         }
5431      }
5432      else if (inst->size == 8)
5433      {
5434         UINT64 reslo, reshi;
5435         if (!compute_hi)
5436         {
5437            dmulu(&reslo, &reslo, src1p.value, src2p.value);
5438            return convert_to_mov_imm(drcbe, dst, inst, &dstp, reslo);
5439         }
5440         else
5441         {
5442            dmulu(&reslo, &reshi, src1p.value, src2p.value);
5443            dst = convert_to_mov_imm(drcbe, dst, inst, &dstp, reslo);
5444            return convert_to_mov_imm(drcbe, dst, inst, &dstp, reshi);
5445         }
5446      }
5447   }
5448
5449   /* 32-bit form */
5450   if (inst->size == 4)
5451   {
5452      /* general case */
5453      emit_mov_r32_p32(drcbe, &dst, REG_EAX, &src1p);                           // mov   eax,src1p
5454      if (src2p.type == DRCUML_PTYPE_MEMORY)
5455         emit_mul_m32(&dst, MABS(src2p.value));                              // mul   [src2p]
5456      else if (src2p.type == DRCUML_PTYPE_INT_REGISTER)
5457         emit_mul_r32(&dst, src2p.value);                                 // mul   src2p
5458      else if (src2p.type == DRCUML_PTYPE_IMMEDIATE)
5459      {
5460         emit_mov_r32_imm(&dst, REG_EDX, src2p.value);                        // mov   edx,src2p
5461         emit_mul_r32(&dst, REG_EDX);                                    // mul   edx
5462      }
5463      emit_mov_p32_r32(drcbe, &dst, &dstp, REG_EAX);                           // mov   dstp,eax
5464      if (compute_hi)
5465         emit_mov_p32_r32(drcbe, &dst, &edstp, REG_EDX);                        // mov   edstp,edx
5466   }
5467
5468   /* 64-bit form */
5469   else if (inst->size == 8)
5470   {
5471      /* general case */
5472      emit_push_p64(drcbe, &dst, &src2p);                                    // push  src2p
5473      emit_push_p64(drcbe, &dst, &src1p);                                    // push  src1p
5474      if (!compute_hi)
5475         emit_push_imm(&dst, (FPTR)&drcbe->reslo);                           // push  &reslo
5476      else
5477         emit_push_imm(&dst, (FPTR)&drcbe->reshi);                           // push  &reshi
5478      emit_push_imm(&dst, (FPTR)&drcbe->reslo);                              // push  &reslo
5479      emit_call(&dst, (x86code *)dmulu);                                    // call  dmulu
5480      emit_add_r32_imm(&dst, REG_ESP, 24);                                 // add   esp,24
5481      emit_mov_r32_m32(&dst, REG_EAX, MABS((UINT32 *)&drcbe->reslo + 0));            // mov   eax,reslo.lo
5482      emit_mov_r32_m32(&dst, REG_EDX, MABS((UINT32 *)&drcbe->reslo + 1));            // mov   edx,reslo.hi
5483      emit_mov_p64_r64(drcbe, &dst, &dstp, REG_EAX, REG_EDX);                     // mov   dstp,edx:eax
5484      if (inst->condflags != 0)
5485         emit_or_r32_r32(&dst, REG_EAX, REG_EDX);                           // or    eax,edx
5486      if (compute_hi)
5487      {
5488         emit_mov_r32_m32(&dst, REG_EAX, MABS((UINT32 *)&drcbe->reshi + 0));         // mov   eax,reshi.lo
5489         emit_mov_r32_m32(&dst, REG_EDX, MABS((UINT32 *)&drcbe->reshi + 1));         // mov   edx,reshi.hi
5490         emit_mov_p64_r64(drcbe, &dst, &edstp, REG_EAX, REG_EDX);               // mov   edstp,edx:eax
5491      }
5492   }
5493   return dst;
5494}
5495
5496
5497/*-------------------------------------------------
5498    op_muls - process a MULS opcode
5499-------------------------------------------------*/
5500
5501static x86code *op_muls(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
5502{
5503   drcuml_parameter dstp, edstp, src1p, src2p;
5504   int compute_hi;
5505
5506   /* validate instruction */
5507   assert(inst->size == 4 || inst->size == 8);
5508   assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
5509
5510   /* normalize parameters */
5511   param_normalize_4_commutative(drcbe, inst, &dstp, PTYPE_MR, &edstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
5512   compute_hi = (dstp.type != edstp.type || dstp.value != edstp.value);
5513
5514   /* degenerate cases -- convert to a move */
5515   if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0)
5516   {
5517      if (inst->size == 4)
5518      {
5519         if (!compute_hi)
5520            return convert_to_mov_imm(drcbe, dst, inst, &dstp, (INT32)src1p.value * (INT32)src2p.value);
5521         else
5522         {
5523            UINT64 result = (INT64)(INT32)src1p.value * (INT64)(INT32)src2p.value;
5524            dst = convert_to_mov_imm(drcbe, dst, inst, &dstp, result & 0xffffffff);
5525            return convert_to_mov_imm(drcbe, dst, inst, &dstp, result >> 32);
5526         }
5527      }
5528      else if (inst->size == 8)
5529      {
5530         UINT64 reslo, reshi;
5531         if (!compute_hi)
5532         {
5533            dmuls(&reslo, &reslo, src1p.value, src2p.value);
5534            return convert_to_mov_imm(drcbe, dst, inst, &dstp, reslo);
5535         }
5536         else
5537         {
5538            dmuls(&reslo, &reshi, src1p.value, src2p.value);
5539            dst = convert_to_mov_imm(drcbe, dst, inst, &dstp, reslo);
5540            return convert_to_mov_imm(drcbe, dst, inst, &dstp, reshi);
5541         }
5542      }
5543   }
5544
5545   /* 32-bit form */
5546   if (inst->size == 4)
5547   {
5548      /* 32-bit destination with memory/immediate or register/immediate */
5549      if (!compute_hi && src1p.type != DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE)
5550      {
5551         if (src1p.type == DRCUML_PTYPE_MEMORY)
5552            emit_imul_r32_m32_imm(&dst, REG_EAX, MABS(src1p.value), src2p.value);   // imul  eax,[src1p],src2p
5553         else if (src1p.type == DRCUML_PTYPE_INT_REGISTER)
5554            emit_imul_r32_r32_imm(&dst, REG_EAX, src1p.value, src2p.value);         // imul  eax,src1p,src2p
5555         emit_mov_p32_r32(drcbe, &dst, &dstp, REG_EAX);                        // mov   dstp,eax
5556      }
5557
5558      /* 32-bit destination, general case */
5559      else if (!compute_hi)
5560      {
5561         emit_mov_r32_p32(drcbe, &dst, REG_EAX, &src1p);                        // mov   eax,src1p
5562         if (src2p.type == DRCUML_PTYPE_MEMORY)
5563            emit_imul_r32_m32(&dst, REG_EAX, MABS(src2p.value));               // imul  eax,[src2p]
5564         else if (src2p.type == DRCUML_PTYPE_INT_REGISTER)
5565            emit_imul_r32_r32(&dst, REG_EAX, src2p.value);                     // imul  eax,src2p
5566         emit_mov_p32_r32(drcbe, &dst, &dstp, REG_EAX);                        // mov   dstp,eax
5567      }
5568
5569      /* 64-bit destination, general case */
5570      else
5571      {
5572         emit_mov_r32_p32(drcbe, &dst, REG_EAX, &src1p);                        // mov   eax,src1p
5573         if (src2p.type == DRCUML_PTYPE_MEMORY)
5574            emit_imul_m32(&dst, MABS(src2p.value));                           // imul  [src2p]
5575         else if (src2p.type == DRCUML_PTYPE_INT_REGISTER)
5576            emit_imul_r32(&dst, src2p.value);                              // imul  src2p
5577         else if (src2p.type == DRCUML_PTYPE_IMMEDIATE)
5578         {
5579            emit_mov_r32_imm(&dst, REG_EDX, src2p.value);                     // mov   edx,src2p
5580            emit_imul_r32(&dst, REG_EDX);                                 // imul  edx
5581         }
5582         emit_mov_p32_r32(drcbe, &dst, &dstp, REG_EAX);                        // mov   dstp,eax
5583         emit_mov_p32_r32(drcbe, &dst, &edstp, REG_EDX);                        // mov   edstp,edx
5584      }
5585   }
5586
5587   /* 64-bit form */
5588   else if (inst->size == 8)
5589   {
5590      /* general case */
5591      emit_push_p64(drcbe, &dst, &src2p);                                    // push  src2p
5592      emit_push_p64(drcbe, &dst, &src1p);                                    // push  src1p
5593      if (!compute_hi)
5594         emit_push_imm(&dst, (FPTR)&drcbe->reslo);                           // push  &reslo
5595      else
5596         emit_push_imm(&dst, (FPTR)&drcbe->reshi);                           // push  &reshi
5597      emit_push_imm(&dst, (FPTR)&drcbe->reslo);                              // push  &reslo
5598      emit_call(&dst, (x86code *)dmuls);                                    // call  dmuls
5599      emit_add_r32_imm(&dst, REG_ESP, 24);                                 // add   esp,24
5600      emit_mov_r32_m32(&dst, REG_EAX, MABS((UINT32 *)&drcbe->reslo + 0));            // mov   eax,reslo.lo
5601      emit_mov_r32_m32(&dst, REG_EDX, MABS((UINT32 *)&drcbe->reslo + 1));            // mov   edx,reslo.hi
5602      emit_mov_p64_r64(drcbe, &dst, &dstp, REG_EAX, REG_EDX);                     // mov   dstp,edx:eax
5603      if (inst->condflags != 0)
5604         emit_or_r32_r32(&dst, REG_EAX, REG_EDX);                           // or    eax,edx
5605      if (compute_hi)
5606      {
5607         emit_mov_r32_m32(&dst, REG_EAX, MABS((UINT32 *)&drcbe->reshi + 0));         // mov   eax,reshi.lo
5608         emit_mov_r32_m32(&dst, REG_EDX, MABS((UINT32 *)&drcbe->reshi + 1));         // mov   edx,reshi.hi
5609         emit_mov_p64_r64(drcbe, &dst, &edstp, REG_EAX, REG_EDX);               // mov   edstp,edx:eax
5610      }
5611   }
5612   return dst;
5613}
5614
5615
5616/*-------------------------------------------------
5617    op_divu - process a DIVU opcode
5618-------------------------------------------------*/
5619
5620static x86code *op_divu(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
5621{
5622   drcuml_parameter dstp, edstp, src1p, src2p;
5623   int compute_rem;
5624   emit_link skip;
5625
5626   /* validate instruction */
5627   assert(inst->size == 4 || inst->size == 8);
5628   assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
5629
5630   /* normalize parameters */
5631   param_normalize_4(drcbe, inst, &dstp, PTYPE_MR, &edstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
5632   compute_rem = (dstp.type != edstp.type || dstp.value != edstp.value);
5633
5634   /* degenerate cases -- convert to a move */
5635   if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0)
5636   {
5637      if (inst->size == 4)
5638      {
5639         if ((UINT32)src2p.value == 0)
5640            return dst;
5641         if (!compute_rem)
5642            return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT32)src1p.value / (UINT32)src2p.value);
5643         else
5644         {
5645            dst = convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT32)src1p.value / (UINT32)src2p.value);
5646            return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT32)src1p.value % (UINT32)src2p.value);
5647         }
5648      }
5649      else if (inst->size == 8)
5650      {
5651         UINT64 reslo, reshi;
5652         if (src2p.value == 0)
5653            return dst;
5654         if (!compute_rem)
5655         {
5656            ddivu(&reslo, &reslo, src1p.value, src2p.value);
5657            return convert_to_mov_imm(drcbe, dst, inst, &dstp, reslo);
5658         }
5659         else
5660         {
5661            ddivu(&reslo, &reshi, src1p.value, src2p.value);
5662            dst = convert_to_mov_imm(drcbe, dst, inst, &dstp, reslo);
5663            return convert_to_mov_imm(drcbe, dst, inst, &dstp, reshi);
5664         }
5665      }
5666   }
5667
5668   /* 32-bit form */
5669   if (inst->size == 4)
5670   {
5671      /* general case */
5672      emit_mov_r32_p32(drcbe, &dst, REG_ECX, &src2p);                           // mov   ecx,src2p
5673      emit_jecxz_link(&dst, &skip);                                       // jecxz skip
5674      emit_mov_r32_p32(drcbe, &dst, REG_EAX, &src1p);                           // mov   eax,src1p
5675      emit_xor_r32_r32(&dst, REG_EDX, REG_EDX);                              // xor   edx,edx
5676      emit_div_r32(&dst, REG_ECX);                                       // div   ecx
5677      emit_mov_p32_r32(drcbe, &dst, &dstp, REG_EAX);                           // mov   dstp,eax
5678      if (compute_rem)
5679         emit_mov_p32_r32(drcbe, &dst, &edstp, REG_EDX);                        // mov   edstp,edx
5680      resolve_link(&dst, &skip);                                       // skip:
5681   }
5682
5683   /* 64-bit form */
5684   else if (inst->size == 8)
5685   {
5686      /* general case */
5687      emit_push_p64(drcbe, &dst, &src2p);                                    // push  src2p
5688      emit_push_p64(drcbe, &dst, &src1p);                                    // push  src1p
5689      if (!compute_rem)
5690         emit_push_imm(&dst, (FPTR)&drcbe->reslo);                           // push  &reslo
5691      else
5692         emit_push_imm(&dst, (FPTR)&drcbe->reshi);                           // push  &reshi
5693      emit_push_imm(&dst, (FPTR)&drcbe->reslo);                              // push  &reslo
5694      emit_call(&dst, (x86code *)ddivu);                                    // call  ddivu
5695      emit_add_r32_imm(&dst, REG_ESP, 24);                                 // add   esp,24
5696      emit_mov_r32_m32(&dst, REG_EAX, MABS((UINT32 *)&drcbe->reslo + 0));            // mov   eax,reslo.lo
5697      emit_mov_r32_m32(&dst, REG_EDX, MABS((UINT32 *)&drcbe->reslo + 1));            // mov   edx,reslo.hi
5698      emit_mov_p64_r64(drcbe, &dst, &dstp, REG_EAX, REG_EDX);                     // mov   dstp,edx:eax
5699      if (inst->condflags != 0)
5700         emit_or_r32_r32(&dst, REG_EAX, REG_EDX);                           // or    eax,edx
5701      if (compute_rem)
5702      {
5703         emit_mov_r32_m32(&dst, REG_EAX, MABS((UINT32 *)&drcbe->reshi + 0));         // mov   eax,reshi.lo
5704         emit_mov_r32_m32(&dst, REG_EDX, MABS((UINT32 *)&drcbe->reshi + 1));         // mov   edx,reshi.hi
5705         emit_mov_p64_r64(drcbe, &dst, &edstp, REG_EAX, REG_EDX);               // mov   edstp,edx:eax
5706      }
5707   }
5708   return dst;
5709}
5710
5711
5712/*-------------------------------------------------
5713    op_divs - process a DIVS opcode
5714-------------------------------------------------*/
5715
5716static x86code *op_divs(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
5717{
5718   drcuml_parameter dstp, edstp, src1p, src2p;
5719   int compute_rem;
5720   emit_link skip;
5721
5722   /* validate instruction */
5723   assert(inst->size == 4 || inst->size == 8);
5724   assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
5725
5726   /* normalize parameters */
5727   param_normalize_4(drcbe, inst, &dstp, PTYPE_MR, &edstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
5728   compute_rem = (dstp.type != edstp.type || dstp.value != edstp.value);
5729
5730   /* degenerate cases -- convert to a move */
5731   if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0)
5732   {
5733      if (inst->size == 4)
5734      {
5735         if ((INT32)src2p.value == 0)
5736            return dst;
5737         if (!compute_rem)
5738            return convert_to_mov_imm(drcbe, dst, inst, &dstp, (INT32)src1p.value / (INT32)src2p.value);
5739         else
5740         {
5741            dst = convert_to_mov_imm(drcbe, dst, inst, &dstp, (INT32)src1p.value / (INT32)src2p.value);
5742            return convert_to_mov_imm(drcbe, dst, inst, &dstp, (INT32)src1p.value % (INT32)src2p.value);
5743         }
5744      }
5745      else if (inst->size == 8)
5746      {
5747         UINT64 reslo, reshi;
5748         if (src2p.value == 0)
5749            return dst;
5750         if (!compute_rem)
5751         {
5752            ddivs(&reslo, &reslo, src1p.value, src2p.value);
5753            return convert_to_mov_imm(drcbe, dst, inst, &dstp, reslo);
5754         }
5755         else
5756         {
5757            ddivs(&reslo, &reshi, src1p.value, src2p.value);
5758            dst = convert_to_mov_imm(drcbe, dst, inst, &dstp, reslo);
5759            return convert_to_mov_imm(drcbe, dst, inst, &dstp, reshi);
5760         }
5761      }
5762   }
5763
5764   /* 32-bit form */
5765   if (inst->size == 4)
5766   {
5767      /* general case */
5768      emit_mov_r32_p32(drcbe, &dst, REG_ECX, &src2p);                           // mov   ecx,src2p
5769      emit_jecxz_link(&dst, &skip);                                       // jecxz skip
5770      emit_mov_r32_p32(drcbe, &dst, REG_EAX, &src1p);                           // mov   eax,src1p
5771      emit_cdq(&dst);                                                   // cdq
5772      emit_idiv_r32(&dst, REG_ECX);                                       // idiv  ecx
5773      emit_mov_p32_r32(drcbe, &dst, &dstp, REG_EAX);                           // mov   dstp,eax
5774      if (compute_rem)
5775         emit_mov_p32_r32(drcbe, &dst, &edstp, REG_EDX);                        // mov   edstp,edx
5776      resolve_link(&dst, &skip);                                       // skip:
5777   }
5778
5779   /* 64-bit form */
5780   else if (inst->size == 8)
5781   {
5782      /* general case */
5783      emit_push_p64(drcbe, &dst, &src2p);                                    // push  src2p
5784      emit_push_p64(drcbe, &dst, &src1p);                                    // push  src1p
5785      if (!compute_rem)
5786         emit_push_imm(&dst, (FPTR)&drcbe->reslo);                           // push  &reslo
5787      else
5788         emit_push_imm(&dst, (FPTR)&drcbe->reshi);                           // push  &reshi
5789      emit_push_imm(&dst, (FPTR)&drcbe->reslo);                              // push  &reslo
5790      emit_call(&dst, (x86code *)ddivs);                                    // call  ddivs
5791      emit_add_r32_imm(&dst, REG_ESP, 24);                                 // add   esp,24
5792      emit_mov_r32_m32(&dst, REG_EAX, MABS((UINT32 *)&drcbe->reslo + 0));            // mov   eax,reslo.lo
5793      emit_mov_r32_m32(&dst, REG_EDX, MABS((UINT32 *)&drcbe->reslo + 1));            // mov   edx,reslo.hi
5794      emit_mov_p64_r64(drcbe, &dst, &dstp, REG_EAX, REG_EDX);                     // mov   dstp,edx:eax
5795      if (inst->condflags != 0)
5796         emit_or_r32_r32(&dst, REG_EAX, REG_EDX);                           // or    eax,edx
5797      if (compute_rem)
5798      {
5799         emit_mov_r32_m32(&dst, REG_EAX, MABS((UINT32 *)&drcbe->reshi + 0));         // mov   eax,reshi.lo
5800         emit_mov_r32_m32(&dst, REG_EDX, MABS((UINT32 *)&drcbe->reshi + 1));         // mov   edx,reshi.hi
5801         emit_mov_p64_r64(drcbe, &dst, &edstp, REG_EAX, REG_EDX);               // mov   edstp,edx:eax
5802      }
5803   }
5804   return dst;
5805}
5806
5807
5808/*-------------------------------------------------
5809    op_and - process a AND opcode
5810-------------------------------------------------*/
5811
5812static x86code *op_and(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
5813{
5814   drcuml_parameter dstp, src1p, src2p;
5815   int dstreg;
5816
5817   /* validate instruction */
5818   assert(inst->size == 4 || inst->size == 8);
5819   assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
5820
5821   /* normalize parameters */
5822   param_normalize_3_commutative(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
5823
5824   /* degenerate cases -- convert to a move */
5825   if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0)
5826      return convert_to_mov_imm(drcbe, dst, inst, &dstp, src1p.value & src2p.value);
5827   if (src2p.type == DRCUML_PTYPE_IMMEDIATE && (src2p.value & size_to_mask[inst->size]) == size_to_mask[inst->size] && inst->condflags == 0)
5828      return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p);
5829
5830   /* pick a target register for the general case */
5831   dstreg = param_select_register(REG_EAX, &dstp, &src2p);
5832
5833   /* 32-bit form */
5834   if (inst->size == 4)
5835   {
5836      /* dstp == src1p in memory */
5837      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
5838         emit_and_m32_p32(drcbe, &dst, MABS(dstp.value), &src2p, inst);            // and   [dstp],src2p
5839
5840      /* general case */
5841      else
5842      {
5843         emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
5844         emit_and_r32_p32(drcbe, &dst, dstreg, &src2p, inst);                  // and   dstreg,src2p
5845         emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
5846      }
5847   }
5848
5849   /* 64-bit form */
5850   else if (inst->size == 8)
5851   {
5852      /* dstp == src1p in memory */
5853      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
5854         emit_and_m64_p64(drcbe, &dst, MABS(dstp.value), &src2p, inst);            // and   [dstp],src2p
5855
5856      /* general case */
5857      else
5858      {
5859         emit_mov_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src1p);                  // mov   dstreg:dstp,[src1p]
5860         emit_and_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src2p, inst);            // and   dstreg:dstp,src2p
5861         emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg, REG_EDX);                  // mov   dstp,dstreg:eax
5862      }
5863   }
5864   return dst;
5865}
5866
5867
5868/*-------------------------------------------------
5869    op_test - process a TEST opcode
5870-------------------------------------------------*/
5871
5872static x86code *op_test(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
5873{
5874   drcuml_parameter src1p, src2p;
5875   int src1reg;
5876
5877   /* validate instruction */
5878   assert(inst->size == 4 || inst->size == 8);
5879   assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
5880
5881   /* normalize parameters */
5882   param_normalize_2_commutative(drcbe, inst, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
5883
5884   /* pick a target register for the general case */
5885   src1reg = param_select_register(REG_EAX, &src1p, NULL);
5886
5887   /* 32-bit form */
5888   if (inst->size == 4)
5889   {
5890      /* src1p in memory */
5891      if (src1p.type == DRCUML_PTYPE_MEMORY)
5892         emit_test_m32_p32(drcbe, &dst, MABS(src1p.value), &src2p, inst);         // test  [src1p],src2p
5893
5894      /* general case */
5895      else
5896      {
5897         emit_mov_r32_p32(drcbe, &dst, src1reg, &src1p);                        // mov   src1reg,src1p
5898         emit_test_r32_p32(drcbe, &dst, src1reg, &src2p, inst);                  // test  src1reg,src2p
5899      }
5900   }
5901
5902   /* 64-bit form */
5903   else if (inst->size == 8)
5904   {
5905      /* src1p in memory */
5906      if (src1p.type == DRCUML_PTYPE_MEMORY)
5907         emit_test_m64_p64(drcbe, &dst, MABS(src1p.value), &src2p, inst);         // test  [dstp],src2p
5908
5909      /* general case */
5910      else
5911      {
5912         emit_mov_r64_p64(drcbe, &dst, src1reg, REG_EDX, &src1p);               // mov   src1reg:dstp,[src1p]
5913         emit_test_r64_p64(drcbe, &dst, src1reg, REG_EDX, &src2p, inst);            // test  src1reg:dstp,src2p
5914      }
5915   }
5916   return dst;
5917}
5918
5919
5920/*-------------------------------------------------
5921    op_or - process a OR opcode
5922-------------------------------------------------*/
5923
5924static x86code *op_or(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
5925{
5926   drcuml_parameter dstp, src1p, src2p;
5927   int dstreg;
5928
5929   /* validate instruction */
5930   assert(inst->size == 4 || inst->size == 8);
5931   assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
5932
5933   /* normalize parameters */
5934   param_normalize_3_commutative(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
5935
5936   /* degenerate cases -- convert to a move */
5937   if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0)
5938      return convert_to_mov_imm(drcbe, dst, inst, &dstp, src1p.value | src2p.value);
5939   if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0)
5940      return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p);
5941
5942   /* pick a target register for the general case */
5943   dstreg = param_select_register(REG_EAX, &dstp, &src2p);
5944
5945   /* 32-bit form */
5946   if (inst->size == 4)
5947   {
5948      /* dstp == src1p in memory */
5949      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
5950         emit_or_m32_p32(drcbe, &dst, MABS(dstp.value), &src2p, inst);            // or    [dstp],src2p
5951
5952      /* general case */
5953      else
5954      {
5955         emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
5956         emit_or_r32_p32(drcbe, &dst, dstreg, &src2p, inst);                     // or    dstreg,src2p
5957         emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
5958      }
5959   }
5960
5961   /* 64-bit form */
5962   else if (inst->size == 8)
5963   {
5964      /* dstp == src1p in memory */
5965      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
5966         emit_or_m64_p64(drcbe, &dst, MABS(dstp.value), &src2p, inst);            // or    [dstp],src2p
5967
5968      /* general case */
5969      else
5970      {
5971         emit_mov_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src1p);                  // mov   dstreg:dstp,[src1p]
5972         emit_or_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src2p, inst);            // or    dstreg:dstp,src2p
5973         emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg, REG_EDX);                  // mov   dstp,dstreg:eax
5974      }
5975   }
5976   return dst;
5977}
5978
5979
5980/*-------------------------------------------------
5981    op_xor - process a XOR opcode
5982-------------------------------------------------*/
5983
5984static x86code *op_xor(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
5985{
5986   drcuml_parameter dstp, src1p, src2p;
5987   int dstreg;
5988
5989   /* validate instruction */
5990   assert(inst->size == 4 || inst->size == 8);
5991   assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
5992
5993   /* normalize parameters */
5994   param_normalize_3_commutative(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
5995
5996   /* degenerate cases -- convert to a move */
5997   if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0)
5998      return convert_to_mov_imm(drcbe, dst, inst, &dstp, src1p.value & src2p.value);
5999   if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0)
6000      return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p);
6001
6002   /* pick a target register for the general case */
6003   dstreg = param_select_register(REG_EAX, &dstp, &src2p);
6004
6005   /* 32-bit form */
6006   if (inst->size == 4)
6007   {
6008      /* dstp == src1p in memory */
6009      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
6010         emit_xor_m32_p32(drcbe, &dst, MABS(dstp.value), &src2p, inst);            // xor   [dstp],src2p
6011
6012      /* general case */
6013      else
6014      {
6015         emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
6016         emit_xor_r32_p32(drcbe, &dst, dstreg, &src2p, inst);                  // xor   dstreg,src2p
6017         emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
6018      }
6019   }
6020
6021   /* 64-bit form */
6022   else if (inst->size == 8)
6023   {
6024      /* dstp == src1p in memory */
6025      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
6026         emit_xor_m64_p64(drcbe, &dst, MABS(dstp.value), &src2p, inst);            // xor   [dstp],src2p
6027
6028      /* general case */
6029      else
6030      {
6031         emit_mov_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src1p);                  // mov   dstreg:dstp,[src1p]
6032         emit_xor_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src2p, inst);            // xor   dstreg:dstp,src2p
6033         emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg, REG_EDX);                  // mov   dstp,dstreg:eax
6034      }
6035   }
6036   return dst;
6037}
6038
6039
6040/*-------------------------------------------------
6041    op_shl - process a SHL opcode
6042-------------------------------------------------*/
6043
6044static x86code *op_shl(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6045{
6046   drcuml_parameter dstp, src1p, src2p;
6047   int dstreg;
6048
6049   /* validate instruction */
6050   assert(inst->size == 4 || inst->size == 8);
6051   assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
6052
6053   /* normalize parameters */
6054   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
6055
6056   /* degenerate cases -- convert to a move */
6057   if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0)
6058      return convert_to_mov_imm(drcbe, dst, inst, &dstp, src1p.value << src2p.value);
6059   if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0)
6060      return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p);
6061
6062   /* pick a target register for the general case */
6063   dstreg = param_select_register(REG_EAX, &dstp, &src2p);
6064
6065   /* 32-bit form */
6066   if (inst->size == 4)
6067   {
6068      /* dstp == src1p in memory */
6069      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
6070         emit_shl_m32_p32(drcbe, &dst, MABS(dstp.value), &src2p, inst);            // shl   [dstp],src2p
6071
6072      /* general case */
6073      else
6074      {
6075         emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
6076         emit_shl_r32_p32(drcbe, &dst, dstreg, &src2p, inst);                  // shl   dstreg,src2p
6077         emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
6078      }
6079   }
6080
6081   /* 64-bit form */
6082   else if (inst->size == 8)
6083   {
6084      /* general case */
6085      emit_mov_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src1p);                     // mov   dstreg:dstp,[src1p]
6086      emit_shl_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src2p, inst);               // shl   dstreg:dstp,src2p
6087      emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg, REG_EDX);                     // mov   dstp,dstreg:eax
6088   }
6089   return dst;
6090}
6091
6092
6093/*-------------------------------------------------
6094    op_shr - process a SHR opcode
6095-------------------------------------------------*/
6096
6097static x86code *op_shr(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6098{
6099   drcuml_parameter dstp, src1p, src2p;
6100   int dstreg;
6101
6102   /* validate instruction */
6103   assert(inst->size == 4 || inst->size == 8);
6104   assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
6105
6106   /* normalize parameters */
6107   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
6108
6109   /* degenerate cases -- convert to a move */
6110   if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0)
6111      return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT64)src1p.value >> src2p.value);
6112   if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0)
6113      return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p);
6114
6115   /* pick a target register for the general case */
6116   dstreg = param_select_register(REG_EAX, &dstp, &src2p);
6117
6118   /* 32-bit form */
6119   if (inst->size == 4)
6120   {
6121      /* dstp == src1p in memory */
6122      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
6123         emit_shr_m32_p32(drcbe, &dst, MABS(dstp.value), &src2p, inst);            // shr   [dstp],src2p
6124
6125      /* general case */
6126      else
6127      {
6128         emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
6129         emit_shr_r32_p32(drcbe, &dst, dstreg, &src2p, inst);                  // shr   dstreg,src2p
6130         emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
6131      }
6132   }
6133
6134   /* 64-bit form */
6135   else if (inst->size == 8)
6136   {
6137      /* general case */
6138      emit_mov_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src1p);                     // mov   dstreg:dstp,[src1p]
6139      emit_shr_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src2p, inst);               // shr   dstreg:dstp,src2p
6140      emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg, REG_EDX);                     // mov   dstp,dstreg:eax
6141   }
6142   return dst;
6143}
6144
6145
6146/*-------------------------------------------------
6147    op_sar - process a SAR opcode
6148-------------------------------------------------*/
6149
6150static x86code *op_sar(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6151{
6152   drcuml_parameter dstp, src1p, src2p;
6153   int dstreg;
6154
6155   /* validate instruction */
6156   assert(inst->size == 4 || inst->size == 8);
6157   assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
6158
6159   /* normalize parameters */
6160   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
6161
6162   /* degenerate cases -- convert to a move */
6163   if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0)
6164   {
6165      if (inst->size == 4)
6166         return convert_to_mov_imm(drcbe, dst, inst, &dstp, (INT32)src1p.value >> src2p.value);
6167      else if (inst->size == 8)
6168         return convert_to_mov_imm(drcbe, dst, inst, &dstp, (INT64)src1p.value >> src2p.value);
6169   }
6170   if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0)
6171      return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p);
6172
6173   /* pick a target register for the general case */
6174   dstreg = param_select_register(REG_EAX, &dstp, &src2p);
6175
6176   /* 32-bit form */
6177   if (inst->size == 4)
6178   {
6179      /* dstp == src1p in memory */
6180      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
6181         emit_sar_m32_p32(drcbe, &dst, MABS(dstp.value), &src2p, inst);            // sar   [dstp],src2p
6182
6183      /* general case */
6184      else
6185      {
6186         emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
6187         emit_sar_r32_p32(drcbe, &dst, dstreg, &src2p, inst);                  // sar   dstreg,src2p
6188         emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
6189      }
6190   }
6191
6192   /* 64-bit form */
6193   else if (inst->size == 8)
6194   {
6195      /* general case */
6196      emit_mov_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src1p);                     // mov   dstreg:dstp,[src1p]
6197      emit_sar_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src2p, inst);               // sar   dstreg:dstp,src2p
6198      emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg, REG_EDX);                     // mov   dstp,dstreg:eax
6199   }
6200   return dst;
6201}
6202
6203
6204/*-------------------------------------------------
6205    op_rol - process a rol opcode
6206-------------------------------------------------*/
6207
6208static x86code *op_rol(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6209{
6210   drcuml_parameter dstp, src1p, src2p;
6211   int dstreg;
6212
6213   /* validate instruction */
6214   assert(inst->size == 4 || inst->size == 8);
6215   assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
6216
6217   /* normalize parameters */
6218   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
6219
6220   /* degenerate cases -- convert to a move */
6221   if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0)
6222      return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT64)src1p.value >> src2p.value);
6223   if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0)
6224      return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p);
6225
6226   /* pick a target register for the general case */
6227   dstreg = param_select_register(REG_EAX, &dstp, &src2p);
6228
6229   /* 32-bit form */
6230   if (inst->size == 4)
6231   {
6232      /* dstp == src1p in memory */
6233      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
6234         emit_rol_m32_p32(drcbe, &dst, MABS(dstp.value), &src2p, inst);            // rol   [dstp],src2p
6235
6236      /* general case */
6237      else
6238      {
6239         emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
6240         emit_rol_r32_p32(drcbe, &dst, dstreg, &src2p, inst);                  // rol   dstreg,src2p
6241         emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
6242      }
6243   }
6244
6245   /* 64-bit form */
6246   else if (inst->size == 8)
6247   {
6248      /* general case */
6249      emit_mov_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src1p);                     // mov   dstreg:dstp,[src1p]
6250      emit_rol_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src2p, inst);               // rol   dstreg:dstp,src2p
6251      emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg, REG_EDX);                     // mov   dstp,dstreg:eax
6252   }
6253   return dst;
6254}
6255
6256
6257/*-------------------------------------------------
6258    op_ror - process a ROR opcode
6259-------------------------------------------------*/
6260
6261static x86code *op_ror(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6262{
6263   drcuml_parameter dstp, src1p, src2p;
6264   int dstreg;
6265
6266   /* validate instruction */
6267   assert(inst->size == 4 || inst->size == 8);
6268   assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
6269
6270   /* normalize parameters */
6271   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
6272
6273   /* degenerate cases -- convert to a move */
6274   if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0)
6275      return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT64)src1p.value >> src2p.value);
6276   if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0)
6277      return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p);
6278
6279   /* pick a target register for the general case */
6280   dstreg = param_select_register(REG_EAX, &dstp, &src2p);
6281
6282   /* 32-bit form */
6283   if (inst->size == 4)
6284   {
6285      /* dstp == src1p in memory */
6286      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
6287         emit_ror_m32_p32(drcbe, &dst, MABS(dstp.value), &src2p, inst);            // ror   [dstp],src2p
6288
6289      /* general case */
6290      else
6291      {
6292         emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
6293         emit_ror_r32_p32(drcbe, &dst, dstreg, &src2p, inst);                  // ror   dstreg,src2p
6294         emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
6295      }
6296   }
6297
6298   /* 64-bit form */
6299   else if (inst->size == 8)
6300   {
6301      /* general case */
6302      emit_mov_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src1p);                     // mov   dstreg:dstp,[src1p]
6303      emit_ror_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src2p, inst);               // ror   dstreg:dstp,src2p
6304      emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg, REG_EDX);                     // mov   dstp,dstreg:eax
6305   }
6306   return dst;
6307}
6308
6309
6310/*-------------------------------------------------
6311    op_rolc - process a ROLC opcode
6312-------------------------------------------------*/
6313
6314static x86code *op_rolc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6315{
6316   drcuml_parameter dstp, src1p, src2p;
6317   int dstreg;
6318
6319   /* validate instruction */
6320   assert(inst->size == 4 || inst->size == 8);
6321   assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
6322
6323   /* normalize parameters */
6324   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
6325
6326   /* degenerate cases -- convert to a move */
6327   if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0)
6328      return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT64)src1p.value >> src2p.value);
6329   if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0)
6330      return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p);
6331
6332   /* pick a target register for the general case */
6333   dstreg = param_select_register(REG_EAX, &dstp, &src2p);
6334
6335   /* 32-bit form */
6336   if (inst->size == 4)
6337   {
6338      /* dstp == src1p in memory */
6339      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
6340         emit_rcl_m32_p32(drcbe, &dst, MABS(dstp.value), &src2p, inst);            // rcl   [dstp],src2p
6341
6342      /* general case */
6343      else
6344      {
6345         emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
6346         emit_rcl_r32_p32(drcbe, &dst, dstreg, &src2p, inst);                  // rcl   dstreg,src2p
6347         emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
6348      }
6349   }
6350
6351   /* 64-bit form */
6352   else if (inst->size == 8)
6353   {
6354      /* general case */
6355      emit_mov_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src1p);                     // mov   dstreg:dstp,[src1p]
6356      emit_rcl_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src2p, inst);               // rcl   dstreg:dstp,src2p
6357      emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg, REG_EDX);                     // mov   dstp,dstreg:eax
6358   }
6359   return dst;
6360}
6361
6362
6363/*-------------------------------------------------
6364    op_rorc - process a RORC opcode
6365-------------------------------------------------*/
6366
6367static x86code *op_rorc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6368{
6369   drcuml_parameter dstp, src1p, src2p;
6370   int dstreg;
6371
6372   /* validate instruction */
6373   assert(inst->size == 4 || inst->size == 8);
6374   assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
6375
6376   /* normalize parameters */
6377   param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI);
6378
6379   /* degenerate cases -- convert to a move */
6380   if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0)
6381      return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT64)src1p.value >> src2p.value);
6382   if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0)
6383      return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p);
6384
6385   /* pick a target register for the general case */
6386   dstreg = param_select_register(REG_EAX, &dstp, &src2p);
6387
6388   /* 32-bit form */
6389   if (inst->size == 4)
6390   {
6391      /* dstp == src1p in memory */
6392      if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY)
6393         emit_rcr_m32_p32(drcbe, &dst, MABS(dstp.value), &src2p, inst);            // rcr   [dstp],src2p
6394
6395      /* general case */
6396      else
6397      {
6398         emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p);                        // mov   dstreg,src1p
6399         emit_rcr_r32_p32(drcbe, &dst, dstreg, &src2p, inst);                  // rcr   dstreg,src2p
6400         emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg);                        // mov   dstp,dstreg
6401      }
6402   }
6403
6404   /* 64-bit form */
6405   else if (inst->size == 8)
6406   {
6407      /* general case */
6408      emit_mov_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src1p);                     // mov   dstreg:dstp,[src1p]
6409      emit_rcr_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src2p, inst);               // rcr   dstreg:dstp,src2p
6410      emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg, REG_EDX);                     // mov   dstp,dstreg:eax
6411   }
6412   return dst;
6413}
6414
6415
6416
6417/***************************************************************************
6418    FLOATING POINT OPERATIONS
6419***************************************************************************/
6420
6421/*-------------------------------------------------
6422    op_fload - process a FLOAD opcode
6423-------------------------------------------------*/
6424
6425static x86code *op_fload(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6426{
6427   drcuml_parameter dstp, basep, indp;
6428
6429   /* validate instruction */
6430   assert(inst->size == 4 || inst->size == 8);
6431   assert(inst->condflags == DRCUML_COND_ALWAYS);
6432
6433   /* normalize parameters */
6434   param_normalize_3(drcbe, inst, &dstp, PTYPE_MF, &basep, PTYPE_M, &indp, PTYPE_MRI);
6435
6436   /* immediate index */
6437   if (indp.type == DRCUML_PTYPE_IMMEDIATE)
6438   {
6439      if (inst->size == 4)
6440         emit_fld_m32(&dst, MABS(basep.value + 4*indp.value));                  // fld   [basep + 4*indp]
6441      else if (inst->size == 8)
6442         emit_fld_m64(&dst, MABS(basep.value + 8*indp.value));                  // fld   [basep + 8*indp]
6443   }
6444
6445   /* other index */
6446   else
6447   {
6448      int indreg = param_select_register(REG_ECX, &indp, NULL);
6449      emit_mov_r32_p32(drcbe, &dst, indreg, &indp);
6450      if (inst->size == 4)
6451         emit_fld_m32(&dst, MISD(indreg, 4, basep.value));                     // fld   [basep + 4*indp]
6452      else if (inst->size == 8)
6453         emit_fld_m64(&dst, MISD(indreg, 8, basep.value));                     // fld   [basep + 8*indp]
6454   }
6455
6456   /* general case */
6457   emit_fstp_p(&dst, inst->size, &dstp);                                    // fstp  dstp
6458
6459   return dst;
6460}
6461
6462
6463/*-------------------------------------------------
6464    op_fstore - process a FSTORE opcode
6465-------------------------------------------------*/
6466
6467static x86code *op_fstore(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6468{
6469   drcuml_parameter srcp, basep, indp;
6470
6471   /* validate instruction */
6472   assert(inst->size == 4 || inst->size == 8);
6473   assert(inst->condflags == DRCUML_COND_ALWAYS);
6474
6475   /* normalize parameters */
6476   param_normalize_3(drcbe, inst, &basep, PTYPE_M, &indp, PTYPE_MRI, &srcp, PTYPE_MF);
6477
6478   /* general case */
6479   emit_fld_p(&dst, inst->size, &srcp);
6480
6481   /* immediate index */
6482   if (indp.type == DRCUML_PTYPE_IMMEDIATE)
6483   {
6484      if (inst->size == 4)
6485         emit_fstp_m32(&dst, MABS(basep.value + 4*indp.value));                  // fstp  [basep + 4*indp]
6486      else if (inst->size == 8)
6487         emit_fstp_m64(&dst, MABS(basep.value + 8*indp.value));                  // fstp  [basep + 8*indp]
6488   }
6489
6490   /* other index */
6491   else
6492   {
6493      int indreg = param_select_register(REG_ECX, &indp, NULL);
6494      emit_mov_r32_p32(drcbe, &dst, indreg, &indp);
6495      if (inst->size == 4)
6496         emit_fstp_m32(&dst, MISD(indreg, 4, basep.value));                     // fstp  [basep + 4*indp]
6497      else if (inst->size == 8)
6498         emit_fstp_m64(&dst, MISD(indreg, 8, basep.value));                     // fstp  [basep + 8*indp]
6499   }
6500   return dst;
6501}
6502
6503
6504/*-------------------------------------------------
6505    op_fread - process a FREAD opcode
6506-------------------------------------------------*/
6507
6508static x86code *op_fread(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6509{
6510   drcuml_parameter dstp, spacep, addrp;
6511
6512   /* validate instruction */
6513   assert(inst->size == 4 || inst->size == 8);
6514   assert(inst->condflags == DRCUML_COND_ALWAYS);
6515
6516   /* normalize parameters */
6517   param_normalize_3(drcbe, inst, &dstp, PTYPE_MF, &spacep, PTYPE_I, &addrp, PTYPE_MRI);
6518
6519   /* set up a call to the read dword/qword handler */
6520   emit_push_p32(drcbe, &dst, &addrp);                                       // push   addrp
6521   if (inst->size == 4)
6522      emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->read_dword);// call   read_dword
6523   else if (inst->size == 8)
6524      emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->read_qword);// call   read_qword
6525   emit_add_r32_imm(&dst, REG_ESP, 4);                                       // add    esp,4
6526
6527   /* store result */
6528   if (inst->size == 4)
6529      emit_mov_p32_r32(drcbe, &dst, &dstp, REG_EAX);                           // mov   dstp,eax
6530   else if (inst->size == 8)
6531      emit_mov_p64_r64(drcbe, &dst, &dstp, REG_EAX, REG_EDX);                     // mov   dstp,edx:eax
6532
6533   return dst;
6534}
6535
6536
6537/*-------------------------------------------------
6538    op_fwrite - process a FWRITE opcode
6539-------------------------------------------------*/
6540
6541static x86code *op_fwrite(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6542{
6543   drcuml_parameter spacep, addrp, srcp;
6544
6545   /* validate instruction */
6546   assert(inst->size == 4 || inst->size == 8);
6547   assert(inst->condflags == DRCUML_COND_ALWAYS);
6548
6549   /* normalize parameters */
6550   param_normalize_3(drcbe, inst, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &srcp, PTYPE_MF);
6551
6552   /* set up a call to the write dword/qword handler */
6553   if (inst->size == 4)
6554      emit_push_p32(drcbe, &dst, &srcp);                                    // push   srcp
6555   else if (inst->size == 8)
6556      emit_push_p64(drcbe, &dst, &srcp);                                    // push   srcp
6557   emit_push_p32(drcbe, &dst, &addrp);                                       // push   addrp
6558   if (inst->size == 4)
6559      emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->write_dword);// call   write_dword
6560   else if (inst->size == 8)
6561      emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->write_qword);// call   write_qword
6562   emit_add_r32_imm(&dst, REG_ESP, 4 + inst->size);                           // add    esp,8
6563
6564   return dst;
6565}
6566
6567
6568/*-------------------------------------------------
6569    op_fmov - process a FMOV opcode
6570-------------------------------------------------*/
6571
6572static x86code *op_fmov(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6573{
6574   drcuml_parameter dstp, srcp;
6575   emit_link skip = { 0 };
6576
6577   /* validate instruction */
6578   assert(inst->size == 4 || inst->size == 8);
6579   assert(inst->condflags == DRCUML_COND_ALWAYS || (inst->condflags >= COND_Z && inst->condflags < DRCUML_COND_MAX));
6580
6581   /* normalize parameters */
6582   param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MF);
6583
6584   /* always start with a jmp */
6585   if (inst->condflags != DRCUML_COND_ALWAYS)
6586      emit_jcc_short_link(&dst, X86_NOT_CONDITION(inst->condflags), &skip);         // jcc   skip
6587
6588   /* general case */
6589   emit_fld_p(&dst, inst->size, &srcp);                                    // fld   srcp
6590   emit_fstp_p(&dst, inst->size, &dstp);                                    // fstp  dstp
6591
6592   /* resolve the jump */
6593   if (skip.target != NULL)
6594      resolve_link(&dst, &skip);                                       // skip:
6595   return dst;
6596}
6597
6598
6599/*-------------------------------------------------
6600    op_ftoi4 - process a FTOI4 opcode
6601-------------------------------------------------*/
6602
6603static x86code *op_ftoi4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6604{
6605   drcuml_parameter dstp, srcp;
6606
6607   /* validate instruction */
6608   assert(inst->size == 4 || inst->size == 8);
6609   assert(inst->condflags == DRCUML_COND_ALWAYS);
6610
6611   /* normalize parameters */
6612   param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF);
6613
6614   /* general case */
6615   emit_fld_p(&dst, inst->size, &srcp);                                    // fld   srcp
6616   if (dstp.type == DRCUML_PTYPE_MEMORY)
6617      emit_fistp_m32(&dst, MABS(dstp.value));                                 // fistp [dstp]
6618   else if (dstp.type == DRCUML_PTYPE_INT_REGISTER)
6619   {
6620      emit_fistp_m32(&dst, MABS(drcbe->reglo[dstp.value]));                     // fistp reglo[dstp]
6621      emit_mov_r32_m32(&dst, dstp.value, MABS(drcbe->reglo[dstp.value]));            // mov   dstp,reglo[dstp]
6622   }
6623   return dst;
6624}
6625
6626
6627/*-------------------------------------------------
6628    op_ftoi4t - process a FTOI4T opcode
6629-------------------------------------------------*/
6630
6631static x86code *op_ftoi4t(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6632{
6633   drcuml_parameter dstp, srcp;
6634
6635   /* validate instruction */
6636   assert(inst->size == 4 || inst->size == 8);
6637   assert(inst->condflags == DRCUML_COND_ALWAYS);
6638
6639   /* normalize parameters */
6640   param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF);
6641
6642   /* general case */
6643   emit_fld_p(&dst, inst->size, &srcp);                                    // fld   srcp
6644   if (dstp.type == DRCUML_PTYPE_MEMORY)
6645      emit_fisttp_m32(&dst, MABS(dstp.value));                              // fisttp [dstp]
6646   else if (dstp.type == DRCUML_PTYPE_INT_REGISTER)
6647   {
6648      emit_fisttp_m32(&dst, MABS(drcbe->reglo[dstp.value]));                     // fisttp reglo[dstp]
6649      emit_mov_r32_m32(&dst, dstp.value, MABS(drcbe->reglo[dstp.value]));            // mov   dstp,reglo[dstp]
6650   }
6651   return dst;
6652}
6653
6654
6655/*-------------------------------------------------
6656    op_ftoi4r - process a FTOI4R opcode
6657-------------------------------------------------*/
6658
6659static x86code *op_ftoi4r(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6660{
6661   drcuml_parameter dstp, srcp;
6662
6663   /* validate instruction */
6664   assert(inst->size == 4 || inst->size == 8);
6665   assert(inst->condflags == DRCUML_COND_ALWAYS);
6666
6667   /* normalize parameters */
6668   param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF);
6669
6670   /* save and set the control word */
6671   emit_fstcw_m16(&dst, MABS(&drcbe->fmodesave));                              // fstcw [fmodesave]
6672   emit_fldcw_m16(&dst, MABS(&fp_control[DRCUML_FMOD_ROUND]));                     // fldcw fpcontrol[DRCUML_FMOD_ROUND]
6673
6674   /* general case */
6675   emit_fld_p(&dst, inst->size, &srcp);                                    // fld   srcp
6676   if (dstp.type == DRCUML_PTYPE_MEMORY)
6677      emit_fistp_m32(&dst, MABS(dstp.value));                                 // fistp [dstp]
6678   else if (dstp.type == DRCUML_PTYPE_INT_REGISTER)
6679   {
6680      emit_fistp_m32(&dst, MABS(drcbe->reglo[dstp.value]));                     // fistp reglo[dstp]
6681      emit_mov_r32_m32(&dst, dstp.value, MABS(drcbe->reglo[dstp.value]));            // mov   dstp,reglo[dstp]
6682   }
6683
6684   /* restore control word and proceed */
6685   emit_fldcw_m16(&dst, MABS(&drcbe->fmodesave));                              // fldcw [fmodesave]
6686   return dst;
6687}
6688
6689
6690/*-------------------------------------------------
6691    op_ftoi4f - process a FTOI4F opcode
6692-------------------------------------------------*/
6693
6694static x86code *op_ftoi4f(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6695{
6696   drcuml_parameter dstp, srcp;
6697
6698   /* validate instruction */
6699   assert(inst->size == 4 || inst->size == 8);
6700   assert(inst->condflags == DRCUML_COND_ALWAYS);
6701
6702   /* normalize parameters */
6703   param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF);
6704
6705   /* save and set the control word */
6706   emit_fstcw_m16(&dst, MABS(&drcbe->fmodesave));                              // fstcw [fmodesave]
6707   emit_fldcw_m16(&dst, MABS(&fp_control[DRCUML_FMOD_FLOOR]));                     // fldcw fpcontrol[DRCUML_FMOD_FLOOR]
6708
6709   /* general case */
6710   emit_fld_p(&dst, inst->size, &srcp);                                    // fld   srcp
6711   if (dstp.type == DRCUML_PTYPE_MEMORY)
6712      emit_fistp_m32(&dst, MABS(dstp.value));                                 // fistp [dstp]
6713   else if (dstp.type == DRCUML_PTYPE_INT_REGISTER)
6714   {
6715      emit_fistp_m32(&dst, MABS(drcbe->reglo[dstp.value]));                     // fistp reglo[dstp]
6716      emit_mov_r32_m32(&dst, dstp.value, MABS(drcbe->reglo[dstp.value]));            // mov   dstp,reglo[dstp]
6717   }
6718
6719   /* restore control word and proceed */
6720   emit_fldcw_m16(&dst, MABS(&drcbe->fmodesave));                              // fldcw [fmodesave]
6721   return dst;
6722}
6723
6724
6725/*-------------------------------------------------
6726    op_ftoi4c - process a FTOI4C opcode
6727-------------------------------------------------*/
6728
6729static x86code *op_ftoi4c(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6730{
6731   drcuml_parameter dstp, srcp;
6732
6733   /* validate instruction */
6734   assert(inst->size == 4 || inst->size == 8);
6735   assert(inst->condflags == DRCUML_COND_ALWAYS);
6736
6737   /* normalize parameters */
6738   param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF);
6739
6740   /* save and set the control word */
6741   emit_fstcw_m16(&dst, MABS(&drcbe->fmodesave));                              // fstcw [fmodesave]
6742   emit_fldcw_m16(&dst, MABS(&fp_control[DRCUML_FMOD_CEIL]));                     // fldcw fpcontrol[DRCUML_FMOD_CEIL]
6743
6744   /* general case */
6745   emit_fld_p(&dst, inst->size, &srcp);                                    // fld   srcp
6746   if (dstp.type == DRCUML_PTYPE_MEMORY)
6747      emit_fistp_m32(&dst, MABS(dstp.value));                                 // fistp [dstp]
6748   else if (dstp.type == DRCUML_PTYPE_INT_REGISTER)
6749   {
6750      emit_fistp_m32(&dst, MABS(drcbe->reglo[dstp.value]));                     // fistp reglo[dstp]
6751      emit_mov_r32_m32(&dst, dstp.value, MABS(drcbe->reglo[dstp.value]));            // mov   dstp,reglo[dstp]
6752   }
6753
6754   /* restore control word and proceed */
6755   emit_fldcw_m16(&dst, MABS(&drcbe->fmodesave));                              // fldcw [fmodesave]
6756   return dst;
6757}
6758
6759
6760/*-------------------------------------------------
6761    op_ftoi8 - process a FTOI8 opcode
6762-------------------------------------------------*/
6763
6764static x86code *op_ftoi8(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6765{
6766   drcuml_parameter dstp, srcp;
6767
6768   /* validate instruction */
6769   assert(inst->size == 4 || inst->size == 8);
6770   assert(inst->condflags == DRCUML_COND_ALWAYS);
6771
6772   /* normalize parameters */
6773   param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF);
6774
6775   /* general case */
6776   emit_fld_p(&dst, inst->size, &srcp);                                    // fld   srcp
6777   if (dstp.type == DRCUML_PTYPE_MEMORY)
6778      emit_fistp_m64(&dst, MABS(dstp.value));                                 // fistp [dstp]
6779   else if (dstp.type == DRCUML_PTYPE_INT_REGISTER)
6780   {
6781      emit_fistp_m64(&dst, MABS(drcbe->reglo[dstp.value]));                     // fistp reglo[dstp]
6782      emit_mov_r32_m32(&dst, dstp.value, MABS(drcbe->reglo[dstp.value]));            // mov   dstp,reglo[dstp]
6783   }
6784   return dst;
6785}
6786
6787
6788/*-------------------------------------------------
6789    op_ftoi8t - process a FTOI8T opcode
6790-------------------------------------------------*/
6791
6792static x86code *op_ftoi8t(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6793{
6794   drcuml_parameter dstp, srcp;
6795
6796   /* validate instruction */
6797   assert(inst->size == 4 || inst->size == 8);
6798   assert(inst->condflags == DRCUML_COND_ALWAYS);
6799
6800   /* normalize parameters */
6801   param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF);
6802
6803   /* general case */
6804   emit_fld_p(&dst, inst->size, &srcp);                                    // fld   srcp
6805   if (dstp.type == DRCUML_PTYPE_MEMORY)
6806      emit_fisttp_m64(&dst, MABS(dstp.value));                              // fisttp [dstp]
6807   else if (dstp.type == DRCUML_PTYPE_INT_REGISTER)
6808   {
6809      emit_fisttp_m64(&dst, MABS(drcbe->reglo[dstp.value]));                     // fisttp reglo[dstp]
6810      emit_mov_r32_m32(&dst, dstp.value, MABS(drcbe->reglo[dstp.value]));            // mov   dstp,reglo[dstp]
6811   }
6812   return dst;
6813}
6814
6815
6816/*-------------------------------------------------
6817    op_ftoi8r - process a FTOI8R opcode
6818-------------------------------------------------*/
6819
6820static x86code *op_ftoi8r(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6821{
6822   drcuml_parameter dstp, srcp;
6823
6824   /* validate instruction */
6825   assert(inst->size == 4 || inst->size == 8);
6826   assert(inst->condflags == DRCUML_COND_ALWAYS);
6827
6828   /* normalize parameters */
6829   param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF);
6830
6831   /* save and set the control word */
6832   emit_fstcw_m16(&dst, MABS(&drcbe->fmodesave));                              // fstcw [fmodesave]
6833   emit_fldcw_m16(&dst, MABS(&fp_control[DRCUML_FMOD_ROUND]));                     // fldcw fpcontrol[DRCUML_FMOD_ROUND]
6834
6835   /* general case */
6836   emit_fld_p(&dst, inst->size, &srcp);                                    // fld   srcp
6837   if (dstp.type == DRCUML_PTYPE_MEMORY)
6838      emit_fistp_m64(&dst, MABS(dstp.value));                                 // fistp [dstp]
6839   else if (dstp.type == DRCUML_PTYPE_INT_REGISTER)
6840   {
6841      emit_fistp_m64(&dst, MABS(drcbe->reglo[dstp.value]));                     // fistp reglo[dstp]
6842      emit_mov_r32_m32(&dst, dstp.value, MABS(drcbe->reglo[dstp.value]));            // mov   dstp,reglo[dstp]
6843   }
6844
6845   /* restore control word and proceed */
6846   emit_fldcw_m16(&dst, MABS(&drcbe->fmodesave));                              // fldcw [fmodesave]
6847   return dst;
6848}
6849
6850
6851/*-------------------------------------------------
6852    op_ftoi8f - process a FTOI8F opcode
6853-------------------------------------------------*/
6854
6855static x86code *op_ftoi8f(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6856{
6857   drcuml_parameter dstp, srcp;
6858
6859   /* validate instruction */
6860   assert(inst->size == 4 || inst->size == 8);
6861   assert(inst->condflags == DRCUML_COND_ALWAYS);
6862
6863   /* normalize parameters */
6864   param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF);
6865
6866   /* save and set the control word */
6867   emit_fstcw_m16(&dst, MABS(&drcbe->fmodesave));                              // fstcw [fmodesave]
6868   emit_fldcw_m16(&dst, MABS(&fp_control[DRCUML_FMOD_FLOOR]));                     // fldcw fpcontrol[DRCUML_FMOD_FLOOR]
6869
6870   /* general case */
6871   emit_fld_p(&dst, inst->size, &srcp);                                    // fld   srcp
6872   if (dstp.type == DRCUML_PTYPE_MEMORY)
6873      emit_fistp_m64(&dst, MABS(dstp.value));                                 // fistp [dstp]
6874   else if (dstp.type == DRCUML_PTYPE_INT_REGISTER)
6875   {
6876      emit_fistp_m64(&dst, MABS(drcbe->reglo[dstp.value]));                     // fistp reglo[dstp]
6877      emit_mov_r32_m32(&dst, dstp.value, MABS(drcbe->reglo[dstp.value]));            // mov   dstp,reglo[dstp]
6878   }
6879
6880   /* restore control word and proceed */
6881   emit_fldcw_m16(&dst, MABS(&drcbe->fmodesave));                              // fldcw [fmodesave]
6882   return dst;
6883}
6884
6885
6886/*-------------------------------------------------
6887    op_ftoi8c - process a FTOI8C opcode
6888-------------------------------------------------*/
6889
6890static x86code *op_ftoi8c(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6891{
6892   drcuml_parameter dstp, srcp;
6893
6894   /* validate instruction */
6895   assert(inst->size == 4 || inst->size == 8);
6896   assert(inst->condflags == DRCUML_COND_ALWAYS);
6897
6898   /* normalize parameters */
6899   param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF);
6900
6901   /* save and set the control word */
6902   emit_fstcw_m16(&dst, MABS(&drcbe->fmodesave));                              // fstcw [fmodesave]
6903   emit_fldcw_m16(&dst, MABS(&fp_control[DRCUML_FMOD_CEIL]));                     // fldcw fpcontrol[DRCUML_FMOD_CEIL]
6904
6905   /* general case */
6906   emit_fld_p(&dst, inst->size, &srcp);                                    // fld   srcp
6907   if (dstp.type == DRCUML_PTYPE_MEMORY)
6908      emit_fistp_m64(&dst, MABS(dstp.value));                                 // fistp [dstp]
6909   else if (dstp.type == DRCUML_PTYPE_INT_REGISTER)
6910   {
6911      emit_fistp_m64(&dst, MABS(drcbe->reglo[dstp.value]));                     // fistp reglo[dstp]
6912      emit_mov_r32_m32(&dst, dstp.value, MABS(drcbe->reglo[dstp.value]));            // mov   dstp,reglo[dstp]
6913   }
6914
6915   /* restore control word and proceed */
6916   emit_fldcw_m16(&dst, MABS(&drcbe->fmodesave));                              // fldcw [fmodesave]
6917   return dst;
6918}
6919
6920
6921/*-------------------------------------------------
6922    op_ffrfs - process a FFRFS opcode
6923-------------------------------------------------*/
6924
6925static x86code *op_ffrfs(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6926{
6927   drcuml_parameter dstp, srcp;
6928
6929   /* validate instruction */
6930   assert(inst->size == 8);
6931   assert(inst->condflags == DRCUML_COND_ALWAYS);
6932
6933   /* normalize parameters */
6934   param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MF);
6935
6936   /* general case */
6937   emit_fld_m32(&dst, MABS(srcp.value));                                    // fld   [srcp]
6938   emit_fstp_p(&dst, inst->size, &dstp);                                    // fstp  dstp
6939
6940   return dst;
6941}
6942
6943
6944/*-------------------------------------------------
6945    op_ffrfd - process a FFRFD opcode
6946-------------------------------------------------*/
6947
6948static x86code *op_ffrfd(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6949{
6950   drcuml_parameter dstp, srcp;
6951
6952   /* validate instruction */
6953   assert(inst->size == 4);
6954   assert(inst->condflags == DRCUML_COND_ALWAYS);
6955
6956   /* normalize parameters */
6957   param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MF);
6958
6959   /* general case */
6960   emit_fld_m64(&dst, MABS(srcp.value));                                    // fld   [srcp]
6961   emit_fstp_p(&dst, inst->size, &dstp);                                    // fstp  dstp
6962
6963   return dst;
6964}
6965
6966
6967/*-------------------------------------------------
6968    op_ffri4 - process a FFRI4 opcode
6969-------------------------------------------------*/
6970
6971static x86code *op_ffri4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
6972{
6973   drcuml_parameter dstp, srcp;
6974
6975   /* validate instruction */
6976   assert(inst->size == 4 || inst->size == 8);
6977   assert(inst->condflags == DRCUML_COND_ALWAYS);
6978
6979   /* normalize parameters */
6980   param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MRI);
6981
6982   /* general case */
6983   if (srcp.type == DRCUML_PTYPE_IMMEDIATE)
6984   {
6985      emit_mov_m32_imm(&dst, MABS(&drcbe->fptemp), srcp.value);                  // mov   [fptemp],srcp
6986      emit_fild_m32(&dst, MABS(&drcbe->fptemp));                              // fild  [fptemp]
6987   }
6988   else if (srcp.type == DRCUML_PTYPE_MEMORY)
6989      emit_fild_m32(&dst, MABS(srcp.value));                                 // fild  [srcp]
6990   else if (srcp.type == DRCUML_PTYPE_INT_REGISTER)
6991   {
6992      emit_mov_m32_r32(&dst, MABS(drcbe->reglo[srcp.value]), srcp.value);            // mov   reglo[srcp],srcp
6993      emit_fild_m32(&dst, MABS(drcbe->reglo[srcp.value]));                     // fild  reglo[srcp]
6994   }
6995   emit_fstp_p(&dst, inst->size, &dstp);                                    // fstp  [dstp]
6996   return dst;
6997}
6998
6999
7000/*-------------------------------------------------
7001    op_ffri8 - process a FFRI8 opcode
7002-------------------------------------------------*/
7003
7004static x86code *op_ffri8(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
7005{
7006   drcuml_parameter dstp, srcp;
7007
7008   /* validate instruction */
7009   assert(inst->size == 4 || inst->size == 8);
7010   assert(inst->condflags == DRCUML_COND_ALWAYS);
7011
7012   /* normalize parameters */
7013   param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MRI);
7014
7015   /* general case */
7016   if (srcp.type == DRCUML_PTYPE_IMMEDIATE)
7017   {
7018      emit_mov_m32_imm(&dst, MABS(&drcbe->fptemp), srcp.value);                  // mov   [fptemp],srcp
7019      emit_mov_m32_imm(&dst, MABS((UINT8 *)&drcbe->fptemp + 4), srcp.value);         // mov   [fptemp+4],srcp
7020      emit_fild_m64(&dst, MABS(&drcbe->fptemp));                              // fild  [fptemp]
7021   }
7022   else if (srcp.type == DRCUML_PTYPE_MEMORY)
7023      emit_fild_m64(&dst, MABS(srcp.value));                                 // fild  [srcp]
7024   else if (srcp.type == DRCUML_PTYPE_INT_REGISTER)
7025   {
7026      emit_mov_m32_r32(&dst, MABS(drcbe->reglo[srcp.value]), srcp.value);            // mov   reglo[srcp],srcp
7027      emit_fild_m64(&dst, MABS(drcbe->reglo[srcp.value]));                     // fild  reglo[srcp]
7028   }
7029   emit_fstp_p(&dst, inst->size, &dstp);                                    // fstp  [dstp]
7030   return dst;
7031}
7032
7033
7034/*-------------------------------------------------
7035    op_fadd - process a FADD opcode
7036-------------------------------------------------*/
7037
7038static x86code *op_fadd(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
7039{
7040   drcuml_parameter dstp, src1p, src2p;
7041
7042   /* validate instruction */
7043   assert(inst->size == 4 || inst->size == 8);
7044   assert(inst->condflags == DRCUML_COND_ALWAYS);
7045
7046   /* normalize parameters */
7047   param_normalize_3(drcbe, inst, &dstp, PTYPE_MF, &src1p, PTYPE_MF, &src2p, PTYPE_MF);
7048
7049   /* general case */
7050   emit_fld_p(&dst, inst->size, &src1p);                                    // fld   src1p
7051   emit_fld_p(&dst, inst->size, &src2p);                                    // fld   src2p
7052   emit_faddp(&dst);                                                   // faddp
7053   emit_fstp_p(&dst, inst->size, &dstp);                                    // fstp  dstp
7054
7055   return dst;
7056}
7057
7058
7059/*-------------------------------------------------
7060    op_fsub - process a FSUB opcode
7061-------------------------------------------------*/
7062
7063static x86code *op_fsub(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
7064{
7065   drcuml_parameter dstp, src1p, src2p;
7066
7067   /* validate instruction */
7068   assert(inst->size == 4 || inst->size == 8);
7069   assert(inst->condflags == DRCUML_COND_ALWAYS);
7070
7071   /* normalize parameters */
7072   param_normalize_3(drcbe, inst, &dstp, PTYPE_MF, &src1p, PTYPE_MF, &src2p, PTYPE_MF);
7073
7074   /* general case */
7075   emit_fld_p(&dst, inst->size, &src1p);                                    // fld   src1p
7076   emit_fld_p(&dst, inst->size, &src2p);                                    // fld   src2p
7077   emit_fsubp(&dst);                                                   // fsubp
7078   emit_fstp_p(&dst, inst->size, &dstp);                                    // fstp  dstp
7079
7080   return dst;
7081}
7082
7083
7084/*-------------------------------------------------
7085    op_fcmp - process a FCMP opcode
7086-------------------------------------------------*/
7087
7088static x86code *op_fcmp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
7089{
7090   drcuml_parameter src1p, src2p;
7091
7092   /* validate instruction */
7093   assert(inst->size == 4 || inst->size == 8);
7094   assert((inst->condflags & ~(DRCUML_FLAG_C | DRCUML_FLAG_V | DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0);
7095
7096   /* normalize parameters */
7097   param_normalize_2(drcbe, inst, &src1p, PTYPE_MF, &src2p, PTYPE_MF);
7098
7099   /* general case */
7100   emit_fld_p(&dst, inst->size, &src2p);                                    // fld   src2p
7101   emit_fld_p(&dst, inst->size, &src1p);                                    // fld   src1p
7102   emit_fcompp(&dst);                                                   // fcompp
7103   emit_fstsw_ax(&dst);                                                // fnstsw ax
7104   emit_sahf(&dst);                                                   // sahf
7105
7106   return dst;
7107}
7108
7109
7110/*-------------------------------------------------
7111    op_fmul - process a FMUL opcode
7112-------------------------------------------------*/
7113
7114static x86code *op_fmul(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
7115{
7116   drcuml_parameter dstp, src1p, src2p;
7117
7118   /* validate instruction */
7119   assert(inst->size == 4 || inst->size == 8);
7120   assert(inst->condflags == DRCUML_COND_ALWAYS);
7121
7122   /* normalize parameters */
7123   param_normalize_3(drcbe, inst, &dstp, PTYPE_MF, &src1p, PTYPE_MF, &src2p, PTYPE_MF);
7124
7125   /* general case */
7126   emit_fld_p(&dst, inst->size, &src1p);                                    // fld   src1p
7127   emit_fld_p(&dst, inst->size, &src2p);                                    // fld   src2p
7128   emit_fmulp(&dst);                                                   // fmulp
7129   emit_fstp_p(&dst, inst->size, &dstp);                                    // fstp  dstp
7130
7131   return dst;
7132}
7133
7134
7135/*-------------------------------------------------
7136    op_fdiv - process a FDIV opcode
7137-------------------------------------------------*/
7138
7139static x86code *op_fdiv(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
7140{
7141   drcuml_parameter dstp, src1p, src2p;
7142
7143   /* validate instruction */
7144   assert(inst->size == 4 || inst->size == 8);
7145   assert(inst->condflags == DRCUML_COND_ALWAYS);
7146
7147   /* normalize parameters */
7148   param_normalize_3(drcbe, inst, &dstp, PTYPE_MF, &src1p, PTYPE_MF, &src2p, PTYPE_MF);
7149
7150   /* general case */
7151   emit_fld_p(&dst, inst->size, &src1p);                                    // fld   src1p
7152   emit_fld_p(&dst, inst->size, &src2p);                                    // fld   src2p
7153   emit_fdivp(&dst);                                                   // fdivp
7154   emit_fstp_p(&dst, inst->size, &dstp);                                    // fstp  dstp
7155
7156   return dst;
7157}
7158
7159
7160/*-------------------------------------------------
7161    op_fneg - process a FNEG opcode
7162-------------------------------------------------*/
7163
7164static x86code *op_fneg(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
7165{
7166   drcuml_parameter dstp, srcp;
7167
7168   /* validate instruction */
7169   assert(inst->size == 4 || inst->size == 8);
7170   assert(inst->condflags == DRCUML_COND_ALWAYS);
7171
7172   /* normalize parameters */
7173   param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MF);
7174
7175   /* general case */
7176   emit_fld_p(&dst, inst->size, &srcp);                                    // fld   srcp
7177   emit_fchs(&dst);                                                   // fchs
7178   emit_fstp_p(&dst, inst->size, &dstp);                                    // fstp  dstp
7179
7180   return dst;
7181}
7182
7183
7184/*-------------------------------------------------
7185    op_fabs - process a FABS opcode
7186-------------------------------------------------*/
7187
7188static x86code *op_fabs(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
7189{
7190   drcuml_parameter dstp, srcp;
7191
7192   /* validate instruction */
7193   assert(inst->size == 4 || inst->size == 8);
7194   assert(inst->condflags == DRCUML_COND_ALWAYS);
7195
7196   /* normalize parameters */
7197   param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MF);
7198
7199   /* general case */
7200   emit_fld_p(&dst, inst->size, &srcp);                                    // fld   srcp
7201   emit_fabs(&dst);                                                   // fabs
7202   emit_fstp_p(&dst, inst->size, &dstp);                                    // fstp  dstp
7203
7204   return dst;
7205}
7206
7207
7208/*-------------------------------------------------
7209    op_fsqrt - process a FSQRT opcode
7210-------------------------------------------------*/
7211
7212static x86code *op_fsqrt(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
7213{
7214   drcuml_parameter dstp, srcp;
7215
7216   /* validate instruction */
7217   assert(inst->size == 4 || inst->size == 8);
7218   assert(inst->condflags == DRCUML_COND_ALWAYS);
7219
7220   /* normalize parameters */
7221   param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MF);
7222
7223   /* general case */
7224   emit_fld_p(&dst, inst->size, &srcp);                                    // fld   srcp
7225   emit_fsqrt(&dst);                                                   // fsqrt
7226   emit_fstp_p(&dst, inst->size, &dstp);                                    // fstp  dstp
7227
7228   return dst;
7229}
7230
7231
7232/*-------------------------------------------------
7233    op_frecip - process a FRECIP opcode
7234-------------------------------------------------*/
7235
7236static x86code *op_frecip(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
7237{
7238   drcuml_parameter dstp, srcp;
7239
7240   /* validate instruction */
7241   assert(inst->size == 4 || inst->size == 8);
7242   assert(inst->condflags == DRCUML_COND_ALWAYS);
7243
7244   /* normalize parameters */
7245   param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MF);
7246
7247   /* general case */
7248   emit_fld1(&dst);                                                   // fld1
7249   emit_fld_p(&dst, inst->size, &srcp);                                    // fld   srcp
7250   emit_fdivp(&dst);                                                   // fdivp
7251   emit_fstp_p(&dst, inst->size, &dstp);                                    // fstp  dstp
7252
7253   return dst;
7254}
7255
7256
7257/*-------------------------------------------------
7258    op_frsqrt - process a FRSQRT opcode
7259-------------------------------------------------*/
7260
7261static x86code *op_frsqrt(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst)
7262{
7263   drcuml_parameter dstp, srcp;
7264
7265   /* validate instruction */
7266   assert(inst->size == 4 || inst->size == 8);
7267   assert(inst->condflags == DRCUML_COND_ALWAYS);
7268
7269   /* normalize parameters */
7270   param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MF);
7271
7272   /* general case */
7273   emit_fld1(&dst);                                                   // fld1
7274   emit_fld_p(&dst, inst->size, &srcp);                                    // fld   srcp
7275   emit_fsqrt(&dst);                                                   // fsqrt
7276   emit_fdivp(&dst);                                                   // fdivp
7277   emit_fstp_p(&dst, inst->size, &dstp);                                    // fstp  dstp
7278
7279   return dst;
7280}
7281
7282
7283
7284/***************************************************************************
7285    MISCELLAENOUS FUNCTIONS
7286***************************************************************************/
7287
7288/*-------------------------------------------------
7289    dmulu - perform a double-wide unsigned multiply
7290-------------------------------------------------*/
7291
7292static void dmulu(UINT64 *dstlo, UINT64 *dsthi, UINT64 src1, UINT64 src2)
7293{
7294   UINT64 lo, hi, prevlo;
7295   UINT64 a, b, temp;
7296
7297   /* shortcut if we don't care about the high bits */
7298   if (dstlo == dsthi)
7299   {
7300      *dstlo = src1 * src2;
7301      return;
7302   }
7303
7304   /* fetch source values */
7305   a = src1;
7306   b = src2;
7307   if (a == 0 || b == 0)
7308   {
7309      *dsthi = *dstlo = 0;
7310      return;
7311   }
7312
7313   /* compute high and low parts first */
7314   lo = (UINT64)(UINT32)(a >> 0)  * (UINT64)(UINT32)(b >> 0);
7315   hi = (UINT64)(UINT32)(a >> 32) * (UINT64)(UINT32)(b >> 32);
7316
7317   /* compute middle parts */
7318   prevlo = lo;
7319   temp = (UINT64)(UINT32)(a >> 32)  * (UINT64)(UINT32)(b >> 0);
7320   lo += temp << 32;
7321   hi += (temp >> 32) + (lo < prevlo);
7322
7323   prevlo = lo;
7324   temp = (UINT64)(UINT32)(a >> 0)  * (UINT64)(UINT32)(b >> 32);
7325   lo += temp << 32;
7326   hi += (temp >> 32) + (lo < prevlo);
7327
7328   /* store the results */
7329   *dsthi = hi;
7330   *dstlo = lo;
7331}
7332
7333
7334/*-------------------------------------------------
7335    dmuls - perform a double-wide signed multiply
7336-------------------------------------------------*/
7337
7338static void dmuls(UINT64 *dstlo, UINT64 *dsthi, INT64 src1, INT64 src2)
7339{
7340   UINT64 lo, hi, prevlo;
7341   UINT64 a, b, temp;
7342
7343   /* shortcut if we don't care about the high bits */
7344   if (dstlo == dsthi)
7345   {
7346      *dstlo = src1 * src2;
7347      return;
7348   }
7349
7350   /* fetch absolute source values */
7351   a = src1; if ((INT64)a < 0) a = -a;
7352   b = src2; if ((INT64)b < 0) b = -b;
7353   if (a == 0 || b == 0)
7354   {
7355      *dsthi = *dstlo = 0;
7356      return;
7357   }
7358
7359   /* compute high and low parts first */
7360   lo = (UINT64)(UINT32)(a >> 0)  * (UINT64)(UINT32)(b >> 0);
7361   hi = (UINT64)(UINT32)(a >> 32) * (UINT64)(UINT32)(b >> 32);
7362
7363   /* compute middle parts */
7364   prevlo = lo;
7365   temp = (UINT64)(UINT32)(a >> 32)  * (UINT64)(UINT32)(b >> 0);
7366   lo += temp << 32;
7367   hi += (temp >> 32) + (lo < prevlo);
7368
7369   prevlo = lo;
7370   temp = (UINT64)(UINT32)(a >> 0)  * (UINT64)(UINT32)(b >> 32);
7371   lo += temp << 32;
7372   hi += (temp >> 32) + (lo < prevlo);
7373
7374   /* adjust for signage */
7375   if ((INT64)(src1 ^ src2) < 0)
7376   {
7377      hi = ~hi + (lo == 0);
7378      lo = ~lo + 1;
7379   }
7380
7381   /* store the results */
7382   *dsthi = hi;
7383   *dstlo = lo;
7384}
7385
7386
7387/*-------------------------------------------------
7388    ddivu - perform a double-wide unsigned divide
7389-------------------------------------------------*/
7390
7391static void ddivu(UINT64 *dstlo, UINT64 *dsthi, UINT64 src1, UINT64 src2)
7392{
7393   /* do nothing if src2 == 0 */
7394   if (src2 == 0)
7395      return;
7396
7397   /* shortcut if no remainder */
7398   *dstlo = src1 / src2;
7399   if (dstlo != dsthi)
7400      *dsthi = src1 % src2;
7401}
7402
7403
7404/*-------------------------------------------------
7405    ddivs - perform a double-wide signed divide
7406-------------------------------------------------*/
7407
7408static void ddivs(UINT64 *dstlo, UINT64 *dsthi, INT64 src1, INT64 src2)
7409{
7410   /* do nothing if src2 == 0 */
7411   if (src2 == 0)
7412      return;
7413
7414   /* shortcut if no remainder */
7415   *dstlo = src1 / src2;
7416   if (dstlo != dsthi)
7417      *dsthi = src1 % src2;
7418}
src/emu/video/cdp1869.c
r2627r2628
1212    - connect to sound system when possible
1313    - white noise
1414    - scanline based update
15   - separate to CDP1869 and CDP1870/6
15    - separate to CDP1869 and CDP1870/6
1616
1717*/
1818
src/emu/video/voodoo.c
r2627r2628
36863686   }
36873687
36883688   /* modify the offset based on the mem_mask */
3689   if (mem_mask)
3689   if (mem_mask != 0xffffffff)
36903690   {
36913691      if (!ACCESSING_BITS_16_31)
36923692         offset |= 0x80000000;
r2627r2628
38603860
38613861      /* return the current scanline for now */
38623862      case vRetrace:
3863
3864         /* eat some cycles since people like polling here */
3865         activecpu_eat_cycles(10);
38633866         result = video_screen_get_vpos(v->screen);
38643867         break;
38653868
src/emu/emuopts.c
r2627r2628
6565   { "playback;pb",                 NULL,        0,                 "playback an input file" },
6666   { "record;rec",                  NULL,        0,                 "record an input file" },
6767   { "mngwrite",                    NULL,        0,                 "optional filename to write a MNG movie of the current session" },
68   { "aviwrite",                    NULL,        0,                 "optional filename to write an AVI movie of the current session" },
6869   { "wavwrite",                    NULL,        0,                 "optional filename to write a WAV file of the current session" },
6970
7071   /* performance options */
src/emu/emuopts.h
r2627r2628
6464#define OPTION_PLAYBACK            "playback"
6565#define OPTION_RECORD            "record"
6666#define OPTION_MNGWRITE            "mngwrite"
67#define OPTION_AVIWRITE            "aviwrite"
6768#define OPTION_WAVWRITE            "wavwrite"
6869
6970/* core performance options */
src/emu/video.c
r2627r2628
1515#include "debugger.h"
1616#include "rendutil.h"
1717#include "ui.h"
18#include "aviio.h"
1819
1920#include "snap.lh"
2021
r2627r2628
7980   vblank_state_changed_func vblank_callback[MAX_VBLANK_CALLBACKS]; /* the array of callbacks */
8081
8182   /* movie recording */
82   mame_file *            movie_file;            /* handle to the open movie file */
83   mame_file *            mng_file;            /* handle to the open movie file */
84   avi_file *            avi_file;            /* handle to the open movie file */
8385   UINT32                movie_frame;         /* current movie frame number */
8486};
8587
r2627r2628
188190static file_error mame_fopen_next(running_machine *machine, const char *pathoption, const char *extension, mame_file **file);
189191
190192/* movie recording */
191static void movie_record_frame(const device_config *screen);
193static void video_mng_record_frame(const device_config *screen);
194static void video_avi_record_frame(const device_config *screen);
192195
193196/* software rendering */
194197static void rgb888_draw_primitives(const render_primitive *primlist, void *dstdata, UINT32 width, UINT32 height, UINT32 pitch);
r2627r2628
345348
346349   /* start recording movie if specified */
347350   filename = options_get_string(mame_options(), OPTION_MNGWRITE);
348   if ((filename[0] != 0) && (machine->primary_screen != NULL))
349      video_movie_begin_recording(machine->primary_screen, filename);
351   if (filename[0] != 0 && machine->primary_screen != NULL)
352      video_mng_begin_recording(machine->primary_screen, filename);
350353
354   filename = options_get_string(mame_options(), OPTION_AVIWRITE);
355   if (filename[0] != 0 && machine->primary_screen != NULL)
356      video_avi_begin_recording(machine->primary_screen, filename);
357
351358   /* if no screens, create a periodic timer to drive updates */
352359   if (machine->primary_screen == NULL)
353360   {
r2627r2628
371378
372379   /* stop recording any movie */
373380   if (machine->primary_screen != NULL)
374      video_movie_end_recording(machine->primary_screen);
381   {
382      video_mng_end_recording(machine->primary_screen);
383      video_avi_end_recording(machine->primary_screen);
384   }
375385
376386   /* free all the graphics elements */
377387   for (i = 0; i < MAX_GFX_ELEMENTS; i++)
r2627r2628
696706
697707   /* if there has been no VBLANK time specified in the MACHINE_DRIVER, compute it now
698708       from the visible area, otherwise just used the supplied value */
699   if ((config->vblank == 0) && !config->oldstyle_vblank_supplied)
709   if (config->vblank == 0 && !config->oldstyle_vblank_supplied)
700710      state->vblank_period = state->scantime * (height - (visarea->max_y + 1 - visarea->min_y));
701711   else
702712      state->vblank_period = config->vblank;
r2627r2628
833843       we only update up to the previous scanline.
834844       This minimizes the number of pixels that might be drawn
835845       incorrectly until we support a pixel level granularity */
836   if ((current_hpos < (state->width / 2)) && (current_vpos > 0))
846   if (current_hpos < (state->width / 2) && current_vpos > 0)
837847      current_vpos = current_vpos - 1;
838848
839849   video_screen_update_partial(screen, current_vpos);
r2627r2628
11571167   assert(config->height > 0);
11581168   assert(config->refresh > 0);
11591169   assert(config->visarea.min_x >= 0);
1160   assert((config->visarea.max_x < config->width) || (config->type == SCREEN_TYPE_VECTOR));
1170   assert(config->visarea.max_x < config->width || config->type == SCREEN_TYPE_VECTOR);
11611171   assert(config->visarea.max_x > config->visarea.min_x);
11621172   assert(config->visarea.min_y >= 0);
1163   assert((config->visarea.max_y < config->height) || (config->type == SCREEN_TYPE_VECTOR));
1173   assert(config->visarea.max_y < config->height || config->type == SCREEN_TYPE_VECTOR);
11641174   assert(config->visarea.max_y > config->visarea.min_y);
11651175
11661176   /* allocate the VBLANK timers */
r2627r2628
12971307      (*state->vblank_callback[i])(screen, TRUE);
12981308
12991309   /* if this is the primary screen and we need to update now */
1300   if ((screen == machine->primary_screen) && !(machine->config->video_attributes & VIDEO_UPDATE_AFTER_VBLANK))
1310   if (screen == machine->primary_screen && !(machine->config->video_attributes & VIDEO_UPDATE_AFTER_VBLANK))
13011311      video_frame_update(machine, FALSE);
13021312
13031313   /* reset the VBLANK start timer for the next frame */
r2627r2628
13271337      (*state->vblank_callback[i])(screen, FALSE);
13281338
13291339   /* if this is the primary screen and we need to update now */
1330   if ((screen == machine->primary_screen) && (machine->config->video_attributes & VIDEO_UPDATE_AFTER_VBLANK))
1340   if (screen == machine->primary_screen && (machine->config->video_attributes & VIDEO_UPDATE_AFTER_VBLANK))
13311341      video_frame_update(machine, FALSE);
13321342
13331343   /* increment the frame number counter */
r2627r2628
14451455   if (phase == MAME_PHASE_RUNNING)
14461456   {
14471457      /* reset partial updates if we're paused or if the debugger is active */
1448      if ((machine->primary_screen != NULL) && (mame_is_paused(machine) || debug || mame_debug_is_active()))
1458      if (machine->primary_screen != NULL && (mame_is_paused(machine) || debug || mame_debug_is_active()))
14491459      {
14501460         void *param = (void *)machine->primary_screen;
14511461         scanline0_callback(machine, param, 0);
r2627r2628
15081518
15091519         /* update our movie recording state */
15101520         if (!mame_is_paused(machine))
1511            movie_record_frame(screen);
1521         {
1522            video_mng_record_frame(screen);
1523            video_avi_record_frame(screen);
1524         }
15121525      }
15131526
15141527      /* reset the screen changed flags */
r2627r2628
21582171/*-------------------------------------------------
21592172    creare_snapshot_bitmap - creates a
21602173    bitmap containing the screenshot for the
2161    given screen number
2174    given screen
21622175-------------------------------------------------*/
21632176
21642177static void create_snapshot_bitmap(const device_config *screen)
r2627r2628
21772190   render_target_set_bounds(global.snap_target, width, height, 0);
21782191
21792192   /* if we don't have a bitmap, or if it's not the right size, allocate a new one */
2180   if ((global.snap_bitmap == NULL) || (width != global.snap_bitmap->width) || (height != global.snap_bitmap->height))
2193   if (global.snap_bitmap == NULL || width != global.snap_bitmap->width || height != global.snap_bitmap->height)
21812194   {
21822195      if (global.snap_bitmap != NULL)
21832196         bitmap_free(global.snap_bitmap);
r2627r2628
22332246***************************************************************************/
22342247
22352248/*-------------------------------------------------
2236    video_is_movie_active - return true if a movie
2237    is currently being recorded
2249    video_mng_is_movie_active - return true if a
2250    MNG movie is currently being recorded
22382251-------------------------------------------------*/
22392252
2240int video_is_movie_active(const device_config *screen)
2253int video_mng_is_movie_active(const device_config *screen)
22412254{
22422255   screen_state *state = get_safe_token(screen);
2243   return (state->movie_file != NULL);
2256   return (state->mng_file != NULL);
22442257}
22452258
22462259
22472260/*-------------------------------------------------
2248    video_movie_begin_recording - begin recording
2261    video_mng_begin_recording - begin recording
22492262    of a MNG movie
22502263-------------------------------------------------*/
22512264
2252void video_movie_begin_recording(const device_config *screen, const char *name)
2265void video_mng_begin_recording(const device_config *screen, const char *name)
22532266{
22542267   screen_state *state = get_safe_token(screen);
22552268   file_error filerr;
22562269
22572270   /* close any existing movie file */
2258   if (state->movie_file != NULL)
2259      video_movie_end_recording(screen);
2271   if (state->mng_file != NULL)
2272      video_mng_end_recording(screen);
22602273
22612274   /* create a new movie file and start recording */
22622275   if (name != NULL)
2263      filerr = mame_fopen(SEARCHPATH_MOVIE, name, OPEN_FLAG_WRITE | OPEN_FLAG_CREATE | OPEN_FLAG_CREATE_PATHS, &state->movie_file);
2276      filerr = mame_fopen(SEARCHPATH_MOVIE, name, OPEN_FLAG_WRITE | OPEN_FLAG_CREATE | OPEN_FLAG_CREATE_PATHS, &state->mng_file);
22642277   else
2265      filerr = mame_fopen_next(screen->machine, SEARCHPATH_MOVIE, "mng", &state->movie_file);
2278      filerr = mame_fopen_next(screen->machine, SEARCHPATH_MOVIE, "mng", &state->mng_file);
22662279   state->movie_frame = 0;
22672280}
22682281
22692282
22702283/*-------------------------------------------------
2271    video_movie_end_recording - stop recording of
2284    video_mng_end_recording - stop recording of
22722285    a MNG movie
22732286-------------------------------------------------*/
22742287
2275void video_movie_end_recording(const device_config *screen)
2288void video_mng_end_recording(const device_config *screen)
22762289{
22772290   screen_state *state = get_safe_token(screen);
22782291
22792292   /* close the file if it exists */
2280   if (state->movie_file != NULL)
2293   if (state->mng_file != NULL)
22812294   {
2282      mng_capture_stop(mame_core_file(state->movie_file));
2283      mame_fclose(state->movie_file);
2284      state->movie_file = NULL;
2295      mng_capture_stop(mame_core_file(state->mng_file));
2296      mame_fclose(state->mng_file);
2297      state->mng_file = NULL;
22852298      state->movie_frame = 0;
22862299   }
22872300}
22882301
22892302
22902303/*-------------------------------------------------
2291    movie_record_frame - record a frame of a
2304    video_mng_record_frame - record a frame of a
22922305    movie
22932306-------------------------------------------------*/
22942307
2295static void movie_record_frame(const device_config *screen)
2308static void video_mng_record_frame(const device_config *screen)
22962309{
22972310   screen_state *state = get_safe_token(screen);
22982311   const rgb_t *palette;
22992312
23002313   /* only record if we have a file */
2301   if (state->movie_file != NULL)
2314   if (state->mng_file != NULL)
23022315   {
23032316      png_info pnginfo = { 0 };
23042317      png_error error;
r2627r2628
23202333         png_add_text(&pnginfo, "System", text);
23212334
23222335         /* start the capture */
2323         error = mng_capture_start(mame_core_file(state->movie_file), global.snap_bitmap, ATTOSECONDS_TO_HZ(state->frame_period));
2336         error = mng_capture_start(mame_core_file(state->mng_file), global.snap_bitmap, ATTOSECONDS_TO_HZ(state->frame_period));
23242337         if (error != PNGERR_NONE)
23252338         {
23262339            png_free(&pnginfo);
2327            video_movie_end_recording(screen);
2340            video_mng_end_recording(screen);
23282341            return;
23292342         }
23302343      }
23312344
23322345      /* write the next frame */
23332346      palette = (screen->machine->palette != NULL) ? palette_entry_list_adjusted(screen->machine->palette) : NULL;
2334      error = mng_capture_frame(mame_core_file(state->movie_file), &pnginfo, global.snap_bitmap, screen->machine->config->total_colors, palette);
2347      error = mng_capture_frame(mame_core_file(state->mng_file), &pnginfo, global.snap_bitmap, screen->machine->config->total_colors, palette);
23352348      png_free(&pnginfo);
23362349      if (error != PNGERR_NONE)
2337      {
2338         video_movie_end_recording(screen);
2339         return;
2340      }
2350         video_mng_end_recording(screen);
23412351
23422352      profiler_mark(PROFILER_END);
23432353   }
r2627r2628
23462356
23472357
23482358/***************************************************************************
2359    AVI MOVIE RECORDING
2360***************************************************************************/
2361
2362/*-------------------------------------------------
2363    video_avi_begin_recording - begin recording
2364    of an AVI movie
2365-------------------------------------------------*/
2366
2367void video_avi_begin_recording(const device_config *screen, const char *name)
2368{
2369   screen_state *state = get_safe_token(screen);
2370   avi_movie_info info;
2371   mame_file *tempfile;
2372   file_error filerr;
2373   avi_error avierr;
2374
2375   /* close any existing movie file */
2376   if (state->avi_file != NULL)
2377      video_avi_end_recording(screen);
2378
2379   /* create a snapshot bitmap so we know what the target size is */
2380   create_snapshot_bitmap(screen);
2381
2382   /* build up information about this new movie */
2383   info.video_format = 0;
2384   info.video_timescale = 1000 * ATTOSECONDS_TO_HZ(state->frame_period);
2385   info.video_sampletime = 1000;
2386   info.video_numsamples = 0;
2387   info.video_width = global.snap_bitmap->width;
2388   info.video_height = global.snap_bitmap->height;
2389   info.video_depth = 24;
2390
2391   info.audio_format = 0;
2392   info.audio_timescale = 1;
2393   info.audio_sampletime = screen->machine->sample_rate;
2394   info.audio_numsamples = 0;
2395   info.audio_channels = 2;
2396   info.audio_samplebits = 16;
2397   info.audio_samplerate = screen->machine->sample_rate;
2398
2399   /* create a new temporary movie file */
2400   if (name != NULL)
2401      filerr = mame_fopen(SEARCHPATH_MOVIE, name, OPEN_FLAG_WRITE | OPEN_FLAG_CREATE | OPEN_FLAG_CREATE_PATHS, &tempfile);
2402   else
2403      filerr = mame_fopen_next(screen->machine, SEARCHPATH_MOVIE, "avi", &tempfile);
2404   state->movie_frame = 0;
2405
2406   /* if we succeeded, make a copy of the name and create the real file over top */
2407   if (filerr == FILERR_NONE)
2408   {
2409      astring *fullname = astring_dupc(mame_file_full_name(tempfile));
2410      mame_fclose(tempfile);
2411
2412      /* create the file and free the string */
2413      avierr = avi_create(astring_c(fullname), &info, &state->avi_file);
2414      astring_free(fullname);
2415   }
2416}
2417
2418
2419/*-------------------------------------------------
2420    video_avi_end_recording - stop recording of
2421    a avi movie
2422-------------------------------------------------*/
2423
2424void video_avi_end_recording(const device_config *screen)
2425{
2426   screen_state *state = get_safe_token(screen);
2427
2428   /* close the file if it exists */
2429   if (state->avi_file != NULL)
2430   {
2431      avi_close(state->avi_file);
2432      state->avi_file = NULL;
2433      state->movie_frame = 0;
2434   }
2435}
2436
2437
2438/*-------------------------------------------------
2439    video_avi_record_frame - record a frame of a
2440    movie
2441-------------------------------------------------*/
2442
2443static void video_avi_record_frame(const device_config *screen)
2444{
2445   screen_state *state = get_safe_token(screen);
2446
2447   /* only record if we have a file */
2448   if (state->avi_file != NULL)
2449   {
2450      avi_error avierr;
2451
2452      profiler_mark(PROFILER_MOVIE_REC);
2453
2454      /* create the bitmap */
2455      create_snapshot_bitmap(screen);
2456
2457      /* write the next frame */
2458      avierr = avi_append_video_frame_rgb32(state->avi_file, global.snap_bitmap);
2459      if (avierr != AVIERR_NONE)
2460         video_avi_end_recording(screen);
2461
2462      profiler_mark(PROFILER_END);
2463   }
2464}
2465
2466
2467/*-------------------------------------------------
2468    video_avi_add_sound - add sound to an AVI
2469    recording
2470-------------------------------------------------*/
2471
2472void video_avi_add_sound(running_machine *machine, const INT16 *sound, int numsamples)
2473{
2474   const device_config *screen;
2475
2476   /* loop over screens that might be recording */
2477   for (screen = video_screen_first(machine->config); screen != NULL; screen = video_screen_next(screen))
2478   {
2479      screen_state *state = get_safe_token(screen);
2480
2481      /* only record if we have a file */
2482      if (state->avi_file != NULL)
2483      {
2484         avi_error avierr;
2485
2486         profiler_mark(PROFILER_MOVIE_REC);
2487
2488         /* write the next frame */
2489         avierr = avi_append_sound_samples(state->avi_file, 0, sound + 0, numsamples, 1);
2490         if (avierr == AVIERR_NONE)
2491            avierr = avi_append_sound_samples(state->avi_file, 1, sound + 1, numsamples, 1);
2492         if (avierr != AVIERR_NONE)
2493            video_avi_end_recording(screen);
2494
2495         profiler_mark(PROFILER_END);
2496      }
2497   }
2498}
2499
2500
2501
2502/***************************************************************************
23492503    SOFTWARE RENDERING
23502504***************************************************************************/
23512505
src/emu/memory.c
r2627r2628
218218   UINT8               spacenum;            /* which address space are we associated with? */
219219   UINT8               isallocated;         /* did we allocate this ourselves? */
220220   offs_t                bytestart, byteend;      /* byte-normalized start/end for verifying a match */
221    UINT8 *               data;               /* pointer to the data for this block */
221   UINT8 *               data;               /* pointer to the data for this block */
222222};
223223
224224typedef struct _bank_data bank_info;
r2627r2628
293293   UINT8 *               region;               /* pointer to memory region */
294294   size_t               regionsize;            /* size of region, in bytes */
295295
296   opbase_handler_func    opbase;               /* opcode base handler */
296   opbase_handler_func    opbase_handler;         /* opcode base handler */
297   opbase_data            opbase;               /* dynamic opcode data */
297298
298   void *               op_ram;               /* dynamic RAM base pointer */
299   void *               op_rom;               /* dynamic ROM base pointer */
300   offs_t               op_mask;            /* dynamic ROM address mask */
301   offs_t               op_mem_min;            /* dynamic ROM/RAM min */
302   offs_t               op_mem_max;            /* dynamic ROM/RAM max */
303   UINT8                opcode_entry;         /* opcode base handler */
304
305299   UINT8               spacemask;            /* mask of which address spaces are used */
306300   addrspace_data          space[ADDRESS_SPACES];   /* info about each address space */
307301};
r2627r2628
312306    GLOBAL VARIABLES
313307***************************************************************************/
314308
315UINT8 *                  opcode_base;               /* opcode base */
316UINT8 *                  opcode_arg_base;            /* opcode argument base */
317offs_t                  opcode_mask;               /* mask to apply to the opcode address */
318offs_t                  opcode_memory_min;            /* opcode memory minimum */
319offs_t                  opcode_memory_max;            /* opcode memory maximum */
320UINT8                   opcode_entry;               /* opcode readmem entry */
309opbase_data               opbase;                     /* opcode data */
321310
322311address_space            active_address_space[ADDRESS_SPACES];/* address space data */
323312
r2627r2628
329318
330319static int               cur_context;               /* current CPU context */
331320
332static opbase_handler_func   opbasefunc;                  /* opcode base override */
321static opbase_handler_func   opbase_handler;               /* opcode base override */
333322
334323static UINT8            debugger_access;            /* treat accesses as coming from the debugger */
335324static UINT8            log_unmap[ADDRESS_SPACES];      /* log unmapped memory accesses */
r2627r2628
439428
440429INLINE void force_opbase_update(void)
441430{
442   opcode_entry = 0xff;
431   opbase.entry = 0xff;
443432   memory_set_opbase(activecpu_get_physical_pc_byte());
444433}
445434
r2627r2628
552541   /* remember dynamic RAM/ROM */
553542   if (cur_context != -1)
554543   {
555      cpudata[cur_context].op_ram = opcode_arg_base;
556      cpudata[cur_context].op_rom = opcode_base;
557      cpudata[cur_context].op_mask = opcode_mask;
558      cpudata[cur_context].op_mem_min = opcode_memory_min;
559      cpudata[cur_context].op_mem_max = opcode_memory_max;
560      cpudata[cur_context].opcode_entry = opcode_entry;
544      cpudata[cur_context].opbase = opbase;
561545   }
562546   cur_context = activecpu;
563547
564   opcode_arg_base = cpudata[activecpu].op_ram;
565   opcode_base = cpudata[activecpu].op_rom;
566   opcode_mask = cpudata[activecpu].op_mask;
567   opcode_memory_min = cpudata[activecpu].op_mem_min;
568   opcode_memory_max = cpudata[activecpu].op_mem_max;
569   opcode_entry = cpudata[activecpu].opcode_entry;
548   opbase = cpudata[activecpu].opbase;
570549
571550   /* program address space */
572551   active_address_space[ADDRESS_SPACE_PROGRAM].bytemask = cpudata[activecpu].space[ADDRESS_SPACE_PROGRAM].bytemask;
r2627r2628
598577      active_address_space[ADDRESS_SPACE_IO].accessors = cpudata[activecpu].space[ADDRESS_SPACE_IO].accessors;
599578   }
600579
601   opbasefunc = cpudata[activecpu].opbase;
580   opbase_handler = cpudata[activecpu].opbase_handler;
602581
603582#ifdef ENABLE_DEBUGGER
604583   if (activecpu != -1 && Machine->debug_mode)
r2627r2628
890869            found = TRUE;
891870
892871            /* if we are executing from here, force an opcode base update */
893            if (cpu_getactivecpu() >= 0 && cpunum == cur_context && opcode_entry == banknum)
872            if (cpu_getactivecpu() >= 0 && cpunum == cur_context && opbase.entry == banknum)
894873               force_opbase_update();
895874         }
896875
r2627r2628
914893
915894opbase_handler_func memory_set_opbase_handler(int cpunum, opbase_handler_func function)
916895{
917   opbase_handler_func old = cpudata[cpunum].opbase;
918   cpudata[cpunum].opbase = function;
896   opbase_handler_func old = cpudata[cpunum].opbase_handler;
897   cpudata[cpunum].opbase_handler = function;
919898   if (cpunum == cpu_getactivecpu())
920      opbasefunc = function;
899      opbase_handler = function;
921900   return old;
922901}
923902
r2627r2628
935914   UINT8 entry;
936915
937916   /* allow overrides */
938   if (opbasefunc != NULL)
917   if (opbase_handler != NULL)
939918   {
940      byteaddress = (*opbasefunc)(Machine, byteaddress);
919      byteaddress = (*opbase_handler)(Machine, byteaddress, &opbase);
941920      if (byteaddress == ~0)
942921         return;
943922   }
r2627r2628
949928      entry = space->readlookup[LEVEL2_INDEX(entry,byteaddress)];
950929
951930   /* keep track of current entry */
952   opcode_entry = entry;
931   opbase.entry = entry;
953932
954933   /* if we don't map to a bank, see if there are any banks we can map to */
955934   if (entry < STATIC_BANK1 || entry >= STATIC_RAM)
r2627r2628
980959
981960   /* compute the adjusted base */
982961   handlers = &active_address_space[ADDRESS_SPACE_PROGRAM].readhandlers[entry];
983   opcode_mask = handlers->bytemask;
984   opcode_arg_base = base - (handlers->bytestart & opcode_mask);
985   opcode_base = based - (handlers->bytestart & opcode_mask);
986   opcode_memory_min = handlers->bytestart;
987   opcode_memory_max = handlers->byteend;
962   opbase.mask = handlers->bytemask;
963   opbase.ram = base - (handlers->bytestart & opbase.mask);
964   opbase.rom = based - (handlers->bytestart & opbase.mask);
965   opbase.mem_min = handlers->bytestart;
966   opbase.mem_max = handlers->byteend;
988967}
989968
990969
r2627r2628
10591038   UINT8 entry;
10601039
10611040   /* if there is a custom mapper, use that */
1062   if (cpudata[cpunum].opbase != NULL)
1041   if (cpudata[cpunum].opbase_handler != NULL)
10631042   {
10641043      /* need to save opcode info */
1065      UINT8 *saved_opcode_base = opcode_base;
1066      UINT8 *saved_opcode_arg_base = opcode_arg_base;
1067      offs_t saved_opcode_mask = opcode_mask;
1068      offs_t saved_opcode_memory_min = opcode_memory_min;
1069      offs_t saved_opcode_memory_max = opcode_memory_max;
1070      UINT8 saved_opcode_entry = opcode_entry;
1044      opbase_data saved_opbase = opbase;
10711045
10721046      /* query the handler */
1073      offs_t new_byteaddress = (*cpudata[cpunum].opbase)(Machine, byteaddress);
1047      offs_t new_byteaddress = (*cpudata[cpunum].opbase_handler)(Machine, byteaddress, &opbase);
10741048
10751049      /* if it returns ~0, we use whatever data the handler set */
10761050      if (new_byteaddress == ~0)
1077         ptr = arg ? &opcode_arg_base[byteaddress] : &opcode_base[byteaddress];
1051         ptr = arg ? &opbase.ram[byteaddress] : &opbase.rom[byteaddress];
10781052
10791053      /* otherwise, we use the new offset in the generic case below */
10801054      else
10811055         byteaddress = new_byteaddress;
10821056
10831057      /* restore opcode info */
1084      opcode_base = saved_opcode_base;
1085      opcode_arg_base = saved_opcode_arg_base;
1086      opcode_mask = saved_opcode_mask;
1087      opcode_memory_min = saved_opcode_memory_min;
1088      opcode_memory_max = saved_opcode_memory_max;
1089      opcode_entry = saved_opcode_entry;
1058      opbase = saved_opbase;
10901059
10911060      /* if we got our pointer, we're done */
10921061      if (ptr != NULL)
r2627r2628
11831152   bankd_ptr[banknum] = bankdata[banknum].entryd[entrynum];
11841153
11851154   /* if we're executing out of this bank, adjust the opbase pointer */
1186   if (opcode_entry == banknum && cpu_getactivecpu() >= 0)
1155   if (opbase.entry == banknum && cpu_getactivecpu() >= 0)
11871156      force_opbase_update();
11881157}
11891158
r2627r2628
12261195   bank_ptr[banknum] = base;
12271196
12281197   /* if we're executing out of this bank, adjust the opbase pointer */
1229   if (opcode_entry == banknum && cpu_getactivecpu() >= 0)
1198   if (opbase.entry == banknum && cpu_getactivecpu() >= 0)
12301199      force_opbase_update();
12311200}
12321201
r2627r2628
14591428      }
14601429
14611430      /* set the RAM/ROM base */
1462      cpu->op_ram = cpu->op_rom = memory_region(REGION_CPU1 + cpunum);
1463      cpu->op_mask = cpu->space[ADDRESS_SPACE_PROGRAM].bytemask;
1464      cpu->op_mem_min = 0;
1465      cpu->op_mem_max = memory_region_length(REGION_CPU1 + cpunum);
1466      cpu->opcode_entry = STATIC_UNMAP;
1467      cpu->opbase = NULL;
1431      cpu->opbase.ram = cpu->opbase.rom = memory_region(REGION_CPU1 + cpunum);
1432      cpu->opbase.mask = cpu->space[ADDRESS_SPACE_PROGRAM].bytemask;
1433      cpu->opbase.mem_min = 0;
1434      cpu->opbase.mem_max = memory_region_length(REGION_CPU1 + cpunum);
1435      cpu->opbase.entry = STATIC_UNMAP;
1436      cpu->opbase_handler = NULL;
14681437   }
14691438}
14701439
src/emu/video.h
r2627r2628
263263
264264/* ----- movie recording ----- */
265265
266int video_is_movie_active(const device_config *screen);
267void video_movie_begin_recording(const device_config *screen, const char *name);
268void video_movie_end_recording(const device_config *screen);
266int video_mng_is_movie_active(const device_config *screen);
267void video_mng_begin_recording(const device_config *screen, const char *name);
268void video_mng_end_recording(const device_config *screen);
269269
270int video_avi_is_movie_active(const device_config *screen);
271void video_avi_begin_recording(const device_config *screen, const char *name);
272void video_avi_end_recording(const device_config *screen);
273void video_avi_add_sound(running_machine *machine, const INT16 *sound, int numsamples);
270274
271275#endif   /* __VIDEO_H__ */
src/emu/emu.mak
r2627r2628
131131   $(EMUMACHINE)/adc1213x.o \
132132    $(EMUMACHINE)/am53cf96.o \
133133    $(EMUMACHINE)/at28c16.o \
134    $(EMUMACHINE)/cdp1852.o \
134135   $(EMUMACHINE)/ds1302.o \
135136   $(EMUMACHINE)/ds2401.o \
136137   $(EMUMACHINE)/ds2404.o \
src/emu/memory.h
r2627r2628
9191typedef UINT32   offs_t;
9292
9393
94/* opbase_data contains state data for opcode handling */
95typedef struct _opbase_data opbase_data;
96struct _opbase_data
97{
98   UINT8 *               rom;            /* opcode ROM base pointer */
99   UINT8 *               ram;            /* opcode RAM base pointer */
100   offs_t               mask;            /* opcode ROM address mask */
101   offs_t               mem_min;         /* opcode ROM/RAM min */
102   offs_t               mem_max;         /* opcode ROM/RAM max */
103   UINT8                entry;            /* opcode readmem entry */
104};
105
106
94107/* opcode base adjustment handler */
95typedef offs_t   (*opbase_handler_func) (ATTR_UNUSED running_machine *machine, ATTR_UNUSED offs_t address);
108typedef offs_t   (*opbase_handler_func) (ATTR_UNUSED running_machine *machine, ATTR_UNUSED offs_t address, ATTR_UNUSED opbase_data *opbase);
96109
97110
98111/* machine read/write handlers */
r2627r2628
362375***************************************************************************/
363376
364377/* opcode base adjustment handler function macro */
365#define OPBASE_HANDLER(name)         offs_t name(ATTR_UNUSED running_machine *machine, ATTR_UNUSED offs_t address)
378#define OPBASE_HANDLER(name)         offs_t name(ATTR_UNUSED running_machine *machine, ATTR_UNUSED offs_t address, opbase_data *opbase)
366379
367380
368381/* machine read/write handler function macros */
r2627r2628
453466
454467
455468/* opcode range safety check */
456#define address_is_unsafe(A)         ((UNEXPECTED((A) < opcode_memory_min) || UNEXPECTED((A) > opcode_memory_max)))
469#define address_is_unsafe(A)         ((UNEXPECTED((A) < opbase.mem_min) || UNEXPECTED((A) > opbase.mem_max)))
457470
458471
459472/* unsafe opcode and opcode argument reading */
460#define cpu_opptr_unsafe(A)            ((void *)&opcode_base[(A) & opcode_mask])
461#define cpu_readop_unsafe(A)         (opcode_base[(A) & opcode_mask])
462#define cpu_readop16_unsafe(A)         (*(UINT16 *)&opcode_base[(A) & opcode_mask])
463#define cpu_readop32_unsafe(A)         (*(UINT32 *)&opcode_base[(A) & opcode_mask])
464#define cpu_readop64_unsafe(A)         (*(UINT64 *)&opcode_base[(A) & opcode_mask])
465#define cpu_readop_arg_unsafe(A)      (opcode_arg_base[(A) & opcode_mask])
466#define cpu_readop_arg16_unsafe(A)      (*(UINT16 *)&opcode_arg_base[(A) & opcode_mask])
467#define cpu_readop_arg32_unsafe(A)      (*(UINT32 *)&opcode_arg_base[(A) & opcode_mask])
468#define cpu_readop_arg64_unsafe(A)      (*(UINT64 *)&opcode_arg_base[(A) & opcode_mask])
473#define cpu_opptr_unsafe(A)            ((void *)&opbase.rom[(A) & opbase.mask])
474#define cpu_readop_unsafe(A)         (opbase.rom[(A) & opbase.mask])
475#define cpu_readop16_unsafe(A)         (*(UINT16 *)&opbase.rom[(A) & opbase.mask])
476#define cpu_readop32_unsafe(A)         (*(UINT32 *)&opbase.rom[(A) & opbase.mask])
477#define cpu_readop64_unsafe(A)         (*(UINT64 *)&opbase.rom[(A) & opbase.mask])
478#define cpu_readop_arg_unsafe(A)      (opbase.ram[(A) & opbase.mask])
479#define cpu_readop_arg16_unsafe(A)      (*(UINT16 *)&opbase.ram[(A) & opbase.mask])
480#define cpu_readop_arg32_unsafe(A)      (*(UINT32 *)&opbase.ram[(A) & opbase.mask])
481#define cpu_readop_arg64_unsafe(A)      (*(UINT64 *)&opbase.ram[(A) & opbase.mask])
469482
470483
471484/* wrappers for dynamic read handler installation */
r2627r2628
800813    GLOBAL VARIABLES
801814***************************************************************************/
802815
803extern UINT8          opcode_entry;            /* current entry for opcode fetching */
804extern UINT8 *         opcode_base;            /* opcode ROM base */
805extern UINT8 *         opcode_arg_base;         /* opcode RAM base */
806extern offs_t         opcode_mask;            /* mask to apply to the opcode address */
807extern offs_t         opcode_memory_min;         /* opcode memory minimum */
808extern offs_t         opcode_memory_max;         /* opcode memory maximum */
809816extern address_space   active_address_space[];      /* address spaces */
810817
811818extern const char *const address_space_names[ADDRESS_SPACES];
r2627r2628
941948UINT64   cpu_readop_arg64_safe(offs_t byteaddress);
942949
943950/* opcode and opcode argument reading */
944INLINE void * cpu_opptr(offs_t byteaddress)         { if (address_is_unsafe(byteaddress)) { memory_set_opbase(byteaddress); } return cpu_opptr_unsafe(byteaddress); }
945INLINE UINT8  cpu_readop(offs_t byteaddress)      { if (address_is_unsafe(byteaddress)) { memory_set_opbase(byteaddress); } return cpu_readop_unsafe(byteaddress); }
946INLINE UINT16 cpu_readop16(offs_t byteaddress)      { if (address_is_unsafe(byteaddress)) { memory_set_opbase(byteaddress); } return cpu_readop16_unsafe(byteaddress); }
947INLINE UINT32 cpu_readop32(offs_t byteaddress)      { if (address_is_unsafe(byteaddress)) { memory_set_opbase(byteaddress); } return cpu_readop32_unsafe(byteaddress); }
948INLINE UINT64 cpu_readop64(offs_t byteaddress)      { if (address_is_unsafe(byteaddress)) { memory_set_opbase(byteaddress); } return cpu_readop64_unsafe(byteaddress); }
949INLINE UINT8  cpu_readop_arg(offs_t byteaddress)   { if (address_is_unsafe(byteaddress)) { memory_set_opbase(byteaddress); } return cpu_readop_arg_unsafe(byteaddress); }
950INLINE UINT16 cpu_readop_arg16(offs_t byteaddress)   { if (address_is_unsafe(byteaddress)) { memory_set_opbase(byteaddress); } return cpu_readop_arg16_unsafe(byteaddress); }
951INLINE UINT32 cpu_readop_arg32(offs_t byteaddress)   { if (address_is_unsafe(byteaddress)) { memory_set_opbase(byteaddress); } return cpu_readop_arg32_unsafe(byteaddress); }
952INLINE UINT64 cpu_readop_arg64(offs_t byteaddress)   { if (address_is_unsafe(byteaddress)) { memory_set_opbase(byteaddress); } return cpu_readop_arg64_unsafe(byteaddress); }
951INLINE void * cpu_opptr(offs_t byteaddress)         { extern opbase_data opbase; if (address_is_unsafe(byteaddress)) { memory_set_opbase(byteaddress); } return cpu_opptr_unsafe(byteaddress); }
952INLINE UINT8  cpu_readop(offs_t byteaddress)      { extern opbase_data opbase; if (address_is_unsafe(byteaddress)) { memory_set_opbase(byteaddress); } return cpu_readop_unsafe(byteaddress); }
953INLINE UINT16 cpu_readop16(offs_t byteaddress)      { extern opbase_data opbase; if (address_is_unsafe(byteaddress)) { memory_set_opbase(byteaddress); } return cpu_readop16_unsafe(byteaddress); }
954INLINE UINT32 cpu_readop32(offs_t byteaddress)      { extern opbase_data opbase; if (address_is_unsafe(byteaddress)) { memory_set_opbase(byteaddress); } return cpu_readop32_unsafe(byteaddress); }
955INLINE UINT64 cpu_readop64(offs_t byteaddress)      { extern opbase_data opbase; if (address_is_unsafe(byteaddress)) { memory_set_opbase(byteaddress); } return cpu_readop64_unsafe(byteaddress); }
956INLINE UINT8  cpu_readop_arg(offs_t byteaddress)   { extern opbase_data opbase; if (address_is_unsafe(byteaddress)) { memory_set_opbase(byteaddress); } return cpu_readop_arg_unsafe(byteaddress); }
957INLINE UINT16 cpu_readop_arg16(offs_t byteaddress)   { extern opbase_data opbase; if (address_is_unsafe(byteaddress)) { memory_set_opbase(byteaddress); } return cpu_readop_arg16_unsafe(byteaddress); }
958INLINE UINT32 cpu_readop_arg32(offs_t byteaddress)   { extern opbase_data opbase; if (address_is_unsafe(byteaddress)) { memory_set_opbase(byteaddress); } return cpu_readop_arg32_unsafe(byteaddress); }
959INLINE UINT64 cpu_readop_arg64(offs_t byteaddress)   { extern opbase_data opbase; if (address_is_unsafe(byteaddress)) { memory_set_opbase(byteaddress); } return cpu_readop_arg64_unsafe(byteaddress); }
953960
954961
955962/***************************************************************************
src/emu/machine/6526cia.c
r2627r2628
6767   UINT8      out;
6868   UINT8      (*read)(void);
6969   void      (*write)(UINT8);
70   UINT8      mask_value; /* in READ operation the value can be forced by a extern electric circuit */
7071};
7172
7273struct _cia_state
r2627r2628
166167   {
167168      cia->port[p].read = intf->port[p].read;
168169      cia->port[p].write = intf->port[p].write;
170      cia->port[p].mask_value = 0xff;
169171   }
170172
171173   /* setup timers */
r2627r2628
190192   state_save_register_item("6526cia", which, cia->port[0].latch);
191193   state_save_register_item("6526cia", which, cia->port[0].in);
192194   state_save_register_item("6526cia", which, cia->port[0].out);
195   state_save_register_item("6526cia", which, cia->port[0].mask_value);
193196   state_save_register_item("6526cia", which, cia->port[1].ddr);
194197   state_save_register_item("6526cia", which, cia->port[1].latch);
195198   state_save_register_item("6526cia", which, cia->port[1].in);
196199   state_save_register_item("6526cia", which, cia->port[1].out);
200   state_save_register_item("6526cia", which, cia->port[1].mask_value);
197201   state_save_register_item("6526cia", which, cia->timer[0].latch);
198202   state_save_register_item("6526cia", which, cia->timer[0].count);
199203   state_save_register_item("6526cia", which, cia->timer[0].mode);
r2627r2628
218222   state_save_register_item("6526cia", which, cia->serial);
219223}
220224
225void cia_set_port_mask_value(int which, int port, int data)
226{
227   cia_state *cia = &cia_array[which];
228   cia->port[port].mask_value = data;
229}
221230
222
223231void cia_reset(void)
224232{
225233   int i, t;
r2627r2628
235243         cia->port[0].latch = 0x00;
236244         cia->port[0].in = 0x00;
237245         cia->port[0].out = 0x00;
246         cia->port[0].mask_value = 0xff;
238247         cia->port[1].latch = 0x00;
239248         cia->port[1].in = 0x00;
240249         cia->port[1].out = 0x00;
250         cia->port[1].mask_value = 0xff;
241251         cia->tod = 0;
242252         cia->tod_latch = 0;
243253         cia->alarm = 0;
r2627r2628
569579      case CIA_PRB:
570580         port = &cia->port[offset & 1];
571581         data = port->read ? (*port->read)() : 0;
572         //data = data & (port->latch | ~port->ddr);
573         data = (data & ~port->ddr) | (port->latch & port->ddr);
582         data = ((data & ~port->ddr) | (port->latch & port->ddr)) & port->mask_value;
574583         port->in = data;
575584
576585         if (offset == CIA_PRB)
src/emu/machine/6526cia.h
r2627r2628
3434/* configuration and reset */
3535void cia_config(int which, const cia6526_interface *intf);
3636void cia_reset(void);
37void cia_set_port_mask_value(int which, int port, int data);
3738
3839/* reading and writing */
3940UINT8 cia_read(int which, offs_t offset);
src/emu/machine/cdp1852.c
r0r2628
1/**********************************************************************
2
3    RCA CDP1852 Byte-Wide Input/Output Port emulation
4
5    Copyright Nicola Salmoria and the MAME Team.
6    Visit http://mamedev.org for licensing and usage restrictions.
7
8**********************************************************************/
9
10#include "driver.h"
11#include "cdp1852.h"
12
13typedef struct _cdp1852_t cdp1852_t;
14struct _cdp1852_t
15{
16   const cdp1852_interface *intf;   /* interface */
17
18   int new_data;               /* new data written */
19   UINT8 data;                  /* data latch */
20   UINT8 next_data;            /* next data*/
21
22   int sr;                     /* service request flag */
23   int next_sr;               /* next value of service request flag */
24
25   /* timers */
26   emu_timer *scan_timer;         /* scan timer */
27};
28
29INLINE cdp1852_t *get_safe_token(const device_config *device)
30{
31   assert(device != NULL);
32   assert(device->token != NULL);
33
34   return (cdp1852_t *)device->token;
35}
36
37static void set_sr_line(const device_config *device, int level)
38{
39   cdp1852_t *cdp1852 = get_safe_token(device);
40
41   if (cdp1852->intf->on_sr_changed)
42   {
43      if (cdp1852->sr != level)
44      {
45         cdp1852->intf->on_sr_changed(device, level);
46         cdp1852->sr = level;
47      }
48   }
49}
50
51static TIMER_CALLBACK( cdp1852_scan_tick )
52{
53   const device_config *device = ptr;
54   cdp1852_t *cdp1852 = get_safe_token(device);
55
56   switch (cdp1852->intf->mode)
57   {
58   case CDP1852_MODE_INPUT:
59      // input data into register
60      cdp1852->data = cdp1852->intf->data_r(device);
61
62      // signal processor
63      set_sr_line(device, 0);
64      break;
65
66   case CDP1852_MODE_OUTPUT:
67      if (cdp1852->new_data)
68      {
69         cdp1852->new_data = 0;
70
71         // latch data into register
72         cdp1852->data = cdp1852->next_data;
73
74         // output data
75         cdp1852->intf->data_w(device, cdp1852->data);
76
77         // signal peripheral device
78         set_sr_line(device, 1);
79
80         cdp1852->next_sr = 0;
81      }
82      else
83      {
84         set_sr_line(device, cdp1852->next_sr);
85      }
86      break;
87   }
88}
89
90/* Data Access */
91
92READ8_DEVICE_HANDLER( cdp1852_data_r )
93{
94   cdp1852_t *cdp1852 = get_safe_token(device);
95
96   if (cdp1852->intf->mode == CDP1852_MODE_INPUT && cdp1852->intf->clock == 0)
97   {
98      // input data into register
99      cdp1852->data = cdp1852->intf->data_r(device);
100   }
101
102   set_sr_line(device, 1);
103
104   return cdp1852->data;
105}
106
107WRITE8_DEVICE_HANDLER( cdp1852_data_w )
108{
109   cdp1852_t *cdp1852 = get_safe_token(device);
110
111   if (cdp1852->intf->mode == CDP1852_MODE_OUTPUT)
112   {
113      cdp1852->next_data = data;
114      cdp1852->new_data = 1;
115   }
116}
117
118/* Device Interface */
119
120static DEVICE_START( cdp1852 )
121{
122   cdp1852_t *cdp1852 = get_safe_token(device);
123   char unique_tag[30];
124
125   /* validate arguments */
126   assert(device != NULL);
127   assert(device->tag != NULL);
128   assert(strlen(device->tag) < 20);
129
130   cdp1852->intf = device->static_config;
131
132   assert(cdp1852->intf != NULL);
133
134   if (cdp1852->intf->mode == CDP1852_MODE_INPUT)
135   {
136      assert(cdp1852->intf->data_r != NULL);
137   }
138   else
139   {
140      assert(cdp1852->intf->clock > 0);
141      assert(cdp1852->intf->data_w != NULL);
142   }
143
144   /* create the timers */
145   if (cdp1852->intf->clock > 0)
146   {
147      cdp1852->scan_timer = timer_alloc(cdp1852_scan_tick, (void *)device);
148      timer_adjust_periodic(cdp1852->scan_timer, attotime_zero, 0, ATTOTIME_IN_HZ(cdp1852->intf->clock));
149   }
150
151   /* register for state saving */
152   state_save_combine_module_and_tag(unique_tag, "CDP1852", device->tag);
153
154   state_save_register_item(unique_tag, 0, cdp1852->new_data);
155   state_save_register_item(unique_tag, 0, cdp1852->data);
156   state_save_register_item(unique_tag, 0, cdp1852->next_data);
157   state_save_register_item(unique_tag, 0, cdp1852->sr);
158   state_save_register_item(unique_tag, 0, cdp1852->next_sr);
159}
160
161static DEVICE_RESET( cdp1852 )
162{
163   cdp1852_t *cdp1852 = get_safe_token(device);
164
165   // reset data register
166   cdp1852->data = 0;
167
168   if (cdp1852->intf->mode == CDP1852_MODE_INPUT)
169   {
170      // reset service request flip-flop
171      set_sr_line(device, 1);
172   }
173   else
174   {
175      // output data
176      cdp1852->intf->data_w(device, 0);
177
178      // reset service request flip-flop
179      set_sr_line(device, 0);
180   }
181}
182
183static DEVICE_SET_INFO( cdp1852 )
184{
185   switch (state)
186   {
187      /* no parameters to set */
188   }
189}
190
191DEVICE_GET_INFO( cdp1852 )
192{
193   switch (state)
194   {
195      /* --- the following bits of info are returned as 64-bit signed integers --- */
196      case DEVINFO_INT_TOKEN_BYTES:               info->i = sizeof(cdp1852_t);            break;
197      case DEVINFO_INT_INLINE_CONFIG_BYTES:         info->i = 0;                        break;
198      case DEVINFO_INT_CLASS:                     info->i = DEVICE_CLASS_PERIPHERAL;         break;
199
200      /* --- the following bits of info are returned as pointers to data or functions --- */
201      case DEVINFO_FCT_SET_INFO:                  info->set_info = DEVICE_SET_INFO_NAME(cdp1852); break;
202      case DEVINFO_FCT_START:                     info->start = DEVICE_START_NAME(cdp1852);   break;
203      case DEVINFO_FCT_STOP:                     /* Nothing */                        break;
204      case DEVINFO_FCT_RESET:                     info->reset = DEVICE_RESET_NAME(cdp1852);   break;
205
206      /* --- the following bits of info are returned as NULL-terminated strings --- */
207      case DEVINFO_STR_NAME:                     info->s = "RCA CDP1852";               break;
208      case DEVINFO_STR_FAMILY:                  info->s = "RCA CDP1800";               break;
209      case DEVINFO_STR_VERSION:                  info->s = "1.0";                     break;
210      case DEVINFO_STR_SOURCE_FILE:               info->s = __FILE__;                     break;
211      case DEVINFO_STR_CREDITS:                  info->s = "Copyright Nicola Salmoria and the MAME Team"; break;
212   }
213}
src/emu/machine/cdp1852.h
r0r2628
1/**********************************************************************
2
3    RCA CDP1852 Byte-Wide Input/Output Port emulation
4
5    Copyright MESS Team.
6    Visit http://mamedev.org for licensing and usage restrictions.
7
8**********************************************************************
9                            _____   _____
10              CSI/_CSI   1 |*    \_/     | 24  Vdd
11                  MODE   2 |             | 23  _SR/SR
12                   DI0   3 |             | 22  DI7
13                   DO0   4 |             | 21  DO7
14                   DI1   5 |             | 20  DI6
15                   DO1   6 |   CDP1852   | 19  DO6
16                   DI2   7 |             | 18  DI5
17                   DO2   8 |             | 17  DO5
18                   DI3   9 |             | 16  DI4
19                   DO3  10 |             | 15  DO4
20                 CLOCK  11 |             | 14  _CLEAR
21                   Vss  12 |_____________| 13  CS2
22
23**********************************************************************/
24
25#ifndef __CDP1852__
26#define __CDP1852__
27
28#define CDP1852_CLOCK_HIGH   0
29
30typedef UINT8 (*cdp1852_data_read_func) (const device_config *device);
31#define CDP1852_DATA_READ(name) UINT8 name(const device_config *device)
32
33typedef void (*cdp1852_data_write_func) (const device_config *device, UINT8 data);
34#define CDP1852_DATA_WRITE(name) void name(const device_config *device, UINT8 data)
35
36typedef void (*cdp1852_on_sr_changed_func) (const device_config *device, int level);
37#define CDP1852_ON_SR_CHANGED(name) void name(const device_config *device, int level)
38
39#define CDP1852      DEVICE_GET_INFO_NAME(cdp1852)
40
41typedef enum _cdp1852_mode cdp1852_mode;
42enum _cdp1852_mode {
43   CDP1852_MODE_INPUT = 0,
44   CDP1852_MODE_OUTPUT
45};
46
47/* interface */
48typedef struct _cdp1852_interface cdp1852_interface;
49struct _cdp1852_interface
50{
51   int clock;               /* the clock (pin 1) of the chip */
52
53   int mode;               /* operation mode */
54
55   /* this gets called for every external data read */
56   cdp1852_data_read_func         data_r;
57
58   /* this gets called for every external data write */
59   cdp1852_data_write_func         data_w;
60
61   /* this gets called for every change of the SR pin (pin 23) */
62   cdp1852_on_sr_changed_func      on_sr_changed;
63};
64#define CDP1852_INTERFACE(name) const cdp1852_interface (name)=
65
66/* device interface */
67DEVICE_GET_INFO( cdp1852 );
68
69/* data access */
70READ8_DEVICE_HANDLER( cdp1852_data_r );
71WRITE8_DEVICE_HANDLER( cdp1852_data_w );
72
73#endif
src/emu/cpuintrf.c
r2627r2628
6161void i8052_get_info(UINT32 state, cpuinfo *info);
6262void i8751_get_info(UINT32 state, cpuinfo *info);
6363void i8752_get_info(UINT32 state, cpuinfo *info);
64void ds5002fp_get_info(UINT32 state, cpuinfo *info);
6465void m6800_get_info(UINT32 state, cpuinfo *info);
6566void m6801_get_info(UINT32 state, cpuinfo *info);
6667void m6802_get_info(UINT32 state, cpuinfo *info);
r2627r2628
401402#if (HAS_I8752)
402403   { CPU_I8752, i8752_get_info },
403404#endif
405#if (HAS_DS5002FP)
406   { CPU_DS5002FP, ds5002fp_get_info },
407#endif
404408#if (HAS_M6800)
405409   { CPU_M6800, m6800_get_info },
406410#endif
src/emu/cpuexec.h
r2627r2628
8888   CPU_I8052,
8989   CPU_I8751,
9090   CPU_I8752,
91   CPU_DS5002FP,
9192   CPU_M6800,
9293   CPU_M6801,
9394   CPU_M6802,
src/emu/sound/tms5110.c
r2627r2628
591591      }
592592      else
593593      {
594         /* generate voiced samples here */
595         if (tms->coeff->subtype & (SUBTYPE_TMS5100 | SUBTYPE_M58817))
596         {
597            if (tms->pitch_count > 50)
598               current_val = 0;
599            else
600               current_val = tms->coeff->chirptable[tms->pitch_count];
594                    /* generate voiced samples here */
595            /* US patent 4331836 Figure 14B shows, and logic would hold, that a pitch based chirp
596             * function has a chirp/peak and then a long chain of zeroes.
597             * The last entry of the chirp rom is at address 0b110011 (50d), the 51st sample,
598             * and if the address reaches that point the ADDRESS incrementer is
599             * disabled, forcing all samples beyond 50d to be == 50d
600             * (address 50d holds zeroes)
601             */
601602
602         }
603         else
604            current_val = (tms->coeff->chirptable[tms->pitch_count%sizeof(tms->coeff->chirptable)]);
603     /*if (tms->coeff->subtype & (SUBTYPE_TMS5100 | SUBTYPE_M58817))*/
604
605      if (tms->pitch_count > 50)
606         current_val = tms->coeff->chirptable[50];
607      else
608         current_val = tms->coeff->chirptable[tms->pitch_count];
605609      }
606610
607611
src/emu/sound/s14001a.c
r2627r2628
11/*
22
3 TSI S14001A emulator v1.11
3 TSI S14001A emulator v1.20
44 By Jonathan Gevaryahu ("Lord Nightmare") with help from Kevin Horton ("kevtris")
55 MAME conversion and integration by R. Belmont
6 Clock Frequency control updated by Zsolt Vasvari
67
78 Copyright Jonathan Gevaryahu.
89
r2627r2628
293294{
294295   UINT8 CurDelta; // Current delta
295296
296   /* on even clocks, audio output is floating, /romen is low so rom data bus is driven, input is latched?
297     * on odd clocks, audio output is driven, /romen is high, state machine 2 is clocked*/
297   /* on even clocks, audio output is floating, /romen is low so rom data bus is driven
298         * on odd clocks, audio output is driven, /romen is high, state machine 2 is clocked
299         */
298300   chip->oddeven = !(chip->oddeven); // invert the clock
299301   if (chip->oddeven == 0) // even clock
300302        {
src/emu/sound/tms5220.c
r2627r2628
11/**********************************************************************************************
22
3     TMS5220 simulator
3     TMS5200/5220 simulator
44
55     Written for MAME by Frank Palazzolo
66     With help from Neill Corlett
r2627r2628
1111     Various fixes by Lord Nightmare
1212     Modularization by Lord Nightmare
1313
14     Much information regarding these lpc encoding comes from US patent 4,209,844
15     US patent 4,331,836 describes the complete 51xx chip
16     US patent 4,335,277 describes the complete 52xx chip
17     Special Thanks to Larry Brantingham for answering questions regarding the chip details
18
1419***********************************************************************************************/
1520
1621#include "sndintrf.h"
r2627r2628
109114   UINT8 RDB_flag;               /* whether we should read data register or status register */
110115
111116   /* flag for variant tmc0285/tms5200 emulation */
112   /* The TMC0285 AKA TMS5200 is an early variant of the TMS5220 used in
117   /* The TMC0285 AKA TMS5200 is an earlier variant of the TMS5220 used in
113118       the early releases of the Speech Module for the TI-99/4(a) computer,
114119       in Zaccaria's 'Money Money', and in a few other places.
115       The TMS5200 has a bug in its state machine PLA which causes it to
116       incorrectly use the LPC K3 parameter table for both K3 and K4.
117       (the chip has the unused K4 parameter in its internal ROM, but it is
118       never accessed due to the PLA bug)
119       The bug was fixed in the quickly-released TMS5220.
120       TI may have sold the remaining stocks of TMS5200s at a discount, and
121       apparently provided a special encoder to work around the bug.
122       Other than the bug, the two chips are identical.
123       Another variant of the TMS5220 is the TMS5220C/TSP5220C, which adds
124       an additional opcode (to select the data rate).
120       The TMS5200 has a different set of LPC coefficients, and a different
121       chirp table than the 5220 (which is not yet dumped)
122       Due to the vast superiority of the quality of the TMS5220, TI may have
123       sold the remaining stocks of TMS5200s at a discount, and provided a
124       special encoder to use the older tables.
125       Other than those differences, the two chips are identical.
126       Another variant of the TMS5220 is the TMS5220C/TSP5220C, which replaces
127       the X0X0 'NOP' opcode with an opcode to select the number of
128       interpolations per frame to either be defined at each frame, or be fixed
129       at either 8, 6, 4, or 2. The TMS5200/5220 is always fixed at 8.
125130     */
126131   tms5220_variant variant;
127132    /* The TMS52xx has two different ways of providing output data: the
128133       analog speaker pin (which was usually used) and the Digital I/O pin.
129       The internal DAC used to feed the analog pin is only 8 bits, while the
130       digital pin gives full 12? bit resolution to the output data.
134       The internal DAC used to feed the analog pin is only 8 bits, and has the
135       funny clipping/clamping logic, while the digital pin gives full 12? bit
136       resolution of the output data.
131137     */
132138    UINT8 digital_select;
133139};
r2627r2628
361367     tms5220_status_read -- read status or data from the TMS5220
362368
363369      From the data sheet:
364        bit 0 = TS - Talk Status is active (high) when the VSP is processing speech data.
370        bit D0(bit 7) = TS - Talk Status is active (high) when the VSP is processing speech data.
365371                Talk Status goes active at the initiation of a Speak command or after nine
366372                bytes of data are loaded into the FIFO following a Speak External command. It
367373                goes inactive (low) when the stop code (Energy=1111) is processed, or
368374                immediately by a buffer empty condition or a reset command.
369        bit 1 = BL - Buffer Low is active (high) when the FIFO buffer is more than half empty.
375        bit D1(bit 6) = BL - Buffer Low is active (high) when the FIFO buffer is more than half empty.
370376                Buffer Low is set when the "Last-In" byte is shifted down past the half-full
371377                boundary of the stack. Buffer Low is cleared when data is loaded to the stack
372378                so that the "Last-In" byte lies above the half-full boundary and becomes the
373379                ninth data byte of the stack.
374        bit 2 = BE - Buffer Empty is active (high) when the FIFO buffer has run out of data
380        bit D2(bit 5) = BE - Buffer Empty is active (high) when the FIFO buffer has run out of data
375381                while executing a Speak External command. Buffer Empty is set when the last bit
376382                of the "Last-In" byte is shifted out to the Synthesis Section. This causes
377383                Talk Status to be cleared. Speed is terminated at some abnormal point and the
r2627r2628
623629        else
624630        {
625631            /* generate voiced samples here */
626         tms->excitation_data = chirptable[tms->pitch_count%sizeof(chirptable)];
632            /* US patent 4331836 Figure 14B shows, and logic would hold, that a pitch based chirp
633             * function has a chirp/peak and then a long chain of zeroes.
634             * The last entry of the chirp rom is at address 0b110011 (50d), the 51st sample,
635             * and if the address reaches that point the ADDRESS incrementer is
636             * disabled, forcing all samples beyond 50d to be == 50d
637             * (address 50d holds zeroes)
638             */
639          if (tms->pitch_count > 50)
640              tms->excitation_data = chirptable[50];
641          else /*tms->pitch_count <= 50*/
642              tms->excitation_data = chirptable[tms->pitch_count];
627643        }
628644
629645        /* Update LFSR every clock, like patent shows */
src/emu/ui.c
r2627r2628
13211321   /* toggle movie recording */
13221322   if (input_ui_pressed(IPT_UI_RECORD_MOVIE))
13231323   {
1324      if (!video_is_movie_active(machine->primary_screen))
1324      if (!video_mng_is_movie_active(machine->primary_screen))
13251325      {
1326         video_movie_begin_recording(machine->primary_screen, NULL);
1326         video_mng_begin_recording(machine->primary_screen, NULL);
13271327         popmessage("REC START");
13281328      }
13291329      else
13301330      {
1331         video_movie_end_recording(machine->primary_screen);
1331         video_mng_end_recording(machine->primary_screen);
13321332         popmessage("REC STOP");
13331333      }
13341334   }
src/emu/sound.c
r2627r2628
753753   if (finalmix_offset > 0)
754754   {
755755      osd_update_audio_stream(machine, finalmix, finalmix_offset / 2);
756      video_avi_add_sound(machine, finalmix, finalmix_offset / 2);
756757      if (wavfile != NULL)
757758         wav_add_data_16(wavfile, finalmix, finalmix_offset);
758759   }
src/emu/fileio.c
r2627r2628
4444#ifdef DEBUG_COOKIE
4545   UINT32         debug_cookie;               /* sanity checking for debugging */
4646#endif
47   astring *      filename;                  /* full filename */
4748   core_file *      file;                     /* core file pointer */
4849   UINT32         openflags;                  /* flags we used for the open */
4950   char         hash[HASH_BUF_SIZE];         /* hash data for the file */
r2627r2628
214215{
215216   file_error filerr = FILERR_NOT_FOUND;
216217   path_iterator iterator;
217   astring *fullname;
218218
219219   /* can either have a hash or open for write, but not both */
220220   if ((openflags & OPEN_FLAG_HAS_CRC) && (openflags & OPEN_FLAG_WRITE))
r2627r2628
240240   path_iterator_init(&iterator, opts, searchpath);
241241
242242   /* loop over paths */
243   fullname = astring_alloc();
244   while (path_iterator_get_next(&iterator, fullname))
243   (*file)->filename = astring_alloc();
244   while (path_iterator_get_next(&iterator, (*file)->filename))
245245   {
246246      /* compute the full pathname */
247      if (astring_len(fullname) > 0)
248         astring_catc(fullname, PATH_SEPARATOR);
249      astring_catc(fullname, filename);
247      if (astring_len((*file)->filename) > 0)
248         astring_catc((*file)->filename, PATH_SEPARATOR);
249      astring_catc((*file)->filename, filename);
250250
251251      /* attempt to open the file directly */
252      filerr = core_fopen(astring_c(fullname), openflags, &(*file)->file);
252      filerr = core_fopen(astring_c((*file)->filename), openflags, &(*file)->file);
253253      if (filerr == FILERR_NONE)
254254         break;
255255
256256      /* if we're opening for read-only we have other options */
257257      if ((openflags & (OPEN_FLAG_READ | OPEN_FLAG_WRITE)) == OPEN_FLAG_READ)
258258      {
259         filerr = fopen_attempt_zipped(fullname, crc, openflags, *file);
259         filerr = fopen_attempt_zipped((*file)->filename, crc, openflags, *file);
260260         if (filerr == FILERR_NONE)
261261            break;
262262      }
263263   }
264   astring_free(fullname);
265264
266265   /* handle errors and return */
267266   if (filerr != FILERR_NONE)
r2627r2628
379378      core_fclose(file->file);
380379   if (file->zipdata != NULL)
381380      free(file->zipdata);
381   if (file->filename != NULL)
382      astring_free(file->filename);
382383   free(file);
383384}
384385
r2627r2628
702703
703704
704705/*-------------------------------------------------
706    mame_file_full_name - return the full filename
707    for a given mame_file
708-------------------------------------------------*/
709
710const char *mame_file_full_name(mame_file *file)
711{
712   /* return a pointer to the string if we have one; otherwise, return NULL */
713   if (file->filename != NULL)
714      return astring_c(file->filename);
715   return NULL;
716}
717
718
719/*-------------------------------------------------
705720    mame_fhash - returns the hash for a file
706721-------------------------------------------------*/
707722
src/emu/fileio.h
r2627r2628
163163/* return the core_file underneath the mame_file */
164164core_file *mame_core_file(mame_file *file);
165165
166/* return the full filename for a given mame_file */
167const char *mame_file_full_name(mame_file *file);
168
166169/* return a hash string for the file with the given functions */
167170const char *mame_fhash(mame_file *file, UINT32 functions);
168171
src/emu/drivers/xtal.h
r2627r2628
112112   XTAL_28MHz         = 28000000,
113113   XTAL_28_37516MHz   = 28375160,      /* Amiga PAL systems */
114114   XTAL_28_63636MHz     = 28636360,      /* Later Leland games and Atari GT, Amiga NTSC , Raiden2 h/w */
115   XTAL_30MHz         = 30000000,      /* Impera Magic Card */
115116   XTAL_30_4761MHz     = 30476100,      /* Taito JC */
116117   XTAL_32MHz         = 32000000,
117118   XTAL_32_22MHz        = 32220000,      /* Typically used on 90's Data East PCBs */
src/lib/util/aviio.c
r2627r2628
5252#define HANDLER_DIB            AVI_FOURCC('D','I','B',' ')
5353#define HANDLER_HFYU         AVI_FOURCC('h','f','y','u')
5454
55#define FORMAT_UYVY            AVI_FOURCC('U','Y','V','Y')
56#define FORMAT_VYUY            AVI_FOURCC('V','Y','U','Y')
57#define FORMAT_YUY2            AVI_FOURCC('Y','U','Y','2')
58#define FORMAT_HFYU            AVI_FOURCC('H','F','Y','U')
59
6055/* main AVI header files */
6156#define AVIF_HASINDEX         0x00000010
6257#define AVIF_MUSTUSEINDEX      0x00000020
r2627r2628
224219static avi_error soundbuf_write_chunk(avi_file *file, UINT32 framenum);
225220static avi_error soundbuf_flush(avi_file *file, int only_flush_full);
226221
222/* RGB helpers */
223static avi_error rgb32_compress_to_rgb(avi_stream *stream, const bitmap_t *bitmap, UINT8 *data, UINT32 numbytes);
224
227225/* YUY helpers */
228226static avi_error yuv_decompress_to_yuy16(avi_stream *stream, const UINT8 *data, UINT32 numbytes, bitmap_t *bitmap);
229227static avi_error yuy16_compress_to_yuy(avi_stream *stream, const bitmap_t *bitmap, UINT8 *data, UINT32 numbytes);
r2627r2628
928926   avi_error avierr;
929927   UINT32 maxlength;
930928
929   /* validate our ability to handle the data */
930   if (stream->format != FORMAT_UYVY && stream->format != FORMAT_VYUY && stream->format != FORMAT_YUY2 && stream->format != FORMAT_HFYU)
931      return AVIERR_UNSUPPORTED_VIDEO_FORMAT;
932
931933   /* double check bitmap format */
932934   if (bitmap->format != BITMAP_FORMAT_YUY16)
933935      return AVIERR_INVALID_BITMAP;
r2627r2628
960962
961963
962964/*-------------------------------------------------
965    avi_append_video_frame_rgb32 - append a frame
966    of video in RGB32 format
967-------------------------------------------------*/
968
969avi_error avi_append_video_frame_rgb32(avi_file *file, const bitmap_t *bitmap)
970{
971   avi_stream *stream = get_video_stream(file);
972   avi_error avierr;
973   UINT32 maxlength;
974
975   /* validate our ability to handle the data */
976   if (stream->format != 0)
977      return AVIERR_UNSUPPORTED_VIDEO_FORMAT;
978
979   /* depth must be 24 */
980   if (stream->depth != 24)
981      return AVIERR_UNSUPPORTED_VIDEO_FORMAT;
982
983   /* double check bitmap format */
984   if (bitmap->format != BITMAP_FORMAT_RGB32)
985      return AVIERR_INVALID_BITMAP;
986
987   /* write out any sound data first */
988   avierr = soundbuf_write_chunk(file, stream->chunks);
989   if (avierr != AVIERR_NONE)
990      return avierr;
991
992   /* make sure we have enough room */
993   maxlength = 3 * stream->width * stream->height;
994   avierr = expand_tempbuffer(file, maxlength);
995   if (avierr != AVIERR_NONE)
996      return avierr;
997
998   /* copy the RGB data to the destination */
999   avierr = rgb32_compress_to_rgb(stream, bitmap, file->tempbuffer, maxlength);
1000   if (avierr != AVIERR_NONE)
1001      return avierr;
1002
1003   /* set the info for this new chunk */
1004   avierr = set_stream_chunk_info(stream, stream->chunks, file->writeoffs, maxlength + 8);
1005   if (avierr != AVIERR_NONE)
1006      return avierr;
1007   stream->samples = file->info.video_numsamples = stream->chunks;
1008
1009   /* write the data */
1010   return chunk_write(file, get_chunkid_for_stream(file, stream), file->tempbuffer, maxlength);
1011}
1012
1013
1014/*-------------------------------------------------
9631015    avi_append_sound_samples - append sound
9641016    samples
9651017-------------------------------------------------*/
9661018
967avi_error avi_append_sound_samples(avi_file *file, int channel, const INT16 *samples, UINT32 numsamples)
1019avi_error avi_append_sound_samples(avi_file *file, int channel, const INT16 *samples, UINT32 numsamples, UINT32 sampleskip)
9681020{
9691021   UINT32 sampoffset = file->soundbuf_chansamples[channel];
9701022   UINT32 sampnum;
r2627r2628
9771029   for (sampnum = 0; sampnum < numsamples; sampnum++)
9781030   {
9791031      INT16 data = *samples++;
1032      samples += sampleskip;
9801033      data = LITTLE_ENDIANIZE_INT16(data);
9811034      file->soundbuf[sampoffset++ * file->info.audio_channels + channel] = data;
9821035   }
r2627r2628
22402293
22412294
22422295/*-------------------------------------------------
2296    rgb32_compress_to_rgb - "compress" an RGB32
2297    bitmap to an RGB encoded frame
2298-------------------------------------------------*/
2299
2300static avi_error rgb32_compress_to_rgb(avi_stream *stream, const bitmap_t *bitmap, UINT8 *data, UINT32 numbytes)
2301{
2302   int height = MIN(stream->height, bitmap->height);
2303   int width = MIN(stream->width, bitmap->width);
2304   UINT8 *dataend = data + numbytes;
2305   int x, y;
2306
2307   /* compressed video */
2308   for (y = 0; y < height; y++)
2309   {
2310      const UINT32 *source = (UINT32 *)bitmap->base + y * bitmap->rowpixels;
2311      UINT8 *dest = data + (stream->height - 1 - y) * stream->width * 3;
2312
2313      for (x = 0; x < width && dest < dataend; x++)
2314      {
2315         UINT32 pix = *source++;
2316         *dest++ = RGB_BLUE(pix);
2317         *dest++ = RGB_GREEN(pix);
2318         *dest++ = RGB_RED(pix);
2319      }
2320
2321      /* fill in any blank space on the right */
2322      for ( ; x < stream->width && dest < dataend; x++)
2323      {
2324         *dest++ = 0;
2325         *dest++ = 0;
2326         *dest++ = 0;
2327      }
2328   }
2329
2330   /* fill in any blank space on the bottom */
2331   for ( ; y < stream->height; y++)
2332   {
2333      UINT8 *dest = data + (stream->height - 1 - y) * stream->width * 3;
2334      for (x = 0; x < stream->width && dest < dataend; x++)
2335      {
2336         *dest++ = 0;
2337         *dest++ = 0;
2338         *dest++ = 0;
2339      }
2340   }
2341
2342   return AVIERR_NONE;
2343}
2344
2345
2346/*-------------------------------------------------
22432347    yuv_decompress_to_yuy16 - decompress a YUV
22442348    encoded frame to a YUY16 bitmap
22452349-------------------------------------------------*/
src/lib/util/aviio.h
r2627r2628
118118avi_error avi_read_sound_samples(avi_file *file, int channel, UINT32 firstsample, UINT32 numsamples, INT16 *output);
119119
120120avi_error avi_append_video_frame_yuy16(avi_file *file, const bitmap_t *bitmap);
121avi_error avi_append_sound_samples(avi_file *file, int channel, const INT16 *samples, UINT32 numsamples);
121avi_error avi_append_video_frame_rgb32(avi_file *file, const bitmap_t *bitmap);
122avi_error avi_append_sound_samples(avi_file *file, int channel, const INT16 *samples, UINT32 numsamples, UINT32 sampleskip);
122123
123124#endif
src/mess/machine/apple3.c
r2627r2628
648648   if ((address & 0xFF00) == 0x0000)
649649   {
650650      opptr = apple3_get_zpa_addr(address);
651      opcode_mask = ~0;
652      opcode_base = opcode_arg_base = opptr - address;
653      opcode_memory_min = address;
654      opcode_memory_max = address;
651      opbase->mask = ~0;
652      opbase->rom = opbase->ram = opptr - address;
653      opbase->mem_min = address;
654      opbase->mem_max = address;
655655      address = ~0;
656656   }
657657   return address;
src/mess/machine/osborne1.c
r2627r2628
194194static OPBASE_HANDLER( osborne1_opbase ) {
195195   if ( ( address & 0xF000 ) == 0x2000 ) {
196196      if ( ! osborne1.bank2_enabled ) {
197         opcode_mask = 0x0fff;
198         opcode_arg_base = mess_ram + 0x2000;
199         opcode_base = mess_ram + 0x2000;
200         opcode_memory_min = 0x2000;
201         opcode_memory_max = 0x2fff;
197         opbase->mask = 0x0fff;
198         opbase->ram = mess_ram + 0x2000;
199         opbase->rom = mess_ram + 0x2000;
200         opbase->mem_min = 0x2000;
201         opbase->mem_max = 0x2fff;
202202         return ~0;
203203      }
204204   }
src/mess/machine/apple2gs.c
r2627r2628
16001600            opptr = apple2gs_getslotmem(address);
16011601      }
16021602
1603      if (opptr)
1603      if (opptr != NULL)
16041604      {
1605         opcode_mask = ~0;
1606         opcode_base = opcode_arg_base = opptr - address;
1607         opcode_memory_min = address;
1608         opcode_memory_max = address;
1605         opbase->mask = ~0;
1606         opbase->rom = opbase->ram = opptr - address;
1607         opbase->mem_min = address;
1608         opbase->mem_max = address;
16091609         address = ~0;
16101610      }
16111611   }
src/mess/machine/lisa.c
r2627r2628
951951         }
952952         else
953953         {   /* system ROMs */
954            opcode_mask = 0xffffff;
955            opcode_base = opcode_arg_base = lisa_rom_ptr - (address & 0xffc000);
956            opcode_memory_min = (address & 0xffc000);
957            opcode_memory_max = (address & 0xffc000) + 0x003fff;
954            opbase->mask = 0xffffff;
955            opbase->rom = opbase->ram = lisa_rom_ptr - (address & 0xffc000);
956            opbase->mem_min = (address & 0xffc000);
957            opbase->mem_max = (address & 0xffc000) + 0x003fff;
958958            /*logerror("ROM (setup mode)\n");*/
959959         }
960960
r2627r2628
983983            /* out of segment limits : bus error */
984984            logerror("illegal opbase address%lX\n", (long) address);
985985         }
986         opcode_mask = 0xffffff;
987         opcode_base = opcode_arg_base = lisa_ram_ptr + mapped_address - address;
988         opcode_memory_min = (address & 0xffc000);
989         opcode_memory_max = (address & 0xffc000) + 0x003fff;
986         opbase->mask = 0xffffff;
987         opbase->rom = opbase->ram = lisa_ram_ptr + mapped_address - address;
988         opbase->mem_min = (address & 0xffc000);
989         opbase->mem_max = (address & 0xffc000) + 0x003fff;
990990         /*logerror("RAM\n");*/
991991         break;
992992
r2627r2628
999999         break;
10001000
10011001      case special_IO:
1002         opcode_mask = 0xffffff;
1003         opcode_base = opcode_arg_base = lisa_rom_ptr + (mapped_address & 0x003fff) - address;
1004         opcode_memory_min = (address & 0xffc000);
1005         opcode_memory_max = (address & 0xffc000) + 0x003fff;
1002         opbase->mask = 0xffffff;
1003         opbase->rom = opbase->ram = lisa_rom_ptr + (mapped_address & 0x003fff) - address;
1004         opbase->mem_min = (address & 0xffc000);
1005         opbase->mem_max = (address & 0xffc000) + 0x003fff;
10061006         /*logerror("ROM\n");*/
10071007         break;
10081008      }
src/mess/machine/c64.c
r2627r2628
692692      memory_set_bankptr (8, c64_memory + 0xe000);
693693   }
694694   /* make sure the opbase function gets called each time */
695   opcode_memory_max = 0xcfff;
695   /* NPW 15-May-2008 - Another hack in the C64 drivers broken! */
696   /* opbase->mem_max = 0xcfff; */
696697   game = c64_game;
697698   exrom = c64_exrom;
698699   old = data;
r2627r2628
969970static OPBASE_HANDLER( c64_opbase ) {
970971   if ( ( address & 0xf000 ) == 0xd000 ) {
971972      if ( c64_io_enabled ) {
972         opcode_mask = 0x0fff;
973         opcode_arg_base = c64_io_mirror;
974         opcode_base = c64_io_mirror;
975         opcode_memory_min = 0x0000;
976         opcode_memory_max = 0xcfff;
973         opbase->mask = 0x0fff;
974         opbase->ram = c64_io_mirror;
975         opbase->rom = c64_io_mirror;
976         opbase->mem_min = 0x0000;
977         opbase->mem_max = 0xcfff;
977978         c64_io_mirror[address & 0x0fff] = c64_read_io( machine, address & 0x0fff );
978979      } else {
979         opcode_mask = 0x0fff;
980         opcode_arg_base = c64_io_ram_r_ptr;
981         opcode_base = c64_io_ram_r_ptr;
982         opcode_memory_min = 0x0000;
983         opcode_memory_max = 0xcfff;
980         opbase->mask = 0x0fff;
981         opbase->ram = c64_io_ram_r_ptr;
982         opbase->rom = c64_io_ram_r_ptr;
983         opbase->mem_min = 0x0000;
984         opbase->mem_max = 0xcfff;
984985      }
985986      return ~0;
986987   }
src/mess/mess.mak
r2627r2628
8181#CPUS += I8052
8282#CPUS += I8751
8383#CPUS += I8752
84#CPUS += DS5002FP
8485CPUS += M6800
8586#CPUS += M6801
8687#CPUS += M6802
src/mess/drivers/a2600.c
r2627r2628
719719static OPBASE_HANDLER( modeSS_opbase )
720720{
721721   if ( address & 0x1000 ) {
722      opcode_mask = 0x7ff;
723      opcode_memory_min = ( address & 0xf800 );
724      opcode_memory_max = ( address & 0xf800 ) | 0x7ff;
722      opbase->mask = 0x7ff;
723      opbase->mem_min = ( address & 0xf800 );
724      opbase->mem_max = ( address & 0xf800 ) | 0x7ff;
725725      if ( address & 0x800 ) {
726         opcode_arg_base = bank_base[2];
727         opcode_base = bank_base[2];
726         opbase->ram = bank_base[2];
727         opbase->rom = bank_base[2];
728728      } else {
729         opcode_arg_base = bank_base[1];
730         opcode_base = bank_base[1];
729         opbase->ram = bank_base[1];
730         opbase->rom = bank_base[1];
731731      }
732732      return ~0;
733733   }
src/mess/drivers/abc80x.c
r2627r2628
11181118{
11191119   if (address >= 0x7800 && address < 0x8000)
11201120   {
1121      opcode_base = opcode_arg_base = memory_region(REGION_CPU1);
1121      opbase->rom = opbase->ram = memory_region(REGION_CPU1);
11221122      return ~0;
11231123   }
11241124
src/mess/drivers/a310.c
r2627r2628
297297   // if the boot ROM is mapped in, do some trickery to make it show up
298298   if (a310_latchrom)
299299   {
300      opcode_mask = 0x1fffff;
301      opcode_memory_min = 0;
302      opcode_memory_max = 0x1fffff;
303      opcode_base = opcode_arg_base = memory_region(REGION_CPU1);
300      opbase->mask = 0x1fffff;
301      opbase->mem_min = 0;
302      opbase->mem_max = 0x1fffff;
303      opbase->rom = opbase->ram = memory_region(REGION_CPU1);
304304   }
305305   else   // executing from logical memory
306306   {
307307      UINT32 page = address / page_sizes[a310_pagesize];
308308
309      opcode_mask = page_sizes[a310_pagesize]-1;
310      opcode_memory_min = page * page_sizes[a310_pagesize];
311      opcode_memory_max = opcode_memory_min + opcode_mask;
312      opcode_base = opcode_arg_base = (UINT8 *)&a310_physmem[(a310_pages[page] * page_sizes[a310_pagesize])>>2];
309      opbase->mask = page_sizes[a310_pagesize]-1;
310      opbase->mem_min = page * page_sizes[a310_pagesize];
311      opbase->mem_max = opbase->mem_min + opbase->mask;
312      opbase->rom = opbase->ram = (UINT8 *)&a310_physmem[(a310_pages[page] * page_sizes[a310_pagesize])>>2];
313313   }
314314
315315   return ~0;
src/mess/drivers/avigo.c
r2627r2628
388388/* this is needed because this driver uses handlers in memory that gets executed */
389389static OPBASE_HANDLER( avigo_opbase_handler )
390390{
391   void *opbase;
391   void *opbase_ptr;
392392
393   opbase = avigo_banked_opbase[address / 0x4000];
394   if (opbase)
393   opbase_ptr = avigo_banked_opbase[address / 0x4000];
394   if (opbase_ptr != NULL)
395395   {
396      opcode_base = opcode_arg_base = opbase;
396      opbase->rom = opbase->ram = opbase_ptr;
397397      address = ~0;
398398   }
399399   return address;
docs/messnew.txt
r2627r2628
6868
6969Source Changes:
7070----------------
71- The core is based on MAME 0.125u1.  This incorporates all features of the
72  update to this core.  [MAME team]
73
7174- Fixed performance problems with -triplebuffer.  (bug #1539) [Justin Kerk]
7275
7376
makefile
r2627r2628
4848endif
4949
5050
51
5152#-------------------------------------------------
5253# specify OS target, which further differentiates
5354# the underlying OS; supported values are:
r2627r2628
105106# uncomment next line to include the internal profiler
106107# PROFILER = 1
107108
108# uncomment next line to use DRC MIPS3 engine
109X86_MIPS3_DRC = 1
109# uncomment the force the universal DRC to always use the C backend
110# you may need to do this if your target architecture does not have
111# a native backend
112# FORCE_DRC_C_BACKEND = 1
110113
111114# uncomment next line to use DRC PowerPC engine
112115X86_PPC_DRC = 1

Previous 15994 Revisions Next


© 1998-2018 The MESS Team