diff --git a/lab1/实验报告/.obsidian/app.json b/lab1/实验报告/.obsidian/app.json new file mode 100644 index 0000000..f699f2f --- /dev/null +++ b/lab1/实验报告/.obsidian/app.json @@ -0,0 +1,9 @@ +{ + "pdfExportSettings": { + "includeName": true, + "pageSize": "Letter", + "landscape": false, + "margin": "0", + "downscalePercent": 100 + } +} \ No newline at end of file diff --git a/lab1/实验报告/.obsidian/appearance.json b/lab1/实验报告/.obsidian/appearance.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/lab1/实验报告/.obsidian/appearance.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/lab1/实验报告/.obsidian/core-plugins.json b/lab1/实验报告/.obsidian/core-plugins.json new file mode 100644 index 0000000..0faa60d --- /dev/null +++ b/lab1/实验报告/.obsidian/core-plugins.json @@ -0,0 +1,33 @@ +{ + "file-explorer": true, + "global-search": true, + "switcher": true, + "graph": true, + "backlink": true, + "canvas": true, + "outgoing-link": true, + "tag-pane": true, + "footnotes": false, + "properties": false, + "page-preview": true, + "daily-notes": true, + "templates": true, + "note-composer": true, + "command-palette": true, + "slash-command": false, + "editor-status": true, + "bookmarks": true, + "markdown-importer": false, + "zk-prefixer": false, + "random-note": false, + "outline": true, + "word-count": true, + "slides": false, + "audio-recorder": false, + "workspaces": false, + "file-recovery": true, + "publish": false, + "sync": true, + "bases": true, + "webviewer": false +} \ No newline at end of file diff --git a/lab1/实验报告/.obsidian/workspace.json b/lab1/实验报告/.obsidian/workspace.json new file mode 100644 index 0000000..a5b9b84 --- /dev/null +++ b/lab1/实验报告/.obsidian/workspace.json @@ -0,0 +1,181 @@ +{ + "main": { + "id": "6bcb496c7a3eb123", + "type": "split", + "children": [ + { + "id": "433dcf384d5f606e", + "type": "tabs", + "children": [ + { + "id": "d047ce653df73cd9", + "type": "leaf", + "state": { + "type": "markdown", + "state": { + "file": "实验报告:实验一 单周期 RISC-V CPU 设计与实现.md", + "mode": "source", + "source": false + }, + "icon": "lucide-file", + "title": "实验报告:实验一 单周期 RISC-V CPU 设计与实现" + } + } + ] + } + ], + "direction": "vertical" + }, + "left": { + "id": "ace00588a6bae164", + "type": "split", + "children": [ + { + "id": "44c58593ca1e022e", + "type": "tabs", + "children": [ + { + "id": "faf84f74156974b0", + "type": "leaf", + "state": { + "type": "file-explorer", + "state": { + "sortOrder": "alphabetical", + "autoReveal": false + }, + "icon": "lucide-folder-closed", + "title": "文件列表" + } + }, + { + "id": "fe5192c3e24c39c8", + "type": "leaf", + "state": { + "type": "search", + "state": { + "query": "", + "matchingCase": false, + "explainSearch": false, + "collapseAll": false, + "extraContext": false, + "sortOrder": "alphabetical" + }, + "icon": "lucide-search", + "title": "搜索" + } + }, + { + "id": "a4d6fbbfc8e710e3", + "type": "leaf", + "state": { + "type": "bookmarks", + "state": {}, + "icon": "lucide-bookmark", + "title": "书签" + } + } + ] + } + ], + "direction": "horizontal", + "width": 300 + }, + "right": { + "id": "eb60e338f069ca50", + "type": "split", + "children": [ + { + "id": "68b948d9c42a6119", + "type": "tabs", + "children": [ + { + "id": "80ad9f8a04a54721", + "type": "leaf", + "state": { + "type": "backlink", + "state": { + "file": "实验报告:实验一 单周期 RISC-V CPU 设计与实现.md", + "collapseAll": false, + "extraContext": false, + "sortOrder": "alphabetical", + "showSearch": false, + "searchQuery": "", + "backlinkCollapsed": false, + "unlinkedCollapsed": true + }, + "icon": "links-coming-in", + "title": "实验报告:实验一 单周期 RISC-V CPU 设计与实现 的反向链接列表" + } + }, + { + "id": "347ab2784c003b83", + "type": "leaf", + "state": { + "type": "outgoing-link", + "state": { + "file": "实验报告:实验一 单周期 RISC-V CPU 设计与实现.md", + "linksCollapsed": false, + "unlinkedCollapsed": true + }, + "icon": "links-going-out", + "title": "实验报告:实验一 单周期 RISC-V CPU 设计与实现 的出链列表" + } + }, + { + "id": "efad3bea5939cf3f", + "type": "leaf", + "state": { + "type": "tag", + "state": { + "sortOrder": "frequency", + "useHierarchy": true, + "showSearch": false, + "searchQuery": "" + }, + "icon": "lucide-tags", + "title": "标签" + } + }, + { + "id": "49569b9a195f30ca", + "type": "leaf", + "state": { + "type": "outline", + "state": { + "file": "实验报告:实验一 单周期 RISC-V CPU 设计与实现.md", + "followCursor": false, + "showSearch": false, + "searchQuery": "" + }, + "icon": "lucide-list", + "title": "实验报告:实验一 单周期 RISC-V CPU 设计与实现 的大纲" + } + } + ] + } + ], + "direction": "horizontal", + "width": 300, + "collapsed": true + }, + "left-ribbon": { + "hiddenItems": { + "switcher:打开快速切换": false, + "graph:查看关系图谱": false, + "canvas:新建白板": false, + "daily-notes:打开/创建今天的日记": false, + "templates:插入模板": false, + "command-palette:打开命令面板": false, + "bases:创建新数据库": false + } + }, + "active": "faf84f74156974b0", + "lastOpenFiles": [ + "makepdf.bat", + "实验报告:实验一 单周期 RISC-V CPU 设计与实现.html", + "实验报告:实验一 单周期 RISC-V CPU 设计与实现.pdf", + "实验报告:实验一 单周期 RISC-V CPU 设计与实现.md", + "b1.png", + "实验报告:实验一 .pdf" + ] +} \ No newline at end of file diff --git a/lab1/实验报告/b1.png b/lab1/实验报告/b1.png new file mode 100644 index 0000000..1e81277 Binary files /dev/null and b/lab1/实验报告/b1.png differ diff --git a/lab1/实验报告/b2.png b/lab1/实验报告/b2.png new file mode 100644 index 0000000..66a464a Binary files /dev/null and b/lab1/实验报告/b2.png differ diff --git a/lab1/实验报告/b3.png b/lab1/实验报告/b3.png new file mode 100644 index 0000000..66c23b1 Binary files /dev/null and b/lab1/实验报告/b3.png differ diff --git a/lab1/实验报告/b41.png b/lab1/实验报告/b41.png new file mode 100644 index 0000000..745799c Binary files /dev/null and b/lab1/实验报告/b41.png differ diff --git a/lab1/实验报告/default-template.latex b/lab1/实验报告/default-template.latex new file mode 100644 index 0000000..fae4258 --- /dev/null +++ b/lab1/实验报告/default-template.latex @@ -0,0 +1,138 @@ +$passoptions.latex()$ +\documentclass[ +$for(babel-otherlangs)$ + $babel-otherlangs$, +$endfor$ +$if(babel-lang)$ + $babel-lang$, +$endif$ +$if(fontsize)$ + $fontsize$, +$endif$ +$if(papersize)$ + $papersize$paper, +$endif$ +$for(classoption)$ + $classoption$$sep$, +$endfor$ +]{$documentclass$} +\usepackage{xeCJK} % 处理中文字体 +\setCJKmainfont{微软雅黑} % 系统自带的中文字体(确保已安装,可选:宋体、黑体) +\usepackage{geometry} % 页边距设置 +\geometry{margin=1in} % 页边距 1 英寸(可调整) +\usepackage{hyperref} % 支持 PDF 书签 +\hypersetup{ + pdfencoding=UnicodeUTF8, % 解决中文书签乱码 + colorlinks=true % 可选:让链接显示颜色 +} +$if(beamerarticle)$ +\usepackage{beamerarticle} % needs to be loaded first +$endif$ +\usepackage{xcolor} +$if(geometry)$ +\usepackage[$for(geometry)$$geometry$$sep$,$endfor$]{geometry} +$endif$ +\usepackage{amsmath,amssymb} +$-- +$-- section numbering +$-- +$if(numbersections)$ +\setcounter{secnumdepth}{$if(secnumdepth)$$secnumdepth$$else$5$endif$} +$else$ +\setcounter{secnumdepth}{-\maxdimen} % remove section numbering +$endif$ +$fonts.latex()$ +$font-settings.latex()$ +$common.latex()$ +$for(header-includes)$ +$header-includes$ +$endfor$ +$after-header-includes.latex()$ +$hypersetup.latex()$ + +$if(title)$ +\title{$title$$if(thanks)$\thanks{$thanks$}$endif$} +$endif$ +$if(subtitle)$ +\usepackage{etoolbox} +\makeatletter +\providecommand{\subtitle}[1]{% add subtitle to \maketitle + \apptocmd{\@title}{\par {\large #1 \par}}{}{} +} +\makeatother +\subtitle{$subtitle$} +$endif$ +\author{$for(author)$$author$$sep$ \and $endfor$} +\date{$date$} + +\begin{document} +$if(has-frontmatter)$ +\frontmatter +$endif$ +$if(title)$ +\maketitle +$if(abstract)$ +\begin{abstract} +$abstract$ +\end{abstract} +$endif$ +$endif$ + +$for(include-before)$ +$include-before$ + +$endfor$ +$if(toc)$ +$if(toc-title)$ +\renewcommand*\contentsname{$toc-title$} +$endif$ +{ +$if(colorlinks)$ +\hypersetup{linkcolor=$if(toccolor)$$toccolor$$else$$endif$} +$endif$ +\setcounter{tocdepth}{$toc-depth$} +\tableofcontents +} +$endif$ +$if(lof)$ +\listoffigures +$endif$ +$if(lot)$ +\listoftables +$endif$ +$if(linestretch)$ +\setstretch{$linestretch$} +$endif$ +$if(has-frontmatter)$ +\mainmatter +$endif$ +$body$ + +$if(has-frontmatter)$ +\backmatter +$endif$ +$if(nocite-ids)$ +\nocite{$for(nocite-ids)$$it$$sep$, $endfor$} +$endif$ +$if(natbib)$ +$if(bibliography)$ +$if(biblio-title)$ +$if(has-chapters)$ +\renewcommand\bibname{$biblio-title$} +$else$ +\renewcommand\refname{$biblio-title$} +$endif$ +$endif$ +\bibliography{$for(bibliography)$$bibliography$$sep$,$endfor$} + +$endif$ +$endif$ +$if(biblatex)$ +\printbibliography$if(biblio-title)$[title=$biblio-title$]$endif$ + +$endif$ +$for(include-after)$ +$include-after$ + +$endfor$ +\end{document} diff --git a/lab1/实验报告/makepdf.bat b/lab1/实验报告/makepdf.bat new file mode 100644 index 0000000..c1a1dd1 --- /dev/null +++ b/lab1/实验报告/makepdf.bat @@ -0,0 +1,35 @@ +@echo off +:: 解决中文乱码(将注释单独成行,避免紧跟命令) +chcp 65001 > nul +:: 启用延迟扩展(确保变量正确解析) +setlocal enabledelayedexpansion + +:: 检查必要文件 +if not exist "*.md" ( + echo 错误:当前目录没有.md文件! + pause + exit /b 1 +) +if not exist "default-template.latex" ( + echo 错误:找不到default-template.latex! + pause + exit /b 1 +) + +:: 转换当前目录下所有.md文件 +for %%f in (*.md) do ( + echo 开始转换:%%f + set "filename=%%~nf" + :: 移除 -V geometry(解决选项冲突,后面解释) + pandoc "%%f" --template=default-template.latex --pdf-engine=xelatex --resource-path=. -o "!filename!.pdf" + if %errorlevel% equ 0 ( + echo 转换成功:!filename!.pdf + ) else ( + echo 转换失败:%%f + ) + echo ---------------------- +) + +endlocal +echo 所有文件处理完毕! +pause \ No newline at end of file diff --git a/lab1/实验报告/report.aux b/lab1/实验报告/report.aux new file mode 100644 index 0000000..b5c3fa8 --- /dev/null +++ b/lab1/实验报告/report.aux @@ -0,0 +1,51 @@ +\relax +\providecommand\hyper@newdestlabel[2]{} +\providecommand*\HyPL@Entry[1]{} +\HyPL@Entry{0<>} +\@writefile{toc}{\contentsline {section}{\numberline {1}实验目的}{1}{section.1}\protected@file@percent } +\@writefile{toc}{\contentsline {section}{\numberline {2}实验环境}{1}{section.2}\protected@file@percent } +\@writefile{toc}{\contentsline {section}{\numberline {3}阶段功能划分}{1}{section.3}\protected@file@percent } +\@writefile{toc}{\contentsline {section}{\numberline {4}模块实现与分析}{2}{section.4}\protected@file@percent } +\@writefile{toc}{\contentsline {subsection}{\numberline {4.1}取指}{2}{subsection.4.1}\protected@file@percent } +\@writefile{toc}{\contentsline {subsubsection}{\numberline {4.1.1}代码实现}{2}{subsubsection.4.1.1}\protected@file@percent } +\newlabel{lst:if_pc}{{1}{2}{取指模块PC更新逻辑}{lstlisting.1}{}} +\@writefile{lol}{\contentsline {lstlisting}{\numberline {1}取指模块PC更新逻辑}{2}{lstlisting.1}\protected@file@percent } +\@writefile{toc}{\contentsline {subsubsection}{\numberline {4.1.2}波形图}{3}{subsubsection.4.1.2}\protected@file@percent } +\@writefile{lof}{\contentsline {figure}{\numberline {4.1}{\ignorespaces 取指模块 (\texttt {InstructionFetch}) 波形图}}{3}{figure.4.1}\protected@file@percent } +\newlabel{fig:if_waveform}{{4.1}{3}{取指模块 (\texttt {InstructionFetch}) 波形图}{figure.4.1}{}} +\@writefile{toc}{\contentsline {subsection}{\numberline {4.2}译码}{3}{subsection.4.2}\protected@file@percent } +\@writefile{toc}{\contentsline {subsubsection}{\numberline {4.2.1}代码实现}{3}{subsubsection.4.2.1}\protected@file@percent } +\newlabel{lst:id_control}{{2}{3}{译码模块控制信号生成逻辑}{lstlisting.2}{}} +\@writefile{lol}{\contentsline {lstlisting}{\numberline {2}译码模块控制信号生成逻辑}{3}{lstlisting.2}\protected@file@percent } +\@writefile{toc}{\contentsline {subsubsection}{\numberline {4.2.2}波形图}{4}{subsubsection.4.2.2}\protected@file@percent } +\@writefile{lof}{\contentsline {figure}{\numberline {4.2}{\ignorespaces 译码模块 (\texttt {InstructionDecode}) 波形图}}{4}{figure.4.2}\protected@file@percent } +\newlabel{fig:id_waveform}{{4.2}{4}{译码模块 (\texttt {InstructionDecode}) 波形图}{figure.4.2}{}} +\@writefile{toc}{\contentsline {subsection}{\numberline {4.3}执行}{4}{subsection.4.3}\protected@file@percent } +\@writefile{toc}{\contentsline {subsubsection}{\numberline {4.3.1}波形图}{5}{subsubsection.4.3.1}\protected@file@percent } +\@writefile{lof}{\contentsline {figure}{\numberline {4.3}{\ignorespaces 执行模块 (\texttt {Execute}) 波形图}}{5}{figure.4.3}\protected@file@percent } +\newlabel{fig:ex_waveform}{{4.3}{5}{执行模块 (\texttt {Execute}) 波形图}{figure.4.3}{}} +\@writefile{toc}{\contentsline {subsubsection}{\numberline {4.3.2}代码实现}{5}{subsubsection.4.3.2}\protected@file@percent } +\newlabel{lst:ex_aluops}{{3}{5}{执行模块ALU操作数选择逻辑}{lstlisting.3}{}} +\@writefile{lol}{\contentsline {lstlisting}{\numberline {3}执行模块ALU操作数选择逻辑}{5}{lstlisting.3}\protected@file@percent } +\@writefile{toc}{\contentsline {subsection}{\numberline {4.4}CPU 模块}{6}{subsection.4.4}\protected@file@percent } +\@writefile{toc}{\contentsline {subsubsection}{\numberline {4.4.1}代码实现}{6}{subsubsection.4.4.1}\protected@file@percent } +\newlabel{lst:cpu_top}{{4}{6}{CPU模块部分连接逻辑}{lstlisting.4}{}} +\@writefile{lol}{\contentsline {lstlisting}{\numberline {4}CPU模块部分连接逻辑}{6}{lstlisting.4}\protected@file@percent } +\@writefile{toc}{\contentsline {section}{\numberline {5}测试与结果分析}{6}{section.5}\protected@file@percent } +\@writefile{toc}{\contentsline {subsection}{\numberline {5.1}单元测试:InstructionDecoderTest 分析}{6}{subsection.5.1}\protected@file@percent } +\@writefile{toc}{\contentsline {subsubsection}{\numberline {5.1.1}L-Type 指令测试分析}{6}{subsubsection.5.1.1}\protected@file@percent } +\@writefile{toc}{\contentsline {subsubsection}{\numberline {5.1.2}波形图分析}{7}{subsubsection.5.1.2}\protected@file@percent } +\@writefile{lof}{\contentsline {figure}{\numberline {5.1}{\ignorespaces \texttt {L-Type} 指令 \texttt {lw x10, 32(x5)} 的译码波形图}}{7}{figure.5.1}\protected@file@percent } +\newlabel{fig:lw_decode_waveform}{{5.1}{7}{\texttt {L-Type} 指令 \texttt {lw x10, 32(x5)} 的译码波形图}{figure.5.1}{}} +\@writefile{toc}{\contentsline {subsection}{\numberline {5.2}整体测试:CPUTest (以 \texttt {FibonacciTest} 为例)}{8}{subsection.5.2}\protected@file@percent } +\@writefile{toc}{\contentsline {subsubsection}{\numberline {5.2.1}\texttt {fibonacci.c} 程序分析}{8}{subsubsection.5.2.1}\protected@file@percent } +\newlabel{lst:fib_c}{{5}{8}{Fibonacci 递归程序 \texttt {fibonacci.c}}{lstlisting.5}{}} +\@writefile{lol}{\contentsline {lstlisting}{\numberline {5}Fibonacci 递归程序 \texttt {fibonacci.c}}{8}{lstlisting.5}\protected@file@percent } +\newlabel{lst:chisel_test}{{6}{8}{Chisel 测试代码片段}{lstlisting.6}{}} +\@writefile{lol}{\contentsline {lstlisting}{\numberline {6}Chisel 测试代码片段}{8}{lstlisting.6}\protected@file@percent } +\@writefile{lof}{\contentsline {figure}{\numberline {5.2}{\ignorespaces Fibonacci 程序执行末期及结果写入内存的波形图}}{9}{figure.5.2}\protected@file@percent } +\newlabel{fig:fibonacci_waveform}{{5.2}{9}{Fibonacci 程序执行末期及结果写入内存的波形图}{figure.5.2}{}} +\@writefile{toc}{\contentsline {subsubsection}{\numberline {5.2.2}波形图分析}{9}{subsubsection.5.2.2}\protected@file@percent } +\@writefile{toc}{\contentsline {section}{\numberline {6}遇到的问题与改进建议}{10}{section.6}\protected@file@percent } +\@writefile{toc}{\contentsline {section}{\numberline {7}实验结论}{10}{section.7}\protected@file@percent } +\gdef \@abspage@last{10} diff --git a/lab1/实验报告/report.pdf b/lab1/实验报告/report.pdf new file mode 100644 index 0000000..c1f1884 Binary files /dev/null and b/lab1/实验报告/report.pdf differ diff --git a/lab1/实验报告/report.tex b/lab1/实验报告/report.tex new file mode 100644 index 0000000..6c2cdd4 --- /dev/null +++ b/lab1/实验报告/report.tex @@ -0,0 +1,353 @@ +\documentclass[12pt]{ctexart} % 使用 ctexart 文档类支持中文,12pt 字号 +\usepackage[utf8]{inputenc} % 输入编码,保持兼容性 +\usepackage[margin=2.5cm]{geometry} % 设置页边距 +\usepackage{graphicx} % 导入图片 +\usepackage{amsmath} % 支持数学公式 +\usepackage{listings} % 代码块高亮 +\usepackage{xcolor} % 用于代码高亮颜色 +\usepackage{hyperref} % 目录、交叉引用可点击,生成PDF书签 +\hypersetup{ + colorlinks=true, % 这是关键,它会让链接文本以颜色显示,而不是边框 + linkcolor=black, % 内部链接(如目录、章节引用)的颜色设为黑色 + citecolor=green, % 引用文献的颜色(如果用不到可以忽略或设为黑色) + urlcolor=blue, % URL链接的颜色(如果用不到可以忽略或设为黑色) + filecolor=magenta, % 文件链接的颜色(如果用不到可以忽略或设为黑色) + % 可以添加更多其他 PDF 元数据,让 PDF 文件信息更完整 + pdftitle={实验一:单周期 RISC-V CPU 设计与实现}, + pdfauthor={朱梓涵}, + pdfsubject={RISC-V CPU 设计与实现报告}, + pdfkeywords={RISC-V, CPU, Chisel, 单周期, 实验报告} +} + % 目录、交叉引用可点击,生成PDF书签 +\usepackage{fancyhdr} % 自定义页眉页脚 +\usepackage{enumitem} % 列表项自定义 + +% --- 页眉页脚设置 --- +\pagestyle{fancy} +\fancyhf{} % 清除所有页眉页脚字段 +\fancyhead[L]{\MakeUppercase{实验一:单周期 RISC-V CPU 设计与实现}} % 左侧页眉:大写实验名称 +\fancyfoot[C]{\thepage} % 居中页脚:页码 +\renewcommand{\headrulewidth}{0.4pt} % 页眉下方的横线粗细 +\renewcommand{\footrulewidth}{0.4pt} % 页脚上方的横线粗细 + +% --- 标题信息 --- +\title{\vspace{-2cm}\textbf{实验一:单周期 RISC-V CPU 设计与实现}} % 标题,垂直间距调整 +\author{朱梓涵 \\ 学号:24325356} % 作者信息 +\date{\today} % 显示当前日期 + +% --- 代码高亮风格定义 (Solarized-light) --- +\definecolor{sol-base03}{HTML}{002b36} +\definecolor{sol-base02}{HTML}{073642} +\definecolor{sol-base01}{HTML}{586e75} +\definecolor{sol-base00}{HTML}{657b83} +\definecolor{sol-base0}{HTML}{839496} +\definecolor{sol-base1}{HTML}{93a1a1} +\definecolor{sol-base2}{HTML}{eee8d5} +\definecolor{sol-base3}{HTML}{fdf6e3} +\definecolor{sol-yellow}{HTML}{b58900} +\definecolor{sol-orange}{HTML}{cb4b16} +\definecolor{sol-red}{HTML}{dc322f} +\definecolor{sol-magenta}{HTML}{d33682} +\definecolor{sol-violet}{HTML}{6c71c4} +\definecolor{sol-blue}{HTML}{268bd2} +\definecolor{sol-cyan}{HTML}{2aa198} +\definecolor{sol-green}{HTML}{859900} + +\lstdefinestyle{ScalaChiselStyle}{ + commentstyle=\color{sol-base01}\itshape, + keywordstyle=\color{sol-green}\bfseries, + stringstyle=\color{sol-cyan}, + basicstyle=\ttfamily\small, + breakatwhitespace=false, + breaklines=true, + captionpos=b, + keepspaces=true, + numbers=none, + showspaces=false, + showstringspaces=false, + showtabs=false, + tabsize=2, + frame=single, + rulecolor=\color{black}, + % 添加 Chisel 相关关键字 + morekeywords={when, Mux, MuxLookup, IndexedSeq, U, io, :=, object, val, def, class, override, package, import, extends, with, Bits, UInt, SInt}, + literate={:}{{\color{sol-base02}:}}1 +} + +\lstdefinestyle{CStyle}{ + commentstyle=\color{sol-base01}\itshape, + keywordstyle=\color{sol-orange}\bfseries, + stringstyle=\color{sol-cyan}, + basicstyle=\ttfamily\small, + breakatwhitespace=false, + breaklines=true, + captionpos=b, + keepspaces=true, + numbers=none, + showspaces=false, + showstringspaces=false, + showtabs=false, + tabsize=2, + frame=single, + rulecolor=\color{black}, +} + +\lstset{style=ScalaChiselStyle} % 默认代码风格为 Scala/Chisel + +% --- 图片计数器与章节联动 --- +\counterwithin{figure}{section} +\counterwithin{table}{section} + +% --- 文档开始 --- +\begin{document} + +\maketitle % 生成标题 +\thispagestyle{empty} % 标题页无页码 + +% --- 正文开始 --- + +\section{实验目的} +本实验旨在深入理解 RISC-V 指令集架构 (ISA) 与单周期 CPU 的工作原理。实验目标包括: +\begin{enumerate}[label=\arabic*.] % 使用 enumerate 环境创建编号列表 + \item 设计并实现一个支持部分 RV32I 指令集的单周期 CPU。 + \item 掌握 CPU 经典五阶段(取指、译码、执行、访存、写回)在单周期模型下的数据通路和控制逻辑设计。 + \item 学习使用 Chisel 硬件描述语言进行模块化硬件设计。 + \item 通过编写单元测试和集成测试,利用 \texttt{chiseltest} 和 GTKWave 波形图验证 CPU 各模块及整体功能的正确性。 +\end{enumerate} + +\section{实验环境} +\begin{itemize} + \item \textbf{操作系统}: Windows 11 + \item \textbf{开发工具}: IntelliJ IDEA + \item \textbf{构建工具}: SBT + \item \textbf{仿真与测试}: Verilator, GTKWave +\end{itemize} + +\section{阶段功能划分} +本单周期 RISC-V CPU 依照经典的五级流水线概念进行设计,虽然是单周期实现,但其内部模块划分仍遵循五阶段的逻辑,确保数据通路和控制逻辑清晰。 +\begin{itemize} + \item \textbf{取指 (Instruction Fetch, IF)}: 程序计数器 (PC) 提供指令地址,指令存储器根据该地址输出 32 位指令。PC 在每个时钟周期默认加 4,若遇跳转或分支成功,则更新为目标地址。 + \item \textbf{译码 (Instruction Decode, ID)}: 译码单元解析指令,提取操作数(寄存器地址 \texttt{rs1}, \texttt{rs2})、目标寄存器地址 (\texttt{rd}) 和立即数。主控制器根据指令操作码生成后续阶段所需的所有控制信号。 + \item \textbf{执行 (Execute, EX)}: 算术逻辑单元 (ALU) 根据译码阶段生成的控制信号,对来自寄存器文件或立即数生成器的操作数进行计算。同时,分支跳转的地址计算和条件判断也在此阶段完成。 + \item \textbf{访存 (Memory Access, MEM)}: 根据译码阶段生成的访存控制信号,与数据存储器进行交互。Load 指令从此阶段读取数据,Store 指令则向此阶段写入数据。 + \item \textbf{写回 (Write Back, WB)}: 将执行结果或从数据存储器中读取的数据写回寄存器文件。写回的数据来源由控制信号决定。 +\end{itemize} + +\section{模块实现与分析} + +\subsection{取指} +\begin{itemize} + \item \textbf{功能}: 根据 \texttt{PC} 的当前值从指令存储器中取出指令,并计算下一周期的 \texttt{PC} 值。 + \item \textbf{实现要点}: \texttt{PC} 的更新逻辑是核心。它由跳转标志 \texttt{jump\_flag\_id} 控制:若跳转发生,\texttt{PC} 更新为 \texttt{jump\_address\_id};否则,顺序执行,\texttt{PC} 更新为 \texttt{PC + 4}。 +\end{itemize} + +\subsubsection{代码实现} +\begin{lstlisting}[caption={取指模块PC更新逻辑}, label={lst:if_pc}] +// lab1(InstructionFetch) +when(io.jump_flag_id) { + pc := io.jump_address_id +}.otherwise { + pc := pc + 4.U +} +// lab1(InstructionFetch) end +\end{lstlisting} + +\subsubsection{波形图} +\begin{figure}[htbp] + \centering + \includegraphics[width=0.9\textwidth]{b1.png} + \caption{取指模块 (\texttt{InstructionFetch}) 波形图} + \label{fig:if_waveform} +\end{figure} + +\subsection{译码} +\begin{itemize} + \item \textbf{功能}: 解析指令,生成立即数,并为后续阶段提供控制信号。 + \item \textbf{实现要点}: 控制信号的生成逻辑是关键。例如,\texttt{memory\_read\_enable} 仅在 L-type 指令时有效,\texttt{wb\_reg\_write\_source} 根据指令类型选择写回数据是来自 ALU、数据存储器还是 PC+4。 +\end{itemize} + +\subsubsection{代码实现} +\begin{lstlisting}[caption={译码模块控制信号生成逻辑}, label={lst:id_control}] +// lab1(InstructionDecode) +io.ex_aluop2_source := Mux(opcode === InstructionTypes.RM, + ALUOp2Source.Register, + ALUOp2Source.Immediate +) +io.memory_read_enable := opcode === InstructionTypes.L +io.memory_write_enable := opcode === InstructionTypes.S +io.wb_reg_write_source := MuxLookup( + opcode, + RegWriteSource.ALUResult +)( + IndexedSeq( + InstructionTypes.L -> RegWriteSource.Memory, + Instructions.jal -> RegWriteSource.NextInstructionAddress, + Instructions.jalr -> RegWriteSource.NextInstructionAddress + ) +) +// lab1(InstructionDecode) end +\end{lstlisting} + +\subsubsection{波形图} +\begin{figure}[htbp] + \centering + \includegraphics[width=0.9\textwidth]{b2.png} + \caption{译码模块 (\texttt{InstructionDecode}) 波形图} + \label{fig:id_waveform} +\end{figure} + +\subsection{执行} +\begin{itemize} + \item \textbf{功能}: 执行 ALU 运算,并处理分支和跳转逻辑。 + \item \textbf{实现要点}: ALU 的两个操作数 \texttt{op1} 和 \texttt{op2} 的来源由译码模块生成的 \texttt{aluop*\_source} 信号决定,实现了操作数的灵活选择。 +\end{itemize} + +\subsubsection{波形图} +\begin{figure}[htbp] + \centering + \includegraphics[width=0.9\textwidth]{b3.png} + \caption{执行模块 (\texttt{Execute}) 波形图} + \label{fig:ex_waveform} +\end{figure} + +\subsubsection{代码实现} +\begin{lstlisting}[caption={执行模块ALU操作数选择逻辑}, label={lst:ex_aluops}] +// lab1(Execute) +alu.io.func := alu_ctrl.io.alu_funct +alu.io.op1 := Mux( + io.aluop1_source === ALUOp1Source.Register, + io.reg1_data, + io.instruction_address +) +alu.io.op2 := Mux( + io.aluop2_source === ALUOp2Source.Register, + io.reg2_data, + io.immediate +) +// lab1(Execute) end +\end{lstlisting} + +\subsection{CPU 模块} +\begin{itemize} + \item \textbf{功能}: 实例化取指、译码、执行等所有子模块,并根据数据通路图将它们正确连接起来。 + \item \textbf{实现要点}: 确保数据流和控制流在模块间的正确传递是顶层连接的关键。 +\end{itemize} + +\subsubsection{代码实现} +\begin{lstlisting}[caption={CPU模块部分连接逻辑}, label={lst:cpu_top}] +// lab1(cpu) +ex.io.instruction := inst_fetch.io.instruction +ex.io.instruction_address := inst_fetch.io.instruction_address +ex.io.reg1_data := regs.io.read_data1 +ex.io.reg2_data := regs.io.read_data2 +ex.io.immediate := id.io.ex_immediate +ex.io.aluop1_source := id.io.ex_aluop1_source +ex.io.aluop2_source := id.io.ex_aluop2_source +// lab1(cpu) end +\end{lstlisting} + +\section{测试与结果分析} + +\subsection{单元测试:InstructionDecoderTest 分析} + +\subsubsection{L-Type 指令测试分析} +测试用例首先向译码模块的 \texttt{io.instruction} 端口输入一条 L-Type 指令。随后,测试代码使用 \texttt{.expect()} 方法断言模块的各个输出端口信号值是否与预设的期望值一致。 +对于 \texttt{lw} 指令,其核心功能是从内存读取数据并写回寄存器。因此: +\begin{itemize} + \item \texttt{memory\_read\_enable} 必须为 \texttt{true},以启动访存阶段的数据读取。 + \item \texttt{reg\_write\_enable} 必须为 \texttt{true},以允许最终结果写入寄存器。 + \item \texttt{wb\_reg\_write\_source} 必须是 \texttt{RegWriteSource.Memory},因为写回的数据源于数据存储器。 + \item \texttt{ex\_immediate} 必须是指令编码中包含的偏移量(此例中为 32),用于地址计算 \texttt{rs1 + imm}。 +\end{itemize} +这些 \texttt{expect} 值精确地定义了 L-Type 指令在译码阶段应生成的正确行为模式,确保了控制通路的正确性。 + +\subsubsection{波形图分析} +\begin{figure}[htbp] + \centering + \includegraphics[width=0.9\textwidth]{b2.png} + \caption{\texttt{L-Type} 指令 \texttt{lw x10, 32(x5)} 的译码波形图} + \label{fig:lw_decode_waveform} +\end{figure} +图 \ref{fig:lw_decode_waveform} 所示波形图展示了 \texttt{lw x10, 32(x5)} 指令(机器码 \texttt{0x02028503})的译码过程。在时钟上升沿之后: +\begin{itemize} + \item \texttt{id\_io\_instruction} 端口稳定为 \texttt{0x02028503}。 + \item \texttt{id\_io\_memory\_read\_enable} + 和 \texttt{id\_io\_reg\_write\_enable} 信号被置为高电平 (1)。 + \item \texttt{id\_io\_wb\_reg\_write\_source} 信号变为 \texttt{01} (二进制),对应 \texttt{RegWriteSource.Memory}。 + \item \texttt{id\_io\_ex\_immediate} 的值为 \texttt{0x00000020},即十进制的 32。 +\end{itemize} +波形图直观地验证了译码模块对于 L-Type 指令的响应与 \texttt{expect} 语句的预期完全一致。 + +\subsection{整体测试:CPUTest (以 \texttt{FibonacciTest} 为例)} + +\subsubsection{\texttt{fibonacci.c} 程序分析} +\begin{itemize} + \item \textbf{程序功能}: + 该 C 语言程序定义了一个递归函数 \texttt{fib(int a)},用于计算斐波那契数列的第 \texttt{a} 项。其逻辑为:如果 \texttt{a} 等于 1 或 2,则返回 1;否则,返回 \texttt{fib(a-1)} 与 \texttt{fib(a-2)} 之和。\texttt{main} 函数是程序的入口点,它调用 \texttt{fib(10)} 来计算斐波那契数列的第 10 项,并将最终结果(即 55)存储到内存地址 \texttt{0x4}。 + \begin{lstlisting}[language=C, caption={Fibonacci 递归程序 \texttt{fibonacci.c}}, label={lst:fib_c}, style=CStyle] +int fib(int a) { + if (a == 1 || a == 2) return 1; + return fib(a - 1) + fib(a - 2); +} + +int main() { + *(int *)(4) = fib(10); // 将 fib(10) 的结果写入内存地址 0x4 +} + \end{lstlisting} + \item \textbf{Chisel 测试检查方式}: + 测试框架首先将 \texttt{fibonacci.asmbin} 文件加载到 CPU 的指令存储器和数据存储器中。然后,它驱动 CPU 执行足够多的时钟周期(通过 \texttt{c.clock.step(1000)} 大循环),以确保递归计算和内存写入操作全部完成。在仿真结束时,测试代码通过读取数据存储器的调试端口: + \begin{lstlisting}[caption={Chisel 测试代码片段}, label={lst:chisel_test}] +c.io.mem_debug_read_address.poke(4.U) // 将调试地址设置为 0x4 +c.clock.step() // 步进一个时钟周期 +c.io.mem_debug_read_data.expect(55.U) // 期望从地址 0x4 读出的数据为 55 + \end{lstlisting} + 上述语句首先将调试端口的读取地址 \texttt{mem\_debug\_read\_address} 设置为 \texttt{0x4},然后步进一个时钟周期以便数据稳定,最后断言 \texttt{mem\_debug\_read\_data} 的值是否等于预期的 \texttt{55}。如果相等,则测试通过,验证了 CPU 能够正确执行递归计算并将结果写入指定内存。 +\end{itemize} + +\subsubsection{波形图分析} +\begin{figure}[htbp] + \centering + \includegraphics[width=0.9\textwidth]{b41.png} + \caption{Fibonacci 程序执行末期及结果写入内存的波形图} + \label{fig:fibonacci_waveform} +\end{figure} +图 \ref{fig:fibonacci_waveform} 展示了 \texttt{fibonacci} 程序执行的最后几个周期以及结果检查阶段。 +\begin{itemize} + \item \textbf{执行分析}: 在波形图的前半部分,可以看到 CPU 正在执行 \texttt{main} 函数中对 \texttt{fib(10)} 的调用,并最终将结果存储到内存地址 \texttt{0x4}。由于斐波那契数列的递归特性,PC 会在函数调用和返回的过程中不断跳转。在程序即将结束时,PC 会最终指向退出或停机指令。 + \item \textbf{结果检查}: \texttt{main} 函数的核心是 \texttt{*(int *)(4) = fib(10);} 这条语句,它会在 \texttt{fib(10)} 的计算结果(即 55)准备好后,将其写入数据存储器的 \texttt{0x4} 地址。 + 在波形图的最后几个周期,我们会观察到以下关键信号变化: + \begin{itemize} + \item \texttt{mem\_io\_memory\_write\_enable} 信号短暂置高(为 \texttt{1}),表明数据存储器发生了写入操作。 + \item \texttt{mem\_io\_memory\_bundle\_address} 信号会显示为 \texttt{0x00000004},确认写入目标是内存地址 \texttt{0x4}。 + \item \texttt{mem\_io\_memory\_bundle\_write\_data[31:0]} 信号会显示为 \texttt{0x00000037}(十六进制的 55),这是 \texttt{fib(10)} 的计算结果。 + \item 在这之后,由于测试使用 \texttt{c.io.mem\_debug\_read\_address.poke(4.U)} 来读取内存,波形图上 \texttt{mem\_io\_debug\_read\_address} 会变为 \texttt{0x4},同时 \texttt{io\_mem\_debug\_read\_data}(或类似输出调试信号)会稳定显示出 \texttt{0x00000037}。 + \end{itemize} +\end{itemize} +这些波形特征与 Chisel 测试中 \texttt{mem\_debug\_read\_data.expect(55.U)} 的断言完美吻合,证明 CPU 正确地执行了 \texttt{fibonacci} 程序并存储了预期结果。 + +\section{遇到的问题与改进建议} + +\begin{enumerate}[label=\arabic*.] + \item \textbf{问题:立即数生成规则复杂,指导文档未详细展开。} + 在实现译码模块时,RISC-V 不同指令格式(I/S/B/U/J)的立即数拼接方式各不相同,实验指导对此描述较为简略,导致初期实现困难。 + \begin{itemize} + \item \textbf{建议}:在实验指导中为每种指令格式提供一个具体的立即数拼接图示或示例代码,能极大帮助理解。 + \end{itemize} + + \item \textbf{问题:缺少对波形图调试的引导案例。} + 初次使用 GTKWave 进行硬件调试时,面对海量的信号线,难以快速定位关键信号。 + \begin{itemize} + \item \textbf{建议}:实验文档中可以附加一个简单的调试案例,例如追踪一条 \texttt{add} 指令的数据流,展示如何从取指 PC 开始,逐步添加 `instruction`, `reg\_read\_data`, `alu\_result`, `reg\_write\_data` 等信号,并解释它们在波形图上的时序关系。 + \end{itemize} + + \item \textbf{问题:部分模块接口和控制信号的设计意图不够明确。} + 例如,\texttt{ALUOp1Source} 和 \texttt{ALUOp2Source} 这类控制信号的设计初衷,需要通过阅读多个模块的代码才能完全理解其作用。 + \begin{itemize} + \item \textbf{建议}:在代码框架的注释中,对关键的 `object` 或 `Enum`(如 \texttt{ALUOp1Source}),增加注释来说明每个选项的用途和对应的指令场景。 + \end{itemize} +\end{enumerate} + +\section{实验结论} +通过本次实验,我成功设计并实现了一个功能基本完备的 RISC-V 单周期 CPU。在实现过程中,我深入理解了数据通路与控制信号在指令执行过程中的协同作用,并掌握了模块化的硬件设计思想。通过编写和分析单元测试与集成测试,我学会了如何利用 \texttt{chiseltest} 和波形图对硬件设计进行验证和调试。本次实验极大地加深了我对计算机组成原理中理论知识的实践理解,为后续更复杂的处理器设计打下了坚实的基础。 + +\end{document} diff --git a/lab1/实验报告/report.toc b/lab1/实验报告/report.toc new file mode 100644 index 0000000..95ebdf1 --- /dev/null +++ b/lab1/实验报告/report.toc @@ -0,0 +1,24 @@ +\contentsline {section}{\numberline {1}实验目的}{3}{section.1}% +\contentsline {section}{\numberline {2}实验环境}{3}{section.2}% +\contentsline {section}{\numberline {3}阶段功能划分}{3}{section.3}% +\contentsline {section}{\numberline {4}模块实现与分析}{4}{section.4}% +\contentsline {subsection}{\numberline {4.1}取指 (Instruction Fetch, IF)}{4}{subsection.4.1}% +\contentsline {subsubsection}{\numberline {4.1.1}代码实现}{4}{subsubsection.4.1.1}% +\contentsline {subsubsection}{\numberline {4.1.2}波形图}{4}{subsubsection.4.1.2}% +\contentsline {subsection}{\numberline {4.2}译码 (Instruction Decode, ID)}{4}{subsection.4.2}% +\contentsline {subsubsection}{\numberline {4.2.1}代码实现}{5}{subsubsection.4.2.1}% +\contentsline {subsubsection}{\numberline {4.2.2}波形图}{6}{subsubsection.4.2.2}% +\contentsline {subsection}{\numberline {4.3}执行 (Execute, EX)}{6}{subsection.4.3}% +\contentsline {subsubsection}{\numberline {4.3.1}代码实现}{6}{subsubsection.4.3.1}% +\contentsline {subsubsection}{\numberline {4.3.2}波形图}{7}{subsubsection.4.3.2}% +\contentsline {subsection}{\numberline {4.4}CPU 顶层模块}{7}{subsection.4.4}% +\contentsline {subsubsection}{\numberline {4.4.1}代码实现}{7}{subsubsection.4.4.1}% +\contentsline {section}{\numberline {5}测试与结果分析}{8}{section.5}% +\contentsline {subsection}{\numberline {5.1}单元测试:InstructionDecoderTest 分析}{8}{subsection.5.1}% +\contentsline {subsubsection}{\numberline {5.1.1}L-Type 指令测试分析}{8}{subsubsection.5.1.1}% +\contentsline {subsubsection}{\numberline {5.1.2}波形图分析}{8}{subsubsection.5.1.2}% +\contentsline {subsection}{\numberline {5.2}整体测试:CPUTest (以 \texttt {FibonacciTest} 为例)}{9}{subsection.5.2}% +\contentsline {subsubsection}{\numberline {5.2.1}\texttt {fibonacci.c} 程序分析}{9}{subsubsection.5.2.1}% +\contentsline {subsubsection}{\numberline {5.2.2}波形图分析}{10}{subsubsection.5.2.2}% +\contentsline {section}{\numberline {6}遇到的问题与改进建议}{11}{section.6}% +\contentsline {section}{\numberline {7}实验结论}{12}{section.7}% diff --git a/lab1/实验报告/实验报告:实验一 单周期 RISC-V CPU 设计与实现.html b/lab1/实验报告/实验报告:实验一 单周期 RISC-V CPU 设计与实现.html new file mode 100644 index 0000000..d4959ba --- /dev/null +++ b/lab1/实验报告/实验报告:实验一 单周期 RISC-V CPU 设计与实现.html @@ -0,0 +1,273 @@ +
+姓名:朱梓涵
+学号:24325356
本实验旨在深入理解 RISC-V 指令集架构 (ISA) 与单周期 CPU 的工作原理。实验目标包括:
+chiseltest 和 GTKWave 波形图验证 CPU 各模块及整体功能的正确性。功能: 根据 PC 的当前值从指令存储器中取出指令,并计算下一周期的 PC 值。
实现要点: PC 的更新逻辑是核心。它由跳转标志 jump_flag_id 控制:若跳转发生,PC 更新为 jump_address_id;否则,顺序执行,PC 更新为 PC + 4。
代码实现:
+// lab1(InstructionFetch)
+when(io.jump_flag_id) {
+ pc := io.jump_address_id
+}.otherwise {
+ pc := pc + 4.U
+}
+// lab1(InstructionFetch) end
+波形图:
+
功能: 解析指令,生成立即数,并为后续阶段提供控制信号。
+实现要点: 控制信号的生成逻辑是关键。例如,memory_read_enable 仅在 L-type 指令时有效,wb_reg_write_source 根据指令类型选择写回数据是来自 ALU、数据存储器还是 PC+4。
代码实现:
+// lab1(InstructionDecode)
+io.ex_aluop2_source := Mux(opcode === InstructionTypes.RM,
+ ALUOp2Source.Register,
+ ALUOp2Source.Immediate
+)
+io.memory_read_enable := opcode === InstructionTypes.L
+io.memory_write_enable := opcode === InstructionTypes.S
+io.wb_reg_write_source := MuxLookup(
+ opcode,
+ RegWriteSource.ALUResult
+)(
+ IndexedSeq(
+ InstructionTypes.L -> RegWriteSource.Memory,
+ Instructions.jal -> RegWriteSource.NextInstructionAddress,
+ Instructions.jalr -> RegWriteSource.NextInstructionAddress
+ )
+)
+// lab1(InstructionDecode) end
+波形图:
+
功能: 执行 ALU 运算,并处理分支和跳转逻辑。
+实现要点: ALU 的两个操作数 op1 和 op2 的来源由译码模块生成的 aluop*_source 信号决定,实现了操作数的灵活选择。
代码实现:
+// lab1(Execute)
+alu.io.func := alu_ctrl.io.alu_funct
+alu.io.op1 := Mux(
+ io.aluop1_source === ALUOp1Source.Register,
+ io.reg1_data,
+ io.instruction_address
+)
+alu.io.op2 := Mux(
+ io.aluop2_source === ALUOp2Source.Register,
+ io.reg2_data,
+ io.immediate
+)
+// lab1(Execute) end
+波形图:
+
功能: 实例化取指、译码、执行等所有子模块,并根据数据通路图将它们正确连接起来。
+实现要点: 确保数据流和控制流在模块间的正确传递是顶层连接的关键。例如,从取指模块输出的指令和指令地址,需要同时送往译码和执行模块。
+代码实现:
+// lab1(cpu)
+ex.io.instruction := inst_fetch.io.instruction
+ex.io.instruction_address := inst_fetch.io.instruction_address
+ex.io.reg1_data := regs.io.read_data1
+ex.io.reg2_data := regs.io.read_data2
+ex.io.immediate := id.io.ex_immediate
+ex.io.aluop1_source := id.io.ex_aluop1_source
+ex.io.aluop2_source := id.io.ex_aluop2_source
+// lab1(cpu) end
+L-Type 指令测试分析:
+测试用例首先向译码模块的 io.instruction 端口输入一条 L-Type 指令。随后,测试代码使用 .expect() 方法断言模块的各个输出端口信号值是否与预设的期望值一致。
对于 lw 指令,其核心功能是从内存读取数据并写回寄存器。因此:
memory_read_enable 必须为 true,以启动访存阶段的数据读取。reg_write_enable 必须为 true,以允许最终结果写入寄存器。wb_reg_write_source 必须是 RegWriteSource.Memory,因为写回的数据源于数据存储器。ex_immediate 必须是指令编码中包含的偏移量(此例中为 32),用于地址计算 rs1 + imm。expect 值精确地定义了 L-Type 指令在译码阶段应生成的正确行为模式,确保了控制通路的正确性。波形图分析:
+
图片展示了 lw x10, 32(x5) 指令(机器码 0x02028503)的译码过程。在时钟上升沿之后:
id_io_instruction 端口稳定为 0x02028503。id_io_memory_read_enable 和 id_io_reg_write_enable 信号被置为高电平 (1)。id_io_wb_reg_write_source 信号变为 01 (二进制),对应 RegWriteSource.Memory。id_io_ex_immediate 的值为 0x00000020,即十进制的 32。expect 语句的预期完全一致。FibonacciTest 为例) fibonacci.c 程序分析:
fib(int a),用于计算斐波那契数列的第 a 项。其逻辑为:如果 a 等于 1 或 2,则返回 1;否则,返回 fib(a-1) 与 fib(a-2) 之和。main 函数是程序的入口点,它调用 fib(10) 来计算斐波那契数列的第 10 项,并将最终结果(即 55)存储到内存地址 0x4。int fib(int a) {
+ if (a == 1 || a == 2) return 1;
+ return fib(a - 1) + fib(a - 2);
+}
+
+int main() {
+ *(int *)(4) = fib(10); // 将 fib(10) 的结果写入内存地址 0x4
+}
+fibonacci.asmbin 文件加载到 CPU 的指令存储器和数据存储器中。然后,它驱动 CPU 执行足够多的时钟周期(通过 c.clock.step(1000) 大循环),以确保递归计算和内存写入操作全部完成。在仿真结束时,测试代码通过读取数据存储器的调试端口:c.io.mem_debug_read_address.poke(4.U) // 将调试地址设置为 0x4
+c.clock.step() // 步进一个时钟周期
+c.io.mem_debug_read_data.expect(55.U) // 期望从地址 0x4 读出的数据为 55
+上述语句首先将调试端口的读取地址 mem_debug_read_address 设置为 0x4,然后步进一个时钟周期以便数据稳定,最后断言 mem_debug_read_data 的值是否等于预期的 55。如果相等,则测试通过,验证了 CPU 能够正确执行递归计算并将结果写入指定内存。波形图分析:
+
图片展示了 fibonacci 程序执行的最后几个周期以及结果检查阶段。
main 函数中对 fib(10) 的调用,并最终将结果存储到内存地址 0x4。由于斐波那契数列的递归特性,PC (inst_fetch_io_instruction_address) 会在函数调用和返回的过程中不断跳转。在程序即将结束时,PC 会最终指向退出或停机指令。main 函数的核心是 *(int *)(4) = fib(10); 这条语句,它会在 fib(10) 的计算结果(即 55)准备好后,将其写入数据存储器的 0x4 地址。mem_io_memory_write_enable 信号短暂置高(为 1),表明数据存储器发生了写入操作。mem_io_memory_bundle_address 信号会显示为 0x00000004,确认写入目标是内存地址 0x4。mem_io_memory_bundle_write_data[31:0] 信号会显示为 0x00000037(十六进制的 55),这是 fib(10) 的计算结果。c.io.mem_debug_read_address.poke(4.U) 来读取内存,波形图上 mem_io_debug_read_address 会变为 0x4,同时 io_mem_debug_read_data(或类似输出调试信号)会稳定显示出 0x00000037。mem_debug_read_data.expect(55.U) 的断言完美吻合,证明 CPU 正确地执行了 fibonacci 程序并存储了预期结果。问题:立即数生成规则复杂,指导文档未详细展开。
+在实现译码模块时,RISC-V 不同指令格式(I/S/B/U/J)的立即数拼接方式各不相同,实验指导对此描述较为简略,导致初期实现困难。
问题:缺少对波形图调试的引导案例。
+初次使用 GTKWave 进行硬件调试时,面对海量的信号线,难以快速定位关键信号。
add 指令的数据流,展示如何从取指 PC 开始,逐步添加 instruction, reg_read_data, alu_result, reg_write_data 等信号,并解释它们在波形图上的时序关系。问题:部分模块接口和控制信号的设计意图不够明确。
+例如,ALUOp1Source 和 ALUOp2Source 这类控制信号的设计初衷,需要通过阅读多个模块的代码才能完全理解其作用。
object 或 Enum(如 ALUOp1Source),增加注释来说明每个选项的用途和对应的指令场景。通过本次实验,我成功设计并实现了一个功能基本完备的 RISC-V 单周期 CPU。在实现过程中,我深入理解了数据通路与控制信号在指令执行过程中的协同作用,并掌握了模块化的硬件设计思想。通过编写和分析单元测试与集成测试,我学会了如何利用 chiseltest 和波形图对硬件设计进行验证和调试。本次实验极大地加深了我对计算机组成原理中理论知识的实践理解,为后续更复杂的处理器设计打下了坚实的基础。
+ +