summaryrefslogtreecommitdiff
path: root/nandgame/counter.sv
blob: 78f054efe8d421527d8782e24de695b6e250533e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// nandgame counter

`timescale 1us/1us

module  counter #(
  parameter DATA_WIDTH = 16
) (
  input [(DATA_WIDTH-1):0] X,
  input wire st,
  // probably opposed to nandgame,
  // which "stores" on rising clock
  // and "outputs" on falling clock,
  // use "regular" stuff??? 
  input wire cl,
  output wire [(DATA_WIDTH-1):0] count
);


/*
A counter component increments a 16-bit number for each clock cycle.

If st (store) is 1, then the input value X is used as the new counter value.

If st is 0, then the previous counter value is incremented by 1.

The counter output changes when cl (clock signal) changes to 0.
Input	Effect
st	cl	
0	0	set next to output + 1
1	0	set next to X

output is the current output of the component. next becomes the current output when cl changes to 0.
*/

reg [(DATA_WIDTH-1):0] r_ctr;

initial begin
  r_ctr = 0;
end

always @(negedge cl) begin
  if (st)
    r_ctr <= X;
  else
    r_ctr <= r_ctr + 1;
end

assign count = r_ctr;

endmodule