interface_modport.sv 1.32 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
interface Interface #(parameter WIDTH = 4) (
    input clock,
    output [$clog2(WIDTH) - 1:0] indices [2]
);
    logic [2*WIDTH-1:0] x;
    modport ModportA(
        input clock,
        output indices,
        input .x(x[2*WIDTH-1:WIDTH]), .y(x[WIDTH-1:0])
    );
    modport ModportB(
        input clock,
        output .x(x)
    );
endinterface

module ModuleA(Interface.ModportA m);
    assign m.indices[0] = $clog2(m.x);
    assign m.indices[1] = $clog2(m.y);
endmodule

module ModuleB(Interface.ModportB m);
23
    parameter WIDTH = 0;
24 25 26 27 28 29 30 31 32 33 34 35
    initial m.x = 1;
    always @(posedge m.clock) begin
        logic temp;
        temp = m.x[WIDTH-1];
        for (integer i = WIDTH-1; i > 0; --i) begin
            m.x[i] = m.x[i-1];
        end
        m.x[0] = temp;
    end
endmodule

module ModuleBWrapper(Interface.ModportB m);
36 37 38 39
    parameter WIDTH = 0;
    ModuleB #(WIDTH) b(m);
    integer i = 0;
    initial #1 $display("shadow i = %d, %b", i, m.x);
40 41 42 43 44 45 46 47 48 49 50
endmodule

module ModuleAWrapper(Interface.ModportA m);
    ModuleA a(m);
endmodule

module Tester(input clock);
    parameter WIDTH = 1;
    logic [WIDTH-1:0] idx1, idx2;
    Interface #(2 ** WIDTH) i(clock, '{idx1, idx2});
    ModuleAWrapper a(i);
51
    ModuleBWrapper #(2 * 2 ** WIDTH) b(i);
52 53 54
    always @(negedge clock)
        $display("%d %0d %2d %2d %b", $time, WIDTH, idx1, idx2, i.x);
endmodule