111 年 國立中正大學電機工程學系碩士班晶片系統組《計算機組織》
第 1 題20 分
Draw a single-cycle datapath for MIPS R-type instructions SW and BNE (including instruction decoder and control signals to multiplexors and state elements).
登入後即可作答並保存紀錄。
核心觀念
本題要求設計 MIPS 單週期資料路徑,涵蓋:
- R-type:暫存器對暫存器運算,例如
add $rd,$rs,$rt SW:將暫存器資料寫入記憶體BNE:兩個暫存器不相等時分支
題目將 SW 與 BNE 和 R-type 並列;嚴格分類上,SW 是 I-type、BNE 也是 I-type,因此本題應理解為「設計可支援 R-type、SW、BNE 的單週期資料路徑」。
MIPS 指令欄位如下:
I-type 指令的立即數為:
分支位址計算公式為:
每條指令在一個 clock cycle 內完成,因此不需要多週期控制器;控制訊號由 instruction decoder 根據 opcode 與 funct 欄位產生。
指令語意與資料流
1. R-type 指令
R-type 指令的格式為:
op = 000000 | rs | rt | rd | shamt | funct
典型運算:
資料流:
- 從 register file 讀取
rs與rt - ALU 的兩個輸入分別選擇 與
- 根據
funct決定 ALU 操作 - 將結果寫回
rd
2. SW
SW rt, imm(rs) 的語意為:
資料流:
- register file 讀出 ,作為 base address
- 將 16 位元立即數符號延伸成 32 位元
- ALU 計算有效位址:
- 將 的資料寫入 data memory
- 不寫回 register file
3. BNE
BNE rs, rt, imm 的語意為:
資料流:
- 讀出 與
- ALU 進行減法:
- 若結果不為零,表示兩者不相等,
Zero=0 - 計算分支目標位址
- 由 PC 輸入多工器選擇分支位址
分支成立條件為:
單週期資料路徑
下圖以標準 MIPS 單週期資料路徑為基礎,加入 BNE 所需的不等比較控制(紅色為 Decoder/Control 產生的控制訊號;PCSrc 由 BranchNE 與 Zero 相與產生,送到 PC 前的多工器,選 PC+4 或 BranchTarget)。
實際硬體中,branch target 的計算可表示為:
PC 輸入多工器的選擇訊號為:
因此:
BNE且 :,選擇BranchTargetBNE且 :,選擇- R-type 與
SW:BNE=0,選擇
各元件與控制訊號
PC 與 instruction memory
PC是 32 位元 state element,於 clock edge 更新- Instruction memory 使用 PC 作為讀取位址
- 指令取出後送入 decoder 與 register file
- 每條指令的順序下一個位址為:
Register file
Register file 有兩個讀取埠與一個寫入埠:
- Read register 1:
rs - Read register 2:
rt - Write register:由
RegDst決定rt或rd - Write data:由
MemtoReg決定 ALU 結果或 data memory 輸出 - 寫入動作由
RegWrite控制
目的暫存器選擇:
ALU 輸入多工器
ALU 第一個輸入固定為 register file 的 ReadData1,即 $R[rs]
第 2 題25 分
Given a 32-bit machine with byte-addressable memories.
(a) (10%) Draw a four-way set-associative cache that has a total of 1024 lines with one word per line.
(b) (5%) Calculate the total amount of cache memory in bytes.
(c) (10%) Find the binary values of (1)tag, (2)index, and (3)byte-offset for byte address 1200 in the cache of (a).
登入後即可作答並保存紀錄。
核心觀念
本題考查快取記憶體的位址切割與組織方式。32 位元、byte-addressable 機器中:
- 一個 word = bits = bytes。
- Cache line 每行存放 word,因此區塊大小為 bytes。
- 四路組合(four-way set associative)表示每個 set 包含 條 cache line。
- 位址切割公式為:
其中:
- Byte offset:指定一個 word 中的哪一個 byte。
- Index:指定 cache 中的哪一個 set。
- Tag:用來與該 set 中各 way 的 tag 比對。
解題方法
(a) 四路組合快取的結構
Cache 總共有 條 lines,每個 set 有 條 lines,因此 set 數量為:
所以需要 個 index bits。
每條 cache line 存放 word,而一個 word 為 bytes,因此需要:
個 byte-offset bits。
Tag 位元數為:
因此 32 位元位址格式如下:
Cache 可表示如下:
每個記憶體區塊依照 index 映射到某一個 set,再於該 set 的四個 ways 中進行 tag 比對。
(b) Cache 的容量
每條 cache line 儲存 word:
第 3 題25 分
Assume a 5-stage pipelined processor similar to MIPS is going to execute the following code fragment.
Loop: LW R2, 0(R1)
ADDI R1, R1, 4
ADD R3, R3, R2
SUBI R4, R4, 1
BNEZ R4, Loop # if R4 != 0 go to Loop
The initial value of R4 is 10. Assume that all possible forwarding paths are present in the pipeline.
The cache is direct-mapped with a line size of 4 words. Initially the cache has all entries marked as invalid. The cache has a 1-cycle hit time and takes an additional 10 cycles on a cache miss. The branch prediction unit predicts each branch as the same as its last seen outcome. Each branch mis-prediction yields a 2-cycle penalty.
Note: answers without clear reasoning are unacceptable!
(a) (15%) What is the runtime in cycles to execute the above code?
(b) (5%) If the order of the ADDI and ADD instructions is switched in the code shown above, will the runtime change? If so, by how much?
(c) (5%) If the order of the ADD and SUBI instructions is switched in the code shown above, will the runtime change? If so, by how much?
登入後即可作答並保存紀錄。
參考書等級:Computer Organization & Design (5 版) – 中等難度,適合作為碩二考前快速複習。
(a) 執行總循環數
指令總數:
pipeline 起始填充: cycles
1. 資料冒險
LW → ADD產生 load‑use hazard,需插入 1 cycle bubble。
次迭代 cycles。
2. 快取未命中
- 直映快取,行長 words → 每 個存取一次 miss。
起始地址假設為 ,則第 次LWmiss。
每次 miss 需額外 cycles(加上 1‑cycle hit)。
次 miss cycles。
3. 分支預測
- 第一次分支未見歷史,預測為 not‑taken → 失誤 cycles。
- 第 次迭代不再跳回,預測仍為 taken → 再失誤 cycles。
共 次失誤 cycles。
4. 合計
第 4 題30 分
Assume a fully-connected neural network containing 1 input layer, 3 hidden layers, and 1 output layer.
The input layer has 256 nodes, each of which represents an 8-bit pixel of a 16*16 grayscale image.
Each of the 3 hidden layers contains 256 neurons, which computes a weighted sum of all nodes in its precedent layer, adds a bias value, and performs ReLU activation (i.e. the result remains the same if it is positive; otherwise it becomes 0). In other words, the j-th neuron of the i-th layer: compute .
The output layer has 10 nodes, each of which represents a digit (i.e. 0, 1, 2, ..., 9). The j-th output node computes without ReLU.
(a) (5%) What is the amount of storage (in bytes) that are needed if the weights and biases are both represented as IEEE 754 single-precision floating-point numbers?
(b) (10%) Due to cost issues, only a 1KByte SRAM is allowed to store the weights on chip (assume biases are handled independently), and the design team decides to implement a direct-mapped cache mechanism to simplify the management of on-chip (i.e. 1Kbyte SRAM) and off-chip (i.e. containing all weights) storages. Assume one cache block stores 32-byte data. What are the on-chip storage requirements in addition to the 1 Kbyte SRAM for weights?
(c) (5%) What is the miss rate of the weight cache in (b)?
(d) (10%) Describe how to improve the weight memory organization in (b) under the same cost constraint. Note: answers without clear reasoning are unacceptable!
登入後即可作答並保存紀錄。
(a) 存儲需求
- 隱藏層 1‒3:每層 256 × 256 = 65 536 個權重
→ 3 × 65 536 = 196 608 個權重 - 輸出層:256 × 10 = 2 560 個權重
- 總權重數 = 196 608 + 2 560 = 199 168 個
每個權重、偏置皆為 IEEE 754 single‑precision (4 Byte)
- 權重儲存量:199 168 × 4 = 796 672 Byte
- 偏置數量:隱藏層 3 × 256 + 10 = 778 個
→ 778 × 4 = 3 112 Byte
總儲存量
【答案】799 784 Byte (≈ 781 KiB)
(b) 1 KiB 權重 SRAM 之外的片上需求
- SRAM 大小:1 KiB = 1 024 Byte
- 快取區塊大小:32 Byte → 每塊 8 個權重
位址位元數:
快取結構:
- 內部資料行數 = 1 024 / 32 = 32 行
- 內部索引位元 = bits
- 區塊位元 = bits
Tag 位元 = 20 − 5 − 5 = 10 bits
每行需儲存 Tag (10 b) + Valid (1 b) = 11 bits
全部 32 行的額外儲存:
(若以最小位元組為單位,可視為 48 Byte。)
【答案】約 44 Byte(約 48 Byte,取整數位元組)
(c) 權重快取失效率
- 每個 32 Byte 區塊含 8 個權重。
- 前向傳播中每個權重只被讀一次,且在記憶體中是連續排列。
一次讀取 8 個連續權重會產生 1 次未命中 + 7 次命中。
因此
【答案】12.5 %
(d) 在同等 1 KiB SRAM 成本下的記憶體組織改進