From a2a23bd69cb47367f865e6e6971f99b950385449 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benito=20Palacios=20S=C3=A1nchez?= Date: Sun, 15 Mar 2015 15:10:16 +0100 Subject: [PATCH] Implemented register manager for #13. Need tests. --- NitroDebugger/NitroDebugger.csproj | 1 + NitroDebugger/RSP/GdbClient.cs | 6 ++ NitroDebugger/RSP/RegisterManager.cs | 100 +++++++++++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 NitroDebugger/RSP/RegisterManager.cs diff --git a/NitroDebugger/NitroDebugger.csproj b/NitroDebugger/NitroDebugger.csproj index 2f72818..0c50c21 100644 --- a/NitroDebugger/NitroDebugger.csproj +++ b/NitroDebugger/NitroDebugger.csproj @@ -81,6 +81,7 @@ + diff --git a/NitroDebugger/RSP/GdbClient.cs b/NitroDebugger/RSP/GdbClient.cs index 03bb65a..5501b42 100644 --- a/NitroDebugger/RSP/GdbClient.cs +++ b/NitroDebugger/RSP/GdbClient.cs @@ -48,6 +48,7 @@ namespace NitroDebugger.RSP this.Connection = new ConnectionManager(this); this.Execution = new ExecutionManager(this); this.Stream = new GdbStream(this); + this.Registers = new RegisterManager(this); this.Root = new GameFile("root", null); } @@ -83,6 +84,11 @@ namespace NitroDebugger.RSP private set; } + public RegisterManager Registers { + get; + private set; + } + public ErrorCode Error { get; private set; diff --git a/NitroDebugger/RSP/RegisterManager.cs b/NitroDebugger/RSP/RegisterManager.cs new file mode 100644 index 0000000..3b5c39e --- /dev/null +++ b/NitroDebugger/RSP/RegisterManager.cs @@ -0,0 +1,100 @@ +// +// RegisterManager.cs +// +// Author: +// Benito Palacios Sánchez +// +// Copyright (c) 2015 Benito Palacios Sánchez +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +using System; +using NitroDebugger.RSP.Packets; + +namespace NitroDebugger.RSP +{ + public class RegisterManager + { + private Register[] registers; + + public RegisterManager(GdbClient client) + { + this.Client = client; + + this.registers = new Register[17]; + for (int i = 0; i < 16; i++) + this.registers[i] = new Register((RegisterType)i, 0x00); + this.registers[16] = new Register(RegisterType.CPSR, 0x00); + } + + public GdbClient Client { + get; + private set; + } + + public Register[] GetRegisters() + { + ReadRegisters(); // TODO: Call it, only when invalidated + return this.registers; + } + + public Register GetRegister(RegisterType type) + { + ReadRegisters(); + if (type != RegisterType.CPSR) + return this.registers[(int)type]; + else + return this.registers[16]; + } + + public void SetRegisters(Register[] registers) + { + this.registers = registers; + WriteRegisters(); + } + + public void SetRegister(Register register) + { + if (register.Type != RegisterType.CPSR) + this.registers[(int)register.Type] = register; + else + this.registers[16] = register; + + WriteSingleRegister(register); + } + + private void ReadRegisters() + { + ReadRegisters command = new ReadRegisters(); + RegistersReply reply = + this.Client.SendCommandWithoutErrorReply(command); + if (reply == null) + return; + + this.registers = reply.GetRegisters(); + } + + private void WriteRegisters() + { + WriteRegisters command = new WriteRegisters(this.registers); + this.Client.SendCommandWithoutErrorReply(command); + } + + private void WriteSingleRegister(Register register) + { + WriteSingleRegister command = new WriteSingleRegister(register); + this.Client.SendCommandWithoutErrorReply(command); + } + } +} +