summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--nandgame/computer.sv3
-rw-r--r--nandgame/counter.sv8
-rw-r--r--nandgame/counter_tb.sv22
3 files changed, 27 insertions, 6 deletions
diff --git a/nandgame/computer.sv b/nandgame/computer.sv
index 3286bfe..3794270 100644
--- a/nandgame/computer.sv
+++ b/nandgame/computer.sv
@@ -80,7 +80,8 @@ counter PC (
.clk_in(clk_in),
.counter_out(PC_addr_int),
.X_in(reg_A_int),
- .st_store_X_in(cpu_do_jump_int)
+ .st_store_X_in(cpu_do_jump_int),
+ .count_enable_in('b1)
);
`ifdef VERILATOR
diff --git a/nandgame/counter.sv b/nandgame/counter.sv
index 2089194..87ecdd7 100644
--- a/nandgame/counter.sv
+++ b/nandgame/counter.sv
@@ -1,11 +1,12 @@
// nandgame counter
+// counts on *falling* edge
`timescale 1us/1us
`ifndef NANDGAME_COUNTER
`define NANDGAME_COUNTER
-module counter #(
+module counter #(
parameter DATA_WIDTH = 16
) (
// input / value to store in counter
@@ -14,6 +15,9 @@ module counter #(
input wire st_store_X_in,
// clock
input wire clk_in,
+ // active high, whether to count
+ // st_store_X_in overrides this in any case.
+ input wire count_enable_in,
// counter value
output wire [(DATA_WIDTH-1):0] counter_out
@@ -45,7 +49,7 @@ end
always @(negedge clk_in) begin
if (st_store_X_in)
counter_int <= X_in;
- else
+ else if (count_enable_in)
counter_int <= counter_int + 1;
end
diff --git a/nandgame/counter_tb.sv b/nandgame/counter_tb.sv
index 1603a31..b725475 100644
--- a/nandgame/counter_tb.sv
+++ b/nandgame/counter_tb.sv
@@ -2,14 +2,15 @@
module counter_tb;
-logic tst_clk, tst_store_X;
+logic tst_clk, tst_store_X, tst_count;
logic [15:0] tst_in_X, tst_result;
counter uut (
.clk_in(tst_clk),
.X_in(tst_in_X),
.counter_out(tst_result),
- .st_store_X_in(tst_store_X)
+ .st_store_X_in(tst_store_X),
+ .count_enable_in(tst_count)
);
string filename;
@@ -23,12 +24,23 @@ initial begin
tst_clk = 0;
tst_store_X = 0;
tst_in_X = 0;
+ tst_count = 0;
end
always #10 tst_clk = ~tst_clk;
initial begin
- #200
+ // start of with count disabled
+ repeat(5) @(posedge tst_clk);
+ assert (tst_result == 0)
+ else $error("Expected 0, got %d", tst_result);
+ tst_count = 1;
+
+ // now that counting is enabled
+ repeat(5) @(posedge tst_clk);
+ assert (tst_result == 5)
+ else $error("Expected 5, got %d", tst_result);
+
@(posedge tst_clk);
tst_store_X = 1;
tst_in_X = 16'd834;
@@ -36,8 +48,12 @@ initial begin
@(posedge tst_clk);
assert (tst_result == 834)
else $error("Expected 834, got %d", tst_result);
+ // this shouldn't matter
+ tst_count = !tst_count;
end
tst_store_X = 0;
+ tst_count = 1;
+
@(posedge tst_clk);
assert (tst_result == 835)
else $error("Expected 835, got %d", tst_result);