Compare commits

..

2 Commits

Author SHA1 Message Date
xinb 8f14f93d7e
Add readme & doc 2025-08-04 02:35:33 +08:00
xinb ee1795a3e0
Add oh2bin file 2025-08-04 02:20:30 +08:00
13 changed files with 212 additions and 2 deletions

View File

@ -1,3 +1,8 @@
# ic_com # ic_com
数字芯片设计公共库 数字芯片设计公共库
## 目录结构
- `lib/`核心功能模块如编码器、FIFO等
- `tb/`:各模块的仿真测试平台
- `doc/`:文档与使用说明

57
doc/README.md Normal file
View File

@ -0,0 +1,57 @@
# ic_com 库功能简介
## 目录结构
- `lib/`核心功能模块如编码器、FIFO等
- `tb/`:各模块的仿真测试平台
- `doc/`:文档与使用说明
## 主要功能模块
### 1. 编码/解码
- `enc_dec/oh_bin/oh2bin.sv`:参数化 one-hot 到二进制编码器,支持面积/频率优化,适用于高性能和低功耗场景。
### 2. FIFO
- `fifo/sync/fifo_sync.sv`同步FIFO支持参数化数据宽度和深度适合多场景缓存。
## 仿真与验证
- `tb/enc_dec/oh_bin/oh2bin_tb.sv`oh2bin编码器的时序仿真测试支持自动化验证。
- `tb/fifo/sync/fifo_sync_tb.sv`同步FIFO的功能仿真测试。
## 波形与调试
- 推荐使用VCS仿真支持FSDB波形输出便于使用Verdi/KDB调试。
## 快速仿真命令
见下方示例。
---
# VCS仿真命令示例
## 1. 生成.f文件文件列表
- 默认各模块`.f`文件需要配置环境变量`IC_COM_DIR`为仓库根目录。
- `lib.f`包含所有lib目录下的源文件
- `tb.f`包含所有tb目录下的测试平台文件
## 2. 推荐VCS命令含FSDB波形与KDB调试
```sh
vcs -full64 -sverilog -debug_access+all +v2k +vcs+lic+wait \
-f lib.f -f tb.f \
-l vcs.log \
-kdb \
+fsdbfile+${USER}_sim.fsdb \
+fsdb+autoflush \
+vcsd \
+vcs+dumpvars+all \
+vcs+fsdbon \
+notimingcheck \
+define+FSDB \
+define+KDB
```
- 默认fsdb文件名为`${USER}_sim.fsdb`,可自动区分不同用户仿真结果。
- 支持KDB调试推荐配合Verdi使用。
## 3. 运行仿真
```sh
./simv
```

4
doc/lib.f Normal file
View File

@ -0,0 +1,4 @@
// doc/lib.f: ic_com库所有心源文件,用各子库f
lib/enc_dec/oh_bin/oh_bin.f
lib/fifo/sync/sync.f

View File

@ -0,0 +1,2 @@
$IC_COM_DIR/lib/enc_dec/oh_bin/oh2bin.sv
$IC_COM_DIR/tb/enc_dec/oh_bin/oh2bin_tb.sv

2
doc/lib/fifo/sync/sync.f Normal file
View File

@ -0,0 +1,2 @@
$IC_COM_DIR/lib/fifo/sync/fifo_sync.sv
$IC_COM_DIR/tb/fifo/sync/fifo_sync_tb.sv

4
doc/tb.f Normal file
View File

@ -0,0 +1,4 @@
// doc/tb.f: ic_com库所有测平台文件,用各子库f
lib/enc_dec/oh_bin/oh_bin.f
lib/fifo/sync/sync.f

View File

@ -0,0 +1 @@
// 已合到 ../../../../lib/enc_dec/oh_bin/oh_bin.f

View File

@ -0,0 +1 @@
// 已合到 ../../../../lib/fifo/sync/sync.f

3
lib.f Normal file
View File

@ -0,0 +1,3 @@
// lib.f: ic_com库有核心源文
lib/enc_dec/oh_bin/oh2bin.sv
lib/fifo/sync/fifo_sync.sv

View File

