Chapter4 Combinational Circuit
Chapter4 Combinational Circuit
Combinational-Circuit
Building Blocks
s
s f
w0 0 w0
f 0
w1 1 1 w1
w0 w0
s f s
w1 w1 f
s0
w0
s1
w1
w2
w3
(c) Circuit
w0 0
w1 1
0
f
1
w2 0
w3 1
w0
w3
w4 s2
s3
w7
w8
w11
w12
w15
x1 y1
x2 y2
x1 0
y1
1
x2 0
y2
1
w1 w2 f
w1 f
w1
0 0 0 w2
0
0 1 1
1 w2 w2
1 0 1 f
1 1 0
w2
w1
0
w3
f
1
(b) Circuit
0 0 0 0
0 0 1 1 w2
w2 ⊕ w3
0 1 0 1 w1
0 1 1 0 w3
1 0 0 1 f
1 0 1 0
w2 ⊕ w3
1 1 0 0
1 1 1 1
0 0 0 0
w3
0 0 1 1 w2
w1
0 1 0 1
w3
0 1 1 0 w3
1 0 0 1 f
w3
1 0 1 0
1 1 0 0
w3
1 1 1 1
0 0 0 0 w1 f
0 0 1 0 w2w3
0
0 1 0 0
1 w2 + w3
0 1 1 1
1 0 0 0
1 0 1 1
1 1 0 1
1 1 1 1
w1
w2
w3
f
(b) Circuit
f
w3
w2
w2
w1
w3
f
1
0
w3
En w0 y0 y4
w1 y1 y5
y2 y6
En y3 y7
w0 y0 y4
w1 y1 y5
y2 y6
w2 w0 y0 y3 y7
En
w3 w1 y1
y2
En En y3 w0 y0 y8
w1 y1 y9
y2 y10
En y3 y11
w0 y0 y12
w1 y1 y13
y2 y14
En y3 y15
w1
s0 w0 y0
s1 w1 y1 f
y2 w2
1 En y3
w3
0 0 0 1 0 0
0 0 1 0 0 1
0 1 0 0 1 0
1 0 0 0 1 1
w0
w1
y0
w2
y1
w3
(b) Circuit
0 0 0 0 d d 0
0 0 0 1 0 0 1
0 0 1 x 0 1 1
0 1 x x 1 0 1
1 x x x 1 1 1
assign f = s ? w1 : w0;
endmodule
endmodule
endmodule
endmodule
Figure 4.26. Code for a 2-to-1 multiplexer using the if-else statement.
module mux4to1 (w0, w1, w2, w3, S, f);
input w0, w1, w2, w3;
input [1:0] S;
output reg f;
always @(*)
if (S == 2'b00)
f = w0;
else if (S == 2'b01)
f = w1;
else if (S == 2'b10)
f = w2;
else if (S == 2'b11)
f = w3;
endmodule
Figure 4.27. Code for a 4-to-1 multiplexer using the if-else statement.
module mux4to1 (W, S, f);
input [0:3] W;
input [1:0] S;
output reg f;
always @(W, S)
if (S == 0)
f = W[0];
else if (S == 1)
f = W[1];
else if (S == 2)
f = W[2];
else if (S == 3)
f = W[3];
endmodule
endmodule
always @(W, S)
case (S)
0: f = W[0];
1: f = W[1];
2: f = W[2];
3: f = W[3];
endcase
endmodule
endmodule
endmodule
endmodule
always @(S, A, B)
case (S)
0: F = 4'b0000;
1: F = B - A;
2: F = A - B;
3: F = A + B;
4: F = A ^ B;
5: F = A | B;
6: F = A & B;
7: F = 4'b1111;
endcase
endmodule
Figure 4.35. Code that represents the functionality of the 74381 ALU chip.
module priority (W, Y, z);
input [3:0] W;
output reg [1:0] Y;
output reg z;
always @(W)
begin
z = 1;
casex (W)
4'b1xxx: Y = 3;
4'b01xx: Y = 2;
4'b001x: Y = 1;
4'b0001: Y = 0;
default: begin
z = 0;
Y = 2'bx;
end
endcase
end
endmodule
endmodule
Figure 4.37. A 2-to-4 binary decoder specified using the for loop.
module priority (W, Y, z);
input [3:0] W;
output reg [1:0] Y;
output reg z;
integer k;
always @(W)
begin
Y = 2'bx;
z = 0;
for (k = 0; k < 4; k = k+1)
if (W[k])
begin
Y = k;
z = 1;
end
end
endmodule
0 0 0 0 0 0 1 x
1 0 1 x 1 1 1 1
x 0 x x x x 1 x
^ 0 1 x ~^ 0 1 x
0 0 1 x 0 1 0 x
1 1 0 x 1 0 1 x
x x x x x x x x
always @(A, B)
begin
AeqB = 0;
AgtB = 0;
AltB = 0;
if(A == B)
AeqB = 1;
else if (A > B)
AgtB = 1;
else
AltB = 1;
end
endmodule
genvar k;
assign C[0] = carryin;
assign carryout = C[n];
generate
for (k = 0; k < n; k = k+1)
begin: fulladd_stage
wire z1, z2, z3; //wires within full-adder
xor (S[k], X[k], Y[k], C[k]);
and (z1, X[k], Y[k]);
and (z2, X[k], C[k]);
and (z3, Y[k], C[k]);
or (C[k+1], z1, z2, z3);
end
endgenerate
endmodule
Figure 4.41. Using the generate loop to define an n-bit ripple-carry adder.
Figure 4.42. Use of a task in Verilog code.
Figure 4.43. The code from Figure 4.42 using a function.
w3 w0 y0
w2 w1 y1
w1 w2 y2
y3
y4
y5
y6
1 En y7
endmodule
endmodule
Figure 4.54. Alternative Verilog code for the circuit in Figure 4.50.
module barrel (W, S, Y);
input [3:0] W;
input [1:0] S;
output [3:0] Y;
wire [3:0] T;
endmodule
endmodule
i4
i5
i6 f
i7
i8
endmodule
endmodule