FPGA 130例-->例5.16 to 例8.5

用for語句實現2個8位數相乘

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

module mult_for(out,a,b);

    parameter size=8;
    output [2*size:1] out;
    reg [2*size:1] out;
    input [size:1] a,b;
    integer i;
    
    always @ (a or b)
        begin
            out=0;
            for (i=1;i<=size;i=i+1)
                if (b[i]) out=out+(a<<(i-1));
        end

endmodule

用repeat實現8位二進制的乘法

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

module mult_repeat(out,a,b);
    parameter size=8;
    input [size:1] a,b;
    output [2*size:1] out;
    reg [2*size:1] temp_a,out;
    reg [size:1] temp_b;
    
    always @ (a or b)
        begin
            out=0;
            temp_a=a;
            temp_b=b;
            repeat(size)
                begin
                    if(temp_b[1]) out=out+temp_a;
                    temp_a=temp_a<<1;
                    temp_b=temp_b>>1;
                end
        end
endmodule

同一循環的不同實現方式

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

module loop1;
    integer i;
    
    initial
        for (i=0;i<4;i=i+1)
        begin
            $display("i=%h",i);
        end
endmodule


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

module loop2;
    integer i;
    
    initial
        begin
            i=0;
            while(i<4)
                begin
                    $display("i=%h",i);
                    i=i+1;
                end
        end
endmodule

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
module loop3;
    integer i;
    
    initial
        begin
         i=0;
         repeat (4)
            begin
                $display("i=%h",i);
                i=i+1;
            end
        end
endmodule

條件編譯舉例

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

