Step 2 - 地址生成与拆分

This commit is contained in:
2026-04-14 00:56:38 +08:00
parent 7eab504f4a
commit 4ab93d7806
7 changed files with 266 additions and 10 deletions
@@ -0,0 +1,39 @@
using System;
namespace VMdemo.Simulation
{
public class AddressGenerator
{
public const int DefaultSeed = 12345;
private readonly int _vaBits;
private readonly ulong _vaMask;
private readonly Random _random;
private readonly byte[] _buffer = new byte[8];
public AddressGenerator(int vaBits, int? seed = null)
{
if (vaBits != 32 && vaBits != 48 && vaBits != 64)
{
throw new ArgumentOutOfRangeException(nameof(vaBits), "vaBits 仅支持 32、48、64。");
}
_vaBits = vaBits;
_vaMask = vaBits == 64 ? ulong.MaxValue : (1UL << vaBits) - 1UL;
_random = new Random(seed ?? DefaultSeed);
}
public ulong MaxVirtualAddress => _vaMask;
public ulong NextVirtualAddress()
{
return NextRawUlong() & _vaMask;
}
private ulong NextRawUlong()
{
_random.NextBytes(_buffer);
return BitConverter.ToUInt64(_buffer, 0);
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a3e8a9161af494c43a623da1945f4373
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: