From 8b57b8c36b0f57cf1082053aa230815b53e780ef Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Tue, 10 Mar 2020 16:41:07 +0800 Subject: [PATCH] emulation: add 32-bit litex timer Litex uses 32-bit CSRs, so add a version of the litex timer module that uses 32-bit CSRs. Signed-off-by: Sean Cross --- emulation/{platonic.repl => betrusted.repl} | 2 +- emulation/boot.resc | 13 +- emulation/peripherals/LiteX_Timer_32.cs | 160 ++++++++++++++++++++ 3 files changed, 171 insertions(+), 4 deletions(-) rename emulation/{platonic.repl => betrusted.repl} (97%) create mode 100644 emulation/peripherals/LiteX_Timer_32.cs diff --git a/emulation/platonic.repl b/emulation/betrusted.repl similarity index 97% rename from emulation/platonic.repl rename to emulation/betrusted.repl index 456b3b377..7e0f3051e 100644 --- a/emulation/platonic.repl +++ b/emulation/betrusted.repl @@ -15,7 +15,7 @@ uartkernel: UART.LiteX_UART @ { } -> cpu@1003 -timer0: Timers.LiteX_Timer @ { +timer0: Timers.LiteX_Timer_32 @ { sysbus 0x60003000; sysbus 0xF0003000 // shadow } diff --git a/emulation/boot.resc b/emulation/boot.resc index 919b6e9bb..eb0aef675 100644 --- a/emulation/boot.resc +++ b/emulation/boot.resc @@ -1,11 +1,18 @@ +# Boot Renode Script (boot.resc) + using sysbus mach create -machine LoadPlatformDescription @platonic.repl + +# Add peripherals that are defined in C#. You must restart Renode +# if you modify these files. +i @peripherals/LiteX_Timer_32.cs + +# Load the Betrusted Renode Platform file +machine LoadPlatformDescription @betrusted.repl machine StartGdbServer 3333 true - -showAnalyzer uart showAnalyzer uartkernel +showAnalyzer uart # The macro `reset` gets called implicitly when running `machine Reset` macro reset diff --git a/emulation/peripherals/LiteX_Timer_32.cs b/emulation/peripherals/LiteX_Timer_32.cs new file mode 100644 index 000000000..f9d05d495 --- /dev/null +++ b/emulation/peripherals/LiteX_Timer_32.cs @@ -0,0 +1,160 @@ +// +// Copyright (c) 2010-2019 Antmicro +// +// This file is licensed under the MIT License. +// Full license text is available in 'licenses/MIT.txt'. +// +using Antmicro.Renode.Core; +using Antmicro.Renode.Core.Structure.Registers; +using Antmicro.Renode.Utilities; +using Antmicro.Renode.Logging; +using Antmicro.Renode.Peripherals.Bus; + +namespace Antmicro.Renode.Peripherals.Timers +{ + // this is a model of LiteX timer in the Betrusted configuration: + // * width: 32 bits + // * csr data width: 32 bit + [AllowedTranslations(AllowedTranslation.ByteToDoubleWord)] + public class LiteX_Timer_32 : BasicDoubleWordPeripheral, IKnownSize + { + public LiteX_Timer_32(Machine machine, long frequency) : base(machine) + { + IRQ = new GPIO(); + innerTimer = new LimitTimer(machine.ClockSource, frequency, this, "LiteXTimer32", eventEnabled: true, autoUpdate: true); + innerTimer.LimitReached += delegate + { + this.Log(LogLevel.Noisy, "Limit reached"); + irqPending.Value = true; + UpdateInterrupts(); + + if(reloadValue == 0) + { + this.Log(LogLevel.Noisy, "No realod value - disabling the timer"); + innerTimer.Enabled = false; + } + innerTimer.Limit = reloadValue; + }; + DefineRegisters(); + } + + public override void Reset() + { + base.Reset(); + innerTimer.Reset(); + latchedValue = 0; + loadValue = 0; + reloadValue = 0; + + UpdateInterrupts(); + } + + public GPIO IRQ { + get; + set; + } + + public long Size { get { return 0x20; }} + + private void DefineRegisters() + { + Registers.Load.Define32(this) + .WithValueField(0, 32, name: "LOAD", writeCallback: (_, val) => + { + loadValue = val; + }); + ; + + // RELOAD0 contains most significant 8 bits + // RELOAD3 contains least significant 8 bits + Registers.Reload.Define32(this) + .WithValueField(0, 32, name: "RELOAD", writeCallback: (_, val) => + { + reloadValue = val; + }); + ; + + Registers.TimerEnable.Define32(this) + .WithFlag(0, name: "ENABLE", writeCallback: (_, val) => + { + if(innerTimer.Enabled == val) + { + return; + } + + if(val) + { + innerTimer.Limit = loadValue; + this.Log(LogLevel.Noisy, "Enabling timer. Load value: 0x{0:X}, reload value: 0x{1:X}", loadValue, reloadValue); + } + + innerTimer.Enabled = val; + }) + ; + + Registers.TimerUpdateValue.Define32(this) + .WithFlag(0, FieldMode.WriteOneToClear, name: "UPDATE_VALUE", writeCallback: (_, val) => + { + if(val) + { + latchedValue = (uint)innerTimer.Value; + } + }); + ; + + // VALUE0 contains most significant 8 bits + // VALUE3 contains least significant 8 bits + Registers.Value.Define32(this) + .WithValueField(0, 32, FieldMode.Read, name: "VALUE", valueProviderCallback: _ => + { + return latchedValue; + }); + ; + + Registers.EventStatus.Define32(this) + .WithFlag(0, FieldMode.Read, name: "EV_STATUS", valueProviderCallback: _ => innerTimer.Value == 0) + ; + + Registers.EventPending.Define32(this) + .WithFlag(0, out irqPending, FieldMode.Read | FieldMode.WriteOneToClear, name: "EV_PENDING", changeCallback: (_, __) => UpdateInterrupts()) + ; + + Registers.EventEnable.Define32(this) + .WithFlag(0, out irqEnabled, name: "EV_ENABLE", changeCallback: (_, __) => UpdateInterrupts()) + ; + } + + private void UpdateInterrupts() + { + this.Log(LogLevel.Noisy, "Setting IRQ: {0}", irqPending.Value && irqEnabled.Value); + IRQ.Set(irqPending.Value && irqEnabled.Value); + } + + private IFlagRegisterField irqEnabled; + private IFlagRegisterField irqPending; + + private uint latchedValue; + private uint loadValue; + private uint reloadValue; + + private readonly LimitTimer innerTimer; + + private const int SubregistersCount = 1; + + private enum Registers + { + Load = 0x00, + + Reload = 0x04, + + TimerEnable = 0x08, + TimerUpdateValue = 0x0C, + + Value = 0x10, + + EventStatus = 0x14, + EventPending = 0x18, + EventEnable = 0x1C + } + } +}