-
Notifications
You must be signed in to change notification settings - Fork 76
Boolean Function
Boolean functions are used to represent the functionality of each gate within the netlist. HAL enables the reverse engineer to operate upon these functions by providing a set of functions to extend, simplify, and evaluate them. In general, they consist of a set of variables that are connected using Boolean operators.
If available, the user can retrieve the Boolean functions of a gate by using get_boolean_function
and get_boolean_functions
respectively. While the first command requires the name of the desired Boolean function as input, the latter simply returns a list of all available Boolean functions.
gate = netlist.get_gate_by_id(1) # get gate with ID 1
func = gate.get_boolean_function("func_name") # return only the Boolean function specified by "func_name"
func_list = gate.get_boolean_functions() # return a list of all Boolean functions of that gate
While for most gates the Boolean functions are actually associated with their gate type, some special gates like LUTs may feature individual functions independent of their gate type. Hence, we recommend always retrieving the Boolean function of a gate from the gate itself and not its gate type, despite the gate type offering similar functionality.
The reverse engineer may also create new Boolean functions by constructing them from a string as follows:
func = hal_py.BooleanFunction.from_string("(A&B)+C"). # constructs a Boolean function object from string
In general, Boolean functions support NOT ("!"
, "'"
), AND ("&"
, "*"
, " "
), OR ("|"
, "+"
), and XOR ("^"
) operations as well as brackets ("("
, ")"
) when being parsed from a string. Since " "
is generally interpreted as AND, be careful with spaces in your equation!
The string representation of a Boolean function is available via the str
function. To print a Boolean function, it suffices to call print
on the function itself.
func_str = str(func) # returns the string representation of the Boolean function
print(func) # prints the Boolean function
In addition to the construction from a string, Boolean functions can also be composed by using their constructors and provided operators. Oftentimes, the size of the Boolean function (in bits) must be provided as well.
var_A = hal_py.BooleanFunction.Var("A") # variable A
const_1 = hal_py.BooleanFunction.Const(hal_py.BooleanFunction.Value.ONE) # constant 1
func = hal_py.BooleanFunction.And(var_1, const_1, 1) # Boolean function "A & 1" of size 1
Two Boolean functions can be joined using the &
, |
, and ^
operators and negated using the ~
operator. Most of these operations are also available in-place.
func_A = hal_py.BooleanFunction.Var("A") # Boolean function containing only variable A
func_B = hal_py.BooleanFunction.Var("B") # Boolean function containing only variable B
func_1 = hal_py.BooleanFunction.Const(hal_py.BooleanFunction.Value.ONE) # constant 1
nand_func = ~(func_A & func_B) # build a NAND from A and B
nand_func |= func_1 # in-place OR
Additionally, two Boolean functions may also be combined by replacing a variable in one function with the other function using substitute
. This allows to retrieve the combined Boolean function of gates that are connected in series. Combining two Boolean functions in this way may require the renaming of some variables before substitution. In the example below, func_1
represents the function of an OR gate, while func_2
gives the function of an AND gate. Both gates use the variables "A"
and "B"
as inputs, although they are not connected to the same net and may thus have different values. Therefore, when replacing "B"
of func_1
with func_2
, at least "A"
of func_2
should be renamed.
func_1 = hal_py.BooleanFunction.from_string("A + B") # create OR function
func_2 = hal_py.BooleanFunction.from_string("A & B") # create AND function
func_2 = func_2.substitute("A", "C") # rename "A" of func_2 to "C"
func_1 = func_1.substitute("B", func_2) # substitute "B" of func_1 with func_2
print(func_1) # prints "A + (C & B)"
Note: For more information on how to combine multiple gates and generate a Boolean Function depending on multiple gates and inputs, have a look at the netlist utilities class