feat:接入iverilog工具

- 将iverilog可以随着插件的下载而下载
- 用户输入自然语言就可以控制生成对应的VCD文件
This commit is contained in:
Roe-xin
2025-12-15 11:09:03 +08:00
parent 94225a3525
commit 22b9a0ed13
71 changed files with 2020 additions and 87 deletions

View File

@ -0,0 +1,183 @@
# Iverilog 工具下载和安装说明
## 重要提示
由于网络限制,需要手动下载 iverilog 工具并放置到插件包中。
## 快速开始
### 步骤 1下载 Iverilog
**Windows x64 用户:**
1. 访问官方下载页面http://bleyer.org/icarus/
2. 下载文件:`iverilog-v12-20220611-x64_setup.exe` (约 18.2 MB)
3. 保存到任意位置
**备用下载地址:**
- GitHub Releases: https://github.com/steveicarus/iverilog/releases
- SourceForge: https://sourceforge.net/projects/iverilog/
### 步骤 2安装到临时目录
1. 运行下载的安装程序 `iverilog-v12-20220611-x64_setup.exe`
2. 选择安装路径(建议:`C:\iverilog-temp`
3. 完成安装
### 步骤 3复制文件到插件目录
**需要复制的文件:**
#### A. 可执行文件(从 `C:\iverilog-temp\bin\` 复制到 `tools\iverilog\bin\`
```
iverilog.exe # Verilog 编译器
vvp.exe # Verilog 仿真器
```
#### B. 依赖的 DLL 文件(从 `C:\iverilog-temp\bin\` 复制到 `tools\iverilog\bin\`
```
libgcc_s_seh-1.dll
libwinpthread-1.dll
libstdc++-6.dll
```
如果运行时提示缺少其他 DLL也需要从安装目录复制。
#### C. 库文件目录(重要!)
将整个 `C:\iverilog-temp\lib\` 目录复制到 `tools\iverilog\lib\`
最终目录结构:
```
tools\iverilog\lib\ivl\
├── system.vpi
├── v2005_math.vpi
├── vhdl_sys.vpi
└── ... (其他 .vpi 和 .vpl 文件)
```
### 步骤 4验证安装
在命令行中运行:
```bash
cd "D:\IC Coder Plugin\ic-coder\tools\iverilog\bin"
.\iverilog.exe -V
```
应该看到版本信息:
```
Icarus Verilog version 12.0 (stable) (s20220611-xxx)
```
### 步骤 5清理
安装完成后,可以:
1. 卸载临时安装的 iverilog通过控制面板
2. 删除临时安装目录 `C:\iverilog-temp`
## 最终目录结构
```
D:\IC Coder Plugin\ic-coder\
└── tools\
└── iverilog\
├── bin\
│ ├── iverilog.exe
│ ├── vvp.exe
│ ├── libgcc_s_seh-1.dll
│ ├── libwinpthread-1.dll
│ └── libstdc++-6.dll
├── lib\
│ └── ivl\
│ ├── system.vpi
│ ├── v2005_math.vpi
│ └── ... (其他库文件)
├── README.md
├── INSTALL.md
└── DOWNLOAD_INSTRUCTIONS.md (本文件)
```
## 文件大小参考
- `iverilog.exe`: ~2 MB
- `vvp.exe`: ~1 MB
- DLL 文件: ~1-2 MB
- lib/ivl/ 目录: ~3-5 MB
- **总计**: 约 7-10 MB
## 自动化脚本(可选)
如果你已经安装了 iverilog可以使用以下 PowerShell 脚本自动复制文件:
```powershell
# 设置路径
$iverilogInstallPath = "C:\iverilog" # 修改为你的安装路径
$pluginToolsPath = "D:\IC Coder Plugin\ic-coder\tools\iverilog"
# 创建目录
New-Item -ItemType Directory -Force -Path "$pluginToolsPath\bin"
New-Item -ItemType Directory -Force -Path "$pluginToolsPath\lib"
# 复制可执行文件
Copy-Item "$iverilogInstallPath\bin\iverilog.exe" "$pluginToolsPath\bin\"
Copy-Item "$iverilogInstallPath\bin\vvp.exe" "$pluginToolsPath\bin\"
# 复制 DLL 文件
Copy-Item "$iverilogInstallPath\bin\*.dll" "$pluginToolsPath\bin\"
# 复制库文件
Copy-Item "$iverilogInstallPath\lib\*" "$pluginToolsPath\lib\" -Recurse
Write-Host "复制完成!"
```
保存为 `copy-iverilog.ps1` 并在 PowerShell 中运行。
## 许可证信息
Icarus Verilog 使用 **GPL v2+** 许可证,允许自由分发。
- 官方网站http://iverilog.icarus.com/
- GitHubhttps://github.com/steveicarus/iverilog
- 许可证文本https://github.com/steveicarus/iverilog/blob/master/COPYING
## 需要帮助?
如果遇到问题,请:
1. 检查是否复制了所有必需的文件
2. 确认 DLL 文件都在 bin 目录中
3. 验证 lib/ivl 目录包含所有 .vpi 文件
4. 查看插件的错误日志
## 其他平台
### macOS
```bash
# 使用 Homebrew 安装
brew install icarus-verilog
# 复制文件到插件目录
cp /usr/local/bin/iverilog "tools/iverilog/bin/"
cp /usr/local/bin/vvp "tools/iverilog/bin/"
cp -r /usr/local/lib/ivl "tools/iverilog/lib/"
```
### Linux
```bash
# Ubuntu/Debian
sudo apt-get install iverilog
# 复制文件到插件目录
cp /usr/bin/iverilog "tools/iverilog/bin/"
cp /usr/bin/vvp "tools/iverilog/bin/"
cp -r /usr/lib/ivl "tools/iverilog/lib/"
```
---
**注意**:完成上述步骤后,插件即可使用内置的 iverilog 工具进行 Verilog 编译和 VCD 文件生成。

131
tools/iverilog/INSTALL.md Normal file
View File

@ -0,0 +1,131 @@
# Iverilog 工具安装指南
## 自动安装(推荐)
插件会自动使用打包在 `tools/iverilog/bin/` 目录中的 iverilog 工具。
## 手动安装步骤
如果需要手动更新或安装 iverilog 工具,请按照以下步骤操作:
### Windows x64
1. **下载安装包**
- 访问http://bleyer.org/icarus/
- 下载:`iverilog-v12-20220611-x64_setup.exe` (18.2MB)
- 或直接下载http://bleyer.org/icarus/iverilog-v12-20220611-x64_setup.exe
2. **安装到临时目录**
- 运行安装程序
- 选择安装路径(例如:`C:\iverilog-temp`
- 完成安装
3. **复制必要文件**
将以下文件从安装目录复制到 `tools/iverilog/bin/`
**核心可执行文件:**
- `iverilog.exe` - Verilog 编译器
- `vvp.exe` - Verilog 仿真器
**依赖的 DLL 文件:**
- `libgcc_s_seh-1.dll`
- `libwinpthread-1.dll`
- `libstdc++-6.dll`
- 以及其他可能需要的 DLL 文件
**库文件目录(重要):**
- 复制整个 `lib/ivl/` 目录到 `tools/iverilog/lib/ivl/`
- 这个目录包含 iverilog 的标准库和模块
4. **目录结构**
最终的目录结构应该是:
```
tools/
└── iverilog/
├── bin/
│ ├── iverilog.exe
│ ├── vvp.exe
│ ├── libgcc_s_seh-1.dll
│ ├── libwinpthread-1.dll
│ └── libstdc++-6.dll
├── lib/
│ └── ivl/
│ ├── system.vpi
│ ├── v2005_math.vpi
│ └── ... (其他库文件)
└── README.md
```
5. **清理**
- 可以卸载临时安装的 iverilog
- 删除临时安装目录
### 验证安装
在命令行中运行:
```bash
cd "D:\IC Coder Plugin\ic-coder\tools\iverilog\bin"
.\iverilog.exe -V
```
应该看到类似输出:
```
Icarus Verilog version 12.0 (stable) (s20220611-xxx)
```
## 文件清单
### 必需文件(约 5-10 MB
**bin/ 目录:**
- iverilog.exe (~2MB)
- vvp.exe (~1MB)
- libgcc_s_seh-1.dll
- libwinpthread-1.dll
- libstdc++-6.dll
**lib/ivl/ 目录:**
- system.vpi
- v2005_math.vpi
- vhdl_sys.vpi
- vhdl_textio.vpi
- va_math.vpi
- 以及其他 .vpi 和 .vpl 文件
## 许可证
Icarus Verilog 使用 GPL v2+ 许可证。
- 官方网站http://iverilog.icarus.com/
- 源代码https://github.com/steveicarus/iverilog
- 许可证https://github.com/steveicarus/iverilog/blob/master/COPYING
## 注意事项
1. **文件大小**:完整的 iverilog 工具约 5-10 MB不包含 GTKWave
2. **依赖项**:确保复制所有必需的 DLL 文件
3. **库文件**lib/ivl/ 目录是必需的,包含 Verilog 标准库
4. **版本**:推荐使用 v12.0 或更高版本
## 故障排除
### 问题:运行时提示缺少 DLL
**解决方案**
- 使用 Dependency Walker 或 Dependencies.exe 检查缺少的 DLL
- 从 iverilog 安装目录复制缺少的 DLL 到 bin/ 目录
### 问题:编译时提示找不到标准库
**解决方案**
- 确保 lib/ivl/ 目录存在且包含所有 .vpi 文件
- 检查 iverilog 是否能找到库文件路径
### 问题vvp 运行失败
**解决方案**
- 确保 vvp.exe 和相关 DLL 都在 bin/ 目录中
- 检查是否有权限问题

247
tools/iverilog/README.md Normal file
View File

@ -0,0 +1,247 @@
# Iverilog 工具集成说明
本插件**已集成** Icarus Verilog (iverilog) 工具,用于 Verilog 代码的编译和仿真,以及 VCD 波形文件的生成。
## 目录结构
```
tools/
└── iverilog/
├── bin/ # 可执行文件目录(已包含 iverilog 工具)
│ ├── iverilog.exe # Verilog 编译器 (Windows)
│ ├── vvp.exe # Verilog 仿真器 (Windows)
│ └── ... # 其他依赖文件
└── README.md # 本说明文件
```
## 工具说明
插件包中**已包含** Windows x64 版本的 iverilog 工具,无需额外安装。
- **版本**: Icarus Verilog 11.0 (或更高)
- **平台**: Windows x64
- **许可证**: GPL v2+
### Windows 安装
1. 访问 Icarus Verilog 官方网站http://bleyer.org/icarus/
2. 下载最新的 Windows 安装包例如iverilog-v11-20210204-x64_setup.exe
3. 运行安装程序,按照提示完成安装
4. 安装完成后,将以下文件复制到插件的 `tools/iverilog/bin/` 目录:
- `iverilog.exe`
- `vvp.exe`
- 以及相关的 DLL 文件(如果有)
**或者**,将 iverilog 安装目录添加到系统 PATH 环境变量中,插件会自动使用系统安装的版本。
### macOS 安装
使用 Homebrew 安装:
```bash
brew install icarus-verilog
```
安装完成后iverilog 会自动添加到系统 PATH 中,插件可以直接使用。
**可选**:如果想将 iverilog 打包到插件中,可以将以下文件复制到 `tools/iverilog/bin/`
- `/usr/local/bin/iverilog`
- `/usr/local/bin/vvp`
### Linux 安装
#### Ubuntu/Debian
```bash
sudo apt-get update
sudo apt-get install iverilog
```
#### Fedora/CentOS
```bash
sudo yum install iverilog
```
#### Arch Linux
```bash
sudo pacman -S iverilog
```
安装完成后iverilog 会自动添加到系统 PATH 中,插件可以直接使用。
## 使用方法
### 1. 准备 Verilog 项目
确保您的项目包含以下文件:
- **顶层模块文件**:包含您的设计代码(例如:`counter.v`
- **Testbench 文件**:包含测试代码,文件名通常包含 `tb``test`(例如:`counter_tb.v`
### 2. Testbench 要求
为了生成 VCD 波形文件,您的 testbench 必须包含以下语句:
```verilog
module counter_tb;
// ... 信号声明 ...
initial begin
// 生成 VCD 文件
$dumpfile("output.vcd"); // 指定 VCD 文件名
$dumpvars(0, counter_tb); // 记录所有信号
// ... 测试代码 ...
#1000 $finish; // 结束仿真
end
// ... 其他测试代码 ...
endmodule
```
### 3. 生成 VCD 文件
在 IC Coder 插件的聊天界面中,输入以下任一命令:
- `生成 VCD`
- `创建 VCD`
- `运行仿真`
- `执行仿真`
- `生成波形`
插件会自动:
1. 检查项目文件完整性
2. 使用 iverilog 编译所有 Verilog 文件
3. 使用 vvp 运行仿真
4. 在项目根目录生成 `output.vcd` 文件
### 4. 查看波形
生成的 VCD 文件可以使用以下工具查看:
- **GTKWave**(推荐):开源波形查看器
- Windows: http://gtkwave.sourceforge.net/
- macOS: `brew install gtkwave`
- Linux: `sudo apt-get install gtkwave`
- **其他工具**
- ModelSim
- Vivado
- 在线 VCD 查看器
## 示例项目
### counter.v顶层模块
```verilog
module counter (
input clk,
input rst_n,
output reg [3:0] count
);
always @(posedge clk or negedge rst_n) begin
if (!rst_n)
count <= 4'b0000;
else
count <= count + 1;
end
endmodule
```
### counter_tb.vTestbench
```verilog
`timescale 1ns/1ps
module counter_tb;
reg clk;
reg rst_n;
wire [3:0] count;
// 实例化被测模块
counter uut (
.clk(clk),
.rst_n(rst_n),
.count(count)
);
// 生成时钟信号
initial begin
clk = 0;
forever #5 clk = ~clk; // 10ns 周期
end
// 测试序列
initial begin
// 生成 VCD 文件
$dumpfile("output.vcd");
$dumpvars(0, counter_tb);
// 初始化
rst_n = 0;
#20;
// 释放复位
rst_n = 1;
#200;
// 结束仿真
$finish;
end
// 监控输出
initial begin
$monitor("Time=%0t rst_n=%b count=%d", $time, rst_n, count);
end
endmodule
```
## 故障排除
### 问题:提示 "iverilog 不可用"
**解决方案**
1. 确认已安装 iverilog
2. 检查 iverilog 是否在系统 PATH 中:
- Windows: 在命令提示符中运行 `iverilog -V`
- macOS/Linux: 在终端中运行 `iverilog -V`
3. 或者将 iverilog 可执行文件复制到 `tools/iverilog/bin/` 目录
### 问题:提示 "项目文件不完整"
**解决方案**
1. 确保项目中至少有一个 `.v``.sv` 文件
2. 确保有 testbench 文件(文件名包含 `tb``test`
3. 确保 testbench 中包含 `$dumpfile``$dumpvars` 语句
### 问题:编译失败
**解决方案**
1. 检查 Verilog 代码语法错误
2. 查看错误输出信息
3. 确保所有模块都正确实例化
### 问题VCD 文件未生成
**解决方案**
1. 确保 testbench 中包含 `$dumpfile("output.vcd")` 语句
2. 确保 testbench 中包含 `$dumpvars` 语句
3. 确保仿真运行了足够的时间(使用 `#时间 $finish;`
## 版本信息
- 推荐 Icarus Verilog 版本v11.0 或更高
- 支持的 Verilog 标准Verilog-1995, Verilog-2001, Verilog-2005, SystemVerilog (部分)
## 相关链接
- Icarus Verilog 官网http://iverilog.icarus.com/
- GTKWave 官网http://gtkwave.sourceforge.net/
- Verilog 教程https://www.asic-world.com/verilog/
## 许可证
Icarus Verilog 是开源软件,遵循 GPL 许可证。

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
tools/iverilog/bin/vvp.exe Normal file

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,20 @@
@echo off
chcp 65001 >nul
echo =====================================
echo Iverilog 文件复制脚本
echo =====================================
echo.
REM 检查 PowerShell 是否可用
where powershell >nul 2>&1
if %errorlevel% neq 0 (
echo 错误: 未找到 PowerShell
pause
exit /b 1
)
REM 运行 PowerShell 脚本
powershell -ExecutionPolicy Bypass -File "%~dp0copy-iverilog.ps1"
echo.
pause

View File

@ -0,0 +1,136 @@
# Iverilog 文件自动复制脚本
# 用于将已安装的 iverilog 复制到插件目录
param(
[string]$IverilogPath = "C:\iverilog"
)
# 设置插件工具路径
$PluginPath = Split-Path -Parent $PSScriptRoot
$ToolsPath = Join-Path $PSScriptRoot ""
Write-Host "=====================================" -ForegroundColor Cyan
Write-Host "Iverilog 文件复制脚本" -ForegroundColor Cyan
Write-Host "=====================================" -ForegroundColor Cyan
Write-Host ""
# 检查源路径是否存在
if (-not (Test-Path $IverilogPath)) {
Write-Host "错误: 找不到 iverilog 安装目录: $IverilogPath" -ForegroundColor Red
Write-Host ""
Write-Host "请指定正确的 iverilog 安装路径,例如:" -ForegroundColor Yellow
Write-Host " .\copy-iverilog.ps1 -IverilogPath 'C:\Program Files\iverilog'" -ForegroundColor Yellow
Write-Host ""
# 尝试查找常见安装位置
$CommonPaths = @(
"C:\iverilog",
"C:\Program Files\iverilog",
"C:\Program Files (x86)\iverilog",
"$env:LOCALAPPDATA\iverilog"
)
Write-Host "正在搜索常见安装位置..." -ForegroundColor Yellow
foreach ($path in $CommonPaths) {
if (Test-Path $path) {
Write-Host "找到: $path" -ForegroundColor Green
$IverilogPath = $path
break
}
}
if (-not (Test-Path $IverilogPath)) {
Write-Host "未找到 iverilog 安装目录" -ForegroundColor Red
exit 1
}
}
Write-Host "源路径: $IverilogPath" -ForegroundColor Green
Write-Host "目标路径: $ToolsPath" -ForegroundColor Green
Write-Host ""
# 创建目标目录
Write-Host "创建目录结构..." -ForegroundColor Cyan
New-Item -ItemType Directory -Force -Path "$ToolsPath\bin" | Out-Null
New-Item -ItemType Directory -Force -Path "$ToolsPath\lib" | Out-Null
# 复制可执行文件
Write-Host "复制可执行文件..." -ForegroundColor Cyan
$executables = @("iverilog.exe", "vvp.exe")
foreach ($exe in $executables) {
$sourcePath = Join-Path "$IverilogPath\bin" $exe
if (Test-Path $sourcePath) {
Copy-Item $sourcePath "$ToolsPath\bin\" -Force
Write-Host "$exe" -ForegroundColor Green
} else {
Write-Host " ✗ 未找到 $exe" -ForegroundColor Red
}
}
# 复制 DLL 文件
Write-Host "复制 DLL 文件..." -ForegroundColor Cyan
$dlls = Get-ChildItem "$IverilogPath\bin\*.dll" -ErrorAction SilentlyContinue
if ($dlls) {
foreach ($dll in $dlls) {
Copy-Item $dll.FullName "$ToolsPath\bin\" -Force
Write-Host "$($dll.Name)" -ForegroundColor Green
}
} else {
Write-Host " ! 未找到 DLL 文件" -ForegroundColor Yellow
}
# 复制库文件
Write-Host "复制库文件..." -ForegroundColor Cyan
$libPath = Join-Path $IverilogPath "lib"
if (Test-Path $libPath) {
Copy-Item "$libPath\*" "$ToolsPath\lib\" -Recurse -Force
# 统计复制的文件
$vpiFiles = Get-ChildItem "$ToolsPath\lib\ivl\*.vpi" -ErrorAction SilentlyContinue
$vplFiles = Get-ChildItem "$ToolsPath\lib\ivl\*.vpl" -ErrorAction SilentlyContinue
Write-Host " ✓ 复制了 $($vpiFiles.Count) 个 .vpi 文件" -ForegroundColor Green
Write-Host " ✓ 复制了 $($vplFiles.Count) 个 .vpl 文件" -ForegroundColor Green
} else {
Write-Host " ✗ 未找到 lib 目录" -ForegroundColor Red
}
Write-Host ""
Write-Host "=====================================" -ForegroundColor Cyan
Write-Host "验证安装..." -ForegroundColor Cyan
Write-Host "=====================================" -ForegroundColor Cyan
# 验证 iverilog
$iverilogExe = Join-Path "$ToolsPath\bin" "iverilog.exe"
if (Test-Path $iverilogExe) {
Write-Host ""
Write-Host "运行 iverilog -V:" -ForegroundColor Yellow
& $iverilogExe -V
Write-Host ""
} else {
Write-Host "错误: iverilog.exe 未找到" -ForegroundColor Red
}
# 显示文件大小统计
Write-Host "=====================================" -ForegroundColor Cyan
Write-Host "文件大小统计" -ForegroundColor Cyan
Write-Host "=====================================" -ForegroundColor Cyan
$binSize = (Get-ChildItem "$ToolsPath\bin" -Recurse -File | Measure-Object -Property Length -Sum).Sum
$libSize = (Get-ChildItem "$ToolsPath\lib" -Recurse -File -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum
$totalSize = $binSize + $libSize
Write-Host "bin/ 目录: $([math]::Round($binSize / 1MB, 2)) MB" -ForegroundColor Green
Write-Host "lib/ 目录: $([math]::Round($libSize / 1MB, 2)) MB" -ForegroundColor Green
Write-Host "总计: $([math]::Round($totalSize / 1MB, 2)) MB" -ForegroundColor Cyan
Write-Host ""
Write-Host "=====================================" -ForegroundColor Cyan
Write-Host "复制完成!" -ForegroundColor Green
Write-Host "=====================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "现在可以使用插件的 VCD 生成功能了。" -ForegroundColor Green
Write-Host ""

View File

@ -0,0 +1,134 @@
# Iverilog 测试示例
这个目录包含一个简单的 Verilog 项目示例,用于测试 IC Coder 插件的 VCD 生成功能。
## 文件说明
- **counter.v** - 一个简单的 4 位计数器模块
- **counter_tb.v** - 计数器的测试平台testbench
## 功能说明
### counter.v顶层模块
这是一个同步复位的 4 位计数器:
- **输入**
- `clk` - 时钟信号
- `rst_n` - 低电平有效的复位信号
- **输出**
- `count[3:0]` - 4 位计数值0-15
### counter_tb.v测试平台
测试平台包含:
- 时钟生成器100MHz周期 10ns
- 复位序列(初始复位 20ns
- VCD 波形文件生成
- 信号监控和显示
## 使用方法
### 方法 1使用 IC Coder 插件(推荐)
1. 在 VS Code 中打开这个 `examples` 目录作为工作区
2. 打开 IC Coder 插件面板
3. 在聊天框中输入以下任一命令:
- `生成 VCD`
- `运行仿真`
- `生成波形`
4. 插件会自动编译并运行仿真,生成 `output.vcd` 文件
### 方法 2手动运行用于测试
在命令行中执行:
```bash
# 进入示例目录
cd "D:\IC Coder Plugin\ic-coder\tools\iverilog\examples"
# 编译 Verilog 文件
"D:\IC Coder Plugin\ic-coder\tools\iverilog\bin\iverilog.exe" -o simulation.vvp counter.v counter_tb.v
# 运行仿真
"D:\IC Coder Plugin\ic-coder\tools\iverilog\bin\vvp.exe" simulation.vvp
# 查看生成的 VCD 文件
ls -lh output.vcd
```
## 预期输出
### 控制台输出
```
Time=0 ns, rst_n=0, count=0 (0x0)
Time=20 ns, rst_n=1, count=0 (0x0)
Time=25 ns, rst_n=1, count=1 (0x1)
Time=35 ns, rst_n=1, count=2 (0x2)
Time=45 ns, rst_n=1, count=3 (0x3)
...
Time=215 ns, rst_n=1, count=15 (0xf)
Final count value: 15
```
### 生成的文件
- **simulation.vvp** - 编译后的中间文件(可以删除)
- **output.vcd** - VCD 波形文件(约 2-5 KB
## 查看波形
使用 GTKWave 查看生成的 VCD 文件:
```bash
gtkwave output.vcd
```
在 GTKWave 中:
1. 在左侧 SST 窗口选择 `counter_tb`
2. 将信号 `clk``rst_n``count[3:0]` 拖到波形窗口
3. 点击 "Zoom Fit" 查看完整波形
## 波形说明
你应该能看到:
- **clk** - 规则的时钟信号10ns 周期)
- **rst_n** - 初始 20ns 为低电平,然后保持高电平
- **count** - 从 0 开始递增到 15 的计数值
## 故障排除
### 问题:编译失败
**检查**
- 确认 iverilog.exe 在 `tools/iverilog/bin/` 目录中
- 确认所有 DLL 文件都已复制
### 问题VCD 文件未生成
**检查**
- testbench 中是否包含 `$dumpfile("output.vcd");`
- testbench 中是否包含 `$dumpvars(0, counter_tb);`
- 仿真是否正常结束(有 `$finish;`
### 问题:路径错误
**解决**
- 确保在正确的工作区目录中运行
- 检查路径中是否有特殊字符
## 扩展练习
你可以修改这个示例来学习:
1. **修改计数器位宽**:将 4 位改为 8 位
2. **添加使能信号**:只在使能时计数
3. **添加加载功能**:可以加载初始值
4. **创建递减计数器**:向下计数
5. **添加溢出标志**:计数到最大值时输出标志
## 参考资料
- Verilog 语法https://www.asic-world.com/verilog/
- Icarus Verilog 文档http://iverilog.icarus.com/
- GTKWave 使用http://gtkwave.sourceforge.net/

View File

@ -0,0 +1,17 @@
// counter.v - 简单的 4 位计数器模块
module counter (
input wire clk,
input wire rst_n,
output reg [3:0] count
);
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
count <= 4'b0000;
end else begin
count <= count + 1;
end
end
endmodule

View File

@ -0,0 +1,54 @@
// counter_tb.v - 计数器测试平台
`timescale 1ns/1ps
module counter_tb;
// 信号声明
reg clk;
reg rst_n;
wire [3:0] count;
// 实例化被测模块
counter uut (
.clk(clk),
.rst_n(rst_n),
.count(count)
);
// 生成时钟信号 (10ns 周期 = 100MHz)
initial begin
clk = 0;
forever #5 clk = ~clk;
end
// 测试序列
initial begin
// 生成 VCD 波形文件
$dumpfile("output.vcd");
$dumpvars(0, counter_tb);
// 初始化信号
rst_n = 0;
// 等待 20ns 后释放复位
#20;
rst_n = 1;
// 运行 200ns 让计数器计数
#200;
// 显示最终计数值
$display("Final count value: %d", count);
// 结束仿真
$finish;
end
// 监控输出变化
initial begin
$monitor("Time=%0t ns, rst_n=%b, count=%d (0x%h)",
$time, rst_n, count, count);
end
endmodule

View File

@ -0,0 +1,43 @@
// Mathematical and physical constants
`ifdef CONSTANTS_VAMS
`else
`define CONSTANTS_VAMS 1
// M_ is a mathematical constant
`define M_E 2.7182818284590452354
`define M_LOG2E 1.4426950408889634074
`define M_LOG10E 0.43429448190325182765
`define M_LN2 0.69314718055994530942
`define M_LN10 2.30258509299404568402
`define M_PI 3.14159265358979323846
`define M_TWO_PI 6.28318530717958647693
`define M_PI_2 1.57079632679489661923
`define M_PI_4 0.78539816339744830962
`define M_1_PI 0.31830988618379067154
`define M_2_PI 0.63661977236758134308
`define M_2_SQRTPI 1.12837916709551257390
`define M_SQRT2 1.41421356237309504880
`define M_SQRT1_2 0.70710678118654752440
/*
* Do we need these? For now they are not available.
*
// The following constants have been taken from http://physics.nist.gov
// P_ is a physical constant
// charge of electron in coulombs
`define P_Q 1.602176462e-19
// speed of light in vacuum in meters/sec
`define P_C 2.99792458e8
// Boltzmann's constant in joules/kelvin
`define P_K 1.3806503e-23
// Planck's constant in joules*sec
`define P_H 6.62606876e-34
// permittivity of vacuum in farads/meter
`define P_EPS0 8.854187817e-12
// permeability of vacuum in henrys/meter
`define P_U0 (4.0e-7 * `M_PI)
// zero celsius in kelvin
`define P_CELSIUS0 273.15
*/
`endif

View File

@ -0,0 +1,72 @@
// Standard definitions for Verilog-AMS
`ifdef DISCIPLINES_VAMS
`else
`define DISCIPLINES_VAMS 1
discipline \logic ;
domain discrete;
enddiscipline
discipline ddiscrete;
domain discrete;
enddiscipline
nature Current;
units = "A";
access = I;
idt_nature = Charge;
`ifdef CURRENT_ABSTOL
abstol = `CURRENT_ABSTOL
`else
abstol = 1e-12;
`endif
endnature
nature Charge;
units = "coul";
access = Q;
ddt_nature = Current;
`ifdef CHARGE_ABSTOL
abstol = `CHARGE_ABSTOL;
`else
abstol = 1e-14;
`endif
endnature
nature Voltage;
units = "V";
access = V;
idt_nature = Flux;
`ifdef VOLTAGE_ABSTOL
abstol = `VOLTAGE_ABSTOL;
`else
abstol = 1e-6;
`endif
endnature
nature Flux;
units = "Wb";
access = Phi;
ddt_nature = Voltage;
`ifdef FLUX_ABSTOL
abstol = `flux_ABSTOL;
`else
abstol = 1e-9;
`endif
endnature
discipline electrical;
potential Voltage;
flow Current;
enddiscipline
discipline voltage;
potential Voltage;
enddiscipline
discipline current;
flow Current;
enddiscipline
`endif // !`ifdef DISCIPLINES_VAMS

View File

@ -0,0 +1,6 @@
functor:synth2
functor:synth
functor:syn-rules
functor:cprop
functor:nodangle
flag:DLL=blif.tgt

View File

@ -0,0 +1,6 @@
functor:synth2
functor:synth
functor:syn-rules
functor:cprop
functor:nodangle
flag:DLL=blif.tgt

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,4 @@
functor:synth2
functor:synth
functor:syn-rules
flag:DLL=null.tgt

View File

@ -0,0 +1 @@
flag:DLL=null.tgt

Binary file not shown.

View File

@ -0,0 +1,6 @@
functor:synth2
functor:synth
functor:syn-rules
functor:cprop
functor:nodangle
flag:DLL=pcb.tgt

View File

@ -0,0 +1,3 @@
functor:cprop
functor:nodangle
flag:DLL=pcb.tgt

Binary file not shown.

View File

@ -0,0 +1,6 @@
functor:synth2
functor:synth
functor:syn-rules
functor:cprop
functor:nodangle
flag:DLL=sizer.tgt

View File

@ -0,0 +1,6 @@
functor:synth2
functor:synth
functor:syn-rules
functor:cprop
functor:nodangle
flag:DLL=sizer.tgt

Binary file not shown.

View File

@ -0,0 +1,6 @@
functor:synth2
functor:synth
functor:syn-rules
functor:cprop
functor:nodangle
flag:DLL=stub.tgt

View File

@ -0,0 +1,3 @@
functor:cprop
functor:nodangle
flag:DLL=stub.tgt

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,7 @@
functor:synth2
functor:synth
functor:syn-rules
functor:cprop
functor:nodangle
-t:dll
flag:DLL=vhdl.tgt

View File

@ -0,0 +1,4 @@
functor:cprop
functor:nodangle
flag:DLL=vhdl.tgt
flag:DISABLE_CONCATZ_GENERATION=true

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,7 @@
functor:synth2
functor:synth
functor:syn-rules
functor:nodangle
functor:exposenodes
flag:DLL=vlog95.tgt
flag:DISABLE_CONCATZ_GENERATION=true

View File

@ -0,0 +1,2 @@
flag:DLL=vlog95.tgt
flag:DISABLE_CONCATZ_GENERATION=true

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,7 @@
functor:synth2
functor:synth
functor:syn-rules
functor:cprop
functor:nodangle
flag:DLL=vvp.tgt
flag:VVP_EXECUTABLE=/c/Source/iverilog-install/bin/vvp

View File

@ -0,0 +1,4 @@
functor:cprop
functor:nodangle
flag:DLL=vvp.tgt
flag:VVP_EXECUTABLE=/c/Source/iverilog-install/bin/vvp

Binary file not shown.

Binary file not shown.