hwt.hdl package

This package contains classes for representation of HDL languages (hdl types, RtlSignal, HdlAssignmentContainer, HdlStmCodeBlockContainer etc.).

It also contains classes for representation of complex HW structures like dense transaction template (TransTmpl, FrameTmpl).

Submodules

hwt.hdl.constants module

Commonly used constants during HW devel.

class hwt.hdl.constants.SENSITIVITY[source]

Bases: enum.Enum

Sensitivity used in sensitivity resolver

ANY = 3
FALLING = 2
RISING = 1

hwt.hdl.frameTmpl module

class hwt.hdl.frameTmpl.FrameTmpl(origin: hwt.hdl.types.hdlType.HdlType, wordWidth: int, startBitAddr: int, endBitAddr: int, transParts: List[hwt.hdl.transPart.TransPart])[source]

Bases: object

Frame template is metainfomation about data structure, it’s the template of transaction for specific interface

Usuall flow of frame generatig consists of these steps:

  1. Describe format of data by HDL type (HStruct, HUnion …)
  2. Convert it to TransTmpl to resolve addresses of each field in structure
  3. Split parts of TransTmpl into words
  4. Use parts in words to assembly frames
Variables:_fieldToTPart – dictionary {HStructField: TransPart} to resolve this association, None by default, builded when packData is called and is not builded
Note:others ivars described in __init__
_FrameTmpl__repr__getName(transPart: hwt.hdl.transPart.TransPart, fieldWidth: int)

Get name string for a field

_FrameTmpl__repr__word(index: int, width: int, padding: int, transParts: List[hwt.hdl.transPart.TransPart])
__init__(origin: hwt.hdl.types.hdlType.HdlType, wordWidth: int, startBitAddr: int, endBitAddr: int, transParts: List[hwt.hdl.transPart.TransPart])[source]
Parameters:
  • origin – instance of HType (usually HStruct) from which this FrameTmpl was generated from
  • wordWidth – width of word on interface where this template should be used
  • startBitAddr – bit offset where this frame starts
  • endBitAddr – bit offset where this frame ends (bit index of first bit behind this frame)
  • transParts – instances of TransPart which are parts of this frame
static _fieldToDataDict(dtype, path, data, res)[source]

Construct dictionary {StructField:value} for faster lookup of values for fields

_wordIndx(addr: int)[source]

convert bit address to index of word where this address is

static fieldToDataDict(dtype, data, res)[source]
static framesFromTransTmpl(transaction: TransTmpl, wordWidth: int, maxFrameLen: Union[int, float] = inf, maxPaddingWords: Union[int, float] = inf, trimPaddingWordsOnStart: bool = False, trimPaddingWordsOnEnd: bool = False) → Generator[FrameTmpl, None, None][source]

Convert transaction template into FrameTmpls

Parameters:
  • transaction – transaction template used which are FrameTmpls created from
  • wordWidth – width of data signal in target interface where frames will be used
  • maxFrameLen – maximum length of frame in bits, if exceeded another frame will be created
  • maxPaddingWords – maximum of continual padding words in frame, if exceed frame is split and words are cut of
  • trimPaddingWordsOnStart – trim padding from start of frame at word granularity
  • trimPaddingWordsOnEnd – trim padding from end of frame at word granularity
Attention:

if maxPaddingWords<inf trimPaddingWordsOnEnd or trimPaddingWordsOnStart has to be True to decide where padding should be trimmed

getWordCnt()[source]

Get count of words in this frame

packData(data)[source]

Pack data into list of BitsVal of specified dataWidth

Parameters:data – dict of values for struct fields {fieldName: value}
Returns:list of BitsVal which are representing values of words
walkWords(showPadding: bool = False) → Generator[Tuple[int, List[Union[hwt.hdl.transPart.TransPart, hwt.hdl.frameTmplUtils.ChoicesOfFrameParts]]], None, None][source]

Walk enumerated words in this frame

Attention:not all indexes has to be present, only words with items will be generated when not showPadding
Parameters:showPadding – padding TransParts are also present
Returns:generator of tuples (wordIndex, list of TransParts in this word)

