-
Notifications
You must be signed in to change notification settings - Fork 654
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build(version): inject git commit SHA to hardware CommitIDModule
- Loading branch information
1 parent
7eef70f
commit 489ff39
Showing
4 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#! /usr/bin/env python3 | ||
|
||
import subprocess | ||
import argparse | ||
import os | ||
from textwrap import dedent | ||
|
||
def get_git_commit_sha(): | ||
try: | ||
sha = subprocess.check_output(['git', 'rev-parse', 'HEAD']).decode('utf-8').strip() | ||
return sha | ||
except subprocess.CalledProcessError: | ||
print("Error: This script must be run from a git repository.") | ||
return None | ||
|
||
def check_if_status_dirty(): | ||
try: | ||
lines = subprocess.check_output(['git', 'status', '-uno', '--porcelain']).decode('utf-8').strip() | ||
return len(lines) != 0 | ||
except subprocess.CalledProcessError: | ||
print("Error: This script must be run from a git repository.") | ||
return None | ||
|
||
def generate_verilog(sha, dirty): | ||
verilog_template = dedent(""" | ||
module CommitIDModule( | ||
output reg [39:0] commitID, // 40-bit output for commit SHA | ||
output reg dirty // if git status is dirty | ||
); | ||
initial begin | ||
commitID = 40'h{sha}; | ||
dirty = 1'b{dirty}; | ||
end | ||
endmodule | ||
""") | ||
return verilog_template.format(sha=(sha.upper())[0:10], dirty=int(dirty)) | ||
|
||
def main(output_path): | ||
sha = get_git_commit_sha() | ||
dirty = check_if_status_dirty() | ||
if sha: | ||
verilog_code = generate_verilog(sha, dirty) | ||
print("Generated Verilog Code:") | ||
if not os.path.exists(os.path.dirname(output_path)): | ||
os.makedirs(os.path.dirname(output_path)) | ||
|
||
filename = os.path.join(output_path, "CommitIDModule.sv") | ||
# 将Verilog代码写入文件 | ||
with open(filename, 'w') as f: | ||
f.write(verilog_code) | ||
print(f"Verilog module has been created at '{filename}'.") | ||
else: | ||
print("Failed to get the git commit SHA.") | ||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser(description="Generate a Verilog module with the current git commit SHA.") | ||
parser.add_argument("-o", "--output", required=True, help="Output file path for the Verilog module.") | ||
args = parser.parse_args() | ||
main(args.output) |
11 changes: 11 additions & 0 deletions
11
src/main/scala/xiangshan/backend/fu/NewCSR/CommitIDModule.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package xiangshan.backend.fu.NewCSR | ||
|
||
import chisel3._ | ||
import chisel3.experimental.noPrefix | ||
|
||
class CommitIDModule extends BlackBox { | ||
val io = IO(noPrefix{new Bundle { | ||
val commitID = Output(UInt(40.W)) | ||
val dirty = Output(Bool()) | ||
}}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters