如何以现实的方式实现存储字节和存储半字

how implement store byte and store half-word in realistic approach

我正在实现单周期 MIPS 处理器的实例。我想实现存储半字和存储字节

我已经向我的 "Data memory" 添加了一个新的输入信号来控制要存储的内容,就像下面的代码一样。

// this was prof. Harris implementation in "digital design and computer 
// architecture book" implementation before i turn the "we" (Write Enable) signal into 2 bits  

module dmem(input logic clk, [1:0]we, //where we is an output of the control unit
            input logic [31:0] a, wd, 
            output logic [31:0] rd);

logic [31:0] RAM[63:0];

assign rd = RAM[a[31:2]]; // word aligned

always_ff @(posedge clk)
    case(we)
        2'b01: RAM[a[31:2]] <= wd; // sw 
        2'b10: RAM[a[31:0]][15:0] <= wd[15:0]; // sh
        2'b11: RAM[a[31:0]][7:0] <= wd[7:0]; // sb
        default:
        // do nothing
        ...

如果不是传统的实现方式,这是否现实?

我正在研究它作为一种爱好,如果我的问题看起来很愚蠢,抱歉

您的 RAM 使用的是字地址,对吗?我说这个 b/c 元素是字大小的。

因此,我们必须使用字地址来索引它们,因此您必须对所有商店类型使用 [31:2],而不仅仅是 sw

如果 a[1] 为 even/0,则存储的一半 (sh) 将进入 RAM[a[31:2]] [15:0],并且 [31: 16] 如果 a[1] 是 odd/1。 (假设是小端——如果是大端则交换这些。)

如果 a[1:0] 为 2'b00,存储字节将进入 RAM[a[31:2]] [7:0],如果 2'b01,[23: [15:8] 16] 如果是 2'b10,如果是 [31:24] 如果是 2'b11.


或者,您可以使用字节对 RAM 进行建模,这样 sb 就很简单了,而 shsw 会更复杂:sw 会需要更新 RAM 中的 4 个不同位置,因为它们在此方案中是字节。

RAM 的主索引必须始终是字地址。我假设半字写可以是字的上半部分和下半部分,字节写可以分配给字内的任何字节。

您可以使用 +: 切片运算符(参见 Indexing vectors and arrays with +:)分配单词的一部分

always @(posedge clk) // NOTE: always_ff is SystemVerilog
  case(we)
    2'b01: RAM[a[31:2]] <= wd; // sw 
    2'b10: RAM[a[31:2]][ {a[1],4'b0000} +: 16] <= wd[15:0]; // sh
    2'b11: RAM[a[31:2]][ {a[1:0],3'b000} +: 8] <= wd[7:0]; // sb
    default:
    // do nothing
  endcase

对于SystemVerilog,还有多维打包数组的选项

//    [shw][sby][dat]    [sw  ]
logic [1:0][1:0][7:0] RAM[63:0];

assign rd = RAM[a[31:2]]; // word aligned

always_ff @(posedge clk)
  case(we)
    2'b01: RAM[a[31:2]] <= wd; // sw 
    2'b10: RAM[a[31:2]][a[1]] <= wd[15:0]; // sh
    2'b11: RAM[a[31:2]][a[1]][a[0]] <= wd[7:0]; // sb
    default:
    // do nothing
  endcase

我不知道大多数合成器处理多维压缩数组的能力如何。任何与 IEEE1364-2001 或更高版本兼容的合成器都将支持 +:,但是我已经看到混合结果比较嵌套 case 语句的结果效率。您需要进行试验。