2
DrMIPS - Educational MIPS simulator
3
Copyright (C) 2013-2014 Bruno Nova <ei08109@fe.up.pt>
5
This program is free software: you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation, either version 3 of the License, or
8
(at your option) any later version.
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
15
You should have received a copy of the GNU General Public License
16
along with this program. If not, see <http://www.gnu.org/licenses/>.
19
package org.feup.brunonova.drmips.gui;
21
import java.awt.Component;
22
import java.awt.event.MouseEvent;
23
import javax.swing.JTable;
24
import javax.swing.SwingConstants;
25
import javax.swing.table.DefaultTableCellRenderer;
26
import javax.swing.table.DefaultTableModel;
27
import org.feup.brunonova.drmips.simulator.mips.AssembledInstruction;
28
import org.feup.brunonova.drmips.simulator.mips.CPU;
31
* The table that presents the instructions being executed in the datapath tab.
35
public class ExecTable extends JTable {
36
/** The model of the table. */
37
private DefaultTableModel model = null;
38
/** The renderer of the table cells. */
39
private ExecTableCellRenderer cellRenderer = null;
40
/** The CPU with the registers to be displayed. */
41
private CPU cpu = null;
42
/** The format of the data (<tt>Util.BINARYL_FORMAT_INDEX/Util.DECIMAL_FORMAT_INDEX/Util.HEXADECIMAL_FORMAT_INDEX</tt>). */
43
private int dataFormat = DrMIPS.DEFAULT_DATAPATH_DATA_FORMAT;
46
* Creates the registers table.
50
model = new DefaultTableModel(1, 1);
51
cellRenderer = new ExecTableCellRenderer();
52
setDefaultRenderer(Object.class, cellRenderer);
54
setFont(new java.awt.Font("Courier New", 0, 12));
55
getTableHeader().setReorderingAllowed(false);
59
* Defines the CPU that is executing the program.
61
* @param format The data format (<tt>Util.BINARYL_FORMAT_INDEX/v.DECIMAL_FORMAT_INDEX/Util.HEXADECIMAL_FORMAT_INDEX</tt>).
63
public void setCPU(CPU cpu, int format) {
65
this.dataFormat = format;
69
model.setColumnCount(0);
71
for(int i = 0; i < 5; i++) model.addColumn(null);
73
model.addColumn(null);
75
model.setRowCount(1); // add 1 row
81
* Refreshes the values in the table.
83
public void refresh() {
88
* Refreshes the values in the table.
89
* @param format The data format (<tt>Util.BINARYL_FORMAT_INDEX/v.DECIMAL_FORMAT_INDEX/Util.HEXADECIMAL_FORMAT_INDEX</tt>).
91
public void refresh(int format) {
92
if(model == null || cpu == null) return;
95
model.setValueAt(getInstructionInIndex(cpu.getPC().getCurrentInstructionIndex()), 0, 0);
96
if(cpu.isPipeline()) {
97
model.setValueAt(getInstructionInIndex(cpu.getIfIdReg().getCurrentInstructionIndex()), 0, 1);
98
model.setValueAt(getInstructionInIndex(cpu.getIdExReg().getCurrentInstructionIndex()), 0, 2);
99
model.setValueAt(getInstructionInIndex(cpu.getExMemReg().getCurrentInstructionIndex()), 0, 3);
100
model.setValueAt(getInstructionInIndex(cpu.getMemWbReg().getCurrentInstructionIndex()), 0, 4);
107
* Returns the code line of the instruction in the specified index.
108
* @param index Index of the instruction.
109
* @return Code line of the instruction, or an empty string if it doesn't exist.
111
private String getInstructionInIndex(int index) {
112
AssembledInstruction i = cpu.getInstructionMemory().getInstruction(index);
113
return (i != null) ? i.getCodeLine() : "";
117
public String getToolTipText(MouseEvent event) {
118
if(cpu == null || model == null) return null;
119
AssembledInstruction i = null;
121
switch(columnAtPoint(event.getPoint())) {
122
case 0: i = cpu.getInstructionMemory().getInstruction(cpu.getPC().getCurrentInstructionIndex()); break;
123
case 1: i = cpu.getInstructionMemory().getInstruction(cpu.getIfIdReg().getCurrentInstructionIndex()); break;
124
case 2: i = cpu.getInstructionMemory().getInstruction(cpu.getIdExReg().getCurrentInstructionIndex()); break;
125
case 3: i = cpu.getInstructionMemory().getInstruction(cpu.getExMemReg().getCurrentInstructionIndex()); break;
126
case 4: i = cpu.getInstructionMemory().getInstruction(cpu.getMemWbReg().getCurrentInstructionIndex()); break;
131
case Util.BINARY_FORMAT_INDEX: return "<html><tt><b>" + Lang.t("type_x", i.getInstruction().getType().getId()) + ": " + i.getInstruction().getMnemonic() + "</b> (" + i.toBinaryString() + ")</tt></html>";
132
case Util.HEXADECIMAL_FORMAT_INDEX: return "<html><tt><b>" + Lang.t("type_x", i.getInstruction().getType().getId()) + ": " + i.getInstruction().getMnemonic() + "</b> (" + i.toHexadecimalString() + ")</tt></html>";
133
default:return "<html><tt><b>" + Lang.t("type_x", i.getInstruction().getType().getId()) + ": " + i.getInstruction().getMnemonic() + "</b> (" + i.toString() + ")</tt></html>";
141
public boolean isCellEditable(int row, int column) {
145
private class ExecTableCellRenderer extends DefaultTableCellRenderer {
147
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
148
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
149
setHorizontalAlignment(SwingConstants.CENTER); // center all values
152
case 0: setForeground(AssembledCodeTable.IF_COLOR); break;
153
case 1: setForeground(AssembledCodeTable.ID_COLOR); break;
154
case 2: setForeground(AssembledCodeTable.EX_COLOR); break;
155
case 3: setForeground(AssembledCodeTable.MEM_COLOR); break;
156
case 4: setForeground(AssembledCodeTable.WB_COLOR); break;