|
| 1 | +package edu.gatech.cs2110.circuitsim.api; |
| 2 | + |
| 3 | +import static com.ra4king.circuitsim.simulator.components.memory.Register.PORT_OUT; |
| 4 | + |
| 5 | +/** |
| 6 | + * Wraps a CircuitSim Register component. Currently changing the value |
| 7 | + * of the register is not supported, but you can observe its value. |
| 8 | + * |
| 9 | + * @author Austin Adams |
| 10 | + */ |
| 11 | +public class Register { |
| 12 | + private com.ra4king.circuitsim.simulator.components.memory.Register reg; |
| 13 | + private Subcircuit subcircuit; |
| 14 | + |
| 15 | + /** |
| 16 | + * Creates a new Register which wraps the provided {@code Register} |
| 17 | + * component and which lives in the provided {@code Subcircuit}. |
| 18 | + * |
| 19 | + * @param reg {@code Register} component to wrap |
| 20 | + * @param subcircuit where this pin lives |
| 21 | + */ |
| 22 | + public Register(com.ra4king.circuitsim.simulator.components.memory.Register reg, |
| 23 | + Subcircuit subcircuit) { |
| 24 | + this.reg = reg; |
| 25 | + this.subcircuit = subcircuit; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Returns the current value of the register. This value is not sign |
| 30 | + * extended. |
| 31 | + */ |
| 32 | + public int getQ() { |
| 33 | + return Bits.betterFloatingErrorMessage(() -> |
| 34 | + subcircuit.getCircuitState().getLastPushed(reg.getPort(PORT_OUT)).getValue()); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Returns the current value of the register, sign-extended to 32 |
| 39 | + * bits. Like {@code getQ()} except sign-extended. |
| 40 | + */ |
| 41 | + public int getQSext() { |
| 42 | + return Bits.sext(getQ(), reg.getBitSize()); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Returns the internal CircuitSim {@code Register} component this |
| 47 | + * object wraps. |
| 48 | + * <p> |
| 49 | + * <b>This exposes an internal CircuitSim API. Do not use unless you |
| 50 | + * know what you are doing.</b> |
| 51 | + * |
| 52 | + * @return the CircuitSim {@code Register} component wrapped by this |
| 53 | + * object. |
| 54 | + */ |
| 55 | + public com.ra4king.circuitsim.simulator.components.memory.Register getRegister() { |
| 56 | + return reg; |
| 57 | + } |
| 58 | + |
| 59 | + // TODO FIXME document |
| 60 | + public MockRegister mock() { |
| 61 | + return subcircuit.mockRegister(reg); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Returns the {@link Subcircuit}, a wrapper around a CircuitSim |
| 66 | + * {@code CircuitBoard} where this Register lives. |
| 67 | + * |
| 68 | + * @return the {@link Subcircuit} where this pin lives |
| 69 | + */ |
| 70 | + public Subcircuit getSubcircuit() { |
| 71 | + return subcircuit; |
| 72 | + } |
| 73 | +} |
0 commit comments