-
Notifications
You must be signed in to change notification settings - Fork 1
/
UI.java
354 lines (276 loc) · 9.98 KB
/
UI.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
class UI extends JPanel implements ActionListener {
static int useCache = -1;
static int usePipeline = -1;
static JLabel cacheLabel;
static JLabel pipelineLabel;
static JComboBox<String> cacheSelect;
static JComboBox<String> pipelineSelect;
static JButton continueButton;
static boolean chosen = false;
static JTextArea DRAMText, L2Text, L1Text;
static JTextArea pipeText;
static JTextArea registerText;
static JLabel L1Hits, L2Hits, L1ConMisses, L2ConMisses, L1ColdMisses, L2ColdMisses;
static JTextField t;
static JTextField associativity;
static JTextField lineLength;
static JTextField cycleCount;
static JFrame frame;
static JButton button;
static JButton cycleButton;
static JButton flushButton;
static JButton runButton;
static DefaultTableModel memTableModel;
static DefaultTableModel regTableModel;
static JTable mem;
static JTable reg;
static int DRAMSize = 2500, L2Size = 500, L1Size = 250;//# of words included in each memory system, since a word has 4 bytes(32bits): L1=1kB, L2=2kB, DRAM=10kB
static int wordsPerLine = 2;
static int a = 2;
static int clock; // change type to Clock
static final String[] STAGE_NAMES = {"Fetch", "Decode", "Execute", "Memory", "Writeback"};
static final String[] REGISTER_NAMES = {"R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9", "R10", "R11", "R12", "R13", "PS", "PC"};
static Memory2 DRAM, L1, L2;
private Pipeline pipe;
public Instruction[] readOut = {new Instruction(-1073741824), new Instruction(-1073741824), new Instruction(-1073741824), new Instruction(-1073741824), new Instruction(-1073741824)};
public UI() {
super();
}
public void setMemory(Memory2 m) {
pipe = new Pipeline(m);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
//drawPipeline(g);
}
private void updatePipeline() {
String str = "";
for (int i = 0; i < 5; i++ ) {
str += STAGE_NAMES[i] + ": " + Integer.toBinaryString(readOut[i] == null ? 0 : readOut[i].instruction) + "\n";
}
pipeText.setText(str);
}
private void updateMemory() { // change to update text areas
DRAMText.setText(DRAM.toString());
if (useCache == 1) {
L1Text.setText(L1.toString());
L2Text.setText(L2.toString());
}
}
private void updateStats() {
if (useCache == 1) {
L1Hits.setText("L1 Cache Hits: " + L1.getHits());
L2Hits.setText("L2 Cache Hits: " + L2.getHits());
L1ConMisses.setText("L1 Cache Conflict Misses: " + L1.getCon());
L2ConMisses.setText("L2 Cache Conflict Misses: " + L2.getCon());
L1ColdMisses.setText("L1 Cache Cold Misses: " + L1.getCold());
L2ColdMisses.setText("L2 Cache Cold Misses: " + L2.getCold());
}
}
private void updateRegisters() {
String str = "";
for (int i = 0; i < pipe.registers.length; i++) {
str += " " + REGISTER_NAMES[i] + ": " + pipe.registers[i] + "\n";
}
registerText.setText(str);
}
// if the button is pressed
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if (s.equals("Submit")) {
// set the text of the label to the text of the field
//label.setText(t.getText());
for (int i = 0; i < Integer.valueOf(t.getText()); i++) {
readOut = usePipeline == 1 ? pipe.cycle() : pipe.cycleNoPipeline();
clock++;
}
cycleCount.setText("" + clock);
updateMemory();
updatePipeline();
updateRegisters();
updateStats();
//regTableModel.fireTableDataChanged();
//remove(reg);
//add(drawRegisters());
//memTableModel.fireTableDataChanged();
//remove(mem);
//add(drawMemory(pipe.memory, 0, 1));
// set the text of field to blank
//t.setText(" ");
} else if (s.equals("Cycle") && pipe.notEndOfProgram()) {
readOut = usePipeline == 1 ? pipe.cycle() : pipe.cycleNoPipeline();
clock++;
cycleCount.setText("" + clock);
//regTableModel.fireTableDataChanged();
//remove(reg);
//add(drawRegisters());
//memTableModel.fireTableDataChanged();
//remove(mem);
//add(drawMemory(pipe.memory, 0, 1));
updateMemory();
updatePipeline();
updateRegisters();
updateStats();
} else if (s.equals("Flush Cache")) {
if (useCache == 1) {
L1.evictAll();
L2.evictAll();
updateMemory();
}
} else if (s.equals("Run")) {
while (pipe.notEndOfProgram()) {
if (usePipeline == 1) pipe.cycle();
else pipe.cycleNoPipeline();
clock++;
}
updateMemory();
updatePipeline();
updateRegisters();
updateStats();
cycleCount.setText("" + clock);
}
}
public static void addStats(JPanel panel, JLabel hits, JLabel conMisses, JLabel coldMisses, Memory2 mem) {
panel.add(hits);
panel.add(conMisses);
panel.add(coldMisses);
}
public static JPanel makeTextArea(String name, JTextArea memText, int level) {
memText.setEditable(false);
JPanel memPanel = new JPanel();
memPanel.setLayout(new BorderLayout());
memPanel.add(new JLabel(name), BorderLayout.PAGE_START);
memPanel.add(new JScrollPane(memText), BorderLayout.CENTER);
return memPanel;
}
public static void main(String[] args) throws Exception {
cacheLabel = new JLabel("Cache? ");
pipelineLabel = new JLabel("Pipeline? ");
String[] yesNo = {"", "Yes", "No"};
cacheSelect = new JComboBox<String>(yesNo);
pipelineSelect = new JComboBox<String>(yesNo);
cacheSelect.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
useCache = cacheSelect.getSelectedItem().equals("Yes") ? 1 : 0;
}
});
pipelineSelect.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
usePipeline = pipelineSelect.getSelectedItem().equals("Yes") ? 1 : 0;
}
});
continueButton = new JButton("Continue");
continueButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (useCache != -1 && usePipeline != -1) chosen = true;
if (!associativity.getText().isEmpty()) a = Integer.valueOf(associativity.getText());
if (!lineLength.getText().isEmpty()) wordsPerLine = Integer.valueOf(lineLength.getText());
}
});
UI ui = new UI();
frame = new JFrame("CS535 Simulator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button = new JButton("Submit");
cycleButton = new JButton("Cycle");
flushButton = new JButton("Flush Cache");
runButton = new JButton("Run");
button.addActionListener(ui);
cycleButton.addActionListener(ui);
flushButton.addActionListener(ui);
runButton.addActionListener(ui);
t = new JTextField(16);
JLabel associativityLabel = new JLabel("Associativity");
associativity = new JTextField(16);
JLabel lineLengthLabel = new JLabel("Line Width");
lineLength = new JTextField(16);
cycleCount = new JTextField(16);
// JPanel p = new JPanel();
ui.add(cacheLabel);
ui.add(cacheSelect);
ui.add(pipelineLabel);
ui.add(pipelineSelect);
ui.add(associativityLabel);
ui.add(associativity);
ui.add(lineLengthLabel);
ui.add(lineLength);
ui.add(continueButton);
frame.add(ui);
frame.setSize(1000, 1000);
frame.setVisible(true);
while (!chosen){Thread.sleep(100);}
ui.remove(cacheLabel);
ui.remove(cacheSelect);
ui.remove(pipelineLabel);
ui.remove(pipelineSelect);
ui.remove(continueButton);
DRAM = new Memory2(DRAMSize, 10, wordsPerLine, -1, 0, null);
L2 = new Memory2(L2Size, 5, wordsPerLine, a, 2, DRAM);
L1 = new Memory2(L1Size, 1, wordsPerLine, a, 1, L2);
ui.setMemory(useCache == 1 ? L1 : DRAM);
JPanel left = new JPanel();
JPanel topRight = new JPanel();
JPanel bottomRight = new JPanel();
left.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
GridLayout leftLayout = new GridLayout(useCache==1 ? 4: 2, 1, 10, 10);
left.setLayout(leftLayout);
registerText = new JTextArea();
left.add(makeTextArea("Registers", registerText, 0));
if (useCache == 1) {
L1Hits = new JLabel();
L1ColdMisses = new JLabel();
L1ConMisses = new JLabel();
L2Hits = new JLabel();
L2ColdMisses = new JLabel();
L2ConMisses = new JLabel();
L1Text = new JTextArea(L1.toString());
L2Text = new JTextArea(L2.toString());
left.add(makeTextArea(L1.getName(), L1Text, 1));
left.add(makeTextArea(L2.getName(), L2Text, 2));
}
DRAMText = new JTextArea(DRAM.toString());
left.add(makeTextArea(DRAM.getName(), DRAMText, 0));
topRight.setLayout(new BorderLayout());
pipeText = new JTextArea();
pipeText.setEditable(false);
ui.updatePipeline();
topRight.add(new JLabel("Pipeline"), BorderLayout.PAGE_START);
topRight.add(pipeText, BorderLayout.CENTER);
bottomRight.setLayout(new GridLayout(2, 0));
JPanel control = new JPanel();
control.add(t);
control.add(cycleCount);
control.add(button);
control.add(cycleButton);
control.add(runButton);
JPanel cache = new JPanel();
cache.setLayout(new GridLayout(2, 3));
if (useCache == 1) {
control.add(flushButton);
cache.add(L1Hits);
cache.add(L1ColdMisses);
cache.add(L1ConMisses);
cache.add(L2Hits);
cache.add(L2ColdMisses);
cache.add(L2ConMisses);
}
bottomRight.add(control);
bottomRight.add(cache);
ui.updateStats();
JSplitPane right = new JSplitPane(SwingConstants.HORIZONTAL, topRight, bottomRight);
JSplitPane whole = new JSplitPane(SwingConstants.VERTICAL, left, right);
frame.remove(ui);
frame.add(whole);
frame.revalidate();
/*
ui.add(ui.drawMemory(L1, 0, 1));
ui.add(ui.drawRegisters());
ui.repaint();
*/
}
}