1 // 2 // Copyright (c) 2010-2024 Antmicro 3 // 4 // This file is licensed under the MIT License. 5 // Full license text is available in 'licenses/MIT.txt'. 6 // 7 using Antmicro.Renode.Core; 8 using Antmicro.Renode.Peripherals.Bus; 9 using System.Collections.Generic; 10 // using InternalCortexMContextState = Antmicro.Renode.Peripherals.CPU.InternalCortexMContextState; 11 12 namespace Antmicro.Renode.Peripherals.CPU 13 { 14 public class SampleStateAwareReaderWithTransactionState : IPeripheralWithTransactionState, IBusPeripheral 15 { SampleStateAwareReaderWithTransactionState(Machine machine)16 public SampleStateAwareReaderWithTransactionState(Machine machine) 17 { 18 sysbus = machine.GetSystemBus(this); 19 } 20 Reset()21 public void Reset() 22 { 23 } 24 Read(ulong address, ulong state)25 public uint Read(ulong address, ulong state) 26 { 27 return sysbus.ReadDoubleWord(address, context: this, cpuState: state); 28 } 29 ReadUsingStateObj(ulong address)30 public uint ReadUsingStateObj(ulong address) 31 { 32 if(!TryConvertUlongToStateObj(0, out var contextState)) 33 { 34 return 0; 35 } 36 return sysbus.ReadDoubleWordWithState(address, this, contextState); 37 } 38 TryConvertStateObjToUlong(IContextState stateObj, out ulong? state)39 public bool TryConvertStateObjToUlong(IContextState stateObj, out ulong? state) 40 { 41 state = null; 42 if((stateObj == null) || !(stateObj is CortexM.ContextState cpuStateObj)) 43 { 44 return false; 45 } 46 state = 0u; 47 state |= (cpuStateObj.Privileged ? 1u : 0) & 1u; 48 state |= (cpuStateObj.CpuSecure ? 2u : 0) & 2u; 49 state |= (cpuStateObj.AttributionSecure ? 4u : 0) & 4u; 50 return true; 51 } 52 TryConvertUlongToStateObj(ulong? state, out IContextState stateObj)53 public bool TryConvertUlongToStateObj(ulong? state, out IContextState stateObj) 54 { 55 stateObj = null; 56 if(!state.HasValue) 57 { 58 return false; 59 } 60 var cpuStateObj = new CortexM.ContextState 61 { 62 Privileged = (state & 1u) == 1u, 63 CpuSecure = (state & 2u) == 2u, 64 AttributionSecure = (state & 4u) == 4u 65 }; 66 stateObj = cpuStateObj; 67 return true; 68 } 69 70 public IReadOnlyDictionary<string, int> StateBits { get { return stateBits; } } 71 72 private static readonly IReadOnlyDictionary<string, int> stateBits = new Dictionary<string, int> 73 { 74 ["privileged"] = 0, 75 ["cpuSecure"] = 1, 76 ["attributionSecure"] = 2, 77 }; 78 79 private IBusController sysbus; 80 } 81 } 82