diff --git a/emulation/betrusted.resc b/emulation/betrusted.resc index 2f52a7e0f..4a9adf667 100644 --- a/emulation/betrusted.resc +++ b/emulation/betrusted.resc @@ -83,6 +83,9 @@ sysbus SilenceRange <0xf0003000, 0xF0003FFF> # Silence POWER sysbus SilenceRange <0xF0014000 100> +# Attach the flash backing to the filesystem +sysbus.spinor.flash BackingFile $ORIGIN/../tools/pddb-images/renode.bin + # Load the SPI flash into RAM sysbus LoadBinary @precursors/soc_csr.bin 0x20000000 @@ -115,7 +118,7 @@ machine LoadPlatformDescription @ec/betrusted-ec.repl # logLevel -1 i2c # logLevel -1 i2c.bq27421 # logLevel -1 wifi -logLevel 0 wifi.wf200 +# logLevel 0 wifi.wf200 # sysbus LogPeripheralAccess wifi true machine StartGdbServer 3334 true diff --git a/emulation/peripherals/BetrustedI2S.cs b/emulation/peripherals/BetrustedI2S.cs index 9354e4a31..eef1ee20e 100644 --- a/emulation/peripherals/BetrustedI2S.cs +++ b/emulation/peripherals/BetrustedI2S.cs @@ -92,7 +92,7 @@ namespace Antmicro.Renode.Peripherals.Sound this.bufferAddress = memAddr; machine.SystemBus.Register(this.audioRam, new BusRangeRegistration(memAddr, memSize)); - CreateRegisters(); + DefineRegisters(); IRQ = new GPIO(); Reset(); } @@ -130,7 +130,7 @@ namespace Antmicro.Renode.Peripherals.Sound IRQ.Set(rxReady || rxError || txReady || txError); } - private void Start() + private void StartTx() { if(enableTx.Value) { @@ -143,7 +143,11 @@ namespace Antmicro.Renode.Peripherals.Sound encoder.SetBufferingBySamplesCount(maxSamplesCount.Value); encoder.Output = OutputFile; } + StartTxThread(); + } + private void StartRx() + { if(enableRx.Value) { if(InputFile == "") @@ -154,60 +158,62 @@ namespace Antmicro.Renode.Peripherals.Sound decoder = new PCMDecoder(sampleWidth, sampleFrequency, numberOfChannels, false, this); decoder.LoadFile(InputFile); } - StartRxTxThread(); + StartRxThread(); } private void Stop() { encoder?.FlushBuffer(); - StopRxTxThread(); - eventStopped.Value = true; + StopTxThread(); + StopRxThread(); + // eventStopped.Value = true; UpdateInterrupts(); } - private void StartRxTxThread() + private void StartTxThread() { - if(!enableI2S.Value) - { - this.Log(LogLevel.Error, "Trying to start aquisition, when peripheral is disabled (ENABLE==0). Will not start"); - return; - } - rxTxThread = machine.ObtainManagedThread(ProcessFrames, (int)(sampleFrequency / (maxSamplesCount.Value * samplesPerDoubleWord))); - rxTxThread.Start(); + txThread = machine.ObtainManagedThread(OutputFrames, (int)(sampleFrequency / (maxSamplesCount.Value * samplesPerDoubleWord))); + txThread.Start(); } - private void StopRxTxThread() + private void StartRxThread() { - if(rxTxThread == null) + rxThread = machine.ObtainManagedThread(InputFrames, (int)(sampleFrequency / (maxSamplesCount.Value * samplesPerDoubleWord))); + rxThread.Start(); + } + + private void StopTxThread() + { + if(txThread == null) { this.Log(LogLevel.Debug, "Trying to stop sampling when it is not active"); return; } - rxTxThread.Stop(); - rxTxThread = null; + txThread.Stop(); + txThread = null; } - private void ProcessFrames() + private void StopRxThread() { - if(enableRx.Value) + if(rxThread == null) { - InputFrames(); - } - if(enableTx.Value) - { - OutputFrames(); + this.Log(LogLevel.Debug, "Trying to stop sampling when it is not active"); + return; } + rxThread.Stop(); + rxThread = null; } + private void OutputFrames() { var currentPointer = txdPointer.Value; // The TXD.PTR register has been copied to internal double-buffers - eventTxPointerUpdated.Value = true; + // eventTxPointerUpdated.Value = true; UpdateInterrupts(); // RxTxMaxCnt denotes number of DoubleWords, we need to calculate samples number - for(var samples = 0u; samples < maxSamplesCount.Value * samplesPerDoubleWord; samples++) + for(var samples = 0u; samples < writeCount.Value * samplesPerDoubleWord; samples++) { var thisSample = machine.SystemBus.ReadDoubleWord(currentPointer + samples * sampleWidth / 8); BitHelper.ClearBits(ref thisSample, (int)sampleWidth, (int)(32 - sampleWidth)); @@ -219,10 +225,10 @@ namespace Antmicro.Renode.Peripherals.Sound { var currentPointer = rxdPointer.Value; // The RXD.PTR register has been copied to internal double-buffers - eventRxPointerUpdated.Value = true; + // eventRxPointerUpdated.Value = true; UpdateInterrupts(); - for(var doubleWords = 0u; doubleWords < maxSamplesCount.Value; doubleWords++) + for(var doubleWords = 0u; doubleWords < readCount.Value; doubleWords++) { // Double word may consist on many samples when sampleWidth is not equal 32bit uint valueToStore = 0; @@ -354,7 +360,7 @@ namespace Antmicro.Renode.Peripherals.Sound return closest; } - private void CreateRegisters() + private void DefineRegisters() { Registers.EventEnable.Define(this) .WithFlag(0, out interruptEnableRxReady, changeCallback: (_, __) => UpdateInterrupts(), name: "RX_READY") @@ -374,135 +380,13 @@ namespace Antmicro.Renode.Peripherals.Sound .WithFlag(2, out eventTxReady, FieldMode.WriteOneToClear, name: "TX_READY") .WithFlag(3, out eventTxError, FieldMode.WriteOneToClear, name: "TX_ERROR") ; - // Registers.TasksStart.Define(this) - // .WithFlag(0, FieldMode.Write, writeCallback: (_, val) => { if(val) Start(); }, name: "TASKS_START") - // .WithReservedBits(1,31); - // Registers.TasksStop.Define(this) - // .WithFlag(0, FieldMode.Write, writeCallback: (_, val) => { if(val) Stop(); }, name: "TASKS_STOP") - // .WithReservedBits(1,31); - // Registers.EventsRxptrUpdated .Define(this) - // .WithFlag(0, out eventRxPointerUpdated, changeCallback: (_, __) => UpdateInterrupts(), name: "EVENTS_RXPTRUPD") - // .WithReservedBits(1,31); - // Registers.EventsStopped.Define(this) - // .WithFlag(0, out eventStopped, changeCallback: (_, __) => UpdateInterrupts(), name: "EVENTS_STOPPED") - // .WithReservedBits(1,31); - // Registers.EventsTxptrUpdated.Define(this) - // .WithFlag(0, out eventTxPointerUpdated, changeCallback: (_, __) => UpdateInterrupts(), name: "EVENTS_TXPTRUPD") - // .WithReservedBits(1,31); - // Registers.InterruptEnable.Define(this) - // .WithReservedBits(0,1) - // .WithFlag(1, out interruptEnableRxPointerUpdated, name: "RXPTRUPD") - // .WithFlag(2, out interruptEnableStopped, name: "STOPPED") - // .WithReservedBits(3,2) - // .WithFlag(5, out interruptEnableTxPointerUpdated, name: "TXPTRUPD") - // .WithReservedBits(6,25); - // Registers.InterruptEnableSet.Define(this) - // .WithReservedBits(0,1) - // .WithFlag(1, - // writeCallback: (_, val) => { interruptEnableRxPointerUpdated.Value |= val; }, - // valueProviderCallback: (_) => { return interruptEnableRxPointerUpdated.Value; }, - // name: "RXPTRUPD") - // .WithFlag(2, - // writeCallback: (_, val) => { interruptEnableStopped.Value |= val; }, - // valueProviderCallback: (_) => { return interruptEnableStopped.Value; }, - // name: "STOPPED") - // .WithReservedBits(3,2) - // .WithFlag(5, - // writeCallback: (_, val) => { interruptEnableTxPointerUpdated.Value |= val; }, - // valueProviderCallback: (_) => { return interruptEnableTxPointerUpdated.Value; }, - // name: "TXPTRUPD") - // .WithReservedBits(6,25); - // Registers.InterruptEnableClear.Define(this) - // .WithReservedBits(0,1) - // .WithFlag(1, - // writeCallback: (_, val) => { interruptEnableRxPointerUpdated.Value &= !val; }, - // valueProviderCallback: (_) => { return interruptEnableRxPointerUpdated.Value; }, - // name: "RXPTRUPD") - // .WithFlag(2, - // writeCallback: (_, val) => { interruptEnableStopped.Value &= !val; }, - // valueProviderCallback: (_) => { return interruptEnableStopped.Value; }, - // name: "STOPPED") - // .WithReservedBits(3,2) - // .WithFlag(5, - // writeCallback: (_, val) => { interruptEnableTxPointerUpdated.Value &= !val; }, - // valueProviderCallback: (_) => { return interruptEnableTxPointerUpdated.Value; }, - // name: "TXPTRUPD") - // .WithReservedBits(6,25); - // Registers.Enable.Define(this) - // .WithFlag(0, out enableI2S, name: "ENABLE") - // .WithReservedBits(1,31); - // Registers.ConfigMode.Define(this) - // .WithValueField(0, 1, - // writeCallback: (_, val) => - // { - // if((Mode)val == Mode.Slave) - // { - // //This requires ability to use master device clock configuration and handling alignment / format properly - // this.Log(LogLevel.Error, "Slave mode unimplemented"); - // } - // }, - // name: "MODE") - // .WithReservedBits(1,31); - // Registers.ConfigRxEnable.Define(this) - // .WithFlag(0, out enableRx, name: "RXEN") - // .WithReservedBits(1,31); - // Registers.ConfigTxEnable.Define(this, 0x1) - // .WithFlag(0, out enableTx, name: "TXEN") - // .WithReservedBits(1,31); - // Registers.ConfigMasterClockEnable.Define(this, 0x1) - // .WithFlag(0, name: "MCKEN") - // .WithReservedBits(1,31); - // Registers.ConfigMasterClockFrequency.Define(this, 0x20000000) - // .WithValueField(0, 32, writeCallback: (_, val) => SetMasterClockFrequency(val), name: "MCKFREQ"); - // Registers.ConfigRatio.Define(this, 0x6) - // .WithValueField(0, 4, writeCallback: (_, val) => SetMasterClockLrckRatio(val), name: "RATIO") - // .WithReservedBits(4,28); - // Registers.ConfigSwidth.Define(this, 0x1) - // .WithValueField(0, 2, writeCallback: (_, val) => SetSampleWidth(val), name: "SWIDTH") - // .WithReservedBits(2,30); - // Registers.ConfigAlign.Define(this) - // .WithTaggedFlag("ALIGN", 0) - // .WithReservedBits(1, 31); - // Registers.ConfigFormat.Define(this) - // .WithTaggedFlag("FORMAT",0) - // .WithReservedBits(1, 31); - // Registers.ConfigChannels.Define(this) - // .WithValueField(0, 2, - // writeCallback: (_, val) => { numberOfChannels = (Channels)val == Channels.Stereo ? 2u : 1u;}, - // name: "CHANNELS") - // .WithReservedBits(2, 30); - // Registers.RxdPointer.Define(this) - // .WithValueField(0, 32, out rxdPointer, name: "PTR"); - // Registers.TxdPointer.Define(this) - // .WithValueField(0, 32, out txdPointer, name: "PTR"); - // Registers.RxTxBufferSize.Define(this) - // .WithValueField(0, 14, out maxSamplesCount, name: "MAXCNT") - // .WithReservedBits(14, 18); - // Registers.PinSelectMasterClock.Define(this, 0xFFFFFFFF) - // .WithTag("PIN", 0, 5) - // .WithTag("PORT", 5, 1) - // .WithReservedBits(6, 25) - // .WithTaggedFlag("CONNECT", 31); - // Registers.PinSelectSCK.Define(this, 0xFFFFFFFF) - // .WithTag("PIN", 0, 5) - // .WithTag("PORT", 5, 1) - // .WithReservedBits(6, 25) - // .WithTaggedFlag("CONNECT", 31); - // Registers.PinSelectLRCK.Define(this, 0xFFFFFFFF) - // .WithTag("PIN", 0, 5) - // .WithTag("PORT", 5, 1) - // .WithReservedBits(6, 25) - // .WithTaggedFlag("CONNECT", 31); - // Registers.PinSelectSDIN.Define(this, 0xFFFFFFFF) - // .WithTag("PIN", 0, 5) - // .WithTag("PORT", 5, 1) - // .WithReservedBits(6, 25) - // .WithTag("CONNECT", 31, 1); - // Registers.PinSelectSDOUT.Define(this, 0xFFFFFFFF) - // .WithValueField(0, 5, name: "PIN") - // .WithValueField(5, 1, name: "PORT") - // .WithReservedBits(6, 25) - // .WithTaggedFlag("CONNECT", 31); + Registers.TxCtl.Define32(this) + .WithFlag(0, writeCallback: (_, val) => { if (val) StartTx(); }, name: "tx_enable") + ; + + Registers.RxCtl.Define32(this) + .WithFlag(0, writeCallback: (_, val) => { if (val) StartRx(); }, name: "rx_enable") + ; } private IFlagRegisterField enableI2S; @@ -529,7 +413,8 @@ namespace Antmicro.Renode.Peripherals.Sound private uint samplesPerDoubleWord; private uint sampleWidth; - private IManagedThread rxTxThread; + private IManagedThread rxThread; + private IManagedThread txThread; private PCMDecoder decoder; private PCMEncoder encoder; private AudioRam audioRam; diff --git a/emulation/peripherals/MXIC_MX66UM1G45G.cs b/emulation/peripherals/MXIC_MX66UM1G45G.cs index 41d37f837..6ded1d5dc 100644 --- a/emulation/peripherals/MXIC_MX66UM1G45G.cs +++ b/emulation/peripherals/MXIC_MX66UM1G45G.cs @@ -5,6 +5,7 @@ // Full license text is available in 'licenses/MIT.txt'. // using System; +using System.IO; using Antmicro.Renode.Core.Structure.Registers; using Antmicro.Renode.Exceptions; using Antmicro.Renode.Logging; @@ -139,6 +140,90 @@ namespace Antmicro.Renode.Peripherals.SPI return 0; } + private string InternalBackingFileName; + private FileStream InternalBackingFile; + public string BackingFile + { + get { return InternalBackingFileName; } + set + { + // If the user passes an empty string, don't do anything. + if (string.IsNullOrEmpty(value)) + { + this.Log(LogLevel.Error, "Unconfiguring backing file"); + if (InternalBackingFile != null) + { + InternalBackingFile.Close(); + } + InternalBackingFileName = null; + InternalBackingFile = null; + return; + } + + // Ensure the path name is fully-qualified, using the current working + // directory if necessary. + if (System.IO.Path.IsPathRooted(value)) + { + InternalBackingFileName = value; + } + else + { + InternalBackingFileName = System.IO.Path.Combine(Environment.CurrentDirectory, value); + } + try + { + this.Log(LogLevel.Debug, "Trying to load or create backing file {0}", InternalBackingFileName); + InternalBackingFile = new FileStream(InternalBackingFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); + byte[] copyBuffer = new byte[4096]; + var currentOffset = 0; + + // Assume that if the length is 0, then the file was just created and we need + // to copy RAM into the newly-created file. + if (InternalBackingFile.Length == 0) + { + this.Log(LogLevel.Debug, "Backing file {0} was created, dumping {1} bytes of ROM to backing file", InternalBackingFileName, underlyingMemory.Size); + // 4096 is a safe size, since that is the page size. + for (currentOffset = 0; currentOffset < underlyingMemory.Size; currentOffset += copyBuffer.Length) + { + underlyingMemory.ReadBytes(currentOffset, copyBuffer.Length, copyBuffer, 0); + InternalBackingFile.Write(copyBuffer, 0, copyBuffer.Length); + } + } + // If the length is NOT 0, read data from the backing file + else + { + this.Log(LogLevel.Debug, "Backing file {0} was loaded, restoring {1} bytes (vs ROM: {2}) of ROM from file", InternalBackingFileName, InternalBackingFile.Length, underlyingMemory.Size); + InternalBackingFile.Seek(0, SeekOrigin.Begin); + if (InternalBackingFile.Length != underlyingMemory.Size) + { + this.Log(LogLevel.Warning, "Backing file {0} was a different size than ROM (file was {1} bytes, ROM is {2} bytes) -- resizing backing file to match ROM size", InternalBackingFileName, InternalBackingFile.Length, underlyingMemory.Size); + InternalBackingFile.SetLength(underlyingMemory.Size); + } + + // 4096 is a safe size, since that is the page size. + for (currentOffset = 0; + (currentOffset < underlyingMemory.Size) && (currentOffset < InternalBackingFile.Length); + currentOffset += copyBuffer.Length) + { + InternalBackingFile.Read(copyBuffer, 0, copyBuffer.Length); + underlyingMemory.WriteBytes(currentOffset, copyBuffer); + } + } + } + catch (IOException e) + { + if (InternalBackingFile != null) + { + InternalBackingFile.Close(); + } + InternalBackingFileName = null; + InternalBackingFile = null; + throw new RecoverableException(e); + } + } + } + + public MappedMemory UnderlyingMemory => underlyingMemory; private void AccumulateAddressBytes(byte addressByte, DecodedOperation.OperationState nextState) @@ -537,6 +622,11 @@ namespace Antmicro.Renode.Peripherals.SPI var length = (int)Math.Min(SegmentSize, underlyingMemory.Size - position); underlyingMemory.WriteBytes(position, segment, length); + if (InternalBackingFile != null) + { + InternalBackingFile.Seek(position, SeekOrigin.Begin); + InternalBackingFile.Write(segment, 0, length); + } position += length; } } @@ -553,6 +643,11 @@ namespace Antmicro.Renode.Peripherals.SPI var position = SegmentSize * (currentOperation.ExecutionAddress / SegmentSize); underlyingMemory.WriteBytes(position, segment); + if (InternalBackingFile != null) + { + InternalBackingFile.Seek(position, SeekOrigin.Begin); + InternalBackingFile.Write(segment, 0, segment.Length); + } } private void EraseSubsector4K() @@ -567,6 +662,11 @@ namespace Antmicro.Renode.Peripherals.SPI var position = 4.KB() * (currentOperation.ExecutionAddress / 4.KB()); underlyingMemory.WriteBytes(position, segment); + if (InternalBackingFile != null) + { + InternalBackingFile.Seek(position, SeekOrigin.Begin); + InternalBackingFile.Write(segment, 0, segment.Length); + } } private void WriteToMemory(byte val) @@ -579,6 +679,13 @@ namespace Antmicro.Renode.Peripherals.SPI var position = currentOperation.ExecutionAddress + currentOperation.CommandBytesHandled; underlyingMemory.WriteByte(position, val); + if (InternalBackingFile != null) + { + var tmp_byte = new byte[1]; + tmp_byte[0] = val; + InternalBackingFile.Seek(position, SeekOrigin.Begin); + InternalBackingFile.Write(tmp_byte, 0, 1); + } } private byte ReadFromMemory()