#include "Process.h"
#include <fstream.h>

ifstream inp("prog.in");
ofstream outp("prog.out");
void main()
{	Simulator M;
	Process writer(0, inp);
	M.insertProcess(&writer);
	
// Use constants or literals to name memory locations	
	const int control = 0;
	const int buffer = 1;
	writer.declare(control);	// Memory 0 is the controller
	writer.declare(buffer);	// Memory 1 is the buffer
	writer.input();
	writer.jmp(5);
	writer.toss();			// Instr 4
	writer.pushv(control);
	writer.pos(4);			// busywait until empty (== 0)
	writer.toss();
	writer.pop(buffer);		// load the buffer
	writer.pushi(1);
	writer.pop(control);	// set the controller = full
	writer.jmp(2);			// repeat
	writer.halt();			// not executed
	
	Process reader1(1, inp, outp);
	M.insertProcess(&reader1);
	
	reader1.declare(control);
	reader1.declare(buffer);
	reader1.jmp(4);
	reader1.toss(); 		// Instr 3
	reader1.pushv(control);
	reader1.zero(3); 		// busywait until full (== 1)
	reader1.toss();
	reader1.pushv(buffer);	// empty the buffer
	reader1.output();
	reader1.toss();
	reader1.pushi(0);
	reader1.pop(control);	// set the controller = empty
	reader1.jmp(2);			// repeat
	reader1.halt();			// not executed
	
	Process reader2(reader1); 	// Identical. 
	reader2.setSerialNumber(2);	//except for serial number.
	M.insertProcess(&reader2);

	M.step(1000);
	
	cout << writer<<endl<<endl;
	cout << reader1<<endl<<endl;
	cout << reader2<<endl<<endl;
	cout << M << endl;
}

// exercises  
// Add a control level that will make the use of this interactive.  Instead of the code in main
// that you see here, provide a menu of options like 
//   step n
//   dump
//   quit
// The environment accepts and exeuctes commands until the user chooses quit. 
//			You may want to adapt the scanner and parser from spreadsheet to this.
//			You will want to write a grammar for your command language. 

// HARD.  Add a label statement to Process.  Interally keep a list of label, address pairs.  
// The reason this is hard (maybe too hard) is that you may use a label before it actually appears.
// What do you do with the instruction at that point?  Requires some form of re doing things.
// See Two pass assembly or backpatching in the literature.

