2014年11月22日 星期六

Verilog ----基礎1

                                      
【例3.14 位全加器 
module adder4(cout,sum,ina,inb,cin); 
output [3:0] sum; 
output cout; 
input [3:0] ina,inb; 
input cin; 
assign {cout,sum}=ina+inb+cin; 
endmodule 

【例3.24 位計數器 
module count4(out,reset,clk); 
output [3:0] out; 
input reset,clk; 
reg [3:0] out; 
always @(posedge clk) 
      begin 
      if  (reset)  out<=0;           //同步復位 
      else           out<=out+1;         //計數 
     end 
endmodule 
  
【例3.3 4 位全加器的仿真程式 
`timescale 1ns/1ns 
`include "adder4.v" 
module adder_tp;              //測試模組的名字 
reg [3:0] a,b;               //測試輸入信號定義為reg  
reg cin; 
wire [3:0] sum;               //測試輸出信號定義為wire  
wire cout; 
integer i,j; 
adder4 adder(sum,cout,a,b,cin);  //調用測試物件 
always #5 cin=~cin;             //設定cin 的取值 
initial 
begin 
a=0;b=0;cin=0; 
for (i=1;i<16;i=i+1) 
#10   a=i;                      //設定a 的取值 
end 

 
程式文本 
    initial 
    begin 
    for (j=1;j<16;j=j+1) 
    #10   b=j;                         //設定b 的取值 
    end 
    
    initial                          //定義結果顯示格式 
    begin 
    $monitor($time,,,"%d + %d + %b={%b,%d}",a,b,cin,cout,sum); 
    #160  $finish; 
    end 
    endmodule 
      
【例3.4 4 位元計數器的仿真程式 
    `timescale 1ns/1ns 
    `include "count4.v" 
    module coun4_tp; 
    reg clk,reset;                     //測試輸入信號定義為reg  
    wire [3:0] out;                    //測試輸出信號定義為wire  
    parameter DELY=100; 
    count4 mycount(out,reset,clk);        //調用測試物件 
    always #(DELY/2) clk = ~clk;        //產生時鐘波形 
    initial 
      begin                              //激勵信號定義 
                   clk =0; reset=0; 
        #DELY     reset=1; 
        #DELY     reset=0; 
        #(DELY*20) $finish; 
      end 

    //定義結果顯示格式 
    initial $monitor($time,,,"clk=%d reset=%d out=%d", clk, reset,out); 
    endmodule 
      
     
【例3.5 AND-OR-NOT “與--非”門電路 
 module AOI(A,B,C,D,F);         //模組名為AOI(埠列表ABCDF) 
     input A,B,C,D;             //模組的輸入埠為ABC
     output F;                  //模組的輸出埠為
 wire A,B,C,D,F;            //定義信號的資料類型 
     assign  F= ~((A&B)|(C&D));   //邏輯功能描述 

 endmodule  

沒有留言:

張貼留言

Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --3

  Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --3 AngularJS 實例 <!DOCTYPE html> <html> <head> <meta charse...