|
我也來寫一個! S( v2 \5 O/ W- i
輸入:clk, rst2 n1 _; Q9 s) W. q% s6 U% x
輸出:tens(十位數), units(個位數)
: V; L$ w/ ~+ W l
" o, Z( x- Y* V# c! M U% p, fcode :
1 f' F& G. ^! Z1 q
0 K, u& }! o* O5 {module BCD(clk, rst, tens, units);, n4 _7 d6 D( G2 t
input clk, rst;
/ ^8 _" L& A) Y9 D8 W2 m output [2:0] tens;
9 X5 V* g2 B8 t6 k% t B output [3:0] units;6 Z* _2 ^ s: B& s* {6 }6 `
reg [2:0] tens, xtens;5 g3 s0 Q* @0 k9 }& r2 i
reg [3:0] units, xunits;0 @$ Z/ ]6 L8 S1 q9 y; I) v
" F8 S& ?0 B9 s$ j, f" C always@(posedge clk or posedge rst); Z+ g$ o6 U. M( N" X
begin2 o5 J6 B* K1 n) l7 C% |
if(rst)7 h. E; v4 B& ?( P7 _9 p
begin [& \; m9 Y* Q% t' ~; g' y' a
tens <= 3'b0;
$ P, J( J b0 _) e( G/ M units <= 4'b0;
& @2 D5 E1 n/ p# V( B y9 n* F! {7 d+ P end g: J/ m! v3 f3 X2 P+ E. m
else
5 G3 C' A7 w( u9 G% V& i% { begin4 B$ F' Z6 s2 f5 U) e4 `$ S6 B
tens <= xtens;* _1 a+ e* G, ~5 O" O, H3 \3 G
units <= xunits;' K0 m: j9 D6 ]& _
end
4 u: ]- a, o# I( F7 v1 ~: Z7 s end ~) N. O- t: \7 u/ P
, `- E5 K6 Y$ n7 r5 b. {/ w always@(tens or units)4 W4 E5 ?$ ^. ]* l o3 [* m6 M
begin
6 ^: S4 p. b" \/ _4 J7 o if(units == 4'd9)$ b7 ^; {2 K8 C1 D% f4 G1 x
begin. f. j" c4 n7 C
xunits = 4'b0;
0 A7 \$ O6 G, d; r xtens = tens + 1;
) X0 W! v$ g4 O$ I1 R if(tens > 3'd5)( J; K9 F9 m' }6 d/ |* h
xtens = 3'b0;0 }- x8 L$ N E5 l- q
else
; e/ J5 |. s* c) q* D: i( P, T xtens = tens;# i& Q2 w% u4 S9 {" h
end5 V" B# G# F: X
else
9 F( g* R, ~4 v9 X0 h7 D+ ^ begin
" G) W; m+ Q5 S xunits = units + 1;" {7 l4 p; h# X
xtens = tens;
! t2 D% `: ~+ Y end
7 K! W* g: {3 }; v end) Q% F8 i. O: O0 D o3 ~* S
endmodule |
|