public static int FindStateNr(TMState ts, string StateName) { TMState tmpState = ts; int StateNr = 0; bool Found = false; while (tmpState != null) { if (StateName.Equals(tmpState.GetStateF())) { Found = true; break; } StateNr++; tmpState = tmpState.GetNext(); } if (Found) { return(StateNr / 2); } else { return(-1); } }
private MyPGM GetSubroutineConn(int States, ref int[] SubConn) { MyPGM ConnMatrix = new MyPGM(States * 2, States * ReadWriteHead.YRailsCnt + 1); TMState tmpState = m_TMStates; SubConn = new int[ConnMatrix.XSize]; int[] ConnY = new int[ConnMatrix.YSize]; m_Connected = new byte[ConnMatrix.XSize]; int ActStateNr = 0; for (int i = 0; i < SubConn.Length; i++) { SubConn[i] = 0; } for (int i = 0; i < ConnY.Length; i++) { ConnY[i] = 0; } while (tmpState != null) { int StateNr = TMLoader.FindStateNr(m_TMStates, tmpState.GetStateN()); int YPos = (ActStateNr / 2) * ReadWriteHead.YRailsCnt; if (StateNr >= 0) { if ((ActStateNr % 2) == 0) { YPos += ReadWriteHead.XReadOutputTrue; } else { YPos += ReadWriteHead.XReadOutputFalse; } bool Dir = false; if (tmpState.GetMove().Equals("R")) { Dir = true; } int SubPos = GetSubOutPos(States, StateNr, Dir); if (tmpState.GetRead().Equals(tmpState.GetWrite())) { if (SubConn[SubPos] == 0) { SubConn[SubPos] = 1; } ConnMatrix.SetValue(SubPos, YPos, 1); } else { SubConn[SubPos] = 2; ConnMatrix.SetValue(SubPos, YPos, 2); } ConnY[YPos] = SubPos + 1; } ActStateNr++; tmpState = tmpState.GetNext(); } FillConnMatrix(ConnMatrix, SubConn, ConnY); return(ConnMatrix); }
public TMState(string StateF, string Read, string StateN, string Write, string Move) { m_StateF = StateF; m_Read = Read; m_StateN = StateN; m_Write = Write; m_Move = Move; m_Next = null; }
public static int CountStates(TMState ts) { TMState tmpState = ts; int cnt = 0; while (tmpState != null) { cnt++; tmpState = tmpState.GetNext(); } return(cnt); }
public Envelope(TMState ts, bool Set) { m_core = new Core(ts, Set); m_States = m_core.States; m_XRailsCnt = (m_core.XRailsCnt + m_States * 4); m_YRailsCnt = (m_core.YRailsCnt + m_States * 2); int XSize = m_XRailsCnt * RailParts.Size; int YSize = m_YRailsCnt * RailParts.Size; m_EnvPgm = new MyPGM(XSize, YSize); m_EnvPgm.CopyAtPos(m_core.CorePgm, m_States * 2 * RailParts.Size, m_States * RailParts.Size); SetConn(); }
private TMState AddState(string state1, string read, string state2, string write, string move) { if (Start == null) { Start = new TMState(state1, read, state2, write, move); Last = Start; } else { Last.SetNext(new TMState(state1, read, state2, write, move)); Last = Last.GetNext(); } return(Last); }
public Core(TMState ts, bool Set) { m_TMStates = ts; m_rp = new RailParts(); m_StateCnt = TMLoader.CountStates(m_TMStates) / 2; m_XRailsCnt = ReadWriteHead.XRailsCnt + m_StateCnt * 2 + 1; m_YRailsCnt = ReadWriteHead.YRailsCnt * m_StateCnt + 2; int XSize = m_XRailsCnt * RailParts.Size; int YSize = m_YRailsCnt * RailParts.Size; m_CorePgm = new MyPGM(XSize, YSize); SetRWHeads(m_StateCnt, Set); SetInputs(m_StateCnt); SetSubroutine(m_StateCnt); }
public Circuit(TMState ts, Tape tp, int StateBegin, int TapePos) { int envXSize = 0; int envYInOut = 0; RailParts rp = new RailParts(); MyPGM straight = rp.GetStraight(1); MyPGM Start = rp.GetStart(); int TpCnt = TMLoader.CountTape(tp); int STCnt = TMLoader.CountStates(ts) / 2; Tape tmpTape = tp; Envelope env = null; for (int i = 0; i < TpCnt; i++) { if (tmpTape.GetSign().Equals("t")) { env = new Envelope(ts, true); } else { env = new Envelope(ts, false); } if (i == 0) { envXSize = (env.XRailsCnt + 1); envYInOut = env.FirstInOutput; int XSize = envXSize * TpCnt; int YSize = env.YRailsCnt; m_CircuitPgm = new MyPGM(XSize * RailParts.Size, YSize * RailParts.Size); } RailParts.SetField(m_CircuitPgm, env.EnvelopePgm, envXSize * i + 1, 0); for (int j = 0; j < STCnt; j++) { if (j == StateBegin && i == TapePos) { RailParts.SetField(m_CircuitPgm, Start, envXSize * i, envYInOut + ReadWriteHead.YRailsCnt * j + 1); } else { RailParts.SetField(m_CircuitPgm, straight, envXSize * i, envYInOut + ReadWriteHead.YRailsCnt * j + 1); } } tmpTape = tmpTape.GetNext(); } }
private void LoadTM_Click(object sender, System.EventArgs e) { OpenFileDialog opfd = new OpenFileDialog(); opfd.Filter = "XML File (*.tm)|*.tm"; opfd.FilterIndex = 1; opfd.RestoreDirectory = true; opfd.Title = "Load Turing Machine"; DialogResult res = opfd.ShowDialog(this); string TMName = opfd.FileName; if (res == DialogResult.OK) { string PgmName = Path.ChangeExtension(TMName, "pgm"); TMLoader TM = new TMLoader(); if (TM.Load(TMName)) { TMState ts = TM.GetStates; Tape tp = TM.GetTape; Circuit circ = new Circuit(ts, tp, TMLoader.FindStateNr(ts, TM.StartState), TM.StartTapePos); circ.CircuitPgm.Write(PgmName); } } }
public void SetNext(TMState next) { m_Next = next; }