Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/RP2040.Core/Cpu/InstructionDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace RP2040.Core.Cpu;

public sealed unsafe class InstructionDecoder : IDisposable

Check warning on line 9 in src/RP2040.Core/Cpu/InstructionDecoder.cs

View workflow job for this annotation

GitHub Actions / Unit Tests

Make sure that using "unsafe" is safe here. (https://rules.sonarsource.com/csharp/RSPEC-6640)

Check warning on line 9 in src/RP2040.Core/Cpu/InstructionDecoder.cs

View workflow job for this annotation

GitHub Actions / Unit Tests

Make sure that using "unsafe" is safe here. (https://rules.sonarsource.com/csharp/RSPEC-6640)
{
public static InstructionDecoder Instance { get; } = new InstructionDecoder();

Expand Down Expand Up @@ -90,6 +90,8 @@
new OpcodeRule(0xFFC0, 0xBAC0, &BitOps.Revsh),
// REV (Rd, Rn)
new OpcodeRule(0xFFC0, 0xBA00, &BitOps.Rev),
// SBCS (Rn, Rm)
new OpcodeRule(0xFFC0, 0x4180, &ArithmeticOps.Sbcs),
// ================================================================
// GROUP 4: Mask 0xFF87 (High Register Special Cases)
// CRITICAL: These are specific cases of the 0xFF00 generic group.
Expand Down Expand Up @@ -233,7 +235,7 @@

private static void HandleUndefined(ushort opcode, CortexM0Plus cpu)
{
throw new Exception($"Undefined Opcode: 0x{opcode:X4} PC={cpu.Registers.PC:X8}");

Check warning on line 238 in src/RP2040.Core/Cpu/InstructionDecoder.cs

View workflow job for this annotation

GitHub Actions / Unit Tests

'System.Exception' should not be thrown by user code. (https://rules.sonarsource.com/csharp/RSPEC-112)

Check warning on line 238 in src/RP2040.Core/Cpu/InstructionDecoder.cs

View workflow job for this annotation

GitHub Actions / Unit Tests

'System.Exception' should not be thrown by user code. (https://rules.sonarsource.com/csharp/RSPEC-112)
}

[ExcludeFromCodeCoverage]
Expand Down
13 changes: 13 additions & 0 deletions src/RP2040.Core/Cpu/Instructions/ArithmeticOps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,19 @@ public static void Rsbs(ushort opcode, CortexM0Plus cpu)
ptrRd = SubWithFlags(cpu, 0, valRn);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Sbcs(ushort opcode, CortexM0Plus cpu)
{
var rm = (opcode >> 3) & 0x7;
var rdn = opcode & 0x7;

ref var ptrRdn = ref cpu.Registers[rdn];
var valRm = cpu.Registers[rm];
var carry = cpu.Registers.C ? 1u : 0u;

ptrRdn = AddWithFlags(cpu, ptrRdn, ~valRm, carry);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SubsImmediate3(ushort opcode, CortexM0Plus cpu)
{
Expand Down
7 changes: 7 additions & 0 deletions src/RP2040.Core/Helpers/InstructionEmiter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,13 @@ public static ushort Rsbs(uint rd, uint rn)
return (ushort)(0x4240 | (rn & 0x7) << 3 | (rd & 0x7));
}

public static ushort Sbcs(uint rn, uint rm)
{
if (rm > 7 || rn > 7)
throw new ArgumentException(LowRegisterIndexOutOfRange);
return (ushort)(0x4180 | ((rm & 0x7) << 3) | (rn & 0x7));
}

public static ushort SubSp(uint imm)
{
if (imm > 508)
Expand Down
1 change: 1 addition & 0 deletions tests/RP2040.Core.Tests/Cpu/InstructionDecoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public static TheoryData<string, ushort, ulong> GetInstructionTestCases()
);
Add("Muls", InstructionEmiter.Muls(R0, R2), &ArithmeticOps.Muls);
Add("Rsbs", InstructionEmiter.Rsbs(R0, R3), &ArithmeticOps.Rsbs);
Add("Sbcs", InstructionEmiter.Sbcs(R0, R3), &ArithmeticOps.Sbcs);
Add("SubsRegister", InstructionEmiter.SubsReg(R1, R2, R4), &ArithmeticOps.SubsRegister);
Add("SubsImm3", InstructionEmiter.SubsImm3(R1, R2, 3), &ArithmeticOps.SubsImmediate3);
Add("SubsImm8", InstructionEmiter.SubsImm8(R1, 0x10), &ArithmeticOps.SubsImmediate8);
Expand Down
45 changes: 45 additions & 0 deletions tests/RP2040.Core.Tests/Cpu/Instructions/ArithmeticOpsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -865,4 +865,49 @@ public void Should_Execute_SubsR5R3R2_And_Set_ZCFlags()
Cpu.Registers.V.Should().BeFalse();
}
}

public class Sbcs : CpuTestBase
Comment thread
lmSeryi marked this conversation as resolved.
{
[Fact]
public void Should_Execute_SbcsR0R3()
{
// Arrange
var opcode = InstructionEmiter.Sbcs(R0, R3);
Bus.WriteHalfWord(0x20000000, opcode);
Cpu.Registers.R0 = 100;
Cpu.Registers.R3 = 55;
Cpu.Registers.C = false;

// Act
Cpu.Step();

// Assert
Cpu.Registers.R0.Should().Be(44);
Cpu.Registers.N.Should().BeFalse();
Cpu.Registers.Z.Should().BeFalse();
Cpu.Registers.C.Should().BeTrue();
Cpu.Registers.V.Should().BeFalse();
}

[Fact]
public void Should_Execute_SbcsR0R3_To_Be_0()
{
// Arrange
var opcode = InstructionEmiter.Sbcs(R0, R3);
Bus.WriteHalfWord(0x20000000, opcode);
Cpu.Registers.R0 = 0;
Cpu.Registers.R3 = 0xffffffff;
Cpu.Registers.C = false;

// Act
Cpu.Step();

// Assert
Cpu.Registers.R0.Should().Be(0);
Cpu.Registers.N.Should().BeFalse();
Cpu.Registers.Z.Should().BeTrue();
Cpu.Registers.C.Should().BeFalse();
Cpu.Registers.V.Should().BeFalse();
}
}
}