2021-01-15 04:37:51 +00:00
|
|
|
///////////////////////////////////////////
|
2022-08-25 00:03:22 +00:00
|
|
|
// ram_ahb.sv
|
2021-01-15 04:37:51 +00:00
|
|
|
//
|
|
|
|
// Written: David_Harris@hmc.edu 9 January 2021
|
|
|
|
// Modified:
|
|
|
|
//
|
2022-08-25 00:03:22 +00:00
|
|
|
// Purpose: On-chip RAM, external to core, with AHB interface
|
2021-01-15 04:37:51 +00:00
|
|
|
//
|
2023-01-14 14:15:35 +00:00
|
|
|
// Documentation: RISC-V System on Chip Design Chapter 6
|
|
|
|
//
|
2023-01-11 23:15:08 +00:00
|
|
|
// A component of the CORE-V-WALLY configurable RISC-V project.
|
2024-01-29 13:38:11 +00:00
|
|
|
// https://github.com/openhwgroup/cvw
|
2021-01-15 04:37:51 +00:00
|
|
|
//
|
2023-01-10 19:35:20 +00:00
|
|
|
// Copyright (C) 2021-23 Harvey Mudd College & Oklahoma State University
|
2021-01-15 04:37:51 +00:00
|
|
|
//
|
2023-01-10 19:35:20 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH SHL-2.1
|
2021-01-15 04:37:51 +00:00
|
|
|
//
|
2023-01-10 19:35:20 +00:00
|
|
|
// Licensed under the Solderpad Hardware License v 2.1 (the “License”); you may not use this file
|
|
|
|
// except in compliance with the License, or, at your option, the Apache License version 2.0. You
|
|
|
|
// may obtain a copy of the License at
|
2021-01-15 04:37:51 +00:00
|
|
|
//
|
2023-01-10 19:35:20 +00:00
|
|
|
// https://solderpad.org/licenses/SHL-2.1/
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, any work distributed under the
|
|
|
|
// License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
|
|
|
// either express or implied. See the License for the specific language governing permissions
|
|
|
|
// and limitations under the License.
|
2022-01-07 12:58:40 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2021-01-15 04:37:51 +00:00
|
|
|
|
2023-05-31 15:02:34 +00:00
|
|
|
module ram_ahb import cvw::*; #(parameter cvw_t P,
|
2023-11-13 23:20:26 +00:00
|
|
|
parameter BASE=0, RANGE = 65535, PRELOAD = 0) (
|
2023-06-15 17:10:38 +00:00
|
|
|
input logic HCLK, HRESETn,
|
|
|
|
input logic HSELRam,
|
2023-05-26 21:24:12 +00:00
|
|
|
input logic [P.PA_BITS-1:0] HADDR,
|
2023-06-15 17:10:38 +00:00
|
|
|
input logic HWRITE,
|
|
|
|
input logic HREADY,
|
|
|
|
input logic [1:0] HTRANS,
|
2023-05-26 21:24:12 +00:00
|
|
|
input logic [P.XLEN-1:0] HWDATA,
|
|
|
|
input logic [P.XLEN/8-1:0] HWSTRB,
|
|
|
|
output logic [P.XLEN-1:0] HREADRam,
|
2023-06-15 17:10:38 +00:00
|
|
|
output logic HRESPRam, HREADYRam
|
2021-01-30 04:43:48 +00:00
|
|
|
);
|
2021-01-15 04:37:51 +00:00
|
|
|
|
2023-06-15 17:10:38 +00:00
|
|
|
localparam ADDR_WIDTH = $clog2(RANGE/8);
|
|
|
|
localparam OFFSET = $clog2(P.XLEN/8);
|
2022-03-31 20:48:15 +00:00
|
|
|
|
2023-05-26 21:24:12 +00:00
|
|
|
logic [P.XLEN/8-1:0] ByteMask;
|
|
|
|
logic [P.PA_BITS-1:0] HADDRD, RamAddr;
|
2023-06-15 17:10:38 +00:00
|
|
|
logic initTrans;
|
|
|
|
logic memwrite, memwriteD, memread;
|
|
|
|
logic nextHREADYRam;
|
|
|
|
logic DelayReady;
|
2022-09-04 18:07:49 +00:00
|
|
|
|
2022-06-13 19:37:43 +00:00
|
|
|
// a new AHB transactions starts when HTRANS requests a transaction,
|
|
|
|
// the peripheral is selected, and the previous transaction is completing
|
2022-09-04 18:07:49 +00:00
|
|
|
assign initTrans = HREADY & HSELRam & HTRANS[1] ;
|
2023-06-15 17:10:38 +00:00
|
|
|
assign memwrite = initTrans & HWRITE;
|
|
|
|
assign memread = initTrans & ~HWRITE;
|
2022-06-09 23:50:43 +00:00
|
|
|
|
|
|
|
flopenr #(1) memwritereg(HCLK, ~HRESETn, HREADY, memwrite, memwriteD);
|
2023-05-26 21:24:12 +00:00
|
|
|
flopenr #(P.PA_BITS) haddrreg(HCLK, ~HRESETn, HREADY, HADDR, HADDRD);
|
2022-06-08 01:39:44 +00:00
|
|
|
|
2022-06-09 23:50:43 +00:00
|
|
|
// Stall on a read after a write because the RAM can't take both adddresses on the same cycle
|
2022-09-04 18:07:49 +00:00
|
|
|
assign nextHREADYRam = (~(memwriteD & memread)) & ~DelayReady;
|
2022-09-04 19:52:40 +00:00
|
|
|
flopr #(1) readyreg(HCLK, ~HRESETn, nextHREADYRam, HREADYRam);
|
2022-09-04 18:07:49 +00:00
|
|
|
|
2021-12-14 21:43:06 +00:00
|
|
|
assign HRESPRam = 0; // OK
|
2022-03-30 16:09:21 +00:00
|
|
|
|
2022-06-09 23:50:43 +00:00
|
|
|
// On writes or during a wait state, use address delayed by one cycle to sync RamAddr with HWDATA or hold stalled address
|
2023-05-26 21:24:12 +00:00
|
|
|
mux2 #(P.PA_BITS) adrmux(HADDR, HADDRD, memwriteD | ~HREADY, RamAddr);
|
2022-06-08 02:06:00 +00:00
|
|
|
|
|
|
|
// single-ported RAM
|
2023-11-13 23:20:26 +00:00
|
|
|
ram1p1rwbe #(P.USE_SRAM, RANGE/8, P.XLEN, PRELOAD) memory(.clk(HCLK), .ce(1'b1),
|
2022-09-21 17:20:00 +00:00
|
|
|
.addr(RamAddr[ADDR_WIDTH+OFFSET-1:OFFSET]), .we(memwriteD), .din(HWDATA), .bwe(HWSTRB), .dout(HREADRam));
|
|
|
|
|
2022-09-04 19:52:40 +00:00
|
|
|
// use this to add arbitrary latency to ram. Helps test AHB controller correctness
|
2024-01-30 14:27:18 +00:00
|
|
|
if(P.RAM_LATENCY > 0) begin
|
2022-09-06 14:21:03 +00:00
|
|
|
logic [7:0] NextCycle, Cycle;
|
|
|
|
logic CntEn, CntRst;
|
|
|
|
logic CycleFlag;
|
|
|
|
|
|
|
|
flopenr #(8) counter (HCLK, ~HRESETn | CntRst, CntEn, NextCycle, Cycle);
|
|
|
|
assign NextCycle = Cycle + 1'b1;
|
2022-09-04 18:07:49 +00:00
|
|
|
|
2022-09-06 14:21:03 +00:00
|
|
|
typedef enum logic {READY, DELAY} statetype;
|
|
|
|
statetype CurrState, NextState;
|
|
|
|
|
|
|
|
always_ff @(posedge HCLK)
|
|
|
|
if (~HRESETn) CurrState <= #1 READY;
|
2023-06-15 17:10:38 +00:00
|
|
|
else CurrState <= #1 NextState;
|
2022-09-04 18:07:49 +00:00
|
|
|
|
2022-09-06 14:21:03 +00:00
|
|
|
always_comb begin
|
2023-03-24 22:32:25 +00:00
|
|
|
case(CurrState)
|
|
|
|
READY: if(initTrans & ~CycleFlag) NextState = DELAY;
|
2023-06-15 17:10:38 +00:00
|
|
|
else NextState = READY;
|
|
|
|
DELAY: if(CycleFlag) NextState = READY;
|
|
|
|
else NextState = DELAY;
|
|
|
|
default: NextState = READY;
|
2023-03-24 22:32:25 +00:00
|
|
|
endcase
|
2022-09-06 14:21:03 +00:00
|
|
|
end
|
2022-09-04 18:07:49 +00:00
|
|
|
|
2024-01-31 19:39:59 +00:00
|
|
|
assign CycleFlag = Cycle == P.RAM_LATENCY[7:0];
|
2022-09-06 14:21:03 +00:00
|
|
|
assign CntEn = NextState == DELAY;
|
|
|
|
assign DelayReady = NextState == DELAY;
|
|
|
|
assign CntRst = NextState == READY;
|
|
|
|
end else begin
|
|
|
|
assign DelayReady = 0;
|
|
|
|
end
|
2023-03-24 22:32:25 +00:00
|
|
|
|
2021-01-15 04:37:51 +00:00
|
|
|
endmodule
|