Advertisement
punjusquad

question_11

Dec 17th, 2023
1,747
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Code your design here
  2.  
  3. //Interface
  4.  
  5. interface ifc(input clk);
  6.   logic rst;
  7.   logic coin;
  8.   logic dwash;
  9.   logic lid;
  10.   logic soak;
  11.   logic wash;
  12.   logic rinse;
  13.   logic spin;
  14.   logic done;
  15.  
  16.   modport DESIGN(
  17.     input clk, rst, coin, dwash, lid,
  18.     output soak, wash, rinse, spin, done
  19.   );
  20.  
  21.   modport TEST(
  22.     output rst, coin, dwash, lid,
  23.     input soak, wash, rinse, spin, done, clk
  24.   );
  25. endinterface
  26.  
  27. // Controller
  28.  
  29. module wmc(
  30.   ifc.DESIGN inf,
  31.   input T,
  32.   output pause
  33. );
  34.  
  35.   reg [2:0] nst, cst;
  36.   localparam [2:0]
  37.   IDLE = 0,
  38.   SOAK = 1,
  39.   WASH = 2,
  40.   RINSE = 3,
  41.   SPIN = 4,
  42.   DWASH = 5,
  43.   DRINSE = 6,
  44.   PAUSE = 7;
  45.   always@(posedge inf.clk or negedge inf.rst) begin
  46.     if(!inf.rst) cst <= IDLE;
  47.     else cst <= nst;
  48.   end
  49.  
  50.   always_comb begin
  51.     nst <= cst;
  52.     case(cst)
  53.       IDLE: if(inf.coin) nst <= SOAK;
  54.       SOAK: if(T) nst <= WASH;
  55.       WASH: if(T) nst <= RINSE;
  56.       RINSE: begin
  57.         if(inf.dwash & T) nst <= DWASH;
  58.         else if(!inf.dwash & T) nst <= SPIN;
  59.       end
  60.       SPIN: begin
  61.         if(inf.lid & !T) nst <= PAUSE;
  62.         else if(T) nst <= IDLE;
  63.       end
  64.       DWASH: if(T) nst <= DRINSE;
  65.       DRINSE: if(T) nst <= SPIN;
  66.       PAUSE: if(!inf.lid) nst <= SPIN;
  67.     endcase
  68.   end
  69.  
  70.   assign inf.soak = (cst == SOAK);
  71.   assign inf.wash = (cst == WASH) | (cst == DWASH);
  72.   assign inf.rinse = (cst == RINSE) | (cst == DRINSE);
  73.   assign inf.spin = (cst == SPIN);
  74.   assign pause = (cst == PAUSE);
  75.   assign inf.done = (cst == IDLE);
  76. endmodule
  77.        
  78. // Timer
  79.  
  80. module timer(
  81.   input clk, rst, pause,
  82.   output T
  83. );
  84.  
  85.   reg [9:0] count;
  86.  
  87.   always@(posedge clk or negedge rst) begin
  88.     if(!rst) count <= 10'd0;
  89.     else if(pause) count<= count;
  90.     else count <= count + 1'b1;
  91.   end
  92.  
  93.   assign T = (&count);
  94. endmodule
  95.  
  96.  
  97. // Top Level Controller
  98.  
  99. module washing_machine(
  100.   ifc.DESIGN inf
  101. );
  102.   wire pause, T;
  103.   wmc W1(.inf(inf), .T(T), .pause(pause));  
  104.   timer T1(.clk(inf.clk), .rst(inf.rst), .pause(pause), .T(T));
  105.  
  106. endmodule
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114. ///////////////////////program block
  115.  
  116.  
  117. // Code your testbench here
  118. // or browse Examples
  119. program tb_washing_machine(
  120.   ifc.TEST inf
  121. );
  122.  
  123.   virtual ifc.TEST inf_h = inf;
  124.    
  125.   initial begin
  126.     inf_h.rst = 0;
  127.     #12 inf_h.rst = 1;
  128.   end
  129.  
  130.   initial begin
  131.     wait(inf_h.rst);
  132.     inf_h.coin = 1'b1;
  133.     inf_h.dwash = 1'b1;
  134.    
  135.     #10 inf_h.coin = 1'b0;
  136.    
  137.     wait(inf_h.spin);
  138.     inf_h.lid = 1'b1;
  139.     #24 inf_h.lid = 1'b0;
  140.    
  141.     wait(inf_h.done);
  142.     $finish;
  143.    
  144.   end
  145.  
  146.   initial begin
  147.     $dumpfile("dump.vcd");
  148.     $dumpvars;  
  149.   end
  150. endprogram
  151.  
  152. module top_washing_machine;
  153.  
  154.   reg clk;
  155.   ifc inf(clk);
  156.   washing_machine DUT(.inf(inf.DESIGN));
  157.   tb_washing_machine TEST(.inf(inf.TEST));
  158.  
  159.   initial begin
  160.     wait(inf.rst);
  161.     clk = 0;
  162.     forever #5 clk = ~clk;
  163.   end
  164.  
  165. endmodule
  166.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement