Verilog---Procedures

这篇文章详细介绍了Verilog语言中Alwaysblocks的不同用法,包括无时钟的组合逻辑、带时钟的同步组合及异步组合、if语句、latch的使用、case和priorityencoder结构,以及如何避免不必要的latches。
摘要由CSDN通过智能技术生成

1.Always blocks(combinational)

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.Always blocks(clocked)

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)
        
        out_always_ff<=a^b;
        

endmodule

3.if statement

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_b2&sel_b1)
                begin
                out_always = b;
                end
            else 
                begin
                  out_always = a;
                end
        end
    
    
    

endmodule

4.if statement latches

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

5.Case statement

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)
            3'b000: out=data0;
            3'b001: out=data1;
            3'b010: out=data2;
            3'b011: out=data3;
            3'b100: out=data4;
            3'b101: out=data5;
            default: out=3'b000;
        endcase
    end

endmodule

6.Priority encoder

module top_module (
    input [3:0] in,
    output reg [1:0] pos  );
   always@(*)
        begin
    case(in)
        4'b0000: pos=3'd0;
        4'b0001: pos=3'd0;
        4'b0010: pos=3'd1;
        4'b0011: pos=3'd0;
        4'b0100: pos=3'd2;
        4'b0101: pos=3'd0;
        4'b0110: pos=3'd1;
        4'b0111: pos=3'd0;
        4'b1000: pos=3'd3;
        4'b1001: pos=3'd0;
        4'b1010: pos=3'd1;
        4'b1011: pos=3'd0;
        4'b1100: pos=3'd2;
        4'b1101: pos=3'd0;
        4'b1110: pos=3'd1;
        4'b1111: pos=3'd0;
        default: pos=3'd0;
    endcase
            end
    

endmodule

7.Priority encoder with casez

module top_module (
    input [7:0] in,
    output reg [2:0] pos );
    
    always@(*)
        begin
            casez(in[7:0])
               // 8'bzzzz_zzzz: pos=3'd0;
                8'bzzzz_zzz1: pos=3'd0;
                8'bzzzz_zz1z: pos=3'd1;
                8'bzzzz_z1zz: pos=3'd2;
                8'bzzzz_1zzz: pos=3'd3;
                8'bzzz1_zzzz: pos=3'd4;
                8'bzz1z_zzzz: pos=3'd5;
                8'bz1zz_zzzz: pos=3'd6;
                8'b1zzz_zzzz: pos=3'd7;
                default: pos=3'd0;
            endcase
        end

endmodule

8.Avoiding latches

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

endmodule

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值