NESCarts.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Cart manager - The peTI-NESulator Project
  3. * NESCart.c
  4. *
  5. * Created by Manoel TRAPIER.
  6. * Copyright (c) 2003-2018 986-Studio. All rights reserved.
  7. *
  8. * $LastChangedDate$
  9. * $Author$
  10. * $HeadURL$
  11. * $Revision$
  12. *
  13. */
  14. /* System Headers */
  15. #if !defined(__TIGCC__) && !defined(__GCC4TI__) && !defined(__GTC__)
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <memory.h>
  19. #else /* Support for TI-68k compilation */
  20. #define TIGCC_COMPAT
  21. #include <tigcclib.h>
  22. #endif
  23. /* peTI-NESulator headers */
  24. #include <os_dependent.h>
  25. #include <NESCarts.h>
  26. #include <os_dependent.h>
  27. #include <mappers/manager.h>
  28. #include <sys/mman.h>
  29. void DumpCartProperties(FILE *out, NesCart * cart)
  30. {
  31. console_printf(Console_Verbose,
  32. "'%s' informations:\n"
  33. " Total ROM Size : 0x%06lX | Total VROM Size : 0x%06lX\n"
  34. " Mapper ID : 0x%06X | Mirroring ? : %s\n"
  35. " Battery ? : %s | 4 Screen ? : %s \n"
  36. " PROMBanks start at : %p |\n"
  37. " VROMBanks start at : %p |\n",
  38. cart->FileName,
  39. cart->PROMSize,
  40. cart->VROMSize,
  41. cart->MapperID,
  42. cart->Flags & iNES_MIRROR? "Horizontal" : "Vertical",
  43. cart->Flags & iNES_BATTERY? "Yes": "No ",
  44. cart->Flags & iNES_4SCREEN? "Yes": "No ",
  45. cart->PROMBanks,
  46. cart->VROMBanks);
  47. }
  48. int LoadCart(const char *filename, NesCart * cart)
  49. {
  50. char buffer[6];
  51. /* Load the cart into memory */
  52. cart->File = (byte *)LoadFilePtr((char *)filename);
  53. if ((cart->File == NULL) || (cart->File == MAP_FAILED))
  54. return -1;
  55. sprintf(buffer, "%c%c%c%c", 0x4E, 0x45, 0x53, 0x1A);
  56. /* Verify that this is a real iNES valid file */
  57. if (memcmp(cart->File, buffer, 4))
  58. return -1;
  59. /* Before go elsewhere, verify that the header is clean !
  60. (aka no DiskDude! in it) */
  61. if (memcmp(cart->File+7, "DiskDude!", 9) == 0)
  62. {
  63. console_printf(Console_Warning, "\n"
  64. "*******************WARNING****************\n"
  65. "* The header of this game is not clean *\n"
  66. "* (DiskDude! pollution) I will only use *\n"
  67. "* basic MapperID (mapper 0-15). This can *\n"
  68. "* led to unexpected behavior... *\n"
  69. "* *\n"
  70. "* PLEASE CLEAN THIS FILE! *\n"
  71. "******************WARNING*****************\n\n");
  72. /* So this rom file is not clean, we can only rely on the "basic" mapperID */
  73. cart->MapperID = cart->File[6]>>4; /* Mapper Type */
  74. }
  75. else
  76. { /* This rom file is clean, we can read the extended MapperID */
  77. cart->MapperID = (cart->File[6]>>4)|(cart->File[7]&0xF0); /* Mapper Type */
  78. }
  79. /* Now fill the structure */
  80. cart->FileName = (char *)filename;
  81. cart->PROMSize = cart->File[4] * 16 * 1024; /* Size of PROM */
  82. cart->VROMSize = cart->File[5] * 8 * 1024; /* Size of VROM */
  83. cart->Flags = cart->File[6] & 0x0F;
  84. /* We don't and we will never support trainer-ed ROM */
  85. if (cart->Flags & iNES_TRAINER)
  86. {
  87. console_printf(Console_Error, "\n"
  88. "********************ERROR*****************\n"
  89. "* This cart have an embedded trainer. *\n"
  90. "* There is NO support for them. *\n"
  91. "* Please use a CLEAN dump if you want *\n"
  92. "* to play this game. *\n"
  93. "********************ERROR*****************\n\n");
  94. return -1;
  95. }
  96. cart->PROMBanks = cart->File + 16; /* Pointer on the first PROM */
  97. cart->VROMBanks = cart->PROMBanks + cart->PROMSize; /* Pointer on the first VROM */
  98. DumpCartProperties(stdout, cart);
  99. return 0;
  100. }