src/memory.c
bool gb_memory_init(gb_memory *mem, const char *filename)
{
    if (!filename) {
        mem->fd = -1;
        mem->mem = mmap((void *) 0x1000000, 0x10008, PROT_READ | PROT_WRITE,
                        MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
        if (mem->mem == MAP_FAILED) {
            LOG_ERROR("Map failed! (%i)\n", errno);
            return false;
        }
    } else {
        mem->fd = open(filename, O_RDONLY);
        if (mem->fd < 0) {
            LOG_ERROR("Could not open file! (%i)\n", errno);
            return false;
        }

        mem->mem = mmap((void *) 0x1000000, 0x8000, PROT_READ, MAP_PRIVATE,
                        mem->fd, 0);
        if (mem->mem == MAP_FAILED) {
            LOG_ERROR("Map failed! (%i)\n", errno);
            return false;
        }

        if (mmap(mem->mem + 0x8000, 0x8000, PROT_READ | PROT_WRITE,
                 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1,
                 0) == MAP_FAILED) {
            LOG_ERROR("Allocating memory failed! (%i)\n", errno);
            return false;
        }
    }

    mem->ram_banks = malloc(MAX_RAM_BANKS * 0x2000);

    mem->filename = filename;

    if (filename) {
        int len = strlen(filename) + 4;
        mem->savname = malloc(len * sizeof(char));
        if (mem->savname != NULL)
            snprintf(mem->savname, len, "%ssav", filename);
    } else {
        mem->savname = NULL;
    }

    mem->mbc = mem->mem[0x0147];
    mem->mbc_mode = 0;
    mem->mbc_data = 0;
    mem->current_rom_bank = 1;
    mem->current_ram_bank = 0;
    mem->rtc_access = false;

    return true;
}
/* initialize memory layout and map file filename */
initialize memory layout and map file filename
initialize memory layout and map file filename
bool gb_memory_init(gb_memory *mem, const char *filename)
{
    if (!filename) {
        mem->fd = -1;
        mem->mem = mmap((void *) 0x1000000, 0x10008, PROT_READ | PROT_WRITE,
                        MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
        if (mem->mem == MAP_FAILED) {
            LOG_ERROR("Map failed! (%i)\n", errno);
            return false;
        }
    } else {
        mem->fd = open(filename, O_RDONLY);
        if (mem->fd < 0) {
            LOG_ERROR("Could not open file! (%i)\n", errno);
            return false;
        }
        mem->mem = mmap((void *) 0x1000000, 0x8000, PROT_READ, MAP_PRIVATE,
                        mem->fd, 0);
        if (mem->mem == MAP_FAILED) {
            LOG_ERROR("Map failed! (%i)\n", errno);
            return false;
        }
        if (mmap(mem->mem + 0x8000, 0x8000, PROT_READ | PROT_WRITE,
                 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1,
                 0) == MAP_FAILED) {
            LOG_ERROR("Allocating memory failed! (%i)\n", errno);
            return false;
        }
    }
    mem->ram_banks = malloc(MAX_RAM_BANKS * 0x2000);
    mem->filename = filename;
    if (filename) {
        int len = strlen(filename) + 4;
        mem->savname = malloc(len * sizeof(char));
        if (mem->savname != NULL)
            snprintf(mem->savname, len, "%ssav", filename);
    } else {
        mem->savname = NULL;
    }
    mem->mbc = mem->mem[0x0147];
    mem->mbc_mode = 0;
    mem->mbc_data = 0;
    mem->current_rom_bank = 1;
    mem->current_ram_bank = 0;
    mem->rtc_access = false;
    return true;
}
bool gb_memory_init ( gb_memory * mem , const char * filename ) { if ( ! filename ) { mem -> fd = -1 ; mem -> mem = mmap ( ( void * ) 0x1000000 , 0x10008 , PROT_READ | PROT_WRITE , MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED , -1 , 0 ) ; if ( mem -> mem == MAP_FAILED ) { LOG_ERROR ( " \n " , errno ) ; return false ; } } else { mem -> fd = open ( filename , O_RDONLY ) ; if ( mem -> fd < 0 ) { LOG_ERROR ( " \n " , errno ) ; return false ; } mem -> mem = mmap ( ( void * ) 0x1000000 , 0x8000 , PROT_READ , MAP_PRIVATE , mem -> fd , 0 ) ; if ( mem -> mem == MAP_FAILED ) { LOG_ERROR ( " \n " , errno ) ; return false ; } if ( mmap ( mem -> mem + 0x8000 , 0x8000 , PROT_READ | PROT_WRITE , MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED , -1 , 0 ) == MAP_FAILED ) { LOG_ERROR ( " \n " , errno ) ; return false ; } } mem -> ram_banks = malloc ( MAX_RAM_BANKS * 0x2000 ) ; mem -> filename = filename ; if ( filename ) { int len = strlen ( filename ) + 4 ; mem -> savname = malloc ( len * sizeof ( char ) ) ; if ( mem -> savname != NULL ) snprintf ( mem -> savname , len , " " , filename ) ; } else { mem -> savname = NULL ; } mem -> mbc = mem -> mem [ 0x0147 ] ; mem -> mbc_mode = 0 ; mem -> mbc_data = 0 ; mem -> current_rom_bank = 1 ; mem -> current_ram_bank = 0 ; mem -> rtc_access = false ; return true ; }
initialize memory layout and map file filename
initialize memory layout and map file filename