~ubuntu-branches/ubuntu/trusty/drmips/trusty-backports

« back to all changes in this revision

Viewing changes to src/simulator/DrMIPSSimulator/src/org/feup/brunonova/drmips/simulator/mips/components/ExtendedALU.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.simulator.mips.components;
 
20
 
 
21
import java.util.Stack;
 
22
import org.feup.brunonova.drmips.simulator.exceptions.InvalidCPUException;
 
23
import org.feup.brunonova.drmips.simulator.mips.Data;
 
24
import org.feup.brunonova.drmips.simulator.mips.IsSynchronous;
 
25
import org.feup.brunonova.drmips.simulator.util.Point;
 
26
 
 
27
/**
 
28
 * An ALU that supports multiplications and divisions, and contains the <tt>HI</tt> and <tt>LO</tt> "registers".
 
29
 * 
 
30
 * @author Bruno Nova
 
31
 */
 
32
public class ExtendedALU extends ALU implements IsSynchronous {
 
33
        private final Data hi, lo;
 
34
        private final Stack<int[]> states = new Stack<int[]>(); // previous values
 
35
        
 
36
        /**
 
37
         * ALU constructor.
 
38
         * @param id ALU's identifier.
 
39
         * @param latency The latency of the component.
 
40
         * @param position The component's position on the GUI.
 
41
         * @param in1Id The identifier of the first input.
 
42
         * @param in2Id The identifier of the second input.
 
43
         * @param controlId The identifier of the control input.
 
44
         * @param outId The identifier of the output
 
45
         * @param zeroId The identifier of the zero output.
 
46
         * @throws InvalidCPUException InvalidCPUException InvalidCPUException If <tt>id</tt> is empty or duplicated.
 
47
         */
 
48
        public ExtendedALU(String id, int latency, Point position, String in1Id, String in2Id, String controlId, String outId, String zeroId) throws InvalidCPUException {
 
49
                super(id, latency, position, in1Id, in2Id, controlId, outId, zeroId);
 
50
                setNameKey("extended_alu");
 
51
                setDescriptionKey("extended_alu_description");
 
52
                
 
53
                hi = new Data();
 
54
                lo = new Data();
 
55
        }
 
56
        
 
57
        @Override
 
58
        public void executeSynchronous() {
 
59
                controlALU.doSynchronousOperation(getInput1().getValue(), getInput2().getValue(), this, getControl().getValue());
 
60
        }
 
61
 
 
62
        @Override
 
63
        public void pushState() {
 
64
                states.push(new int[] {hi.getValue(), lo.getValue()});
 
65
        }
 
66
 
 
67
        @Override
 
68
        public void popState() {
 
69
                if(hasSavedStates()) {
 
70
                        int[] val = states.pop();
 
71
                        hi.setValue(val[0]);
 
72
                        lo.setValue(val[1]);
 
73
                }
 
74
        }
 
75
 
 
76
        @Override
 
77
        public boolean hasSavedStates() {
 
78
                return !states.empty();
 
79
        }
 
80
 
 
81
        @Override
 
82
        public void clearSavedStates() {
 
83
                states.clear();
 
84
        }
 
85
 
 
86
        @Override
 
87
        public void resetFirstState() {
 
88
                while(hasSavedStates())
 
89
                        popState();
 
90
        }
 
91
        
 
92
        @Override
 
93
        public boolean isWritingState() {
 
94
                return controlALU.isWritingState(getControl().getValue());
 
95
        }
 
96
        
 
97
        /**
 
98
         * Returns the <tt>HI</tt> "register".
 
99
         * @return The <tt>HI</tt> "register".
 
100
         */
 
101
        public final Data getHI() {
 
102
                return hi;
 
103
        }
 
104
        
 
105
        /**
 
106
         * Returns the <tt>HI</tt> "register".
 
107
         * @return The <tt>HI</tt> "register".
 
108
         */
 
109
        public final Data getLO() {
 
110
                return lo;
 
111
        }
 
112
        
 
113
        /**
 
114
         * Resets the <tt>HI</tt> and <tt>LO</tt> registers to 0.
 
115
         */
 
116
        public final void reset() {
 
117
                hi.setValue(0);
 
118
                lo.setValue(0);
 
119
        }
 
120
}