Advertisement
nelson33

Untitled

Sep 22nd, 2023
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VeriLog 4.72 KB | Source Code | 0 0
  1. `timescale 1ns/1ps
  2. module Parameterized_Ping_Pong_Counter (
  3.     input clk,
  4.     input rst_n,
  5.     input enable,
  6.     input flip,
  7.     input [3:0] max,
  8.     input [3:0] min,
  9.     output reg direction,
  10.     output reg [3:0] out
  11. );
  12.  
  13. reg [3:0] pre_out;
  14.  
  15. always @(posedge clk or negedge rst_n) begin
  16.     pre_out = out;
  17.     if (!rst_n) begin
  18.         out <= min;
  19.         direction <= 1'b1;
  20.     end
  21.     else begin
  22.         if (enable && (pre_out <= max && pre_out >= min) && max > min) begin
  23.             if (flip) begin
  24.                 direction = !direction;
  25.             end
  26.             if (pre_out == max) begin
  27.                 direction = 1'b0;
  28.             end
  29.             else if (pre_out == min) begin
  30.                 direction = 1'b1;
  31.             end
  32.             if (direction == 1'b1) begin
  33.                 out <= pre_out + 1'b1;
  34.             end
  35.             else begin
  36.                 out <= pre_out - 1'b1;
  37.             end
  38.         end
  39.     end
  40. end
  41.  
  42. endmodule
  43.  
  44. // Output signals can be reg or wire
  45. // add your design here
  46. /*
  47. reg [3:0] next_out;
  48. reg [3:0] pre_out;
  49. reg just_reset = 1'b0;
  50.  
  51. always @(*) begin
  52.     if (enable) begin
  53.         if (max == min) begin
  54.             next_out = out;
  55.         end
  56.         if (max - min == 1) begin
  57.             if (out == max) begin
  58.                 next_out = out - 1;
  59.                 direction = 1'b1;
  60.             end
  61.             else begin
  62.                 next_out = out + 1;
  63.                 direction = 1'b0;
  64.             end
  65.         end
  66.         else begin
  67.             if (out == max) begin
  68.                 next_out = out - 1;
  69.             end
  70.             else if (out == min) begin
  71.                 next_out = out + 1;
  72.             end
  73.             else if (pre_out == max || pre_out == min) begin
  74.                 if (just_reset) begin
  75.                     if (direction) begin
  76.                         next_out = out + 1;
  77.                     end
  78.                     else begin
  79.                         next_out = out - 1;
  80.                     end
  81.                     just_reset = 0;
  82.                 end
  83.                 else begin
  84.                     direction = !direction;
  85.                     if (direction) begin
  86.                         next_out = out + 1;
  87.                     end
  88.                     else begin
  89.                         next_out = out - 1;
  90.                     end
  91.                 end
  92.             end
  93.             else begin
  94.                 if (direction) begin
  95.                     next_out = out + 1;
  96.                 end
  97.                 else begin
  98.                     next_out = out - 1;
  99.                 end
  100.             end
  101.         end
  102.     end
  103. end
  104.  
  105. always @(posedge clk or negedge rst_n) begin
  106.     if (~rst_n) begin
  107.         out <= min;
  108.         direction <= 1'b1;
  109.         just_reset = 1'b1;
  110.     end else begin
  111.         if (enable) begin
  112.             if (flip) begin
  113.                 if (direction) begin
  114.                     direction <= 1'b0;
  115.                 end
  116.                 else begin
  117.                     direction <= 1'b1;
  118.                 end
  119.             end
  120.             out <= next_out;
  121.             pre_out <= out;
  122.         end
  123.     end
  124. end
  125.  
  126. always @(negedge clk) begin
  127.     next_out_value <= next_out;
  128.     out_value = out;
  129. end
  130. */
  131.  
  132. /*
  133.         if (direction) begin
  134.             if (out < max) begin
  135.                 next_out = out + 1'b1;
  136.             end
  137.             if (out == max) begin
  138.                 direction = 1'b0;
  139.             end
  140.         end
  141.         else begin
  142.             if (out > min) begin
  143.                 next_out = out - 1'b1;
  144.             end
  145.             if (out == min) begin
  146.                 direction = 1'b1;
  147.             end
  148.         end
  149. */
  150.  
  151. /*
  152.             if (direction == 1'b1) begin
  153.                 if (count == 1'b0) begin
  154.                     if (out == max) begin
  155.                         next_out = out - 1'b1;
  156.                         count = 1'b1;
  157.                     end
  158.                     else begin
  159.                         next_out = out + 1'b1;
  160.                     end
  161.                 end
  162.                 else begin
  163.                     next_out = out - 1'b1;
  164.                     direction = 1'b0;
  165.                     count = 1'b0;
  166.                 end
  167.             end
  168.             else begin
  169.                 if (count == 1'b0) begin
  170.                     if (out == min) begin
  171.                         next_out = out + 1'b1;
  172.                         count = 1'b1;
  173.                     end
  174.                     else begin
  175.                         next_out = out - 1'b1;
  176.                     end
  177.                 end
  178.                 else begin
  179.                     next_out = out + 1'b1;
  180.                     direction = 1'b1;
  181.                     count= 1'b0;
  182.                 end
  183.             end
  184. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement