stackoverflow 21322049 - EDA Playground
Warning! This exercise has been opened in another tab; autosave has been disabled. Close this tab or refresh to reactivate.

 Languages & Libraries

 Tools & Simulators

 Examples

204


17
 
1
// Code your testbench here
2
module tb;
3
bit clk;
4
always #5ns clk++;
5
top_with_if  duti(.*);
6
top_with_st  duts(.*);
7
top_with_wst dutw(.*);
8
initial begin
9
10
  $monitor("duti x:%b y:%b z:%b bus:%b  :::  duts x:%b y:%b z:%b bus:%b  :::  dutw x:%b y:%b z:%b bus:%b",
11
    duti.point.x,duti.point.y,duti.point.z,duti.point.bus,
12
    duts.point.x,duts.point.y,duts.point.z,duts.point.bus,
13
    dutw.point.x,dutw.point.y,dutw.point.z,dutw.point.bus);
14
  #50ns;
15
  $finish(2);
16
end
17
endmodule
xxxxxxxxxx
1
94
 
1
2
function bit [7:0] func(bit [7:0] a,b);
3
  static bit [2:0] base;
4
  bit [7:0] tmp;
5
  foreach(tmp[i]) tmp[i] = b[7-i];
6
  return (a^tmp) + (base++);
7
endfunction
8
9
    typedef struct {logic [7:0] x, y, z, bus; } s_point;
10
11
    module m_x_st (ref s_point point, input clk);
12
      always_ff @(posedge clk)
13
        point.x <= func(point.y, point.z);
14
      //assign point.bus = (point.y!=point.z) ? 'z : point.x; // NO tir-state
15
    endmodule
16
    module m_y_st (ref s_point point, input clk);
17
      always_ff @(posedge clk)
18
        point.y <= func(point.x, point.z);
19
      //assign point.bus = (point.x!=point.z) ? 'z : point.y; // NO tir-state
20
    endmodule
21
    module m_z_st (ref s_point point, input clk);
22
      always_ff @(posedge clk)
23
        point.z <= func(point.x, point.y);
24
      //assign point.bus = (point.x!=point.y) ? 'z : point.z; // NO tir-state
25
    endmodule
26
    
27
    module top_with_st (input clk);
28
        s_point point;
29
        m_x_st mx_inst (point,clk);
30
        m_y_st my_inst (point,clk);
31
        m_z_st mz_inst (point,clk);
32
    endmodule
33
34
    module m_x_wst (inout wire s_point point, input clk);
35
      logic [$size(point.x)-1:0] tmp;
36
      assign point.x = tmp;
37
      always_ff @(posedge clk)
38
        tmp <= func(point.y, point.z);
39
      assign point.bus = (point.y!=point.z) ? 'z : point.x; // tir-state
40
    endmodule
41
    module m_y_wst (inout wire s_point point, input clk);
42
      logic [$size(point.y)-1:0] tmp;
43
      assign point.y = tmp;
44
      always_ff @(posedge clk)
45
        tmp <= func(point.x, point.z);
46
      assign point.bus = (point.x!=point.z) ? 'z : point.y; // tir-state
47
    endmodule
48
    module m_z_wst (inout wire s_point point, input clk);
49
      logic [$size(point.z)-1:0] tmp;
50
      assign point.z = tmp;
51
      always_ff @(posedge clk)
52
        tmp <= func(point.x, point.y);
53
      assign point.bus = (point.x!=point.y) ? 'z : point.z; // tri-state
54
    endmodule
55
    
56
    module top_with_wst (input clk);
57
        wire s_point point; // must have the wire keyword
58
        m_x_wst mx_inst (point,clk);
59
        m_y_wst my_inst (point,clk);
60
        m_z_wst mz_inst (point,clk);
61
    endmodule
62
63
64
    interface if_point;
65
      logic [7:0] x, y, z;
66
      wire  [7:0] bus; // tri-state must be wire
67
    endinterface
68
69
    module m_x_if (if_point point, input clk);
70
      always_ff @(posedge clk)
71
        point.x <= func(point.y, point.z);
72
      assign point.bus = (point.y!=point.z) ? 'z : point.x;
73
    endmodule
74
    module m_y_if (if_point point, input clk);
75
      always_ff @(posedge clk)
76
        point.y <= func(point.x, point.z);
77
      assign point.bus = (point.x!=point.z) ? 'z : point.y;
78
    endmodule
79
    module m_z_if (if_point point, input clk);
80
      always_ff @(posedge clk)
81
        point.z <= func(point.x, point.y);
82
      assign point.bus = (point.x!=point.y) ? 'z : point.z;
83
    endmodule
84
    
85
    module top_with_if (input clk);
86
        if_point point();
87
        m_x_if mx_inst (point,clk);
88
        m_y_if my_inst (point,clk);
89
        m_z_if mz_inst (point,clk);
90
    endmodule
91
92
93
94
575 views and 0 likes     
 
http://stackoverflow.com/questions/21322049/connecting-hierarchical-modules-struct-vs-interface-in-systemverilog
1130:0