|
| 1 | +`default_nettype none |
| 2 | +`timescale 1ns/1ns |
| 3 | + |
| 4 | +// LOAD-STORE UNIT WITH CACHE |
| 5 | +// > Handles asynchronous memory load and store operations through cache |
| 6 | +// > Each thread in each core has its own LSU with cache |
| 7 | +// > LDR, STR instructions are executed here |
| 8 | +module lsu_cached ( |
| 9 | + input wire clk, |
| 10 | + input wire reset, |
| 11 | + input wire enable, |
| 12 | + |
| 13 | + // State |
| 14 | + input [2:0] core_state, |
| 15 | + |
| 16 | + // Memory Control Signals |
| 17 | + input decoded_mem_read_enable, |
| 18 | + input decoded_mem_write_enable, |
| 19 | + |
| 20 | + // Registers |
| 21 | + input [7:0] rs, |
| 22 | + input [7:0] rt, |
| 23 | + |
| 24 | + // Data Memory (through controller) |
| 25 | + output reg mem_read_valid, |
| 26 | + output reg [7:0] mem_read_address, |
| 27 | + input mem_read_ready, |
| 28 | + input [7:0] mem_read_data, |
| 29 | + output reg mem_write_valid, |
| 30 | + output reg [7:0] mem_write_address, |
| 31 | + output reg [7:0] mem_write_data, |
| 32 | + input mem_write_ready, |
| 33 | + |
| 34 | + // LSU Outputs |
| 35 | + output reg [1:0] lsu_state, |
| 36 | + output reg [7:0] lsu_out |
| 37 | +); |
| 38 | + localparam IDLE = 2'b00, REQUESTING = 2'b01, WAITING = 2'b10, DONE = 2'b11; |
| 39 | + |
| 40 | + // Cache signals |
| 41 | + reg cache_read_request; |
| 42 | + reg cache_write_request; |
| 43 | + reg [7:0] cache_address; |
| 44 | + reg [7:0] cache_write_data; |
| 45 | + wire cache_read_ready; |
| 46 | + wire cache_write_ready; |
| 47 | + wire [7:0] cache_read_data; |
| 48 | + |
| 49 | + // Instantiate cache |
| 50 | + cache #( |
| 51 | + .CACHE_LINES(64), |
| 52 | + .ADDR_BITS(8), |
| 53 | + .DATA_BITS(8), |
| 54 | + .INDEX_BITS(6), |
| 55 | + .TAG_BITS(2) |
| 56 | + ) cache_inst ( |
| 57 | + .clk(clk), |
| 58 | + .reset(reset), |
| 59 | + .enable(enable), |
| 60 | + |
| 61 | + // LSU interface |
| 62 | + .read_request(cache_read_request), |
| 63 | + .write_request(cache_write_request), |
| 64 | + .address(cache_address), |
| 65 | + .write_data(cache_write_data), |
| 66 | + .read_ready(cache_read_ready), |
| 67 | + .write_ready(cache_write_ready), |
| 68 | + .read_data(cache_read_data), |
| 69 | + |
| 70 | + // Memory controller interface |
| 71 | + .mem_read_valid(mem_read_valid), |
| 72 | + .mem_read_address(mem_read_address), |
| 73 | + .mem_read_ready(mem_read_ready), |
| 74 | + .mem_read_data(mem_read_data), |
| 75 | + .mem_write_valid(mem_write_valid), |
| 76 | + .mem_write_address(mem_write_address), |
| 77 | + .mem_write_data(mem_write_data), |
| 78 | + .mem_write_ready(mem_write_ready) |
| 79 | + ); |
| 80 | + |
| 81 | + always @(posedge clk) begin |
| 82 | + if (reset) begin |
| 83 | + lsu_state <= IDLE; |
| 84 | + lsu_out <= 0; |
| 85 | + cache_read_request <= 0; |
| 86 | + cache_write_request <= 0; |
| 87 | + cache_address <= 0; |
| 88 | + cache_write_data <= 0; |
| 89 | + end else if (enable) begin |
| 90 | + // Handle memory read (LDR instruction) |
| 91 | + if (decoded_mem_read_enable) begin |
| 92 | + case (lsu_state) |
| 93 | + IDLE: begin |
| 94 | + if (core_state == 3'b011) begin // REQUEST state |
| 95 | + lsu_state <= REQUESTING; |
| 96 | + end |
| 97 | + end |
| 98 | + REQUESTING: begin |
| 99 | + cache_read_request <= 1; |
| 100 | + cache_address <= rs; |
| 101 | + lsu_state <= WAITING; |
| 102 | + end |
| 103 | + WAITING: begin |
| 104 | + if (cache_read_ready) begin |
| 105 | + cache_read_request <= 0; |
| 106 | + lsu_out <= cache_read_data; |
| 107 | + lsu_state <= DONE; |
| 108 | + end |
| 109 | + end |
| 110 | + DONE: begin |
| 111 | + if (core_state == 3'b110) begin // UPDATE state |
| 112 | + lsu_state <= IDLE; |
| 113 | + end |
| 114 | + end |
| 115 | + endcase |
| 116 | + end |
| 117 | + |
| 118 | + // Handle memory write (STR instruction) |
| 119 | + if (decoded_mem_write_enable) begin |
| 120 | + case (lsu_state) |
| 121 | + IDLE: begin |
| 122 | + if (core_state == 3'b011) begin // REQUEST state |
| 123 | + lsu_state <= REQUESTING; |
| 124 | + end |
| 125 | + end |
| 126 | + REQUESTING: begin |
| 127 | + cache_write_request <= 1; |
| 128 | + cache_address <= rs; |
| 129 | + cache_write_data <= rt; |
| 130 | + lsu_state <= WAITING; |
| 131 | + end |
| 132 | + WAITING: begin |
| 133 | + if (cache_write_ready) begin |
| 134 | + cache_write_request <= 0; |
| 135 | + lsu_state <= DONE; |
| 136 | + end |
| 137 | + end |
| 138 | + DONE: begin |
| 139 | + if (core_state == 3'b110) begin // UPDATE state |
| 140 | + lsu_state <= IDLE; |
| 141 | + end |
| 142 | + end |
| 143 | + endcase |
| 144 | + end |
| 145 | + end |
| 146 | + end |
| 147 | +endmodule |
0 commit comments