CAM 的SV实现

在这里插入图片描述
在这里插入图片描述

// If an update is performed to the same address as a lookup in the same clock cycle ?
module cam #(
    parameter NUM_ENTRIES = 2,
    parameter KEY_WIDTH   = 32,
    parameter INDEX_WIDTH = $clog2(NUM_ENTRIES)
) (
    input                            clk,
    input                            reset,

    // Lookup interface
    input        [KEY_WIDTH   - 1:0] lookup_key,
    output logic [INDEX_WIDTH - 1:0] lookup_idx,
    output logic                     lookup_hit,

    // Update interface
    input                            update_en,
    input [KEY_WIDTH   - 1:0]        update_key,
    input [INDEX_WIDTH - 1:0]        update_idx,
    input                            update_valid
);

    logic [KEY_WIDTH   - 1:0] lookup_table [NUM_ENTRIES - 1:0];
    logic [NUM_ENTRIES - 1:0] entry_valid;

    logic [NUM_ENTRIES - 1:0] hit_oh;

    // lookup
    for (genvar test_index = 0; test_index < NUM_ENTRIES; test_index++)
    begin : lookup_gen
        assign hit_oh[test_index] = entry_valid[test_index]
            && lookup_table[test_index] == lookup_key;
    end

    assign lookup_hit = |hit_oh;

    oh_to_idx #(.NUM_SIGNALS(NUM_ENTRIES)) oh_to_idx_hit (
        .one_hot (hit_oh),
        .index   (lookup_idx)
    );

    // update
    always_ff @(posedge clk, posedge reset) begin
        if (reset) begin
            entry_valid <= '0;
        end else if (update_en) begin
            assert($onehot0(hit_oh));
            entry_valid[update_idx] <= update_valid;
        end
    end

    always_ff @(posedge clk) begin
        if (update_en)
            lookup_table[update_idx] <= update_key;
    end

`ifdef SIMULATION
    // Check for duplicate entries
    always_ff @(posedge clk, posedge reset) begin
        if (!reset && update_en && update_valid) begin : test
            for (int i = 0; i < NUM_ENTRIES; i++) begin
                if (entry_valid[i] && lookup_table[i] == update_key
                    && INDEX_WIDTH'(i) != update_idx)
                begin
                    $display("%m: added duplicate entry to CAM");
                    $display("  original slot %d new slot %d", i, update_idx);
                    $finish;
                end
            end
        end
    end
`endif

endmodule
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值