# ============================================================================= # check_rtl_text.ps1 —— detect stray non-Verilog text in the RTL sources. # # WHY: this project's .v files were twice corrupted by a text tool that inserted a # line of README prose into them ("Tcl ..." / a Chinese sentence), which made # Vivado fail with # ERROR: [Synth 8-2715] syntax error near "..." 'non-printable character 0xe4' # and produced a cascade of bogus errors in the included ROM .vh. Running this # script takes a second and catches it before spending a synthesis run. # # WHAT IT CHECKS # 1) a line inside .v/.vh/.xdc that has non-ASCII characters *outside* a comment # (i.e. real code position) -> corruption; # 2) a line that looks like pasted prose/markdown (leading "|", "**", or a line # that is bare text after `endmodule`). # # USAGE (from the repository root or from pipelined-cpu/): # powershell -ExecutionPolicy Bypass -File pipelined-cpu\tools\check_rtl_text.ps1 # Exit code 0 = clean, 1 = found something. # ============================================================================= param( # -Fix: 自动删除"整行都是粘贴进来的正文"的坏行(保守:只删不含 ; ( ) = 等代码标点的整行)。 # 被部分覆盖的行(例如某个端口行被改坏)只报告,需手工或 git 修。 [switch]$Fix ) $ErrorActionPreference = 'Stop' $srcs = Join-Path (Split-Path -Parent $PSScriptRoot) 'srcs' if (-not (Test-Path $srcs)) { Write-Output "ERROR: cannot find $srcs"; exit 2 } $bad = 0 $files = Get-ChildItem -Recurse -Path $srcs -Include *.v, *.vh, *.xdc -File foreach ($f in $files) { $rel = $f.FullName.Substring($srcs.Length).TrimStart('\', '/') # 用 .NET 按 UTF-8 读原始字节:PS 5.1 的 Get-Content 会按 GBK 解码,中文会乱。 # 按行切开时保留行尾(\r\n / \n),写回时逐字节保持原样,不归一化换行/编码/BOM。 $bytes = [System.IO.File]::ReadAllBytes($f.FullName) $text = [System.Text.Encoding]::UTF8.GetString($bytes) $lines = @([regex]::Split($text, "(?<=\n)")) $drop = @() for ($i = 0; $i -lt $lines.Count; $i++) { $line = $lines[$i].TrimEnd("`r", "`n") $lineNo = $i + 1 # ---- 只去掉 // (或 .xdc 的 #) 之后的部分,其余按代码位置检查 ---- # 注意:这里不处理 /* */ 块注释(srcs 里没有块注释,之前因此漏报过坏行) $code = $line if ($f.Extension -eq '.xdc') { $h = $code.IndexOf('#'); if ($h -ge 0) { $code = $code.Substring(0, $h) } } else { $c = $code.IndexOf('//'); if ($c -ge 0) { $code = $code.Substring(0, $c) } } # 字符串字面量里的中文是合法的(如 $display("中文")),先去掉再判断 $code = $code -replace '"[^"]*"', '""' $trim = $line.Trim() # ---- check 1: non-ASCII in code position ---- if ($code -match '[^\x00-\x7F]') { # 整行只有正文(不含 ; ( ) = ` ' " 之类代码标点)⇒ 可以安全整行删掉 $onlyProse = ($trim -notmatch '[;()=`''"]') $tag = if ($onlyProse) { ' [整行正文,可用 -Fix 删掉]' } else { ' [部分覆盖,需手工/git 修]' } Write-Output ("!! $rel`:$lineNo non-ASCII text in code position${tag}:") Write-Output (" " + $trim) if ($Fix -and $onlyProse) { $drop += $i } $bad++ continue } # ---- check 2: pasted prose / markdown leftovers ---- if ($trim -match '^\||^\*\*|^#[^0-9#]' -and $f.Extension -ne '.xdc') { Write-Output ("!! $rel`:$lineNo looks like pasted prose:") Write-Output (" " + $trim) $bad++ } } if ($Fix -and $drop.Count -gt 0) { $kept = for ($i = 0; $i -lt $lines.Count; $i++) { if ($drop -notcontains $i) { $lines[$i] } } [System.IO.File]::WriteAllText($f.FullName, ($kept -join ''), (New-Object System.Text.UTF8Encoding($false))) Write-Output (" -Fix: 已从 $rel 删除 " + $drop.Count + " 行(编码/换行保持不变),请重跑自检确认") } } Write-Output "" Write-Output ("scanned " + $files.Count + " files under srcs/") if ($bad -eq 0) { Write-Output "RTL text check: OK (no stray non-Verilog text found)." exit 0 } Write-Output ("RTL text check: FAILED, " + $bad + " suspicious line(s) - fix them before synthesis.") Write-Output "Hint1: 这类行通常是编辑器/工具粘贴进来的正文;整行正文可以用 -Fix 删掉:" Write-Output " powershell -ExecutionPolicy Bypass -File tools\check_rtl_text.ps1 -Fix" Write-Output "Hint2: 若报的是 [部分覆盖],对照 git diff 手工修(被编辑器保存覆盖时,先关掉那个标签页再改)。" exit 1