HDLbits答案更新系列3(2 Verilog Language 2.4 Procedures)

目录

前言

2.4 Procedure

2.4.1 Always blocks (combination)(Alwaysblock1)

2.4.2 Always blocks(clocked)(Alwaysblock2)

2.4.3 If statement(Always if)

2.4.4 If statement latches(Always if2)

2.4.5 Case statement(Always case)

2.4.6 Priority encoder(Always case2)

2.4.7 Priority encoder with casez(Always casez)

2.4.8 Avoiding latches(Always nolatches)

结语

HDLbits网站链接


前言

今天继续更新一个小节的内容。

2.4 Procedure

2.4.1 Always blocks (combination)(Alwaysblock1)

// synthesis verilog_input_version verilog_2001
module top_module(
    input a, 
    input b,
    output wire out_assign,
    output reg out_alwaysblock
);
    
    assign out_assign = a & b;
    
    always@(*)begin
        out_alwaysblock = a & b;
    end

endmodule

2.4.2 Always blocks(clocked)(Alwaysblock2)

// synthesis verilog_input_version verilog_2001
module top_module(
    input clk,
    input a,
    input b,
    output wire out_assign,
    output reg out_always_comb,
    output reg out_always_ff   );
    
    assign out_assign = a ^ b;
    
    always@(*)begin
        out_always_comb = a ^ b;
    end
    
    always@(posedge clk)begin
        out_always_ff <= a ^ b;
    end

endmodule

2.4.3 If statement(Always if)

// synthesis verilog_input_version verilog_2001
module top_module(
    input a,
    input b,
    input sel_b1,
    input sel_b2,
    output wire out_assign,
    output reg out_always   ); 
    
    assign out_assign = (sel_b1 & sel_b2) ? b : a;
        
    always@(*)begin
        if(sel_b1 & sel_b2)begin
            out_always = b;
        end
        else begin
            out_always = a;
        end
    end
    
    //second way
    /*
    always@(*)begin
        case({sel_b1, sel_b2})
            0:begin
                out_always = a;
            end
            1:begin
                out_always = a;
            end
            2:begin
                out_always = a;
            end
            3:begin
                out_always = b;
            end
        endcase
    end
    */            

endmodule

2.4.4 If statement latches(Always if2)

// synthesis verilog_input_version verilog_2001
module top_module (
    input      cpu_overheated,
    output reg shut_off_computer,
    input      arrived,
    input      gas_tank_empty,
    output reg keep_driving  ); //

    always @(*) begin
        if (cpu_overheated)
            shut_off_computer = 1;
        else
            shut_off_computer = 0;
    end

    always @(*) begin
        if (~arrived)
            keep_driving = ~gas_tank_empty;
        else
            keep_driving = 0;
    end

endmodule

2.4.5 Case statement(Always case)

// synthesis verilog_input_version verilog_2001
module top_module ( 
    input [2:0] sel, 
    input [3:0] data0,
    input [3:0] data1,
    input [3:0] data2,
    input [3:0] data3,
    input [3:0] data4,
    input [3:0] data5,
    output reg [3:0] out   );//

    always@(*) begin  // This is a combinational circuit
        case(sel)
            0:begin
                out = data0;
            end
            1:begin
                out = data1;
            end
            2:begin
                out = data2;
            end
            3:begin
                out = data3;
            end
            4:begin
                out = data4;
            end
            5:begin
                out = data5;
            end
            default:begin
                out = 4'd0;
            end
        endcase
    end

endmodule

2.4.6 Priority encoder(Always case2)

// synthesis verilog_input_version verilog_2001
module top_module (
    input [3:0] in,
    output reg [1:0] pos  );
    
    always@(*)begin
        casez(in)
            4'bzzz1:begin
                pos = 2'd0;
            end
            4'bzz10:begin
                pos = 2'd1;
            end
            4'bz100:begin
                pos = 2'd2;
            end
            4'b1000:begin
                pos = 2'd3;
            end
            default:begin
                pos = 2'd0;
            end
        endcase
    end            

endmodule

这道题博主为了方便,直接使用了casez。 

2.4.7 Priority encoder with casez(Always casez)

// synthesis verilog_input_version verilog_2001
module top_module (
    input [7:0] in,
    output reg [2:0] pos  );
    
    always@(*)begin
        casez(in)
            8'bzzzzzzz1:begin
                pos = 3'd0;
            end
            8'bzzzzzz10:begin
                pos = 3'd1;
            end
            8'bzzzzz100:begin
                pos = 3'd2;
            end
            8'bzzzz1000:begin
                pos = 3'd3;
            end
            8'bzzz10000:begin
                pos = 3'd4;
            end
            8'bzz100000:begin
                pos = 3'd5;
            end
            8'bz1000000:begin
                pos = 3'd6;
            end
            8'b10000000:begin
                pos = 3'd7;
            end
            default:begin
                pos = 3'd0;
            end
        endcase
    end
            
endmodule

2.4.8 Avoiding latches(Always nolatches)

// synthesis verilog_input_version verilog_2001
module top_module (
    input [15:0] scancode,
    output reg left,
    output reg down,
    output reg right,
    output reg up  ); 
    
    always@(*)begin
        left = 1'b0;
        down = 1'b0;
        right = 1'b0;
        up = 1'b0;
        case(scancode)
            16'he06b:begin
                left = 1'b1;
            end
            16'he072:begin
                down = 1'b1;
            end
            16'he074:begin
                right = 1'b1;
            end
            16'he075:begin
                up = 1'b1;
            end
        endcase
    end

endmodule

组合逻辑条件没有补全会产生latch,if...else的else分支没补全可能会产生latch,case没补全可能会产生,但不是一定会产生,博主的这种答案就不会产生latch,因为在case之前就先给变量赋了值,大家要注意这个问题。 

结语

今天先更新一个小节内容吧,明天继续更新~

HDLbits网站链接

https://hdlbits.01xz.net/wiki/Main_Page

你可能感兴趣的:(HDLbits答案更新系列3(2 Verilog Language 2.4 Procedures))