2021年4月24日 星期六

HBLbits_Verilog Basic_Exams/m2014 q4j

 HBLbits_Verilog Basic_Exams/m2014 q4j

Implement the following circuit:

Exams m2014q4j.png


module top_module (
    input [3:0] x,
    input [3:0] y, 
    output [4:0] sum);
    wire [2:0]cout;
    wire cin;
    assign cin=1'b0;
    fadd u0(x[0],y[0],cin,cout[0],sum[0]);
    fadd u1(x[1],y[1],cout[0],cout[1],sum[1]);
    fadd u2(x[2],y[2],cout[1],cout[2],sum[2]);
    fadd u3(x[3],y[3],cout[2],sum[4],sum[3]);
endmodule
module fadd( 
    input a, b, cin,
    output cout, sum );
    assign {cout, sum}=a+b+cin;
     //assign cout = a & b | a & cin | b & cin;
    //assign sum = a ^ b ^ cin;
endmodule
//另一方法
module top_module ( 
     input [3:0] x,
     input [3:0] y, 
     output [4:0] sum ); 
 // This circuit is a 4-bit ripple-carry adder with carry-out. 
 assign sum = x+y;
// Verilog addition automatically produces the carry-out bit.
 // Verilog quirk: Even though the value of (x+y) includes the carry-out, (x+y) is still considered to be a 4-bit number (The max width of the two operands). 
 // This is correct: 
 // assign sum = (x+y); // But this is incorrect: 
 // assign sum = {x+y};
// Concatenation operator: This discards the carry-out 
endmodule

沒有留言:

張貼留言

113 學年度第 1 學期 RFID應用課程 Arduino程式

113 學年度第 1 學期 RFID應用課程 Arduino程式 https://www.mediafire.com/file/zr0h0p3iosq12jw/MFRC522+(2).7z/file 內含修改過後的 MFRC522 程式庫 (原程式有錯誤) //定義MFRC522...