111 年 國立中正大學電機工程學系碩士班計算機工程組《計算機組織》

📄 試題原卷 免費註冊後即可對照原始考卷 PDF免費註冊

第 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 指令欄位如下:

op=[31:26],rs=[25:21],rt=[20:16],rd=[15:11]\text{op} = [31:26],\quad rs=[25:21],\quad rt=[20:16],\quad rd=[15:11]

I-type 指令的立即數為:

imm=[15:0]\text{imm}=[15:0]

分支位址計算公式為:

BranchTarget=PC+4+(SignExt(imm)≪2)\text{BranchTarget} = PC+4+\left(\text{SignExt}(\text{imm})\ll 2\right)

每條指令在一個 clock cycle 內完成,因此不需要多週期控制器;控制訊號由 instruction decoder 根據 opcode 與 funct 欄位產生。


指令語意與資料流

1. R-type 指令

R-type 指令的格式為:

op = 000000 | rs | rt | rd | shamt | funct

典型運算:

R[rd]←R[rs] ALU R[rt]R[rd]\leftarrow R[rs]\ \text{ALU}\ R[rt]

資料流:

  1. 從 register file 讀取 rs 與 rt
  2. ALU 的兩個輸入分別選擇 R[rs]R[rs] 與 R[rt]R[rt]
  3. 根據 funct 決定 ALU 操作
  4. 將結果寫回 rd

2. SW

SW rt, imm(rs) 的語意為:

Memory[R[rs]+SignExt(imm)]←R[rt]\text{Memory}[R[rs]+\text{SignExt}(\text{imm})]\leftarrow R[rt]

資料流:

  1. register file 讀出 R[rs]R[rs],作為 base address
  2. 將 16 位元立即數符號延伸成 32 位元
  3. ALU 計算有效位址:
Address=R[rs]+SignExt(imm)\text{Address}=R[rs]+\text{SignExt}(\text{imm})
  1. 將 R[rt]R[rt] 的資料寫入 data memory
  2. 不寫回 register file

3. BNE

BNE rs, rt, imm 的語意為:

若 R[rs]≠R[rt],則 PC←PC+4+(SignExt(imm)≪2)\text{若 }R[rs]\neq R[rt]\text{,則 }PC\leftarrow PC+4+\left(\text{SignExt}(\text{imm})\ll2\right)

資料流:

  1. 讀出 R[rs]R[rs] 與 R[rt]R[rt]
  2. ALU 進行減法:
R[rs]−R[rt]R[rs]-R[rt]
  1. 若結果不為零,表示兩者不相等,Zero=0
  2. 計算分支目標位址
  3. 由 PC 輸入多工器選擇分支位址

分支成立條件為:

BranchTaken=BNE∧¬Zero\text{BranchTaken}=\text{BNE}\land \lnot\text{Zero}

單週期資料路徑

下圖以標準 MIPS 單週期資料路徑為基礎,加入 BNE 所需的不等比較控制。

                              +----------------+
                      +------>|  Instruction   |
                      |       |    Memory      |
                      |       +--------+-------+
                      |                |
                      |            Instruction
                      |                |
                      |       +--------v-------+
                      |       | Instruction     |
                      |       | Decoder         |
                      |       | opcode, funct  |
                      |       +---+---------+---+
                      |           |         |
                      |     Control signals  |
                      |           |         |
                      |           |         v
                      |           |    +------------+
                      |           |    | ALU Control|
                      |           |    +------+-----+
                      |           |           |
                      |           |           v
