diff --git a/articles/20220327-riscv-specs.md b/articles/20220327-riscv-specs.md new file mode 100644 index 0000000000000000000000000000000000000000..2a259fb23e2be305b99e0f19b3dcae6d10a91333 --- /dev/null +++ b/articles/20220327-riscv-specs.md @@ -0,0 +1,97 @@ + +> Author: iosdevlog +> Date: 2022/03/27 +> Project: + +# RISC-V Specs 简介 + +## 指令集架构 + +指令集架构(英语:Instruction Set Architecture,缩写为 `ISA`),又称指令集或指令集体系,是计算机体系结构中与程序设计有关的部分,包含了基本数据类型,指令集,寄存器,寻址模式,存储体系,中断,异常处理以及外部 I/O。指令集架构包含一系列的 `opcode` 即操作码(机器语言),以及由特定处理器执行的基本命令。 + +不同的处理器“家族”——例如 Intel IA-32 和 x86-64、IBM/Freescale Power 和 ARM 处理器家族——有不同的指令集架构。 + +指令集体系与微架构(一套用于执行指令集的微处理器设计方法)不同。使用不同微架构的电脑可以共享一种指令集。例如 Intel 的 Pentium 和 AMD 的 AMD Athlon,两者几乎采用相同版本的 `x86` 指令集体系,但是两者在内部设计上有本质的区别。 + +## 设计 ISA 的 7 种衡量标准 + +![设计 ISA 的 7 种衡量标准](images/riscv_specs/isa.png) + +* 成本: 集成电路 +* 简洁性: 缩小芯片面积 +* 性能: 𝑖𝑛𝑠𝑡𝑟𝑢𝑐𝑡𝑖𝑜𝑛𝑠 * 𝑐𝑦𝑐𝑙𝑒𝑠 * 𝑡𝑖𝑚𝑒 +* 架构/实现分离: 延迟分支 +* 提升空间: 添加自定义指令 +* 程序大小: 嵌入式 +* 于编程/编译/链接: 寄存器 + +## RISC-V + +![RISC-V](https://images.weserv.nl/?url=upload.wikimedia.org/wikipedia/commons/thumb/9/9a/RISC-V-logo.svg/2560px-RISC-V-logo.svg.png) + +| 属性 | 说明 | +| ----------- | ----------- | +| 推出年份 | 2010 | +| 设计公司 | 加州大学柏克莱分校 | +| 最新架构版本 | 2.2 | +| 是否开放架构 | 是 | +| 体系结构类型 | Load-store | +| 字长/寄存器资料宽度 | 32、64、128 | +| 字节序 | 小端序 | +| 指令编码长度 | 不定长度 | +| 指令集架构设计策略 | RISC | +| 扩展指令集 | M、A、F、D、Q、C、P | +| 分支预测结构 | 比较和分支 | +| 通用寄存器 | 16、32(包括一个始终为零的寄存器) | +| 浮点寄存器 | 32 | + +RISC-V(发音为“risk-five”)是一个基于精简指令集(RISC)原则的开源指令集架构(ISA),简易解释为开源软件运动相对应的一种“开源硬件”。 + +该项目 2010 年始于加州大学柏克莱分校,但许多贡献者是该大学以外的志愿者和行业工作者。 + +与大多数指令集相比,RISC-V 指令集可以自由地用于任何目的,允许任何人设计、制造和销售RISC-V 芯片和软件而不必支付给任何公司专利费。 + +虽然这不是第一个开源指令集,但它具有重要意义,因为其设计使其适用于现代计算设备(如仓库规模云计算机、高端移动电话和微小嵌入式系统)。 + +设计者考虑到了这些用途中的性能与功率效率。该指令集还具有众多支持的软件,这解决了新指令集通常的弱点。 + +RISC-V 指令集的设计考虑了小型、快速、低功耗的现实情况来实做,但并没有对特定的微架构做过度的设计。 + +## Unprivileged ISA 非特权 ISA + +* 模块化与增量型 ISA +* 保持向后的二进制兼容性 +* 模块化 + * 核心是一个名为 RV32I 的基础 ISA,运行一个完整的软件栈 + * RV32I 是固定的,永远不会改变 +* 惯例是把代表扩展的字母附加到指令集名称之后作为指示 + * 例如,RV32IMFD 将乘法(RV32M),单精度浮点(RV32F)和双精度浮点 (RV32D)的扩展添加了基础指令集(RV32I)中 + +## RISC-V 指令格式 + +![instruction](images/riscv_specs/instruction.png) + +## RISC-V 寻址模式 + +![address](images/riscv_specs/address.png) + +1. 立即数寻址 + * 操作数是操作本身的常量 +1. 寄存器寻址 + * 操作数在寄存器 +1. 基址寻址 + * 操作数于内存中,其地址是寄存 器和指令中的常量之和 +1. PC 相对寻址 + * 分支地址是 PC 和指令中常量之和 + +## RISC-V 基础整数指令集 + +**寄存器** + +![register](images/riscv_specs/register.png) + +## 参考文档 + +1. [RISC-V Platform](https://github.com/riscv/riscv-platform-specs/blob/main/riscv-platform-spec.adoc/) +2. [D1_SDK_Howto](https://linux-sunxi.org/D1_SDK_Howto) +3. [ADB Download - Get the latest version of ADB and fastboot](https://adbdownload.com/) diff --git a/articles/images/riscv_atomics/riscv-aqrl.drawio.png b/articles/images/riscv_atomics/riscv-aqrl.drawio.png old mode 100755 new mode 100644 diff --git a/articles/images/riscv_specs/address.png b/articles/images/riscv_specs/address.png new file mode 100644 index 0000000000000000000000000000000000000000..e5fd22c3c447b3f0acaba396d887f32f086f4635 Binary files /dev/null and b/articles/images/riscv_specs/address.png differ diff --git a/articles/images/riscv_specs/instruction.png b/articles/images/riscv_specs/instruction.png new file mode 100644 index 0000000000000000000000000000000000000000..cd2e30dbd3d7351d71d3d24720ac336d54c90d31 Binary files /dev/null and b/articles/images/riscv_specs/instruction.png differ diff --git a/articles/images/riscv_specs/isa.png b/articles/images/riscv_specs/isa.png new file mode 100644 index 0000000000000000000000000000000000000000..07ac083f428878399713dfbcbb54b9422d014e17 Binary files /dev/null and b/articles/images/riscv_specs/isa.png differ diff --git a/articles/images/riscv_specs/register.png b/articles/images/riscv_specs/register.png new file mode 100644 index 0000000000000000000000000000000000000000..e5ff4bf0fdb829b753414fa2e6b97ac0c572fd86 Binary files /dev/null and b/articles/images/riscv_specs/register.png differ diff --git a/test/microbench/logs/D1-H-nezha-rv64imafdcvu-sv39-riscv64-20220330-123559-O1.log b/test/microbench/logs/D1-H-nezha-rv64imafdcvu-sv39-riscv64-20220330-123559-O1.log new file mode 100644 index 0000000000000000000000000000000000000000..dff76ad63973347a3fbb3b50ba37fbc24e6647a4 --- /dev/null +++ b/test/microbench/logs/D1-H-nezha-rv64imafdcvu-sv39-riscv64-20220330-123559-O1.log @@ -0,0 +1,70 @@ +cd benchmark && \ +git checkout -- test/CMakeLists.txt && \ +sed -i -e "/compile_benchmark_test(basic_test)/icompile_benchmark_test(riscv64)" test/CMakeLists.txt && \ +sed -i -e "/compile_benchmark_test(basic_test)/iadd_test(NAME riscv64 COMMAND riscv64 --benchmark_min_time=0.01)\n" test/CMakeLists.txt +touch benchmark/test/riscv64.dep +cd benchmark && cmake -DCMAKE_BUILD_TYPE=Release -DBENCHMARK_DOWNLOAD_DEPENDENCIES=on -DGOOGLETEST_PATH=/root/riscv-linux/test/microbench/benchmark/build/third_party/googletest -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DCMAKE_C_FLAGS="" -DCMAKE_CXX_FLAGS="" -S . -B "build" +-- git version: v1.6.1-38-g60b16f11-dirty normalized to 1.6.1.38 +-- Version: 1.6.1.38 +-- Performing Test HAVE_STD_REGEX -- success +-- Performing Test HAVE_GNU_POSIX_REGEX -- failed to compile +-- Performing Test HAVE_POSIX_REGEX -- success +-- Performing Test HAVE_STEADY_CLOCK -- success +-- Looking for Google Test sources +-- Looking for Google Test sources in /root/riscv-linux/test/microbench/benchmark/build/third_party/googletest +33mCMake Warning at CMakeLists.txt:37 (message): + Did not find Google Test sources! Fetching from web... + +0m +-- Configuring done +-- Generating done +-- Build files have been written to: /root/riscv-linux/test/microbench/benchmark/build/third_party/googletest +gmake[2]: Entering directory '/root/riscv-linux/test/microbench/benchmark/build/third_party/googletest' +gmake[3]: Entering directory '/root/riscv-linux/test/microbench/benchmark/build/third_party/googletest' +gmake[4]: Entering directory '/root/riscv-linux/test/microbench/benchmark/build/third_party/googletest' +gmake[4]: Leaving directory '/root/riscv-linux/test/microbench/benchmark/build/third_party/googletest' +gmake[4]: Entering directory '/root/riscv-linux/test/microbench/benchmark/build/third_party/googletest' +[ 11%] 34m1mPerforming update step for 'googletest'0m +[ 22%] 34m1mNo patch step for 'googletest'0m +[ 33%] 34m1mNo configure step for 'googletest'0m +[ 44%] 34m1mNo build step for 'googletest'0m +[ 55%] 34m1mNo install step for 'googletest'0m +[ 66%] 34m1mNo test step for 'googletest'0m +[ 77%] 34m1mCompleted 'googletest'0m +gmake[4]: Leaving directory '/root/riscv-linux/test/microbench/benchmark/build/third_party/googletest' +[100%] Built target googletest +gmake[3]: Leaving directory '/root/riscv-linux/test/microbench/benchmark/build/third_party/googletest' +gmake[2]: Leaving directory '/root/riscv-linux/test/microbench/benchmark/build/third_party/googletest' +-- Could NOT find Python (missing: Python_EXECUTABLE Interpreter) +-- Configuring done +-- Generating done +-- Build files have been written to: /root/riscv-linux/test/microbench/benchmark/build +cd benchmark && cmake --build "build" --config Release --target riscv64 +35m1mConsolidate compiler generated dependencies of target benchmark0m +[ 83%] Built target benchmark +35m1mConsolidate compiler generated dependencies of target benchmark_main0m +[ 91%] Built target benchmark_main +35m1mConsolidate compiler generated dependencies of target riscv640m +[ 95%] 32mBuilding CXX object test/CMakeFiles/riscv64.dir/riscv64.cc.o0m +[100%] 32m1mLinking CXX executable riscv640m +[100%] Built target riscv64 +benchmark/build/test/riscv64 +2022-03-30T12:36:23+00:00 +Running benchmark/build/test/riscv64 +Run on (1 X 1008 MHz CPU ) +Load Average: 1.34, 1.09, 1.05 +------------------------------------------------------------------------- +Benchmark Time CPU Iterations +------------------------------------------------------------------------- +BM_nop 3.04 ns 3.00 ns 233102365 +BM_ub 3.55 ns 3.50 ns 199748032 +BM_bnez 3.06 ns 3.00 ns 233100041 +BM_beqz 3.04 ns 3.00 ns 233037089 +BM_load_bnez 4.05 ns 4.00 ns 174797235 +BM_load_beqz 4.06 ns 4.00 ns 174791178 +BM_cache_miss_load_bnez 8.09 ns 4.00 ns 174642329 +BM_cache_miss_load_beqz 8.09 ns 4.01 ns 174668293 +BM_branch_miss_load_bnez 8.12 ns 4.01 ns 174707219 +BM_branch_miss_load_beqz 8.10 ns 4.01 ns 174678174 +BM_cache_branch_miss_load_bnez 8.13 ns 4.01 ns 174812969 +BM_cache_branch_miss_load_beqz 8.13 ns 4.01 ns 174722775 diff --git a/test/microbench/logs/D1-H-nezha-rv64imafdcvu-sv39-riscv64-20220330-123822-O0.log b/test/microbench/logs/D1-H-nezha-rv64imafdcvu-sv39-riscv64-20220330-123822-O0.log new file mode 100644 index 0000000000000000000000000000000000000000..c7c3029e5d4946044de57916639c3e1806c2471a --- /dev/null +++ b/test/microbench/logs/D1-H-nezha-rv64imafdcvu-sv39-riscv64-20220330-123822-O0.log @@ -0,0 +1,70 @@ +cd benchmark && \ +git checkout -- test/CMakeLists.txt && \ +sed -i -e "/compile_benchmark_test(basic_test)/icompile_benchmark_test(riscv64)" test/CMakeLists.txt && \ +sed -i -e "/compile_benchmark_test(basic_test)/iadd_test(NAME riscv64 COMMAND riscv64 --benchmark_min_time=0.01)\n" test/CMakeLists.txt +touch benchmark/test/riscv64.dep +cd benchmark && cmake -DCMAKE_BUILD_TYPE=Release -DBENCHMARK_DOWNLOAD_DEPENDENCIES=on -DGOOGLETEST_PATH=/root/riscv-linux/test/microbench/benchmark/build/third_party/googletest -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DCMAKE_C_FLAGS="" -DCMAKE_CXX_FLAGS="" -S . -B "build" +-- git version: v1.6.1-38-g60b16f11-dirty normalized to 1.6.1.38 +-- Version: 1.6.1.38 +-- Performing Test HAVE_STD_REGEX -- success +-- Performing Test HAVE_GNU_POSIX_REGEX -- failed to compile +-- Performing Test HAVE_POSIX_REGEX -- success +-- Performing Test HAVE_STEADY_CLOCK -- success +-- Looking for Google Test sources +-- Looking for Google Test sources in /root/riscv-linux/test/microbench/benchmark/build/third_party/googletest +33mCMake Warning at CMakeLists.txt:37 (message): + Did not find Google Test sources! Fetching from web... + +0m +-- Configuring done +-- Generating done +-- Build files have been written to: /root/riscv-linux/test/microbench/benchmark/build/third_party/googletest +gmake[2]: Entering directory '/root/riscv-linux/test/microbench/benchmark/build/third_party/googletest' +gmake[3]: Entering directory '/root/riscv-linux/test/microbench/benchmark/build/third_party/googletest' +gmake[4]: Entering directory '/root/riscv-linux/test/microbench/benchmark/build/third_party/googletest' +gmake[4]: Leaving directory '/root/riscv-linux/test/microbench/benchmark/build/third_party/googletest' +gmake[4]: Entering directory '/root/riscv-linux/test/microbench/benchmark/build/third_party/googletest' +[ 11%] 34m1mPerforming update step for 'googletest'0m +[ 22%] 34m1mNo patch step for 'googletest'0m +[ 33%] 34m1mNo configure step for 'googletest'0m +[ 44%] 34m1mNo build step for 'googletest'0m +[ 55%] 34m1mNo install step for 'googletest'0m +[ 66%] 34m1mNo test step for 'googletest'0m +[ 77%] 34m1mCompleted 'googletest'0m +gmake[4]: Leaving directory '/root/riscv-linux/test/microbench/benchmark/build/third_party/googletest' +[100%] Built target googletest +gmake[3]: Leaving directory '/root/riscv-linux/test/microbench/benchmark/build/third_party/googletest' +gmake[2]: Leaving directory '/root/riscv-linux/test/microbench/benchmark/build/third_party/googletest' +-- Could NOT find Python (missing: Python_EXECUTABLE Interpreter) +-- Configuring done +-- Generating done +-- Build files have been written to: /root/riscv-linux/test/microbench/benchmark/build +cd benchmark && cmake --build "build" --config Release --target riscv64 +35m1mConsolidate compiler generated dependencies of target benchmark0m +[ 83%] Built target benchmark +35m1mConsolidate compiler generated dependencies of target benchmark_main0m +[ 91%] Built target benchmark_main +35m1mConsolidate compiler generated dependencies of target riscv640m +[ 95%] 32mBuilding CXX object test/CMakeFiles/riscv64.dir/riscv64.cc.o0m +[100%] 32m1mLinking CXX executable riscv640m +[100%] Built target riscv64 +benchmark/build/test/riscv64 +2022-03-30T12:38:47+00:00 +Running benchmark/build/test/riscv64 +Run on (1 X 1008 MHz CPU ) +Load Average: 1.43, 1.21, 1.10 +------------------------------------------------------------------------- +Benchmark Time CPU Iterations +------------------------------------------------------------------------- +BM_nop 10.1 ns 10.0 ns 69957559 +BM_ub 10.2 ns 10.0 ns 69873414 +BM_bnez 10.1 ns 10.0 ns 69937321 +BM_beqz 10.1 ns 10.0 ns 69938862 +BM_load_bnez 12.2 ns 12.0 ns 58255983 +BM_load_beqz 12.2 ns 12.0 ns 58298861 +BM_cache_miss_load_bnez 24.3 ns 12.0 ns 58107258 +BM_cache_miss_load_beqz 24.3 ns 12.0 ns 58252024 +BM_branch_miss_load_bnez 24.2 ns 12.0 ns 58267620 +BM_branch_miss_load_beqz 24.3 ns 12.0 ns 58236516 +BM_cache_branch_miss_load_bnez 24.4 ns 12.0 ns 58232276 +BM_cache_branch_miss_load_beqz 24.7 ns 12.0 ns 58142354