module A(input wire reset, input wire clock, output reg a_reg_o);
always @(posedge clock)
begin
if(reset == 1'h1)
begin
a_reg_o = 1'h0;
end
else
begin
a_reg_o = 1'h1;
end
end
endmodule
module B(input wire reset, input wire clock, input a_i);
reg b;
always @(posedge clock)
begin
if(reset == 1'h1)
begin
b = 1'h0;
end
else
begin
if(a_i == 1'h1)
begin
b = 1'h1;
end
else
begin
b = 1'h0;
end
end
end
endmodule
module Main(input wire reset, input wire clock);
wire a_o;
A a(reset, clock, a_o);
B b(reset, clock, a_o);
endmodule
Code Verilog - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 module A(input wire reset, input wire clock, output reg a_reg_o); always @(posedge clock) begin if(reset == 1'h1) begin a_reg_o = 1'h0; end else begin a_reg_o = 1'h1; end end endmodule
Code Verilog - [expand] 1 2 3 4 5 6 module A(input wire reset, input wire clock, output reg a_reg_o); always @(posedge clock) begin a_reg_o <= (reset == 1'h1) ? 1'h0 : 1'h1; end endmodule
Code Verilog - [expand] 1 2 3 4 5 6 7 8 9 module B(input wire reset, input wire clock, input a_i); reg b; always @(posedge clock) begin b <= reset ? 1'h0 : a_i ? 1'h1 : 1'h0; end endmodule
Code Verilog - [expand] 1 2 3 4 5 6 7 8 9 10 11 module B(input wire reset, input wire clock, input a_i); reg b; always @(posedge clock) begin if (reset) b <= 0; else b <= a_i ? 1'h1 : 1'h0; end endmodule
The first rule should really be: use '<=' in edge triggered always blocks '=' should be used exclusively in combinational always blocks.Just follow these two rules and you will avoid 99% of race conditions. You still may faces some race conditions when you make combinational logic, but then it is usually a fail of programming logic than a fail in the code.
- Just use the "<=" inside always blocks instead "=" as vGoodtimes suggested.
- Additionally, I strongly avoid to use always to non-clocked signals (ex: "always @(reset)") . It generates combinational logic which can be almost always be achieved by other ways.
Magic description of a FF!? Where did you come up with that? From the LRM....pbernardi said:Going out of help scope you have asked, I personally also always try to use the ? : operands instead if-else whenever I can. The ? : operands fits very well into hardware, its like a magic description of a flip-flop, and save lot of lines into your code.
Syntax 11-2—Conditional operator syntax (excerpt from Annex A)Code:conditional_expression ::= // from A.8.3 cond_predicate ? { attribute_instance } expression : expression cond_predicate ::= // from A.6.6 expression_or_cond_pattern { &&& expression_or_cond_pattern } expression_or_cond_pattern ::= expression | cond_pattern cond_pattern ::= expression matches pattern
This subclause describes the traditional notation where cond_predicate is just a single expression.
SystemVerilog also allows cond_predicate to perform pattern matching, which is described in 12.6.
If cond_predicate is true, the operator returns the value of the first expression without evaluating the second
expression; if false, it returns the value of the second expression without evaluating the first expression. If
cond_predicate evaluates to an ambiguous value (x or z), then both the first expression and the second
expression shall be evaluated, and compared for logical equivalence as described in 11.4.5. If that
comparison is true (1), the operator shall return either the first or second expression. Otherwise the operator
returns a result based on the data types of the expressions.
This isn't a magic FF, it's a 2-to1 multiplex.pbernardi said:This would led to a "a_reg_out" flip-flop that chose between 0 and 1, depending of the "reset" input .
What!? What tool would use two flip-flops to implement the reformatted code shown above? It is ONE FF called a_reg_o that has a synchronous reset. What makes you think it will use two FFs!?pbernardi said:But, inside the FPGAs, generally there is a dedicated pin to reset! This mean, you don't need to use additional logic to use the reset. For example, The module "B" as you wrote probably uses two flip-flops. Rewriting it to use ? : operands:Code:always @(posedge clock) begin if (reset) begin a_reg_o = 1'h0; end else begin a_reg_o = 1'h1; end end
What!? I think you need to READ the LRM. There isn't a cascaded of FFs, what you've done is find an obscure way of writing my clear easy to see reformatted code above using two conditionals (i.e 2-to-1 multiplexers)pbernardi said:
Code Verilog - [expand] 1 2 3 4 5 6 7 8 9 module B(input wire reset, input wire clock, input a_i); reg b; always @(posedge clock) begin b <= reset ? 1'h0 : a_i ? 1'h1 : 1'h0; end endmodule
You look at the code and you see: it uses cascated flip-flops (for reset and a_i). Your software can optmize it to use the reset or not. But if you write:
No, this is functionally identical to all the other versions you've written...A synchronous resetable D FF.pbernardi said:
Code Verilog - [expand] 1 2 3 4 5 6 7 8 9 10 11 module B(input wire reset, input wire clock, input a_i); reg b; always @(posedge clock) begin if (reset) b <= 0; else b <= a_i ? 1'h1 : 1'h0; end endmodule
You will use one flip-flop with reset.
I have no clue what you are trying to imply here, though I suspect you have a misunderstanding of the fundamentals.pbernardi said:EDIT: Note that if you want to use the flip-flop reset, the output must be always "0". For example, if you would like the reset to put "1" you your output, this would lead to use of 2 flip-flops, because flip-flop reset cannot be used in this case.
Code Verilog - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 // for those that like really concise code: always @ (posedge clk) if (reset) b <= 1'b0; b <= a; // somewhat more verbose but preferred by me: always @ (posedge clk) begin if (reset) begin b <= 1'b0; end else begin b <= a; end end
The first rule should really be: use '<=' in edge triggered always blocks '=' should be used exclusively in combinational always blocks.
The second rule I don't consider a rule, it's more of a suggestion to not use combinational logic, which I consider more of a coding style issue.
Magic description of a FF!? Where did you come up with that? From the LRM....
This isn't a magic FF, it's a 2-to1 multiplex.
What!? What tool would use two flip-flops to implement the reformatted code shown above? It is ONE FF called a_reg_o that has a synchronous reset. What makes you think it will use two FFs!?
What!? I think you need to READ the LRM. There isn't a cascaded of FFs, what you've done is find an obscure way of writing my clear easy to see reformatted code above using two conditionals (i.e 2-to-1 multiplexers)
Code Verilog - [expand] 1 2 3 4 5 6 7 8 9 if (IMMDP2 || MIDX) PMYP2 <= 1'b0; else PMYP2 <= IDATA[27] && !MIDX ? IDATA[21] : INST == `OP5 ? IDATA[21] : IDATA[30] && OP ? IDATA[21] : CFG ? IDATA[21] : INST == `RTT1 ? IDATA[16] : INST == `RTT3 ? IDATA[7] : 1'b0;
No, this is functionally identical to all the other versions you've written...A synchronous resetable D FF.
always @(posedge clock) begin
if (reset) begin
a_reg_o = 1'h0;
end else begin
a_reg_o = 1'h1;
end
end
I have no clue what you are trying to imply here, though I suspect you have a misunderstanding of the fundamentals.
FYI this is how you should code a D-FF not the ugly way you've describe it.
Code Verilog - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 // for those that like really concise code: always @ (posedge clk) if (reset) b <= 1'b0; b <= a; // somewhat more verbose but preferred by me: always @ (posedge clk) begin if (reset) begin b <= 1'b0; end else begin b <= a; end end
No confusing conditional expressions assigning constants.
Code Verilog - [expand] 1 2 3 4 5 6 7 8 9 reg [7:0] a,b=0; always @ (posedge clk) begin if (reset) b <= 8'h0; end else b <= (a == 8'h55) ? 8'h11 : 8'h22; end
Code Verilog - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 reg [7:0] a,b=0; always @ (posedge clk) begin if (reset) begin b <= 8'h0; end else begin if (a==8'h55) begin b <= 8'h11; end else begin b <= 8'h22; end end
@pbernardi
But what about some if/else with multiple lines of code ? I think you admit that using different styles of coding is not good ? However I agree that ? : operand would use much less lines of code.
Code Verilog - [expand] 1 2 3 4 5 b <= condition 1 ? result 1 : condition 2 ? result 2 : condition 3 ? result 3 : ... condition n ? result n : result n+1;
Code Verilog - [expand] 1 2 3 b <= condition 1 ? condition 2 ? result 2 : condition 3 ? result 3 : condition 4 ? result 4 : result 5 : result 6;
Code Verilog - [expand] 1 2 3 b <= (condition 1 ? (condition 2 ? result 2 : (condition 3 ? result 3 : (condition 4 ? result 4 : result 5))) : result 6);
Well, as you can see, note that some people does not like this style of coding. But I see this very well-fitted to FPGA design.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?