Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- `timescale 1ns/1ps
- module Parameterized_Ping_Pong_Counter (
- input clk,
- input rst_n,
- input enable,
- input flip,
- input [3:0] max,
- input [3:0] min,
- output reg direction,
- output reg [3:0] out
- );
- reg [3:0] pre_out;
- always @(posedge clk or negedge rst_n) begin
- pre_out = out;
- if (!rst_n) begin
- out <= min;
- direction <= 1'b1;
- end
- else begin
- if (enable && (pre_out <= max && pre_out >= min) && max > min) begin
- if (flip) begin
- direction = !direction;
- end
- if (pre_out == max) begin
- direction = 1'b0;
- end
- else if (pre_out == min) begin
- direction = 1'b1;
- end
- if (direction == 1'b1) begin
- out <= pre_out + 1'b1;
- end
- else begin
- out <= pre_out - 1'b1;
- end
- end
- end
- end
- endmodule
- // Output signals can be reg or wire
- // add your design here
- /*
- reg [3:0] next_out;
- reg [3:0] pre_out;
- reg just_reset = 1'b0;
- always @(*) begin
- if (enable) begin
- if (max == min) begin
- next_out = out;
- end
- if (max - min == 1) begin
- if (out == max) begin
- next_out = out - 1;
- direction = 1'b1;
- end
- else begin
- next_out = out + 1;
- direction = 1'b0;
- end
- end
- else begin
- if (out == max) begin
- next_out = out - 1;
- end
- else if (out == min) begin
- next_out = out + 1;
- end
- else if (pre_out == max || pre_out == min) begin
- if (just_reset) begin
- if (direction) begin
- next_out = out + 1;
- end
- else begin
- next_out = out - 1;
- end
- just_reset = 0;
- end
- else begin
- direction = !direction;
- if (direction) begin
- next_out = out + 1;
- end
- else begin
- next_out = out - 1;
- end
- end
- end
- else begin
- if (direction) begin
- next_out = out + 1;
- end
- else begin
- next_out = out - 1;
- end
- end
- end
- end
- end
- always @(posedge clk or negedge rst_n) begin
- if (~rst_n) begin
- out <= min;
- direction <= 1'b1;
- just_reset = 1'b1;
- end else begin
- if (enable) begin
- if (flip) begin
- if (direction) begin
- direction <= 1'b0;
- end
- else begin
- direction <= 1'b1;
- end
- end
- out <= next_out;
- pre_out <= out;
- end
- end
- end
- always @(negedge clk) begin
- next_out_value <= next_out;
- out_value = out;
- end
- */
- /*
- if (direction) begin
- if (out < max) begin
- next_out = out + 1'b1;
- end
- if (out == max) begin
- direction = 1'b0;
- end
- end
- else begin
- if (out > min) begin
- next_out = out - 1'b1;
- end
- if (out == min) begin
- direction = 1'b1;
- end
- end
- */
- /*
- if (direction == 1'b1) begin
- if (count == 1'b0) begin
- if (out == max) begin
- next_out = out - 1'b1;
- count = 1'b1;
- end
- else begin
- next_out = out + 1'b1;
- end
- end
- else begin
- next_out = out - 1'b1;
- direction = 1'b0;
- count = 1'b0;
- end
- end
- else begin
- if (count == 1'b0) begin
- if (out == min) begin
- next_out = out + 1'b1;
- count = 1'b1;
- end
- else begin
- next_out = out - 1'b1;
- end
- end
- else begin
- next_out = out + 1'b1;
- direction = 1'b1;
- count= 1'b0;
- end
- end
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement