Skip to content

Instantly share code, notes, and snippets.

@infval
Last active January 30, 2022 20:01
Show Gist options
  • Save infval/24a7b56f661666453344b4c650e5ddd5 to your computer and use it in GitHub Desktop.
Save infval/24a7b56f661666453344b4c650e5ddd5 to your computer and use it in GitHub Desktop.
Get NES mapper
#include <stdio.h>
#include <stdint.h>
#define HEADER_SIZE 16
int IsNES20(uint8_t* header)
{
return (header[7] & 0x0C) == 0x08;
}
unsigned GetMapper(uint8_t* header)
{
unsigned mapper = 0;
mapper = (header[7] & 0xF0) | ((header[6] & 0xF0) >> 4);
if (IsNES20(header)) {
mapper |= (header[8] & 0x0F) << 8;
}
return mapper;
}
unsigned GetSubmapper(uint8_t* header)
{
unsigned submapper = 0xFF;
if (IsNES20(header)) {
submapper = (header[8] & 0xF0) >> 4;
}
return submapper;
}
int main(int argc, char* argv[])
{
if (argc != 2) {
return 1;
}
FILE* fp = NULL;
fp = fopen(argv[1], "rb");
if (fp == NULL) {
return 2;
}
uint8_t header[HEADER_SIZE] = {};
if (fread(header, sizeof(uint8_t), HEADER_SIZE, fp) != HEADER_SIZE) {
fclose(fp);
return 3;
}
fclose(fp);
printf("Mapper: %u", GetMapper(header));
if (IsNES20(header)) {
printf("\nSubmapper: %u", GetSubmapper(header));
}
return 0;
}
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
def get_mapper(header):
mapper = (header[7] & 0xF0) | ((header[6] & 0xF0) >> 4)
if (header[7] & 0x0C) == 0x08:
mapper |= (header[8] & 0x0F) << 8
return mapper
if __name__ == '__main__':
if len(sys.argv) > 1:
with open(sys.argv[1], "rb") as f:
print(get_mapper(f.read(16)))
input("Press any key...")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment