Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Conditional operator & case

fragnen

Full Member level 2
Joined
Apr 3, 2019
Messages
147
Helped
0
Reputation
0
Reaction score
1
Trophy points
18
Activity points
1,057
Can below conditional operator be used inside sequential always block?
c ? a: b;

Can case statements be used inside sequential always blocks?
 

danadakk

Advanced Member level 4
Joined
Mar 26, 2018
Messages
1,281
Helped
232
Reputation
478
Reaction score
279
Trophy points
83
Activity points
5,912
Yes on case inside always -



Regards, Dana.
--- Updated ---

Can below conditional operator be used inside sequential always block?
c ? a: b;

Can case statements be used inside sequential always blocks?

Try a test case on the operator question.....

Regards, Dana.
 
Last edited:

fragnen

Full Member level 2
Joined
Apr 3, 2019
Messages
147
Helped
0
Reputation
0
Reaction score
1
Trophy points
18
Activity points
1,057
danadakk

Your link is showing case statement inside combinational always block. The post#1 was for case statement inside sequential always block. Also looking for use of conditional operator pr case statement inside sequential always bloc for synthesizable codes.
 

FvM

Super Moderator
Staff member
Joined
Jan 22, 2008
Messages
48,945
Helped
14,340
Reputation
28,944
Reaction score
13,084
Trophy points
1,393
Location
Bochum, Germany
Activity points
282,393
Why don't you simply review the Verilog language reference manual or a text book. You'll see that there are no restrictions regarding usage of case statement and conditional operator.

According to Verilog nomenclature, all always blocks, also combinational always(*) blocks are considered sequential blocks.
 
Last edited:

dave_59

Advanced Member level 3
Joined
Dec 15, 2011
Messages
821
Helped
363
Reputation
730
Reaction score
357
Trophy points
1,353
Location
Fremont, CA, USA
Activity points
6,721
The always construct is an instance of a procedural context. Procedural code may contain compound statements enclosed in an begin/end block, in which case those statements execute sequentially. This is in contrast to a fork/join block which execute those statements concurrently. Whether that always construct contains a singe statement or compound statements executing sequentially is independent of the construct representing sequential or combinational hardware.

You typically see the conditional operator ?: used outside of a procedural context, line a continuous assign statement in place of a the procedural if/else statement, but there nothing preventing you from using it wherever an expression is allowed.
 

fragnen

Full Member level 2
Joined
Apr 3, 2019
Messages
147
Helped
0
Reputation
0
Reaction score
1
Trophy points
18
Activity points
1,057
FvM/Dave_59

By sequential always block those always blocks which is triggered on either posedge or negedge of a variable like clock or reset, are being referred in the query at post#1. Can you please now reply to the queries at post#1? Can you please also state whether such a conditional operator or case statement inside an always block which is triggered either by posedge or negedge of a variable like clock or reset, are synthesizable or not?

Regards
 

FvM

Super Moderator
Staff member
Joined
Jan 22, 2008
Messages
48,945
Helped
14,340
Reputation
28,944
Reaction score
13,084
Trophy points
1,393
Location
Bochum, Germany
Activity points
282,393
If you understand how an always block works, you won't ask. The assignments in the case statement or an assignment with conditional operator behave just the same as simple assignments, which are "triggered" by the respective clock or - in hardware view - infer a register. Case or conditional operator creates different combinational logic in front of the register.
 

fragnen

Full Member level 2
Joined
Apr 3, 2019
Messages
147
Helped
0
Reputation
0
Reaction score
1
Trophy points
18
Activity points
1,057
Will there be cons if a flipflop is coded as rtl in the following way instead of using traditional if-else inside the same always block ?

always @(posedge clock or negedge rst)
(!rst) ? (q<=0): (q<=d);
 

ThisIsNotSam

