|
我也來寫一個
7 D/ I! {" V, k輸入:clk, rst
: z! m0 a: m. U( @4 q) P2 `輸出:tens(十位數), units(個位數)
2 a9 _! Y; V5 ~0 J! f$ {5 b7 Y) Y. g5 g3 ?3 Y2 t6 F
code :
6 j$ Q8 c) V4 Y+ j: H: C$ y& D) M
* k. Z: }" @, j7 J/ B( U- |$ e8 `7 }module BCD(clk, rst, tens, units);3 M2 H, r0 c3 p( c0 C" T
input clk, rst;2 f5 q+ p# r2 f) x ^ u7 {8 j6 y
output [2:0] tens;7 k) c7 S9 y6 f- n2 j0 I! H
output [3:0] units;
& G5 l* P' Z! Q. ~6 g( P8 X S! y reg [2:0] tens, xtens;# D1 o) u8 H( {" W+ d0 J! L
reg [3:0] units, xunits;
% w6 ~: l! e, a% b; J* T: i' z
' Z% Z6 A" L" i- F. B- V2 T# B always@(posedge clk or posedge rst)
5 W$ a) D8 @5 P" w4 D begin
3 t4 p: G% G8 W. _9 S7 L; o6 c7 F if(rst)
: k5 Q9 I9 s! Y- @# E+ ? begin
' b; o, Z5 x! i$ T( ^ tens <= 3'b0;7 j( Q" t K( x" K: E U
units <= 4'b0;! F% w; B# T) ?# ?- i1 H6 A! X9 Z9 C
end- g% C4 @$ s: S+ v# j9 i7 R7 L ^# C
else
; [/ O9 s& Z* Z! F0 h begin
" a0 E6 o( B( |# z! F0 v tens <= xtens;
! E4 k# Q6 g% l m1 i7 s units <= xunits;6 M% W* r ^1 [8 N
end
) X7 R/ o1 _% j; J$ i0 Y end
4 \) ^ j) S( F' ]
3 M% ]) K6 Q: z" t$ h always@(tens or units)
- S; o0 r( @, c5 y! r4 e begin
( n( G% H' g7 G5 Q! y3 V if(units == 4'd9)
% P6 ^8 F6 e: _) p& F, O+ L begin- U' N, _3 y2 H& ~) I
xunits = 4'b0;
. l1 Q) P9 J8 m/ f4 {; A xtens = tens + 1;0 P0 X5 Q" S7 m6 ?& w; h
if(tens > 3'd5)
l) b* ]& D3 D) A$ @) c2 C5 J6 b1 y xtens = 3'b0;2 h# P; q Q+ c7 b
else
. d* s6 \9 N) B" @; Y/ V xtens = tens;
; T3 `0 n* n: [1 y. I end" M, _: k& {0 c! [0 t1 E$ y6 ^
else
& L% c% \# R& A begin
- E: }" {6 I3 X xunits = units + 1;# t0 |0 ]/ M+ E2 Y" r* m
xtens = tens;
; W- N- {/ P1 W0 z/ P4 e end- s# G$ i- Q0 J% }1 \6 N+ P
end0 r( ]* h+ K- m! @- m1 Z+ k$ H5 T
endmodule |
|