|
ALU:+ }9 x) z/ b9 l" B, k0 A1 Q
module ALU(CTRL,A,B,Q,Code);
' g& X6 R& r% w: R0 Rinput CTRL;8 [6 H+ O# K2 |% g
input A;
) O- J) M! } ^% ^input B;
$ j/ u; c; ]8 x$ x. voutput Q;
, o7 t1 T4 I$ I: Ginput[2:0] Code;
) V9 l6 k0 B `6 C- bif (CTLR === 0)
" B {4 E6 C% [& v Q = 0;' F* h/ d2 G2 J8 y+ K
else
' |* Q# S! [) ]# ]6 p& i) u case(Code)
" p& d1 F1 K' F' w3 c, D 3'b000: Q = A+1;
$ {( a9 ^. l% C- {" i. d7 p 3'b001: Q = A+B+1;
9 t5 Y9 e$ |5 N; d4 x* a4 X' }+ D 3'b010: Q = A;+ ^' z9 k; w; p x9 ~
3'b011: Q = A+B;! \! Y5 S7 ^: Q7 b
3'b100: Q = A-1;+ n* X' V% X8 W. Y8 M
3'b101: Q = A-B;' L0 t4 _: Y$ g1 F% `
3'b110: Q = B;% y( O; e% k( S
default: Q = A-B-1;/ j& G2 q! ~: ]: o" s% T
endcase' I& G# M0 y8 a1 ?) z0 {
endmodule. H6 \- @( B+ R( B' a
0 N, p, M; g$ [! Y
: k2 `8 d4 P2 ~( l! g( i# ]testbench:; O1 f5 w9 g+ K: n2 C
module ALU_tb();; O8 L2 h2 N1 B0 B# b
reg A,B,CTRL;
, J9 f+ t& T4 Y& Qwire Q;
$ M/ F# N# u, j" `/ H4 zreg[2:0] Code;
: v, X3 n1 f3 y5 \* uinteger case_num;
# x1 e8 I4 X7 u- r% H( t* S0 Yinitial begin
C8 G7 ?& u2 K) t. g/ M, U CTRL=0; S( T/ v$ T$ p+ v& H
case_num = 1;# m8 r# ~+ I& x6 Y
A=1 'b0;4 B7 ]5 j6 u9 @6 p( w: y
B=1 'b1;
: N# k J9 z8 A! k" T8 b c #100;( y# }+ |; H, T. v) M) D6 R) X
if(Q==0)
2 N' D% v* g" P4 J3 [ $display("[Simulation Info] Case %2d PASS.",case_num);& t0 b1 a2 H P% e6 [& S, x S
else
+ t) z ]" X( ?, V $display("[Simulation Info] Case %2d ERROR.",case_num);1 } `/ K6 | I0 M) ~# @1 R6 S
#100;
P2 {7 R2 {" e: T* w CTRL=1;
0 i5 o2 a- v( d0 m' j4 \ case_num = 2;/ z# w* L0 x" }9 N/ y* ]
A= 1 'b1;
4 x4 K$ d( \$ Q1 ?0 t! [ B= 1 'b0;1 {. x$ `* n9 ~* T5 W
Code = 3 'b000;& h3 F2 t# {4 m8 Q, ]/ X4 }7 W
#20;0 R7 t7 h6 ^+ V1 [6 Y! J
if(Q==0)" n* p! R: n6 e# D+ Q
$display("[Simulation Info] Case %2d PASS.",case_num);$ o) v, T' ~# E* w1 }2 h+ V, l; i) T. M
else
) U* P7 [- @! n. s5 G $display("[Simulation Info] Case %2d ERROR.",case_num);/ \& F, I9 X* t+ B; U
#100;
7 o( Y1 ~: H3 a/ E case_num = 3;
6 ?5 s2 T% p: i0 y Code = 3 'b001;
& Z8 g, |; Q/ |0 z, c; Y# d #20;* G- _7 r. T' l ]$ B$ q z
if(Q==0)2 u# \. z' }- a9 {3 a
$display("[Simulation Info] Case %2d PASS.",case_num);# J( d4 x3 u! B# s- z! Q
else; N( O! V& a3 f
$display("[Simulation Info] Case %2d ERROR.",case_num);
7 S- y2 q u0 u f- D' [. k #100;5 J0 ~# N( d* ^* h2 N
case_num = 4;
& M% O# z. G- j. `* h Code = 3 'b010;
% L* y1 H) ?! ~1 j! g( m #20;
3 W- K, m& s% g0 h if(Q==1)4 P: x. z, I! \, c3 C& a
$display("[Simulation Info] Case %2d PASS.",case_num);- j `/ s6 I0 d- e! o
else
) X+ ^! f& S$ K' Y5 B1 L7 ` $display("[Simulation Info] Case %2d ERROR.",case_num);
/ o, F) e( x& |+ U" ? #100;
; J, b3 m3 `0 F: z% f8 d: v case_num = 5;
$ Z) @7 Z: c$ l4 X Code = 3 'b011;: U3 P, z X+ @( Z* E4 T# Y& [
#20;
5 M2 e; Y! D5 z! o) a, C4 H( c if(Q==1)& d- s" }. L/ D: p( P" j
$display("[Simulation Info] Case %2d PASS.",case_num);) C1 O$ I/ J! g8 b' P, v
else
; C' a8 R: M7 A5 V: y $display("[Simulation Info] Case %2d ERROR.",case_num);! ~# S% t4 J' T
#100;3 T* p' B* ^" W- l& y- D
case_num = 6;
( A+ e; r) ] p2 W Code = 3 'b100;
6 j o) D1 }9 [ #20;. O# ]( U0 z0 r9 g5 }# E; f
if(Q==0)/ A6 K, w9 j0 o8 L( Y4 {+ ]
$display("[Simulation Info] Case %2d PASS.",case_num);
2 ?6 M2 z' A4 n2 h else7 o: K/ N' u/ c: T
$display("[Simulation Info] Case %2d ERROR.",case_num);0 ~% {+ ~ e0 g+ `# u' `; e7 p
#100; b4 z8 L! K3 ~& S$ o" w+ M% n
case_num = 7;2 e1 c+ i4 k/ ~+ f/ |; E1 B
Code = 3 'b101;/ W* P; K$ R/ L, x& j( G
#20;
9 h- s2 R5 d! ^& C if(Q==1)' V, k+ @1 ~* m
$display("[Simulation Info] Case %2d PASS.",case_num);9 D& H1 n+ E: f2 ^4 }. o! W
else/ J6 g" @6 Q/ ?! V( k. w
$display("[Simulation Info] Case %2d ERROR.",case_num);
0 [; B( b4 ~3 F3 i/ z #100;2 W) N# g2 c2 I2 B' W) }) V$ H
case_num = 8;
; y2 ~& Z6 C' r* M. K% ~( s$ q Code = 3 'b110;
5 o: K: e8 H' C, Z* }& D #20;
; t8 z! v' B7 o I9 b* C, d if(Q==0)
1 T7 h8 b: n- g1 q2 @1 s $display("[Simulation Info] Case %2d PASS.",case_num);
* ], h% r1 y" L5 K* h- M2 L else
/ E, ]; R, i( D0 a $display("[Simulation Info] Case %2d ERROR.",case_num); b7 S+ T W/ M: I) t; v( q
#100;% \; \: g( W- K
case_num = 9;
- @& e$ v, h' t Code = 3 'b111;. `# B' K. n. i% _, j* C; T
#20;
/ ?; U2 ?; Z& u& e4 z4 a if(Q==0)
' H: h/ r; B6 `3 n" j6 U [ $display("[Simulation Info] Case %2d PASS.",case_num);8 [" p; j* w% |2 b4 t
else' r7 T/ x/ @+ l; }. F! ?5 a6 y
$display("[Simulation Info] Case %2d ERROR.",case_num);
; E7 A3 v* J$ N6 W7 [ w #100;
4 b: S6 X4 l V( ~# t $finish;* ?% v# N- y \5 d3 {" J9 A5 m
end
& n" @1 U/ `( D: {endmodule |
|