# Ideas on how to create factory for blocks/ signals

**URL:** <https://discourse.myhdl.org/t/ideas-on-how-to-create-factory-for-blocks-signals/101>\
**Category:** Support\
**Created:** [June 25, 2016, 10:30pm UTC](https://discourse.myhdl.org/t/ideas-on-how-to-create-factory-for-blocks-signals/101 "2016-06-25T22:30:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![forumulator](https://avatars.discourse-cdn.com/v4/letter/f/41988e/32.png) [@forumulator](https://discourse.myhdl.org/u/forumulator)\
**Post date:** [June 25, 2016, 10:30pm UTC](https://discourse.myhdl.org/t/ideas-on-how-to-create-factory-for-blocks-signals/101/1 "2016-06-25T22:30:38Z")

</div>

Hi ,

Suppose I have 3 `myhdl.block`s `dut1`, `dut2`, `dut3`, of which I want to test many times in various combinations. Each of the duts take several signal arguments (have several ports). My problem is this. While creating the tests, I have to initialize the signals and instantiate the blocks repeatedly, and this leads to code duplication. For example,

![](https://canada1.discourse-cdn.com/flex030/uploads/myhdl/original/1X/0247424d5cdfdb35b2f102078fba588d56fafdd3.JPG)

I’d like some ideas on how to make a factory function that automatically does all the initializations and instantiations, so that I can just call `factory_func()`, return all the signals and blocks, and then write my test.

Here is what I did:

 ![](https://canada1.discourse-cdn.com/flex030/uploads/myhdl/original/1X/1b43c8b7b12828646c2abb5632aaf965c5e6c136.JPG)

Is this acceptable?

---

<div class="post-metadata">

**Author:** ![cfelton](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.myhdl.org/cfelton/32/3_2.png) [@cfelton](https://discourse.myhdl.org/u/cfelton)\
**Post date:** [June 27, 2016, 11:25am UTC](https://discourse.myhdl.org/t/ideas-on-how-to-create-factory-for-blocks-signals/101/2 "2016-06-27T11:25:32Z")

</div>

Personally, I won’t combine the definition of my top-level ports  
with the tests in the class. I would simply define the signals  
in a class and define the tests outside. You will need to duplicate  
the _dut_ instatiation but that is minor.

Often what I will do is create a dictionary of the top-level ports  
to be used:

```python
myblock_portmap = dict(sig1=Signal(bool(0)), ...)

```

and then in the test simply resuse the dict

```python
tbdut = myblock(**myblock_portmap)

```

The top-level ports are only defined onces and the instantiation  
is minimal (not a big deal to repeat).

The above can be used in multiple tests, this will have some down  
sides in that the signals might not be reinitialized. Depending  
on the usage you could run into trouble.

In you case you would want to some thing slightly different where  
you have a signal in multiple portmaps:

```python
sig1 = Signal()
blk1_portmap = dict(sig1=sig1, sig3=Signal())
blk2_portmap = dict(sig1=sig1, sig2=Signal())

```

My usage typically is slightly different, I often attached a  
default dictionary portmap to the blocks and subblocks

```python
myblock.portmap = dict(...)

```

This portmap is fixed (static) it works well for top-levels that  
are specific to a certain hardware but not as useful for subblocks  
where the ports are more dynamic. In addition, I will use a  
thin `PortMap` object

```python
class PortMap(dict):
    def __init__ (self, **ports):
        for k, v in ports.items():
            self. __dict__ [k] = v
        super(PortMap, self). __init__ (**ports)

# usage in the test
pm = portmap = PortMap(sig1=Signal(bool(0)), sig3=Signal(bool(0)))
dut = myblock(**portmap)

@instance
def tbstim():
    pm.sig1.next = False
    pm.sig3.next = True

```

---

<div class="post-metadata">

**Author:** ![Ravi\_Jain](https://avatars.discourse-cdn.com/v4/letter/r/47e85d/32.png) [@Ravi\_Jain](https://discourse.myhdl.org/u/Ravi_Jain)\
**Post date:** [June 28, 2016, 5:58pm UTC](https://discourse.myhdl.org/t/ideas-on-how-to-create-factory-for-blocks-signals/101/3 "2016-06-28T17:58:46Z")

</div>

I faced the same issue. I looked into pytest fixtures for it. [https://pytest.org/latest/fixture.html](https://pytest.org/latest/fixture.html)

My code after using it: [https://github.com/ravijain056/GEMAC/blob/dev/test/test\_mangement.py](https://github.com/ravijain056/GEMAC/blob/dev/test/test_mangement.py)

@cfelton Whats your say on using fixtures? Please review the code.

---

<div class="post-metadata">

**Author:** ![cfelton](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.myhdl.org/cfelton/32/3_2.png) [@cfelton](https://discourse.myhdl.org/u/cfelton)\
**Post date:** [July 6, 2016, 1:34am UTC](https://discourse.myhdl.org/t/ideas-on-how-to-create-factory-for-blocks-signals/101/4 "2016-07-06T01:34:12Z")

</div>

@Ravi_Jain yes, I think using the test fixture is a reasonable approach.
