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 enum
Please seelookupMemory(String,int,boolean,MemoryType)
for details on this enum.static enum
Please seemockPulser(String,boolean,PulserType)
for details on this enum. -
Method Summary
Modifier and TypeMethodDescriptionstatic Subcircuit
Loads the subcircuit namedsubcircuitName
from the.sim
filesimFilePath
.com.ra4king.circuitsim.simulator.Circuit
Returns the CircuitSimCircuit
for this subcircuit.com.ra4king.circuitsim.gui.CircuitBoard
Returns the CircuitSimCircuitBoard
for this subcircuit.com.ra4king.circuitsim.gui.CircuitManager
com.ra4king.circuitsim.gui.CircuitSim
Returns theCircuitSim
instance simulating the circuit.com.ra4king.circuitsim.simulator.CircuitState
Returns the CircuitSimCircuitState
of this subcircuit.getName()
Returns the name of this subcircuit as written in the test file.int
Returns the number of Input Pin or Output Pin components in this circuit.com.ra4king.circuitsim.simulator.Simulator
Returns the CircuitSimSimulator
running 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 labelledlabel
in this subcircuit and returns a wrapper around it.Finds a Pin component labelledpinLabel
in this subcircuit and returns a wrapper around it.lookupRegister
(String regLabel, int wantBits, boolean recursive) Finds a Register component labelledregLabel
in 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.void
Resets the simulation for this subcircuit.snitchTunnel
(String label, int wantBits) Add anOutputPin
that 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
String
holding the subcircuit name. Not normalized, so represents exactly what the test file specified. - See Also:
-
getCircuitSim
public com.ra4king.circuitsim.gui.CircuitSim getCircuitSim()Returns theCircuitSim
instance simulating the circuit.This exposes an internal CircuitSim API. Do not use unless you know what you are doing.
- Returns:
- the
CircuitSim
instance used for simulation
-
getSimulator
public com.ra4king.circuitsim.simulator.Simulator getSimulator()Returns the CircuitSimSimulator
running the circuit.This exposes an internal CircuitSim API. Do not use unless you know what you are doing.
- Returns:
- the
Simulator
instance used for simulation
-
getCircuitBoard
public com.ra4king.circuitsim.gui.CircuitBoard getCircuitBoard()Returns the CircuitSimCircuitBoard
for 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
CircuitBoard
of this subcircuit
-
getCircuitManager
public com.ra4king.circuitsim.gui.CircuitManager getCircuitManager() -
getCircuit
public com.ra4king.circuitsim.simulator.Circuit getCircuit()Returns the CircuitSimCircuit
for 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
Circuit
of this subcircuit
-
getCircuitState
public com.ra4king.circuitsim.simulator.CircuitState getCircuitState()Returns the CircuitSimCircuitState
of this subcircuit.This exposes an internal CircuitSim API. Do not use unless you know what you are doing.
- Returns:
- the
CircuitState
of top level state of this subcircuit
-
fromPath
Loads the subcircuit namedsubcircuitName
from the.sim
filesimFilePath
.Note that for the sake of students' stress levels,
subcircuitName
is normalized to a lowercase alphanumeric string before searching the.sim
file 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
Subcircuit
simulating 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 labelledpinLabel
in 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,
pinLabel
is normalized as described forlookupSubcircuit(CircuitSim,String)
before lookup.- Parameters:
pinLabel
- the label of the pin, ornull
to 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
InputPin
orOutputPin
depending onwantInputPin
- Throws:
IllegalArgumentException
- if the subcircuit does not contain exactly one matching pin- See Also:
-
lookupRegister
Finds a Register component labelledregLabel
in 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
regLabel
is null, only 1 register period.Also for the sake of students' stress levels,
regLabel
is normalized as described forlookupSubcircuit(CircuitSim,String)
before lookup.- Parameters:
regLabel
- the label of the register, ornull
to say this should be the only registerwantBits
- how many bits the register found should haverecursive
- whether or not to recursively search subcircuits- Returns:
- a
Register
wrapping 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 labelledlabel
in 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,
pinLabel
is normalized as described forlookupSubcircuit(CircuitSim,String)
before lookup.- Parameters:
label
- the label of the RAM/ROM, ornull
to 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.RAM
orSubcircuit.MemoryType.ROM
- Returns:
- either an
Ram
orRom
depending ontype
- Throws:
IllegalArgumentException
- if the subcircuit does not contain exactly one matching RAM/ROM- See Also:
-
snitchTunnel
Add anOutputPin
that 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,
pinLabel
is normalized as described forlookupSubcircuit(CircuitSim,String)
before lookup.- Parameters:
label
- the label of the tunnel. Must not benull
wantBits
- how many bits the tunnel found should have- Returns:
- an
OutputPin
wrapping an Pin component attached to the specified tunnel - Throws:
IllegalArgumentException
- iflabel
is 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 behindMockRegister
s, 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
MockRegister
which 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 (noteButton
andClock
have their own methods that callMockPulser.pulse()
).- Parameters:
label
- the label of the button/clock, ornull
if 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
Button
orClock
(depending ontype
) wrapping an InputPin which allows you to send pulses into the student's circuit - See Also:
-