How to integrate jinja module inside PyGears

Viewed 26

I created jinja module as example, which looks like this

{% from 'snippet.j2' import module with context %}
{% call module() %}
    logic [$size(din.data)-1 : 0] res;
    assign res = din.data * din.data;
  {% if params['half'] %}
    assign dout.data = res / 2;
  {% else %}
    assign dout.data = res;
  {% endif %}
    assign din.ready = dout.ready;
    assign dout.valid = din.valid;
{% endcall %}

How should I use this module inside PyGears?

1 Answers

Okay, I think this should work.

If I understood correctly you are trying to create a Jinja template for a module that will multiply with 1/2 (in other words divide by two). First of all, make sure your Jinja file and module are named the same (this is a must so PyGears would know which Jinja template to use).

Having all this in mind let's say our module name is mulh

Python file would be something like this:

from pygears import gear, Intf, reg
from pygears.typing import Uint
from pygears.hdl import hdlgen
@gear
def mulh(din: Uint,*,half=False)->b'din*din':
    pass
mulh(Intf(Uint[8]))
hdlgen('/mulh', outdir='.')

This code will call your Jinja file and the HDL output would look like something like this:

module mulh
(
    input logic clk,
    input logic rst,
    dti.consumer din, // u8 (8)
    dti.producer dout // u16 (16)
);
    typedef logic [7:0] din_t; // u8
    typedef logic [15:0] dout_t; // u16
    din_t din_s;
    dout_t dout_s;
    assign din_s = din.data;
    assign dout.data = dout_s;
    logic [$size(din.data)-1 : 0] res;
    assign res          = din.data * din.data;
    assign dout.data    = res;
    assign din.ready    = dout.ready;
    assign dout.valid   = din.valid;
endmodule

To make it easier to picture all of this I made this picture bellow enter image description here

Related