Class SubBinOps
Wrapper for several operations on a chain storing one or two binary counters.
Implements MSB detection, comparison, addition, subtraction, multiplication and division with remainder. Only one of these operations can be performed at a time. This wrapper is implemented such that all subroutines share their state attribute, making it very memory efficient.
This procedure requires at least 2 pins and it always uses the "outermost / leftmost" pins when traversing the chain. If an amoebot occurs on the chain multiple times, its predecessor and successor directions must be different for all occurrences.
Usage:
-
Establish a chain of amoebots such that each amoebot knows its predecessor and successor
(except the start and end amoebots). Each amoebot should store a bit
aand a bitb(if an operation for two counters is required). -
Initialize using the Init(Mode, bool, Direction, Direction, bool, bool) method.
You must pass the mode of operation, the bit
aand the two chain directions, plus the bitband the marked MSB ofafor some operations. The chain start should have no predecessor and the end should have no successor. - Call SetupPinConfig(PinConfiguration) to modify the pin configuration.
- Call ActivateSend() in the same round to start the procedure.
- After this, call ActivateReceive(), SetupPinConfig(PinConfiguration) and ActivateSend() in this order in every round.
- The procedure can be paused after each ActivateReceive() call and resumed by continuing with SetupPinConfig(PinConfiguration) in some future round.
- Call IsFinished() after ActivateReceive() to check whether the procedure is finished. The result is thereafter available through one of the interface methods, such as ResultBit(). If the operation has an optional overflow check (addition/subtraction) that you want to use, call IsFinishedOverflow() instead.
Namespace: AS2.Subroutines.BinaryOps
Assembly: .dll
Syntax
public class SubBinOps : Subroutine
Constructors
| Edit this page View SourceSubBinOps(Particle)
Declaration
public SubBinOps(Particle p)
Parameters
| Type | Name | Description |
|---|---|---|
| Particle | p |
Fields
| Edit this page View Sourceadd
Declaration
private SubAddition add
Field Value
| Type | Description |
|---|---|
| SubAddition |
comp
Declaration
private SubComparison comp
Field Value
| Type | Description |
|---|---|
| SubComparison |
div
Declaration
private SubDivision div
Field Value
| Type | Description |
|---|---|
| SubDivision |
mode
Declaration
private ParticleAttribute<SubBinOps.Mode> mode
Field Value
| Type | Description |
|---|---|
| ParticleAttribute<SubBinOps.Mode> |
msb
Declaration
private SubMSBDetection msb
Field Value
| Type | Description |
|---|---|
| SubMSBDetection |
mult
Declaration
private SubMultiplication mult
Field Value
| Type | Description |
|---|---|
| SubMultiplication |
state
Declaration
private ParticleAttribute<int> state
Field Value
| Type | Description |
|---|---|
| ParticleAttribute<int> |
sub
Declaration
private SubSubtraction sub
Field Value
| Type | Description |
|---|---|
| SubSubtraction |
Methods
| Edit this page View SourceActivateReceive()
Activation during ActivateBeep() to receive the beeps sent in the last round. Should always be called before SetupPinConfig(PinConfiguration) and ActivateSend(), except in the very first activation, where it should not be called.
Declaration
public void ActivateReceive()
ActivateSend()
Activation during ActivateBeep() to send the beeps required for this step. Must be called after ActivateReceive() and SetupPinConfig(PinConfiguration).
Declaration
public void ActivateSend()
CompResult()
Returns the result of the comparison procedure.
Should only be called after IsFinished()
returns true.
Declaration
public SubComparison.ComparisonResult CompResult()
Returns
| Type | Description |
|---|---|
| SubComparison.ComparisonResult | The result of comparing |
HaveOverflow()
Checks whether the operation created an overflow
(only works for addition, subtraction and multiplication).
Should only be called after IsFinished()
returns true.
Declaration
public bool HaveOverflow()
Returns
| Type | Description |
|---|---|
| bool |
Init(Mode, bool, Direction, Direction, bool, bool)
Initializes the subroutine. Must be called by each amoebot on the chain.
Declaration
public void Init(SubBinOps.Mode mode, bool a, Direction predDir, Direction succDir, bool b = false, bool msbA = false)
Parameters
| Type | Name | Description |
|---|---|---|
| SubBinOps.Mode | mode | The operation to be initialized. |
| bool | a | This amoebot's bit of |
| Direction | predDir | The direction of the predecessor. Should be NONE only at the start of the chain. |
| Direction | succDir | The direction of the successor. Should be NONE only at the end of the chain. |
| bool | b | The amoebot's bit of |
| bool | msbA | Whether this amoebot stores the MSB of |
IsFinished()
Checks whether the current procedure is finished. Should be called after
ActivateReceive(). Note that for addition and subtraction,
this becomes true before the overflow has been determined. Call
IsFinishedOverflow() to check whether the procedure has
finished completely.
Declaration
public bool IsFinished()
Returns
| Type | Description |
|---|---|
| bool |
|
IsFinishedOverflow()
Checks whether the current addition or subtraction procedure has
determined its overflow flag. This is similar to
IsFinished() but it will become true later.
Declaration
public bool IsFinishedOverflow()
Returns
| Type | Description |
|---|---|
| bool |
|
IsMSB()
Checks whether this amoebot is the MSB.
Should only be called after IsFinished()
returns true.
Declaration
public bool IsMSB()
Returns
| Type | Description |
|---|---|
| bool |
|
RemainderBit()
Returns the remainder bit resulting from division.
Should only be called after IsFinished()
returns true.
Declaration
public bool RemainderBit()
Returns
| Type | Description |
|---|---|
| bool |
|
ResultBit()
Returns the resulting bit for mathematical operations.
For division, this returns the bit of the quotient.
Should only be called after IsFinished()
returns true.
Declaration
public bool ResultBit()
Returns
| Type | Description |
|---|---|
| bool | This amoebot's bit of the resulting number. |
SetupPinConfig(PinConfiguration)
Sets up the required circuits for the next step in the given pin configuration. This must be called after ActivateReceive() and before ActivateSend().
Declaration
public void SetupPinConfig(PinConfiguration pc)
Parameters
| Type | Name | Description |
|---|---|---|
| PinConfiguration | pc | The pin configuration to set up. Partition set IDs will always equal one of the IDs of the contained pins. |