/// <summary> /// Method that executes single cycle of entire machine /// 1. All processors read data /// 2. All processors execute computations /// 3. All processors write their results /// </summary> /// <returns>Returns true if there are any working processors and false if all of them are stopped</returns> private bool Tick() { memory.ClearRequests(); int workingProcessorsCount; List <RWRequest> readRequests = GetReadRequests(); List <RWRequest> writeRequests = new List <RWRequest>(); //Step 1. We post read requests to memory state = PRAMState.Reading; this.memory.PostReadRequests(readRequests); //Step 2. Now all processors are called with data they have read or null if they havent posted any new reqeuests state = PRAMState.Processing; workingProcessorsCount = this.RunProcessors(readRequests); //Step 3. We now collect all write requests from processors state = PRAMState.Writing; writeRequests = this.GetWriteRequests(); //Step 4. And post them to memory memory.PostWriteRequests(writeRequests); //Step 5. Now we write data to memory foreach (RWRequest request in writeRequests) { memory.WriteData(request); } tickCount++; if (workingProcessorsCount > 0) { return(true); } this.isStopped = true; return(false); }
/// <summary> /// This method performs consecutively each step of PRAM machine workcycle /// </summary> /// <returns>true if machine is working and false otherwise</returns> public bool Step() { if (state == PRAMState.Reading) { state = PRAMState.Processing; memory.ClearRequests(); Read(); return(true); } if (state == PRAMState.Processing) { state = PRAMState.Writing; int workingProcessorsCount = Process(GetReadRequests()); if (workingProcessorsCount > 0) { return(true); } // When machine stops all remaining write requests are processed Write(); this.isStopped = true; return(false); } state = PRAMState.Reading; Write(); tickCount++; return(true); }
/// <summary> /// Empty PRAM machine model /// </summary> public PRAMMachineModel() { this.processors = new List <ProcessorModel>(); this.pram = new PRAMModel(); this.state = PRAMState.Reading; this.tickCount = 0; this.isStopped = false; }
/// <summary> /// Full model built from list of the machines processors, its memory, tick count and state /// </summary> /// <param name="processors">List of models of the processors from the machine</param> /// <param name="pram">Model of the machines memory</param> /// <param name="tickCount">Global clock ticks elapsed since the start of the machine</param> /// <param name="state">Current machine state in the work cycle</param> /// <param name="actualReadCount">Curent number of read requests</param> /// <param name="actualWriteCount">Curent number of write requests</param> public PRAMMachineModel(List <ProcessorModel> processors, PRAMModel pram, int tickCount, PRAMState state, bool isStopped, int actualReadCount, int actualWriteCount) { this.processors = processors; this.pram = pram; this.tickCount = tickCount; this.state = state; this.isStopped = isStopped; this.actualReadCount = actualReadCount; this.actualWriteCount = actualWriteCount; }
/// <summary> /// Constructor of PRAM machine /// </summary> /// <param name="processors">List of processors that will be used by machine</param> /// <param name="memory">Machines memory</param> public PRAMMachine(List <Processor> processors, PRAM <MemoryType> memory) { this.state = PRAMState.Reading; this.tickCount = 0; this.processors = processors; for (int i = 0; i < processors.Count; i++) { processors[i].Number = i; } this.memory = memory; this.isStopped = false; }