Files

438 lines
10 KiB
Markdown
Raw Permalink Normal View History

2026-05-22 10:02:42 +08:00
# TBgen_App - 自动化 Testbench 生成工具
基于 CorrectBench 的多阶段自动化 Testbench 生成工具,支持根据 DUT 和项目描述生成 Verilog Testbench。
## 功能特性
- **多阶段流程**: TBgen → TBsim → TBcheck → CGA → TBeval
- **多种模型支持**: Qwen, GPT, Claude 等主流 LLM
- **自动化评估**: 支持覆盖率评估和 Mutant Kill 检测
- **灵活配置**: 通过 YAML 配置文件自定义各项参数
---
## 目录结构
```
TBgen_App/
├── config/ # 配置文件夹
│ ├── config.py # 配置加载模块
│ ├── default.yaml # 默认配置
│ └── key_API.json # API密钥需用户填写
├── autoline/ # 核心流程模块
│ ├── TB_autoline.py # 主流程编排
│ ├── TB1_gen.py # TB生成
│ ├── TB2_syncheck.py # 语法检查
│ ├── TB3_funccheck.py # 功能检查
│ ├── TB4_eval.py # 评估
│ └── TB_cga.py # CGA优化
├── prompt_scripts/ # Prompt脚本
├── data/ # 数据集管理
├── utils/ # 工具函数
├── LLM_call.py # LLM调用
├── loader_saver.py # 文件加载保存
├── main.py # 命令行入口
├── run_tbgen.py # Python API
├── requirements.txt
└── README.md
```
---
## 使用前配置
### 1. 安装依赖
```bash
cd TBgen_App
# 创建虚拟环境(推荐)
python3 -m venv venv
source venv/bin/activate
# 安装依赖
pip install -r requirements.txt
pip install matplotlib
```
### 2. 配置 API 密钥(必须)
编辑 `config/key_API.json`,填入您的 API 密钥:
```json
{
"OPENAI_API_KEY": "your-openai-key",
"ANTHROPIC_API_KEY": "your-anthropic-key",
"dashscope": "your-dashscope-key"
}
```
**注意**: 当前工具主要使用 `dashscope` (Qwen模型)请确保填入有效的阿里云API密钥。
### 3. 选择模型
**配置文件位置**: `config/custom.yaml`
支持的模型:
- **Qwen**: `qwen-max`, `qwen-plus`, `qwen-cursor`
- **GPT**: `gpt-4o`, `gpt-4o-mini`, `gpt-4-turbo`
- **Claude**: `claude-3-5-sonnet`, `claude-3-opus`
**修改位置** (第6行):
```yaml
gpt:
model: "qwen-max" # 修改这里选择模型
```
例如使用 GPT-4o:
```yaml
gpt:
model: "gpt-4o"
```
**注意**: 当前工具主要使用 `dashscope` (Qwen模型),请确保在 `config/key_API.json` 中填入有效的阿里云API密钥。
---
## 使用方法
### 方式一:配置文件运行(推荐)
1. **修改配置文件** `config/custom.yaml`
```yaml
autoline:
probset:
path: "/home/zhang/CorrectBench/data/HDLBits/HDLBits_data.jsonl"
only: ['fsm_ps2'] # 指定要运行的题目
gpt:
model: "qwen-max" # 指定模型
save:
root: "./output/fsm_ps2/" # 指定输出目录
```
2. **运行**
```bash
cd /home/zhang/CorrectBench/TBgen_App
source venv/bin/activate
./venv/bin/python -c "
import sys
sys.path.insert(0, '.')
from autoline.TB_autoline import run_autoline
run_autoline()
"
```
---
## 输入说明
### 1. dut_code / -d DUT文件
**必需**。DUT的完整Verilog代码包括
- module声明
- 所有端口定义
- 实现逻辑
### 2. description / -w 描述文件
**必需**。项目需求描述,应该包含:
- 功能说明
- 输入输出规格
- 端口定义
- 特殊要求或边界条件
**示例**:
```
一个8位无符号乘法器
- 输入: a[7:0], b[7:0] (8位无符号数)
- 输出: y[15:0] (16位乘积)
- 复位: rst (同步高有效)
- 时钟: clk (上升沿触发)
```
### 3. header / --header
**必需**。DUT的module声明行用于提取端口信息。
格式: `module module_name(端口列表);`
示例:
```verilog
module multiplier(input clk, input rst, input [7:0] a, input [7:0] b, output reg [15:0] y);
```
---
## 输出说明
### 1. 主输出目录
```
output/{task_id}/
└── {task_id}/
├── final_TB.v # 最终Verilog Testbench
├── final_TB.py # Python校验脚本
├── run_info.json # 运行信息
└── run_info_short.json # 简化运行信息
```
### 2. 完整输出目录(含中间文件)
```
output/{task_id}/{task_id}/
├── final_TB.v # 最终TB
├── final_TB.py # Python Checker
├── run_info.json # 详细运行信息
├── 1_TBgen/ # TB生成阶段
│ ├── stage_0.txt # LLM对话 (Circuit Type检测)
│ ├── stage_1.txt # LLM对话 (Spec生成)
│ ├── stage_2.txt # LLM对话 (Scenario生成)
│ ├── stage_3.txt # LLM对话 (Python规则生成)
│ ├── stage_4.txt # LLM对话 (TB代码生成)
│ ├── stage_4b.txt # LLM对话 (SEQ优化)
│ ├── stage_5.txt # LLM对话 (Pychecker代码)
│ ├── stage_checklist.txt # Checklist检查
│ └── TBgen_codes/ # 生成的TB代码
│ ├── {task_id}_tb.v # TB Verilog代码
│ └── {task_id}_tb.py # TB Python Checker
└── ...
```
### 3. 返回结果字典
```python
{
"TB_code_v": "...", # TB Verilog代码
"TB_code_py": "...", # TB Python代码
"run_info": {...}, # 详细运行信息
"cga_coverage": 0.0, # CGA覆盖率
"full_pass": None # 是否全部通过
}
```
---
### 配置文件详解
> 配置文件位于 `config/custom.yaml`,所有参数修改在此文件中进行。
#### 1. 指定要运行的题目
```yaml
autoline:
probset:
path: "/home/zhang/CorrectBench/data/HDLBits/HDLBits_data.jsonl"
only: ['fsm_ps2'] # 只运行 fsm_ps2 题目
```
如需运行多个题目:
```yaml
only: ['fsm_ps2', 'rule110', 'lemmings3']
```
#### 2. 指定模型
编辑 `config/custom.yaml`,修改 `gpt.model` 字段:
```yaml
gpt:
model: "qwen-max" # 可选: qwen-max, qwen-plus, gpt-4o, gpt-4o-mini, claude-3-5-sonnet 等
```
支持的模型列表:
| 模型 | model 参数值 |
|------|-------------|
| Qwen Max | `qwen-max` |
| Qwen Plus | `qwen-plus` |
| GPT-4o | `gpt-4o` |
| GPT-4o Mini | `gpt-4o-mini` |
| Claude 3.5 Sonnet | `claude-3-5-sonnet` |
| Claude 3 Opus | `claude-3-opus` |
#### 3. 指定输出目录
```yaml
save:
root: "/home/zhang/CorrectBench/TBgen_App/output/fsm_ps2/"
```
#### 4. 运行模式
```yaml
autoline:
onlyrun: "TBgensimeval" # 生成+检查+评估(不含CGA)
# onlyrun: null # 完整流程 (gen→sim→check→cga→eval)
# onlyrun: "TBgen" # 仅生成TB
# onlyrun: "TBgensim" # 生成+语法检查
```
#### 5. CGA 覆盖率优化
```yaml
autoline:
cga:
enabled: True # 是否启用 CGA
max_iter: 10 # 最大迭代次数
target_coverage: 100.0 # 目标覆盖率
```
#### 6. API 密钥配置
编辑 `config/key_API.json`:
```json
{
"OPENAI_API_KEY": "your-openai-key",
"ANTHROPIC_API_KEY": "your-anthropic-key",
"dashscope": "your-dashscope-key"
}
```
然后在 `config/custom.yaml` 中指定密钥路径:
```yaml
gpt:
key_path: "config/key_API.json"
```
**注意**: 当前工具主要使用 `dashscope` (Qwen模型)请确保填入有效的阿里云API密钥。
---
## 注意事项
### 1. 必须配置项
修改文件 `config/custom.yaml`
- [ ] `gpt.key_path` - API密钥文件默认为 `config/key_API.json`
- [ ] `config/key_API.json` - 填入有效的 API 密钥
- [ ] `autoline.probset.only` - 指定要运行的题目
- [ ] `gpt.model` - 选择使用的模型(在 `config/custom.yaml` 第6行
### 2. Header格式要求
**必须**与DUT代码中的module声明一致:
```verilog
// 正确
header = "module foo(input clk, output [7:0] y);"
// 错误 - 缺少分号
header = "module foo(input clk, output [7:0] y)"
```
### 3. 时序逻辑DUT
如果DUT是时序逻辑always块header中**必须**使用 `output reg` 而非 `output wire`:
```verilog
header = "module foo(input clk, input rst, output reg [7:0] y);"
```
### 4. 关于CGA优化
CGA (Coverage-Guided Agent) 优化可以提升测试覆盖率,但会增加运行时间。
`config/custom.yaml` 中设置:
```yaml
autoline:
cga:
enabled: True # 启用 CGA
# enabled: False # 禁用 CGA快速测试
```
### 5. 运行时间
完整流程运行时间取决于:
- LLM模型响应速度
- DUT复杂度
- 是否启用CGA优化
### 6. 常见错误
| 错误信息 | 原因 | 解决方法 |
|----------|------|----------|
| `API Key not found` | key_API.json配置错误 | 检查并填入有效密钥 |
| `ModuleNotFoundError` | 依赖未安装 | `pip install -r requirements.txt` |
| `AutoLogger...AttributeError` | 初始化顺序问题 | 确保使用正确的运行命令 |
| `timeout` | LLM响应超时 | 降低复杂度或切换模型 |
---
## 运行示例
### 方式一:使用配置文件运行(推荐)
通过 `config/custom.yaml` 配置题目,然后运行:
```bash
cd /home/zhang/CorrectBench/TBgen_App
source venv/bin/activate
./venv/bin/python -c "
import sys
sys.path.insert(0, '.')
from autoline.TB_autoline import run_autoline
run_autoline()
"
```
### 方式二Python API
```bash
cd /home/zhang/CorrectBench/TBgen_App
source venv/bin/activate
./venv/bin/python -c "
import sys
sys.path.insert(0, '.')
from run_tbgen import generate_tb
dut_code = '''module multiplier(
input clk,
input rst,
input [7:0] a,
input [7:0] b,
output reg [15:0] y
);
always @(posedge clk) begin
if (rst) y <= 16'b0;
else y <= a * b;
end
endmodule'''
description = '一个8位乘法器'
header = 'module multiplier(input clk, input rst, input [7:0] a, input [7:0] b, output reg [15:0] y);'
tb_path, result = generate_tb(
dut_code=dut_code,
description=description,
header=header,
task_id='multiplier_test',
model='qwen-max',
enable_cga=True
)
print(f'TB saved to: {tb_path}')
print(f'Coverage: {result.get(\"cga_coverage\", 0)}')
"
```
### 配置文件说明
编辑 `config/custom.yaml` 中的 `autoline.probset.only` 指定要运行的题目:
```yaml
autoline:
probset:
path: "/path/to/HDLBits_data.jsonl"
only: ['fsm_ps2', 'rule110'] # 指定题目列表
```
运行结果保存在 `save.root` 指定的目录下。
---
## License
本项目基于 CorrectBench 构建。