import java.io.*; import java.awt.*; import javax.swing.*; import javax.swing.border.*; import java.awt.event.*; import java.util.*; public class Project extends JFrame implements ActionListener { private int size, assoc, blocksize,nSets,nBlock,tmp,nindex,taglength,temporary,x; private int writeCount,missCount,readCount,length,num; private Integer address; private double tagLength,noSets,noBlock,nIndex; private BufferedReader inData; private JTextField[] JT = new JTextField[8]; private String line,tag,index,block,binAdd,temp,action,tp; private String memory[],mem[][]; private StringTokenizer st; public Project() { // Initialize GUI setTitle("Cache Simulator"); setDefaultCloseOperation(EXIT_ON_CLOSE); Container pane = getContentPane(); pane.setLayout(new GridLayout(10,1)); // Initialize JPanels JPanel panel[] = new JPanel[10]; // Initialize JLabels JLabel[] label = new JLabel[9]; label[0] = new JLabel("Enter the filename"); label[1] = new JLabel("Enter the size of the cache in bytes: "); label[2] = new JLabel("Enter the associativity: "); label[3] = new JLabel("Enter the blocksize in bytes: "); label[4] = new JLabel("Results of Simulation"); label[5] = new JLabel("Number of L1 reads: "); label[6] = new JLabel("Number of L1 read misses: "); label[7] = new JLabel("Number of L1 writes: "); label[8] = new JLabel("L1 Miss rate: "); for(int i=0;i<4;i++) //Add the labels and text boxes to the GUI { panel[i]=new JPanel(new GridLayout(1,2)); JT[i] = new JTextField("",4); JT[i].setHorizontalAlignment(JTextField.CENTER); JT[i].setFont(new Font("", Font.PLAIN, 12)); panel[i].add(label[i]); panel[i].add(JT[i]); pane.add(panel[i]); } panel[4]=new JPanel(new BorderLayout()); JButton go = new JButton("Go"); //Make a button to start the simulation go.addActionListener(this); panel[4].add(go); pane.add(panel[4]); panel[5]=new JPanel(new FlowLayout()); panel[5].add(label[4],BorderLayout.CENTER); pane.add(panel[5]); for(int i=5;i<9;i++) //Add the remaining labels and text boxes to the GUI { panel[i+1]=new JPanel(new GridLayout(1,2)); JT[i-1] = new JTextField("",4); JT[i-1].setHorizontalAlignment(JTextField.CENTER); JT[i-1].setFont(new Font("", Font.PLAIN, 12)); panel[i+1].add(label[i]); panel[i+1].add(JT[i-1]); pane.add(panel[i+1]); } for(int i=4;i<8;i++) { JT[i].setEditable(false); JT[i].setBackground(new Color(255,255,255)); } } public static void main(String[] args) { Project p = new Project(); p.pack(); p.setVisible(true); } public void actionPerformed(ActionEvent ev) //hey look, the buttons work { readCount=0; writeCount=0; missCount=0; if(JT[0].getText().equals("")) JT[0].setText("trace.txt"); if(JT[1].getText().equals("")) JT[1].setText("2048"); size = Integer.parseInt(JT[1].getText().trim()); if(JT[2].getText().equals("")) JT[2].setText("4"); for(int i=0;i<4;i++) //Make sure the inputs are above zero { if(JT[i].getText().equals("0")||JT[i].getText().contains("-")) { JOptionPane.showMessageDialog(null,"Zero or negatives are not valid values."); System.exit(0); } } assoc = Integer.parseInt(JT[2].getText().trim()); memory = new String[size]; //array for cache with an associativity of 1 mem = new String[size][assoc]; //Array for cache with an associativity greater than 1 if(JT[3].getText().equals("")) JT[3].setText("32"); blocksize = Integer.parseInt(JT[3].getText().trim()); //Find number of sets noSets = size/(blocksize * assoc); if((noSets-(int)noSets) >=.5) nSets = (int)noSets+1; else nSets = (int)noSets; nIndex = Math.log(nSets)/Math.log(2); nindex = (int)nIndex; double test = nSets%2; if(test == 1) nSets+=1; //Find block offset noBlock = Math.log(blocksize)/Math.log(2); if((noBlock-(int)noBlock) >=.5) nBlock=(int)noBlock+1; else nBlock=(int)noBlock; tagLength = 32 - nBlock - nIndex; taglength = (int)tagLength; try { inData = new BufferedReader(new FileReader(JT[0].getText().trim())); line = inData.readLine(); } catch(FileNotFoundException e) { JOptionPane.showMessageDialog(null,"File not Found"); } catch(IOException e) { JOptionPane.showMessageDialog(null,"Error reading file"); } for(int i=0;i Integer.parseInt(s)) { LRU=Integer.parseInt(s); k=j; } } } } mem[tmp][k]=""+tag+" "+temporary; //write the new tag } readCount++; } else if(assoc!=1&& action.equals("")) { //Its a write with associativity greater than one int k=0; writeCount++; if(!mem[tmp][0].equals("")) { int j; for(j=0;j4) tp = tp.substring(0,6); JT[7].setText(tp); } }