From a7870ac9de65055a612623dc2b4fae9e12382d67 Mon Sep 17 00:00:00 2001 From: "Andreas.Olofsson" Date: Wed, 22 Apr 2020 23:15:12 -0400 Subject: [PATCH] Cleaned up counter -Not functional --- common/hdl/oh_counter.v | 71 ++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 47 deletions(-) diff --git a/common/hdl/oh_counter.v b/common/hdl/oh_counter.v index c405735..e13c7f2 100644 --- a/common/hdl/oh_counter.v +++ b/common/hdl/oh_counter.v @@ -1,68 +1,45 @@ //############################################################################# -//# Function: Binary to gray encoder # +//# Function: Generic counter # //############################################################################# //# Author: Andreas Olofsson # //# License: MIT (see LICENSE file in OH! repository) # //############################################################################# -module oh_counter #(parameter DW = 32, // width of data inputs - parameter TYPE = "INCREMENT" // also DECREMENT, GRAY, LFSR +module oh_counter #(parameter DW = 32 // width of data inputs ) ( - input clk, // clk input - input in, // input to count - input en, // enable counter - input load, // load counter - input [DW-1:0] load_data,// load data - output [DW-1:0] count, // current count value - output carry, // carry out from counter - output zero // counter is zero + //inputs + input clk, // clk input + input in, // input to count + input en, // enable counter + input dir,//0=increment, 1=decrement + input autowrap, //auto wrap around + input load, // load counter + input [DW-1:0] load_data, // input data to load + //outputs + output [DW-1:0] count, // count value + output wraparund // wraparound indicator ); // local variables reg [DW-1:0] count; - reg carry; wire [DW-1:0] count_in; - wire carry_in; - // configure counter based on type - generate - if(TYPE=="INCREMENT") - begin - assign {carry_in,count_in[DW-1:0]} = count[DW-1:0] + in; - end - else if(TYPE=="DECREMENT") - begin - assign count_in[DW-1:0] = count[DW-1:0] + in; - end - else if (TYPE=="GRAY") - begin - initial - $display ("NOT IMPLEMENTED"); - end - else if (TYPE=="LFSR") - begin - initial - $display ("NOT IMPLEMENTED"); - end - endgenerate - + //Select count direction + assign count_in[DW-1:0] = dir ? count[DW-1:0] - in : + count[DW-1:0] + in ; + // counter always @(posedge clk) if(load) - begin - carry <= 1'b0; - count[DW-1:0] <= load_data[DW-1:0]; - end - else if (en) - begin - carry <= carry_in; - count[DW-1:0] <= count_in[DW-1:0]; - end - + count[DW-1:0] <= load_data[DW-1:0]; + else if (en & ~(wraparound & ~autowrap)) + count[DW-1:0] <= count_in[DW-1:0]; + // counter expired - assign zero = ~(count[DW-1:0]); - + assign wraparound = (dir & en & ~(|count[DW-1:0])) | + (~dir & en & (&count[DW-1:0])); + endmodule // oh_counter