Quote Originally Posted by tonia6970 View Post
If i have some nes rom game, then how Can i see its source code ?
You have to understand what the contents of an NES rom is and how to code in 6502 assembly (all 56 opcodes and 13 address modes, not including illegal opcodes ) and know the hexadecimal numbers of the instructions are to know the source code. The rom contains more than just code, but also the chr graphics file. When you read a rom, the first thing you will see is 'N' 'E' 'S' and the hexadecimal '1A'. The next value is how many PRGROM pages there are (the source code). The next value is how many CHRROM pages are there (the graphics). The value after that is known as the ROM_Control_Byte_1. The value after that is known as ROM_Control_Byte_2. Using this formula:

Code:
nes_rom.Mapper = (ROM_Control_Byte_1 >> 4) + (ROM_Control_Byte_2 & 0xf0);
it determines what mapper number the rom is. And there is lots of em. 256 available but actually less than that because some mapper numbers are unused or are death mappers. You can see it here: https://wiki.nesdev.com/w/index.php/Mapper

ROM_Control_Byte_1 also determines what kind of screen mirroring. Is it horizontal vertical, single screen, or four screen? And determines if there is a battery save or trainer is present. If there is one, its gonna determine where in the rom the source code is found. I'll show you a small clip of code from my C++ version of my NES emulator to show you what I mean:

C++ Code:
  1. if (nes_rom.Trainer_Present)
  2. {
  3.     NES_File.seekg(16);
  4.     NES_File.read((char *)Train, 0x200);
  5.     NES_File.seekg(528);
  6.     NES_File.read(reinterpret_cast<char *>(nes_rom.PRG_ROM.data()), nes_rom.PRG_ROM.size());
  7.     NES_File.seekg((nes_rom.PRG_ROM_Pages * 0x4000) + 528);
  8.     NES_File.read(reinterpret_cast<char *>(nes_rom.CHR_ROM.data()), nes_rom.CHR_ROM.size());
  9. }
  10. else
  11. {
  12.     NES_File.seekg(16);
  13.     NES_File.read(reinterpret_cast<char *>(nes_rom.PRG_ROM.data()), nes_rom.PRG_ROM.size());
  14.     NES_File.seekg((nes_rom.PRG_ROM_Pages * 0x4000) + 16);
  15.     NES_File.read(reinterpret_cast<char *>(nes_rom.CHR_ROM.data()), nes_rom.CHR_ROM.size());
  16. }

So nes_rom.PRG_ROM_Pages * 0x4000 plus the offset shows you where the source code to your rom is located as well as how many pages. So if there is one page, theres 0x4000 lines of code (16,384 in standard decimal format) times the number of pages. at either 16 or 528 depending if there is a trainer present.

As for the code, have fun comparing each hexadecimal you come across with these

C++ Code:
  1. //////////////////////
  2.     // Constants:
  3.     //////////////////////
  4.     // ADC: 0x69; 0x65; 0x75; 0x6d; 0x7d; 0x79; 0x61; 0x71
  5.     static const byte ADC_IMMEDIATE = 0x69;
  6.     static const byte ADC_ZERO_PAGE = 0x65;
  7.     static const byte ADC_ZERO_PAGE_X = 0x75;
  8.     static const byte ADC_ABSOLUTE = 0x6d;
  9.     static const byte ADC_ABSOLUTE_X = 0x7d;
  10.     static const byte ADC_ABSOLUTE_Y = 0x79;
  11.     static const byte ADC_INDIRECT_X = 0x61;
  12.     static const byte ADC_INDIRECT_Y = 0x71;
  13.  
  14.     // AND: 0x29; 0x25; 0x35; 0x2d; 0x3d; 0x39; 0x21; 0x31
  15.     static const byte AND_IMMEDIATE = 0x29;
  16.     static const byte AND_ZERO_PAGE = 0x25;
  17.     static const byte AND_ZERO_PAGE_X = 0x35;
  18.     static const byte AND_ABSOLUTE = 0x2d;
  19.     static const byte AND_ABSOLUTE_X = 0x3d;
  20.     static const byte AND_ABSOLUTE_Y = 0x39;
  21.     static const byte AND_INDIRECT_X = 0x21;
  22.     static const byte AND_INDIRECT_Y = 0x31;
  23.  
  24.     // ASL: 0x0a; 0x06; 0x16; 0x0e; 0x1e
  25.     static const byte ASL_ACCUMULATOR = 0x0a;
  26.     static const byte ASL_ZERO_PAGE = 0x06;
  27.     static const byte ASL_ZERO_PAGE_X = 0x16;
  28.     static const byte ASL_ABSOLUTE = 0x0e;
  29.     static const byte ASL_ABSOLUTE_X = 0x1e;
  30.  
  31.     // BCC: 0x90
  32.     static const byte BCC_RELATIVE = 0x90;
  33.  
  34.     // BCS: 0xb0
  35.     static const byte BCS_RELATIVE = 0xb0;
  36.  
  37.     // BEQ: 0xf0
  38.     static const byte BEQ_RELATIVE = 0xf0;
  39.  
  40.     // BIT: 0x24; 0x2c
  41.     static const byte BIT_ZERO_PAGE = 0x24;
  42.     static const byte BIT_ABSOLUTE = 0x2c;
  43.  
  44.     // BMI: 0x30
  45.     static const byte BMI_RELATIVE = 0x30;
  46.  
  47.     // BNE: 0xd0
  48.     static const byte BNE_RELATIVE = 0xd0;
  49.  
  50.     // BPL: 0x10
  51.     static const byte BPL_RELATIVE = 0x10;
  52.  
  53.     // BRK: 0x00
  54.     static const byte BRK_IMPLIED = 0x00;
  55.  
  56.     // BVC: 0x50
  57.     static const byte BVC_RELATIVE = 0x50;
  58.  
  59.     // BVS: 0x70
  60.     static const byte BVS_RELATIVE = 0x70;
  61.  
  62.     // CLC: 0x18
  63.     static const byte CLC_IMPLIED = 0x18;
  64.  
  65.     // CLD: 0xd8
  66.     static const byte CLD_IMPLIED = 0xd8;
  67.  
  68.     // CLI: 0x58
  69.     static const byte CLI_IMPLIED = 0x58;
  70.  
  71.     // CLV: 0xb8
  72.     static const byte CLV_IMPLIED = 0xb8;
  73.  
  74.     // CMP: 0xc9; 0xc5; 0xd5; 0xcd; 0xdd; 0xd9; 0xc1; 0xd1
  75.     static const byte CMP_IMMEDIATE = 0xc9;
  76.     static const byte CMP_ZERO_PAGE = 0xc5;
  77.     static const byte CMP_ZERO_PAGE_X = 0xd5;
  78.     static const byte CMP_ABSOLUTE = 0xcd;
  79.     static const byte CMP_ABSOLUTE_X = 0xdd;
  80.     static const byte CMP_ABSOLUTE_Y = 0xd9;
  81.     static const byte CMP_INDIRECT_X = 0xc1;
  82.     static const byte CMP_INDIRECT_Y = 0xd1;
  83.  
  84.     // CPX: 0xe0; 0xe4; 0xec
  85.     static const byte CPX_IMMEDIATE = 0xe0;
  86.     static const byte CPX_ZERO_PAGE = 0xe4;
  87.     static const byte CPX_ABSOLUTE = 0xec;
  88.  
  89.     // CPY: 0xc0; 0xc4; 0xcc
  90.     static const byte CPY_IMMEDIATE = 0xc0;
  91.     static const byte CPY_ZERO_PAGE = 0xc4;
  92.     static const byte CPY_ABSOLUTE = 0xcc;
  93.  
  94.     // DEC: 0xc6; 0xd6; 0xce; 0xde
  95.     static const byte DEC_ZERO_PAGE = 0xc6;
  96.     static const byte DEC_ZERO_PAGE_X = 0xd6;
  97.     static const byte DEC_ABSOLUTE = 0xce;
  98.     static const byte DEC_ABSOLUTE_X = 0xde;
  99.  
  100.     // DEX: 0xca;
  101.     static const byte DEX_IMPLIED = 0xca;
  102.  
  103.     // DEY: 0x88;
  104.     static const byte DEY_IMPLIED = 0x88;
  105.  
  106.     // EOR: 0x49; 0x45; 0x55; 0x4d; 0x5d; 0x59; 0x41; 0x51
  107.     static const byte EOR_IMMEDIATE = 0x49;
  108.     static const byte EOR_ZERO_PAGE = 0x45;
  109.     static const byte EOR_ZERO_PAGE_X = 0x55;
  110.     static const byte EOR_ABSOLUTE = 0x4d;
  111.     static const byte EOR_ABSOLUTE_X = 0x5d;
  112.     static const byte EOR_ABSOLUTE_Y = 0x59;
  113.     static const byte EOR_INDIRECT_X = 0x41;
  114.     static const byte EOR_INDIRECT_Y = 0x51;
  115.  
  116.     // INC: 0xe6; 0xf6; 0xee; 0xfe
  117.     static const byte INC_ZERO_PAGE = 0xe6;
  118.     static const byte INC_ZERO_PAGE_X = 0xf6;
  119.     static const byte INC_ABSOLUTE = 0xee;
  120.     static const byte INC_ABSOLUTE_X = 0xfe;
  121.  
  122.     // INX: 0xe8
  123.     static const byte INX_IMPLIED = 0xe8;
  124.  
  125.     // INY: 0xc8
  126.     static const byte INY_IMPLIED = 0xc8;
  127.  
  128.     // JMP: 0x4c; 0x6c
  129.     static const byte JMP_ABSOLUTE = 0x4c;
  130.     static const byte JMP_INDIRECT = 0x6c;
  131.  
  132.     // JSR: 0x20
  133.     static const byte JSR_ABSOLUTE = 0x20;
  134.  
  135.     // LDA: 0xa9; 0xa5; 0xb5; 0xad; 0xbd; 0xb9; 0xa1; 0xb1
  136.     static const byte LDA_IMMEDIATE = 0xa9;
  137.     static const byte LDA_ZERO_PAGE = 0xa5;
  138.     static const byte LDA_ZERO_PAGE_X = 0xb5;
  139.     static const byte LDA_ABSOLUTE = 0xad;
  140.     static const byte LDA_ABSOLUTE_X = 0xbd;
  141.     static const byte LDA_ABSOLUTE_Y = 0xb9;
  142.     static const byte LDA_INDIRECT_X = 0xa1;
  143.     static const byte LDA_INDIRECT_Y = 0xb1;
  144.  
  145.     // LDX: 0xa2; 0xa6; 0xb6; 0xae; 0xbe
  146.     static const byte LDX_IMMEDIATE = 0xa2;
  147.     static const byte LDX_ZERO_PAGE = 0xa6;
  148.     static const byte LDX_ZERO_PAGE_Y = 0xb6;
  149.     static const byte LDX_ABSOLUTE = 0xae;
  150.     static const byte LDX_ABSOLUTE_Y = 0xbe;
  151.  
  152.     // LDY: 0xa0; 0xa4; 0xb4; 0xac; 0xbc
  153.     static const byte LDY_IMMEDIATE = 0xa0;
  154.     static const byte LDY_ZERO_PAGE = 0xa4;
  155.     static const byte LDY_ZERO_PAGE_X = 0xb4;
  156.     static const byte LDY_ABSOLUTE = 0xac;
  157.     static const byte LDY_ABSOLUTE_X = 0xbc;
  158.  
  159.     // LSR: 0x4a; 0x46; 0x56; 0x4e; 0x5e
  160.     static const byte LSR_ACCUMULATOR = 0x4a;
  161.     static const byte LSR_ZERO_PAGE = 0x46;
  162.     static const byte LSR_ZERO_PAGE_X = 0x56;
  163.     static const byte LSR_ABSOLUTE = 0x4e;
  164.     static const byte LSR_ABSOLUTE_X = 0x5e;
  165.  
  166.     // NOP: 0xea
  167.     static const byte NOP_IMPLIED = 0xea;
  168.  
  169.     // ORA: 0x09; 0x05; 0x15; 0x0d; 0x1d; 0x19; 0x01; 0x11
  170.     static const byte ORA_IMMEDIATE = 0x09;
  171.     static const byte ORA_ZERO_PAGE = 0x05;
  172.     static const byte ORA_ZERO_PAGE_X = 0x15;
  173.     static const byte ORA_ABSOLUTE = 0x0d;
  174.     static const byte ORA_ABSOLUTE_X = 0x1d;
  175.     static const byte ORA_ABSOLUTE_Y = 0x19;
  176.     static const byte ORA_INDIRECT_X = 0x01;
  177.     static const byte ORA_INDIRECT_Y = 0x11;
  178.  
  179.     // PHA: 0x48
  180.     static const byte PHA_IMPLIED = 0x48;
  181.  
  182.     // PHP: 0x08
  183.     static const byte PHP_IMPLIED = 0x08;
  184.  
  185.     // PLA: 0x68
  186.     static const byte PLA_IMPLIED = 0x68;
  187.  
  188.     // PLP: 0x28
  189.     static const byte PLP_IMPLIED = 0x28;
  190.  
  191.     // ROL: 0x2a; 0x26; 0x36; 0x2e; 0x3e;
  192.     static const byte ROL_ACCUMULATOR = 0x2a;
  193.     static const byte ROL_ZERO_PAGE = 0x26;
  194.     static const byte ROL_ZERO_PAGE_X = 0x36;
  195.     static const byte ROL_ABSOLUTE = 0x2e;
  196.     static const byte ROL_ABSOLUTE_X = 0x3e;
  197.  
  198.     // ROR: 0x6a; 0x66; 0x76; 0x6e; 0x7e;
  199.     static const byte ROR_ACCUMULATOR = 0x6a;
  200.     static const byte ROR_ZERO_PAGE = 0x66;
  201.     static const byte ROR_ZERO_PAGE_X = 0x76;
  202.     static const byte ROR_ABSOLUTE = 0x6e;
  203.     static const byte ROR_ABSOLUTE_X = 0x7e;
  204.  
  205.     // RTI: 0x40
  206.     static const byte RTI_IMPLIED = 0x40;
  207.  
  208.     // RTS: 0x60
  209.     static const byte RTS_IMPLIED = 0x60;
  210.  
  211.     // SBC: 0xe9; 0xe5; 0xf5; 0xed; 0xfd; 0xf9; 0xe1; 0xf1
  212.     static const byte SBC_IMMEDIATE = 0xe9;
  213.     static const byte SBC_ZERO_PAGE = 0xe5;
  214.     static const byte SBC_ZERO_PAGE_X = 0xf5;
  215.     static const byte SBC_ABSOLUTE = 0xed;
  216.     static const byte SBC_ABSOLUTE_X = 0xfd;
  217.     static const byte SBC_ABSOLUTE_Y = 0xf9;
  218.     static const byte SBC_INDIRECT_X = 0xe1;
  219.     static const byte SBC_INDIRECT_Y = 0xf1;
  220.  
  221.     // SEC: 0x38
  222.     static const byte SEC_IMPLIED = 0x38;
  223.  
  224.     // SED: 0xf8
  225.     static const byte SED_IMPLIED = 0xf8;
  226.  
  227.     // SEI: 0x78
  228.     static const byte SEI_IMPLIED = 0x78;
  229.  
  230.     // STA: 0x85; 0x95; 0x8d; 0x9d; 0x99; 0x81; 0x91
  231.     static const byte STA_ZERO_PAGE = 0x85;
  232.     static const byte STA_ZERO_PAGE_X = 0x95;
  233.     static const byte STA_ABSOLUTE = 0x8d;
  234.     static const byte STA_ABSOLUTE_X = 0x9d;
  235.     static const byte STA_ABSOLUTE_Y = 0x99;
  236.     static const byte STA_INDIRECT_X = 0x81;
  237.     static const byte STA_INDIRECT_Y = 0x91;
  238.  
  239.     // STX: 0x86; 0x96; 0x8e
  240.     static const byte STX_ZERO_PAGE = 0x86;
  241.     static const byte STX_ZERO_PAGE_Y = 0x96;
  242.     static const byte STX_ABSOLUTE = 0x8e;
  243.  
  244.     // STY: 0x84; 0x94; 0x8c
  245.     static const byte STY_ZERO_PAGE = 0x84;
  246.     static const byte STY_ZERO_PAGE_X = 0x94;
  247.     static const byte STY_ABSOLUTE = 0x8c;
  248.  
  249.     // TAX: 0xaa
  250.     static const byte TAX_IMPLIED = 0xaa;
  251.  
  252.     // TAY: 0xa8
  253.     static const byte TAY_IMPLIED = 0xa8;
  254.  
  255.     // TSX: 0xba
  256.     static const byte TSX_IMPLIED = 0xba;
  257.  
  258.     // TXA: 0x8a
  259.     static const byte TXA_IMPLIED = 0x8a;
  260.  
  261.     // TXS: 0x9a
  262.     static const byte TXS_IMPLIED = 0x9a;
  263.  
  264.     // TYA: 0x98
  265.     static const byte TYA_IMPLIED = 0x98;