Bài giảng Kiến trúc máy tính - Chương: Thiết kế bộ xử lý theo kiến trúc MIPS - Nguyễn Kim Khánh

pdf 74 trang ngocly 50
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Kiến trúc máy tính - Chương: Thiết kế bộ xử lý theo kiến trúc MIPS - Nguyễn Kim Khánh", để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên

Tài liệu đính kèm:

  • pdfbai_giang_kien_truc_may_tinh_chuong_thiet_ke_bo_xu_ly_theo_k.pdf

Nội dung text: Bài giảng Kiến trúc máy tính - Chương: Thiết kế bộ xử lý theo kiến trúc MIPS - Nguyễn Kim Khánh

  1. NKK-HUST Kiến trúc máy tính Thiết kế bộ xử lý theo kiến trúc MIPS Nguyễn Kim Khánh Trường Đại học Bách khoa Hà Nội Jan2015 Computer Architecture - MIPS 1
  2. NKK-HUST Nội dung 1. Thực hiện bộ xử lý MIPS cơ bản 2. Thiết kế khối datapath 3. Thiết kế control unit 4. Kỹ thuật đường ống lệnh Jan2015 Computer Architecture - MIPS 2
  3. NKK-HUST 1. Thực hiện bộ xử lý MIPS cơ bản n Xem xét hai cách thực hiện bộ xử lý theo kiến trúc MIPS: n Phiên bản đơn giản n Phiên bản được đường ống hóa (gần với thực tế) n Chỉ thực hiện với một số lệnh cơ bản của MIPS, nhưng chỉ ra hầu hết các khía cạnh: n Các lệnh tham chiếu bộ nhớ: lw, sw n Các lệnh số học/logic: add, sub, and, or, slt n Các lệnh chuyển điều khiển: beq, j Jan2015 Computer Architecture - MIPS 3
  4. NKK-HUST Tổng quan quá trình thực hiện các lệnh n Hai bước đầu tiên với mỗi lệnh: n Đưa địa chỉ từ bộ đếm chương trình PC đến bộ nhớ lệnh, tìm và nhận lệnh từ bộ nhớ này n Sử dụng các số hiệu thanh ghi trong lệnh để chọn và đọc một hoặc hai thanh ghi: n Lệnh lw: đọc 1 thanh ghi n Các lệnh khác (không kể lệnh jump): đọc 2 thanh ghi Jan2015 Computer Architecture - MIPS 4
  5. NKK-HUST Tổng quan quá trình thực hiện các lệnh (tiếp) n Các bước tiếp theo tùy thuộc vào loại lệnh: n Sử dụng ALU hoặc bộ cộng Add để: n Tính kết quả phép toán với các lệnh số học/logic n So sánh các toán hạng với lệnh branch n Tính địa chỉ đích với các lệnh branch n Tính địa chỉ ngăn nhớ dữ liệu với lệnh load/store n Truy cập bộ nhớ dữ liệu với lệnh load/store n Lệnh lw: đọc dữ liệu từ bộ nhớ n Lệnh sw: ghi dữ liệu ra bộ nhớ n Ghi dữ liệu đến thanh ghi đích: n Các lệnh số học/logic: kết quả phép toán n Lệnh lw: dữ liệu được đọc từ bộ nhớ dữ liệu Jan2015 Computer Architecture - MIPS 5
  6. NKK-HUST Tổng quan quá trình thực hiện các lệnh (tiếp) n Thay đổi nội dung bộ đếm chương trình PC: n Với các lệnh rẽ nhánh (branch), tùy thuộc vào kết quả so sánh: n Điều kiện thỏa mãn: PC ß địa chỉ đích (địa chỉ của lệnh cần rẽ tới) n Điều kiện không thỏa mãn: PC ß PC + 4 (địa chỉ của lệnh kế tiếp) n Với các lệnh còn lại (không kể các lệnh jump) n PC ß PC + 4 (địa chỉ của lệnh kế tiếp) Jan2015 Computer Architecture - MIPS 6
  7. NKK-HUST Sơ đồ khái quát của bộ xử lý MIPS Jan2015 Computer Architecture - MIPS 7
  8. NKK-HUST Sử dụng bộ chọn kênh (MUX) n Không thể nối trực tiếp tại các vị trí được đánh dấu n Sử dụng MUX Jan2015 Computer Architecture - MIPS 8
  9. NKK-HUST Bộ xử lý với các đường điều khiển chính Jan2015 Computer Architecture - MIPS 9
  10. NKK-HUST 2. Thiết kế khối Datapath n Datapath: gồm các thành phần để xử lý dữ liệu và địa chỉ n Tập thanh ghi, ALUs, MUX’s, bộ nhớ, n Sẽ xây dựng tăng dần khối datapath cho MIPS Jan2015 Computer Architecture - MIPS 10
  11. NKK-HUST 252 Chapter 4 The Processor 252 Các252Chapter thành 4 The Processor phChapterần 4 để The Processor thực hiện nhận lệnh Instruction Instruction address Instruction address address Instruction Add Sum PC Instruction InstructionPC PCAdd Sum Add Sum Instruction Instruction Instruction memory memory memory a. Instruction memory b. Program counter c. Adder a. Instruction memory b. Program counter c. Adder a. Instruction memory n Bộb. đếProgramm counterchươngc. Adder trình PC: FIGURE 4.5 Two state elements are needed to store and access instructions, and an FIGURE 4.5 Two state elements are needed to store and access instructions, and an FIGURE 4.5 Two state elements are needed to store and accessadder instructions, is needed to compute and an the next instruction address. T e state elements are the instruction n adder is needed to compute the next instruction address. T e state elements are the instruction adder is needed to compute the next instructionThanh address. ghi 32-bitT e statememory elements ch andứ are a the the đị program instructiona ch counter.ỉ c Tủ ea instruction lệnh memory hiệ nneed t ạonlyi provide read access because the memory and the program counter.datapath doesT e instructionnot write instructions. memory need Since only the instruction provide read memory access only because reads, thewe treat it as combinational memory and the program counter. T e instruction datapathmemory does need not only write provide instructions. read Since access the becauseinstruction the memory only reads, we treat it as combinational n Địa chỉ khởi độlogic:ng the= output 0xBFC0 at any time ref ects0000 the contents of the location specif ed by the address input, and no read datapath does not write instructions. Since the instructionlogic: thememory output onlyat any reads, timecontrol re wef ects treat signal the it contents asis needed.combinational of the(We location will need speci to writef ed bythe the instruction address input,memory and when no read we load the program; this is logic: the output at any time ref ects the contents of thecontrol location signal speci is needed.f ed by (We thenot willaddress hard need to add,input,to write and and wethe ignore noinstruction read it for simplicity.) memory when T e program we load counter the program; is a 32-bit this register is that is written at the control signal is needed. (We will needn to writeBộ the nh instructionnot ớhard ltoệ add,memorynh and we (when Instructionignoreend we it of forload every simplicity.) the clock program; cycle T e and programmemory this thus is does counter not needis a 32-bit): a write register control that signal. is written T e adder at the is an ALU wired to always not hard to add, and we ignore it for simplicity.) T e programend of every counter clock is cycle a 32-bit andadd thus register its does two that not32-bit needis inputswritten a write and at controlplace the the signal. sum on T its e adder output. is an ALU wired to always add its two 32-bit inputs and place the sum on its output. end of every clock cycle and thus does not needn aCh writeứ controla các signal. l ệT nh e adder c isủ ana ALUch wiredươ tong always trình add its two 32-bit inputs and place the sum on its output. n Khi có địa chỉ lệwhichnh t wereừ PC introduced đưa in đế Chaptern thì 2. Recalllệnh that đượ a typicalc đọ instancec ra of such an which were introducedinstruction in Chapter is add 2. Recall$t1,$t2,$t3 that a typical, which instance reads $t2 of suchand $t3 an and writes $t1. instruction is add $t1,$t2,$t3T e processor’s, which 32 general-purpose reads $t2 and registers$t3 and are writes stored $t1 in. a structure called a which were introduced in Chaptern 2. Recall that a typical instance of such an Bộ registercộngT f ele processor’s A( stateAdd ): 32 register đượgeneral-purpose f cle . sA ửregister registersdụ f ngle is are a collectiont storedăng in nof a ộregistersstructurei dung in called which PC a any register can be instruction is add $t1,$t2,$t3, which reads $t2 and $t3 and writes $t1. register f le A statethêm elementregister 4 that để consists f le .tr A ỏ register tớreadi f lle ệor is nhwritten a collection k byế specifying ti ofế registersp the numberin which of any the register incan the be f le. T e register f le T e processor’selement 32 general-purpose that consists of registersa readset of orregisters written are thatstored by specifying incontains a structure the the number register called ofstate thea of register the computer. in the f le.In Taddition, e register we f willle need an ALU to Jan2015 can be read and written Computer Architecture - MIPS 11 register f le A state register f le. A registerof a set off leregisters is a collection that contains of registers the register in which operatestate any of onregisterthe the computer. values can read be In fromaddition, the registers. we will need an ALU to element that consists can be read and written by supplying a register read or written by specifying the numbernumberoperate of tothe be onregisteraccessed. the values in the read R-formatf le. from T ethe instructionsregister registers. f le have three register operands, so we will need to read two of a set of registers that by supplying a register data words from the register f le and write one data word into the register f le for contains the registernumber state to beof accessed. the computer.R-format In addition, instructions we will have need three an register ALU tooperands, so we will need to read two can be read and written operate on the values read from the registers.data words from the registereach instruction. f le and writeFor each one data data word word to intobe read the from register the registers,f le for we need an input by supplying a register each instruction. For eachto the data register word f tole thatbe read speci fromf es thethe registerregisters, number we need to bean readinput and an output from number to be accessed. R-format instructions have three register operands, so wethe will register need fto le readthat will two carry the value that has been read from the registers. To data words from the register f le and writeto theone register data word f le that into speci thef esregister the register f le fornumber to be read and an output from the register f le that willwrite carry a data the word, value we that will has need been two readinputs: from one theto specify registers. the registerTo number to be each instruction. For each data word to be read from the registers,written we and need one anto supplyinput the data to be written into the register. T e register f le write a data word, we will need two inputs: one to specify the register number to be always outputs the contents of whatever register numbers are on the Read register to the register f le that specif es the registerwritten number and toone be to read supply and the an data output to be from written into the register. T e register f le inputs. Writes, however, are controlled by the write control signal, which must be the register f le that will carry the value alwaysthat has outputs been theread contents from theof whatever registers. register To numbers are on the Read register write a data word, we will need two inputs: one to specify theasserted register for number a write toto beoccur at the clock edge. Figure 4.7a shows the result; we inputs. Writes, however,need are a totalcontrolled of four by inputs the write (three control for register signal, numbers which mustand one be for data) and two written and one to supply the data to beasserted written forinto a writethe register. tooutputs occur T (bothat e the register for clock data). fedge. le T e Figureregister 4.7anumber shows inputs the areresult; 5 bits we wide to specify one always outputs the contents of whatever registerneed a total numbers of four are inputsof on32 registersthe(three Read for (32 register = 25), whereas numbers the and data one input for and data) two and data two output buses are each inputs. Writes, however, are controlled byoutputs the write (both control for data). 32signal, bitsT e wide. registerwhich numbermust be inputs are 5 bits wide to specify one asserted for a write to occur at the clockof 32edge. registers Figure (32 4.7a = 25 ),shows whereasFigure the4.7b the result;shows data input the we ALU, and which two data takes output two 32-bit buses inputs are each and produces a 32-bit need a total of four inputs (three for register32 bits numbers wide. and result,one for as welldata) as anda 1-bit two signal if the result is 0. T e 4-bit control signal of the ALU is outputs (both for data). T e register numberFigure inputs 4.7b are shows 5 bits thedescribed wide ALU, towhich in specifydetail takes in one two Appendix 32-bit inputs B; we andwill reviewproduces the a ALU 32-bit control shortly when 5 result, as well as a 1-bitw signale need ifto the know result how is to0. setT eit. 4-bit control signal of the ALU is of 32 registers (32 = 2 ), whereas the datadescribed input and in two detail data in output Appendix buses B ;are we eachwill review the ALU control shortly when 32 bits wide. we need to know how to set it. Figure 4.7b shows the ALU, which takes two 32-bit inputs and produces a 32-bit result, as well as a 1-bit signal if the result is 0. T e 4-bit control signal of the ALU is described in detail in Appendix B; we will review the ALU control shortly when we need to know how to set it.
  12. NKK-HUST Thực hiện phần nhận lệnh Increment by 4 for next instruction 32-bit register Jan2015 Computer Architecture - MIPS 12
  13. NKK-HUST Thực hiện lệnh số học/logic kiểu R op rs rt rd shamt funct 31:26 25:21 20:16 15:11 10:6 5:0 n bits 31:26: mã thao tác (opcode) n 000000 với các lệnh kiểu R n bits 25:21: số hiệu thanh ghi nguồn thứ nhất rs n bits 20:16: số hiệu thanh ghi nguồn thứ hai rt n bits 15:11: số hiệu thanh ghi đích rd n bits 10:6: số bit được dịch với các lệnh dịch bit n 00000 với các lệnh khác n bits 5:0: mã hàm để xác định phép toán (function code) Jan2015 Computer Architecture - MIPS 13
  14. NKK-HUST 310 CácChapter 4thành The Processor phần thực hiện lệnh kiểu R 5 Read ALU operation 4 register 1 Read Register 5 Read data 1 numbers register 2 Zero Data ALU 5 Registers ALU Write result register Read Write data 2 Data Data RegWrite a. Registers b. ALU n Tập thanh ghi (Registers): có 32 thanh ghi 32-bit, mỗi thanh FIGURE 4.7 The two elements needed to implement R-format ALU operations are the ghi đượregisterc file xác and the đị ALU.nh The b registerởi s fileố contains hiệ allu the 5-bit registers and has two read ports and one write port. The design of multiported register files is discussed in Section C.8 of Appendix C. The register file n Readalways outputs register the conte 1,nts ofRead the regist registerers corresponding 2: tocác the Read đầ registu vàoer inputs để on chthe ọoutputs;n các no thanh ghi cầothern đọcontrolc inputs are needed. In contrast, a register write must be explicitly indicated by asserting the write control signal. Remember that writes are edge-t riggered, so that all the write inputs (i.e., the value to n Writebe written, register: the register number đầu, and vào the write để contr cholọ signal)n thanh must be validghi at c theầ clockn ghi edge. Since writes to the register file are edge-t riggered, our design can legally read and write the same register within a clock cycle: n Readthe read willdata get the 1, value Read written in data an earlier 2: clock hai cycle, đầ whileu thera value dữ writte liện uwill đọ be availablec từ tothanh a read in ghi (32-bit) a subsequent clock cycle. The inputs carrying the register number to the register file are all 5 bits wide, whereas n Writethe lines Data:carrying data đầ valuesu vào are 32 bitsdữ wide. liệ Theu operghiation vào to be thanh performed ghiby the (32-bit)ALU is controlled with the ALU operation signal, which will be 4 bits wide, using the ALU designed in Appendix C. We will n RegWriteuse the Zero dete: ctiontín outputhiệu of đtheiề ALUu khishortlyể nto implementghi dữ branches. liệu vàoThe overflo thanhw output ghi will not be needed until Section 4.9, when we discuss exceptions; we omit it until then. n ALU để thực hiện các phép toán số học/logic Jan2015 Computer Architecture - MIPS 14 sign-extend To increase In addition, we will need a unit to sign-extend the 16-bit offset field in the the size of a data item by instruction to a 32-bit signed value, and a data memory unit to read from or write replicating the high-order to. The data memory must be written on store instructions; hence, data memory sign bit of the original has read and write control signals, an address input, and an input for the data to be data item in the high- order bits of the larger, written into memory. Figure 4.8 shows these two elements. destina tion data item. The beq instruction has three operands, two registers that are compared for equality, and a 16-bit offset used to compute the branch target address branch target address relative to the branch instruction address. Its form is beq $t1,$t2,offset. To The address specified in implement this instruction, we must compute the branch target address by adding a branch, which becomes the sign-extended offset field of the instruction to the PC. There are two details the new program counter (PC) if the branch is in the definition of branch instructions (see Chapter 2) to which we must pay taken. In the MIPS attention: architecture the branch ■ The instruction set architecture specifies that the base for the branch address target is given by the sum of the offset field of calculation is the address of the instruction following the branch. Since we the instruction and the compute PC + 4 (the address of the next instruction) in the instruction fetch address of the instruction datapath, it is easy to use this value as the base for computing the branch following the branch. target address.
  15. NKK-HUST Mô tả thực hiện lệnh số học/logic kiểu R n Hai số hiệu thanh ghi rs và rt đưa đến hai đầu vào Read register 1, Read register 2 để chọn hai thanh ghi nguồn n Số hiệu thanh ghi rd đưa đến đầu vào Write register để chọn thanh ghi đích n Hai dữ liệu từ hai thanh ghi nguồn được đọc ra 2 đầu ra Read data 1 và Read data 2 , rồi đưa đến đầu vào của ALU n ALU operation (4-bit): tín hiệu điều khiển chọn phép toán ở ALU n ALU thực hiện phép toán tương ứng, kết quả phép toán ở đầu ra ALU result được đưa về đầu vào Write Data của tập thanh ghi để ghi vào thanh ghi đích n RegWrite: tín hiệu điều khiển ghi dữ liệu vào thanh ghi đích Jan2015 Computer Architecture - MIPS 15
  16. NKK-HUST Thực hiện các lệnh lw/sw op rs rt imm 31:26 25:21 20:16 15:0 n bits 31:26 là mã thao tác n 100011 (35) với lệnh lw n 101011 (43) với lệnh sw n bits 25:21: số hiệu thanh ghi cơ sở rs n bits 20:16: số hiệu thanh ghi rt n thanh ghi đích với lệnh lw n thanh ghi nguồn với lệnh sw 15 15 n bits 15:0: hằng số imm có giá trị trong dải [-2 , +2 – 1] n Địa chỉ từ nhớ dữ liệu = nội dung thanh ghi rs + imm n lw rt, imm(rs) # (rt) = mem[(rs)+SignExtImm] n sw rt, imm(rs) # mem[(rs)+SignExtImm] = (rt) Jan2015 Computer Architecture - MIPS 16
  17. NKK-HUST Các thành phần thực hiện các lệnh lw/sw 310 Chapter 4 The Processor Tập thanh ghi Bộ số học/logic 5 Read ALU operation 4 register 1 Read Register 5 Read data 1 numbers register 2 Zero Data ALU 5 Registers ALU Write result register Read Write data 2 Data Data 4.3 Building a Datapath 311 RegWrite a. Registers Bộ nhb.ớ ALU dữ liệu Bộ mở rộng số có dấu FIGURE 4.7 The two elements needed to implement R-format ALUMemWrite operations are the từ 16-bit à 32-bit register file and the ALU. The register file contains all the registers and has two read ports and one write port. The design of multiported register files is discussed in Section C.8 of Appendix C. The register file always outputs the contents of the registers corresponding to the Read register inputs Readon the outputs; no other control inputs are needed. In contrast, a register write mustAddress be explicitly indicated by asserting the write control signal. Remember that writes are edge-t riggered, so that all the write inputsdata (i.e., the value to 16 32 be written, the register number, and the write control signal) must be valid at the clock edge. Since writes to Sign- the register file are edge-t riggered, our design can legally read and write the same register within a clock cycle: the read will get the value written in an earlier clock cycle, while the value written willData be available to a read in extend a subsequent clock cycle. The inputs carrying the register number toWrite the register filememory are all 5 bits wide, whereas the lines carrying data values are 32 bits wide. The operation to be performed by the ALU is controlled with the ALU operation signal, which will be 4 bits wide, using the ALUdata designed in Appendix C. We will use the Zero detection output of the ALU shortly to implement branches. The overflow output will not be needed until Section 4.9, when we discuss exceptions; we omit it until then. MemRead Jan2015 Computer Architecture - MIPS 17 a. Data memory unit b. Sign extension unit sign-extend To increase In addition, we will need a unit to sign-extend the 16-bit offset field in the the size of a data item by instruction to a 32-bit signedFIGURE value, 4.8 and The a data two memory units unit needed to read to from implement or write loads and stores, in addition to the replicating the high-order to. The data memory mustregister be writte filen andon store ALU instruct of Figureions; 4.7 hence,, are data the memory data memory unit and the sign extension unit. sign bit of the original has read and write contrTheol signals, memory an addressunit is a input, state elementand an input with forinputs the fordata the to addressbe and the write data, and a single output for data item in the high- order bits of the larger, written into memory. Figurethe read 4.8 showsresult. theseThere two are elements.separate read and write controls, although only one of these may be asserted on destina tion data item. The beq instructionany has given thre clock.e oper Theands, memory two regi unitsters needs that a readare signal,compared since, unlike the register file, reading the value of an for equality, and a 16-invalidbit offs addresset used can to cause comp probute lems,the branch as we will target see in addressChapter 5. The sign extension unit has a 16-bit input that branch target address relative to the branch instis sign-extendedruction address. into Its a form32-bit is result beq appearing$t1,$t2,offset on the output. To (see Chapter 2). We assume the data memory is The address specified in implement this instruction,edge-triggered we must comp for writes.ute the Standard branch targ memoryet addr chipsess by actual addingly have a write enable signal that is used for writes. a branch, which becomes the sign-extended offset field of the instruction to the PC. There are two details the new program counter Although the write enable is not edge-triggered, our edge-triggered design could easily be adapted to work (PC) if the branch is in the definition of branwithch instrealructions memory (see chips. Chap Seeter Sect 2)ion to C.8whic ofh we mustAppendix pay C for further discussion of how real memory taken. In the MIPS attention: chips work. architecture the branch ■ The instruction set architecture specifies that the base for the branch address target is given by the sum of the offset field of calculation is the address of the instruction following the branch. Since we the instruction and the compute PC + 4 (the address■ The of architecture the next instruct alsoion) instates the instruct that ionthe fetc offseth field is shifted left 2 bits so that it address of the instruction datapath, it is easy to useis this a wordvalue asoffset; the base this for shift comput increasesing the branch the effective range of the offset field by a following the branch. target address. factor of 4. To deal with the latter complication, we will need to shift the offset field by 2. branch taken A branch As well as computing the branch target address, we must also determine whether where the branch the next instruction is the instruction that follows sequentially or the instruction condition is satisfied and at the branch target address. When the condition is true (i.e., the oper ands are the program counter (PC) becomes the branch target. equal), the branch target address becomes the new PC, and we say that the branch All unconditional branches is taken. If the operands are not equal, the incremented PC should replace the are taken branches. current PC (just as for any other normal instruction); in this case, we say that the branch is not taken. branch not taken or Thus, the branch datapath must do two operations: compute the branch target (untaken branch) address and compare the register contents. (Branches also affect the instruction A branch where the fetch portion of the datapath, as we will deal with shortly.) Figure 4.9 shows the branch condition is false and the program counter structure of the datapath segment that handles branches. To compute the branch (PC) becomes the address target address, the branch datapath includes a sign extension unit, from Figure 4.8 of the instruction that and an adder. To perform the compare, we need to use the register file shown in sequentially follows the Figure 4.7a to supply the two register operands (although we will not need to write branch. into the register file). In addition, the comparison can be done using the ALU we designed in Appendix C. Since that ALU provides an output signal that indicates whether the result was 0, we can send the two register operands to the ALU with the
  18. NKK-HUST Thực hiện lệnh lw n Số hiệu thanh ghi rs đưa đến đầu vào Read register 1 để chọn thanh ghi cơ sở rs, nội dung rs được đưa ra đầu ra Read Data 1, rồi chuyển đến ALU n Hằng số imm 16-bit đưa đến bộ Sign-extend để mở rộng thành 32-bit, rồi chuyển đến ALU n ALU cộng hai giá trị trên đưa ra ALU result chính là địa chỉ của dữ liệu cần đọc từ bộ nhớ dữ liệu; địa chỉ này được đưa đến đầu vào Address của bộ nhớ dữ liệu n Số hiệu thanh ghi rt đưa đến đầu vào Write Register để chọn thanh ghi đích n Dữ liệu 32-bit ở bộ nhớ dữ liệu, tại vị trí địa chỉ đã được tính, được đọc ra ở đầu ra Read data của bộ nhớ dữ liệu nhờ tín hiệu điều khiển MemRead, rồi đưa về đầu vào Write Data của tập thanh ghi để ghi vào thanh ghi đích rt Jan2015 Computer Architecture - MIPS 18
  19. NKK-HUST Thực hiện lệnh sw n Số hiệu thanh ghi rs đưa đến đầu vào Read register 1 để chọn thanh ghi cơ sở rs, nội dung rs được đưa ra đầu ra Read Data 1, rồi chuyển đến ALU n Hằng số imm 16-bit đưa đến bộ Sign-extend để mở rộng thành 32-bit, rồi chuyển đến ALU n ALU cộng hai giá trị trên đưa ra ALU result chính là địa chỉ của vị trí ở bộ nhớ dữ liệu; địa chỉ này được đưa đến đầu vào Address của bộ nhớ dữ liệu n Số hiệu thanh ghi rt đưa đến đầu vào Read register 2 để chọn thanh ghi dữ liệu nguồn rt, nội dung rt được đưa ra đầu ra Read data 2 n Dữ liệu 32-bit này sẽ được đưa đến đầu vào Write data của bộ nhớ dữ liệu và được ghi vào vị trí nhớ có địa chỉ đã được tính, nhờ tín hiệu điều khiển MemWrite Jan2015 Computer Architecture - MIPS 19
  20. NKK-HUST Thực hiện lệnh Branch (beq, bne) 4 or 5 rs rt imm 31:26 25:21 20:16 15:0 n bits 31:26 mã thao tác n 000100 (4) với lệnh beq n 000101 (5) với lệnh bne n bits 25:21 số hiệu thanh ghi nguồn rs n bits 20:16 số hiệu thanh ghi nguồn rt 15 15 n bits 15:0 hằng số imm có giá trị trong dải [-2 , +2 – 1] n So sánh nội dung hai thanh ghi rs và rt: n Nếu điều kiện đúng: rẽ nhánh đến nhãn đích n PC ß (PC +4) + hằng số x 4 n Nếu điều kiện sai: chuyển sang thực hiện lệnh kế tiếp n PC ß PC + 4 Jan2015 Computer Architecture - MIPS 20
  21. NKK-HUST Các thành phần thực hiện lệnh Branch Just re-routes wires Sign-bit wire replicated Jan2015 Computer Architecture - MIPS 21
  22. NKK-HUST Thực hiện lệnh Branch (beq, bne) n Nhờ các số hiệu thanh ghi rs và rt, hai toán hạng nguồn được đọc ra đưa đến ALU n ALU trừ hai toán hạng và thiết lập giá trị ở đầu ra “Zero” n Hiệu = 0 à đầu ra Zero = 1 n Hiệu <> 0 à đầu ra Zero = 0 n Đầu ra Zero này được đưa đến mạch logic điều khiển rẽ nhánh n Bộ cộng Add tính địa chỉ đích rẽ nhánh n Hằng số imm 16-bit được mở rộng theo kiểu có dấu thành 32-bit, rồi dịch trái 2 bit n Cộng với PC (PC đã được tăng 4) à Địa chỉ đích = (PC+4) + (hằng số đã mở rộng 32-bit, dịch trái 2 bit) n Điều kiện đúng: PC ß địa chỉ đích rẽ nhánh (rẽ nhánh xảy ra) n Điều kiện sai: PC ß PC+4 (chuyển sang lệnh kế tiếp) Jan2015 Computer Architecture - MIPS 22
  23. NKK-HUST Hợp các thành phần cho các lệnh n Datapath cho các lệnh thực hiện trong 1 chu kỳ n Mỗi phần tử của datapath chỉ có thể làm một chức năng trong mỗi chu kỳ n Do đó, cần tách rời bộ nhớ lệnh và bộ nhớ dữ liệu n Sử dụng các bộ chọn kênh để chọn dữ liệu nguồn cho các lệnh khác nhau Jan2015 Computer Architecture - MIPS 23
  24. NKK-HUST Datapath cho các lệnh R-Type/Load/Store • ALUSrc: tín hiệu điều khiển chọn toán hạng đưa đến ALU: • Lệnh kiểu R: toán hạng từ thanh ghi nguồn thứ hai • Lệnh lw/sw: Hằng số imm 16-bit được mở rộng thành 32-bit (tính địa chỉ) • MemtoReg: tín hiệu điều khiển chọn dữ liệu đưa về thanh ghi đích: • Lệnh kiểu R: lấy kết quả từ ALU result • Lệnh lw: dữ liệu đọc (Read data) từ bộ nhớ dữ liệu Jan2015 Computer Architecture - MIPS 24
  25. NKK-HUST Datapath đơn giản cho các lệnh R/lw/sw/branch • PCSrc: tín hiệu điều khiển chọn giá trị cập nhật PC • Không rẽ nhánh: PC ß PC+4 • Rẽ nhánh: PC ß (PC+4) + (hằng số imm đã mở rộng thành 32-bit << 2) Jan2015 Computer Architecture - MIPS 25
  26. NKK-HUST 3. Thiết kế Control Unit n Đơn vị điều khiển có hai phần: n Bộ điều khiển ALU n Bộ điều khiển chính Jan2015 Computer Architecture - MIPS 26
  27. NKK-HUST Thiết kế bộ điều khiển ALU n ALU được sử dụng để: n Load/Store: F = add (xác định địa chỉ bộ nhớ dữ liệu) n Branch: F = subtract (so sánh) n Các lệnh số học/logic : F phụ thuộc vào funct code ALU control lines Function 0000 AND 0001 OR 0010 add 0110 subtract 0111 set-on-less-than 1100 NOR Jan2015 Computer Architecture - MIPS 27
  28. NKK-HUST Tín hiệu điều khiển ALU n Bộ điều khiển ALU sử dụng mạch logic tổ hợp: n Đầu vào: 2-bit ALUOp được tạo ra từ opcode của lệnh và 6-bit của function code n Đầu ra: các tín hiệu điều khiển ALU (ALU control) gồm 4 bit Opcode ALUOp Operation funct ALU function ALU control lw 00 load word XXXXXX add 0010 sw 00 store word XXXXXX add 0010 beq 01 branch equal XXXXXX subtract 0110 R-type 10 add 100000 add 0010 subtract 100010 subtract 0110 AND 100100 AND 0000 OR 100101 OR 0001 set-on-less-than 101010 set-on-less-than 0111 Jan2015 Computer Architecture - MIPS 28
  29. NKK-HUST Thiết kế bộ điều khiển chính n Các tín hiệu điều khiển được tạo ra từ lệnh R-type 0 rs rt rd shamt funct 31:26 25:21 20:16 15:11 10:6 5:0 lw/sw 35 or 43 rs rt imm 31:26 25:21 20:16 15:0 beq 4 rs rt imm 31:26 25:21 20:16 15:0 opcode always read, write for sign-extend read except R-type and add for load and load Jan2015 Computer Architecture - MIPS 29
  30. NKK-HUST Datapath và Control Unit Jan2015 Computer Architecture - MIPS 30
  31. NKK-HUST Các tín hiệu điều khiển Tên tín hiệu Hiệu ứng khi tín hiệu = 0 Hiệu ứng khi tín hiệu = 1 RegDst Số hiệu thanh ghi đích là các Số hiệu thanh ghi đích là các bit 15:11 bit 20:16 (rt) (rd) Branch Không có lệnh rẽ nhánh beq Có lệnh rẽ nhánh beq (Branch =1) & (Zero=1): rẽ nhánh xảy ra (Branch =1) & (Zero=0): rẽ nhánh không xảy ra RegWrite Không làm gì cả Ghi dữ liệu trên đầu vào Write Data ở tập thanh ghi đến thanh ghi đích ALUSrc Toán hạng thứ hai của ALU Toán hạng thứ hai của ALU là giá trị 16 lấy từ thanh ghi nguồn thứ bit thấp của lệnh (bits 15:0) được mở hai (Read data 2) rộng có dấu thành 32-bit PCSrc PC ß PC+4 PC ß địa chỉ đích Jan2015 Computer Architecture - MIPS 31
  32. NKK-HUST Các tín hiệu điều khiển (tiếp) Tên tín hiệu Hiệu ứng khi tín hiệu = 0 Hiệu ứng khi tín hiệu = 1 MemRead Không làm gì cả Nội dung ngăn nhớ dữ liệu, được xác định bởi địa chỉ do ALU tính, được đưa ra đầu ra Read data của bộ nhớ dữ liệu MemWrite Không làm gì cả Dữ liệu trên đầu vào Write Data của bộ nhớ dữ liệu được ghi vào ngăn nhớ có địa chỉ do ALU tính MemtoReg Giá trị được đưa đến đầu Giá trị được đưa đến đầu vào Write vào Write data của tập thanh data của tập thanh ghi là từ bộ nhớ dữ ghi là từ ALU result liệu Jan2015 Computer Architecture - MIPS 32
  33. NKK-HUST Thực hiện lệnh số học/logic kiểu R Jan2015 Computer Architecture - MIPS 33
  34. NKK-HUST Thực hiện lệnh Load Jan2015 Computer Architecture - MIPS 34
  35. NKK-HUST Thực hiện lệnh beq Jan2015 Computer Architecture - MIPS 35
  36. NKK-HUST Thực hiện lệnh Jump 2 address 31:26 25:0 n Bits 31:26 là mã thao tác = 000010 n Bits 25:0: phần địa chỉ n PC nhận giá trị sau: n Địa chỉ đích = PC31 28 : (address << 2) n 4 bit bên trái là của PC cũ n 26-bit của lệnh jump (bits 25:0) n 2 bit cuối là 00 n Cần thêm tín hiệu điều khiển được giải mã từ opcode Jan2015 Computer Architecture - MIPS 36
  37. NKK-HUST Datapath thêm cho lệnh jump Jan2015 Computer Architecture - MIPS 37
  38. NKK-HUST Thiết kế đơn chu kỳ (single-cycle) n Chu kỳ xung nhịp có độ dài bằng nhau với tất cả các lệnh à chu kỳ xung nhịp được xác định bởi thời gian thực thi lệnh lâu nhất n Ví dụ: Lệnh load sử dụng 5 đơn vị chức năng: Bộ nhớ lệnh → tập thanh ghi → ALU → bộ nhớ dữ liệu → tập thanh ghi n Thời gian thực hiện chương trình tăng à hiệu năng giảm n Chúng ta sẽ tăng hiệu năng bằng kỹ thuật đường ống lệnh (pipelining) Jan2015 Computer Architecture - MIPS 38
  39. NKK-HUST 4. Đường ống lệnh ở MIPS 5 công đoạn: 1. IF: Instruction fetch from memory – Nhận lệnh từ bộ nhớ 2. ID: Instruction decode & register read – Giải mã lệnh và đọc thanh ghi 3. EX: Execute operation or calculate address – Thực hiện thao tác hoặc tính toán địa chỉ 4. MEM: Access memory operand – Truy nhập toán hạng bộ nhớ 5. WB: Write result back to register – Ghi kết quả trả về thanh ghi Jan2015 Computer Architecture - MIPS 39
  40. NKK-HUST Hiệu năng của đường ống n Giả thiết thời gian cho các công đoạn: n 100ps với đọc hoặc ghi thanh ghi n 200ps cho các công đoạn khác n Thời gian của datapath đơn chu kỳ với một số lệnh: Instr Instr fetch Register ALU op Memory Register Total time read access write lw 200ps 100 ps 200ps 200ps 100 ps 800ps sw 200ps 100 ps 200ps 200ps 700ps R-format 200ps 100 ps 200ps 100 ps 600ps beq 200ps 100 ps 200ps 500ps Jan2015 Computer Architecture - MIPS 40
  41. NKK-HUST Hiệu năng của đường ống Single-cycle (Tc= 800ps) Pipelined (Tc= 200ps) Jan2015 Computer Architecture - MIPS 41
  42. NKK-HUST Độ tăng tốc của đường ống n Nếu tất cả các công đoạn có thời gian thực hiện như nhau và số lệnh của chương trình là lớn: Thời gian thực hiện pipeline = Thời gian thực hiện tuần tự Số công đoạn Jan2015 Computer Architecture - MIPS 42
  43. NKK-HUST Thiết kế đường ống lệnh n Kiến trúc tập lệnh MIPS được thiết kế phù hợp với kỹ thuật đường ống n Tất cả các lệnh là 32-bits n Dễ dàng để nhận và giải mã lệnh trong một chu kỳ n Intel x86: lệnh từ 1 đến 17 bytes n Có ít dạng lệnh và thông dụng n Có thể giải mã và đọc thanh ghi trong một bước n Địa chỉ hóa cho các lệnh load/store n Có thể tính địa chỉ trong công đoạn thứ 3, truy cập bộ nhớ công đoạn thứ 4 n Toán hạng bộ nhớ nằm thẳng hàng trên các băng nhớ n Truy cập bộ nhớ chỉ mất một chu kỳ Jan2015 Computer Architecture - MIPS 43
  44. NKK-HUST Các mối trở ngại (Hazard) của đường ống lệnh n Hazard: Tình huống ngăn cản bắt đầu của lệnh tiếp theo ở chu kỳ tiếp theo n Hazard cấu trúc: do tài nguyên được yêu cầu đang bận n Hazard dữ liệu: cần phải đợi để lệnh trước hoàn thành việc đọc/ghi dữ liệu n Hazard điều khiển: do rẽ nhánh gây ra Jan2015 Computer Architecture - MIPS 44
  45. NKK-HUST Hazard cấu trúc n Xung đột khi sử dụng tài nguyên n Trong đường ống của MIPS với một bộ nhớ dùng chung n Lệnh Load/store yêu cầu truy cập dữ liệu n Nhận lệnh cần trì hoãn cho chu kỳ đó n Bởi vậy, datapath kiểu đường ống yêu cầu bộ nhớ lệnh và bộ nhớ dữ liệu tách rời (hoặc cache lệnh/cache dữ liệu tách rời) Jan2015 Computer Architecture - MIPS 45
  46. NKK-HUST Hazard dữ liệu n Lệnh phụ thuộc vào việc hoàn thành truy cập dữ liệu của lệnh trước đó add $s0, $t0, $t1 sub $t2, $s0, $t3 Jan2015 Computer Architecture - MIPS 46
  47. NKK-HUST Forwarding (gửi vượt trước) n Sử dụng kết quả ngay sau khi nó được tính n Không đợi đến khi kết quả được lưu đến thanh ghi n Yêu cầu có đường kết nối thêm trong datapath Jan2015 Computer Architecture - MIPS 47
  48. NKK-HUST Hazard dữ liệu với lệnh load n Không phải luôn luôn có thể tránh trì hoãn bằng cách forwarding n Nếu giá trị chưa được tính khi cần thiết n Không thể chuyển ngược thời gian n Cần chèn bước trì hoãn (stall hay bubble) Jan2015 Computer Architecture - MIPS 48
  49. NKK-HUST Lập lịch mã để tránh trì hoãn n Thay đổi trình tự mã để tránh sử dụng kết quả load ở lệnh tiếp theo n Mã C: a = b + e; c = b + f; lw $t1, 0($t0) lw $t1, 0($t0) lw $t2, 4($t0) lw $t2, 4($t0) stall add $t3, $t1, $t2 lw $t4, 8($t0) sw $t3, 12($t0) add $t3, $t1, $t2 lw $t4, 8($t0) sw $t3, 12($t0) stall add $t5, $t1, $t4 add $t5, $t1, $t4 sw $t5, 16($t0) sw $t5, 16($t0) 13 cycles 11 cycles Jan2015 Computer Architecture - MIPS 49
  50. NKK-HUST Hazard điều khiển n Rẽ nhánh xác định luồng điều khiển n Nhận lệnh tiếp theo phụ thuộc vào kết quả rẽ nhánh n Đường ống không thể luôn nhận đúng lệnh n Vẫn đang làm ở công đoạn giải mã lệnh (ID) của lệnh rẽ nhánh n Với đường ống của MIPS n Cần so sánh thanh ghi và tính địa chỉ đích sớm trong đường ống n Thêm phần cứng để thực hiện việc đó trong công đoạn ID Jan2015 Computer Architecture - MIPS 50
  51. NKK-HUST Trì hoãn khi rẽ nhánh n Đợi cho đến khi kết quả rẽ nhánh đã được xác định trước khi nhận lệnh tiếp theo Jan2015 Computer Architecture - MIPS 51
  52. NKK-HUST Dự đoán rẽ nhánh n Những đường ống dài hơn không thể sớm xác định dễ dàng kết quả rẽ nhánh n Cách trì hoãn không đáp ứng được n Dự đoán kết quả rẽ nhánh n Chỉ trì hoãn khi dự đoán là sai n Với MIPS n Có thể dự đoán rẽ nhánh không xẩy ra n Nhận lệnh ngay sau lệnh rẽ nhánh (không làm trễ) Jan2015 Computer Architecture - MIPS 52
  53. NKK-HUST MIPS với dự đoán rẽ nhánh không xẩy ra Prediction correct Prediction incorrect Jan2015 Computer Architecture - MIPS 53
  54. NKK-HUST Đặc điểm của đường ống n Kỹ thuật đường ống cải thiện hiệu năng bằng cách tăng số lệnh thực hiện n Thực hiện nhiều lệnh đồng thời n Mỗi lệnh có cùng thời gian thực hiện n Các dạng hazard: n Cấu trúc, dữ liệu, điều khiển n Thiết kế tập lệnh ảnh hưởng đến độ phức tạp của việc thực hiện đường ống Jan2015 Computer Architecture - MIPS 54
  55. NKK-HUST MIPS Datapath được ống hóa theo đơn chu kỳ MEM WB Jan2015 Computer Architecture - MIPS 55
  56. NKK-HUST Nhận xét n Các lệnh và các dữ liệu được chuyển từ trái sang phải qua 5 công đoạn. n Có hai ngoại lệ từ phải sang trái: n Công đoạn write-back đặt kết quả về thanh ghi ở giữa datapath à dẫn đến data hazard n Chọn giá trị tiếp theo của PC là PC+4 hay địa chỉ đích rẽ nhánh từ công đoạn MEM à dẫn đến control hazard Jan2015 Computer Architecture - MIPS 56
  57. NKK-HUST Các thanh ghi đường ống n Cần các thanh ghi đặt giữa các công đoạn n Để giữ thông tin được tạo ra bởi chu kỳ trước Jan2015 Computer Architecture - MIPS 57
  58. NKK-HUST Hoạt động của đường ống n Dòng lệnh được đưa qua datapath đường ống theo từng chu kỳ. n Có hai cách thực hiện: n Đơn chu kỳ (Single-clock-cycle) n Đa chu kỳ (Multi-clock-cycle) n Xem xét đường ống đơn chu kỳ với load & store Jan2015 Computer Architecture - MIPS 58
  59. NKK-HUST IF cho lệnh Load, Store Jan2015 Computer Architecture - MIPS 59
  60. NKK-HUST ID cho lệnh Load, Store Jan2015 Computer Architecture - MIPS 60
  61. NKK-HUST EX cho lệnh Load Jan2015 Computer Architecture - MIPS 61
  62. NKK-HUST MEM cho lệnh Load Jan2015 Computer Architecture - MIPS 62
  63. NKK-HUST WB cho lệnh Load Wrong register number Jan2015 Computer Architecture - MIPS 63
  64. NKK-HUST Datapath được hiệu chỉnh cho lệnh Load Jan2015 Computer Architecture - MIPS 64
  65. NKK-HUST EX cho lệnh Store Jan2015 Computer Architecture - MIPS 65
  66. NKK-HUST MEM cho lệnh Store Jan2015 Computer Architecture - MIPS 66
  67. NKK-HUST WB cho lệnh Store Jan2015 Computer Architecture - MIPS 67
  68. NKK-HUST Giản đồ đường ống đa chu kỳ n Dạng tài nguyên được sử dụng Jan2015 Computer Architecture - MIPS 68
  69. NKK-HUST Giản đồ đường ống đa chu kỳ n Dạng truyền thống Jan2015 Computer Architecture - MIPS 69
  70. NKK-HUST Giản đồ đường ống đơn chu kỳ n Công đoạn của đường ống trong chu kỳ đã cho Jan2015 Computer Architecture - MIPS 70
  71. NKK-HUST Điều khiển đường ống (dạng đơn giản) Jan2015 Computer Architecture - MIPS 71
  72. NKK-HUST Điều khiển đường ống n Các tín hiệu điều khiển được tạo ra từ lệnh n Như thực hiện đơn chu kỳ Jan2015 Computer Architecture - MIPS 72
  73. NKK-HUST Điều khiển đường ống Jan2015 Computer Architecture - MIPS 73
  74. NKK-HUST Hết Jan2015 Computer Architecture - MIPS 74