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

« back to all changes in this revision

Viewing changes to src/simulator/DrMIPSSimulator/src/org/feup/brunonova/drmips/simulator/mips/components/And.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.ArrayList;
 
22
import java.util.List;
 
23
import org.feup.brunonova.drmips.simulator.exceptions.InvalidCPUException;
 
24
import org.feup.brunonova.drmips.simulator.mips.Input;
 
25
import org.feup.brunonova.drmips.simulator.util.Dimension;
 
26
import org.feup.brunonova.drmips.simulator.util.Point;
 
27
 
 
28
/**
 
29
 * Class that represents an AND port with 2 inputs and 1 output with the size of 1 bit.
 
30
 * 
 
31
 * @author Bruno Nova
 
32
 */
 
33
public class And extends SimpleBinaryOperationComponent {
 
34
        /**
 
35
         * And constructor.
 
36
         * @param id And's identifier.
 
37
         * @param latency The latency of the component.
 
38
         * @param position The component's position on the GUI.
 
39
         * @param in1Id The identifier of first the input.
 
40
         * @param in2Id The identifier of second the input.
 
41
         * @param outId The identifier of the output.
 
42
         * @throws InvalidCPUException If <tt>id</tt> is empty or duplicated.
 
43
         */
 
44
        public And(String id, int latency, Point position, String in1Id, String in2Id, String outId) throws InvalidCPUException {
 
45
                super(id, latency, "AND", "and", "and_description", position, new Dimension(30, 30), in1Id, in2Id, outId, 1);
 
46
        }
 
47
 
 
48
        @Override
 
49
        protected int operation(int in1, int in2) {
 
50
                return in1 & in2;
 
51
        }
 
52
 
 
53
        @Override
 
54
        protected List<Input> getLatencyInputs() {
 
55
                ArrayList<Input> inList = new ArrayList<Input>();
 
56
                int val1 = getInput1().getValue();
 
57
                int val2 = getInput2().getValue();
 
58
                if (val1 == val2) {  // inputs have identical logic values
 
59
                        if (val1 == 1) {  // both 1; use both
 
60
                                inList.add(getInput1());
 
61
                                inList.add(getInput2());
 
62
                        } else { // both 0; use just the earliest input
 
63
                                int lat1 = getInput1().getAccumulatedLatency();
 
64
                                int lat2 = getInput2().getAccumulatedLatency();
 
65
                                if (lat1 <= lat2) {
 
66
                                        inList.add(getInput1());
 
67
                                } else {
 
68
                                        inList.add(getInput2());
 
69
                                }
 
70
                        }
 
71
                } else if (val1 == 1) { // only val2 == 0
 
72
                        inList.add(getInput2());
 
73
                } else { // only val1 == 0
 
74
                        inList.add(getInput1());
 
75
                }
 
76
                return inList;
 
77
        }
 
78
}