1 //
2 // Copyright (c) 2010-2025 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 Antmicro.Renode.Core;
9 using Antmicro.Renode.Peripherals.CPU;
10 using Antmicro.Renode.Peripherals.Bus;
11 using Antmicro.Renode.Time;
12 using ELFSharp.ELF;
13 
14 namespace Antmicro.Renode.Peripherals.SystemC
15 {
16     public abstract class SystemCCPU : BaseCPU, IGPIOReceiver, ITimeSink, IDisposable
17     {
SystemCCPU(IMachine machine, string address, int port, string cpuType, Endianess endianess = Endianess.LittleEndian, CpuBitness bitness = CpuBitness.Bits32, int timeSyncPeriodUS = 1000, bool disableTimeoutCheck = false)18         public SystemCCPU(IMachine machine, string address, int port, string cpuType, Endianess endianess = Endianess.LittleEndian, CpuBitness bitness = CpuBitness.Bits32, int timeSyncPeriodUS = 1000, bool disableTimeoutCheck = false)
19             : base(0, cpuType, machine, endianess, bitness)
20         {
21             systemCPeripheral = new SystemCPeripheral(machine, address, port, timeSyncPeriodUS, disableTimeoutCheck);
22         }
23 
SetRegisterValue32(int register, uint value)24         public virtual void SetRegisterValue32(int register, uint value)
25         {
26             systemCPeripheral.WriteRegister(4, (long)register, value);
27         }
28 
GetRegisterValue32(int register)29         public virtual uint GetRegisterValue32(int register)
30         {
31             return (uint)systemCPeripheral.ReadRegister(4, (long)register);
32         }
33 
Dispose()34         public override void Dispose()
35         {
36             systemCPeripheral.Dispose();
37         }
38 
ExecuteInstructions(ulong numberOfInstructionsToExecute, out ulong numberOfExecutedInstructions)39         public override ExecutionResult ExecuteInstructions(ulong numberOfInstructionsToExecute, out ulong numberOfExecutedInstructions)
40         {
41             numberOfExecutedInstructions = numberOfInstructionsToExecute;
42             totalExecutedInstructions += numberOfInstructionsToExecute;
43             return ExecutionResult.Ok;
44         }
45 
OnGPIO(int number, bool value)46         public void OnGPIO(int number, bool value)
47         {
48             systemCPeripheral.OnGPIO(number, value);
49         }
50 
AddDirectConnection(byte connectionIndex, IDirectAccessPeripheral target)51         public void AddDirectConnection(byte connectionIndex, IDirectAccessPeripheral target)
52         {
53             systemCPeripheral.AddDirectConnection(connectionIndex, target);
54         }
55 
ReadQuadWord(long offset)56         public ulong ReadQuadWord(long offset)
57         {
58             return systemCPeripheral.ReadQuadWord(offset);
59         }
60 
WriteQuadWord(long offset, ulong value)61         public void WriteQuadWord(long offset, ulong value)
62         {
63             systemCPeripheral.WriteQuadWord(offset, value);
64         }
65 
ReadDoubleWord(long offset)66         public uint ReadDoubleWord(long offset)
67         {
68             return systemCPeripheral.ReadDoubleWord(offset);
69         }
70 
WriteDoubleWord(long offset, uint value)71         public void WriteDoubleWord(long offset, uint value)
72         {
73             systemCPeripheral.WriteDoubleWord(offset, value);
74         }
75 
ReadWord(long offset)76         public ushort ReadWord(long offset)
77         {
78             return systemCPeripheral.ReadWord(offset);
79         }
80 
WriteWord(long offset, ushort value)81         public void WriteWord(long offset, ushort value)
82         {
83             systemCPeripheral.WriteWord(offset, value);
84         }
85 
ReadByte(long offset)86         public byte ReadByte(long offset)
87         {
88             return systemCPeripheral.ReadByte(offset);
89         }
90 
WriteByte(long offset, byte value)91         public void WriteByte(long offset, byte value)
92         {
93             systemCPeripheral.WriteByte(offset, value);
94         }
95 
ReadDirect(byte dataLength, long offset, byte connectionIndex)96         public ulong ReadDirect(byte dataLength, long offset, byte connectionIndex)
97         {
98             return systemCPeripheral.ReadDirect(dataLength, offset, connectionIndex);
99         }
100 
WriteDirect(byte dataLength, long offset, ulong value, byte connectionIndex)101         public void WriteDirect(byte dataLength, long offset, ulong value, byte connectionIndex)
102         {
103             systemCPeripheral.WriteDirect(dataLength, offset, value, connectionIndex);
104         }
105 
Reset()106         public override void Reset()
107         {
108             totalExecutedInstructions = 0;
109             systemCPeripheral.Reset();
110             base.Reset();
111         }
112 
113         public override ulong ExecutedInstructions => totalExecutedInstructions;
114 
115         public string SystemCExecutablePath
116         {
117             get => systemCPeripheral.SystemCExecutablePath;
118             set => systemCPeripheral.SystemCExecutablePath = value;
119         }
120 
121         private readonly SystemCPeripheral systemCPeripheral;
122         private ulong totalExecutedInstructions;
123     }
124 }
125