Advertisement
Guest User

Untitled

a guest
Apr 11th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. // CountDown Timer
  3. //
  4.  
  5.  
  6. module testbench2();
  7.  
  8.     // Device under test I/O
  9.     logic clk, reset;
  10.     logic [4:0] therapyDuration;
  11.     logic [5:0] SEC;
  12.     logic [4:0] MIN;
  13.  
  14.     //device under test
  15.     countDownTimer cdt(clk, reset, en, therapyDuration, SEC, MIN);
  16.  
  17.     initial
  18.         forever begin
  19.             clk=0; #2; clk=1; #2;
  20.         end
  21.  
  22.     initial begin
  23.         therapyDuration = 5'd30;
  24.         reset = 1;
  25.         #30;
  26.         reset = 0;
  27.         #35000;
  28.     end
  29.  
  30. endmodule
  31.  
  32.  
  33. module timer(input logic clk, reset,
  34.              input logic [4:0] therapyduration;
  35.              output logic [5:0] therapysec,
  36.              output logic [4:0] therapymin,
  37.              output logic [5:0] pausesec,
  38.              output logic [4:0] pausemin);
  39.  
  40.     // Pause duration of 10 minutes.
  41.     pauseduration = 5'd10;
  42.  
  43.     countDownTimer therapytimer(clk, reset, count_en, therapyduration, therapysec, therapymin, COUNT_END);
  44.     countDownTimer pausetimer(clk, reset|pause_reset, pause_en, pauseduration, pausesec, pausemin, PAUSE_END);
  45.     countDownTimer_FSM fsm(SWITCH, STOP, COUNT_END, PAUSE, PAUSE_END, count_en, pause_en, pause_reset);
  46.  
  47. endmodule
  48.  
  49. module countDownTimer(input logic clk, reset, en,
  50.                       input logic [4:0] therapyDuration,
  51.                       output logic [5:0] SEC,
  52.                       output logic [4:0] MIN
  53.                       output logic END);
  54.    
  55.     //CLK frequency 4Hz
  56.    
  57.     //MAIN CLK COUNTER //
  58.     logic rst_countMainClk;
  59.     logic [31:0] countClk;
  60.     counter32bit countMainClk(clk, rst_countMainClk, en, countClk);
  61.     assign rst_countMainClk = reset | (countClk == 32'd3); //clk_f - 1 = 4-1
  62.     //assign rst_countMainClk = reset | (countClk == 32'd49_999_999)
  63.  
  64.     //SECOND TIMER //
  65.     logic enSEC;
  66.     assign enSEC = (countClk== 32'd3);
  67.     //assign enSEC = (countClk== 32'd49_999_999);
  68.  
  69.     always_ff @(posedge clk)
  70.         if(reset) SEC <= 6'd0;
  71.         else if(enSEC)
  72.         begin
  73.             if( (SEC == 6'd0) ) SEC <= 6'd59;
  74.             else                      SEC <= SEC - 1'b1;           
  75.         end
  76.  
  77.     //MINUTE TIMER //
  78.     logic enMIN;
  79.     assign enMIN = (SEC == 6'd0) & enSEC;
  80.    
  81.     always_ff @(posedge clk)
  82.         if(reset)        MIN <= therapyDuration;
  83.         else if(enMIN)  MIN <= MIN - 1'b1;
  84.  
  85.     assign END = (enSEC == 5'b0 & enMIN == 4'b0);
  86.  
  87. endmodule
  88.  
  89.  
  90.  
  91. countDownTimer_FSM(input logic SWITCH, STOP, COUNT_END, PAUSE, PAUSE_END,
  92.                          output logic count_en, pause_en, pause_reset);
  93.        
  94.     typedef enum logic [1:0] {S0,S1,S2,S3} statetype;
  95.     statetype state, nextstate;
  96.    
  97.     //state register
  98.     always_ff @(posedge clk, posedge reset)
  99.         if(reset) state <= S0;          //reset to IDLE state
  100.         else      state <= nextstate;
  101.    
  102.     //next state logic
  103.     always_comb
  104.         case(state)
  105.             //IDLE STATE
  106.             S0: if(SWITCH) nextstate = S1;
  107.                  else           nextstate = S0;
  108.             //COUNT STATE
  109.             S1: if(PAUSE)                   nextstate = S2;
  110.                  else if(STOP | COUNT_END) nextstate = S3;
  111.                  else                           neststate = S1;
  112.             //PAUSE STATE
  113.             S2: if(SWITCH)                  nextstate = S1;
  114.                  else if(STOP | PAUSE_END) nextstate = S3
  115.                  else                               nextstate = S2;
  116.             //END STATE
  117.             S3: nextstate = S0;
  118.            
  119.             default: nextstate = S0;
  120.         endcase
  121.    
  122.     //output logic
  123.     // Countdown therapy timer only when in COUNT state.
  124.     assign count_en = (state == S1);
  125.     // Countdown pause timer only when in PAUSE state.
  126.     assign pause_en = (state == S2);
  127.     // Reset pause if not in PAUSE state.
  128.     assign pause_reset = ~pause_en;
  129.  
  130.     // assign IDLE_state = (state == S0);
  131.     // assign COUNT_state = (state == S1);
  132.     // assign PAUSE_state = (state == S2);  
  133.     // assign END_state = (state == S3);
  134.                          
  135. endmodule
  136.  
  137.  
  138.  
  139. module counter32bit(input logic clk, reset, en,
  140.                           output logic [31:0] q);
  141.    
  142.     always_ff @(posedge clk)
  143.         if(reset)   q <= 0;
  144.         else if(en) q <= q + 1'b1;
  145.  
  146. endmodule
  147.  
  148.  
  149. /*
  150.         R                  
  151. clk     0   1   2   3   4   5   6   1   2   3   4   5   6   1  
  152. count   0   0   1   2   3   4   0   0   1   2   3   4   0   0
  153. EN                              EN                      EN     
  154. reset                           r                       r
  155. */
  156.  
  157. /*
  158.  
  159. 50MHz = 50,000,000 clk cycles
  160. en  = (count == 49,999,998)
  161. rst = (count == 49,999,999)
  162.  
  163.  
  164. 6Hz = 6 clk cycles
  165. en  = (count == 4) or (count == clkcyles-2)
  166. rst = (count == 5) or (count == clkcyles-1)
  167.  
  168. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement