|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
//// ////
|
|
//// ////
|
|
//// This file is part of the project ////
|
|
//// "instruction_list_pipelined_processor_with_peripherals" ////
|
|
//// ////
|
|
//// http://opencores.org/project,instruction_list_pipelined_processor_with_peripherals ////
|
|
//// ////
|
|
//// ////
|
|
//// Author: ////
|
|
//// - Mahesh Sukhdeo Palve ////
|
|
//// ////
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
//// ////
|
|
//// ////
|
|
//// ////
|
|
//// This source file may be used and distributed without ////
|
|
//// restriction provided that this copyright statement is not ////
|
|
//// removed from the file and that any derivative work contains ////
|
|
//// the original copyright notice and the associated disclaimer. ////
|
|
//// ////
|
|
//// This source file is free software; you can redistribute it ////
|
|
//// and/or modify it under the terms of the GNU Lesser General ////
|
|
//// Public License as published by the Free Software Foundation; ////
|
|
//// either version 2.1 of the License, or (at your option) any ////
|
|
//// later version. ////
|
|
//// ////
|
|
//// This source is distributed in the hope that it will be ////
|
|
//// useful, but WITHOUT ANY WARRANTY; without even the implied ////
|
|
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ////
|
|
//// PURPOSE. See the GNU Lesser General Public License for more ////
|
|
//// details. ////
|
|
//// ////
|
|
//// You should have received a copy of the GNU Lesser General ////
|
|
//// Public License along with this source; if not, download it ////
|
|
//// from http://www.opencores.org/lgpl.shtml ////
|
|
//// ////
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
`include "timescale.v"
|
`include "timescale.v"
|
`include "defines.v"
|
`include "defines.v"
|
|
|
|
|
module counter (clk, reset, preset, type, DN, CU, CD, ACC);
|
module counter (clk, reset, preset, type, DN, CU, CD, ACC);
|
|
|
input clk, reset;
|
input clk, reset;
|
input [`tcPresetLen-1:0] preset;
|
input [`tcPresetLen-1:0] preset;
|
input [`tcTypeLen-1:0] type;
|
input [`tcTypeLen-1:0] type;
|
|
|
output DN, CU, CD;
|
output DN, CU, CD;
|
output [`tcAccLen-1:0] ACC;
|
output [`tcAccLen-1:0] ACC;
|
|
|
reg DN = 0, CU = 0, CD = 0;
|
reg DN = 0, CU = 0, CD = 0;
|
reg [`tcAccLen-1:0] ACC = 0;
|
reg [`tcAccLen-1:0] ACC = 0;
|
|
|
reg [`tcTypeLen-1:0] CounterType;
|
reg [`tcTypeLen-1:0] CounterType;
|
reg [`tcTypeLen-1:0] typeNext;
|
reg [`tcTypeLen-1:0] typeNext;
|
|
|
|
|
parameter UpCounter = `tcTypeLen'b01;
|
parameter UpCounter = `tcTypeLen'b01;
|
parameter DownCounter = `tcTypeLen'b10;
|
parameter DownCounter = `tcTypeLen'b10;
|
parameter defaultType = `tcTypeLen'b00;
|
parameter defaultType = `tcTypeLen'b00;
|
|
|
|
|
|
|
always @ (type)
|
always @ (type)
|
begin
|
begin
|
|
|
case (type)
|
case (type)
|
|
|
`counterType1 : begin
|
`counterType1 : begin
|
typeNext = UpCounter;
|
typeNext = UpCounter;
|
end
|
end
|
|
|
`counterType2 : begin
|
`counterType2 : begin
|
typeNext = DownCounter;
|
typeNext = DownCounter;
|
end
|
end
|
|
|
default : begin
|
default : begin
|
$display ("\ncounter is of undefined type.\n Valid types are Up counter & Down counter");
|
$display ("\ncounter is of undefined type.\n Valid types are Up counter & Down counter");
|
end
|
end
|
endcase
|
endcase
|
end
|
end
|
|
|
|
|
always @ (posedge clk or posedge reset)
|
always @ (posedge clk or posedge reset)
|
begin
|
begin
|
|
|
if (reset)
|
if (reset)
|
begin
|
begin
|
$display ("counter module is reset");
|
$display ("counter module is reset");
|
CounterType = defaultType;
|
CounterType = defaultType;
|
end
|
end
|
else
|
else
|
begin
|
begin
|
CounterType = typeNext;
|
CounterType = typeNext;
|
end
|
end
|
end
|
end
|
|
|
|
|
always @ (posedge clk)
|
always @ (posedge clk)
|
begin
|
begin
|
|
|
case (CounterType)
|
case (CounterType)
|
|
|
UpCounter : begin
|
UpCounter : begin
|
CD = 0; // CD id always 0 for this state
|
CD = 0; // CD id always 0 for this state
|
|
|
if (reset)
|
if (reset)
|
begin
|
begin
|
ACC = `tcAccLen-1'b0; // starts at lowest value
|
ACC = `tcAccLen-1'b0; // starts at lowest value
|
CU = 0;
|
CU = 0;
|
DN = 0;
|
DN = 0;
|
end
|
end
|
else
|
else
|
begin
|
begin
|
ACC = ACC + 1'b1;
|
ACC = ACC + 1'b1;
|
CU = 1'b1;
|
CU = 1'b1;
|
if (ACC > preset)
|
if (ACC > preset)
|
begin
|
begin
|
DN = 1'b1;
|
DN = 1'b1;
|
end
|
end
|
end
|
end
|
end
|
end
|
|
|
|
|
|
|
DownCounter : begin
|
DownCounter : begin
|
CU = 0; // CU id always 0 for this state
|
CU = 0; // CU id always 0 for this state
|
|
|
if (reset)
|
if (reset)
|
begin
|
begin
|
ACC = `tcAccLen-1'b1; // starts at highest value
|
ACC = `tcAccLen-1'b1; // starts at highest value
|
CD = 0;
|
CD = 0;
|
DN = 0;
|
DN = 0;
|
end
|
end
|
else
|
else
|
begin
|
begin
|
ACC = ACC - 1'b1;
|
ACC = ACC - 1'b1;
|
CD = 1'b1;
|
CD = 1'b1;
|
if (ACC < preset)
|
if (ACC < preset)
|
begin
|
begin
|
DN = 1'b1;
|
DN = 1'b1;
|
end
|
end
|
end
|
end
|
end
|
end
|
|
|
|
|
|
|
default : begin
|
default : begin
|
$display ("\nerror in counter type ");
|
$display ("\nerror in counter type ");
|
end
|
end
|
|
|
endcase
|
endcase
|
end
|
end
|
|
|
|
|
endmodule
|
endmodule
|
|
|