[RTLLM-p034] Miscellaneous/RISC-V/instr_reg


提交解答


分數: 5
時間限制: 2.0s
記憶體限制: 256M

作者:
題目代碼
題目類型
允許的語言
Verilog

CPU 指令暫存器

題目說明

請設計一個名為 instr_reg 的 CPU 指令暫存器模組。

此模組接收一個 8 位元輸入資料 data,並根據 2 位元控制訊號 fetch,將資料儲存至兩個不同的內部暫存器:

ins_p1
ins_p2

其中:

  • fetch = 2'b01 時,將 data 儲存至 ins_p1
  • fetch = 2'b10 時,將 data 儲存至 ins_p2
  • fetch 為其他值時,兩個暫存器皆保持原值。

模組再將 ins_p1 分割為兩個欄位:

ins = ins_p1[7:5]
ad1 = ins_p1[4:0]

而輸出 ad2 則直接對應完整的 ins_p2

ad2 = ins_p2

模組介面

輸入埠
埠名稱 方向 位元寬度 說明
clk input 1 時脈訊號
rst input 1 低電位有效同步重置訊號
fetch input 2 指令來源控制訊號
data input 8 欲寫入的 8 位元資料
輸出埠
埠名稱 方向 位元寬度 說明
ins output 3 ins_p1 的高 3 位元
ad1 output 5 ins_p1 的低 5 位元
ad2 output 8 ins_p2 的完整 8 位元資料

內部暫存器

模組包含兩個 8 位元暫存器:

reg [7:0] ins_p1;
reg [7:0] ins_p2;

用途如下:

暫存器 寫入條件 用途
ins_p1 fetch == 2'b01 儲存主要指令,並分割為 insad1
ins_p2 fetch == 2'b10 儲存第二筆完整資料,輸出至 ad2

Reset 行為

題目說明 rst 為低電位有效 Reset。

當時脈上升沿到來且:

rst = 0

時,兩個內部暫存器必須清為 0:

ins_p1 = 0
ins_p2 = 0

因此輸出也會變為:

ins = 000
ad1 = 00000
ad2 = 00000000

由於 Reset 只在 posedge clk 時被檢查,因此本題為低電位有效同步 Reset。

時序區塊應使用:

always @(posedge clk)

Fetch 控制行為

fetch = 2'b01

data 寫入 ins_p1

ins_p1 <= data;

ins_p2 保持原值。


fetch = 2'b10

data 寫入 ins_p2

ins_p2 <= data;

ins_p1 保持原值。


fetch = 2'b00

兩個暫存器皆保持原值。


fetch = 2'b11

題目未定義同時寫入兩個暫存器,因此兩個暫存器皆保持原值。


輸出欄位分割

輸出 insins_p1 的高 3 位元:

assign ins = ins_p1[7:5];

輸出 ad1ins_p1 的低 5 位元:

assign ad1 = ins_p1[4:0];

輸出 ad2ins_p2 的完整內容:

assign ad2 = ins_p2;

範例

假設 Reset 後輸入:

fetch = 01
data  = 01011100

則在下一個時脈上升沿後:

ins_p1 = 01011100

因此:

ins = 010
ad1 = 11100

ins_p2 尚未被寫入,因此:

ad2 = 00000000

接著若輸入:

fetch = 10
data  = 10100101

則下一個時脈上升沿後:

ins_p2 = 10100101

此時:

ins = 010
ad1 = 11100
ad2 = 10100101

設計要求

  • 模組名稱必須為 instr_reg
  • rst 為低電位有效同步 Reset。
  • 模組內部必須包含 ins_p1ins_p2 兩個 8 位元暫存器。
  • Reset 時兩個暫存器皆清為 0。
  • fetch = 2'b01 時,只更新 ins_p1
  • fetch = 2'b10 時,只更新 ins_p2
  • fetch = 2'b002'b11 時,暫存器保持原值。
  • ins 必須等於 ins_p1[7:5]
  • ad1 必須等於 ins_p1[4:0]
  • ad2 必須等於 ins_p2
  • 時序邏輯必須使用 nonblocking assignment。

程式設計模板

module instr_reg (
    input            clk,
    input            rst,
    input      [1:0] fetch,
    input      [7:0] data,
    output     [2:0] ins,
    output     [4:0] ad1,
    output     [7:0] ad2
);

    reg [7:0] ins_p1;
    reg [7:0] ins_p2;

    // Write your code here

endmodule

評論

目前沒有評論。