Concatenation + assign statement

108 views
Skip to first unread message

Marcel M

unread,
May 22, 2021, 6:48:24 AM5/22/21
to pymtl-users

Hi, 

I used to program with verilog and I want to learn PyMTL3 so I want to know how to write the following red lines with PyMTL3

assign w_CARRY [0] = 1'b0;

assign o_result = {w_CARRY[2], w_SUM };   // Verilog Concatenation


`include "full_adder.v"

module ripple_carry_adder_2_FA

  (

   input [1:0]  i_add_term1,

   input [1:0]  i_add_term2,

   output [2:0] o_result

   );

     

  wire [2:0]    w_CARRY;

  wire [1:0]    w_SUM;

   

  // No carry input on first full adder 

  assign w_CARRY[0] = 1'b0;

   

  full_adder full_adder_1

    (

      .i_bit1(i_add_term1[0]),

      .i_bit2(i_add_term2[0]),

      .i_carry(w_CARRY[0]),

      .o_sum(w_SUM[0]),

      .o_carry(w_CARRY[1])

      );

 

  full_adder full_adder_2

    (

      .i_bit1(i_add_term1[1]),

      .i_bit2(i_add_term2[1]),

      .i_carry(w_CARRY[1]),

      .o_sum(w_SUM[1]),

      .o_carry(w_CARRY[2])

      );

   

  assign o_result = {w_CARRY[2], w_SUM };   // Verilog Concatenation

 

endmodule



Thanks

Best regards,

Marcel

Christopher Batten

unread,
May 23, 2021, 1:58:39 PM5/23/21
to Marcel M, pymtl-users

Hi Marcel,

Thank you for your interest in PyMTL3! Verilog assign statements correspond to either using the connection operator or using an update block in PyMTL3.

For example, there are two ways to assign constant to a signal. First you can use the connect operator (//=) to directly connect a signal to a constant in a single line like this:

s.w_CARRY[0] //= 0

Second, you can use a combinational update block with a blocking assignment like this:

@update
def upA():
s.w_CARRY[0] @= 0

Note that if you want to do some kind of logic then you _cannot_ do this:

s.a //= a.b + s.c

Instead you can use an update block like this:

@update
def upA():
s.a @= s.b + s.c

So the connect operator (//=) is only for _connecting_ signals (and constants) which is a subset of what you can do with assign statements in Verilog. PyMTL3 does however have some cool syntactic sugar that enables you to do this:

s.a //= lambda: s.b + s.c

Essentially this will create a combinational update block for you. For concatenation, PyMTL3 provides a concat operator that enables you to do this:

@update
def upA():
s.o_result @= concat( w_CARRY[2], w_SUM )

or you can also connect to a lambda:

s.o_result //= lambda: concat( w_CARRY[2], w_SUM )

Note that concatenation operator is essentially "logic" so you cannot do this:

s.o_result //= concat( w_CARRY[2], w_SUM )

Hope this helps!
-c
> --
> You received this message because you are subscribed to the Google Groups "pymtl-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to pymtl-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/pymtl-users/7cadbe8d-d1b4-4e36-95a5-528c3f41c9e8n%40googlegroups.com.

Peitian Pan

unread,
May 23, 2021, 2:48:49 PM5/23/21
to pymtl-users
Hi Marcel,

A follow up on Chris's response: instead of referring to the signals directly by their name (e.g., `w_CARRY[2]` and `w_SUM`), you might wish to refer to them as (e.g., `s.w_CARRY[2]` and `s.w_SUM`). The `s.` prefix tells the framework that you are trying to access a signal that belongs to component `s`, which is an instance of `full_adder`.

Let us know if you have further questions!

Best,
Peitian

Christopher Batten

unread,
May 23, 2021, 3:03:45 PM5/23/21
to Peitian Pan, pymtl-users
Ah good catch Peitian! Yes you have to use the s. prefix

Sent from my iPhone

On May 23, 2021, at 2:48 PM, Peitian Pan <pp...@cornell.edu> wrote:

Hi Marcel,

Marcel M

unread,
May 24, 2021, 4:11:05 AM5/24/21
to pymtl-users
Hi,
Thanks for your reply.
I still have a problem with the Concatenation.
When I write
s.out //= lambda: concat(s.w_CARRY[2], s.w_SUM)
or
@update
def upA():
     s.out @= concat(s.w_CARRY[2], s.w_SUM)
I get errors .
How can I fix them .


#=========================================================================
# RippleCarryAdder
#=========================================================================
from pymtl3 import *
from FullAdderGL import FullAdderGL

class RippleCarryAdder( Component ):
      def construct(s):
           s.in0 = InPort (2)
           s.in1 = InPort (2)
           s.out = OutPort(3)

           s.w_CARRY = Wire(3)

           s.w_SUM = Wire (2)

           s.w_CARRY[0] //= 0

           s.fas = FullAdderGL()
           s.in0[0] //= s.fas.a
           s.in1[0] //= s.fas.b
           s.fas.cin //= s.w_CARRY[0]
           s.w_SUM[0] //= s.fas.sum
           s.w_CARRY[1] //= s.fas.cout

           s.fas2 = FullAdderGL()

           s.in0[1] //= s.fas2.a
           s.in1[1] //= s.fas2.b
           s.w_CARRY[1] //= s.fas2.cin
           s.w_SUM[1] //= s.fas2.sum
           s.w_CARRY[2] //= s.fas2.cout

          @update
           def upA():
                   s.out @= concat(s.w_CARRY[2], s.w_SUM)





#-------------------------------------------------------------------------
# main
#-------------------------------------------------------------------------

if __name__ == "__main__":

    dut = RippleCarryAdder()
    dut.apply( DefaultPassGroup(textwave=True) )
    dut.sim_reset()

    dut.in0 @= 2
    dut.in1 @= 0
    dut.sim_tick()

    dut.in0 @= 2
    dut.in1 @= 2
    dut.sim_tick()

    dut.in0 @= 0x1
    dut.in1 @= 0x2
    dut.sim_tick()
    dut.sim_tick()

   dut.print_textwave()


Best regards,

Marcel

RippleCarryAdder.py
Capture d’écran (777).png
Capture d’écran (778).png

Christopher Batten

unread,
May 24, 2021, 8:59:07 AM5/24/21
to Marcel M, pymtl-users

Hi Marcel,

It would be best if you could (1) prepare a GitHub repo or better yet, simply use repl.it; and (2) clearly indicate the observable error so that others can reproduce your error and help provide advice. For example, your code below does not include the implementation of your FullAdderGL so it is hard for us to reproduce.

I did my best to reproduce your example in a repl.it here:

https://replit.com/@cbatten/ripple-carry-adder-example

Seems to work fine for me. In the code you have cut-and-pasted in your email there are some indentation errors ... so I am not quite sure what error you were seeing ...

Best,
-c
> To view this discussion on the web visit https://groups.google.com/d/msgid/pymtl-users/8b70fdd7-b288-424f-a970-3d01e8686ae5n%40googlegroups.com.
> <RippleCarryAdder.py><Capture d’écran (777).png><Capture d’écran (778).png>

Marcel M

unread,
May 25, 2021, 7:12:23 AM5/25/21
to pymtl-users
Hi Christopher ,
Thanks for your reply.
When I write  "s.out @=s.w_CARRY" ,  it works fine ,but when I write  "s.out @= concat(s.w_CARRY[2], s.w_SUM)" ,  I get errors .
I attached here a screen recording.

Best regards,

Marcel

Concat.mp4

Shunning Jiang

unread,
May 25, 2021, 7:57:47 AM5/25/21
to Marcel M, pymtl-users
Ah, what is the Python 3 version on your end? It looks very familiar
to some issues I was having when I try to let PyMTL3 support Python
3.9.
> --
> You received this message because you are subscribed to the Google Groups "pymtl-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to pymtl-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/pymtl-users/b13f6b9e-3cba-4e86-abda-5f7d032555d1n%40googlegroups.com.



--

Shunning Jiang

Ph.D. Candidate

Computer Systems Laboratory

School of Electrical and Computer Engineering

Cornell University

http://www.csl.cornell.edu/~shunning

Marcel M

unread,
May 25, 2021, 2:53:47 PM5/25/21
to pymtl-users
Python 3.9.4

Shunning Jiang

unread,
May 25, 2021, 3:01:42 PM5/25/21
to Marcel M, pymtl-users
Yes that's the reason why you are seeing the error but Chris didn't. So currently our framework is thoroughly tested to work with 3.6.x and 3.7.x. From 3.8.x to 3.9.x there is a critical AST change in Python which breaks some of our code. We are developing solutions to support both 3.6/7/8 and beyond 9.

If you are able to switch Python versions you could try to use Python 3.7.x to actually get the full experience. Otherwise we will push an upgrade to the framework when we finish the compatibility patch.

Best,
Shunning

Marcel M

unread,
May 25, 2021, 4:39:37 PM5/25/21
to pymtl-users
I downloaded Python 3.7 and now it works fine .
Thank you so much.
Best regards,
Marcel
Reply all
Reply to author
Forward
0 new messages