首页 > 其他 > 详细

状态机写法

时间:2014-03-21 04:13:13      阅读:560      评论:0      收藏:0      [点我收藏+]

状态机的三段式写法

bubuko.com,布布扣
bubuko.com,布布扣
 1 module FSM(clk,rst,in,out);
 2 02 input clk,rst;
 3 03 input [7:0] in;
 4 04 output [7:0] out;
 5 05 
 6 06 parameter [1:0] //synopsys enum code
 7 07 START = 2d0,
 8 08 SA = 1,
 9 09 SB = 2,
10 10 SC = 3;
11 11 
12 12 reg [1:0] CS,NS;
13 13 reg [7:0] tmp_out,out;
14 14 
15 15 // state transfer
16 16 always @ (posedge clk or negedge rst)
17 17 begin
18 18 if (!rst) CS <= #1 START;
19 19 else      CS <= #1 NS;
20 20 end
21 21 
22 22 // state transfer discipline
23 23 always @ (in or CS)
24 24 begin
25 25     NS = START;
26 26     case (CS)
27 27        START: case (in[7:6])
28 28                        2b11: NS = SA;
29 29                        2b00: NS = SC;
30 30                        default: NS = START;
31 31                        endcase
32 32        SA: if(in == 8h3c) NS = SB;
33 33        SB: begin
34 34                 if (in == 8h88) NS = SC;
35 35                 else             NS = START;
36 36                 end
37 37        SC: case(1b1) //synopsys parallel_case full_case
38 38                 (in == 8d0): NS = SA;
39 39                 (8d0 < in && in < 8d38): NS = START;
40 40                 (in > 8d37): NS = SB;
41 41                   endcase
42 42        endcase
43 43 end
44 44 
45 45 // temp out
46 46 always @ (CS)
47 47 begin
48 48     tmp_out = 8bX;
49 49     case (CS)
50 50        START: tmp_out = 8h00;
51 51        SA:   tmp_out = 8h08;
52 52        SB:   tmp_out = 8h18;
53 53        SC:   tmp_out = 8h28;
54 54     endcase
55 55 end
56 56 
57 57 // reg out
58 58 always @ (posedge clk or negedge rst)
59 59 begin
60 60     if (!rst) out <= #1 8b0;
61 61     else     out <= #1 tmp_out;
62 62 end
63 63 
64 64 endmodule
View Code
bubuko.com,布布扣

状态机写法,布布扣,bubuko.com

状态机写法

原文:http://www.cnblogs.com/spongebob123/p/3614612.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!