Video
Đăng bởi Quản trị viên OpenER ·
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ả.
BORA NIKOLIC
Welcome back to our module on designing a RISC-V CPU. CPU is a digital system. So based on the fundamental principles of designing digital logic, we should be able to build one.
But how do we really go about building a CPU?
We have heard about state machines as a common way of describing digital logic, or digital systems to be more precise.
So can we use a state machine to describe the CPU or to design a CPU? It shouldn't be a surprise that the answer is yes.
We can view a CPU design as the design of a state machine.
Let's think if one instruction, and then we can try to extrapolate that to all the instructions that we have in a CPU.
So state in a CPU is contained in the registers, the memory, and the program counter.
We don't have any other part of a CPU that stores the data. So that would set what would be combination logic doing.
So for example, in this case, if we are doing an addition operation, all relevant contents would be in the instruction memory that we have over here pointed.
We wouldn't care much about the contents of a data memory, which is DMEM, but we would care about the registers and the program counter.
The contents of the registers and the program counter in the instruction memory would be used to perform the function, to execute that instruction.
So if the instruction is a register-based add, then this combinational logic would perform an add based on the contents of the state elements.
And the result will be written back into the state elements, into the registers, and the memory would update the program counter.
We can have a different bubble of combinational logic for every instruction. And one can imagine that we could build really a processor that way.
Every single one of these 30-something instructions in RV32I would have its own dedicated bubble of logic. And then we would essentially use multiplexers.
And the select input on those multiplexers would be a type of instruction that we are executing. And we would be updating the state based on these different outputs of these different combination logic models.
But that's not practical because many instructions are going to share the same datapath.
So in general, we are going to try to build this datapath as a cloud of logic that can execute all the instructions.
So the instructions would start executing on a tick of a clock, on the rising edge of a clock, go through the combinational logic, and present the outputs of it back to the state elements.
On the next tick of a clock, on the next rising edge of a clock, we'll write it back as a new state in the state elements. We'll keep doing that.
By doing that sequence, we would execute all the instructions that we have.
Now, building one monolithic cloud of logic that would be executing all instructions is also not practical. It's really hard to think of it that way.
It is hard to design that kind of a logic. There is just too much stuff going on in there.
So a general solution to that is to break up instruction and execution into phases and have a bubble of logic associated with each of these phases.
It's a lot easier to divide and conquer this, particularly given the fact that all instructions have similar phases.
Not all of them have all the phases of execution, but most of all of them have at least some phases of that execution.
So to actually see what am I talking about, let's take a look at common phases of execution that would correspond to separate stages in the datapath.
So stage 1 would be instruction fetch. Stage 2 is instruction decode. Stage 3 is execute. Stage 4 is memory access.
And stage 5 is right back to registers. What does this mean? We're going to see it in much more detail a bit later. But I'll just give you a quick preview for now.
Instruction fetch gets the instruction from the memory and stores it in the processor.
Then instruction decode looks at that process, that instruction, and determines what it is. So decodes what is the operation that you would like to do.
In the execution stage, we actually perform the operation. Commonly, that may be done by the arithmetic logic unit.
And we'll find out that we are using arithmetic logic unit not just for calculating arithmetic and logic operations, whether they register or immediate based, but also for branches.
Then in the fourth phase, we would access the memory. If we are doing any of these instructions that have to deal with the memory, we would access the memory in the fourth stage.
These are loads and stores.
And finally, in the fifth stage, if we need to write back the content in the registers, like for example, in case of loads, we would complete this instruction in the fifth stage by writing back to the register.
Let's take a look at schematically how does this look like. So here is a diagram of a datapath for a processor. It's a very generic datapath. And it is shown here.
Arranged are the elements that we have seen before, we have heard before, but we haven't seen them really connected in a particular way.
In the first stage, we have a program counter. Then there is an instruction memory. There is a registered file, ALU, and the data memory.
And in this case, we are assuming that we have two separate memories, the instruction memory and the data memory.
Although, they're a part of one physical memory, but we are assuming that we are separately treating that part of a memory that contains instructions from a part of memory that contains the data.
We'll see later why do we do that and how do we do that.
So just to get a little bit about insight of what is going on in here, there is some logic around the program counter.
So the program counter, when it is executing the instructions in sequence, it will be incremented by 4 bytes to point to the next 32-bit word in RISC-V.
This mux here is to bypass that increment and write a branch target if the branch is to be taken.
In that way, the program counter would be pointing to the instruction where the branch lengths.
Then we have an instruction memory that has instructions in there. And these instructions will point to the registers that you would like to work with.
So it would issue addresses that correspond to the destination register, or first and second source registers. Finally, we have the ALU and the data memory.
And from the data memory, there is this path where we write back to the registers.
Let's take a look at the phases of execution, and they're executed in sequence from the first one to the fifth one. The instruction fetch happens by the program counter pointing to the instruction memory.
That is all what it is about the fetch. The instruction pointer points to an allocation in the instruction memory where the next instruction is. That's how we fetch the instruction.
Then when you have that instruction, we decode it.
Often, that phase is associated with the register read, because at the same time, we know that our formats of the instructions are rigid, and particular parts of the instructions are going to be used as addresses to the register file.
In the third phase or stage of execution, we will execute.
We will perform addition, or subtraction, or branch calculation. Then we will access the memory by using the result of the ALU.
And then, finally, the result of the memory access will be written back into the register file.
We're going to see this in much more detail in many, many examples for every single one of the instructions or instruction types that we have introduced previously.
For now, it is important to also understand this concept of a single cycle datapath.
So all of these five phases of execution are going to happen during one clock cycle.
So we are going to start the instruction execution on the first rising edge of a clock with the instruction fetch.
And we are going to write back the final result on the next rising edge of a clock.
So at that point, the registers will be updated, and the program counter will be updated to contain the new address of the next instruction to be fetched.
Later on, we'll find out that we can break this up into multiple clock cycles, but all of our discussion for the next few segments is going to be dealing with this, what we call a single cycle datapath, where all five phases of execution happen within one clock cycle.
So how do we build a datapath then? We have seen some of these elements. Here are the components that we need. They're all familiar.
We have all seen them in the module on digital systems. So the combinatorial elements that we have are adders, multiplexers, ALUs.
That will be arranged like LEGOs. That's why this is so fun, because we can do it essentially as assembling a LEGO kit.
And then we need to complement them with state elements, which are the elements that store the data and the clocking methodology.
And for now, we are sticking with a very simple clocking methodology that corresponds to a single cycle CPU. Let's talk a little bit about these state elements.
The first one that we'll definitely need is the register. This register is, again, a fairly simple structure. And we have seen it before. It's a collection of flip-flops, if you will.
So a 32-bit register will consist of 32 flip-flops. And they're all going to be written together on the rising edge of a clock.
So in this case, we have a data in port into that register, and a data out port in that register. We are labeling it as having a port that is N bits wide.
What does that mean? Well, it is a shorthand notation for having N wires, and most commonly in 32-bit datapaths, that number N is going to be 32.
So we have a data port that is 32 wires wide into the register and data out port that is also 32 bits wide.
The register gets updated only on the rising edge of a clock. So the new value gets written on the rising edge of the clock if the write enable signal is asserted.
If the write enable signal is not asserted, if it is deasserted, if it is equal to zero, the values in the register will not change. So let's recap that one more time.
We will change the values of the data on the port or bus data in.
On the rising edge of a clock, that new value, those new values will be written into the register, and they will stay at the output of that register until the next clock cycle.
And that's it. The next thing, the next building block in this hierarchy is the register file. The register file is a collection of registers. So a registry is a collection of flip-flops.
A register file is a collection of registers.
So in RV32I, we need a register file with 32 registers to hold all the registers that we need for our architecture.
One thing to keep in mind that there is a limitation on how many registers we can access at a time.
The wires, when you get to the really nitty-gritty details of how we implement processors, we'll find out that wires are a real challenge to fit to our access in any possible order of these registers.
The architecture of RISC-V requires that we should be able to read two registers simultaneously out of a register file, and that we can write to one of these registers.
So in short, this would be called a two-read, single-write type of registry file, two-read, single-write.
The way how we access the register file is, again, by using the addresses and the buses.
There is one input bus that contains the new value that is going to be written in it, and it is in RISC-V RV32I 32 bits wide.
And there are two output buses, bus A and bus B, that will contain the values that are in registers A and B, or source registers one and two.
The way how we select which one of these two, which of the 32 registers would put their data out on the bus A or bus B is by setting their addresses at these ports, RA and RB.
So when we select — when we put a 5-bit number that corresponds to a 5-bit address of any of the 32 registers on RA, we are going to get the contents of that register on the bus A.
And correspondingly, when we put an address on a register on port RB, we are going to get its contents on the output RB.
When we would like to write to a register, we need to enable it for writing. We don't want to scribble over the registers accidentally.
So we have to say that we mean it by asserting the write enable, by putting the datum on the bus W, setting the address of the register that you would like to write into on RW, and on the rising edge of the clock, this value from bus W is going to be transferred to the corresponding register.
This is kind of a bit of a magic register file. We need only clock to write into it.
Well, we assume that every time we just assert — as soon as we put the values RA and RB at its address inputs, the outputs are going to show up.
So we don't need a clock in order to read the register file.
There is, of course, some delay until these outputs show up. We call that the access time. Then there are more state elements, elements that contain the state.
So we have the memory, and memory is, again, fairly magical. We have data in bus and data out bus and the address. We also have a clock.
And write enable, it will work very similarly to what we have had before. So in this case, we have multiplexed read and write access to the memory. What does that mean?
When we put an address here and don't clock it, the data will magically, that corresponds to that address, show up at the data out bus.
When we would like to write into the memory, we will assert the write enable.
And on the next rising edge of a clock, data in will be written into a corresponding memory location where the address corresponds to.
Again, there are delays associated with reading and writing the memories, which we'll call read and write access times when needed.
So let's recap what is the state that is required by the RV32I ISA.
Each instruction during execution reads and updates the state of three sets of elements — the registers, the program counter, and the memory.
So registers, or the register file holds 32 registers, each being of 32 bits wide, meaning that we have 1,024 total flip-flops that are organized in 32 registers where each of these 32 registers is 32 bits wide.
The first register that you would like to address or work with is specified by the RS1 field in the instruction.
The second register that you would like to read from is specified by the RS2 field. So we have two read ports.
The right register or the destination where we would like to write the result to is specified by the RD field in the instruction. And we know that the x0 register is essentially a zero.
So we actually don't need to have flip-flops in that row of the register file. We can just connect all of those to 0. We'll be anyways ignoring any request to write there.
The next one, the next state element is the program counter. It's also a 32-bit register that holds the address of the current instruction that we are executing.
And we are going to update it during the execution of every instruction to point to the next instruction that it will be executing, whether it's the next instruction sequence, which is 4 bytes away, or the target of a branch.
Finally, we have memory. And we hinted that when we are designing memory, it's a monolithic piece where we store both the data and the program.
But for the purpose of designing a data path, we break it up into two parts — the instruction memory and the data memory.
They're still a part of the same physical memory, but in all of our drawings, we are going to treat it separately.
And we'll see a little bit later in just the next module that we enable that by using some pieces, a concept called caches.
We'll have separate instruction and data caches, which hold the copies of the corresponding parts of the main memory, but will be on chip and fast to access.
We are not going to discuss that for now. We'll just imagine that we have two separate memories.
So instructions are read or fetched from the instruction memory, which we'll call IMEM, and load and store instructions access the data memory, which is the DMEM.
So we are now ready to actually build our first instruction. We'll do that after the 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.