mirror of
https://github.com/SikongJueluo/Mini-Nav.git
synced 2026-07-12 20:15:31 +08:00
refactor(cam): remove read noise from noise architecture (Phase 2)
- Make cam_read_noise a pass-through module, removing all noise injection logic - Switch write noise to use noise_mask_bernoulli instead of noise_mask_grouped - Add state machine to cam_write_noise for mask generation timing - Remove noise_mask_grouped.sv (no longer needed) - Remove read noise parameters from cam_noisy and cam_top - Update simulation and benchmark code to reflect read noise removal - Sync documentation to reflect Phase 2 architecture
This commit is contained in:
@@ -10,7 +10,6 @@ VERILOG_SOURCES := $(RTL_CAM_TOP)
|
||||
WRITE_NOISE_EN := 1
|
||||
WRITE_NOISE_RATE_NUM := 1
|
||||
WRITE_NOISE_RATE_DEN := 100
|
||||
READ_NOISE_EN := 0
|
||||
|
||||
include $(SIM_ROOT)/mk/cocotb-common.mk
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
"""
|
||||
CAM 写入噪声(Write Noise)集成测试 —— 专用配置。
|
||||
|
||||
本文件测试 WRITE_NOISE_EN=1, READ_NOISE_EN=0 配置下,
|
||||
本文件测试 WRITE_NOISE_EN=1 配置下,
|
||||
写入噪声模块的正确性。默认噪声率约 1%(NUM=1, DEN=100)。
|
||||
|
||||
=== 测试列表 ===
|
||||
@@ -14,7 +14,7 @@ CAM 写入噪声(Write Noise)集成测试 —— 专用配置。
|
||||
|
||||
=== 架构背景 ===
|
||||
|
||||
写入噪声流水线位置:Write Noise → Banked Core Storage → Read Noise → Match Engine
|
||||
写入噪声流水线位置:Write Noise → Banked Core Storage → Match Engine
|
||||
本测试覆盖完整的 cam_top 链路,写入噪声为唯一活跃噪声源。
|
||||
|
||||
=== Makefile 子目标 ===
|
||||
@@ -30,7 +30,6 @@ import numpy as np
|
||||
from cocotb.clock import Clock
|
||||
from cocotb.triggers import RisingEdge
|
||||
from model.ref_model import (
|
||||
generate_write_flip_mask,
|
||||
match_top1,
|
||||
random_hashes,
|
||||
)
|
||||
@@ -89,71 +88,7 @@ async def default_noise_reproducible(dut):
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════════════
|
||||
# 测试 2:精确 RTL-vs-模型 PRNG 掩码匹配
|
||||
# ── RTL 存储的哈希与 ref_model.py 生成的掩码逐位一致
|
||||
# ═══════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
|
||||
@cocotb.test()
|
||||
async def exact_noise_model_match(dut):
|
||||
"""精确噪声模型匹配:RTL 的 PRNG 输出必须与 Python 参考模型逐位一致。
|
||||
|
||||
测试方法:
|
||||
1. 用固定 RTL seed 和已知噪声参数,在 Python 中预计算每行的 flip 掩码
|
||||
2. 预期存储值 = 原始值 XOR flip_mask
|
||||
3. 写入原始值到 RTL,查询预期存储值
|
||||
4. 断言每行 score = HASH_BITS(完全匹配)
|
||||
|
||||
这验证了 RTL 的 LFSR 实现与 Python 模型的 PRNG 使用相同的
|
||||
多项式、相同的位宽、相同的种子初始化序列。
|
||||
"""
|
||||
if not hasattr(dut, "score_debug_flat"):
|
||||
dut._log.info("Skipping exact_noise_model_match: requires SIM_DEBUG.")
|
||||
return
|
||||
|
||||
rtol = None
|
||||
atol = None
|
||||
cocotb.start_soon(Clock(dut.clk, 10, unit="ns").start())
|
||||
await reset_dut(dut)
|
||||
|
||||
hash_bits = dut_hash_bits(dut)
|
||||
noise_bits = get_param(dut, "WRITE_NOISE_BITS", 8)
|
||||
rate_num = get_param(dut, "WRITE_NOISE_RATE_NUM", 1)
|
||||
rate_den = get_param(dut, "WRITE_NOISE_RATE_DEN", 100)
|
||||
|
||||
n_test_rows = 4
|
||||
rng = np.random.default_rng(99)
|
||||
rows = random_hashes(rng, n_test_rows, width=hash_bits)
|
||||
|
||||
RTL_SEED = 0xB504_F32D_B504_F32D
|
||||
prng_state = (RTL_SEED << 64) | RTL_SEED
|
||||
expected_stored = []
|
||||
for row in rows:
|
||||
flip, prng_state = generate_write_flip_mask(
|
||||
prng_state,
|
||||
hash_bits,
|
||||
noise_bits,
|
||||
rate_num,
|
||||
rate_den,
|
||||
)
|
||||
expected_stored.append(row ^ flip)
|
||||
|
||||
for idx, val in enumerate(rows):
|
||||
await write_row(dut, idx, val)
|
||||
|
||||
for idx, expected in enumerate(expected_stored):
|
||||
top1_index, top1_score, score_debug = await query_once(dut, expected)
|
||||
assert score_debug is not None, (
|
||||
"score_debug required for mask match verification"
|
||||
)
|
||||
assert int(score_debug[idx]) == hash_bits, (
|
||||
f"Row {idx}: expected stored hash to match model prediction, "
|
||||
f"score={score_debug[idx]} != {hash_bits}"
|
||||
)
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════════════
|
||||
# 测试 3:零噪声率(WRITE_NOISE_EN=1, RATE_NUM=0)
|
||||
# 测试 2:零噪声率(WRITE_NOISE_EN=1, RATE_NUM=0)
|
||||
# ── 噪声模块已连接但翻转概率为 0 → 行为应与无噪声一致
|
||||
# ═══════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
@@ -195,80 +130,4 @@ async def zero_rate_noise(dut):
|
||||
assert np.array_equal(score_debug, expected.scores)
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════════════
|
||||
# 测试 4:100% 噪声率(RATE_NUM=1, RATE_DEN=1)
|
||||
# ── 每组都翻转 → 精确验证 PRNG 掩码生成
|
||||
# ═══════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
|
||||
@cocotb.test()
|
||||
async def full_rate_noise(dut):
|
||||
"""完全速率噪声:每组 100% 翻转概率。
|
||||
|
||||
使用固定 RTL seed (0xB504F32DB504F32D),用 Python 模型预计算
|
||||
写入全 0 和全 1 行后应存储的哈希值,然后验证 RTL 实际存储的哈希
|
||||
与模型预测完全一致。
|
||||
|
||||
这是最低容忍度的噪声测试——要求 score_debug_flat(SIM_DEBUG)
|
||||
且每行的分数必须精确等于 HASH_BITS。
|
||||
"""
|
||||
rate_num = get_param(dut, "WRITE_NOISE_RATE_NUM", 1)
|
||||
rate_den = get_param(dut, "WRITE_NOISE_RATE_DEN", 100)
|
||||
if rate_num != 1 or rate_den != 1:
|
||||
dut._log.info(
|
||||
"Skipping full_rate_noise: requires WRITE_NOISE_RATE_NUM=1, RATE_DEN=1."
|
||||
)
|
||||
return
|
||||
if not hasattr(dut, "score_debug_flat"):
|
||||
dut._log.info(
|
||||
"Skipping full_rate_noise: requires SIM_DEBUG (score_debug_flat)."
|
||||
)
|
||||
return
|
||||
|
||||
cocotb.start_soon(Clock(dut.clk, 10, unit="ns").start())
|
||||
await reset_dut(dut)
|
||||
|
||||
hash_bits = dut_hash_bits(dut)
|
||||
num_rows = dut_num_rows(dut)
|
||||
noise_bits = get_param(dut, "WRITE_NOISE_BITS", 8)
|
||||
all_zero = 0
|
||||
all_one = (1 << hash_bits) - 1
|
||||
|
||||
RTL_SEED = 0xB504_F32D_B504_F32D
|
||||
prng_state = (RTL_SEED << 64) | RTL_SEED
|
||||
|
||||
flip0, prng_state = generate_write_flip_mask(
|
||||
prng_state,
|
||||
hash_bits,
|
||||
noise_bits,
|
||||
rate_num,
|
||||
rate_den,
|
||||
)
|
||||
expected_row0 = all_zero ^ flip0
|
||||
|
||||
flip1, prng_state = generate_write_flip_mask(
|
||||
prng_state,
|
||||
hash_bits,
|
||||
noise_bits,
|
||||
rate_num,
|
||||
rate_den,
|
||||
)
|
||||
expected_row1 = all_one ^ flip1
|
||||
|
||||
rows = [0] * num_rows
|
||||
rows[0] = all_zero
|
||||
rows[1] = all_one
|
||||
|
||||
await write_rows(dut, rows)
|
||||
|
||||
top1_index, top1_score, score_debug = await query_once(dut, expected_row0)
|
||||
assert score_debug is not None, "score_debug required for full_rate_noise"
|
||||
assert int(score_debug[0]) == hash_bits, (
|
||||
f"Row 0: expected exact match, score={score_debug[0]} != {hash_bits}"
|
||||
)
|
||||
|
||||
top1_index, top1_score, score_debug = await query_once(dut, expected_row1)
|
||||
assert score_debug is not None
|
||||
assert int(score_debug[1]) == hash_bits, (
|
||||
f"Row 1: expected exact match, score={score_debug[1]} != {hash_bits}"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user