2014年6月7日 星期六

Verilog ----基礎4

Verilog ----基礎4

          【例7.1】調用門元件實現的4 1 MUX 
module mux4_1a(out,in1,in2,in3,in4,cntrl1,cntrl2); 
 output out; 
 input in1,in2,in3,in4,cntrl1,cntrl2; 
 wire notcntrl1,notcntrl2,w,x,y,z; 
 not  (notcntrl1,cntrl2),  (notcntrl2,cntrl2); 
and  (w,in1,notcntrl1,notcntrl2), 
       (x,in2,notcntrl1,cntrl2), 
       (y,in3,cntrl1,notcntrl2), 
       (z,in4,cntrl1,cntrl2); 
 or   (out,w,x,y,z); 
 endmodule 

  
  【例7.2 】用case 語句描述的4 1 MUX 
 module mux4_1b(out,in1,in2,in3,in4,cntrl1,cntrl2); 
 output out; 
 input in1,in2,in3,in4,cntrl1,cntrl2; 
 reg out; 
 always@(in1 or in2 or in3 or in4 or cntrl1 or cntrl2) 
     case ({cntrl1,cntrl2}) 
          2'b00:out=in1; 
          2'b01:out=in2; 
          2'b10:out=in3; 
          2'b11:out=in4; 
          default:out=2'bx; 
     endcase 
 endmodule 

  
  【例7.3 】行為描述方式實現的4 位元計數器 
 module count4(clk,clr,out); 
 input clk,clr; 
 output [3:0] out; 
 reg [3:0] out; 
 always @(posedge clk or posedge clr) 
      begin 
      if (clr)    out<=0; 
      else            out<=out+1; 
      end 
 endmodule 

  

      【例7.4 】資料流程方式描述的4 1 MUX 

     module mux4_1c(out,in1,in2,in3,in4,cntrl1,cntrl2); 
     output out; 
     input in1,in2,in3,in4,cntrl1,cntrl2; 
    assign out=(in1 & ~cntrl1 & ~cntrl2)|(in2 & ~cntrl1 & cntrl2)| 

                (in3 & cntrl1 & ~cntrl2)|(in4 & cntrl1 & cntrl2); 

     endmodule 

      
      【例7.5 】用條件運算符描述的4 1 MUX 
     module mux4_1d(out,in1,in2,in3,in4,cntrl1,cntrl2); 
     output out; 
     input in1,in2,in3,in4,cntrl1,cntrl2; 
     assign out=cntrl1 ?  (cntrl2 ? in4:in3):(cntrl2 ? in2:in1); 
     endmodule 
      
      【例7.6 】門級結構描述的2 1MUX 
     module mux2_1a(out,a,b,sel); 
     output out; 
     input a,b,sel; 
     not  (sel_,sel); 
     and  (a1,a,sel_), 
           (a2,b,sel); 
     or  (out,a1,a2); 
     endmodule 
      
      【例7.7 】行為描述的2 1MUX 
     module mux2_1b (out,a,b,sel); 
     output out; 
     input a,b,sel; 
     reg out; 
     always @(a or b or sel) 
          begin 
          if (sel)        out = b; 
                else     out = a; 
          end 
     endmodule 

      
      【例7.8】資料流程描述的2 1MUX 
module MUX2_1c(out,a,b,sel); 
     output out; 
input a,b,sel; 
     assign out = sel ? b : a; 
endmodule 

 
 【例7.9 】調用門元件實現的1 位半加器 
module half_add1 (a,b,sum,cout); 
input a,b; 
output sum,cout;  
and  (cout,a,b); 
xor  (sum,a,b); 
endmodule 

 
 【例7.10】資料流程方式描述的1 位元元半加器 
module half_add2(a,b,sum,cout); 
input a,b; 
output sum,cout; 
assign sum=a^b; 
assign cout=a&b; 
endmodule 

 【例7.11】採用行為描述的1 位半加器 
module half_add3 (a,b,sum,cout); 
input a,b; 
output sum,cout; 
reg sum,cout; 
always @(a or b) 
  begin 
     case ({a,b})              //真值表描述 
     2'b00: begin  sum=0; cout=0;  end 
     2'b01: begin  sum=1; cout=0;  end 
     2'b10: begin  sum=1; cout=0;  end 
     2'b11: begin  sum=0; cout=1;  end 
     endcase 
  end 
endmodule 

 
 【例7.12】採用行為描述的1 位半加器 
module half_add4 (a,b,sum,cout); 
input a,b; 
output sum,cout; 
     reg sum,cout; 
       always @(a or b) 
              begin 
              sum= a^b; 
              cout=a&b; 
              end 
endmodule 

        
        【例7.13】調用門元件實現的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 

           【例7.14】資料流程描述的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 
        
        【例7.151 位全加器 
       module full_add3(a,b,cin,sum,cout); 
       input a,b,cin; 
       output sum,cout; 
       assign {cout,sum}=a+b+cin; 
       endmodule  
       
        【例7.16】行為描述的1 位全加器 
module full_add4(a,b,cin,sum,cout); 
       input a,b,cin; 
       output sum,cout; 
reg sum,cout;    //always 塊中被賦值的變數應定義為reg  
reg m1,m2,m3; 
always @(a or b or cin) 
      begin 
      sum = (a ^ b) ^ cin; 
      m1 = a & b; 
      m2 = b & cin; 
     m3 = a & cin; 
      cout = (m1|m2)|m3; 
      end 
 endmodule 

  
  【例7.17】混合描述的1 位全加器 
 module full_add5(a,b,cin,sum,cout); 
 input a,b,cin; 
 output sum,cout; 
 reg cout,m1,m2,m3;     //always 塊中被賦值的變數應定義為reg  
 wire s1; 
 xor x1(s1,a,b);                         //調用門元件 
 always @(a or b or cin)                 //always 塊語句 
     begin 
      m1 = a & b; 
      m2 = b & cin; 
      m3 = a & cin; 
      cout = (m1| m2) | m3; 
      end 
 assign sum = s1 ^ cin;                  //assign持續賦值語句 
 endmodule 

  
  【例7.18】結構描述的4 位元級連全加器 
 `include "full_add1.v" 
module add4_1(sum,cout,a,b,cin); 
output [3:0] sum; 
output cout; 
input [3:0] a,b; 
input cin; 
full_add1 f0(a[0],b[0],cin,sum[0],cin1);    //級連描述 
full_add1 f1(a[1],b[1],cin1,sum[1],cin2); 
full_add1 f2(a[2],b[2],cin2,sum[2],cin3); 
full_add1 f3(a[3],b[3],cin3,sum[3],cout); 
endmodule 

       【例7.19】資料流程描述的4 位元全加器 
    module add4_2(cout,sum,a,b,cin); 
     output[3:0] sum; 
     output cout; 
     input [3:0] a,b; 
     input cin; 
     assign {cout,sum}=a+b+cin; 
     endmodule 

      
       【例7.20 】行為描述的4 位全加器 
    module add4_3(cout,sum,a,b,cin); 
     output [3:0] sum; 
     output cout; 
     input [3:0] a,b; 
     input cin; 
     reg [3:0] sum; 
     reg cout; 
     always @(a or b or cin) 
    begin 
          {cout,sum}=a+b+cin; 
     end 
     endmodule 

          

沒有留言:

張貼留言

Node-Red & ModeBus FC=1

Node-Red & ModeBus FC=1 write address=0..7   8bits read address=16..23 8bits  [{"id":"cf7c1253d4d997dd","type&q...