Previous 15994 Revisions Next

r14021 Saturday 14th January, 2012 at 14:04:31 UTC by Miodrag Milanović
Sync with MAME r15028 (nw)
[src/emu]diexec.h distate.c distate.h emu.mak
[src/emu/cpu]cpu.mak
[src/emu/cpu/arm7]arm7.c arm7core.c arm7core.h arm7exec.c arm7help.h arm7ops.c* arm7ops.h* arm7thmb.c arm7thmb.h
[src/emu/cpu/esrip]esrip.c
[src/emu/drivers]empty.c testcpu.c*
[src/emu/machine]nvram.c nvram.h
[src/mame]mame.lst mame.mak
[src/mame/drivers]astropc.c beaminv.c capbowl.c exzisus.c itech32.c merit.c n8080.c pacman.c pcxt.c pgm2.c rainbow.c s11a.c s8.c suprnova.c toaplan1.c wpc_dot.c
[src/mame/includes]rainbow.h
[src/mame/layout]beaminv.lay*
[src/mame/video]rainbow.c tatsumi.c

src/mame/mame.lst
r14020r14021
7979ctrpllrp      // hack
8080eyes         // (c) 1982 Digitrex Techstar + "Rockola presents"
8181eyes2         // (c) 1982 Techstar + "Rockola presents"
82eyeszac         // (c) 1982 Zaccaria / bootleg
82eyeszac         // (c) 1982 Techstar / Zaccaria
83eyeszacb      // bootleg
8384birdiy         // (c) 1983 Mama Top
8485mrtnt         // (c) 1983 Telko
8586gorkans         // (c) 1984 Techstar
r14020r14021
11041105spacelnc      // (c) 1979 Nintendo
11051106sheriff         // (c) 1979 Nintendo
11061107bandido         // (c) 1980 Exidy
1108westgun2      // (c) 1980 Taito Corporation
11071109helifire      // (c) 1980 Nintendo
11081110helifirea      // (c) 1980 Nintendo
11091111radarscp      // (c) 1980 Nintendo
r14020r14021
81168118jjparads      // 1996.12 (c) 1996 Electro Design Co. (Japan)
81178119sarukani      // 1997.01 (c) 1997 Kaneko / Mediaworks (Japan)
81188120vblokbrk      // 1997.?? (c) 1997 Kaneko / Mediaworks (Asia)
8119galpanis      // 1997.04 (c) 1997 Kaneko (Japan)
8121galpanis      // 1997.04 (c) 1997 Kaneko (Europe)
8122galpanisj      // 1997.04 (c) 1997 Kaneko (Japan)
81208123galpanisk      // 1997.04 (c) 1997 Kaneko (Korea)
81218124sengekis      // 1997.?? (c) 1997 Kaneko / Warashi (Asia)
81228125sengekisj      // 1997.07 (c) 1997 Kaneko / Warashi (Japan)
r14020r14021
99759978histryma      // bootleg of Fighter's History
99769979bbprot         // unfinished prototype
99779980beaminv         // (c) 1979 Tekunon Kougyou (Arcade TV Game List - P.95, Left, 9 from top)
9978beaminva      // (c) 1979 Tekunon Kougyou (Arcade TV Game List - P.95, Left, 9 from top)
9981pacominv      // (c) 1979 Pacom Corporation
99799982mcatadv         // (c) 1993 Wintechno
99809983mcatadvj      // (c) 1993 Wintechno
99819984catt         // (c) 1993 Wintechno
src/mame/includes/rainbow.h
r14020r14021
3535};
3636
3737
38/*----------- defined in machine/rbisland.c -----------*/
38/*----------- defined in machine/rainbow.c -----------*/
3939
4040void rbisland_cchip_init(running_machine &machine, int version);
4141READ16_HANDLER( rbisland_cchip_ctrl_r );
r14020r14021
4545WRITE16_HANDLER( rbisland_cchip_ram_w );
4646
4747
48/*----------- defined in video/rbisland.c -----------*/
48/*----------- defined in video/rainbow.c -----------*/
4949
5050SCREEN_UPDATE_IND16( rainbow );
5151VIDEO_START( jumping );
src/mame/mame.mak
r14020r14021
19081908
19091909$(DRIVERS)/balsente.o:   $(LAYOUT)/stocker.lh
19101910
1911$(DRIVERS)/beaminv.o:   $(LAYOUT)/beaminv.lh
1912
19111913$(DRIVERS)/bfm_sc1.o:   $(LAYOUT)/bfm_sc1.lh
19121914
19131915$(DRIVERS)/bfm_sc2.o:   $(LAYOUT)/bfm_sc2.lh \
src/mame/video/rainbow.c
r14020r14021
6565/***************************************************************************
6666
6767Jumping uses different sprite controller
68than rbisland island. - values are remapped
68than Rainbow Island. - values are remapped
6969at address 0x2EA in the code. Apart from
7070physical layout, the main change is that
7171the Y settings are active low.
src/mame/video/tatsumi.c
r14020r14021
249249/********************************************************************/
250250
251251template<class _BitmapClass>
252INLINE void roundupt_drawgfxzoomrotate(
252INLINE void roundupt_drawgfxzoomrotate(tatsumi_state *state,
253253      _BitmapClass &dest_bmp, const rectangle &clip, const gfx_element *gfx,
254254      UINT32 code,UINT32 color,int flipx,int flipy,UINT32 ssx,UINT32 ssy,
255255      int scalex, int scaley, int rotate, int write_priority_only )
256256{
257   tatsumi_state *state = gfx->machine().driver_data<tatsumi_state>();
258257   rectangle myclip;
259258
260259   if (!scalex || !scaley) return;
r14020r14021
642641
643642            for (w = 0; w < x_width; w++) {
644643               if (rotate)
645                  roundupt_drawgfxzoomrotate(
644                  roundupt_drawgfxzoomrotate(state,
646645                        state->m_temp_bitmap,cliprect,machine.gfx[0],
647646                        base,
648647                        color,flip_x,flip_y,x_pos,render_y,
649648                        scale,scale,0,write_priority_only);
650649               else
651                  roundupt_drawgfxzoomrotate(
650                  roundupt_drawgfxzoomrotate(state,
652651                        bitmap,cliprect,machine.gfx[0],
653652                        base,
654653                        color,flip_x,flip_y,x_pos,render_y,
src/mame/layout/beaminv.lay
r0r14021
1<?xml version="1.0"?>
2<mamelayout version="2">
3   <element name="overlay">
4      <rect>
5         <bounds left="0" top="0" right="216" bottom="248" />
6         <color red="1" green="1" blue="1" />
7      </rect>
8      <rect>
9         <bounds left="0" top="0" right="216" bottom="26" />
10         <color red="0.160" green="0.578" blue="0.738" />
11      </rect>
12      <rect>
13         <bounds left="0" top="26" right="216" bottom="217" />
14         <color red="0.675" green="0.351" blue="0.480" />
15      </rect>
16      <rect>
17         <bounds left="0" top="217" right="216" bottom="248" />
18         <color red="0.160" green="0.578" blue="0.738" />
19      </rect>
20   </element>
21
22   <view name="Color Overlay">
23      <screen index="0">
24         <bounds left="0" top="0" right="3" bottom="4" />
25      </screen>
26      <overlay element="overlay">
27         <bounds left="0" top="0" right="3" bottom="4" />
28      </overlay>
29   </view>
30</mamelayout>
src/mame/drivers/wpc_dot.c
r14020r14021
413413ROM_END
414414
415415
416GAME(1991,   tfdmd_l3,   0,      wpc_dot,   wpc_dot,   wpc_dot,   ROT0,   "Bally",            "WPC Test Fixture: DMD (L-3)",            GAME_IS_SKELETON_MECHANICAL)
417GAME(1991,   gi_l9,      0,      wpc_dot,   wpc_dot,   wpc_dot,   ROT0,   "Bally",            "Gilligan's Island (L-9)",            GAME_IS_SKELETON_MECHANICAL)
418GAME(1991,   gi_l3,      gi_l9,   wpc_dot,   wpc_dot,   wpc_dot,   ROT0,   "Bally",            "Gilligan's Island (L-3)",            GAME_IS_SKELETON_MECHANICAL)
419GAME(1991,   gi_l4,      gi_l9,   wpc_dot,   wpc_dot,   wpc_dot,   ROT0,   "Bally",            "Gilligan's Island (L-4)",            GAME_IS_SKELETON_MECHANICAL)
420GAME(1991,   gi_l6,      gi_l9,   wpc_dot,   wpc_dot,   wpc_dot,   ROT0,   "Bally",            "Gilligan's Island (L-6)",            GAME_IS_SKELETON_MECHANICAL)
421GAME(1992,   hshot_p8,   0,      wpc_dot,   wpc_dot,   wpc_dot,   ROT0,   "Midway",            "Hot Shot Basketball (P-8)",            GAME_IS_SKELETON_MECHANICAL)
422GAME(1991,   hurr_l2,   0,      wpc_dot,   wpc_dot,   wpc_dot,   ROT0,   "Williams",            "Hurricane (L-2)",            GAME_IS_SKELETON_MECHANICAL)
423GAME(1991,   pz_f4,      0,      wpc_dot,   wpc_dot,   wpc_dot,   ROT0,   "Bally",            "Party Zone (F-4)",            GAME_IS_SKELETON_MECHANICAL)
424GAME(1991,   pz_l1,      pz_f4,   wpc_dot,   wpc_dot,   wpc_dot,   ROT0,   "Bally",            "Party Zone (L-1)",            GAME_IS_SKELETON_MECHANICAL)
425GAME(1991,   pz_l2,      pz_f4,   wpc_dot,   wpc_dot,   wpc_dot,   ROT0,   "Bally",            "Party Zone (L-2)",            GAME_IS_SKELETON_MECHANICAL)
426GAME(1991,   pz_l3,      pz_f4,   wpc_dot,   wpc_dot,   wpc_dot,   ROT0,   "Bally",            "Party Zone (L-3)",            GAME_IS_SKELETON_MECHANICAL)
427GAME(1991,   sf_l1,      0,      wpc_dot,   wpc_dot,   wpc_dot,   ROT0,   "Williams",            "Slugfest (L-1)",            GAME_IS_SKELETON_MECHANICAL)
428GAME(1991,   t2_l8,      0,      wpc_dot,   wpc_dot,   wpc_dot,   ROT0,   "Williams",            "Terminator 2: Judgment Day (L-8)",            GAME_IS_SKELETON_MECHANICAL)
429GAME(1991,   t2_l6,      t2_l8,   wpc_dot,   wpc_dot,   wpc_dot,   ROT0,   "Williams",            "Terminator 2: Judgment Day (L-6)",            GAME_IS_SKELETON_MECHANICAL)
430GAME(1991,   t2_p2f,      t2_l8,   wpc_dot,   wpc_dot,   wpc_dot,   ROT0,   "Williams",            "Terminator 2: Judgment Day (P-2F) Profanity",            GAME_IS_SKELETON_MECHANICAL)
431GAME(1991,   t2_l4,      t2_l8,   wpc_dot,   wpc_dot,   wpc_dot,   ROT0,   "Williams",            "Terminator 2: Judgment Day (L-4)",            GAME_IS_SKELETON_MECHANICAL)
432GAME(1991,   t2_l3,      t2_l8,   wpc_dot,   wpc_dot,   wpc_dot,   ROT0,   "Williams",            "Terminator 2: Judgment Day (L-3)",            GAME_IS_SKELETON_MECHANICAL)
433GAME(1991,   t2_l2,      t2_l8,   wpc_dot,   wpc_dot,   wpc_dot,   ROT0,   "Williams",            "Terminator 2: Judgment Day (L-2)",            GAME_IS_SKELETON_MECHANICAL)
416GAME(1991,   tfdmd_l3,   0,      wpc_dot,   wpc_dot,   wpc_dot,   ROT0,   "Bally",      "WPC Test Fixture: DMD (L-3)",               GAME_IS_SKELETON_MECHANICAL)
417GAME(1991,   gi_l9,      0,      wpc_dot,   wpc_dot,   wpc_dot,   ROT0,   "Bally",      "Gilligan's Island (L-9)",                  GAME_IS_SKELETON_MECHANICAL)
418GAME(1991,   gi_l3,      gi_l9,   wpc_dot,   wpc_dot,   wpc_dot,   ROT0,   "Bally",      "Gilligan's Island (L-3)",                  GAME_IS_SKELETON_MECHANICAL)
419GAME(1991,   gi_l4,      gi_l9,   wpc_dot,   wpc_dot,   wpc_dot,   ROT0,   "Bally",      "Gilligan's Island (L-4)",                  GAME_IS_SKELETON_MECHANICAL)
420GAME(1991,   gi_l6,      gi_l9,   wpc_dot,   wpc_dot,   wpc_dot,   ROT0,   "Bally",      "Gilligan's Island (L-6)",                  GAME_IS_SKELETON_MECHANICAL)
421GAME(1992,   hshot_p8,   0,      wpc_dot,   wpc_dot,   wpc_dot,   ROT0,   "Midway",      "Hot Shot Basketball (P-8)",               GAME_IS_SKELETON_MECHANICAL)
422GAME(1991,   hurr_l2,   0,      wpc_dot,   wpc_dot,   wpc_dot,   ROT0,   "Williams",      "Hurricane (L-2)",                        GAME_IS_SKELETON_MECHANICAL)
423GAME(1991,   pz_f4,      0,      wpc_dot,   wpc_dot,   wpc_dot,   ROT0,   "Bally",      "The Party Zone (F-4)",                     GAME_IS_SKELETON_MECHANICAL)
424GAME(1991,   pz_l1,      pz_f4,   wpc_dot,   wpc_dot,   wpc_dot,   ROT0,   "Bally",      "The Party Zone (L-1)",                     GAME_IS_SKELETON_MECHANICAL)
425GAME(1991,   pz_l2,      pz_f4,   wpc_dot,   wpc_dot,   wpc_dot,   ROT0,   "Bally",      "The Party Zone (L-2)",                     GAME_IS_SKELETON_MECHANICAL)
426GAME(1991,   pz_l3,      pz_f4,   wpc_dot,   wpc_dot,   wpc_dot,   ROT0,   "Bally",      "The Party Zone (L-3)",                     GAME_IS_SKELETON_MECHANICAL)
427GAME(1991,   sf_l1,      0,      wpc_dot,   wpc_dot,   wpc_dot,   ROT0,   "Williams",      "Slugfest (L-1)",                        GAME_IS_SKELETON_MECHANICAL)
428GAME(1991,   t2_l8,      0,      wpc_dot,   wpc_dot,   wpc_dot,   ROT0,   "Williams",      "Terminator 2: Judgment Day (L-8)",            GAME_IS_SKELETON_MECHANICAL)
429GAME(1991,   t2_l6,      t2_l8,   wpc_dot,   wpc_dot,   wpc_dot,   ROT0,   "Williams",      "Terminator 2: Judgment Day (L-6)",            GAME_IS_SKELETON_MECHANICAL)
430GAME(1991,   t2_p2f,      t2_l8,   wpc_dot,   wpc_dot,   wpc_dot,   ROT0,   "Williams",      "Terminator 2: Judgment Day (P-2F) Profanity",   GAME_IS_SKELETON_MECHANICAL)
431GAME(1991,   t2_l4,      t2_l8,   wpc_dot,   wpc_dot,   wpc_dot,   ROT0,   "Williams",      "Terminator 2: Judgment Day (L-4)",            GAME_IS_SKELETON_MECHANICAL)
432GAME(1991,   t2_l3,      t2_l8,   wpc_dot,   wpc_dot,   wpc_dot,   ROT0,   "Williams",      "Terminator 2: Judgment Day (L-3)",            GAME_IS_SKELETON_MECHANICAL)
433GAME(1991,   t2_l2,      t2_l8,   wpc_dot,   wpc_dot,   wpc_dot,   ROT0,   "Williams",      "Terminator 2: Judgment Day (L-2)",            GAME_IS_SKELETON_MECHANICAL)
src/mame/drivers/capbowl.c
r14020r14021
374374
375375   MCFG_MACHINE_START(capbowl)
376376   MCFG_MACHINE_RESET(capbowl)
377   MCFG_NVRAM_ADD_CUSTOM("nvram", capbowl_state, init_nvram)
377   MCFG_NVRAM_ADD_CUSTOM_DRIVER("nvram", capbowl_state, init_nvram)
378378
379379   MCFG_TICKET_DISPENSER_ADD("ticket", 100, TICKET_MOTOR_ACTIVE_HIGH, TICKET_STATUS_ACTIVE_LOW)
380380
src/mame/drivers/pcxt.c
r14020r14021
202202Pit8253
203203*********************************/
204204
205UINT8 pc_speaker_get_spk(running_machine &machine)
205// pc_speaker_get_spk, pc_speaker_set_spkrdata, and pc_speaker_set_input already exists in MESS, can the implementations be merged?
206UINT8 pcxt_speaker_get_spk(running_machine &machine)
206207{
207208   pcxt_state *state = machine.driver_data<pcxt_state>();
208209   return state->m_pc_spkrdata & state->m_pc_input;
209210}
210211
211
212void pc_speaker_set_spkrdata(running_machine &machine, UINT8 data)
212void pcxt_speaker_set_spkrdata(running_machine &machine, UINT8 data)
213213{
214214   device_t *speaker = machine.device("speaker");
215215   pcxt_state *state = machine.driver_data<pcxt_state>();
216216   state->m_pc_spkrdata = data ? 1 : 0;
217   speaker_level_w( speaker, pc_speaker_get_spk(machine) );
217   speaker_level_w( speaker, pcxt_speaker_get_spk(machine) );
218218}
219219
220
221void pc_speaker_set_input(running_machine &machine, UINT8 data)
220void pcxt_speaker_set_input(running_machine &machine, UINT8 data)
222221{
223222   device_t *speaker = machine.device("speaker");
224223   pcxt_state *state = machine.driver_data<pcxt_state>();
225224   state->m_pc_input = data ? 1 : 0;
226   speaker_level_w( speaker, pc_speaker_get_spk(machine) );
225   speaker_level_w( speaker, pcxt_speaker_get_spk(machine) );
227226}
228227
229228
230229static WRITE_LINE_DEVICE_HANDLER( ibm5150_pit8253_out2_changed )
231230{
232   pc_speaker_set_input( device->machine(), state );
231   pcxt_speaker_set_input( device->machine(), state );
233232}
234233
235234
r14020r14021
304303
305304   /* PPI controller port B*/
306305   pit8253_gate2_w(state->m_pit8253, BIT(data, 0));
307   pc_speaker_set_spkrdata( device->machine(), data & 0x02 );
306   pcxt_speaker_set_spkrdata( device->machine(), data & 0x02 );
308307   state->m_port_b_data = data;
309308// device_t *beep = device->machine().device("beep");
310309// device_t *cvsd = device->machine().device("cvsd");
src/mame/drivers/s11a.c
r14020r14021
189189   ROM_RELOAD(0x10000+0x8000, 0x8000)
190190ROM_END
191191
192GAME(1987,   f14_l1,      0,      williams_s11a,   williams_s11a,   williams_s11a,   ROT0,   "Williams",            "F14 Tomcat (L-1)",         GAME_IS_SKELETON_MECHANICAL)
193GAME(1987,   f14_p3,      f14_l1,   williams_s11a,   williams_s11a,   williams_s11a,   ROT0,   "Williams",            "F14 Tomcat (P-3)",         GAME_IS_SKELETON_MECHANICAL)
194GAME(1987,   f14_p4,      f14_l1,   williams_s11a,   williams_s11a,   williams_s11a,   ROT0,   "Williams",            "F14 Tomcat (P-4)",         GAME_IS_SKELETON_MECHANICAL)
195GAME(1987,   fire_l3,   0,      williams_s11a,   williams_s11a,   williams_s11a,   ROT0,   "Williams",            "Fire! (L-3)",            GAME_IS_SKELETON_MECHANICAL)
196GAME(1987,   milln_l3,   0,      williams_s11a,   williams_s11a,   williams_s11a,   ROT0,   "Williams",            "Millionaire (L-3)",      GAME_IS_SKELETON_MECHANICAL)
197GAME(1987,   pb_l5,      0,      williams_s11a,   williams_s11a,   williams_s11a,   ROT0,   "Williams",            "Pinbot (L-5)",            GAME_IS_SKELETON_MECHANICAL)
198GAME(1987,   pb_l2,      pb_l5,   williams_s11a,   williams_s11a,   williams_s11a,   ROT0,   "Williams",            "Pinbot (L-2)",            GAME_IS_SKELETON_MECHANICAL)
199GAME(1987,   pb_l3,      pb_l5,   williams_s11a,   williams_s11a,   williams_s11a,   ROT0,   "Williams",            "Pinbot (L-3)",            GAME_IS_SKELETON_MECHANICAL)
192GAME(1987,   f14_l1,      0,      williams_s11a,   williams_s11a,   williams_s11a,   ROT0,   "Williams",      "F14 Tomcat (L-1)",      GAME_IS_SKELETON_MECHANICAL)
193GAME(1987,   f14_p3,      f14_l1,   williams_s11a,   williams_s11a,   williams_s11a,   ROT0,   "Williams",      "F14 Tomcat (P-3)",      GAME_IS_SKELETON_MECHANICAL)
194GAME(1987,   f14_p4,      f14_l1,   williams_s11a,   williams_s11a,   williams_s11a,   ROT0,   "Williams",      "F14 Tomcat (P-4)",      GAME_IS_SKELETON_MECHANICAL)
195GAME(1987,   fire_l3,   0,      williams_s11a,   williams_s11a,   williams_s11a,   ROT0,   "Williams",      "Fire! (L-3)",         GAME_IS_SKELETON_MECHANICAL)
196GAME(1987,   milln_l3,   0,      williams_s11a,   williams_s11a,   williams_s11a,   ROT0,   "Williams",      "Millionaire (L-3)",   GAME_IS_SKELETON_MECHANICAL)
197GAME(1986,   pb_l5,      0,      williams_s11a,   williams_s11a,   williams_s11a,   ROT0,   "Williams",      "Pin-Bot (L-5)",      GAME_IS_SKELETON_MECHANICAL)
198GAME(1986,   pb_l2,      pb_l5,   williams_s11a,   williams_s11a,   williams_s11a,   ROT0,   "Williams",      "Pin-Bot (L-2)",      GAME_IS_SKELETON_MECHANICAL)
199GAME(1986,   pb_l3,      pb_l5,   williams_s11a,   williams_s11a,   williams_s11a,   ROT0,   "Williams",      "Pin-Bot (L-3)",      GAME_IS_SKELETON_MECHANICAL)
src/mame/drivers/pgm2.c
r14020r14021
8585   256*32
8686};
8787
88/* sprites aren't tile based, this is variable width 1bpp data, colour data is almost certainly in sprites b */
89/* there don't seem to be any indexes into the colour data, probably provided by the program, or the colour data references the bitmaps (reverse of PGM) */
90static const gfx_layout tiles32x8x1_layout =
91{
92   32,8,
93   RGN_FRAC(1,1),
94   1,
95   { 0 },
96   { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31 },
97   { 0*32,1*32,2*32,3*32,4*32,5*32,6*32,7*32 },
98   8*32
99};
88100
101
102
103
89104static GFXDECODE_START( pgm2 )
90105   GFXDECODE_ENTRY( "tiles", 0, tiles8x8_layout, 0, 16 )
91106   GFXDECODE_ENTRY( "bgtile", 0, tiles32x32x8_layout, 0, 16 )
92107   // not tile based
93   GFXDECODE_ENTRY( "spritesa", 0, tiles8x8_layout, 0, 16 )
94   GFXDECODE_ENTRY( "spritesb", 0, tiles8x8_layout, 0, 16 )
108   GFXDECODE_ENTRY( "spritesa", 0, tiles32x8x1_layout, 0, 16 )
109   GFXDECODE_ENTRY( "spritesb", 0, tiles32x8x1_layout, 0, 16 )
95110GFXDECODE_END
96111
97112
r14020r14021
101116   /* basic machine hardware */
102117   MCFG_CPU_ADD("maincpu", ARM7, 20000000)   // ?? unknown CPU, has internal ROM.
103118   MCFG_CPU_PROGRAM_MAP(pgm2_map)
104   MCFG_DEVICE_DISABLE()
119//   MCFG_DEVICE_DISABLE()
105120
106121   MCFG_MACHINE_START( pgm2 )
107122   MCFG_MACHINE_RESET( pgm2 )
r14020r14021
138153   ROM_LOAD16_BYTE( "ig-a.u35",  0x000000, 0x0800000, CRC(083a8315) SHA1(0dba25e132fbb12faa59ced648c27b881dc73478) )
139154   ROM_LOAD16_BYTE( "ig-a.u36",  0x000001, 0x0800000, CRC(e197221d) SHA1(5574b1e3da4b202db725be906dd868edc2fd4634) )
140155   
141   /* probably sprites - mask data and colour data like PGM1? */
142   ROM_REGION( 0x2000000, "spritesa", 0 )
143   ROM_LOAD( "ig-a.u12",  0x000000, 0x1000000, CRC(113a331c) SHA1(ee6b31bb2b052cc8799573de0d2f0a83f0ab4f6a) )
144   ROM_LOAD( "ig-a.u16",  0x000001, 0x1000000, CRC(fbf411c8) SHA1(5089b5cc9bbf6496ef1367c6255e63e9ab895117) )
156   ROM_REGION( 0x2000000, "spritesa", 0 ) // 1bpp sprite mask data
157   ROM_LOAD32_WORD( "ig-a.u12",  0x000000, 0x1000000, CRC(113a331c) SHA1(ee6b31bb2b052cc8799573de0d2f0a83f0ab4f6a) )
158   ROM_LOAD32_WORD( "ig-a.u16",  0x000002, 0x1000000, CRC(fbf411c8) SHA1(5089b5cc9bbf6496ef1367c6255e63e9ab895117) )
145159
146   ROM_REGION( 0x4000000, "spritesb", 0 )
160   ROM_REGION( 0x4000000, "spritesb", 0 ) // sprite colour data?
147161   ROM_LOAD16_BYTE( "ig-a.u18",  0x000000, 0x2000000, CRC(43501fa6) SHA1(58ccce6d393964b771fec3f5c583e3ede57482a3) )
148162   ROM_LOAD16_BYTE( "ig-a.u26",  0x000001, 0x2000000, CRC(7051d020) SHA1(3d9b24c6fda4c9699bb9f00742e0888059b623e1) )
149163
r14020r14021
162176   ROM_LOAD16_BYTE( "ig-a.u35",  0x000000, 0x0800000, CRC(083a8315) SHA1(0dba25e132fbb12faa59ced648c27b881dc73478) )
163177   ROM_LOAD16_BYTE( "ig-a.u36",  0x000001, 0x0800000, CRC(e197221d) SHA1(5574b1e3da4b202db725be906dd868edc2fd4634) )
164178   
165   /* probably sprites - mask data and colour data like PGM1? */
166   ROM_REGION( 0x2000000, "spritesa", 0 )
167   ROM_LOAD( "ig-a.u12",  0x000000, 0x1000000, CRC(113a331c) SHA1(ee6b31bb2b052cc8799573de0d2f0a83f0ab4f6a) )
168   ROM_LOAD( "ig-a.u16",  0x000001, 0x1000000, CRC(fbf411c8) SHA1(5089b5cc9bbf6496ef1367c6255e63e9ab895117) )
179   ROM_REGION( 0x2000000, "spritesa", 0 ) // 1bpp sprite mask data
180   ROM_LOAD32_WORD( "ig-a.u12",  0x000000, 0x1000000, CRC(113a331c) SHA1(ee6b31bb2b052cc8799573de0d2f0a83f0ab4f6a) )
181   ROM_LOAD32_WORD( "ig-a.u16",  0x000002, 0x1000000, CRC(fbf411c8) SHA1(5089b5cc9bbf6496ef1367c6255e63e9ab895117) )
169182
170   ROM_REGION( 0x4000000, "spritesb", 0 )
183   ROM_REGION( 0x4000000, "spritesb", 0 ) // sprite colour data?
171184   ROM_LOAD16_BYTE( "ig-a.u18",  0x000000, 0x2000000, CRC(43501fa6) SHA1(58ccce6d393964b771fec3f5c583e3ede57482a3) )
172185   ROM_LOAD16_BYTE( "ig-a.u26",  0x000001, 0x2000000, CRC(7051d020) SHA1(3d9b24c6fda4c9699bb9f00742e0888059b623e1) )
173186
r14020r14021
186199   ROM_LOAD16_BYTE( "ig-a3.u35",  0x000000, 0x0800000, CRC(2d46b1f6) SHA1(ea8c805eda6292e86a642e9633d8fee7054d10b1) )
187200   ROM_LOAD16_BYTE( "ig-a3.u36",  0x000001, 0x0800000, CRC(df710c36) SHA1(f826c3f496c4f17b46d18af1d8e02cac7b7027ac) )
188201   
189   /* probably sprites - mask data and colour data like PGM1? */
190   ROM_REGION( 0x2000000, "spritesa", 0 )
191   ROM_LOAD( "ig-a3.u12",  0x000000, 0x1000000, CRC(0bf63836) SHA1(b8e4f1951f8074b475b795bd7840c5a375b6f5ef) )
192   ROM_LOAD( "ig-a3.u16",  0x000001, 0x1000000, CRC(4a378542) SHA1(5d06a8a8796285a786ebb690c34610f923ef5570) )
202   ROM_REGION( 0x2000000, "spritesa", 0 ) // 1bpp sprite mask data
203   ROM_LOAD32_WORD( "ig-a3.u12",  0x000000, 0x1000000, CRC(0bf63836) SHA1(b8e4f1951f8074b475b795bd7840c5a375b6f5ef) )
204   ROM_LOAD32_WORD( "ig-a3.u16",  0x000002, 0x1000000, CRC(4a378542) SHA1(5d06a8a8796285a786ebb690c34610f923ef5570) )
193205
194   ROM_REGION( 0x4000000, "spritesb", 0 )
206   ROM_REGION( 0x4000000, "spritesb", 0 ) // sprite colour data?
195207   ROM_LOAD16_BYTE( "ig-a3.u18",  0x000000, 0x2000000, CRC(8d923e1f) SHA1(14371cf385dd8857017d3111cd4710f4291b1ae2) )
196208   ROM_LOAD16_BYTE( "ig-a3.u26",  0x000001, 0x2000000, CRC(5b6fbf3f) SHA1(d1f52e230b91ee6cde939d7c2b74da7fd6527e73) )
197209
r14020r14021
210222   ROM_LOAD16_BYTE( "ig-a3.u35",  0x000000, 0x0800000, CRC(2d46b1f6) SHA1(ea8c805eda6292e86a642e9633d8fee7054d10b1) )
211223   ROM_LOAD16_BYTE( "ig-a3.u36",  0x000001, 0x0800000, CRC(df710c36) SHA1(f826c3f496c4f17b46d18af1d8e02cac7b7027ac) )
212224   
213   /* probably sprites - mask data and colour data like PGM1? */
214   ROM_REGION( 0x2000000, "spritesa", 0 )
215   ROM_LOAD( "ig-a3.u12",  0x000000, 0x1000000, CRC(0bf63836) SHA1(b8e4f1951f8074b475b795bd7840c5a375b6f5ef) )
216   ROM_LOAD( "ig-a3.u16",  0x000001, 0x1000000, CRC(4a378542) SHA1(5d06a8a8796285a786ebb690c34610f923ef5570) )
225   ROM_REGION( 0x2000000, "spritesa", 0 ) // 1bpp sprite mask data
226   ROM_LOAD32_WORD( "ig-a3.u12",  0x000000, 0x1000000, CRC(0bf63836) SHA1(b8e4f1951f8074b475b795bd7840c5a375b6f5ef) )
227   ROM_LOAD32_WORD( "ig-a3.u16",  0x000002, 0x1000000, CRC(4a378542) SHA1(5d06a8a8796285a786ebb690c34610f923ef5570) )
217228
218   ROM_REGION( 0x4000000, "spritesb", 0 )
229   ROM_REGION( 0x4000000, "spritesb", 0 ) // sprite colour data?
219230   ROM_LOAD16_BYTE( "ig-a3.u18",  0x000000, 0x2000000, CRC(8d923e1f) SHA1(14371cf385dd8857017d3111cd4710f4291b1ae2) )
220231   ROM_LOAD16_BYTE( "ig-a3.u26",  0x000001, 0x2000000, CRC(5b6fbf3f) SHA1(d1f52e230b91ee6cde939d7c2b74da7fd6527e73) )
221232
r14020r14021
223234   ROM_LOAD16_WORD_SWAP( "ig-a3.u37",   0x000000, 0x2000000, CRC(45cdf422) SHA1(8005d284bcee73cff37a147fcd1c3e9f039a7203) )
224235ROM_END
225236
237static void iga_u16_decode(UINT16 *rom, int len, int ixor)
238{
239   int i;
226240
241   for (i = 1; i < len / 2; i+=2)
242   {
243      UINT16 x = ixor;
244
245      if ( (i>>1) & 0x000001) x ^= 0x0010;
246      if ( (i>>1) & 0x000002) x ^= 0x2004;
247      if ( (i>>1) & 0x000004) x ^= 0x0801;
248      if ( (i>>1) & 0x000008) x ^= 0x0300;
249      if ( (i>>1) & 0x000010) x ^= 0x0080;
250      if ( (i>>1) & 0x000020) x ^= 0x0020;
251      if ( (i>>1) & 0x000040) x ^= 0x4008;
252      if ( (i>>1) & 0x000080) x ^= 0x1002;
253      if ( (i>>1) & 0x000100) x ^= 0x0400;
254      if ( (i>>1) & 0x000200) x ^= 0x0040;
255      if ( (i>>1) & 0x000400) x ^= 0x8000;
256
257      rom[i] ^= x;
258      rom[i] = BITSWAP16(rom[i], 8,9,10,11,12,13,14,15,0,1,2,3,4,5,6,7);
259   }
260}
261
262static void iga_u12_decode(UINT16* rom, int len, int ixor)
263{
264   int i;
265
266   for (i = 0; i < len / 2; i+=2)
267   {
268      UINT16 x = ixor;
269
270      if ( (i>>1) & 0x000001) x ^= 0x9004;
271      if ( (i>>1) & 0x000002) x ^= 0x0028;
272      if ( (i>>1) & 0x000004) x ^= 0x0182;
273      if ( (i>>1) & 0x000008) x ^= 0x0010;
274      if ( (i>>1) & 0x000010) x ^= 0x2040;
275      if ( (i>>1) & 0x000020) x ^= 0x0801;
276      if ( (i>>1) & 0x000040) x ^= 0x0000;
277      if ( (i>>1) & 0x000080) x ^= 0x0000;
278      if ( (i>>1) & 0x000100) x ^= 0x4000;
279      if ( (i>>1) & 0x000200) x ^= 0x0600;
280      if ( (i>>1) & 0x000400) x ^= 0x0000;
281
282      rom[i] ^= x;
283      rom[i] = BITSWAP16(rom[i], 8,9,10,11,12,13,14,15,0,1,2,3,4,5,6,7);
284   }
285}
286
287static DRIVER_INIT( orleg2 )
288{
289   UINT16 *src = (UINT16 *)machine.region("spritesa")->base();
290
291   iga_u12_decode(src, 0x2000000, 0x4761);
292   iga_u16_decode(src, 0x2000000, 0xc79f);
293}
294
295static DRIVER_INIT( kov2nl )
296{
297   UINT16 *src = (UINT16 *)machine.region("spritesa")->base();
298
299   iga_u12_decode(src, 0x2000000, 0xa193);
300   iga_u16_decode(src, 0x2000000, 0xb780);
301}
302
303
227304/* PGM2 */
228GAME( 2007, orleg2,       0,         pgm2,    pgm2,     0,       ROT0, "IGS", "Oriental Legend 2 (V104, China)", GAME_IS_SKELETON )
229GAME( 2007, orleg2o,      orleg2,    pgm2,    pgm2,     0,       ROT0, "IGS", "Oriental Legend 2 (V103, China)", GAME_IS_SKELETON )
305GAME( 2007, orleg2,       0,         pgm2,    pgm2,     orleg2,       ROT0, "IGS", "Oriental Legend 2 (V104, China)", GAME_IS_SKELETON )
306GAME( 2007, orleg2o,      orleg2,    pgm2,    pgm2,     orleg2,       ROT0, "IGS", "Oriental Legend 2 (V103, China)", GAME_IS_SKELETON )
230307
231GAME( 2008, kov2nl,       0,         pgm2,    pgm2,     0,       ROT0, "IGS", "Knights of Valour 2 New Legend (V302, China)", GAME_IS_SKELETON )
232GAME( 2008, kov2nlo,      kov2nl,    pgm2,    pgm2,     0,       ROT0, "IGS", "Knights of Valour 2 New Legend (V301, China)", GAME_IS_SKELETON )
308GAME( 2008, kov2nl,       0,         pgm2,    pgm2,     kov2nl,       ROT0, "IGS", "Knights of Valour 2 New Legend (V302, China)", GAME_IS_SKELETON )
309GAME( 2008, kov2nlo,      kov2nl,    pgm2,    pgm2,     kov2nl,       ROT0, "IGS", "Knights of Valour 2 New Legend (V301, China)", GAME_IS_SKELETON )
src/mame/drivers/beaminv.c
r14020r14021
3333      . 2 players game : [0x1839] = 0xaa (player 1) or 0x55 (player 2)
3434  - Credits are stored at address 0x1837 (BCD coded, range 0x00-0x99)
3535
362) 'beaminva'
362) 'pacominv'
3737
3838  - Routine to handle the analog inputs at 0x04bd.
3939    Contents from 0x3400 (IN2) is compared with contents from 0x1d05 (value in RAM).
r14020r14021
5353
5454#include "emu.h"
5555#include "cpu/z80/z80.h"
56#include "beaminv.lh"
5657
5758
5859class beaminv_state : public driver_device
r14020r14021
297298INPUT_PORTS_END
298299
299300
300static INPUT_PORTS_START( beaminva )
301static INPUT_PORTS_START( pacominv )
301302   PORT_INCLUDE( beaminv )
302303
303304   PORT_MODIFY("DSW")
r14020r14021
365366ROM_END
366367
367368
368ROM_START( beaminva )
369ROM_START( pacominv )
369370   ROM_REGION( 0x10000, "maincpu", 0 )
370371   ROM_LOAD( "rom_0", 0x0000, 0x0400, CRC(67e100dd) SHA1(5f58e2ed3da14c48f7c382ee6091a59caf8e0609) )
371372   ROM_LOAD( "rom_1", 0x0400, 0x0400, CRC(442bbe98) SHA1(0e0382d4f6491629449759747019bd453a458b66) )
r14020r14021
383384 *
384385 *************************************/
385386
386GAME( 1979, beaminv,  0,       beaminv, beaminv,  0, ROT270, "Tekunon Kougyou", "Beam Invader (set 1)", GAME_NO_SOUND | GAME_SUPPORTS_SAVE )
387GAME( 1979, beaminva, beaminv, beaminv, beaminva, 0, ROT270, "Tekunon Kougyou", "Beam Invader (set 2)", GAME_NO_SOUND | GAME_SUPPORTS_SAVE ) // what's the real title ?
387GAMEL( 1979, beaminv,  0,       beaminv, beaminv,  0, ROT270, "Tekunon Kougyou",   "Beam Invader",  GAME_NO_SOUND | GAME_SUPPORTS_SAVE, layout_beaminv )
388GAMEL( 1979, pacominv, beaminv, beaminv, pacominv, 0, ROT270, "Pacom Corporation", "Pacom Invader", GAME_NO_SOUND | GAME_SUPPORTS_SAVE, layout_beaminv )
src/mame/drivers/rainbow.c
r14020r14021
176176   Green - Protection Fairy, gives you the protection fairy for about 8 seconds at the beginning
177177           of each round, or after you die. (NOT perm. like RI, code at $C528 in RIE handles this,
178178           and is distinctly different from the code at $BC8C in RI)
179   Blue - Yellow Potion, gives you perm. fast rbisland power
180   Indigo - Red Potion, gives you perm. 2X rbisland power
179   Blue - Yellow Potion, gives you perm. fast rainbow power
180   Indigo - Red Potion, gives you perm. 2X rainbow power
181181   Violet - Shoes, gives you perm. fast feet.
182182
183183   Unlike the secret rooms in Rainbow Islands, in Rainbow Islands Extra, only 2 letters of the code
r14020r14021
194194   If you enter all 10 secret rooms in Rainbow Islands Extra, and do NOT collect the powerups in
195195   _any_ of the rooms, you will receive "SPECIAL BIG BONUS !! 50000000 PTS."
196196
197   If you enter all 10 secret rooms, and do NOT collect the bubble-rbisland power in secret room 10,
197   If you enter all 10 secret rooms, and do NOT collect the bubble-rainbow power in secret room 10,
198198   (though you may collect them in others) you will receive "SPECIAL BIG BONUS !! 10000000 PTS."
199199
200   If you collect the bubble-rbisland power in secret room 10, you will receive "ALL ROOM CLEAR
200   If you collect the bubble-rainbow power in secret room 10, you will receive "ALL ROOM CLEAR
201201   5000000" bonus instead.
202202
203203   In Rainbow Islands Extra (as in Rainbow Islands), if you collect 2 of any colour 'cane'...
r14020r14021
260260    L -> left
261261    R -> right
262262    J -> jump
263    B -> rbisland
263    B -> rainbow
264264    S -> start
265265
266266                  |  regular   |  extra
src/mame/drivers/s8.c
r14020r14021
7171   ROM_RELOAD(0x8000, 0x4000)
7272ROM_END
7373
74GAME(1984,   pfevr_l2,      0,         williams_s8,   williams_s8,   williams_s8,   ROT0,   "Williams",      "Pennant Fever Baseball (L-2)",   GAME_IS_SKELETON_MECHANICAL)
75GAME(1984,   pfevr_p3,      pfevr_l2,   williams_s8,   williams_s8,   williams_s8,   ROT0,   "Williams",      "Pennant Fever Baseball (P-3)",   GAME_IS_SKELETON_MECHANICAL)
74GAME(1984,   pfevr_l2,      0,         williams_s8,   williams_s8,   williams_s8,   ROT0,   "Williams",      "Pennant Fever (L-2)",   GAME_IS_SKELETON_MECHANICAL)
75GAME(1984,   pfevr_p3,      pfevr_l2,   williams_s8,   williams_s8,   williams_s8,   ROT0,   "Williams",      "Pennant Fever (P-3)",   GAME_IS_SKELETON_MECHANICAL)
src/mame/drivers/suprnova.c
r14020r14021
11411141
11421142ROM_START( galpanis )
11431143   ROM_REGION( 0x080000, "maincpu", 0 ) /* SH-2 Code */
1144   ROM_LOAD       ( "sknse1.u10", 0x000000, 0x080000, CRC(e2b9d7d1) SHA1(b530a3bb9dedc8cfafcba9f1f10277590be04a15) ) /* Europe BIOS */
1145
1146   ROM_REGION32_BE( 0x200000, "user1", 0 ) /* SH-2 Code mapped at 0x04000000 */
1147   ROM_LOAD16_BYTE( "gps-000-e1.u10", 0x000000, 0x100000, CRC(b9ea3c44) SHA1(c1913545cd71ee75e60ade744a2a1054f770b981) )
1148   ROM_LOAD16_BYTE( "gps-001-e1.u8",  0x000001, 0x100000, CRC(ded57bd0) SHA1(4c0122f0521829d4d83b6b1c403f7e6470f14951) )
1149
1150   ROM_REGION( 0x1000000, "gfx1", 0 )
1151   ROM_LOAD( "gps10000.u24", 0x000000, 0x400000, CRC(a1a7acf2) SHA1(52c86ae907f0c0236808c19f652955b09e90ec5a) )
1152   ROM_LOAD( "gps10100.u20", 0x400000, 0x400000, CRC(49f764b6) SHA1(9f4289858c3dac625ef623cc381a47b45aa5d8e2) )
1153   ROM_LOAD( "gps10200.u17", 0x800000, 0x400000, CRC(51980272) SHA1(6c0706d913b33995579aaf0688c4bf26d6d35a78) )
1154
1155   ROM_REGION( 0x800000, "gfx2", 0 )
1156   ROM_LOAD( "gps20000.u16", 0x000000, 0x400000, CRC(c146a09e) SHA1(5af5a7b9d9a55ec7aba3fd85a3a0211b92b1b84f) )
1157   ROM_LOAD( "gps20100.u13", 0x400000, 0x400000, CRC(9dfa2dc6) SHA1(a058c42fd76c23c0e5c8c11f5617fd29e056be7d) )
1158
1159   ROM_REGION( 0x800000, "gfx3", ROMREGION_ERASE00 ) /* Tiles Plane B */
1160   /* First 0x040000 bytes (0x03ff Tiles) are RAM Based Tiles */
1161   /* 0x040000 - 0x3fffff empty? */
1162
1163   ROM_REGION( 0x400000, "ymz", 0 ) /* Samples */
1164   ROM_LOAD( "gps30000.u4", 0x000000, 0x400000, CRC(9e4da8e3) SHA1(6506d9300a442883357003a05fd2c78d364c35bb) )
1165ROM_END
1166
1167ROM_START( galpanisj )
1168   ROM_REGION( 0x080000, "maincpu", 0 ) /* SH-2 Code */
11441169   ROM_LOAD       ( "sknsj1.u10",   0x000000, 0x080000, CRC(7e2b836c) SHA1(92c5a7a2472496028bff0e5980d41dd294f42144) ) /* Japan BIOS */
11451170
11461171   ROM_REGION32_BE( 0x200000, "user1", 0 ) /* SH-2 Code mapped at 0x04000000 */
1147   ROM_LOAD16_BYTE( "gps-e.u10", 0x000000, 0x100000, CRC(c6938c3f) SHA1(05853ee6a44a55702788a75580b04a4be45e9bcb) )
1148   ROM_LOAD16_BYTE( "gps-o.u8",  0x000001, 0x100000, CRC(e764177a) SHA1(3a1333eb1022ed1a275b9c3d44b5f4ab81618fb6) )
1172   ROM_LOAD16_BYTE( "gps-000-j1.u10", 0x000000, 0x100000, CRC(c6938c3f) SHA1(05853ee6a44a55702788a75580b04a4be45e9bcb) )
1173   ROM_LOAD16_BYTE( "gps-001-j1.u8",  0x000001, 0x100000, CRC(e764177a) SHA1(3a1333eb1022ed1a275b9c3d44b5f4ab81618fb6) )
11491174
11501175   ROM_REGION( 0x1000000, "gfx1", 0 )
11511176   ROM_LOAD( "gps10000.u24", 0x000000, 0x400000, CRC(a1a7acf2) SHA1(52c86ae907f0c0236808c19f652955b09e90ec5a) )
r14020r14021
17251750GAME( 1996, galpani4,  skns,     sknsj, cyvern,   galpani4,  ROT0,  "Kaneko", "Gals Panic 4 (Japan)", GAME_IMPERFECT_GRAPHICS )
17261751GAME( 1996, galpani4k, galpani4, sknsk, cyvern,   galpani4,  ROT0,  "Kaneko", "Gals Panic 4 (Korea)", GAME_IMPERFECT_GRAPHICS )
17271752GAME( 1996, jjparads,  skns,     sknsj, skns_1p,  jjparads,  ROT0,  "Electro Design", "Jan Jan Paradise", GAME_IMPERFECT_GRAPHICS )
1728GAME( 1997, galpanis,  skns,     sknsj, galpanis, galpanis,  ROT0,  "Kaneko", "Gals Panic S - Extra Edition (Japan)", GAME_IMPERFECT_GRAPHICS )
1753GAME( 1997, galpanis,  skns,     sknsj, galpanis, galpanis,  ROT0,  "Kaneko", "Gals Panic S - Extra Edition (Europe)", GAME_IMPERFECT_GRAPHICS )
1754GAME( 1997, galpanisj, galpanis, sknsk, galpanis, galpanis,  ROT0,  "Kaneko", "Gals Panic S - Extra Edition (Japan)", GAME_IMPERFECT_GRAPHICS )
17291755GAME( 1997, galpanisk, galpanis, sknsk, galpanis, galpanis,  ROT0,  "Kaneko", "Gals Panic S - Extra Edition (Korea)", GAME_IMPERFECT_GRAPHICS )
17301756GAME( 1997, jjparad2,  skns,     sknsj, skns_1p,  jjparad2,  ROT0,  "Electro Design", "Jan Jan Paradise 2", GAME_IMPERFECT_GRAPHICS )
17311757GAME( 1997, sengekis,  skns,     sknsa, skns,     sengekis,  ROT90, "Kaneko / Warashi", "Sengeki Striker (Asia)", GAME_IMPERFECT_GRAPHICS )
src/mame/drivers/itech32.c
r14020r14021
17091709   MCFG_CPU_PROGRAM_MAP(sound_map)
17101710
17111711   MCFG_MACHINE_RESET(itech32)
1712   MCFG_NVRAM_ADD_CUSTOM("nvram", itech32_state, nvram_init)
1712   MCFG_NVRAM_ADD_CUSTOM_DRIVER("nvram", itech32_state, nvram_init)
17131713
17141714   MCFG_TICKET_DISPENSER_ADD("ticket", 200, TICKET_MOTOR_ACTIVE_HIGH, TICKET_STATUS_ACTIVE_HIGH)
17151715
src/mame/drivers/toaplan1.c
r14020r14021
345345#include "includes/toaplan1.h"
346346#include "sound/3812intf.h"
347347
348// vfreq - 57.59 Hz
349// hfreq - ? kHz
350#define PIXEL_CLOCK         (XTAL_28MHz/4)
348351
352#define HTOTAL            (464)
353#define HBEND            (0)
354#define HBSTART            (320)
349355
356#define VTOTAL            (262)
357#define VBEND            (0)
358#define VBSTART            (256)
359
360
350361/***************************** 68000 Memory Map *****************************/
351362
352363static ADDRESS_MAP_START( rallybik_main_map, AS_PROGRAM, 16 )
r14020r14021
15261537   MCFG_VIDEO_ATTRIBUTES(VIDEO_UPDATE_BEFORE_VBLANK)
15271538
15281539   MCFG_SCREEN_ADD("screen", RASTER)
1529   MCFG_SCREEN_REFRESH_RATE(55.14)      /* verified on pcb */
1530   MCFG_SCREEN_SIZE(512, 512)   /* 512x288 active */
1540   MCFG_SCREEN_RAW_PARAMS(PIXEL_CLOCK, HTOTAL, HBEND, HBSTART, VTOTAL, VBEND, VBSTART)
15311541   MCFG_SCREEN_VISIBLE_AREA(0, 319, 0, 239)
15321542   MCFG_SCREEN_UPDATE_STATIC(rallybik)
15331543   MCFG_SCREEN_VBLANK_STATIC(rallybik)
r14020r14021
15651575   MCFG_VIDEO_ATTRIBUTES(VIDEO_UPDATE_BEFORE_VBLANK)
15661576
15671577   MCFG_SCREEN_ADD("screen", RASTER)
1568   MCFG_SCREEN_REFRESH_RATE(57.59)      /* verified on pcb */
1569   MCFG_SCREEN_SIZE(512, 512)   /* 512x320 active */
1578   MCFG_SCREEN_RAW_PARAMS(PIXEL_CLOCK, HTOTAL, HBEND, HBSTART, VTOTAL, VBEND, VBSTART)
15701579   MCFG_SCREEN_VISIBLE_AREA(0, 319, 0, 239)
15711580   MCFG_SCREEN_UPDATE_STATIC(toaplan1)
15721581   MCFG_SCREEN_VBLANK_STATIC(toaplan1)
r14020r14021
16041613   MCFG_VIDEO_ATTRIBUTES(VIDEO_UPDATE_BEFORE_VBLANK)
16051614
16061615   MCFG_SCREEN_ADD("screen", RASTER)
1607   MCFG_SCREEN_REFRESH_RATE(60)
1608   MCFG_SCREEN_SIZE(512, 512)   /* 512x240 active */
1616   MCFG_SCREEN_RAW_PARAMS(PIXEL_CLOCK, HTOTAL, HBEND, HBSTART, VTOTAL, VBEND, VBSTART)
16091617   MCFG_SCREEN_VISIBLE_AREA(0, 319, 16, 255)
16101618   MCFG_SCREEN_UPDATE_STATIC(toaplan1)
16111619   MCFG_SCREEN_VBLANK_STATIC(toaplan1)
r14020r14021
16431651   MCFG_VIDEO_ATTRIBUTES(VIDEO_UPDATE_BEFORE_VBLANK)
16441652
16451653   MCFG_SCREEN_ADD("screen", RASTER)
1646   MCFG_SCREEN_REFRESH_RATE( (XTAL_28MHz / 4) / (450 * 282) )   /* fixed by SUZ */
1647   MCFG_SCREEN_SIZE(512, 512)   /* 512x240 mostly active, 512x512 actually used */
1654   MCFG_SCREEN_RAW_PARAMS(PIXEL_CLOCK, HTOTAL, HBEND, HBSTART, VTOTAL, VBEND, VBSTART)
16481655   MCFG_SCREEN_VISIBLE_AREA(0, 319, 16, 255)
16491656   MCFG_SCREEN_UPDATE_STATIC(toaplan1)
16501657   MCFG_SCREEN_VBLANK_STATIC(toaplan1)
r14020r14021
16861693   MCFG_VIDEO_ATTRIBUTES(VIDEO_UPDATE_BEFORE_VBLANK)
16871694
16881695   MCFG_SCREEN_ADD("screen", RASTER)
1689   MCFG_SCREEN_REFRESH_RATE(55.14)      /* verified on pcb */
1690   MCFG_SCREEN_SIZE(512, 512)   /* 512x240 active */
1696   MCFG_SCREEN_RAW_PARAMS(PIXEL_CLOCK, HTOTAL, HBEND, HBSTART, VTOTAL, VBEND, VBSTART)
16911697   MCFG_SCREEN_VISIBLE_AREA(0, 319, 16, 255)
16921698   MCFG_SCREEN_UPDATE_STATIC(toaplan1)
16931699   MCFG_SCREEN_VBLANK_STATIC(toaplan1)
r14020r14021
17231729   MCFG_VIDEO_ATTRIBUTES(VIDEO_UPDATE_BEFORE_VBLANK)
17241730
17251731   MCFG_SCREEN_ADD("screen", RASTER)
1726   MCFG_SCREEN_REFRESH_RATE(57.59)      /* verified on pcb */
1727   MCFG_SCREEN_SIZE(512, 512)   /* 512x320 active */
1732   MCFG_SCREEN_RAW_PARAMS(PIXEL_CLOCK, HTOTAL, HBEND, HBSTART, VTOTAL, VBEND, VBSTART)
17281733   MCFG_SCREEN_VISIBLE_AREA(0, 319, 0, 239)
17291734   MCFG_SCREEN_UPDATE_STATIC(toaplan1)
17301735   MCFG_SCREEN_VBLANK_STATIC(samesame)
r14020r14021
17621767   MCFG_VIDEO_ATTRIBUTES(VIDEO_UPDATE_BEFORE_VBLANK)
17631768
17641769   MCFG_SCREEN_ADD("screen", RASTER)
1765   MCFG_SCREEN_REFRESH_RATE(60)
1766   MCFG_SCREEN_SIZE(512, 512)   /* 512x256 active */
1770   MCFG_SCREEN_RAW_PARAMS(PIXEL_CLOCK, HTOTAL, HBEND, HBSTART, VTOTAL, VBEND, VBSTART)
17671771   MCFG_SCREEN_VISIBLE_AREA(0, 319, 0, 239)
17681772   MCFG_SCREEN_UPDATE_STATIC(toaplan1)
17691773   MCFG_SCREEN_VBLANK_STATIC(toaplan1)
r14020r14021
17991803   MCFG_VIDEO_ATTRIBUTES(VIDEO_UPDATE_BEFORE_VBLANK)
18001804
18011805   MCFG_SCREEN_ADD("screen", RASTER)
1802   MCFG_SCREEN_REFRESH_RATE(57.59)      /* verified on pcb */
1803   MCFG_SCREEN_SIZE(512, 512)   /* 512x256 active */
1806   MCFG_SCREEN_RAW_PARAMS(PIXEL_CLOCK, HTOTAL, HBEND, HBSTART, VTOTAL, VBEND, VBSTART)
18041807   MCFG_SCREEN_VISIBLE_AREA(0, 319, 0, 239)
18051808   MCFG_SCREEN_UPDATE_STATIC(toaplan1)
18061809   MCFG_SCREEN_VBLANK_STATIC(toaplan1)
src/mame/drivers/n8080.c
r14020r14021
55    - Space Fever
66    - Space Fever High Splitter
77    - Space Launcher
8    - Sheriff / Bandido
8    - Sheriff / Bandido / Western Gun 2
99    - Helifire
1010
1111***************************************************************************/
r14020r14021
315315INPUT_PORTS_END
316316
317317
318static INPUT_PORTS_START( westgun2 )
319   PORT_START("IN0")
320   PORT_BIT( 0x01, IP_ACTIVE_HIGH, IPT_JOYSTICKLEFT_RIGHT )
321   PORT_BIT( 0x02, IP_ACTIVE_HIGH, IPT_JOYSTICKLEFT_LEFT )
322   PORT_BIT( 0x04, IP_ACTIVE_HIGH, IPT_JOYSTICKLEFT_UP )
323   PORT_BIT( 0x08, IP_ACTIVE_HIGH, IPT_JOYSTICKLEFT_DOWN )
324   PORT_BIT( 0x10, IP_ACTIVE_HIGH, IPT_JOYSTICKRIGHT_RIGHT )
325   PORT_BIT( 0x20, IP_ACTIVE_HIGH, IPT_JOYSTICKRIGHT_LEFT )
326   PORT_BIT( 0x40, IP_ACTIVE_HIGH, IPT_JOYSTICKRIGHT_UP )
327   PORT_BIT( 0x80, IP_ACTIVE_HIGH, IPT_JOYSTICKRIGHT_DOWN )
328
329   PORT_START("IN1")
330   PORT_BIT( 0x01, IP_ACTIVE_HIGH, IPT_JOYSTICKLEFT_RIGHT ) PORT_COCKTAIL
331   PORT_BIT( 0x02, IP_ACTIVE_HIGH, IPT_JOYSTICKLEFT_LEFT ) PORT_COCKTAIL
332   PORT_BIT( 0x04, IP_ACTIVE_HIGH, IPT_JOYSTICKLEFT_UP ) PORT_COCKTAIL
333   PORT_BIT( 0x08, IP_ACTIVE_HIGH, IPT_JOYSTICKLEFT_DOWN ) PORT_COCKTAIL
334   PORT_BIT( 0x10, IP_ACTIVE_HIGH, IPT_JOYSTICKRIGHT_RIGHT ) PORT_COCKTAIL
335   PORT_BIT( 0x20, IP_ACTIVE_HIGH, IPT_JOYSTICKRIGHT_LEFT ) PORT_COCKTAIL
336   PORT_BIT( 0x40, IP_ACTIVE_HIGH, IPT_JOYSTICKRIGHT_UP ) PORT_COCKTAIL
337   PORT_BIT( 0x80, IP_ACTIVE_HIGH, IPT_JOYSTICKRIGHT_DOWN ) PORT_COCKTAIL
338
339   PORT_START("IN2")
340   PORT_BIT( 0x01, IP_ACTIVE_HIGH, IPT_BUTTON1 )
341   PORT_BIT( 0x02, IP_ACTIVE_HIGH, IPT_BUTTON1 ) PORT_COCKTAIL
342   PORT_BIT( 0x04, IP_ACTIVE_HIGH, IPT_START1 )
343   PORT_BIT( 0x08, IP_ACTIVE_HIGH, IPT_START2 )
344   PORT_DIPNAME( 0x10, 0x00, DEF_STR( Lives ))
345   PORT_DIPSETTING(    0x00, "3" )
346   PORT_DIPSETTING(    0x10, "4" )
347   PORT_DIPNAME( 0x20, 0x20, DEF_STR( Cabinet ))
348   PORT_DIPSETTING(    0x20, DEF_STR( Upright ))
349   PORT_DIPSETTING(    0x00, DEF_STR( Cocktail ))
350   PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_UNUSED ) /* enables diagnostic ROM at $2400 */
351   PORT_BIT( 0x80, IP_ACTIVE_HIGH, IPT_COIN1 )
352
353   PORT_START("IN3")
354
355INPUT_PORTS_END
356
357
318358static INPUT_PORTS_START( helifire )
319359   PORT_START("IN0")
320360   PORT_BIT( 0x01, IP_ACTIVE_HIGH, IPT_JOYSTICK_RIGHT )
r14020r14021
566606MACHINE_CONFIG_END
567607
568608
609static MACHINE_CONFIG_DERIVED( westgun2, sheriff )
610
611   /* basic machine hardware */
612   MCFG_CPU_REPLACE("maincpu", I8080, XTAL_19_968MHz / 10)
613   MCFG_CPU_CONFIG(n8080_cpu_config)
614   MCFG_CPU_PROGRAM_MAP(main_cpu_map)
615   MCFG_CPU_IO_MAP(main_io_map)
616
617MACHINE_CONFIG_END
618
619
569620static MACHINE_CONFIG_START( helifire, n8080_state )
570621
571622   /* basic machine hardware */
r14020r14021
873924   ROM_LOAD( "82s137.3l", 0x0000, 0x0400, CRC(820f8cdd) SHA1(197eeb008c140558e7c1ab2b2bd0f6a27096877c) )
874925ROM_END
875926
927ROM_START( westgun2 )
928   ROM_REGION( 0x8000, "maincpu", 0 )
929   ROM_LOAD( "rf01.ic36",    0x0000, 0x0800, CRC(7eabc538) SHA1(8f0540a5cb391b83aafc0365b44686628eae5c77) )
930   ROM_LOAD( "rf02.ic35",    0x0800, 0x0800, CRC(3344d6a5) SHA1(ea2a8413401b53c9d1b9c653ac3a98855a35cce6) )
931   ROM_LOAD( "rf03.ic34",    0x1000, 0x0800, CRC(d4bb08fd) SHA1(92c0821f259037b193658997289b6b41c6f67215) )
932   ROM_LOAD( "rf04.ic33",    0x1800, 0x0800, CRC(60b71f0d) SHA1(10650426972afb0ccb964548a52879ed3f0b316a) )
933   ROM_LOAD( "rf05.ic32",    0x2000, 0x0800, CRC(81e650fb) SHA1(e600567125294d1411fcad3a015edb98cee36ff8) )
934
935   ROM_REGION( 0x0800, "audiocpu", 0 )
936   ROM_LOAD( "rf06.ic35",   0x0000, 0x0800, CRC(4eafe957) SHA1(78e03402219c0ad814f63ae507eadc636d95f755) )
937
938   ROM_REGION( 0x0400, "proms", 0 )
939   ROM_LOAD( "82s137.3l", 0x0000, 0x0400, CRC(820f8cdd) SHA1(197eeb008c140558e7c1ab2b2bd0f6a27096877c) ) // rf07 not dumped, taken from parent
940ROM_END
941
876942ROM_START( helifire )
877943   ROM_REGION( 0x8000, "maincpu", 0 )
878944   ROM_LOAD( "hf.f1",    0x0000, 0x0400, CRC(032f89ca) SHA1(63b0310875ed78a6385e44eea781ddcc4a63557c) )
r14020r14021
917983GAME( 1979, spacelnc,   0,        spacefev, spacelnc, 0, ROT270, "Nintendo", "Space Launcher", GAME_SUPPORTS_SAVE )
918984GAME( 1979, sheriff,    0,        sheriff,  sheriff,  0, ROT270, "Nintendo", "Sheriff", GAME_SUPPORTS_SAVE )
919985GAME( 1980, bandido,    sheriff,  sheriff,  bandido,  0, ROT270, "Nintendo (Exidy license)", "Bandido", GAME_SUPPORTS_SAVE )
986GAME( 1980, westgun2,   sheriff,  westgun2, westgun2, 0, ROT270, "Nintendo (Taito Corporation license)", "Western Gun Part II", GAME_SUPPORTS_SAVE ) // official Taito PCBs, but title/copyright not shown
920987GAME( 1980, helifire,   0,        helifire, helifire, 0, ROT270, "Nintendo", "HeliFire (set 1)", GAME_IMPERFECT_SOUND | GAME_IMPERFECT_GRAPHICS | GAME_NO_COCKTAIL | GAME_SUPPORTS_SAVE )
921988GAME( 1980, helifirea,  helifire, helifire, helifire, 0, ROT270, "Nintendo", "HeliFire (set 2)", GAME_IMPERFECT_SOUND | GAME_IMPERFECT_GRAPHICS | GAME_NO_COCKTAIL | GAME_SUPPORTS_SAVE )
src/mame/drivers/exzisus.c
r14020r14021
447447
448448GAME( 1987, exzisus,  0,       exzisus, exzisus, 0, ROT0, "Taito Corporation", "Exzisus (Japan, dedicated)",  0 )
449449GAME( 1987, exzisusa, exzisus, exzisus, exzisus, 0, ROT0, "Taito Corporation", "Exzisus (Japan, conversion)", 0 )
450GAME( 1987, exzisust, exzisus, exzisus, exzisus, 0, ROT0, "Taito Corporation", "Exzisus (TAD License)", 0 )
450GAME( 1987, exzisust, exzisus, exzisus, exzisus, 0, ROT0, "Taito Corporation (TAD license)", "Exzisus (TAD license)", 0 )
src/mame/drivers/merit.c
r14020r14021
12661266   MCFG_CPU_MODIFY("maincpu")
12671267   MCFG_CPU_PROGRAM_MAP(dodge_map)
12681268
1269   MCFG_NVRAM_REPLACE_CUSTOM("nvram", merit_state, dodge_nvram_init)
1269   MCFG_NVRAM_REPLACE_CUSTOM_DRIVER("nvram", merit_state, dodge_nvram_init)
12701270MACHINE_CONFIG_END
12711271
12721272static MACHINE_CONFIG_DERIVED( tictac, pitboss )
src/mame/drivers/astropc.c
r14020r14021
2929};
3030
3131static ADDRESS_MAP_START( astropc_map, AS_PROGRAM, 32, astropc_state )
32   AM_RANGE(0x000c0000, 0x000fffff) AM_ROM AM_REGION("bios", 0 )
3233   AM_RANGE(0xfffc0000, 0xffffffff) AM_ROM AM_REGION("bios", 0 )
3334ADDRESS_MAP_END
3435
src/mame/drivers/pacman.c
r14020r14021
45354535
45364536   ROM_REGION( 0x2000, "gfx1", 0 )
45374537   ROM_LOAD( "g38205.5d",    0x0000, 0x1000, CRC(03b1b4c7) SHA1(a90b2fbaee2888ee4f0bcdf80a069c8594ef5ea1) )  /* this one has a (c) sign */
4538   ROM_LOAD( "e5",           0x1000, 0x1000, CRC(a42b5201) SHA1(2e5cede3b6039c7bd5230de27d02aaa3f35a7b64) )
4538   ROM_LOAD( "g38206.5e",    0x1000, 0x1000, CRC(a42b5201) SHA1(2e5cede3b6039c7bd5230de27d02aaa3f35a7b64) )
45394539
45404540   ROM_REGION( 0x0120, "proms", 0 )
45414541   ROM_LOAD( "82s123.7f",    0x0000, 0x0020, CRC(2fc650bd) SHA1(8d0268dee78e47c712202b0ec4f1f51109b1f2a5) )
r14020r14021
45464546   ROM_LOAD( "82s126.3m",    0x0100, 0x0100, CRC(77245b66) SHA1(0c4d0bee858b97632411c440bea6948a74759746) )   /* timing - not used */
45474547ROM_END
45484548
4549/* It's just a decrypted version of Eyes with the copyright changes...
4550 roms marked with a comment were in the set but we're not using them
45514549
4552 This is only supported because it's by Zaccaria, a known manufacturer of original games
4553 */
45544550ROM_START( eyeszac )
45554551   ROM_REGION( 0x10000, "maincpu", 0 )
4552   ROM_LOAD( "1.7d",         0x0000, 0x1000, BAD_DUMP CRC(568851aa) SHA1(a97963556a6d77400afaafd73bcc32cb7f3a54d2) )
4553   ROM_LOAD( "2.7e",         0x1000, 0x1000, BAD_DUMP CRC(9a0dba3b) SHA1(9f66f814bc2d483488df8918d872c7d6ce1bea3d) )
4554   ROM_LOAD( "3.7f",         0x2000, 0x1000, BAD_DUMP CRC(5a12aa81) SHA1(1adb1b033066cabbd62a5d33012ed1c66b955943) )
4555   ROM_LOAD( "4.7h",         0x3000, 0x1000, BAD_DUMP CRC(b11958a1) SHA1(fa66fec80594f313f605e2b904dfe34693a1aa7d) )
4556
4557   ROM_REGION( 0x2000, "gfx1", 0 )
4558   ROM_LOAD( "5.5d",         0x0000, 0x1000, BAD_DUMP CRC(7b2c4d53) SHA1(82e1a70c5cb76519dca252cfcea2a69c3601e36f) )
4559   ROM_LOAD( "6.5e",         0x1000, 0x1000, BAD_DUMP CRC(bccb4f1a) SHA1(0abf73b78a95b7e911480d41e0136dbc635b4a34) )
4560
4561   ROM_REGION( 0x0120, "proms", 0 )
4562   ROM_LOAD( "82s123.7f",    0x0000, 0x0020, CRC(2fc650bd) SHA1(8d0268dee78e47c712202b0ec4f1f51109b1f2a5) ) // taken from parent
4563   ROM_LOAD( "82s129.4a",    0x0020, 0x0100, CRC(d8d78829) SHA1(19820d1651423210083a087fb70ebea73ad34951) ) // taken from parent
4564
4565   ROM_REGION( 0x0200, "namco", 0 )   /* sound PROMs */
4566   ROM_LOAD( "82s126.1m",    0x0000, 0x0100, CRC(a9cc86bf) SHA1(bbcec0570aeceb582ff8238a4bc8546a23430081) ) // taken from parent
4567   ROM_LOAD( "82s126.3m",    0x0100, 0x0100, CRC(77245b66) SHA1(0c4d0bee858b97632411c440bea6948a74759746) ) // taken from parent
4568ROM_END
4569
4570/* It's just a decrypted version of Eyes with the copyright changes...
4571 roms marked with a comment were in the set but we're not using them */
4572ROM_START( eyeszacb )
4573   ROM_REGION( 0x10000, "maincpu", 0 )
45564574/**/ROM_LOAD( "11.bin",          0x0000, 0x0800, CRC(69c1602a) SHA1(47b0935406b7ee2f414de58da1d4e81c6277a0c2) ) // "no diagnostics, bad custom??" (unused)
45574575   ROM_LOAD( "1.bin",           0x0000, 0x0800, CRC(a4a9d7a0) SHA1(f0b807d2fa347e50df52971aa7539a88f342bad6) )
45584576   ROM_LOAD( "5.bin",           0x0800, 0x0800, CRC(c32b3f73) SHA1(80d2e987f0318b984e5c7c4d0b5faa262eebeca4) )
r14020r14021
58785896GAME( 1982, pacplus,  0,        pacman,   pacman,   pacplus,  ROT90,  "Namco (Midway license)", "Pac-Man Plus", GAME_SUPPORTS_SAVE )
58795897GAME( 1982, joyman,   puckman,  pacman,   pacman,   0,        ROT90,  "hack", "Joyman", GAME_SUPPORTS_SAVE )
58805898GAME( 1982, ctrpllrp, puckman,  pacman,   pacman,   0,        ROT90,  "hack", "Caterpillar Pacman Hack", GAME_SUPPORTS_SAVE )
5881GAME( 1982, eyes,     0,        pacman,   eyes,     eyes,     ROT90,  "Digitrex Techstar (Rock-Ola license)", "Eyes (Digitrex Techstar)", GAME_SUPPORTS_SAVE )
5882GAME( 1982, eyes2,    eyes,     pacman,   eyes,     eyes,     ROT90,  "Techstar (Rock-Ola license)", "Eyes (Techstar)", GAME_SUPPORTS_SAVE )
5883GAME( 1982, eyeszac,  eyes,     pacman,   eyes,     0,        ROT90,  "bootleg (Zaccaria)", "Eyes (Zaccaria)", GAME_SUPPORTS_SAVE )
5899GAME( 1982, eyes,     0,        pacman,   eyes,     eyes,     ROT90,  "Digitrex Techstar (Rock-Ola license)", "Eyes (US set 1)", GAME_SUPPORTS_SAVE )
5900GAME( 1982, eyes2,    eyes,     pacman,   eyes,     eyes,     ROT90,  "Techstar (Rock-Ola license)", "Eyes (US set 2)", GAME_SUPPORTS_SAVE )
5901GAME( 1982, eyeszac,  eyes,     pacman,   eyes,     eyes,     ROT90,  "Techstar (Zaccaria license)", "Eyes (Italy)", GAME_SUPPORTS_SAVE | GAME_NOT_WORKING ) // bad dump
5902GAME( 1982, eyeszacb, eyes,     pacman,   eyes,     0,        ROT90,  "bootleg", "Eyes (bootleg)", GAME_SUPPORTS_SAVE ) // based on Zaccaria version
58845903GAME( 1983, birdiy,   0,        birdiy,   pacman,   0,        ROT270, "Mama Top", "Birdiy", GAME_SUPPORTS_SAVE )
58855904GAME( 1983, mrtnt,    0,        pacman,   mrtnt,    eyes,     ROT90,  "Techstar (Telko license)", "Mr. TNT", GAME_SUPPORTS_SAVE )
58865905GAME( 1983, gorkans,  mrtnt,    pacman,   mrtnt,    0,        ROT90,  "Techstar", "Gorkans", GAME_SUPPORTS_SAVE )
src/emu/cpu/cpu.mak
r14020r14021
8080OBJDIRS += $(CPUOBJ)/arm7
8181CPUOBJS += $(CPUOBJ)/arm7/arm7.o
8282CPUOBJS += $(CPUOBJ)/arm7/arm7thmb.o
83CPUOBJS += $(CPUOBJ)/arm7/arm7ops.o
8384DASMOBJS += $(CPUOBJ)/arm7/arm7dasm.o
8485endif
8586
r14020r14021
8990$(CPUOBJ)/arm7/arm7.o:   $(CPUSRC)/arm7/arm7.c \
9091                  $(CPUSRC)/arm7/arm7.h \
9192                  $(CPUSRC)/arm7/arm7help.h \
92                  $(CPUSRC)/arm7/arm7exec.c \
9393                  $(CPUSRC)/arm7/arm7thmb.c \
94                  $(CPUSRC)/arm7/arm7ops.c \
9495                  $(CPUSRC)/arm7/arm7core.c
9596
97$(CPUOBJ)/arm7/arm7ops.o:   $(CPUSRC)/arm7/arm7ops.c \
98                  $(CPUSRC)/arm7/arm7.h \
99                  $(CPUSRC)/arm7/arm7help.h \
100                  $(CPUSRC)/arm7/arm7core.h \
96101
102$(CPUOBJ)/arm7/arm7thmb.o:   $(CPUSRC)/arm7/arm7thmb.c \
103                  $(CPUSRC)/arm7/arm7.h \
104                  $(CPUSRC)/arm7/arm7help.h \
105                  $(CPUSRC)/arm7/arm7core.h \
97106
98107#-------------------------------------------------
99108# Advanced Digital Chips SE3208
src/emu/cpu/esrip/esrip.c
r14020r14021
16181618      default:   INVALID;
16191619   }
16201620
1621   cpustate->ct = res && 1;
1621   cpustate->ct = res ? 1 : 0;
16221622}
16231623
16241624
src/emu/cpu/arm7/arm7core.c
r14020r14021
3636
3737      Therefore, to use the core, you simpy include this file along with the .h file into your own cpu specific
3838      implementation, and therefore, this file shouldn't be compiled as part of your project directly.
39      Additionally, you will need to include arm7exec.c in your cpu's execute routine.
4039
4140      For better or for worse, the code itself is very much intact from it's arm 2/3/6 origins from
4241      Bryan & Phil's work. I contemplated merging it in, but thought the fact that the CPSR is
r14020r14021
7776    By Bryan McPhail (bmcphail@tendril.co.uk) and Phil Stroffolino
7877*****************************************************************************/
7978
80#define ARM7_DEBUG_CORE 0
8179
8280/* Prototypes */
8381
84// SJE: should these be inline? or are they too big to see any benefit?
8582
86static void HandleCoProcDO(arm_state *cpustate, UINT32 insn);
87static void HandleCoProcRT(arm_state *cpustate, UINT32 insn);
88static void HandleCoProcDT(arm_state *cpustate, UINT32 insn);
89static void HandleHalfWordDT(arm_state *cpustate, UINT32 insn);
90static void HandleSwap(arm_state *cpustate, UINT32 insn);
91static void HandlePSRTransfer(arm_state *cpustate, UINT32 insn);
92static void HandleALU(arm_state *cpustate, UINT32 insn);
93static void HandleMul(arm_state *cpustate, UINT32 insn);
94static void HandleUMulLong(arm_state *cpustate, UINT32 insn);
95static void HandleSMulLong(arm_state *cpustate, UINT32 insn);
96INLINE void HandleBranch(arm_state *cpustate, UINT32 insn);       // pretty short, so inline should be ok
97static void HandleMemSingle(arm_state *cpustate, UINT32 insn);
98static void HandleMemBlock(arm_state *cpustate, UINT32 insn);
99static UINT32 decodeShift(arm_state *cpustate, UINT32 insn, UINT32 *pCarry);
100INLINE void SwitchMode(arm_state *cpustate, int);
83extern UINT32 decodeShift(arm_state *cpustate, UINT32 insn, UINT32 *pCarry);
84
10185void arm7_check_irq_state(arm_state *cpustate);
10286
10387
r14020r14021
10690write32_device_func arm7_coproc_do_callback;    // holder for the co processor Data Operations Callback func.
10791read32_device_func arm7_coproc_rt_r_callback;   // holder for the co processor Register Transfer Read Callback func.
10892write32_device_func arm7_coproc_rt_w_callback;  // holder for the co processor Register Transfer Write Callback Callback func.
109// holder for the co processor Data Transfer Read & Write Callback funcs
110void (*arm7_coproc_dt_r_callback)(arm_state *cpustate, UINT32 insn, UINT32 *prn, UINT32 (*read32)(arm_state *cpustate, UINT32 addr));
111void (*arm7_coproc_dt_w_callback)(arm_state *cpustate, UINT32 insn, UINT32 *prn, void (*write32)(arm_state *cpustate, UINT32 addr, UINT32 data));
11293
11394#ifdef UNUSED_DEFINITION
11495// custom dasm callback handlers for co-processor instructions
r14020r14021
130111
131112
132113
133// I could prob. convert to macro, but Switchmode shouldn't occur that often in emulated code..
134INLINE void SwitchMode(arm_state *cpustate, int cpsr_mode_val)
135{
136    UINT32 cspr = GET_CPSR & ~MODE_FLAG;
137    SET_CPSR(cspr | cpsr_mode_val);
138}
139114
140
141/* Decodes an Op2-style shifted-register form.  If @carry@ is non-zero the
142 * shifter carry output will manifest itself as @*carry == 0@ for carry clear
143 * and @*carry != 0@ for carry set.
144
145   SJE: Rules:
146   IF RC = 256, Result = no shift.
147   LSL   0   = Result = RM, Carry = Old Contents of CPSR C Bit
148   LSL(0,31) = Result shifted, least significant bit is in carry out
149   LSL  32   = Result of 0, Carry = Bit 0 of RM
150   LSL >32   = Result of 0, Carry out 0
151   LSR   0   = LSR 32 (see below)
152   LSR  32   = Result of 0, Carry = Bit 31 of RM
153   LSR >32   = Result of 0, Carry out 0
154   ASR >=32  = ENTIRE Result = bit 31 of RM
155   ROR  32   = Result = RM, Carry = Bit 31 of RM
156   ROR >32   = Same result as ROR n-32 until amount in range of 1-32 then follow rules
157*/
158
159static UINT32 decodeShift(arm_state *cpustate, UINT32 insn, UINT32 *pCarry)
160{
161    UINT32 k  = (insn & INSN_OP2_SHIFT) >> INSN_OP2_SHIFT_SHIFT;  // Bits 11-7
162    UINT32 rm = GET_REGISTER(cpustate, insn & INSN_OP2_RM);
163    UINT32 t  = (insn & INSN_OP2_SHIFT_TYPE) >> INSN_OP2_SHIFT_TYPE_SHIFT;
164
165    if ((insn & INSN_OP2_RM) == 0xf) {
166        // "If a register is used to specify the shift amount the PC will be 12 bytes ahead." (instead of 8)
167        rm += t & 1 ? 12 : 8;
168    }
169
170    /* All shift types ending in 1 are Rk, not #k */
171    if (t & 1)
172    {
173//      LOG(("%08x:  RegShift %02x %02x\n", R15, k >> 1, GET_REGISTER(cpustate, k >> 1)));
174#if ARM7_DEBUG_CORE
175            if ((insn & 0x80) == 0x80)
176                LOG(("%08x:  RegShift ERROR (p36)\n", R15));
177#endif
178
179        // see p35 for check on this
180        //k = GET_REGISTER(cpustate, k >> 1) & 0x1f;
181
182        // Keep only the bottom 8 bits for a Register Shift
183        k = GET_REGISTER(cpustate, k >> 1) & 0xff;
184
185        if (k == 0) /* Register shift by 0 is a no-op */
186        {
187//          LOG(("%08x:  NO-OP Regshift\n", R15));
188            /* TODO this is wrong for at least ROR by reg with lower
189             *      5 bits 0 but lower 8 bits non zero */
190            if (pCarry)
191                *pCarry = GET_CPSR & C_MASK;
192            return rm;
193        }
194    }
195    /* Decode the shift type and perform the shift */
196    switch (t >> 1)
197    {
198    case 0:                     /* LSL */
199        // LSL  32   = Result of 0, Carry = Bit 0 of RM
200        // LSL >32   = Result of 0, Carry out 0
201        if (k >= 32)
202        {
203            if (pCarry)
204                *pCarry = (k == 32) ? rm & 1 : 0;
205            return 0;
206        }
207        else
208        {
209            if (pCarry)
210            {
211            // LSL      0   = Result = RM, Carry = Old Contents of CPSR C Bit
212            // LSL (0,31)   = Result shifted, least significant bit is in carry out
213            *pCarry = k ? (rm & (1 << (32 - k))) : (GET_CPSR & C_MASK);
214            }
215            return k ? LSL(rm, k) : rm;
216        }
217        break;
218
219    case 1:                         /* LSR */
220        if (k == 0 || k == 32)
221        {
222            if (pCarry)
223                *pCarry = rm & SIGN_BIT;
224            return 0;
225        }
226        else if (k > 32)
227        {
228            if (pCarry)
229                *pCarry = 0;
230            return 0;
231        }
232        else
233        {
234            if (pCarry)
235                *pCarry = (rm & (1 << (k - 1)));
236            return LSR(rm, k);
237        }
238        break;
239
240    case 2:                     /* ASR */
241        if (k == 0 || k > 32)
242            k = 32;
243
244        if (pCarry)
245            *pCarry = (rm & (1 << (k - 1)));
246        if (k >= 32)
247            return rm & SIGN_BIT ? 0xffffffffu : 0;
248        else
249        {
250            if (rm & SIGN_BIT)
251                return LSR(rm, k) | (0xffffffffu << (32 - k));
252            else
253                return LSR(rm, k);
254        }
255        break;
256
257    case 3:                     /* ROR and RRX */
258        if (k)
259        {
260            while (k > 32)
261                k -= 32;
262            if (pCarry)
263                *pCarry = rm & (1 << (k - 1));
264            return ROR(rm, k);
265        }
266        else
267        {
268            /* RRX */
269            if (pCarry)
270                *pCarry = (rm & 1);
271            return LSR(rm, 1) | ((GET_CPSR & C_MASK) << 2);
272        }
273        break;
274    }
275
276    LOG(("%08x: Decodeshift error\n", R15));
277    return 0;
278} /* decodeShift */
279
280
281static int loadInc(arm_state *cpustate, UINT32 pat, UINT32 rbv, UINT32 s, int mode)
282{
283    int i, result;
284    UINT32 data;
285
286    result = 0;
287    rbv &= ~3;
288    for (i = 0; i < 16; i++)
289    {
290        if ((pat >> i) & 1)
291        {
292            if (cpustate->pendingAbtD == 0) // "Overwriting of registers stops when the abort happens."
293            {
294            data = READ32(rbv += 4);
295            if (i == 15) {
296                if (s) /* Pull full contents from stack */
297                    SET_MODE_REGISTER(cpustate, mode, 15, data);
298                else /* Pull only address, preserve mode & status flags */
299                   if (MODE32)
300                       SET_MODE_REGISTER(cpustate, mode, 15, data);
301                    else
302                    {
303                       SET_MODE_REGISTER(cpustate, mode, 15, (GET_MODE_REGISTER(cpustate, mode, 15) & ~0x03FFFFFC) | (data & 0x03FFFFFC));
304                    }
305            } else
306                SET_MODE_REGISTER(cpustate, mode, i, data);
307         }
308            result++;
309        }
310    }
311    return result;
312}
313
314static int loadDec(arm_state *cpustate, UINT32 pat, UINT32 rbv, UINT32 s, int mode)
315{
316    int i, result;
317    UINT32 data;
318
319    result = 0;
320    rbv &= ~3;
321    for (i = 15; i >= 0; i--)
322    {
323        if ((pat >> i) & 1)
324        {
325            if (cpustate->pendingAbtD == 0) // "Overwriting of registers stops when the abort happens."
326            {
327            data = READ32(rbv -= 4);
328            if (i == 15) {
329                if (s) /* Pull full contents from stack */
330                    SET_MODE_REGISTER(cpustate, mode, 15, data);
331                else /* Pull only address, preserve mode & status flags */
332                   if (MODE32)
333                       SET_MODE_REGISTER(cpustate, mode, 15, data);
334                    else
335                    {
336                       SET_MODE_REGISTER(cpustate, mode, 15, (GET_MODE_REGISTER(cpustate, mode, 15) & ~0x03FFFFFC) | (data & 0x03FFFFFC));
337                    }
338            }
339            else
340                SET_MODE_REGISTER(cpustate, mode, i, data);
341            }
342            result++;
343        }
344    }
345    return result;
346}
347
348static int storeInc(arm_state *cpustate, UINT32 pat, UINT32 rbv, int mode)
349{
350    int i, result;
351
352    result = 0;
353    for (i = 0; i < 16; i++)
354    {
355        if ((pat >> i) & 1)
356        {
357#if ARM7_DEBUG_CORE
358            if (i == 15) /* R15 is plus 12 from address of STM */
359                LOG(("%08x: StoreInc on R15\n", R15));
360#endif
361            WRITE32(rbv += 4, GET_MODE_REGISTER(cpustate, mode, i));
362            result++;
363        }
364    }
365    return result;
366} /* storeInc */
367
368static int storeDec(arm_state *cpustate, UINT32 pat, UINT32 rbv, int mode)
369{
370    int i, result;
371
372    result = 0;
373    for (i = 15; i >= 0; i--)
374    {
375        if ((pat >> i) & 1)
376        {
377#if ARM7_DEBUG_CORE
378            if (i == 15) /* R15 is plus 12 from address of STM */
379                LOG(("%08x: StoreDec on R15\n", R15));
380#endif
381            WRITE32(rbv -= 4, GET_MODE_REGISTER(cpustate, mode, i));
382            result++;
383        }
384    }
385    return result;
386} /* storeDec */
387
388115/***************************************************************************
389116 *                            Main CPU Funcs
390117 ***************************************************************************/
r14020r14021
423150    R15 = 0;
424151}
425152
426// Execute used to be here.. moved to separate file (arm7exec.c) to be included by cpu cores separately
427
428153// CPU CHECK IRQ STATE
429154// Note: couldn't find any exact cycle counts for most of these exceptions
430155void arm7_check_irq_state(arm_state *cpustate)
r14020r14021
587312
588313    ARM7_CHECKIRQ;
589314}
590
591/***************************************************************************
592 *                            OPCODE HANDLING
593 ***************************************************************************/
594
595// Co-Processor Data Operation
596static void HandleCoProcDO(arm_state *cpustate, UINT32 insn)
597{
598    // This instruction simply instructs the co-processor to do something, no data is returned to ARM7 core
599    if (arm7_coproc_do_callback)
600        arm7_coproc_do_callback(cpustate->device, insn, 0, 0);    // simply pass entire opcode to callback - since data format is actually dependent on co-proc implementation
601    else
602        LOG(("%08x: Co-Processor Data Operation executed, but no callback defined!\n", R15));
603}
604
605// Co-Processor Register Transfer - To/From Arm to Co-Proc
606static void HandleCoProcRT(arm_state *cpustate, UINT32 insn)
607{
608
609    /* xxxx 1110 oooL nnnn dddd cccc ppp1 mmmm */
610
611    // Load (MRC) data from Co-Proc to ARM7 register
612    if (insn & 0x00100000)       // Bit 20 = Load or Store
613    {
614        if (arm7_coproc_rt_r_callback)
615        {
616            UINT32 res = arm7_coproc_rt_r_callback(cpustate->device, insn, 0);   // RT Read handler must parse opcode & return appropriate result
617            if (cpustate->pendingUnd == 0)
618           {
619              SET_REGISTER(cpustate, (insn >> 12) & 0xf, res);
620           }
621        }
622        else
623            LOG(("%08x: Co-Processor Register Transfer executed, but no RT Read callback defined!\n", R15));
624    }
625    // Store (MCR) data from ARM7 to Co-Proc register
626    else
627    {
628        if (arm7_coproc_rt_w_callback)
629            arm7_coproc_rt_w_callback(cpustate->device, insn, GET_REGISTER(cpustate, (insn >> 12) & 0xf), 0);
630        else
631            LOG(("%08x: Co-Processor Register Transfer executed, but no RT Write callback defined!\n", R15));
632    }
633}
634
635/* Data Transfer - To/From Arm to Co-Proc
636   Loading or Storing, the co-proc function is responsible to read/write from the base register supplied + offset
637   8 bit immediate value Base Offset address is << 2 to get the actual #
638
639  issues - #1 - the co-proc function, needs direct access to memory reads or writes (ie, so we must send a pointer to a func)
640         - #2 - the co-proc may adjust the base address (especially if it reads more than 1 word), so a pointer to the register must be used
641                but the old value of the register must be restored if write back is not set..
642         - #3 - when post incrementing is used, it's up to the co-proc func. to add the offset, since the transfer
643                address supplied in that case, is simply the base. I suppose this is irrelevant if write back not set
644                but if co-proc reads multiple address, it must handle the offset adjustment itself.
645*/
646// todo: test with valid instructions
647static void HandleCoProcDT(arm_state *cpustate, UINT32 insn)
648{
649    UINT32 rn = (insn >> 16) & 0xf;
650    UINT32 rnv = GET_REGISTER(cpustate, rn);    // Get Address Value stored from Rn
651    UINT32 ornv = rnv;                // Keep value of Rn
652    UINT32 off = (insn & 0xff) << 2;  // Offset is << 2 according to manual
653    UINT32 *prn = &ARM7REG(rn);       // Pointer to our register, so it can be changed in the callback
654
655    // Pointers to read32/write32 functions
656    void (*write32)(arm_state *cpustate, UINT32 addr, UINT32 data);
657    UINT32 (*read32)(arm_state *cpustate, UINT32 addr);
658    write32 = PTR_WRITE32;
659    read32 = PTR_READ32;
660
661#if ARM7_DEBUG_CORE
662    if (((insn >> 16) & 0xf) == 15 && (insn & 0x200000))
663        LOG(("%08x: Illegal use of R15 as base for write back value!\n", R15));
664#endif
665
666    // Pre-Increment base address (IF POST INCREMENT - CALL BACK FUNCTION MUST DO IT)
667    if ((insn & 0x1000000) && off)
668    {
669        // Up - Down bit
670        if (insn & 0x800000)
671            rnv += off;
672        else
673            rnv -= off;
674    }
675
676    // Load (LDC) data from ARM7 memory to Co-Proc memory
677    if (insn & 0x00100000)
678    {
679        if (arm7_coproc_dt_r_callback)
680            arm7_coproc_dt_r_callback(cpustate, insn, prn, read32);
681        else
682            LOG(("%08x: Co-Processer Data Transfer executed, but no READ callback defined!\n", R15));
683    }
684    // Store (STC) data from Co-Proc to ARM7 memory
685    else
686    {
687        if (arm7_coproc_dt_w_callback)
688            arm7_coproc_dt_w_callback(cpustate, insn, prn, write32);
689        else
690            LOG(("%08x: Co-Processer Data Transfer executed, but no WRITE callback defined!\n", R15));
691    }
692
693   if (cpustate->pendingUnd != 0) return;
694
695    // If writeback not used - ensure the original value of RN is restored in case co-proc callback changed value
696    if ((insn & 0x200000) == 0)
697        SET_REGISTER(cpustate, rn, ornv);
698}
699
700INLINE void HandleBranch(arm_state *cpustate, UINT32 insn)
701{
702    UINT32 off = (insn & INSN_BRANCH) << 2;
703
704    /* Save PC into LR if this is a branch with link */
705    if (insn & INSN_BL)
706    {
707        SET_REGISTER(cpustate, 14, R15 + 4);
708    }
709
710    /* Sign-extend the 24-bit offset in our calculations */
711    if (off & 0x2000000u)
712    {
713       if (MODE32)
714           R15 -= ((~(off | 0xfc000000u)) + 1) - 8;
715        else
716           R15 = ((R15 - (((~(off | 0xfc000000u)) + 1) - 8)) & 0x03FFFFFC) | (R15 & ~0x03FFFFFC);
717    }
718    else
719    {
720       if (MODE32)
721           R15 += off + 8;
722        else
723           R15 = ((R15 + (off + 8)) & 0x03FFFFFC) | (R15 & ~0x03FFFFFC);
724    }
725}
726
727static void HandleMemSingle(arm_state *cpustate, UINT32 insn)
728{
729    UINT32 rn, rnv, off, rd, rnv_old = 0;
730
731    /* Fetch the offset */
732    if (insn & INSN_I)
733    {
734        /* Register Shift */
735        off = decodeShift(cpustate, insn, NULL);
736    }
737    else
738    {
739        /* Immediate Value */
740        off = insn & INSN_SDT_IMM;
741    }
742
743    /* Calculate Rn, accounting for PC */
744    rn = (insn & INSN_RN) >> INSN_RN_SHIFT;
745
746    if (insn & INSN_SDT_P)
747    {
748        /* Pre-indexed addressing */
749        if (insn & INSN_SDT_U)
750        {
751           if ((MODE32) || (rn != eR15))
752               rnv = (GET_REGISTER(cpustate, rn) + off);
753            else
754               rnv = (GET_PC + off);
755        }
756        else
757        {
758           if ((MODE32) || (rn != eR15))
759               rnv = (GET_REGISTER(cpustate, rn) - off);
760         else
761               rnv = (GET_PC - off);
762        }
763
764        if (insn & INSN_SDT_W)
765        {
766            rnv_old = GET_REGISTER(cpustate, rn);
767            SET_REGISTER(cpustate, rn, rnv);
768
769    // check writeback???
770        }
771        else if (rn == eR15)
772        {
773            rnv = rnv + 8;
774        }
775    }
776    else
777    {
778        /* Post-indexed addressing */
779        if (rn == eR15)
780        {
781           if (MODE32)
782               rnv = R15 + 8;
783           else
784               rnv = GET_PC + 8;
785        }
786        else
787        {
788            rnv = GET_REGISTER(cpustate, rn);
789        }
790    }
791
792    /* Do the transfer */
793    rd = (insn & INSN_RD) >> INSN_RD_SHIFT;
794    if (insn & INSN_SDT_L)
795    {
796        /* Load */
797        if (insn & INSN_SDT_B)
798        {
799            UINT32 data = READ8(rnv);
800            if (cpustate->pendingAbtD == 0)
801            {
802                SET_REGISTER(cpustate, rd, data);
803            }
804        }
805        else
806        {
807            UINT32 data = READ32(rnv);
808            if (cpustate->pendingAbtD == 0)
809            {
810                if (rd == eR15)
811                {
812                   if (MODE32)
813                     R15 = data - 4;
814                 else
815                    R15 = (R15 & ~0x03FFFFFC) /* N Z C V I F M1 M0 */ | ((data - 4) & 0x03FFFFFC);
816               // LDR, PC takes 2S + 2N + 1I (5 total cycles)
817                    ARM7_ICOUNT -= 2;
818                }
819                else
820                {
821                    SET_REGISTER(cpustate, rd, data);
822                }
823            }
824        }
825    }
826    else
827    {
828        /* Store */
829        if (insn & INSN_SDT_B)
830        {
831#if ARM7_DEBUG_CORE
832                if (rd == eR15)
833                    LOG(("Wrote R15 in byte mode\n"));
834#endif
835
836            WRITE8(rnv, (UINT8) GET_REGISTER(cpustate, rd) & 0xffu);
837        }
838        else
839        {
840#if ARM7_DEBUG_CORE
841                if (rd == eR15)
842                    LOG(("Wrote R15 in 32bit mode\n"));
843#endif
844
845            //WRITE32(rnv, rd == eR15 ? R15 + 8 : GET_REGISTER(cpustate, rd));
846            WRITE32(rnv, rd == eR15 ? R15 + 8 + 4 : GET_REGISTER(cpustate, rd)); // manual says STR rd = PC, +12
847        }
848        // Store takes only 2 N Cycles, so add + 1
849        ARM7_ICOUNT += 1;
850    }
851
852   if (cpustate->pendingAbtD != 0)
853   {
854      if ((insn & INSN_SDT_P) && (insn & INSN_SDT_W))
855      {
856         SET_REGISTER(cpustate, rn, rnv_old);
857      }
858   }
859   else
860   {
861
862    /* Do post-indexing writeback */
863    if (!(insn & INSN_SDT_P)/* && (insn & INSN_SDT_W)*/)
864    {
865        if (insn & INSN_SDT_U)
866        {
867            /* Writeback is applied in pipeline, before value is read from mem,
868                so writeback is effectively ignored */
869            if (rd == rn) {
870                SET_REGISTER(cpustate, rn, GET_REGISTER(cpustate, rd));
871                // todo: check for offs... ?
872            }
873            else {
874
875                if ((insn & INSN_SDT_W) != 0)
876                    LOG(("%08x:  RegisterWritebackIncrement %d %d %d\n", R15, (insn & INSN_SDT_P) != 0, (insn & INSN_SDT_W) != 0, (insn & INSN_SDT_U) != 0));
877
878                SET_REGISTER(cpustate, rn, (rnv + off));
879            }
880        }
881        else
882        {
883            /* Writeback is applied in pipeline, before value is read from mem,
884                so writeback is effectively ignored */
885            if (rd == rn) {
886                SET_REGISTER(cpustate, rn, GET_REGISTER(cpustate, rd));
887            }
888            else {
889                SET_REGISTER(cpustate, rn, (rnv - off));
890
891                if ((insn & INSN_SDT_W) != 0)
892                    LOG(("%08x:  RegisterWritebackDecrement %d %d %d\n", R15, (insn & INSN_SDT_P) != 0, (insn & INSN_SDT_W) != 0, (insn & INSN_SDT_U) != 0));
893            }
894        }
895    }
896
897   }
898
899//  ARM7_CHECKIRQ
900
901} /* HandleMemSingle */
902
903static void HandleHalfWordDT(arm_state *cpustate, UINT32 insn)
904{
905    UINT32 rn, rnv, off, rd, rnv_old = 0;
906
907    // Immediate or Register Offset?
908    if (insn & 0x400000) {               // Bit 22 - 1 = immediate, 0 = register
909        // imm. value in high nibble (bits 8-11) and lo nibble (bit 0-3)
910        off = (((insn >> 8) & 0x0f) << 4) | (insn & 0x0f);
911    }
912    else {
913        // register
914        off = GET_REGISTER(cpustate, insn & 0x0f);
915    }
916
917    /* Calculate Rn, accounting for PC */
918    rn = (insn & INSN_RN) >> INSN_RN_SHIFT;
919
920    if (insn & INSN_SDT_P)
921    {
922        /* Pre-indexed addressing */
923        if (insn & INSN_SDT_U)
924        {
925            rnv = (GET_REGISTER(cpustate, rn) + off);
926        }
927        else
928        {
929            rnv = (GET_REGISTER(cpustate, rn) - off);
930        }
931
932        if (insn & INSN_SDT_W)
933        {
934            rnv_old = GET_REGISTER(cpustate, rn);
935            SET_REGISTER(cpustate, rn, rnv);
936
937        // check writeback???
938        }
939        else if (rn == eR15)
940        {
941            rnv = (rnv) + 8;
942        }
943    }
944    else
945    {
946        /* Post-indexed addressing */
947        if (rn == eR15)
948        {
949            rnv = R15 + 8;
950        }
951        else
952        {
953            rnv = GET_REGISTER(cpustate, rn);
954        }
955    }
956
957    /* Do the transfer */
958    rd = (insn & INSN_RD) >> INSN_RD_SHIFT;
959
960    /* Load */
961    if (insn & INSN_SDT_L)
962    {
963        // Signed?
964        if (insn & 0x40)
965        {
966            UINT32 newval = 0;
967
968            // Signed Half Word?
969            if (insn & 0x20) {
970                UINT16 signbyte, databyte;
971                databyte = READ16(rnv) & 0xFFFF;
972                signbyte = (databyte & 0x8000) ? 0xffff : 0;
973                newval = (UINT32)(signbyte << 16)|databyte;
974            }
975            // Signed Byte
976            else {
977                UINT8 databyte;
978                UINT32 signbyte;
979                databyte = READ8(rnv) & 0xff;
980                signbyte = (databyte & 0x80) ? 0xffffff : 0;
981                newval = (UINT32)(signbyte << 8)|databyte;
982            }
983
984         if (cpustate->pendingAbtD == 0)
985         {
986
987            // PC?
988            if (rd == eR15)
989            {
990                R15 = newval + 8;
991                // LDR(H,SH,SB) PC takes 2S + 2N + 1I (5 total cycles)
992                ARM7_ICOUNT -= 2;
993
994            }
995            else
996            {
997                SET_REGISTER(cpustate, rd, newval);
998                R15 += 4;
999            }
1000
1001            }
1002            else
1003            {
1004                R15 += 4;
1005            }
1006
1007        }
1008        // Unsigned Half Word
1009        else
1010        {
1011         UINT32 newval = READ16(rnv);
1012
1013         if (cpustate->pendingAbtD == 0)
1014         {
1015
1016            if (rd == eR15)
1017            {
1018                R15 = newval + 8;
1019                // extra cycles for LDR(H,SH,SB) PC (5 total cycles)
1020                ARM7_ICOUNT -= 2;
1021            }
1022            else
1023            {
1024                SET_REGISTER(cpustate, rd, newval);
1025                R15 += 4;
1026            }
1027
1028            }
1029            else
1030            {
1031                R15 += 4;
1032            }
1033
1034        }
1035
1036
1037    }
1038    /* Store or ARMv5+ dword insns */
1039    else
1040    {
1041       if ((insn & 0x60) == 0x40)   // LDRD
1042   {
1043      SET_REGISTER(cpustate, rd, READ32(rnv));
1044      SET_REGISTER(cpustate, rd+1, READ32(rnv+4));
1045                R15 += 4;
1046   }
1047       else if ((insn & 0x60) == 0x60)   // STRD
1048   {
1049      WRITE32(rnv, GET_REGISTER(cpustate, rd));
1050      WRITE32(rnv+4, GET_REGISTER(cpustate, rd+1));
1051                R15 += 4;
1052   }
1053   else
1054   {
1055           // WRITE16(rnv, rd == eR15 ? R15 + 8 : GET_REGISTER(cpustate, rd));
1056           WRITE16(rnv, rd == eR15 ? R15 + 8 + 4 : GET_REGISTER(cpustate, rd)); // manual says STR RD=PC, +12 of address
1057
1058// if R15 is not increased then e.g. "STRH R10, [R15,#$10]" will be executed over and over again
1059#if 0
1060           if (rn != eR15)
1061#endif
1062               R15 += 4;
1063
1064           // STRH takes 2 cycles, so we add + 1
1065           ARM7_ICOUNT += 1;
1066   }
1067    }
1068
1069   if (cpustate->pendingAbtD != 0)
1070   {
1071      if ((insn & INSN_SDT_P) && (insn & INSN_SDT_W))
1072      {
1073         SET_REGISTER(cpustate, rn, rnv_old);
1074      }
1075   }
1076   else
1077   {
1078
1079    // SJE: No idea if this writeback code works or makes sense here..
1080
1081    /* Do post-indexing writeback */
1082    if (!(insn & INSN_SDT_P)/* && (insn & INSN_SDT_W)*/)
1083    {
1084        if (insn & INSN_SDT_U)
1085        {
1086            /* Writeback is applied in pipeline, before value is read from mem,
1087                so writeback is effectively ignored */
1088            if (rd == rn) {
1089                SET_REGISTER(cpustate, rn, GET_REGISTER(cpustate, rd));
1090                // todo: check for offs... ?
1091            }
1092            else {
1093
1094                if ((insn & INSN_SDT_W) != 0)
1095                    LOG(("%08x:  RegisterWritebackIncrement %d %d %d\n", R15, (insn & INSN_SDT_P) != 0, (insn & INSN_SDT_W) != 0, (insn & INSN_SDT_U) != 0));
1096
1097                SET_REGISTER(cpustate, rn, (rnv + off));
1098            }
1099        }
1100        else
1101        {
1102            /* Writeback is applied in pipeline, before value is read from mem,
1103                so writeback is effectively ignored */
1104            if (rd == rn) {
1105                SET_REGISTER(cpustate, rn, GET_REGISTER(cpustate, rd));
1106            }
1107            else {
1108                SET_REGISTER(cpustate, rn, (rnv - off));
1109
1110                if ((insn & INSN_SDT_W) != 0)
1111                    LOG(("%08x:  RegisterWritebackDecrement %d %d %d\n", R15, (insn & INSN_SDT_P) != 0, (insn & INSN_SDT_W) != 0, (insn & INSN_SDT_U) != 0));
1112            }
1113        }
1114    }
1115
1116    }
1117
1118}
1119
1120static void HandleSwap(arm_state *cpustate, UINT32 insn)
1121{
1122    UINT32 rn, rm, rd, tmp;
1123
1124    rn = GET_REGISTER(cpustate, (insn >> 16) & 0xf);  // reg. w/read address
1125    rm = GET_REGISTER(cpustate, insn & 0xf);          // reg. w/write address
1126    rd = (insn >> 12) & 0xf;                // dest reg
1127
1128#if ARM7_DEBUG_CORE
1129    if (rn == 15 || rm == 15 || rd == 15)
1130        LOG(("%08x: Illegal use of R15 in Swap Instruction\n", R15));
1131#endif
1132
1133    // can be byte or word
1134    if (insn & 0x400000)
1135    {
1136        tmp = READ8(rn);
1137        WRITE8(rn, rm);
1138        SET_REGISTER(cpustate, rd, tmp);
1139    }
1140    else
1141    {
1142        tmp = READ32(rn);
1143        WRITE32(rn, rm);
1144        SET_REGISTER(cpustate, rd, tmp);
1145    }
1146
1147    R15 += 4;
1148    // Instruction takes 1S+2N+1I cycles - so we subtract one more..
1149    ARM7_ICOUNT -= 1;
1150}
1151
1152static void HandlePSRTransfer(arm_state *cpustate, UINT32 insn)
1153{
1154    int reg = (insn & 0x400000) ? SPSR : eCPSR; // Either CPSR or SPSR
1155    UINT32 newval, val = 0;
1156    int oldmode = GET_CPSR & MODE_FLAG;
1157
1158    // get old value of CPSR/SPSR
1159    newval = GET_REGISTER(cpustate, reg);
1160
1161    // MSR (bit 21 set) - Copy value to CPSR/SPSR
1162    if ((insn & 0x00200000))
1163    {
1164        // Immediate Value?
1165        if (insn & INSN_I) {
1166            // Value can be specified for a Right Rotate, 2x the value specified.
1167            int by = (insn & INSN_OP2_ROTATE) >> INSN_OP2_ROTATE_SHIFT;
1168            if (by)
1169                val = ROR(insn & INSN_OP2_IMM, by << 1);
1170            else
1171                val = insn & INSN_OP2_IMM;
1172        }
1173        // Value from Register
1174        else
1175        {
1176            val = GET_REGISTER(cpustate, insn & 0x0f);
1177        }
1178
1179        // apply field code bits
1180        if (reg == eCPSR)
1181        {
1182            if (oldmode != eARM7_MODE_USER)
1183            {
1184                if (insn & 0x00010000)
1185                {
1186                    newval = (newval & 0xffffff00) | (val & 0x000000ff);
1187                }
1188                if (insn & 0x00020000)
1189                {
1190                    newval = (newval & 0xffff00ff) | (val & 0x0000ff00);
1191                }
1192                if (insn & 0x00040000)
1193                {
1194                    newval = (newval & 0xff00ffff) | (val & 0x00ff0000);
1195                }
1196            }
1197
1198            // status flags can be modified regardless of mode
1199            if (insn & 0x00080000)
1200            {
1201                // TODO for non ARMv5E mask should be 0xf0000000 (ie mask Q bit)
1202                newval = (newval & 0x00ffffff) | (val & 0xf8000000);
1203            }
1204        }
1205        else    // SPSR has stricter requirements
1206        {
1207            if (((GET_CPSR & 0x1f) > 0x10) && ((GET_CPSR & 0x1f) < 0x1f))
1208            {
1209                if (insn & 0x00010000)
1210                {
1211                    newval = (newval & 0xffffff00) | (val & 0xff);
1212                }
1213                if (insn & 0x00020000)
1214                {
1215                    newval = (newval & 0xffff00ff) | (val & 0xff00);
1216                }
1217                if (insn & 0x00040000)
1218                {
1219                    newval = (newval & 0xff00ffff) | (val & 0xff0000);
1220                }
1221                if (insn & 0x00080000)
1222                {
1223                    // TODO for non ARMv5E mask should be 0xf0000000 (ie mask Q bit)
1224                    newval = (newval & 0x00ffffff) | (val & 0xf8000000);
1225                }
1226            }
1227        }
1228
1229#if 0
1230        // force valid mode
1231        newval |= 0x10;
1232#endif
1233
1234        // Update the Register
1235        if (reg == eCPSR)
1236           SET_CPSR(newval);
1237        else
1238           SET_REGISTER(cpustate, reg, newval);
1239
1240        // Switch to new mode if changed
1241        if ((newval & MODE_FLAG) != oldmode)
1242            SwitchMode(cpustate, GET_MODE);
1243
1244    }
1245    // MRS (bit 21 clear) - Copy CPSR or SPSR to specified Register
1246    else
1247    {
1248        SET_REGISTER(cpustate, (insn >> 12)& 0x0f, GET_REGISTER(cpustate, reg));
1249    }
1250}
1251
1252static void HandleALU(arm_state *cpustate, UINT32 insn)
1253{
1254    UINT32 op2, sc = 0, rd, rn, opcode;
1255    UINT32 by, rdn;
1256
1257   // Normal Data Processing : 1S
1258   // Data Processing with register specified shift : 1S + 1I
1259   // Data Processing with PC written : 2S + 1N
1260   // Data Processing with register specified shift and PC written : 2S + 1N + 1I
1261
1262    opcode = (insn & INSN_OPCODE) >> INSN_OPCODE_SHIFT;
1263
1264    rd = 0;
1265    rn = 0;
1266
1267    /* --------------*/
1268    /* Construct Op2 */
1269    /* --------------*/
1270
1271    /* Immediate constant */
1272    if (insn & INSN_I)
1273    {
1274        by = (insn & INSN_OP2_ROTATE) >> INSN_OP2_ROTATE_SHIFT;
1275        if (by)
1276        {
1277            op2 = ROR(insn & INSN_OP2_IMM, by << 1);
1278            sc = op2 & SIGN_BIT;
1279        }
1280        else
1281        {
1282            op2 = insn & INSN_OP2;      // SJE: Shouldn't this be INSN_OP2_IMM?
1283            sc = GET_CPSR & C_MASK;
1284        }
1285    }
1286    /* Op2 = Register Value */
1287    else
1288    {
1289        op2 = decodeShift(cpustate, insn, (insn & INSN_S) ? &sc : NULL);
1290
1291        // LD TODO sc will always be 0 if this applies
1292        if (!(insn & INSN_S))
1293            sc = 0;
1294
1295        // extra cycle (register specified shift)
1296        ARM7_ICOUNT -= 1;
1297    }
1298
1299    // LD TODO this comment is wrong
1300    /* Calculate Rn to account for pipelining */
1301    if ((opcode & 0xd) != 0xd) /* No Rn in MOV */
1302    {
1303        if ((rn = (insn & INSN_RN) >> INSN_RN_SHIFT) == eR15)
1304        {
1305#if ARM7_DEBUG_CORE
1306            LOG(("%08x:  Pipelined R15 (Shift %d)\n", R15, (insn & INSN_I ? 8 : insn & 0x10u ? 12 : 12)));
1307#endif
1308         if (MODE32)
1309               rn = R15 + 8;
1310            else
1311               rn = GET_PC + 8;
1312        }
1313        else
1314        {
1315            rn = GET_REGISTER(cpustate, rn);
1316        }
1317    }
1318
1319    /* Perform the operation */
1320
1321    switch (opcode)
1322    {
1323    /* Arithmetic operations */
1324    case OPCODE_SBC:
1325        rd = (rn - op2 - (GET_CPSR & C_MASK ? 0 : 1));
1326        HandleALUSubFlags(rd, rn, op2);
1327        break;
1328    case OPCODE_CMP:
1329    case OPCODE_SUB:
1330        rd = (rn - op2);
1331        HandleALUSubFlags(rd, rn, op2);
1332        break;
1333    case OPCODE_RSC:
1334        rd = (op2 - rn - (GET_CPSR & C_MASK ? 0 : 1));
1335        HandleALUSubFlags(rd, op2, rn);
1336        break;
1337    case OPCODE_RSB:
1338        rd = (op2 - rn);
1339        HandleALUSubFlags(rd, op2, rn);
1340        break;
1341    case OPCODE_ADC:
1342        rd = (rn + op2 + ((GET_CPSR & C_MASK) >> C_BIT));
1343        HandleALUAddFlags(rd, rn, op2);
1344        break;
1345    case OPCODE_CMN:
1346    case OPCODE_ADD:
1347        rd = (rn + op2);
1348        HandleALUAddFlags(rd, rn, op2);
1349        break;
1350
1351    /* Logical operations */
1352    case OPCODE_AND:
1353    case OPCODE_TST:
1354        rd = rn & op2;
1355        HandleALULogicalFlags(rd, sc);
1356        break;
1357    case OPCODE_BIC:
1358        rd = rn & ~op2;
1359        HandleALULogicalFlags(rd, sc);
1360        break;
1361    case OPCODE_TEQ:
1362    case OPCODE_EOR:
1363        rd = rn ^ op2;
1364        HandleALULogicalFlags(rd, sc);
1365        break;
1366    case OPCODE_ORR:
1367        rd = rn | op2;
1368        HandleALULogicalFlags(rd, sc);
1369        break;
1370    case OPCODE_MOV:
1371        rd = op2;
1372        HandleALULogicalFlags(rd, sc);
1373        break;
1374    case OPCODE_MVN:
1375        rd = (~op2);
1376        HandleALULogicalFlags(rd, sc);
1377        break;
1378    }
1379
1380    /* Put the result in its register if not one of the test only opcodes (TST,TEQ,CMP,CMN) */
1381    rdn = (insn & INSN_RD) >> INSN_RD_SHIFT;
1382    if ((opcode & 0xc) != 0x8)
1383    {
1384        // If Rd = R15, but S Flag not set, Result is placed in R15, but CPSR is not affected (page 44)
1385        if (rdn == eR15 && !(insn & INSN_S))
1386        {
1387         if (MODE32)
1388         {
1389               R15 = rd;
1390           }
1391           else
1392           {
1393               R15 = (R15 & ~0x03FFFFFC) | (rd & 0x03FFFFFC);
1394           }
1395            // extra cycles (PC written)
1396            ARM7_ICOUNT -= 2;
1397        }
1398        else
1399        {
1400            // Rd = 15 and S Flag IS set, Result is placed in R15, and current mode SPSR moved to CPSR
1401            if (rdn == eR15) {
1402
1403            if (MODE32)
1404            {
1405            // When Rd is R15 and the S flag is set the result of the operation is placed in R15 and the SPSR corresponding to
1406            // the current mode is moved to the CPSR. This allows state changes which automatically restore both PC and
1407            // CPSR. --> This form of instruction should not be used in User mode. <--
1408
1409            if (GET_MODE != eARM7_MODE_USER)
1410            {
1411                   // Update CPSR from SPSR
1412                   SET_CPSR(GET_REGISTER(cpustate, SPSR));
1413                   SwitchMode(cpustate, GET_MODE);
1414               }
1415
1416                R15 = rd;
1417
1418                }
1419                else
1420                {
1421                  UINT32 temp;
1422               R15 = rd; //(R15 & 0x03FFFFFC) | (rd & 0xFC000003);
1423               temp = (GET_CPSR & 0x0FFFFF20) | (rd & 0xF0000000) /* N Z C V */ | ((rd & 0x0C000000) >> (26 - 6)) /* I F */ | (rd & 0x00000003) /* M1 M0 */;
1424               SET_CPSR( temp);
1425               SwitchMode( cpustate, temp & 3);
1426                }
1427
1428                // extra cycles (PC written)
1429                ARM7_ICOUNT -= 2;
1430
1431                /* IRQ masks may have changed in this instruction */
1432//              ARM7_CHECKIRQ;
1433            }
1434            else
1435                /* S Flag is set - Write results to register & update CPSR (which was already handled using HandleALU flag macros) */
1436                SET_REGISTER(cpustate, rdn, rd);
1437        }
1438    }
1439    // SJE: Don't think this applies any more.. (see page 44 at bottom)
1440    /* TST & TEQ can affect R15 (the condition code register) with the S bit set */
1441    else if (rdn == eR15)
1442    {
1443        if (insn & INSN_S) {
1444#if ARM7_DEBUG_CORE
1445                LOG(("%08x: TST class on R15 s bit set\n", R15));
1446#endif
1447         if (MODE32)
1448               R15 = rd;
1449            else
1450            {
1451               UINT32 temp;
1452            R15 = (R15 & 0x03FFFFFC) | (rd & ~0x03FFFFFC);
1453            temp = (GET_CPSR & 0x0FFFFF20) | (rd & 0xF0000000) /* N Z C V */ | ((rd & 0x0C000000) >> (26 - 6)) /* I F */ | (rd & 0x00000003) /* M1 M0 */;
1454            SET_CPSR( temp);
1455            SwitchMode( cpustate, temp & 3);
1456         }
1457
1458            /* IRQ masks may have changed in this instruction */
1459//          ARM7_CHECKIRQ;
1460        }
1461        else
1462        {
1463#if ARM7_DEBUG_CORE
1464                LOG(("%08x: TST class on R15 no s bit set\n", R15));
1465#endif
1466        }
1467        // extra cycles (PC written)
1468        ARM7_ICOUNT -= 2;
1469    }
1470
1471    // compensate for the -3 at the end
1472    ARM7_ICOUNT += 2;
1473}
1474
1475static void HandleMul(arm_state *cpustate, UINT32 insn)
1476{
1477    UINT32 r, rm, rs;
1478
1479   // MUL takes 1S + mI and MLA 1S + (m+1)I cycles to execute, where S and I are as
1480   // defined in 6.2 Cycle Types on page 6-2.
1481   // m is the number of 8 bit multiplier array cycles required to complete the
1482   // multiply, which is controlled by the value of the multiplier operand
1483   // specified by Rs.
1484
1485   rm = GET_REGISTER(cpustate, insn & INSN_MUL_RM);
1486   rs = GET_REGISTER(cpustate, (insn & INSN_MUL_RS) >> INSN_MUL_RS_SHIFT);
1487
1488    /* Do the basic multiply of Rm and Rs */
1489    r = rm * rs;
1490
1491#if ARM7_DEBUG_CORE
1492    if ((insn & INSN_MUL_RM) == 0xf ||
1493        ((insn & INSN_MUL_RS) >> INSN_MUL_RS_SHIFT) == 0xf ||
1494        ((insn & INSN_MUL_RN) >> INSN_MUL_RN_SHIFT) == 0xf)
1495        LOG(("%08x:  R15 used in mult\n", R15));
1496#endif
1497
1498    /* Add on Rn if this is a MLA */
1499    if (insn & INSN_MUL_A)
1500    {
1501        r += GET_REGISTER(cpustate, (insn & INSN_MUL_RN) >> INSN_MUL_RN_SHIFT);
1502        // extra cycle for MLA
1503      ARM7_ICOUNT -= 1;
1504    }
1505
1506    /* Write the result */
1507    SET_REGISTER(cpustate, (insn & INSN_MUL_RD) >> INSN_MUL_RD_SHIFT, r);
1508
1509    /* Set N and Z if asked */
1510    if (insn & INSN_S)
1511    {
1512        SET_CPSR((GET_CPSR & ~(N_MASK | Z_MASK)) | HandleALUNZFlags(r));
1513    }
1514
1515   if (rs & SIGN_BIT) rs = -rs;
1516   if (rs < 0x00000100) ARM7_ICOUNT -= 1 + 1;
1517   else if (rs < 0x00010000) ARM7_ICOUNT -= 1 + 2;
1518   else if (rs < 0x01000000) ARM7_ICOUNT -= 1 + 3;
1519   else ARM7_ICOUNT -= 1 + 4;
1520
1521   ARM7_ICOUNT += 3;
1522}
1523
1524// todo: add proper cycle counts
1525static void HandleSMulLong(arm_state *cpustate, UINT32 insn)
1526{
1527    INT32 rm, rs;
1528    UINT32 rhi, rlo;
1529    INT64 res = 0;
1530
1531   // MULL takes 1S + (m+1)I and MLAL 1S + (m+2)I cycles to execute, where m is the
1532   // number of 8 bit multiplier array cycles required to complete the multiply, which is
1533   // controlled by the value of the multiplier operand specified by Rs.
1534
1535    rm  = (INT32)GET_REGISTER(cpustate, insn & 0xf);
1536    rs  = (INT32)GET_REGISTER(cpustate, ((insn >> 8) & 0xf));
1537    rhi = (insn >> 16) & 0xf;
1538    rlo = (insn >> 12) & 0xf;
1539
1540#if ARM7_DEBUG_CORE
1541        if ((insn & 0xf) == 15 || ((insn >> 8) & 0xf) == 15 || ((insn >> 16) & 0xf) == 15 || ((insn >> 12) & 0xf) == 15)
1542            LOG(("%08x: Illegal use of PC as a register in SMULL opcode\n", R15));
1543#endif
1544
1545    /* Perform the multiplication */
1546    res = (INT64)rm * rs;
1547
1548    /* Add on Rn if this is a MLA */
1549    if (insn & INSN_MUL_A)
1550    {
1551        INT64 acum = (INT64)((((INT64)(GET_REGISTER(cpustate, rhi))) << 32) | GET_REGISTER(cpustate, rlo));
1552        res += acum;
1553        // extra cycle for MLA
1554      ARM7_ICOUNT -= 1;
1555    }
1556
1557    /* Write the result (upper dword goes to RHi, lower to RLo) */
1558    SET_REGISTER(cpustate, rhi, res >> 32);
1559    SET_REGISTER(cpustate, rlo, res & 0xFFFFFFFF);
1560
1561    /* Set N and Z if asked */
1562    if (insn & INSN_S)
1563    {
1564        SET_CPSR((GET_CPSR & ~(N_MASK | Z_MASK)) | HandleLongALUNZFlags(res));
1565    }
1566
1567   if (rs < 0) rs = -rs;
1568   if (rs < 0x00000100) ARM7_ICOUNT -= 1 + 1 + 1;
1569   else if (rs < 0x00010000) ARM7_ICOUNT -= 1 + 2 + 1;
1570   else if (rs < 0x01000000) ARM7_ICOUNT -= 1 + 3 + 1;
1571   else ARM7_ICOUNT -= 1 + 4 + 1;
1572
1573   ARM7_ICOUNT += 3;
1574}
1575
1576// todo: add proper cycle counts
1577static void HandleUMulLong(arm_state *cpustate, UINT32 insn)
1578{
1579    UINT32 rm, rs;
1580    UINT32 rhi, rlo;
1581    UINT64 res = 0;
1582
1583   // MULL takes 1S + (m+1)I and MLAL 1S + (m+2)I cycles to execute, where m is the
1584   // number of 8 bit multiplier array cycles required to complete the multiply, which is
1585   // controlled by the value of the multiplier operand specified by Rs.
1586
1587    rm  = (INT32)GET_REGISTER(cpustate, insn & 0xf);
1588    rs  = (INT32)GET_REGISTER(cpustate, ((insn >> 8) & 0xf));
1589    rhi = (insn >> 16) & 0xf;
1590    rlo = (insn >> 12) & 0xf;
1591
1592#if ARM7_DEBUG_CORE
1593        if (((insn & 0xf) == 15) || (((insn >> 8) & 0xf) == 15) || (((insn >> 16) & 0xf) == 15) || (((insn >> 12) & 0xf) == 15))
1594            LOG(("%08x: Illegal use of PC as a register in SMULL opcode\n", R15));
1595#endif
1596
1597    /* Perform the multiplication */
1598    res = (UINT64)rm * rs;
1599
1600    /* Add on Rn if this is a MLA */
1601    if (insn & INSN_MUL_A)
1602    {
1603        UINT64 acum = (UINT64)((((UINT64)(GET_REGISTER(cpustate, rhi))) << 32) | GET_REGISTER(cpustate, rlo));
1604        res += acum;
1605        // extra cycle for MLA
1606      ARM7_ICOUNT -= 1;
1607    }
1608
1609    /* Write the result (upper dword goes to RHi, lower to RLo) */
1610    SET_REGISTER(cpustate, rhi, res >> 32);
1611    SET_REGISTER(cpustate, rlo, res & 0xFFFFFFFF);
1612
1613    /* Set N and Z if asked */
1614    if (insn & INSN_S)
1615    {
1616        SET_CPSR((GET_CPSR & ~(N_MASK | Z_MASK)) | HandleLongALUNZFlags(res));
1617    }
1618
1619   if (rs < 0x00000100) ARM7_ICOUNT -= 1 + 1 + 1;
1620   else if (rs < 0x00010000) ARM7_ICOUNT -= 1 + 2 + 1;
1621   else if (rs < 0x01000000) ARM7_ICOUNT -= 1 + 3 + 1;
1622   else ARM7_ICOUNT -= 1 + 4 + 1;
1623
1624   ARM7_ICOUNT += 3;
1625}
1626
1627static void HandleMemBlock(arm_state *cpustate, UINT32 insn)
1628{
1629    UINT32 rb = (insn & INSN_RN) >> INSN_RN_SHIFT;
1630    UINT32 rbp = GET_REGISTER(cpustate, rb);
1631    int result;
1632
1633#if ARM7_DEBUG_CORE
1634    if (rbp & 3)
1635        LOG(("%08x: Unaligned Mem Transfer @ %08x\n", R15, rbp));
1636#endif
1637
1638   // Normal LDM instructions take nS + 1N + 1I and LDM PC takes (n+1)S + 2N + 1I
1639   // incremental cycles, where S,N and I are as defined in 6.2 Cycle Types on page 6-2.
1640   // STM instructions take (n-1)S + 2N incremental cycles to execute, where n is the
1641   // number of words transferred.
1642
1643    if (insn & INSN_BDT_L)
1644    {
1645        /* Loading */
1646        if (insn & INSN_BDT_U)
1647        {
1648            /* Incrementing */
1649            if (!(insn & INSN_BDT_P))
1650            {
1651                rbp = rbp + (- 4);
1652            }
1653
1654            // S Flag Set, but R15 not in list = User Bank Transfer
1655            if (insn & INSN_BDT_S && (insn & 0x8000) == 0)
1656            {
1657               // !! actually switching to user mode triggers a section permission fault in Happy Fish 302-in-1 (BP C0030DF4, press F5 ~16 times) !!
1658                // set to user mode - then do the transfer, and set back
1659                //int curmode = GET_MODE;
1660                //SwitchMode(cpustate, eARM7_MODE_USER);
1661                LOG(("%08x: User Bank Transfer not fully tested - please check if working properly!\n", R15));
1662                result = loadInc(cpustate, insn & 0xffff, rbp, insn & INSN_BDT_S, eARM7_MODE_USER);
1663                // todo - not sure if Writeback occurs on User registers also..
1664                //SwitchMode(cpustate, curmode);
1665            }
1666            else
1667                result = loadInc(cpustate, insn & 0xffff, rbp, insn & INSN_BDT_S, GET_MODE);
1668
1669            if ((insn & INSN_BDT_W) && (cpustate->pendingAbtD == 0))
1670            {
1671#if ARM7_DEBUG_CORE
1672                    if (rb == 15)
1673                        LOG(("%08x:  Illegal LDRM writeback to r15\n", R15));
1674#endif
1675            // "A LDM will always overwrite the updated base if the base is in the list." (also for a user bank transfer?)
1676            // GBA "V-Rally 3" expects R0 not to be overwritten with the updated base value [BP 8077B0C]
1677            if (((insn >> rb) & 1) == 0)
1678            {
1679               SET_REGISTER(cpustate, rb, GET_REGISTER(cpustate, rb) + result * 4);
1680            }
1681            }
1682
1683            // R15 included? (NOTE: CPSR restore must occur LAST otherwise wrong registers restored!)
1684            if ((insn & 0x8000) && (cpustate->pendingAbtD == 0)) {
1685                R15 -= 4;     // SJE: I forget why i did this?
1686                // S - Flag Set? Signals transfer of current mode SPSR->CPSR
1687                if (insn & INSN_BDT_S)
1688            {
1689                   if (MODE32)
1690                   {
1691                       SET_CPSR(GET_REGISTER(cpustate, SPSR));
1692                       SwitchMode(cpustate, GET_MODE);
1693                    }
1694                    else
1695                    {
1696                       UINT32 temp;
1697//                      LOG(("LDM + S | R15 %08X CPSR %08X\n", R15, GET_CPSR));
1698                  temp = (GET_CPSR & 0x0FFFFF20) | (R15 & 0xF0000000) /* N Z C V */ | ((R15 & 0x0C000000) >> (26 - 6)) /* I F */ | (R15 & 0x00000003) /* M1 M0 */;
1699                  SET_CPSR( temp);
1700                  SwitchMode(cpustate, temp & 3);
1701                    }
1702                }
1703                // LDM PC - takes 2 extra cycles
1704                ARM7_ICOUNT -= 2;
1705            }
1706        }
1707        else
1708        {
1709            /* Decrementing */
1710            if (!(insn & INSN_BDT_P))
1711            {
1712                rbp = rbp - (- 4);
1713            }
1714
1715            // S Flag Set, but R15 not in list = User Bank Transfer
1716            if (insn & INSN_BDT_S && ((insn & 0x8000) == 0))
1717            {
1718                // set to user mode - then do the transfer, and set back
1719                //int curmode = GET_MODE;
1720                //SwitchMode(cpustate, eARM7_MODE_USER);
1721                LOG(("%08x: User Bank Transfer not fully tested - please check if working properly!\n", R15));
1722                result = loadDec(cpustate, insn & 0xffff, rbp, insn & INSN_BDT_S, eARM7_MODE_USER);
1723                // todo - not sure if Writeback occurs on User registers also..
1724                //SwitchMode(cpustate, curmode);
1725            }
1726            else
1727                result = loadDec(cpustate, insn & 0xffff, rbp, insn & INSN_BDT_S, GET_MODE);
1728
1729            if ((insn & INSN_BDT_W) && (cpustate->pendingAbtD == 0))
1730            {
1731                if (rb == 0xf)
1732                    LOG(("%08x:  Illegal LDRM writeback to r15\n", R15));
1733            // "A LDM will always overwrite the updated base if the base is in the list." (also for a user bank transfer?)
1734            if (((insn >> rb) & 1) == 0)
1735            {
1736               SET_REGISTER(cpustate, rb, GET_REGISTER(cpustate, rb) - result * 4);
1737            }
1738            }
1739
1740            // R15 included? (NOTE: CPSR restore must occur LAST otherwise wrong registers restored!)
1741            if ((insn & 0x8000) && (cpustate->pendingAbtD == 0)) {
1742                R15 -= 4;     // SJE: I forget why i did this?
1743                // S - Flag Set? Signals transfer of current mode SPSR->CPSR
1744                if (insn & INSN_BDT_S)
1745            {
1746                   if (MODE32)
1747                   {
1748                       SET_CPSR(GET_REGISTER(cpustate, SPSR));
1749                       SwitchMode(cpustate, GET_MODE);
1750                    }
1751                    else
1752                    {
1753                       UINT32 temp;
1754//                      LOG(("LDM + S | R15 %08X CPSR %08X\n", R15, GET_CPSR));
1755                  temp = (GET_CPSR & 0x0FFFFF20) /* N Z C V I F M4 M3 M2 M1 M0 */ | (R15 & 0xF0000000) /* N Z C V */ | ((R15 & 0x0C000000) >> (26 - 6)) /* I F */ | (R15 & 0x00000003) /* M1 M0 */;
1756                  SET_CPSR( temp);
1757                  SwitchMode(cpustate, temp & 3);
1758                    }
1759                }
1760                // LDM PC - takes 2 extra cycles
1761                ARM7_ICOUNT -= 2;
1762            }
1763        }
1764        // LDM (NO PC) takes (n)S + 1N + 1I cycles (n = # of register transfers)
1765        ARM7_ICOUNT -= result + 1 + 1;
1766    } /* Loading */
1767    else
1768    {
1769        /* Storing */
1770        if (insn & (1 << eR15))
1771        {
1772#if ARM7_DEBUG_CORE
1773                LOG(("%08x: Writing R15 in strm\n", R15));
1774#endif
1775            /* special case handling if writing to PC */
1776            R15 += 12;
1777        }
1778        if (insn & INSN_BDT_U)
1779        {
1780            /* Incrementing */
1781            if (!(insn & INSN_BDT_P))
1782            {
1783                rbp = rbp + (- 4);
1784            }
1785
1786            // S Flag Set = User Bank Transfer
1787            if (insn & INSN_BDT_S)
1788            {
1789                // todo: needs to be tested..
1790
1791                // set to user mode - then do the transfer, and set back
1792                //int curmode = GET_MODE;
1793                //SwitchMode(cpustate, eARM7_MODE_USER);
1794                LOG(("%08x: User Bank Transfer not fully tested - please check if working properly!\n", R15));
1795                result = storeInc(cpustate, insn & 0xffff, rbp, eARM7_MODE_USER);
1796                // todo - not sure if Writeback occurs on User registers also..
1797                //SwitchMode(cpustate, curmode);
1798            }
1799            else
1800                result = storeInc(cpustate, insn & 0xffff, rbp, GET_MODE);
1801
1802            if ((insn & INSN_BDT_W) && (cpustate->pendingAbtD == 0))
1803            {
1804                SET_REGISTER(cpustate, rb, GET_REGISTER(cpustate, rb) + result * 4);
1805            }
1806        }
1807        else
1808        {
1809            /* Decrementing */
1810            if (!(insn & INSN_BDT_P))
1811            {
1812                rbp = rbp - (-4);
1813            }
1814
1815            // S Flag Set = User Bank Transfer
1816            if (insn & INSN_BDT_S)
1817            {
1818                // set to user mode - then do the transfer, and set back
1819                //int curmode = GET_MODE;
1820                //SwitchMode(cpustate, eARM7_MODE_USER);
1821                LOG(("%08x: User Bank Transfer not fully tested - please check if working properly!\n", R15));
1822                result = storeDec(cpustate, insn & 0xffff, rbp, eARM7_MODE_USER);
1823                // todo - not sure if Writeback occurs on User registers also..
1824                //SwitchMode(cpustate, curmode);
1825            }
1826            else
1827                result = storeDec(cpustate, insn & 0xffff, rbp, GET_MODE);
1828
1829            if ((insn & INSN_BDT_W) && (cpustate->pendingAbtD == 0))
1830            {
1831                SET_REGISTER(cpustate, rb, GET_REGISTER(cpustate, rb) - result * 4);
1832            }
1833        }
1834        if (insn & (1 << eR15))
1835            R15 -= 12;
1836
1837        // STM takes (n-1)S + 2N cycles (n = # of register transfers)
1838        ARM7_ICOUNT -= (result - 1) + 2;
1839    }
1840
1841    // We will specify the cycle count for each case, so remove the -3 that occurs at the end
1842    ARM7_ICOUNT += 3;
1843
1844} /* HandleMemBlock */
src/emu/cpu/arm7/arm7thmb.c
r14020r14021
33#include "arm7thmb.h"
44#include "arm7help.h"
55
6arm7thumb_ophandler thumb_handler[0x10] =
6// this is our master dispatch jump table for THUMB mode, representing [(insn & 0xffc0) >> 6] bits of the 16-bit decoded instruction
7arm7thumb_ophandler thumb_handler[0x40*0x10] =
78{
8   thumbblock00,
9   thumbblock01,
10   thumbblock02,
11   thumbblock03,
12   thumbblock04,
13   thumbblock05,
14   thumbblock06,
15   thumbblock07,
16   thumbblock08,
17   thumbblock09,
18   thumbblock0a,
19   thumbblock0b,
20   thumbblock0c,
21   thumbblock0d,
22   thumbblock0e,
23   thumbblock0f,
9// #define THUMB_SHIFT_R       ((UINT16)0x0800)
10   tg00_0,      tg00_0,      tg00_0,      tg00_0,      tg00_0,      tg00_0,      tg00_0,      tg00_0,      tg00_0,      tg00_0,      tg00_0,      tg00_0,      tg00_0,      tg00_0,      tg00_0,      tg00_0,      tg00_0,      tg00_0,      tg00_0,      tg00_0,      tg00_0,      tg00_0,      tg00_0,      tg00_0,      tg00_0,      tg00_0,      tg00_0,      tg00_0,      tg00_0,      tg00_0,      tg00_0,      tg00_0,      tg00_1,      tg00_1,      tg00_1,      tg00_1,      tg00_1,      tg00_1,      tg00_1,      tg00_1,      tg00_1,      tg00_1,      tg00_1,      tg00_1,      tg00_1,      tg00_1,      tg00_1,      tg00_1,      tg00_1,      tg00_1,      tg00_1,      tg00_1,      tg00_1,      tg00_1,      tg00_1,      tg00_1,      tg00_1,      tg00_1,      tg00_1,      tg00_1,      tg00_1,      tg00_1,      tg00_1,      tg00_1,
11// #define THUMB_INSN_ADDSUB   ((UINT16)0x0800)   // #define THUMB_ADDSUB_TYPE   ((UINT16)0x0600)
12   tg01_0,      tg01_0,      tg01_0,      tg01_0,      tg01_0,      tg01_0,      tg01_0,      tg01_0,      tg01_0,      tg01_0,      tg01_0,      tg01_0,      tg01_0,      tg01_0,      tg01_0,      tg01_0,      tg01_0,      tg01_0,      tg01_0,      tg01_0,      tg01_0,      tg01_0,      tg01_0,      tg01_0,      tg01_0,      tg01_0,      tg01_0,      tg01_0,      tg01_0,      tg01_0,      tg01_0,      tg01_0,      tg01_10,   tg01_10,   tg01_10,   tg01_10,   tg01_10,   tg01_10,   tg01_10,   tg01_10,   tg01_11,   tg01_11,   tg01_11,   tg01_11,   tg01_11,   tg01_11,   tg01_11,   tg01_11,   tg01_12,   tg01_12,   tg01_12,   tg01_12,   tg01_12,   tg01_12,   tg01_12,   tg01_12,   tg01_13,   tg01_13,   tg01_13,   tg01_13,   tg01_13,   tg01_13,   tg01_13,   tg01_13,
13// #define THUMB_INSN_CMP      ((UINT16)0x0800)
14   tg02_0,      tg02_0,      tg02_0,      tg02_0,      tg02_0,      tg02_0,      tg02_0,      tg02_0,      tg02_0,      tg02_0,      tg02_0,      tg02_0,      tg02_0,      tg02_0,      tg02_0,      tg02_0,      tg02_0,      tg02_0,      tg02_0,      tg02_0,      tg02_0,      tg02_0,      tg02_0,      tg02_0,      tg02_0,      tg02_0,      tg02_0,      tg02_0,      tg02_0,      tg02_0,      tg02_0,      tg02_0,      tg02_1,      tg02_1,      tg02_1,      tg02_1,      tg02_1,      tg02_1,      tg02_1,      tg02_1,      tg02_1,      tg02_1,      tg02_1,      tg02_1,      tg02_1,      tg02_1,      tg02_1,      tg02_1,      tg02_1,      tg02_1,      tg02_1,      tg02_1,      tg02_1,      tg02_1,      tg02_1,      tg02_1,      tg02_1,      tg02_1,      tg02_1,      tg02_1,      tg02_1,      tg02_1,      tg02_1,      tg02_1,
15// #define THUMB_INSN_SUB      ((UINT16)0x0800)
16   tg03_0,      tg03_0,      tg03_0,      tg03_0,      tg03_0,      tg03_0,      tg03_0,      tg03_0,      tg03_0,      tg03_0,      tg03_0,      tg03_0,      tg03_0,      tg03_0,      tg03_0,      tg03_0,      tg03_0,      tg03_0,      tg03_0,      tg03_0,      tg03_0,      tg03_0,      tg03_0,      tg03_0,      tg03_0,      tg03_0,      tg03_0,      tg03_0,      tg03_0,      tg03_0,      tg03_0,      tg03_0,      tg03_1,      tg03_1,      tg03_1,      tg03_1,      tg03_1,      tg03_1,      tg03_1,      tg03_1,      tg03_1,      tg03_1,      tg03_1,      tg03_1,      tg03_1,      tg03_1,      tg03_1,      tg03_1,      tg03_1,      tg03_1,      tg03_1,      tg03_1,      tg03_1,      tg03_1,      tg03_1,      tg03_1,      tg03_1,      tg03_1,      tg03_1,      tg03_1,      tg03_1,      tg03_1,      tg03_1,      tg03_1,
17//#define THUMB_GROUP4_TYPE   ((UINT16)0x0c00)  //#define THUMB_ALUOP_TYPE    ((UINT16)0x03c0)  // #define THUMB_HIREG_OP      ((UINT16)0x0300)  // #define THUMB_HIREG_H       ((UINT16)0x00c0)
18   tg04_00_00,   tg04_00_01,   tg04_00_02,   tg04_00_03,   tg04_00_04,   tg04_00_05,   tg04_00_06,   tg04_00_07,   tg04_00_08,   tg04_00_09,   tg04_00_0a,   tg04_00_0b,   tg04_00_0c,   tg04_00_0d,   tg04_00_0e,   tg04_00_0f,   tg04_01_00,   tg04_01_01,   tg04_01_02,   tg04_01_03,   tg04_01_10,   tg04_01_11,   tg04_01_12,   tg04_01_13,   tg04_01_20,   tg04_01_21,   tg04_01_22,   tg04_01_23,   tg04_01_30,   tg04_01_31,   tg04_01_32,   tg04_01_33,   tg04_0203,   tg04_0203,   tg04_0203,   tg04_0203,   tg04_0203,   tg04_0203,   tg04_0203,   tg04_0203,   tg04_0203,   tg04_0203,   tg04_0203,   tg04_0203,   tg04_0203,   tg04_0203,   tg04_0203,   tg04_0203,   tg04_0203,   tg04_0203,   tg04_0203,   tg04_0203,   tg04_0203,   tg04_0203,   tg04_0203,   tg04_0203,   tg04_0203,   tg04_0203,   tg04_0203,   tg04_0203,   tg04_0203,   tg04_0203,   tg04_0203,   tg04_0203,
19//#define THUMB_GROUP5_TYPE   ((UINT16)0x0e00)
20   tg05_0,      tg05_0,      tg05_0,      tg05_0,      tg05_0,      tg05_0,      tg05_0,      tg05_0,      tg05_1,      tg05_1,      tg05_1,      tg05_1,      tg05_1,      tg05_1,      tg05_1,      tg05_1,      tg05_2,      tg05_2,      tg05_2,      tg05_2,      tg05_2,      tg05_2,      tg05_2,      tg05_2,      tg05_3,      tg05_3,      tg05_3,      tg05_3,      tg05_3,      tg05_3,      tg05_3,      tg05_3,      tg05_4,      tg05_4,      tg05_4,      tg05_4,      tg05_4,      tg05_4,      tg05_4,      tg05_4,      tg05_5,      tg05_5,      tg05_5,      tg05_5,      tg05_5,      tg05_5,      tg05_5,      tg05_5,      tg05_6,      tg05_6,      tg05_6,      tg05_6,      tg05_6,      tg05_6,      tg05_6,      tg05_6,      tg05_7,      tg05_7,      tg05_7,      tg05_7,      tg05_7,      tg05_7,      tg05_7,      tg05_7,
21//#define THUMB_LSOP_L        ((UINT16)0x0800)
22   tg06_0,      tg06_0,      tg06_0,      tg06_0,      tg06_0,      tg06_0,      tg06_0,      tg06_0,      tg06_0,      tg06_0,      tg06_0,      tg06_0,      tg06_0,      tg06_0,      tg06_0,      tg06_0,      tg06_0,      tg06_0,      tg06_0,      tg06_0,      tg06_0,      tg06_0,      tg06_0,      tg06_0,      tg06_0,      tg06_0,      tg06_0,      tg06_0,      tg06_0,      tg06_0,      tg06_0,      tg06_0,      tg06_1,      tg06_1,      tg06_1,      tg06_1,      tg06_1,      tg06_1,      tg06_1,      tg06_1,      tg06_1,      tg06_1,      tg06_1,      tg06_1,      tg06_1,      tg06_1,      tg06_1,      tg06_1,      tg06_1,      tg06_1,      tg06_1,      tg06_1,      tg06_1,      tg06_1,      tg06_1,      tg06_1,      tg06_1,      tg06_1,      tg06_1,      tg06_1,      tg06_1,      tg06_1,      tg06_1,      tg06_1,
23//#define THUMB_LSOP_L        ((UINT16)0x0800)
24   tg07_0,      tg07_0,      tg07_0,      tg07_0,      tg07_0,      tg07_0,      tg07_0,      tg07_0,      tg07_0,      tg07_0,      tg07_0,      tg07_0,      tg07_0,      tg07_0,      tg07_0,      tg07_0,      tg07_0,      tg07_0,      tg07_0,      tg07_0,      tg07_0,      tg07_0,      tg07_0,      tg07_0,      tg07_0,      tg07_0,      tg07_0,      tg07_0,      tg07_0,      tg07_0,      tg07_0,      tg07_0,      tg07_1,      tg07_1,      tg07_1,      tg07_1,      tg07_1,      tg07_1,      tg07_1,      tg07_1,      tg07_1,      tg07_1,      tg07_1,      tg07_1,      tg07_1,      tg07_1,      tg07_1,      tg07_1,      tg07_1,      tg07_1,      tg07_1,      tg07_1,      tg07_1,      tg07_1,      tg07_1,      tg07_1,      tg07_1,      tg07_1,      tg07_1,      tg07_1,      tg07_1,      tg07_1,      tg07_1,      tg07_1,
25// #define THUMB_HALFOP_L      ((UINT16)0x0800)
26   tg08_0,      tg08_0,      tg08_0,      tg08_0,      tg08_0,      tg08_0,      tg08_0,      tg08_0,      tg08_0,      tg08_0,      tg08_0,      tg08_0,      tg08_0,      tg08_0,      tg08_0,      tg08_0,      tg08_0,      tg08_0,      tg08_0,      tg08_0,      tg08_0,      tg08_0,      tg08_0,      tg08_0,      tg08_0,      tg08_0,      tg08_0,      tg08_0,      tg08_0,      tg08_0,      tg08_0,      tg08_0,      tg08_1,      tg08_1,      tg08_1,      tg08_1,      tg08_1,      tg08_1,      tg08_1,      tg08_1,      tg08_1,      tg08_1,      tg08_1,      tg08_1,      tg08_1,      tg08_1,      tg08_1,      tg08_1,      tg08_1,      tg08_1,      tg08_1,      tg08_1,      tg08_1,      tg08_1,      tg08_1,      tg08_1,      tg08_1,      tg08_1,      tg08_1,      tg08_1,      tg08_1,      tg08_1,      tg08_1,      tg08_1,
27// #define THUMB_STACKOP_L     ((UINT16)0x0800)
28   tg09_0,      tg09_0,      tg09_0,      tg09_0,      tg09_0,      tg09_0,      tg09_0,      tg09_0,      tg09_0,      tg09_0,      tg09_0,      tg09_0,      tg09_0,      tg09_0,      tg09_0,      tg09_0,      tg09_0,      tg09_0,      tg09_0,      tg09_0,      tg09_0,      tg09_0,      tg09_0,      tg09_0,      tg09_0,      tg09_0,      tg09_0,      tg09_0,      tg09_0,      tg09_0,      tg09_0,      tg09_0,      tg09_1,      tg09_1,      tg09_1,      tg09_1,      tg09_1,      tg09_1,      tg09_1,      tg09_1,      tg09_1,      tg09_1,      tg09_1,      tg09_1,      tg09_1,      tg09_1,      tg09_1,      tg09_1,      tg09_1,      tg09_1,      tg09_1,      tg09_1,      tg09_1,      tg09_1,      tg09_1,      tg09_1,      tg09_1,      tg09_1,      tg09_1,      tg09_1,      tg09_1,      tg09_1,      tg09_1,      tg09_1,
29// #define THUMB_RELADDR_SP    ((UINT16)0x0800)
30   tg0a_0,      tg0a_0,      tg0a_0,      tg0a_0,      tg0a_0,      tg0a_0,      tg0a_0,      tg0a_0,      tg0a_0,      tg0a_0,      tg0a_0,      tg0a_0,      tg0a_0,      tg0a_0,      tg0a_0,      tg0a_0,      tg0a_0,      tg0a_0,      tg0a_0,      tg0a_0,      tg0a_0,      tg0a_0,      tg0a_0,      tg0a_0,      tg0a_0,      tg0a_0,      tg0a_0,      tg0a_0,      tg0a_0,      tg0a_0,      tg0a_0,      tg0a_0,      tg0a_1,      tg0a_1,      tg0a_1,      tg0a_1,      tg0a_1,      tg0a_1,      tg0a_1,      tg0a_1,      tg0a_1,      tg0a_1,      tg0a_1,      tg0a_1,      tg0a_1,      tg0a_1,      tg0a_1,      tg0a_1,      tg0a_1,      tg0a_1,      tg0a_1,      tg0a_1,      tg0a_1,      tg0a_1,      tg0a_1,      tg0a_1,      tg0a_1,      tg0a_1,      tg0a_1,      tg0a_1,      tg0a_1,      tg0a_1,      tg0a_1,      tg0a_1,
31// #define THUMB_STACKOP_TYPE  ((UINT16)0x0f00)
32   tg0b_0,      tg0b_0,      tg0b_0,      tg0b_0,      tg0b_1,      tg0b_1,      tg0b_1,      tg0b_1,      tg0b_2,      tg0b_2,      tg0b_2,      tg0b_2,      tg0b_3,      tg0b_3,      tg0b_3,      tg0b_3,      tg0b_4,      tg0b_4,      tg0b_4,      tg0b_4,      tg0b_5,      tg0b_5,      tg0b_5,      tg0b_5,      tg0b_6,      tg0b_6,      tg0b_6,      tg0b_6,      tg0b_7,      tg0b_7,      tg0b_7,      tg0b_7,      tg0b_8,      tg0b_8,      tg0b_8,      tg0b_8,      tg0b_9,      tg0b_9,      tg0b_9,      tg0b_9,      tg0b_a,      tg0b_a,      tg0b_a,      tg0b_a,      tg0b_b,      tg0b_b,      tg0b_b,      tg0b_b,      tg0b_c,      tg0b_c,      tg0b_c,      tg0b_c,      tg0b_d,      tg0b_d,      tg0b_d,      tg0b_d,      tg0b_e,      tg0b_e,      tg0b_e,      tg0b_e,      tg0b_f,      tg0b_f,      tg0b_f,      tg0b_f,     
33// #define THUMB_MULTLS        ((UINT16)0x0800)
34   tg0c_0,      tg0c_0,      tg0c_0,      tg0c_0,      tg0c_0,      tg0c_0,      tg0c_0,      tg0c_0,      tg0c_0,      tg0c_0,      tg0c_0,      tg0c_0,      tg0c_0,      tg0c_0,      tg0c_0,      tg0c_0,      tg0c_0,      tg0c_0,      tg0c_0,      tg0c_0,      tg0c_0,      tg0c_0,      tg0c_0,      tg0c_0,      tg0c_0,      tg0c_0,      tg0c_0,      tg0c_0,      tg0c_0,      tg0c_0,      tg0c_0,      tg0c_0,      tg0c_1,      tg0c_1,      tg0c_1,      tg0c_1,      tg0c_1,      tg0c_1,      tg0c_1,      tg0c_1,      tg0c_1,      tg0c_1,      tg0c_1,      tg0c_1,      tg0c_1,      tg0c_1,      tg0c_1,      tg0c_1,      tg0c_1,      tg0c_1,      tg0c_1,      tg0c_1,      tg0c_1,      tg0c_1,      tg0c_1,      tg0c_1,      tg0c_1,      tg0c_1,      tg0c_1,      tg0c_1,      tg0c_1,      tg0c_1,      tg0c_1,      tg0c_1,
35// #define THUMB_COND_TYPE     ((UINT16)0x0f00)
36   tg0d_0,      tg0d_0,      tg0d_0,      tg0d_0,      tg0d_1,      tg0d_1,      tg0d_1,      tg0d_1,      tg0d_2,      tg0d_2,      tg0d_2,      tg0d_2,      tg0d_3,      tg0d_3,      tg0d_3,      tg0d_3,      tg0d_4,      tg0d_4,      tg0d_4,      tg0d_4,      tg0d_5,      tg0d_5,      tg0d_5,      tg0d_5,      tg0d_6,      tg0d_6,      tg0d_6,      tg0d_6,      tg0d_7,      tg0d_7,      tg0d_7,      tg0d_7,      tg0d_8,      tg0d_8,      tg0d_8,      tg0d_8,      tg0d_9,      tg0d_9,      tg0d_9,      tg0d_9,      tg0d_a,      tg0d_a,      tg0d_a,      tg0d_a,      tg0d_b,      tg0d_b,      tg0d_b,      tg0d_b,      tg0d_c,      tg0d_c,      tg0d_c,      tg0d_c,      tg0d_d,      tg0d_d,      tg0d_d,      tg0d_d,      tg0d_e,      tg0d_e,      tg0d_e,      tg0d_e,      tg0d_f,      tg0d_f,      tg0d_f,      tg0d_f,     
37// #define THUMB_BLOP_LO       ((UINT16)0x0800)
38   tg0e_0,      tg0e_0,      tg0e_0,      tg0e_0,      tg0e_0,      tg0e_0,      tg0e_0,      tg0e_0,      tg0e_0,      tg0e_0,      tg0e_0,      tg0e_0,      tg0e_0,      tg0e_0,      tg0e_0,      tg0e_0,      tg0e_0,      tg0e_0,      tg0e_0,      tg0e_0,      tg0e_0,      tg0e_0,      tg0e_0,      tg0e_0,      tg0e_0,      tg0e_0,      tg0e_0,      tg0e_0,      tg0e_0,      tg0e_0,      tg0e_0,      tg0e_0,      tg0e_1,      tg0e_1,      tg0e_1,      tg0e_1,      tg0e_1,      tg0e_1,      tg0e_1,      tg0e_1,      tg0e_1,      tg0e_1,      tg0e_1,      tg0e_1,      tg0e_1,      tg0e_1,      tg0e_1,      tg0e_1,      tg0e_1,      tg0e_1,      tg0e_1,      tg0e_1,      tg0e_1,      tg0e_1,      tg0e_1,      tg0e_1,      tg0e_1,      tg0e_1,      tg0e_1,      tg0e_1,      tg0e_1,      tg0e_1,      tg0e_1,      tg0e_1,
39// #define THUMB_BLOP_LO       ((UINT16)0x0800)
40   tg0f_0,      tg0f_0,      tg0f_0,      tg0f_0,      tg0f_0,      tg0f_0,      tg0f_0,      tg0f_0,      tg0f_0,      tg0f_0,      tg0f_0,      tg0f_0,      tg0f_0,      tg0f_0,      tg0f_0,      tg0f_0,      tg0f_0,      tg0f_0,      tg0f_0,      tg0f_0,      tg0f_0,      tg0f_0,      tg0f_0,      tg0f_0,      tg0f_0,      tg0f_0,      tg0f_0,      tg0f_0,      tg0f_0,      tg0f_0,      tg0f_0,      tg0f_0,      tg0f_1,      tg0f_1,      tg0f_1,      tg0f_1,      tg0f_1,      tg0f_1,      tg0f_1,      tg0f_1,      tg0f_1,      tg0f_1,      tg0f_1,      tg0f_1,      tg0f_1,      tg0f_1,      tg0f_1,      tg0f_1,      tg0f_1,      tg0f_1,      tg0f_1,      tg0f_1,      tg0f_1,      tg0f_1,      tg0f_1,      tg0f_1,      tg0f_1,      tg0f_1,      tg0f_1,      tg0f_1,      tg0f_1,      tg0f_1,      tg0f_1,      tg0f_1,
2441};
2542
43 /* Shift operations */
2644
27const void thumbblock00(arm_state *cpustate, UINT32 pc, UINT32 insn) /* Shift operations */
45const void tg00_0(arm_state *cpustate, UINT32 pc, UINT32 insn) /* Shift left */
2846{
2947   UINT32 rs, rd, rrs;
3048   INT32 offs;
3149
3250    SET_CPSR(GET_CPSR & ~(N_MASK | Z_MASK));
33    if (insn & THUMB_SHIFT_R) /* Shift right */
51
52   rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
53    rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
54    rrs = GET_REGISTER(cpustate, rs);
55    offs = (insn & THUMB_SHIFT_AMT) >> THUMB_SHIFT_AMT_SHIFT;
56    if (offs != 0)
3457    {
35        rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
36        rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
37        rrs = GET_REGISTER(cpustate, rs);
38        offs = (insn & THUMB_SHIFT_AMT) >> THUMB_SHIFT_AMT_SHIFT;
39        if (offs != 0)
58        SET_REGISTER(cpustate, rd, rrs << offs);
59        if (rrs & (1 << (31 - (offs - 1))))
4060        {
41            SET_REGISTER(cpustate, rd, rrs >> offs);
42            if (rrs & (1 << (offs - 1)))
43            {
44                SET_CPSR(GET_CPSR | C_MASK);
45            }
46            else
47            {
48                SET_CPSR(GET_CPSR & ~C_MASK);
49            }
61            SET_CPSR(GET_CPSR | C_MASK);
5062        }
5163        else
5264        {
53            SET_REGISTER(cpustate, rd, 0);
54            if (rrs & 0x80000000)
55            {
56                SET_CPSR(GET_CPSR | C_MASK);
57            }
58            else
59            {
60                SET_CPSR(GET_CPSR & ~C_MASK);
61            }
65            SET_CPSR(GET_CPSR & ~C_MASK);
6266        }
63        SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
64        SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(cpustate, rd)));
65        R15 += 2;
6667    }
67    else /* Shift left */
68    else
6869    {
69        rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
70        rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
71        rrs = GET_REGISTER(cpustate, rs);
72        offs = (insn & THUMB_SHIFT_AMT) >> THUMB_SHIFT_AMT_SHIFT;
73        if (offs != 0)
70        SET_REGISTER(cpustate, rd, rrs);
71    }
72    SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
73    SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(cpustate, rd)));
74    R15 += 2;
75}
76
77const void tg00_1(arm_state *cpustate, UINT32 pc, UINT32 insn) /* Shift right */
78{
79   UINT32 rs, rd, rrs;
80   INT32 offs;
81
82    SET_CPSR(GET_CPSR & ~(N_MASK | Z_MASK));
83
84    rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
85    rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
86    rrs = GET_REGISTER(cpustate, rs);
87    offs = (insn & THUMB_SHIFT_AMT) >> THUMB_SHIFT_AMT_SHIFT;
88    if (offs != 0)
89    {
90        SET_REGISTER(cpustate, rd, rrs >> offs);
91        if (rrs & (1 << (offs - 1)))
7492        {
75            SET_REGISTER(cpustate, rd, rrs << offs);
76            if (rrs & (1 << (31 - (offs - 1))))
77            {
78                SET_CPSR(GET_CPSR | C_MASK);
79            }
80            else
81            {
82                SET_CPSR(GET_CPSR & ~C_MASK);
83            }
93            SET_CPSR(GET_CPSR | C_MASK);
8494        }
8595        else
8696        {
87            SET_REGISTER(cpustate, rd, rrs);
97            SET_CPSR(GET_CPSR & ~C_MASK);
8898        }
89        SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
90        SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(cpustate, rd)));
91        R15 += 2;
9299    }
100    else
101    {
102        SET_REGISTER(cpustate, rd, 0);
103        if (rrs & 0x80000000)
104        {
105            SET_CPSR(GET_CPSR | C_MASK);
106        }
107        else
108        {
109            SET_CPSR(GET_CPSR & ~C_MASK);
110        }
111    }
112    SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
113    SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(cpustate, rd)));
114    R15 += 2;
93115}
94116
95const void thumbblock01(arm_state *cpustate, UINT32 pc, UINT32 insn) /* Arithmetic */
117/* Arithmetic */
118
119const void tg01_0(arm_state *cpustate, UINT32 pc, UINT32 insn)
96120{
97   UINT32 rn, rs, rd, imm, rrs;
121   UINT32 rs, rd, rrs;
98122   INT32 offs;
123   /* ASR.. */
124   //if (insn & THUMB_SHIFT_R) /* Shift right */
125   {
126      rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
127      rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
128      rrs = GET_REGISTER(cpustate, rs);
129      offs = (insn & THUMB_SHIFT_AMT) >> THUMB_SHIFT_AMT_SHIFT;
130      if (offs == 0)
131      {
132         offs = 32;
133      }
134      if (offs >= 32)
135      {
136         if (rrs >> 31)
137         {
138            SET_CPSR(GET_CPSR | C_MASK);
139         }
140         else
141         {
142            SET_CPSR(GET_CPSR & ~C_MASK);
143         }
144         SET_REGISTER(cpustate, rd, (rrs & 0x80000000) ? 0xFFFFFFFF : 0x00000000);
145      }
146      else
147      {
148         if ((rrs >> (offs - 1)) & 1)
149         {
150            SET_CPSR(GET_CPSR | C_MASK);
151         }
152         else
153         {
154            SET_CPSR(GET_CPSR & ~C_MASK);
155         }
156         SET_REGISTER(cpustate, rd,
157                     (rrs & 0x80000000)
158                     ? ((0xFFFFFFFF << (32 - offs)) | (rrs >> offs))
159                     : (rrs >> offs));
160      }
161      SET_CPSR(GET_CPSR & ~(N_MASK | Z_MASK));
162      SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(cpustate, rd)));
163      R15 += 2;
164   }
165}
99166
100    if (insn & THUMB_INSN_ADDSUB)
167const void tg01_10(arm_state *cpustate, UINT32 pc, UINT32 insn)  /* ADD Rd, Rs, Rn */
168{
169   UINT32 rn, rs, rd;
170
171 
172    rn = GET_REGISTER(cpustate, (insn & THUMB_ADDSUB_RNIMM) >> THUMB_ADDSUB_RNIMM_SHIFT);
173    rs = GET_REGISTER(cpustate, (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT);
174    rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
175    SET_REGISTER(cpustate, rd, rs + rn);
176    HandleThumbALUAddFlags(GET_REGISTER(cpustate, rd), rs, rn);
177
178}
179
180const void tg01_11(arm_state *cpustate, UINT32 pc, UINT32 insn) /* SUB Rd, Rs, Rn */
181{
182   UINT32 rn, rs, rd;
183 
184    rn = GET_REGISTER(cpustate, (insn & THUMB_ADDSUB_RNIMM) >> THUMB_ADDSUB_RNIMM_SHIFT);
185    rs = GET_REGISTER(cpustate, (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT);
186    rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
187    SET_REGISTER(cpustate, rd, rs - rn);
188    HandleThumbALUSubFlags(GET_REGISTER(cpustate, rd), rs, rn);
189
190}
191
192const void tg01_12(arm_state *cpustate, UINT32 pc, UINT32 insn) /* ADD Rd, Rs, #imm */
193{
194   UINT32 rs, rd, imm;
195 
196    imm = (insn & THUMB_ADDSUB_RNIMM) >> THUMB_ADDSUB_RNIMM_SHIFT;
197    rs = GET_REGISTER(cpustate, (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT);
198    rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
199    SET_REGISTER(cpustate, rd, rs + imm);
200    HandleThumbALUAddFlags(GET_REGISTER(cpustate, rd), rs, imm);
201
202}
203
204const void tg01_13(arm_state *cpustate, UINT32 pc, UINT32 insn) /* SUB Rd, Rs, #imm */
205{
206   UINT32 rs, rd, imm;
207 
208    imm = (insn & THUMB_ADDSUB_RNIMM) >> THUMB_ADDSUB_RNIMM_SHIFT;
209    rs = GET_REGISTER(cpustate, (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT);
210    rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
211    SET_REGISTER(cpustate, rd, rs - imm);
212    HandleThumbALUSubFlags(GET_REGISTER(cpustate, rd), rs,imm);
213
214}
215
216/* CMP / MOV */
217
218const void tg02_0(arm_state *cpustate, UINT32 pc, UINT32 insn)
219{
220   UINT32 rd, op2;
221
222   rd = (insn & THUMB_INSN_IMM_RD) >> THUMB_INSN_IMM_RD_SHIFT;
223   op2 = (insn & THUMB_INSN_IMM);
224   SET_REGISTER(cpustate, rd, op2);
225   SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
226   SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(cpustate, rd)));
227   R15 += 2;
228}
229
230const void tg02_1(arm_state *cpustate, UINT32 pc, UINT32 insn)
231{
232   UINT32 rn, rd, op2;
233
234   rn = GET_REGISTER(cpustate, (insn & THUMB_INSN_IMM_RD) >> THUMB_INSN_IMM_RD_SHIFT);
235   op2 = insn & THUMB_INSN_IMM;
236   rd = rn - op2;
237   HandleThumbALUSubFlags(rd, rn, op2);
238   //mame_printf_debug("%08x: xxx Thumb instruction: CMP R%d (%08x), %02x (N=%d, Z=%d, C=%d, V=%d)\n", pc, (insn & THUMB_INSN_IMM_RD) >> THUMB_INSN_IMM_RD_SHIFT, GET_REGISTER(cpustate, (insn & THUMB_INSN_IMM_RD) >> THUMB_INSN_IMM_RD_SHIFT), op2, N_IS_SET(GET_CPSR) ? 1 : 0, Z_IS_SET(GET_CPSR) ? 1 : 0, C_IS_SET(GET_CPSR) ? 1 : 0, V_IS_SET(GET_CPSR) ? 1 : 0);
239}
240
241
242
243/* ADD/SUB immediate */
244
245const void tg03_0(arm_state *cpustate, UINT32 pc, UINT32 insn) /* ADD Rd, #Offset8 */
246{
247   UINT32 rn, rd, op2;
248
249   rn = GET_REGISTER(cpustate, (insn & THUMB_INSN_IMM_RD) >> THUMB_INSN_IMM_RD_SHIFT);
250   op2 = insn & THUMB_INSN_IMM;
251   rd = rn + op2;
252   //mame_printf_debug("%08x:  Thumb instruction: ADD R%d, %02x\n", pc, (insn & THUMB_INSN_IMM_RD) >> THUMB_INSN_IMM_RD_SHIFT, op2);
253   SET_REGISTER(cpustate, (insn & THUMB_INSN_IMM_RD) >> THUMB_INSN_IMM_RD_SHIFT, rd);
254   HandleThumbALUAddFlags(rd, rn, op2);
255}
256
257const void tg03_1(arm_state *cpustate, UINT32 pc, UINT32 insn) /* SUB Rd, #Offset8 */
258{
259   UINT32 rn, rd, op2;
260
261   rn = GET_REGISTER(cpustate, (insn & THUMB_INSN_IMM_RD) >> THUMB_INSN_IMM_RD_SHIFT);
262   op2 = insn & THUMB_INSN_IMM;
263   //mame_printf_debug("%08x:  Thumb instruction: SUB R%d, %02x\n", pc, (insn & THUMB_INSN_IMM_RD) >> THUMB_INSN_IMM_RD_SHIFT, op2);
264   rd = rn - op2;
265   SET_REGISTER(cpustate, (insn & THUMB_INSN_IMM_RD) >> THUMB_INSN_IMM_RD_SHIFT, rd);
266   HandleThumbALUSubFlags(rd, rn, op2);
267}
268
269
270
271/* Rd & Rm instructions */
272
273const void tg04_00_00(arm_state *cpustate, UINT32 pc, UINT32 insn) /* AND Rd, Rs */
274{
275   UINT32 rs, rd;
276 
277   rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
278   rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
279   SET_REGISTER(cpustate, rd, GET_REGISTER(cpustate, rd) & GET_REGISTER(cpustate, rs));
280   SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
281   SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(cpustate, rd)));
282   R15 += 2;
283
284}
285
286const void tg04_00_01(arm_state *cpustate, UINT32 pc, UINT32 insn) /* EOR Rd, Rs */
287{
288   UINT32 rs, rd;
289 
290   rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
291   rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
292   SET_REGISTER(cpustate, rd, GET_REGISTER(cpustate, rd) ^ GET_REGISTER(cpustate, rs));
293   SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
294   SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(cpustate, rd)));
295   R15 += 2;
296
297}
298
299const void tg04_00_02(arm_state *cpustate, UINT32 pc, UINT32 insn) /* LSL Rd, Rs */
300{
301   UINT32 rs, rd, rrd;
302   INT32 offs;
303 
304   rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
305   rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
306   rrd = GET_REGISTER(cpustate, rd);
307   offs = GET_REGISTER(cpustate, rs) & 0x000000ff;
308   if (offs > 0)
309   {
310      if (offs < 32)
311      {
312         SET_REGISTER(cpustate, rd, rrd << offs);
313         if (rrd & (1 << (31 - (offs - 1))))
314         {
315            SET_CPSR(GET_CPSR | C_MASK);
316         }
317         else
318         {
319            SET_CPSR(GET_CPSR & ~C_MASK);
320         }
321      }
322      else if (offs == 32)
323      {
324         SET_REGISTER(cpustate, rd, 0);
325         if (rrd & 1)
326         {
327            SET_CPSR(GET_CPSR | C_MASK);
328         }
329         else
330         {
331            SET_CPSR(GET_CPSR & ~C_MASK);
332         }
333      }
334      else
335      {
336         SET_REGISTER(cpustate, rd, 0);
337         SET_CPSR(GET_CPSR & ~C_MASK);
338      }
339   }
340   SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
341   SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(cpustate, rd)));
342   R15 += 2;
343
344}
345
346const void tg04_00_03(arm_state *cpustate, UINT32 pc, UINT32 insn) /* LSR Rd, Rs */
347{
348   UINT32 rs, rd, rrd;
349   INT32 offs;
350 
351   rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
352   rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
353   rrd = GET_REGISTER(cpustate, rd);
354   offs = GET_REGISTER(cpustate, rs) & 0x000000ff;
355   if (offs >  0)
356   {
357      if (offs < 32)
358      {
359         SET_REGISTER(cpustate, rd, rrd >> offs);
360         if (rrd & (1 << (offs - 1)))
361         {
362            SET_CPSR(GET_CPSR | C_MASK);
363         }
364         else
365         {
366            SET_CPSR(GET_CPSR & ~C_MASK);
367         }
368      }
369      else if (offs == 32)
370      {
371         SET_REGISTER(cpustate, rd, 0);
372         if (rrd & 0x80000000)
373         {
374            SET_CPSR(GET_CPSR | C_MASK);
375         }
376         else
377         {
378            SET_CPSR(GET_CPSR & ~C_MASK);
379         }
380      }
381      else
382      {
383         SET_REGISTER(cpustate, rd, 0);
384         SET_CPSR(GET_CPSR & ~C_MASK);
385      }
386   }
387   SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
388   SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(cpustate, rd)));
389   R15 += 2;
390
391}
392
393const void tg04_00_04(arm_state *cpustate, UINT32 pc, UINT32 insn) /* ASR Rd, Rs */
394{
395   UINT32 rs, rd, rrs, rrd;
396 
397   rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
398   rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
399   rrs = GET_REGISTER(cpustate, rs)&0xff;
400   rrd = GET_REGISTER(cpustate, rd);
401   if (rrs != 0)
402   {
403      if (rrs >= 32)
404      {
405         if (rrd >> 31)
406         {
407            SET_CPSR(GET_CPSR | C_MASK);
408         }
409         else
410         {
411            SET_CPSR(GET_CPSR & ~C_MASK);
412         }
413         SET_REGISTER(cpustate, rd, (GET_REGISTER(cpustate, rd) & 0x80000000) ? 0xFFFFFFFF : 0x00000000);
414      }
415      else
416      {
417         if ((rrd >> (rrs-1)) & 1)
418         {
419            SET_CPSR(GET_CPSR | C_MASK);
420         }
421         else
422         {
423            SET_CPSR(GET_CPSR & ~C_MASK);
424         }
425         SET_REGISTER(cpustate, rd, (rrd & 0x80000000)
426                     ? ((0xFFFFFFFF << (32 - rrs)) | (rrd >> rrs))
427                     : (rrd >> rrs));
428      }
429   }
430   SET_CPSR(GET_CPSR & ~(N_MASK | Z_MASK));
431   SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(cpustate, rd)));
432   R15 += 2;
433
434}
435
436const void tg04_00_05(arm_state *cpustate, UINT32 pc, UINT32 insn) /* ADC Rd, Rs */
437{
438   UINT32 rn, rs, rd, op2;
439 
440   rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
441   rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
442   op2=(GET_CPSR & C_MASK) ? 1 : 0;
443   rn=GET_REGISTER(cpustate, rd) + GET_REGISTER(cpustate, rs) + op2;
444   HandleThumbALUAddFlags(rn, GET_REGISTER(cpustate, rd), (GET_REGISTER(cpustate, rs))); // ?
445   SET_REGISTER(cpustate, rd, rn);
446
447}
448
449const void tg04_00_06(arm_state *cpustate, UINT32 pc, UINT32 insn)  /* SBC Rd, Rs */
450{
451   UINT32 rn, rs, rd, op2;
452
453   rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
454   rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
455   op2=(GET_CPSR & C_MASK) ? 0 : 1;
456   rn=GET_REGISTER(cpustate, rd) - GET_REGISTER(cpustate, rs) - op2;
457   HandleThumbALUSubFlags(rn, GET_REGISTER(cpustate, rd), (GET_REGISTER(cpustate, rs))); //?
458   SET_REGISTER(cpustate, rd, rn);
459
460}
461
462const void tg04_00_07(arm_state *cpustate, UINT32 pc, UINT32 insn) /* ROR Rd, Rs */
463{
464   UINT32 rs, rd, imm, rrd;
465 
466   rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
467   rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
468   rrd = GET_REGISTER(cpustate, rd);
469   imm = GET_REGISTER(cpustate, rs) & 0x0000001f;
470   SET_REGISTER(cpustate, rd, (rrd >> imm) | (rrd << (32 - imm)));
471   if (rrd & (1 << (imm - 1)))
472   {
473      SET_CPSR(GET_CPSR | C_MASK);
474   }
475   else
476   {
477      SET_CPSR(GET_CPSR & ~C_MASK);
478   }
479   SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
480   SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(cpustate, rd)));
481   R15 += 2;
482
483}
484
485const void tg04_00_08(arm_state *cpustate, UINT32 pc, UINT32 insn) /* TST Rd, Rs */
486{
487   UINT32 rs, rd;
488 
489   rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
490   rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
491   SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
492   SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(cpustate, rd) & GET_REGISTER(cpustate, rs)));
493   R15 += 2;
494
495}
496
497const void tg04_00_09(arm_state *cpustate, UINT32 pc, UINT32 insn) /* NEG Rd, Rs */
498{
499   UINT32 rn, rs, rd, rrs;
500 
501   rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
502   rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
503   rrs = GET_REGISTER(cpustate, rs);
504   rn = 0 - rrs;
505   SET_REGISTER(cpustate, rd, rn);
506   HandleThumbALUSubFlags(GET_REGISTER(cpustate, rd), 0, rrs);
507
508}
509
510const void tg04_00_0a(arm_state *cpustate, UINT32 pc, UINT32 insn) /* CMP Rd, Rs */
511{
512   UINT32 rn, rs, rd;
513 
514   rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
515   rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
516   rn = GET_REGISTER(cpustate, rd) - GET_REGISTER(cpustate, rs);
517   HandleThumbALUSubFlags(rn, GET_REGISTER(cpustate, rd), GET_REGISTER(cpustate, rs));
518
519}
520
521
522const void tg04_00_0b(arm_state *cpustate, UINT32 pc, UINT32 insn) /* CMN Rd, Rs - check flags, add dasm */
523{
524   UINT32 rn, rs, rd;
525 
526   rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
527   rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
528   rn = GET_REGISTER(cpustate, rd) + GET_REGISTER(cpustate, rs);
529   HandleThumbALUAddFlags(rn, GET_REGISTER(cpustate, rd), GET_REGISTER(cpustate, rs));
530
531}
532
533const void tg04_00_0c(arm_state *cpustate, UINT32 pc, UINT32 insn) /* ORR Rd, Rs */
534{
535   UINT32 rs, rd;
536 
537   rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
538   rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
539   SET_REGISTER(cpustate, rd, GET_REGISTER(cpustate, rd) | GET_REGISTER(cpustate, rs));
540   SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
541   SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(cpustate, rd)));
542   R15 += 2;
543
544}
545
546const void tg04_00_0d(arm_state *cpustate, UINT32 pc, UINT32 insn) /* MUL Rd, Rs */
547{
548   UINT32 rn, rs, rd;
549 
550   rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
551   rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
552   rn = GET_REGISTER(cpustate, rd) * GET_REGISTER(cpustate, rs);
553   SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
554   SET_REGISTER(cpustate, rd, rn);
555   SET_CPSR(GET_CPSR | HandleALUNZFlags(rn));
556   R15 += 2;
557
558}
559
560const void tg04_00_0e(arm_state *cpustate, UINT32 pc, UINT32 insn) /* BIC Rd, Rs */
561{
562   UINT32 rs, rd;
563 
564   rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
565   rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
566   SET_REGISTER(cpustate, rd, GET_REGISTER(cpustate, rd) & (~GET_REGISTER(cpustate, rs)));
567   SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
568   SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(cpustate, rd)));
569   R15 += 2;
570
571}
572const void tg04_00_0f(arm_state *cpustate, UINT32 pc, UINT32 insn) /* MVN Rd, Rs */
573{
574   UINT32 rs, rd, op2;
575 
576   rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
577   rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
578   op2 = GET_REGISTER(cpustate, rs);
579   SET_REGISTER(cpustate, rd, ~op2);
580   SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
581   SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(cpustate, rd)));
582   R15 += 2;
583
584}
585
586/* ADD Rd, Rs group */
587
588const void tg04_01_00(arm_state *cpustate, UINT32 pc, UINT32 insn)
589{
590//   UINT32 rs, rd;
591//   rs = (insn & THUMB_HIREG_RS) >> THUMB_HIREG_RS_SHIFT;
592//   rd = insn & THUMB_HIREG_RD;
593
594
595   fatalerror("%08x: G4-1-0 Undefined Thumb instruction: %04x %x\n", pc, insn, (insn & THUMB_HIREG_H) >> THUMB_HIREG_H_SHIFT);
596
597   R15 += 2;
598
599}
600
601const void tg04_01_01(arm_state *cpustate, UINT32 pc, UINT32 insn) /* ADD Rd, HRs */
602{
603   UINT32 rs, rd;
604   rs = (insn & THUMB_HIREG_RS) >> THUMB_HIREG_RS_SHIFT;
605   rd = insn & THUMB_HIREG_RD;
606
607 
608   SET_REGISTER(cpustate, rd, GET_REGISTER(cpustate, rd) + GET_REGISTER(cpustate, rs+8));
609   // emulate the effects of pre-fetch
610   if (rs == 7)
611   {
612      SET_REGISTER(cpustate, rd, GET_REGISTER(cpustate, rd) + 4);
613   }
614
615   R15 += 2;
616}
617
618const void tg04_01_02(arm_state *cpustate, UINT32 pc, UINT32 insn) /* ADD HRd, Rs */
619{
620   UINT32 rs, rd;
621   rs = (insn & THUMB_HIREG_RS) >> THUMB_HIREG_RS_SHIFT;
622   rd = insn & THUMB_HIREG_RD;
623
624 
625   SET_REGISTER(cpustate, rd+8, GET_REGISTER(cpustate, rd+8) + GET_REGISTER(cpustate, rs));
626   if (rd == 7)
627   {
628      R15 += 2;
629   }
630
631   R15 += 2;
632}
633
634const void tg04_01_03(arm_state *cpustate, UINT32 pc, UINT32 insn) /* Add HRd, HRs */
635{
636   UINT32 rs, rd;
637   rs = (insn & THUMB_HIREG_RS) >> THUMB_HIREG_RS_SHIFT;
638   rd = insn & THUMB_HIREG_RD;
639
640 
641   SET_REGISTER(cpustate, rd+8, GET_REGISTER(cpustate, rd+8) + GET_REGISTER(cpustate, rs+8));
642   // emulate the effects of pre-fetch
643   if (rs == 7)
644   {
645      SET_REGISTER(cpustate, rd+8, GET_REGISTER(cpustate, rd+8) + 4);
646   }
647   if (rd == 7)
648   {
649      R15 += 2;
650   }
651
652   R15 += 2;
653}
654
655
656const void tg04_01_10(arm_state *cpustate, UINT32 pc, UINT32 insn)  /* CMP Rd, Rs */
657{
658   UINT32 rn, rs, rd;
659
660   rs = GET_REGISTER(cpustate, ((insn & THUMB_HIREG_RS) >> THUMB_HIREG_RS_SHIFT));
661   rd = GET_REGISTER(cpustate, insn & THUMB_HIREG_RD);
662   rn = rd - rs;
663   HandleThumbALUSubFlags(rn, rd, rs);
664
665}
666
667const void tg04_01_11(arm_state *cpustate, UINT32 pc, UINT32 insn) /* CMP Rd, Hs */
668{
669   UINT32 rn, rs, rd;
670 
671   rs = GET_REGISTER(cpustate, ((insn & THUMB_HIREG_RS) >> THUMB_HIREG_RS_SHIFT) + 8);
672   rd = GET_REGISTER(cpustate, insn & THUMB_HIREG_RD);
673   rn = rd - rs;
674   HandleThumbALUSubFlags(rn, rd, rs);
675
676}
677
678const void tg04_01_12(arm_state *cpustate, UINT32 pc, UINT32 insn) /* CMP Hd, Rs */
679{
680   UINT32 rn, rs, rd;
681 
682   rs = GET_REGISTER(cpustate, ((insn & THUMB_HIREG_RS) >> THUMB_HIREG_RS_SHIFT));
683   rd = GET_REGISTER(cpustate, (insn & THUMB_HIREG_RD) + 8);
684   rn = rd - rs;
685   HandleThumbALUSubFlags(rn, rd, rs);
686
687}
688
689const void tg04_01_13(arm_state *cpustate, UINT32 pc, UINT32 insn) /* CMP Hd, Hs */
690{
691   UINT32 rn, rs, rd;
692 
693   rs = GET_REGISTER(cpustate, ((insn & THUMB_HIREG_RS) >> THUMB_HIREG_RS_SHIFT) + 8);
694   rd = GET_REGISTER(cpustate, (insn & THUMB_HIREG_RD) + 8);
695   rn = rd - rs;
696   HandleThumbALUSubFlags(rn, rd, rs);
697
698}
699
700 /* MOV group */
701
702// "The action of H1 = 0, H2 = 0 for Op = 00 (ADD), Op = 01 (CMP) and Op = 10 (MOV) is undefined, and should not be used."
703const void tg04_01_20(arm_state *cpustate, UINT32 pc, UINT32 insn) /* MOV Rd, Rs (undefined) */
704{
705   UINT32 rs, rd;
706     
707   
708   rs = (insn & THUMB_HIREG_RS) >> THUMB_HIREG_RS_SHIFT;
709   rd = insn & THUMB_HIREG_RD;
710   SET_REGISTER(cpustate, rd, GET_REGISTER(cpustate, rs));
711   R15 += 2;
712
713}
714
715const void tg04_01_21(arm_state *cpustate, UINT32 pc, UINT32 insn) /* MOV Rd, Hs */
716{
717   UINT32 rs, rd;
718       
719   rs = (insn & THUMB_HIREG_RS) >> THUMB_HIREG_RS_SHIFT;
720   rd = insn & THUMB_HIREG_RD;
721   if (rs == 7)
722   {
723      SET_REGISTER(cpustate, rd, GET_REGISTER(cpustate, rs + 8) + 4);
724   }
725   else
726   {
727      SET_REGISTER(cpustate, rd, GET_REGISTER(cpustate, rs + 8));
728   }
729   R15 += 2;
730
731}
732
733const void tg04_01_22(arm_state *cpustate, UINT32 pc, UINT32 insn) /* MOV Hd, Rs */
734{
735   UINT32 rs, rd;
736 
737   rs = (insn & THUMB_HIREG_RS) >> THUMB_HIREG_RS_SHIFT;
738   rd = insn & THUMB_HIREG_RD;
739   SET_REGISTER(cpustate, rd + 8, GET_REGISTER(cpustate, rs));
740   if (rd != 7)
741   {
742      R15 += 2;
743   }
744   else
745   {
746      R15 &= ~1;
747   }
748
749}
750
751const void tg04_01_23(arm_state *cpustate, UINT32 pc, UINT32 insn) /* MOV Hd, Hs */
752{
753   UINT32 rs, rd;
754 
755   rs = (insn & THUMB_HIREG_RS) >> THUMB_HIREG_RS_SHIFT;
756   rd = insn & THUMB_HIREG_RD;
757   if (rs == 7)
758   {
759      SET_REGISTER(cpustate, rd + 8, GET_REGISTER(cpustate, rs+8)+4);
760   }
761   else
762   {
763      SET_REGISTER(cpustate, rd + 8, GET_REGISTER(cpustate, rs+8));
764   }
765   if (rd != 7)
766   {
767      R15 += 2;
768   }
769   if (rd == 7)
770   {
771      R15 &= ~1;
772   }
773
774}
775
776
777const void tg04_01_30(arm_state *cpustate, UINT32 pc, UINT32 insn)
778{
779   UINT32 addr;
780   UINT32 rd;
781
782
783   rd = (insn & THUMB_HIREG_RS) >> THUMB_HIREG_RS_SHIFT;
784   addr = GET_REGISTER(cpustate, rd);
785   if (addr & 1)
786   {
787      addr &= ~1;
788   }
789   else
790   {
791      SET_CPSR(GET_CPSR & ~T_MASK);
792      if (addr & 2)
793      {
794         addr += 2;
795      }
796   }
797   R15 = addr;
798
799}
800
801const void tg04_01_31(arm_state *cpustate, UINT32 pc, UINT32 insn)
802{
803   UINT32 addr;
804
805
806   addr = GET_REGISTER(cpustate, ((insn & THUMB_HIREG_RS) >> THUMB_HIREG_RS_SHIFT) + 8);
807   if ((((insn & THUMB_HIREG_RS) >> THUMB_HIREG_RS_SHIFT) + 8) == 15)
808   {
809      addr += 2;
810   }
811   if (addr & 1)
812   {
813      addr &= ~1;
814   }
815   else
816   {
817      SET_CPSR(GET_CPSR & ~T_MASK);
818      if (addr & 2)
819      {
820         addr += 2;
821      }
822   }
823   R15 = addr;
824
825}
826
827const void tg04_01_32(arm_state *cpustate, UINT32 pc, UINT32 insn)
828{
829//   UINT32 addr;
830//   UINT32 rd;
831
832
833   fatalerror("%08x: G4-3 Undefined Thumb instruction: %04x\n", pc, insn);
834   R15 += 2;
835
836}
837
838const void tg04_01_33(arm_state *cpustate, UINT32 pc, UINT32 insn)
839{
840//   UINT32 addr;
841//   UINT32 rd;
842
843
844   fatalerror("%08x: G4-3 Undefined Thumb instruction: %04x\n", pc, insn);
845   R15 += 2;
846
847}
848
849
850
851
852
853
854const void tg04_0203(arm_state *cpustate, UINT32 pc, UINT32 insn)
855{
856   UINT32 readword;
857
858   readword = READ32((R15 & ~2) + 4 + ((insn & THUMB_INSN_IMM) << 2));
859   SET_REGISTER(cpustate, (insn & THUMB_INSN_IMM_RD) >> THUMB_INSN_IMM_RD_SHIFT, readword);
860   R15 += 2;
861}
862
863/* LDR* STR* group */
864
865const void tg05_0(arm_state *cpustate, UINT32 pc, UINT32 insn)  /* STR Rd, [Rn, Rm] */
866{
867   UINT32 addr;
868   UINT32 rm, rn, rd;
869 
870    rm = (insn & THUMB_GROUP5_RM) >> THUMB_GROUP5_RM_SHIFT;
871    rn = (insn & THUMB_GROUP5_RN) >> THUMB_GROUP5_RN_SHIFT;
872    rd = (insn & THUMB_GROUP5_RD) >> THUMB_GROUP5_RD_SHIFT;
873    addr = GET_REGISTER(cpustate, rn) + GET_REGISTER(cpustate, rm);
874    WRITE32(addr, GET_REGISTER(cpustate, rd));
875    R15 += 2;
876 
877}
878
879const void tg05_1(arm_state *cpustate, UINT32 pc, UINT32 insn)  /* STRH Rd, [Rn, Rm] */
880{
881   UINT32 addr;
882   UINT32 rm, rn, rd;
883 
884    rm = (insn & THUMB_GROUP5_RM) >> THUMB_GROUP5_RM_SHIFT;
885    rn = (insn & THUMB_GROUP5_RN) >> THUMB_GROUP5_RN_SHIFT;
886    rd = (insn & THUMB_GROUP5_RD) >> THUMB_GROUP5_RD_SHIFT;
887    addr = GET_REGISTER(cpustate, rn) + GET_REGISTER(cpustate, rm);
888    WRITE16(addr, GET_REGISTER(cpustate, rd));
889    R15 += 2;
890
891}
892
893const void tg05_2(arm_state *cpustate, UINT32 pc, UINT32 insn)  /* STRB Rd, [Rn, Rm] */
894{
895   UINT32 addr;
896   UINT32 rm, rn, rd;
897 
898    rm = (insn & THUMB_GROUP5_RM) >> THUMB_GROUP5_RM_SHIFT;
899    rn = (insn & THUMB_GROUP5_RN) >> THUMB_GROUP5_RN_SHIFT;
900    rd = (insn & THUMB_GROUP5_RD) >> THUMB_GROUP5_RD_SHIFT;
901    addr = GET_REGISTER(cpustate, rn) + GET_REGISTER(cpustate, rm);
902    WRITE8(addr, GET_REGISTER(cpustate, rd));
903    R15 += 2;
904
905}
906
907const void tg05_3(arm_state *cpustate, UINT32 pc, UINT32 insn)  /* LDSB Rd, [Rn, Rm] todo, add dasm */
908{
909   UINT32 addr;
910   UINT32 rm, rn, rd, op2;
911 
912    rm = (insn & THUMB_GROUP5_RM) >> THUMB_GROUP5_RM_SHIFT;
913    rn = (insn & THUMB_GROUP5_RN) >> THUMB_GROUP5_RN_SHIFT;
914    rd = (insn & THUMB_GROUP5_RD) >> THUMB_GROUP5_RD_SHIFT;
915    addr = GET_REGISTER(cpustate, rn) + GET_REGISTER(cpustate, rm);
916    op2 = READ8(addr);
917    if (op2 & 0x00000080)
101918    {
102        switch ((insn & THUMB_ADDSUB_TYPE) >> THUMB_ADDSUB_TYPE_SHIFT)
919        op2 |= 0xffffff00;
920    }
921    SET_REGISTER(cpustate, rd, op2);
922    R15 += 2;
923
924}
925
926const void tg05_4(arm_state *cpustate, UINT32 pc, UINT32 insn)  /* LDR Rd, [Rn, Rm] */
927{
928   UINT32 addr;
929   UINT32 rm, rn, rd, op2;
930 
931    rm = (insn & THUMB_GROUP5_RM) >> THUMB_GROUP5_RM_SHIFT;
932    rn = (insn & THUMB_GROUP5_RN) >> THUMB_GROUP5_RN_SHIFT;
933    rd = (insn & THUMB_GROUP5_RD) >> THUMB_GROUP5_RD_SHIFT;
934    addr = GET_REGISTER(cpustate, rn) + GET_REGISTER(cpustate, rm);
935    op2 = READ32(addr);
936    SET_REGISTER(cpustate, rd, op2);
937    R15 += 2;
938
939}
940
941const void tg05_5(arm_state *cpustate, UINT32 pc, UINT32 insn)  /* LDRH Rd, [Rn, Rm] */
942{
943   UINT32 addr;
944   UINT32 rm, rn, rd, op2;
945 
946    rm = (insn & THUMB_GROUP5_RM) >> THUMB_GROUP5_RM_SHIFT;
947    rn = (insn & THUMB_GROUP5_RN) >> THUMB_GROUP5_RN_SHIFT;
948    rd = (insn & THUMB_GROUP5_RD) >> THUMB_GROUP5_RD_SHIFT;
949    addr = GET_REGISTER(cpustate, rn) + GET_REGISTER(cpustate, rm);
950    op2 = READ16(addr);
951    SET_REGISTER(cpustate, rd, op2);
952    R15 += 2;
953
954}
955
956const void tg05_6(arm_state *cpustate, UINT32 pc, UINT32 insn)  /* LDRB Rd, [Rn, Rm] */
957{
958   UINT32 addr;
959   UINT32 rm, rn, rd, op2;
960 
961    rm = (insn & THUMB_GROUP5_RM) >> THUMB_GROUP5_RM_SHIFT;
962    rn = (insn & THUMB_GROUP5_RN) >> THUMB_GROUP5_RN_SHIFT;
963    rd = (insn & THUMB_GROUP5_RD) >> THUMB_GROUP5_RD_SHIFT;
964    addr = GET_REGISTER(cpustate, rn) + GET_REGISTER(cpustate, rm);
965    op2 = READ8(addr);
966    SET_REGISTER(cpustate, rd, op2);
967    R15 += 2;
968
969}
970
971const void tg05_7(arm_state *cpustate, UINT32 pc, UINT32 insn)  /* LDSH Rd, [Rn, Rm] */
972{
973   UINT32 addr;
974   UINT32 rm, rn, rd, op2;
975 
976    rm = (insn & THUMB_GROUP5_RM) >> THUMB_GROUP5_RM_SHIFT;
977    rn = (insn & THUMB_GROUP5_RN) >> THUMB_GROUP5_RN_SHIFT;
978    rd = (insn & THUMB_GROUP5_RD) >> THUMB_GROUP5_RD_SHIFT;
979    addr = GET_REGISTER(cpustate, rn) + GET_REGISTER(cpustate, rm);
980    op2 = READ16(addr);
981    if (op2 & 0x00008000)
982    {
983        op2 |= 0xffff0000;
984    }
985    SET_REGISTER(cpustate, rd, op2);
986    R15 += 2;
987
988}
989
990 /* Word Store w/ Immediate Offset */
991
992const void tg06_0(arm_state *cpustate, UINT32 pc, UINT32 insn) /* Store */
993{
994   UINT32 rn, rd;
995   INT32 offs;
996
997   rn = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
998   rd = insn & THUMB_ADDSUB_RD;
999   offs = ((insn & THUMB_LSOP_OFFS) >> THUMB_LSOP_OFFS_SHIFT) << 2;
1000   WRITE32(GET_REGISTER(cpustate, rn) + offs, GET_REGISTER(cpustate, rd));
1001   R15 += 2;
1002}
1003
1004const void tg06_1(arm_state *cpustate, UINT32 pc, UINT32 insn) /* Load */
1005{
1006   UINT32 rn, rd;
1007   INT32 offs;
1008
1009   rn = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
1010   rd = insn & THUMB_ADDSUB_RD;
1011   offs = ((insn & THUMB_LSOP_OFFS) >> THUMB_LSOP_OFFS_SHIFT) << 2;
1012   SET_REGISTER(cpustate, rd, READ32(GET_REGISTER(cpustate, rn) + offs)); // fix
1013   R15 += 2;
1014}
1015
1016/* Byte Store w/ Immeidate Offset */
1017
1018const void tg07_0(arm_state *cpustate, UINT32 pc, UINT32 insn) /* Store */
1019{
1020   UINT32 rn, rd;
1021   INT32 offs;
1022
1023   rn = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
1024   rd = insn & THUMB_ADDSUB_RD;
1025   offs = (insn & THUMB_LSOP_OFFS) >> THUMB_LSOP_OFFS_SHIFT;
1026   WRITE8(GET_REGISTER(cpustate, rn) + offs, GET_REGISTER(cpustate, rd));
1027   R15 += 2;
1028}
1029
1030const void tg07_1(arm_state *cpustate, UINT32 pc, UINT32 insn)  /* Load */
1031{
1032   UINT32 rn, rd;
1033   INT32 offs;
1034
1035   rn = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
1036   rd = insn & THUMB_ADDSUB_RD;
1037   offs = (insn & THUMB_LSOP_OFFS) >> THUMB_LSOP_OFFS_SHIFT;
1038   SET_REGISTER(cpustate, rd, READ8(GET_REGISTER(cpustate, rn) + offs));
1039   R15 += 2;
1040}
1041
1042/* Load/Store Halfword */
1043
1044const void tg08_0(arm_state *cpustate, UINT32 pc, UINT32 insn) /* Store */
1045{
1046   UINT32 rs, rd, imm;
1047
1048   imm = (insn & THUMB_HALFOP_OFFS) >> THUMB_HALFOP_OFFS_SHIFT;
1049   rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
1050   rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
1051   WRITE16(GET_REGISTER(cpustate, rs) + (imm << 1), GET_REGISTER(cpustate, rd));
1052   R15 += 2;
1053}
1054
1055const void tg08_1(arm_state *cpustate, UINT32 pc, UINT32 insn) /* Load */
1056{
1057   UINT32 rs, rd, imm;
1058
1059   imm = (insn & THUMB_HALFOP_OFFS) >> THUMB_HALFOP_OFFS_SHIFT;
1060   rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
1061   rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
1062   SET_REGISTER(cpustate, rd, READ16(GET_REGISTER(cpustate, rs) + (imm << 1)));
1063   R15 += 2;
1064}
1065
1066 /* Stack-Relative Load/Store */
1067
1068const void tg09_0(arm_state *cpustate, UINT32 pc, UINT32 insn) /* Store */
1069{
1070   UINT32 rd;
1071   INT32 offs;
1072
1073   rd = (insn & THUMB_STACKOP_RD) >> THUMB_STACKOP_RD_SHIFT;
1074   offs = (UINT8)(insn & THUMB_INSN_IMM);
1075   WRITE32(GET_REGISTER(cpustate, 13) + ((UINT32)offs << 2), GET_REGISTER(cpustate, rd));
1076   R15 += 2;
1077}
1078
1079const void tg09_1(arm_state *cpustate, UINT32 pc, UINT32 insn) /* Load */
1080{
1081   UINT32 readword;
1082   UINT32 rd;
1083   INT32 offs;
1084
1085   rd = (insn & THUMB_STACKOP_RD) >> THUMB_STACKOP_RD_SHIFT;
1086   offs = (UINT8)(insn & THUMB_INSN_IMM);
1087   readword = READ32(GET_REGISTER(cpustate, 13) + ((UINT32)offs << 2));
1088   SET_REGISTER(cpustate, rd, readword);
1089   R15 += 2;
1090}
1091
1092 /* Get relative address */
1093
1094const void tg0a_0(arm_state *cpustate, UINT32 pc, UINT32 insn)  /* ADD Rd, PC, #nn */
1095{
1096   UINT32 rd;
1097   INT32 offs;
1098
1099    rd = (insn & THUMB_RELADDR_RD) >> THUMB_RELADDR_RD_SHIFT;
1100    offs = (UINT8)(insn & THUMB_INSN_IMM) << 2;
1101    SET_REGISTER(cpustate, rd, ((R15 + 4) & ~2) + offs);
1102    R15 += 2;
1103}
1104
1105const void tg0a_1(arm_state *cpustate, UINT32 pc, UINT32 insn) /* ADD Rd, SP, #nn */
1106{
1107   UINT32 rd;
1108   INT32 offs;
1109
1110   rd = (insn & THUMB_RELADDR_RD) >> THUMB_RELADDR_RD_SHIFT;
1111   offs = (UINT8)(insn & THUMB_INSN_IMM) << 2;
1112   SET_REGISTER(cpustate, rd, GET_REGISTER(cpustate, 13) + offs);
1113   R15 += 2;
1114}
1115
1116 /* Stack-Related Opcodes */
1117
1118const void tg0b_0(arm_state *cpustate, UINT32 pc, UINT32 insn) /* ADD SP, #imm */
1119{
1120   UINT32 addr;
1121
1122 
1123    addr = (insn & THUMB_INSN_IMM);
1124    addr &= ~THUMB_INSN_IMM_S;
1125    SET_REGISTER(cpustate, 13, GET_REGISTER(cpustate, 13) + ((insn & THUMB_INSN_IMM_S) ? -(addr << 2) : (addr << 2)));
1126    R15 += 2;
1127
1128}
1129
1130const void tg0b_1(arm_state *cpustate, UINT32 pc, UINT32 insn)
1131{
1132//   UINT32 addr;
1133//   INT32 offs;
1134
1135    fatalerror("%08x: Gb Undefined Thumb instruction: %04x\n", pc, insn);
1136    R15 += 2;
1137
1138}
1139
1140const void tg0b_2(arm_state *cpustate, UINT32 pc, UINT32 insn)
1141{
1142//   UINT32 addr;
1143//   INT32 offs;
1144
1145    fatalerror("%08x: Gb Undefined Thumb instruction: %04x\n", pc, insn);
1146    R15 += 2;
1147
1148}
1149
1150const void tg0b_3(arm_state *cpustate, UINT32 pc, UINT32 insn)
1151{
1152//   UINT32 addr;
1153//   INT32 offs;
1154
1155    fatalerror("%08x: Gb Undefined Thumb instruction: %04x\n", pc, insn);
1156    R15 += 2;
1157
1158}
1159
1160const void tg0b_4(arm_state *cpustate, UINT32 pc, UINT32 insn) /* PUSH {Rlist} */
1161{
1162   INT32 offs;
1163 
1164    for (offs = 7; offs >= 0; offs--)
1165    {
1166        if (insn & (1 << offs))
1031167        {
104            case 0x0: /* ADD Rd, Rs, Rn */
105                rn = GET_REGISTER(cpustate, (insn & THUMB_ADDSUB_RNIMM) >> THUMB_ADDSUB_RNIMM_SHIFT);
106                rs = GET_REGISTER(cpustate, (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT);
107                rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
108                SET_REGISTER(cpustate, rd, rs + rn);
109                HandleThumbALUAddFlags(GET_REGISTER(cpustate, rd), rs, rn);
110                break;
111            case 0x1: /* SUB Rd, Rs, Rn */
112                rn = GET_REGISTER(cpustate, (insn & THUMB_ADDSUB_RNIMM) >> THUMB_ADDSUB_RNIMM_SHIFT);
113                rs = GET_REGISTER(cpustate, (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT);
114                rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
115                SET_REGISTER(cpustate, rd, rs - rn);
116                HandleThumbALUSubFlags(GET_REGISTER(cpustate, rd), rs, rn);
117                break;
118            case 0x2: /* ADD Rd, Rs, #imm */
119                imm = (insn & THUMB_ADDSUB_RNIMM) >> THUMB_ADDSUB_RNIMM_SHIFT;
120                rs = GET_REGISTER(cpustate, (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT);
121                rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
122                SET_REGISTER(cpustate, rd, rs + imm);
123                HandleThumbALUAddFlags(GET_REGISTER(cpustate, rd), rs, imm);
124                break;
125            case 0x3: /* SUB Rd, Rs, #imm */
126                imm = (insn & THUMB_ADDSUB_RNIMM) >> THUMB_ADDSUB_RNIMM_SHIFT;
127                rs = GET_REGISTER(cpustate, (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT);
128                rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
129                SET_REGISTER(cpustate, rd, rs - imm);
130                HandleThumbALUSubFlags(GET_REGISTER(cpustate, rd), rs,imm);
131                break;
132            default:
133                fatalerror("%08x: G1 Undefined Thumb instruction: %04x\n", pc, insn);
134                R15 += 2;
135                break;
1168            SET_REGISTER(cpustate, 13, GET_REGISTER(cpustate, 13) - 4);
1169            WRITE32(GET_REGISTER(cpustate, 13), GET_REGISTER(cpustate, offs));
1361170        }
1371171    }
138    else
1172    R15 += 2;
1173
1174}
1175
1176const void tg0b_5(arm_state *cpustate, UINT32 pc, UINT32 insn) /* PUSH {Rlist}{LR} */
1177{
1178   INT32 offs;
1179 
1180    SET_REGISTER(cpustate, 13, GET_REGISTER(cpustate, 13) - 4);
1181    WRITE32(GET_REGISTER(cpustate, 13), GET_REGISTER(cpustate, 14));
1182    for (offs = 7; offs >= 0; offs--)
1391183    {
140        /* ASR.. */
141        //if (insn & THUMB_SHIFT_R) /* Shift right */
1184        if (insn & (1 << offs))
1421185        {
143            rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
144            rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
145            rrs = GET_REGISTER(cpustate, rs);
146            offs = (insn & THUMB_SHIFT_AMT) >> THUMB_SHIFT_AMT_SHIFT;
147            if (offs == 0)
148            {
149                offs = 32;
150            }
151            if (offs >= 32)
152            {
153                if (rrs >> 31)
154                {
155                    SET_CPSR(GET_CPSR | C_MASK);
156                }
157                else
158                {
159                    SET_CPSR(GET_CPSR & ~C_MASK);
160                }
161                SET_REGISTER(cpustate, rd, (rrs & 0x80000000) ? 0xFFFFFFFF : 0x00000000);
162            }
163            else
164            {
165                if ((rrs >> (offs - 1)) & 1)
166                {
167                    SET_CPSR(GET_CPSR | C_MASK);
168                }
169                else
170                {
171                    SET_CPSR(GET_CPSR & ~C_MASK);
172                }
173                SET_REGISTER(cpustate, rd,
174                                (rrs & 0x80000000)
175                                ? ((0xFFFFFFFF << (32 - offs)) | (rrs >> offs))
176                                : (rrs >> offs));
177            }
178            SET_CPSR(GET_CPSR & ~(N_MASK | Z_MASK));
179            SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(cpustate, rd)));
180            R15 += 2;
1186            SET_REGISTER(cpustate, 13, GET_REGISTER(cpustate, 13) - 4);
1187            WRITE32(GET_REGISTER(cpustate, 13), GET_REGISTER(cpustate, offs));
1811188        }
1821189    }
1190    R15 += 2;
1191
1831192}
1841193
185const void thumbblock02(arm_state *cpustate, UINT32 pc, UINT32 insn) /* CMP / MOV */
1194const void tg0b_6(arm_state *cpustate, UINT32 pc, UINT32 insn)
1861195{
187   UINT32 rn, rd, op2;
1196//   UINT32 addr;
1197//   INT32 offs;
1881198
189    if (insn & THUMB_INSN_CMP)
1199    fatalerror("%08x: Gb Undefined Thumb instruction: %04x\n", pc, insn);
1200    R15 += 2;
1201
1202}
1203
1204const void tg0b_7(arm_state *cpustate, UINT32 pc, UINT32 insn)
1205{
1206//   UINT32 addr;
1207//   INT32 offs;
1208
1209    fatalerror("%08x: Gb Undefined Thumb instruction: %04x\n", pc, insn);
1210    R15 += 2;
1211
1212}
1213
1214const void tg0b_8(arm_state *cpustate, UINT32 pc, UINT32 insn)
1215{
1216//   UINT32 addr;
1217//   INT32 offs;
1218
1219    fatalerror("%08x: Gb Undefined Thumb instruction: %04x\n", pc, insn);
1220    R15 += 2;
1221
1222}
1223
1224const void tg0b_9(arm_state *cpustate, UINT32 pc, UINT32 insn)
1225{
1226//   UINT32 addr;
1227//   INT32 offs;
1228
1229    fatalerror("%08x: Gb Undefined Thumb instruction: %04x\n", pc, insn);
1230    R15 += 2;
1231
1232}
1233
1234const void tg0b_a(arm_state *cpustate, UINT32 pc, UINT32 insn)
1235{
1236//   UINT32 addr;
1237//   INT32 offs;
1238
1239    fatalerror("%08x: Gb Undefined Thumb instruction: %04x\n", pc, insn);
1240    R15 += 2;
1241
1242}
1243
1244const void tg0b_b(arm_state *cpustate, UINT32 pc, UINT32 insn)
1245{
1246//   UINT32 addr;
1247//   INT32 offs;
1248
1249    fatalerror("%08x: Gb Undefined Thumb instruction: %04x\n", pc, insn);
1250    R15 += 2;
1251
1252}
1253
1254const void tg0b_c(arm_state *cpustate, UINT32 pc, UINT32 insn) /* POP {Rlist} */
1255{
1256   INT32 offs;
1257 
1258    for (offs = 0; offs < 8; offs++)
1901259    {
191        rn = GET_REGISTER(cpustate, (insn & THUMB_INSN_IMM_RD) >> THUMB_INSN_IMM_RD_SHIFT);
192        op2 = insn & THUMB_INSN_IMM;
193        rd = rn - op2;
194        HandleThumbALUSubFlags(rd, rn, op2);
195        //mame_printf_debug("%08x: xxx Thumb instruction: CMP R%d (%08x), %02x (N=%d, Z=%d, C=%d, V=%d)\n", pc, (insn & THUMB_INSN_IMM_RD) >> THUMB_INSN_IMM_RD_SHIFT, GET_REGISTER(cpustate, (insn & THUMB_INSN_IMM_RD) >> THUMB_INSN_IMM_RD_SHIFT), op2, N_IS_SET(GET_CPSR) ? 1 : 0, Z_IS_SET(GET_CPSR) ? 1 : 0, C_IS_SET(GET_CPSR) ? 1 : 0, V_IS_SET(GET_CPSR) ? 1 : 0);
1260        if (insn & (1 << offs))
1261        {
1262            SET_REGISTER(cpustate, offs, READ32(GET_REGISTER(cpustate, 13)));
1263            SET_REGISTER(cpustate, 13, GET_REGISTER(cpustate, 13) + 4);
1264        }
1961265    }
197    else
1266    R15 += 2;
1267
1268}
1269
1270const void tg0b_d(arm_state *cpustate, UINT32 pc, UINT32 insn) /* POP {Rlist}{PC} */
1271{
1272   INT32 offs;
1273 
1274    for (offs = 0; offs < 8; offs++)
1981275    {
199        rd = (insn & THUMB_INSN_IMM_RD) >> THUMB_INSN_IMM_RD_SHIFT;
200        op2 = (insn & THUMB_INSN_IMM);
201        SET_REGISTER(cpustate, rd, op2);
202        SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
203        SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(cpustate, rd)));
204        R15 += 2;
1276        if (insn & (1 << offs))
1277        {
1278            SET_REGISTER(cpustate, offs, READ32(GET_REGISTER(cpustate, 13)));
1279            SET_REGISTER(cpustate, 13, GET_REGISTER(cpustate, 13) + 4);
1280        }
2051281    }
1282    R15 = READ32(GET_REGISTER(cpustate, 13)) & ~1;
1283    SET_REGISTER(cpustate, 13, GET_REGISTER(cpustate, 13) + 4);
1284
2061285}
2071286
208const void thumbblock03(arm_state *cpustate, UINT32 pc, UINT32 insn) /* ADD/SUB immediate */
1287const void tg0b_e(arm_state *cpustate, UINT32 pc, UINT32 insn)
2091288{
210   UINT32 rn, rd, op2;
1289//   UINT32 addr;
1290//   INT32 offs;
2111291
212    if (insn & THUMB_INSN_SUB) /* SUB Rd, #Offset8 */
1292    fatalerror("%08x: Gb Undefined Thumb instruction: %04x\n", pc, insn);
1293    R15 += 2;
1294
1295}
1296
1297const void tg0b_f(arm_state *cpustate, UINT32 pc, UINT32 insn)
1298{
1299//   UINT32 addr;
1300//   INT32 offs;
1301
1302    fatalerror("%08x: Gb Undefined Thumb instruction: %04x\n", pc, insn);
1303    R15 += 2;
1304
1305}
1306
1307/* Multiple Load/Store */
1308
1309// "The address should normally be a word aligned quantity and non-word aligned addresses do not affect the instruction."
1310// "However, the bottom 2 bits of the address will appear on A[1:0] and might be interpreted by the memory system."
1311
1312// GBA "BB Ball" performs an unaligned read with A[1:0] = 2 and expects A[1] not to be ignored [BP 800B90A,(R4&3)!=0]
1313// GBA "Gadget Racers" performs an unaligned read with A[1:0] = 1 and expects A[0] to be ignored [BP B72,(R0&3)!=0]
1314
1315const void tg0c_0(arm_state *cpustate, UINT32 pc, UINT32 insn) /* Store */
1316{
1317   UINT32 rd;
1318   INT32 offs;
1319
1320    UINT32 ld_st_address;
1321
1322    rd = (insn & THUMB_MULTLS_BASE) >> THUMB_MULTLS_BASE_SHIFT;
1323
1324
1325    ld_st_address = GET_REGISTER(cpustate, rd);
1326
1327    for (offs = 0; offs < 8; offs++)
2131328    {
214        rn = GET_REGISTER(cpustate, (insn & THUMB_INSN_IMM_RD) >> THUMB_INSN_IMM_RD_SHIFT);
215        op2 = insn & THUMB_INSN_IMM;
216        //mame_printf_debug("%08x:  Thumb instruction: SUB R%d, %02x\n", pc, (insn & THUMB_INSN_IMM_RD) >> THUMB_INSN_IMM_RD_SHIFT, op2);
217        rd = rn - op2;
218        SET_REGISTER(cpustate, (insn & THUMB_INSN_IMM_RD) >> THUMB_INSN_IMM_RD_SHIFT, rd);
219        HandleThumbALUSubFlags(rd, rn, op2);
1329        if (insn & (1 << offs))
1330        {
1331            WRITE32(ld_st_address & ~3, GET_REGISTER(cpustate, offs));
1332            ld_st_address += 4;
1333        }
2201334    }
221    else /* ADD Rd, #Offset8 */
1335    SET_REGISTER(cpustate, rd, ld_st_address);
1336    R15 += 2;
1337}
1338
1339
1340const void tg0c_1(arm_state *cpustate, UINT32 pc, UINT32 insn) /* Load */
1341{
1342   UINT32 rd;
1343   INT32 offs;
1344
1345    UINT32 ld_st_address;
1346
1347    rd = (insn & THUMB_MULTLS_BASE) >> THUMB_MULTLS_BASE_SHIFT;
1348
1349
1350    ld_st_address = GET_REGISTER(cpustate, rd);
1351
1352
1353    int rd_in_list;
1354
1355    rd_in_list = insn & (1 << rd);
1356    for (offs = 0; offs < 8; offs++)
2221357    {
223        rn = GET_REGISTER(cpustate, (insn & THUMB_INSN_IMM_RD) >> THUMB_INSN_IMM_RD_SHIFT);
224        op2 = insn & THUMB_INSN_IMM;
225        rd = rn + op2;
226        //mame_printf_debug("%08x:  Thumb instruction: ADD R%d, %02x\n", pc, (insn & THUMB_INSN_IMM_RD) >> THUMB_INSN_IMM_RD_SHIFT, op2);
227        SET_REGISTER(cpustate, (insn & THUMB_INSN_IMM_RD) >> THUMB_INSN_IMM_RD_SHIFT, rd);
228        HandleThumbALUAddFlags(rd, rn, op2);
1358        if (insn & (1 << offs))
1359        {
1360            SET_REGISTER(cpustate, offs, READ32(ld_st_address & ~1));
1361            ld_st_address += 4;
1362        }
2291363    }
1364   
1365   if (!rd_in_list)
1366        SET_REGISTER(cpustate, rd, ld_st_address);
1367   
1368   R15 += 2;
2301369}
2311370
232const void thumbblock04(arm_state *cpustate, UINT32 pc, UINT32 insn) /* Rd & Rm instructions */
1371/* Conditional Branch */
1372
1373const void tg0d_0(arm_state *cpustate, UINT32 pc, UINT32 insn) // COND_EQ:
2331374{
234   UINT32 readword, addr;
235   UINT32 rn, rs, rd, op2, imm, rrs, rrd;
2361375   INT32 offs;
2371376
238    switch ((insn & THUMB_GROUP4_TYPE) >> THUMB_GROUP4_TYPE_SHIFT)
1377    offs = (INT8)(insn & THUMB_INSN_IMM);
1378//case
1379    if (Z_IS_SET(GET_CPSR))
2391380    {
240        case 0x0:
241            switch ((insn & THUMB_ALUOP_TYPE) >> THUMB_ALUOP_TYPE_SHIFT)
242            {
243                case 0x0: /* AND Rd, Rs */
244                    rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
245                    rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
246                    SET_REGISTER(cpustate, rd, GET_REGISTER(cpustate, rd) & GET_REGISTER(cpustate, rs));
247                    SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
248                    SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(cpustate, rd)));
249                    R15 += 2;
250                    break;
251                case 0x1: /* EOR Rd, Rs */
252                    rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
253                    rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
254                    SET_REGISTER(cpustate, rd, GET_REGISTER(cpustate, rd) ^ GET_REGISTER(cpustate, rs));
255                    SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
256                    SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(cpustate, rd)));
257                    R15 += 2;
258                    break;
259                case 0x2: /* LSL Rd, Rs */
260                    rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
261                    rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
262                    rrd = GET_REGISTER(cpustate, rd);
263                    offs = GET_REGISTER(cpustate, rs) & 0x000000ff;
264                    if (offs > 0)
265                    {
266                        if (offs < 32)
267                        {
268                            SET_REGISTER(cpustate, rd, rrd << offs);
269                            if (rrd & (1 << (31 - (offs - 1))))
270                            {
271                                SET_CPSR(GET_CPSR | C_MASK);
272                            }
273                            else
274                            {
275                                SET_CPSR(GET_CPSR & ~C_MASK);
276                            }
277                        }
278                        else if (offs == 32)
279                        {
280                            SET_REGISTER(cpustate, rd, 0);
281                            if (rrd & 1)
282                            {
283                                SET_CPSR(GET_CPSR | C_MASK);
284                            }
285                            else
286                            {
287                                SET_CPSR(GET_CPSR & ~C_MASK);
288                            }
289                        }
290                        else
291                        {
292                            SET_REGISTER(cpustate, rd, 0);
293                            SET_CPSR(GET_CPSR & ~C_MASK);
294                        }
295                    }
296                    SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
297                    SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(cpustate, rd)));
298                    R15 += 2;
299                    break;
300                case 0x3: /* LSR Rd, Rs */
301                    rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
302                    rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
303                    rrd = GET_REGISTER(cpustate, rd);
304                    offs = GET_REGISTER(cpustate, rs) & 0x000000ff;
305                    if (offs >  0)
306                    {
307                        if (offs < 32)
308                        {
309                            SET_REGISTER(cpustate, rd, rrd >> offs);
310                            if (rrd & (1 << (offs - 1)))
311                            {
312                                SET_CPSR(GET_CPSR | C_MASK);
313                            }
314                            else
315                            {
316                                SET_CPSR(GET_CPSR & ~C_MASK);
317                            }
318                        }
319                        else if (offs == 32)
320                        {
321                            SET_REGISTER(cpustate, rd, 0);
322                            if (rrd & 0x80000000)
323                            {
324                                SET_CPSR(GET_CPSR | C_MASK);
325                            }
326                            else
327                            {
328                                SET_CPSR(GET_CPSR & ~C_MASK);
329                            }
330                        }
331                        else
332                        {
333                            SET_REGISTER(cpustate, rd, 0);
334                            SET_CPSR(GET_CPSR & ~C_MASK);
335                        }
336                    }
337                    SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
338                    SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(cpustate, rd)));
339                    R15 += 2;
340                    break;
341                case 0x4: /* ASR Rd, Rs */
342                    rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
343                    rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
344                    rrs = GET_REGISTER(cpustate, rs)&0xff;
345                    rrd = GET_REGISTER(cpustate, rd);
346                    if (rrs != 0)
347                    {
348                        if (rrs >= 32)
349                        {
350                            if (rrd >> 31)
351                            {
352                                SET_CPSR(GET_CPSR | C_MASK);
353                            }
354                            else
355                            {
356                                SET_CPSR(GET_CPSR & ~C_MASK);
357                            }
358                            SET_REGISTER(cpustate, rd, (GET_REGISTER(cpustate, rd) & 0x80000000) ? 0xFFFFFFFF : 0x00000000);
359                        }
360                        else
361                        {
362                            if ((rrd >> (rrs-1)) & 1)
363                            {
364                                SET_CPSR(GET_CPSR | C_MASK);
365                            }
366                            else
367                            {
368                                SET_CPSR(GET_CPSR & ~C_MASK);
369                            }
370                            SET_REGISTER(cpustate, rd, (rrd & 0x80000000)
371                                            ? ((0xFFFFFFFF << (32 - rrs)) | (rrd >> rrs))
372                                            : (rrd >> rrs));
373                        }
374                    }
375                    SET_CPSR(GET_CPSR & ~(N_MASK | Z_MASK));
376                    SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(cpustate, rd)));
377                    R15 += 2;
378                    break;
379                case 0x5: /* ADC Rd, Rs */
380                    rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
381                    rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
382                    op2=(GET_CPSR & C_MASK) ? 1 : 0;
383                    rn=GET_REGISTER(cpustate, rd) + GET_REGISTER(cpustate, rs) + op2;
384                    HandleThumbALUAddFlags(rn, GET_REGISTER(cpustate, rd), (GET_REGISTER(cpustate, rs))); // ?
385                    SET_REGISTER(cpustate, rd, rn);
386                    break;
387                case 0x6: /* SBC Rd, Rs */
388                    rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
389                    rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
390                    op2=(GET_CPSR & C_MASK) ? 0 : 1;
391                    rn=GET_REGISTER(cpustate, rd) - GET_REGISTER(cpustate, rs) - op2;
392                    HandleThumbALUSubFlags(rn, GET_REGISTER(cpustate, rd), (GET_REGISTER(cpustate, rs))); //?
393                    SET_REGISTER(cpustate, rd, rn);
394                    break;
395                case 0x7: /* ROR Rd, Rs */
396                    rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
397                    rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
398                    rrd = GET_REGISTER(cpustate, rd);
399                    imm = GET_REGISTER(cpustate, rs) & 0x0000001f;
400                    SET_REGISTER(cpustate, rd, (rrd >> imm) | (rrd << (32 - imm)));
401                    if (rrd & (1 << (imm - 1)))
402                    {
403                        SET_CPSR(GET_CPSR | C_MASK);
404                    }
405                    else
406                    {
407                        SET_CPSR(GET_CPSR & ~C_MASK);
408                    }
409                    SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
410                    SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(cpustate, rd)));
411                    R15 += 2;
412                    break;
413                case 0x8: /* TST Rd, Rs */
414                    rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
415                    rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
416                    SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
417                    SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(cpustate, rd) & GET_REGISTER(cpustate, rs)));
418                    R15 += 2;
419                    break;
420                case 0x9: /* NEG Rd, Rs */
421                    rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
422                    rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
423                    rrs = GET_REGISTER(cpustate, rs);
424                    rn = 0 - rrs;
425                    SET_REGISTER(cpustate, rd, rn);
426                    HandleThumbALUSubFlags(GET_REGISTER(cpustate, rd), 0, rrs);
427                    break;
428                case 0xa: /* CMP Rd, Rs */
429                    rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
430                    rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
431                    rn = GET_REGISTER(cpustate, rd) - GET_REGISTER(cpustate, rs);
432                    HandleThumbALUSubFlags(rn, GET_REGISTER(cpustate, rd), GET_REGISTER(cpustate, rs));
433                    break;
434                case 0xb: /* CMN Rd, Rs - check flags, add dasm */
435                    rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
436                    rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
437                    rn = GET_REGISTER(cpustate, rd) + GET_REGISTER(cpustate, rs);
438                    HandleThumbALUAddFlags(rn, GET_REGISTER(cpustate, rd), GET_REGISTER(cpustate, rs));
439                    break;
440                case 0xc: /* ORR Rd, Rs */
441                    rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
442                    rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
443                    SET_REGISTER(cpustate, rd, GET_REGISTER(cpustate, rd) | GET_REGISTER(cpustate, rs));
444                    SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
445                    SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(cpustate, rd)));
446                    R15 += 2;
447                    break;
448                case 0xd: /* MUL Rd, Rs */
449                    rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
450                    rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
451                    rn = GET_REGISTER(cpustate, rd) * GET_REGISTER(cpustate, rs);
452                    SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
453                    SET_REGISTER(cpustate, rd, rn);
454                    SET_CPSR(GET_CPSR | HandleALUNZFlags(rn));
455                    R15 += 2;
456                    break;
457                case 0xe: /* BIC Rd, Rs */
458                    rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
459                    rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
460                    SET_REGISTER(cpustate, rd, GET_REGISTER(cpustate, rd) & (~GET_REGISTER(cpustate, rs)));
461                    SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
462                    SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(cpustate, rd)));
463                    R15 += 2;
464                    break;
465                case 0xf: /* MVN Rd, Rs */
466                    rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
467                    rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
468                    op2 = GET_REGISTER(cpustate, rs);
469                    SET_REGISTER(cpustate, rd, ~op2);
470                    SET_CPSR(GET_CPSR & ~(Z_MASK | N_MASK));
471                    SET_CPSR(GET_CPSR | HandleALUNZFlags(GET_REGISTER(cpustate, rd)));
472                    R15 += 2;
473                    break;
474                default:
475                    fatalerror("%08x: G4-0 Undefined Thumb instruction: %04x %x\n", pc, insn, (insn & THUMB_ALUOP_TYPE) >> THUMB_ALUOP_TYPE_SHIFT);
476                    R15 += 2;
477                    break;
478            }
479            break;
480        case 0x1:
481            switch ((insn & THUMB_HIREG_OP) >> THUMB_HIREG_OP_SHIFT)
482            {
483                case 0x0: /* ADD Rd, Rs */
484                    rs = (insn & THUMB_HIREG_RS) >> THUMB_HIREG_RS_SHIFT;
485                    rd = insn & THUMB_HIREG_RD;
486                    switch ((insn & THUMB_HIREG_H) >> THUMB_HIREG_H_SHIFT)
487                    {
488                        case 0x1: /* ADD Rd, HRs */
489                            SET_REGISTER(cpustate, rd, GET_REGISTER(cpustate, rd) + GET_REGISTER(cpustate, rs+8));
490                            // emulate the effects of pre-fetch
491                            if (rs == 7)
492                            {
493                                SET_REGISTER(cpustate, rd, GET_REGISTER(cpustate, rd) + 4);
494                            }
495                            break;
496                        case 0x2: /* ADD HRd, Rs */
497                            SET_REGISTER(cpustate, rd+8, GET_REGISTER(cpustate, rd+8) + GET_REGISTER(cpustate, rs));
498                            if (rd == 7)
499                            {
500                                R15 += 2;
501                            }
502                            break;
503                        case 0x3: /* Add HRd, HRs */
504                            SET_REGISTER(cpustate, rd+8, GET_REGISTER(cpustate, rd+8) + GET_REGISTER(cpustate, rs+8));
505                            // emulate the effects of pre-fetch
506                            if (rs == 7)
507                            {
508                                SET_REGISTER(cpustate, rd+8, GET_REGISTER(cpustate, rd+8) + 4);
509                            }
510                            if (rd == 7)
511                            {
512                                R15 += 2;
513                            }
514                            break;
515                        default:
516                            fatalerror("%08x: G4-1-0 Undefined Thumb instruction: %04x %x\n", pc, insn, (insn & THUMB_HIREG_H) >> THUMB_HIREG_H_SHIFT);
517                            break;
518                    }
519                    R15 += 2;
520                    break;
521                case 0x1: /* CMP */
522                    switch ((insn & THUMB_HIREG_H) >> THUMB_HIREG_H_SHIFT)
523                    {
524                        case 0x0: /* CMP Rd, Rs */
525                            rs = GET_REGISTER(cpustate, ((insn & THUMB_HIREG_RS) >> THUMB_HIREG_RS_SHIFT));
526                            rd = GET_REGISTER(cpustate, insn & THUMB_HIREG_RD);
527                            rn = rd - rs;
528                            HandleThumbALUSubFlags(rn, rd, rs);
529                            break;
530                        case 0x1: /* CMP Rd, Hs */
531                            rs = GET_REGISTER(cpustate, ((insn & THUMB_HIREG_RS) >> THUMB_HIREG_RS_SHIFT) + 8);
532                            rd = GET_REGISTER(cpustate, insn & THUMB_HIREG_RD);
533                            rn = rd - rs;
534                            HandleThumbALUSubFlags(rn, rd, rs);
535                            break;
536                        case 0x2: /* CMP Hd, Rs */
537                            rs = GET_REGISTER(cpustate, ((insn & THUMB_HIREG_RS) >> THUMB_HIREG_RS_SHIFT));
538                            rd = GET_REGISTER(cpustate, (insn & THUMB_HIREG_RD) + 8);
539                            rn = rd - rs;
540                            HandleThumbALUSubFlags(rn, rd, rs);
541                            break;
542                        case 0x3: /* CMP Hd, Hs */
543                            rs = GET_REGISTER(cpustate, ((insn & THUMB_HIREG_RS) >> THUMB_HIREG_RS_SHIFT) + 8);
544                            rd = GET_REGISTER(cpustate, (insn & THUMB_HIREG_RD) + 8);
545                            rn = rd - rs;
546                            HandleThumbALUSubFlags(rn, rd, rs);
547                            break;
548                        default:
549                            fatalerror("%08x: G4-1 Undefined Thumb instruction: %04x %x\n", pc, insn, (insn & THUMB_HIREG_H) >> THUMB_HIREG_H_SHIFT);
550                            R15 += 2;
551                            break;
552                    }
553                    break;
554                case 0x2: /* MOV */
555                    switch ((insn & THUMB_HIREG_H) >> THUMB_HIREG_H_SHIFT)
556                    {
557                        case 0x0:       // MOV Rd, Rs (undefined)
558                            // "The action of H1 = 0, H2 = 0 for Op = 00 (ADD), Op = 01 (CMP) and Op = 10 (MOV) is undefined, and should not be used."
559                            rs = (insn & THUMB_HIREG_RS) >> THUMB_HIREG_RS_SHIFT;
560                            rd = insn & THUMB_HIREG_RD;
561                            SET_REGISTER(cpustate, rd, GET_REGISTER(cpustate, rs));
562                            R15 += 2;
563                            break;
564                        case 0x1:       // MOV Rd, Hs
565                            rs = (insn & THUMB_HIREG_RS) >> THUMB_HIREG_RS_SHIFT;
566                            rd = insn & THUMB_HIREG_RD;
567                            if (rs == 7)
568                            {
569                                SET_REGISTER(cpustate, rd, GET_REGISTER(cpustate, rs + 8) + 4);
570                            }
571                            else
572                            {
573                                SET_REGISTER(cpustate, rd, GET_REGISTER(cpustate, rs + 8));
574                            }
575                            R15 += 2;
576                            break;
577                        case 0x2:       // MOV Hd, Rs
578                            rs = (insn & THUMB_HIREG_RS) >> THUMB_HIREG_RS_SHIFT;
579                            rd = insn & THUMB_HIREG_RD;
580                            SET_REGISTER(cpustate, rd + 8, GET_REGISTER(cpustate, rs));
581                            if (rd != 7)
582                            {
583                                R15 += 2;
584                            }
585                            else
586                            {
587                                R15 &= ~1;
588                            }
589                            break;
590                        case 0x3:       // MOV Hd, Hs
591                            rs = (insn & THUMB_HIREG_RS) >> THUMB_HIREG_RS_SHIFT;
592                            rd = insn & THUMB_HIREG_RD;
593                            if (rs == 7)
594                            {
595                                SET_REGISTER(cpustate, rd + 8, GET_REGISTER(cpustate, rs+8)+4);
596                            }
597                            else
598                            {
599                                SET_REGISTER(cpustate, rd + 8, GET_REGISTER(cpustate, rs+8));
600                            }
601                            if (rd != 7)
602                            {
603                                R15 += 2;
604                            }
605                            if (rd == 7)
606                            {
607                                R15 &= ~1;
608                            }
609                            break;
610                        default:
611                            fatalerror("%08x: G4-2 Undefined Thumb instruction: %04x (%x)\n", pc, insn, (insn & THUMB_HIREG_H) >> THUMB_HIREG_H_SHIFT);
612                            R15 += 2;
613                            break;
614                    }
615                    break;
616                case 0x3:
617                    switch ((insn & THUMB_HIREG_H) >> THUMB_HIREG_H_SHIFT)
618                    {
619                        case 0x0:
620                            rd = (insn & THUMB_HIREG_RS) >> THUMB_HIREG_RS_SHIFT;
621                            addr = GET_REGISTER(cpustate, rd);
622                            if (addr & 1)
623                            {
624                                addr &= ~1;
625                            }
626                            else
627                            {
628                                SET_CPSR(GET_CPSR & ~T_MASK);
629                                if (addr & 2)
630                                {
631                                    addr += 2;
632                                }
633                            }
634                            R15 = addr;
635                            break;
636                        case 0x1:
637                            addr = GET_REGISTER(cpustate, ((insn & THUMB_HIREG_RS) >> THUMB_HIREG_RS_SHIFT) + 8);
638                            if ((((insn & THUMB_HIREG_RS) >> THUMB_HIREG_RS_SHIFT) + 8) == 15)
639                            {
640                                addr += 2;
641                            }
642                            if (addr & 1)
643                            {
644                                addr &= ~1;
645                            }
646                            else
647                            {
648                                SET_CPSR(GET_CPSR & ~T_MASK);
649                                if (addr & 2)
650                                {
651                                    addr += 2;
652                                }
653                            }
654                            R15 = addr;
655                            break;
656                        default:
657                            fatalerror("%08x: G4-3 Undefined Thumb instruction: %04x\n", pc, insn);
658                            R15 += 2;
659                            break;
660                    }
661                    break;
662                default:
663                    fatalerror("%08x: G4-x Undefined Thumb instruction: %04x\n", pc, insn);
664                    R15 += 2;
665                    break;
666            }
667            break;
668        case 0x2:
669        case 0x3:
670            readword = READ32((R15 & ~2) + 4 + ((insn & THUMB_INSN_IMM) << 2));
671            SET_REGISTER(cpustate, (insn & THUMB_INSN_IMM_RD) >> THUMB_INSN_IMM_RD_SHIFT, readword);
672            R15 += 2;
673            break;
674        default:
675            fatalerror("%08x: G4-y Undefined Thumb instruction: %04x\n", pc, insn);
676            R15 += 2;
677            break;
1381        R15 += 4 + (offs << 1);
6781382    }
1383    else
1384    {
1385        R15 += 2;
1386    }
1387
6791388}
6801389
681const void thumbblock05(arm_state *cpustate, UINT32 pc, UINT32 insn) /* LDR* STR* */
1390const void tg0d_1(arm_state *cpustate, UINT32 pc, UINT32 insn) // COND_NE:
6821391{
683   UINT32 addr;
684   UINT32 rm, rn, rd, op2;
1392   INT32 offs;
6851393
686    switch ((insn & THUMB_GROUP5_TYPE) >> THUMB_GROUP5_TYPE_SHIFT)
1394    offs = (INT8)(insn & THUMB_INSN_IMM);
1395//case
1396    if (Z_IS_CLEAR(GET_CPSR))
6871397    {
688        case 0x0: /* STR Rd, [Rn, Rm] */
689            rm = (insn & THUMB_GROUP5_RM) >> THUMB_GROUP5_RM_SHIFT;
690            rn = (insn & THUMB_GROUP5_RN) >> THUMB_GROUP5_RN_SHIFT;
691            rd = (insn & THUMB_GROUP5_RD) >> THUMB_GROUP5_RD_SHIFT;
692            addr = GET_REGISTER(cpustate, rn) + GET_REGISTER(cpustate, rm);
693            WRITE32(addr, GET_REGISTER(cpustate, rd));
694            R15 += 2;
695            break;
696        case 0x1: /* STRH Rd, [Rn, Rm] */
697            rm = (insn & THUMB_GROUP5_RM) >> THUMB_GROUP5_RM_SHIFT;
698            rn = (insn & THUMB_GROUP5_RN) >> THUMB_GROUP5_RN_SHIFT;
699            rd = (insn & THUMB_GROUP5_RD) >> THUMB_GROUP5_RD_SHIFT;
700            addr = GET_REGISTER(cpustate, rn) + GET_REGISTER(cpustate, rm);
701            WRITE16(addr, GET_REGISTER(cpustate, rd));
702            R15 += 2;
703            break;
704        case 0x2: /* STRB Rd, [Rn, Rm] */
705            rm = (insn & THUMB_GROUP5_RM) >> THUMB_GROUP5_RM_SHIFT;
706            rn = (insn & THUMB_GROUP5_RN) >> THUMB_GROUP5_RN_SHIFT;
707            rd = (insn & THUMB_GROUP5_RD) >> THUMB_GROUP5_RD_SHIFT;
708            addr = GET_REGISTER(cpustate, rn) + GET_REGISTER(cpustate, rm);
709            WRITE8(addr, GET_REGISTER(cpustate, rd));
710            R15 += 2;
711            break;
712        case 0x3: /* LDSB Rd, [Rn, Rm] todo, add dasm */
713            rm = (insn & THUMB_GROUP5_RM) >> THUMB_GROUP5_RM_SHIFT;
714            rn = (insn & THUMB_GROUP5_RN) >> THUMB_GROUP5_RN_SHIFT;
715            rd = (insn & THUMB_GROUP5_RD) >> THUMB_GROUP5_RD_SHIFT;
716            addr = GET_REGISTER(cpustate, rn) + GET_REGISTER(cpustate, rm);
717            op2 = READ8(addr);
718            if (op2 & 0x00000080)
719            {
720                op2 |= 0xffffff00;
721            }
722            SET_REGISTER(cpustate, rd, op2);
723            R15 += 2;
724            break;
725        case 0x4: /* LDR Rd, [Rn, Rm] */
726            rm = (insn & THUMB_GROUP5_RM) >> THUMB_GROUP5_RM_SHIFT;
727            rn = (insn & THUMB_GROUP5_RN) >> THUMB_GROUP5_RN_SHIFT;
728            rd = (insn & THUMB_GROUP5_RD) >> THUMB_GROUP5_RD_SHIFT;
729            addr = GET_REGISTER(cpustate, rn) + GET_REGISTER(cpustate, rm);
730            op2 = READ32(addr);
731            SET_REGISTER(cpustate, rd, op2);
732            R15 += 2;
733            break;
734        case 0x5: /* LDRH Rd, [Rn, Rm] */
735            rm = (insn & THUMB_GROUP5_RM) >> THUMB_GROUP5_RM_SHIFT;
736            rn = (insn & THUMB_GROUP5_RN) >> THUMB_GROUP5_RN_SHIFT;
737            rd = (insn & THUMB_GROUP5_RD) >> THUMB_GROUP5_RD_SHIFT;
738            addr = GET_REGISTER(cpustate, rn) + GET_REGISTER(cpustate, rm);
739            op2 = READ16(addr);
740            SET_REGISTER(cpustate, rd, op2);
741            R15 += 2;
742            break;
743        case 0x6: /* LDRB Rd, [Rn, Rm] */
744            rm = (insn & THUMB_GROUP5_RM) >> THUMB_GROUP5_RM_SHIFT;
745            rn = (insn & THUMB_GROUP5_RN) >> THUMB_GROUP5_RN_SHIFT;
746            rd = (insn & THUMB_GROUP5_RD) >> THUMB_GROUP5_RD_SHIFT;
747            addr = GET_REGISTER(cpustate, rn) + GET_REGISTER(cpustate, rm);
748            op2 = READ8(addr);
749            SET_REGISTER(cpustate, rd, op2);
750            R15 += 2;
751            break;
752        case 0x7: /* LDSH Rd, [Rn, Rm] */
753            rm = (insn & THUMB_GROUP5_RM) >> THUMB_GROUP5_RM_SHIFT;
754            rn = (insn & THUMB_GROUP5_RN) >> THUMB_GROUP5_RN_SHIFT;
755            rd = (insn & THUMB_GROUP5_RD) >> THUMB_GROUP5_RD_SHIFT;
756            addr = GET_REGISTER(cpustate, rn) + GET_REGISTER(cpustate, rm);
757            op2 = READ16(addr);
758            if (op2 & 0x00008000)
759            {
760                op2 |= 0xffff0000;
761            }
762            SET_REGISTER(cpustate, rd, op2);
763            R15 += 2;
764            break;
765        default:
766            fatalerror("%08x: G5 Undefined Thumb instruction: %04x\n", pc, insn);
767            R15 += 2;
768            break;
1398        R15 += 4 + (offs << 1);
7691399    }
1400    else
1401    {
1402        R15 += 2;
1403    }
1404
7701405}
7711406
772const void thumbblock06(arm_state *cpustate, UINT32 pc, UINT32 insn) /* Word Store w/ Immediate Offset */
1407const void tg0d_2(arm_state *cpustate, UINT32 pc, UINT32 insn) // COND_CS:
7731408{
774   UINT32 rn, rd;
7751409   INT32 offs;
7761410
777    if (insn & THUMB_LSOP_L) /* Load */
1411    offs = (INT8)(insn & THUMB_INSN_IMM);
1412//case
1413    if (C_IS_SET(GET_CPSR))
7781414    {
779        rn = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
780        rd = insn & THUMB_ADDSUB_RD;
781        offs = ((insn & THUMB_LSOP_OFFS) >> THUMB_LSOP_OFFS_SHIFT) << 2;
782        SET_REGISTER(cpustate, rd, READ32(GET_REGISTER(cpustate, rn) + offs)); // fix
783        R15 += 2;
1415        R15 += 4 + (offs << 1);
7841416    }
785    else /* Store */
1417    else
7861418    {
787        rn = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
788        rd = insn & THUMB_ADDSUB_RD;
789        offs = ((insn & THUMB_LSOP_OFFS) >> THUMB_LSOP_OFFS_SHIFT) << 2;
790        WRITE32(GET_REGISTER(cpustate, rn) + offs, GET_REGISTER(cpustate, rd));
7911419        R15 += 2;
7921420    }
1421
7931422}
7941423
795const void thumbblock07(arm_state *cpustate, UINT32 pc, UINT32 insn) /* Byte Store w/ Immeidate Offset */
1424const void tg0d_3(arm_state *cpustate, UINT32 pc, UINT32 insn) // COND_CC:
7961425{
797   UINT32 rn, rd;
7981426   INT32 offs;
7991427
800    if (insn & THUMB_LSOP_L) /* Load */
1428    offs = (INT8)(insn & THUMB_INSN_IMM);
1429//case
1430    if (C_IS_CLEAR(GET_CPSR))
8011431    {
802        rn = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
803        rd = insn & THUMB_ADDSUB_RD;
804        offs = (insn & THUMB_LSOP_OFFS) >> THUMB_LSOP_OFFS_SHIFT;
805        SET_REGISTER(cpustate, rd, READ8(GET_REGISTER(cpustate, rn) + offs));
806        R15 += 2;
1432        R15 += 4 + (offs << 1);
8071433    }
808    else /* Store */
1434    else
8091435    {
810        rn = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
811        rd = insn & THUMB_ADDSUB_RD;
812        offs = (insn & THUMB_LSOP_OFFS) >> THUMB_LSOP_OFFS_SHIFT;
813        WRITE8(GET_REGISTER(cpustate, rn) + offs, GET_REGISTER(cpustate, rd));
8141436        R15 += 2;
8151437    }
1438
8161439}
8171440
818const void thumbblock08(arm_state *cpustate, UINT32 pc, UINT32 insn) /* Load/Store Halfword */
1441const void tg0d_4(arm_state *cpustate, UINT32 pc, UINT32 insn) // COND_MI:
8191442{
820   UINT32 rs, rd, imm;
821   
822    if (insn & THUMB_HALFOP_L) /* Load */
1443   INT32 offs;
1444
1445    offs = (INT8)(insn & THUMB_INSN_IMM);
1446//case
1447    if (N_IS_SET(GET_CPSR))
8231448    {
824        imm = (insn & THUMB_HALFOP_OFFS) >> THUMB_HALFOP_OFFS_SHIFT;
825        rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
826        rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
827        SET_REGISTER(cpustate, rd, READ16(GET_REGISTER(cpustate, rs) + (imm << 1)));
828        R15 += 2;
1449        R15 += 4 + (offs << 1);
8291450    }
830    else /* Store */
1451    else
8311452    {
832        imm = (insn & THUMB_HALFOP_OFFS) >> THUMB_HALFOP_OFFS_SHIFT;
833        rs = (insn & THUMB_ADDSUB_RS) >> THUMB_ADDSUB_RS_SHIFT;
834        rd = (insn & THUMB_ADDSUB_RD) >> THUMB_ADDSUB_RD_SHIFT;
835        WRITE16(GET_REGISTER(cpustate, rs) + (imm << 1), GET_REGISTER(cpustate, rd));
8361453        R15 += 2;
8371454    }
1455
8381456}
8391457
840const void thumbblock09(arm_state *cpustate, UINT32 pc, UINT32 insn) /* Stack-Relative Load/Store */
1458const void tg0d_5(arm_state *cpustate, UINT32 pc, UINT32 insn) // COND_PL:
8411459{
842   UINT32 readword;
843   UINT32 rd;
8441460   INT32 offs;
8451461
846    if (insn & THUMB_STACKOP_L)
1462    offs = (INT8)(insn & THUMB_INSN_IMM);
1463//case
1464    if (N_IS_CLEAR(GET_CPSR))
8471465    {
848        rd = (insn & THUMB_STACKOP_RD) >> THUMB_STACKOP_RD_SHIFT;
849        offs = (UINT8)(insn & THUMB_INSN_IMM);
850        readword = READ32(GET_REGISTER(cpustate, 13) + ((UINT32)offs << 2));
851        SET_REGISTER(cpustate, rd, readword);
852        R15 += 2;
1466        R15 += 4 + (offs << 1);
8531467    }
8541468    else
8551469    {
856        rd = (insn & THUMB_STACKOP_RD) >> THUMB_STACKOP_RD_SHIFT;
857        offs = (UINT8)(insn & THUMB_INSN_IMM);
858        WRITE32(GET_REGISTER(cpustate, 13) + ((UINT32)offs << 2), GET_REGISTER(cpustate, rd));
8591470        R15 += 2;
8601471    }
1472
8611473}
8621474
863const void thumbblock0a(arm_state *cpustate, UINT32 pc, UINT32 insn) /* Get relative address */
1475const void tg0d_6(arm_state *cpustate, UINT32 pc, UINT32 insn) // COND_VS:
8641476{
865   UINT32 rd;
8661477   INT32 offs;
8671478
868    if (insn & THUMB_RELADDR_SP) /* ADD Rd, SP, #nn */
1479    offs = (INT8)(insn & THUMB_INSN_IMM);
1480//case
1481    if (V_IS_SET(GET_CPSR))
8691482    {
870        rd = (insn & THUMB_RELADDR_RD) >> THUMB_RELADDR_RD_SHIFT;
871        offs = (UINT8)(insn & THUMB_INSN_IMM) << 2;
872        SET_REGISTER(cpustate, rd, GET_REGISTER(cpustate, 13) + offs);
873        R15 += 2;
1483        R15 += 4 + (offs << 1);
8741484    }
875    else /* ADD Rd, PC, #nn */
1485    else
8761486    {
877        rd = (insn & THUMB_RELADDR_RD) >> THUMB_RELADDR_RD_SHIFT;
878        offs = (UINT8)(insn & THUMB_INSN_IMM) << 2;
879        SET_REGISTER(cpustate, rd, ((R15 + 4) & ~2) + offs);
8801487        R15 += 2;
8811488    }
1489
8821490}
8831491
884const void thumbblock0b(arm_state *cpustate, UINT32 pc, UINT32 insn) /* Stack-Related Opcodes */
1492const void tg0d_7(arm_state *cpustate, UINT32 pc, UINT32 insn) // COND_VC:
8851493{
886   UINT32 addr;
8871494   INT32 offs;
8881495
889    switch ((insn & THUMB_STACKOP_TYPE) >> THUMB_STACKOP_TYPE_SHIFT)
1496    offs = (INT8)(insn & THUMB_INSN_IMM);
1497//case
1498    if (V_IS_CLEAR(GET_CPSR))
8901499    {
891        case 0x0: /* ADD SP, #imm */
892            addr = (insn & THUMB_INSN_IMM);
893            addr &= ~THUMB_INSN_IMM_S;
894            SET_REGISTER(cpustate, 13, GET_REGISTER(cpustate, 13) + ((insn & THUMB_INSN_IMM_S) ? -(addr << 2) : (addr << 2)));
895            R15 += 2;
896            break;
897        case 0x4: /* PUSH {Rlist} */
898            for (offs = 7; offs >= 0; offs--)
899            {
900                if (insn & (1 << offs))
901                {
902                    SET_REGISTER(cpustate, 13, GET_REGISTER(cpustate, 13) - 4);
903                    WRITE32(GET_REGISTER(cpustate, 13), GET_REGISTER(cpustate, offs));
904                }
905            }
906            R15 += 2;
907            break;
908        case 0x5: /* PUSH {Rlist}{LR} */
909            SET_REGISTER(cpustate, 13, GET_REGISTER(cpustate, 13) - 4);
910            WRITE32(GET_REGISTER(cpustate, 13), GET_REGISTER(cpustate, 14));
911            for (offs = 7; offs >= 0; offs--)
912            {
913                if (insn & (1 << offs))
914                {
915                    SET_REGISTER(cpustate, 13, GET_REGISTER(cpustate, 13) - 4);
916                    WRITE32(GET_REGISTER(cpustate, 13), GET_REGISTER(cpustate, offs));
917                }
918            }
919            R15 += 2;
920            break;
921        case 0xc: /* POP {Rlist} */
922            for (offs = 0; offs < 8; offs++)
923            {
924                if (insn & (1 << offs))
925                {
926                    SET_REGISTER(cpustate, offs, READ32(GET_REGISTER(cpustate, 13)));
927                    SET_REGISTER(cpustate, 13, GET_REGISTER(cpustate, 13) + 4);
928                }
929            }
930            R15 += 2;
931            break;
932        case 0xd: /* POP {Rlist}{PC} */
933            for (offs = 0; offs < 8; offs++)
934            {
935                if (insn & (1 << offs))
936                {
937                    SET_REGISTER(cpustate, offs, READ32(GET_REGISTER(cpustate, 13)));
938                    SET_REGISTER(cpustate, 13, GET_REGISTER(cpustate, 13) + 4);
939                }
940            }
941            R15 = READ32(GET_REGISTER(cpustate, 13)) & ~1;
942            SET_REGISTER(cpustate, 13, GET_REGISTER(cpustate, 13) + 4);
943            break;
944        default:
945            fatalerror("%08x: Gb Undefined Thumb instruction: %04x\n", pc, insn);
946            R15 += 2;
947            break;
1500        R15 += 4 + (offs << 1);
9481501    }
1502    else
1503    {
1504        R15 += 2;
1505    }
1506
9491507}
9501508
951const void thumbblock0c(arm_state *cpustate, UINT32 pc, UINT32 insn) /* Multiple Load/Store */
1509const void tg0d_8(arm_state *cpustate, UINT32 pc, UINT32 insn) // COND_HI:
9521510{
953   UINT32 rd;
9541511   INT32 offs;
9551512
956    UINT32 ld_st_address;
1513    offs = (INT8)(insn & THUMB_INSN_IMM);
1514//case
1515    if (C_IS_SET(GET_CPSR) && Z_IS_CLEAR(GET_CPSR))
1516    {
1517        R15 += 4 + (offs << 1);
1518    }
1519    else
1520    {
1521        R15 += 2;
1522    }
9571523
958    rd = (insn & THUMB_MULTLS_BASE) >> THUMB_MULTLS_BASE_SHIFT;
1524}
9591525
960    // "The address should normally be a word aligned quantity and non-word aligned addresses do not affect the instruction."
961    // "However, the bottom 2 bits of the address will appear on A[1:0] and might be interpreted by the memory system."
1526const void tg0d_9(arm_state *cpustate, UINT32 pc, UINT32 insn) // COND_LS:
1527{
1528   INT32 offs;
9621529
963    // GBA "BB Ball" performs an unaligned read with A[1:0] = 2 and expects A[1] not to be ignored [BP 800B90A,(R4&3)!=0]
964    // GBA "Gadget Racers" performs an unaligned read with A[1:0] = 1 and expects A[0] to be ignored [BP B72,(R0&3)!=0]
1530    offs = (INT8)(insn & THUMB_INSN_IMM);
1531//case
1532    if (C_IS_CLEAR(GET_CPSR) || Z_IS_SET(GET_CPSR))
1533    {
1534        R15 += 4 + (offs << 1);
1535    }
1536    else
1537    {
1538        R15 += 2;
1539    }
9651540
966    ld_st_address = GET_REGISTER(cpustate, rd);
1541}
9671542
968    if (insn & THUMB_MULTLS) /* Load */
1543const void tg0d_a(arm_state *cpustate, UINT32 pc, UINT32 insn) // COND_GE:
1544{
1545   INT32 offs;
1546
1547    offs = (INT8)(insn & THUMB_INSN_IMM);
1548//case
1549    if (!(GET_CPSR & N_MASK) == !(GET_CPSR & V_MASK))
9691550    {
970        int rd_in_list;
971
972        rd_in_list = insn & (1 << rd);
973        for (offs = 0; offs < 8; offs++)
974        {
975            if (insn & (1 << offs))
976            {
977                SET_REGISTER(cpustate, offs, READ32(ld_st_address & ~1));
978                ld_st_address += 4;
979            }
980        }
981        if (!rd_in_list)
982            SET_REGISTER(cpustate, rd, ld_st_address);
983        R15 += 2;
1551        R15 += 4 + (offs << 1);
9841552    }
985    else /* Store */
1553    else
9861554    {
987        for (offs = 0; offs < 8; offs++)
988        {
989            if (insn & (1 << offs))
990            {
991                WRITE32(ld_st_address & ~3, GET_REGISTER(cpustate, offs));
992                ld_st_address += 4;
993            }
994        }
995        SET_REGISTER(cpustate, rd, ld_st_address);
9961555        R15 += 2;
9971556    }
1557
9981558}
9991559
1000const void thumbblock0d(arm_state *cpustate, UINT32 pc, UINT32 insn) /* Conditional Branch */
1560const void tg0d_b(arm_state *cpustate, UINT32 pc, UINT32 insn) // COND_LT:
10011561{
10021562   INT32 offs;
10031563
10041564    offs = (INT8)(insn & THUMB_INSN_IMM);
1005    switch ((insn & THUMB_COND_TYPE) >> THUMB_COND_TYPE_SHIFT)
1565//case
1566    if (!(GET_CPSR & N_MASK) != !(GET_CPSR & V_MASK))
10061567    {
1007        case COND_EQ:
1008            if (Z_IS_SET(GET_CPSR))
1009            {
1010                R15 += 4 + (offs << 1);
1011            }
1012            else
1013            {
1014                R15 += 2;
1015            }
1016            break;
1017        case COND_NE:
1018            if (Z_IS_CLEAR(GET_CPSR))
1019            {
1020                R15 += 4 + (offs << 1);
1021            }
1022            else
1023            {
1024                R15 += 2;
1025            }
1026            break;
1027        case COND_CS:
1028            if (C_IS_SET(GET_CPSR))
1029            {
1030                R15 += 4 + (offs << 1);
1031            }
1032            else
1033            {
1034                R15 += 2;
1035            }
1036            break;
1037        case COND_CC:
1038            if (C_IS_CLEAR(GET_CPSR))
1039            {
1040                R15 += 4 + (offs << 1);
1041            }
1042            else
1043            {
1044                R15 += 2;
1045            }
1046            break;
1047        case COND_MI:
1048            if (N_IS_SET(GET_CPSR))
1049            {
1050                R15 += 4 + (offs << 1);
1051            }
1052            else
1053            {
1054                R15 += 2;
1055            }
1056            break;
1057        case COND_PL:
1058            if (N_IS_CLEAR(GET_CPSR))
1059            {
1060                R15 += 4 + (offs << 1);
1061            }
1062            else
1063            {
1064                R15 += 2;
1065            }
1066            break;
1067        case COND_VS:
1068            if (V_IS_SET(GET_CPSR))
1069            {
1070                R15 += 4 + (offs << 1);
1071            }
1072            else
1073            {
1074                R15 += 2;
1075            }
1076            break;
1077        case COND_VC:
1078            if (V_IS_CLEAR(GET_CPSR))
1079            {
1080                R15 += 4 + (offs << 1);
1081            }
1082            else
1083            {
1084                R15 += 2;
1085            }
1086            break;
1087        case COND_HI:
1088            if (C_IS_SET(GET_CPSR) && Z_IS_CLEAR(GET_CPSR))
1089            {
1090                R15 += 4 + (offs << 1);
1091            }
1092            else
1093            {
1094                R15 += 2;
1095            }
1096            break;
1097        case COND_LS:
1098            if (C_IS_CLEAR(GET_CPSR) || Z_IS_SET(GET_CPSR))
1099            {
1100                R15 += 4 + (offs << 1);
1101            }
1102            else
1103            {
1104                R15 += 2;
1105            }
1106            break;
1107        case COND_GE:
1108            if (!(GET_CPSR & N_MASK) == !(GET_CPSR & V_MASK))
1109            {
1110                R15 += 4 + (offs << 1);
1111            }
1112            else
1113            {
1114                R15 += 2;
1115            }
1116            break;
1117        case COND_LT:
1118            if (!(GET_CPSR & N_MASK) != !(GET_CPSR & V_MASK))
1119            {
1120                R15 += 4 + (offs << 1);
1121            }
1122            else
1123            {
1124                R15 += 2;
1125            }
1126            break;
1127        case COND_GT:
1128            if (Z_IS_CLEAR(GET_CPSR) && !(GET_CPSR & N_MASK) == !(GET_CPSR & V_MASK))
1129            {
1130                R15 += 4 + (offs << 1);
1131            }
1132            else
1133            {
1134                R15 += 2;
1135            }
1136            break;
1137        case COND_LE:
1138            if (Z_IS_SET(GET_CPSR) || !(GET_CPSR & N_MASK) != !(GET_CPSR & V_MASK))
1139            {
1140                R15 += 4 + (offs << 1);
1141            }
1142            else
1143            {
1144                R15 += 2;
1145            }
1146            break;
1147        case COND_AL:
1148            fatalerror("%08x: Undefined Thumb instruction: %04x (ARM9 reserved)\n", pc, insn);
1149            R15 += 2;
1150            break;
1151        case COND_NV:   // SWI (this is sort of a "hole" in the opcode encoding)
1152            cpustate->pendingSwi = 1;
1153            ARM7_CHECKIRQ;
1154            break;
1568        R15 += 4 + (offs << 1);
11551569    }
1570    else
1571    {
1572        R15 += 2;
1573    }
1574
11561575}
11571576
1158const void thumbblock0e(arm_state *cpustate, UINT32 pc, UINT32 insn) /* B #offs */
1577const void tg0d_c(arm_state *cpustate, UINT32 pc, UINT32 insn) // COND_GT:
11591578{
1160   UINT32 addr;
11611579   INT32 offs;
11621580
1163    if (insn & THUMB_BLOP_LO)
1581    offs = (INT8)(insn & THUMB_INSN_IMM);
1582//case
1583    if (Z_IS_CLEAR(GET_CPSR) && !(GET_CPSR & N_MASK) == !(GET_CPSR & V_MASK))
11641584    {
1165        addr = GET_REGISTER(cpustate, 14);
1166        addr += (insn & THUMB_BLOP_OFFS) << 1;
1167        addr &= 0xfffffffc;
1168        SET_REGISTER(cpustate, 14, (R15 + 4) | 1);
1169        R15 = addr;
1585        R15 += 4 + (offs << 1);
11701586    }
11711587    else
11721588    {
1173        offs = (insn & THUMB_BRANCH_OFFS) << 1;
1174        if (offs & 0x00000800)
1175        {
1176            offs |= 0xfffff800;
1177        }
1178        R15 += 4 + offs;
1589        R15 += 2;
11791590    }
1591
11801592}
11811593
1182const void thumbblock0f(arm_state *cpustate, UINT32 pc, UINT32 insn) /* BL */
1594const void tg0d_d(arm_state *cpustate, UINT32 pc, UINT32 insn) // COND_LE:
11831595{
1184   UINT32 addr;
1596   INT32 offs;
11851597
1186    if (insn & THUMB_BLOP_LO)
1598    offs = (INT8)(insn & THUMB_INSN_IMM);
1599//case
1600    if (Z_IS_SET(GET_CPSR) || !(GET_CPSR & N_MASK) != !(GET_CPSR & V_MASK))
11871601    {
1188        addr = GET_REGISTER(cpustate, 14) & ~1;
1189        addr += (insn & THUMB_BLOP_OFFS) << 1;
1190        SET_REGISTER(cpustate, 14, (R15 + 2) | 1);
1191        R15 = addr;
1192        //R15 += 2;
1602        R15 += 4 + (offs << 1);
11931603    }
11941604    else
11951605    {
1196        addr = (insn & THUMB_BLOP_OFFS) << 12;
1197        if (addr & (1 << 22))
1198        {
1199            addr |= 0xff800000;
1200        }
1201        addr += R15 + 4;
1202        SET_REGISTER(cpustate, 14, addr);
12031606        R15 += 2;
12041607    }
1608
12051609}
1610
1611const void tg0d_e(arm_state *cpustate, UINT32 pc, UINT32 insn) // COND_AL:
1612{
1613//   INT32 offs;
1614
1615//    offs = (INT8)(insn & THUMB_INSN_IMM);
1616//case
1617    fatalerror("%08x: Undefined Thumb instruction: %04x (ARM9 reserved)\n", pc, insn);
1618    R15 += 2;
1619
1620}
1621
1622const void tg0d_f(arm_state *cpustate, UINT32 pc, UINT32 insn) // COND_NV:   // SWI (this is sort of a "hole" in the opcode encoding)
1623{
1624//   INT32 offs;
1625
1626//    offs = (INT8)(insn & THUMB_INSN_IMM);
1627
1628//case
1629    cpustate->pendingSwi = 1;
1630    ARM7_CHECKIRQ;
1631
1632}
1633
1634 /* B #offs */
1635
1636const void tg0e_0(arm_state *cpustate, UINT32 pc, UINT32 insn)
1637{
1638   INT32 offs;
1639
1640    offs = (insn & THUMB_BRANCH_OFFS) << 1;
1641    if (offs & 0x00000800)
1642    {
1643        offs |= 0xfffff800;
1644    }
1645    R15 += 4 + offs;
1646}
1647
1648
1649const void tg0e_1(arm_state *cpustate, UINT32 pc, UINT32 insn)
1650{
1651   UINT32 addr;
1652   
1653    addr = GET_REGISTER(cpustate, 14);
1654    addr += (insn & THUMB_BLOP_OFFS) << 1;
1655    addr &= 0xfffffffc;
1656    SET_REGISTER(cpustate, 14, (R15 + 4) | 1);
1657    R15 = addr;
1658}
1659
1660 /* BL */
1661
1662const void tg0f_0(arm_state *cpustate, UINT32 pc, UINT32 insn)
1663{
1664   UINT32 addr;
1665
1666    addr = (insn & THUMB_BLOP_OFFS) << 12;
1667    if (addr & (1 << 22))
1668    {
1669        addr |= 0xff800000;
1670    }
1671    addr += R15 + 4;
1672    SET_REGISTER(cpustate, 14, addr);
1673    R15 += 2;
1674}
1675
1676
1677const void tg0f_1(arm_state *cpustate, UINT32 pc, UINT32 insn) /* BL */
1678{
1679   UINT32 addr;
1680
1681    addr = GET_REGISTER(cpustate, 14) & ~1;
1682    addr += (insn & THUMB_BLOP_OFFS) << 1;
1683    SET_REGISTER(cpustate, 14, (R15 + 2) | 1);
1684    R15 = addr;
1685    //R15 += 2;
1686}
src/emu/cpu/arm7/arm7core.h
r14020r14021
3434#define __ARM7CORE_H__
3535
3636#define ARM7_MMU_ENABLE_HACK 0
37#define ARM7_DEBUG_CORE 0
3738
39
3840/****************************************************************************************************
3941 *  INTERRUPT LINES/EXCEPTIONS
4042 ***************************************************************************************************/
r14020r14021
390392#define THUMB_ADDSUB_RNIMM  ((UINT16)0x01c0)
391393#define THUMB_ADDSUB_RS     ((UINT16)0x0038)
392394#define THUMB_ADDSUB_RD     ((UINT16)0x0007)
393#define THUMB_INSN_ADDSUB   ((UINT16)0x0800)
394395#define THUMB_INSN_CMP      ((UINT16)0x0800)
395396#define THUMB_INSN_SUB      ((UINT16)0x0800)
396397#define THUMB_INSN_IMM_RD   ((UINT16)0x0700)
397398#define THUMB_INSN_IMM_S    ((UINT16)0x0080)
398399#define THUMB_INSN_IMM      ((UINT16)0x00ff)
400#define THUMB_INSN_ADDSUB   ((UINT16)0x0800)
399401#define THUMB_ADDSUB_TYPE   ((UINT16)0x0600)
400402#define THUMB_HIREG_OP      ((UINT16)0x0300)
401403#define THUMB_HIREG_H       ((UINT16)0x00c0)
src/emu/cpu/arm7/arm7help.h
r14020r14021
55#define ARM7_ICOUNT         cpustate->iCount
66
77
8extern void SwitchMode(arm_state *cpustate, int cpsr_mode_val);
9
10#if 0
11#define LOG(x) mame_printf_debug x
12#else
13#define LOG(x) logerror x
14#endif
15
816/***************
917 * helper funcs
1018 ***************/
r14020r14021
8290
8391typedef const void (*arm7thumb_ophandler)(arm_state*, UINT32, UINT32);
8492
85extern arm7thumb_ophandler thumb_handler[0x10];
93extern arm7thumb_ophandler thumb_handler[0x40*0x10];
8694
95typedef const void (*arm7ops_ophandler)(arm_state*, UINT32);
96
97extern arm7ops_ophandler ops_handler[0x10];
98
99extern void (*arm7_coproc_dt_r_callback)(arm_state *cpustate, UINT32 insn, UINT32 *prn, UINT32 (*read32)(arm_state *cpustate, UINT32 addr));
100extern void (*arm7_coproc_dt_w_callback)(arm_state *cpustate, UINT32 insn, UINT32 *prn, void (*write32)(arm_state *cpustate, UINT32 addr, UINT32 data));
101
102
87103/***************************************************************************
88104 * Default Memory Handlers
89105 ***************************************************************************/
src/emu/cpu/arm7/arm7.c
r14020r14021
4141#include "arm7thmb.h"
4242#include "arm7help.h"
4343
44#if 0
45#define LOG(x) mame_printf_debug x
46#else
47#define LOG(x) logerror x
48#endif
4944
5045/* prototypes of coprocessor functions */
5146static WRITE32_DEVICE_HANDLER(arm7_do_callback);
r14020r14021
5449void arm7_dt_r_callback(arm_state *cpustate, UINT32 insn, UINT32 *prn, UINT32 (*read32)(arm_state *cpustate, UINT32 addr));
5550void arm7_dt_w_callback(arm_state *cpustate, UINT32 insn, UINT32 *prn, void (*write32)(arm_state *cpustate, UINT32 addr, UINT32 data));
5651
52// holder for the co processor Data Transfer Read & Write Callback funcs
53void (*arm7_coproc_dt_r_callback)(arm_state *cpustate, UINT32 insn, UINT32 *prn, UINT32 (*read32)(arm_state *cpustate, UINT32 addr));
54void (*arm7_coproc_dt_w_callback)(arm_state *cpustate, UINT32 insn, UINT32 *prn, void (*write32)(arm_state *cpustate, UINT32 addr, UINT32 data));
5755
56
5857INLINE arm_state *get_safe_token(device_t *device)
5958{
6059   assert(device != NULL);
r14020r14021
9695   ARM7REG(eCPSR) = val;
9796}
9897
99INLINE INT64 saturate_qbit_overflow(arm_state *cpustate, INT64 res)
100{
101   if (res > 2147483647)   // INT32_MAX
102   {   // overflow high? saturate and set Q
103      res = 2147483647;
104      SET_CPSR(GET_CPSR | Q_MASK);
105   }
106   else if (res < (-2147483647-1))   // INT32_MIN
107   {   // overflow low? saturate and set Q
108      res = (-2147483647-1);
109      SET_CPSR(GET_CPSR | Q_MASK);
110   }
11198
112   return res;
113}
114
11599/**************************************************************************
116100 * ARM TLB IMPLEMENTATION
117101 **************************************************************************/
r14020r14021
533517   /* nothing to do here */
534518}
535519
520#define UNEXECUTED() \
521   R15 += 4; \
522   ARM7_ICOUNT +=2; /* Any unexecuted instruction only takes 1 cycle (page 193) */ \
523
536524static CPU_EXECUTE( arm7 )
537525{
538/* include the arm7 core execute code */
539#include "arm7exec.c"
526    UINT32 pc;
527    UINT32 insn;
528    arm_state *cpustate = get_safe_token(device);
529
530    do
531    {
532        debugger_instruction_hook(cpustate->device, GET_PC);
533
534        /* handle Thumb instructions if active */
535        if (T_IS_SET(GET_CPSR))
536        {
537         UINT32 raddr;
538
539            pc = R15;
540
541         // "In Thumb state, bit [0] is undefined and must be ignored. Bits [31:1] contain the PC."
542         raddr = pc & (~1);
543
544         if ( COPRO_CTRL & COPRO_CTRL_MMU_EN )
545         {
546             if (!arm7_tlb_translate(cpustate, &raddr, ARM7_TLB_ABORT_P | ARM7_TLB_READ))
547             {
548                goto skip_exec;
549             }
550         }
551
552         insn = cpustate->direct->read_decrypted_word(raddr);
553         thumb_handler[(insn & 0xffc0) >> 6](cpustate, pc, insn);
554         
555        }
556        else
557        {
558         UINT32 raddr;
559
560            /* load 32 bit instruction */
561            pc = GET_PC;
562
563         // "In ARM state, bits [1:0] of r15 are undefined and must be ignored. Bits [31:2] contain the PC."
564         raddr = pc & (~3);
565
566       if ( COPRO_CTRL & COPRO_CTRL_MMU_EN )
567       {
568          if (!arm7_tlb_translate(cpustate, &raddr, ARM7_TLB_ABORT_P | ARM7_TLB_READ))
569          {
570             goto skip_exec;
571          }
572       }
573
574#if 0
575         if (MODE26)
576         {
577            UINT32 temp1, temp2;
578            temp1 = GET_CPSR & 0xF00000C3;
579            temp2 = (R15 & 0xF0000000) | ((R15 & 0x0C000000) >> (26 - 6)) | (R15 & 0x00000003);
580            if (temp1 != temp2) fatalerror( "%08X: 32-bit and 26-bit modes are out of sync (%08X %08X)", pc, temp1, temp2);
581         }
582#endif
583
584            insn = cpustate->direct->read_decrypted_dword(raddr);
585
586            /* process condition codes for this instruction */
587            switch (insn >> INSN_COND_SHIFT)
588            {
589            case COND_EQ:
590               if (Z_IS_CLEAR(GET_CPSR))
591                  { UNEXECUTED();  goto skip_exec; }
592               break;
593            case COND_NE:
594               if (Z_IS_SET(GET_CPSR))
595                  { UNEXECUTED();  goto skip_exec; }
596               break;
597            case COND_CS:
598               if (C_IS_CLEAR(GET_CPSR))
599                  { UNEXECUTED();  goto skip_exec; }
600               break;
601            case COND_CC:
602               if (C_IS_SET(GET_CPSR))
603                  { UNEXECUTED();  goto skip_exec; }
604               break;
605            case COND_MI:
606               if (N_IS_CLEAR(GET_CPSR))
607                  { UNEXECUTED();  goto skip_exec; }
608               break;
609            case COND_PL:
610               if (N_IS_SET(GET_CPSR))
611                  { UNEXECUTED();  goto skip_exec; }
612               break;
613            case COND_VS:
614               if (V_IS_CLEAR(GET_CPSR))
615                  { UNEXECUTED();  goto skip_exec; }
616               break;
617            case COND_VC:
618               if (V_IS_SET(GET_CPSR))
619                  { UNEXECUTED();  goto skip_exec; }
620               break;
621            case COND_HI:
622               if (C_IS_CLEAR(GET_CPSR) || Z_IS_SET(GET_CPSR))
623                  { UNEXECUTED();  goto skip_exec; }
624               break;
625            case COND_LS:
626               if (C_IS_SET(GET_CPSR) && Z_IS_CLEAR(GET_CPSR))
627                  { UNEXECUTED();  goto skip_exec; }
628               break;
629            case COND_GE:
630               if (!(GET_CPSR & N_MASK) != !(GET_CPSR & V_MASK)) /* Use x ^ (x >> ...) method */
631                  { UNEXECUTED();  goto skip_exec; }
632               break;
633            case COND_LT:
634               if (!(GET_CPSR & N_MASK) == !(GET_CPSR & V_MASK))
635                  { UNEXECUTED();  goto skip_exec; }
636               break;
637            case COND_GT:
638               if (Z_IS_SET(GET_CPSR) || (!(GET_CPSR & N_MASK) != !(GET_CPSR & V_MASK)))
639                  { UNEXECUTED();  goto skip_exec; }
640               break;
641            case COND_LE:
642               if (Z_IS_CLEAR(GET_CPSR) && (!(GET_CPSR & N_MASK) == !(GET_CPSR & V_MASK)))
643                 { UNEXECUTED();  goto skip_exec; }
644               break;
645            case COND_NV:
646               { UNEXECUTED();  goto skip_exec; }
647               break;
648            }
649            /*******************************************************************/
650            /* If we got here - condition satisfied, so decode the instruction */
651            /*******************************************************************/
652         ops_handler[((insn & 0xF000000) >> 24)](cpustate, insn);
653      }
654
655skip_exec:
656
657        ARM7_CHECKIRQ;
658
659        /* All instructions remove 3 cycles.. Others taking less / more will have adjusted this # prior to here */
660        ARM7_ICOUNT -= 3;
661    } while (ARM7_ICOUNT > 0);
540662}
541663
542
543664static void set_irq_line(arm_state *cpustate, int irqline, int state)
544665{
545666    // must call core
src/emu/cpu/arm7/arm7thmb.h
r14020r14021
11
22
33
4const void thumbblock00(arm_state *cpustate, UINT32 pc, UINT32 insn);
5const void thumbblock01(arm_state *cpustate, UINT32 pc, UINT32 insn);
6const void thumbblock02(arm_state *cpustate, UINT32 pc, UINT32 insn);
7const void thumbblock03(arm_state *cpustate, UINT32 pc, UINT32 insn);
8const void thumbblock04(arm_state *cpustate, UINT32 pc, UINT32 insn);
9const void thumbblock05(arm_state *cpustate, UINT32 pc, UINT32 insn);
10const void thumbblock06(arm_state *cpustate, UINT32 pc, UINT32 insn);
11const void thumbblock07(arm_state *cpustate, UINT32 pc, UINT32 insn);
12const void thumbblock08(arm_state *cpustate, UINT32 pc, UINT32 insn);
13const void thumbblock09(arm_state *cpustate, UINT32 pc, UINT32 insn);
14const void thumbblock0a(arm_state *cpustate, UINT32 pc, UINT32 insn);
15const void thumbblock0b(arm_state *cpustate, UINT32 pc, UINT32 insn);
16const void thumbblock0c(arm_state *cpustate, UINT32 pc, UINT32 insn);
17const void thumbblock0d(arm_state *cpustate, UINT32 pc, UINT32 insn);
18const void thumbblock0e(arm_state *cpustate, UINT32 pc, UINT32 insn);
19const void thumbblock0f(arm_state *cpustate, UINT32 pc, UINT32 insn);
4const void tg00_0(arm_state *cpustate, UINT32 pc, UINT32 insn);
5const void tg00_1(arm_state *cpustate, UINT32 pc, UINT32 insn);
6const void tg01_0(arm_state *cpustate, UINT32 pc, UINT32 insn);
7const void tg01_10(arm_state *cpustate, UINT32 pc, UINT32 insn);
8const void tg01_11(arm_state *cpustate, UINT32 pc, UINT32 insn);
9const void tg01_12(arm_state *cpustate, UINT32 pc, UINT32 insn);
10const void tg01_13(arm_state *cpustate, UINT32 pc, UINT32 insn);
11const void tg02_0(arm_state *cpustate, UINT32 pc, UINT32 insn);
12const void tg02_1(arm_state *cpustate, UINT32 pc, UINT32 insn);
13const void tg03_0(arm_state *cpustate, UINT32 pc, UINT32 insn);
14const void tg03_1(arm_state *cpustate, UINT32 pc, UINT32 insn);
15const void tg04_00_00(arm_state *cpustate, UINT32 pc, UINT32 insn);
16const void tg04_00_01(arm_state *cpustate, UINT32 pc, UINT32 insn);
17const void tg04_00_02(arm_state *cpustate, UINT32 pc, UINT32 insn);
18const void tg04_00_03(arm_state *cpustate, UINT32 pc, UINT32 insn);
19const void tg04_00_04(arm_state *cpustate, UINT32 pc, UINT32 insn);
20const void tg04_00_05(arm_state *cpustate, UINT32 pc, UINT32 insn);
21const void tg04_00_06(arm_state *cpustate, UINT32 pc, UINT32 insn);
22const void tg04_00_07(arm_state *cpustate, UINT32 pc, UINT32 insn);
23const void tg04_00_08(arm_state *cpustate, UINT32 pc, UINT32 insn);
24const void tg04_00_09(arm_state *cpustate, UINT32 pc, UINT32 insn);
25const void tg04_00_0a(arm_state *cpustate, UINT32 pc, UINT32 insn);
26const void tg04_00_0b(arm_state *cpustate, UINT32 pc, UINT32 insn);
27const void tg04_00_0c(arm_state *cpustate, UINT32 pc, UINT32 insn);
28const void tg04_00_0d(arm_state *cpustate, UINT32 pc, UINT32 insn);
29const void tg04_00_0e(arm_state *cpustate, UINT32 pc, UINT32 insn);
30const void tg04_00_0f(arm_state *cpustate, UINT32 pc, UINT32 insn);
31const void tg04_01_00(arm_state *cpustate, UINT32 pc, UINT32 insn);
32const void tg04_01_01(arm_state *cpustate, UINT32 pc, UINT32 insn);
33const void tg04_01_02(arm_state *cpustate, UINT32 pc, UINT32 insn);
34const void tg04_01_03(arm_state *cpustate, UINT32 pc, UINT32 insn);
35const void tg04_01_10(arm_state *cpustate, UINT32 pc, UINT32 insn);
36const void tg04_01_11(arm_state *cpustate, UINT32 pc, UINT32 insn);
37const void tg04_01_12(arm_state *cpustate, UINT32 pc, UINT32 insn);
38const void tg04_01_13(arm_state *cpustate, UINT32 pc, UINT32 insn);
39const void tg04_01_20(arm_state *cpustate, UINT32 pc, UINT32 insn);
40const void tg04_01_21(arm_state *cpustate, UINT32 pc, UINT32 insn);
41const void tg04_01_22(arm_state *cpustate, UINT32 pc, UINT32 insn);
42const void tg04_01_23(arm_state *cpustate, UINT32 pc, UINT32 insn);
43const void tg04_01_30(arm_state *cpustate, UINT32 pc, UINT32 insn);
44const void tg04_01_31(arm_state *cpustate, UINT32 pc, UINT32 insn);
45const void tg04_01_32(arm_state *cpustate, UINT32 pc, UINT32 insn);
46const void tg04_01_33(arm_state *cpustate, UINT32 pc, UINT32 insn);
47const void tg04_0203(arm_state *cpustate, UINT32 pc, UINT32 insn);
48const void tg05_0(arm_state *cpustate, UINT32 pc, UINT32 insn);
49const void tg05_1(arm_state *cpustate, UINT32 pc, UINT32 insn);
50const void tg05_2(arm_state *cpustate, UINT32 pc, UINT32 insn);
51const void tg05_3(arm_state *cpustate, UINT32 pc, UINT32 insn);
52const void tg05_4(arm_state *cpustate, UINT32 pc, UINT32 insn);
53const void tg05_5(arm_state *cpustate, UINT32 pc, UINT32 insn);
54const void tg05_6(arm_state *cpustate, UINT32 pc, UINT32 insn);
55const void tg05_7(arm_state *cpustate, UINT32 pc, UINT32 insn);
56const void tg06_0(arm_state *cpustate, UINT32 pc, UINT32 insn);
57const void tg06_1(arm_state *cpustate, UINT32 pc, UINT32 insn);
58const void tg07_0(arm_state *cpustate, UINT32 pc, UINT32 insn);
59const void tg07_1(arm_state *cpustate, UINT32 pc, UINT32 insn);
60const void tg08_0(arm_state *cpustate, UINT32 pc, UINT32 insn);
61const void tg08_1(arm_state *cpustate, UINT32 pc, UINT32 insn);
62const void tg09_0(arm_state *cpustate, UINT32 pc, UINT32 insn);
63const void tg09_1(arm_state *cpustate, UINT32 pc, UINT32 insn);
64const void tg0a_0(arm_state *cpustate, UINT32 pc, UINT32 insn);
65const void tg0a_1(arm_state *cpustate, UINT32 pc, UINT32 insn);
66const void tg0b_0(arm_state *cpustate, UINT32 pc, UINT32 insn);
67const void tg0b_1(arm_state *cpustate, UINT32 pc, UINT32 insn);
68const void tg0b_2(arm_state *cpustate, UINT32 pc, UINT32 insn);
69const void tg0b_3(arm_state *cpustate, UINT32 pc, UINT32 insn);
70const void tg0b_4(arm_state *cpustate, UINT32 pc, UINT32 insn);
71const void tg0b_5(arm_state *cpustate, UINT32 pc, UINT32 insn);
72const void tg0b_6(arm_state *cpustate, UINT32 pc, UINT32 insn);
73const void tg0b_7(arm_state *cpustate, UINT32 pc, UINT32 insn);
74const void tg0b_8(arm_state *cpustate, UINT32 pc, UINT32 insn);
75const void tg0b_9(arm_state *cpustate, UINT32 pc, UINT32 insn);
76const void tg0b_a(arm_state *cpustate, UINT32 pc, UINT32 insn);
77const void tg0b_b(arm_state *cpustate, UINT32 pc, UINT32 insn);
78const void tg0b_c(arm_state *cpustate, UINT32 pc, UINT32 insn);
79const void tg0b_d(arm_state *cpustate, UINT32 pc, UINT32 insn);
80const void tg0b_e(arm_state *cpustate, UINT32 pc, UINT32 insn);
81const void tg0b_f(arm_state *cpustate, UINT32 pc, UINT32 insn);
82const void tg0c_0(arm_state *cpustate, UINT32 pc, UINT32 insn);
83const void tg0c_1(arm_state *cpustate, UINT32 pc, UINT32 insn);
84const void tg0d_0(arm_state *cpustate, UINT32 pc, UINT32 insn);
85const void tg0d_1(arm_state *cpustate, UINT32 pc, UINT32 insn);
86const void tg0d_2(arm_state *cpustate, UINT32 pc, UINT32 insn);
87const void tg0d_3(arm_state *cpustate, UINT32 pc, UINT32 insn);
88const void tg0d_4(arm_state *cpustate, UINT32 pc, UINT32 insn);
89const void tg0d_5(arm_state *cpustate, UINT32 pc, UINT32 insn);
90const void tg0d_6(arm_state *cpustate, UINT32 pc, UINT32 insn);
91const void tg0d_7(arm_state *cpustate, UINT32 pc, UINT32 insn);
92const void tg0d_8(arm_state *cpustate, UINT32 pc, UINT32 insn);
93const void tg0d_9(arm_state *cpustate, UINT32 pc, UINT32 insn);
94const void tg0d_a(arm_state *cpustate, UINT32 pc, UINT32 insn);
95const void tg0d_b(arm_state *cpustate, UINT32 pc, UINT32 insn);
96const void tg0d_c(arm_state *cpustate, UINT32 pc, UINT32 insn);
97const void tg0d_d(arm_state *cpustate, UINT32 pc, UINT32 insn);
98const void tg0d_e(arm_state *cpustate, UINT32 pc, UINT32 insn);
99const void tg0d_f(arm_state *cpustate, UINT32 pc, UINT32 insn);
100const void tg0e_0(arm_state *cpustate, UINT32 pc, UINT32 insn);
101const void tg0e_1(arm_state *cpustate, UINT32 pc, UINT32 insn);
102const void tg0f_0(arm_state *cpustate, UINT32 pc, UINT32 insn);
103const void tg0f_1(arm_state *cpustate, UINT32 pc, UINT32 insn);
20104
src/emu/cpu/arm7/arm7ops.c
r0r14021
1#include "emu.h"
2#include "arm7core.h"
3#include "arm7ops.h"
4#include "arm7help.h"
5
6INLINE INT64 saturate_qbit_overflow(arm_state *cpustate, INT64 res)
7{
8   if (res > 2147483647)   // INT32_MAX
9   {   // overflow high? saturate and set Q
10      res = 2147483647;
11      SET_CPSR(GET_CPSR | Q_MASK);
12   }
13   else if (res < (-2147483647-1))   // INT32_MIN
14   {   // overflow low? saturate and set Q
15      res = (-2147483647-1);
16      SET_CPSR(GET_CPSR | Q_MASK);
17   }
18
19   return res;
20}
21
22// I could prob. convert to macro, but Switchmode shouldn't occur that often in emulated code..
23void SwitchMode(arm_state *cpustate, int cpsr_mode_val)
24{
25    UINT32 cspr = GET_CPSR & ~MODE_FLAG;
26    SET_CPSR(cspr | cpsr_mode_val);
27}
28
29
30/* Decodes an Op2-style shifted-register form.  If @carry@ is non-zero the
31 * shifter carry output will manifest itself as @*carry == 0@ for carry clear
32 * and @*carry != 0@ for carry set.
33
34   SJE: Rules:
35   IF RC = 256, Result = no shift.
36   LSL   0   = Result = RM, Carry = Old Contents of CPSR C Bit
37   LSL(0,31) = Result shifted, least significant bit is in carry out
38   LSL  32   = Result of 0, Carry = Bit 0 of RM
39   LSL >32   = Result of 0, Carry out 0
40   LSR   0   = LSR 32 (see below)
41   LSR  32   = Result of 0, Carry = Bit 31 of RM
42   LSR >32   = Result of 0, Carry out 0
43   ASR >=32  = ENTIRE Result = bit 31 of RM
44   ROR  32   = Result = RM, Carry = Bit 31 of RM
45   ROR >32   = Same result as ROR n-32 until amount in range of 1-32 then follow rules
46*/
47
48UINT32 decodeShift(arm_state *cpustate, UINT32 insn, UINT32 *pCarry)
49{
50    UINT32 k  = (insn & INSN_OP2_SHIFT) >> INSN_OP2_SHIFT_SHIFT;  // Bits 11-7
51    UINT32 rm = GET_REGISTER(cpustate, insn & INSN_OP2_RM);
52    UINT32 t  = (insn & INSN_OP2_SHIFT_TYPE) >> INSN_OP2_SHIFT_TYPE_SHIFT;
53
54    if ((insn & INSN_OP2_RM) == 0xf) {
55        // "If a register is used to specify the shift amount the PC will be 12 bytes ahead." (instead of 8)
56        rm += t & 1 ? 12 : 8;
57    }
58
59    /* All shift types ending in 1 are Rk, not #k */
60    if (t & 1)
61    {
62//      LOG(("%08x:  RegShift %02x %02x\n", R15, k >> 1, GET_REGISTER(cpustate, k >> 1)));
63#if ARM7_DEBUG_CORE
64            if ((insn & 0x80) == 0x80)
65                LOG(("%08x:  RegShift ERROR (p36)\n", R15));
66#endif
67
68        // see p35 for check on this
69        //k = GET_REGISTER(cpustate, k >> 1) & 0x1f;
70
71        // Keep only the bottom 8 bits for a Register Shift
72        k = GET_REGISTER(cpustate, k >> 1) & 0xff;
73
74        if (k == 0) /* Register shift by 0 is a no-op */
75        {
76//          LOG(("%08x:  NO-OP Regshift\n", R15));
77            /* TODO this is wrong for at least ROR by reg with lower
78             *      5 bits 0 but lower 8 bits non zero */
79            if (pCarry)
80                *pCarry = GET_CPSR & C_MASK;
81            return rm;
82        }
83    }
84    /* Decode the shift type and perform the shift */
85    switch (t >> 1)
86    {
87    case 0:                     /* LSL */
88        // LSL  32   = Result of 0, Carry = Bit 0 of RM
89        // LSL >32   = Result of 0, Carry out 0
90        if (k >= 32)
91        {
92            if (pCarry)
93                *pCarry = (k == 32) ? rm & 1 : 0;
94            return 0;
95        }
96        else
97        {
98            if (pCarry)
99            {
100            // LSL      0   = Result = RM, Carry = Old Contents of CPSR C Bit
101            // LSL (0,31)   = Result shifted, least significant bit is in carry out
102            *pCarry = k ? (rm & (1 << (32 - k))) : (GET_CPSR & C_MASK);
103            }
104            return k ? LSL(rm, k) : rm;
105        }
106        break;
107
108    case 1:                         /* LSR */
109        if (k == 0 || k == 32)
110        {
111            if (pCarry)
112                *pCarry = rm & SIGN_BIT;
113            return 0;
114        }
115        else if (k > 32)
116        {
117            if (pCarry)
118                *pCarry = 0;
119            return 0;
120        }
121        else
122        {
123            if (pCarry)
124                *pCarry = (rm & (1 << (k - 1)));
125            return LSR(rm, k);
126        }
127        break;
128
129    case 2:                     /* ASR */
130        if (k == 0 || k > 32)
131            k = 32;
132
133        if (pCarry)
134            *pCarry = (rm & (1 << (k - 1)));
135        if (k >= 32)
136            return rm & SIGN_BIT ? 0xffffffffu : 0;
137        else
138        {
139            if (rm & SIGN_BIT)
140                return LSR(rm, k) | (0xffffffffu << (32 - k));
141            else
142                return LSR(rm, k);
143        }
144        break;
145
146    case 3:                     /* ROR and RRX */
147        if (k)
148        {
149            while (k > 32)
150                k -= 32;
151            if (pCarry)
152                *pCarry = rm & (1 << (k - 1));
153            return ROR(rm, k);
154        }
155        else
156        {
157            /* RRX */
158            if (pCarry)
159                *pCarry = (rm & 1);
160            return LSR(rm, 1) | ((GET_CPSR & C_MASK) << 2);
161        }
162        break;
163    }
164
165    LOG(("%08x: Decodeshift error\n", R15));
166    return 0;
167} /* decodeShift */
168
169
170static int loadInc(arm_state *cpustate, UINT32 pat, UINT32 rbv, UINT32 s, int mode)
171{
172    int i, result;
173    UINT32 data;
174
175    result = 0;
176    rbv &= ~3;
177    for (i = 0; i < 16; i++)
178    {
179        if ((pat >> i) & 1)
180        {
181            if (cpustate->pendingAbtD == 0) // "Overwriting of registers stops when the abort happens."
182            {
183            data = READ32(rbv += 4);
184            if (i == 15) {
185                if (s) /* Pull full contents from stack */
186                    SET_MODE_REGISTER(cpustate, mode, 15, data);
187                else /* Pull only address, preserve mode & status flags */
188                   if (MODE32)
189                       SET_MODE_REGISTER(cpustate, mode, 15, data);
190                    else
191                    {
192                       SET_MODE_REGISTER(cpustate, mode, 15, (GET_MODE_REGISTER(cpustate, mode, 15) & ~0x03FFFFFC) | (data & 0x03FFFFFC));
193                    }
194            } else
195                SET_MODE_REGISTER(cpustate, mode, i, data);
196         }
197            result++;
198        }
199    }
200    return result;
201}
202
203static int loadDec(arm_state *cpustate, UINT32 pat, UINT32 rbv, UINT32 s, int mode)
204{
205    int i, result;
206    UINT32 data;
207
208    result = 0;
209    rbv &= ~3;
210    for (i = 15; i >= 0; i--)
211    {
212        if ((pat >> i) & 1)
213        {
214            if (cpustate->pendingAbtD == 0) // "Overwriting of registers stops when the abort happens."
215            {
216            data = READ32(rbv -= 4);
217            if (i == 15) {
218                if (s) /* Pull full contents from stack */
219                    SET_MODE_REGISTER(cpustate, mode, 15, data);
220                else /* Pull only address, preserve mode & status flags */
221                   if (MODE32)
222                       SET_MODE_REGISTER(cpustate, mode, 15, data);
223                    else
224                    {
225                       SET_MODE_REGISTER(cpustate, mode, 15, (GET_MODE_REGISTER(cpustate, mode, 15) & ~0x03FFFFFC) | (data & 0x03FFFFFC));
226                    }
227            }
228            else
229                SET_MODE_REGISTER(cpustate, mode, i, data);
230            }
231            result++;
232        }
233    }
234    return result;
235}
236
237static int storeInc(arm_state *cpustate, UINT32 pat, UINT32 rbv, int mode)
238{
239    int i, result;
240
241    result = 0;
242    for (i = 0; i < 16; i++)
243    {
244        if ((pat >> i) & 1)
245        {
246#if ARM7_DEBUG_CORE
247            if (i == 15) /* R15 is plus 12 from address of STM */
248                LOG(("%08x: StoreInc on R15\n", R15));
249#endif
250            WRITE32(rbv += 4, GET_MODE_REGISTER(cpustate, mode, i));
251            result++;
252        }
253    }
254    return result;
255} /* storeInc */
256
257static int storeDec(arm_state *cpustate, UINT32 pat, UINT32 rbv, int mode)
258{
259    int i, result;
260
261    result = 0;
262    for (i = 15; i >= 0; i--)
263    {
264        if ((pat >> i) & 1)
265        {
266#if ARM7_DEBUG_CORE
267            if (i == 15) /* R15 is plus 12 from address of STM */
268                LOG(("%08x: StoreDec on R15\n", R15));
269#endif
270            WRITE32(rbv -= 4, GET_MODE_REGISTER(cpustate, mode, i));
271            result++;
272        }
273    }
274    return result;
275} /* storeDec */
276
277/***************************************************************************
278 *                            OPCODE HANDLING
279 ***************************************************************************/
280
281// Co-Processor Data Operation
282static void HandleCoProcDO(arm_state *cpustate, UINT32 insn)
283{
284    // This instruction simply instructs the co-processor to do something, no data is returned to ARM7 core
285    if (arm7_coproc_do_callback)
286        arm7_coproc_do_callback(cpustate->device, insn, 0, 0);    // simply pass entire opcode to callback - since data format is actually dependent on co-proc implementation
287    else
288        LOG(("%08x: Co-Processor Data Operation executed, but no callback defined!\n", R15));
289}
290
291// Co-Processor Register Transfer - To/From Arm to Co-Proc
292static void HandleCoProcRT(arm_state *cpustate, UINT32 insn)
293{
294
295    /* xxxx 1110 oooL nnnn dddd cccc ppp1 mmmm */
296
297    // Load (MRC) data from Co-Proc to ARM7 register
298    if (insn & 0x00100000)       // Bit 20 = Load or Store
299    {
300        if (arm7_coproc_rt_r_callback)
301        {
302            UINT32 res = arm7_coproc_rt_r_callback(cpustate->device, insn, 0);   // RT Read handler must parse opcode & return appropriate result
303            if (cpustate->pendingUnd == 0)
304           {
305              SET_REGISTER(cpustate, (insn >> 12) & 0xf, res);
306           }
307        }
308        else
309            LOG(("%08x: Co-Processor Register Transfer executed, but no RT Read callback defined!\n", R15));
310    }
311    // Store (MCR) data from ARM7 to Co-Proc register
312    else
313    {
314        if (arm7_coproc_rt_w_callback)
315            arm7_coproc_rt_w_callback(cpustate->device, insn, GET_REGISTER(cpustate, (insn >> 12) & 0xf), 0);
316        else
317            LOG(("%08x: Co-Processor Register Transfer executed, but no RT Write callback defined!\n", R15));
318    }
319}
320
321/* Data Transfer - To/From Arm to Co-Proc
322   Loading or Storing, the co-proc function is responsible to read/write from the base register supplied + offset
323   8 bit immediate value Base Offset address is << 2 to get the actual #
324
325  issues - #1 - the co-proc function, needs direct access to memory reads or writes (ie, so we must send a pointer to a func)
326         - #2 - the co-proc may adjust the base address (especially if it reads more than 1 word), so a pointer to the register must be used
327                but the old value of the register must be restored if write back is not set..
328         - #3 - when post incrementing is used, it's up to the co-proc func. to add the offset, since the transfer
329                address supplied in that case, is simply the base. I suppose this is irrelevant if write back not set
330                but if co-proc reads multiple address, it must handle the offset adjustment itself.
331*/
332// todo: test with valid instructions
333static void HandleCoProcDT(arm_state *cpustate, UINT32 insn)
334{
335    UINT32 rn = (insn >> 16) & 0xf;
336    UINT32 rnv = GET_REGISTER(cpustate, rn);    // Get Address Value stored from Rn
337    UINT32 ornv = rnv;                // Keep value of Rn
338    UINT32 off = (insn & 0xff) << 2;  // Offset is << 2 according to manual
339    UINT32 *prn = &ARM7REG(rn);       // Pointer to our register, so it can be changed in the callback
340
341    // Pointers to read32/write32 functions
342    void (*write32)(arm_state *cpustate, UINT32 addr, UINT32 data);
343    UINT32 (*read32)(arm_state *cpustate, UINT32 addr);
344    write32 = PTR_WRITE32;
345    read32 = PTR_READ32;
346
347#if ARM7_DEBUG_CORE
348    if (((insn >> 16) & 0xf) == 15 && (insn & 0x200000))
349        LOG(("%08x: Illegal use of R15 as base for write back value!\n", R15));
350#endif
351
352    // Pre-Increment base address (IF POST INCREMENT - CALL BACK FUNCTION MUST DO IT)
353    if ((insn & 0x1000000) && off)
354    {
355        // Up - Down bit
356        if (insn & 0x800000)
357            rnv += off;
358        else
359            rnv -= off;
360    }
361
362    // Load (LDC) data from ARM7 memory to Co-Proc memory
363    if (insn & 0x00100000)
364    {
365        if (arm7_coproc_dt_r_callback)
366            arm7_coproc_dt_r_callback(cpustate, insn, prn, read32);
367        else
368            LOG(("%08x: Co-Processer Data Transfer executed, but no READ callback defined!\n", R15));
369    }
370    // Store (STC) data from Co-Proc to ARM7 memory
371    else
372    {
373        if (arm7_coproc_dt_w_callback)
374            arm7_coproc_dt_w_callback(cpustate, insn, prn, write32);
375        else
376            LOG(("%08x: Co-Processer Data Transfer executed, but no WRITE callback defined!\n", R15));
377    }
378
379   if (cpustate->pendingUnd != 0) return;
380
381    // If writeback not used - ensure the original value of RN is restored in case co-proc callback changed value
382    if ((insn & 0x200000) == 0)
383        SET_REGISTER(cpustate, rn, ornv);
384}
385
386INLINE void HandleBranch(arm_state *cpustate, UINT32 insn)
387{
388    UINT32 off = (insn & INSN_BRANCH) << 2;
389
390    /* Save PC into LR if this is a branch with link */
391    if (insn & INSN_BL)
392    {
393        SET_REGISTER(cpustate, 14, R15 + 4);
394    }
395
396    /* Sign-extend the 24-bit offset in our calculations */
397    if (off & 0x2000000u)
398    {
399       if (MODE32)
400           R15 -= ((~(off | 0xfc000000u)) + 1) - 8;
401        else
402           R15 = ((R15 - (((~(off | 0xfc000000u)) + 1) - 8)) & 0x03FFFFFC) | (R15 & ~0x03FFFFFC);
403    }
404    else
405    {
406       if (MODE32)
407           R15 += off + 8;
408        else
409           R15 = ((R15 + (off + 8)) & 0x03FFFFFC) | (R15 & ~0x03FFFFFC);
410    }
411}
412
413static void HandleMemSingle(arm_state *cpustate, UINT32 insn)
414{
415    UINT32 rn, rnv, off, rd, rnv_old = 0;
416
417    /* Fetch the offset */
418    if (insn & INSN_I)
419    {
420        /* Register Shift */
421        off = decodeShift(cpustate, insn, NULL);
422    }
423    else
424    {
425        /* Immediate Value */
426        off = insn & INSN_SDT_IMM;
427    }
428
429    /* Calculate Rn, accounting for PC */
430    rn = (insn & INSN_RN) >> INSN_RN_SHIFT;
431
432    if (insn & INSN_SDT_P)
433    {
434        /* Pre-indexed addressing */
435        if (insn & INSN_SDT_U)
436        {
437           if ((MODE32) || (rn != eR15))
438               rnv = (GET_REGISTER(cpustate, rn) + off);
439            else
440               rnv = (GET_PC + off);
441        }
442        else
443        {
444           if ((MODE32) || (rn != eR15))
445               rnv = (GET_REGISTER(cpustate, rn) - off);
446         else
447               rnv = (GET_PC - off);
448        }
449
450        if (insn & INSN_SDT_W)
451        {
452            rnv_old = GET_REGISTER(cpustate, rn);
453            SET_REGISTER(cpustate, rn, rnv);
454
455    // check writeback???
456        }
457        else if (rn == eR15)
458        {
459            rnv = rnv + 8;
460        }
461    }
462    else
463    {
464        /* Post-indexed addressing */
465        if (rn == eR15)
466        {
467           if (MODE32)
468               rnv = R15 + 8;
469           else
470               rnv = GET_PC + 8;
471        }
472        else
473        {
474            rnv = GET_REGISTER(cpustate, rn);
475        }
476    }
477
478    /* Do the transfer */
479    rd = (insn & INSN_RD) >> INSN_RD_SHIFT;
480    if (insn & INSN_SDT_L)
481    {
482        /* Load */
483        if (insn & INSN_SDT_B)
484        {
485            UINT32 data = READ8(rnv);
486            if (cpustate->pendingAbtD == 0)
487            {
488                SET_REGISTER(cpustate, rd, data);
489            }
490        }
491        else
492        {
493            UINT32 data = READ32(rnv);
494            if (cpustate->pendingAbtD == 0)
495            {
496                if (rd == eR15)
497                {
498                   if (MODE32)
499                     R15 = data - 4;
500                 else
501                    R15 = (R15 & ~0x03FFFFFC) /* N Z C V I F M1 M0 */ | ((data - 4) & 0x03FFFFFC);
502               // LDR, PC takes 2S + 2N + 1I (5 total cycles)
503                    ARM7_ICOUNT -= 2;
504                }
505                else
506                {
507                    SET_REGISTER(cpustate, rd, data);
508                }
509            }
510        }
511    }
512    else
513    {
514        /* Store */
515        if (insn & INSN_SDT_B)
516        {
517#if ARM7_DEBUG_CORE
518                if (rd == eR15)
519                    LOG(("Wrote R15 in byte mode\n"));
520#endif
521
522            WRITE8(rnv, (UINT8) GET_REGISTER(cpustate, rd) & 0xffu);
523        }
524        else
525        {
526#if ARM7_DEBUG_CORE
527                if (rd == eR15)
528                    LOG(("Wrote R15 in 32bit mode\n"));
529#endif
530
531            //WRITE32(rnv, rd == eR15 ? R15 + 8 : GET_REGISTER(cpustate, rd));
532            WRITE32(rnv, rd == eR15 ? R15 + 8 + 4 : GET_REGISTER(cpustate, rd)); // manual says STR rd = PC, +12
533        }
534        // Store takes only 2 N Cycles, so add + 1
535        ARM7_ICOUNT += 1;
536    }
537
538   if (cpustate->pendingAbtD != 0)
539   {
540      if ((insn & INSN_SDT_P) && (insn & INSN_SDT_W))
541      {
542         SET_REGISTER(cpustate, rn, rnv_old);
543      }
544   }
545   else
546   {
547
548    /* Do post-indexing writeback */
549    if (!(insn & INSN_SDT_P)/* && (insn & INSN_SDT_W)*/)
550    {
551        if (insn & INSN_SDT_U)
552        {
553            /* Writeback is applied in pipeline, before value is read from mem,
554                so writeback is effectively ignored */
555            if (rd == rn) {
556                SET_REGISTER(cpustate, rn, GET_REGISTER(cpustate, rd));
557                // todo: check for offs... ?
558            }
559            else {
560
561                if ((insn & INSN_SDT_W) != 0)
562                    LOG(("%08x:  RegisterWritebackIncrement %d %d %d\n", R15, (insn & INSN_SDT_P) != 0, (insn & INSN_SDT_W) != 0, (insn & INSN_SDT_U) != 0));
563
564                SET_REGISTER(cpustate, rn, (rnv + off));
565            }
566        }
567        else
568        {
569            /* Writeback is applied in pipeline, before value is read from mem,
570                so writeback is effectively ignored */
571            if (rd == rn) {
572                SET_REGISTER(cpustate, rn, GET_REGISTER(cpustate, rd));
573            }
574            else {
575                SET_REGISTER(cpustate, rn, (rnv - off));
576
577                if ((insn & INSN_SDT_W) != 0)
578                    LOG(("%08x:  RegisterWritebackDecrement %d %d %d\n", R15, (insn & INSN_SDT_P) != 0, (insn & INSN_SDT_W) != 0, (insn & INSN_SDT_U) != 0));
579            }
580        }
581    }
582
583   }
584
585//  ARM7_CHECKIRQ
586
587} /* HandleMemSingle */
588
589static void HandleHalfWordDT(arm_state *cpustate, UINT32 insn)
590{
591    UINT32 rn, rnv, off, rd, rnv_old = 0;
592
593    // Immediate or Register Offset?
594    if (insn & 0x400000) {               // Bit 22 - 1 = immediate, 0 = register
595        // imm. value in high nibble (bits 8-11) and lo nibble (bit 0-3)
596        off = (((insn >> 8) & 0x0f) << 4) | (insn & 0x0f);
597    }
598    else {
599        // register
600        off = GET_REGISTER(cpustate, insn & 0x0f);
601    }
602
603    /* Calculate Rn, accounting for PC */
604    rn = (insn & INSN_RN) >> INSN_RN_SHIFT;
605
606    if (insn & INSN_SDT_P)
607    {
608        /* Pre-indexed addressing */
609        if (insn & INSN_SDT_U)
610        {
611            rnv = (GET_REGISTER(cpustate, rn) + off);
612        }
613        else
614        {
615            rnv = (GET_REGISTER(cpustate, rn) - off);
616        }
617
618        if (insn & INSN_SDT_W)
619        {
620            rnv_old = GET_REGISTER(cpustate, rn);
621            SET_REGISTER(cpustate, rn, rnv);
622
623        // check writeback???
624        }
625        else if (rn == eR15)
626        {
627            rnv = (rnv) + 8;
628        }
629    }
630    else
631    {
632        /* Post-indexed addressing */
633        if (rn == eR15)
634        {
635            rnv = R15 + 8;
636        }
637        else
638        {
639            rnv = GET_REGISTER(cpustate, rn);
640        }
641    }
642
643    /* Do the transfer */
644    rd = (insn & INSN_RD) >> INSN_RD_SHIFT;
645
646    /* Load */
647    if (insn & INSN_SDT_L)
648    {
649        // Signed?
650        if (insn & 0x40)
651        {
652            UINT32 newval = 0;
653
654            // Signed Half Word?
655            if (insn & 0x20) {
656                UINT16 signbyte, databyte;
657                databyte = READ16(rnv) & 0xFFFF;
658                signbyte = (databyte & 0x8000) ? 0xffff : 0;
659                newval = (UINT32)(signbyte << 16)|databyte;
660            }
661            // Signed Byte
662            else {
663                UINT8 databyte;
664                UINT32 signbyte;
665                databyte = READ8(rnv) & 0xff;
666                signbyte = (databyte & 0x80) ? 0xffffff : 0;
667                newval = (UINT32)(signbyte << 8)|databyte;
668            }
669
670         if (cpustate->pendingAbtD == 0)
671         {
672
673            // PC?
674            if (rd == eR15)
675            {
676                R15 = newval + 8;
677                // LDR(H,SH,SB) PC takes 2S + 2N + 1I (5 total cycles)
678                ARM7_ICOUNT -= 2;
679
680            }
681            else
682            {
683                SET_REGISTER(cpustate, rd, newval);
684                R15 += 4;
685            }
686
687            }
688            else
689            {
690                R15 += 4;
691            }
692
693        }
694        // Unsigned Half Word
695        else
696        {
697         UINT32 newval = READ16(rnv);
698
699         if (cpustate->pendingAbtD == 0)
700         {
701
702            if (rd == eR15)
703            {
704                R15 = newval + 8;
705                // extra cycles for LDR(H,SH,SB) PC (5 total cycles)
706                ARM7_ICOUNT -= 2;
707            }
708            else
709            {
710                SET_REGISTER(cpustate, rd, newval);
711                R15 += 4;
712            }
713
714            }
715            else
716            {
717                R15 += 4;
718            }
719
720        }
721
722
723    }
724    /* Store or ARMv5+ dword insns */
725    else
726    {
727       if ((insn & 0x60) == 0x40)   // LDRD
728   {
729      SET_REGISTER(cpustate, rd, READ32(rnv));
730      SET_REGISTER(cpustate, rd+1, READ32(rnv+4));
731                R15 += 4;
732   }
733       else if ((insn & 0x60) == 0x60)   // STRD
734   {
735      WRITE32(rnv, GET_REGISTER(cpustate, rd));
736      WRITE32(rnv+4, GET_REGISTER(cpustate, rd+1));
737                R15 += 4;
738   }
739   else
740   {
741           // WRITE16(rnv, rd == eR15 ? R15 + 8 : GET_REGISTER(cpustate, rd));
742           WRITE16(rnv, rd == eR15 ? R15 + 8 + 4 : GET_REGISTER(cpustate, rd)); // manual says STR RD=PC, +12 of address
743
744// if R15 is not increased then e.g. "STRH R10, [R15,#$10]" will be executed over and over again
745#if 0
746           if (rn != eR15)
747#endif
748               R15 += 4;
749
750           // STRH takes 2 cycles, so we add + 1
751           ARM7_ICOUNT += 1;
752   }
753    }
754
755   if (cpustate->pendingAbtD != 0)
756   {
757      if ((insn & INSN_SDT_P) && (insn & INSN_SDT_W))
758      {
759         SET_REGISTER(cpustate, rn, rnv_old);
760      }
761   }
762   else
763   {
764
765    // SJE: No idea if this writeback code works or makes sense here..
766
767    /* Do post-indexing writeback */
768    if (!(insn & INSN_SDT_P)/* && (insn & INSN_SDT_W)*/)
769    {
770        if (insn & INSN_SDT_U)
771        {
772            /* Writeback is applied in pipeline, before value is read from mem,
773                so writeback is effectively ignored */
774            if (rd == rn) {
775                SET_REGISTER(cpustate, rn, GET_REGISTER(cpustate, rd));
776                // todo: check for offs... ?
777            }
778            else {
779
780                if ((insn & INSN_SDT_W) != 0)
781                    LOG(("%08x:  RegisterWritebackIncrement %d %d %d\n", R15, (insn & INSN_SDT_P) != 0, (insn & INSN_SDT_W) != 0, (insn & INSN_SDT_U) != 0));
782
783                SET_REGISTER(cpustate, rn, (rnv + off));
784            }
785        }
786        else
787        {
788            /* Writeback is applied in pipeline, before value is read from mem,
789                so writeback is effectively ignored */
790            if (rd == rn) {
791                SET_REGISTER(cpustate, rn, GET_REGISTER(cpustate, rd));
792            }
793            else {
794                SET_REGISTER(cpustate, rn, (rnv - off));
795
796                if ((insn & INSN_SDT_W) != 0)
797                    LOG(("%08x:  RegisterWritebackDecrement %d %d %d\n", R15, (insn & INSN_SDT_P) != 0, (insn & INSN_SDT_W) != 0, (insn & INSN_SDT_U) != 0));
798            }
799        }
800    }
801
802    }
803
804}
805
806static void HandleSwap(arm_state *cpustate, UINT32 insn)
807{
808    UINT32 rn, rm, rd, tmp;
809
810    rn = GET_REGISTER(cpustate, (insn >> 16) & 0xf);  // reg. w/read address
811    rm = GET_REGISTER(cpustate, insn & 0xf);          // reg. w/write address
812    rd = (insn >> 12) & 0xf;                // dest reg
813
814#if ARM7_DEBUG_CORE
815    if (rn == 15 || rm == 15 || rd == 15)
816        LOG(("%08x: Illegal use of R15 in Swap Instruction\n", R15));
817#endif
818
819    // can be byte or word
820    if (insn & 0x400000)
821    {
822        tmp = READ8(rn);
823        WRITE8(rn, rm);
824        SET_REGISTER(cpustate, rd, tmp);
825    }
826    else
827    {
828        tmp = READ32(rn);
829        WRITE32(rn, rm);
830        SET_REGISTER(cpustate, rd, tmp);
831    }
832
833    R15 += 4;
834    // Instruction takes 1S+2N+1I cycles - so we subtract one more..
835    ARM7_ICOUNT -= 1;
836}
837
838static void HandlePSRTransfer(arm_state *cpustate, UINT32 insn)
839{
840    int reg = (insn & 0x400000) ? SPSR : eCPSR; // Either CPSR or SPSR
841    UINT32 newval, val = 0;
842    int oldmode = GET_CPSR & MODE_FLAG;
843
844    // get old value of CPSR/SPSR
845    newval = GET_REGISTER(cpustate, reg);
846
847    // MSR (bit 21 set) - Copy value to CPSR/SPSR
848    if ((insn & 0x00200000))
849    {
850        // Immediate Value?
851        if (insn & INSN_I) {
852            // Value can be specified for a Right Rotate, 2x the value specified.
853            int by = (insn & INSN_OP2_ROTATE) >> INSN_OP2_ROTATE_SHIFT;
854            if (by)
855                val = ROR(insn & INSN_OP2_IMM, by << 1);
856            else
857                val = insn & INSN_OP2_IMM;
858        }
859        // Value from Register
860        else
861        {
862            val = GET_REGISTER(cpustate, insn & 0x0f);
863        }
864
865        // apply field code bits
866        if (reg == eCPSR)
867        {
868            if (oldmode != eARM7_MODE_USER)
869            {
870                if (insn & 0x00010000)
871                {
872                    newval = (newval & 0xffffff00) | (val & 0x000000ff);
873                }
874                if (insn & 0x00020000)
875                {
876                    newval = (newval & 0xffff00ff) | (val & 0x0000ff00);
877                }
878                if (insn & 0x00040000)
879                {
880                    newval = (newval & 0xff00ffff) | (val & 0x00ff0000);
881                }
882            }
883
884            // status flags can be modified regardless of mode
885            if (insn & 0x00080000)
886            {
887                // TODO for non ARMv5E mask should be 0xf0000000 (ie mask Q bit)
888                newval = (newval & 0x00ffffff) | (val & 0xf8000000);
889            }
890        }
891        else    // SPSR has stricter requirements
892        {
893            if (((GET_CPSR & 0x1f) > 0x10) && ((GET_CPSR & 0x1f) < 0x1f))
894            {
895                if (insn & 0x00010000)
896                {
897                    newval = (newval & 0xffffff00) | (val & 0xff);
898                }
899                if (insn & 0x00020000)
900                {
901                    newval = (newval & 0xffff00ff) | (val & 0xff00);
902                }
903                if (insn & 0x00040000)
904                {
905                    newval = (newval & 0xff00ffff) | (val & 0xff0000);
906                }
907                if (insn & 0x00080000)
908                {
909                    // TODO for non ARMv5E mask should be 0xf0000000 (ie mask Q bit)
910                    newval = (newval & 0x00ffffff) | (val & 0xf8000000);
911                }
912            }
913        }
914
915#if 0
916        // force valid mode
917        newval |= 0x10;
918#endif
919
920        // Update the Register
921        if (reg == eCPSR)
922           SET_CPSR(newval);
923        else
924           SET_REGISTER(cpustate, reg, newval);
925
926        // Switch to new mode if changed
927        if ((newval & MODE_FLAG) != oldmode)
928            SwitchMode(cpustate, GET_MODE);
929
930    }
931    // MRS (bit 21 clear) - Copy CPSR or SPSR to specified Register
932    else
933    {
934        SET_REGISTER(cpustate, (insn >> 12)& 0x0f, GET_REGISTER(cpustate, reg));
935    }
936}
937
938static void HandleALU(arm_state *cpustate, UINT32 insn)
939{
940    UINT32 op2, sc = 0, rd, rn, opcode;
941    UINT32 by, rdn;
942
943   // Normal Data Processing : 1S
944   // Data Processing with register specified shift : 1S + 1I
945   // Data Processing with PC written : 2S + 1N
946   // Data Processing with register specified shift and PC written : 2S + 1N + 1I
947
948    opcode = (insn & INSN_OPCODE) >> INSN_OPCODE_SHIFT;
949
950    rd = 0;
951    rn = 0;
952
953    /* --------------*/
954    /* Construct Op2 */
955    /* --------------*/
956
957    /* Immediate constant */
958    if (insn & INSN_I)
959    {
960        by = (insn & INSN_OP2_ROTATE) >> INSN_OP2_ROTATE_SHIFT;
961        if (by)
962        {
963            op2 = ROR(insn & INSN_OP2_IMM, by << 1);
964            sc = op2 & SIGN_BIT;
965        }
966        else
967        {
968            op2 = insn & INSN_OP2;      // SJE: Shouldn't this be INSN_OP2_IMM?
969            sc = GET_CPSR & C_MASK;
970        }
971    }
972    /* Op2 = Register Value */
973    else
974    {
975        op2 = decodeShift(cpustate, insn, (insn & INSN_S) ? &sc : NULL);
976
977        // LD TODO sc will always be 0 if this applies
978        if (!(insn & INSN_S))
979            sc = 0;
980
981        // extra cycle (register specified shift)
982        ARM7_ICOUNT -= 1;
983    }
984
985    // LD TODO this comment is wrong
986    /* Calculate Rn to account for pipelining */
987    if ((opcode & 0xd) != 0xd) /* No Rn in MOV */
988    {
989        if ((rn = (insn & INSN_RN) >> INSN_RN_SHIFT) == eR15)
990        {
991#if ARM7_DEBUG_CORE
992            LOG(("%08x:  Pipelined R15 (Shift %d)\n", R15, (insn & INSN_I ? 8 : insn & 0x10u ? 12 : 12)));
993#endif
994         if (MODE32)
995               rn = R15 + 8;
996            else
997               rn = GET_PC + 8;
998        }
999        else
1000        {
1001            rn = GET_REGISTER(cpustate, rn);
1002        }
1003    }
1004
1005    /* Perform the operation */
1006
1007    switch (opcode)
1008    {
1009    /* Arithmetic operations */
1010    case OPCODE_SBC:
1011        rd = (rn - op2 - (GET_CPSR & C_MASK ? 0 : 1));
1012        HandleALUSubFlags(rd, rn, op2);
1013        break;
1014    case OPCODE_CMP:
1015    case OPCODE_SUB:
1016        rd = (rn - op2);
1017        HandleALUSubFlags(rd, rn, op2);
1018        break;
1019    case OPCODE_RSC:
1020        rd = (op2 - rn - (GET_CPSR & C_MASK ? 0 : 1));
1021        HandleALUSubFlags(rd, op2, rn);
1022        break;
1023    case OPCODE_RSB:
1024        rd = (op2 - rn);
1025        HandleALUSubFlags(rd, op2, rn);
1026        break;
1027    case OPCODE_ADC:
1028        rd = (rn + op2 + ((GET_CPSR & C_MASK) >> C_BIT));
1029        HandleALUAddFlags(rd, rn, op2);
1030        break;
1031    case OPCODE_CMN:
1032    case OPCODE_ADD:
1033        rd = (rn + op2);
1034        HandleALUAddFlags(rd, rn, op2);
1035        break;
1036
1037    /* Logical operations */
1038    case OPCODE_AND:
1039    case OPCODE_TST:
1040        rd = rn & op2;
1041        HandleALULogicalFlags(rd, sc);
1042        break;
1043    case OPCODE_BIC:
1044        rd = rn & ~op2;
1045        HandleALULogicalFlags(rd, sc);
1046        break;
1047    case OPCODE_TEQ:
1048    case OPCODE_EOR:
1049        rd = rn ^ op2;
1050        HandleALULogicalFlags(rd, sc);
1051        break;
1052    case OPCODE_ORR:
1053        rd = rn | op2;
1054        HandleALULogicalFlags(rd, sc);
1055        break;
1056    case OPCODE_MOV:
1057        rd = op2;
1058        HandleALULogicalFlags(rd, sc);
1059        break;
1060    case OPCODE_MVN:
1061        rd = (~op2);
1062        HandleALULogicalFlags(rd, sc);
1063        break;
1064    }
1065
1066    /* Put the result in its register if not one of the test only opcodes (TST,TEQ,CMP,CMN) */
1067    rdn = (insn & INSN_RD) >> INSN_RD_SHIFT;
1068    if ((opcode & 0xc) != 0x8)
1069    {
1070        // If Rd = R15, but S Flag not set, Result is placed in R15, but CPSR is not affected (page 44)
1071        if (rdn == eR15 && !(insn & INSN_S))
1072        {
1073         if (MODE32)
1074         {
1075               R15 = rd;
1076           }
1077           else
1078           {
1079               R15 = (R15 & ~0x03FFFFFC) | (rd & 0x03FFFFFC);
1080           }
1081            // extra cycles (PC written)
1082            ARM7_ICOUNT -= 2;
1083        }
1084        else
1085        {
1086            // Rd = 15 and S Flag IS set, Result is placed in R15, and current mode SPSR moved to CPSR
1087            if (rdn == eR15) {
1088
1089            if (MODE32)
1090            {
1091            // When Rd is R15 and the S flag is set the result of the operation is placed in R15 and the SPSR corresponding to
1092            // the current mode is moved to the CPSR. This allows state changes which automatically restore both PC and
1093            // CPSR. --> This form of instruction should not be used in User mode. <--
1094
1095            if (GET_MODE != eARM7_MODE_USER)
1096            {
1097                   // Update CPSR from SPSR
1098                   SET_CPSR(GET_REGISTER(cpustate, SPSR));
1099                   SwitchMode(cpustate, GET_MODE);
1100               }
1101
1102                R15 = rd;
1103
1104                }
1105                else
1106                {
1107                  UINT32 temp;
1108               R15 = rd; //(R15 & 0x03FFFFFC) | (rd & 0xFC000003);
1109               temp = (GET_CPSR & 0x0FFFFF20) | (rd & 0xF0000000) /* N Z C V */ | ((rd & 0x0C000000) >> (26 - 6)) /* I F */ | (rd & 0x00000003) /* M1 M0 */;
1110               SET_CPSR( temp);
1111               SwitchMode( cpustate, temp & 3);
1112                }
1113
1114                // extra cycles (PC written)
1115                ARM7_ICOUNT -= 2;
1116
1117                /* IRQ masks may have changed in this instruction */
1118//              ARM7_CHECKIRQ;
1119            }
1120            else
1121                /* S Flag is set - Write results to register & update CPSR (which was already handled using HandleALU flag macros) */
1122                SET_REGISTER(cpustate, rdn, rd);
1123        }
1124    }
1125    // SJE: Don't think this applies any more.. (see page 44 at bottom)
1126    /* TST & TEQ can affect R15 (the condition code register) with the S bit set */
1127    else if (rdn == eR15)
1128    {
1129        if (insn & INSN_S) {
1130#if ARM7_DEBUG_CORE
1131                LOG(("%08x: TST class on R15 s bit set\n", R15));
1132#endif
1133         if (MODE32)
1134               R15 = rd;
1135            else
1136            {
1137               UINT32 temp;
1138            R15 = (R15 & 0x03FFFFFC) | (rd & ~0x03FFFFFC);
1139            temp = (GET_CPSR & 0x0FFFFF20) | (rd & 0xF0000000) /* N Z C V */ | ((rd & 0x0C000000) >> (26 - 6)) /* I F */ | (rd & 0x00000003) /* M1 M0 */;
1140            SET_CPSR( temp);
1141            SwitchMode( cpustate, temp & 3);
1142         }
1143
1144            /* IRQ masks may have changed in this instruction */
1145//          ARM7_CHECKIRQ;
1146        }
1147        else
1148        {
1149#if ARM7_DEBUG_CORE
1150                LOG(("%08x: TST class on R15 no s bit set\n", R15));
1151#endif
1152        }
1153        // extra cycles (PC written)
1154        ARM7_ICOUNT -= 2;
1155    }
1156
1157    // compensate for the -3 at the end
1158    ARM7_ICOUNT += 2;
1159}
1160
1161static void HandleMul(arm_state *cpustate, UINT32 insn)
1162{
1163    UINT32 r, rm, rs;
1164
1165   // MUL takes 1S + mI and MLA 1S + (m+1)I cycles to execute, where S and I are as
1166   // defined in 6.2 Cycle Types on page 6-2.
1167   // m is the number of 8 bit multiplier array cycles required to complete the
1168   // multiply, which is controlled by the value of the multiplier operand
1169   // specified by Rs.
1170
1171   rm = GET_REGISTER(cpustate, insn & INSN_MUL_RM);
1172   rs = GET_REGISTER(cpustate, (insn & INSN_MUL_RS) >> INSN_MUL_RS_SHIFT);
1173
1174    /* Do the basic multiply of Rm and Rs */
1175    r = rm * rs;
1176
1177#if ARM7_DEBUG_CORE
1178    if ((insn & INSN_MUL_RM) == 0xf ||
1179        ((insn & INSN_MUL_RS) >> INSN_MUL_RS_SHIFT) == 0xf ||
1180        ((insn & INSN_MUL_RN) >> INSN_MUL_RN_SHIFT) == 0xf)
1181        LOG(("%08x:  R15 used in mult\n", R15));
1182#endif
1183
1184    /* Add on Rn if this is a MLA */
1185    if (insn & INSN_MUL_A)
1186    {
1187        r += GET_REGISTER(cpustate, (insn & INSN_MUL_RN) >> INSN_MUL_RN_SHIFT);
1188        // extra cycle for MLA
1189      ARM7_ICOUNT -= 1;
1190    }
1191
1192    /* Write the result */
1193    SET_REGISTER(cpustate, (insn & INSN_MUL_RD) >> INSN_MUL_RD_SHIFT, r);
1194
1195    /* Set N and Z if asked */
1196    if (insn & INSN_S)
1197    {
1198        SET_CPSR((GET_CPSR & ~(N_MASK | Z_MASK)) | HandleALUNZFlags(r));
1199    }
1200
1201   if (rs & SIGN_BIT) rs = -rs;
1202   if (rs < 0x00000100) ARM7_ICOUNT -= 1 + 1;
1203   else if (rs < 0x00010000) ARM7_ICOUNT -= 1 + 2;
1204   else if (rs < 0x01000000) ARM7_ICOUNT -= 1 + 3;
1205   else ARM7_ICOUNT -= 1 + 4;
1206
1207   ARM7_ICOUNT += 3;
1208}
1209
1210// todo: add proper cycle counts
1211static void HandleSMulLong(arm_state *cpustate, UINT32 insn)
1212{
1213    INT32 rm, rs;
1214    UINT32 rhi, rlo;
1215    INT64 res = 0;
1216
1217   // MULL takes 1S + (m+1)I and MLAL 1S + (m+2)I cycles to execute, where m is the
1218   // number of 8 bit multiplier array cycles required to complete the multiply, which is
1219   // controlled by the value of the multiplier operand specified by Rs.
1220
1221    rm  = (INT32)GET_REGISTER(cpustate, insn & 0xf);
1222    rs  = (INT32)GET_REGISTER(cpustate, ((insn >> 8) & 0xf));
1223    rhi = (insn >> 16) & 0xf;
1224    rlo = (insn >> 12) & 0xf;
1225
1226#if ARM7_DEBUG_CORE
1227        if ((insn & 0xf) == 15 || ((insn >> 8) & 0xf) == 15 || ((insn >> 16) & 0xf) == 15 || ((insn >> 12) & 0xf) == 15)
1228            LOG(("%08x: Illegal use of PC as a register in SMULL opcode\n", R15));
1229#endif
1230
1231    /* Perform the multiplication */
1232    res = (INT64)rm * rs;
1233
1234    /* Add on Rn if this is a MLA */
1235    if (insn & INSN_MUL_A)
1236    {
1237        INT64 acum = (INT64)((((INT64)(GET_REGISTER(cpustate, rhi))) << 32) | GET_REGISTER(cpustate, rlo));
1238        res += acum;
1239        // extra cycle for MLA
1240      ARM7_ICOUNT -= 1;
1241    }
1242
1243    /* Write the result (upper dword goes to RHi, lower to RLo) */
1244    SET_REGISTER(cpustate, rhi, res >> 32);
1245    SET_REGISTER(cpustate, rlo, res & 0xFFFFFFFF);
1246
1247    /* Set N and Z if asked */
1248    if (insn & INSN_S)
1249    {
1250        SET_CPSR((GET_CPSR & ~(N_MASK | Z_MASK)) | HandleLongALUNZFlags(res));
1251    }
1252
1253   if (rs < 0) rs = -rs;
1254   if (rs < 0x00000100) ARM7_ICOUNT -= 1 + 1 + 1;
1255   else if (rs < 0x00010000) ARM7_ICOUNT -= 1 + 2 + 1;
1256   else if (rs < 0x01000000) ARM7_ICOUNT -= 1 + 3 + 1;
1257   else ARM7_ICOUNT -= 1 + 4 + 1;
1258
1259   ARM7_ICOUNT += 3;
1260}
1261
1262// todo: add proper cycle counts
1263static void HandleUMulLong(arm_state *cpustate, UINT32 insn)
1264{
1265    UINT32 rm, rs;
1266    UINT32 rhi, rlo;
1267    UINT64 res = 0;
1268
1269   // MULL takes 1S + (m+1)I and MLAL 1S + (m+2)I cycles to execute, where m is the
1270   // number of 8 bit multiplier array cycles required to complete the multiply, which is
1271   // controlled by the value of the multiplier operand specified by Rs.
1272
1273    rm  = (INT32)GET_REGISTER(cpustate, insn & 0xf);
1274    rs  = (INT32)GET_REGISTER(cpustate, ((insn >> 8) & 0xf));
1275    rhi = (insn >> 16) & 0xf;
1276    rlo = (insn >> 12) & 0xf;
1277
1278#if ARM7_DEBUG_CORE
1279        if (((insn & 0xf) == 15) || (((insn >> 8) & 0xf) == 15) || (((insn >> 16) & 0xf) == 15) || (((insn >> 12) & 0xf) == 15))
1280            LOG(("%08x: Illegal use of PC as a register in SMULL opcode\n", R15));
1281#endif
1282
1283    /* Perform the multiplication */
1284    res = (UINT64)rm * rs;
1285
1286    /* Add on Rn if this is a MLA */
1287    if (insn & INSN_MUL_A)
1288    {
1289        UINT64 acum = (UINT64)((((UINT64)(GET_REGISTER(cpustate, rhi))) << 32) | GET_REGISTER(cpustate, rlo));
1290        res += acum;
1291        // extra cycle for MLA
1292      ARM7_ICOUNT -= 1;
1293    }
1294
1295    /* Write the result (upper dword goes to RHi, lower to RLo) */
1296    SET_REGISTER(cpustate, rhi, res >> 32);
1297    SET_REGISTER(cpustate, rlo, res & 0xFFFFFFFF);
1298
1299    /* Set N and Z if asked */
1300    if (insn & INSN_S)
1301    {
1302        SET_CPSR((GET_CPSR & ~(N_MASK | Z_MASK)) | HandleLongALUNZFlags(res));
1303    }
1304
1305   if (rs < 0x00000100) ARM7_ICOUNT -= 1 + 1 + 1;
1306   else if (rs < 0x00010000) ARM7_ICOUNT -= 1 + 2 + 1;
1307   else if (rs < 0x01000000) ARM7_ICOUNT -= 1 + 3 + 1;
1308   else ARM7_ICOUNT -= 1 + 4 + 1;
1309
1310   ARM7_ICOUNT += 3;
1311}
1312
1313static void HandleMemBlock(arm_state *cpustate, UINT32 insn)
1314{
1315    UINT32 rb = (insn & INSN_RN) >> INSN_RN_SHIFT;
1316    UINT32 rbp = GET_REGISTER(cpustate, rb);
1317    int result;
1318
1319#if ARM7_DEBUG_CORE
1320    if (rbp & 3)
1321        LOG(("%08x: Unaligned Mem Transfer @ %08x\n", R15, rbp));
1322#endif
1323
1324   // Normal LDM instructions take nS + 1N + 1I and LDM PC takes (n+1)S + 2N + 1I
1325   // incremental cycles, where S,N and I are as defined in 6.2 Cycle Types on page 6-2.
1326   // STM instructions take (n-1)S + 2N incremental cycles to execute, where n is the
1327   // number of words transferred.
1328
1329    if (insn & INSN_BDT_L)
1330    {
1331        /* Loading */
1332        if (insn & INSN_BDT_U)
1333        {
1334            /* Incrementing */
1335            if (!(insn & INSN_BDT_P))
1336            {
1337                rbp = rbp + (- 4);
1338            }
1339
1340            // S Flag Set, but R15 not in list = User Bank Transfer
1341            if (insn & INSN_BDT_S && (insn & 0x8000) == 0)
1342            {
1343               // !! actually switching to user mode triggers a section permission fault in Happy Fish 302-in-1 (BP C0030DF4, press F5 ~16 times) !!
1344                // set to user mode - then do the transfer, and set back
1345                //int curmode = GET_MODE;
1346                //SwitchMode(cpustate, eARM7_MODE_USER);
1347                LOG(("%08x: User Bank Transfer not fully tested - please check if working properly!\n", R15));
1348                result = loadInc(cpustate, insn & 0xffff, rbp, insn & INSN_BDT_S, eARM7_MODE_USER);
1349                // todo - not sure if Writeback occurs on User registers also..
1350                //SwitchMode(cpustate, curmode);
1351            }
1352            else
1353                result = loadInc(cpustate, insn & 0xffff, rbp, insn & INSN_BDT_S, GET_MODE);
1354
1355            if ((insn & INSN_BDT_W) && (cpustate->pendingAbtD == 0))
1356            {
1357#if ARM7_DEBUG_CORE
1358                    if (rb == 15)
1359                        LOG(("%08x:  Illegal LDRM writeback to r15\n", R15));
1360#endif
1361            // "A LDM will always overwrite the updated base if the base is in the list." (also for a user bank transfer?)
1362            // GBA "V-Rally 3" expects R0 not to be overwritten with the updated base value [BP 8077B0C]
1363            if (((insn >> rb) & 1) == 0)
1364            {
1365               SET_REGISTER(cpustate, rb, GET_REGISTER(cpustate, rb) + result * 4);
1366            }
1367            }
1368
1369            // R15 included? (NOTE: CPSR restore must occur LAST otherwise wrong registers restored!)
1370            if ((insn & 0x8000) && (cpustate->pendingAbtD == 0)) {
1371                R15 -= 4;     // SJE: I forget why i did this?
1372                // S - Flag Set? Signals transfer of current mode SPSR->CPSR
1373                if (insn & INSN_BDT_S)
1374            {
1375                   if (MODE32)
1376                   {
1377                       SET_CPSR(GET_REGISTER(cpustate, SPSR));
1378                       SwitchMode(cpustate, GET_MODE);
1379                    }
1380                    else
1381                    {
1382                       UINT32 temp;
1383//                      LOG(("LDM + S | R15 %08X CPSR %08X\n", R15, GET_CPSR));
1384                  temp = (GET_CPSR & 0x0FFFFF20) | (R15 & 0xF0000000) /* N Z C V */ | ((R15 & 0x0C000000) >> (26 - 6)) /* I F */ | (R15 & 0x00000003) /* M1 M0 */;
1385                  SET_CPSR( temp);
1386                  SwitchMode(cpustate, temp & 3);
1387                    }
1388                }
1389                // LDM PC - takes 2 extra cycles
1390                ARM7_ICOUNT -= 2;
1391            }
1392        }
1393        else
1394        {
1395            /* Decrementing */
1396            if (!(insn & INSN_BDT_P))
1397            {
1398                rbp = rbp - (- 4);
1399            }
1400
1401            // S Flag Set, but R15 not in list = User Bank Transfer
1402            if (insn & INSN_BDT_S && ((insn & 0x8000) == 0))
1403            {
1404                // set to user mode - then do the transfer, and set back
1405                //int curmode = GET_MODE;
1406                //SwitchMode(cpustate, eARM7_MODE_USER);
1407                LOG(("%08x: User Bank Transfer not fully tested - please check if working properly!\n", R15));
1408                result = loadDec(cpustate, insn & 0xffff, rbp, insn & INSN_BDT_S, eARM7_MODE_USER);
1409                // todo - not sure if Writeback occurs on User registers also..
1410                //SwitchMode(cpustate, curmode);
1411            }
1412            else
1413                result = loadDec(cpustate, insn & 0xffff, rbp, insn & INSN_BDT_S, GET_MODE);
1414
1415            if ((insn & INSN_BDT_W) && (cpustate->pendingAbtD == 0))
1416            {
1417                if (rb == 0xf)
1418                    LOG(("%08x:  Illegal LDRM writeback to r15\n", R15));
1419            // "A LDM will always overwrite the updated base if the base is in the list." (also for a user bank transfer?)
1420            if (((insn >> rb) & 1) == 0)
1421            {
1422               SET_REGISTER(cpustate, rb, GET_REGISTER(cpustate, rb) - result * 4);
1423            }
1424            }
1425
1426            // R15 included? (NOTE: CPSR restore must occur LAST otherwise wrong registers restored!)
1427            if ((insn & 0x8000) && (cpustate->pendingAbtD == 0)) {
1428                R15 -= 4;     // SJE: I forget why i did this?
1429                // S - Flag Set? Signals transfer of current mode SPSR->CPSR
1430                if (insn & INSN_BDT_S)
1431            {
1432                   if (MODE32)
1433                   {
1434                       SET_CPSR(GET_REGISTER(cpustate, SPSR));
1435                       SwitchMode(cpustate, GET_MODE);
1436                    }
1437                    else
1438                    {
1439                       UINT32 temp;
1440//                      LOG(("LDM + S | R15 %08X CPSR %08X\n", R15, GET_CPSR));
1441                  temp = (GET_CPSR & 0x0FFFFF20) /* N Z C V I F M4 M3 M2 M1 M0 */ | (R15 & 0xF0000000) /* N Z C V */ | ((R15 & 0x0C000000) >> (26 - 6)) /* I F */ | (R15 & 0x00000003) /* M1 M0 */;
1442                  SET_CPSR( temp);
1443                  SwitchMode(cpustate, temp & 3);
1444                    }
1445                }
1446                // LDM PC - takes 2 extra cycles
1447                ARM7_ICOUNT -= 2;
1448            }
1449        }
1450        // LDM (NO PC) takes (n)S + 1N + 1I cycles (n = # of register transfers)
1451        ARM7_ICOUNT -= result + 1 + 1;
1452    } /* Loading */
1453    else
1454    {
1455        /* Storing */
1456        if (insn & (1 << eR15))
1457        {
1458#if ARM7_DEBUG_CORE
1459                LOG(("%08x: Writing R15 in strm\n", R15));
1460#endif
1461            /* special case handling if writing to PC */
1462            R15 += 12;
1463        }
1464        if (insn & INSN_BDT_U)
1465        {
1466            /* Incrementing */
1467            if (!(insn & INSN_BDT_P))
1468            {
1469                rbp = rbp + (- 4);
1470            }
1471
1472            // S Flag Set = User Bank Transfer
1473            if (insn & INSN_BDT_S)
1474            {
1475                // todo: needs to be tested..
1476
1477                // set to user mode - then do the transfer, and set back
1478                //int curmode = GET_MODE;
1479                //SwitchMode(cpustate, eARM7_MODE_USER);
1480                LOG(("%08x: User Bank Transfer not fully tested - please check if working properly!\n", R15));
1481                result = storeInc(cpustate, insn & 0xffff, rbp, eARM7_MODE_USER);
1482                // todo - not sure if Writeback occurs on User registers also..
1483                //SwitchMode(cpustate, curmode);
1484            }
1485            else
1486                result = storeInc(cpustate, insn & 0xffff, rbp, GET_MODE);
1487
1488            if ((insn & INSN_BDT_W) && (cpustate->pendingAbtD == 0))
1489            {
1490                SET_REGISTER(cpustate, rb, GET_REGISTER(cpustate, rb) + result * 4);
1491            }
1492        }
1493        else
1494        {
1495            /* Decrementing */
1496            if (!(insn & INSN_BDT_P))
1497            {
1498                rbp = rbp - (-4);
1499            }
1500
1501            // S Flag Set = User Bank Transfer
1502            if (insn & INSN_BDT_S)
1503            {
1504                // set to user mode - then do the transfer, and set back
1505                //int curmode = GET_MODE;
1506                //SwitchMode(cpustate, eARM7_MODE_USER);
1507                LOG(("%08x: User Bank Transfer not fully tested - please check if working properly!\n", R15));
1508                result = storeDec(cpustate, insn & 0xffff, rbp, eARM7_MODE_USER);
1509                // todo - not sure if Writeback occurs on User registers also..
1510                //SwitchMode(cpustate, curmode);
1511            }
1512            else
1513                result = storeDec(cpustate, insn & 0xffff, rbp, GET_MODE);
1514
1515            if ((insn & INSN_BDT_W) && (cpustate->pendingAbtD == 0))
1516            {
1517                SET_REGISTER(cpustate, rb, GET_REGISTER(cpustate, rb) - result * 4);
1518            }
1519        }
1520        if (insn & (1 << eR15))
1521            R15 -= 12;
1522
1523        // STM takes (n-1)S + 2N cycles (n = # of register transfers)
1524        ARM7_ICOUNT -= (result - 1) + 2;
1525    }
1526
1527    // We will specify the cycle count for each case, so remove the -3 that occurs at the end
1528    ARM7_ICOUNT += 3;
1529
1530} /* HandleMemBlock */
1531
1532
1533arm7ops_ophandler ops_handler[0x10] =
1534{
1535   arm7ops_0123, arm7ops_0123, arm7ops_0123, arm7ops_0123,arm7ops_4567,arm7ops_4567,arm7ops_4567,arm7ops_4567,arm7ops_89,arm7ops_89,arm7ops_ab,arm7ops_ab,arm7ops_cd,arm7ops_cd,arm7ops_e,arm7ops_f,
1536};
1537
1538const void arm7ops_0123(arm_state *cpustate, UINT32 insn)
1539{
1540//case 0:
1541//case 1:
1542//case 2:
1543//case 3:
1544   /* Branch and Exchange (BX) */
1545   if ((insn & 0x0ffffff0) == 0x012fff10)     // bits 27-4 == 000100101111111111110001
1546   {
1547      R15 = GET_REGISTER(cpustate, insn & 0x0f);
1548      // If new PC address has A0 set, switch to Thumb mode
1549      if (R15 & 1) {
1550         SET_CPSR(GET_CPSR|T_MASK);
1551         R15--;
1552      }
1553   }
1554   else if ((insn & 0x0ff000f0) == 0x01600010)   // CLZ - v5
1555   {
1556      UINT32 rm = insn&0xf;
1557      UINT32 rd = (insn>>12)&0xf;
1558
1559      SET_REGISTER(cpustate, rd, count_leading_zeros(GET_REGISTER(cpustate, rm)));
1560
1561      R15 += 4;
1562   }
1563   else if ((insn & 0x0ff000f0) == 0x01000050)   // QADD - v5
1564   {
1565      INT32 src1 = GET_REGISTER(cpustate, insn&0xf);
1566      INT32 src2 = GET_REGISTER(cpustate, (insn>>16)&0xf);
1567      INT64 res;
1568
1569      res = saturate_qbit_overflow(cpustate, (INT64)src1 + (INT64)src2);
1570
1571      SET_REGISTER(cpustate, (insn>>12)&0xf, (INT32)res);
1572      R15 += 4;
1573   }
1574   else if ((insn & 0x0ff000f0) == 0x01400050)   // QDADD - v5
1575   {
1576      INT32 src1 = GET_REGISTER(cpustate, insn&0xf);
1577      INT32 src2 = GET_REGISTER(cpustate, (insn>>16)&0xf);
1578      INT64 res;
1579
1580      // check if doubling operation will overflow
1581      res = (INT64)src2 * 2;
1582      saturate_qbit_overflow(cpustate, res);
1583
1584      src2 *= 2;
1585      res = saturate_qbit_overflow(cpustate, (INT64)src1 + (INT64)src2);
1586
1587      SET_REGISTER(cpustate, (insn>>12)&0xf, (INT32)res);
1588      R15 += 4;
1589   }
1590   else if ((insn & 0x0ff000f0) == 0x01200050)   // QSUB - v5
1591   {
1592      INT32 src1 = GET_REGISTER(cpustate, insn&0xf);
1593      INT32 src2 = GET_REGISTER(cpustate, (insn>>16)&0xf);
1594      INT64 res;
1595
1596      res = saturate_qbit_overflow(cpustate, (INT64)src1 - (INT64)src2);
1597
1598      SET_REGISTER(cpustate, (insn>>12)&0xf, (INT32)res);
1599      R15 += 4;
1600   }
1601   else if ((insn & 0x0ff000f0) == 0x01600050)   // QDSUB - v5
1602   {
1603      INT32 src1 = GET_REGISTER(cpustate, insn&0xf);
1604      INT32 src2 = GET_REGISTER(cpustate, (insn>>16)&0xf);
1605      INT64 res;
1606
1607      // check if doubling operation will overflow
1608      res = (INT64)src2 * 2;
1609      saturate_qbit_overflow(cpustate, res);
1610
1611      src2 *= 2;
1612      res = saturate_qbit_overflow(cpustate, (INT64)src1 - (INT64)src2);
1613
1614      SET_REGISTER(cpustate, (insn>>12)&0xf, (INT32)res);
1615      R15 += 4;
1616   }
1617   else if ((insn & 0x0ff00090) == 0x01000080)   // SMLAxy - v5
1618   {
1619      INT32 src1 = GET_REGISTER(cpustate, insn&0xf);
1620      INT32 src2 = GET_REGISTER(cpustate, (insn>>8)&0xf);
1621      INT32 res1;
1622
1623      // select top and bottom halves of src1/src2 and sign extend if necessary
1624      if (insn & 0x20)
1625      {
1626         src1 >>= 16;
1627      }
1628      else
1629      {
1630         src1 &= 0xffff;
1631         if (src1 & 0x8000)
1632         {
1633            src1 |= 0xffff;
1634         }
1635      }
1636
1637      if (insn & 0x40)
1638      {
1639         src2 >>= 16;
1640      }
1641      else
1642      {
1643         src2 &= 0xffff;
1644         if (src2 & 0x8000)
1645         {
1646            src2 |= 0xffff;
1647         }
1648      }
1649
1650      // do the signed multiply
1651      res1 = src1 * src2;
1652      // and the accumulate.  NOTE: only the accumulate can cause an overflow, which is why we do it this way.
1653      saturate_qbit_overflow(cpustate, (INT64)res1 + (INT64)GET_REGISTER(cpustate, (insn>>12)&0xf));
1654
1655      SET_REGISTER(cpustate, (insn>>16)&0xf, res1 + GET_REGISTER(cpustate, (insn>>12)&0xf));
1656      R15 += 4;
1657   }
1658   else if ((insn & 0x0ff00090) == 0x01400080)   // SMLALxy - v5
1659   {
1660      INT32 src1 = GET_REGISTER(cpustate, insn&0xf);
1661      INT32 src2 = GET_REGISTER(cpustate, (insn>>8)&0xf);
1662      INT64 dst;
1663
1664      // select top and bottom halves of src1/src2 and sign extend if necessary
1665      if (insn & 0x20)
1666      {
1667         src1 >>= 16;
1668      }
1669      else
1670      {
1671         src1 &= 0xffff;
1672         if (src1 & 0x8000)
1673         {
1674            src1 |= 0xffff;
1675         }
1676      }
1677
1678      if (insn & 0x40)
1679      {
1680         src2 >>= 16;
1681      }
1682      else
1683      {
1684         src2 &= 0xffff;
1685         if (src2 & 0x8000)
1686         {
1687            src2 |= 0xffff;
1688         }
1689      }
1690
1691      dst = (INT64)GET_REGISTER(cpustate, (insn>>12)&0xf);
1692      dst |= (INT64)GET_REGISTER(cpustate, (insn>>16)&0xf)<<32;
1693
1694      // do the multiply and accumulate
1695      dst += (INT64)src1 * (INT64)src2;
1696
1697      // write back the result
1698      SET_REGISTER(cpustart, (insn>>12)&0xf, (UINT32)(dst&0xffffffff));
1699      SET_REGISTER(cpustart, (insn>>16)&0xf, (UINT32)(dst>>32));
1700   }
1701   else if ((insn & 0x0ff00090) == 0x01600080)   // SMULxy - v5
1702   {
1703      INT32 src1 = GET_REGISTER(cpustate, insn&0xf);
1704      INT32 src2 = GET_REGISTER(cpustate, (insn>>8)&0xf);
1705      INT32 res;
1706
1707      // select top and bottom halves of src1/src2 and sign extend if necessary
1708      if (insn & 0x20)
1709      {
1710         src1 >>= 16;
1711      }
1712      else
1713      {
1714         src1 &= 0xffff;
1715         if (src1 & 0x8000)
1716         {
1717            src1 |= 0xffff;
1718         }
1719      }
1720
1721      if (insn & 0x40)
1722      {
1723         src2 >>= 16;
1724      }
1725      else
1726      {
1727         src2 &= 0xffff;
1728         if (src2 & 0x8000)
1729         {
1730            src2 |= 0xffff;
1731         }
1732      }
1733
1734      res = src1 * src2;
1735      SET_REGISTER(cpustart, (insn>>16)&0xf, res);
1736   }
1737   else if ((insn & 0x0ff000b0) == 0x012000a0)   // SMULWy - v5
1738   {
1739      INT32 src1 = GET_REGISTER(cpustate, insn&0xf);
1740      INT32 src2 = GET_REGISTER(cpustate, (insn>>8)&0xf);
1741      INT64 res;
1742
1743      if (insn & 0x40)
1744      {
1745         src2 >>= 16;
1746      }
1747      else
1748      {
1749         src2 &= 0xffff;
1750         if (src2 & 0x8000)
1751         {
1752            src2 |= 0xffff;
1753         }
1754      }
1755
1756      res = (INT64)src1 * (INT64)src2;
1757      res >>= 16;
1758      SET_REGISTER(cpustart, (insn>>16)&0xf, (UINT32)res);
1759   }
1760   else if ((insn & 0x0ff000b0) == 0x01200080)   // SMLAWy - v5
1761   {
1762      INT32 src1 = GET_REGISTER(cpustate, insn&0xf);
1763      INT32 src2 = GET_REGISTER(cpustate, (insn>>8)&0xf);
1764      INT32 src3 = GET_REGISTER(cpustate, (insn>>12)&0xf);
1765      INT64 res;
1766
1767      if (insn & 0x40)
1768      {
1769         src2 >>= 16;
1770      }
1771      else
1772      {
1773         src2 &= 0xffff;
1774         if (src2 & 0x8000)
1775         {
1776            src2 |= 0xffff;
1777         }
1778      }
1779
1780      res = (INT64)src1 * (INT64)src2;
1781      res >>= 16;
1782
1783      // check for overflow and set the Q bit
1784      saturate_qbit_overflow(cpustate, (INT64)src3 + res);
1785
1786      // do the real accumulate
1787      src3 += (INT32)res;
1788
1789      // write the result back
1790      SET_REGISTER(cpustart, (insn>>16)&0xf, (UINT32)res);
1791   }
1792   else
1793   /* Multiply OR Swap OR Half Word Data Transfer */
1794   if ((insn & 0x0e000000) == 0 && (insn & 0x80) && (insn & 0x10))  // bits 27-25=000 bit 7=1 bit 4=1
1795   {
1796      /* Half Word Data Transfer */
1797      if (insn & 0x60)         // bits = 6-5 != 00
1798      {
1799         HandleHalfWordDT(cpustate, insn);
1800      }
1801      else
1802      /* Swap */
1803      if (insn & 0x01000000)   // bit 24 = 1
1804      {
1805         HandleSwap(cpustate, insn);
1806      }
1807      /* Multiply Or Multiply Long */
1808      else
1809      {
1810         /* multiply long */
1811         if (insn & 0x800000) // Bit 23 = 1 for Multiply Long
1812         {
1813            /* Signed? */
1814            if (insn & 0x00400000)
1815               HandleSMulLong(cpustate, insn);
1816            else
1817               HandleUMulLong(cpustate, insn);
1818         }
1819         /* multiply */
1820         else
1821         {
1822            HandleMul(cpustate, insn);
1823         }
1824         R15 += 4;
1825      }
1826   }
1827   /* Data Processing OR PSR Transfer */
1828   else if ((insn & 0x0c000000) == 0)   // bits 27-26 == 00 - This check can only exist properly after Multiplication check above
1829   {
1830      /* PSR Transfer (MRS & MSR) */
1831      if (((insn & 0x00100000) == 0) && ((insn & 0x01800000) == 0x01000000)) // S bit must be clear, and bit 24,23 = 10
1832      {
1833         HandlePSRTransfer(cpustate, insn);
1834         ARM7_ICOUNT += 2;       // PSR only takes 1 - S Cycle, so we add + 2, since at end, we -3..
1835         R15 += 4;
1836      }
1837      /* Data Processing */
1838      else
1839      {
1840         HandleALU(cpustate, insn);
1841      }
1842   }
1843//   break;
1844}
1845
1846const void arm7ops_4567(arm_state *cpustate, UINT32 insn) /* Data Transfer - Single Data Access */
1847{
1848//case 4:
1849//case 5:
1850//case 6:
1851//case 7:
1852   HandleMemSingle(cpustate, insn);
1853   R15 += 4;
1854//   break;
1855}
1856   
1857const void arm7ops_89(arm_state *cpustate, UINT32 insn) /* Block Data Transfer/Access */
1858{
1859//case 8:
1860//case 9:
1861   HandleMemBlock(cpustate, insn);
1862   R15 += 4;
1863//   break;
1864}
1865
1866const void arm7ops_ab(arm_state *cpustate, UINT32 insn) /* Branch or Branch & Link */
1867{
1868//case 0xa:
1869//case 0xb:
1870   HandleBranch(cpustate, insn);
1871//   break;
1872}
1873
1874const void arm7ops_cd(arm_state *cpustate, UINT32 insn) /* Co-Processor Data Transfer */
1875{
1876//case 0xc:
1877//case 0xd:
1878   HandleCoProcDT(cpustate, insn);
1879   R15 += 4;
1880//   break;
1881}
1882
1883const void arm7ops_e(arm_state *cpustate, UINT32 insn) /* Co-Processor Data Operation or Register Transfer */
1884{
1885//case 0xe:
1886   if (insn & 0x10)
1887      HandleCoProcRT(cpustate, insn);
1888   else
1889      HandleCoProcDO(cpustate, insn);
1890   R15 += 4;
1891//   break;
1892}
1893
1894const void arm7ops_f(arm_state *cpustate, UINT32 insn) /* Software Interrupt */
1895{
1896   cpustate->pendingSwi = 1;
1897   ARM7_CHECKIRQ;
1898   //couldn't find any cycle counts for SWI
1899//   break;
1900}
src/emu/cpu/arm7/arm7exec.c
src/emu/cpu/arm7/arm7ops.h
r0r14021
1
2const void arm7ops_0123(arm_state *cpustate, UINT32 insn);
3const void arm7ops_4567(arm_state *cpustate, UINT32 insn);
4const void arm7ops_89(arm_state *cpustate, UINT32 insn);
5const void arm7ops_ab(arm_state *cpustate, UINT32 insn);
6const void arm7ops_cd(arm_state *cpustate, UINT32 insn);
7const void arm7ops_e(arm_state *cpustate, UINT32 insn);
8const void arm7ops_f(arm_state *cpustate, UINT32 insn);
No newline at end of file
src/emu/distate.c
r14020r14021
489489//  of indexed state from a string
490490//-------------------------------------------------
491491
492void device_state_interface::set_state(int index, const char *string)
492void device_state_interface::set_state_string(int index, const char *string)
493493{
494494   // NULL or out-of-range entry is a no-op
495495   const device_state_entry *entry = state_find_entry(index);
src/emu/distate.h
r14020r14021
161161
162162   // state setters
163163   void set_state(int index, UINT64 value);
164   void set_state(int index, const char *string);
164   void set_state_string(int index, const char *string);
165165
166166public:   // protected eventually
167167
src/emu/drivers/empty.c
r14020r14021
44
55    Empty driver.
66
7    Copyright Nicola Salmoria and the MAME Team.
8    Visit http://mamedev.org for licensing and usage restrictions.
7****************************************************************************
98
9    Copyright Aaron Giles
10    All rights reserved.
11
12    Redistribution and use in source and binary forms, with or without
13    modification, are permitted provided that the following conditions are
14    met:
15
16        * Redistributions of source code must retain the above copyright
17          notice, this list of conditions and the following disclaimer.
18        * Redistributions in binary form must reproduce the above copyright
19          notice, this list of conditions and the following disclaimer in
20          the documentation and/or other materials provided with the
21          distribution.
22        * Neither the name 'MAME' nor the names of its contributors may be
23          used to endorse or promote products derived from this software
24          without specific prior written permission.
25
26    THIS SOFTWARE IS PROVIDED BY AARON GILES ''AS IS'' AND ANY EXPRESS OR
27    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29    DISCLAIMED. IN NO EVENT SHALL AARON GILES BE LIABLE FOR ANY DIRECT,
30    INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34    STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
35    IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36    POSSIBILITY OF SUCH DAMAGE.
37
1038**************************************************************************/
1139
1240#include "emu.h"
r14020r14021
1543#include "uimain.h"
1644
1745
18/*************************************
19 *
20 *  Machine "start"
21 *
22 *************************************/
46//**************************************************************************
47//  DRIVER STATE
48//**************************************************************************
2349
24static MACHINE_START( ___empty )
50class empty_state : public driver_device
2551{
26   /* force the UI to show the game select screen */
27   ui_menu_force_game_select(machine, &machine.render().ui_container());
28}
52public:
53   // constructor
54   empty_state(const machine_config &mconfig, device_type type, const char *tag)
55      : driver_device(mconfig, type, tag)
56   {
57   }
2958
59   virtual void machine_start()
60   {
61      // force the UI to show the game select screen
62      ui_menu_force_game_select(machine(), &machine().render().ui_container());
63   }
64   
65   UINT32 screen_update(screen_device &screen, bitmap_rgb32 &bitmap, const rectangle &cliprect)
66   {
67      bitmap.fill(RGB_BLACK);
68      return 0;
69   }
70};
3071
31static SCREEN_UPDATE_RGB32( empty )
32{
33   bitmap.fill(RGB_BLACK);
34   return 0;
35}
3672
3773
74//**************************************************************************
75//  MACHINE DRIVERS
76//**************************************************************************
3877
39/*************************************
40 *
41 *  Machine drivers
42 *
43 *************************************/
78static MACHINE_CONFIG_START( ___empty, empty_state )
4479
45static MACHINE_CONFIG_START( ___empty, driver_device )
46
47   MCFG_MACHINE_START(___empty)
48
49   /* video hardware */
80   // video hardware
5081   MCFG_SCREEN_ADD("screen", RASTER)
51   MCFG_SCREEN_UPDATE_STATIC(empty)
82   MCFG_SCREEN_UPDATE_DRIVER(empty_state, screen_update)
5283   MCFG_SCREEN_SIZE(640,480)
5384   MCFG_SCREEN_VISIBLE_AREA(0,639, 0,479)
5485   MCFG_SCREEN_REFRESH_RATE(30)
r14020r14021
5687
5788
5889
59/*************************************
60 *
61 *  ROM definitions
62 *
63 *************************************/
90//**************************************************************************
91//  ROM DEFINITIONS
92//**************************************************************************
6493
6594ROM_START( ___empty )
6695   ROM_REGION( 0x10, "user1", ROMREGION_ERASEFF )
r14020r14021
6897
6998
7099
71/*************************************
72 *
73 *  Game drivers
74 *
75 *************************************/
100//**************************************************************************
101//  GAME DRIVERS
102//**************************************************************************
76103
77104GAME( 2007, ___empty, 0, ___empty, 0, 0, ROT0, "MAME", "No Driver Loaded", GAME_NO_SOUND )
src/emu/drivers/testcpu.c
r0r14021
1/*************************************************************************
2
3    testcpu.c
4
5    Example driver for performing CPU stress tests.
6
7****************************************************************************
8
9    Copyright Aaron Giles
10    All rights reserved.
11
12    Redistribution and use in source and binary forms, with or without
13    modification, are permitted provided that the following conditions are
14    met:
15
16        * Redistributions of source code must retain the above copyright
17          notice, this list of conditions and the following disclaimer.
18        * Redistributions in binary form must reproduce the above copyright
19          notice, this list of conditions and the following disclaimer in
20          the documentation and/or other materials provided with the
21          distribution.
22        * Neither the name 'MAME' nor the names of its contributors may be
23          used to endorse or promote products derived from this software
24          without specific prior written permission.
25
26    THIS SOFTWARE IS PROVIDED BY AARON GILES ''AS IS'' AND ANY EXPRESS OR
27    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29    DISCLAIMED. IN NO EVENT SHALL AARON GILES BE LIABLE FOR ANY DIRECT,
30    INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34    STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
35    IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36    POSSIBILITY OF SUCH DAMAGE.
37
38**************************************************************************/
39
40#define ADDRESS_MAP_MODERN
41
42#include "emu.h"
43#include "cpu/powerpc/ppc.h"
44
45
46//**************************************************************************
47//  CONSTANTS
48//**************************************************************************
49
50#define RAM_BASE 0x80000000
51
52
53
54//**************************************************************************
55//  DRIVER STATE
56//**************************************************************************
57
58class testcpu_state : public driver_device
59{
60public:
61   // constructor
62   testcpu_state(const machine_config &mconfig, device_type type, const char *tag)
63      : driver_device(mconfig, type, tag),
64        m_cpu(*this, "maincpu"),
65        m_ram(*this, "ram")
66   {
67   }
68
69   // timer callback; used to wrest control of the system
70   virtual void device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
71   {
72      static const UINT32 sample_instructions[] =
73      {
74         0x3d40f900,      // li r10,0xf9000000
75         0x394af000,      // addi r10,r10,-0x1000
76         0x38600146,      // li r3,0x00000146
77         0x38800004,      // li r4,0x00000004
78         0x7c64572c,      // sthbrx r3,r4,r10
79         0x38600000,      // li r3,0x00000000
80         0x986a0070      // stb r3,0x0070(r10)
81      };
82
83      // iterate over instructions     
84      for (int instnum = 0; instnum < ARRAY_LENGTH(sample_instructions); instnum++)
85      {
86         // write the instruction to execute, followed by a BLR which will terminate the
87         // basic block in the DRC
88         m_space->write_dword(RAM_BASE, sample_instructions[instnum]);
89         m_space->write_dword(RAM_BASE + 4, 0x4e800020);
90         
91         // initialize the register state
92         m_cpu->set_state(PPC_PC, RAM_BASE);
93         for (int regnum = 0; regnum < 32; regnum++)
94            m_cpu->set_state(PPC_R0 + regnum, regnum | (regnum << 8) | (regnum << 16) | (regnum << 24));
95         m_cpu->set_state(PPC_CR, 0);
96         m_cpu->set_state(PPC_LR, 0x12345678);
97         m_cpu->set_state(PPC_CTR, 0x1000);
98         m_cpu->set_state(PPC_XER, 0);
99         for (int regnum = 0; regnum < 32; regnum++)
100         {
101            double value = double(regnum | (regnum << 8) | (regnum << 16) | (regnum << 24));
102            m_cpu->set_state(PPC_F0 + regnum, d2u(value));
103         }
104
105         // output initial state
106         printf("==================================================\n");
107         printf("Initial state:\n");
108         dump_state(true);
109
110         // execute one instruction
111         *m_cpu->m_icountptr = 0;
112         m_cpu->run();
113         
114         // dump the final register state
115         printf("Final state:\n");
116         dump_state(false);
117      }
118     
119      // all done; just bail
120      throw emu_fatalerror(0, "All done");
121   }
122   
123   // startup code; do basic configuration and set a timer to go off immediately
124   virtual void machine_start()
125   {
126      // find the CPU's address space
127      m_space = m_cpu->space(AS_PROGRAM);
128     
129      // configure DRC in the most compatible mode
130      ppcdrc_set_options(m_cpu, PPCDRC_COMPATIBLE_OPTIONS);
131
132      // set a timer to go off right away
133      timer_set(attotime::zero);
134   }
135   
136   // dump the current CPU state
137   void dump_state(bool disassemble)
138   {
139      char buffer[256];
140      UINT8 instruction[32];
141      buffer[0] = 0;
142      int bytes = 0;
143      if (disassemble)
144      {
145         // fill in an array of bytes in the CPU's natural order
146         int maxbytes = m_cpu->max_opcode_bytes();
147         for (int bytenum = 0; bytenum < maxbytes; bytenum++)
148            instruction[bytenum] = m_space->read_byte(RAM_BASE + bytenum);
149
150         // disassemble the current instruction         
151         bytes = m_cpu->disassemble(buffer, RAM_BASE, instruction, instruction) & DASMFLAG_LENGTHMASK;
152      }
153     
154      // output the registers
155      printf("PC : %08X", UINT32(m_cpu->state(PPC_PC)));
156      if (disassemble && bytes > 0)
157      {
158         printf(" => ");
159         for (int bytenum = 0; bytenum < bytes; bytenum++)
160            printf("%02X", instruction[bytenum]);
161         printf("  %s", buffer);
162      }
163      printf("\n");
164      for (int regnum = 0; regnum < 32; regnum++)
165      {
166         printf("R%-2d: %08X   ", regnum, UINT32(m_cpu->state(PPC_R0 + regnum)));
167         if (regnum % 4 == 3) printf("\n");
168      }
169      printf("CR : %08X   LR : %08X   CTR: %08X   XER: %08X\n",
170            UINT32(m_cpu->state(PPC_CR)), UINT32(m_cpu->state(PPC_LR)),
171            UINT32(m_cpu->state(PPC_CTR)), UINT32(m_cpu->state(PPC_XER)));
172      for (int regnum = 0; regnum < 32; regnum++)
173      {
174         printf("F%-2d: %10g   ", regnum, u2d(m_cpu->state(PPC_F0 + regnum)));
175         if (regnum % 4 == 3) printf("\n");
176      }
177   }
178   
179   // report reads from anywhere
180   READ64_MEMBER( general_r )
181   {
182      UINT64 fulloffs = offset;
183      UINT64 result = fulloffs + (fulloffs << 8) + (fulloffs << 16) + (fulloffs << 24) + (fulloffs << 32);
184      printf("Read from %08X & %016" I64FMT "X = %016" I64FMT "X\n", offset * 8, mem_mask, result);
185      return result;
186   }
187
188   // report writes to anywhere
189   WRITE64_MEMBER( general_w )
190   {
191      printf("Write to %08X & %016" I64FMT "X = %016" I64FMT "X\n", offset * 8, mem_mask, data);
192   }
193
194private:
195   // internal state
196   required_device<ppc603e_device> m_cpu;
197   required_shared_ptr<UINT64> m_ram;
198   address_space *m_space;
199};
200
201
202
203//**************************************************************************
204//  ADDRESS MAPS
205//**************************************************************************
206
207static ADDRESS_MAP_START( ppc_mem, AS_PROGRAM, 64, testcpu_state )
208   AM_RANGE(RAM_BASE, RAM_BASE+7) AM_RAM AM_SHARE("ram")
209   AM_RANGE(0x00000000, 0xffffffff) AM_READWRITE(general_r, general_w)
210ADDRESS_MAP_END
211
212
213
214//**************************************************************************
215//  MACHINE DRIVERS
216//**************************************************************************
217
218static const powerpc_config ppc_config =
219{
220   // 603e, Stretch, 1.3
221   66000000      // Multiplier 1, Bus = 66MHz, Core = 66MHz
222};
223
224static MACHINE_CONFIG_START( testcpu, testcpu_state )
225
226   // CPUs
227   MCFG_CPU_ADD("maincpu", PPC603E, 66000000)
228   MCFG_CPU_CONFIG(ppc_config)
229   MCFG_CPU_PROGRAM_MAP(ppc_mem)
230MACHINE_CONFIG_END
231
232
233
234//**************************************************************************
235//  ROM DEFINITIONS
236//**************************************************************************
237
238ROM_START( testcpu )
239   ROM_REGION( 0x10, "user1", ROMREGION_ERASEFF )
240ROM_END
241
242
243
244//**************************************************************************
245//  GAME DRIVERS
246//**************************************************************************
247
248GAME( 2012, testcpu, 0, testcpu, 0, 0, ROT0, "MAME", "CPU Tester", GAME_NO_SOUND )
src/emu/emu.mak
r14020r14021
138138
139139EMUDRIVEROBJS = \
140140   $(EMUDRIVERS)/empty.o \
141   $(EMUDRIVERS)/testcpu.o \
141142
142143EMUMACHINEOBJS = \
143144   $(EMUMACHINE)/53c810.o      \
src/emu/machine/nvram.c
r14020r14021
9595void nvram_device::device_start()
9696{
9797   // bind our handler
98   if (!m_custom_handler.isnull())
99      m_custom_handler = nvram_init_delegate(m_custom_handler, *m_owner);
98   m_custom_handler.bind_relative_to(*owner());
10099}
101100
102101
src/emu/machine/nvram.h
r14020r14021
6464   MCFG_DEVICE_ADD(_tag, NVRAM, 0) \
6565   nvram_device::static_set_default_value(*device, nvram_device::DEFAULT_NONE); \
6666
67#define MCFG_NVRAM_ADD_CUSTOM(_tag, _class, _method) \
67#define MCFG_NVRAM_ADD_CUSTOM_DRIVER(_tag, _class, _method) \
6868   MCFG_DEVICE_ADD(_tag, NVRAM, 0) \
69   nvram_device::static_set_custom_handler(*device, nvram_init_delegate(&_class::_method, #_class "::" #_method, (_class *)0)); \
69   nvram_device::static_set_custom_handler(*device, nvram_init_delegate(&_class::_method, #_class "::" #_method, NULL, (_class *)0)); \
7070
7171
7272#define MCFG_NVRAM_REPLACE_0FILL(_tag) \
r14020r14021
8181   MCFG_DEVICE_REPLACE(_tag, NVRAM, 0) \
8282   nvram_device::static_set_default_value(*device, nvram_device::DEFAULT_RANDOM); \
8383
84#define MCFG_NVRAM_REPLACE_CUSTOM(_tag, _class, _method) \
84#define MCFG_NVRAM_REPLACE_CUSTOM_DRIVER(_tag, _class, _method) \
8585   MCFG_DEVICE_REPLACE(_tag, NVRAM, 0) \
86   nvram_device::static_set_custom_handler(*device, nvram_init_delegate(&_class::_method, #_class "::" #_method, (_class *)0)); \
86   nvram_device::static_set_custom_handler(*device, nvram_init_delegate(&_class::_method, #_class "::" #_method, NULL, (_class *)0)); \
8787
8888
8989
r14020r14021
9595
9696
9797// custom initialization for default state
98typedef delegate<void (nvram_device &, void *, size_t)> nvram_init_delegate;
98typedef device_delegate<void (nvram_device &, void *, size_t)> nvram_init_delegate;
9999
100100
101101// ======================> nvram_device
src/emu/diexec.h
r14020r14021
144144class device_execute_interface : public device_interface
145145{
146146   friend class device_scheduler;
147   friend class testcpu_state;
147148
148149public:
149150   // construction/destruction

Previous 15994 Revisions Next


© 1998-2018 The MESS Team