Advertisement
jack96013

ex5_37

Jun 7th, 2020
2,090
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //ex5_37
  2. module ex5_37_a(output reg y,output reg[2:0] state,input x,clk,reset);
  3.     reg[2:0] next_state;
  4.     parameter a=3'b000,b=3'b001,c=3'b010,d=3'b011,e=3'b100,f=3'b101,g=3'b110;
  5.     always@(posedge clk,negedge reset)
  6.         if(reset==0) state<=a;
  7.         else state<=next_state;
  8.     always @(state,x)
  9.     begin
  10.         y=0; //Default situation
  11.         case  (state)
  12.         a:if(x) begin next_state=b;y=0;end else begin next_state=a;y=0;end
  13.         b:if(x) begin next_state=d;y=0;end else begin next_state=c;y=0;end
  14.         c:if(x) begin next_state=d;y=0;end else begin next_state=a;y=0;end
  15.         d:if(x) begin next_state=f;y=1;end else begin next_state=e;y=0;end
  16.         e:if(x) begin next_state=f;y=1;end else begin next_state=a;y=0;end
  17.         f:if(x) begin next_state=f;y=1;end else begin next_state=g;y=0;end
  18.         g:if(x) begin next_state=f;y=1;end else begin next_state=a;y=0;end
  19.         default:next_state=a; //self-correcting
  20.         endcase
  21.     end
  22. endmodule
  23.  
  24. //ex5_37_b
  25. module ex5_37_b(output reg y,output reg[2:0] state,input x,clk,reset);
  26.     reg[2:0] next_state;
  27.     parameter a=3'b000,b=3'b001,c=3'b010,d=3'b011,e=3'b100;
  28.     always@(posedge clk,negedge reset)
  29.         if(reset==0) state<=a;
  30.         else state<=next_state;
  31.     always @(state,x)
  32.     begin
  33.         y=0; //Default situation
  34.         case  (state)
  35.         a:if(x) begin next_state=b;y=0;end else begin next_state=a;y=0;end
  36.         b:if(x) begin next_state=d;y=0;end else begin next_state=c;y=0;end
  37.         c:if(x) begin next_state=d;y=0;end else begin next_state=a;y=0;end
  38.         d:if(x) begin next_state=d;y=1;end else begin next_state=e;y=0;end
  39.         e:if(x) begin next_state=d;y=1;end else begin next_state=a;y=0;end
  40.         default:next_state=a; //self-correcting
  41.         endcase
  42.     end
  43. endmodule
  44.  
  45. //Compare
  46. module t_ex5_37();
  47.     wire y_a,y_b;
  48.     wire[2:0] state_a,state_b;
  49.     reg x,clk,reset;
  50.  
  51.     ex5_37_a m0(y_a,state_a,x,clk,reset);
  52.     ex5_37_b m1(y_b,state_b,x,clk,reset);
  53.  
  54.     initial begin clk=0; forever #5 clk =~ clk; end
  55.     initial fork
  56.         reset=1;
  57.         x=0;
  58.         #2 reset=0;
  59.         #3 reset=1;
  60.         #20 x=1;
  61.         #40 x=0;
  62.         #60 x=1;
  63.         #100 x=0;
  64.         #120 x=1;
  65.         #160 x=0;
  66.         #180 x=1;
  67.         #200 $finish;
  68.     join
  69. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement