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

沒有留言:

張貼留言

2024產專班 作業2 (純模擬)

2024產專班 作業2  (純模擬) 1) LED ON,OFF,TIMER,FLASH 模擬 (switch 控制) 2)RFID卡號模擬 (buttom  模擬RFID UID(不從ESP32) Node-Red 程式 [{"id":"d8886...