Newer
Older
B-ASIC Core Operations Module.
TODO: More info.
"""

Jacob Wahlman
committed
from b_asic.operation import Operation
Angus Lothian
committed
from b_asic.abstract_operation import AbstractOperation
from b_asic.abstract_graph_component import AbstractGraphComponent
from b_asic.graph_component import Name, TypeName
Angus Lothian
committed
class Input(Operation, AbstractGraphComponent):
@property
def type_name(self) -> TypeName:
return "in"
Angus Lothian
committed
class Constant(AbstractOperation):
Angus Lothian
committed
def __init__(self, value: Number = 0, name: Name = ""):
super().__init__(name)
self._output_ports = [OutputPort(0, self)] # TODO: Generate appropriate ID for ports.
def evaluate(self, inputs: list) -> list:
return [self.param("value")]
@property
def type_name(self) -> TypeName:
return "const"
Angus Lothian
committed
class Addition(AbstractOperation):
Angus Lothian
committed
def __init__(self, source1: OutputPort = None, source2: OutputPort = None, name: Name = ""):
super().__init__(name)
self._input_ports = [InputPort(0, self), InputPort(1, self)] # TODO: Generate appropriate ID for ports.
self._output_ports = [OutputPort(0, self)] # TODO: Generate appropriate ID for ports.
if source1 is not None:
self._input_ports[0].connect_to_port(source1)
if source2 is not None:
self._input_ports[1].connect_to_port(source2)
def evaluate(self, inputs: list) -> list:
return [inputs[0] + inputs[1]]
@property
def type_name(self) -> TypeName:
return "add"

Jacob Wahlman
committed
Angus Lothian
committed
class ConstantMultiplication(AbstractOperation):
"""Unary constant multiplication operation.
TODO: More info.
"""
Angus Lothian
committed
def __init__(self, coefficient: Number, source1: OutputPort = None, name: Name = ""):
super().__init__(name)
self._input_ports = [InputPort(0, self)] # TODO: Generate appropriate ID for ports.
self._output_ports = [OutputPort(0, self)] # TODO: Generate appropriate ID for ports.
Angus Lothian
committed
if source1 is not None:
self._input_ports[0].connect_to_port(source1)
def evaluate(self, inputs: list) -> list:
return [inputs[0] * self.param("coefficient")]
@property
def type_name(self) -> TypeName:
return "const_mul"