Tác giả giữ toàn bộ bản quyền (All Rights Reserved) — không phải giấy phép mở. Bạn vẫn xem và học miễn phí trên OpenER; muốn tái sử dụng cần xin phép tác giả.
[MUSIC PLAYING] PROFESSOR: Welcome back to RISC V CPU design.
We have designed more than 50% of our data path — much more than 50% of the data path so far by adding instructions that we needed for R type, I type, and S type instructions.
Now we are going to add support for the branches, which are slightly different. Remember branches? What do branches do?
They compare the contents of the registers rs1 and rs2, and depending on the condition of a branch, update the program counter.
So if the branch condition is met, the program counter is updated to a new address that is specified as the offset — immediate offset to the current value of the program counter.
If the prime condition is not met, then it goes to the next instruction, which is 4 bytes away.
Instruction encoding for B types — for B format encoding — is similar to the S format, except that the immediate now uses those 12 bits to encode a 13-bit range.
The last bit is always 0, because we try to use this range to represent values of minus 4,096 to plus 4,094 in 2 byte increments.
Let's see how does our data path look like so far.
So we have our program counter that so far as ability only to be updated to the next instruction, which is 4 bytes away — only to do something to enable this other update based on the outcome of the branch, where the instruction memory, register file, immediate computation, ALU for execution,
and the memory. When we are looking at branches, we clearly don't need to work with the memory. So we will not be writing or reading anything from the memory.
We are also not updating the register file. So a lot of that part of the path will not be lit up. But we need to do two things simultaneously for which we'll need resources.
We need to perform the branch computation and branch condition evaluation and computation of the new address.
So in order to add branches, we need to look at what functionality we need to have in our data path.
So the state changes not now by changing the contents of the registers or the memory. The change of state is by changing the program counter.
Program counter takes two values after the completion of a branch.
It either is PC plus 4 or it is PC plus immediate or this immediate is of a new type — is of the S type.
There are six different branch instructions — beq, bne, blt, bge, and bgltu, bgeu.
The last two being unsigned versions of the blt and beg that essentially evaluate conditions whether the contents of rs1 and rs2 are equal to each other, not equal to each other, less than, greater or equal, and so on.
So what does our data path need to do?
It needs to evaluate the contents of rs1 and rs2, needs to compare them, and then needs to compute PC plus the immediate.
But we have only one ALU that we have been able to use so far. So we need to add more hardware.
Now you may want to think which additional hard — what should the additional hardware do? Should it be used to calculate PC plus immediate, or to perform the comparison?
Well, we have a powerful ALU that can do all kinds of things, so let's not mess with that. And it is already wired to calculate, to take on the immediate.
So let's not touch that. What we should add is a simpler hardware that is going to perform the branch comparison.
So let's take a look at how do we modify the data path. Here is our new data path. It got a few new additions.
Most notable one is that we have a multiplexer in front of a program counter that enables us to write either PC plus 4 or the new address.
Then we have a little bit bigger piece of hardware that does the branch comparison.
It takes one input, whether the branch is signed or unsigned, and produces two single bit outputs — is if the values are equal or less than each other.
And then we have one more multiplexer here in the data part.
This multiplexer enables us to feed to the A port of the ALU — the top part of the ALU — either the rs1 contents, which is what we did before, or the program counter.
So we are going to use the ALU to add the immediate value to the program counter. And we are going to take that output and write it back into the program counter.
There are a few things here — additional things that need to happen. We need to set the immediate select to the branch type of an immediate.
That means we need to generate yet another type of an immediate in addition to I and S type of immediates.
Then we need to control the branch comparator with — whether it's signed or unsigned, based on the coding of the instruction.
And then we need to control this multiplexer as well, in front of the A input to the ALU. What is inside the branch comparator?
Well, it is a piece of logic that compares the values that are in rs1 and rs2, fairly straightforward and not very difficult to implement in logic.
Notice that we are supporting six different branches. Two of them being variants of each other, signed or unsigned. But we have only two possible outcomes.
So out of these four basic types, whether something is equal to other or less than, we support the other ones directly, because branch if greater or equal is exactly the opposite of branch on less than.
So if we just negate the output of branch on less than, we get branch greater or equal. So that is it.
We have, therefore, one input, single bit control, and two outputs out of a branch comparator.
The other kind of an important thing to mention about branches and encoding of immediates in B types is it's the difference of RISC V from some other ISAs.
In other ISAs what you will find out is that the immediate value is — the whole immediate value is shifted by one bit in the instruction encoding, which requires us to use a wide multiplex that
shifts left or right what we would like to pick as an immediate from the instruction.
So there are 12 inputs to a multiplexer that produce 12 outputs, but all of them are occupied. RISC V does it a little bit differently.
It keeps most of the immediate values, 11 out of 12 immediate values, in the same place as what we have had in the S format, and just moves one immediate value.
That's the rationale why we have that kind of an encoding that looked a little bit strange early on.
But as a result, now we just need one multiplexer with two inputs to move that bit to the right position between the two formats.
So S immediate and B immediate are very similar to each other. All we need to do is to place the 11th immediate in the right spot.
Please just take a look at that, and convince yourself that this is true.
So just to recap immediate encoding, we have seen three types of immediate so far — I type, S type, and B type. They're very similar to each other.
And when we produce the actual immediate that is 31 bits — 32 bits wide out of a 12-bit value that is encoded inside the instruction, we do that in a similar fashion.
So we always sign extend. The bottom parts between I immediates and S immediates are different.
So it's a one two way multiplexer that we have seen before. And the B immediate shares the same idea.
The instructions, 30 to 25 — in the instruction bits, 30 to 25 — are in the same position, instruction bits 11 to 8 are in the same position.
We just move the instruction bit 7 to a different position. So it's again one single bit two way multiplexer. And we always sign extend based on the most significant bit.
Let's just light up the branch path, and that will allow us to wrap up the discussion about the branches. So here is what happens when we're executing a branch instruction.
It is a little bit different than the instructions that we have seen so far. We'll first touch an instruction by pointing the program counter to instruction memory.
Then we will point — we will fetch that instruction, and the instruction will address appropriate fields in the register file. We only care about the two source registers.
We don't care about the destination register. And therefore register right enable is going to be disabled.
We also start the immediate generation through sign extension. But notice this, we prepare the next value of the program counter first.
We increment it by 4, because we may take that value, or we send the program counter downstream to the ALU.
We don't know what is going to be the outcome of the branch until we perform the comparison. So we set the control here to correspond to the branch.
So immediate select is a B. register right enable is 0.
We send also a signal to the branch comparator, should we have an unsigned or signed comparison, set the appropriate inputs to the multiplexers, ALU is still going to do the addition this time of a program counter value with the immediate.
And memory is going to be set to read. We are not going to write to it. We don't want to even accidentally write to it.
And we are going to disregard any output that might come out of that.
The next thing in execution is we are going to get the outputs of the two registers, rs1 and rs2. We are going to perform branch comparison.
The output of a branch comparison is going to tell us what to do. Where does the PC go? Which is the next value of the PC that we need to take?
ALU is going to add the immediate offset to the current value of the program counter, and bring that over to the input of the multiplexer that is sitting in front of the program counter.
Based on whether the branch is taking it in or not, we update the program counter. And that is it. That completes the branch.
That is the only state that is being updated after quite a bit of lighting up of the data path.
This is it that we need to know about the branches. We have added the data path that supports the branches.
And we'll see that we actually have now a huge majority of what we need to implement the rest of the instruction formats. So we'll do that after a break.
Đăng nhập để đánh giá học liệu này.
Đăng nhập để bình luận — chưa ai bình luận về học liệu này.
Không có khóa học liên quan.