Results 1 to 8 of 8

Thread: newbie question: synchronous versus combinational bus mux using case statement

  1. #1
    Join Date
    Mar 2018
    Posts
    7
    Rep Power
    1

    Default newbie question: synchronous versus combinational bus mux using case statement

    Hello all-

    Can anyone explain why the first (synchronous) version below compiles in quartus, but the second (combinational) does not? In the 2nd case the error is:

    Error (10137): Verilog HDL Procedural Assignment error at my_first_fpga.v(26): object "LED" on left-hand side of assignment must have a variable data type
    Error (10137): Verilog HDL Procedural Assignment error at my_first_fpga.v(27): object "LED" on left-hand side of assignment must have a variable data type

    Is there a proper way to code a combinational bus mux using a case statement?

    Thanks
    -J

    Code:
    module my_first_fpga    ( input     wire            CLOCK_50,
                              input     wire    [1:0]    KEY,
                              output     reg        [3:0]    LED
                            );
                            
        reg        [31:0]    count;
               
        always @ (posedge CLOCK_50)
        begin    
            count <= count + 1;
            case(KEY[0])
            0    :    LED = count[24:21];
            1    :    LED = count[26:23];
            endcase
        end  
    endmodule
    Code:
    module my_first_fpga    ( input     wire            CLOCK_50,
                              input     wire    [1:0]    KEY,
                              output     wire    [3:0]    LED
                            );
                            
        reg        [31:0]    count;
        
        always @ (posedge CLOCK_50)
        begin    
            count <= count + 1;
        end
        
        always @ (KEY[0], count[26:21])
        case(KEY[0])
        0    :    LED = count[24:21];
        1    :    LED = count[26:23];
        endcase
        
    endmodule

  2. #2
    Tricky is offline Moderator **Forum Master**
    Join Date
    Oct 2008
    Posts
    6,031
    Rep Power
    1

    Default Re: newbie question: synchronous versus combinational bus mux using case statement

    make LED a reg in 2nd code.
    A reg is not automatically a register, it is just a data type. A wire can only be used in an assign statement. reg types can be used in procedural code.

  3. #3
    Join Date
    Dec 2007
    Location
    Bochum Germany
    Posts
    6,319
    Rep Power
    1

    Default Re: newbie question: synchronous versus combinational bus mux using case statement

    Or set Verilog HDL input to System Verilog, it will accept wire as assignment target.

  4. #4
    Join Date
    Mar 2018
    Posts
    7
    Rep Power
    1

    Default Re: newbie question: synchronous versus combinational bus mux using case statement

    Quote Originally Posted by Tricky View Post
    make LED a reg in 2nd code.
    A reg is not automatically a register, it is just a data type. A wire can only be used in an assign statement. reg types can be used in procedural code.
    Hi, thanks for the reply.

    So, it sounds like you are suggesting that a reg type can be used to generate combinational logic... say by using it outside of an always @ (posedge) block. I hadn't thought of this, but I'll give it a try and see if it works.

    -J

  5. #5
    Tricky is offline Moderator **Forum Master**
    Join Date
    Oct 2008
    Posts
    6,031
    Rep Power
    1

    Default Re: newbie question: synchronous versus combinational bus mux using case statement

    This is a quirk of Verilog. Reg is just a data type in the Verilog. What it maps to in logic is down to the behaviour in the code. It is just unfortunate they chose the name "reg"

  6. #6
    Join Date
    Mar 2018
    Posts
    7
    Rep Power
    1

    Default Re: newbie question: synchronous versus combinational bus mux using case statement

    Quote Originally Posted by Tricky View Post
    This is a quirk of Verilog. Reg is just a data type in the Verilog. What it maps to in logic is down to the behavior in the code. It is just unfortunate they chose the name "reg"
    Yes, indeed, sir. I found both the code snippets below generate the desired combinational logic. I would guess the 2nd one might be frowned upon.

    Would you be able to recommend a book that covers these sorts of issues? It would be nice to find one that is written in Verilog 2001.

    Thanks again,
    -J
    Code:
        always @ (KEY[0],count[26:21])
        case(KEY[0])
        0    :    LED = count[24:21];
        1    :    LED = count[26:23];
        endcase
    Code:
        always @ (KEY[0])
        case(KEY[0])
        0    :    LED = count[24:21];
        1    :    LED = count[26:23];
        endcase

  7. #7
    Tricky is offline Moderator **Forum Master**
    Join Date
    Oct 2008
    Posts
    6,031
    Rep Power
    1

    Default Re: newbie question: synchronous versus combinational bus mux using case statement

    The second code wont match between simulation and synthesis as you dont have count in the sensitivity list.
    Also, I recommend using non-blocking assignments in always blocks <=

  8. #8
    Join Date
    Oct 2008
    Location
    Silicon Valley, USA
    Posts
    332
    Rep Power
    1

    Default Re: newbie question: synchronous versus combinational bus mux using case statement

    Quote Originally Posted by Tricky View Post
    The second code wont match between simulation and synthesis as you dont have count in the sensitivity list.
    Also, I recommend using non-blocking assignments in always blocks <=
    The first statement I agree with. Not so the second; it depends on what you are trying to accomplish.

    If you are trying to model physical registers, than using the non-blocking assignment <= makes sense, as it evaluates the right side, and then assigns the result to the left side based on the event used in the enclosing always block (typically 'posedge clk').

    If you want to model pure combinatorial logic and use 'reg' variables as intermediate holding values, with no intercycle memory, then blocking = assignment makes more sense, as it works within the procedural flow. Example:

    Code:
    begin : label
          reg tmp;
          tmp = a;
          if (b == c) tmp = d;
          if (e == f) tmp = g;
          h = tmp;
    end
    produces the same result as:

    Code:
    h = (e == f) ? g : ((b == c) ? d : a);
    Of course this is a very contrived example but sometimes it is more clear to write out the combinatorial logic as a multi line procedure rather than to combine the whole thing into a single equation.

Similar Threads

  1. In verilog when the default case in a case statement is ignored by synthesis
    By sstrode in forum Quartus II and EDA Tools Discussion
    Replies: 6
    Last Post: March 27th, 2015, 05:39 AM
  2. verilog case statement
    By taimoortareen in forum General Altera Discussion
    Replies: 2
    Last Post: February 23rd, 2014, 04:19 AM
  3. Nest if statement in case statement
    By deded84 in forum General Altera Discussion
    Replies: 5
    Last Post: April 4th, 2013, 03:35 AM
  4. Case don't care statement won't work
    By alegomaster in forum Quartus II and EDA Tools Discussion
    Replies: 6
    Last Post: June 28th, 2012, 03:04 PM
  5. stronge behavior of case statement
    By zhangyi17 in forum Quartus II and EDA Tools Discussion
    Replies: 5
    Last Post: December 1st, 2008, 10:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •