public void IdentityMaptTest() { var(location, data) = TlbHelper.PrepareIdentityMap(MemorySize); _mmu.CopyToPhysical(location, data); _mmu._pageDirectory = location; var storeRand = new Random(0); var getRand = new Random(0); for (uint address = 0; address < location; address += 4) { var value = (uint)storeRand.Next(); _mmu.StoreVirtualLong(address, value); var gotValue = _mmu.GetVirtualLong(address); gotValue.Should().Be(value, $"fail at first pass, address {address}"); } for (uint address = 0; address < location; address += 4) { var expectedValue = (uint)getRand.Next(); var value = _mmu.GetVirtualLong(address); value.Should().Be(expectedValue, $"fail at second pass, address {address}"); } }
static void Main(string[] args) { const int memorySize = 32 * 1024 * 1024; var mmu = new Mmu(memorySize); var decoder = new InstructionDecoder(); var cpu = new Cpu <PhysicalMemoryAccessor, Tracing>(mmu, decoder); if (false /*cpu is Cpu<VirtualMemoryAccessor>*/) { var(location, data) = TlbHelper.PrepareIdentityMap(memorySize); mmu.CopyToPhysical(location, data); mmu._pageDirectory = location; } else { mmu._pageDirectory = memorySize - 4; } var elf = ELFReader.Load <uint>("D:\\fibo"); var startAddress = elf.EntryPoint; var loadableSegments = elf.Segments.Where(s => s.Type == SegmentType.Load); foreach (var segment in loadableSegments) { var content = segment.GetMemoryContents(); var address = segment.PhysicalAddress; mmu.CopyToPhysical(address, content); } cpu.Execute((int)startAddress); var stoper = Stopwatch.StartNew(); for (var i = 0; i < 10; ++i) { cpu.Execute((int)startAddress); } stoper.Stop(); Console.WriteLine(stoper.ElapsedMilliseconds); Console.WriteLine(cpu.InstructionsExecuted); Console.WriteLine($"{(double)cpu.InstructionsExecuted / stoper.ElapsedMilliseconds / 1000} MIPS"); cpu.Reset(); stoper = Stopwatch.StartNew(); for (var i = 0; i < 100; ++i) { cpu.Execute((int)startAddress); } stoper.Stop(); Console.WriteLine(stoper.ElapsedMilliseconds); Console.WriteLine(cpu.InstructionsExecuted); Console.WriteLine($"{(double)cpu.InstructionsExecuted / stoper.ElapsedMilliseconds / 1000} MIPS"); cpu.Reset(); stoper = Stopwatch.StartNew(); for (var i = 0; i < 100; ++i) { cpu.Execute((int)startAddress); } stoper.Stop(); Console.WriteLine(stoper.ElapsedMilliseconds); Console.WriteLine(cpu.InstructionsExecuted); Console.WriteLine($"{(double)cpu.InstructionsExecuted / stoper.ElapsedMilliseconds / 1000} MIPS"); }