package text_area; /* A cache simulation * Updated 24 March 1997 for JDK1.1 */ import java.applet.*; import java.awt.*; import java.util.Vector; import eduni.simanim.*; import eduni.simjava.*; import eduni.simdiag.*; // A single cache block frame class CacheFrame extends Sim_entity { private Sim_port io; private int state; // Do we contain info? - EMPTY | FULL private int addr_tag; // If we do what memory address is it? private static final int EMPTY = 0; private static final int FULL = 1; public CacheFrame(String name, int x, int y) { super(name, "frame", x, y); io = new Sim_port("io", "port", Anim_port.LEFT, 4); add_port(io); state = EMPTY; addr_tag = -1; add_param(new Anim_param("State", Anim_param.STATE, "empty")); add_param(new Anim_param("Tag", Anim_param.NAME_VALUE, "Empty", 52, 12)); } // Cache can send us: // tag: 0, data: address - You now hold the data at this address // tag: 1, data: address - Do you have this address? // We can reply: // tag: 0 no we are empty // tag: 1 yes we do // tag: -1 no, we have some other address public void body() { Sim_event ev = new Sim_event(); //dump_state(); while(true) { sim_wait(ev); sim_hold(0.01); if(ev.get_tag() == 1) { // An enquire to see if we have an address if(state == EMPTY) { // Tell cache we are empty sim_schedule(io, 0.0, 0); sim_trace(1, "S io E"); } else { // We are full, do we have the right address? if(addr_tag == ((Integer)ev.get_data()).intValue()) { // OK send back the data sim_schedule(io, 0.0, 1); sim_trace(1, "S io H"); } else { // Tell cache it missed sim_schedule(io, 0.0, -1); sim_trace(1, "S io M"); } } } else { // An update to tell us our new address state = FULL; addr_tag = ((Integer)ev.get_data()).intValue(); dump_state(); } } } public void dump_state() { if(state == EMPTY) sim_trace(1, "P empty "+addr_tag); if(state == FULL) sim_trace(1, "P full "+addr_tag); } } class Cache extends Sim_entity { private Sim_port frames[]; private Sim_port mem, cpu; private int assoc, hits, requests; public Cache(String name, int assoc, int num_frames, int x, int y) { super(name, "cache", x, y); cpu = new Sim_port("cpu", "port", Anim_port.TOP, 2); add_port(cpu); mem = new Sim_port("mem", "port", Anim_port.BOTTOM, 2); add_port(mem); frames = new Sim_port[num_frames]; for(int i=0; i