package lab07;

import java.util.ArrayList;
import java.util.List;

/**
 * Model a Pippin Program - a list of Pippin instructions.
 * @author cs140
 */
public class Program {
	
	private List<Instruction> program;
	
	/**
	 * Constructor - creates a new empty program (no instructions).
	 */
	public Program() {
		program=new ArrayList<Instruction>();
	}

	/**
	 * Adds a new instruction to the program.
	 * @param e an instruction
	 * @return true if instruction was added, false otherwise
	 */
	public boolean add(Instruction e) { return program.add(e); }

	/**
	 * Remove all instructions from the program, leaving it empty.
	 */
	public void clear() { program.clear(); }

	/**
	 * Get the size of the program.
	 * @return number of instructions in the program
	 */
	public int size() { return program.size(); }
	
	/**
	 * Load the program into the CPU.
	 * <ul>
	 * <li>Store instructions into memory, starting at location 0
	 * <li>Set the CPU's instructionCounter to 0 (beginning of the program)
	 * <li>Set the CPU's dataMemoryBase to 1 after the last instruction loaded
	 * </ul>
	 * @param cpu CPU object in which to load this program
	 */
	public void load(CPU cpu) {
		int instructionCounter=0;
		cpu.setInstructionPointer(instructionCounter);
		Memory mem = cpu.getMemory();
		for(Instruction inst : program) {
			inst.store(mem,instructionCounter);
			instructionCounter ++;
		}
		cpu.setDataMemoryBase(instructionCounter);
	}
	
}
