<commit_before>/*
 * regfile.sv
 */

import riscv::addr_t;
import riscv::word_t;

/**
 * Module: regfile
 *
 * A register file.
 */
module regfile (
    input  logic  clk,

    input  addr_t raddr1,
    output word_t rdata1,

    input  addr_t raddr2,
    output word_t rdata2,

    input  logic  wen,
    input  addr_t waddr,
    input  word_t wdata
);

    localparam ADDR_WIDTH = $bits(addr_t);

    word_t regs [0:2**ADDR_WIDTH-2];

`ifndef SYNTHESIS
    initial
        for (int i = 0; i < 2**ADDR_WIDTH-1; i = i + 1)
            regs[i] = 0;
`endif

    logic rzero1 = raddr1 == 'b0;
    logic rzero2 = raddr2 == 'b0;
    logic wzero  = waddr  == 'b0;

    always @(negedge clk)
        if (wen && ~wzero)
            regs[waddr - 1] <= wdata;

    assign rdata1 = rzero1 ? 'b0 : regs[raddr1 - 1];
    assign rdata2 = rzero2 ? 'b0 : regs[raddr2 - 1];

endmodule<commit_msg>regfile: simplify and prevent out of range access<commit_after>/*
 * regfile.sv
 */

import riscv::addr_t;
import riscv::word_t;

/**
 * Module: regfile
 *
 * A register file.
 */
module regfile (
    input  logic  clk,

    input  addr_t raddr1,
    output word_t rdata1,

    input  addr_t raddr2,
    output word_t rdata2,

    input  logic  wen,
    input  addr_t waddr,
    input  word_t wdata
);

    word_t regs [0:2**$bits(addr_t)-1];

`ifndef SYNTHESIS
    initial
        for (int i = 0; i < 2**$bits(addr_t)-1; i = i + 1)
            regs[i] = 0;
`endif

    always @(negedge clk)
        if (wen && waddr != 'd0)
            regs[waddr] <= wdata;

    assign rdata1 = raddr1 == 'd0 ? 'd0 : regs[raddr1];
    assign rdata2 = raddr2 == 'd0 ? 'd0 : regs[raddr2];

endmodule
{'fraction_non_alphanumeric': 0.12642986152919927, 'fraction_numerical': 0.03190848886213125, 'mean_word_length': 2.304174950298211, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1403195', 'n_tokens_mistral': 694, 'n_tokens_neox': 667, 'n_words': 175}