NESCarts.c 3.8 KB

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