Skip to content

Commit

Permalink
build(version): inject git commit SHA to hardware CommitIDModule
Browse files Browse the repository at this point in the history
  • Loading branch information
huxuan0307 committed Oct 30, 2024
1 parent 7eef70f commit 489ff39
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ TEST_FILE = $(shell find ./src/test/scala -name '*.scala')

MEM_GEN = ./scripts/vlsi_mem_gen
MEM_GEN_SEP = ./scripts/gen_sep_mem.sh
GIT_COMMITID_GEN = ./scripts/genGitCommitID.py

CONFIG ?= DefaultConfig
NUM_CORES ?= 1
Expand Down Expand Up @@ -148,6 +149,7 @@ test-jar:

$(TOP_V): $(SCALA_FILE)
mkdir -p $(@D)
$(GIT_COMMITID_GEN) "-o" $(@D)
$(TIME_CMD) mill -i xiangshan.runMain $(FPGATOP) \
--target-dir $(@D) --config $(CONFIG) --issue $(ISSUE) $(FPGA_MEM_ARGS) \
--num-cores $(NUM_CORES) $(RELEASE_ARGS)
Expand All @@ -164,6 +166,7 @@ verilog: $(TOP_V)

$(SIM_TOP_V): $(SCALA_FILE) $(TEST_FILE)
mkdir -p $(@D)
$(GIT_COMMITID_GEN) "-o" $(@D)
@echo -e "\n[mill] Generating Verilog files..." > $(TIMELOG)
@date -R | tee -a $(TIMELOG)
$(TIME_CMD) mill -i xiangshan.test.runMain $(SIMTOP) \
Expand Down
61 changes: 61 additions & 0 deletions scripts/genGitCommitID.py
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 src/main/scala/xiangshan/backend/fu/NewCSR/CommitIDModule.scala
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())
}})
}
6 changes: 6 additions & 0 deletions src/main/scala/xiangshan/backend/fu/NewCSR/NewCSR.scala
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,12 @@ class NewCSR(implicit val p: Parameters) extends Module

val permitMod = Module(new CSRPermitModule)
val sstcIRGen = Module(new SstcInterruptGen)
val commidIdMod = Module(new CommitIDModule)

val gitCommitSHA = WireInit(commidIdMod.io.commitID)
val gitDirty = WireInit(commidIdMod.io.dirty)
dontTouch(gitCommitSHA)
dontTouch(gitDirty)

private val wenLegal = permitMod.io.out.hasLegalWen

Expand Down

0 comments on commit 489ff39

Please sign in to comment.