emulation: engines: cache registers in RAM

Don't consult RAM every time we want to read a register. This improves
performance at the cost of potentially having stale registers.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2021-07-15 18:10:38 +08:00
parent 31748ab4d4
commit 3f1593e56e

View file

@ -28,35 +28,56 @@ namespace Antmicro.Renode.Peripherals.Miscellaneous
this.data = new UInt32[size / 4]; this.data = new UInt32[size / 4];
this.FIELD_MAX = BigInteger.Pow(2, 255) - 19; this.FIELD_MAX = BigInteger.Pow(2, 255) - 19;
this.UINT_MAX = BigInteger.Pow(2, 256) - 1; this.UINT_MAX = BigInteger.Pow(2, 256) - 1;
this.c0 = new BigInteger(0); this.ZERO = new BigInteger(0);
this.c1 = new BigInteger(1); this.ONE = new BigInteger(1);
this.c2 = new BigInteger(121665); // (A-2)/4 this.AM24 = new BigInteger(121665); // (A-2)/4
this.c3 = this.FIELD_MAX; this.AP24 = new BigInteger(121666); // (A+2)/4
this.c4 = new BigInteger(121666); // (A+2)/4 this.FIVE = new BigInteger(5);
this.c5 = new BigInteger(5); this.TEN = new BigInteger(10);
this.c6 = new BigInteger(10); this.TWENTY = new BigInteger(20);
this.c7 = new BigInteger(20); this.FIFTY = new BigInteger(50);
this.c8 = new BigInteger(50); this.ONE_HUNDRED = new BigInteger(100);
this.c9 = new BigInteger(100); this.registers = new BigInteger[32];
for (var i = 0; i < this.registers.Length; i++)
{
this.registers[i] = this.ZERO;
}
} }
public void SyncRegistersFromRam(ulong rf)
{
for (var i = 0; i < this.registers.Length; i++)
{
this.registers[i] = ReadRegisterFromRam(rf, (ulong)i);
}
}
public void SyncRegistersToRam(ulong rf)
{
for (var i = 0; i < this.registers.Length; i++)
{
WriteRegisterToRam(rf, (ulong)i, this.registers[i]);
}
}
public BigInteger UINT_MAX;
public BigInteger ZERO;
public BigInteger ONE;
public BigInteger AM24;
public BigInteger FIELD_MAX; public BigInteger FIELD_MAX;
private BigInteger UINT_MAX; public BigInteger AP24;
private BigInteger c0; public BigInteger FIVE;
private BigInteger c1; public BigInteger TEN;
private BigInteger c2; public BigInteger TWENTY;
private BigInteger c3; public BigInteger FIFTY;
private BigInteger c4; public BigInteger ONE_HUNDRED;
private BigInteger c5;
private BigInteger c6;
private BigInteger c7;
private BigInteger c8;
private BigInteger c9;
private Machine machine; private Machine machine;
private Engine engine; private Engine engine;
private long size; private long size;
public UInt32[] data; public UInt32[] data;
public BigInteger[] registers;
public long Size { get { return size; } } public long Size { get { return size; } }
public void WriteDoubleWord(long address, uint value) public void WriteDoubleWord(long address, uint value)
@ -69,32 +90,36 @@ namespace Antmicro.Renode.Peripherals.Miscellaneous
return this.data[offset / 4]; return this.data[offset / 4];
} }
public BigInteger ReadRegister(ulong rf, ulong register, bool constant) public BigInteger ReadRegister(ulong register, bool constant)
{ {
// this.Log(LogLevel.Error, "Reading register {0} (const? {1}) from rf {2}", register, constant, rf); // this.Log(LogLevel.Error, "Reading register {0} (const? {1}) from rf {2}", register, constant, rf);
if (constant) if (constant)
{ {
switch (register) switch (register)
{ {
case 0: return this.c0; case 0: return this.ZERO;
case 1: return this.c1; case 1: return this.ONE;
case 2: return this.c2; case 2: return this.AM24;
case 3: return this.c3; case 3: return this.FIELD_MAX;
case 4: return this.c4; case 4: return this.AP24;
case 5: return this.c5; case 5: return this.FIVE;
case 6: return this.c6; case 6: return this.TEN;
case 7: return this.c7; case 7: return this.TWENTY;
case 8: return this.c8; case 8: return this.FIFTY;
case 9: return this.c9; case 9: return this.ONE_HUNDRED;
default: return new BigInteger(0); default: return this.ZERO;
} }
} }
return this.registers[register];
}
public BigInteger ReadRegisterFromRam(ulong rf, ulong register)
{
var src_bytes = new byte[33]; // Keep leading byte 0 to force unsigned var src_bytes = new byte[33]; // Keep leading byte 0 to force unsigned
if ((rf >= 16) || (register >= 32)) // if ((rf >= 16) || (register >= 32))
{ // {
throw new Exception(String.Format("Register #{0} or RegisterFile {1} is out of range", register, rf)); // throw new Exception(String.Format("Register #{0} or RegisterFile {1} is out of range", register, rf));
} // }
for (ulong i = 0; i < 32; i += 4) for (ulong i = 0; i < 32; i += 4)
{ {
@ -117,14 +142,14 @@ namespace Antmicro.Renode.Peripherals.Miscellaneous
return new BigInteger(src_bytes); return new BigInteger(src_bytes);
} }
public void WriteRegister(long rf, long register, BigInteger value) public void WriteRegisterToRam(ulong rf, ulong register, BigInteger value)
{ {
if ((rf >= 16) || (register >= 32)) // if ((rf >= 16) || (register >= 32))
{ // {
throw new Exception(String.Format("Register #{0} or RegisterFile {1} is out of range", register, rf)); // throw new Exception(String.Format("Register #{0} or RegisterFile {1} is out of range", register, rf));
} // }
var bytes = (value & this.UINT_MAX).ToByteArray(); var bytes = value.ToByteArray();
if (bytes.Length < 32) if (bytes.Length < 32)
{ {
bytes = bytes.Concat(new byte[32 - bytes.Length]); bytes = bytes.Concat(new byte[32 - bytes.Length]);
@ -140,6 +165,11 @@ namespace Antmicro.Renode.Peripherals.Miscellaneous
} }
} }
public void WriteRegister(ulong register, BigInteger value)
{
this.registers[register] = value & this.UINT_MAX;
}
public String FormatValue(BigInteger value) public String FormatValue(BigInteger value)
{ {
var trimmed_value = value & this.UINT_MAX; var trimmed_value = value & this.UINT_MAX;
@ -308,10 +338,13 @@ namespace Antmicro.Renode.Peripherals.Miscellaneous
private void EngineThread() private void EngineThread()
{ {
// this.Log(LogLevel.Error, "ENGINE: Beginning execution. Current MPC: 0x{0:X}", this.mpstart);
try try
{ {
// this.Log(LogLevel.Error, "ENGINE: Beginning execution. Current MPC: 0x{0:X}", this.mpc); // Interlocked.Exchange(ref this.mpc, this.mpstart);
this.mpc = this.mpstart; this.mpc = this.mpstart;
var window = this.window;
this.engineRam.SyncRegistersFromRam(window);
while (true) while (true)
{ {
@ -326,74 +359,75 @@ namespace Antmicro.Renode.Peripherals.Miscellaneous
switch (op.Op) switch (op.Op)
{ {
case 0: // PSA case 0: // PSA
this.engineRam.WriteRegister(this.window, op.Wd, this.engineRam.ReadRegister(this.window, op.Ra, op.Ca)); // this.engineRam.WriteRegister(op.Wd, this.engineRam.ReadRegister(op.Ra, op.Ca));
this.engineRam.WriteRegister(op.Wd, this.engineRam.ReadRegister(op.Ra, op.Ca));
break; break;
case 1: // PSB case 1: // PSB
this.engineRam.WriteRegister(this.window, op.Wd, this.engineRam.ReadRegister(this.window, op.Rb, op.Cb)); this.engineRam.WriteRegister(op.Wd, this.engineRam.ReadRegister(op.Rb, op.Cb));
break; break;
case 2: // MSK case 2: // MSK
if ((this.engineRam.ReadRegister(this.window, op.Ra, op.Ca) & 1) != 0) if ((this.engineRam.ReadRegister(op.Ra, op.Ca) & 1) != 0)
{ {
this.engineRam.WriteRegister(this.window, op.Wd, this.engineRam.ReadRegister(this.window, op.Rb, op.Cb)); this.engineRam.WriteRegister(op.Wd, this.engineRam.ReadRegister(op.Rb, op.Cb));
} }
else else
{ {
this.engineRam.WriteRegister(this.window, op.Wd, new BigInteger(0)); this.engineRam.WriteRegister(op.Wd, this.engineRam.ZERO);
} }
break; break;
case 3: // XOR case 3: // XOR
this.engineRam.WriteRegister(this.window, op.Wd, this.engineRam.WriteRegister(op.Wd,
(BigInteger.Pow(2, 256) | this.engineRam.ReadRegister(this.window, op.Ra, op.Ca)) ^ this.engineRam.ReadRegister(this.window, op.Rb, op.Cb)); (BigInteger.Pow(2, 256) | this.engineRam.ReadRegister(op.Ra, op.Ca)) ^ this.engineRam.ReadRegister(op.Rb, op.Cb));
break; break;
case 4: // NOT case 4: // NOT
this.engineRam.WriteRegister(this.window, op.Wd, this.engineRam.WriteRegister(op.Wd,
~(this.engineRam.ReadRegister(this.window, op.Ra, op.Ca) | BigInteger.Pow(2, 256))); ~(this.engineRam.ReadRegister(op.Ra, op.Ca) | BigInteger.Pow(2, 256)));
break; break;
case 5: // ADD case 5: // ADD
this.engineRam.WriteRegister(this.window, op.Wd, this.engineRam.WriteRegister(op.Wd,
this.engineRam.ReadRegister(this.window, op.Ra, op.Ca) + this.engineRam.ReadRegister(this.window, op.Rb, op.Cb)); this.engineRam.ReadRegister(op.Ra, op.Ca) + this.engineRam.ReadRegister(op.Rb, op.Cb));
break; break;
case 6: // SUB case 6: // SUB
{ {
var left = this.engineRam.ReadRegister(this.window, op.Ra, op.Ca); var left = this.engineRam.ReadRegister(op.Ra, op.Ca);
var right = this.engineRam.ReadRegister(this.window, op.Rb, op.Cb); var right = this.engineRam.ReadRegister(op.Rb, op.Cb);
var result = left - right; var result = left - right;
// Wrap integer // Wrap integer
if (result < 0) if (result < 0)
{ {
result = BigInteger.Pow(2, 256) - result; result = BigInteger.Pow(2, 256) - result;
} }
this.engineRam.WriteRegister(this.window, op.Wd, result); this.engineRam.WriteRegister(op.Wd, result);
} }
break; break;
case 7: // MUL case 7: // MUL
this.engineRam.WriteRegister(this.window, op.Wd, this.engineRam.WriteRegister(op.Wd,
(this.engineRam.ReadRegister(this.window, op.Ra, op.Ca) (this.engineRam.ReadRegister(op.Ra, op.Ca)
* this.engineRam.ReadRegister(this.window, op.Rb, op.Cb)) * this.engineRam.ReadRegister(op.Rb, op.Cb))
% this.engineRam.FIELD_MAX % this.engineRam.FIELD_MAX
); );
break; break;
case 8: // TRD case 8: // TRD
if (this.engineRam.ReadRegister(this.window, op.Ra, op.Ca) >= this.engineRam.FIELD_MAX) if (this.engineRam.ReadRegister(op.Ra, op.Ca) >= this.engineRam.FIELD_MAX)
{ {
this.engineRam.WriteRegister(this.window, op.Wd, this.engineRam.FIELD_MAX); this.engineRam.WriteRegister(op.Wd, this.engineRam.FIELD_MAX);
} }
else else
{ {
this.engineRam.WriteRegister(this.window, op.Wd, new BigInteger(0)); this.engineRam.WriteRegister(op.Wd, this.engineRam.ZERO);
} }
break; break;
case 9: // BRZ case 9: // BRZ
if (this.engineRam.ReadRegister(this.window, op.Ra, op.Ca) == 0) if (this.engineRam.ReadRegister(op.Ra, op.Ca) == 0)
{ {
this.mpc = (ushort)((int)this.mpc + op.Immediate); this.mpc = (ushort)((int)this.mpc + op.Immediate);
} }
@ -403,18 +437,17 @@ namespace Antmicro.Renode.Peripherals.Miscellaneous
break; break;
case 11: // SHL case 11: // SHL
this.engineRam.WriteRegister(this.window, op.Wd, this.engineRam.WriteRegister(op.Wd, this.engineRam.ReadRegister(op.Ra, op.Ca) << 1);
this.engineRam.ReadRegister(this.window, op.Ra, op.Ca) << 1);
break; break;
case 12: // XBT case 12: // XBT
if ((this.engineRam.ReadRegister(this.window, op.Ra, op.Ca) & (new BigInteger(1) << 254)) != 0) if ((this.engineRam.ReadRegister(op.Ra, op.Ca) & (this.engineRam.ONE << 254)) != 0)
{ {
this.engineRam.WriteRegister(this.window, op.Wd, new BigInteger(1)); this.engineRam.WriteRegister(op.Wd, this.engineRam.ONE);
} }
else else
{ {
this.engineRam.WriteRegister(this.window, op.Wd, new BigInteger(0)); this.engineRam.WriteRegister(op.Wd, this.engineRam.ZERO);
} }
break; break;
@ -422,12 +455,13 @@ namespace Antmicro.Renode.Peripherals.Miscellaneous
this.illegalOpcodeStatus = true; this.illegalOpcodeStatus = true;
throw new Exception("Unhandled opcode"); throw new Exception("Unhandled opcode");
} }
// Interlocked.Increment(ref this.mpc);
this.mpc += 1; this.mpc += 1;
if (op.Op == 10) if (op.Op == 10)
{ {
break; break;
} }
// this.Log(LogLevel.Error, " (Result) {0}", this.engineRam.FormatValue(this.engineRam.ReadRegister(this.window, op.Wd, false))); // this.Log(LogLevel.Error, " (Result) {0}", this.engineRam.FormatValue(this.engineRam.ReadRegister(op.Wd, false)));
} }
} }
catch (Exception e) catch (Exception e)
@ -435,7 +469,8 @@ namespace Antmicro.Renode.Peripherals.Miscellaneous
this.Log(LogLevel.Error, "ENGINE execution exception {0}", e); this.Log(LogLevel.Error, "ENGINE execution exception {0}", e);
} }
this.finishedStatus = true; this.finishedStatus = true;
this.engineExecution = null; // this.engineExecution = null;
this.engineRam.SyncRegistersToRam(window);
this.UpdateInterrupts(); this.UpdateInterrupts();
this.finishedStatus = false; this.finishedStatus = false;
this.illegalOpcodeStatus = false; this.illegalOpcodeStatus = false;
@ -467,8 +502,7 @@ namespace Antmicro.Renode.Peripherals.Miscellaneous
} }
else else
{ {
this.engineExecution = new Thread(new ThreadStart(this.EngineThread)); this.engineExecution = new Thread(this.EngineThread) { Name = "Engine Execution", IsBackground = true };
this.engineExecution.IsBackground = true;
this.engineExecution.Start(); this.engineExecution.Start();
} }
} }
@ -486,7 +520,7 @@ namespace Antmicro.Renode.Peripherals.Miscellaneous
Registers.STATUS.Define(this) Registers.STATUS.Define(this)
.WithFlag(0, name: "RUNNING", valueProviderCallback: (_) => this.engineExecution.IsAlive) .WithFlag(0, name: "RUNNING", valueProviderCallback: (_) => this.engineExecution.IsAlive)
.WithValueField(1, 10, name: "MPC", valueProviderCallback: (_) => 0) .WithValueField(1, 10, name: "MPC", valueProviderCallback: (_) => (uint)this.mpc)
.WithFlag(11, name: "PAUSE_GNT", valueProviderCallback: (_) => false) .WithFlag(11, name: "PAUSE_GNT", valueProviderCallback: (_) => false)
; ;
@ -558,7 +592,7 @@ namespace Antmicro.Renode.Peripherals.Miscellaneous
private UInt16 mpstart; private UInt16 mpstart;
private UInt16 mplen; private UInt16 mplen;
private UInt16 mpresume; private UInt16 mpresume;
private UInt16 mpc; private int mpc;
private IFlagRegisterField powerIsOn; private IFlagRegisterField powerIsOn;
private IFlagRegisterField pauseRequest; private IFlagRegisterField pauseRequest;