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

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
/*
    DrMIPS - Educational MIPS simulator
    Copyright (C) 2013-2014 Bruno Nova <ei08109@fe.up.pt>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package org.feup.brunonova.drmips.simulator.mips.components;

import java.util.Stack;
import org.feup.brunonova.drmips.simulator.exceptions.InvalidCPUException;
import org.feup.brunonova.drmips.simulator.mips.Data;
import org.feup.brunonova.drmips.simulator.mips.IsSynchronous;
import org.feup.brunonova.drmips.simulator.util.Point;

/**
 * An ALU that supports multiplications and divisions, and contains the <tt>HI</tt> and <tt>LO</tt> "registers".
 * 
 * @author Bruno Nova
 */
public class ExtendedALU extends ALU implements IsSynchronous {
	private final Data hi, lo;
	private final Stack<int[]> states = new Stack<int[]>(); // previous values
	
	/**
	 * ALU constructor.
	 * @param id ALU's identifier.
	 * @param latency The latency of the component.
	 * @param position The component's position on the GUI.
	 * @param in1Id The identifier of the first input.
	 * @param in2Id The identifier of the second input.
	 * @param controlId The identifier of the control input.
	 * @param outId The identifier of the output
	 * @param zeroId The identifier of the zero output.
	 * @throws InvalidCPUException InvalidCPUException InvalidCPUException If <tt>id</tt> is empty or duplicated.
	 */
	public ExtendedALU(String id, int latency, Point position, String in1Id, String in2Id, String controlId, String outId, String zeroId) throws InvalidCPUException {
		super(id, latency, position, in1Id, in2Id, controlId, outId, zeroId);
		setNameKey("extended_alu");
		setDescriptionKey("extended_alu_description");
		
		hi = new Data();
		lo = new Data();
	}
	
	@Override
	public void executeSynchronous() {
		controlALU.doSynchronousOperation(getInput1().getValue(), getInput2().getValue(), this, getControl().getValue());
	}

	@Override
	public void pushState() {
		states.push(new int[] {hi.getValue(), lo.getValue()});
	}

	@Override
	public void popState() {
		if(hasSavedStates()) {
			int[] val = states.pop();
			hi.setValue(val[0]);
			lo.setValue(val[1]);
		}
	}

	@Override
	public boolean hasSavedStates() {
		return !states.empty();
	}

	@Override
	public void clearSavedStates() {
		states.clear();
	}

	@Override
	public void resetFirstState() {
		while(hasSavedStates())
			popState();
	}
	
	@Override
	public boolean isWritingState() {
		return controlALU.isWritingState(getControl().getValue());
	}
	
	/**
	 * Returns the <tt>HI</tt> "register".
	 * @return The <tt>HI</tt> "register".
	 */
	public final Data getHI() {
		return hi;
	}
	
	/**
	 * Returns the <tt>HI</tt> "register".
	 * @return The <tt>HI</tt> "register".
	 */
	public final Data getLO() {
		return lo;
	}
	
	/**
	 * Resets the <tt>HI</tt> and <tt>LO</tt> registers to 0.
	 */
	public final void reset() {
		hi.setValue(0);
		lo.setValue(0);
	}
}