~ubuntu-branches/ubuntu/vivid/drmips/vivid-backports

« back to all changes in this revision

Viewing changes to src/pc/DrMIPS/src/org/feup/brunonova/drmips/gui/ExecTable.java

  • Committer: Package Import Robot
  • Author(s): Bruno Nova
  • Date: 2014-09-27 12:24:17 UTC
  • Revision ID: package-import@ubuntu.com-20140927122417-2gadkwt9k0u7j4zu
Tags: upstream-1.2.3
Import upstream version 1.2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    DrMIPS - Educational MIPS simulator
 
3
    Copyright (C) 2013-2014 Bruno Nova <ei08109@fe.up.pt>
 
4
 
 
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.
 
9
 
 
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.
 
14
 
 
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/>.
 
17
*/
 
18
 
 
19
package org.feup.brunonova.drmips.gui;
 
20
 
 
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;
 
29
 
 
30
/**
 
31
 * The table that presents the instructions being executed in the datapath tab.
 
32
 * 
 
33
 * @author Bruno Nova
 
34
 */
 
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;
 
44
        
 
45
        /**
 
46
         * Creates the registers table.
 
47
         */
 
48
        public ExecTable() {
 
49
                super();
 
50
                model = new DefaultTableModel(1, 1);
 
51
                cellRenderer = new ExecTableCellRenderer();
 
52
                setDefaultRenderer(Object.class, cellRenderer);
 
53
                setModel(model);
 
54
                setFont(new java.awt.Font("Courier New", 0, 12));
 
55
                getTableHeader().setReorderingAllowed(false);
 
56
        }
 
57
        
 
58
        /**
 
59
         * Defines the CPU that is executing the program.
 
60
         * @param cpu The CPU.
 
61
         * @param format The data format (<tt>Util.BINARYL_FORMAT_INDEX/v.DECIMAL_FORMAT_INDEX/Util.HEXADECIMAL_FORMAT_INDEX</tt>).
 
62
         */
 
63
        public void setCPU(CPU cpu, int format) {
 
64
                this.cpu = cpu;
 
65
                this.dataFormat = format;
 
66
                
 
67
                // Set the columns
 
68
                model.setRowCount(0);
 
69
                model.setColumnCount(0);
 
70
                if(cpu.isPipeline())
 
71
                        for(int i = 0; i < 5; i++) model.addColumn(null);
 
72
                else
 
73
                        model.addColumn(null);
 
74
                
 
75
                model.setRowCount(1); // add 1 row
 
76
                
 
77
                refresh(format);
 
78
        }
 
79
        
 
80
        /**
 
81
         * Refreshes the values in the table.
 
82
         */
 
83
        public void refresh() {
 
84
                refresh(dataFormat);
 
85
        }
 
86
        
 
87
        /**
 
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>).
 
90
         */
 
91
        public void refresh(int format) {
 
92
                if(model == null || cpu == null) return;
 
93
                dataFormat = format;
 
94
                
 
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);
 
101
                }
 
102
                
 
103
                repaint();
 
104
        }
 
105
        
 
106
        /**
 
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.
 
110
         */
 
111
        private String getInstructionInIndex(int index) {
 
112
                AssembledInstruction i = cpu.getInstructionMemory().getInstruction(index);
 
113
                return (i != null) ? i.getCodeLine() : "";
 
114
        }
 
115
 
 
116
        @Override
 
117
        public String getToolTipText(MouseEvent event) {
 
118
                if(cpu == null || model == null) return null;
 
119
                AssembledInstruction i = null;
 
120
                
 
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;
 
127
                }
 
128
                
 
129
                if(i != null) {
 
130
                        switch(dataFormat) {
 
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>";
 
134
                        }
 
135
                }
 
136
                else
 
137
                        return null;
 
138
        }
 
139
        
 
140
        @Override
 
141
        public boolean isCellEditable(int row, int column) {
 
142
                return false;
 
143
        }
 
144
 
 
145
        private class ExecTableCellRenderer extends DefaultTableCellRenderer {
 
146
                @Override
 
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
 
150
        
 
151
                        switch(column) {
 
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;
 
157
                        }
 
158
                        
 
159
                        return c;
 
160
                }
 
161
        }
 
162
}