module compile(out,a,b);
    output out;
    input a,b;
    
    `ifdef add
        assign out=a+b;
    `else
        assign out=a-b;
    `endif
endmodule

加法計數器中的進程

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

module count6_1(clk,datain,reset,load,qout,cout);
    input [3:0] datain;
    input clk,reset,load;
    output [3:0] qout;
    reg [3:0] qout;
    output cout;
    
    always @ (clk)
        begin
            if (!reset) qout<=4'd0;
            else if (load) qout<=datain;
            else qout<=qout+4'd1;
        end
        
    assign cout=(qout==4'hf)?1:0;
    
endmodule
任務舉例
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
module alutask(code,a,b,c);
    input [1:0] code;
    input [3:0] a,b;
    output [4:0] c;
    reg [4:0] c;
    
    task my_and;
        input [3:0] a,b;
        output [4:0] out;
        integer i;
        begin
            for(i=3;i<=0;i=i-1)
            out[i]=a[i]^b[i];
        end
    endtask
    
    always @ (code or a or b)
        begin
            case(code)
                2'b00:my_and(a,b,c);
                2'b01:c=a|b;
                2'b10:c=a-b;
                2'b11:c=a+b;
            endcase        //這裏發現了一個錯誤,加了分號,導致modelsim仿真不通過。
        end
    
endmodule

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function [7:0] get0;
input [7:0] x;
reg [7:0] count;
integer i;
begin
    count=0;
    for(i=0;i<8;i=i+1)
    if(x[i]==1'b0) count=count+1;
    get0=count;
end
endfunction

函數

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

module code_83(din,dout);
    input [7:0] din;
    output [2:0] dout;
    
    function [2:0] code;
        input [7:0] din;
        casex(din)
            8'b1xxxxxxx:code=3'h7;
            8'b01xxxxxx:code=3'h6;
            8'b001xxxxx:code=3'h5;
            8'b0001xxxx:code=3'h4;
            8'b00001xxx:code=3'h3;
            8'b000001xx:code=3'h2;
            8'b0000001x:code=3'h1;
            8'b00000001:code=3'h0;
            default:code=3'hx;
        endcase
    endfunction
    
    assign dout=code(din);
endmodule
階乘運算函數
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
module funct(result,clk,n,reset);
    output [31:0] result;
    reg [31:0] result;
    input [3:0] n;
    input clk,reset;
    
    always @ (posedge clk)
        begin
            if(!reset) result=0;
            else result<=2*factorial(n);
        end
        
    function [31:0] factorial;
        input [3:0] opa;
        reg [3:0] i;
        
        begin
            factorial=opa?1:0;
            for(i=2;i<=opa;i=i+1)
            factorial=i*factorial;
        end
        
    endfunction
    
endmodule
順序執行模塊1
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
module serial1(q,a,clk);
    output q,a;
    reg q,a;
    input clk;
    
    always @ (posedge clk)
        begin
            q=~q;
            a=~q;
        end

endmodule

順序執行模塊2

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

module serial2(q,a,clk);
    output q,a;
    reg q,a;
    input clk;
    
    always @ (posedge clk)
        begin
            a=~q;
            q=~q;
        end
endmodule

並行執行模塊1

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

module paral1(q,a,clk);
    output q,a;
    reg q,a;
    input clk;
    
    always @ (posedge clk)
        begin
            q=~q;
        end
    always @ (posedge clk)
        begin
            a=~q;
        end
endmodule

並行執行模塊2

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

module paral2(q,a,clk);
    output q,a;
    reg q,a;
    input clk;
    
    always @ (posedge clk)
        begin
            a=~q;
        end
        
    always @ (posedge clk)
        begin
            q=~q;
        end
endmodule

用case語句描述的4選1 MUX

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

module mux4_1b(out,in1,in2,in3,in4,ctrl1,ctrl2);
    output out;
    reg out;
    input in1,in2,in3,in4,ctrl1,ctrl2;
    
    always @ (in1 or in2 or in3 or in4 or ctrl1 or ctrl2)
        begin
            case({ctrl1,ctrl2})
                2'b00: out=in1;
                2'b01: out=in2;
                2'b10: out=in3;
                2'b11: out=in4;
                default: out=2'bx;
            endcase
        end
endmodule

行爲描述方式實現的4位計數器

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

module count4(out,clr,clk);
    output [3:0] out;
    reg [3:0] out;
    input clr,clk;
    
    always @ (posedge clk)
        begin
            if (!clr) out=0;
            else out=out+1;
        end
endmodule

調用門元件實現的1位全加器

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

module full_add1(a,b,cin,sum,cout);
    input a,b,cin;
    output sum,cout;
    wire s1,m1,m2,m3;
    and (m1,a,b),(m2,b,cin),(m3,a,cin);
    xor (s1,a,b),(sum,s1,cin);
    or (cout,m1,m2,m3);
endmodule

數據流描述的1位全加器

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

module full_add2(a,b,cin,sum,cout);
    input a,b,cin;
    output sum,cout;
    assign sum=a^b^cin;
    assign cout=(a&b)|(b&cin)|(cin&a);
endmodule

1位全加器

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

module full_add3(a,b,cin,sum,cout);
    input a,b,cin;
    output sum,cout;
    assign {cout,sum}=a+b+cin;
endmodule

$time與$realtime的區別

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

`timescale 10ns/1ns
module time_dif;
    reg ts;
    parameter delay=2.6;
    initial
        begin
            #delay ts=1;
            #delay ts=0;
            #delay ts=1;
            #delay ts=0;
        end
    initial $monitor($time,,,"ts=%b",ts);
endmodule

$random函數的使用

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

`timescale 1ns/1ns
module random_tp;
  integer data;
  integer i;
  parameter delay=10;
 
  initial $monitor($time,,,"data=%b",data);
 
  initial
    begin
      for(i=0;i<=10;i=i+1)
      #delay data=$random;
    end
endmodule

1位全加器進位輸出UDP元件

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

primitive carry_udp(cout,cin,a,b);
    output cout;
    input cin,a,b;
    
    table
    //cin a b : cout
        0 0 0:0;
        0 1 0:0;
        0 0 1:0;
        0 1 1:1;
        1 0 0:0;
        1 0 1:1;
        1 1 0:1;
        1 1 1:1;
    endtable
        
endprimitive

用簡縮符?表述的1位全加器進位輸出UDP元件

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

primitive carry_udpx2(cout,cin,a,b);
    output cout;
    input cin,a,b;
    table
        //cin a b:cout
        ? 0 0:0;
        0 ? 0:0;
        0 0 ?:0;
        ? 1 1:1;
        1 ? 1:1;
        1 1 ?:1;
    endtable
endprimitive
發佈了30 篇原創文章 · 獲贊 39 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章