388 Star 2.7K Fork 722

liangkangnan / tinyriscv

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

与本项目配套的设计文档《从零开始写RISC-V处理器》,已经更新完成。

本项目目前一共有3个分支,分别是master、bram和verilator,分别对应入门、进阶和高级三个级别,大家可以根据自身情况选择。

本项目是一直在更新的,verilator分支持续开发中,如果哪天不再更新了,我会特别说明的。

如果对本项目有疑问或者建议的话,优先通过邮件交流,谢谢。

1.初衷

本开源项目的初衷是本人想入门RISC-V,熟悉RISC-V的指令内容和汇编语法。

本人对RISC-V很感兴趣,很看好RISC-V的发展前景,觉得RISC-V就是CPU中的Linux。由于RISC-V是这两年才开始迅速发展的,因此关于RISC-V的学习参考资料目前还很少,特别是适合入门的资料,因此学习起来进度很缓慢,于是萌生了自己从零开始写RISC-V处理器核的想法。

本人是一名FPGA小白,为了快速入门、深入掌握RISC-V,我开始了学习FPGA和verilog的"艰难"历程。我工作的内容是和嵌入式软件相关的,平时根本不会接触到FPGA,也不会用到RISC-V,因此只能用业余时间来学习RISC-V。

网上有不少关于RISC-V的开源项目,但是大多都写得很"高深",对于我这种小白来说学习起来是非常吃力的,不太适合入门。本项目目前的代码量非常少,是很简单易懂的,对于想入门RISC-V的同学来说是一个很好的参考,希望能够吸引更多的同学参与到RISC-V的学习中来,促进RISC-V的发展,如果能起到抛砖引玉的作用的话那就更好了,也许说是砖的话就有点夸大了,但哪怕是起到一颗沙子的作用,也就足矣。

2.介绍

本项目实现的是一个单核32位的小型RISC-V处理器核(tinyriscv),采用verilog语言编写。设计目标是对标ARM Cortex-M3系列处理器。tinyriscv有以下特点:

  1. 支持RV32IM指令集,通过RISC-V指令兼容性测试;
  2. 采用三级流水线,即取指,译码,执行;
  3. 可以运行C语言程序;
  4. 支持JTAG,可以通过openocd读写内存(在线更新程序);
  5. 支持中断;
  6. 支持总线;
  7. 支持FreeRTOS;
  8. 支持通过串口更新程序;
  9. 容易移植到任何FPGA平台(如果资源足够的话);

项目中的各目录说明:

rtl:该目录包含tinyriscv的所有verilog源码;

sim:该目录包含仿真批处理bat文件和脚本;

tests:该目录包含测试程序源码,其中example目录为C语言程序例程源码,isa目录为RV32指令测试源码;

tools:该目录包含编译汇编和C语言程序所需GNU工具链和将二进制文件转成仿真所需的mem格式文件的脚本,还有通过串口下载程序的脚本。

pic:存放图片;

tb:该目录包含仿真的testbench文件;

fpga:存放FPGA相关文件,比如约束文件;

tinyriscv的整体框架如下:

tinyriscv整体框架

tinyriscv目前外挂了6个外设,每个外设的空间大小为256MB,地址空间分配如下图所示:

地址空间分配

3.CoreMark测试

目前tinyriscv在Xilinx Artix-7 35T FPGA平台(时钟50MHz)上运行CoreMark跑分程序的结果如下图所示:

tinyriscv跑分

可知,tinyriscv的跑分成绩为2.4。

选了几款其他MCU的跑分结果如下图所示:

其他MCU跑分结果

更多MCU的跑分结果,可以到coremark官网查询。

4.如何使用

本项目可以运行在Windows和Linux平台(macOS平台理论上也是可以的),编译仿真工具使用的是iverilog和vpp,波形查看工具使用的是gtkwave。

4.1Windows平台环境搭建

在使用之前需要安装以下工具:

  1. 安装iverilog工具

可以在这里http://bleyer.org/icarus/下载,安装过程中记得同意把iverilog添加到环境变量中,当然也可以在安装完成后手动进行添加。安装完成后iverilog、vvp和gtkwave等工具也就安装好了。

  1. 安装GNU工具链

