How-to ? : Internal signals of same type than external ones

I have written a “deglitch” component that I’d like to be “universal” : The component should be able to interface with boolean or bit-vector signals.

class deglitch():This text will be hidden
    def __init__(self, deglitch_delay, initial_output=None):
        self.deglitch_delay = deglitch_delay
        self.initial_output = initial_output

    @block
    def inst(self, i_Clk,  i_Data, o_Data):

        DataSync = Signal(i_Data.val)
        DataMemo = Signal(i_Data.val)
        Counter  = Signal(intbv(0, min=0, max=self.deglitch_delay+1))
        DataOut  = Signal(o_Data.val)

        @always_seq(posedge(i_Clk), reset=None)
        def proc():
            DataSync.next = i_Data
            DataMemo.next = DataSync
            
            if DataMemo != DataSync :
                Counter.next = 0
            else :
                if Counter == self.deglitch_delay :
                    Counter.next = 0
                else :
                    Counter.next = Counter + 1
                
                if Counter == self.deglitch_delay-1 :
                    DataOut.next = DataMemo
                    #o_Data.next = DataMemo
            
        @always_comb
        def logic():
            o_Data.next = DataOut
        
        return proc, logic

My problem is with internal signals that need to adapt themselves to external signals : DataSync, DataMemo and DataOut type must be the same than i_Data and o_Data.
I tried many syntaxes and ended with “DataSync = Signal(i_Data.val)”.
This syntax works with simulation and VHDL conversion.

I’m not very comfortable with this syntax.

  1. Is this the right way of writing ?
  2. Using this syntax, I can’t specify an initial value for the output. Is there a workaround ?

Hi DrPi,

I addressed some of this in Discourse: New, copy, clone or duplicate?
And I have added this copy() function to my Signal class (in my local MyHDL library) and in my experimental Array() and StructType() classes.
It exactly achieves what you want :slight_smile: and I use it all the time.
My copy() function re-uses the initial value of the original object. I don’t readily see a use for the initial_output. But the copy() could be easily extended to take this initial_output argument.
However, I don’t know if it is a good idea to add extra arguments to the copy() call as this will divert it from the _copy
() and _deepcopy_() hooks.

Regards,

Josy

Hi Josyb,

Thanks for remembering me the discussion. It is not clear to me if you modified MyHDL to make copy() work.
I tried the copy() method but whatever I do, I get the following exception :
RuntimeError: maximum recursion depth exceeded while calling a Python object

Can you provide a small example ?

I actually modified my local MyHDL _Signal.py source to provide a copy() member.
If you call copy() Python will try to do the work for you. I wonder where the recursion comes from.
I guess you have to live with newsig = Signal(oldsig.val) for the time being.

I read up on ‘New Duplicate Copy Clone’ (google search) and actually clone() might be the best word after all to describe what is going on. It would also avoid any similarities to and confusion with the built-in copy() and deepcopy().

I’ll stick with newsig = Signal(oldsig.val) for now.
Could you make a Pull-Request on github ?
Thanks for your help.

I’d like to have a formal discussion involving @jandecaluwe (and others) before submitting a PR.

Ok.
Please, let me know when you have news on this topic.