hwt.hdl.frameTmplUtils module

class hwt.hdl.frameTmplUtils.ChoiceOfFrameParts(origin, tmpl)[source]

Bases: list

Variables:
  • origin – ChoicesOfFrameParts instance
  • tmpl – TransTmpl which was item generated from
__init__(origin, tmpl)[source]

Initialize self. See help(type(self)) for accurate signature.

class hwt.hdl.frameTmplUtils.ChoicesOfFrameParts(startOfPart: int, origin: hwt.hdl.transTmpl.OneOfTransaction)[source]

Bases: hwt.hdl.frameTmplUtils.TransPartGroup

List of ChoiceOfFrameParts One of ChoiceOfFrameParts is used to represent the word, item depends on context

Variables:
  • origin – OneOfTransaction instance
  • startOfPart – bit addr of start of this group of frame parts
  • endOfPart – bit addr of end of this group of frame parts
  • _isLast – flag which means this is the last part of original union
__init__(startOfPart: int, origin: hwt.hdl.transTmpl.OneOfTransaction)[source]

Initialize self. See help(type(self)) for accurate signature.

resolveEnd()[source]
class hwt.hdl.frameTmplUtils.TransPartGroup[source]

Bases: list

Abstract parent class for groups of TransParts

bit_length()[source]
getBusWordBitRange() → Tuple[int, int][source]
Returns:bit range which contains data of this part on bus data signal
isLastPart() → bool[source]
Returns:True if this part is last in parts derived from original field else False
setIsLast(val: bool) → None[source]
class hwt.hdl.frameTmplUtils.TransTmplWordIterator(wordWidth)[source]

Bases: object

Iterator which reinterprets any structure as generator of bits of specified width

__init__(wordWidth)[source]

Initialize self. See help(type(self)) for accurate signature.

fullWordCnt(start: int, end: int)[source]

Count of complete words between two addresses

groupByWordIndex(transaction: TransTmpl, offset: int)[source]

Group transaction parts splited on words to words

Parameters:transaction – TransTmpl instance which parts should be grupped into words
Returns:generator of tuples (wordIndex, list of transaction parts in this word)
splitOnWords(transaction, addrOffset=0)[source]
Returns:generator of TransPart instance
hwt.hdl.frameTmplUtils.groupIntoChoices(splitsOnWord, wordWidth: int, origin: hwt.hdl.transTmpl.OneOfTransaction)[source]
Parameters:splitsOnWord – list of lists of parts (fields splited on word boundaries)
Returns:generators of ChoicesOfFrameParts for each word which are not crossing word boundaries
hwt.hdl.frameTmplUtils.iterSort(iterators, cmpFn)[source]

Sort items from iterators(generators) by alwas selecting item with lowest value (min first)

Returns:generator of tuples (origin index, item) where origin index is index of iterator in “iterators” from where item commes from

hwt.hdl.hdlObject module

class hwt.hdl.hdlObject.HdlObject[source]

Bases: object

Base Hdl object class for object which can be directly serialized to target HDL language

hwt.hdl.operator module

class hwt.hdl.operator.Operator(operator: hwt.hdl.operatorDefs.OpDefinition, operands: Tuple[Union[hwt.synthesizer.rtlLevel.mainBases.RtlSignalBase, hwt.hdl.value.HValue]])[source]

Bases: hwt.hdl.hdlObject.HdlObject

Class of operator in expression tree

Variables:
  • operands – list of operands
  • evalFn – function to evaluate this operator
  • operator – OpDefinition instance
  • result – result signal of this operator
__init__(operator: hwt.hdl.operatorDefs.OpDefinition, operands: Tuple[Union[hwt.synthesizer.rtlLevel.mainBases.RtlSignalBase, hwt.hdl.value.HValue]])[source]

Initialize self. See help(type(self)) for accurate signature.

_destroy()[source]
_replace_input(inp: hwt.synthesizer.rtlLevel.rtlSignal.RtlSignal, replacement: hwt.synthesizer.rtlLevel.rtlSignal.RtlSignal)[source]

Replace operand signal (non-recursively)