+-----+    +--------+ |       +---+-----------+---+
| PC  |--->|  +4    |-+------>| Register File     |
+--+--+    +--------+         | Read rs, read rt  |
   |                          | Write rd/rt       |
   |                          +---+-----------+---+
   |                              |           |
   |                         ReadData1    ReadData2
   |                              |           |
   |                              |           +------------------+
   |                              |                              |
   |                              v                              v
   |                         +----+-----+                  +-----+------+
   |                         |           |                  |   MUX      |
   |                         |   ALU    |<-----------------| ALUSrc     |
   |                         |           |                  +-----+------+
   |                         +----+--+---+                        |
   |                              |  |                            |
   |                         ALUResult Zero                        |
   |                              |  |                            |
   |                              |  +----> BNE branch condition  |
   |                              v                               |
   |                       +------+-------+                       |
   |                       |  Data Memory |                       |
   |                       | read / write |                       |
   |                       +------+-------+                       |
   |                              |                               |
   |                              v                               |
   |                         +----+-----+                         |
   |                         |   MUX     |                         |
   |                         | MemtoReg  |                         |
   |                         +----+-----+                         |
   |                              |                               |
   |                              +------> Register File write data
   |
   |        SignExt(imm) << 2
   |              |
   |              v
   |       +------+------+
   | PC+4->| Branch Adder |---- BranchTarget
   |       +-------------+
   |
   +------------------------------+
                                  |
                         +--------v--------+
                         |      MUX        |
                         | PCSrc: PC+4 /   |
                         | BranchTarget    |
                         +--------+--------+
                                  |
                                  v
                                  PC

實際硬體中,branch target 的計算可表示為:

BranchTarget=(PC+4)+(SignExt(imm)≪2)\text{BranchTarget} = (PC+4)+(\text{SignExt}(\text{imm})\ll2)

PC 輸入多工器的選擇訊號為:

PCSrc=BNE∧¬ZeroPCSrc=\text{BNE}\land\lnot\text{Zero}

因此:

  • BNE 且 R[rs]≠R[rt]R[rs]\neq R[rt]:PCSrc=1PCSrc=1,選擇 BranchTarget
  • BNE 且 R[rs]=R[rt]R[rs]=R[rt]:PCSrc=0PCSrc=0,選擇 PC+4PC+4
  • R-type 與 SW:BNE=0,選擇 PC+4PC+4

各元件與控制訊號

PC 與 instruction memory

  • PC 是 32 位元 state element,於 clock edge 更新
  • Instruction memory 使用 PC 作為讀取位址
  • 指令取出後送入 decoder 與 register file
  • 每條指令的順序下一個位址為:
PCnext=PC+4PC_{\text{next}}=PC+4

Register file

Register file 有兩個讀取埠與一個寫入埠:

  • Read register 1:rs
  • Read register 2:rt
  • Write register:由 RegDst 決定 rt 或 rd
  • Write data:由 MemtoReg 決定 ALU 結果或 data memory 輸出
  • 寫入動作由 RegWrite 控制

目的暫存器選擇:

