Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update RNG to use Borland C++ RNG Routine #579

Merged
merged 4 commits into from
Jul 15, 2023
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
37 changes: 37 additions & 0 deletions MBBSEmu.Tests/ExportedModules/Majorbbs/rand_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Collections.Generic;
using Xunit;

namespace MBBSEmu.Tests.ExportedModules.Majorbbs
{
/// <summary>
/// Tests verified against results from Borland C++ 4.5 for DOS
/// </summary>
public class rand_Tests : ExportedModuleTestBase
{
private const int RAND_ORDINAL = 486;

[Theory]
[InlineData(1234, 1356, 2236414843)]
[InlineData(2236414843, 29133, 4056779384)]
[InlineData(4056779384, 21998, 3589159641)]
[InlineData(3589159641, 27018, 1770671342)]
[InlineData(1770671342, 30398, 4139675975)]
[InlineData(4139675975, 13799, 904336820)]
[InlineData(904336820, 26300, 3871102533)]
[InlineData(3871102533, 16520, 3230196298)]
[InlineData(3230196298, 26957, 1766679891)]
[InlineData(1766679891, 21523, 1410548784)]
public void srandTest(uint seed, ushort expectedRandom, uint newSeed)
{
Reset();

mbbsEmuMemoryCore.SetDWord("RANDSEED", seed);

ExecuteApiTest(HostProcess.ExportedModules.Majorbbs.Segment, RAND_ORDINAL, new List<ushort>());

//Verify Results
Assert.Equal(expectedRandom, mbbsEmuCpuRegisters.AX);
Assert.Equal(newSeed, mbbsEmuMemoryCore.GetDWord("RANDSEED"));
}
}
}
11 changes: 0 additions & 11 deletions MBBSEmu.Tests/ExportedModules/Majorbbs/random_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,5 @@ public void lngrndTest_invalid()
//Verify Results
Assert.Equal(mbbsEmuCpuRegisters.GetLong(), expectedValue);
}

[Fact]
public void randTest()
{
Reset();

ExecuteApiTest(HostProcess.ExportedModules.Majorbbs.Segment,RAND_ORDINAL, new List<ushort>());

//Verify Results
Assert.InRange(mbbsEmuCpuRegisters.AX, 1, 32767);
}
}
}
24 changes: 24 additions & 0 deletions MBBSEmu.Tests/ExportedModules/Majorbbs/srand_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Collections.Generic;
using Xunit;

namespace MBBSEmu.Tests.ExportedModules.Majorbbs
{
public class srand_Tests : ExportedModuleTestBase
{
private const int SRAND_ORDINAL = 561;

[Theory]
[InlineData(0)]
[InlineData(1)]
[InlineData(ushort.MaxValue)]
public void srandTest(ushort seed)
{
Reset();

ExecuteApiTest(HostProcess.ExportedModules.Majorbbs.Segment, SRAND_ORDINAL, new List<ushort> { seed });

//Verify Results
Assert.Equal(seed, mbbsEmuMemoryCore.GetDWord("RANDSEED"));
}
}
}
34 changes: 27 additions & 7 deletions MBBSEmu/HostProcess/ExportedModules/Majorbbs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@
Module.Memory.AllocateVariable("TXTVARS", TextvarStruct.Size * MaxTextVariables, true); //Up to 64 Text Variables per Module
Module.Memory.SetArray("TXTVARS", new TextvarStruct("SYSTEM", new FarPtr(Segment, 9000)).Data); //Set 1st var as SYSTEM variable reference with special pointer
Module.Memory.AllocateVariable("HDLCON", FarPtr.Size); //Handles the connection for the current User

Module.Memory.AllocateVariable("RANDSEED", sizeof(uint)); //Seed value for Borland C++ Pseudo-Random Number Generator
Module.Memory.SetDWord("RANDSEED", (uint)_random.Next(1, ushort.MaxValue)); //Seed RNG with a random value

_tfsState = Module.Memory.AllocateVariable("TFSTATE", sizeof(ushort));
_tfspst = Module.Memory.AllocateVariable("TFSPST", FarPtr.Size);
Expand Down Expand Up @@ -585,7 +586,6 @@
switch (ordinal)
{
//Ignored Ordinals
case 561: //srand() handled internally
case 614: //unfrez -- unlocks video memory, ignored
case 174: //DSAIRP
case 189: //ENAIRP
Expand Down Expand Up @@ -1363,6 +1363,9 @@
case 129:
cncuid();
break;
case 561:
srand();
break;
default:
_logger.Error($"Unknown Exported Function Ordinal in MAJORBBS: {ordinal}:{Ordinals.MAJORBBS[ordinal]}");
throw new ArgumentOutOfRangeException($"Unknown Exported Function Ordinal in MAJORBBS: {ordinal}:{Ordinals.MAJORBBS[ordinal]}");
Expand Down Expand Up @@ -2344,12 +2347,14 @@
/// <returns></returns>
private void rand()
{
var randomValue = _random.Next(1, short.MaxValue);
uint multiplier = 0x15A4E35;
var seed = Module.Memory.GetDWord("RANDSEED");

var newSeed = (seed * multiplier) + 1;

#if DEBUG
//_logger.Debug($"Generated random number {randomValue} and saved it to AX");
#endif
Registers.AX = (ushort)randomValue;
Module.Memory.SetDWord("RANDSEED", newSeed);

Registers.AX = (ushort)((newSeed >> 16) & 0x7FFF);
}

/// <summary>
Expand Down Expand Up @@ -6078,7 +6083,7 @@
{
tokenList.Add(Module.Memory.GetString(messagePointer).ToArray());
}
catch (Exception e)

Check warning on line 6086 in MBBSEmu/HostProcess/ExportedModules/Majorbbs.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'e' is declared but never used

Check warning on line 6086 in MBBSEmu/HostProcess/ExportedModules/Majorbbs.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'e' is declared but never used
{
_logger.Warn($"Error Reading string at {messagePointer}, terminating list at {tokenList.Count} elements");
break;
Expand Down Expand Up @@ -8334,5 +8339,20 @@
/// Returns Pointer to the HDLCON Routine
/// </summary>
private ReadOnlySpan<byte> hdlcon => Module.Memory.GetVariablePointer("HDLCON").Data;

/// <summary>
/// Sets the Seed for the Pseudo-Random Number Generator
///
/// While the seed stored in memory is a 32-bit long, only the lower 16-bits are set
/// via the srand() method. The high 16-bits are always set to zero.
///
/// Signature: void srand(int seed)
/// </summary>
private void srand()
{
var randomSeed = GetParameter(0);

Module.Memory.SetDWord("RANDSEED", randomSeed);
}
}
}
Loading