Attention:costly operation because all records in operand cache for all inputs may be potentially updated
_walk_public_drivers(seen: set) → Generator[hwt.synthesizer.rtlLevel.rtlSignal.RtlSignal, None, None][source]

Walk all non hidden signals in an expression

_walk_sensitivity(casualSensitivity: Set[hwt.synthesizer.rtlLevel.rtlSignal.RtlSignal], seen: Set[hwt.synthesizer.rtlLevel.rtlSignal.RtlSignal], ctx: hwt.hdl.sensitivityCtx.SensitivityCtx)[source]
See:hwt.synthesizer.rtlLevel.rtlSignal.RtlSignal._walk_sensitivity()
staticEval()[source]

Recursively statistically evaluate result of this operator

static withRes(opDef, operands, resT)[source]

Create operator with result signal

Variables:
  • resT – data type of result signal
  • outputs – iterable of signals which are outputs from this operator
hwt.hdl.operator.getCtxFromOps(operands)[source]
hwt.hdl.operator.isConst(item)[source]
Returns:True if expression is constant

hwt.hdl.operatorDefs module

class hwt.hdl.operatorDefs.AllOps[source]

Bases: object

Attention:Remember that and operator “and” is & and “or” is |, “and” and “or” can not be used because they can not be overloaded
Attention:These are operators of internal AST, they are not equal to verilog or vhdl operators
ADD = <OpDefinition ADD>
AND = <OpDefinition AND>
BitsAsSigned = <OpDefinition BitsAsSigned>
BitsAsUnsigned = <OpDefinition BitsAsUnsigned>
BitsAsVec = <OpDefinition BitsAsVec>
CALL = <OpDefinition CALL>
CONCAT = <OpDefinition CONCAT>
DIV = <OpDefinition DIV>
DOT = <OpDefinition DOT>
DOWNTO = <OpDefinition DOWNTO>
EQ = <OpDefinition EQ>
FALLING_EDGE = <OpDefinition FALLING_EDGE>
GE = <OpDefinition GE>
GT = <OpDefinition GT>
INDEX = <OpDefinition INDEX>
LE = <OpDefinition LE>
LT = <OpDefinition LT>
MINUS_UNARY = <OpDefinition MINUS_UNARY>
MOD = <OpDefinition MOD>
MUL = <OpDefinition MUL>
NE = <OpDefinition NE>
NOT = <OpDefinition NOT>
OR = <OpDefinition OR>
POW = <OpDefinition POW>
RISING_EDGE = <OpDefinition RISING_EDGE>
SUB = <OpDefinition SUB>
TERNARY = <OpDefinition TERNARY>
TO = <OpDefinition TO>
XOR = <OpDefinition XOR>
class hwt.hdl.operatorDefs.OpDefinition(evalFn, allowsAssignTo=False)[source]

Bases: object

Operator definition

Variables:
  • id – name of operator
  • _evalFn – function which evaluates operands
__init__(evalFn, allowsAssignTo=False)[source]

Initialize self. See help(type(self)) for accurate signature.

eval(operator, simulator=None)[source]

Load all operands and process them by self._evalFn

hwt.hdl.operatorDefs._getVal(v)[source]
hwt.hdl.operatorDefs.bitsAsSignedFn(a)[source]
hwt.hdl.operatorDefs.bitsAsUnsignedFn(a)[source]
hwt.hdl.operatorDefs.bitsAsVec(a)[source]
hwt.hdl.operatorDefs.bitsToIntFn(a)[source]
hwt.hdl.operatorDefs.callFn(fn, *operands, **kwargs)[source]
hwt.hdl.operatorDefs.concatFn(a, b)[source]
hwt.hdl.operatorDefs.dotOpFn(a, name)[source]
hwt.hdl.operatorDefs.downtoFn(a, b)[source]
hwt.hdl.operatorDefs.eqFn(a, b)[source]
hwt.hdl.operatorDefs.intToBitsFn(a, t)[source]
hwt.hdl.operatorDefs.isEventDependentOp(operator)[source]
hwt.hdl.operatorDefs.o

list of weak references to the object (if defined)

