Implements Write Registers command for #12

This commit is contained in:
Benito Palacios Sánchez 2015-03-15 14:28:16 +01:00 committed by Superfranci99
parent 145c75a0b3
commit 8830ed37a7
3 changed files with 105 additions and 7 deletions

View File

@ -244,7 +244,20 @@ namespace UnitTests
[Test] [Test]
public void FactoryRegistersReply() public void FactoryRegistersReply()
{ {
Register[] expected = new Register[] { Register[] expected = CreateRegisters();
byte[] binRegisters = CreateNetworkRegisters();
string regString = BitConverter.ToString(binRegisters).Replace("-", "");
ReadRegisters readRegisters = new ReadRegisters();
ReplyPacket reply = ReplyPacketFactory.CreateReplyPacket(regString, readRegisters);
Assert.IsInstanceOf<RegistersReply>(reply);
Assert.AreEqual(expected, ((RegistersReply)reply).GetRegisters());
}
private Register[] CreateRegisters()
{
return new Register[] {
new Register(RegisterType.R0, 0), new Register(RegisterType.R1, 1), new Register(RegisterType.R0, 0), new Register(RegisterType.R1, 1),
new Register(RegisterType.R2, 2), new Register(RegisterType.R3, 3), new Register(RegisterType.R2, 2), new Register(RegisterType.R3, 3),
new Register(RegisterType.R4, 4), new Register(RegisterType.R5, 5), new Register(RegisterType.R4, 4), new Register(RegisterType.R5, 5),
@ -255,7 +268,11 @@ namespace UnitTests
new Register(RegisterType.SP, 14), new Register(RegisterType.PC, 15), new Register(RegisterType.SP, 14), new Register(RegisterType.PC, 15),
new Register(RegisterType.CPSR, 255) new Register(RegisterType.CPSR, 255)
}; };
byte[] binRegisters = new byte[] { }
private byte[] CreateNetworkRegisters()
{
return new byte[] {
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x02, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
0x04, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
@ -281,13 +298,19 @@ namespace UnitTests
// CPSR // CPSR
0xFF, 0x0, 0x0, 0x00 0xFF, 0x0, 0x0, 0x00
}; };
string regString = BitConverter.ToString(binRegisters).Replace("-", ""); }
ReadRegisters readRegisters = new ReadRegisters(); [Test]
ReplyPacket reply = ReplyPacketFactory.CreateReplyPacket(regString, readRegisters); public void CreateWriteRegistersCommand()
{
Register[] registers = CreateRegisters();
string networkRegisters = BitConverter.ToString(CreateNetworkRegisters());
networkRegisters = networkRegisters.Replace("-", "");
Assert.IsInstanceOf<RegistersReply>(reply); WriteRegisters cmd = null;
Assert.AreEqual(expected, ((RegistersReply)reply).GetRegisters()); Assert.DoesNotThrow(() => cmd = new WriteRegisters(registers));
Assert.AreEqual("G", cmd.Command);
Assert.AreEqual("G" + networkRegisters, cmd.Pack());
} }
} }
} }

View File

@ -79,6 +79,7 @@
<Compile Include="RSP\Packets\ReadRegisters.cs" /> <Compile Include="RSP\Packets\ReadRegisters.cs" />
<Compile Include="RSP\Packets\RegistersReply.cs" /> <Compile Include="RSP\Packets\RegistersReply.cs" />
<Compile Include="Register.cs" /> <Compile Include="Register.cs" />
<Compile Include="RSP\Packets\WriteRegisters.cs" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup> <ItemGroup>

View File

@ -0,0 +1,74 @@
//
// WriteRegisters.cs
//
// Author:
// Benito Palacios Sánchez <benito356@gmail.com>
//
// 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 <http://www.gnu.org/licenses/>.
using System;
using System.Linq;
using System.Text;
namespace NitroDebugger.RSP.Packets
{
public class WriteRegisters : CommandPacket
{
private Register[] registers;
public WriteRegisters(Register[] registers)
: base("G")
{
if (registers.Length != 17)
throw new ArgumentException("Invalid number of registers");
if (Enumerable.Range(0, 16).Any(i => registers[i].Type != (RegisterType)i) ||
registers[16].Type != RegisterType.CPSR)
throw new ArgumentException("Invalid registers");
this.registers = registers;
}
public Register[] GetRegisters()
{
return this.registers;
}
protected override string PackArguments()
{
StringBuilder args = new StringBuilder();
for (int i = 0; i < 16; i++)
args.AppendFormat("{0:08}", RegisterDataToString(registers[i]));
// Floating point registers, not used but sent
args.Append(new String('0', 192));
// Floating point status registers, not used but sent
args.Append(new String('0', 8));
// CPRS
args.AppendFormat("{0:04}", RegisterDataToString(registers[16]));
return args.ToString();
}
private string RegisterDataToString(Register reg)
{
return BitConverter.ToString(BitConverter.GetBytes(reg.Value))
.Replace("-", "");
}
}
}