|
我也來寫一個- l! V: |! N$ S# ~# ?# U7 w
輸入:clk, rst) }5 N$ ~4 v8 D
輸出:tens(十位數), units(個位數)
, X! a# F9 N. q) a& u! T5 N- ] T# i5 k# ^* p; p
code : : b; X- u; x2 g4 ^+ V& s, f
$ c+ A4 e3 t3 ]. }( W+ emodule BCD(clk, rst, tens, units);3 g6 p! l' }9 l: ]
input clk, rst;5 g8 G5 ]. O% @
output [2:0] tens;# m/ K. E0 J1 ?7 i7 ?' X
output [3:0] units;
! O) P4 ?3 r' E2 c% I6 U reg [2:0] tens, xtens;" W% J7 z4 h' J
reg [3:0] units, xunits;
$ l3 w5 Y/ A0 _! T6 Z7 k/ @
9 L: _: {( Q& |- Y( r always@(posedge clk or posedge rst)0 X- m8 D, K1 a6 e" J4 @/ m& X
begin+ o4 `+ D- T) ]$ S! X7 i
if(rst)3 ~0 T2 v) W1 A$ [3 C
begin
& E. s" H9 W+ \$ ^% D' f' P tens <= 3'b0;) k' r/ j/ B8 w V( _% N8 n
units <= 4'b0;
" J4 H$ K2 U8 |4 a. a/ H( w end7 @( U8 q! t( K6 b y
else2 i( _) J- a3 T0 c- R
begin
+ h9 j1 p2 ^6 l9 w8 e/ ?" J9 d/ J- y tens <= xtens;
- ^0 N8 Z4 ^% {; P2 P2 Z units <= xunits;# m- o/ \0 ?* o* T
end9 h2 S/ M# C+ L+ V M% |2 a
end
* u/ `9 @$ b) u3 o, C
$ z% D! n! @8 m always@(tens or units)
9 ^, d5 m: K8 o begin* {$ O3 h" D4 f/ Q; ~
if(units == 4'd9)
1 X& `) ?) |9 N6 b begin) {! Y' N* ?- f# m7 `
xunits = 4'b0;8 q+ ~! V$ y; R' F
xtens = tens + 1;. l6 \: s# x" J! Z6 E
if(tens > 3'd5)+ y1 q8 K6 \, {" l
xtens = 3'b0;
% h, m" d4 d. H- i& O else
5 q9 [% s0 e& U+ @0 G& z xtens = tens;
1 A' A+ G6 C$ b end
1 b2 R" f6 L+ b else
3 I+ b+ ]' c- L4 v# h& R: I. a Z begin7 `5 u0 ~- \# K* {: \
xunits = units + 1;
6 p3 @- z3 A1 k& |& q/ k xtens = tens;
2 e+ k# q1 H% y3 ^* {* l. q end5 O3 |3 v: t& |6 ?- g5 G3 S
end3 ^/ J7 \( O$ W1 m# e6 }
endmodule |
|