import byucc.jhdl.base.*;
import byucc.jhdl.Logic.*;

public class accum extends Logic {
  public static CellInterface[] cell_interface = {
    in("clr", 1),
    in("a", 8),
    out("sum", 8),
  };
  
  public accum(Node parent, Wire clr, Wire a, Wire sum) {
    super(parent);
    connect("clr", clr);
    connect("a", a);
    connect("sum", sum);
    
    // An accumulator is simply an adder, 
    //   a register bank, 
    //   and an AND gate that forces zero on the feedback path
    
    // Here is the output wire for the adder
    Wire addOut = wire(8, "addOut");

    // Here is the feedback from the register to add back in
    Wire feedback = wire(8, "feedback");

    // Create an instance of our adder
    new NBitAdder(this, a, feedback, addOut);

    // Build the actual accumulator register out of 8 flip flops
    // The reg_o() call works for any width wires
    reg_o(addOut, sum);


    // To clear the adder, take the outputs of the register and 
    //   pass them through an AND gate.  When "clr" is high, make
    //   the AND gate outputs be zero.
    // We will use a for-loop to build these
    for (int i=0;i<8;i++)
      and_o(not(clr), sum.gw(i), feedback.gw(i));

  }
}


