You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

553 lines
14 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LzFramework.UI;
using LzFramework;
using UnityEngine.UI;
using System.Text;
using UnityEngine.U2D;
using Newtonsoft.Json;
using EduCoderTool;
namespace DisComputer
{
public class IIINonNeumannControll : MonoBehaviour
{
public static IIINonNeumannControll Instance;
public IIICommandUnitSet commandUnitSet;
[SerializeField]
private IIIRegUnit iregUnit;
[SerializeField]
private IIIPRegUnit pRegUnit;
[SerializeField]
private IIIDecoderUnit decoderUnit;
[SerializeField]
private IIIOperationUnitSet operationUnitSet;
[SerializeField]
private IIIALUUnit aLUUnit;
[SerializeField]
private IIIRegsUnitSet regsUnitSet;
[SerializeField]
private IIIMEMUnitSet mEMUnitSet;
[SerializeField]
private IIICmdControll cmdControll;
[SerializeField]
private Text cmdTipInfo_txt;
public bool isDebug;
public CommandType curCmdType;
bool isShow;
private void Awake()
{
Instance = this;
}
// Start is called before the first frame update
void Start()
{
TTUIPage.ShowPage<TipView>();
ReadGameConfig();
}
/// <summary>
/// 读取配置文件
/// </summary>
void ReadGameConfig()
{
if (isDebug)
{
StartCoroutine(StartProgress());
}
else
{
WebGLConfiguratiobReader webGLConfiguratiobReader = new WebGLConfiguratiobReader("Config/vonneuState.json");
CoroutineManager.Singleton.CreateCoroutine(webGLConfiguratiobReader.ReadCoroutine(),
() =>
{
Debug.LogFormat("On Start");
}, (finish) => {
Debug.LogFormat("On Finish");
VonneumanType stateConfig = JsonConvert.DeserializeObject<VonneumanType>(webGLConfiguratiobReader.ReadResult);
List<CommandType> cmdTypeList = new List<CommandType>();
if (string.Equals(stateConfig.Vonneuman, "Mov"))
{
cmdTypeList.Add(CommandType.Mov1);
cmdTypeList.Add(CommandType.Mov2);
cmdTypeList.Add(CommandType.Mov3);
}
else if (string.Equals(stateConfig.Vonneuman, "Operator"))
{
cmdTypeList.Add(CommandType.Add);
cmdTypeList.Add(CommandType.Div);
cmdTypeList.Add(CommandType.Sub);
cmdTypeList.Add(CommandType.Mul);
}
else if (string.Equals(stateConfig.Vonneuman, "Jmp"))
{
cmdTypeList.Add(CommandType.Jmp);
cmdTypeList.Add(CommandType.Jz);
}
else
{
cmdTypeList.Add(CommandType.In);
cmdTypeList.Add(CommandType.Out);
}
int typeId = Random.Range(0, cmdTypeList.Count);
curCmdType = cmdTypeList[typeId];
StartCoroutine(StartProgress());
});
}
}
IEnumerator StartProgress()
{
yield return new WaitForSeconds(0.02f);
Rewind();
yield return new WaitForSeconds(0.01f);
InitCmdMap();
cmdControll.start_btn.gameObject.SetActive(true);
}
/// <summary>
/// 复原结构图
/// </summary>
public void Rewind()
{
commandUnitSet.RewindCommand();
iregUnit.RewindUnit();
pRegUnit.RewindUnit();
decoderUnit.RewindUnit();
operationUnitSet.Rewind();
aLUUnit.RewindUnit();
regsUnitSet.RewinRegsUnitSet();
mEMUnitSet.RewindMEMUnitSet();
}
/// <summary>
/// 初始化指令图
/// </summary>
public void InitCmdMap()
{
VonNeumann.data = new CommandData();
VonNeumann.data.command = curCmdType;
Debug.LogFormat("Reset cmd type {0}",VonNeumann.data.command.ToString());
commandUnitSet.GetComponent<IIICommandUnitSet>().SetCmdConfigs();
SetCmdTipInfo();
}
/// <summary>
/// 显示参数设置界面
/// </summary>
public void ShowVonSetView()
{
if (isShow)
return;
isShow = true;
MessageContainer.SendMessage(VonNeumann.data.command.ToString().ToLower(), this, MsgName.TipVonneumannOpset);
}
/// <summary>
/// 发送通关成功
/// </summary>
public void SendSuccessToWeb()
{
WebData webData = new WebData();
webData.Data = "";
webData.GameState = "Success";
string json = JsonConvert.SerializeObject(webData);
WebConnecter.Singleton.SendDataToWeb(json);
}
public void SwitchCmd()
{
switch (VonNeumann.data.command)
{
//运算式
case CommandType.Add:
ActOperation();
break;
case CommandType.Div:
ActOperation();
break;
case CommandType.Mul:
ActOperation();
break;
case CommandType.Sub:
ActOperation();
break;
//mov
case CommandType.Mov1:
ActMov1();
break;
case CommandType.Mov2:
ActMov2();
break;
case CommandType.Mov3:
ActMov3();
break;
case CommandType.Jmp:
ActJmp();
break;
case CommandType.Jz:
ActJz();
break;
//IO
case CommandType.In:
ActIn();
break;
case CommandType.Out:
ActOut();
break;
}
cmdControll.SetCmdInfo();
}
#region mov 指令集
void ActMov1()
{
VonNeumann.isRunning = true;
pRegUnit.ShakeUnit();
pRegUnit.SetInfoTxt(GetMemAdd(VonNeumann.curpRegIndex.ToString()));
pRegUnit.ShowArrow(VonNeumann.CommandIndex, false, () =>
{
mEMUnitSet.comboomItems[VonNeumann.movMemIndex].CommandAct(VonNeumann.data.num02.ToString());
Act();
});
}
void ActMov2()
{
VonNeumann.isRunning = true;
pRegUnit.ShakeUnit();
pRegUnit.SetInfoTxt(GetMemAdd(VonNeumann.curpRegIndex.ToString()));
pRegUnit.ShowArrow(VonNeumann.CommandIndex, false, () =>
{
Act();
});
}
void ActMov3()
{
VonNeumann.isRunning = true;
pRegUnit.ShakeUnit();
pRegUnit.SetInfoTxt(GetMemAdd(VonNeumann.curpRegIndex.ToString()));
pRegUnit.ShowArrow(VonNeumann.CommandIndex, false, () =>
{
Act();
});
}
#endregion
#region 运算集
/// <summary>
/// 显示运算设置
/// </summary>
void OnShowActOperation()
{
cmdControll.optionsView.OnShow();
}
/// <summary>
/// 执行运算
/// </summary>
public void ActOperation()
{
if (!this.gameObject.activeSelf)
return;
VonNeumann.isRunning = true;
iregUnit.nextUnit = decoderUnit.gameObject;
iregUnit.pReg_unit = pRegUnit;
pRegUnit.ShakeUnit();
pRegUnit.SetInfoTxt(GetMemAdd(VonNeumann.curpRegIndex.ToString()));
pRegUnit.ShowArrow(VonNeumann.CommandIndex, false, () =>
{
ActCmdUnitSet();
});
void ActCmdUnitSet()
{
commandUnitSet.Command(() =>
{
if (iregUnit.pReg_unit != null)
{
iregUnit.SetInfoTxt(VonNeumann.data.ActName);
//启动Ireg=> pReg
iregUnit.ShowArrow(1, false, () =>
{
string tempStr = GetMemAdd((VonNeumann.curpRegIndex + 1).ToString());
Debug.LogFormat(">>>>>>>>>>>>>>>>>>>> tempStr {0} cmd {1}",tempStr, (VonNeumann.curpRegIndex + 1));
iregUnit.pReg_unit.SetInfoTxt(tempStr);
//启动pReg=> command
iregUnit.pReg_unit.Active(() =>
{
//iReg结束回调
iregUnit.ShowArrow(0, false, () =>
{
iregUnit.nextUnit.GetComponent<IIIDecoderUnit>().Active();
});
}, VonNeumann.CommandIndex + 1);
});
}
});
}
}
#endregion
#region 条件跳转
/// <summary>
/// 无条件跳转
/// </summary>
void ActJmp()
{
VonNeumann.isRunning = true;
pRegUnit.ShakeUnit();
pRegUnit.SetInfoTxt(GetMemAdd(VonNeumann.curpRegIndex.ToString()));
pRegUnit.ShowArrow(VonNeumann.CommandIndex, false, () =>
{
Act();
});
}
/// <summary>
/// 条件跳转
/// </summary>
void ActJz()
{
VonNeumann.isRunning = true;
pRegUnit.ShakeUnit();
pRegUnit.SetInfoTxt(GetMemAdd(VonNeumann.curpRegIndex.ToString()));
pRegUnit.ShowArrow(VonNeumann.CommandIndex, false, () =>
{
Act();
});
}
#endregion
#region I/O输入输出
void ActIn()
{
VonNeumann.isRunning = true;
pRegUnit.ShakeUnit();
pRegUnit.SetInfoTxt(GetMemAdd(VonNeumann.curpRegIndex.ToString()));
pRegUnit.ShowArrow(VonNeumann.CommandIndex, false, () =>
{
Act();
});
}
void ActOut()
{
VonNeumann.isRunning = true;
pRegUnit.ShakeUnit();
pRegUnit.SetInfoTxt(GetMemAdd(VonNeumann.curpRegIndex.ToString()));
pRegUnit.ShowArrow(VonNeumann.CommandIndex, false, () =>
{
Act();
});
}
#endregion
string GetMemoryAdd()
{
int i = VonNeumann.CommandIndex + 1;
string val = "00" + (i).ToString();
return val;
}
/// <summary>
/// 起步动作
/// </summary>
void Act()
{
iregUnit.nextUnit = decoderUnit.gameObject;
iregUnit.pReg_unit = pRegUnit;
commandUnitSet.Command(() =>
{
if (iregUnit.pReg_unit != null)
{
iregUnit.SetInfoTxt(VonNeumann.data.ActName);
//启动Ireg=> pReg
iregUnit.ShowArrow(1, false, () =>
{
string tempStr = GetMemAdd((VonNeumann.curpRegIndex + 1).ToString());
iregUnit.pReg_unit.SetInfoTxt(tempStr);
//启动pReg=> command
iregUnit.pReg_unit.Active(() =>
{
//iReg结束回调
iregUnit.ShowArrow(0, false, () =>
{
iregUnit.nextUnit.GetComponent<IIIDecoderUnit>().Active();
});
}, VonNeumann.CommandIndex + 1);
});
}
});
}
/// <summary>
/// 设置命令提示
/// </summary>
void SetCmdTipInfo()
{
string tempStr = VonNeumann.cmdTipInfo;
string[] splitTempStr = tempStr.Split(' ');
StringBuilder builder = new StringBuilder();
for (int i = 1; i < splitTempStr.Length; i++)
{
builder.Append(splitTempStr[i] + " ");
}
cmdTipInfo_txt.text = builder.ToString();
}
/// <summary>
/// 重新设置命令提示
/// </summary>
public void ResetCmdTipInfo()
{
string tempStr = VonNeumann.cmdTipInfo;
string[] splitTempStr = tempStr.Split(' ');
splitTempStr[1] = VonNeumann.data.command.ToString().ToLower();
StringBuilder builder = new StringBuilder();
for(int i = 0;i< splitTempStr.Length; i++)
{
builder.Append(splitTempStr[i] + " ");
}
VonNeumann.cmdTipInfo = builder.ToString();
Debug.LogFormat(">>>>>>>>>>>>>>>>>>>>> reset cmd {0}", VonNeumann.cmdTipInfo);
SetCmdTipInfo();
}
/// <summary>
/// 内存设置
/// </summary>
/// <param name="val"></param>
/// <returns></returns>
public string GetMemAdd(string val)
{
int data = int.Parse(val);
if (data < 10)
{
val = "00" + data.ToString();
}
else if (data >= 10 && data < 100)
{
val = "0" + data.ToString();
}
return val;
}
}
/// <summary>
/// 诺依曼四大类指令
/// </summary>
public class VonneumanType
{
/// <summary>
/// Mov Operator Jmp IO
/// </summary>
public string Vonneuman { get; set; }
}
}