<commit_before>//
// sprocketnes/mapper.rs
//
// Copyright (c) 2013 Mozilla Foundation
// Author: <NAME>
//

use mem::Mem;
use rom::Rom;

pub struct Mapper {
    rom: &Rom,

    // FIXME: ~fn is awful here; we need the function region work.
    // FIXME: We should trait-ify this. It's hard because of the region in Rom though. I guess we
    // need region-parameterized &Traits to fix this. Dependent on the function region work.
    // NB: This exposed a nasty Rust bug! ~IMapper segfaults; see "segfault" branch.
    loadb: ~fn(this: &mut Mapper, addr: u16) -> u8,
    storeb: ~fn(this: &mut Mapper, addr: u16, val: u8),
}

pub impl Mapper {
    static fn new(rom: &a/Rom) -> Mapper/&a {
        match rom.header.mapper() {
            0 => Mapper {
                rom: rom,
                loadb:  |this, addr|      Nrom.loadb(this, addr),
                storeb: |this, addr, val| Nrom.storeb(this, addr, val),
            },
            _ => fail ~"unsupported mapper"
        }
    }
}

//
// Mapper 0 (NROM)
//
// See http://wiki.nesdev.com/w/index.php/NROM
//

// TODO: RAM.
pub struct Nrom;

// TODO: Support 32K ROMs (NROM-256).
pub impl Nrom {
    fn loadb(self, this: &mut Mapper, addr: u16) -> u8 {
        if addr <= 0x8000 {
            0   // FIXME
        } else {
            // FIXME: Unsafe get for speed?
            this.rom.prg[addr & 0x3fff]
        }
    }
    fn storeb(self, _: &mut Mapper, _: u16, _: u8) {
        // TODO
    }
}
<commit_msg>Handle 32K NROMs properly<commit_after>//
// sprocketnes/mapper.rs
//
// Copyright (c) 2013 Mozilla Foundation
// Author: <NAME>
//

use mem::Mem;
use rom::Rom;

pub struct Mapper {
    rom: &Rom,

    // FIXME: ~fn is awful here; we need the function region work.
    // FIXME: We should trait-ify this. It's hard because of the region in Rom though. I guess we
    // need region-parameterized &Traits to fix this. Dependent on the function region work.
    // NB: This exposed a nasty Rust bug! ~IMapper segfaults; see "segfault" branch.
    loadb: ~fn(this: &mut Mapper, addr: u16) -> u8,
    storeb: ~fn(this: &mut Mapper, addr: u16, val: u8),
}

pub impl Mapper {
    static fn new(rom: &a/Rom) -> Mapper/&a {
        match rom.header.mapper() {
            0 => Mapper {
                rom: rom,
                loadb:  |this, addr|      Nrom.loadb(this, addr),
                storeb: |this, addr, val| Nrom.storeb(this, addr, val),
            },
            _ => fail ~"unsupported mapper"
        }
    }
}

//
// Mapper 0 (NROM)
//
// See http://wiki.nesdev.com/w/index.php/NROM
//

// TODO: RAM.
pub struct Nrom;

pub impl Nrom {
    fn loadb(self, this: &mut Mapper, addr: u16) -> u8 {
        if addr < 0x8000 {
            0   // FIXME
        } else {
            // FIXME: Unsafe get for speed?
            if this.rom.prg.len() > 16384 {
                this.rom.prg[addr & 0x7fff]
            } else {
                this.rom.prg[addr & 0x3fff]
            }
        }
    }
    fn storeb(self, _: &mut Mapper, _: u16, _: u8) {
        // TODO
    }
}

{'fraction_non_alphanumeric': 0.14009186351706038, 'fraction_numerical': 0.021653543307086614, 'mean_word_length': 1.7693006357856493, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6553488', 'n_tokens_mistral': 1124, 'n_tokens_neox': 1069, 'n_words': 324}