package lab08;

/**
 * Holds a main method to run a Pippin Computer simulation 
 *  @author cs140
 */
public class TryCPU {
	
	private TryCPU() {} // private null constructor to prevent javadoc 

	/**
	 * Main method to run a Pippin computer simulation.
	 * 
	 * Loads a program which determines if memory[0] is divisible by memory[1].
	 * The result is stored in memory[3] as either a 0 (is not divisible) or 1 (is divisible)
	 * @param args Array of Strings from command line.
	 * <ul>
	 * <li>if the first argument is -trace, turn on tracing.
	 * <li>if there is another argument, convert it to integer, and use it for memory[0]. If not, default t0 43
	 * <li>if there is another argument, convert it to integer, and use if for memory[1]. If not, default to 3.
	 * </ul>
	 */
	public static void main(String[] args) {
	
		
		int nextArg=0;
		if (args.length>nextArg && args[nextArg].equals("-trace")) { 
			Trace.startTrace();
			nextArg++;
		}
		Memory mem=new Memory(1024);
		CPU cpu = new CPU(mem);
		if (nextArg>= args.length) {
			System.out.println("Please invoke as [-trace] <pgm.pexe> [ <arg1> [ <arg2> ] ]");
			return;
		}
		Program prog = new Program(args[nextArg]);
		nextArg++;
		
		Job job1 = new Job("gcd1",cpu,prog,10);
		cpu.addJob(job1);
		
		if (args.length>nextArg) {
			job1.initData(0, Integer.parseInt(args[nextArg])); // Put command line arg into mem[0]
			nextArg++;
		} else job1.initData(0, 36);
		
		if (args.length>nextArg) {
			job1.initData(1, Integer.parseInt(args[nextArg])); // Put command line arg into mem[1]
			nextArg++;
		} else job1.initData(1, 21);
		
		Job job2 = new Job("gcd2",cpu,prog,5);
		cpu.addJob(job2);
		
		if (args.length>nextArg) {
			job2.initData(0, Integer.parseInt(args[nextArg])); // Put command line arg into mem[0]
			nextArg++;
		} else job2.initData(0, 25);
		
		if (args.length>nextArg) {
			job2.initData(1, Integer.parseInt(args[nextArg])); // Put command line arg into mem[1]
			nextArg++;
		} else job2.initData(1, 125);
		
		
		if (Trace.getTrace()) mem.dump("Memory Before execution");

		System.out.println("Job 1 Running program on " + job1.getData(0) + " and " + job1.getData(1));
		System.out.println("Job 2 Running program on " + job2.getData(0) + " and " + job2.getData(1));
		
		cpu.runJobs(10);
		
		System.out.println("Job 1 Greatest Common Divisor is " + job1.getData(0));
		System.out.println("Job 2 Greatest Common Divisor is " + job2.getData(0));
	}

}
