/* A cache simulation
 * Updated 24 March 1997 for JDK1.1
 * Updated 5 Aug 97 to put entities in separate files to 
 * keep jar tool happy.
 */
package cache;

import java.applet.*;
import java.awt.*;
import eduni.simanim.*;
import eduni.simjava.*;
import eduni.simdiag.*;

public class Applet5 extends Anim_applet implements Runnable {
  Panel inputs;
  Label reqLabel, assocLabel;
  TextField reqField;
  Choice assocChoice;

  public void anim_init() {
    int i;
    inputs = new Panel();
    inputs.setLayout(new GridLayout(0,2));
    reqLabel = new Label("Memory requests:", Label.RIGHT);
    inputs.add(reqLabel);
    reqField = new TextField("5",3);
    inputs.add(reqField);

    assocLabel = new Label("Cache associativity:", Label.RIGHT);
    inputs.add(assocLabel);
    assocChoice = new Choice();
    assocChoice.addItem("direct mapped");
    for(i=2; i<8; i*=2) assocChoice.addItem(" "+i+" way");
    assocChoice.addItem("fully associative");
    assocChoice.select(2);
    inputs.add(assocChoice);

    // Add a trace saver
    trace_out.addTraceListener( new TraceSaver("tracefile") );

    this.add("North", inputs);
  }

  //public boolean handleEvent(Event e) {
  //  return super.handleEvent(e);
  //}

  public void anim_layout() {
    int reqs, assoc, i;
    int NUM_FRAMES = 8;

    // Extract GUI info
    reqs = Integer.parseInt(reqField.getText());
    assoc = 1;
    assoc <<= assocChoice.getSelectedIndex();

    // Build ents
    Sim_system.add(new Cpu("CPU", reqs, 100, 20));
    Sim_system.add(new Memory("Memory", 100, 218));
    for(i=0; i<NUM_FRAMES; i++) {
      Sim_system.add(new CacheFrame("Frame_"+i, 170, 56+i*20));
    }
    Sim_system.add(new Cache("Cache", assoc, NUM_FRAMES, 108, 102));

    // Link ents
    Sim_system.link_ports("CPU", "io", "Cache", "cpu");
    Sim_system.link_ports("Memory", "io", "Cache", "mem");
    for(i=0; i<NUM_FRAMES; i++) {
      Sim_system.link_ports("Cache", "frame_"+i, "Frame_"+i, "io");
    }
  }
}
