将此原理图转换为verilog代码,编译失败

时间:2016-04-06 08:51:23

标签: verilog sequential

Resulting Schematic after synthesis Schematic to Verilog

以下是完整代码。 我想将下面的原理图实现为Verilog代码。只是有点困惑,如果,我可以将组合逻辑和顺序逻辑写成一个总是阻塞或不阻止。 其次,敏感列表需要时钟脉冲和输入变化。这是一个手解决的解决方案,但现在我想把它带入Verilog并在Verilog中实现并查看输出。

module Q4a(
input x,
 input clock,
output z
);
reg z; 
reg y1,y2;
reg d1,d2;
//wire x;
//wire clock;

always @(x,y1,y2)
begin
d1=(~x)&y2;
d2=x;
z=x&y1; 
end

always @(clock)
begin 
//y1<=(~x)&y2;
//y2<=x;
//z<=x&y1;
y1<=d1;
y2<=d2;

end
endmodule

2 个答案:

答案 0 :(得分:2)

x和z在Verilog中有特殊含义,最好使用其他一些变量名称。

module Q4a(
 input x,
 input clock,
 output reg z //Just declare as reg here
);

reg y1,y2;
reg d1,d2;

// Use automatic sensitivity list
always @* begin
  d1=(~x)&y2;
  d2=x;
  z=x&y1; 
end

//Filp-flops use `posedge` to make edge sensitive
always @(posedge clock) begin 
  y1<=d1;
  y2<=d2;
end

endmodule

这在EDA Playground上的vcs中编译。 但要复习我会写为:

module Q4a(
 input      x,
 input      clock,
 output reg z
);

reg y1,y2;

always @* begin
  z = x & y1; 
end

always @(posedge clock) begin 
  y1 <= ~x & y2;
  y2 <= x;
end

endmodule

答案 1 :(得分:1)

总是使用always begin ... end是不必要的,你可以使用直接赋值语句来编写组合电路。

见下面的代码:

module Q4a (
             input wire x,
             input wire clock,
             input wire rst_n,
             output wire z
        );

wire d1;
reg  y1;
reg  y2;

assign d1 = ~x & y2;
assign z  =  x & y1;

always @ (posedge clock or negedge rst_n)
begin
  if(rst_n) begin
    y1 <= 1'b0;
    y2 <= 1'b0;
  end else begin
    y1 <= d1;
    y2 <= x;  // x is d2 too.
  end
end

endmodule

或者你也可以这样做,

assign z  =  x & y1;

always @ (posedge clock or negedge rst_n)
begin
  if(rst_n) begin
    y1 <= 1'b0;
    y2 <= 1'b0;
  end else begin
    y1 <= ~x & y2;
    y2 <= x;  // x is d2 too.
  end
end