hwt.hdl.operatorDefs.onFallingEdgeFn(a)[source]
hwt.hdl.operatorDefs.onRisingEdgeFn(a)[source]
hwt.hdl.operatorDefs.power(base, exp)[source]
hwt.hdl.operatorDefs.ternaryFn(a, b, c)[source]
hwt.hdl.operatorDefs.toFn(a, b)[source]

hwt.hdl.operatorUtils module

hwt.hdl.operatorUtils._replace_input_in_expr(expr: Union[hwt.synthesizer.rtlLevel.mainBases.RtlSignalBase, hwt.hdl.value.HValue], toReplace: Union[Tuple[hwt.synthesizer.rtlLevel.mainBases.RtlSignalBase, hwt.synthesizer.rtlLevel.mainBases.RtlSignalBase], Dict[hwt.synthesizer.rtlLevel.mainBases.RtlSignalBase, hwt.synthesizer.rtlLevel.mainBases.RtlSignalBase]]) → hwt.synthesizer.rtlLevel.mainBases.RtlSignalBase[source]
Returns:newly rewritten expression with the subexpression replaced, True if changed else False
hwt.hdl.operatorUtils.replace_input_in_expr(topStatement: hwt.hdl.statements.statement.HdlStatement, parentStm: hwt.hdl.statements.statement.HdlStatement, expr: Union[hwt.synthesizer.rtlLevel.mainBases.RtlSignalBase, hwt.hdl.value.HValue], toReplace: Union[Tuple[hwt.synthesizer.rtlLevel.mainBases.RtlSignalBase, hwt.synthesizer.rtlLevel.mainBases.RtlSignalBase], Dict[hwt.synthesizer.rtlLevel.mainBases.RtlSignalBase, hwt.synthesizer.rtlLevel.mainBases.RtlSignalBase]]) → Tuple[hwt.synthesizer.rtlLevel.mainBases.RtlSignalBase, bool][source]
Returns:tuple (newExpression, True if expr is toReplace and should be replaced else False)

hwt.hdl.portItem module

class hwt.hdl.portItem.HdlPortItem(name, direction, dtype, unit)[source]

Bases: object

HDL entity/module/component port item Used to split signal paths on component boundary.

Note:src/dst are named based on input output signal direction both dst and src can be parent/component signal, it depends on direction
__init__(name, direction, dtype, unit)[source]

Initialize self. See help(type(self)) for accurate signature.

_walk_sensitivity(casualSensitivity: set, seen: set, ctx: hwt.hdl.sensitivityCtx.SensitivityCtx)[source]
See:hwt.synthesizer.rtlLevel.rtlSignal.RtlSignal._walk_sensitivity()
connectInternSig(signal)[source]

Connect signal from internal side of of this component to this port.

connectOuterSig(signal)[source]

Connect to port item on subunit

getInternSig()[source]

return signal inside unit which has this port

getOuterSig()[source]

return signal inside unit which has this port

hwt.hdl.sensitivityCtx module

class hwt.hdl.sensitivityCtx.SensitivityCtx(initSeq=None)[source]

Bases: hwt.pyUtils.uniqList.UniqList

Sensitivity list used for resolution of sensitivity for statement instances

Variables:contains_ev_dependency – True if this contains event dependent sensitivity
__init__(initSeq=None)[source]

Initialize self. See help(type(self)) for accurate signature.

clear()[source]

Remove all items from list.

extend(items)[source]

Extend list by appending elements from the iterable.

hwt.hdl.transPart module

class hwt.hdl.transPart.TransPart(parent: FrameTmpl, tmpl: Optional[hwt.hdl.transTmpl.TransTmpl], canBeRemoved: bool, startOfPart: int, endOfPart: int, inFieldOffset: int)[source]

Bases: object

Container for informations about parts of TransTmpl split on databus words

Variables:
  • parent – instance of FrameTmpl
  • tmpl – origin template which is this representation of (StructField/HdlType instance)
  • canBeRemoved – True if it is padding to assert data alignment and is not actually part of data and can be removed
  • isPadding – True if this TransaPart is just a padding and contains non valid data
  • startOfPart – bit addr of start of this part
  • endOfPart – bit addr of end of this part
  • inFieldOffset – bit offset of this part in parent field
