1 //
2 // Copyright (c) 2010-2018 Antmicro
3 //
4 // This file is licensed under the MIT License.
5 // Full license text is available in 'licenses/MIT.txt'.
6 //
7 using System;
8 using System.Collections.Generic;
9 using Antmicro.Renode.Core;
10 using Antmicro.Renode.Core.Structure;
11 using Antmicro.Renode.Core.Structure.Registers;
12 using Antmicro.Renode.Logging;
13 using Antmicro.Renode.Peripherals.Bus;
14 using Antmicro.Renode.Peripherals.PCI.BAR;
15 using Antmicro.Renode.Utilities;
16 
17 namespace Antmicro.Renode.Peripherals.PCI
18 {
19     public class PCIeMemory : PCIeEndpoint
20     {
PCIeMemory(IPCIeRouter parent, uint size)21         public PCIeMemory(IPCIeRouter parent, uint size) : base(parent)
22         {
23             this.memory = new uint[size / 4];
24             for(var i = 0u; i < HeaderType.MaxNumberOfBARs(); ++i)
25             {
26                 AddBaseAddressRegister(i, new MemoryBaseAddressRegister(size, MemoryBaseAddressRegister.BarType.LocateIn32Bit, true));
27             }
28         }
29 
Reset()30         public override void Reset()
31         {
32             base.Reset();
33             Array.Clear(memory, 0, memory.Length);
34         }
35 
WriteDoubleWordToBar(uint bar, long offset, uint value)36         protected override void WriteDoubleWordToBar(uint bar, long offset, uint value)
37         {
38             offset /= 4; //we keep uints, so we divide the offset
39             if(offset >= memory.Length)
40             {
41                 this.Log(LogLevel.Warning, "Trying to write 0x{0:X} out of memory range, at offset 0x{1:X}. Size of memory is 0x{2:X}.", value, offset, memory.Length);
42                 return;
43             }
44             memory[offset] = value;
45         }
46 
ReadDoubleWordFromBar(uint bar, long offset)47         protected override uint ReadDoubleWordFromBar(uint bar, long offset)
48         {
49             offset /= 4; //we keep uints, so we divide the offset
50             if(offset >= memory.Length)
51             {
52                 this.Log(LogLevel.Warning, "Trying to read from offset 0x{0:X}, beyond the memory range 0x{1:X}.", offset, memory.Length);
53                 return 0u;
54             }
55             return memory[offset];
56         }
57 
58         private uint[] memory;
59     }
60 }