Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. //verilog code
  2.  
  3. module moore_fsm (clock, x, z);
  4.  
  5. input clock, x;
  6.  
  7. output reg z;
  8.  
  9. reg [2:0] present_state, next_state;
  10.  
  11. always @ (posedge clock)
  12.  
  13. present_state <= next_state;
  14.  
  15.  
  16. always @ (present_state or x)
  17.  
  18. begin
  19.  
  20. case(present_state)
  21.  
  22. 3'b000: begin z <= 1'b0; if (~x) next_state <= 3'b001; else next_state <= 3'b000; end
  23. 3'b001: begin z <= 1'b0; if (x) next_state <= 3'b010; else next_state <= 3'b001; end
  24. 3'b010: begin z <= 1'b0; if (~x) next_state <= 3'b011; else next_state <= 3'b000; end
  25. 3'b011: begin z <= 1'b1; if (x) next_state <= 3'b100; else next_state <= 3'b011; end
  26. 3'b100: begin z <= 1'b1; if (~x) next_state <= 3'b000; else next_state <= 3'b101; end
  27. 3'b101: begin z <= 1'b1; if (~x) next_state <= 3'b011; else next_state <= 3'b101; end
  28. default: next_state <= 3'b000;
  29. endcase
  30.  
  31. end
  32.  
  33. endmodule
  34.  
  35. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  36.  
  37. //testbench
  38.  
  39. module tb_moore_fsm();
  40.  
  41. reg clock, x;
  42.  
  43. wire z;
  44.  
  45. moore_fsm uut(clock, x, z);
  46.  
  47. initial
  48. begin
  49. x = 1;
  50. #25;
  51. x = 1;
  52. #25;
  53. x = 0;
  54. #25;
  55. x = 0;
  56. #25;
  57. x = 1;
  58. #25;
  59. x = 0;
  60. #25;
  61. x = 1;
  62. #25;
  63. x = 0;
  64. #25;
  65. x = 1;
  66. #25;
  67. x = 0;
  68. #25;
  69. x = 0;
  70. #25;
  71.  
  72. end
  73.  
  74. always
  75. begin
  76. clock = 1;
  77. #10;
  78. clock = 0;
  79. #10;
  80. end
  81.  
  82. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement