2022-08-31 19:45:01 +00:00
///////////////////////////////////////////
// busfsm.sv
//
2023-01-18 22:52:46 +00:00
// Written: Ross Thompson ross1728@gmail.com
// Created: December 29, 2021
// Modified: 18 January 2023
2022-08-31 19:45:01 +00:00
//
2023-01-18 22:52:46 +00:00
// Purpose: Controller for cache to AHB bus interface
2022-08-31 19:45:01 +00:00
//
2023-01-18 22:52:46 +00:00
// Documentation: RISC-V System on Chip Design Chapter 9 (Figure 9.9)
//
2023-01-11 23:15:08 +00:00
// A component of the CORE-V-WALLY configurable RISC-V project.
2022-08-31 19:45:01 +00:00
//
2023-01-10 19:35:20 +00:00
// Copyright (C) 2021-23 Harvey Mudd College & Oklahoma State University
2022-08-31 19:45:01 +00:00
//
2023-01-10 19:35:20 +00:00
// SPDX-License-Identifier: Apache-2.0 WITH SHL-2.1
2022-08-31 19:45:01 +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
2022-08-31 19:45:01 +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-08-31 19:45:01 +00:00
////////////////////////////////////////////////////////////////////////////////////////////////
2023-01-18 22:58:03 +00:00
`define BURST_EN 1 // Enables burst mode. Disable to show the lost performance.
2022-08-31 19:45:01 +00:00
// HCLK and clk must be the same clock!
2023-01-18 22:52:46 +00:00
module buscachefsm # (
2023-01-27 22:27:04 +00:00
parameter BeatCountThreshold , // Largest beat index
2023-03-28 19:47:08 +00:00
parameter AHBWLOGBWPL , // Log2 of BEATSPERLINE
parameter READ_ONLY_CACHE
2023-01-18 22:52:46 +00:00
) (
2023-03-24 20:15:38 +00:00
input logic HCLK ,
input logic HRESETn ,
2023-01-15 03:19:34 +00:00
// IEU interface
2023-04-14 04:02:15 +00:00
input logic Stall , // Core pipeline is stalled
input logic Flush , // Pipeline stage flush. Prevents bus transaction from starting
input logic [ 1 : 0 ] BusRW , // Uncached memory operation read/write control: 10: read, 01: write
2023-12-29 21:07:20 +00:00
input logic BusAtomic , // Uncache atomic memory operation
2023-11-28 03:24:30 +00:00
input logic BusCMOZero , // Uncached cbo.zero must write zero to full sized cacheline without going through the cache
2023-04-14 04:02:15 +00:00
output logic BusStall , // Bus is busy with an in flight memory operation
output logic BusCommitted , // Bus is busy with an in flight memory operation and it is not safe to take an interrupt
2023-03-24 20:15:38 +00:00
// ahb cache interface locals.
2023-04-14 04:02:15 +00:00
output logic CaptureEn , // Enable updating the Fetch buffer with valid data from HRDATA
2023-03-24 20:15:38 +00:00
// cache interface
2023-04-14 04:02:15 +00:00
input logic [ 1 : 0 ] CacheBusRW , // Cache bus operation, 01: writeback, 10: fetch
output logic CacheBusAck , // Handshack to $ indicating bus transaction completed
2023-01-15 03:19:34 +00:00
// lsu interface
2023-01-18 22:47:40 +00:00
output logic [ AHBWLOGBWPL - 1 : 0 ] BeatCount , // Beat position within the cache line in the Address Phase
output logic [ AHBWLOGBWPL - 1 : 0 ] BeatCountDelayed , // Beat within the cache line in the second (Data) cache stage
2023-04-14 04:02:15 +00:00
output logic SelBusBeat , // Tells the cache to select the word from ReadData or WriteData from BeatCount rather than PAdr
2023-01-15 03:19:34 +00:00
// BUS interface
2023-04-14 04:02:15 +00:00
input logic HREADY , // AHB peripheral ready
output logic [ 1 : 0 ] HTRANS , // AHB transaction type, 00: IDLE, 10 NON_SEQ, 11 SEQ
output logic HWRITE , // AHB 0: Read operation 1: Write operation
output logic [ 2 : 0 ] HBURST // AHB burst length
2022-08-31 19:45:01 +00:00
) ;
2023-12-29 21:07:20 +00:00
typedef enum logic [ 2 : 0 ] { ADR_PHASE , DATA_PHASE , ATOMIC_PHASE , MEM3 , CACHE_FETCH , CACHE_WRITEBACK } busstatetype ;
2022-08-31 19:45:01 +00:00
typedef enum logic [ 1 : 0 ] { AHB_IDLE = 2 'b00 , AHB_BUSY = 2 'b01 , AHB_NONSEQ = 2 'b10 , AHB_SEQ = 2 'b11 } ahbtranstype ;
2023-01-21 00:47:36 +00:00
busstatetype CurrState , NextState ;
2022-08-31 19:45:01 +00:00
2023-01-18 22:47:40 +00:00
logic [ AHBWLOGBWPL - 1 : 0 ] NextBeatCount ;
2023-03-24 20:15:38 +00:00
logic FinalBeatCount ;
logic [ 2 : 0 ] LocalBurstType ;
logic BeatCntEn ;
logic BeatCntReset ;
logic CacheAccess ;
2023-11-28 03:24:30 +00:00
logic BusWrite ;
assign BusWrite = CacheBusRW [ 0 ] | BusCMOZero ;
2022-08-31 19:45:01 +00:00
always_ff @ ( posedge HCLK )
2023-04-14 04:02:15 +00:00
if ( ~ HRESETn | Flush ) CurrState < = # 1 ADR_PHASE ;
else CurrState < = # 1 NextState ;
2022-08-31 19:45:01 +00:00
always_comb begin
2023-03-24 20:01:38 +00:00
case ( CurrState )
ADR_PHASE: if ( HREADY & | BusRW ) NextState = DATA_PHASE ;
2023-12-29 21:07:20 +00:00
else if ( HREADY & BusWrite ) NextState = CACHE_WRITEBACK ;
2023-03-24 20:01:38 +00:00
else if ( HREADY & CacheBusRW [ 1 ] ) NextState = CACHE_FETCH ;
else NextState = ADR_PHASE ;
2023-12-29 21:07:20 +00:00
DATA_PHASE: if ( HREADY & BusAtomic ) NextState = ATOMIC_PHASE ;
else if ( HREADY & ~ BusAtomic ) NextState = MEM3 ;
else NextState = DATA_PHASE ;
ATOMIC_PHASE: if ( HREADY ) NextState = MEM3 ;
else NextState = ATOMIC_PHASE ;
MEM3: if ( Stall ) NextState = MEM3 ;
else NextState = ADR_PHASE ;
CACHE_FETCH: if ( HREADY & FinalBeatCount & CacheBusRW [ 0 ] ) NextState = CACHE_WRITEBACK ;
else if ( HREADY & FinalBeatCount & CacheBusRW [ 1 ] ) NextState = CACHE_FETCH ;
else if ( HREADY & FinalBeatCount & ~ | CacheBusRW ) NextState = ADR_PHASE ;
else NextState = CACHE_FETCH ;
CACHE_WRITEBACK: if ( HREADY & FinalBeatCount & CacheBusRW [ 0 ] ) NextState = CACHE_WRITEBACK ;
else if ( HREADY & FinalBeatCount & CacheBusRW [ 1 ] ) NextState = CACHE_FETCH ;
else if ( HREADY & FinalBeatCount & BusCMOZero ) NextState = MEM3 ;
else if ( HREADY & FinalBeatCount & ~ | CacheBusRW ) NextState = ADR_PHASE ;
else NextState = CACHE_WRITEBACK ;
2023-03-24 20:01:38 +00:00
default : NextState = ADR_PHASE ;
endcase
2022-08-31 19:45:01 +00:00
end
// IEU, LSU, and IFU controls
// Used to store data from data phase of AHB.
2023-01-18 22:47:40 +00:00
flopenr # ( AHBWLOGBWPL ) BeatCountReg ( HCLK , ~ HRESETn | BeatCntReset , BeatCntEn , NextBeatCount , BeatCount ) ;
flopenr # ( AHBWLOGBWPL ) BeatCountDelayedReg ( HCLK , ~ HRESETn | BeatCntReset , BeatCntEn , BeatCount , BeatCountDelayed ) ;
2022-11-09 23:52:50 +00:00
assign NextBeatCount = BeatCount + 1 'b1 ;
2023-01-18 22:47:40 +00:00
assign FinalBeatCount = BeatCountDelayed = = BeatCountThreshold [ AHBWLOGBWPL - 1 : 0 ] ;
2023-07-21 21:31:26 +00:00
assign BeatCntEn = ( ( ( NextState = = CACHE_WRITEBACK | NextState = = CACHE_FETCH ) & HREADY & ~ Flush ) |
( NextState = = ADR_PHASE & | CacheBusRW & HREADY ) ) & ~ Flush ;
2022-11-09 23:52:50 +00:00
assign BeatCntReset = NextState = = ADR_PHASE ;
2022-09-02 22:17:40 +00:00
2023-07-21 21:31:26 +00:00
assign CaptureEn = ( CurrState = = DATA_PHASE & BusRW [ 1 ] & ~ Flush ) | ( CurrState = = CACHE_FETCH & HREADY ) ;
2022-11-09 23:43:06 +00:00
assign CacheAccess = CurrState = = CACHE_FETCH | CurrState = = CACHE_WRITEBACK ;
2022-09-02 22:17:40 +00:00
2023-12-29 17:03:38 +00:00
assign BusStall = ( CurrState = = ADR_PHASE & ( ( | BusRW ) | ( | CacheBusRW ) | BusCMOZero ) ) |
2023-03-24 20:01:38 +00:00
//(CurrState == DATA_PHASE & ~BusRW[0]) | // *** replace the next line with this. Fails uart test but i think it's a test problem not a hardware problem.
( CurrState = = DATA_PHASE ) |
2023-12-29 21:07:20 +00:00
( CurrState = = ATOMIC_PHASE ) |
( CurrState = = CACHE_FETCH & ~ FinalBeatCount ) |
( CurrState = = CACHE_WRITEBACK & ~ FinalBeatCount ) ;
2023-12-29 17:03:38 +00:00
2023-03-28 19:47:08 +00:00
assign BusCommitted = ( CurrState ! = ADR_PHASE ) & ~ ( READ_ONLY_CACHE & CurrState = = MEM3 ) ;
2022-08-31 19:45:01 +00:00
// AHB bus interface
2023-12-29 17:03:38 +00:00
assign HTRANS = ( CurrState = = ADR_PHASE & HREADY & ( ( | BusRW ) | ( | CacheBusRW ) | BusCMOZero ) & ~ Flush ) |
2023-12-29 21:07:20 +00:00
( CurrState = = DATA_PHASE & BusAtomic ) |
2023-07-21 21:31:26 +00:00
( CacheAccess & FinalBeatCount & | CacheBusRW & HREADY & ~ Flush ) ? AHB_NONSEQ : // if we have a pipelined request
2022-11-09 23:52:50 +00:00
( CacheAccess & | BeatCount ) ? ( `BURST_EN ? AHB_SEQ : AHB_NONSEQ ) : AHB_IDLE ;
2022-09-06 14:21:21 +00:00
2023-12-29 21:07:20 +00:00
assign HWRITE = ( ( BusRW [ 0 ] & ~ BusAtomic ) | BusWrite & ~ Flush ) | ( CurrState = = DATA_PHASE & BusAtomic ) |
( CurrState = = CACHE_WRITEBACK & | BeatCount ) ;
2023-07-21 21:31:26 +00:00
assign HBURST = `BURST_EN & ( ( | CacheBusRW & ~ Flush ) | ( CacheAccess & | BeatCount ) ) ? LocalBurstType : 3 'b0 ;
2022-08-31 19:45:01 +00:00
always_comb begin
2022-11-09 23:52:50 +00:00
case ( BeatCountThreshold )
2022-08-31 19:45:01 +00:00
0 : LocalBurstType = 3 'b000 ;
3 : LocalBurstType = 3 'b011 ; // INCR4
7 : LocalBurstType = 3 'b101 ; // INCR8
15 : LocalBurstType = 3 'b111 ; // INCR16
default : LocalBurstType = 3 'b001 ; // INCR without end.
endcase
end
// communication to cache
2023-11-28 03:24:30 +00:00
assign CacheBusAck = ( CacheAccess & HREADY & FinalBeatCount & ~ BusCMOZero ) ;
assign SelBusBeat = ( CurrState = = ADR_PHASE & ( BusRW [ 0 ] | BusWrite ) ) |
2023-03-24 20:15:38 +00:00
( CurrState = = DATA_PHASE & BusRW [ 0 ] ) |
2023-12-29 21:07:20 +00:00
( CurrState = = ATOMIC_PHASE & BusRW [ 0 ] ) |
2022-11-09 23:43:06 +00:00
( CurrState = = CACHE_WRITEBACK ) |
2022-09-13 21:19:10 +00:00
( CurrState = = CACHE_FETCH ) ;
2022-08-31 19:45:01 +00:00
endmodule