Class Subcircuit
Holds a CircuitSim CircuitBoard (also known as a "subcircuit") and
the CircuitSim instance used to simulate it.
Your tests shouldn't need to touch this, since it mainly provides
methods for loading a subcircuit from a file and poking at its
internal state to find components to test — all things CircuitSimExtension handles
for you. Nevertheless, these APIs are public and documented for fun
and for the benefit of those hacking on the CircuitSim tester. Just beware
that they may not be stable APIs.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumPlease seelookupMemory(String,int,boolean,MemoryType)for details on this enum.static enumPlease seemockPulser(String,boolean,PulserType)for details on this enum. -
Method Summary
Modifier and TypeMethodDescriptionstatic SubcircuitLoads the subcircuit namedsubcircuitNamefrom the.simfilesimFilePath.com.ra4king.circuitsim.simulator.CircuitReturns the CircuitSimCircuitfor this subcircuit.com.ra4king.circuitsim.gui.CircuitBoardReturns the CircuitSimCircuitBoardfor this subcircuit.com.ra4king.circuitsim.gui.CircuitManagercom.ra4king.circuitsim.gui.CircuitSimReturns theCircuitSiminstance simulating the circuit.com.ra4king.circuitsim.simulator.CircuitStateReturns the CircuitSimCircuitStateof this subcircuit.getName()Returns the name of this subcircuit as written in the test file.intReturns the number of Input Pin or Output Pin components in this circuit.com.ra4king.circuitsim.simulator.SimulatorReturns the CircuitSimSimulatorrunning the circuit.lookupComponentCounts(Collection<String> componentNames, boolean inverse, boolean recursive) Finds if components with the given names or in the given component categories exist in this subcircuit.lookupMemory(String label, int wantBits, boolean recursive, Subcircuit.MemoryType type) Finds a RAM/ROM component labelledlabelin this subcircuit and returns a wrapper around it.Finds a Pin component labelledpinLabelin this subcircuit and returns a wrapper around it.lookupRegister(String regLabel, int wantBits, boolean recursive) Finds a Register component labelledregLabelin this subcircuit and returns a wrapper around it.mockPulser(String label, boolean recursive, Subcircuit.PulserType type) Mocks a CircuitSim Button or Clock component (i.e., a "pulser") in this subcircuit by replacing it with an input pin.mockRegister(com.ra4king.circuitsim.simulator.components.memory.Register reg) Mocks a CircuitSim register in this subcircuit by replacing it with input and output pins.voidResets the simulation for this subcircuit.snitchTunnel(String label, int wantBits) Add anOutputPinthat spys on the value of the specified tunnel.
-
Method Details
-
getName
Returns the name of this subcircuit as written in the test file.- Returns:
- a
Stringholding the subcircuit name. Not normalized, so represents exactly what the test file specified. - See Also:
-
getCircuitSim
public com.ra4king.circuitsim.gui.CircuitSim getCircuitSim()Returns theCircuitSiminstance simulating the circuit.This exposes an internal CircuitSim API. Do not use unless you know what you are doing.
- Returns:
- the
CircuitSiminstance used for simulation
-
getSimulator
public com.ra4king.circuitsim.simulator.Simulator getSimulator()Returns the CircuitSimSimulatorrunning the circuit.This exposes an internal CircuitSim API. Do not use unless you know what you are doing.
- Returns:
- the
Simulatorinstance used for simulation
-
getCircuitBoard
public com.ra4king.circuitsim.gui.CircuitBoard getCircuitBoard()Returns the CircuitSimCircuitBoardfor this subcircuit. This is a GUI-side thing.This exposes an internal CircuitSim API. Do not use unless you know what you are doing.
- Returns:
- the
CircuitBoardof this subcircuit
-
getCircuitManager
public com.ra4king.circuitsim.gui.CircuitManager getCircuitManager() -
getCircuit
public com.ra4king.circuitsim.simulator.Circuit getCircuit()Returns the CircuitSimCircuitfor this subcircuit. This is a simulation-side thing.This exposes an internal CircuitSim API. Do not use unless you know what you are doing.
- Returns:
- the
Circuitof this subcircuit
-
getCircuitState
public com.ra4king.circuitsim.simulator.CircuitState getCircuitState()Returns the CircuitSimCircuitStateof this subcircuit.This exposes an internal CircuitSim API. Do not use unless you know what you are doing.
- Returns:
- the
CircuitStateof top level state of this subcircuit
-
fromPath
Loads the subcircuit namedsubcircuitNamefrom the.simfilesimFilePath.Note that for the sake of students' stress levels,
subcircuitNameis normalized to a lowercase alphanumeric string before searching the.simfile for it, as are the names of subcircuits in the file. So"1-bit adder!"will match"1 Bit Adder","1bit adder","1bitadder","1 B I T A D DD E R", and so on.- Parameters:
simFilePath- path to the subcircuit. Usually relative, like"adder.sim"subcircuitName- name of the subcircuit. Normalized as described above before lookup.- Returns:
- a new
Subcircuitsimulating the requested subcircuit - Throws:
Exception- specified byCircuitSim.loadCircuits()in violation of all good taste on Earth
-
resetSimulation
public void resetSimulation()Resets the simulation for this subcircuit.The same thing as clicking Simulation → Reset Simulation in CircuitSim.
-
getPinCount
public int getPinCount()Returns the number of Input Pin or Output Pin components in this circuit. Does not include subcircuits.- Returns:
- the total pin count of this subcircuit
-
lookupComponentCounts
public Map<String,Integer> lookupComponentCounts(Collection<String> componentNames, boolean inverse, boolean recursive) Finds if components with the given names or in the given component categories exist in this subcircuit.- Parameters:
componentNames- an iterable of component names, component category names, or some mixtureinverse- true if you want to invert the search, else falserecursive- true if you want to search subcircuits, else false- Returns:
- a map of distinct component names matching the given criteria to their number occurrences
-
lookupPin
Finds a Pin component labelledpinLabelin this subcircuit and returns a wrapper around it.To make sure the tester is deterministic, this method requires the subcircuit contain exactly one pin with a matching label. The algorithm checks this before verifying if a match has the right bit size or direction (input/output).
Also for the sake of students' stress levels,
pinLabelis normalized as described forlookupSubcircuit(CircuitSim,String)before lookup.- Parameters:
pinLabel- the label of the pin, ornullto say this should be the only pinwantInputPin- whether the pin found should be input or outputwantBits- how many bits the pin found should haverecursive- whether or not to recursively search subcircuits- Returns:
- either an
InputPinorOutputPindepending onwantInputPin - Throws:
IllegalArgumentException- if the subcircuit does not contain exactly one matching pin- See Also:
-
lookupRegister
Finds a Register component labelledregLabelin this subcircuit and returns a wrapper around it.To make sure the tester is deterministic, this method requires the subcircuit contain exactly one register with a matching label, or if
regLabelis null, only 1 register period.Also for the sake of students' stress levels,
regLabelis normalized as described forlookupSubcircuit(CircuitSim,String)before lookup.- Parameters:
regLabel- the label of the register, ornullto say this should be the only registerwantBits- how many bits the register found should haverecursive- whether or not to recursively search subcircuits- Returns:
- a
Registerwrapping the internal CircuitSim register component - Throws:
IllegalArgumentException- if the subcircuit does not contain exactly one matching register- See Also:
-
lookupMemory
public BaseMemory lookupMemory(String label, int wantBits, boolean recursive, Subcircuit.MemoryType type) Finds a RAM/ROM component labelledlabelin this subcircuit and returns a wrapper around it.To make sure the tester is deterministic, this method requires the subcircuit contain exactly one pin with a matching label. The algorithm checks this before verifying if a match has the right bit size or direction (input/output).
Also for the sake of students' stress levels,
pinLabelis normalized as described forlookupSubcircuit(CircuitSim,String)before lookup.- Parameters:
label- the label of the RAM/ROM, ornullto say this should be the only RAM (or only ROM)wantBits- how many bits the RAM/ROM found should haverecursive- whether or not to recursively search subcircuitstype- whether to look for aSubcircuit.MemoryType.RAMorSubcircuit.MemoryType.ROM- Returns:
- either an
RamorRomdepending ontype - Throws:
IllegalArgumentException- if the subcircuit does not contain exactly one matching RAM/ROM- See Also:
-
snitchTunnel
Add anOutputPinthat spys on the value of the specified tunnel.Sadly, a recursive search is not currently supported, but it could be added within the next 10-15 years — watch this space!
For the sake of students' stress levels,
pinLabelis normalized as described forlookupSubcircuit(CircuitSim,String)before lookup.- Parameters:
label- the label of the tunnel. Must not benullwantBits- how many bits the tunnel found should have- Returns:
- an
OutputPinwrapping an Pin component attached to the specified tunnel - Throws:
IllegalArgumentException- iflabelis null
-
mockRegister
Mocks a CircuitSim register in this subcircuit by replacing it with input and output pins. Useful for testing the combinational logic in a sequential circuit with exactly one register. For more details on the motivation behindMockRegisters, seeMockRegister.Each register port will be disconnected from anything else and replaced with a new Pin component reconnected to everything like before.
Normally this will be called by
Register.mock()and you won't need to call it directly.- Parameters:
reg- A CircuitSim register component to mock out- Returns:
- a
MockRegisterwhich effectively acts a collection of input and output pins - See Also:
-
mockPulser
Mocks a CircuitSim Button or Clock component (i.e., a "pulser") in this subcircuit by replacing it with an input pin. Useful for manipulating the clock when grading combination circuits. For more details, seeMockPulser.The button/clock will be disconnected from anything else and replaced with a new input Pin component reconnected to everything like before. You can call
MockPulser.pulse()to send a pulse (noteButtonandClockhave their own methods that callMockPulser.pulse()).- Parameters:
label- the label of the button/clock, ornullif this should be the only button or only clock in the circuitrecursive- whether or not to recursively search subcircuitstype- whether to look for a clock or button- Returns:
- a
ButtonorClock(depending ontype) wrapping an InputPin which allows you to send pulses into the student's circuit - See Also:
-