__init__(parent: FrameTmpl, tmpl: Optional[hwt.hdl.transTmpl.TransTmpl], canBeRemoved: bool, startOfPart: int, endOfPart: int, inFieldOffset: int)[source]

Initialize self. See help(type(self)) for accurate signature.

bit_length() → int[source]
Returns:bit length of this part
getBusWordBitRange() → Tuple[int, int][source]
Returns:bit range which contains data of this part on bus data signal
getFieldBitRange() → Tuple[int, int][source]
Returns:bit range which contains data of this part on interface of field
isLastPart() → bool[source]
Returns:True if this part is last in parts derived from original field else False

hwt.hdl.transTmpl module

class hwt.hdl.transTmpl.OneOfTransaction(parent: hwt.hdl.transTmpl.TransTmpl, offset: int, shouldEnterFn: Callable[[hwt.hdl.transTmpl.TransTmpl], Tuple[bool, bool]], possibleTransactions: Tuple[hwt.hdl.transTmpl.TransTmpl])[source]

Bases: object

Container of possible transactions for transactions deriverd from HUnion type

Variables:
  • parent – parent TransTmpl instance
  • offset – bit addr offset in parent type structure
  • shouldEnterFn – function(transTmpl) which should return (shouldEnter, shouldUse) where shouldEnter is flag that means iterator should look inside of this actual object and shouldUse flag means that this field should be used (=generator should yield it)
  • possibleTransactions – tuple of TransTmpl instances from which only one can be used in same time
__init__(parent: hwt.hdl.transTmpl.TransTmpl, offset: int, shouldEnterFn: Callable[[hwt.hdl.transTmpl.TransTmpl], Tuple[bool, bool]], possibleTransactions: Tuple[hwt.hdl.transTmpl.TransTmpl])[source]

Initialize self. See help(type(self)) for accurate signature.

walkFlattenChilds() → Generator[Union[Tuple[Tuple[int, int], hwt.hdl.transTmpl.TransTmpl], hwt.hdl.transTmpl.OneOfTransaction], None, None][source]
Returns:generator of generators of tuples ((startBitAddress, endBitAddress), TransTmpl instance) for each possiblility in this transaction
class hwt.hdl.transTmpl.TransTmpl(dtype: hwt.hdl.types.hdlType.HdlType, bitAddr: int = 0, parent: Optional[TransTmpl] = None, origin: Optional[hwt.hdl.types.struct.HStructField] = None, rel_field_path: hwt.synthesizer.typePath.TypePath = ())[source]

Bases: object

Transaction template for types of constant size

Container of informations about frames generated from any HType (HStruct etc.) * contains precalculated address range for all members of type

Note:

Array/Stream items are are stored as a single instance so the memory consumption of this object is entirely independent on size of arrays which it describes.

Variables:
  • dtype – type of this item
  • bitAddr – offset of start of this item in bits
  • bitAddrEnd – end of this item in bits
  • parent – object which generated this item, optional TransTmpl
  • origin – object which was template for generating of this item
  • itemCnt – if this transaction template is for array or stream this is item count for such an array or stream
  • childrenAreChoice – flag which tells if children are sequence or only one of them can be used in same time
  • rel_field_path – path in original data type relative to parent
__init__(dtype: hwt.hdl.types.hdlType.HdlType, bitAddr: int = 0, parent: Optional[TransTmpl] = None, origin: Optional[hwt.hdl.types.struct.HStructField] = None, rel_field_path: hwt.synthesizer.typePath.TypePath = ())[source]

Initialize self. See help(type(self)) for accurate signature.

_loadFromArray(dtype: hwt.hdl.types.hdlType.HdlType, bitAddr: int) → int[source]

Parse HArray type to this transaction template instance

Returns:address of it’s end
_loadFromBits(dtype: hwt.hdl.types.hdlType.HdlType, bitAddr: int)[source]

Parse Bits type to this transaction template instance

