Skip to content

Commit

Permalink
Merge pull request #628 from mbbsemu/GE-Fixes
Browse files Browse the repository at this point in the history
PHAPI, `TOKOPT`, `CHROPT` and `SAMTO` Fixes
  • Loading branch information
paladine authored Jul 24, 2024
2 parents 8cdd708 + dfc6c75 commit 75a89ef
Show file tree
Hide file tree
Showing 9 changed files with 419 additions and 53 deletions.
125 changes: 125 additions & 0 deletions MBBSEmu.Tests/CPU/IMUL_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
using Iced.Intel;
using Xunit;
using static Iced.Intel.AssemblerRegisters;

namespace MBBSEmu.Tests.CPU
{
public class IMUL_Tests : CpuTestBase
{
[Theory]
[InlineData(127, 127, 1, 63, true, true)]
[InlineData(-1, -1, 1, 0, false, false)]
[InlineData(-127, -1, 127, 0, false, false)]
[InlineData(-127, 2, 2, -1, true, true)]
[InlineData(-127, -127, 1, 63, true, true)]
[InlineData(127, -1, -127, -1, false, false)]
[InlineData(5, 5, 25, 0, false, false)]
public void IMUL_8_R8_Test(sbyte alValue, sbyte valueToMultiply, sbyte expectedALValue, sbyte expectedAHValue, bool carryFlag,
bool overflowFlag)
{
Reset();
mbbsEmuCpuRegisters.AL = (byte)alValue;
mbbsEmuCpuRegisters.BL = (byte)valueToMultiply;

var instructions = new Assembler(16);
instructions.imul(bl);
CreateCodeSegment(instructions);

mbbsEmuCpuCore.Tick();

//Verify Results
Assert.Equal(expectedALValue, (sbyte)mbbsEmuCpuRegisters.AL);
Assert.Equal(expectedAHValue, (sbyte)mbbsEmuCpuRegisters.AH);
Assert.Equal(carryFlag, mbbsEmuCpuRegisters.CarryFlag);
Assert.Equal(overflowFlag, mbbsEmuCpuRegisters.OverflowFlag);
}

[Theory]
[InlineData(32767, 1, 32767, false, false)] // Max positive * 1
[InlineData(-32768, 1, -32768, false, false)] // Max negative * 1
[InlineData(100, 100, 10000, false, false)] // Positive * Positive
[InlineData(-100, -100, 10000, false, false)] // Negative * Negative
[InlineData(100, -100, -10000, false, false)] // Positive * Negative
[InlineData(32767, 2, -2, true, true)] // Positive overflow case
[InlineData(-32768, 2, 0, true, true)] // Negative overflow case
[InlineData(0, 32767, 0, false, false)] // Zero * Positive
[InlineData(0, -32768, 0, false, false)] // Zero * Negative
[InlineData(0, 0, 0, false, false)] // Zero * Zero
public void IMUL_16_R16_Test(short axValue, short valueToMultiply, short expectedValue, bool carryFlag,
bool overflowFlag)
{
Reset();
mbbsEmuCpuRegisters.AX = (ushort)axValue;
mbbsEmuCpuRegisters.BX = (ushort)valueToMultiply;

var instructions = new Assembler(16);
instructions.imul(bx);
CreateCodeSegment(instructions);

mbbsEmuCpuCore.Tick();

//Verify Results
Assert.Equal(expectedValue, (short)mbbsEmuCpuRegisters.AX);
Assert.Equal(carryFlag, mbbsEmuCpuRegisters.CarryFlag);
Assert.Equal(overflowFlag, mbbsEmuCpuRegisters.OverflowFlag);
}


[Theory]
[InlineData(1, -1, -1, false, false)]
[InlineData(-1, -1, 1, false, false)]
[InlineData(-127, -1, 127, false, false)]
[InlineData(127, -1, -127, false, false)]
[InlineData(short.MaxValue, -1, short.MinValue + 1, false, false)]
[InlineData(short.MaxValue, -2, 2, true, true)]
public void IMUL_16_R16_3OP_Test(short bxValue, short valueToMultiply, short expectedValue, bool carryFlag,
bool overflowFlag)
{
Reset();
mbbsEmuCpuRegisters.BX = (ushort)bxValue;

var instructions = new Assembler(16);
//AX = BX * ValueToMultiply
instructions.imul(ax, bx, valueToMultiply);
CreateCodeSegment(instructions);

mbbsEmuCpuCore.Tick();

//Verify Results
Assert.Equal(expectedValue, (short)mbbsEmuCpuRegisters.AX);
Assert.Equal(carryFlag, mbbsEmuCpuRegisters.CarryFlag);
Assert.Equal(overflowFlag, mbbsEmuCpuRegisters.OverflowFlag);
}

[Theory]
[InlineData(1, -1, -1, false, false)]
[InlineData(5, 10, 50, false, false)]
[InlineData(-1, -1, 1, false, false)]
[InlineData(-127, -1, 127, false, false)]
[InlineData(127, -1, -127, false, false)]
[InlineData(short.MaxValue, -1, short.MinValue + 1, false, false)]
[InlineData(short.MaxValue, -2, 2, true, true)]
public void IMUL_16_M16_3OP_Test(short memoryValue, short valueToMultiply, short expectedValue, bool carryFlag,
bool overflowFlag)
{
Reset();

//Setup Memory
CreateDataSegment(new byte[ushort.MaxValue]);
mbbsEmuMemoryCore.SetWord(2,0, (ushort)memoryValue);
mbbsEmuCpuRegisters.DS = 2;

var instructions = new Assembler(16);
//AX == DS:[0] * ValueToMultiply
instructions.imul(ax, __word_ptr[0], valueToMultiply);
CreateCodeSegment(instructions);

mbbsEmuCpuCore.Tick();

//Verify Results
Assert.Equal(expectedValue, (short)mbbsEmuCpuRegisters.AX);
Assert.Equal(carryFlag, mbbsEmuCpuRegisters.CarryFlag);
Assert.Equal(overflowFlag, mbbsEmuCpuRegisters.OverflowFlag);
}
}
}
2 changes: 0 additions & 2 deletions MBBSEmu.Tests/CPU/MUL_Tests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Iced.Intel;
using MBBSEmu.CPU;
using MBBSEmu.Extensions;
using Xunit;
using static Iced.Intel.AssemblerRegisters;