可以通过百度网盘下载(链接: https://pan.baidu.com/s/1bYgslKxHMjtiZtIPsB2caQ 提取码: 9n3c),或者通过微云下载https://share.weiyun.com/5bMOsu9,下载完成后将压缩包解压到本项目的tools目录下。注意目录的层次结构,解压后的工具路径应该如下所示:

tinyriscv\tools\gnu-mcu-eclipse-riscv-none-gcc-8.2.0-2.2-20190521-0004-win64\bin\riscv-none-embed-gcc

  1. 安装make工具

可以通过百度网盘下载(链接: https://pan.baidu.com/s/1nFaUIwv171PDXuF7TziDFg 提取码: 9ntc),或者通过微云下载https://share.weiyun.com/59xtmWR,下载完成后直接解压,然后将make所在的路径添加到环境变量里。

  1. 安装python3

python官网下载win版本的python,注意要下载python3版本的。网速慢的同学可以通过百度网盘下载(链接: https://pan.baidu.com/s/1gNC8L5dZTsN6E5TJD2rmnQ 提取码: 3b4t),或者通过微云下载https://share.weiyun.com/XwzSQHND。安装完后将python添加到环境变量里。

  1. 下载tinyriscv代码

使用git clone命令下载,不要使用zip方式下载,否则有些文件会有格式问题。

git clone https://gitee.com/liangkangnan/tinyriscv.git

4.2Linux平台环境搭建

这里以Ubuntu系统为例进行说明。

  1. 下载iverilog源码

git clone https://github.com/steveicarus/iverilog.git

  1. 切换到v11分支(必须用V11或以上的版本)

git checkout v11-branch

  1. 安装依赖

sudo apt-get install autoconf gperf flex bison build-essential

  1. 编译、安装iverilog和vvp
sh autoconf.sh
./configure
make
make install
  1. 创建python软链接

sudo ln -s /usr/bin/python3.8 /usr/bin/python

其中/usr/bin/python3.8对应你实际安装的python版本。

  1. 安装gtkwave

sudo apt-get install gtkwave

  1. 安装GNU工具链

可以从百度网盘下载(链接:https://pan.baidu.com/s/10uLZYf_cgtH94kZI_N6JhQ 提取码:uk0w)或者自行下载其他版本的,下载后解压到tools目录下。

最后,需要确认tests/example/common.mk文件里的这几行内容要与你安装的工具链对应得上。

toolchain

4.3运行指令测试程序

这里以Windows平台为例进行说明,Linux平台是类似的。

4.3.1 运行旧的指令测试程序

旧的指令测试程序属于比较早的指令兼容性测试方法,虽然目前RISC-V官方已经不更新了,但仍然是一个比较好的测试参考。

下面以add指令为例,说明如何运行旧的指令测试程序。

打开CMD窗口,进入到sim目录,执行以下命令:

python .\sim_new_nowave.py ..\tests\isa\generated\rv32ui-p-add.bin inst.data

如果运行成功的话就可以看到"PASS"的打印。其他指令使用方法类似。

也可以一次性对所有指令进行测试,方法如下。

打开CMD窗口进入到sim目录下,执行以下命令:

python .\test_all_isa.py

4.3.2运行新的指令测试程序

新的指令兼容性(riscv-compliance)测试项相对于旧的指令兼容性测试项来说对指令的测试更加严谨,可以精确到每一条指令的运行结果,而且RISC-V官方一直在更新。

下面以add指令为例,说明如何运行新的指令测试程序。

打开CMD窗口,进入到sim/compliance_test目录,执行以下命令:

python .\compliance_test.py ..\..\tests\riscv-compliance\build_generated\rv32i\I-ADD-01.elf.bin inst.data

如果运行成功的话就可以看到"PASS"的打印。其他指令使用方法类似。

new_test_output

4.4运行C语言程序

C语言程序例程位于tests\example目录里。

下面以simple程序为例进行说明。

打开CMD窗口,进入到tests\example\simple目录,执行以下命令编译:

make

编译成功之后,进入到sim目录,执行以下命令开始测试:

python .\sim_new_nowave.py ..\tests\example\simple\simple.bin inst.data

5.移植到FPGA

详细的移植方法请查看本项目下的fpga目录中的README.md文件。

6.未来计划

  1. 写设计文档;
  2. 优化资源和功耗(主要是译码和执行部分);
  3. 支持硬件中断嵌套和快速实时中断;
  4. ......

7.更新记录

2020-10-25:支持Linux平台编译、仿真。新建bram分支,使用BRAM代替LUTRAM(DRAM),节省一半以上的LUT资源,并且可以运行更大的程序。

2020-07-04:支持通过UART烧写固件;

2020-05-27:增加新的指令兼容性(riscv-compliance)测试项。

2020-05-05:支持spi master,增加spi测试例程。

2020-04-25:支持FreeRTOS(v10.3.1)。

2020-04-18:适当添加代码注释;优化中断管理模块。

2020-04-11:增加CoreMark跑分例程和跑分成绩。

2020-04-05:支持CSR指令。

2020-03-29:重大更新,主要更新如下:

  1. 支持RIB(RISC-V Internal Bus)总线;
  2. 优化乘法代码,节省了2/3的DSP资源;
  3. 优化除法代码,解决了除法模块的BUG;
  4. 完善C语言例程、启动代码和链接脚本;
  5. 增加一次性对所有指令进行测试的脚本;

2020-03-08:支持中断,为此增加了timer模块来验证。

2020-03-01:支持JTAG,配合openocd可进行内存读写。JTAG文档参考深入浅出RISC-V调试

2020-02-23:支持在Xilinx Artix-7平台上运行。详见tinyriscv_vivado

2020-01-13:支持RV32M的除法指令。其C语言实现详见div

2020-01-02:支持RV32M的乘法指令。

2019-12-06:第一次发布。

8.其他

如有疑问或者建议,欢迎私信或者发邮件(liangkangnan@163.com)给我。

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright Blue Liang, liangkangnan@163.com Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

一个从零开始写的极简、非常易懂的RISC-V处理器核。 展开 收起
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Verilog
1
https://gitee.com/liangkangnan/tinyriscv.git
git@gitee.com:liangkangnan/tinyriscv.git
liangkangnan
tinyriscv
tinyriscv
master

搜索帮助

14c37bed 8189591 565d56ea 8189591