Advanced Member level 5
Joined
Apr 6, 2016
Messages
2,181
Helped
382
Reputation
764
Reaction score
396
Trophy points
83
Activity points
11,148
Will there be cons if a flipflop is coded as rtl in the following way instead of using traditional if-else inside the same always block ?

always @(posedge clock or negedge rst)
(!rst) ? (q<=0): (q<=d);
yes, you will lose your job for complete inability to write logic like a human. fired for cause on the spot.

in a more serious tone, this style goes against typical coding styles. synthesis tools might complain or might even misinterpret the intent (unlikely). in short, there is no good reason why anyone would ever write this freakshow of a code.
 

fragnen

Full Member level 2
Joined
Apr 3, 2019
Messages
147
Helped
0
Reputation
0
Reaction score
1
Trophy points
18
Activity points
1,057
yes, you will lose your job for complete inability to write logic like a human. fired for cause on the spot.

in a more serious tone, this style goes against typical coding styles. synthesis tools might complain or might even misinterpret the intent (unlikely). in short, there is no good reason why anyone would ever write this freakshow of a code.
Let's wait on what FvM and dave_59 says on this.
 

FvM

Super Moderator
Staff member
Joined
Jan 22, 2008
Messages
48,945
Helped
14,340
Reputation
28,944
Reaction score
13,084
Trophy points
1,393
Location
Bochum, Germany
Activity points
282,393
1. Please correct syntax errors
Code:
always @(posedge clock or negedge rst)
q<= !rst ? 0 : d;

2. Similar to ThisIsNotSam, I'd prefer readable code. With this single line FF description, you'd start considerations, if it complies to the register code template.

3. At least Quartus recognizes the register description

1632398766487.png
 

fragnen

Full Member level 2
Joined
Apr 3, 2019
Messages
147
Helped
0
Reputation
0
Reaction score
1
Trophy points
18
Activity points
1,057
Why will not the below code work? What Verilog LRM violation is there that the below code will not synthesize to a D-Flipflop?

always @(posedge clock or negedge rst)
(!rst) ? (q<=0): (q<=d);
 

ThisIsNotSam

Advanced Member level 5
Joined
Apr 6, 2016
Messages
2,181
Helped
382
Reputation
764
Reaction score
396
Trophy points
83
Activity points
11,148
Why will not the below code work? What Verilog LRM violation is there that the below code will not synthesize to a D-Flipflop?

always @(posedge clock or negedge rst)
(!rst) ? (q<=0): (q<=d);
even if it was somehow LRM-compliant, it doesn't mean much. synthesizable RTL is a subset of the Verilog language. not everything that simulates becomes (useful) hardware.
 

fragnen

Full Member level 2
Joined
Apr 3, 2019
Messages
147
Helped
0
Reputation
0
Reaction score
1
Trophy points
18
Activity points
1,057
It's a conditional expression without an assignment (no LHS). Not valid statement.
Sorry, it was missed. You are right.
--- Updated ---

ThisIsNotSam

What do you think on the FvM's code of flipflop as below?
always @(posedge clock or negedge rst)
q<= !rst ? 0 : d;
 

fragnen

Full Member level 2
Joined
Apr 3, 2019
Messages
147
Helped
0
Reputation
0
Reaction score
1
Trophy points
18
Activity points
1,057
What will be the pros and cons of using this below two codes for coding a synthesizable rtl for a flipflop with enable?

always @(posedge clock or negedge rst)
q<= !rst ? 0 : (enable? d : q);

Versus

always@(posedge clock or negedge rst)
if(!rst)
q<= 1'b0;
else if (enable)
q<=d;
 

ThisIsNotSam

Advanced Member level 5
Joined
Apr 6, 2016
Messages
2,181
Helped
382
Reputation
764
Reaction score
396
Trophy points
83
Activity points
11,148
pros of style 1: none
cons of style 1: see post #10

pros of style 2: you don't get bullied by your colleagues during code review
cons of style 2: none
 

LaTeX Commands Quick-Menu:

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top