<commit_before>'use strict';

const debug = require('debug')('gameboy:cart:mbc1');
const Serializable = require('../../util/serializable');
const { ROM_SIZE, RAM_SIZE } = require('../headers');


const createMemory = (count, size, data = []) => new Array(count)
    .fill(null)
    .map((v, i) => {
        const offset = i * size;
        if (offset + size <= data.length) {
            return data.slice(offset, offset + size);
        }
        return new Uint8Array(size);
    });

const wrap = (array, i) => array[i & (array.length - 1)];

class MBC1 extends Serializable({ exclude: ['_rom'] }) {
    constructor (data, romSize, ramSize) {
        super();

        this._lbank = 1;
        this._hbank = 0;
        this._ramEnabled = false;
        this._mode = 0;

        this._rom = createMemory(ROM_SIZE[romSize], 0x4000, data);
        this._ram = createMemory(RAM_SIZE[ramSize], 0x2000);
        console.log('rom', this._rom, ROM_SIZE[romSize]);
        console.log('ram', this._ram, RAM_SIZE[ramSize]);
    }

    readByte (addr) {
        switch (addr >> 12) {
            case 0x0: case 0x1:
            case 0x2: case 0x3:
                let bank = 0;
                if (this._mode == 1) bank = this._hbank << 5;
                return wrap(this._rom, bank)[addr];
            case 0x4: case 0x5:
            case 0x6: case 0x7: {
                const bank = this._lbank | (this._hbank << 5);
                return wrap(this._rom, bank)[addr & 0x3fff];
            }
            case 0xa: case 0xb: {
                if (!this._ramEnabled) {
                    debug('cannot read ram while disabled');<commit_msg>add missing braces<commit_after>'use strict';

const debug = require('debug')('gameboy:cart:mbc1');
const Serializable = require('../../util/serializable');
const { ROM_SIZE, RAM_SIZE } = require('../headers');


const createMemory = (count, size, data = []) => new Array(count)
    .fill(null)
    .map((v, i) => {
        const offset = i * size;
        if (offset + size <= data.length) {
            return data.slice(offset, offset + size);
        }
        return new Uint8Array(size);
    });

const wrap = (array, i) => array[i & (array.length - 1)];

class MBC1 extends Serializable({ exclude: ['_rom'] }) {
    constructor (data, romSize, ramSize) {
        super();

        this._lbank = 1;
        this._hbank = 0;
        this._ramEnabled = false;
        this._mode = 0;

        this._rom = createMemory(ROM_SIZE[romSize], 0x4000, data);
        this._ram = createMemory(RAM_SIZE[ramSize], 0x2000);
        console.log('rom', this._rom, ROM_SIZE[romSize]);
        console.log('ram', this._ram, RAM_SIZE[ramSize]);
    }

    readByte (addr) {
        switch (addr >> 12) {
            case 0x0: case 0x1:
            case 0x2: case 0x3: {
                let bank = 0;
                if (this._mode == 1) bank = this._hbank << 5;
                return wrap(this._rom, bank)[addr];
            }
            case 0x4: case 0x5:
            case 0x6: case 0x7: {
                const bank = this._lbank | (this._hbank << 5);
                return wrap(this._rom, bank)[addr & 0x3fff];
            }
            case 0xa: case 0xb: {
                if (!this._ramEnabled) {
                    debug('cannot read ram while disabled');
{'fraction_non_alphanumeric': 0.1465491030708422, 'fraction_numerical': 0.02614776527820006, 'mean_word_length': 1.9296527159394479, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '202426', 'n_tokens_mistral': 1100, 'n_tokens_neox': 1009, 'n_words': 259}