Previous 15994 Revisions Next

r14463 Wednesday 15th February, 2012 at 03:12:36 UTC by MooglyGuy
[N64] Partially fix PIF access, several more games recognize cart SRAM, cart FlashROM, cart EEPROM, and controller paks [Ryan Holtz]

(nw) Among others, Rampage: World Tour now properly saves its memory pak data properly.
[src/mame/includes]n64.h
[src/mame/machine]n64.c
[src/mess/drivers]n64.c

src/mame/machine/n64.c
r14462r14463
102102   si_pif_addr = 0;
103103   si_status = 0;
104104
105   memset(eeprom, 0, sizeof(eeprom));
106   memset(mempack, 0, sizeof(mempack));
105   memset(m_save_data.eeprom, 0, 2048);
107106
108107   dp_clock = 0;
109108
r14462r14463
12391238   if(cart_addr & 0x04000000)
12401239   {
12411240      cart16 = (UINT16*)n64_sram;
1242      cart_addr = (pi_cart_addr & 0x00007fff) >> 1;
1241      cart_addr = (pi_cart_addr & 0x0001ffff) >> 1;
12431242   }
12441243
12451244    cart_addr &= ((machine().region("user2")->bytes() >> 1) - 1);
r14462r14463
14601459}
14611460
14621461// Serial Interface
1463UINT8 n64_periphs::calc_mempack_crc(UINT8 *buffer, int length)
1462UINT8 n64_periphs::calc_mempak_crc(UINT8 *buffer, int length)
14641463{
14651464   UINT32 crc = 0;
14661465   UINT32 temp2 = 0;
r14462r14463
14991498   return crc;
15001499}
15011500
1501INLINE UINT8 convert_to_bcd(int val)
1502{
1503   return ((val / 10) << 4) | (val % 10);
1504}
1505
15021506int n64_periphs::pif_channel_handle_command(int channel, int slength, UINT8 *sdata, int rlength, UINT8 *rdata)
15031507{
15041508   UINT8 command = sdata[0];
r14462r14463
15121516            case 0:
15131517            case 1:
15141518            {
1519               printf("Read controller %d status\n", channel + 1);
15151520               rdata[0] = 0x05;
15161521               rdata[1] = 0x00;
1517               rdata[2] = 0x02;
1522               rdata[2] = 0x01;
15181523               return 0;
15191524            }
15201525            case 2:
15211526            case 3:
15221527            {
1528               printf("Read controller %d status (NC)\n", channel + 1);
15231529               // not connected
15241530               return 1;
15251531            }
15261532            case 4:
15271533            {
1534               printf("Read EEPROM status\n");
15281535               rdata[0] = 0x00;
1529               rdata[1] = 0x80;
1536               rdata[1] = 0xc0;
15301537               rdata[2] = 0x00;
15311538
15321539               return 1;
r14462r14463
15581565            case 0: //p1 inputs
15591566            case 1: //p2 inputs
15601567            {
1568               printf("Read p%d inputs\n", channel + 1);
15611569                    buttons = input_port_read(machine(), portnames[(channel*3) + 0]);
15621570                    x = input_port_read(machine(), portnames[(channel*3) + 1]) - 128;
15631571                    y = input_port_read(machine(), portnames[(channel*3) + 2]) - 128;
r14462r14463
15711579            case 2:
15721580            case 3:
15731581            {
1582               printf("Controller %d not connected\n", channel + 1);
15741583               // not connected
15751584               return 1;
15761585            }
r14462r14463
15791588         break;
15801589      }
15811590
1582      case 0x02:
1591      case 0x02: // Read mempak
15831592      {
15841593         UINT32 address;
15851594
15861595         address = (sdata[1] << 8) | (sdata[2]);
15871596         address &= ~0x1f;
15881597
1589         if(address == 0x400)
1598         printf("Read mempak at %04x\n", address);
1599
1600         if(address == 0x8000)
15901601         {
15911602            for(int i = 0; i < rlength-1; i++)
15921603            {
15931604               rdata[i] = 0x00;
15941605            }
15951606
1596            rdata[rlength-1] = calc_mempack_crc(rdata, rlength-1);
1607            rdata[rlength-1] = calc_mempak_crc(rdata, rlength-1);
1608            return 0;
15971609         }
15981610         else if(address < 0x7fe0)
15991611         {
16001612            for(int i = 0; i < rlength-1; i++)
16011613            {
1602               rdata[i] = mempack[address+i];
1614               rdata[i] = m_save_data.mempak[channel & 1][address+i];
16031615            }
16041616
1605            rdata[rlength-1] = calc_mempack_crc(rdata, rlength-1);
1617            rdata[rlength-1] = calc_mempak_crc(rdata, rlength-1);
1618            return 0;
16061619         }
1620
16071621         return 1;
16081622      }
1609      case 0x03:
1623      case 0x03: // Write mempak
16101624      {
16111625         UINT32 address = (sdata[1] << 8) | (sdata[2]);
16121626         address &= ~0x1f;
16131627
1614         if (address == 0x8000)
1628         printf("Write mempak at %04x\n", address);
1629         if (address >= 0x8000)
16151630         {
16161631
16171632         }
r14462r14463
16191634         {
16201635            for(int i = 3; i < slength; i++)
16211636            {
1622               mempack[address++] = sdata[i];
1637               m_save_data.mempak[channel & 1][address++] = sdata[i];
16231638            }
16241639         }
16251640
1626         rdata[0] = calc_mempack_crc(&sdata[3], slength-3);
1641         rdata[0] = calc_mempak_crc(&sdata[3], slength-3);
16271642
1628         return 1;
1643         return 0;
16291644      }
16301645
16311646      case 0x04:      // Read from EEPROM
r14462r14463
16401655            fatalerror("handle_pif: write EEPROM (bytes to send %d, bytes to receive %d)\n", slength, rlength);
16411656         }
16421657
1643         UINT8 block_offset = sdata[1] * 8;
1658         UINT16 block_offset = sdata[1] * 8;
16441659
1660         printf("Read EEPROM at %04x\n", block_offset);
1661
16451662         for(int i=0; i < 8; i++)
16461663         {
1647            rdata[i] = eeprom[block_offset+i];
1664            rdata[i] = m_save_data.eeprom[block_offset+i];
16481665         }
16491666
1650         return 1;
1667         return 0;
16511668      }
16521669
16531670      case 0x05:      // Write to EEPROM
r14462r14463
16621679            fatalerror("handle_pif: write EEPROM (bytes to send %d, bytes to receive %d)\n", slength, rlength);
16631680         }
16641681
1665         UINT8 block_offset = sdata[1] * 8;
1682         UINT16 block_offset = sdata[1] * 8;
1683
1684         printf("Write EEPROM at %04x\n", block_offset);
1685
16661686         for(int i = 0; i < 8; i++)
16671687         {
1668            eeprom[block_offset+i] = sdata[2+i];
1688            m_save_data.eeprom[block_offset+i] = sdata[2+i];
16691689         }
16701690
1691         return 0;
1692      }
1693
1694      case 0x06:      // Read RTC Status
1695      {
1696         printf("Read RTC Status\n");
1697         rdata[0] = 0x00;
1698         rdata[1] = 0x10;
1699         rdata[2] = 0x00;
1700         return 0;
1701      }
1702
1703      case 0x07:      // Read RTC Block
1704      {
1705         switch(sdata[1])
1706         {
1707            case 0:
1708               printf("Read RTC Block Header\n");
1709               rdata[0] = 0x00;
1710               rdata[1] = 0x02;
1711               rdata[8] = 0x00;
1712               return 0;
1713
1714            case 1:
1715               return 0;
1716
1717            case 2:
1718               system_time systime;
1719               machine().base_datetime(systime);
1720               rdata[0] = convert_to_bcd(systime.local_time.second); // Seconds
1721               rdata[1] = convert_to_bcd(systime.local_time.minute); // Minutes
1722               rdata[2] = 0x80 | convert_to_bcd(systime.local_time.hour); // Hours
1723               rdata[3] = convert_to_bcd(systime.local_time.mday); // Day of month
1724               rdata[4] = convert_to_bcd(systime.local_time.weekday); // Day of week
1725               rdata[5] = convert_to_bcd(systime.local_time.month + 1); // Month
1726               rdata[6] = convert_to_bcd(systime.local_time.year % 100); // Year
1727               rdata[7] = convert_to_bcd(systime.local_time.year / 100); // Century
1728               rdata[8] = 0x00;
1729               printf("Read RTC Time\n");
1730               return 0;
1731         }
16711732         return 1;
16721733      }
16731734
16741735      case 0xff:      // reset
16751736      {
1737         printf("Reset\n");
16761738         rdata[0] = 0xff;
16771739         rdata[1] = 0xff;
16781740         rdata[2] = 0xff;
r14462r14463
16911753
16921754void n64_periphs::handle_pif()
16931755{
1756   printf("Before:\n");
1757   for(int i = 0; i < 0x40; i++)
1758   {
1759      printf("%02x ", pif_cmd[i]);
1760      if((i & 0xf) == 0xf)
1761      {
1762         printf("\n");
1763      }
1764   }
16941765   if(pif_cmd[0x3f] == 0x1)      // only handle the command if the last byte is 1
16951766   {
16961767      int channel = 0;
r14462r14463
16991770
17001771      while(cmd_ptr < 0x3f && !end)
17011772      {
1702         UINT8 bytes_to_send = pif_cmd[cmd_ptr++];
1773         INT8 bytes_to_send = (INT8)pif_cmd[cmd_ptr++];
1774         printf("bytes to send: 0x%02x\n", bytes_to_send);
17031775
1704         if (bytes_to_send == 0xfe)
1776         if (bytes_to_send == -2)
17051777         {
17061778            end = 1;
1779            printf("end\n");
17071780         }
1708         else if (bytes_to_send == 0xff)
1781         else if (bytes_to_send < 0)
17091782         {
1783            printf("do nothing\n");
17101784            // do nothing
17111785         }
17121786         else
r14462r14463
17161790               UINT8 recv_buffer[0x40];
17171791               UINT8 send_buffer[0x40];
17181792
1719               UINT8 bytes_to_recv = pif_cmd[cmd_ptr++];
1793               INT8 bytes_to_recv = pif_cmd[cmd_ptr++];
1794               printf("bytes to receive: 0x%02x\n", bytes_to_recv);
17201795
1796               if (bytes_to_recv == -2)
1797               {
1798                  break; // Hack, shouldn't need to do this
1799               }
1800
17211801               for(int j = 0; j < bytes_to_send; j++)
17221802               {
17231803                  send_buffer[j] = pif_cmd[cmd_ptr++];
17241804               }
17251805
17261806                    int res = pif_channel_handle_command(channel, bytes_to_send, send_buffer, bytes_to_recv, recv_buffer);
1807               printf("result: %d\n", res);
17271808
17281809               if (res == 0)
17291810               {
1811                  printf("cmd_ptr (%d) + bytes_to_recv (%d) = %d\n", cmd_ptr, bytes_to_recv, cmd_ptr + bytes_to_recv);
17301812                  if (cmd_ptr + bytes_to_recv > 0x3f)
17311813                  {
17321814                     fatalerror("cmd_ptr overflow\n");
r14462r14463
17491831
17501832      pif_ram[0x3f] = 0;
17511833   }
1834
1835   printf("After:\n");
1836   for(int i = 0; i < 0x40; i++)
1837   {
1838      printf("%02x ", pif_ram[i]);
1839      if((i & 0xf) == 0xf)
1840      {
1841         printf("\n");
1842      }
1843   }
17521844}
17531845
17541846void n64_periphs::pif_dma(int direction)
r14462r14463
17991891
18001892READ32_MEMBER( n64_periphs::si_reg_r )
18011893{
1802   //printf("si_reg_r %08x\n", offset * 4);
1894   printf("si_reg_r %08x\n", offset * 4);
18031895   switch (offset)
18041896   {
18051897      //case 0x00/4:      // SI_DRAM_ADDR_REG
r14462r14463
18131905
18141906WRITE32_MEMBER( n64_periphs::si_reg_w )
18151907{
1816   //printf("si_reg_w %08x %08x %08x\n", offset * 4, data, mem_mask);
1908   printf("si_reg_w %08x %08x %08x\n", offset * 4, data, mem_mask);
18171909   switch (offset)
18181910   {
18191911      case 0x00/4:      // SI_DRAM_ADDR_REG
r14462r14463
18831975    signal_rcp_interrupt(SI_INTERRUPT);
18841976}
18851977
1886//static UINT16 crc_seed = 0x3f;
1978static void n64_machine_stop(running_machine &machine)
1979{
1980   n64_periphs *periphs = machine.device<n64_periphs>("rcp");
18871981
1982   device_image_interface *image = dynamic_cast<device_image_interface *>(periphs->m_nvram_image);
1983.   //printf("Saving stuff\n");
1984   UINT8 data[0x30800];
1985   memcpy(data, n64_sram, 0x20000);
1986   memcpy(data + 0x20000, periphs->m_save_data.eeprom, 0x800);
1987   memcpy(data + 0x20800, periphs->m_save_data.mempak[0], 0x8000);
1988   memcpy(data + 0x28800, periphs->m_save_data.mempak[1], 0x8000);
1989   image->battery_save(data, 0x30800);
1990}
1991
18881992MACHINE_START( n64 )
18891993{
1890   mips3drc_set_options(machine.device("maincpu"), MIPS3DRC_FASTEST_OPTIONS + MIPS3DRC_STRICT_VERIFY + MIPS3DRC_STRICT_COP1);
1994   mips3drc_set_options(machine.device("maincpu"), MIPS3DRC_COMPATIBLE_OPTIONS);
18911995
18921996   /* configure fast RAM regions for DRC */
18931997   mips3drc_add_fastram(machine.device("maincpu"), 0x00000000, 0x007fffff, FALSE, rdram);
r14462r14463
18962000   rspdrc_flush_drc_cache(machine.device("rsp"));
18972001   rspdrc_add_dmem(machine.device("rsp"), rsp_dmem);
18982002   rspdrc_add_imem(machine.device("rsp"), rsp_imem);
2003
2004   /* add a hook for battery save */
2005   machine.add_notifier(MACHINE_NOTIFY_EXIT, machine_notify_delegate(FUNC(n64_machine_stop),&machine));
18992006}
19002007
19012008MACHINE_RESET( n64 )
src/mame/includes/n64.h
r14462r14463
2727
2828#define AUDIO_DMA_DEPTH     2
2929
30struct n64_savable_data_t
31{
32   UINT8 sram[0x20000];
33   UINT8 eeprom[2048];
34   UINT8 mempak[2][0x8000];
35};
36
3037class n64_periphs : public device_t
3138{
3239private:
r14462r14463
8794   UINT32 vi_intr;
8895   UINT32 vi_vburst;
8996
97   /* nvram-specific for MESS */
98   device_t *m_nvram_image;
99
100   n64_savable_data_t m_save_data;
101
90102protected:
91103    // device-level overrides
92104    virtual void device_start();
r14462r14463
169181   void pif_dma(int direction);
170182   void handle_pif();
171183   int pif_channel_handle_command(int channel, int slength, UINT8 *sdata, int rlength, UINT8 *rdata);
172   UINT8 calc_mempack_crc(UINT8 *buffer, int length);
184   UINT8 calc_mempak_crc(UINT8 *buffer, int length);
173185   UINT8 pif_ram[0x40];
174186   UINT8 pif_cmd[0x40];
175187   UINT32 si_dram_addr;
r14462r14463
177189   UINT32 si_pif_addr_rd64b;
178190   UINT32 si_pif_addr_wr64b;
179191   UINT32 si_status;
180   UINT8 eeprom[512];
181   UINT8 mempack[0x8000];
182192   UINT32 cic_status;
183193
194   n64_savable_data_t savable_data;
195
184196   // Video Interface (VI) functions
185197   void vi_recalculate_resolution();
186198};
src/mess/drivers/n64.c
r14462r14463
2828   AM_RANGE(0x04600000, 0x046fffff) AM_DEVREADWRITE_MODERN("rcp", n64_periphs, pi_reg_r, pi_reg_w)   // Peripheral Interface
2929   AM_RANGE(0x04700000, 0x047fffff) AM_DEVREADWRITE_MODERN("rcp", n64_periphs, ri_reg_r, ri_reg_w)   // RDRAM Interface
3030   AM_RANGE(0x04800000, 0x048fffff) AM_DEVREADWRITE_MODERN("rcp", n64_periphs, si_reg_r, si_reg_w)   // Serial Interface
31   AM_RANGE(0x08000000, 0x08007fff) AM_RAM AM_BASE(&n64_sram)      // Cartridge SRAM
31   AM_RANGE(0x08000000, 0x0801ffff) AM_RAM AM_BASE(&n64_sram)      // Cartridge SRAM
3232   AM_RANGE(0x10000000, 0x13ffffff) AM_ROM AM_REGION("user2", 0)   // Cartridge
3333   AM_RANGE(0x1fc00000, 0x1fc007bf) AM_ROM AM_REGION("user1", 0)   // PIF ROM
3434   AM_RANGE(0x1fc007c0, 0x1fc007ff) AM_DEVREADWRITE_MODERN("rcp", n64_periphs, pif_ram_r, pif_ram_w)
r14462r14463
9898   62500000         /* system clock */
9999};
100100
101static void mempak_format(UINT8* pak)
102{
103   unsigned char pak_header[] =
104   {
105      0x81,0x01,0x02,0x03, 0x04,0x05,0x06,0x07, 0x08,0x09,0x0a,0x0b, 0x0c,0x0d,0x0e,0x0f,
106      0x10,0x11,0x12,0x13, 0x14,0x15,0x16,0x17, 0x18,0x19,0x1a,0x1b, 0x1c,0x1d,0x1e,0x1f,
107      0xff,0xff,0xff,0xff, 0x05,0x1a,0x5f,0x13, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
108      0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0x01,0xff, 0x66,0x25,0x99,0xcd,
109      0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
110      0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
111      0xff,0xff,0xff,0xff, 0x05,0x1a,0x5f,0x13, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
112      0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0x01,0xff, 0x66,0x25,0x99,0xcd,
113      0xff,0xff,0xff,0xff, 0x05,0x1a,0x5f,0x13, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
114      0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0x01,0xff, 0x66,0x25,0x99,0xcd,
115      0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
116      0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
117      0xff,0xff,0xff,0xff, 0x05,0x1a,0x5f,0x13, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
118      0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0x01,0xff, 0x66,0x25,0x99,0xcd,
119      0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
120      0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
121      0x00,0x71,0x00,0x03, 0x00,0x03,0x00,0x03, 0x00,0x03,0x00,0x03, 0x00,0x03,0x00,0x03
122   };
123
124   for (int i = 0; i < 0x8000; i += 2)
125   {
126      pak[i+0] = 0x00;
127      pak[i+1] = 0x03;
128   }
129    memcpy(pak, pak_header, 272);
130}
131
101132static DEVICE_IMAGE_LOAD(n64_cart)
102133{
103134   int i, length;
135   n64_periphs *periphs = image.device().machine().device<n64_periphs>("rcp");
104136   UINT8 *cart = image.device().machine().region("user2")->base();
105137
106138   if (image.software_entry() == NULL)
r14462r14463
142174      }
143175   }
144176
177   periphs->m_nvram_image = image;
178
145179   logerror("cart length = %d\n", length);
180
181   device_image_interface *battery_image = dynamic_cast<device_image_interface *>(periphs->m_nvram_image);
182   if(battery_image)
183   {
184      printf("Loading\n");
185      UINT8 data[0x30800];
186      battery_image->battery_load(data, 0x30800, 0x00);
187      memcpy(n64_sram, data, 0x20000);
188      memcpy(periphs->m_save_data.eeprom, data + 0x20000, 0x800);
189      memcpy(periphs->m_save_data.mempak[0], data + 0x20800, 0x8000);
190      memcpy(periphs->m_save_data.mempak[1], data + 0x28800, 0x8000);
191   }
192   else
193   {
194      memset(periphs->m_save_data.eeprom, 0, 0x800);
195      mempak_format(periphs->m_save_data.mempak[0]);
196      mempak_format(periphs->m_save_data.mempak[1]);
197   }
146198   return IMAGE_INIT_PASS;
147199}
148200

Previous 15994 Revisions Next


© 1998-2018 The MESS Team