# Beginner Risc-V CPU Branch Prediction Q

**URL:** <https://forums.sifive.com/t/beginner-risc-v-cpu-branch-prediction-q/3714>\
**Category:** RISC-V\
**Created:** [May 29, 2020, 12:25am UTC](https://forums.sifive.com/t/beginner-risc-v-cpu-branch-prediction-q/3714 "2020-05-29T00:25:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheGreatNed](https://avatars.discourse-cdn.com/v4/letter/t/c5a1d2/32.png) [@TheGreatNed](https://forums.sifive.com/u/TheGreatNed)\
**Post date:** [May 29, 2020, 12:25am UTC](https://forums.sifive.com/t/beginner-risc-v-cpu-branch-prediction-q/3714/1 "2020-05-29T00:25:09Z")

</div>

Hi!

I am making a very simple RV32I CPU in verilog. No pipelining, all instructions executed immediately at the next rising edge of the clock (single cycle execution for everything).

I just want to make sure:

1. Since it’s single-cycle, there is no ‘fetch-decode-execute’ stages (obviously…?)
2. That means that there is no such thing as Branch Prediction, since jumps cannot be optimized beyond 1 cycle
3. That also means there is no need for a RAS (Return Address Stack). It will provide no benefit.

Lastly, are there any C compilers that will work with a simple custom core like this?

Tyvm!!

---

<div class="post-metadata">

**Author:** ![jimw](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@jimw](https://forums.sifive.com/u/jimw)\
**Post date:** [May 29, 2020, 1:11am UTC](https://forums.sifive.com/t/beginner-risc-v-cpu-branch-prediction-q/3714/2 "2020-05-29T01:11:48Z")

</div>

gcc supports -march=rv32i, or you can use the configure option --with-arch=rv32i to get a gcc build that supports rv32i by default. You will probably also need -mabi=ilp32 to specify the ABI in use, or --with-abi=ilp32 to specify it at configure time. SiFive has prebuilt compilers on its web site, or you can build your own using sources from our github sifive/freedom-tools tree, or from the RISC-V International sources in github riscv/riscv-gnu-toolchain. The latter is simpler to use.

---

<div class="post-metadata">

**Author:** ![bruce](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.sifive.com/bruce/32/452_2.png) [@bruce](https://forums.sifive.com/u/bruce)\
**Post date:** [May 29, 2020, 10:43am UTC](https://forums.sifive.com/t/beginner-risc-v-cpu-branch-prediction-q/3714/3 "2020-05-29T10:43:32Z")

</div>

I don’t know whether you’d be interested in looking at another core along the same lines?

> **[hamsternz/Rudi-RV32I](https://github.com/hamsternz/Rudi-RV32I)**
>
> A rudimental RISCV CPU supporting RV32I instructions, in VHDL - hamsternz/Rudi-RV32I

As Jim says, gcc is happy to make RV32I code and will generate library calls for multiplication and division and floating point (if used). The NewLib “embedded” runtime library is also happy with this.

---

<div class="post-metadata">

**Author:** ![TheGreatNed](https://avatars.discourse-cdn.com/v4/letter/t/c5a1d2/32.png) [@TheGreatNed](https://forums.sifive.com/u/TheGreatNed)\
**Post date:** [May 30, 2020, 11:34pm UTC](https://forums.sifive.com/t/beginner-risc-v-cpu-branch-prediction-q/3714/4 "2020-05-30T23:34:41Z")

</div>

Looking into gcc now, thanks. Is my understanding of branch prediction and single-cycle cores correct? (No reason for it or even HINT instructions)

---

<div class="post-metadata">

**Author:** ![jimw](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@jimw](https://forums.sifive.com/u/jimw)\
**Post date:** [May 31, 2020, 12:32am UTC](https://forums.sifive.com/t/beginner-risc-v-cpu-branch-prediction-q/3714/5 "2020-05-31T00:32:36Z")

</div>

The code emitted by gcc will work regardless of whether you have branch prediction support, and regardless of whether you have single cycle cores or not. Neither one affects the correctness of the code. The only issue is that for best performance, you might want to optimize differently. But for a single cycle core with no branch prediction, it probably doesn’t matter how you optimize, the code will take the same amount of time to execute regardless of how the instructions are scheduled.