Expand Down
62 changes: 62 additions & 0 deletions MBBSEmu.Tests/ExportedModules/Majorbbs/tokopt_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using MBBSEmu.Memory;
using MBBSEmu.Module;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;

namespace MBBSEmu.Tests.ExportedModules.Majorbbs
{
public class tokopt_Tests : ExportedModuleTestBase
{
private const int TOKPT_ORDINAL = 602;

[Theory]
[InlineData(new[] { "TEST1", "TEST2", "TEST3" }, "TEST", 0)]
[InlineData(new[] { "TEST1", "TEST2", "TEST3" }, "TEST2", 2)]
[InlineData(new[] { "TEST1", "TEST2", "TEST3" }, "Testing Multiple Words: TEST", 0)]
[InlineData(new[] { "TEST1", "TEST2", "TEST3" }, "Testing Multiple Words: TEST3", 3)]
public void tokopt_Test(string[] tokens, string valueToTest, ushort expectedResult)
{
//Reset State
Reset();

//Allocate Memory for each Token which will be passed in as params to the function
var tokenPointers = new List<FarPtr>();
foreach(var token in tokens)
{
var tokenPointer =
mbbsEmuMemoryCore.AllocateVariable($"TOKEN{tokenPointers.Count + 1}", (ushort)(token.Length + 1));
mbbsEmuMemoryCore.SetArray(tokenPointer, Encoding.ASCII.GetBytes(token));

tokenPointers.Add(tokenPointer);
}


//Set Argument Values to be Passed In
var mcvPointer = (ushort)majorbbs.McvPointerDictionary.Allocate(new McvFile("TEST.MCV",
new Dictionary<int, byte[]> { { 0, Encoding.ASCII.GetBytes(valueToTest) } }));

mbbsEmuMemoryCore.SetPointer("CURRENT-MCV", new FarPtr(0xFFFF, mcvPointer));

//Build Parameters List
var parameters = new List<ushort>();
parameters.Add(0); //Ordinal of MCV Value
//Add Token Pointers
foreach (var tokenPointer in tokenPointers)
{
parameters.Add(tokenPointer.Offset);
parameters.Add(tokenPointer.Segment);
}
//Terminate with Null Pointer
parameters.Add(0);
parameters.Add(0);

//Execute Test
ExecuteApiTest(HostProcess.ExportedModules.Majorbbs.Segment, TOKPT_ORDINAL, parameters);

//Verify Results
Assert.Equal(expectedResult, mbbsEmuCpuRegisters.AX);
}
}
}
8 changes: 4 additions & 4 deletions MBBSEmu.Tests/MBBSEmu.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Iced" Version="1.10.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Loading

0 comments on commit 75a89ef

Please sign in to comment.