# 訊息

## English

``````module fulladder (input a, b, c_in, output sum, c_out);
wire s1, c1, c2;

xor g1(s1, a, b);
xor g2(sum, s1, c_in);
and g3(c1, a,b);
and g4(c2, s1, c_in) ;
xor g5(c_out, c2, c1) ;

endmodule

module adder4(input signed [3:0] a, input signed [3:0] b, input c_in,
output signed [3:0] sum, output c_out);
wire [3:0] c;

fulladder fa1(a[0],b[0], c_in, sum[0], c[1]) ;
fulladder fa2(a[1],b[1], c[1], sum[1], c[2]) ;
fulladder fa3(a[2],b[2], c[2], sum[2], c[3]) ;
fulladder fa4(a[3],b[3], c[3], sum[3], c_out) ;

endmodule

module xor4(input [3:0] a, input [3:0] b, output [3:0] y);
assign y = a ^ b;
endmodule

module addSub4(input op, input signed [3:0] a, input signed [3:0] b,
output signed [3:0] sum, output c_out);

wire [3:0] bop;

xor4 x1(b, {op,op,op,op}, bop);
adder4 a1(a, bop, op, sum, c_out);

endmodule

module main;
reg signed [3:0] a;
reg signed [3:0] b;
wire signed [3:0] sum;
reg op;
wire c_out;

addSub4 DUT (op, a, b, sum, c_out);

initial
begin
a = 4'b0101;
b = 4'b0000;
op = 1'b0;
end

always #50 begin
op=op+1;
\$monitor("%dns monitor: op=%d a=%d b=%d sum=%d", \$stime, op, a, b, sum);
end

always #100 begin
b=b+1;
end

initial #2000 \$finish;

endmodule```
```

# Icarus 編譯執行結果

``````D:\ccc101\icarus\ccc>iverilog -o addSub4 addSub4.v

50ns monitor: op=1 a= 5 b= 0 sum= 5
100ns monitor: op=0 a= 5 b= 1 sum= 6
150ns monitor: op=1 a= 5 b= 1 sum= 4
200ns monitor: op=0 a= 5 b= 2 sum= 7
250ns monitor: op=1 a= 5 b= 2 sum= 3
300ns monitor: op=0 a= 5 b= 3 sum=-8
350ns monitor: op=1 a= 5 b= 3 sum= 2
400ns monitor: op=0 a= 5 b= 4 sum=-7
450ns monitor: op=1 a= 5 b= 4 sum= 1
500ns monitor: op=0 a= 5 b= 5 sum=-6
550ns monitor: op=1 a= 5 b= 5 sum= 0
600ns monitor: op=0 a= 5 b= 6 sum=-5
650ns monitor: op=1 a= 5 b= 6 sum=-1
700ns monitor: op=0 a= 5 b= 7 sum=-4
750ns monitor: op=1 a= 5 b= 7 sum=-2
800ns monitor: op=0 a= 5 b=-8 sum=-3
850ns monitor: op=1 a= 5 b=-8 sum=-3
900ns monitor: op=0 a= 5 b=-7 sum=-2
950ns monitor: op=1 a= 5 b=-7 sum=-4
1000ns monitor: op=0 a= 5 b=-6 sum=-1
1050ns monitor: op=1 a= 5 b=-6 sum=-5
1100ns monitor: op=0 a= 5 b=-5 sum= 0
1150ns monitor: op=1 a= 5 b=-5 sum=-6
1200ns monitor: op=0 a= 5 b=-4 sum= 1
1250ns monitor: op=1 a= 5 b=-4 sum=-7
1300ns monitor: op=0 a= 5 b=-3 sum= 2
1350ns monitor: op=1 a= 5 b=-3 sum=-8
1400ns monitor: op=0 a= 5 b=-2 sum= 3
1450ns monitor: op=1 a= 5 b=-2 sum= 7
1500ns monitor: op=0 a= 5 b=-1 sum= 4
1550ns monitor: op=1 a= 5 b=-1 sum= 6
1600ns monitor: op=0 a= 5 b= 0 sum= 5
1650ns monitor: op=1 a= 5 b= 0 sum= 5
1700ns monitor: op=0 a= 5 b= 1 sum= 6
1750ns monitor: op=1 a= 5 b= 1 sum= 4
1800ns monitor: op=0 a= 5 b= 2 sum= 7
1850ns monitor: op=1 a= 5 b= 2 sum= 3
1900ns monitor: op=0 a= 5 b= 3 sum=-8
1950ns monitor: op=1 a= 5 b= 3 sum= 2
2000ns monitor: op=0 a= 5 b= 4 sum=-7```
```