<commit_before>using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace Noxa.Emulation.Psp.Cpu
{
	unsafe class Memory : IMemory
	{
		// This is my uber simple memory manager
		// The C++/CLI implementation is much more general, however it's also slower
		// It's better for a debugger, where as this should be better for speed purposes

		//_memory->DefineSegment( MemoryType::PhysicalMemory, "Main Memory", 0x08000000, 0x01FFFFFF );
		//_memory->DefineSegment( MemoryType::PhysicalMemory, "Hardware Vectors", 0x1FC00000, 0x000FFFFF );
		//_memory->DefineSegment( MemoryType::PhysicalMemory, "Scratchpad", 0x00010000, 0x00003FFF );
		//_memory->DefineSegment( MemoryType::PhysicalMemory, "Frame Buffer", 0x04000000, 0x001FFFFF );
		//_memory->DefineSegment( MemoryType::HardwareMapped, "Hardware IO 1", 0x1C000000, 0x03BFFFFF );
		//_memory->DefineSegment( MemoryType::HardwareMapped, "Hardware IO 2", 0x1FD00000, 0x002FFFFF );

		internal byte[] _mainMemory;
		private byte[] _scratchPad;
		private byte[] _frameBufferBytes;
		private IMemorySegment _frameBuffer;

		public Memory()
		{
			_mainMemory = new byte[ 0x01FFFFFF ];
			_scratchPad = new byte[ 0x00003FFF ];
			_frameBufferBytes = new byte[ 0x001FFFFF ];
		}

		public IMemorySegment DefineSegment( MemoryType type, string name, int baseAddress, int length )
		{
			throw new NotSupportedException();
		}

		public void RegisterSegment( IMemorySegment segment )
		{
			if( segment.BaseAddress == 0x04000000 )
			{
				_frameBufferBytes = null;
				_frameBuffer = segment;
			}
			else
			{
				Debug.Assert( false, "Cannot override other segments" );
			}
		}

		public IMemorySegment FindSegment( string name )
		{
			throw new NotSupportedException();
		}

		public IMemorySegment FindSegment( int baseAddress )
		{
			throw new NotSupportedException();
		}

		public int ReadWord( int address )
		{
			if( ( address >= 0x08000000 ) && ( address < 0x9FFFFFF ) )
			{
				address -= 0x08000000;
				fixed( byte* basePointer = _mainMemory )
				{
					int* ptr = ( int* )( basePointer + address );
					return *ptr;
				}
			}
			else if( ( address >= 0x00010000 ) && ( address < 0x00003FFF ) )
			{
				address -= 0x00010000;
				fixed( byte* basePointer = _scratchPad )
				{
					int* ptr = ( int* )( basePointer + address );
					return *ptr;
				}
			}
			else if( ( address >= 0x04000000 ) && ( address < 0x001FFFFF ) )
			{
				if( _frameBuffer != null )
					return _frameBuffer.ReadWord( address );
				else
				{
					address -= 0x04000000;
					fixed( byte* basePointer = _frameBufferBytes )
					{
						int* ptr = ( int* )( basePointer + address );
						return *ptr;
					}
				}
			}
			else
				return 0;
		}

		public byte[] ReadBytes( int address, int count )
		{
			throw new NotImplementedException();<commit_msg>Added simple ReadBytes implementation - this really should be removed, as it's slow as hell and allocates a ton of memory In a bit I'll write a more direct access method It would also be nice to have a 'friend' accessor to the main memory (using reflection to get at it) so that the video driver can grab the bytes from the CPU fast when it becomes dynarec too<commit_after>using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace Noxa.Emulation.Psp.Cpu
{
	unsafe class Memory : IMemory
	{
		// This is my uber simple memory manager
		// The C++/CLI implementation is much more general, however it's also slower
		// It's better for a debugger, where as this should be better for speed purposes

		//_memory->DefineSegment( MemoryType::PhysicalMemory, "Main Memory", 0x08000000, 0x01FFFFFF );
		//_memory->DefineSegment( MemoryType::PhysicalMemory, "Hardware Vectors", 0x1FC00000, 0x000FFFFF );
		//_memory->DefineSegment( MemoryType::PhysicalMemory, "Scratchpad", 0x00010000, 0x00003FFF );
		//_memory->DefineSegment( MemoryType::PhysicalMemory, "Frame Buffer", 0x04000000, 0x001FFFFF );
		//_memory->DefineSegment( MemoryType::HardwareMapped, "Hardware IO 1", 0x1C000000, 0x03BFFFFF );
		//_memory->DefineSegment( MemoryType::HardwareMapped, "Hardware IO 2", 0x1FD00000, 0x002FFFFF );

		internal byte[] _mainMemory;
		private byte[] _scratchPad;
		private byte[] _frameBufferBytes;
		private IMemorySegment _frameBuffer;

		public Memory()
		{
			_mainMemory = new byte[ 0x01FFFFFF ];
			_scratchPad = new byte[ 0x00003FFF ];
			_frameBufferBytes = new byte[ 0x001FFFFF ];
		}

		public IMemorySegment DefineSegment( MemoryType type, string name, int baseAddress, int length )
		{
			throw new NotSupportedException();
		}

		public void RegisterSegment( IMemorySegment segment )
		{
			if( segment.BaseAddress == 0x04000000 )
			{
				_frameBufferBytes = null;
				_frameBuffer = segment;
			}
			else
			{
				Debug.Assert( false, "Cannot override other segments" );
			}
		}

		public IMemorySegment FindSegment( string name )
		{
			throw new NotSupportedException();
		}

		public IMemorySegment FindSegment( int baseAddress )
		{
			throw new NotSupportedException();
		}

		public int ReadWord( int address )
		{
			if( ( address >= 0x08000000 ) && ( address < 0x9FFFFFF ) )
			{
				address -= 0x08000000;
				fixed( byte* basePointer = _mainMemory )
				{
					int* ptr = ( int* )( basePointer + address );
					return *ptr;
				}
			}
			else if( ( address >= 0x00010000 ) && ( address < 0x00003FFF ) )
			{
				address -= 0x00010000;
				fixed( byte* basePointer = _scratchPad )
				{
					int* ptr = ( int* )( basePointer + address );
					return *ptr;
				}
			}
			else if( ( address >= 0x04000000 ) && ( address < 0x001FFFFF ) )
			{
				if( _frameBuffer != null )
					return _frameBuffer.ReadWord( address );
				else
				{
					address -= 0x04000000;
					fixed( byte* basePointer = _frameBufferBytes )
					{
						int* ptr = ( int* )( basePointer + address );
						return *ptr;
					}
				}
			}
			else
				return 0;
		}

		public byte[] ReadBytes( int address, int count )
		{
			if( ( address >= 0x08000000 ) && ( address < 0x9FFFFFF ) )
			{
				address -= 0x08000000;
				byte[] buffer = new byte[ count ];
				Array.Copy( _mainMemory, address, buffer, 0, count );
				return buffer;
			}
			else if( ( address >= 0x00010000 ) && ( address < 0x00003FFF ) )
				throw new NotImplementedException();
			else if( ( address >= 0x04000000 ) && ( address < 0x001FFFFF ) )
				throw new NotImplementedException();
			else
				return null;
{'fraction_non_alphanumeric': 0.10658645688724357, 'fraction_numerical': 0.058152090081752275, 'mean_word_length': 3.325550366911274, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 24, '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': '3822714', 'n_tokens_mistral': 2733, 'n_tokens_neox': 2021, 'n_words': 588}