NESCarts.c 4.0 KB

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