package lab06;

/**
 * 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) {
		Program prog = new Program();
		prog.add(Instruction.factory("LOD","DIR",0));
		prog.add(Instruction.factory("DIV","DIR",1));
		prog.add(Instruction.factory("MUL","DIR",1));
		prog.add(Instruction.factory("SUB","DIR",0));
		prog.add(Instruction.factory("STO","DIR",3));
		prog.add(Instruction.factory("CMZ","DIR",3));
		prog.add(Instruction.factory("STO","DIR",3));
		prog.add(Instruction.factory("HLT","NOM",0));
		
		int nextArg=0;
		if (args.length>nextArg && args[nextArg].equals("-trace")) { 
			Trace.startTrace();
			nextArg++;
		}
		Memory mem=new Memory(1024);
		CPU cpu = new CPU(mem);
		prog.load(cpu);
		
		if (args.length>nextArg) {
			cpu.setData(0, Integer.parseInt(args[nextArg])); // Put command line arg into mem[0]
			nextArg++;
		} else cpu.setData(0, 43);
		
		if (args.length>nextArg) {
			cpu.setData(1, Integer.parseInt(args[nextArg])); // Put command line arg into mem[0]
			nextArg++;
		} else cpu.setData(1, 3);
		
		if (Trace.getTrace()) mem.dump("Memory Before execution");
		
		System.out.println("Running program on " + cpu.getData(0) + " and " + cpu.getData(1));
		cpu.run();
		
		System.out.println(cpu.getData(0) +  
				(cpu.getData(3)==1?" is":" is not") + 
				" divisble by "  + cpu.getData(1));
	}

}
