Ejemplo n.º 1
0
        /// <summary>
        ///     Reads one byte from the specified bank, at the specified offset
        /// </summary>
        /// <param name="offset"></param>
        /// <returns></returns>
        public byte ReadByte(int offset)
        {
            // CHR ROM
            if (offset < 0x2000)
            {
                return(_chrRom[offset]);
            }

            //PPU Registers
            if (offset <= 0x3FFF)
            {
                return(ReadInterceptors.TryGetValue(offset, out currentReadInterceptor) ? currentReadInterceptor(offset) : (byte)0x0);
            }

            //Some UxROM boards from Nintendo have bus conflicts, we avoid these here
            if (offset >= 0x6000 && offset < 0x8000)
            {
                return(0x0);
            }

            //16 KB switchable PRG ROM bank
            if (offset <= 0xBFFF)
            {
                return(_prgRom[_prgBank0Offset + (offset - 0x8000)]);
            }

            //16 KB PRG ROM bank, fixed to the last bank
            if (offset <= 0xFFFF)
            {
                return(_prgRom[_prgBank1Offset + (offset - 0xC000)]);
            }

            throw new ArgumentOutOfRangeException(nameof(offset), offset, "Maximum value of offset is 0xFFFF");
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Reads one byte from the specified bank, at the specified offset
        /// </summary>
        /// <param name="memoryType"></param>
        /// <param name="offset"></param>
        /// <returns></returns>
        public byte ReadByte(enumMemoryType memoryType, int offset)
        {
            switch (memoryType)
            {
            case enumMemoryType.CPU:
                return(_prgRom[CpuOffsetToPrgRomOffset(offset)]);

            case enumMemoryType.PPU:
                return(!ReadInterceptors.TryGetValue(offset, out currentReadInterceptor) ? _chrRom[offset] : currentReadInterceptor(offset));

            default:
                throw new ArgumentOutOfRangeException(nameof(memoryType), memoryType, null);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        ///     Reads one byte from the specified bank, at the specified offset
        /// </summary>
        /// <param name="memoryType"></param>
        /// <param name="offset"></param>
        /// <returns></returns>
        public byte ReadByte(int offset)
        {
            // CHR Bank 0 == $0000-$0FFF
            // CHR Bank 1 == $1000-$1FFF
            if (offset <= 0x1FFF)
            {
                var chrBankOffset = offset / 0x1000 == 0 ? _chrBank0Offset : _chrBank1Offset;
                chrBankOffset += offset % 0x1000;
                return(_chrRom[chrBankOffset]);
            }

            //PPU Registers
            if (offset <= 0x3FFF)
            {
                return(ReadInterceptors.TryGetValue(offset, out currentReadInterceptor) ? currentReadInterceptor(offset) : (byte)0x0);
            }

            // PRG RAM Bank == $6000-$7FFF
            if (offset >= 0x6000 && offset <= 0x7FFF)
            {
                if (!_usePrgRam)
                {
                    throw new AccessViolationException($"Attempt to read PRG RAM when disabled. Offset ${offset:X4}");
                }

                return(_prgRam[offset - 0x6000]);
            }

            // PRG Bank 0 == $8000-$BFFF
            // PRG Bank 1 == $C000-$FFFF
            if (offset >= 0x8000 && offset <= 0xFFFF)
            {
                //Map address $8000 to our _prgRom start of 0x0000;
                var prgBaseOffset = offset - 0x8000;
                var prgBankOffset = prgBaseOffset / 0x4000 == 0 ? _prgBank0Offset : _prgBank1Offset;
                prgBankOffset += prgBaseOffset % 0x4000;
                return(_prgRom[prgBankOffset]);
            }

            throw new ArgumentOutOfRangeException(nameof(offset), offset, "Maximum value of offset is 0xFFFF");
        }
Ejemplo n.º 4
0
        /// <summary>
        ///     Reads one byte from the specified bank, at the specified offset
        /// </summary>
        /// <param name="memoryType"></param>
        /// <param name="offset"></param>
        /// <returns></returns>
        public byte ReadByte(int offset)
        {
            //CHR ROM
            if (offset < 0x2000)
            {
                return(_chrRom[offset]);
            }

            //PPU Interceptors (Read)
            if (offset <= 0x3FFF)
            {
                return(ReadInterceptors.TryGetValue(offset, out currentReadInterceptor)
                    ? currentReadInterceptor(offset)
                    : (byte)0x0);
            }

            //PRG ROM
            if (offset <= 0xFFFF)
            {
                return(_prgRom[offset - 0x8000]);
            }

            throw new ArgumentOutOfRangeException(nameof(offset), offset, "Maximum value of offset is 0xFFFF");
        }