|
ALU:. C( _5 \, w4 z ~- N0 K8 o+ `
module ALU(CTRL,A,B,Q,Code);
1 p( N" o, G3 M: A/ `9 A5 ?' xinput CTRL;
! g+ J9 E K" Einput A;
8 f$ I* t5 _. O' _2 ?; f3 v5 Kinput B;' E' g q @$ a& p" Z
output Q;/ \7 B" y& W& j. e$ {% P- g
input[2:0] Code;$ o& ~. i( e0 r# k0 o% J
if (CTLR === 0)! K4 I0 e, V; J2 C* i
Q = 0;. L2 J5 `# Q% R
else
! E- w8 N2 y4 G9 h" v case(Code)* e) R+ Q, N2 E& m7 z( K/ F
3'b000: Q = A+1;8 }3 S4 ^% r3 N
3'b001: Q = A+B+1;* c3 O! Z/ z3 [! A0 N) J+ H
3'b010: Q = A;
8 m+ B1 F: M; Y: Z: f 3'b011: Q = A+B;
: V, H& G6 M7 N( O! [ 3'b100: Q = A-1;
2 Y8 ~- Y# G! ^7 C6 { 3'b101: Q = A-B;
" o7 S/ J* @# z1 w6 z 3'b110: Q = B;
7 c4 {' c5 j3 h9 g z default: Q = A-B-1;
M- y6 i+ K/ r endcase/ f7 @6 M8 P8 W- o
endmodule
4 `8 \6 [* x8 F/ n0 q
; b) f/ p$ b/ I
" ^: M6 o! p8 ` J6 R" Jtestbench:# R3 e* d# Q, ^: n2 v% Q1 @: \) ]! {
module ALU_tb();( f! h/ A. q. p% P- W) r$ X4 c
reg A,B,CTRL;
' v5 L4 ]4 V/ ] w" P& \7 @+ zwire Q;
7 ~& c" Y5 W5 T9 @4 t* w# o* Xreg[2:0] Code;
9 u& j* q' w) B1 F, r/ h5 r& F6 y; Cinteger case_num;
2 }# ~6 I, C9 q) ^% q; ^initial begin% R5 \! D9 j {& c2 B& H9 u" M2 v2 M
CTRL=0;8 B1 F. ? h- Q3 k7 I) K# g
case_num = 1;; S6 ~8 J, _: W/ U" m% h
A=1 'b0;
; r( W9 u' J) R- v B=1 'b1;
9 o2 P% C0 v6 \' f$ T/ M& v8 ^ #100;5 M, S: u" `3 V# S, |3 Z
if(Q==0)
( [: a* d6 K( }$ s* H5 x $display("[Simulation Info] Case %2d PASS.",case_num);
' V+ O5 }/ w0 |& l" q/ V9 ? else8 |2 N* o6 S6 ~ W6 E
$display("[Simulation Info] Case %2d ERROR.",case_num);- A4 |% X6 l( s
#100;
2 V. G$ y" J1 q& N2 @: N CTRL=1;
2 |* I# z- {- ]/ c case_num = 2;
9 `9 r% l9 u' D) ` A= 1 'b1;
; K# A! n' u5 F. e$ e- g B= 1 'b0;5 z* S+ C/ m$ d& Q
Code = 3 'b000;) f: }+ u2 `# s) B6 m
#20;( H8 C+ }- C7 |: e, P( T A
if(Q==0) E* q) w: T& @/ T! j: a
$display("[Simulation Info] Case %2d PASS.",case_num);
+ K- q- c X9 y* u. _ else$ ~. s. @3 t) S0 {
$display("[Simulation Info] Case %2d ERROR.",case_num);2 f _: h% Y% F7 a( Z2 @# W: w* A6 }
#100;6 N n5 M% C I$ I/ @6 m
case_num = 3;
o" I: u4 b) O/ R& }; o: W$ S! b Code = 3 'b001;
& D) ]2 l0 n. m+ t% |3 ~0 V #20;# S! m# P7 _ [- \- J
if(Q==0)
+ W0 H4 l0 ]7 F \ $display("[Simulation Info] Case %2d PASS.",case_num);+ c3 [* J' |% Y, l) u& N9 ~
else1 x* K& V. B& d
$display("[Simulation Info] Case %2d ERROR.",case_num);
- s0 x g+ o4 L: O& C #100;% N# J# ~+ W9 z
case_num = 4;4 w4 N- y# o o
Code = 3 'b010;
9 I' m8 y8 c2 h0 y #20;
9 L0 m1 V0 B9 o1 f, o# c: a9 e if(Q==1)
- ]; ?2 o+ _$ K $display("[Simulation Info] Case %2d PASS.",case_num);) I' H8 i% r& y# e$ k
else' \) b7 X) h! i2 T! T# [/ d
$display("[Simulation Info] Case %2d ERROR.",case_num);, N' ]3 |3 j1 W% E
#100;: T2 a6 }+ M, T/ h1 \
case_num = 5;: x. x1 w( C# Y% c7 R" P% F
Code = 3 'b011;
! ]# z) J* w* l {2 Y- E7 u #20;' b$ B7 |0 p: g, f+ R0 W
if(Q==1)/ d# |! D5 ?5 D4 c+ w; I
$display("[Simulation Info] Case %2d PASS.",case_num);
& L8 H, i4 c1 G% z8 S else- H4 G/ z. {/ J+ I
$display("[Simulation Info] Case %2d ERROR.",case_num);
+ r6 I( _) G& y; b0 x4 q #100;3 X; g+ b' s! n$ j: o, ~/ N
case_num = 6;
+ Y/ S1 a4 i$ w: K4 j8 e Code = 3 'b100;
' R3 m* m( r* O) G" p. S. q #20;4 C9 K& g1 d d+ D3 \) I5 s
if(Q==0)
7 y D+ S/ B1 B1 Y C6 O8 K! J" E $display("[Simulation Info] Case %2d PASS.",case_num);
_$ o# U* y) L' o else4 Z; D4 i" }9 d3 {
$display("[Simulation Info] Case %2d ERROR.",case_num);/ C W/ v' N* Y
#100;6 M6 ?4 @& j0 h/ u; A" y
case_num = 7;+ m* s2 y% _/ c4 e( Q- R& I, {
Code = 3 'b101;6 _! _& d" H" W( {" |" W ?
#20;( R! u- i6 T! A1 j8 I' n
if(Q==1)
! S8 T9 ]; g# g' r4 B: g $display("[Simulation Info] Case %2d PASS.",case_num);0 Z' ^# u$ q% ]3 }% h' q5 H
else6 {) Q4 D3 \, b+ o
$display("[Simulation Info] Case %2d ERROR.",case_num);: ^/ H* m2 f3 L/ o/ _
#100;
. d4 m. u0 v: i% | case_num = 8;; x, r, k) M5 ~3 K) y4 N
Code = 3 'b110;/ M# D9 Q5 E& ?0 ~ F! F* {# r5 S
#20;
8 B+ `( Q& y: J. X! ~% h: N) p if(Q==0): g! d5 G. }- m" R& w
$display("[Simulation Info] Case %2d PASS.",case_num);
! z, B5 V/ p7 i a& r8 b else
+ D8 B9 X1 j' Y8 U $display("[Simulation Info] Case %2d ERROR.",case_num);
8 w1 O& C) A2 X' D: D: D7 [7 p #100;
" Z1 q `6 q, K) n case_num = 9;- k5 }: ~( w1 k7 r% _ ?% [" r N
Code = 3 'b111;
! _9 m/ N; H/ `" @, H5 q #20;2 n- J7 N, N. K I' p5 H
if(Q==0): c3 e6 u' q; H) Q W6 ]
$display("[Simulation Info] Case %2d PASS.",case_num);! s+ O% n8 q# V
else% F, f; {( A _; {* J
$display("[Simulation Info] Case %2d ERROR.",case_num);# T, I3 q! }- p" Q B7 z( m
#100;* z8 o# ^) m- q6 M9 s1 `/ r
$finish;
! Q! [3 P: f0 a6 J( z! ^2 | end
2 X; k: f: I! cendmodule |
|