Previous 15994 Revisions Next

r5377 Sunday 9th August, 2009 at 02:00:14 UTC by R. Belmont
GBA: adding a cleaned up version of the previous WIP driver
[docs]messnew.txt
[src/mess]mess.mak messdriv.c
[src/mess/drivers]gba.c*
[src/mess/includes]gba.h*
[src/mess/video]gba.c*

src/mess/video/gba.c
r0r5377
1/***************************************************************************
2
3  gba.c
4
5  File to handle emulation of the video hardware of the Game Boy Advance
6
7  By R. Belmont & MooglyGuy
8
9***************************************************************************/
10
11#include "driver.h"
12#include "video/generic.h"
13#include "includes/gba.h"
14
15#define VERBOSE_LEVEL   (0)
16#define DISABLE_ROZ   (1)
17
18static UINT16 mosaic_offset[16][4096];
19
20INLINE void verboselog(running_machine *machine, int n_level, const char *s_fmt, ...)
21{
22   if( VERBOSE_LEVEL >= n_level )
23   {
24      va_list v;
25      char buf[ 32768 ];
26      va_start( v, s_fmt );
27      vsprintf( buf, s_fmt, v );
28      va_end( v );
29      logerror( "%08x: %s", cpu_get_pc(machine->cpu[0]), buf );
30   }
31}
32
33static void draw_4bpp_tile(gba_state *gba_state, UINT16 *scanline, UINT32 gba_vrambase, UINT16 tilenum, int scnx, int tiley, int vflip, int hflip, int pal, int mosX)
34{
35   int pixx, yofs;
36   UINT8 *pgba_vram = (UINT8 *)&gba_state->gba_vram[gba_vrambase>>2], pixel;
37   UINT16 *pgba_pram = (UINT16 *)gba_state->gba_pram;
38
39   if (vflip)
40   {
41      yofs = 28-(tiley*4);
42   }
43   else
44   {
45      yofs = tiley*4;
46   }
47
48   if (hflip)
49   {
50      for (pixx = 0; pixx < 8; pixx+= 2)
51      {
52         pixel = pgba_vram[(tilenum*32)+(3-(pixx/2))+yofs];
53
54         if (pixel & 0xf0)
55         {
56            scanline[scnx+pixx] = pgba_pram[(pixel>>4)+pal]&0x7fff;
57         }
58         if (pixel & 0x0f)
59         {
60            scanline[scnx+pixx+1] = pgba_pram[(pixel&0xf)+pal]&0x7fff;
61         }
62      }
63   }
64   else
65   {
66      for (pixx = 0; pixx < 8; pixx+= 2)
67      {
68         pixel = pgba_vram[(tilenum*32)+(pixx/2)+yofs];
69
70         if (pixel & 0x0f)
71         {
72            scanline[scnx+pixx] = pgba_pram[(pixel&0xf)+pal]&0x7fff;
73         }
74         if (pixel & 0xf0)
75         {
76            scanline[scnx+pixx+1] = pgba_pram[(pixel>>4)+pal]&0x7fff;
77         }
78      }
79   }
80}
81
82static void draw_8bpp_tile(gba_state *gba_state, UINT16 *scanline, UINT32 gba_vrambase, UINT16 tilenum, int scnx, int tiley, int vflip, int hflip, int pal, int mosX)
83{
84   int pixx, yofs;
85   UINT8 *pgba_vram = (UINT8 *)&gba_state->gba_vram[gba_vrambase>>2], pixel;
86   UINT16 *pgba_pram = (UINT16 *)gba_state->gba_pram;
87
88   if (vflip)
89   {
90      yofs = 56-(tiley*8);
91   }
92   else
93   {
94      yofs = tiley*8;
95   }
96
97   if (hflip)
98   {
99      for (pixx = 0; pixx < 8; pixx++)
100      {
101         pixel = pgba_vram[(tilenum*32)+(7-pixx)+yofs];
102
103         if (pixel)
104         {
105            scanline[scnx+pixx] = pgba_pram[pixel+pal]&0x7fff;
106         }
107      }
108   }
109   else
110   {
111      for (pixx = 0; pixx < 8; pixx++)
112      {
113         pixel = pgba_vram[(tilenum*32)+pixx+yofs];
114
115         if (pixel)
116         {
117            scanline[scnx+pixx] = pgba_pram[pixel+pal]&0x7fff;
118         }
119      }
120   }
121}
122
123static void draw_roz_scanline(gba_state *gba_state, UINT16 *scanline, int ypos, UINT32 enablemask, UINT32 ctrl, INT32 X, INT32 Y, INT32 PA, INT32 PB, INT32 PC, INT32 PD, int priority)
124{
125#if !DISABLE_ROZ
126   UINT32 base, mapbase, size, ovr;
127   INT32 sizes[4] = { 128, 256, 512, 1024 };
128   INT32 cx, cy, psx, psy, x, px, py;
129   UINT8 *mgba_vram = (UINT8 *)gba_state->gba_vram, tile;
130   UINT16 *pgba_pram = (UINT16 *)gba_state->gba_pram;
131   UINT16 pixel;
132
133   if ((gba_state->DISPCNT & enablemask) && ((ctrl & 3) == priority))
134   {
135      base = ((ctrl>>2)&3) * 0x4000;      // VRAM base of tiles
136      mapbase = ((ctrl>>8)&0x1f) * 0x800;   // VRAM base of map
137      size = ((ctrl>>14) & 3);      // size of map in submaps
138      ovr = ((ctrl >>13) & 1);
139
140      // sign extend roz parameters
141      if (X & 0x08000000) X |= 0xf0000000;
142      if (Y & 0x08000000) Y |= 0xf0000000;
143      if (PA & 0x8000) PA |= 0xffff0000;
144      if (PB & 0x8000) PB |= 0xffff0000;
145      if (PC & 0x8000) PC |= 0xffff0000;
146      if (PD & 0x8000) PD |= 0xffff0000;
147
148      cx = cy = sizes[size] / 2;
149
150//      psx = ((PA * (X - cx)) & ~63) + ((PB * (Y - cy)) & ~63) + ((PB * ypos) & ~63) + (cx << 8); //pixel start x
151//      psy = ((PC * (X - cx)) & ~63) + ((PD * (Y - cy)) & ~63) + ((PD * ypos) & ~63) + (cy << 8); //pixel start y     
152      psx = ((PA * (X - cx))) + ((PB * (Y - cy))) + ((PB * ypos)) + (cx << 8); //pixel start x
153      psy = ((PC * (X - cx))) + ((PD * (Y - cy))) + ((PD * ypos)) + (cy << 8); //pixel start y     
154
155
156      /*
157
158 x2 = A*(x1-x0) + B*(y1-y0) + x0
159 y2 = C*(x1-x0) + D*(y1-y0) + y0
160
161 x2-x0 = A*(x1-x0) + B*(y1-y0)
162 y2-y0 = C*(x1-x0) + D*(y1-y0)
163      */
164
165      for (x = 0; x < 240; x++)
166      {
167         px = psx + (PA * x); //pixel x position
168         py = psy + (PC * x); //pixel y position
169
170         // mask 'floating-point' bits (low 8 bits)
171         px >>= 8;
172         py >>= 8;
173
174         // do infinite repeat for now
175         px %= sizes[size];
176         py %= sizes[size];
177
178         // px and py are now offsets into the tilemap
179         tile = mgba_vram[mapbase + (px/8) + ((py/8)*sizes[size]/8)];
180
181         // get the pixel
182              pixel = mgba_vram[(tile*64) + (px%8) + ((py%8)*8)];
183
184         // plot it
185         if (pixel)
186         {
187            scanline[x] = pgba_pram[pixel]&0x7fff;
188         }
189      }     
190   }
191#endif
192}
193
194static void draw_bg_scanline(gba_state *gba_state, UINT16 *scanline, int ypos, UINT32 enablemask, UINT32 ctrl, UINT32 hofs, UINT32 vofs, int priority)
195{
196   UINT32 base, mapbase, size, mode;
197   UINT16 *mgba_vram = (UINT16 *)gba_state->gba_vram, tile;
198   int mx, my, tx, mosX, mosY;
199
200   if (ctrl & 0x40)   // mosaic
201   {
202      mosX = gba_state->MOSAIC & 0xf;
203      mosY = (gba_state->MOSAIC>>4) & 0xf;
204
205      ypos = mosaic_offset[mosY][ypos];
206   }
207   else
208   {
209      mosX = mosY = 0;
210   }
211
212   if ((gba_state->DISPCNT & enablemask) && ((ctrl & 3) == priority))
213   {
214      base = ((ctrl>>2)&3) * 0x4000;      // VRAM base of tiles
215      mapbase = ((ctrl>>8)&0x1f) * 0x800;   // VRAM base of map
216      size = ((ctrl>>14) & 3);      // size of map in submaps
217      mode = (gba_state->BG0CNT & 0x80) ? 1 : 0;   // 1 for 8bpp, 0 for 4bpp
218
219      mx = hofs/8;      // X tile offset
220      my = (ypos + vofs) / 8;   // Y tile offset
221
222      switch (size)
223      {
224         case 0:   // 32x32
225            for (tx = 0; tx < 31; tx++)
226            {
227               tile = mgba_vram[(mapbase>>1) + ((tx + mx) % 32) + ((my % 32) * 32)];
228
229               if (mode)
230               {
231                  draw_8bpp_tile(gba_state, scanline, base, (tile&0x3ff)<<1, (tx*8)-(hofs % 8), (ypos + vofs) % 8, (tile&0x800)?1:0, (tile&0x400)?1:0, 0, mosX);
232               }
233               else
234               {
235                  draw_4bpp_tile(gba_state, scanline, base, tile&0x3ff, (tx*8)-(hofs % 8), (ypos + vofs) % 8, (tile&0x800)?1:0, (tile&0x400)?1:0, ((tile&0xf000)>>8), mosX);
236               }
237            }
238            break;
239
240         case 1:   // 64x32
241            for (tx = 0; tx < 31; tx++)
242            {
243               int mapxofs = ((tx + mx) % 64);
244
245               if (mapxofs >= 32)
246               {
247                  mapxofs %= 32;
248                  mapxofs += (32*32);   // offset to reach the second page of the tilemap
249               }
250
251               tile = mgba_vram[(mapbase>>1) + mapxofs + ((my % 32) * 32)];
252
253               if (mode)
254               {
255                  draw_8bpp_tile(gba_state, scanline, base, (tile&0x3ff)<<1, (tx*8)-(hofs % 8), (ypos + vofs) % 8, (tile&0x800)?1:0, (tile&0x400)?1:0, 0, mosX);
256               }
257               else
258               {
259                  draw_4bpp_tile(gba_state, scanline, base, tile&0x3ff, (tx*8)-(hofs % 8), (ypos + vofs) % 8, (tile&0x800)?1:0, (tile&0x400)?1:0, ((tile&0xf000)>>8), mosX);
260               }
261            }
262            break;
263
264         case 2:   // 32x64
265            for (tx = 0; tx < 31; tx++)
266            {
267               int mapyofs = (my % 64);
268
269               if (mapyofs >= 32)
270               {
271                  mapyofs %= 32;
272                  mapyofs += 32;
273               }
274
275               tile = mgba_vram[(mapbase>>1) + ((tx + mx) % 32) + (mapyofs * 32)];
276
277               if (mode)
278               {
279                  draw_8bpp_tile(gba_state, scanline, base, (tile&0x3ff)<<1, (tx*8)-(hofs % 8), (ypos + vofs) % 8, (tile&0x800)?1:0, (tile&0x400)?1:0, 0, mosX);
280               }
281               else
282               {
283                  draw_4bpp_tile(gba_state, scanline, base, tile&0x3ff, (tx*8)-(hofs % 8), (ypos + vofs) % 8, (tile&0x800)?1:0, (tile&0x400)?1:0, ((tile&0xf000)>>8), mosX);
284               }
285            }
286            break;
287
288         case 3: // 64x64
289         #if 0
290            if (1)
291            {
292               int mapyofs = (my % 64);
293
294               if (mapyofs >= 32)
295               {
296                  mapyofs %= 32;
297                  mapyofs += 64;
298               }
299
300//               printf("%d yofs %d (ypos %d vofs %d my %d) tileofs %x\n", video_screen_get_vpos(machine->primary_screen), mapyofs, ypos, vofs, my, (mapbase>>1) + (mapyofs * 32));
301            }
302         #endif
303
304            for (tx = 0; tx < 31; tx++)
305            {
306               int mapxofs = ((tx + mx) % 64);
307               int mapyofs = (my % 64);
308
309               if (mapyofs >= 32)
310               {
311                  mapyofs %= 32;
312                  mapyofs += 64;
313               }
314
315               if (mapxofs >= 32)
316               {
317                  mapxofs %= 32;
318                  mapxofs += (32*32);   // offset to reach the second/fourth page of the tilemap
319               }
320
321               tile = mgba_vram[(mapbase>>1) + mapxofs + (mapyofs * 32)];
322
323               if (mode)
324               {
325                  draw_8bpp_tile(gba_state, scanline, base, (tile&0x3ff)<<1, (tx*8)-(hofs % 8), (ypos + vofs) % 8, (tile&0x800)?1:0, (tile&0x400)?1:0, 0, mosX);
326               }
327               else
328               {
329                  draw_4bpp_tile(gba_state, scanline, base, tile&0x3ff, (tx*8)-(hofs % 8), (ypos + vofs) % 8, (tile&0x800)?1:0, (tile&0x400)?1:0, ((tile&0xf000)>>8), mosX);
330               }
331            }
332            break;
333      }
334
335   }
336}
337
338static void draw_mode3_scanline(gba_state *gba_state, UINT16 *scanline, int y)
339{
340   int x;
341   UINT16 *pgba_vram = (UINT16 *)gba_state->gba_vram;
342
343   for (x = 0; x < 240; x++)
344   {
345      scanline[x] = pgba_vram[(240*y)+x]&0x7fff;
346   }
347}
348
349static void draw_mode4_scanline(gba_state *gba_state, UINT16 *scanline, int y)
350{
351   UINT32 base;
352   int x;
353   UINT8 *pgba_vram = (UINT8 *)gba_state->gba_vram, pixel;
354   UINT16 *pgba_pram = (UINT16 *)gba_state->gba_pram;
355
356   if (gba_state->DISPCNT & DISPCNT_FRAMESEL)
357   {
358      base = 0xa000;
359   }
360   else
361   {
362      base = 0;
363   }
364
365   for (x = 0; x < 240; x++)
366   {
367      pixel = pgba_vram[base+(240*y)+x];
368      if (pixel != 0)
369      {
370         scanline[x] = pgba_pram[pixel]&0x7fff;
371      }
372   }
373}
374
375static void draw_mode5_scanline(gba_state *gba_state, UINT16 *scanline, int y)
376{
377   UINT32 base;
378   int x;
379   UINT16 *pgba_vram = (UINT16 *)gba_state->gba_vram;
380
381   if (gba_state->DISPCNT & DISPCNT_FRAMESEL)
382   {
383      base = 0xa000>>1;
384   }
385   else
386   {
387      base = 0;
388   }
389
390   for (x = 0; x < 160; x++)
391   {
392      scanline[x] = pgba_vram[base+(160*y)+x];
393   }
394}
395
396static void draw_gba_oam(gba_state *gba_state, running_machine *machine, UINT16 *scanline, int y, int priority)
397{
398   INT16 gba_oamindex;
399   UINT8 tilex, tiley;
400   UINT32 tilebytebase, tileindex, tiledrawindex;
401   UINT8 width, height;
402   UINT16 *pgba_oam = (UINT16 *)gba_state->gba_oam;
403
404   if( gba_state->DISPCNT & DISPCNT_OBJ_EN )
405   {
406      for( gba_oamindex = 127; gba_oamindex >= 0; gba_oamindex-- )
407      {
408         UINT16 attr0, attr1, attr2;
409
410         attr0 = pgba_oam[(4*gba_oamindex)+0];
411         attr1 = pgba_oam[(4*gba_oamindex)+1];
412         attr2 = pgba_oam[(4*gba_oamindex)+2];
413
414         if (((attr0 & OBJ_MODE) != OBJ_MODE_WINDOW) && (((attr2 >> 10) & 3) == priority))
415         {
416            width = 0;
417            height = 0;
418            switch (attr0 & OBJ_SHAPE )
419            {
420               case OBJ_SHAPE_SQR:
421                  switch(attr1 & OBJ_SIZE )
422                  {
423                     case OBJ_SIZE_8:
424                        width = 1;
425                        height = 1;
426                        break;
427                     case OBJ_SIZE_16:
428                        width = 2;
429                        height = 2;
430                        break;
431                     case OBJ_SIZE_32:
432                        width = 4;
433                        height = 4;
434                        break;
435                     case OBJ_SIZE_64:
436                        width = 8;
437                        height = 8;
438                        break;
439                  }
440                  break;
441               case OBJ_SHAPE_HORIZ:
442                  switch(attr1 & OBJ_SIZE )
443                  {
444                     case OBJ_SIZE_8:
445                        width = 2;
446                        height = 1;
447                        break;
448                     case OBJ_SIZE_16:
449                        width = 4;
450                        height = 1;
451                        break;
452                     case OBJ_SIZE_32:
453                        width = 4;
454                        height = 2;
455                        break;
456                     case OBJ_SIZE_64:
457                        width = 8;
458                        height = 4;
459                        break;
460                  }
461                  break;
462               case OBJ_SHAPE_VERT:
463                  switch(attr1 & OBJ_SIZE )
464                  {
465                     case OBJ_SIZE_8:
466                        width = 1;
467                        height = 2;
468                        break;
469                     case OBJ_SIZE_16:
470                        width = 1;
471                        height = 4;
472                        break;
473                     case OBJ_SIZE_32:
474                        width = 2;
475                        height = 4;
476                        break;
477                     case OBJ_SIZE_64:
478                        width = 4;
479                        height = 8;
480                        break;
481                  }
482                  break;
483               default:
484                  width = 0;
485                  height = 0;
486                  verboselog(machine, 0, "OAM error: Trying to draw OBJ with OBJ_SHAPE = 3!\n" );
487                  break;
488            }
489
490            tiledrawindex = tileindex = (attr2 & OBJ_TILENUM);
491            tilebytebase = 0x10000;   // the index doesn't change in the higher modes, we just ignore sprites that are out of range
492
493             if (attr0 & OBJ_USEROZ)
494            {
495                // don't draw ROZ sprites for now
496//               printf("OAM: ROZ\n");
497            }
498            else
499            {
500               INT16 sx, sy;
501               int vflip, hflip;
502
503               if (attr0 & OBJ_DISABLE)
504               {
505                  continue;
506               }
507
508               sx = (attr1 & OBJ_X_COORD);
509               sy = (attr0 & OBJ_Y_COORD);
510               vflip = (attr1 & OBJ_VFLIP) ? 1 : 0;
511               hflip = (attr1 & OBJ_HFLIP) ? 1 : 0;
512
513               if (sy > 160)
514               {
515                  sy = 0 - (255-sy);
516               }
517
518               if (sx > 256)
519               {
520                  sx = 0 - (511-sx);
521               }
522
523               // on this scanline?
524               if ((y >= sy) && (y < (sy+(height*8))))
525               {
526                  // Y = scanline we're drawing
527                  // SY = starting Y position of sprite
528                  if (vflip)
529                  {
530                     tiley = (sy+(height*8)-1) - y;
531                  }
532                  else
533                  {
534                     tiley = y - sy;
535                  }
536
537                  if (( gba_state->DISPCNT & DISPCNT_VRAM_MAP ) == DISPCNT_VRAM_MAP_2D)
538                  {
539                     tiledrawindex = (tileindex + ((tiley/8) * 32));
540                  }
541                  else
542                  {
543                     if ((attr0 & OBJ_PALMODE) == OBJ_PALMODE_16)
544                     {
545                        tiledrawindex = (tileindex + ((tiley/8) * width));
546                     }
547                     else
548                     {
549                        tiledrawindex = (tileindex + ((tiley/8) * (width*2)));
550                     }
551                  }
552
553                  if (hflip)
554                  {
555                     tiledrawindex += (width-1);
556                     for (tilex = 0; tilex < width; tilex++)
557                     {
558                        if ((attr0 & OBJ_PALMODE) == OBJ_PALMODE_16)
559                        {
560                           draw_4bpp_tile(gba_state, scanline, tilebytebase, tiledrawindex, sx+(tilex*8), tiley % 8, 0, hflip, 256+((attr2&0xf000)>>8), 0);
561                        }
562                        else
563                        {
564                           draw_8bpp_tile(gba_state, scanline, tilebytebase, tiledrawindex, sx+(tilex*8), tiley % 8, 0, hflip, 256, 0);
565                           tiledrawindex++;
566                        }
567                        tiledrawindex--;
568                     }
569                  }
570                  else
571                  {
572                     for (tilex = 0; tilex < width; tilex++)
573                     {
574                        if ((attr0 & OBJ_PALMODE) == OBJ_PALMODE_16)
575                        {
576                           draw_4bpp_tile(gba_state, scanline, tilebytebase, tiledrawindex, sx+(tilex*8), tiley % 8, 0, hflip, 256+((attr2&0xf000)>>8), 0);
577                        }
578                        else
579                        {
580                           draw_8bpp_tile(gba_state, scanline, tilebytebase, tiledrawindex, sx+(tilex*8), tiley % 8, 0, hflip, 256, 0);
581                           tiledrawindex++;
582                        }
583                        tiledrawindex++;
584                     }
585                  }
586               }
587            }
588         }
589      }
590   }
591}
592
593void gba_draw_scanline(running_machine *machine, int y)
594{
595   UINT16 *pgba_pram, *scanline;
596   int prio;
597   bitmap_t *bitmap = tmpbitmap;
598   int i;
599   static UINT16 xferscan[240+2048];   // up to 1024 pixels of slop on either side to allow easier clip handling
600   gba_state *gba_state = machine->driver_data;
601
602   pgba_pram = (UINT16 *)gba_state->gba_pram;
603
604   scanline = BITMAP_ADDR16(bitmap, y, 0);
605
606   // forced blank
607   if (gba_state->DISPCNT & DISPCNT_BLANK)
608   {
609      // forced blank is white
610      for (i = 0; i < 240; i++)
611      {
612         scanline[i] = 0x7fff;
613      }
614      return;
615   }
616
617   // BG color
618   for (i = 0; i < 240; i++)
619   {
620      xferscan[1024+i] = pgba_pram[0]&0x7fff;
621   }
622
623
624//   printf("mode %d\n", (gba_state->DISPCNT & 7));
625
626//   if (y == 0) printf("DISPCNT %04x\n", gba_state->DISPCNT);
627
628   switch (gba_state->DISPCNT & 7)
629   {
630      case 0:
631         for (prio = 3; prio >= 0; prio--)
632         {
633            draw_bg_scanline(gba_state, &xferscan[1024], y, DISPCNT_BG3_EN, gba_state->BG3CNT, gba_state->BG3HOFS, gba_state->BG3VOFS, prio);
634            draw_bg_scanline(gba_state, &xferscan[1024], y, DISPCNT_BG2_EN, gba_state->BG2CNT, gba_state->BG2HOFS, gba_state->BG2VOFS, prio);
635            draw_bg_scanline(gba_state, &xferscan[1024], y, DISPCNT_BG1_EN, gba_state->BG1CNT, gba_state->BG1HOFS, gba_state->BG1VOFS, prio);
636            draw_bg_scanline(gba_state, &xferscan[1024], y, DISPCNT_BG0_EN, gba_state->BG0CNT, gba_state->BG0HOFS, gba_state->BG0VOFS, prio);
637            draw_gba_oam(gba_state, machine, &xferscan[1024], y, prio);
638         }
639         break;
640
641      case 1:
642         for (prio = 3; prio >= 0; prio--)
643         {
644            draw_roz_scanline(gba_state, &xferscan[1024], y, DISPCNT_BG2_EN, gba_state->BG2CNT, gba_state->BG2X, gba_state->BG2Y, gba_state->BG2PA, gba_state->BG2PB, gba_state->BG2PC, gba_state->BG2PD, prio);
645            draw_bg_scanline(gba_state, &xferscan[1024], y, DISPCNT_BG1_EN, gba_state->BG1CNT, gba_state->BG1HOFS, gba_state->BG1VOFS, prio);
646            draw_bg_scanline(gba_state, &xferscan[1024], y, DISPCNT_BG0_EN, gba_state->BG0CNT, gba_state->BG0HOFS, gba_state->BG0VOFS, prio);
647            draw_gba_oam(gba_state, machine, &xferscan[1024], y, prio);
648         }
649         break;
650
651      case 2:
652         for (prio = 3; prio >= 0; prio--)
653         {
654            draw_roz_scanline(gba_state, &xferscan[1024], y, DISPCNT_BG3_EN, gba_state->BG3CNT, gba_state->BG3X, gba_state->BG3Y, gba_state->BG3PA, gba_state->BG3PB, gba_state->BG3PC, gba_state->BG3PD, prio);
655            draw_roz_scanline(gba_state, &xferscan[1024], y, DISPCNT_BG2_EN, gba_state->BG2CNT, gba_state->BG2X, gba_state->BG2Y, gba_state->BG2PA, gba_state->BG2PB, gba_state->BG2PC, gba_state->BG2PD, prio);
656            draw_gba_oam(gba_state, machine, &xferscan[1024], y, prio);
657         }
658         break;
659
660      case 3:
661         draw_mode3_scanline(gba_state, &xferscan[1024], y);
662         draw_gba_oam(gba_state, machine, &xferscan[1024], y, 3);
663         draw_gba_oam(gba_state, machine, &xferscan[1024], y, 2);
664         draw_gba_oam(gba_state, machine, &xferscan[1024], y, 1);
665         draw_gba_oam(gba_state, machine, &xferscan[1024], y, 0);
666         break;
667
668      case 4:
669         draw_mode4_scanline(gba_state, &xferscan[1024], y);
670         draw_gba_oam(gba_state, machine, &xferscan[1024], y, 3);
671         draw_gba_oam(gba_state, machine, &xferscan[1024], y, 2);
672         draw_gba_oam(gba_state, machine, &xferscan[1024], y, 1);
673         draw_gba_oam(gba_state, machine, &xferscan[1024], y, 0);
674         break;
675
676      case 5:
677         draw_mode5_scanline(gba_state, &xferscan[1024], y);
678         draw_gba_oam(gba_state, machine, &xferscan[1024], y, 3);
679         draw_gba_oam(gba_state, machine, &xferscan[1024], y, 2);
680         draw_gba_oam(gba_state, machine, &xferscan[1024], y, 1);
681         draw_gba_oam(gba_state, machine, &xferscan[1024], y, 0);
682         break;
683   }
684
685
686   // copy the working scanline to the real one
687   memcpy(scanline, &xferscan[1024], 240*2);
688
689   return;
690}
691
692void gba_video_start(running_machine *machine)
693{
694   int level, x;
695
696   /* generate a table to make mosaic fast */
697   for (level = 0; level < 16; level++)
698   {
699      for (x = 0; x < 4096; x++)
700      {
701         mosaic_offset[level][x] = (x / (level + 1)) * (level + 1);
702      }
703   }
704}
src/mess/includes/gba.h
r0r5377
1#ifndef _GBA_H_
2#define _GBA_H_
3
4#define DISPSTAT_VBL         0x0001
5#define DISPSTAT_HBL         0x0002
6#define DISPSTAT_VCNT         0x0004
7#define DISPSTAT_VBL_IRQ_EN      0x0008
8#define DISPSTAT_HBL_IRQ_EN      0x0010
9#define DISPSTAT_VCNT_IRQ_EN      0x0020
10#define DISPSTAT_VCNT_VALUE      0xff00
11
12#define INT_VBL            0x0001
13#define INT_HBL            0x0002
14#define INT_VCNT         0x0004
15#define INT_TM0_OVERFLOW      0x0008
16#define INT_TM1_OVERFLOW      0x0010
17#define INT_TM2_OVERFLOW      0x0020
18#define INT_TM3_OVERFLOW      0x0040
19#define INT_SIO            0x0080
20#define INT_DMA0         0x0100
21#define INT_DMA1         0x0200
22#define INT_DMA2         0x0400
23#define INT_DMA3         0x0800
24#define INT_KEYPAD         0x1000
25#define INT_GAMEPAK         0x2000
26
27#define DISPCNT_MODE         0x0007
28#define DISPCNT_FRAMESEL      0x0010
29#define DISPCNT_HBL_FREE      0x0020
30
31#define DISPCNT_VRAM_MAP      0x0040
32#define DISPCNT_VRAM_MAP_2D      0x0000
33#define DISPCNT_VRAM_MAP_1D      0x0040
34
35#define DISPCNT_BLANK         0x0080
36#define DISPCNT_BG0_EN         0x0100
37#define DISPCNT_BG1_EN         0x0200
38#define DISPCNT_BG2_EN         0x0400
39#define DISPCNT_BG3_EN         0x0800
40#define DISPCNT_OBJ_EN         0x1000
41#define DISPCNT_WIN0_EN         0x2000
42#define DISPCNT_WIN1_EN         0x4000
43#define DISPCNT_OBJWIN_EN      0x8000
44
45#define OBJ_Y_COORD         0x00ff
46#define OBJ_USEROZ         0x0100
47#define OBJ_DOUBLE         0x0200
48#define OBJ_DISABLE         0x0200
49
50#define OBJ_MODE         0x0c00
51#define OBJ_MODE_NORMAL         0x0000
52#define OBJ_MODE_ALPHA         0x0400
53#define OBJ_MODE_WINDOW         0x0800
54
55#define OBJ_MOSAIC         0x1000
56
57#define OBJ_PALMODE         0x2000
58#define OBJ_PALMODE_16         0x0000
59#define OBJ_PALMODE_256         0x2000
60
61#define OBJ_SHAPE         0xc000
62#define OBJ_SHAPE_SQR         0x0000
63#define OBJ_SHAPE_HORIZ         0x4000
64#define OBJ_SHAPE_VERT         0x8000
65
66#define OBJ_X_COORD         0x01ff
67#define OBJ_SCALE_PARAM         0x3e00
68#define OBJ_SCALE_PARAM_SHIFT      9
69#define OBJ_HFLIP         0x1000
70#define OBJ_VFLIP         0x2000
71#define OBJ_SIZE         0xc000
72#define OBJ_SIZE_8         0x0000
73#define OBJ_SIZE_16         0x4000
74#define OBJ_SIZE_32         0x8000
75#define OBJ_SIZE_64         0xc000
76
77#define OBJ_TILENUM             0x03ff
78#define OBJ_PRIORITY         0x0c00
79#define OBJ_PRIORITY_SHIFT      10
80#define OBJ_PALNUM         0xf000
81#define OBJ_PALNUM_SHIFT      12
82
83// states for carts with flash saves
84enum
85{
86    FLASH_IDLEBYTE0,
87    FLASH_IDLEBYTE1,
88    FLASH_IDLEBYTE2,
89    FLASH_IDENT,
90    FLASH_ERASEBYTE0,
91    FLASH_ERASEBYTE1,
92    FLASH_ERASEBYTE2,
93    FLASH_ERASE_ALL,
94    FLASH_ERASE_4K,
95    FLASH_WRITE
96};
97
98enum
99{
100   EEP_IDLE,
101   EEP_COMMAND,
102   EEP_ADDR,
103   EEP_AFTERADDR,
104   EEP_READ,
105   EEP_WRITE,
106   EEP_AFTERWRITE,
107   EEP_READFIRST
108};
109
110/* driver state */
111typedef struct
112{
113   UINT16 DISPCNT,   GRNSWAP, DISPSTAT;
114   UINT16 BG0CNT, BG1CNT, BG2CNT, BG3CNT;
115   UINT16 BG0HOFS, BG0VOFS, BG1HOFS, BG1VOFS, BG2HOFS, BG2VOFS, BG3HOFS, BG3VOFS;
116   UINT16 BG2PA, BG2PB, BG2PC, BG2PD, BG2X, BG2Y, BG3PA, BG3PB, BG3PC, BG3PD, BG3X, BG3Y;
117   UINT16 WIN0H, WIN1H, WIN0V, WIN1V, WININ, WINOUT;
118   UINT16 MOSAIC;
119   UINT16 BLDCNT;
120   UINT16 BLDALPHA;
121   UINT16 BLDY;
122   UINT8  SOUNDCNT_X;
123   UINT16 SOUNDCNT_H;
124   UINT16 SOUNDBIAS;
125   UINT16 SIOMULTI0, SIOMULTI1, SIOMULTI2, SIOMULTI3;
126   UINT16 SIOCNT, SIODATA8;
127   UINT16 KEYCNT;
128   UINT16 RCNT;
129   UINT16 JOYCNT;
130   UINT32 JOY_RECV, JOY_TRANS;
131   UINT16 JOYSTAT;
132   UINT16 IR, IE, IF, IME;
133   UINT16 WAITCNT;
134   UINT8  POSTFLG;
135   UINT8  HALTCNT;
136
137   UINT32 *gba_pram;
138   UINT32 *gba_vram;
139   UINT32 *gba_oam;
140
141   UINT32 dma_regs[16];
142   UINT32 dma_src[4], dma_dst[4], dma_cnt[4], dma_srcadd[4], dma_dstadd[4];
143   UINT32 timer_regs[4];
144   UINT16 timer_reload[4];
145
146   UINT32 gba_sram[0x10000/4];
147   UINT8 gba_eeprom[0x2000];
148   UINT32 gba_flash64k[0x10000/4];
149   UINT8 flash64k_state;
150   UINT8 flash64k_page;
151   int eeprom_state, eeprom_command, eeprom_count, eeprom_addr, eeprom_bits;
152   UINT8 eep_data;
153
154   emu_timer *dma_timer[4], *tmr_timer[4], *irq_timer;
155   emu_timer *scan_timer, *hbl_timer;
156
157   int fifo_a_ptr, fifo_b_ptr, fifo_a_in, fifo_b_in;
158   UINT8 fifo_a[20], fifo_b[20];
159} gba_state;
160
161// defined in video/gba.c
162void gba_draw_scanline(running_machine *machine, int y);
163void gba_video_start(running_machine *machine);
164
165#endif
src/mess/messdriv.c
r5376r5377
6969    DRIVER( sfcbox )    /* Nintendo Super Famicom Box                       */
7070    DRIVER( n64 )       /* Nintendo N64                                     */
7171    DRIVER( pokemini )  /* Nintendo Pokemon Mini                            */
72    DRIVER( gba )   /* Nintendo Game Boy Advance Handheld               */
7273
7374    DRIVER( megaduck )  /* Megaduck                                         */
7475
src/mess/mess.mak
r5376r5377
10371037   $(MESS_MACHINE)/pokemini.o   \
10381038   $(MESS_DRIVERS)/pokemini.o   \
10391039   $(MESS_DRIVERS)/vboy.o      \
1040   $(MESS_DRIVERS)/gba.o      \
1041   $(MESS_VIDEO)/gba.o      \
10401042
10411043$(MESSOBJ)/nokia.a:            \
10421044   $(MESS_DRIVERS)/mikromik.o   \
src/mess/drivers/gba.c
r0r5377
1/***************************************************************************
2
3  gba.c
4
5  Driver file to handle emulation of the Nintendo Game Boy Advance.
6
7  By R. Belmont & MooglyGuy
8
9  TODO:
10  - Raster interrupts have issues
11  - Roz sprites
12  - Roz BG support
13  - Windows
14  - Blending
15
16***************************************************************************/
17
18#include "driver.h"
19#include "state.h"
20#include "video/generic.h"
21#include "cpu/arm7/arm7.h"
22#include "cpu/arm7/arm7core.h"
23#include "devices/cartslot.h"
24#include "includes/gba.h"
25#include "includes/gb.h"
26#include "sound/dac.h"
27
28#define VERBOSE_LEVEL   (0)
29
30INLINE void verboselog(running_machine *machine, int n_level, const char *s_fmt, ...)
31{
32   if( VERBOSE_LEVEL >= n_level )
33   {
34      va_list v;
35      char buf[ 32768 ];
36      va_start( v, s_fmt );
37      vsprintf( buf, s_fmt, v );
38      va_end( v );
39      logerror( "%08x: %s", cpu_get_pc(machine->cpu[0]), buf );
40   }
41}
42
43static UINT32 timer_clks[4] = { 16777216, 16777216/64, 16777216/256, 16777216/1024 };
44
45static UINT8 *nvptr;
46static UINT32 nvsize = 0;
47static const device_config *nvimage;
48
49static void gba_machine_stop(running_machine *machine)
50{
51   // only do this if the cart loader detected some form of backup
52   if (nvsize > 0)
53   {
54      image_battery_save(nvimage, nvptr, nvsize);
55   }
56}
57
58static PALETTE_INIT( gba )
59{
60   UINT8 r, g, b;
61   for( b = 0; b < 32; b++ )
62   {
63      for( g = 0; g < 32; g++ )
64      {
65         for( r = 0; r < 32; r++ )
66         {
67            palette_set_color_rgb( machine, ( b << 10 ) | ( g << 5 ) | r, pal5bit(r), pal5bit(g), pal5bit(b) );
68         }
69      }
70   }
71}
72
73static void dma_exec(running_machine *machine, FPTR ch);
74
75static void gba_request_irq(running_machine *machine, UINT32 int_type)
76{
77   gba_state *gba_state = machine->driver_data;
78
79   // set flag for later recovery
80   gba_state->IF |= int_type;
81
82   // is this specific interrupt enabled?
83   int_type &= gba_state->IE;
84   if (int_type != 0)
85   {
86      // master enable?
87      if (gba_state->IME & 1)
88      {
89         cpu_set_input_line(machine->cpu[0], ARM7_IRQ_LINE, ASSERT_LINE);
90         cpu_set_input_line(machine->cpu[0], ARM7_IRQ_LINE, CLEAR_LINE);
91      }
92   }
93}
94
95TIMER_CALLBACK( dma_complete )
96{
97   int ctrl;
98   FPTR ch;
99   static UINT32 ch_int[4] = { INT_DMA0, INT_DMA1, INT_DMA2, INT_DMA3 };
100   gba_state *gba_state = machine->driver_data;
101
102   ch = param;
103
104//   printf("dma complete: ch %d\n", ch);
105
106   timer_adjust_oneshot(gba_state->dma_timer[ch], attotime_never, 0);
107
108   ctrl = gba_state->dma_regs[(ch*3)+2] >> 16;
109
110   // IRQ
111   if (ctrl & 0x4000)
112   {
113      gba_request_irq(machine, ch_int[ch]);
114   }
115
116   // if we're supposed to repeat, don't clear "active" and then the next vbl/hbl will retrigger us
117   // always clear active for immediate DMAs though
118   if (!((ctrl>>9) & 1) || ((ctrl & 0x3000) == 0))
119   {
120//      printf("clear active for ch %d\n", ch);
121      gba_state->dma_regs[(ch*3)+2] &= ~0x80000000;   // clear "active" bit
122   }
123   else
124   {
125      // if repeat, reload the count
126      if ((ctrl>>9) & 1)
127      {
128         gba_state->dma_cnt[ch] = gba_state->dma_regs[(ch*3)+2]&0xffff;
129
130         // if increment & reload mode, reload the destination
131         if (((ctrl>>5)&3) == 3)
132         {
133            gba_state->dma_dst[ch] = gba_state->dma_regs[(ch*3)+1];
134         }
135      }
136   }
137}
138
139static void dma_exec(running_machine *machine, FPTR ch)
140{
141   int i, cnt;
142   int ctrl;
143   int srcadd, dstadd;
144   UINT32 src, dst;
145   const address_space *space = cpu_get_address_space(machine->cpu[0], ADDRESS_SPACE_PROGRAM);
146   gba_state *gba_state = machine->driver_data;
147
148   src = gba_state->dma_src[ch];
149   dst = gba_state->dma_dst[ch];
150   ctrl = gba_state->dma_regs[(ch*3)+2] >> 16;
151   srcadd = gba_state->dma_srcadd[ch];
152    dstadd = gba_state->dma_dstadd[ch];
153
154   cnt = gba_state->dma_cnt[ch];
155   if (!cnt)
156   {
157      if (ch == 3)
158      {
159         cnt = 0x10000;
160      }
161      else
162      {
163         cnt = 0x4000;
164      }
165   }
166
167   // override special parameters
168   if ((ctrl & 0x3000) == 0x3000)      // special xfer mode
169   {
170      switch (ch)
171      {
172         case 1:         // Ch 1&2 are for audio DMA
173         case 2:
174            dstadd = 2;   // don't increment destination
175            cnt = 4;   // always transfer 4 32-bit words
176            ctrl |= 0x400;   // always 32-bit
177            break;
178
179         case 3:
180            printf("Unsupported DMA 3 special mode\n");
181            break;
182      }
183   }
184   else
185   {
186//      if (dst >= 0x6000000 && dst <= 0x6017fff)
187//         printf("DMA exec: ch %d from %08x to %08x, mode %04x, count %04x (PC %x) (%s)\n", (int)ch, src, dst, ctrl, cnt, activecpu_get_pc(), ((ctrl>>10) & 1) ? "32" : "16");
188   }
189
190   for (i = 0; i < cnt; i++)
191   {
192      if ((ctrl>>10) & 1)
193      {
194         src &= 0xfffffffc;
195         dst &= 0xfffffffc;
196
197         // 32-bit
198         memory_write_dword(space, dst, memory_read_dword(space, src));
199         switch (dstadd)
200         {
201            case 0:   // increment
202               dst += 4;
203               break;
204            case 1:   // decrement
205               dst -= 4;
206               break;
207            case 2:   // don't move
208               break;
209            case 3:   // increment and reload
210               dst += 4;
211               break;
212         }
213         switch (srcadd)
214         {
215            case 0:   // increment
216               src += 4;
217               break;
218            case 1:   // decrement
219               src -= 4;
220               break;
221            case 2:   // don't move
222               break;
223            case 3:   // not used
224               printf("DMA: Bad srcadd 3!\n");
225               src += 4;
226               break;
227         }
228      }
229      else
230      {
231         src &= 0xfffffffe;
232         dst &= 0xfffffffe;
233
234          // 16-bit
235         memory_write_word(space, dst, memory_read_word(space, src));
236         switch (dstadd)
237         {
238            case 0:   // increment
239               dst += 2;
240               break;
241            case 1:   // decrement
242               dst -= 2;
243               break;
244            case 2:   // don't move
245               break;
246            case 3:   // increment and reload
247               dst += 2;
248               break;
249         }
250         switch (srcadd)
251         {
252            case 0:   // increment
253               src += 2;
254               break;
255            case 1:   // decrement
256               src -= 2;
257               break;
258            case 2:   // don't move
259               break;
260            case 3:   // not used
261               printf("DMA: Bad srcadd 3!\n");
262               break;
263         }
264      }
265   }
266
267   gba_state->dma_src[ch] = src;
268   gba_state->dma_dst[ch] = dst;
269
270//   printf("settng DMA timer %d for %d cycs (tmr %x)\n", ch, cnt, (UINT32)gba_state->dma_timer[ch]);
271//   timer_adjust_oneshot(gba_state->dma_timer[ch], ATTOTIME_IN_CYCLES(0, cnt), ch);
272   dma_complete(machine, NULL, ch);
273}
274
275static void audio_tick(running_machine *machine, int ref)
276{
277   gba_state *gba_state = machine->driver_data;
278
279   if (!(gba_state->SOUNDCNT_X & 0x80))
280   {
281      return;
282   }
283
284   if (!ref)
285   {
286      if (gba_state->fifo_a_ptr != gba_state->fifo_a_in)
287      {
288         if (gba_state->fifo_a_ptr == 17)
289         {
290            gba_state->fifo_a_ptr = 0;
291         }
292
293         if (gba_state->SOUNDCNT_H & 0x100)
294         {
295            const device_config *dac_device = devtag_get_device(machine, "direct_a_left");
296
297            dac_signed_data_w(dac_device, gba_state->fifo_a[gba_state->fifo_a_ptr]^0x80);
298         }
299         if (gba_state->SOUNDCNT_H & 0x200)
300         {
301            const device_config *dac_device = devtag_get_device(machine, "direct_a_right");
302
303            dac_signed_data_w(dac_device, gba_state->fifo_a[gba_state->fifo_a_ptr]^0x80);
304         }
305         gba_state->fifo_a_ptr++;
306      }
307
308      // fifo empty?
309      if (gba_state->fifo_a_ptr == gba_state->fifo_a_in)
310      {
311         // is a DMA set up to feed us?
312         if ((gba_state->dma_regs[(1*3)+1] == 0x40000a0) && ((gba_state->dma_regs[(1*3)+2] & 0x30000000) == 0x30000000))
313         {
314            // channel 1 it is
315            dma_exec(machine, 1);
316         }
317         if ((gba_state->dma_regs[(2*3)+1] == 0x40000a0) && ((gba_state->dma_regs[(2*3)+2] & 0x30000000) == 0x30000000))
318         {
319            // channel 2 it is
320            dma_exec(machine, 2);
321         }
322      }
323   }
324   else
325   {
326      if (gba_state->fifo_b_ptr != gba_state->fifo_b_in)
327      {
328         if (gba_state->fifo_b_ptr == 17)
329         {
330            gba_state->fifo_b_ptr = 0;
331         }
332
333         if (gba_state->SOUNDCNT_H & 0x1000)
334         {
335            const device_config *dac_device = devtag_get_device(machine, "direct_b_left");
336
337            dac_signed_data_w(dac_device, gba_state->fifo_b[gba_state->fifo_b_ptr]^0x80);
338         }
339         if (gba_state->SOUNDCNT_H & 0x2000)
340         {
341            const device_config *dac_device = devtag_get_device(machine, "direct_b_right");
342
343            dac_signed_data_w(dac_device, gba_state->fifo_b[gba_state->fifo_b_ptr]^0x80);
344         }
345         gba_state->fifo_b_ptr++;
346      }
347
348      if (gba_state->fifo_b_ptr == gba_state->fifo_b_in)
349      {
350         // is a DMA set up to feed us?
351         if ((gba_state->dma_regs[(1*3)+1] == 0x40000a4) && ((gba_state->dma_regs[(1*3)+2] & 0x30000000) == 0x30000000))
352         {
353            // channel 1 it is
354            dma_exec(machine, 1);
355         }
356         if ((gba_state->dma_regs[(2*3)+1] == 0x40000a4) && ((gba_state->dma_regs[(2*3)+2] & 0x30000000) == 0x30000000))
357         {
358            // channel 2 it is
359            dma_exec(machine, 2);
360         }
361      }
362   }
363}
364
365static TIMER_CALLBACK(timer_expire)
366{
367   static UINT32 tmr_ints[4] = { INT_TM0_OVERFLOW, INT_TM1_OVERFLOW, INT_TM2_OVERFLOW, INT_TM3_OVERFLOW };
368   FPTR tmr = (FPTR) param;
369   gba_state *gba_state = machine->driver_data;
370
371//   printf("Timer %d expired, SOUNDCNT_H %04x\n", tmr, gba_state->SOUNDCNT_H);
372
373   // check if timers 0 or 1 are feeding directsound
374   if (tmr == 0)
375   {
376      if ((gba_state->SOUNDCNT_H & 0x400) == 0)
377      {
378         audio_tick(machine, 0);
379      }
380
381      if ((gba_state->SOUNDCNT_H & 0x4000) == 0)
382      {
383         audio_tick(machine, 1);
384      }
385   }
386
387   if (tmr == 1)
388   {
389      if ((gba_state->SOUNDCNT_H & 0x400) == 0x400)
390      {
391         audio_tick(machine, 0);
392      }
393
394      if ((gba_state->SOUNDCNT_H & 0x4000) == 0x4000)
395      {
396         audio_tick(machine, 1);
397      }
398   }
399
400   // Handle count-up timing
401   switch (tmr)
402   {
403   case 0:
404           if (gba_state->timer_regs[1] & 0x40000)
405           {
406              gba_state->timer_regs[1] = ( ( gba_state->timer_regs[1] & 0x0000ffff ) + 1 ) & 0x0000ffff;
407              if( ( gba_state->timer_regs[1] & 0x0000ffff ) == 0 )
408              {
409                      gba_state->timer_regs[1] |= gba_state->timer_reload[1];
410                      if( ( gba_state->timer_regs[1] & 0x400000 ) && ( gba_state->IME != 0 ) )
411                      {
412                         gba_request_irq( machine, tmr_ints[1] );
413                      }
414                      if( ( gba_state->timer_regs[2] & 0x40000 ) )
415                      {
416                         gba_state->timer_regs[2] = ( ( gba_state->timer_regs[2] & 0x0000ffff ) + 1 ) & 0x0000ffff;
417                         if( ( gba_state->timer_regs[2] & 0x0000ffff ) == 0 )
418                         {
419                                 gba_state->timer_regs[2] |= gba_state->timer_reload[2];
420                                 if( ( gba_state->timer_regs[2] & 0x400000 ) && ( gba_state->IME != 0 ) )
421                                 {
422                                    gba_request_irq( machine, tmr_ints[2] );
423                                 }
424                                 if( ( gba_state->timer_regs[3] & 0x40000 ) )
425                                 {
426                                    gba_state->timer_regs[3] = ( ( gba_state->timer_regs[3] & 0x0000ffff ) + 1 ) & 0x0000ffff;
427                                    if( ( gba_state->timer_regs[3] & 0x0000ffff ) == 0 )
428                                    {
429                                            gba_state->timer_regs[3] |= gba_state->timer_reload[3];
430                                            if( ( gba_state->timer_regs[3] & 0x400000 ) && ( gba_state->IME != 0 ) )
431                                            {
432                                               gba_request_irq( machine, tmr_ints[3] );
433                                            }
434                                    }
435                                 }
436                         }
437                      }
438              }
439           }
440           break;
441   case 1:
442           if (gba_state->timer_regs[2] & 0x40000)
443           {
444              gba_state->timer_regs[2] = ( ( gba_state->timer_regs[2] & 0x0000ffff ) + 1 ) & 0x0000ffff;
445              if( ( gba_state->timer_regs[2] & 0x0000ffff ) == 0 )
446              {
447                      gba_state->timer_regs[2] |= gba_state->timer_reload[2];
448                      if( ( gba_state->timer_regs[2] & 0x400000 ) && ( gba_state->IME != 0 ) )
449                      {
450                         gba_request_irq( machine, tmr_ints[2] );
451                      }
452                      if( ( gba_state->timer_regs[3] & 0x40000 ) )
453                      {
454                         gba_state->timer_regs[3] = ( ( gba_state->timer_regs[3] & 0x0000ffff ) + 1 ) & 0x0000ffff;
455                         if( ( gba_state->timer_regs[3] & 0x0000ffff ) == 0 )
456                         {
457                                 gba_state->timer_regs[2] |= gba_state->timer_reload[2];
458                                 if( ( gba_state->timer_regs[3] & 0x400000 ) && ( gba_state->IME != 0 ) )
459                                 {
460                                    gba_request_irq( machine, tmr_ints[3] );
461                                 }
462                         }
463                      }
464              }
465           }
466           break;
467   case 2:
468           if (gba_state->timer_regs[3] & 0x40000)
469           {
470              gba_state->timer_regs[3] = ( ( gba_state->timer_regs[3] & 0x0000ffff ) + 1 ) & 0x0000ffff;
471              if( ( gba_state->timer_regs[3] & 0x0000ffff ) == 0 )
472              {
473                      gba_state->timer_regs[3] |= gba_state->timer_reload[3];
474                      if( ( gba_state->timer_regs[3] & 0x400000 ) && ( gba_state->IME != 0 ) )
475                      {
476                         gba_request_irq( machine, tmr_ints[3] );
477                      }
478              }
479           }
480           break;
481   }
482
483   // are we supposed to IRQ?
484   if ((gba_state->timer_regs[tmr] & 0x400000) && (gba_state->IME != 0))
485   {
486      gba_request_irq(machine, tmr_ints[tmr]);
487   }
488}
489
490TIMER_CALLBACK(handle_irq)
491{
492   gba_state *gba_state = machine->driver_data;
493
494   gba_request_irq(machine, gba_state->IF);
495
496   timer_adjust_oneshot(gba_state->irq_timer, attotime_never, 0);
497}
498
499static READ32_HANDLER( gba_io_r )
500{
501   UINT32 retval = 0;
502   running_machine *machine = space->machine;
503   const device_config *gb_device = devtag_get_device(space->machine, "custom");
504   gba_state *gba_state = machine->driver_data;
505
506   switch( offset )
507   {
508      case 0x0000/4:
509         if( (mem_mask) & 0x0000ffff )
510         {
511            verboselog(machine, 2, "GBA IO Register Read: DISPCNT (%08x) = %04x\n", 0x04000000 + ( offset << 2 ), gba_state->DISPCNT );
512            retval |= gba_state->DISPCNT;
513         }
514         if( (mem_mask) & 0xffff0000 )
515         {
516            verboselog(machine, 2, "GBA IO Register Read: Green Swap (%08x) = %04x\n", 0x04000000 + ( offset << 2 ) + 2, gba_state->GRNSWAP );
517            retval |= gba_state->GRNSWAP << 16;
518         }
519         break;
520      case 0x0004/4:
521         retval = gba_state->DISPSTAT | (video_screen_get_vpos(machine->primary_screen)<<16);
522         break;
523      case 0x0008/4:
524         if( (mem_mask) & 0x0000ffff )
525         {
526            verboselog(machine, 2, "GBA IO Register Read: BG0CNT (%08x) = %04x\n", 0x04000000 + ( offset << 2 ), gba_state->BG0CNT );
527            retval |= gba_state->BG0CNT;
528         }
529         if( (mem_mask) & 0xffff0000 )
530         {
531            verboselog(machine, 2, "GBA IO Register Read: BG1CNT (%08x) = %04x\n", 0x04000000 + ( offset << 2 ) + 2, gba_state->BG1CNT );
532            retval |= gba_state->BG1CNT << 16;
533         }
534         break;
535      case 0x000c/4:
536         if( (mem_mask) & 0x0000ffff )
537         {
538            verboselog(machine, 2, "GBA IO Register Read: BG2CNT (%08x) = %04x\n", 0x04000000 + ( offset << 2 ), gba_state->BG2CNT );
539            retval |= gba_state->BG2CNT;
540         }
541         if( (mem_mask) & 0xffff0000 )
542         {
543            verboselog(machine, 2, "GBA IO Register Read: BG3CNT (%08x) = %04x\n", 0x04000000 + ( offset << 2 ) + 2, gba_state->BG3CNT );
544            retval |= gba_state->BG3CNT << 16;
545         }
546         break;
547      case 0x0010/4:
548         if( (mem_mask) & 0x0000ffff )
549         {
550            verboselog(machine, 2, "GBA IO Register Read: BG0HOFS (%08x) = %04x\n", 0x04000000 + ( offset << 2 ), 0 );
551         }
552         if( (mem_mask) & 0xffff0000 )
553         {
554            verboselog(machine, 2, "GBA IO Register Read: BG0VOFS (%08x) = %04x\n", 0x04000000 + ( offset << 2 ) + 2, 0 );
555         }
556         break;
557      case 0x0014/4:
558         if( (mem_mask) & 0x0000ffff )
559         {
560            verboselog(machine, 2, "GBA IO Register Read: BG1HOFS (%08x) = %04x\n", 0x04000000 + ( offset << 2 ), 0 );
561         }
562         if( (mem_mask) & 0xffff0000 )
563         {
564            verboselog(machine, 2, "GBA IO Register Read: BG1VOFS (%08x) = %04x\n", 0x04000000 + ( offset << 2 ) + 2, 0 );
565         }
566         break;
567      case 0x0018/4:
568         if( (mem_mask) & 0x0000ffff )
569         {
570            verboselog(machine, 2, "GBA IO Register Read: BG2HOFS (%08x) = %04x\n", 0x04000000 + ( offset << 2 ), 0 );
571         }
572         if( (mem_mask) & 0xffff0000 )
573         {
574            verboselog(machine, 2, "GBA IO Register Read: BG2VOFS (%08x) = %04x\n", 0x04000000 + ( offset << 2 ) + 2, 0 );
575         }
576         break;
577      case 0x001c/4:
578         if( (mem_mask) & 0x0000ffff )
579         {
580            verboselog(machine, 2, "GBA IO Register Read: BG3HOFS (%08x) = %04x\n", 0x04000000 + ( offset << 2 ), 0 );
581         }
582         if( (mem_mask) & 0xffff0000 )
583         {
584            verboselog(machine, 2, "GBA IO Register Read: BG3VOFS (%08x) = %04x\n", 0x04000000 + ( offset << 2 ) + 2, 0 );
585         }
586         break;
587      case 0x0020/4:
588         if( (mem_mask) & 0x0000ffff )
589         {
590            verboselog(machine, 2, "GBA IO Register Read: BG2PA (%08x) = %04x\n", 0x04000000 + ( offset << 2 ), 0 );
591         }
592         if( (mem_mask) & 0xffff0000 )
593         {
594            verboselog(machine, 2, "GBA IO Register Read: BG2PB (%08x) = %04x\n", 0x04000000 + ( offset << 2 ) + 2, 0 );
595         }
596         break;
597      case 0x0024/4:
598         if( (mem_mask) & 0x0000ffff )
599         {
600            verboselog(machine, 2, "GBA IO Register Read: BG2PC (%08x) = %04x\n", 0x04000000 + ( offset << 2 ), 0 );
601         }
602         if( (mem_mask) & 0xffff0000 )
603         {
604            verboselog(machine, 2, "GBA IO Register Read: BG2PD (%08x) = %04x\n", 0x04000000 + ( offset << 2 ) + 2, 0 );
605         }
606         break;
607      case 0x0028/4:
608         if( (mem_mask) & 0x0000ffff )
609         {
610            verboselog(machine, 2, "GBA IO Register Read: BG2X_LSW (%08x) = %04x\n", 0x04000000 + ( offset << 2 ), 0 );
611         }
612         if( (mem_mask) & 0xffff0000 )
613         {
614            verboselog(machine, 2, "GBA IO Register Read: BG2X_MSW (%08x) = %04x\n", 0x04000000 + ( offset << 2 ) + 2, 0 );
615         }
616         break;
617      case 0x002c/4:
618         if( (mem_mask) & 0x0000ffff )
619         {
620            verboselog(machine, 2, "GBA IO Register Read: BG2Y_LSW (%08x) = %04x\n", 0x04000000 + ( offset << 2 ), 0 );
621         }
622         if( (mem_mask) & 0xffff0000 )
623         {
624            verboselog(machine, 2, "GBA IO Register Read: BG2Y_MSW (%08x) = %04x\n", 0x04000000 + ( offset << 2 ) + 2, 0 );
625         }
626         break;
627      case 0x0030/4:
628         if( (mem_mask) & 0x0000ffff )
629         {
630            verboselog(machine, 2, "GBA IO Register Read: BG3PA (%08x) = %04x\n", 0x04000000 + ( offset << 2 ), 0 );
631         }
632         if( (mem_mask) & 0xffff0000 )
633         {
634            verboselog(machine, 2, "GBA IO Register Read: BG3PB (%08x) = %04x\n", 0x04000000 + ( offset << 2 ) + 2, 0 );
635         }
636         break;
637      case 0x0034/4:
638         if( (mem_mask) & 0x0000ffff )
639         {
640            verboselog(machine, 2, "GBA IO Register Read: BG3PC (%08x) = %04x\n", 0x04000000 + ( offset << 2 ), 0 );
641         }
642         if( (mem_mask) & 0xffff0000 )
643         {
644            verboselog(machine, 2, "GBA IO Register Read: BG3PD (%08x) = %04x\n", 0x04000000 + ( offset << 2 ) + 2, 0 );
645         }
646         break;
647      case 0x0038/4:
648         if( (mem_mask) & 0x0000ffff )
649         {
650            verboselog(machine, 2, "GBA IO Register Read: BG3X_LSW (%08x) = %04x\n", 0x04000000 + ( offset << 2 ), 0 );
651         }
652         if( (mem_mask) & 0xffff0000 )
653         {
654            verboselog(machine, 2, "GBA IO Register Read: BG3X_MSW (%08x) = %04x\n", 0x04000000 + ( offset << 2 ) + 2, 0 );
655         }
656         break;
657      case 0x003c/4:
658         if( (mem_mask) & 0x0000ffff )
659         {
660            verboselog(machine, 2, "GBA IO Register Read: BG3Y_LSW (%08x) = %04x\n", 0x04000000 + ( offset << 2 ), 0 );
661         }
662         if( (mem_mask) & 0xffff0000 )
663         {
664            verboselog(machine, 2, "GBA IO Register Read: BG3Y_MSW (%08x) = %04x\n", 0x04000000 + ( offset << 2 ) + 2, 0 );
665         }
666         break;
667      case 0x0040/4:
668         if( (mem_mask) & 0x0000ffff )
669         {
670            verboselog(machine, 2, "GBA IO Register Read: WIN0H (%08x) = %04x\n", 0x04000000 + ( offset << 2 ), 0 );
671         }
672         if( (mem_mask) & 0xffff0000 )
673         {
674            verboselog(machine, 2, "GBA IO Register Read: WIN1H (%08x) = %04x\n", 0x04000000 + ( offset << 2 ) + 2, 0 );
675         }
676         break;
677      case 0x0044/4:
678         if( (mem_mask) & 0x0000ffff )
679         {
680            verboselog(machine, 2, "GBA IO Register Read: WIN0V (%08x) = %04x\n", 0x04000000 + ( offset << 2 ), 0 );
681         }
682         if( (mem_mask) & 0xffff0000 )
683         {
684            verboselog(machine, 2, "GBA IO Register Read: WIN1V (%08x) = %04x\n", 0x04000000 + ( offset << 2 ) + 2, 0 );
685         }
686         break;
687      case 0x0048/4:
688         if( (mem_mask) & 0x0000ffff )
689         {
690            verboselog(machine, 2, "GBA IO Register Read: WININ (%08x) = %04x\n", 0x04000000 + ( offset << 2 ), gba_state->WININ );
691            retval |= gba_state->WININ;
692         }
693         if( (mem_mask) & 0xffff0000 )
694         {
695            verboselog(machine, 2, "GBA IO Register Read: WINOUT (%08x) = %04x\n", 0x04000000 + ( offset << 2 ) + 2, gba_state->WINOUT );
696            retval |= gba_state->WINOUT << 16;
697         }
698         break;
699      case 0x004c/4:
700         if( (mem_mask) & 0x0000ffff )
701         {
702            verboselog(machine, 2, "GBA IO Register Read: MOSAIC (%08x) = %04x\n", 0x04000000 + ( offset << 2 ), 0 );
703         }
704         if( (mem_mask) & 0xffff0000 )
705         {
706            verboselog(machine, 2, "GBA IO Register Read: UNKNOWN (%08x) = %04x\n", 0x04000000 + ( offset << 2 ) + 2, 0 );
707         }
708         break;
709      case 0x0050/4:
710         if( (mem_mask) & 0x0000ffff )
711         {
712            verboselog(machine, 2, "GBA IO Register Read: BLDCNT (%08x) = %04x\n", 0x04000000 + ( offset << 2 ), gba_state->BLDCNT );
713            retval |= gba_state->BLDCNT;
714         }
715         if( (mem_mask) & 0xffff0000 )
716         {
717            verboselog(machine, 2, "GBA IO Register Read: BLDALPHA (%08x) = %04x\n", 0x04000000 + ( offset << 2 ) + 2, 0 );
718         }
719         break;
720      case 0x0054/4:
721         if( (mem_mask) & 0x0000ffff )
722         {
723            verboselog(machine, 2, "GBA IO Register Read: BLDY (%08x) = %04x\n", 0x04000000 + ( offset << 2 ), 0 );
724         }
725         if( (mem_mask) & 0xffff0000 )
726         {
727            verboselog(machine, 2, "GBA IO Register Read: UNKNOWN (%08x) = %04x\n", 0x04000000 + ( offset << 2 ) + 2, 0 );
728         }
729         break;
730      case 0x0058/4:
731         if( (mem_mask) & 0x0000ffff )
732         {
733            verboselog(machine, 2, "GBA IO Register Read: UNKNOWN (%08x) = %04x\n", 0x04000000 + ( offset << 2 ), 0 );
734         }
735         if( (mem_mask) & 0xffff0000 )
736         {
737            verboselog(machine, 2, "GBA IO Register Read: UNKNOWN (%08x) = %04x\n", 0x04000000 + ( offset << 2 ) + 2, 0 );
738         }
739         break;
740      case 0x005c/4:
741         if( (mem_mask) & 0x0000ffff )
742         {
743            verboselog(machine, 2, "GBA IO Register Read: UNKNOWN (%08x) = %04x\n", 0x04000000 + ( offset << 2 ), 0 );
744         }
745         if( (mem_mask) & 0xffff0000 )
746         {
747            verboselog(machine, 2, "GBA IO Register Read: UNKNOWN (%08x) = %04x\n", 0x04000000 + ( offset << 2 ) + 2, 0 );
748         }
749         break;
750      case 0x0060/4:
751         retval = gb_sound_r(gb_device, 0) | gb_sound_r(gb_device, 1)<<16 | gb_sound_r(gb_device, 2)<<24;
752         break;
753      case 0x0064/4:
754         retval = gb_sound_r(gb_device, 3) | gb_sound_r(gb_device, 4)<<8;
755         break;
756      case 0x0068/4:
757         retval = gb_sound_r(gb_device, 6) | gb_sound_r(gb_device, 7)<<8;
758         break;
759      case 0x006c/4:
760         retval = gb_sound_r(gb_device, 8) | gb_sound_r(gb_device, 9)<<8;
761         break;
762      case 0x0070/4:
763         retval = gb_sound_r(gb_device, 0xa) | gb_sound_r(gb_device, 0xb)<<16 | gb_sound_r(gb_device, 0xc)<<24;
764         break;
765      case 0x0074/4:
766         retval = gb_sound_r(gb_device, 0xd) | gb_sound_r(gb_device, 0xe)<<8;
767         break;
768      case 0x0078/4:
769         retval = gb_sound_r(gb_device, 0x10) | gb_sound_r(gb_device, 0x11)<<8;
770         break;
771      case 0x007c/4:
772         retval = gb_sound_r(gb_device, 0x12) | gb_sound_r(gb_device, 0x13)<<8;
773         break;
774      case 0x0080/4:
775         retval = gb_sound_r(gb_device, 0x14) | gb_sound_r(gb_device, 0x15)<<8;
776         if( (mem_mask) & 0xffff0000 )
777         {
778            verboselog(machine, 2, "GBA IO Register Read: SOUNDCNT_H (%08x) = %04x\n", 0x04000000 + ( offset << 2 ) + 2, gba_state->SOUNDCNT_H );
779            retval |= gba_state->SOUNDCNT_H << 16;
780         }
781         break;
782      case 0x0084/4:
783         retval = gb_sound_r(gb_device, 0x16);
784         break;
785      case 0x0088/4:
786         if( (mem_mask) & 0x0000ffff )
787         {
788            verboselog(machine, 2, "GBA IO Register Read: SOUNDBIAS (%08x) = %04x\n", 0x04000000 + ( offset << 2 ), gba_state->SOUNDBIAS );
789            retval |= gba_state->SOUNDBIAS;
790         }
791         if( (mem_mask) & 0xffff0000 )
792         {
793            verboselog(machine, 2, "GBA IO Register Read: UNKNOWN (%08x) = %04x\n", 0x04000000 + ( offset << 2 ) + 2, 0 );
794         }
795         break;
796      case 0x0090/4:
797         retval = gb_wave_r(gb_device, 0) | gb_wave_r(gb_device, 1)<<8 | gb_wave_r(gb_device, 2)<<16 | gb_wave_r(gb_device, 3)<<24;
798         break;
799      case 0x0094/4:
800         retval = gb_wave_r(gb_device, 4) | gb_wave_r(gb_device, 5)<<8 | gb_wave_r(gb_device, 6)<<16 | gb_wave_r(gb_device, 7)<<24;
801         break;
802      case 0x0098/4:
803         retval = gb_wave_r(gb_device, 8) | gb_wave_r(gb_device, 9)<<8 | gb_wave_r(gb_device, 10)<<16 | gb_wave_r(gb_device, 11)<<24;
804         break;
805      case 0x009c/4:
806         retval = gb_wave_r(gb_device, 12) | gb_wave_r(gb_device, 13)<<8 | gb_wave_r(gb_device, 14)<<16 | gb_wave_r(gb_device, 15)<<24;
807         break;
808      case 0x00a0/4:
809      case 0x00a4/4:
810         return 0;   // (does this actually do anything on real h/w?)
811         break;
812      case 0x00b0/4:
813      case 0x00b4/4:
814      case 0x00b8/4:
815      case 0x00bc/4:
816      case 0x00c0/4:
817      case 0x00c4/4:
818      case 0x00c8/4:
819      case 0x00cc/4:
820      case 0x00d0/4:
821      case 0x00d4/4:
822      case 0x00d8/4:
823      case 0x00dc/4:
824         {
825            // no idea why here, but it matches VBA better
826            if (((offset-0xb0/4) % 3) == 2)
827            {
828               return gba_state->dma_regs[offset-(0xb0/4)] & 0xff000000;
829            }
830
831            return gba_state->dma_regs[offset-(0xb0/4)];
832         }
833         break;
834      case 0x0100/4:
835      case 0x0104/4:
836      case 0x0108/4:
837      case 0x010c/4:
838         return gba_state->timer_regs[offset-(0x100/4)];
839         break;
840      case 0x0120/4:
841         if( (mem_mask) & 0x0000ffff )
842         {
843            verboselog(machine, 2, "GBA IO Register Read: SIOMULTI0 (%08x) = %04x\n", 0x04000000 + ( offset << 2 ), gba_state->SIOMULTI0 );
844            retval |= gba_state->SIOMULTI0;
845         }
846         if( (mem_mask) & 0xffff0000 )
847         {
848            verboselog(machine, 2, "GBA IO Register Read: SIOMULTI1 (%08x) = %04x\n", 0x04000000 + ( offset << 2 ) + 2, gba_state->SIOMULTI1 );
849            retval |= gba_state->SIOMULTI1 << 16;
850         }
851         break;
852      case 0x0124/4:
853         if( (mem_mask) & 0x0000ffff )
854         {
855            verboselog(machine, 2, "GBA IO Register Read: SIOMULTI2 (%08x) = %04x\n", 0x04000000 + ( offset << 2 ), gba_state->SIOMULTI2 );
856            retval |= gba_state->SIOMULTI2;
857         }
858         if( (mem_mask) & 0xffff0000 )
859         {
860            verboselog(machine, 2, "GBA IO Register Read: SIOMULTI3 (%08x) = %04x\n", 0x04000000 + ( offset << 2 ) + 2, gba_state->SIOMULTI3 );
861            retval |= gba_state->SIOMULTI3 << 16;
862         }
863         break;
864      case 0x0128/4:
865         if( (mem_mask) & 0x0000ffff )
866         {
867            verboselog(machine, 2, "GBA IO Register Read: SIOCNT (%08x) = %04x\n", 0x04000000 + ( offset << 2 ), gba_state->SIOCNT );
868            retval |= gba_state->SIOCNT;
869         }
870         if( (mem_mask) & 0xffff0000 )
871         {
872            verboselog(machine, 2, "GBA IO Register Read: SIODATA8 (%08x) = %04x\n", 0x04000000 + ( offset << 2 ) + 2, gba_state->SIODATA8 );
873            retval |= gba_state->SIODATA8 << 16;
874         }
875         break;
876      case 0x0130/4:
877         if( (mem_mask) & 0x0000ffff )   // KEYINPUT
878         {
879            return input_port_read(machine, "IN0");
880         }
881         if( (mem_mask) & 0xffff0000 )
882         {
883            verboselog(machine, 2, "GBA IO Register Read: KEYCNT (%08x) = %04x\n", 0x04000000 + ( offset << 2 ) + 2, gba_state->KEYCNT );
884            retval |= gba_state->KEYCNT << 16;
885         }
886         break;
887      case 0x0134/4:
888         if( (mem_mask) & 0x0000ffff )
889         {
890            verboselog(machine, 2, "GBA IO Register Read: RCNT (%08x) = %04x\n", 0x04000000 + ( offset << 2 ), gba_state->RCNT );
891            retval |= gba_state->RCNT;
892         }
893         if( (mem_mask) & 0xffff0000 )
894         {
895            verboselog(machine, 2, "GBA IO Register Read: IR (%08x) = %04x\n", 0x04000000 + ( offset << 2 ) + 2, 0 );
896         }
897         break;
898      case 0x0140/4:
899         if( (mem_mask) & 0x0000ffff )
900         {
901            verboselog(machine, 2, "GBA IO Register Read: JOYCNT (%08x) = %04x\n", 0x04000000 + ( offset << 2 ), gba_state->JOYCNT );
902            retval |= gba_state->JOYCNT;
903         }
904         if( (mem_mask) & 0xffff0000 )
905         {
906            verboselog(machine, 2, "GBA IO Register Read: UNKNOWN (%08x) = %04x\n", 0x04000000 + ( offset << 2 ) + 2, 0 );
907         }
908         break;
909      case 0x0150/4:
910         if( (mem_mask) & 0x0000ffff )
911         {
912            verboselog(machine, 2, "GBA IO Register Read: JOY_RECV_LSW (%08x) = %04x\n", 0x04000000 + ( offset << 2 ), gba_state->JOY_RECV & 0x0000ffff );
913            retval |= gba_state->JOY_RECV & 0x0000ffff;
914         }
915         if( (mem_mask) & 0xffff0000 )
916         {
917            verboselog(machine, 2, "GBA IO Register Read: JOY_RECV_MSW (%08x) = %04x\n", 0x04000000 + ( offset << 2 ) + 2, ( gba_state->JOY_RECV & 0xffff0000 ) >> 16 );
918            retval |= gba_state->JOY_RECV & 0xffff0000;
919         }
920         break;
921      case 0x0154/4:
922         if( (mem_mask) & 0x0000ffff )
923         {
924            verboselog(machine, 2, "GBA IO Register Read: JOY_TRANS_LSW (%08x) = %04x\n", 0x04000000 + ( offset << 2 ), gba_state->JOY_TRANS & 0x0000ffff );
925            retval |= gba_state->JOY_TRANS & 0x0000ffff;
926         }
927         if( (mem_mask) & 0xffff0000 )
928         {
929            verboselog(machine, 2, "GBA IO Register Read: JOY_TRANS_MSW (%08x) = %04x\n", 0x04000000 + ( offset << 2 ) + 2, ( gba_state->JOY_TRANS & 0xffff0000 ) >> 16 );
930            retval |= gba_state->JOY_TRANS & 0xffff0000;
931         }
932         break;
933      case 0x0158/4:
934         if( (mem_mask) & 0x0000ffff )
935         {
936            verboselog(machine, 2, "GBA IO Register Read: JOYSTAT (%08x) = %04x\n", 0x04000000 + ( offset << 2 ), gba_state->JOYSTAT );
937            retval |= gba_state->JOYSTAT;
938         }
939         if( (mem_mask) & 0xffff0000 )
940         {
941            verboselog(machine, 2, "GBA IO Register Read: UNKNOWN (%08x) = %04x\n", 0x04000000 + ( offset << 2 ) + 2, 0 );
942         }
943         break;
944      case 0x0200/4:
945         if( (mem_mask) & 0x0000ffff )
946         {
947//            printf("Read: IE (%08x) = %04x\n", 0x04000000 + ( offset << 2 ), gba_state->IE );
948         }
949         if( (mem_mask) & 0xffff0000 )
950         {
951            verboselog(machine, 2, "GBA IO Register Read: IF (%08x) = %04x\n", 0x04000000 + ( offset << 2 ) + 2, gba_state->IF );
952         }
953
954         retval = gba_state->IE | (gba_state->IF<<16);
955         break;
956      case 0x0204/4:
957         if( (mem_mask) & 0x0000ffff )
958         {
959            verboselog(machine, 2, "GBA IO Register Read: WAITCNT (%08x) = %04x\n", 0x04000000 + ( offset << 2 ), gba_state->WAITCNT );
960            retval |= gba_state->WAITCNT;
961         }
962         if( (mem_mask) & 0xffff0000 )
963         {
964            verboselog(machine, 2, "GBA IO Register Read: UNKNOWN (%08x) = %04x\n", 0x04000000 + ( offset << 2 ) + 2, 0 );
965         }
966         break;
967      case 0x0208/4:
968         if( (mem_mask) & 0x0000ffff )
969         {
970            verboselog(machine, 2, "GBA IO Register Read: IME (%08x) = %04x\n", 0x04000000 + ( offset << 2 ), gba_state->IME );
971            retval |= gba_state->IME;
972         }
973         if( (mem_mask) & 0xffff0000 )
974         {
975            verboselog(machine, 2, "GBA IO Register Read: UNKNOWN (%08x) = %04x\n", 0x04000000 + ( offset << 2 ) + 2, 0 );
976         }
977         break;
978      case 0x0300/4:
979         retval = gba_state->HALTCNT << 8;
980         break;
981      default:
982//         verboselog(machine, 0, "Unknown GBA I/O register Read: %08x (%08x)\n", 0x04000000 + ( offset << 2 ), ~mem_mask );
983         break;
984   }
985   return retval;
986}
987
988static WRITE32_HANDLER( gba_io_w )
989{
990   running_machine *machine = space->machine;
991   const device_config *gb_device = devtag_get_device(space->machine, "custom");
992   gba_state *gba_state = machine->driver_data;
993
994   switch( offset )
995   {
996      case 0x0000/4:
997         if( (mem_mask) & 0x0000ffff )
998         {
999            verboselog(machine, 2, "GBA IO Register Write: DISPCNT (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), data & 0x0000ffff, ~mem_mask );
1000            gba_state->DISPCNT = ( gba_state->DISPCNT & ~mem_mask ) | ( data & mem_mask );
1001         }
1002         break;
1003         if( (mem_mask) & 0xffff0000 )
1004         {
1005            verboselog(machine, 2, "GBA IO Register Write: Green Swap (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), ( data & 0xffff0000 ) >> 16, ~mem_mask );
1006            gba_state->GRNSWAP = ( gba_state->GRNSWAP & ( ~mem_mask >> 16 ) ) | ( ( data & mem_mask ) >> 16 );
1007         }
1008         break;
1009      case 0x0004/4:
1010         if( (mem_mask) & 0x0000ffff )
1011         {
1012//            printf("DISPSTAT (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), data & 0x0000ffff, ~mem_mask );
1013            gba_state->DISPSTAT = ( gba_state->DISPSTAT & ~mem_mask ) | ( data & mem_mask ) ;
1014         }
1015         break;
1016      case 0x0008/4:
1017         if( (mem_mask) & 0x0000ffff )
1018         {
1019            verboselog(machine, 2, "GBA IO Register Write: BG0CNT (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), data & 0x0000ffff, ~mem_mask );
1020            gba_state->BG0CNT = ( gba_state->BG0CNT & ~mem_mask ) | ( data & mem_mask );
1021         }
1022         if( (mem_mask) & 0xffff0000 )
1023         {
1024            verboselog(machine, 2, "GBA IO Register Write: BG1CNT (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), ( data & 0xffff0000 ) >> 16, ~mem_mask );
1025                gba_state->BG1CNT = ( gba_state->BG1CNT & ( ~mem_mask >> 16 ) ) | ( ( data & mem_mask ) >> 16 );
1026         }
1027         break;
1028      case 0x000c/4:
1029         if( (mem_mask) & 0x0000ffff )
1030         {
1031            verboselog(machine, 2, "GBA IO Register Write: BG2CNT (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), data & 0x0000ffff, ~mem_mask );
1032            gba_state->BG2CNT = ( gba_state->BG2CNT & ~mem_mask ) | ( data & mem_mask );
1033         }
1034         if( (mem_mask) & 0xffff0000 )
1035         {
1036            verboselog(machine, 2, "GBA IO Register Write: BG3CNT (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), ( data & 0xffff0000 ) >> 16, ~mem_mask );
1037                gba_state->BG3CNT = ( gba_state->BG3CNT & ( ~mem_mask >> 16 ) ) | ( ( data & mem_mask ) >> 16 );
1038         }
1039         break;
1040      case 0x0010/4:
1041         if( (mem_mask) & 0x0000ffff )
1042         {
1043            verboselog(machine, 2, "GBA IO Register Write: BG0HOFS (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), data & 0x0000ffff, ~mem_mask );
1044            gba_state->BG0HOFS = ( gba_state->BG0HOFS & ~mem_mask ) | ( data & mem_mask );
1045         }
1046         if( (mem_mask) & 0xffff0000 )
1047         {
1048            verboselog(machine, 2, "GBA IO Register Write: BG0VOFS (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), ( data & 0xffff0000 ) >> 16, ~mem_mask );
1049                gba_state->BG0VOFS = ( gba_state->BG0VOFS & ( ~mem_mask >> 16 ) ) | ( ( data & mem_mask ) >> 16 );
1050         }
1051         break;
1052      case 0x0014/4:
1053         if( (mem_mask) & 0x0000ffff )
1054         {
1055            verboselog(machine, 2, "GBA IO Register Write: BG1HOFS (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), data & 0x0000ffff, ~mem_mask );
1056            gba_state->BG1HOFS = ( gba_state->BG1HOFS & ~mem_mask ) | ( data & mem_mask );
1057         }
1058         if( (mem_mask) & 0xffff0000 )
1059         {
1060            verboselog(machine, 2, "GBA IO Register Write: BG1VOFS (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), ( data & 0xffff0000 ) >> 16, ~mem_mask );
1061                gba_state->BG1VOFS = ( gba_state->BG1VOFS & ( ~mem_mask >> 16 ) ) | ( ( data & mem_mask ) >> 16 );
1062         }
1063         break;
1064      case 0x0018/4:
1065         if( (mem_mask) & 0x0000ffff )
1066         {
1067            verboselog(machine, 2, "GBA IO Register Write: BG2HOFS (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), data & 0x0000ffff, ~mem_mask );
1068            gba_state->BG2HOFS = ( gba_state->BG2HOFS & ~mem_mask ) | ( data & mem_mask );
1069         }
1070         if( (mem_mask) & 0xffff0000 )
1071         {
1072            verboselog(machine, 2, "GBA IO Register Write: BG2VOFS (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), ( data & 0xffff0000 ) >> 16, ~mem_mask );
1073                gba_state->BG2VOFS = ( gba_state->BG2VOFS & ( ~mem_mask >> 16 ) ) | ( ( data & mem_mask ) >> 16 );
1074         }
1075         break;
1076      case 0x001c/4:
1077         if( (mem_mask) & 0x0000ffff )
1078         {
1079            verboselog(machine, 2, "GBA IO Register Write: BG3HOFS (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), data & 0x0000ffff, ~mem_mask );
1080            gba_state->BG3HOFS = ( gba_state->BG3HOFS & ~mem_mask ) | ( data & mem_mask );
1081         }
1082         if( (mem_mask) & 0xffff0000 )
1083         {
1084            verboselog(machine, 2, "GBA IO Register Write: BG3VOFS (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), ( data & 0xffff0000 ) >> 16, ~mem_mask );
1085                gba_state->BG3VOFS = ( gba_state->BG3VOFS & ( ~mem_mask >> 16 ) ) | ( ( data & mem_mask ) >> 16 );
1086         }
1087         break;
1088      case 0x0020/4:
1089         if( (mem_mask) & 0x0000ffff )
1090         {
1091            verboselog(machine, 2, "GBA IO Register Write: BG2PA (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), data & 0x0000ffff, ~mem_mask );
1092            gba_state->BG2PA = ( gba_state->BG2PA & ~mem_mask ) | ( data & mem_mask );
1093         }
1094         if( (mem_mask) & 0xffff0000 )
1095         {
1096            verboselog(machine, 2, "GBA IO Register Write: BG2PB (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), ( data & 0xffff0000 ) >> 16, ~mem_mask );
1097                gba_state->BG2PB = ( gba_state->BG2PB & ( ~mem_mask >> 16 ) ) | ( ( data & mem_mask ) >> 16 );
1098         }
1099         break;
1100      case 0x0024/4:
1101         if( (mem_mask) & 0x0000ffff )
1102         {
1103            verboselog(machine, 2, "GBA IO Register Write: BG2PC (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), data & 0x0000ffff, ~mem_mask );
1104            gba_state->BG2PC = ( gba_state->BG2PC & ~mem_mask ) | ( data & mem_mask );
1105         }
1106         if( (mem_mask) & 0xffff0000 )
1107         {
1108            verboselog(machine, 2, "GBA IO Register Write: BG2PD (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), ( data & 0xffff0000 ) >> 16, ~mem_mask );
1109                gba_state->BG2PD = ( gba_state->BG2PD & ( ~mem_mask >> 16 ) ) | ( ( data & mem_mask ) >> 16 );
1110         }
1111         break;
1112      case 0x0028/4:
1113         if( (mem_mask) & 0x0000ffff )
1114         {
1115            verboselog(machine, 2, "GBA IO Register Write: BG2X_LSW (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), data & 0x0000ffff, ~mem_mask );
1116            gba_state->BG2X = ( gba_state->BG2X & ~mem_mask ) | ( data & mem_mask );
1117         }
1118         if( (mem_mask) & 0xffff0000 )
1119         {
1120            verboselog(machine, 2, "GBA IO Register Write: BG2X_MSW (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), ( data & 0xffff0000 ) >> 16, ~mem_mask );
1121                      gba_state->BG2X = ( gba_state->BG2X & ( ~mem_mask >> 16 ) ) | ( ( data & mem_mask ) >> 16 );
1122         }
1123         break;
1124      case 0x002c/4:
1125         if( (mem_mask) & 0x0000ffff )
1126         {
1127            verboselog(machine, 2, "GBA IO Register Write: BG2Y_LSW (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), data & 0x0000ffff, ~mem_mask );
1128            gba_state->BG2Y = ( gba_state->BG2Y & ~mem_mask ) | ( data & mem_mask );
1129         }
1130         if( (mem_mask) & 0xffff0000 )
1131         {
1132            verboselog(machine, 2, "GBA IO Register Write: BG2Y_MSW (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), ( data & 0xffff0000 ) >> 16, ~mem_mask );
1133                      gba_state->BG2Y = ( gba_state->BG2Y & ( ~mem_mask >> 16 ) ) | ( ( data & mem_mask ) >> 16 );
1134         }
1135         break;
1136      case 0x0030/4:
1137         if( (mem_mask) & 0x0000ffff )
1138         {
1139            verboselog(machine, 2, "GBA IO Register Write: BG3PA (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), data & 0x0000ffff, ~mem_mask );
1140            gba_state->BG3PA = ( gba_state->BG3PA & ~mem_mask ) | ( data & mem_mask );
1141         }
1142         if( (mem_mask) & 0xffff0000 )
1143         {
1144            verboselog(machine, 2, "GBA IO Register Write: BG3PB (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), ( data & 0xffff0000 ) >> 16, ~mem_mask );
1145                gba_state->BG3PB = ( gba_state->BG3PB & ( ~mem_mask >> 16 ) ) | ( ( data & mem_mask ) >> 16 );
1146         }
1147         break;
1148      case 0x0034/4:
1149         if( (mem_mask) & 0x0000ffff )
1150         {
1151            verboselog(machine, 2, "GBA IO Register Write: BG3PC (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), data & 0x0000ffff, ~mem_mask );
1152            gba_state->BG3PC = ( gba_state->BG3PC & ~mem_mask ) | ( data & mem_mask );
1153         }
1154         if( (mem_mask) & 0xffff0000 )
1155         {
1156            verboselog(machine, 2, "GBA IO Register Write: BG3PD (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), ( data & 0xffff0000 ) >> 16, ~mem_mask );
1157                gba_state->BG3PD = ( gba_state->BG3PD & ( ~mem_mask >> 16 ) ) | ( ( data & mem_mask ) >> 16 );
1158         }
1159         break;
1160      case 0x0038/4:
1161         if( (mem_mask) & 0x0000ffff )
1162         {
1163            verboselog(machine, 2, "GBA IO Register Write: BG3X_LSW (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), data & 0x0000ffff, ~mem_mask );
1164            gba_state->BG3X = ( gba_state->BG3X & ~mem_mask ) | ( data & mem_mask );
1165         }
1166         if( (mem_mask) & 0xffff0000 )
1167         {
1168            verboselog(machine, 2, "GBA IO Register Write: BG3X_MSW (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), ( data & 0xffff0000 ) >> 16, ~mem_mask );
1169                gba_state->BG3X = ( gba_state->BG3X & ( ~mem_mask >> 16 ) ) | ( ( data & mem_mask ) >> 16 );
1170         }
1171         break;
1172      case 0x003c/4:
1173         if( (mem_mask) & 0x0000ffff )
1174         {
1175            verboselog(machine, 2, "GBA IO Register Write: BG3Y_LSW (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), data & 0x0000ffff, ~mem_mask );
1176            gba_state->BG3Y = ( gba_state->BG3Y & ~mem_mask ) | ( data & mem_mask );
1177         }
1178         if( (mem_mask) & 0xffff0000 )
1179         {
1180            verboselog(machine, 2, "GBA IO Register Write: BG3Y_MSW (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), ( data & 0xffff0000 ) >> 16, ~mem_mask );
1181                gba_state->BG3Y = ( gba_state->BG3Y & ( ~mem_mask >> 16 ) ) | ( ( data & mem_mask ) >> 16 );
1182         }
1183         break;
1184      case 0x0040/4:
1185         if( (mem_mask) & 0x0000ffff )
1186         {
1187            verboselog(machine, 2, "GBA IO Register Write: WIN0H (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), data & 0x0000ffff, ~mem_mask );
1188            gba_state->WIN0H = ( gba_state->WIN0H & ~mem_mask ) | ( data & mem_mask );
1189         }
1190         if( (mem_mask) & 0xffff0000 )
1191         {
1192            verboselog(machine, 2, "GBA IO Register Write: WIN1H (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), ( data & 0xffff0000 ) >> 16, ~mem_mask );
1193                gba_state->WIN1H = ( gba_state->WIN1H & ( ~mem_mask >> 16 ) ) | ( ( data & mem_mask ) >> 16 );
1194         }
1195         break;
1196      case 0x0044/4:
1197         if( (mem_mask) & 0x0000ffff )
1198         {
1199            verboselog(machine, 2, "GBA IO Register Write: WIN0V (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), data & 0x0000ffff, ~mem_mask );
1200            gba_state->WIN0V = ( gba_state->WIN0V & ~mem_mask ) | ( data & mem_mask );
1201         }
1202         if( (mem_mask) & 0xffff0000 )
1203         {
1204            verboselog(machine, 2, "GBA IO Register Write: WIN1V (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), ( data & 0xffff0000 ) >> 16, ~mem_mask );
1205                gba_state->WIN1V = ( gba_state->WIN1V & ( ~mem_mask >> 16 ) ) | ( ( data & mem_mask ) >> 16 );
1206         }
1207         break;
1208      case 0x0048/4:
1209         if( (mem_mask) & 0x0000ffff )
1210         {
1211            verboselog(machine, 2, "GBA IO Register Write: WININ (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), data & 0x0000ffff, ~mem_mask );
1212            gba_state->WININ = ( gba_state->WININ & ~mem_mask ) | ( data & mem_mask );
1213         }
1214         if( (mem_mask) & 0xffff0000 )
1215         {
1216            verboselog(machine, 2, "GBA IO Register Write: WINOUT (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), ( data & 0xffff0000 ) >> 16, ~mem_mask );
1217                gba_state->WINOUT = ( gba_state->WINOUT & ( ~mem_mask >> 16 ) ) | ( ( data & mem_mask ) >> 16 );
1218         }
1219         break;
1220      case 0x004c/4:
1221         if( (mem_mask) & 0x0000ffff )
1222         {
1223            verboselog(machine, 2, "GBA IO Register Write: MOSAIC (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), data & 0x0000ffff, ~mem_mask );
1224            gba_state->MOSAIC = ( gba_state->MOSAIC & ~mem_mask ) | ( data & mem_mask );
1225         }
1226         if( (mem_mask) & 0xffff0000 )
1227         {
1228            verboselog(machine, 2, "GBA IO Register Write: UNKNOWN (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), ( data & 0xffff0000 ) >> 16, ~mem_mask );
1229         }
1230         break;
1231      case 0x0050/4:
1232         if( (mem_mask) & 0x0000ffff )
1233         {
1234            verboselog(machine, 2, "GBA IO Register Write: BLDCNT (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), data & 0x0000ffff, ~mem_mask );
1235            gba_state->BLDCNT = ( gba_state->BLDCNT & ~mem_mask ) | ( data & mem_mask );
1236         }
1237         if( (mem_mask) & 0xffff0000 )
1238         {
1239            verboselog(machine, 2, "GBA IO Register Write: BLDALPHA (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), ( data & 0xffff0000 ) >> 16, ~mem_mask );
1240                gba_state->BLDALPHA = ( gba_state->BLDALPHA & ( ~mem_mask >> 16 ) ) | ( ( data & mem_mask ) >> 16 );
1241         }
1242         break;
1243      case 0x0054/4:
1244         if( (mem_mask) & 0x0000ffff )
1245         {
1246            verboselog(machine, 2, "GBA IO Register Write: BLDY (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), data & 0x0000ffff, ~mem_mask );
1247            gba_state->BLDY = ( gba_state->BLDY & ~mem_mask ) | ( data & mem_mask );
1248         }
1249         if( (mem_mask) & 0xffff0000 )
1250         {
1251            verboselog(machine, 2, "GBA IO Register Write: UNKNOWN (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), ( data & 0xffff0000 ) >> 16, ~mem_mask );
1252         }
1253         break;
1254      case 0x0058/4:
1255         if( (mem_mask) & 0x0000ffff )
1256         {
1257            verboselog(machine, 2, "GBA IO Register Write: UNKNOWN (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), data & 0x0000ffff, ~mem_mask );
1258         }
1259         if( (mem_mask) & 0xffff0000 )
1260         {
1261            verboselog(machine, 2, "GBA IO Register Write: UNKNOWN (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), ( data & 0xffff0000 ) >> 16, ~mem_mask );
1262         }
1263         break;
1264      case 0x005c/4:
1265         if( (mem_mask) & 0x0000ffff )
1266         {
1267            verboselog(machine, 2, "GBA IO Register Write: UNKNOWN (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), data & 0x0000ffff, ~mem_mask );
1268         }
1269         if( (mem_mask) & 0xffff0000 )
1270         {
1271            verboselog(machine, 2, "GBA IO Register Write: UNKNOWN (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), ( data & 0xffff0000 ) >> 16, ~mem_mask );
1272         }
1273         break;
1274      case 0x0060/4:
1275         if( (mem_mask) & 0x000000ff )   // SOUNDCNTL
1276         {
1277            gb_sound_w(gb_device, 0, data);
1278         }
1279         if( (mem_mask) & 0x00ff0000 )
1280         {
1281            gb_sound_w(gb_device, 1, data>>16);   // SOUND1CNT_H
1282         }
1283         if( (mem_mask) & 0xff000000 )
1284         {
1285            gb_sound_w(gb_device, 2, data>>24);
1286         }
1287         break;
1288      case 0x0064/4:
1289         if( (mem_mask) & 0x000000ff )   // SOUNDCNTL
1290         {
1291            gb_sound_w(gb_device, 3, data);
1292         }
1293         if( (mem_mask) & 0x0000ff00 )
1294         {
1295            gb_sound_w(gb_device, 4, data>>8);   // SOUND1CNT_H
1296         }
1297         break;
1298      case 0x0068/4:
1299         if( (mem_mask) & 0x000000ff )
1300         {
1301            gb_sound_w(gb_device, 6, data);
1302         }
1303         if( (mem_mask) & 0x0000ff00 )
1304         {
1305            gb_sound_w(gb_device, 7, data>>8);
1306         }
1307         break;
1308      case 0x006c/4:
1309         if( (mem_mask) & 0x000000ff )
1310         {
1311            gb_sound_w(gb_device, 8, data);
1312         }
1313         if( (mem_mask) & 0x0000ff00 )
1314         {
1315            gb_sound_w(gb_device, 9, data>>8);
1316         }
1317         break;
1318      case 0x0070/4:   //SND3CNTL and H
1319         if( (mem_mask) & 0x000000ff )   // SOUNDCNTL
1320         {
1321            gb_sound_w(gb_device, 0xa, data);
1322         }
1323         if( (mem_mask) & 0x00ff0000 )
1324         {
1325            gb_sound_w(gb_device, 0xb, data>>16);   // SOUND1CNT_H
1326         }
1327         if( (mem_mask) & 0xff000000 )
1328         {
1329            gb_sound_w(gb_device, 0xc, data>>24);
1330         }
1331         break;
1332      case 0x0074/4:
1333         if( (mem_mask) & 0x000000ff )
1334         {
1335            gb_sound_w(gb_device, 0xd, data);
1336         }
1337         if( (mem_mask) & 0x0000ff00 )
1338         {
1339            gb_sound_w(gb_device, 0xe, data>>8);
1340         }
1341         break;
1342      case 0x0078/4:
1343         if( (mem_mask) & 0x000000ff )
1344         {
1345            gb_sound_w(gb_device, 0x10, data);
1346         }
1347         if( (mem_mask) & 0x0000ff00 )
1348         {
1349            gb_sound_w(gb_device, 0x11, data>>8);
1350         }
1351         break;
1352      case 0x007c/4:
1353         if( (mem_mask) & 0x000000ff )
1354         {
1355            gb_sound_w(gb_device, 0x12, data);
1356         }
1357         if( (mem_mask) & 0x0000ff00 )
1358         {
1359            gb_sound_w(gb_device, 0x13, data>>8);
1360         }
1361         break;
1362      case 0x0080/4:
1363         if( (mem_mask) & 0x000000ff )
1364         {
1365            gb_sound_w(gb_device, 0x14, data);
1366         }
1367         if( (mem_mask) & 0x0000ff00 )
1368         {
1369            gb_sound_w(gb_device, 0x15, data>>8);
1370         }
1371
1372         if ((mem_mask) & 0xffff0000)
1373         {
1374            data >>= 16;
1375            gba_state->SOUNDCNT_H = data;
1376
1377            // DAC A reset?
1378            if (data & 0x0800)
1379            {
1380               const device_config *gb_a_l = devtag_get_device(machine, "direct_a_left");
1381               const device_config *gb_a_r = devtag_get_device(machine, "direct_a_right");
1382                           
1383               gba_state->fifo_a_ptr = 17;
1384               gba_state->fifo_a_in = 17;
1385               dac_signed_data_w(gb_a_l, 0x80);
1386               dac_signed_data_w(gb_a_r, 0x80);
1387            }
1388
1389            // DAC B reset?
1390            if (data & 0x8000)
1391            {
1392               const device_config *gb_b_l = devtag_get_device(machine, "direct_b_left");
1393               const device_config *gb_b_r = devtag_get_device(machine, "direct_b_right");
1394                           
1395               gba_state->fifo_b_ptr = 17;
1396               gba_state->fifo_b_in = 17;
1397               dac_signed_data_w(gb_b_l, 0x80);
1398               dac_signed_data_w(gb_b_r, 0x80);
1399            }
1400         }
1401         break;
1402      case 0x0084/4:
1403         if( (mem_mask) & 0x000000ff )
1404         {
1405            const device_config *gb_a_l = devtag_get_device(machine, "direct_a_left");
1406            const device_config *gb_a_r = devtag_get_device(machine, "direct_a_right");
1407            const device_config *gb_b_l = devtag_get_device(machine, "direct_b_left");
1408            const device_config *gb_b_r = devtag_get_device(machine, "direct_b_right");
1409            const device_config *gb_device = devtag_get_device(space->machine, "custom");
1410
1411            gb_sound_w(gb_device, 0x16, data);
1412            if ((data & 0x80) && !(gba_state->SOUNDCNT_X & 0x80))
1413            {
1414               gba_state->fifo_a_ptr = gba_state->fifo_a_in = 17;
1415               gba_state->fifo_b_ptr = gba_state->fifo_b_in = 17;
1416               dac_signed_data_w(gb_a_l, 0x80);
1417               dac_signed_data_w(gb_a_r, 0x80);
1418               dac_signed_data_w(gb_b_l, 0x80);
1419               dac_signed_data_w(gb_b_r, 0x80);
1420            }
1421            gba_state->SOUNDCNT_X = data;
1422         }
1423         break;
1424      case 0x0088/4:
1425         if( (mem_mask) & 0x0000ffff )
1426         {
1427            verboselog(machine, 2, "GBA IO Register Write: SOUNDBIAS (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), data & 0x0000ffff, ~mem_mask );
1428            gba_state->SOUNDBIAS = ( gba_state->SOUNDBIAS & ~mem_mask ) | ( data & mem_mask );
1429         }
1430         if( (mem_mask) & 0xffff0000 )
1431         {
1432            verboselog(machine, 2, "GBA IO Register Write: UNKNOWN (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), ( data & 0xffff0000 ) >> 16, ~mem_mask );
1433         }
1434         break;
1435      case 0x0090/4:
1436         if( (mem_mask) & 0x000000ff )
1437         {
1438            gb_wave_w(gb_device, 0, data);
1439         }
1440         if( (mem_mask) & 0x0000ff00 )
1441         {
1442            gb_wave_w(gb_device, 1, data>>8);
1443         }
1444         if( (mem_mask) & 0x00ff0000 )
1445         {
1446            gb_wave_w(gb_device, 2, data>>16);
1447         }
1448         if( (mem_mask) & 0xff000000 )
1449         {
1450            gb_wave_w(gb_device, 3, data>>24);
1451         }
1452         break;
1453      case 0x0094/4:
1454         if( (mem_mask) & 0x000000ff )
1455         {
1456            gb_wave_w(gb_device, 4, data);
1457         }
1458         if( (mem_mask) & 0x0000ff00 )
1459         {
1460            gb_wave_w(gb_device, 5, data>>8);
1461         }
1462         if( (mem_mask) & 0x00ff0000 )
1463         {
1464            gb_wave_w(gb_device, 6, data>>16);
1465         }
1466         if( (mem_mask) & 0xff000000 )
1467         {
1468            gb_wave_w(gb_device, 7, data>>24);
1469         }
1470         break;
1471      case 0x0098/4:
1472         if( (mem_mask) & 0x000000ff )
1473         {
1474            gb_wave_w(gb_device, 8, data);
1475         }
1476         if( (mem_mask) & 0x0000ff00 )
1477         {
1478            gb_wave_w(gb_device, 9, data>>8);
1479         }
1480         if( (mem_mask) & 0x00ff0000 )
1481         {
1482            gb_wave_w(gb_device, 0xa, data>>16);
1483         }
1484         if( (mem_mask) & 0xff000000 )
1485         {
1486            gb_wave_w(gb_device, 0xb, data>>24);
1487         }
1488         break;
1489      case 0x009c/4:
1490         if( (mem_mask) & 0x000000ff )
1491         {
1492            gb_wave_w(gb_device, 0xc, data);
1493         }
1494         if( (mem_mask) & 0x0000ff00 )
1495         {
1496            gb_wave_w(gb_device, 0xd, data>>8);
1497         }
1498         if( (mem_mask) & 0x00ff0000 )
1499         {
1500            gb_wave_w(gb_device, 0xe, data>>16);
1501         }
1502         if( (mem_mask) & 0xff000000 )
1503         {
1504            gb_wave_w(gb_device, 0xf, data>>24);
1505         }
1506         break;
1507      case 0x00a0/4:
1508         gba_state->fifo_a_in %= 17;
1509         gba_state->fifo_a[gba_state->fifo_a_in++] = (data)&0xff;
1510         gba_state->fifo_a_in %= 17;
1511         gba_state->fifo_a[gba_state->fifo_a_in++] = (data>>8)&0xff;
1512         gba_state->fifo_a_in %= 17;
1513         gba_state->fifo_a[gba_state->fifo_a_in++] = (data>>16)&0xff;
1514         gba_state->fifo_a_in %= 17;
1515         gba_state->fifo_a[gba_state->fifo_a_in++] = (data>>24)&0xff;
1516         break;
1517      case 0x00a4/4:
1518         gba_state->fifo_b_in %= 17;
1519         gba_state->fifo_b[gba_state->fifo_b_in++] = (data)&0xff;
1520         gba_state->fifo_b_in %= 17;
1521         gba_state->fifo_b[gba_state->fifo_b_in++] = (data>>8)&0xff;
1522         gba_state->fifo_b_in %= 17;
1523         gba_state->fifo_b[gba_state->fifo_b_in++] = (data>>16)&0xff;
1524         gba_state->fifo_b_in %= 17;
1525         gba_state->fifo_b[gba_state->fifo_b_in++] = (data>>24)&0xff;
1526         break;
1527      case 0x00b0/4:
1528      case 0x00b4/4:
1529      case 0x00b8/4:
1530
1531      case 0x00bc/4:
1532      case 0x00c0/4:
1533      case 0x00c4/4:
1534
1535      case 0x00c8/4:
1536      case 0x00cc/4:
1537      case 0x00d0/4:
1538
1539      case 0x00d4/4:
1540      case 0x00d8/4:
1541      case 0x00dc/4:
1542         {
1543            int ch;
1544
1545            offset -= (0xb0/4);
1546
1547            ch = offset / 3;
1548
1549//            printf("%08x: DMA(%d): %x to reg %d (mask %08x)\n", activecpu_get_pc(), ch, data, offset%3, ~mem_mask);
1550
1551            if (((offset % 3) == 2) && ((~mem_mask & 0xffff0000) == 0))
1552            {
1553               int ctrl = data>>16;
1554
1555               // retrigger/restart on a rising edge.
1556               // also reload internal regs
1557               // (note: Metroid Fusion fails if we enforce the "rising edge" requirement...)
1558               if (ctrl & 0x8000) //&& !(gba_state->dma_regs[offset] & 0x80000000))
1559               {
1560                  gba_state->dma_src[ch] = gba_state->dma_regs[(ch*3)+0];
1561                  gba_state->dma_dst[ch] = gba_state->dma_regs[(ch*3)+1];
1562                  gba_state->dma_srcadd[ch] = (ctrl>>7)&3;
1563                  gba_state->dma_dstadd[ch] = (ctrl>>5)&3;
1564
1565                        COMBINE_DATA(&gba_state->dma_regs[offset]);
1566                        gba_state->dma_cnt[ch] = gba_state->dma_regs[(ch*3)+2]&0xffff;
1567
1568                        // immediate start
1569                  if ((ctrl & 0x3000) == 0)
1570                  {
1571                     dma_exec(machine, ch);
1572                     return;
1573                  }
1574                }
1575            }
1576
1577            COMBINE_DATA(&gba_state->dma_regs[offset]);
1578         }
1579         break;
1580      case 0x0100/4:
1581      case 0x0104/4:
1582      case 0x0108/4:
1583      case 0x010c/4:
1584         {
1585            double rate, clocksel;
1586
1587            offset -= (0x100/4);
1588
1589            COMBINE_DATA(&gba_state->timer_regs[offset]);
1590
1591//            printf("%x to timer %d (mask %x PC %x)\n", data, offset, ~mem_mask, cpu_get_pc(space->cpu));
1592
1593            if (ACCESSING_BITS_0_15)
1594            {
1595                    gba_state->timer_reload[offset] = ( gba_state->timer_reload[offset] & ~mem_mask ) | ( ( data & 0x0000ffff ) & mem_mask );
1596            }
1597
1598            // enabling this timer?
1599            if ((ACCESSING_BITS_16_31) && (data & 0x800000))
1600            {
1601               double final;
1602
1603               rate = 0x10000 - (gba_state->timer_regs[offset] & 0xffff);
1604
1605               clocksel = timer_clks[(gba_state->timer_regs[offset] >> 16) & 3];
1606
1607               final = clocksel / rate;
1608
1609//               printf("Enabling timer %d @ %f Hz\n", offset, final);
1610
1611               // enable the timer
1612               if( !(data & 0x40000) ) // if we're not in Count-Up mode
1613               {
1614                  timer_adjust_periodic(gba_state->tmr_timer[offset], ATTOTIME_IN_HZ(final), offset, ATTOTIME_IN_HZ(final));
1615               }
1616            }
1617         }
1618         break;
1619      case 0x0120/4:
1620         if( (mem_mask) & 0x0000ffff )
1621         {
1622            verboselog(machine, 2, "GBA IO Register Write: SIOMULTI0 (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), data & mem_mask, ~mem_mask );
1623            gba_state->SIOMULTI0 = ( gba_state->SIOMULTI0 & ~mem_mask ) | ( data & mem_mask );
1624         }
1625         if( (mem_mask) & 0xffff0000 )
1626         {
1627            verboselog(machine, 2, "GBA IO Register Write: SIOMULTI1 (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), ( data & mem_mask ) >> 16, ~mem_mask );
1628                gba_state->SIOMULTI1 = ( gba_state->SIOMULTI1 & ( ~mem_mask >> 16 ) ) | ( ( data & mem_mask ) >> 16 );
1629         }
1630         break;
1631      case 0x0124/4:
1632         if( (mem_mask) & 0x0000ffff )
1633         {
1634            verboselog(machine, 2, "GBA IO Register Write: SIOMULTI2 (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), data & mem_mask, ~mem_mask );
1635            gba_state->SIOMULTI2 = ( gba_state->SIOMULTI2 & ~mem_mask ) | ( data & mem_mask );
1636         }
1637         if( (mem_mask) & 0xffff0000 )
1638         {
1639            verboselog(machine, 2, "GBA IO Register Write: SIOMULTI3 (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), ( data & mem_mask ) >> 16, ~mem_mask );
1640                gba_state->SIOMULTI3 = ( gba_state->SIOMULTI3 & ( ~mem_mask >> 16 ) ) | ( ( data & mem_mask ) >> 16 );
1641         }
1642         break;
1643      case 0x0128/4:
1644         if( (mem_mask) & 0x0000ffff )
1645         {
1646            verboselog(machine, 2, "GBA IO Register Write: SIOCNT (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), data & mem_mask, ~mem_mask );
1647            gba_state->SIOCNT = ( gba_state->SIOCNT & ~mem_mask ) | ( data & mem_mask );
1648         }
1649         if( (mem_mask) & 0xffff0000 )
1650         {
1651            verboselog(machine, 2, "GBA IO Register Write: SIODATA8 (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), ( data & mem_mask ) >> 16, ~mem_mask );
1652                gba_state->SIODATA8 = ( gba_state->SIODATA8 & ( ~mem_mask >> 16 ) ) | ( ( data & mem_mask ) >> 16 );
1653         }
1654         break;
1655      case 0x0130/4:
1656         if( (mem_mask) & 0xffff0000 )
1657         {
1658//            printf("KEYCNT = %04x\n", data>>16);
1659            verboselog(machine, 2, "GBA IO Register Write: KEYCNT (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), ( data & mem_mask ) >> 16, ~mem_mask );
1660                gba_state->KEYCNT = ( gba_state->KEYCNT & ( ~mem_mask >> 16 ) ) | ( ( data & mem_mask ) >> 16 );
1661         }
1662         break;
1663      case 0x0134/4:
1664         if( (mem_mask) & 0x0000ffff )
1665         {
1666            verboselog(machine, 2, "GBA IO Register Write: RCNT (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), data & mem_mask, ~mem_mask );
1667            gba_state->RCNT = ( gba_state->RCNT & ~mem_mask ) | ( data & mem_mask );
1668         }
1669         if( (mem_mask) & 0xffff0000 )
1670         {
1671            verboselog(machine, 2, "GBA IO Register Write: IR (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), ( data & mem_mask ) >> 16, ~mem_mask );
1672                gba_state->IR = ( gba_state->IR & ( ~mem_mask >> 16 ) ) | ( ( data & mem_mask ) >> 16 );
1673         }
1674         break;
1675      case 0x0140/4:
1676         if( (mem_mask) & 0x0000ffff )
1677         {
1678            verboselog(machine, 2, "GBA IO Register Write: JOYCNT (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), data & mem_mask, ~mem_mask );
1679            gba_state->JOYCNT = ( gba_state->JOYCNT & ~mem_mask ) | ( data & mem_mask );
1680         }
1681         if( (mem_mask) & 0xffff0000 )
1682         {
1683            verboselog(machine, 2, "GBA IO Register Write: UNKNOWN (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), ( data & mem_mask ) >> 16, ~mem_mask );
1684         }
1685         break;
1686      case 0x0150/4:
1687         if( (mem_mask) & 0x0000ffff )
1688         {
1689            verboselog(machine, 2, "GBA IO Register Write: JOY_RECV_LSW (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), data & mem_mask, ~mem_mask );
1690            gba_state->JOY_RECV = ( gba_state->JOY_RECV & ~mem_mask ) | ( data & mem_mask );
1691         }
1692         if( (mem_mask) & 0xffff0000 )
1693         {
1694            verboselog(machine, 2, "GBA IO Register Write: JOY_RECV_MSW (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), ( data & mem_mask ) >> 16, ~mem_mask );
1695                gba_state->JOY_RECV = ( gba_state->JOY_RECV & ( ~mem_mask >> 16 ) ) | ( ( data & mem_mask ) >> 16 );
1696         }
1697         break;
1698      case 0x0154/4:
1699         if( (mem_mask) & 0x0000ffff )
1700         {
1701            verboselog(machine, 2, "GBA IO Register Write: JOY_TRANS_LSW (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), data & mem_mask, ~mem_mask );
1702            gba_state->JOY_TRANS = ( gba_state->JOY_TRANS & ~mem_mask ) | ( data & mem_mask );
1703         }
1704         if( (mem_mask) & 0xffff0000 )
1705         {
1706            verboselog(machine, 2, "GBA IO Register Write: JOY_TRANS_MSW (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), ( data & mem_mask ) >> 16, ~mem_mask );
1707                gba_state->JOY_TRANS = ( gba_state->JOY_TRANS & ( ~mem_mask >> 16 ) ) | ( ( data & mem_mask ) >> 16 );
1708         }
1709         break;
1710      case 0x0158/4:
1711         if( (mem_mask) & 0x0000ffff )
1712         {
1713            verboselog(machine, 2, "GBA IO Register Write: JOYSTAT (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), data & mem_mask, ~mem_mask );
1714            gba_state->JOYSTAT = ( gba_state->JOYSTAT & ~mem_mask ) | ( data & mem_mask );
1715         }
1716         if( (mem_mask) & 0xffff0000 )
1717         {
1718            verboselog(machine, 2, "GBA IO Register Write: UNKNOWN (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), ( data & mem_mask ) >> 16, ~mem_mask );
1719         }
1720         break;
1721      case 0x0200/4:
1722         if( (mem_mask) & 0x0000ffff )
1723         {
1724//            printf("IE (%08x) = %04x raw %x (%08x) (scan %d PC %x)\n", 0x04000000 + ( offset << 2 ), data & mem_mask, data, ~mem_mask, video_screen_get_vpos(machine->primary_screen), cpu_get_pc(space->cpu));
1725            gba_state->IE = ( gba_state->IE & ~mem_mask ) | ( data & mem_mask );
1726             /*
1727            if (gba_state->IE & gba_state->IF)
1728            {
1729               gba_request_irq(machine, gba_state->IF);
1730            }
1731            */
1732         }
1733         if( (mem_mask) & 0xffff0000 )
1734         {
1735            verboselog(machine, 2, "GBA IO Register Write: IF (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ) + 2, ( data & mem_mask ) >> 16, ~mem_mask );
1736            gba_state->IF &= ~( ( data & mem_mask ) >> 16 );
1737
1738            // if we still have interrupts, yank the IRQ line again
1739            if (gba_state->IF)
1740            {
1741               timer_adjust_oneshot(gba_state->irq_timer, cpu_clocks_to_attotime(machine->cpu[0], 120), 0);
1742            }
1743         }
1744         break;
1745      case 0x0204/4:
1746         if( (mem_mask) & 0x0000ffff )
1747         {
1748            verboselog(machine, 2, "GBA IO Register Write: WAITCNT (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), data & mem_mask, ~mem_mask );
1749            gba_state->WAITCNT = ( gba_state->WAITCNT & ~mem_mask ) | ( data & mem_mask );
1750         }
1751         if( (mem_mask) & 0xffff0000 )
1752         {
1753            verboselog(machine, 2, "GBA IO Register Write: UNKNOWN (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), ( data & mem_mask ) >> 16, ~mem_mask );
1754         }
1755         break;
1756      case 0x0208/4:
1757         if( (mem_mask) & 0x0000ffff )
1758         {
1759            verboselog(machine, 3, "GBA IO Register Write: IME (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), data & mem_mask, ~mem_mask );
1760            gba_state->IME = ( gba_state->IME & ~mem_mask ) | ( data & mem_mask );
1761            if (gba_state->IF)
1762            {
1763               timer_adjust_oneshot(gba_state->irq_timer, attotime_zero, 0);
1764            }
1765         }
1766         if( (mem_mask) & 0xffff0000 )
1767         {
1768            verboselog(machine, 3, "GBA IO Register Write: UNKNOWN (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), ( data & mem_mask ) >> 16, ~mem_mask );
1769         }
1770         break;
1771      case 0x0300/4:
1772         if( (mem_mask) & 0x0000ffff )
1773         {
1774            if( (mem_mask) & 0x000000ff )
1775            {
1776               verboselog(machine, 2, "GBA IO Register Write: POSTFLG (%08x) = %02x (%08x)\n", 0x04000000 + ( offset << 2 ), data & 0x000000ff, ~mem_mask );
1777               gba_state->POSTFLG = data & 0x000000ff;
1778            }
1779            else
1780            {
1781               gba_state->HALTCNT = data & 0x000000ff;
1782
1783               // either way, wait for an IRQ
1784               cpu_spinuntil_int(machine->cpu[0]);
1785            }
1786         }
1787         if( (mem_mask) & 0xffff0000 )
1788         {
1789            verboselog(machine, 2, "GBA IO Register Write: UNKNOWN (%08x) = %04x (%08x)\n", 0x04000000 + ( offset << 2 ), ( data & 0xffff0000 ) >> 16, ~mem_mask );
1790         }
1791         break;
1792      default:
1793//         verboselog(machine, 0, "Unknown GBA I/O register write: %08x = %08x (%08x)\n", 0x04000000 + ( offset << 2 ), data, ~mem_mask );
1794         break;
1795   }
1796}
1797
1798static ADDRESS_MAP_START( gbadvance_map, ADDRESS_SPACE_PROGRAM, 32 )
1799   AM_RANGE(0x00000000, 0x00003fff) AM_ROMBANK(1)
1800   AM_RANGE(0x02000000, 0x0203ffff) AM_RAM AM_MIRROR(0xfc0000)
1801   AM_RANGE(0x03000000, 0x03007fff) AM_RAM AM_MIRROR(0xff8000)
1802   AM_RANGE(0x04000000, 0x040003ff) AM_READWRITE( gba_io_r, gba_io_w )
1803   AM_RANGE(0x05000000, 0x050003ff) AM_RAM AM_BASE_MEMBER(gba_state, gba_pram)   // Palette RAM
1804   AM_RANGE(0x06000000, 0x06017fff) AM_RAM AM_BASE_MEMBER(gba_state, gba_vram)   // VRAM
1805   AM_RANGE(0x07000000, 0x070003ff) AM_RAM AM_BASE_MEMBER(gba_state, gba_oam)   // OAM
1806   AM_RANGE(0x08000000, 0x09ffffff) AM_ROM AM_REGION("cartridge", 0)   // cartridge ROM (mirror 0)
1807   AM_RANGE(0x0a000000, 0x0bffffff) AM_ROM AM_REGION("cartridge", 0)   // cartridge ROM (mirror 1)
1808   AM_RANGE(0x0c000000, 0x0cffffff) AM_ROM AM_REGION("cartridge", 0)   // final mirror
1809ADDRESS_MAP_END
1810
1811INPUT_PORTS_START( gbadv )
1812   PORT_START("IN0")
1813   PORT_BIT( 0xfc00, IP_ACTIVE_HIGH, IPT_BUTTON6) PORT_UNUSED
1814   PORT_BIT( 0x0200, IP_ACTIVE_LOW, IPT_BUTTON5 ) PORT_NAME("P1 R") PORT_PLAYER(1)   // R
1815   PORT_BIT( 0x0100, IP_ACTIVE_LOW, IPT_BUTTON4 ) PORT_NAME("P1 L") PORT_PLAYER(1)   // L
1816   PORT_BIT( 0x0080, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN ) PORT_PLAYER(1)
1817   PORT_BIT( 0x0040, IP_ACTIVE_LOW, IPT_JOYSTICK_UP ) PORT_PLAYER(1)
1818   PORT_BIT( 0x0020, IP_ACTIVE_LOW, IPT_JOYSTICK_LEFT ) PORT_PLAYER(1)
1819   PORT_BIT( 0x0010, IP_ACTIVE_LOW, IPT_JOYSTICK_RIGHT ) PORT_PLAYER(1)
1820   PORT_BIT( 0x0008, IP_ACTIVE_LOW, IPT_START ) PORT_PLAYER(1)   // START
1821   PORT_BIT( 0x0004, IP_ACTIVE_LOW, IPT_BUTTON3 ) PORT_NAME("SELECT") PORT_PLAYER(1)   // SELECT
1822   PORT_BIT( 0x0002, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_NAME("B") PORT_PLAYER(1)   // B
1823   PORT_BIT( 0x0001, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_NAME("A") PORT_PLAYER(1)   // A
1824INPUT_PORTS_END
1825
1826TIMER_CALLBACK( perform_hbl )
1827{
1828   int ch, ctrl;
1829   gba_state *gba_state = machine->driver_data;
1830
1831   gba_draw_scanline(machine, video_screen_get_vpos(machine->primary_screen));   // we are now in hblank
1832   gba_state->DISPSTAT |= DISPSTAT_HBL;
1833   if ((gba_state->DISPSTAT & DISPSTAT_HBL_IRQ_EN ) != 0)
1834   {
1835      gba_request_irq(machine, INT_HBL);
1836   }
1837
1838   for (ch = 0; ch < 4; ch++)
1839   {
1840      ctrl = gba_state->dma_regs[(ch*3)+2]>>16;
1841
1842      // HBL-triggered DMA?
1843      if ((ctrl & 0x8000) && ((ctrl & 0x3000) == 0x2000))
1844      {
1845         dma_exec(machine, ch);
1846      }
1847   }
1848
1849   timer_adjust_oneshot(gba_state->hbl_timer, attotime_never, 0);
1850}
1851
1852TIMER_CALLBACK( perform_scan )
1853{
1854   int scanline;
1855   gba_state *gba_state = machine->driver_data;
1856
1857   // clear hblank and raster IRQ flags
1858   gba_state->DISPSTAT &= ~(DISPSTAT_HBL|DISPSTAT_VCNT);
1859
1860   scanline = video_screen_get_vpos(machine->primary_screen);
1861
1862   // handle VCNT match interrupt/flag
1863   if (scanline == ((gba_state->DISPSTAT >> 8) & 0xff))
1864   {
1865      gba_state->DISPSTAT |= DISPSTAT_VCNT;
1866      if (gba_state->DISPSTAT & DISPSTAT_VCNT_IRQ_EN)
1867      {
1868         gba_request_irq(machine, INT_VCNT);
1869      }
1870   }
1871
1872   // scanline zero means not in VBL anymore
1873   if (scanline == 0)
1874   {
1875      gba_state->DISPSTAT &= ~DISPSTAT_VBL;
1876   }
1877   else if (scanline == 160)   // scanline 160 means entering VBL
1878   {
1879      int ch, ctrl;
1880
1881      gba_state->DISPSTAT |= DISPSTAT_VBL;
1882//      printf("DISPSTAT %x %x\n", gba_state->DISPSTAT, gba_state->DISPSTAT & DISPSTAT_VBL_IRQ_EN);
1883      if ((gba_state->DISPSTAT & DISPSTAT_VBL_IRQ_EN) != 0)
1884      {
1885         gba_request_irq(machine, INT_VBL);
1886      }
1887
1888      for (ch = 0; ch < 4; ch++)
1889      {
1890         ctrl = gba_state->dma_regs[(ch*3)+2]>>16;
1891
1892         // VBL-triggered DMA?
1893         if ((ctrl & 0x8000) && ((ctrl & 0x3000) == 0x1000))
1894         {
1895            dma_exec(machine, ch);
1896         }
1897      }
1898   }
1899
1900   timer_adjust_oneshot(gba_state->hbl_timer, video_screen_get_time_until_pos(machine->primary_screen, scanline, 240), 0);
1901   timer_adjust_oneshot(gba_state->scan_timer, video_screen_get_time_until_pos(machine->primary_screen, ( scanline + 1 ) % 228, 0), 0);
1902}
1903
1904static MACHINE_RESET( gba )
1905{
1906   const device_config *gb_a_l = devtag_get_device(machine, "direct_a_left");
1907   const device_config *gb_a_r = devtag_get_device(machine, "direct_a_right");
1908   const device_config *gb_b_l = devtag_get_device(machine, "direct_b_left");
1909   const device_config *gb_b_r = devtag_get_device(machine, "direct_b_right");
1910   gba_state *gba_state = machine->driver_data;
1911
1912   memset(gba_state, 0, sizeof(gba_state));
1913   gba_state->SOUNDBIAS = 0x0200;
1914   gba_state->flash64k_state = FLASH_IDLEBYTE0;
1915   gba_state->eeprom_state = EEP_IDLE;
1916   gba_state->SIOMULTI0 = 0xffff;
1917   gba_state->SIOMULTI1 = 0xffff;
1918   gba_state->SIOMULTI2 = 0xffff;
1919   gba_state->SIOMULTI3 = 0xffff;
1920   gba_state->KEYCNT = 0x03ff;
1921   gba_state->RCNT = 0x8000;
1922   gba_state->JOYSTAT = 0x0002;
1923
1924   timer_adjust_oneshot(gba_state->scan_timer, video_screen_get_time_until_pos(machine->primary_screen, 1, 0), 0);
1925   timer_adjust_oneshot(gba_state->hbl_timer, attotime_never, 0);
1926   timer_adjust_oneshot(gba_state->dma_timer[0], attotime_never, 0);
1927   timer_adjust_oneshot(gba_state->dma_timer[1], attotime_never, 1);
1928   timer_adjust_oneshot(gba_state->dma_timer[2], attotime_never, 2);
1929   timer_adjust_oneshot(gba_state->dma_timer[3], attotime_never, 3);
1930
1931   gba_state->fifo_a_ptr = gba_state->fifo_b_ptr = 17;   // indicate empty
1932   gba_state->fifo_a_in = gba_state->fifo_b_in = 17;
1933
1934   // and clear the DACs
1935        dac_signed_data_w(gb_a_l, 0x80);
1936        dac_signed_data_w(gb_a_r, 0x80);
1937        dac_signed_data_w(gb_b_l, 0x80);
1938        dac_signed_data_w(gb_b_r, 0x80);
1939}
1940
1941static MACHINE_START( gba )
1942{
1943   gba_state *gba_state = machine->driver_data;
1944
1945   /* add a hook for battery save */
1946   add_exit_callback(machine, gba_machine_stop);
1947
1948   /* create a timer to fire scanline functions */
1949   gba_state->scan_timer = timer_alloc(machine, perform_scan, 0);
1950   gba_state->hbl_timer = timer_alloc(machine, perform_hbl, 0);
1951   timer_adjust_oneshot(gba_state->scan_timer, video_screen_get_time_until_pos(machine->primary_screen, 1, 0), 0);
1952
1953   /* and one for each DMA channel */
1954   gba_state->dma_timer[0] = timer_alloc(machine, dma_complete, 0);
1955   gba_state->dma_timer[1] = timer_alloc(machine, dma_complete, 0);
1956   gba_state->dma_timer[2] = timer_alloc(machine, dma_complete, 0);
1957   gba_state->dma_timer[3] = timer_alloc(machine, dma_complete, 0);
1958   timer_adjust_oneshot(gba_state->dma_timer[0], attotime_never, 0);
1959   timer_adjust_oneshot(gba_state->dma_timer[1], attotime_never, 1);
1960   timer_adjust_oneshot(gba_state->dma_timer[2], attotime_never, 2);
1961   timer_adjust_oneshot(gba_state->dma_timer[3], attotime_never, 3);
1962
1963   /* also one for each timer (heh) */
1964   gba_state->tmr_timer[0] = timer_alloc(machine, timer_expire, 0);
1965   gba_state->tmr_timer[1] = timer_alloc(machine, timer_expire, 0);
1966   gba_state->tmr_timer[2] = timer_alloc(machine, timer_expire, 0);
1967   gba_state->tmr_timer[3] = timer_alloc(machine, timer_expire, 0);
1968   timer_adjust_oneshot(gba_state->tmr_timer[0], attotime_never, 0);
1969   timer_adjust_oneshot(gba_state->tmr_timer[1], attotime_never, 1);
1970   timer_adjust_oneshot(gba_state->tmr_timer[2], attotime_never, 2);
1971   timer_adjust_oneshot(gba_state->tmr_timer[3], attotime_never, 3);
1972
1973   /* and an IRQ handling timer */
1974   gba_state->irq_timer = timer_alloc(machine, handle_irq, 0);
1975   timer_adjust_oneshot(gba_state->irq_timer, attotime_never, 0);
1976
1977   gba_video_start(machine);
1978}
1979
1980ROM_START( gba )
1981   ROM_REGION( 0x8000, "bios", ROMREGION_ERASE00 )
1982   ROM_LOAD( "gba_state->bin", 0x000000, 0x004000, CRC(81977335) )
1983
1984   /* cartridge region - 32 MBytes (128 Mbit) */
1985   ROM_REGION( 0x2000000, "cartridge", ROMREGION_ERASEFF )
1986ROM_END
1987
1988static READ32_HANDLER( sram_r )
1989{
1990   gba_state *gba_state = space->machine->driver_data;
1991
1992   return gba_state->gba_sram[offset];
1993}
1994
1995static WRITE32_HANDLER( sram_w )
1996{
1997   gba_state *gba_state = space->machine->driver_data;
1998
1999   COMBINE_DATA(&gba_state->gba_sram[offset]);
2000}
2001
2002static READ32_HANDLER( flash64k_r )
2003{
2004   gba_state *gba_state = space->machine->driver_data;
2005   switch( gba_state->flash64k_state )
2006   {
2007      case FLASH_IDLEBYTE0:
2008      case FLASH_IDLEBYTE1:
2009      case FLASH_IDLEBYTE2:
2010      case FLASH_ERASEBYTE0:
2011      case FLASH_ERASEBYTE1:
2012      case FLASH_ERASEBYTE2:
2013              return gba_state->gba_flash64k[offset];
2014      case FLASH_IDENT:
2015              if( offset == 0 )
2016              {
2017              return ( gba_state->gba_flash64k[0] & 0xffff0000 ) | 0x00001b32;
2018              }
2019              else
2020              {
2021              return gba_state->gba_flash64k[offset];
2022              }
2023              return 0x00001b32;
2024      case FLASH_ERASE_ALL:
2025      case FLASH_ERASE_4K:
2026              return gba_state->gba_flash64k[offset];
2027   }
2028   return gba_state->gba_flash64k[offset];
2029}
2030
2031static WRITE32_HANDLER( flash64k_w )
2032{
2033   gba_state *gba_state = space->machine->driver_data;
2034
2035   switch( gba_state->flash64k_state )
2036   {
2037      case FLASH_IDLEBYTE0:
2038      case FLASH_ERASEBYTE0:
2039              if( offset == 0x5555/4 && ~mem_mask == 0xffff00ff )
2040              {
2041              if( ( data & mem_mask ) == 0x0000aa00 )
2042              {
2043                      if( gba_state->flash64k_state == FLASH_IDLEBYTE0 )
2044                      {
2045                         gba_state->flash64k_state = FLASH_IDLEBYTE1;
2046                      }
2047                      else if( gba_state->flash64k_state == FLASH_ERASEBYTE0 )
2048                      {
2049                         gba_state->flash64k_state = FLASH_ERASEBYTE1;
2050                      }
2051              }
2052              }
2053              break;
2054      case FLASH_IDLEBYTE1:
2055      case FLASH_ERASEBYTE1:
2056              if( offset == 0x2aaa/4 && ~mem_mask == 0xff00ffff )
2057              {
2058              if( ( data & mem_mask ) == 0x00550000 )
2059              {
2060                      if( gba_state->flash64k_state == FLASH_IDLEBYTE1 )
2061                      {
2062                         gba_state->flash64k_state = FLASH_IDLEBYTE2;
2063                      }
2064                      else if( gba_state->flash64k_state == FLASH_ERASEBYTE1 )
2065                      {
2066                         gba_state->flash64k_state = FLASH_ERASEBYTE2;
2067                      }
2068              }
2069              }
2070              break;
2071      case FLASH_IDLEBYTE2:
2072      case FLASH_ERASEBYTE2:
2073              if( offset == 0x5555/4 && ~mem_mask == 0xffff00ff )
2074              {
2075                 if( gba_state->flash64k_state == FLASH_IDLEBYTE2 )
2076                 {
2077                         switch( ( data & mem_mask ) >> 8 )
2078                         {
2079                         case 0x80:
2080                                 gba_state->flash64k_state = FLASH_ERASEBYTE0;
2081                                 break;
2082                         case 0x90:
2083                                 gba_state->flash64k_state = FLASH_IDENT;
2084                                 break;
2085                         case 0xa0:
2086                                 gba_state->flash64k_state = FLASH_WRITE;
2087                                 break;
2088                         }
2089                 }
2090                 else if( gba_state->flash64k_state == FLASH_ERASEBYTE2 )
2091                 {
2092                         if( ( data & mem_mask ) == 0x00001000 )
2093                         {
2094                            UINT32 flashWord;
2095                            for( flashWord = 0; flashWord < 0x10000/4; flashWord++ )
2096                            {
2097                                    gba_state->gba_flash64k[flashWord] = 0xffffffff;
2098                            }
2099                            gba_state->flash64k_state = FLASH_ERASE_ALL;
2100                         }
2101                 }
2102              }
2103              else if( ( offset & 0xffffc3ff ) == 0 && ( data & mem_mask ) == 0x00000030 )
2104              {
2105                 UINT32 flashWord;
2106                 gba_state->flash64k_page = offset >> 10;
2107                 for( flashWord = offset; flashWord < offset + 0x1000/4; flashWord++ )
2108                 {
2109                         gba_state->gba_flash64k[flashWord] = 0xffffffff;
2110                 }
2111                 gba_state->flash64k_state = FLASH_ERASE_4K;
2112              }
2113              break;
2114      case FLASH_IDENT:
2115              // Hack; any sensibly-written game should follow up with the relevant read, which will reset the state to FLASH_IDLEBYTE0.
2116              gba_state->flash64k_state = FLASH_IDLEBYTE0;
2117              flash64k_w( space, offset, data, ~mem_mask );
2118              break;
2119      case FLASH_ERASE_4K:
2120              // Hack; any sensibly-written game should follow up with the relevant read, which will reset the state to FLASH_IDLEBYTE0.
2121              gba_state->flash64k_state = FLASH_IDLEBYTE0;
2122              flash64k_w( space, offset, data, ~mem_mask );
2123              break;
2124      case FLASH_ERASE_ALL:
2125              // Hack; any sensibly-written game should follow up with the relevant read, which will reset the state to FLASH_IDLEBYTE0.
2126              gba_state->flash64k_state = FLASH_IDLEBYTE0;
2127              flash64k_w( space, offset, data, ~mem_mask );
2128              break;
2129      case FLASH_WRITE:
2130              COMBINE_DATA(&gba_state->gba_flash64k[offset]);
2131              gba_state->flash64k_state = FLASH_IDLEBYTE0;
2132              break;
2133   }
2134}
2135
2136static READ32_HANDLER( eeprom_r )
2137{
2138   UINT32 out;
2139   gba_state *gba_state = space->machine->driver_data;
2140
2141   switch (gba_state->eeprom_state)
2142   {
2143      case EEP_IDLE:
2144//         printf("eeprom_r: @ %x, mask %08x (state %d) (PC=%x) = %d\n", offset, ~mem_mask, gba_state->eeprom_state, activecpu_get_pc(), 1);
2145         return 0x00010001;   // "ready"
2146         break;
2147
2148        case EEP_READFIRST:
2149            gba_state->eeprom_count--;
2150
2151            if (!gba_state->eeprom_count)
2152            {
2153                gba_state->eeprom_count = 64;
2154                gba_state->eeprom_bits = 0;
2155                gba_state->eep_data = 0;
2156                gba_state->eeprom_state = EEP_READ;
2157            }
2158            break;
2159      case EEP_READ:
2160         if ((gba_state->eeprom_bits == 0) && (gba_state->eeprom_count))
2161         {
2162            gba_state->eep_data = gba_state->gba_eeprom[gba_state->eeprom_addr];
2163             //      printf("EEPROM read @ %x = %x (%x)\n", gba_state->eeprom_addr, gba_state->eep_data, (gba_state->eep_data & 0x80) ? 1 : 0);
2164            gba_state->eeprom_addr++;
2165            gba_state->eeprom_bits = 8;
2166         }
2167
2168         out = (gba_state->eep_data & 0x80) ? 1 : 0;
2169         out |= (out<<16);
2170         gba_state->eep_data <<= 1;
2171
2172         gba_state->eeprom_bits--;
2173         gba_state->eeprom_count--;
2174
2175         if (!gba_state->eeprom_count)
2176         {
2177            gba_state->eeprom_state = EEP_IDLE;
2178         }
2179
2180//         printf("out = %08x\n", out);
2181//         printf("eeprom_r: @ %x, mask %08x (state %d) (PC=%x) = %08x\n", offset, ~mem_mask, gba_state->eeprom_state, activecpu_get_pc(), out);
2182         return out;
2183         break;
2184   }
2185//   printf("eeprom_r: @ %x, mask %08x (state %d) (PC=%x) = %d\n", offset, ~mem_mask, gba_state->eeprom_state, activecpu_get_pc(), 0);
2186   return 0;
2187}
2188
2189static WRITE32_HANDLER( eeprom_w )
2190{
2191   gba_state *gba_state = space->machine->driver_data;
2192
2193   if (~mem_mask == 0x0000ffff)
2194   {
2195      data >>= 16;
2196   }
2197
2198//   printf("eeprom_w: %x @ %x (state %d) (PC=%x)\n", data, offset, gba_state->eeprom_state, activecpu_get_pc());
2199
2200   switch (gba_state->eeprom_state)
2201   {
2202      case EEP_IDLE:
2203         if (data == 1)
2204         {
2205            gba_state->eeprom_state++;
2206         }
2207         break;
2208
2209      case EEP_COMMAND:
2210         if (data == 1)
2211         {
2212                gba_state->eeprom_command = EEP_READFIRST;
2213         }
2214         else
2215         {
2216            gba_state->eeprom_command = EEP_WRITE;
2217         }
2218         gba_state->eeprom_state = EEP_ADDR;
2219         gba_state->eeprom_count = 6;
2220         gba_state->eeprom_addr = 0;
2221         break;
2222
2223      case EEP_ADDR:
2224         gba_state->eeprom_addr <<= 1;
2225         gba_state->eeprom_addr |= (data & 1);
2226         gba_state->eeprom_count--;
2227         if (!gba_state->eeprom_count)
2228         {
2229            gba_state->eeprom_addr *= 8;   // each address points to 8 bytes
2230                if (gba_state->eeprom_command == EEP_READFIRST)
2231            {
2232               gba_state->eeprom_state = EEP_AFTERADDR;
2233            }
2234            else
2235            {
2236               gba_state->eeprom_count = 64;
2237               gba_state->eeprom_bits = 8;
2238               gba_state->eeprom_state = EEP_WRITE;
2239               gba_state->eep_data = 0;
2240            }
2241         }
2242         break;
2243
2244      case EEP_AFTERADDR:
2245         gba_state->eeprom_state = gba_state->eeprom_command;
2246         gba_state->eeprom_count = 64;
2247         gba_state->eeprom_bits = 0;
2248         gba_state->eep_data = 0;
2249            if( gba_state->eeprom_state == EEP_READFIRST )
2250            {
2251                gba_state->eeprom_count = 4;
2252            }
2253         break;
2254
2255      case EEP_WRITE:
2256         gba_state->eep_data<<= 1;
2257         gba_state->eep_data |= (data & 1);
2258         gba_state->eeprom_bits--;
2259         gba_state->eeprom_count--;
2260
2261         if (gba_state->eeprom_bits == 0)
2262         {
2263            printf("%08x: EEPROM: %02x to %x\n", cpu_get_pc(space->   machine->cpu[0]), gba_state->eep_data, gba_state->eeprom_addr );
2264            gba_state->gba_eeprom[gba_state->eeprom_addr] = gba_state->eep_data;
2265            gba_state->eeprom_addr++;
2266            gba_state->eep_data = 0;
2267            gba_state->eeprom_bits = 8;
2268         }
2269
2270         if (!gba_state->eeprom_count)
2271         {
2272            gba_state->eeprom_state = EEP_AFTERWRITE;
2273         }
2274         break;
2275
2276      case EEP_AFTERWRITE:
2277         gba_state->eeprom_state = EEP_IDLE;
2278         break;
2279   }
2280}
2281
2282static DEVICE_IMAGE_LOAD( gba_cart )
2283{
2284   UINT8 *ROM = memory_region(image->machine, "cartridge");
2285   int i;
2286   gba_state *gba_state = image->machine->driver_data;
2287
2288   nvsize = 0;
2289   nvptr = (UINT8 *)NULL;
2290
2291   image_fread(image, ROM, image_length(image));
2292
2293   for (i = 0; i < image_length(image); i++)
2294   {
2295      if (!memcmp(&ROM[i], "EEPROM_", 7))
2296      {
2297         nvptr = (UINT8 *)&gba_state->gba_eeprom;
2298         nvsize = 0x2000;
2299
2300         if (image_length(image) <= (16*1024*1024))
2301         {
2302            memory_install_read32_handler(cpu_get_address_space(image->machine->cpu[0], ADDRESS_SPACE_PROGRAM), 0xd000000, 0xdffffff, 0, 0, eeprom_r);
2303            memory_install_write32_handler(cpu_get_address_space(image->machine->cpu[0], ADDRESS_SPACE_PROGRAM), 0xd000000, 0xdffffff, 0, 0, eeprom_w);
2304         }
2305         else
2306         {
2307            memory_install_read32_handler(cpu_get_address_space(image->machine->cpu[0], ADDRESS_SPACE_PROGRAM), 0xdffff00, 0xdffffff, 0, 0, eeprom_r);
2308            memory_install_write32_handler(cpu_get_address_space(image->machine->cpu[0], ADDRESS_SPACE_PROGRAM), 0xdffff00, 0xdffffff, 0, 0, eeprom_w);
2309         }
2310         break;
2311      }
2312      else if (!memcmp(&ROM[i], "SRAM_", 5))
2313      {
2314         nvptr = (UINT8 *)&gba_state->gba_sram;
2315         nvsize = 0x10000;
2316
2317         memory_install_read32_handler(cpu_get_address_space(image->machine->cpu[0], ADDRESS_SPACE_PROGRAM), 0xe000000, 0xe00ffff, 0, 0, sram_r);
2318         memory_install_write32_handler(cpu_get_address_space(image->machine->cpu[0], ADDRESS_SPACE_PROGRAM), 0xe000000, 0xe00ffff, 0, 0, sram_w);
2319         break;
2320      }
2321      else if (!memcmp(&ROM[i], "FLASH1M_", 8))
2322      {
2323         printf("game has 1M FLASH\n");
2324         break;
2325      }
2326      else if (!memcmp(&ROM[i], "FLASH", 5))
2327      {
2328         nvptr = (UINT8 *)&gba_state->gba_flash64k;
2329         nvsize = 0x10000;
2330
2331         memory_install_read32_handler(cpu_get_address_space(image->machine->cpu[0], ADDRESS_SPACE_PROGRAM), 0xe000000, 0xe007fff, 0, 0, flash64k_r);
2332         memory_install_write32_handler(cpu_get_address_space(image->machine->cpu[0], ADDRESS_SPACE_PROGRAM), 0xe000000, 0xe007fff, 0, 0, flash64k_w);
2333         break;
2334      }
2335      else if (!memcmp(&ROM[i], "SIIRTC_V", 8))
2336      {
2337         printf("game has RTC\n");
2338         break;
2339      }
2340   }
2341
2342   // if save media was found, reload it
2343   if (nvsize > 0)
2344   {
2345      image_battery_load(image, nvptr, nvsize);
2346      nvimage = image;
2347   }
2348   else
2349   {
2350      nvimage = NULL;
2351      nvsize = 0;
2352   }
2353
2354   // mirror the ROM
2355   switch (image_length(image))
2356   {
2357      case 2*1024*1024:
2358         memcpy(ROM+0x200000, ROM, 0x200000);
2359      // intentional fall-through
2360      case 4*1024*1024:
2361         memcpy(ROM+0x400000, ROM, 0x400000);
2362      // intentional fall-through
2363      case 8*1024*1024:
2364         memcpy(ROM+0x800000, ROM, 0x800000);
2365      // intentional fall-through
2366      case 16*1024*1024:
2367         memcpy(ROM+0x1000000, ROM, 0x1000000);
2368         break;
2369   }
2370
2371   return INIT_PASS;
2372}
2373
2374static MACHINE_DRIVER_START( gbadv )
2375   MDRV_DRIVER_DATA(gba_state)
2376
2377   MDRV_CPU_ADD("maincpu", ARM7, 16777216)
2378   MDRV_CPU_PROGRAM_MAP(gbadvance_map)
2379
2380   MDRV_MACHINE_START(gba)
2381   MDRV_MACHINE_RESET(gba)
2382
2383   MDRV_SCREEN_ADD("gbalcd", RASTER)   // htot hst vwid vtot vst vis
2384   MDRV_SCREEN_RAW_PARAMS(16777216/4, 308, 0,  240, 228, 0,  160)
2385   MDRV_SCREEN_FORMAT(BITMAP_FORMAT_INDEXED16)
2386   MDRV_PALETTE_LENGTH(32768)
2387   MDRV_PALETTE_INIT( gba )
2388
2389   MDRV_VIDEO_START(generic_bitmapped)
2390   MDRV_VIDEO_UPDATE(generic_bitmapped)
2391
2392   MDRV_SPEAKER_STANDARD_STEREO("spkleft", "spkright")
2393   MDRV_SOUND_ADD("custom", GAMEBOY, 0)
2394   MDRV_SOUND_ROUTE(0, "spkleft", 0.50)
2395   MDRV_SOUND_ROUTE(1, "spkright", 0.50)
2396   MDRV_SOUND_ADD("direct_a_left", DAC, 0)         // GBA direct sound A left
2397   MDRV_SOUND_ROUTE(ALL_OUTPUTS, "spkleft", 0.50)
2398   MDRV_SOUND_ADD("direct_a_right", DAC, 0)      // GBA direct sound A right
2399   MDRV_SOUND_ROUTE(ALL_OUTPUTS, "spkright", 0.50)
2400   MDRV_SOUND_ADD("direct_b_left", DAC, 0)         // GBA direct sound B left
2401   MDRV_SOUND_ROUTE(ALL_OUTPUTS, "spkleft", 0.50)
2402   MDRV_SOUND_ADD("direct_b_right", DAC, 0)      // GBA direct sound B right
2403   MDRV_SOUND_ROUTE(ALL_OUTPUTS, "spkright", 0.50)
2404
2405   MDRV_CARTSLOT_ADD("cart")
2406   MDRV_CARTSLOT_EXTENSION_LIST("gba,bin")
2407   MDRV_CARTSLOT_LOAD(gba_cart)
2408MACHINE_DRIVER_END
2409
2410/* this emulates the GBA's hardware protection: the BIOS returns only zeros when the PC is not in it,
2411   and some games verify that as a protection check (notably Metroid Fusion) */
2412static DIRECT_UPDATE_HANDLER( gba_direct )
2413{
2414   if (address > 0x4000)
2415   {
2416      memory_set_bankptr(space->machine, 1, memory_region(space->machine, "bios")+0x4000);
2417   }
2418   else
2419   {
2420      memory_set_bankptr(space->machine, 1, memory_region(space->machine, "bios"));
2421   }
2422
2423   return address;
2424}
2425
2426static DRIVER_INIT(gbadv)
2427{
2428   memory_set_direct_update_handler( cpu_get_address_space(machine->cpu[0], ADDRESS_SPACE_PROGRAM), gba_direct );
2429}
2430
2431/*    YEAR  NAME PARENT COMPAT MACHINE INPUT   INIT   CONFIG COMPANY     FULLNAME */
2432CONS( 2001, gba, 0,     0,     gbadv,  gbadv,  gbadv, 0,     "Nintendo", "Game Boy Advance", GAME_SUPPORTS_SAVE)
docs/messnew.txt
r5376r5377
33New System Drivers Supported (in no particular order):
44------------------------------------------------------
55- Tandy 1000RL  [Wilbert Pol]
6- Nintendo Game Boy Advance [R. Belmont, MooglyGuy]
67
78
8
99Systems Promoted from GAME_NOT_WORKING:
1010---------------------------------------
1111- Elektor Junior [Manfred Schneider]

Previous 15994 Revisions Next


© 1998-2018 The MESS Team