|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
x
如题,个人感觉可能是状态机写得不好,贴出来想请大神们指导一下,嘻嘻:
- module vendor(moda,changeten,changefive,changeone,outp,clk,sort,coin10,coin5,coin1,cancel,confirm);
- input clk,coin10,coin5,coin1,cancel,confirm;
- input [1:0] sort;
- output reg moda;
- output reg changeten;
- output reg changefive;
- output reg [2:0] changeone;
- output reg [1:0] outp;
- reg [4:0] coins_sum;
- reg [4:0] coins_sum10;
- reg [4:0] coins_sum5;
- reg [4:0] coins_sum1;
- reg current_state;
- reg next_state;
- parameter idle=0;
- parameter juice=1;
-
- always@(negedge coin10 or posedge moda)
- begin
- if(moda)
- coins_sum10=5'b00000;
- else if(!coin10)
- coins_sum10 = coins_sum10+5'b01010;
- else ;
- end
- always@(negedge coin5 or posedge moda)
- begin
- if(moda)
- coins_sum5=5'b00000;
- else if(!coin5)
- coins_sum5 = coins_sum5+5'b00101;
- else ;
- end
- always@(negedge coin1 or posedge moda)
- begin
- if(moda)
- coins_sum1=5'b00000;
- else if(!coin1)
- coins_sum1 = coins_sum1+5'b00001;
- else ;
- end
- always@(negedge confirm or posedge moda)
- begin
- if(moda)
- coins_sum=5'b00000;
- else
- coins_sum=coins_sum1+coins_sum5+coins_sum10;
- end
- always@(posedge clk or posedge cancel)
- begin
- if(cancel)
- current_state = idle;
- else
- current_state = next_state;
- end
- always@(confirm or cancel or current_state)
- begin
- case(current_state)
- idle:begin
- if(sort==2'b01 | sort==2'b10 | sort==2'b11)
- begin
- if(confirm==1)
- next_state = juice;
- else if(cancel==1)
- next_state = idle;
- else
- next_state = current_state;
- end
- else
- next_state = current_state;
- end
- juice:begin
- if(sort==2'b01)
- begin
- if((coins_sum>5'b01010) | (coins_sum==5'b01010))
- begin
- if(confirm==0)
- next_state=idle;
- else if(cancel==1)
- next_state=idle;
- else
- next_state = current_state;
- end
- else
- next_state = current_state;
- end
- else if(sort==2'b10)
- begin
- if((coins_sum>5'b01110) | (coins_sum==5'b01110))
- begin
- if(confirm==0)
- next_state=idle;
- else if(cancel==1)
- next_state=idle;
- else
- next_state = current_state;
- end
- else
- next_state = current_state;
- end
- else
- begin
- if((coins_sum>5'b10100) | (coins_sum==5'b10100))
- begin
- if(confirm==0)
- next_state=idle;
- else if(cancel==1)
- next_state=idle;
- else
- next_state = current_state;
- end
- else
- next_state = current_state;
- end
- end
- endcase
- end
-
-
- always@(confirm or cancel or current_state)
- begin
- case(current_state)
- idle:
- begin
- moda=1;
- changeten=0;
- changefive=0;
- changeone=3'b000;
- outp=2'b00;
- end
- juice:
- begin
- moda=0;
- if(sort==2'b01)
- begin
- if((coins_sum>5'b01010) | (coins_sum==5'b01010))
- begin
- if(confirm==0)
- begin
- changeten=0;
- changefive=0;
- changeone=3'b000;
- outp=2'b01;
- end
- else if(cancel==1)
- begin
- changeten=1;
- changefive=0;
- changeone=3'b000;
- outp=2'b00;
- end
- else
- begin
- changeten=0;
- changefive=0;
- changeone=3'b000;
- outp=2'b00;
- end
- end
- else
- begin
- changeten=0;
- changefive=0;
- changeone=3'b000;
- outp=2'b00;
- end
- end
- else if(sort==2'b10)
- begin
- if((coins_sum>5'b01110) | (coins_sum==5'b01110))
- begin
- if(confirm==0)
- begin
- if(coins_sum>5'b01111)
- begin
- changeten=0;
- changefive=1;
- changeone=3'b001;
- outp=2'b10;
- end
- else
- begin
- changeten=0;
- changefive=0;
- changeone=3'b001;
- outp=2'b10;
- end
- end
- else if(cancel==1)
- begin
- changeten=1;
- changefive=0;
- changeone=3'b100;
- outp=2'b00;
- end
- else
- begin
- changeten=0;
- changefive=0;
- changeone=3'b000;
- outp=2'b00;
- end
- end
- else
- begin
- changeten=0;
- changefive=0;
- changeone=3'b000;
- outp=2'b00;
- end
- end
- else
- begin
- if((coins_sum>5'b10100) | (coins_sum==5'b10100))
- begin
- if(confirm==0)
- begin
- changeten=0;
- changefive=0;
- changeone=3'b000;
- outp=2'b11;
- end
- else if(cancel==1)
- begin
- changeten=1;
- changefive=0;
- changeone=3'b101;
- outp=2'b00;
- end
- else
- begin
- changeten=0;
- changefive=0;
- changeone=3'b000;
- outp=2'b00;
- end
- end
- else
- begin
- changeten=0;
- changefive=0;
- changeone=3'b000;
- outp=2'b00;
- end
- end
- end
- endcase
- end
- endmodule
复制代码 |
|