@ -0,0 +1,44 @@
// =============================================================
// 模块名称oh2bin
// 功能描述:参数化 one-hot 到二进制编码器
// 参数:
// WIDTH - one-hot 输入宽度
// AREA_OPT- 1: 面积优化0: 频率优化
// =============================================================
module oh2bin #(
parameter WIDTH = 8, // one-hot 输入宽度
parameter AREA_OPT = 1 // 1: 面积优化0: 频率优化
)(
input logic [WIDTH-1:0] oh, // one-hot 输入
output logic [$clog2(WIDTH)-1:0] bin // 二进制输出
);
// =========================================================
// 面积优化方案使用for循环优先节省面积
// =========================================================
generate
if (AREA_OPT) begin : gen_area_opt
always_comb begin
bin = '0;
for (int i = 0; i < WIDTH; i++) begin
if (oh[i]) bin = i[$clog2(WIDTH)-1:0];
end
end
end else begin : gen_speed_opt
// =========================================================
// 频率优化方案并行掩码法参考pulp-platform/common_cells实现
// =========================================================
for (genvar j = 0; j < $clog2(WIDTH); j++) begin : gen_jl
logic [WIDTH-1:0] tmp_mask; //xinbbin每1bit的掩码实际上取巧了。相当于是逆向思维。还可以正向编码待补充.
for (genvar i = 0; i < WIDTH; i++) begin : gen_il
logic [$clog2(WIDTH)-1:0] tmp_i;
assign tmp_i = i[$clog2(WIDTH)-1:0];
assign tmp_mask[i] = tmp_i[j];
end
assign bin[j] = |(tmp_mask & oh);
end
end
endgenerate
endmodule

3
tb.f Normal file
View File

@ -0,0 +1,3 @@
// tb.f: ic_com库所测试平台文
tb/enc_dec/oh_bin/oh2bin_tb.sv
tb/fifo/sync/fifo_sync_tb.sv

View File

@ -0,0 +1,78 @@
// =============================================================
// 测试模块oh2bin_tb
// 功能描述onehot到bin编码器仿真测试默认频率优化方案
// 仿真时钟频率1GHz周期1ns
// =============================================================
`timescale 1ns/1ps
module oh2bin_tb;
parameter WIDTH = 8;
parameter BINW = $clog2(WIDTH);
logic clk;
logic rst_n;
logic [WIDTH-1:0] oh;
logic [BINW-1:0] bin;
logic [BINW-1:0] bin_q;
// 实例化被测模块AREA_OPT=0为频率优化方案
oh2bin #(
.WIDTH(WIDTH),
.AREA_OPT(0)
) dut (
.oh(oh),
.bin(bin)
);
// 时钟生成
// 仿真时钟频率1GHz周期1ns
initial clk = 0;
always #0.5 clk = ~clk; // 1ns周期1GHz
// 采样输出
always_ff @(posedge clk or negedge rst_n) begin
if (!rst_n)
bin_q <= '0;
else
bin_q <= bin;
end
// 参考模型
function [BINW-1:0] ref_oh2bin(input [WIDTH-1:0] oh_in);
integer i;
ref_oh2bin = '0;
for (i = 0; i < WIDTH; i = i + 1) begin
if (oh_in[i]) ref_oh2bin = i[BINW-1:0];
end
endfunction
initial begin
rst_n = 0;
oh = '0;
#20;
rst_n = 1;
$display("==== oh2bin 频率优化方案时序仿真 ====");
for (int i = 0; i < WIDTH; i++) begin
@(negedge clk);
oh = '0;
oh[i] = 1'b1;
@(negedge clk);
if (bin_q !== ref_oh2bin(oh)) begin
$display("[ERROR] oh=%b, bin=%0d, ref=%0d", oh, bin_q, ref_oh2bin(oh));
end else begin
$display("[PASS] oh=%b, bin=%0d", oh, bin_q);
end
end
// 非法输入测试
@(negedge clk);
oh = '0;
@(negedge clk);
$display("[INFO] all zero input, bin=%0d", bin_q);
@(negedge clk);
oh = '1;
@(negedge clk);
$display("[INFO] all one input, bin=%0d", bin_q);
$display("==== 测试结束 ====");
$finish;
end
endmodule

View File

@ -1,4 +1,9 @@
// =============================================================
// 测试模块fifo_sync_tb
// 功能描述同步FIFO仿真测试
// 仿真时钟频率1GHz周期0.5ns
// =============================================================
`timescale 1ns/1ps `timescale 1ns/1ps
module fifo_sync_tb; module fifo_sync_tb;
@ -37,9 +42,10 @@ module fifo_sync_tb;
.err(err) .err(err)
); );
// 仿真时钟频率1GHz周期0.5ns
// 时钟生成 // 时钟生成
initial clk = 0; initial clk = 0;
always #5 clk = ~clk; always #0.25 clk = ~clk; // 0.5ns周期1GHz
// 复位 // 复位
initial begin initial begin