This repository has been archived on 2026-04-18. You can view files and clone it, but cannot push or open issues or pull requests.
Virtual-Memory-Demo/Assets/Scripts/UI/SimulationTraceView.cs

99 lines
2.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using TMPro;
using UnityEngine;
using VMdemo.Simulation;
namespace VMdemo.UI
{
public class SimulationTraceView : MonoBehaviour
{
[SerializeField] private TMP_Text splitTraceText;
[SerializeField] private TMP_Text pageTableTraceText;
[SerializeField] private TMP_Text decisionReasonText;
[SerializeField] private TMP_Text pathSummaryText;
private void Awake()
{
EnsureRichTextEnabled(splitTraceText);
}
public void ShowNotInitialized()
{
SetText(splitTraceText, "地址拆分可视化N/A");
SetText(pageTableTraceText, "多级页表查询可视化N/A");
SetText(decisionReasonText, "分支原因N/A");
SetText(pathSummaryText, "路径摘要N/A");
}
public void Render(TranslatorEngine engine)
{
if (engine == null)
{
ShowNotInitialized();
return;
}
SetText(
splitTraceText,
SimulationUIFormatter.FormatAddressSplitTrace(
engine.State,
engine.VaBits,
engine.OffsetBits,
engine.L1Bits,
engine.L2Bits));
SetText(
pageTableTraceText,
SimulationUIFormatter.FormatPageTableWalkTrace(engine.State, engine.PageTable));
SetText(
decisionReasonText,
string.IsNullOrEmpty(engine.State.LastDecisionReason)
? "分支原因:暂无"
: $"分支原因:{engine.State.LastDecisionReason}");
SetText(pathSummaryText, BuildPathSummary(engine.State));
}
private static string BuildPathSummary(SimulationState state)
{
if (state == null)
{
return "路径摘要N/A";
}
if (state.IsTlbHit)
{
return "路径摘要TLB命中 -> 直接合成PA";
}
if (state.IsPageTableHit)
{
return "路径摘要TLB未命中 -> 页表命中 -> 合成PA";
}
if (state.IsPageFault)
{
return "路径摘要TLB未命中 -> 页表未命中 -> 缺页处理 -> 合成PA";
}
return "路径摘要:进行中";
}
private static void SetText(TMP_Text target, string value)
{
if (target != null)
{
target.text = value;
}
}
private static void EnsureRichTextEnabled(TMP_Text target)
{
if (target != null)
{
target.richText = true;
}
}
}
}