WriteReg={rd,RegDst=1rt,RegDst=0\text{WriteReg}= \begin{cases} rd,&\text{RegDst}=1\\ rt,&\text{RegDst}=0 \end{cases}

ALU 輸入多工器

ALU 第一個輸入固定為 register file 的 ReadData1,即 R[rs]R[rs]。

第二個輸入由 ALUSrc 選擇:

ALUInput2={R[rt],ALUSrc=0SignExt(imm),ALUSrc=1\text{ALUInput2}= \begin{cases} R[rt],&\text{ALUSrc}=0\\ \text{SignExt}(\text{imm}),&\text{ALUSrc}=1 \end{cases}

因此:

  • R-type:需要 R[rt]R[rt],所以 ALUSrc=0
  • SW:需要立即數計算位址,所以 ALUSrc=1
  • BNE:比較 R[rs]R[rs] 與 $R[rt]
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 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 = 3232 bits = 44 bytes。
  • Cache line 每行存放 11 word,因此區塊大小為 44 bytes。
  • 四路組合(four-way set associative)表示每個 set 包含 44 條 cache line。
  • 位址切割公式為:
位址位元數=Tag bits+Index bits+Byte-offset bits\text{位址位元數} = \text{Tag bits} + \text{Index bits} + \text{Byte-offset bits}

其中:

  • Byte offset:指定一個 word 中的哪一個 byte。
  • Index:指定 cache 中的哪一個 set。
  • Tag:用來與該 set 中各 way 的 tag 比對。

解題方法

(a) 四路組合快取的結構

Cache 總共有 10241024 條 lines,每個 set 有 44 條 lines,因此 set 數量為:

Sets=10244=256=28\text{Sets} = \frac{1024}{4} = 256 = 2^8

所以需要 88 個 index bits。

每條 cache line 存放 11 word,而一個 word 為 44 bytes,因此需要:

log⁡24=2\log_2 4 = 2

個 byte-offset bits。

Tag 位元數為:

32−8−2=2232 - 8 - 2 = 22

因此 32 位元位址格式如下:

Tag (22 bits)∣Index (8 bits)∣Byte offset (2 bits)\boxed{ \text{Tag }(22\text{ bits}) \mid \text{Index }(8\text{ bits}) \mid \text{Byte offset }(2\text{ bits}) }

Cache 可表示如下:

                         32-bit byte address
              ┌────────────────┬──────────┬────────────┐
              │   Tag 22 bits  │ Index 8  │ Offset 2   │
              └────────────────┴──────────┴────────────┘
                                      │
                                      ▼
                              選擇 256 個 sets 之一

Set 0       ┌─────────────────────────────────────────┐
            │ Way 0 │ Valid │ Tag │ 1 word = 32 bits │
            │ Way 1 │ Valid │ Tag │ 1 word = 32 bits │
            │ Way 2 │ Valid │ Tag │ 1 word = 32 bits │
            │ Way 3 │ Valid │ Tag │ 1 word = 32 bits │
            └─────────────────────────────────────────┘

Set 1       ┌─────────────────────────────────────────┐
            │ Way 0 │ Valid │ Tag │ 1 word = 32 bits │
            │ Way 1 │ Valid │ Tag │ 1 word = 32 bits │
            │ Way 2 │ Valid │ Tag │ 1 word = 32 bits │
            │ Way 3 │ Valid │ Tag │ 1 word = 32 bits │
            └─────────────────────────────────────────┘

   ⋮

Set 255     ┌─────────────────────────────────────────┐
            │ Way 0 │ Valid │ Tag │ 1 word = 32 bits │
            │ Way 1 │ Valid │ Tag │ 1 word = 32 bits │
            │ Way 2 │ Valid │ Tag │ 1 word = 32 bits │
            │ Way 3 │ Valid │ Tag │ 1 word = 32 bits │
            └─────────────────────────────────────────┘

每個記憶體區塊依照 index 映射到某一個 set,再於該 set 的四個 ways 中進行 tag 比對。


(b) Cache 的容量

每條 cache line 儲存 11 word:

1 word=32 bits=4 bytes1\text{ word}=32\text{ bits}=4\text{ bytes}

因此 cache data 容量為:

1024 lines×4 bytes/line=4096 bytes1024\text{ lines}\times 4\text{ bytes/line} = 4096\text{ bytes} 4096 bytes=4 KiB4096\text{ bytes}=4\text{ KiB}

此答案計算的是 cache 的資料容量;若要計入 tag、valid bit 或 replacement bits,題目還必須提供相關欄位規格。


(c) 位址 1200 的 Tag、Index 與 Byte offset

先將十進位位址轉為十六進位:

120010=0x4B01200_{10}=0x4B0

轉為 32 位元二進位:

120010=0000000000000000000001001011000021200_{10} = 00000000000000000000010010110000_2

依照位址格式切割:

0000000000000000000001 | 00101100 | 00
        Tag 22 bits     Index 8  Offset 2

(1) Tag

Tag 是最高 22 位元:

Tag=00000000000000000000012\boxed{\text{Tag}=0000000000000000000001_2}

其十進位值為:

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 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) 執行總循環數

指令總數:5  instr×10=505\;\text{instr}\times 10=50
pipeline 起始填充:44 cycles

1. 資料冒險

  • LW → ADD 產生 load‑use hazard,需插入 1 cycle bubble。
    1010 次迭代 ⇒10\Rightarrow 10 cycles。

2. 快取未命中

  • 直映快取,行長 44 words → 每 44 個存取一次 miss。
    起始地址假設為 00,則第 1,5,91,5,9 次 LW miss。
    每次 miss 需額外 1010 cycles(加上 1‑cycle hit)。
    33 次 miss ⇒3×10=30\Rightarrow 3\times10=30 cycles。

3. 分支預測

  • 第一次分支未見歷史,預測為 not‑taken → 失誤 22 cycles。
  • 第 1010 次迭代不再跳回,預測仍為 taken → 再失誤 22 cycles。
    共 22 次失誤 ⇒4\Rightarrow 4 cycles。

4. 合計

總 cycles=50⏟指令+4⏟pipeline fill+10⏟load‑use+30⏟cache miss+4⏟branch miss=98  cycles\begin{aligned} \text{總 cycles}&= \underbrace{50}_{\text{指令}}+ \underbrace{4}_{\text{pipeline fill}}+ \underbrace{10}_{\text{load‑use}}+ \underbrace{30}_{\text{cache miss}}+ \underbrace{4}_{\text{branch miss}}\\ &= 98\;\text{cycles} \end{aligned}
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 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: xi,jx_{i,j} compute max⁡(∑k=0255xi−1,k×wi,j,k+bi,j,0)\max\left(\sum_{k=0}^{255} x_{i-1,k} \times w_{i,j,k} + b_{i,j}, 0\right).
The output layer has 10 nodes, each of which represents a digit (i.e. 0, 1, 2, ..., 9). The j-th output node x4,jx_{4,j} computes ∑k=0255x3,k×w4,j,k+b4,j\sum_{k=0}^{255} x_{3,k} \times w_{4,j,k} + b_{4,j} 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

總儲存量
796 672 Byte+3 112 Byte=799 784 Byte≈781.4 KiB796\,672\ \text{Byte} + 3\,112\ \text{Byte}=799\,784\ \text{Byte}\approx 781.4\ \text{KiB}

【答案】799 784 Byte (≈ 781 KiB)


(b) 1 KiB 權重 SRAM 之外的片上需求

  • SRAM 大小:1 KiB = 1 024 Byte
  • 快取區塊大小:32 Byte → 每塊 8 個權重

位址位元數:
⌈log⁡2(796 672)⌉=20 bits\lceil\log_2(796\,672)\rceil = 20\ \text{bits}

快取結構:

  • 內部資料行數 = 1 024 / 32 = 32 行
  • 內部索引位元 = log⁡232=5\log_2 32 = 5 bits
  • 區塊位元 = log⁡232=5\log_2 32 = 5 bits

Tag 位元 = 20 − 5 − 5 = 10 bits
每行需儲存 Tag (10 b) + Valid (1 b) = 11 bits

全部 32 行的額外儲存:
32×11 bits=352 bits=44 Byte32 \times 11\ \text{bits}=352\ \text{bits}=44\ \text{Byte}

(若以最小位元組為單位,可視為 48 Byte。)

【答案】約 44 Byte(約 48 Byte,取整數位元組)


(c) 權重快取失效率

  • 每個 32 Byte 區塊含 8 個權重。
  • 前向傳播中每個權重只被讀一次,且在記憶體中是連續排列。

一次讀取 8 個連續權重會產生 1 次未命中 + 7 次命中。
因此
Miss Rate=18=0.125=12.5%\text{Miss Rate}= \frac{1}{8}=0.125=12.5\%

【答案】12.5 %


(d) 在同等 1 KiB SRAM 成本下的記憶體組織改進

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

其他考古題