Returns:address of it’s end
_loadFromHStream(dtype: hwt.hdl.types.stream.HStream, bitAddr: int) → int[source]

Parse HStream type to this transaction template instance

Returns:address of it’s end
_loadFromHStruct(dtype: hwt.hdl.types.hdlType.HdlType, bitAddr: int)[source]

Parse HStruct type to this transaction template instance

Returns:address of it’s end
_loadFromHType(dtype: hwt.hdl.types.hdlType.HdlType, bitAddr: int) → None[source]

Parse any HDL type to this transaction template instance

_loadFromUnion(dtype: hwt.hdl.types.hdlType.HdlType, bitAddr: int) → int[source]

Parse HUnion type to this transaction template instance

Returns:address of it’s end
bit_length() → int[source]
Returns:number of bits in this transaction
getFieldPath()[source]

Get field path which specifies the location in original HdlType data type

walkFlatten(offset: int = 0, shouldEnterFn=<function _default_shouldEnterFn>) → Generator[Union[Tuple[Tuple[int, int], hwt.hdl.transTmpl.TransTmpl], hwt.hdl.transTmpl.OneOfTransaction], None, None][source]

Walk fields in instance of TransTmpl

Parameters:
  • offset – optional offset for all children in this TransTmpl
  • shouldEnterFn – function (transTmpl) which returns True when field should be split on it’s children
  • shouldEnterFn – function(transTmpl) which should return (shouldEnter, shouldUse) where shouldEnter is flag that means iterator should look inside of this actual object and shouldUse flag means that this field should be used (=generator should yield it)
Returns:

generator of tuples ((startBitAddress, endBitAddress), TransTmpl instance)

hwt.hdl.transTmpl._default_shouldEnterFn(transTmpl: hwt.hdl.transTmpl.TransTmpl) → Tuple[bool, bool][source]

hwt.hdl.value module

class hwt.hdl.value.HValue(dtype: HdlType, val, vld_mask)[source]

Bases: typing.Generic

Wrap around hdl value with overloaded operators http://www.rafekettler.com/magicmethods.html

operators are overloaded in every type separately

__init__(dtype: HdlType, val, vld_mask)[source]
Parameters:
  • val – pythonic value representing this value
  • dtype – data type object from which this value was derived from
  • vld_mask – validity mask for value
_auto_cast(toT)[source]

type cast to compatible type

_dtype
_eq(other)[source]
classmethod _from_py(typeObj, val, vld_mask)[source]

from_py without value normalization and type checking

_is_full_valid()[source]
_reinterpret_cast(toT)[source]

type cast to type of same size

_walk_sensitivity(casualSensitivity: set, seen: set, ctx: hwt.hdl.sensitivityCtx.SensitivityCtx)[source]
See:hwt.synthesizer.rtlLevel.rtlSignal.RtlSignal._walk_sensitivity()
classmethod from_py(typeObj, val, vld_mask=None)[source]
staticEval()[source]
val
vld_mask
hwt.hdl.value.areHValues(*items)[source]
Returns:True if all arguments are instances of HValue class else False

hwt.hdl.valueUtils module

hwt.hdl.valueUtils.areSameHVals(a: Union[None, List[hwt.hdl.value.HValue]], b: Union[None, List[hwt.hdl.value.HValue]]) → bool[source]
Returns:True if two vectors of HValue/RtlSignal instances are same
Note:not just equal
hwt.hdl.valueUtils.isSameHVal(a: hwt.hdl.value.HValue, b: hwt.hdl.value.HValue) → bool[source]
Returns:True if two Value instances are same
Note:not just equal

hwt.hdl.variables module

class hwt.hdl.variables.SignalItem(name, dtype, def_val=None, virtual_only=False)[source]

Bases: hwt.hdl.hdlObject.HdlObject

basic hdl signal used to design circuits

__init__(name, dtype, def_val=None, virtual_only=False)[source]
Parameters:
  • name – name for better orientation in netlists (used only in serialization)
  • dtype – data type of this signal
  • def_val – value for initialization
  • virtual_only – flag indicates that this assignments is only virtual and should not be added into netlist, because it is only for internal notation
_set_def_val()[source]