- A Logic Circuit Simulation Library in C++ |
Line states only of lcs::Bus
objects can be set. lcs::InputBus
objects are readonly busses. Moreover, setting the line states of an lcs::Bus
object is provided through a locking mechanism. Such a mechanism is provided so that two lcs::Module
derivates do not end up setting the line states of the same bus un-noticed. In reality, a short-circuit occurs if two modules drive the same bus. The locking mechanism detects such a short-circuit (which in software occurs if one single bus is assigned to the output of two different modules) and warns the user. Moreover, because of such a locking mechanism, bus lines can only be set by a module which is a derivative of the lcs::Module
class and has locked the bus. Hence, this topic guide is usefull only for functional module implementers (see this for more information on implementing one's own module classes). Other users can ignore the rest of the discussion below.
A module desirous of driving a bus should lock the bus. Once locked, only the module which locked the bus can un-lock it. Locking and un-locking are done using the lcs::Bus::lock
and lcs::Bus::unLock
functions. A warning message is displayed if a module tries to lock a bus which has already been locked by another module. Moreover, if a module tries to set the lines of bus which has been locked by another module, then a lcs::ShortCircuitException
is thrown following a warning message.
Unlocking a bus is neccessary as it then allows other modules to lock the bus and set its lines. Typically, the output busses of a module should be locked in the constructor of the module class by passing the this
argument to the lock
function of the output busses. Similarly, the same output busses should be un-locked by the destructor of the module class by passing the this
argument to the unLock
function of the output busses.
[NOTE: The function lcs::Bus::lock
takes a second parameter which indicates the assignment delay for the bus. Read more on this here.]
A module which has successfully locked a bus can set the lines of the bus through the class lcs::Bus::LineAccessor
. The LineAccessor
object corresponding to a particular bus can be obtained from the lcs::Bus::getLineAccessor
function of the bus. The bus lines can then be set using the overloaded operator[]
of the LineAccessor
object. A LineAccessor
object is typically used when over-riding the functions lcs::Module::onStateChange
, lcs::Module::onPosEdge
, or lcs::Module::onNegEdge
while implementing functional modules. A detailed example of using the functions lcs::Bus::lock
, lcs::Bus::unLock
, lcs::Bus::getLineAccessor
, and the class lcs::Bus::LineAccessor
can be found here.