-- FIFODriver.mesa
-- last edited by Suzuki: July 12, 1981  8:58 PM 

DIRECTORY
	FIFO,
	FIFOConnectorBool,
	FIFOConnectorCard,
	FIFOScheduler,
	ImageDefs,
	SimIO,
	WF;

FIFODriver: MONITOR  
	IMPORTS 
		DataAv: FIFOConnectorBool,
		SpaceAv: FIFOConnectorBool,
		Init: FIFOConnectorBool,
		ReadReq: FIFOConnectorBool,
		WriteReq: FIFOConnectorBool,
		DataOut: FIFOConnectorCard,
		DataIn: FIFOConnectorCard,
		FIFO, FIFOScheduler, ImageDefs, SimIO, WF = {

p,q,r,s: PROCESS;
i: CARDINAL;

Reader: PROC = {
	WF.WF0["  Reader started.*n"];
	THROUGH [0..50) DO
		FIFOScheduler.Delay[10];
		UNTIL DataAv.Get[]=TRUE DO 
			FIFOScheduler.StandardDelay[] ENDLOOP;
		ReadReq.Put[TRUE];
		WF.WF1["%u  ", DataOut.Get[]];
		FIFOScheduler.Delay[5];
		ReadReq.Put[FALSE];
	ENDLOOP;
	WF.WF0["  Reader terminated.*n"];
	FIFOScheduler.ProcessEnd[];
	};

Writer: PROC = {
	WF.WF0[" Writer started.*n"];
	FOR i IN [0..50) DO
		FIFOScheduler.Delay[10];
		UNTIL SpaceAv.Get[]=TRUE DO 
			FIFOScheduler.StandardDelay[] ENDLOOP;
		DataIn.Place[i];
		WriteReq.Put[TRUE];
		WF.WF1["%u-  ", i];
		FIFOScheduler.Delay[5];
		WriteReq.Put[FALSE];
	ENDLOOP;
	WF.WF0[" Writer terminated.*n"];
	FIFOScheduler.ProcessEnd[];
	};

Start: PROC = {
	ReadReq.Stuff[FALSE];
	WriteReq.Stuff[FALSE];
	FIFOScheduler.Delay[10];
	Init.Put[TRUE];
	FIFOScheduler.Delay[5];
	Init.Put[FALSE];
	FIFOScheduler.ProcessEnd[];
	};

SimIO.Start["FIFO.jam"];
r ← FIFOScheduler.Fork[FIFO.Main];
s ← FIFOScheduler.Fork[Start];
p ← FIFOScheduler.Fork[Reader];
q ← FIFOScheduler.Fork[Writer];

[] ← FIFOScheduler.Join[p];
[] ← FIFOScheduler.Join[q];
[] ← FIFOScheduler.Join[s];
SimIO.Stop[];
ImageDefs.StopMesa[];
}.