2016-04-27 77 views
2

幾個星期前我開始使用Verilog,現在我正在FPGA板上實現MIPS流水線,而且我正在流水線階段的MEM部分。我試圖對數據存儲單元進行編碼(在圖片 - >數據存儲單元中)。數據存儲單元

Snapshot

我不明白,使用memread的。我明白,如果memwrite爲1,則會傳遞當前地址的內容以讀取數據。

到目前爲止,這是我的代碼:

module data_memory (
input wire [31:0] addr,    // Memory Address 
input wire [31:0] write_data,  // Memory Address Contents 
input wire memwrite, memread, 
output reg [31:0] read_data  // Output of Memory Address Contents 
); 

reg [31:0] MEMO[0:255]; // 256 words of 32-bit memory 

integer i; 

initial begin 

    read_data <= 0; 

    for (i = 0; i < 256; i = i + 1) 
    MEMO[i] = i; 

    end 

always @ (addr) begin 

    //**I don't understand the use of memread**// 

    if (memwrite == 1'b1) 
     MEMO[addr] <= write_data; 
    end 
end 

assign read_data = MEMO[addr]; 

endmodule 

我需要另一個if語句的memread?任何幫助是極大的讚賞。謝謝

回答

4

在上面編碼的設計中,您不使用memread,而是選擇通過模塊的最後一行組合地從存儲器讀取數據。沒有關於圖表中的內存如何正常工作的更多細節,難以說明memread的確切用法。典型的存儲器只有一個memwrite,並假定如果提供了一個地址並且memwrite被取消斷言,則該存取是一個讀取。在這種情況下,我只能假設memread應該被斷言從內存中讀取。另外,我建議一些修改自己的代碼,使其更好地工作,並按照一個更好的同步設計風格(這將包括memread所以你可以看到它如何被使用):

module data_memory (
input wire [31:0] addr,   // Memory Address 
input wire [31:0] write_data, // Memory Address Contents 
input wire memwrite, memread, 
input wire clk,     // All synchronous elements, including memories, should have a clock signal 
output reg [31:0] read_data  // Output of Memory Address Contents 
); 

reg [31:0] MEMO[0:255]; // 256 words of 32-bit memory 

integer i; 

initial begin 
    read_data <= 0; 
    for (i = 0; i < 256; i = i + 1) begin 
    MEMO[i] = i; 
    end 
end 

// Using @(addr) will lead to unexpected behavior as memories are synchronous elements like registers 
always @(posedge clk) begin 
    if (memwrite == 1'b1) begin 
    MEMO[addr] <= write_data; 
    end 
    // Use memread to indicate a valid address is on the line and read the memory into a register at that address when memread is asserted 
    if (memread == 1'b1) begin 
    read_data <= MEMO[addr]; 
    end 
end 

endmodule 

重要的是還要注意在您的設計中需要一個時鐘。在這個級別的大多數框圖將假設省略時鐘,但是所有同步元件(存儲器和寄存器)將被同步到公共時鐘(或者在一些情況下爲多個時鐘)。

0

@Unn提供優異的答案,而且我只想補充一點,如果你不使用read_enable,那麼它可能不同步的數據讀取操作,還優選翻牌上read_clk輸出read_data

在這裏看到下面的參考。

parameter RAM_WIDTH = <ram_width>; 
parameter RAM_ADDR_BITS = <ram_addr_bits>; 

(* RAM_STYLE="{AUTO | BLOCK | BLOCK_POWER1 | BLOCK_POWER2}" *) 
reg [RAM_WIDTH-1:0] <ram_name> [(2**RAM_ADDR_BITS)-1:0]; 
reg [RAM_WIDTH-1:0] <output_dataB>; 

<reg_or_wire> [RAM_ADDR_BITS-1:0] <addressA>, <addressB>; 
<reg_or_wire> [RAM_WIDTH-1:0] <input_dataA>; 

// The forllowing code is only necessary if you wish to initialize the RAM 
// contents via an external file (use $readmemb for binary data) 
initial 
    $readmemh("<data_file_name>", <ram_name>, <begin_address>, <end_address>); 

always @(posedge <clockA>) 
    if (<enableA>) 
     if (<write_enableA>) 
     <ram_name>[<addressA>] <= <input_dataA>; 

always @(posedge <clockB>) 
    if (<enableB>) 
     <output_dataB> <= <ram_name>[<addressB>];