summaryrefslogtreecommitdiff
path: root/nandgame/comb_mem.sv
blob: b68fc22d0dfa94a7c046ffb06935627b8c9fa0ff (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// nandgame "combined memory"
// contains registers A, D and *A result
// located in memory.

`timescale 1us/1us

`include "../playground/my_mem.v"

`ifndef COMB_MEM
`define COMB_MEM

module comb_mem #(
  parameter DATA_WIDTH = 16
) (
    // store to A register
    input store_to_a_in,
    // store to D register
    input store_to_d_in,
    // store to address in memory pointed to by A (currently)
    input store_to_pa_in,

    // value to store
    input [(DATA_WIDTH-1):0] X_in,
    // output registers updated on falling edge
    input wire clk_in,

    // content of A register
    output reg [(DATA_WIDTH-1):0] reg_A_out,
    // content of D register
    output reg [(DATA_WIDTH-1):0] reg_D_out,
    // content memory pointed to by A register
    output reg [(DATA_WIDTH-1):0] reg_pA_out
);

wire inv_clk_int;
// my hw uses posedge, nandgame uses negedge.
assign inv_clk_int = ~clk_in;

my_mem #(
    .DATA_WIDTH(DATA_WIDTH),
    .DATA_DEPTH(65536)
) nand_memory (
    .clk_i(inv_clk_int),
    .write_en_i(store_to_pa_in),
    .read_en_i(1'b1),
    .r_read_addr(reg_A_out),
    .r_write_addr(reg_A_out),
    .data_o(),
    .async_data_o(reg_pA_out),
    .data_i(X_in)
);

initial begin
    reg_A_out = 0;
    reg_D_out = 0;
end

always @(negedge clk_in) begin
    if (store_to_a_in)
        reg_A_out <= X_in;

    if (store_to_d_in)
        reg_D_out <= X_in;
end

endmodule

`endif