Previous 15994 Revisions Next

r8315 Friday 25th June, 2010 at 09:57:47 UTC by Fabio Priuli
[NES] separating pcb-based emulation from the mapper-based code (part 2)
[src/mess/includes]nes.h
[src/mess/machine]nes.c nes_ines.c nes_mmc.h
[src/mess/video]nes.c

src/mess/video/nes.c
r8314r8315
6464   ppu2c0x_render(state->ppu, bitmap, 0, 0, 0, 0);
6565
6666   /* if this is a disk system game, check for the flip-disk key */
67   if (state->mapper == 20)
67   if (state->disk_expansion && state->pcb_id == NO_BOARD)
6868   {
6969      // latch this input so it doesn't go at warp speed
7070      if ((input_port_read(screen->machine, "FLIPDISK") & 0x01) && (!state->last_frame_flip))
src/mess/includes/nes.h
r8314r8315
232232
233233   /***** FDS-floppy related *****/
234234
235   int     disk_expansion;
236
235237   UINT8   fds_sides;
236238   UINT8   *fds_data;   // here, we store a copy of the disk
237239   UINT8   *fds_ram;   // here, we emulate the RAM adapter
src/mess/machine/nes.c
r8314r8315
2525
2626static void init_nes_core(running_machine *machine);
2727static void nes_machine_stop(running_machine *machine);
28static READ8_HANDLER(nes_fds_r);
29static WRITE8_HANDLER(nes_fds_w);
30static void fds_irq(running_device *device, int scanline, int vblank, int blanked);
2831
2932/***************************************************************************
3033    FUNCTIONS
r8314r8315
5255
5356   memory_set_bankptr(machine, "bank10", state->rom);
5457
58   /* If there is Disk Expansion and no cart has been loaded, setup memory accordingly */
59   if (state->disk_expansion && state->pcb_id == NO_BOARD)
60   {
61      state->slow_banking = 0;
62      /* If we are loading a disk we have already filled state->fds_data and we don't want to overwrite it,
63       if we are loading a cart image identified as mapper 20 (probably wrong mapper...) we need to alloc
64       memory for use in nes_fds_r/nes_fds_w. Same goes for allocation of fds_ram (used for bank2)  */
65      if (state->fds_data == NULL)
66      {
67         UINT32 size = memory_region_length(machine, "maincpu") - 0x10000;
68         state->fds_data = auto_alloc_array_clear(machine, UINT8, size);
69         memcpy(state->fds_data, state->rom, size);   // copy in fds_data the cart PRG
70      }
71      if (state->fds_ram == NULL)
72         state->fds_ram = auto_alloc_array(machine, UINT8, 0x8000);
73     
74      memory_install_read8_handler(space, 0x4030, 0x403f, 0, 0, nes_fds_r);
75      memory_install_read_bank(space, 0x6000, 0xdfff, 0, 0, "bank2");
76      memory_install_read_bank(space, 0xe000, 0xffff, 0, 0, "bank1");
77     
78      memory_install_write8_handler(space, 0x4020, 0x402f, 0, 0, nes_fds_w);
79      memory_install_write_bank(space, 0x6000, 0xdfff, 0, 0, "bank2");
80     
81      memory_set_bankptr(machine, "bank1", &state->rom[0xe000]);
82      memory_set_bankptr(machine, "bank2", state->fds_ram);
83      return;
84   }
85
5586   /* Set up the mapper callbacks */
5687   if (state->format == 1)
5788      mapper_handlers_setup(machine);
r8314r8315
6192   /* Set up the memory handlers for the mapper */
6293   switch (state->mapper)
6394   {
64      case 20:
65         state->slow_banking = 0;
66         /* If we are loading a disk we have already filled state->fds_data and we don't want to overwrite it,
67            if we are loading a cart image identified as mapper 20 (probably wrong mapper...) we need to alloc
68            memory for use in nes_fds_r/nes_fds_w. Same goes for allocation of fds_ram (used for bank2)  */
69         if (state->fds_data == NULL)
70         {
71            UINT32 size = memory_region_length(machine, "maincpu") - 0x10000;
72            state->fds_data = auto_alloc_array_clear(machine, UINT8, size);
73            memcpy(state->fds_data, state->rom, size);   // copy in fds_data the cart PRG
74         }
75         if (state->fds_ram == NULL)
76            state->fds_ram = auto_alloc_array(machine, UINT8, 0x8000);
77
78         memory_install_read8_handler(space, 0x4030, 0x403f, 0, 0, nes_fds_r);
79         memory_install_read_bank(space, 0x6000, 0xdfff, 0, 0, "bank2");
80         memory_install_read_bank(space, 0xe000, 0xffff, 0, 0, "bank1");
81
82         memory_install_write8_handler(space, 0x4020, 0x402f, 0, 0, nes_fds_w);
83         memory_install_write_bank(space, 0x6000, 0xdfff, 0, 0, "bank2");
84
85         memory_set_bankptr(machine, "bank1", &state->rom[0xe000]);
86         memory_set_bankptr(machine, "bank2", state->fds_ram);
87         break;
8895      case 50:
8996         memory_install_write8_handler(space, 0x4020, 0x403f, 0, 0, nes_mapper50_add_w);
9097         memory_install_write8_handler(space, 0x40a0, 0x40bf, 0, 0, nes_mapper50_add_w);
r8314r8315
185192   state->mid_ram_enable = 1;
186193   
187194   /* Reset the mapper variables. Will also mark the char-gen ram as dirty */
195   if (state->disk_expansion && state->pcb_id == NO_BOARD)
196      ppu2c0x_set_hblank_callback(state->ppu, fds_irq);
197
188198   if (state->format == 1)
189199      nes_mapper_reset(machine);
190200
r8314r8315
13171327   hash_compute(dest, &data[16], length - 16, functions);
13181328}
13191329
1330/**************************
1331 
1332 Disk System emulation
1333 
1334**************************/
13201335
1336static void fds_irq( running_device *device, int scanline, int vblank, int blanked )
1337{
1338   nes_state *state = (nes_state *)device->machine->driver_data;
1339   
1340   if (state->IRQ_enable_latch)
1341      cpu_set_input_line(state->maincpu, M6502_IRQ_LINE, HOLD_LINE);
1342   
1343   if (state->IRQ_enable)
1344   {
1345      if (state->IRQ_count <= 114)
1346      {
1347         cpu_set_input_line(state->maincpu, M6502_IRQ_LINE, HOLD_LINE);
1348         state->IRQ_enable = 0;
1349         state->fds_status0 |= 0x01;
1350      }
1351      else
1352         state->IRQ_count -= 114;
1353   }
1354}
1355
1356static READ8_HANDLER( nes_fds_r )
1357{
1358   nes_state *state = (nes_state *)space->machine->driver_data;
1359   UINT8 ret = 0x00;
1360   
1361   switch (offset)
1362   {
1363      case 0x00: /* $4030 - disk status 0 */
1364         ret = state->fds_status0;
1365         /* clear the disk IRQ detect flag */
1366         state->fds_status0 &= ~0x01;
1367         break;
1368      case 0x01: /* $4031 - data latch */
1369         /* don't read data if disk is unloaded */
1370         if (state->fds_data == NULL)
1371            ret = 0;
1372         else if (state->fds_current_side)
1373            ret = state->fds_data[(state->fds_current_side - 1) * 65500 + state->fds_head_position++];
1374         else
1375            ret = 0;
1376         break;
1377      case 0x02: /* $4032 - disk status 1 */
1378         /* return "no disk" status if disk is unloaded */
1379         if (state->fds_data == NULL)
1380            ret = 1;
1381         else if (state->fds_last_side != state->fds_current_side)
1382         {
1383            /* If we've switched disks, report "no disk" for a few reads */
1384            ret = 1;
1385            state->fds_count++;
1386            if (state->fds_count == 50)
1387            {
1388               state->fds_last_side = state->fds_current_side;
1389               state->fds_count = 0;
1390            }
1391         }
1392         else
1393            ret = (state->fds_current_side == 0); /* 0 if a disk is inserted */
1394         break;
1395      case 0x03: /* $4033 */
1396         ret = 0x80;
1397         break;
1398      default:
1399         ret = 0x00;
1400         break;
1401   }
1402
1403   return ret;
1404}
1405
1406static WRITE8_HANDLER( nes_fds_w )
1407{
1408   nes_state *state = (nes_state *)space->machine->driver_data;
1409   
1410   switch (offset)
1411   {
1412      case 0x00:
1413         state->IRQ_count_latch = (state->IRQ_count_latch & 0xff00) | data;
1414         break;
1415      case 0x01:
1416         state->IRQ_count_latch = (state->IRQ_count_latch & 0x00ff) | (data << 8);
1417         break;
1418      case 0x02:
1419         state->IRQ_count = state->IRQ_count_latch;
1420         state->IRQ_enable = data;
1421         break;
1422      case 0x03:
1423         // d0 = sound io (1 = enable)
1424         // d1 = disk io (1 = enable)
1425         break;
1426      case 0x04:
1427         /* write data out to disk */
1428         break;
1429      case 0x05:
1430         state->fds_motor_on = BIT(data, 0);
1431         
1432         if (BIT(data, 1))
1433            state->fds_head_position = 0;
1434         
1435         state->fds_read_mode = BIT(data, 2);
1436         set_nt_mirroring(space->machine, BIT(data, 3) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
1437         
1438         if ((!(data & 0x40)) && (state->fds_write_reg & 0x40))
1439            state->fds_head_position -= 2; // ???
1440         
1441         state->IRQ_enable_latch = BIT(data, 7);
1442         state->fds_write_reg = data;
1443         break;
1444   }
1445}
1446
13211447static void nes_load_proc( running_device *image )
13221448{
13231449   nes_state *state = (nes_state *)image->machine->driver_data;
r8314r8315
13501476DRIVER_INIT( famicom )
13511477{
13521478   nes_state *state = (nes_state *)machine->driver_data;
1479   int i;
13531480
1354   /* clear some of the cart variables we don't use */
1481   /* clear some of the variables we don't use */
13551482   state->trainer = 0;
13561483   state->battery = 0;
13571484   state->prg_ram = 0;
1358   state->prg_chunks = state->chr_chunks = 0;
1359
1360   /* initialize the system as if using a mapper 20 cart */
1361   state->format = 1;
1362   state->mapper = 20;
13631485   state->four_screen_vram = 0;
13641486   state->hard_mirroring = 0;
1487   state->prg_chunks = state->chr_chunks = 0;
13651488
1489   /* initialize the disk system */
1490   state->disk_expansion = 1;
1491   state->pcb_id = NO_BOARD;
1492
13661493   state->fds_sides = 0;
1367   state->fds_data = NULL;
1368
1494   state->fds_last_side = 0;
1495   state->fds_count = 0;
1496   state->fds_motor_on = 0;
1497   state->fds_door_closed = 0;
1498   state->fds_current_side = 1;
1499   state->fds_head_position = 0;
1500   state->fds_status0 = 0;
1501   state->fds_read_mode = state->fds_write_reg = 0;
1502   
13691503   state->fds_data = auto_alloc_array_clear(machine, UINT8, 65500 * 2);
13701504   state->fds_ram = auto_alloc_array_clear(machine, UINT8, 0x8000);
13711505   state_save_register_global_pointer(machine, state->fds_ram, 0x8000);
13721506
1507   // setup CHR accesses to 8k VRAM
1508   state->vram = memory_region(machine, "gfx2");
1509   for (i = 0; i < 8; i++)
1510   {
1511      state->chr_map[i].source = CHRRAM;
1512      state->chr_map[i].origin = i * 0x400; // for save state uses!
1513      state->chr_map[i].access = &state->vram[state->chr_map[i].origin];
1514   }
1515   
13731516   floppy_install_load_proc(floppy_get_device(machine, 0), nes_load_proc);
13741517   floppy_install_unload_proc(floppy_get_device(machine, 0), nes_unload_proc);
13751518}
src/mess/machine/nes_mmc.h
r8314r8315
116116WRITE8_HANDLER( nes_nt_w );
117117READ8_HANDLER( nes_nt_r );
118118
119READ8_HANDLER( nes_fds_r );
120WRITE8_HANDLER( nes_fds_w );
121119WRITE8_HANDLER( nes_mapper50_add_w );
122120WRITE8_HANDLER( smb2jb_extra_w );
123121
src/mess/machine/nes_ines.c
r8314r8315
18981898
18991899/*************************************************************
19001900
1901    Mapper 20
1902
1903    Known Boards: Reserved for FDS
1904    Games: any FDS disk
1905
1906    In MESS: Supported
1907
1908*************************************************************/
1909
1910static void fds_irq( running_device *device, int scanline, int vblank, int blanked )
1911{
1912   nes_state *state = (nes_state *)device->machine->driver_data;
1913
1914   if (state->IRQ_enable_latch)
1915      cpu_set_input_line(state->maincpu, M6502_IRQ_LINE, HOLD_LINE);
1916
1917   if (state->IRQ_enable)
1918   {
1919      if (state->IRQ_count <= 114)
1920      {
1921         cpu_set_input_line(state->maincpu, M6502_IRQ_LINE, HOLD_LINE);
1922         state->IRQ_enable = 0;
1923         state->fds_status0 |= 0x01;
1924      }
1925      else
1926         state->IRQ_count -= 114;
1927   }
1928}
1929
1930READ8_HANDLER( nes_fds_r )
1931{
1932   nes_state *state = (nes_state *)space->machine->driver_data;
1933   UINT8 ret = 0x00;
1934   LOG_MMC(("fds_r, offset: %04x\n", offset));
1935
1936   switch (offset)
1937   {
1938      case 0x00: /* $4030 - disk status 0 */
1939         ret = state->fds_status0;
1940         /* clear the disk IRQ detect flag */
1941         state->fds_status0 &= ~0x01;
1942         break;
1943      case 0x01: /* $4031 - data latch */
1944         /* don't read data if disk is unloaded */
1945         if (state->fds_data == NULL)
1946            ret = 0;
1947         else if (state->fds_current_side)
1948            ret = state->fds_data[(state->fds_current_side - 1) * 65500 + state->fds_head_position++];
1949         else
1950            ret = 0;
1951         break;
1952      case 0x02: /* $4032 - disk status 1 */
1953         /* return "no disk" status if disk is unloaded */
1954         if (state->fds_data == NULL)
1955            ret = 1;
1956         else if (state->fds_last_side != state->fds_current_side)
1957         {
1958            /* If we've switched disks, report "no disk" for a few reads */
1959            ret = 1;
1960            state->fds_count++;
1961            if (state->fds_count == 50)
1962            {
1963               state->fds_last_side = state->fds_current_side;
1964               state->fds_count = 0;
1965            }
1966         }
1967         else
1968            ret = (state->fds_current_side == 0); /* 0 if a disk is inserted */
1969         break;
1970      case 0x03: /* $4033 */
1971         ret = 0x80;
1972         break;
1973      default:
1974         ret = 0x00;
1975         break;
1976   }
1977
1978   LOG_FDS(("fds_r, address: %04x, data: %02x\n", offset + 0x4030, ret));
1979
1980   return ret;
1981}
1982
1983WRITE8_HANDLER( nes_fds_w )
1984{
1985   nes_state *state = (nes_state *)space->machine->driver_data;
1986   LOG_MMC(("fds_w, offset: %04x, data: %02x\n", offset, data));
1987
1988   switch (offset)
1989   {
1990      case 0x00:
1991         state->IRQ_count_latch = (state->IRQ_count_latch & 0xff00) | data;
1992         break;
1993      case 0x01:
1994         state->IRQ_count_latch = (state->IRQ_count_latch & 0x00ff) | (data << 8);
1995         break;
1996      case 0x02:
1997         state->IRQ_count = state->IRQ_count_latch;
1998         state->IRQ_enable = data;
1999         break;
2000      case 0x03:
2001         // d0 = sound io (1 = enable)
2002         // d1 = disk io (1 = enable)
2003         break;
2004      case 0x04:
2005         /* write data out to disk */
2006         break;
2007      case 0x05:
2008         state->fds_motor_on = BIT(data, 0);
2009
2010         if (BIT(data, 1))
2011            state->fds_head_position = 0;
2012
2013         state->fds_read_mode = BIT(data, 2);
2014         set_nt_mirroring(space->machine, BIT(data, 3) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
2015
2016         if ((!(data & 0x40)) && (state->fds_write_reg & 0x40))
2017            state->fds_head_position -= 2; // ???
2018
2019         state->IRQ_enable_latch = BIT(data, 7);
2020         state->fds_write_reg = data;
2021         break;
2022   }
2023
2024   LOG_FDS(("fds_w, address: %04x, data: %02x\n", offset + 0x4020, data));
2025}
2026
2027/*************************************************************
2028
20291901    Mapper 21 & 25
20301902
20311903    Known Boards: Konami VRC4A & VRC4C (21), VRC4B & VRC4D (25)
r8314r8315
1079610668   { 17, "FFE F8xxx",                 mapper17_l_w, NULL, NULL, NULL, NULL, NULL, ffe_irq },
1079710669   { 18, "Jaleco SS88006",            NULL, NULL, NULL, mapper18_w, NULL, NULL, jaleco_irq },
1079810670   { 19, "Namcot 106 + N106",         mapper19_l_w, mapper19_l_r, NULL, mapper19_w, NULL, NULL, namcot_irq },
10799   { 20, "Famicom Disk System",       NULL, NULL, NULL, NULL, NULL, NULL, fds_irq },
1080010671   { 21, "Konami VRC 4a",             NULL, NULL, NULL, konami_vrc4a_w, NULL, NULL, konami_irq },
1080110672   { 22, "Konami VRC 2a",             NULL, NULL, NULL, konami_vrc2a_w, NULL, NULL, NULL },
1080210673   { 23, "Konami VRC 2b",             NULL, NULL, NULL, konami_vrc2b_w, NULL, NULL, konami_irq },
r8314r8315
1120511076         state->mmc_chr_source = state->chr_chunks ? CHRROM : CHRRAM;
1120611077         set_nt_mirroring(machine, PPU_MIRROR_VERT);
1120711078         break;
11208      case 20:
11209         state->fds_last_side = 0;
11210         state->fds_count = 0;
11211         state->fds_motor_on = 0;
11212         state->fds_door_closed = 0;
11213         state->fds_current_side = 1;
11214         state->fds_head_position = 0;
11215         state->fds_status0 = 0;
11216         state->fds_read_mode = state->fds_write_reg = 0;
11217         break;
1121811079      case 21:
1121911080      case 22:
1122011081      case 23:

Previous 15994 Revisions Next


© 1998-2018 The MESS Team